Skip to content

Upstreams and proxies as resources - #29

Merged
fylorn merged 2 commits into
mainfrom
feat/structured-config-edits
Sep 17, 2026
Merged

fylorn merged 2 commits into
mainfrom
feat/structured-config-edits

Conversation

@fylorn

@fylorn fylorn commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Why

The desktop UI built an upstream as a YAML fragment by string concatenation and sent it through the generic patch endpoint. Every failure mode of that landed on the user's only config file:

  • Unquoted values. A proxy password or API key containing # or : read back as a comment or a mapping.
  • Renames. Routes and groups still pointed at the old name, so validation rejected the save without saying why.
  • Deletes. Deleting an upstream a rule still used was rejected the same way.

The next UI edits upstreams and proxies in dialogs, so these writes need to be structural.

What

Resource endpoints (tw-control/src/resources.rs)

  • POST /providers, PUT /providers/{name}, DELETE /providers/{name}, and the same three for /proxies. They take a structure, and serde does the quoting.
  • On update, leaving out the key or base_url keeps the stored value, because the overview only shows them masked. Proxy auth is keep / none / set for the same reason.
  • A rename moves every reference in the same write, so it is one version. That covers rule targets, provider_would_be, group members and a select group's choice, and for proxies the upstreams using them.
  • Deleting something that is still referenced returns 409 and names what references it.
  • POST /providers/test probes an unsaved definition with its own protocol and proxy, through the client builder forwarding uses. An unsaved OAuth credential is never refreshed just to test it.
  • POST /proxies/test completes the proxy handshake, credentials included. The old TCP-only check reported a wrong password as reachable.

Edit primitives

  • tw-yaml gains three functions:

    • put replaces or creates the value at a path, creating missing parent maps.
    • remove_key removes a key, and its parent map too if that leaves it empty.
    • replace_item replaces a list item in place.

    Each one re-parses and checks two things: the target reads back as written, and every node outside it is unchanged. insert and clear_seq now go through put rather than their own key splicing.

  • tw_config::edit upserts items by name:

    • Only fields that changed are written, so comments beside the other fields survive.
    • A flow-style item is rewritten as a block in the same position.
    • The result is checked against the expected semantic tree before it is returned.
  • tw_config::refs finds references and renames them.

  • Two position bugs in the editor are fixed:

    • A new key used to take its indentation from the first line of the parent's block. A block-style value starts one level deeper on the next line, so when the parent's first key held a list or a mapping, the new key landed inside it and the self-check refused the write. The column now comes from the sibling keys.
    • The parser marks a blank value (key:) on the colon rather than after it. Nodes now carry the position after the colon, which removes the per-caller workaround and stops key: with a trailing space from being written as key:x.

Removed, since nothing uses them any more:

  • /setup and generate_with_provider.
  • /probe.
  • The proxy and base_url modes of /l1.

Changed views

  • ProviderView reports slugs instead of labels, plus the credential kind, whether the protocol is explicit, the manual model list and references.
  • ProxyView.used_by lists names. The username stays out of the overview, as before.

Tests

  • Property tests run put and remove_key against about 2,800 generated documents each, alongside the existing set property test, and compare the semantic tree before and after. A fourth adds a new key to a random mapping of each document. The generator now also writes blank values.
  • tw-control/tests/resources.rs has 12 tests against the real config file:
    • A key with # and : round-trips.
    • A comment beside an untouched field survives.
    • A rename moves references in one version.
    • Deleting while referenced is a 409 that names the referrer.
    • A proxy password is kept on edit and never appears in the overview.
    • The test endpoints run against a fake upstream and a fake CONNECT proxy that checks credentials.
  • Unit tests cover the edit layer, references, and the proxy handshake, including auth.
  • fmt, clippy -D warnings and cargo test --workspace pass.
  • scripts/smoke.sh: 48 passed, 0 failed, 1 over target and not a regression (memory 35 MB against a 30 MB target; the script only counts 40 MB and above as a regression).

🤖 Generated with Claude Code

The UI used to turn an upstream into a YAML fragment by string
concatenation and hand it to the generic patch endpoint. That had three
failure modes, and each wrote or rejected the user's only config file:

- A value with `#` or `: ` in it (a proxy password, an API key) was read
  back as a comment or a mapping.
- Renaming an upstream left routes and groups pointing at the old name,
  so the save was rejected by validation with no hint why.
- Deleting an upstream that a rule still used was rejected the same way.

The control plane now owns these writes:

- `POST /providers`, `PUT /providers/{name}`, `DELETE /providers/{name}`,
  and the same three for `/proxies`. The body is a structure; serde does
  the quoting. Omitting the key or base_url on update keeps the stored
  one, since the overview only ever shows them masked. Proxy auth is
  keep / none / set for the same reason.
- A rename moves every reference (rule targets, provider_would_be,
  group members, a select group's choice; providers using a proxy) in
  the same write, so it is one version. A delete that is still
  referenced is a 409 naming who references it.
- `POST /providers/test` probes an unsaved definition with its own
  protocol and proxy, through the same client builder forwarding uses.
  An unsaved OAuth credential is not refreshed just to test it: the
  server may revoke the refresh token on use.
- `POST /proxies/test` completes the proxy handshake, credentials
  included. Measuring only the TCP connect reported a wrong password as
  reachable.

Underneath, `tw-yaml` gains `put` (replace or create a value at a path,
creating missing parent maps), `remove_key` (removing an emptied parent
map too), and `replace_item`. Each re-parses and checks that the target
reads back as written and that every node outside it is unchanged, and
property tests run them against thousands of generated documents.
`tw_config::edit` builds item upserts on top: only changed fields are
written, so comments beside untouched fields survive, and a flow-style
item is rewritten as a block in place. `insert` and `clear_seq` now use
`put` instead of their own key-splicing code.

Removed without replacement, since nothing uses them any more: `/setup`
(and `generate_with_provider`), `/probe`, and the proxy and base_url
modes of `/l1`. `ProviderView` reports slugs instead of labels, plus the
credential kind, whether the protocol is explicit, the manual model list
and references; `ProxyView.used_by` lists names.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Thanks for the PR. One thing before review: this targets main, and it should target dev.

main is a release-only line — branch-protected, linear history, and every commit on it is a tagged release snapshot. Routine work lands on dev and reaches main with the next release. GitHub pre-filled main here because it is the default branch, so this is our defaults being unhelpful rather than anything you did wrong.

No need to close and reopen: click Edit next to the PR title and change the base branch to dev. The commits and the discussion carry over.

See CONTRIBUTING.md and CONTRIBUTING.md for the branch contract.

… after its colon

Two position bugs in the structural editor, both caught while writing
`pricing.auto_update` next to an existing `pricing.sheets` list:

- A new key took its indentation from the first line of the parent's
  block. A child's byte range is its value's range, and a block-style
  value starts on the next line, one level deeper. When the parent's
  first key held a list or a mapping, the new key landed inside that
  list item, and the self-check refused the write. The column now comes
  from the direct child keys themselves.
- The parser marks a blank value (`key:`) on the colon, not after it.
  Walking back from there finds the previous key's colon. Nodes now
  carry the position after the colon, so every consumer sees where the
  value actually goes, and the per-caller workaround is gone. Writing
  into `key: ` with a trailing space also stops producing `key:x`.

A new property test adds a key to a random mapping of a random document,
and the document generator now writes blank values.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@fylorn
fylorn merged commit 1e93c9f into main Sep 17, 2026
1 check passed
@fylorn
fylorn deleted the feat/structured-config-edits branch September 17, 2026 04:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant