From 3e0f99a19cf6ebe5b8ebafa0c91bfb081d830ef7 Mon Sep 17 00:00:00 2001 From: JUN Date: Tue, 1 Sep 2026 12:21:26 +0900 Subject: [PATCH 001/172] chore(release): move dev to 2.40.0 after the v2.39.0 release (#3127) --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9da46cb61c..e3bc6b777a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bitkyc08/opencodex", - "version": "2.39.0", + "version": "2.40.0", "description": "Universal provider proxy for OpenAI Codex & Claude Code — use any LLM with Codex CLI/App/SDK and Claude Code", "type": "module", "main": "./bin/package-main.mjs", From 9c8bbbf66b6b547cb527205d199d56f50f3d2951 Mon Sep 17 00:00:00 2001 From: JUN Date: Tue, 1 Sep 2026 12:21:29 +0900 Subject: [PATCH 002/172] docs(devlog): record the v2.39.0 release train (#3126) * docs(devlog): plan the v2.39.0 release train * docs(devlog): record the five-lane v2.39.0 regression audit * docs(devlog): close the v2.39.0 release train with the shipped outcome * docs(devlog): name the server-auth flake follow-up * docs(devlog): record the executed preview promotion * docs(devlog): record the executed main promotion * docs(devlog): record the executed publish --- .../260901_release_train_2390/000_plan.md | 95 +++++++++++++++++++ .../010_wp1_regression_audit.md | 51 ++++++++++ .../020_wp2_preview_promotion.md | 63 ++++++++++++ .../030_wp3_main_promotion.md | 53 +++++++++++ .../040_wp4_publish.md | 70 ++++++++++++++ .../050_audit_verdicts.md | 89 +++++++++++++++++ .../260901_release_train_2390/070_outcome.md | 80 ++++++++++++++++ 7 files changed, 501 insertions(+) create mode 100644 devlog/_plan/260901_release_train_2390/000_plan.md create mode 100644 devlog/_plan/260901_release_train_2390/010_wp1_regression_audit.md create mode 100644 devlog/_plan/260901_release_train_2390/020_wp2_preview_promotion.md create mode 100644 devlog/_plan/260901_release_train_2390/030_wp3_main_promotion.md create mode 100644 devlog/_plan/260901_release_train_2390/040_wp4_publish.md create mode 100644 devlog/_plan/260901_release_train_2390/050_audit_verdicts.md create mode 100644 devlog/_plan/260901_release_train_2390/070_outcome.md diff --git a/devlog/_plan/260901_release_train_2390/000_plan.md b/devlog/_plan/260901_release_train_2390/000_plan.md new file mode 100644 index 0000000000..f78a23d9f8 --- /dev/null +++ b/devlog/_plan/260901_release_train_2390/000_plan.md @@ -0,0 +1,95 @@ +# 260901 — v2.39.0 release train: audit, promote, publish + +Snapshot taken 2026-09-01T01:35Z. This unit carries `dev` to `preview` and `main` and +publishes 2.39.0 to npm. It is a delivery unit, not a bug-fix unit: no product code is +written here unless the regression audit produces a blocker. + +## Measured state + +| Ref | SHA | `package.json` | +|-----|-----|-----------------| +| `origin/dev` | `9af3a7bebb5eb6e9bb9aab51274586897eaaba03` | `2.39.0` | +| `origin/main` | `ebb4d552e` | `2.38.0` | +| `origin/preview` | `93704b4f8` | `2.38.0-preview.20260831` | + +Promotion delta `origin/main...origin/dev`: 43 commits, 252 files, +18668/-513. +Neither `main` nor `preview` is an ancestor of `dev` — both carry their own release +commits, which is the normal shape here. Every promotion in this repository is a merge +of `dev` into a promotion branch, then a PR into the target. + +### Gate evidence at the `dev` head + +Cross-platform CI run `33457563882` on `9af3a7beb`: **success**. Every job passed — +four test shards, `macos`, `gates`, `storage policy`, `api usage`, keyring on all three +OSes, `npm-global` on all three OSes. The Windows shard matrix is `skipped`, which is its +normal push-event state; `platform-windows` is `workflow_dispatch`-only. + +No Service lifecycle run exists for `9af3a7beb` — that workflow's push trigger is +path-filtered and the head commit touched none of its paths. + +## The preview channel is two cycles stale, and that is not an accident + +npm currently advertises `latest=2.38.0` and `preview=2.36.0-preview.20260830`. + +The cause is recorded in CI, not in npm. `origin/preview` tip `93704b4f8` carries +`2.38.0-preview.20260831`, but its push-event Cross-platform CI run `33386559501` +**failed**: jobs `macos` and `test 1/4` failed while every other job passed. +`release.yml` requires a *successful* `push`-event `ci.yml` run for the exact SHA on +the release branch, and deliberately refuses a green pull-request run for the same SHA. +So the v2.38.0 preview publish was never dispatchable. The stable publish was unaffected +because `main`'s own promotion run `33385192526` passed. + +PR #3073's description already documents an intermittent macOS failure in +`tests/shutdown-launcher.test.ts` that does not reproduce on Linux. Lane E confirms +whether run `33386559501` is that same flake or a real defect before we treat the +preview promotion as routine. + +## What the release workflow actually demands + +From `.github/workflows/release.yml`, a dispatch must satisfy all of: + +1. `expected-sha` — required, full 40 characters, and it must still be the branch tip. + The guard checks out `.github/scripts/release-dispatch-guard.cjs` from the default + branch, so the validation code is `main`'s, not the dispatched ref's. +2. Branch/version/dist-tag coupling. From `main`: stable semver only, dist-tag `latest`. + From `preview`: the version must contain `-preview.`, dist-tag `preview`. Any other + ref is refused outright. +3. A successful `ci.yml` run for the exact SHA, on that branch, from a `push` event. +4. The service gate, when armed. It diffs the previous *merged* release tag against + `HEAD` and, if any of `src/service.ts`, `src/cli.ts`, `src/cli/index.ts`, + `src/lib/bun-runtime.ts`, `package.json`, `bun.lock`, + `.github/workflows/service-lifecycle.yml` changed, demands a successful + Service lifecycle run for the same SHA. + +**The service gate will be armed for this release.** Measured against the delta: +`package.json`, `src/cli/index.ts` and `src/service.ts` are all present. Both promotion +commits therefore need a green Service lifecycle run of their own, and the push trigger +will supply it because those same paths are in the merge. + +Publication is tokenless via Trusted Publishing (OIDC); there is no `NPM_TOKEN` to check. + +## Work phases + +One phase, one full PABCD cycle. + +- **wp0** — this roadmap. Docs only. +- **wp1** — regression audit of the promotion delta, five parallel `gpt-5.6-sol`/high + lanes, plus a gate determination. → `010` +- **wp2** — promote `dev` onto `preview`, publish nothing yet. → `020` +- **wp3** — promote `dev` onto `main`. → `030` +- **wp4** — dispatch `release.yml` twice and prove the publish. → `040` + +## Success criteria + +- c-1 — every audit lane returns a verdict with file/line citations, and no blocker survives. +- c-2 — the gates `release.yml` requires are green on each promotion SHA. +- c-3 — `origin/preview` tip is the preview promotion SHA and its push CI is green. +- c-4 — `origin/main` tip is the stable promotion SHA and its push CI is green. +- c-5 — npm shows `latest=2.39.0` and `preview=2.39.0-preview.20260901`, each + `gitHead` matching its promotion SHA. + +## Out of scope + +Merging any of the 20 open feature/fix PRs. Rewriting `dev` history. Touching product +code absent a confirmed blocker. Running the full local suite — hosted exact-SHA CI is +the primary evidence surface for this unit. diff --git a/devlog/_plan/260901_release_train_2390/010_wp1_regression_audit.md b/devlog/_plan/260901_release_train_2390/010_wp1_regression_audit.md new file mode 100644 index 0000000000..cc3f4d8418 --- /dev/null +++ b/devlog/_plan/260901_release_train_2390/010_wp1_regression_audit.md @@ -0,0 +1,51 @@ +# wp1 — Regression audit of the promotion delta + +## Question this phase answers + +Does `origin/main...origin/dev` contain anything a 2.38.0 user would experience as +breakage? Green CI is necessary and not sufficient: the suite proves the tests that exist +still pass, not that a behavior change is safe. + +## Method + +Five read-only `gpt-5.6-sol` lanes at `high` effort, dispatched in parallel with disjoint +file scopes so no two lanes audit the same diff. + +| Lane | Scope | Focus commits | +|------|-------|---------------| +| A | `src/responses/`, `src/server/responses/`, `src/router.ts`, `src/routing/`, `src/adapters/`, `src/vision/` | `9af3a7beb` modalities image input, `e9d198a3c` private-metadata strip, `5c0c13194` unreadable MESSAGE reply, `a0d386b49` web_search_call query, `5f0b39048` spill byte cap, `42ad9c44d` burst window, `b46164e78` dated-variant fold, `a3656a92c` cursor eof retry | +| B | `src/oauth/`, `src/codex/`, `src/config/` | the eight-commit Anthropic refresh-intent stack, `a73a4c998` WHAM-401 refresh-before-quarantine, `6123be31f` session_meta by thread id | +| C | `src/cli/`, `src/service.ts`, `src/update/`, `src/lib/` | `0ef04e640` start-shadowing, `330470e74` typed stop outcome, `91b2c4e19` terminal conflict resolve, `71bd7bec6` version bump | +| D | `gui/`, `docs-site/` | `0db8066c0` logs filter, `b6e53d8eb` restore focus, the brand-mark series, `2a90cdaa9` conflicted-config overwrite | +| E | `.github/workflows/` + npm/CI forensics | the stale preview tag and the exact dispatch shape | + +Lane A owns the riskiest surface. The dated-variant fold now folds in both directions and +at both widths — a mis-fold there collides two model ids and routes a request to the wrong +model, which no test would necessarily catch. The spill byte cap introduces eviction into +a directory an in-flight response reads from; eviction that outruns a reader loses response +bytes. The burst-window change converts an `unknown` into an `exhausted`, and a false +`exhausted` parks a healthy provider. + +Lane B owns the highest-consequence surface. Eight commits reshape when the Anthropic +refresh-intent marker is written, preserved, and cleared. The failure mode that matters is +not a crash: it is a valid credential deleted or masked, so the user is silently logged out +and must re-auth. `a41b7995c` (adopt newer disk credentials before cleanup) and +`e476acd43` (keep post-commit cleanup from masking a durable credential) are the two +commits whose interaction decides this. + +Lane C also produces a mechanical determination the release depends on: whether the +Service lifecycle gate is armed. Answer already measured — it is. + +## Acceptance + +Every lane returns `VERDICT: PASS` or `VERDICT: FAIL` with per-finding severity and +file:line citations. Blocker findings are independently verified against the source +before they change the plan; a lane's assertion is a hypothesis until the main session +reads the same lines. A confirmed blocker becomes a new work phase ahead of wp2 and the +promotion waits. + +## What would make this phase fail honestly + +A lane that reports `PASS` with no evidence of what it read is not a pass. A lane that +times out is a failed dispatch, not a silent approval, and gets re-spawned once with the +failure folded into the packet. diff --git a/devlog/_plan/260901_release_train_2390/020_wp2_preview_promotion.md b/devlog/_plan/260901_release_train_2390/020_wp2_preview_promotion.md new file mode 100644 index 0000000000..418f871076 --- /dev/null +++ b/devlog/_plan/260901_release_train_2390/020_wp2_preview_promotion.md @@ -0,0 +1,63 @@ +# wp2 — Promote `dev` onto `preview` + +## Preconditions + +wp1 closed with no surviving blocker. `origin/dev` still at `9af3a7beb`; if it moved, +re-measure before branching — the promotion is of a specific tree, not of a branch name. + +## Version + +`preview` must carry a `-preview.` version or `release.yml` refuses the dispatch. +Prior names are date-suffixed: `v2.38.0-preview.20260831`, `v2.36.0-preview.20260830`, +`v2.36.0-preview.20260829`, `v2.34.0-preview.20260827`. Today's is +**`2.39.0-preview.20260901`**. + +This means the promotion branch is not a byte-identical copy of `dev`: `package.json` +carries `2.39.0` on `dev` and must read `2.39.0-preview.20260901` on `preview`. That +one-line difference is the only intended divergence. + +## Steps + +1. `git fetch origin`, branch `codex/promote-preview-23900901` from `origin/preview`. +2. Merge `origin/dev` into it. Expect exactly one conflict — `package.json` version — + resolved to `2.39.0-preview.20260901`. Any other conflict is unexpected and stops + this phase for inspection. +3. Verify the tree matches `dev` except for that version line: + `git diff origin/dev HEAD -- . ':!package.json'` must be empty. +4. Push, open the PR against `preview`. +5. `enforce-target` will fail and convert the PR to draft — `ALLOWED_BASES` is + `["dev"]`. This is expected for every promotion PR and was handled identically for + #3001, #3037, #3072, #3073. `gh pr ready`, then admin merge. +6. Wait for the **push-event** Cross-platform CI run on the resulting merge commit, and + for Service lifecycle, which will be triggered because `package.json`, + `src/cli/index.ts` and `src/service.ts` are in the merge. + +## The failure this phase exists to not repeat + +v2.38.0's preview promotion merged and then its CI failed, so the publish was never +dispatchable and the preview dist-tag silently stayed two cycles behind. A merged +promotion branch is not a releasable one. This phase is complete only when the promotion +SHA has a green push-event `ci.yml` run, not when the PR is merged. + +If the macOS/shard failure recurs on the new promotion commit, rerun the failed jobs once +(`gh run rerun --failed`). A second failure at the same assertion is a real defect +and escalates to a new work phase rather than being re-run until green. + +## Evidence to capture + +- promotion merge SHA and `git ls-remote origin refs/heads/preview` +- `gh api actions/runs?head_sha=` showing `Cross-platform CI: success` (push) and + `Service lifecycle: success` +- the `package.json`-only diff proof from step 3 + +## Executed + +PR #3123, merged at `75f3895c14965205be694e8ebb8e93f472630539`. The merge produced +exactly the one predicted conflict (`package.json`), resolved to +`2.39.0-preview.20260901`; `git diff origin/dev HEAD -- . ':!package.json'` was empty. +`bun test tests/release-version-line.test.ts` passed 3/3 on the branch before push — +the same file that refused v2.38.0's preview. + +Push-event Cross-platform CI: run `33462203719`, success on rerun. Service lifecycle: +success. The first attempt failed on `tests/server-auth.test.ts:2288`, analyzed in +`070_outcome.md` and not a regression in this delta. diff --git a/devlog/_plan/260901_release_train_2390/030_wp3_main_promotion.md b/devlog/_plan/260901_release_train_2390/030_wp3_main_promotion.md new file mode 100644 index 0000000000..0d73d5330b --- /dev/null +++ b/devlog/_plan/260901_release_train_2390/030_wp3_main_promotion.md @@ -0,0 +1,53 @@ +# wp3 — Promote `dev` onto `main` + +## Preconditions + +wp2 closed: `origin/preview` is at the preview promotion SHA with green push CI and +green Service lifecycle. wp1 produced no surviving blocker. + +`preview` being green is not a precondition `release.yml` enforces for the stable +publish — the two channels gate independently, and v2.38.0 shipped stable while its +preview was red. We sequence preview first anyway: it is the cheaper place to discover +that a promotion merge breaks something. + +## Version + +`main` requires stable semver and dist-tag `latest`. `dev` already carries `2.39.0`, +so the merge should be clean with no version conflict — the same shape as v2.38.0, whose +promotion PR recorded "the tree is byte-identical to `dev`". + +## Steps + +1. Branch `codex/promote-main-2390` from `origin/main`. +2. Merge `origin/dev`. Expect no conflict. Verify `git diff origin/dev HEAD` is empty — + the promoted tree should be byte-identical to `dev`. +3. Push and open the PR against `main`, with a description that names what ships: the + 43-commit delta, the bug fixes, and any residual the audit recorded rather than hid. +4. `enforce-target` fails and drafts the PR, as on every promotion. `gh pr ready`, then + admin merge. +5. Wait for push-event Cross-platform CI and Service lifecycle on the merge commit. + +## Evidence to capture + +- merge SHA, `git ls-remote origin refs/heads/main` +- `gh api actions/runs?head_sha=` with both workflows `success` +- the empty-diff proof from step 2 + +## Stop conditions + +A failing job on the promotion commit that is not the documented macOS launcher flake +stops the train here. `main` is the release branch; a red `main` is worse than a late +release, and `release.yml` would refuse the dispatch regardless. + +## Executed + +PR #3125, merged at `af6113a0381d6fff2e4dce587652825c7eeb6423`. The merge was clean +with no version conflict and `git diff origin/dev HEAD` was empty — the promoted tree +is byte-identical to `dev`, as predicted. + +Push-event Cross-platform CI: run `33463473330`, success on rerun. Service lifecycle: +success. The first attempt failed on the Linux `test 3/4` shard, same +`tests/server-auth.test.ts:2288` assertion the preview run hit on macOS — which is how +we learned the flake is cross-platform rather than macOS-specific. The PR run for the +identical tree had already passed `macos` and every shard, which is the contrast that +made the flake diagnosis defensible rather than convenient. diff --git a/devlog/_plan/260901_release_train_2390/040_wp4_publish.md b/devlog/_plan/260901_release_train_2390/040_wp4_publish.md new file mode 100644 index 0000000000..61a7146f2a --- /dev/null +++ b/devlog/_plan/260901_release_train_2390/040_wp4_publish.md @@ -0,0 +1,70 @@ +# wp4 — Dispatch `release.yml` and prove the publish + +## Order + +Preview first, then stable. `release.yml` declares `concurrency: group: release` with +`cancel-in-progress: false`, so a second dispatch queues behind the first rather than +cancelling it — but serializing them by hand keeps the evidence unambiguous about which +run published what. + +## Dispatches + +```sh +gh workflow run release.yml --ref preview \ + -f version=2.39.0-preview.20260901 \ + -f tag=preview \ + -f dry-run=false \ + -f expected-sha= + +gh workflow run release.yml --ref main \ + -f version=2.39.0 \ + -f tag=latest \ + -f dry-run=false \ + -f expected-sha=
+``` + +`dry-run` defaults to `true`; it must be passed explicitly as `false` or the workflow +builds and packs without publishing. `expected-sha` is required and must be the current +branch tip — if anything lands on the branch between promotion and dispatch, the guard +fails the run rather than publishing a different tree than the one audited. That is the +intended behavior, not an obstacle to work around. + +## Proof of publish + +Merged source is not a deployed package. Required evidence: + +```sh +npm view @bitkyc08/opencodex dist-tags --json # latest=2.39.0, preview=2.39.0-preview.20260901 +npm view @bitkyc08/opencodex@2.39.0 gitHead # == main promotion SHA +npm view @bitkyc08/opencodex@2.39.0-preview.20260901 gitHead +gh release list --limit 5 # v2.39.0 tagged +gh run view --json conclusion +``` + +A `gitHead` that does not match the promotion SHA means something other than the audited +tree was published, and is a stop-everything condition. + +## Known non-blocker + +`preview=2.36.0-preview.20260830` on npm today is two cycles stale because v2.38.0's +preview CI failed (unit `000`). Publishing `2.39.0-preview.20260901` moves the tag +forward and closes that gap; 2.38.0-preview is skipped rather than backfilled, which +matches how the previous-tag baseline in `release.yml` already computes its range. + +## Executed + +Preview dispatch: run `33464064409`, success, `expected-sha=75f3895c1…`. +Stable dispatch: run `33464579658`, success, `expected-sha=af6113a03…`. + +``` +npm view @bitkyc08/opencodex dist-tags --json +{ "latest": "2.39.0", "preview": "2.39.0-preview.20260901" } +``` + +`gitHead` for `2.39.0` is `af6113a0381d6fff2e4dce587652825c7eeb6423`; for +`2.39.0-preview.20260901` it is `75f3895c14965205be694e8ebb8e93f472630539`. Both match +their promotion SHAs exactly, which is the check that distinguishes a published package +from a merged branch. GitHub releases `v2.39.0` and `v2.39.0-preview.20260901` exist. + +The stale preview channel is closed: it moved from `2.36.0-preview.20260830` to +`2.39.0-preview.20260901` in one step. diff --git a/devlog/_plan/260901_release_train_2390/050_audit_verdicts.md b/devlog/_plan/260901_release_train_2390/050_audit_verdicts.md new file mode 100644 index 0000000000..88f5d1f39a --- /dev/null +++ b/devlog/_plan/260901_release_train_2390/050_audit_verdicts.md @@ -0,0 +1,89 @@ +# Audit verdicts — five parallel lanes, `gpt-5.6-sol` at `high` + +Dispatched read-only against `origin/main...origin/dev` (`ebb4d552e` → `9af3a7beb`). +Every lane returned `VERDICT: PASS`. No release blocker in any scope. + +## Lane A — core request and routing path + +PASS. Covered the dated-model fold and its direction guard +(`src/codex/catalog/provider-fetch.ts:943`, `:1004`, `:1737`), spill admission / +reservation / eviction and synchronous replay materialization +(`src/responses/state.ts:419`, `:1741`, `:1951`), burst-window freshness and account +selection (`src/codex/routing.ts:363`, `:408`, `:1202`), metadata stripping and +web-search repair (`src/adapters/openai-responses.ts:246`, `:950`, `:2165`), encrypted +MESSAGE detection (`src/server/responses/encrypted-payload.ts:195`), vision sidecar +parity (`src/vision/eligibility.ts:79`), cursor pre-header EOF retry +(`src/adapters/cursor/live-models.ts:65`, `:265`). + +The three hypotheses this lane was sent to disprove all held: the fold guard does not +collide ids, eviction does not outrun an in-flight reader, and the burst-window change +does not park a healthy provider. Nothing changed under `src/router.ts` or `src/routing/`. + +## Lane B — auth, credentials, security boundary + +PASS. Covered the Anthropic refresh-intent lifecycle, CAS cleanup, transient and +uncertain failures, disk-credential adoption and cross-process locking +(`src/oauth/index.ts:602`, `:642`, `:794`; `src/oauth/store.ts:161`, `:183`, `:236`, +`:279`), owner-only credential writes (`src/config/atomic-write.ts:118`, `:160`), the +WHAM-401 refresh/replay path with generation fencing and bounded recovery +(`src/codex/auth-api.ts:984`, `:1021`, `:1063`; `src/codex/account-store.ts:611`, +`:659`, `:854`; `src/codex/quota-401-recovery.ts:57`, `:97`). + +`bun run privacy:scan` exited 0: `Privacy scan passed`. + +## Lane C — CLI, service, update, lifecycle + +PASS, and it settled the gate question. `ocx start` port probing is deliberate and cold +installs still get defaults (`src/cli/index.ts:215`); health retries three times +(`src/cli/dispatch.ts:573`). History-only restoration now exits 79 +(`src/cli/index.ts:1043`), with Bun and Node update lanes sharing one fail-closed +decision (`src/update/stop-decision.mjs:26`, `bin/ocx.mjs:375`) and the durable launcher +mirroring the child exit code (`bin/ocx.mjs:711`). Normal systemd/launchd stop is +unaffected. Version 2.39.0 is derived from `package.json` everywhere rather than +duplicated into constants. + +**Gate determination: both promotion SHAs need their own successful Service lifecycle +run.** The green `dev` run does not substitute for a promotion-SHA run. + +## Lane D — GUI and docs + +PASS. All 111 changed files accounted for. Conflict overwrite is consent-gated — the +locked switch cannot trigger it; a danger button opens a consequence dialog and the PUT +with `overwriteConflict: true` only follows confirmation +(`gui/src/pages/integrations/FileIntegrationPage.tsx:231`, `:284`). All 68 referenced +SVG paths exist in source and in built output, including all 29 new provider marks, with +no active-content SVG payload. `bun run build:gui` exit 0, `bun run lint:gui` exit 0, +docs build produced 401 pages. + +## Lane E — release mechanics, and the stale preview tag + +PASS on blockers, and it corrected an assumption in `000_plan.md`. + +**The v2.38.0 preview CI failure was not the macOS launcher flake.** Run `33386559501` +failed `macos` and `test 1/4` for one deterministic reason: + +> package.json version 2.38.0-preview.20260831 is BEHIND the highest release tag v2.38.0 + +That is `tests/release-version-line.test.ts:112`, and the same-core rule it rests on is +asserted non-vacuously at `:128`: `compareReleaseTags("v2.34.0-preview.1", "v2.34.0")` +is negative. SemVer orders a prerelease below its own stable. Cutting +`2.38.0-preview.*` **after** `v2.38.0` had already shipped was a version-selection +mistake, and the test caught it exactly as designed. Verified independently by reading +the test source; the lane's account is correct. + +This matters for us: it is not a flake to rerun past. Our +`2.39.0-preview.20260901` is a prerelease of a *future* core version relative to +`v2.38.0`, which the same helper orders as ahead. The trap is avoided by construction. + +Lane E also flagged a topology detail worth recording: a plain merge of `dev` into +`preview` baselines its service-gate diff from `v2.36.0-preview.20260830`, because +neither parent contains `v2.38.0`. The main merge baselines from `v2.38.0`. Both diffs +include the service paths, so both need the run either way. + +## Standing residual + +PR #3073 documents an intermittent macOS `tests/shutdown-launcher.test.ts` failure that +does not reproduce on Linux. It did not appear in run `33386559501` and is not implicated +in this release, but it can still surface on a promotion run. If it does, it is a +known test-harness issue, not a product regression — rerun once and escalate only if the +same assertion fails twice. diff --git a/devlog/_plan/260901_release_train_2390/070_outcome.md b/devlog/_plan/260901_release_train_2390/070_outcome.md new file mode 100644 index 0000000000..dee343b48b --- /dev/null +++ b/devlog/_plan/260901_release_train_2390/070_outcome.md @@ -0,0 +1,80 @@ +# Outcome — v2.39.0 shipped on both channels + +`DONE`. Both channels published, each `gitHead` matching the exact promotion SHA. + +| Channel | Version | Promotion SHA | npm `gitHead` | +|---------|---------|---------------|----------------| +| stable | `2.39.0` | `af6113a0381d6fff2e4dce587652825c7eeb6423` | matches | +| preview | `2.39.0-preview.20260901` | `75f3895c14965205be694e8ebb8e93f472630539` | matches | + +`npm view @bitkyc08/opencodex dist-tags` reads `latest=2.39.0`, +`preview=2.39.0-preview.20260901`. GitHub releases `v2.39.0` and +`v2.39.0-preview.20260901` both exist. Release runs `33464579658` (stable) and +`33464064409` (preview), both success. + +**The stale preview channel is fixed.** It had been stranded at +`2.36.0-preview.20260830` for two cycles. + +## Promotion sequence + +PR #3123 (`dev` → `preview`) merged at `75f3895c1`; PR #3125 (`dev` → `main`) merged +at `af6113a03`. Both PRs failed `enforce-target` and were drafted, as every promotion +PR is; both were readied and admin-merged. Both promotion SHAs needed and got their own +green push-event Cross-platform CI and Service lifecycle runs. + +## The audit found nothing, and that was checked rather than assumed + +Five parallel `gpt-5.6-sol`/high lanes returned PASS across the request path, +credentials, CLI/service, GUI/docs, and release mechanics — recorded in `050`. + +## What actually cost time: a real cross-platform flake + +`tests/server-auth.test.ts:2288` — +`server local API auth > websocket passthrough refreshes pool auth for each response.create turn` +— failed **three times** on this train: twice on macOS (preview PR run and preview push +run) and once on **Linux** `test 3/4` (main push run). Every failure was the same +assertion, always the *first* element: + +``` +expect(seenAuth).toEqual(["Bearer old-access-token", "Bearer new-access-token"]) +- Expected - 1 ++ Received + 1 +``` + +It passes locally on macOS and passed on rerun every time. The file is **not in the +promotion delta**, so this is not a v2.39.0 regression. + +The mechanism, from reading the test: the stored credential is saved with +`expiresAt: now + 120_000` (`:2237`) while `REFRESH_SKEW_MS` is `60_000` +(`src/codex/account-store.ts:22`). The refresh predicate is +`cred.expiresAt > Date.now() + REFRESH_SKEW_MS` (`:717`), so the credential is only +60 s clear of the skew boundary. Critically, `startServer(0)` runs at `:2245` +**before** `Date.now` is pinned at `:2249` — so any work the server does in that +window reads the real clock. When the first turn's read lands on the wrong side of that +boundary, the refresh fires early and `seenAuth[0]` is already the new token. The +second element is always correct, which is exactly the signature of an early first +refresh rather than a missing second one. + +This is a genuine test defect, not runner slowness. The 30 s CI watchdog floor in +`tests/helpers/ci-watchdog.ts` does not help, because nothing here times out. + +**A fix already exists and is not merged.** Commit `926a8d8c4` +(`test(auth): pin websocket refresh account`) on `codex/3063-combo-compact-failover` +pins the account namespace and routes both turns through `ws-refresh/gpt-test`. It +rides on PR #3109, which is about combo compact failover and unrelated to this test. +That fix should be split onto its own PR to `dev` so the flake stops taxing every +release train — it cost three reruns and roughly 45 minutes here. + +## Residual + +PR #3073's intermittent macOS `tests/shutdown-launcher.test.ts` failure did not appear +on this train and remains open. + +## Follow-up owed + +One item, and it is not this unit's to close: split `926a8d8c4` out of PR #3109 onto +its own PR against `dev`. The commit is a two-line test change that pins the account +namespace so both WebSocket turns route through `ws-refresh/gpt-test`; it has no +relationship to combo compact failover and should not wait on that review. Until it +lands, every release train pays the same three-rerun tax on a test that is not testing +the thing that breaks. From 33d32b6a34049480f5457358fcd3796260ae52a4 Mon Sep 17 00:00:00 2001 From: JUN Date: Tue, 1 Sep 2026 12:24:48 +0900 Subject: [PATCH 003/172] test(auth): pin the websocket refresh account to stop a cross-platform flake (#3128) --- tests/server-auth.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/server-auth.test.ts b/tests/server-auth.test.ts index 5efb577b73..448246e1b2 100644 --- a/tests/server-auth.test.ts +++ b/tests/server-auth.test.ts @@ -2230,6 +2230,7 @@ describe("server local API auth", () => { { id: "main", email: "main@example.test", isMain: true }, { id: "pool-a", email: "pool@example.test", isMain: false, chatgptAccountId: "acct-pool-a" }, ], + codexAccountNamespaces: { "ws-refresh": "pool-a" }, activeCodexAccountId: "pool-a", } as OcxConfig); saveCodexAccountCredential("pool-a", { @@ -2278,10 +2279,10 @@ describe("server local API auth", () => { }); await waitForOpen; - ws.send(JSON.stringify({ type: "response.create", model: "gpt-test", input: "hello" })); + ws.send(JSON.stringify({ type: "response.create", model: "ws-refresh/gpt-test", input: "hello" })); await waitForTerminal(); Date.now = () => now + 180_000; - ws.send(JSON.stringify({ type: "response.create", model: "gpt-test", input: "again" })); + ws.send(JSON.stringify({ type: "response.create", model: "ws-refresh/gpt-test", input: "again" })); await waitForTerminal(); ws.close(); From 6f415baeffbcf3cca579cd7f940de7186f0adda3 Mon Sep 17 00:00:00 2001 From: JUN Date: Tue, 1 Sep 2026 12:28:17 +0900 Subject: [PATCH 004/172] fix(release): call the dev version bump instead of listening for an event that never fires (#3129) --- .github/workflows/dev-version-bump.yml | 33 ++++++++++++++++++------ .github/workflows/release.yml | 35 ++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/.github/workflows/dev-version-bump.yml b/.github/workflows/dev-version-bump.yml index 45ecd4ae3f..b884b04ace 100644 --- a/.github/workflows/dev-version-bump.yml +++ b/.github/workflows/dev-version-bump.yml @@ -14,18 +14,35 @@ name: Dev version bump # sign-off that a bot cannot supply. Until that merge the red persists. This converts a # forgotten chore into a queued, reviewable change - not into an automatic repair. # -# A `release` event resolves this workflow file from the repository DEFAULT branch -# (`main`), not from `dev` - the same trap documented in cleanup-closed-pr-branches.yml. -# So merging this file to `dev` installs it but arms nothing; it first fires after an -# ordinary dev -> main promotion carries it there. +# WHY THIS IS CALLED, NOT TRIGGERED. It used to listen for `release: published`, and in +# that form it ran ZERO times across v2.37.0, v2.38.0 and v2.39.0 - every one of those +# bumps was still opened by hand (#3045, #3076, #3127). The workflow was not broken; the +# event never existed. `release.yml` creates the GitHub release with +# `GH_TOKEN: ${{ github.token }}`, and GitHub does not start workflow runs from events +# raised by the default `GITHUB_TOKEN`. A `release: published` listener therefore cannot +# observe a release this repository publishes itself, no matter which branch it sits on. +# +# The fix keeps the credential surface unchanged: no PAT, no app token, no +# `contents: write` on the release job. `release.yml` CALLS this workflow directly after +# a successful publish, so the run is a child of the release run instead of a reaction to +# an event that is never delivered. +# +# A `workflow_call` body resolves from the CALLER's ref, and `release.yml` only ever runs +# on `main` or `preview` (its own branch gate). So this file must be on `main` to take +# effect - the same promotion requirement the old comment described, now for a different +# reason. # # There is deliberately no `workflow_dispatch`: a branch-selected manual run executes # THAT branch body with `contents: write`. Re-drive a missed run by running # `bun scripts/bump-dev-version.ts package.json` locally and opening the pull # request normally. on: - release: - types: [published] + workflow_call: + inputs: + released-version: + description: "The tag that just published, e.g. v2.39.0" + required: true + type: string permissions: {} @@ -69,7 +86,7 @@ jobs: - name: Decide the version dev should carry id: decide env: - RELEASED_VERSION: ${{ github.event.release.tag_name }} + RELEASED_VERSION: ${{ inputs.released-version }} run: | set -euo pipefail bun scripts/bump-dev-version.ts "${RELEASED_VERSION}" package.json @@ -88,7 +105,7 @@ jobs: env: GH_TOKEN: ${{ github.token }} NEXT_VERSION: ${{ steps.decide.outputs.version }} - RELEASED_VERSION: ${{ github.event.release.tag_name }} + RELEASED_VERSION: ${{ inputs.released-version }} run: | set -euo pipefail diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 31ede9ab9d..458bb67e0a 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -36,6 +36,41 @@ concurrency: cancel-in-progress: false jobs: + # Move `dev` past the version that just published. + # + # This is a CALL, not a `release: published` listener. The release is created with + # `github.token`, and GitHub does not start workflow runs from events that token + # raises - so a listener cannot observe a release this repository publishes itself. In + # that form it ran ZERO times across v2.37.0, v2.38.0 and v2.39.0 while every one of + # those bumps was opened by hand (#3045, #3076, #3127). + # + # `needs: publish` means this is skipped unless the publish job succeeded, so a failed + # publish or a failed release creation never opens a bump pull request; the explicit + # condition only adds the dry-run case. The called workflow declares its own + # `contents: write` / `pull-requests: write` for its own job, so nothing here gains + # write access. + # + # Both channels call this, and the double-call is safe because `bump-dev-version.ts` + # compares against what `dev` already carries. In the usual train `dev` is already at + # the stable core when the preview publishes, so that call returns `changed=false` + # ("dev already carries 2.40.0, which is ahead of the published 2.40.0-preview.*") and + # every later step is gated on that output. The stable call returns `changed=true` and + # opens the one pull request. A preview publishing while `dev` is genuinely behind + # still bumps it, which is the point. + # + # It is declared FIRST in this file, ahead of the jobs it depends on, because + # tests/ci-workflows.test.ts splits the workflow on `- name:` and reads each `run:` + # block to the start of the next one when it checks that dispatch inputs never + # interpolate into shell source. A job declared between two steps lands inside that + # window and reads as shell. Job order in YAML carries no execution meaning - `needs` + # does - so declaring it before its own dependency costs nothing. + bump-dev-version: + needs: publish + if: ${{ inputs.dry-run != true }} + uses: ./.github/workflows/dev-version-bump.yml + with: + released-version: v${{ inputs.version }} + validate-dispatch: runs-on: ubuntu-latest permissions: From 132b557ad801a1ea4e6832c3fb7c5aecb92d5fbd Mon Sep 17 00:00:00 2001 From: JUN Date: Tue, 1 Sep 2026 12:29:29 +0900 Subject: [PATCH 005/172] docs(devlog): record the post-release automation repairs (#3130) --- .../080_post_release_repairs.md | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 devlog/_plan/260901_release_train_2390/080_post_release_repairs.md diff --git a/devlog/_plan/260901_release_train_2390/080_post_release_repairs.md b/devlog/_plan/260901_release_train_2390/080_post_release_repairs.md new file mode 100644 index 0000000000..3c9fd2e2e1 --- /dev/null +++ b/devlog/_plan/260901_release_train_2390/080_post_release_repairs.md @@ -0,0 +1,80 @@ +# Post-release repairs — the automation that never ran + +Two follow-ups from `070_outcome.md`, both landed on `dev`. + +## 1. The dev-version bump never fired (#3129) + +`070` recorded that the v2.39.0 bump was opened by hand. The reason turned out to be +worse than a missed run: `.github/workflows/dev-version-bump.yml` had **never executed**. +`gh api repos/lidge-jun/opencodex/actions/workflows/346296606/runs` returned +`total_count: 0` — zero runs across v2.37.0, v2.38.0 and v2.39.0, while #3045, #3076 and +#3127 were all opened by hand. + +### Cause + +`release.yml` creates the GitHub release with `GH_TOKEN: ${{ github.token }}` +(`release.yml:350`), and GitHub does not start workflow runs from events raised by the +default `GITHUB_TOKEN`. A `release: published` listener therefore cannot observe a +release this repository publishes itself, on any branch. + +The workflow's own header blamed something else — the default-branch resolution trap for +`release` events. That trap is real, and #3013 correctly moved the file to `main` to +satisfy it, but satisfying it armed nothing. Two plausible explanations for the same +silence, and the repository acted on the wrong one for three releases. + +The tag push is not an escape hatch either: `release.yml` pushes `refs/tags/vX.Y.Z` with +the same token, and no run exists for those pushes. Confirmed by querying push-event runs +with a `v2.*` head branch — empty. + +### Fix + +`release.yml` now **calls** the bump workflow after a successful publish, so the run is a +child of the release run instead of a reaction to an undelivered event. The bump workflow +becomes `on: workflow_call` with a `released-version` input. + +No new credential: no PAT, no app token, no `contents: write` added to the release job. +The called workflow keeps its write scopes on its own job, and `Protect dev` still means +a human merges the PR. Only the ignition changed. + +### One thing the tests taught + +`bump-dev-version` is declared **first** in `release.yml`, ahead of the jobs it depends +on. `tests/ci-workflows.test.ts:735` splits the workflow on `- name:` and reads each +`run:` block to the start of the next one, checking that dispatch inputs never +interpolate into shell source. A job declared after the last step falls inside that +window and reads as shell — the test failed on two placements before this one, correctly +both times. Job order carries no execution meaning (`needs` does), so the placement is +free and the injection check stays strict rather than being relaxed to accommodate us. + +A comment written during this work claimed a preview publish would move `dev` to the +preview's stable core. Checking it against the script instead of trusting it showed +`changed=false` — `dev` is normally already at that core when the preview publishes. The +comment was corrected before commit. + +### Activation delay, stated rather than discovered later + +A `workflow_call` body resolves from the **caller's** ref, and `release.yml` only runs on +`main` or `preview`. This takes effect after an ordinary `dev` → `main` promotion carries +it there; the next release is the first real exercise. Same shape of delay #3013 had, for +a different reason. + +## 2. The server-auth websocket flake (#3128) + +`926a8d8c4` cherry-picked out of #3109 onto its own branch, authorship preserved. The +commit is a two-line test change — pin `codexAccountNamespaces` and route both turns +through `ws-refresh/gpt-test` — and had no relationship to that PR's combo-compact-failover +subject. Analysis of the race is in `070`. + +## Result + +`dev` at `6f415baef`, carrying 2.40.0: + +``` +6f415baef fix(release): call the dev version bump instead of listening for an event that never fires (#3129) +33d32b6a3 test(auth): pin the websocket refresh account to stop a cross-platform flake (#3128) +9c8bbbf66 docs(devlog): record the v2.39.0 release train (#3126) +3e0f99a19 chore(release): move dev to 2.40.0 after the v2.39.0 release (#3127) +``` + +`#3127` should be the last hand-opened bump. Whether it is gets settled by the next +release, not by this note. From abcda8e134d7e3222d72877fe83c77fcb492a821 Mon Sep 17 00:00:00 2001 From: JUN Date: Tue, 1 Sep 2026 12:57:16 +0900 Subject: [PATCH 006/172] docs(devlog): record the 2026-08-31 non-priority-70 bug triage round (#3114) * docs(devlog): record the 2026-08-31 non-priority-70 bug triage round * docs(devlog): record the #3070 fix and the final CI state * docs(devlog): record why #1527 was left open rather than half-fixed * docs(devlog): record #3021 moving from unsolvable to fixed --- .../000_roadmap.md | 103 +++++ .../001_scan_verdicts.md | 77 ++++ .../002_audit_round1_synthesis.md | 101 +++++ .../003_audit_round2_synthesis.md | 64 +++ .../004_audit_round3_synthesis.md | 54 +++ .../070_outcome.md | 421 ++++++++++++++++++ 6 files changed, 820 insertions(+) create mode 100644 devlog/_plan/260831_bug_triage_nonprio70/000_roadmap.md create mode 100644 devlog/_plan/260831_bug_triage_nonprio70/001_scan_verdicts.md create mode 100644 devlog/_plan/260831_bug_triage_nonprio70/002_audit_round1_synthesis.md create mode 100644 devlog/_plan/260831_bug_triage_nonprio70/003_audit_round2_synthesis.md create mode 100644 devlog/_plan/260831_bug_triage_nonprio70/004_audit_round3_synthesis.md create mode 100644 devlog/_plan/260831_bug_triage_nonprio70/070_outcome.md diff --git a/devlog/_plan/260831_bug_triage_nonprio70/000_roadmap.md b/devlog/_plan/260831_bug_triage_nonprio70/000_roadmap.md new file mode 100644 index 0000000000..bcd247e866 --- /dev/null +++ b/devlog/_plan/260831_bug_triage_nonprio70/000_roadmap.md @@ -0,0 +1,103 @@ +# 260831 — bug triage round: everything the priority-70 train does not own + +A concurrent session owns the >=70 train (`devlog/_plan/260831_prio70_train_round2/`): +issues #3071, #3032, #3026, #3029, #3008, #3019 and their PRs #3069, #3056, #3040, +#3020. This unit owns the rest of the open bug surface and drives it to zero, keeping +only the handful that genuinely cannot be resolved from this tree. + +## Frozen snapshot + +Taken 2026-08-31T15:45:55Z against `dev` = `b4303bb9e`. Anything opened after that +timestamp is queued for the next round and does not change this round's acceptance +scope. + +**Bug issues (11):** #3070 #3068 #3064 #3059 #3051 #3024 #3021 #2999 #2813 #1527 #1419 +**Bug-labelled PRs (13):** #3078 #3067 #3066 #3063 #3053 #3052 #3041 #3039 #3038 #3034 +#3003 #3000 #2989 +**Also in scope, not bug-labelled:** #3030 (`chore`), carried only as a wrong-branch +janitorial closure. Audit round 1 caught this misclassification; see `002`. + +Issue #3009 and PR #3039 are in scope. They are easy to confuse with the train's #3008 +and PR #3040 — different defect, different file, different lane. + +## Why this roadmap is deliberately shallow + +The prio-70 unit wrote six diff-level decade docs before implementing anything. That +worked for six deep defects. This round has twenty-five items whose correct +disposition is mostly *closure*, and pre-writing a diff for an item that turns out to +be already fixed is wasted precision that then has to be un-written. + +So this document locks only what a roadmap must lock: the scope, the cluster +partition, the order, and the candidate disposition per item. **The diff-level design +for each cluster is produced in that work-phase's own A phase**, against the tree as +it stands when that phase starts, and recorded in the phase's own decade doc. This is +an explicit, user-directed deviation from DIFFLEVEL-ROADMAP-01. + +## Disposition vocabulary + +Every item leaves this round through exactly one of: + +| verdict | meaning | +| --- | --- | +| `MERGE` | the PR is correct and complete; squash after exact-head CI | +| `CHERRY_PICK` | only part of the PR is correct; take those hunks | +| `REIMPLEMENT` | the diagnosis is right and the remedy is wrong; rewrite on `dev` with a red-then-green regression | +| `CLOSE_FIXED` | already fixed on `dev`; cite the commit | +| `CLOSE_INVALID` | the claimed code path contradicts the tree | +| `CLOSE_DUPLICATE` | name the survivor | +| `CLOSE_NOT_REPRO` | no reproduction is possible against current `dev` | +| `UNSOLVABLE` | stays open; name exactly what external input is missing | + +A closure without a `file:line` or commit SHA in its comment does not count. + +## Work-phase map + +The order below is the audited order, not the original one. Audit round 1 found three +file collisions the first ordering ignored, two of them with the concurrent >=70 train +(`002`, findings 5-7). Train-blocked phases run late so an external dependency never +stalls the round. + +| # | wp | cluster | items | candidate disposition | blocked by | +| --- | --- | --- | --- | --- | --- | +| 0 | wp0 | this roadmap + live rescan | all of scope | — | — | +| 1 | wp1 | closes with no code | #3068, PR #3030 | duplicate; wrong-branch | — | +| 2 | wp2 | model catalog dated variants | #3024, PR #3034, PR #3041 | widen the suffix one-way; cherry-pick the merge tests | — | +| 3 | wp3 | cursor discovery transport | #3051, PR #3052 | merge after rebase | — | +| 4 | wp4 | windows service and scheduler | #3064 + PR #3067, #3009 + PR #3039 | one reimplement, one merge-after-fix | — | +| 5 | wp7 | residual bug PRs | PR #3078, PR #3053 | reimplement on dev; merge | #3053 needs wp2 | +| 6 | wp6 | account-pool auth and quota | PR #2989, then #2999 + PR #3000, then PR #3003 | merge; portable rewrite; merge | #2999 rewrite needs #2989 first (same file); #3003 needs train #3020 | +| 7 | wp5 | upstream request and compact metadata | PR #3066, then PR #3063, then PR #3038 | merge; merge; close the duplicate | #3066 and #3063 share `openai-responses.ts`, so #3066 lands first and #3063 rebases onto it. Train #3089 merged at `a0d386b49`, so the external blocker is gone (`004`) | +| 8 | wp9 | residual issue fixes | #3070, #1527, #3021, #3059 | four bounded reimplementations | — | +| 9 | wp8 | closeout | — | receipts, residual set, final audit | all | + +Each row is one full PABCD cycle. The candidate column is what this round's four +read-only `xai/grok-4.6` lanes concluded; none of it is binding until that phase's own +A phase confirms it against the tree as it stands then. + +**Every phase re-reads `gh pr diff --name-only` for its own PRs before merging, and pairs +that list against every other PR it is about to touch.** #3063 grew from two files to +five during wp0 and picked up two train-owned files, which moved it from wp7 to wp5 +(`003`); pairing then exposed that it also collides with #3066 (`004`). A file list +captured at scan time is not a fact about merge time, and neither is a blocker — train +#3089 merged at `a0d386b49` while this roadmap was being audited. + +## Declared unsolvable + +#2813 (needs a live Luna Reserve account's `/v1/models` and `/api/models` dumps) and +#1419 (needs macOS `.ips` crash frames on Bun 1.4.0, which the maintainer already +declined to claim was fixed). Both are argued in `002`. Two of the allowed three-to-four +slots are spent; the rest stay unspent until a phase earns one. + +## Constraints this round runs under + +- No local full suite. Focused `bun test tests/.test.ts` or `bun run test:changed` + only; whole-suite evidence comes from hosted exact-head CI or `ssh lidge`. +- Every commit and push uses `--no-verify`. `dev` is protected, so every change lands + through a branch and a PR, merged after exact-head CI is green. +- Read-only `xai/grok-4.6` lanes, unlimited, for investigation and audit. +- No file owned by the >=70 train is touched. + +## Terminal outcome + +`DONE` requires every scoped item terminal, at most four left open, each with a +recorded reason. Receipts land in `070_outcome.md`. diff --git a/devlog/_plan/260831_bug_triage_nonprio70/001_scan_verdicts.md b/devlog/_plan/260831_bug_triage_nonprio70/001_scan_verdicts.md new file mode 100644 index 0000000000..c7c2af0c88 --- /dev/null +++ b/devlog/_plan/260831_bug_triage_nonprio70/001_scan_verdicts.md @@ -0,0 +1,77 @@ +# 001 — live rescan verdicts for all 25 scoped items + +Four read-only `xai/grok-4.6` high-effort lanes, split so no two lanes shared a +verdict: (A) issues a prior scan believed the tree already answered, (B) the model +catalog dated-variant cluster, (C) platform/service and cursor transport, (D) request +metadata, account-pool auth, and the residual bug PRs. Every lane was instructed that +the tree wins over the issue body and the PR description. + +## The headline: the prior scan was wrong in both directions + +The round-2 below-bar table (`260831_prio70_train_round2/000_plan.md`) was written to +justify *exclusion* from a merge train, not to decide disposition. Read as disposition, +it misclassifies five items: + +| item | prior scan said | the tree says | +| --- | --- | --- | +| #3041 | reverse inference can resurrect retired ids | the author **removed** the reverse fold in `4e131140c`; the danger is in `aef4bec2`, which is no longer the head | +| #3070 | model filtering landed in `b68edc077` | that commit is CLI/API only and does not touch `gui/src/pages/Logs.tsx`; the dashboard still cannot find a Terra row | +| #1527 | all four named mechanisms are fixed on `dev` | all four SHAs are ancestors, but `envelope_exhausted` still silently full-replays for external-root models | +| #3021 | one occurrence, no ciphertext captured | `structurallyValidFernetTokens` already exists, so a bounded output filter needs no reporter ciphertext | +| #3053 | no linked user report | the runtime/catalog drift is real and the PR's tests drive production; absence of an issue number is not a defect | + +#3059 and #1419 held up only halfway, and audit round 1 caught the other half. The tree +does contradict #3059's unmount path, but a real focus residual survives that +refutation, so it became a wp9 fix instead of a close. #1419 was reported against a Bun +this tree no longer ships, but the maintainer explicitly declined to claim 1.4.0 fixed +it, so it became the second declared `UNSOLVABLE` instead of a close. See `002`. + +## Verdicts + +| item | verdict | one-line basis | phase | +| --- | --- | --- | --- | +| #3068 | `CLOSE_DUPLICATE` | same author, body and `input[240]` log as #3071; author already said "superseded" | wp1 | +| #3059 | `REIMPLEMENT` (was `CLOSE_INVALID`; see `002`) | the reported unmount cannot run — `refresh()` keeps stale data at `gui/src/client-resource.ts:339-341` — but the focus residual at `RestoreDialog.tsx:49-50` is real | wp9 | +| #1419 | `UNSOLVABLE` (was `CLOSE_NOT_REPRO`; see `002`) | `27764f342` moved the pin to Bun 1.4.0 and 200 TLS-failure cases produced no SIGTRAP, but the maintainer explicitly declined to claim that fixed the reporter's trap | residual | +| PR #3030 | `CLOSE_INVALID` | the branch is 61 files / +5114 of unrelated `main` work, and the classification it tests does not exist in `provider-routes.ts:957` | wp1 | +| #3024 | `REIMPLEMENT` | widen the suffix matcher one-way only; a live base row is not callability evidence for a configured dated snapshot | wp2 | +| PR #3034 | `MERGE_AFTER_REBASE` | the calendar matcher is the better vehicle; graft #3041's merge-loop tests, whose reverse test is the real resurrection guard | wp2 | +| PR #3041 | `CHERRY_PICK` | take the two merge tests and the directional comment; leave `isDateSuffix`, which still folds `0231` | wp2 | +| #3051 | via PR | — | wp3 | +| PR #3052 | `MERGE_AFTER_REBASE` | one production line; a pre-header EOF is `status === 0` and must be `transport`, not `HTTP unknown` | wp3 | +| #3064 | via PR | — | wp4 | +| PR #3067 | `REIMPLEMENT` | `[^\\\\/]*` leaves a fully CJK segment with no anchors, so `...\\김병준\\...` matches `...\\Admin\\...`; restrict the lossy run to `[?\\uFFFD]*` and give `` its own matcher | wp4 | +| #3009 | via PR | — | wp4 | +| PR #3039 | `MERGE_AFTER_REBASE` | correct remedy; restore `expect(probes).toBe(1)` and pin the 45s budget absolutely | wp4 | +| PR #3066 | `MERGE_AFTER_REBASE` | strips at the noncanonical adapter boundary only, copy-on-write, ChatGPT preserved; tests drive `buildRequest` | wp5 | +| PR #3038 | `CLOSE_DUPLICATE` | same defect, wrong layer (mutates canonical ChatGPT too) and its tests stay green with both call sites deleted | wp5 | +| #2999 | `REIMPLEMENT` | refresh lock is keyed on `OPENCODEX_HOME` while the file lives in `CODEX_HOME`; coordinate on the existing native-main claim instead | wp6 | +| PR #3000 | `REIMPLEMENT` (close in favor of the rewrite) | `dlopen(\"libc.so.6\")` breaks musl, and a late cancel discards an already-rotated grant | wp6 | +| PR #3003 | `MERGE_AFTER_REBASE` | a failed WHAM prime writes no quota, so the account is stale forever; the PR's tests drive `primeCodexPoolQuotas` | wp6 | +| PR #2989 | `MERGE_AFTER_REBASE` | the existing 503 test never re-enters, so it stays green on the broken path; the PR's tests do re-enter | wp6 | +| PR #3078 | `REIMPLEMENT` on `dev` | both production hunks are right; it targets `main` and its test file does not typecheck | wp7 | +| PR #3063 | `MERGE_AFTER_REBASE` | the second commit's tests do drive `handleResponsesCompact`; the "vacuous" reading was of the first commit | wp5 (moved: it now edits two train-owned files, `003`) | +| PR #3053 | `MERGE_AS_IS` | already rebased onto `b4303bb9e`; mirrors `isModelTextOnly` at both catalog sites | wp7 | +| #3070 | `REIMPLEMENT` | add a Logs model/provider query; the intercepted toggle stays Luna-only | wp9 | +| #1527 | `REIMPLEMENT` | fail closed on `envelope_exhausted` for external-root models instead of silently full-replaying | wp9 | +| #3021 | `REIMPLEMENT` | replace a client-visible Fernet payload with a structured error; do not widen recovery to `MESSAGE` | wp9 | +| #2813 | `UNSOLVABLE` | needs `/v1/models` and `/api/models` dumps from an account actually in Reserve; a picker screenshot cannot separate proxy-missing from client-filter | residual | + +## Residual candidates + +Two are declared unsolvable after audit round 1: #2813 and #1419. The round budget +allows three to four, so the remaining slots are held for phases that hit a genuine +wall, not spent in advance. + +## Note on phase numbering + +wp9 (residual issue reimplementations: #3070, #1527, #3021, and #3059 after audit round +1) was appended after this scan, because the roadmap assumed those would close without +code. wp8 remains the closeout and runs last. + +## This table is living + +Two audit rounds moved four rows after they were first written (#3059, #1419, #3063, +and #3030's label). PR file lists in particular are a moving target — #3063 grew from +two files to five during wp0 — so every phase re-reads `gh pr diff --name-only` for its +own PRs before merging rather than trusting this table's snapshot. diff --git a/devlog/_plan/260831_bug_triage_nonprio70/002_audit_round1_synthesis.md b/devlog/_plan/260831_bug_triage_nonprio70/002_audit_round1_synthesis.md new file mode 100644 index 0000000000..9a19db1647 --- /dev/null +++ b/devlog/_plan/260831_bug_triage_nonprio70/002_audit_round1_synthesis.md @@ -0,0 +1,101 @@ +# 002 — audit round 1: nine findings, seven upheld, one rebutted, one reclassified + +One adversarial `xai/grok-4.6` round against `000` and `001`. Verdict FAIL. Every +finding was re-checked against the tree by the main session before it was accepted or +rebutted; the reviewer's own citations were not taken on trust either. + +## Upheld — these change the plan + +**1. #3059 is not a clean `CLOSE_INVALID`.** The lane's mechanism analysis is right: +`refresh()` keeps stale data (`gui/src/client-resource.ts:339-341` — `shouldShowLoading` +is true only when `data === undefined` or `forceLoading`), so the `if (!status)` branch +at `gui/src/pages/integrations/FileIntegrationPage.tsx:175` is cold-load only and the +reported unmount cannot run. But a real focus residual survives that refutation, and +the code says so itself at `gui/src/pages/integrations/RestoreDialog.tsx:64-66`: + +> The row's button is gone from the DOM in the collapsed case, so this is a best +> effort: focus returns only if the trigger survived the close. + +The reporter's diagnosis is wrong and their experience is real. Closing as invalid +would discard the second half. **#3059 moves to wp9 as a bounded fix**: restore focus to +a stable element when the trigger did not survive, rather than dropping focus to +``. The trigger is the per-row button in +`gui/src/pages/integrations/RollbackHistory.tsx:48-56`, which is exactly the element the +collapsed case removes. The comment quoted above is at `RestoreDialog.tsx:49-50`, inside +the effect cleanup — not at `:64-66`, which is the `submit` body. + +**2. PR #3030 is `chore`, not `bug`.** `gh pr view 3030 --json labels` returns +`["chore","intake: hygiene-blocked"]`. The frozen scope called it a bug PR. Corrected +count: **13 bug-labelled PRs** (excluding the train's #3020) plus #3030, which stays in +scope only as a wrong-branch janitorial closure and is labelled as such. The citation +`provider-routes.ts:957` was also imprecise — line 957 is the `jsonResponse` inside the +catch; the point is that the whole catch block (`:955-965`) has no timeout +classification and `rg "Connection test timed out" src tests` returns nothing. + +**3. #1419 must not be closed.** The maintainer's own last comment keeps it open in +writing: "That is encouraging but **not** proof your crash is fixed... Claiming 1.4 +resolved your specific trap would go beyond what I can show." Closing it as not-repro +would contradict a recorded maintainer position. **#1419 becomes the second +`UNSOLVABLE`**: it needs macOS `DiagnosticReports` `.ips` frames from a recurrence on +Bun 1.4.0, which no one on this tree can synthesize. + +**5. PR #3066 collides with the train.** #3066 and the train's #3089 (the reopened +#3071 fix, head `codex/3071-web-search-query`) both edit +`src/adapters/openai-responses.ts`, and #3089 rewrites `backfillWebSearchQueries` +immediately above #3066's insertion point. **Ordering constraint: wp5 does not merge +until #3089 lands, then rebases onto that head.** If #3089 has not landed when wp5 comes +up, wp5 waits and a later phase runs first. + +**6. PR #3003 collides with the train.** #3003 and the train's #3020 both edit +`src/codex/auth-api.ts`, and both rewrite `primeCodexPoolQuotas` / +`fetchPoolAccountQuota`. **Ordering constraint: #3020 lands first, then #3003 rebases.** + +**7. Internal collision inside this round.** wp2 (#3034/#3041) and wp7 (#3053) both edit +`src/codex/catalog/provider-fetch.ts`. They are not disjoint. **wp2 lands before #3053.** + +**8. The phase map contradicted the verdict table.** `000` put #3070 and #1527 in wp1 as +closures while `001` marked both `REIMPLEMENT`; #3021 had the same split. Executing wp1 +from `000` would have closed two issues this scan had just proved still need code. The +`000` table is corrected and wp9 is now scheduled in it. + +**9. #3068 closes only as a duplicate of #3071.** The survivor is open and owned by the +other train, so the closing comment names #3071 and #3089 and claims nothing about a +fix being present. + +## Rebutted + +**4. #3041's `isDateSuffix` does accept `0231`.** The reviewer read the rejection tests +(`0001`, `1300`, `1240`) and concluded February 31 is rejected too. It is not: + +``` +$ git show refs/tmp/pr-3041:src/codex/catalog/provider-fetch.ts | rg -A6 'function isDateSuffix' +948:function isDateSuffix(suffix: string): boolean { +949: if (/^\d{8}$/.test(suffix)) return true; +950: if (!/^\d{4}$/.test(suffix)) return false; +951: const month = Number(suffix.slice(0, 2)); +952: const day = Number(suffix.slice(2)); +953: return month >= 1 && month <= 12 && day >= 1 && day <= 31; +954:} +``` + +`0231` is month 2, day 31: both bounds pass, so it folds. `1240` is rejected because day +40 exceeds 31, which is what the reviewer's cited test actually proves. The eight-digit +branch is worse — bare `/^\d{8}$/` folds `20250229`. #3034's calendar matcher rejects +both. The `CHERRY_PICK` verdict stands unchanged. + +## Revised residual set + +| item | why it cannot be resolved this round | +| --- | --- | +| #2813 | needs `/v1/models` and `/api/models` dumps from an account actually in Luna Reserve; a picker screenshot cannot separate proxy-missing from client-filter, and one blind catalog-field PR (#2862) already failed | +| #1419 | needs macOS `.ips` crash frames from a recurrence on the Bun this tree ships; the maintainer already declined to claim 1.4.0 fixed it | + +Two of the allowed three-to-four slots are spent. The rest are held for phases that hit +a real wall. + +## Corrected phase order + +**Superseded by `003` and `004`.** The order this round produced put #3063 in wp7 and left +#2989 and #3000 unordered against each other; rounds 2 and 3 fixed both. `000` carries +the authoritative order — this section is kept only so the amendment history reads in +sequence. diff --git a/devlog/_plan/260831_bug_triage_nonprio70/003_audit_round2_synthesis.md b/devlog/_plan/260831_bug_triage_nonprio70/003_audit_round2_synthesis.md new file mode 100644 index 0000000000..9f97366c97 --- /dev/null +++ b/devlog/_plan/260831_bug_triage_nonprio70/003_audit_round2_synthesis.md @@ -0,0 +1,64 @@ +# 003 — audit round 2: five findings, all upheld, including one of my own errors + +Same reviewer, resumed. It was asked to audit only the amendments. Verdict FAIL again. +All five stand. + +## 1. My `rg` was broken, and the reviewer caught it + +I told the reviewer `RollbackHistory` does not exist in this tree. It does: +`gui/src/pages/integrations/RollbackHistory.tsx`. My search was +`rg -n 'RollbackHistory' gui/src --include='*.tsx' -l`, and ripgrep rejected +`--include` as an unknown flag — that is a **glob**, and ripgrep spells it `-g`. The +command errored out and I read the empty result as absence. + +This is worth recording because the failure mode is silent: a tool that exits non-zero +on an unknown flag produces no matches, and no matches looks exactly like a confirmed +negative. A negative search result is only evidence when the command actually ran. + +The residual stands as the reviewer originally framed it: the restore trigger is the +per-row button in `RollbackHistory.tsx:55-58`, which the collapsed case removes from the +DOM, so `restoreFocusRef.current?.focus?.()` has nothing to focus. + +## 2. Citation off by five lines + +The self-documenting comment is at `RestoreDialog.tsx:49-50`, inside the effect cleanup, +not `:64-66`, which is the `submit` body. Fixed in `002`. + +## 3. #3063 now touches two train-owned files + +This one is a live-state change, not a reading error. When lane D judged #3063 it +reported two files. `gh pr diff 3063 --name-only` now returns five: + +``` +src/adapters/openai-responses.ts +src/bridge.ts +src/server/responses/compact.ts +src/types/request.ts +tests/server-combo-failover-e2e.test.ts +``` + +The first two are exactly what the train's #3089 rewrites. #3063 therefore inherits the +same constraint as #3066 and moves out of wp7 into wp5, which is the train-blocked +phase. It also shares `src/server/responses/compact.ts` with #3038 — harmless, because +#3038 is being closed, but it means wp5 owns the whole compact/metadata surface. + +This is the concrete argument for refreshing PR file lists at the phase that merges +them rather than at the scan: a PR is a moving target and this one moved during wp0. + +## 4. #3000 and #2989 collide with each other + +Both edit `src/oauth/index.ts` and `tests/oauth-refresh.test.ts`. #2989 is a merge and +#3000 is a rewrite, so wp6 merges #2989 first and the #2999 rewrite rebases onto it. +Recorded as an explicit intra-phase order, not left to chance. + +## 5. 001's table still said the old thing + +`001` is the living verdict table and still carried `#3059 CLOSE_INVALID / wp1` after +`002` moved it. A synthesis document that corrects a table without editing the table +leaves two contradicting sources, and the later phase reads the table. Fixed. + +## Amended order after round 2 + +wp1 → wp2 → wp3 → wp4 → wp7 (#3078, #3053-after-wp2) → wp6 (#2989, then #2999 rewrite; +#3003 after train #3020) → wp5 (#3066, #3063, close #3038 — all after train #3089) → +wp9 (#3070, #1527, #3021, #3059) → wp8. diff --git a/devlog/_plan/260831_bug_triage_nonprio70/004_audit_round3_synthesis.md b/devlog/_plan/260831_bug_triage_nonprio70/004_audit_round3_synthesis.md new file mode 100644 index 0000000000..b318172873 --- /dev/null +++ b/devlog/_plan/260831_bug_triage_nonprio70/004_audit_round3_synthesis.md @@ -0,0 +1,54 @@ +# 004 — audit round 3: four findings, all upheld, and one external unblock + +Same reviewer, third pass. Verdict FAIL. Three findings are document drift; the fourth +is a real collision and comes with news that changes the schedule. + +## The news: #3089 already merged + +`gh pr view 3089` → `MERGED 2026-08-31T16:47:15Z` at `a0d386b49`, and `origin/dev` now has +it at the tip. The train's #3071 fix landed while wp0 was being audited, so wp5's +external blocker is gone before wp5 ever ran. Train PR #3020 is still `OPEN`, so #3003's +blocker stands. + +A blocker that dissolves on its own is the argument for keeping the wp5 ordering rule +as "re-read state at the phase" rather than "wait for a fact recorded at scan time." + +## 4. #3066 and #3063 collide with each other + +Both edit `src/adapters/openai-responses.ts`. wp5 listed them as two merges with one +shared external blocker and no order between them. With #3089 merged, both can now land +on the same file in either order, which is exactly when an unordered pair bites. + +**wp5 order: #3066 first** (it is the narrower change — one strip call inside the +existing noncanonical block), then #3063 rebases onto that head, then #3038 closes +without merging. #3063 ∩ #3038 on `compact.ts` is therefore harmless. + +This pair was found by the reviewer pairing every scoped PR's file list against every +other, which is the check that caught #3063's growth in round 2 as well. It is now a +standing step, not a one-off. + +## 1-3. Document drift + +- `002` still carried `RestoreDialog.tsx:64-66` while `003` claimed it was corrected. The + claim was true of `001` and false of `002`. Fixed, with the `:49-50` / `:64-66` distinction + spelled out so it cannot drift back. +- `001`'s prose still said "#3059 and #1419 held up" and "only #2813 is currently + declared unsolvable", contradicting its own rewritten rows two paragraphs above. + Fixed. +- `002`'s phase order was the pre-round-2 sequence. It is now explicitly marked + superseded rather than silently rewritten, so the amendment history stays readable. + +All three are the same defect: correcting a table without correcting the prose that +summarizes it. The reviewer read the prose. So will the next phase. + +## Confirmed disjoint + +#3067 ∩ #3039 share `src/service.ts` but at `@@ -1935` and `@@ -660`, and wp4 already +orders them reimplement-then-merge. No other in-round or train overlap is unaccounted +for. + +## Order after round 3 + +wp1 → wp2 → wp3 → wp4 → wp7 (#3078; #3053 after wp2) → wp6 (#2989, then #2999/#3000, +then #3003 after train #3020) → wp5 (#3066, then #3063, then close #3038) → wp9 (#3070, +#1527, #3021, #3059) → wp8. diff --git a/devlog/_plan/260831_bug_triage_nonprio70/070_outcome.md b/devlog/_plan/260831_bug_triage_nonprio70/070_outcome.md new file mode 100644 index 0000000000..830e7413b4 --- /dev/null +++ b/devlog/_plan/260831_bug_triage_nonprio70/070_outcome.md @@ -0,0 +1,421 @@ +# 070 — outcome and receipts + +One row per work-phase, filled as it closes. Local full suites are forbidden this +round, so any suite-level receipt names hosted CI or `lidge`. + +## wp0 — scan and shallow roadmap (docs-only) + +- Status: closed. +- Deliverable: five documents — `000` roadmap and audited phase order, `001` living verdict + table for every scoped item, `002`/`003`/`004` audit syntheses. +- Research: four read-only `xai/grok-4.6` high-effort lanes over disjoint clusters. Every + load-bearing claim was re-verified in-tree by the main session before it entered a + document. +- Audit: four adversarial rounds, same reviewer resumed. Findings 9, 5, 4, 0. +- Commit: `d7bd430a4`. + +### Receipt — wp0 + +``` +bun test tests/repo-hygiene.test.ts -> exit 0, 12 pass / 0 fail / 23 expect() +``` + +That is the focused file covering a tracked `devlog/` change. No other focused set +applies to a docs-only phase. + +### What the scan changed about the plan + +- **The prior round's below-bar table is not a disposition table.** It was written to + justify exclusion from a merge train, and read as disposition it misclassifies five + items in both directions. #3041's dangerous reverse fold was already removed by its + author; #3070, #1527, #3021 and #3053 are not the non-defects it implies. +- **Two closes became something else.** #3059's reporter has the mechanism wrong and the + experience right, so it is a wp9 fix. #1419 cannot be closed because the maintainer + already declined, in writing, to claim Bun 1.4.0 fixed it. +- **Four file collisions were invisible at scan time.** Two with the concurrent train + (#3020 vs #3003, #3089 vs #3066/#3063) and two inside this round (#3034 vs #3053, + #3066 vs #3063, #3000 vs #2989). Each now has an explicit order. +- **A PR grew mid-audit.** #3063 went from two files to five and picked up two + train-owned files, which moved it a whole phase. The standing rule is now to re-read + and pair every file list at merge time. +- **A blocker dissolved mid-audit.** Train #3089 merged at `a0d386b49` while wp0 was + being audited, so wp5's external dependency was gone before wp5 ran. +- **One of my own searches was silently broken.** `rg --include='*.tsx'` is not a + ripgrep flag; the command errored and I read the empty output as proof a component + did not exist. The reviewer caught it. A negative search is evidence only when the + command actually ran. + +## wp1 — closes with no code (#3068, PR #3030) + +- Status: closed. Terminal outcome `DONE`. +- **#3068 needed nothing from this round.** A live refresh at 2026-08-31T16:55Z found it + already `CLOSED`, along with #3071, handled by the concurrent train when #3089 merged + at `a0d386b49`. The phase shrank from two items to one before it ran. +- **PR #3030 closed** at 2026-08-31T17:06:13Z as a wrong-base duplicate of #3025. + Both point at the identical fork head `38df9ff652f961576dfeddf16fe0c92774d56eb7`; + `dev...38df9ff` and `main...38df9ff` are the same 26 commits, so there are no + #3025-only commits to lose. +- **The audit corrected the closing comment before it was posted.** My basis said the + timeout classification the PR describes does not exist. It does not exist *on `dev`* + (`src/server/management/provider-routes.ts:956-963` still returns `err.message` or + `"Connection test failed"`), but it does exist on the shared head at `:956-966` with a + test at `tests/provider-connection-test.test.ts:486`. Closing on the stronger claim + would have told the author their work does not exist. The posted comment states the + distinction. +- Snapshot discipline: #3094 and #3093 arrived after the frozen snapshot and are queued + for the next round, not folded into this one. + +### Receipt — wp1 + +No code changed, so no focused test applies. Evidence is the closure itself: + +``` +gh pr close 3030 -> CLOSED 2026-08-31T17:06:13Z +gh pr view 3025 -> OPEN, base=dev, head=38df9ff652... (identical) +``` + +## wp2 — catalog dated variants (#3024, PR #3034, PR #3041) + +- Status: PR open, awaiting maintainer review. **PR #3100**, head `7063e3eb1`. +- Two commits: #3034's calendar matcher cherry-picked with authorship intact, then three + merge-loop regressions carried from #3041. +- **#3024 stays open.** The reported direction — configured `deepseek-v4-pro-0813` against + a live `deepseek-v4-pro` — still drops, by design, and the PR says so rather than + claiming the issue is fixed. Executed on the branch: + `dropped: ["deepseek-v4-pro-0813"]` for the reported direction, + `dropped: []` for the reverse. +- **Both reviewers were retired under DISPATCH-RETIRE-01** after 29 and 25 minutes of + silence, so the A gate was satisfied by a direct main-session audit. That audit is + stronger than the packet it replaced: it probed 22 suffix shapes, re-ran both + mutations, read all three retention paths, and executed the reported case. + +### Receipt — wp2 + +``` +bun test tests/codex-catalog.test.ts -> 254 pass / 0 fail / 980 expect() +bun x tsc --noEmit -> exit 0 +gh pr checks 3100 -> 23 pass, 1 skipping (windows is dispatch-only) +``` + +Mutations, both restored afterwards: + +| mutation | result | +| --- | --- | +| bidirectional merge loop | 253 pass / 1 fail — only the resurrection guard | +| suffix narrowed to `/^\d{8}$/` | 241 pass / 13 fail | + +## wp3 — cursor discovery EOF (#3051, PR #3052) + +- Status: PR open, CI running. **PR #3102**, head `2b11e98a9`. +- #3052 (author @terrytan95) cherry-picked onto current `dev` with authorship intact. The + patch needed no changes: one production line that classifies a pre-header stream end as + `transport` instead of `http`, which is the difference between retried and recorded as a + discovery failure. +- Closes #3051. + +### Receipt — wp3 + +``` +bun test tests/cursor-hardening.test.ts -> 42 pass / 0 fail / 89 expect() +bun x tsc --noEmit -> exit 0 +``` + +Mutation: deleting the single production line gives 41 pass / 1 fail, exactly +`retries an HTTP/2 stream that ends before response headers`. Restored to 42/0. + +## wp4 — windows service and scheduler (#3064/PR #3067, #3009/PR #3039) + +- Status: PR open. **PR #3104**, head `b727f8f81`, closes both #3009 and #3064. +- Two reimplementations landed on one branch because both edit `src/service.ts` and a + stacked pair is cheaper to review than a conflicting one. + +**#3009 / PR #3039.** The production logic was right and is carried as-is. Two things +were not: #3039 relaxed `expect(probes).toBe(1)` to `toBeGreaterThanOrEqual(1)` in the +zero-budget test, which is the exact assertion that stops a future change from sleeping +when the caller asked not to wait — "at least one" passes against the version it exists +to forbid. Its Windows-budget test asserted only `> linux`, which accepts 21s for a +service that bound past 20s. Both restored to absolute pins. + +**#3064 / PR #3067.** The diagnosis and the relocation are right: the mangling happens +inside `schtasks` before the bytes exist, so reading the query as a buffer cannot help. +The remedy was too wide. #3067 compiles every unrepresentable run to `[^\\/]*`, which +forbids a separator but allows arbitrary ASCII — and a segment that is entirely +non-ASCII then has no anchors at all. `C:\Users\\.opencodex\service-launcher.vbs` +would match `C:\Users\Admin\...`, so this process could adopt, repair or delete another +account's task, with the same hole on ``. #3067's own tests use `Người`, whose +surviving ASCII letters hide the case. The tolerance is now a substitution class only. + +### Receipt — wp4 + +``` +bun test tests/service.test.ts -> 187 pass / 0 fail / 608 expect() +bun x tsc --noEmit -> exit 0 +``` + +Three independent mutations, each restored: + +| mutation | result | +| --- | --- | +| remove the `waited` guard | 181 pass / 1 fail — the zero-budget test | +| remove the grace probe | 181 pass / 1 fail — the #3009 test | +| widen the substitution class back to `[^\\/]*` | 186 pass / 1 fail — `rejects another account's path that is merely the same shape` | + +Three mutations, three different failures. Each guard is load-bearing on its own. + +## wp7 — residual bug PRs (PR #3078, PR #3053) + +- Status: done. **PR #3105** (#3053 rebased) and **PR #3106** (#3078 reimplemented); + **#3078 closed**. +- #3053 needed nothing but a rebase. The runtime treats a model as sidecar-covered on + `noVisionModels` OR a text-only `modelInputModalities` declaration + (`src/vision/index.ts:31-38`); both catalog advertise sites checked only the first, so + a declared-text-only model stayed text-only in `/v1/models` and the Codex app refused + attachments client-side before the sidecar it is covered by ever ran. +- #3078's two production hunks were right and neither defect was otherwise on the + board. It could not be merged: it targets `main`, and `tests/cli-health-retry.test.ts` + declares `const servers: Server[]` while importing only `IncomingMessage` and + `ServerResponse`, so the head fails `tsc`. PR #3106 keeps both hunks and replaces the + port-binding fixture with a dependency-injected assertion plus a source oracle. + +### Receipt — wp7 + +``` +bun test tests/catalog-vision-sidecar-modalities.test.ts tests/codex-catalog.test.ts + -> 241 pass / 0 fail / 1052 expect() +bun test tests/cli-dispatch.test.ts -> 29 pass / 0 fail / 116 expect() +bun x tsc --noEmit -> exit 0 +``` + +| mutation | result | +| --- | --- | +| drop the modalities half of `sidecarCovered` | 17 pass / 2 fail | +| drop `probeConfiguredPort` from `handleStart` | 28 pass / 1 fail | +| drop the health retry budget | 28 pass / 1 fail | + +## wp6 — account-pool auth (PR #2989, #2999/PR #3000, PR #3003) + +- Status: two of three done. **PR #3111** (#2989 rebased) and **PR #3112** (#2999 + reimplemented); **#3000 closed**. **#3003 remains blocked** on train PR #3020. + +**#2989.** Eight author commits, carried unchanged. The defect: a durable refresh +intent survives a non-terminal failure, so one Anthropic 503 makes the next attempt +treat the token as possibly consumed and demand manual reauth. What makes it worth +recording is why `dev`'s own test missed it — `Anthropic transient failures do not mark +needsReauth` asserts only the first throw and never re-enters, and re-entry is where the +stale intent does its damage. A test that stops before the bug cannot see the bug. + +**#2999.** The lock is keyed under `OPENCODEX_HOME`; the file it protects lives under +`CODEX_HOME`, which every install shares. Two proxies with different homes took two +unrelated locks over one credential. Fixed by wrapping the refresh in the `CODEX_HOME` +claim the other native-main paths already use — no new primitive. + +**#3000 was not merged, and the reason is not style.** Its +`atomic-file-preserving-replace.ts` `dlopen`s `libc.so.6` and throws "No rename fallback is +safe" otherwise; musl names its libc `libc.so`, so credential publication would throw on +Alpine — worse than the race. And it throws on `signal.aborted` *before* +`persistRefreshedMainAuthJson`, so a late cancel discards a grant the provider already +rotated. A cancelled wait must not decide the fate of a refresh that succeeded. + +### Receipt — wp6 + +``` +bun test tests/oauth-refresh.test.ts -> 55 pass / 0 fail / 264 expect() +bun test tests/codex-main-account-refresh.test.ts tests/core-lab-boundary.test.ts + -> 21 pass / 0 fail / 63 expect() +bun x tsc --noEmit -> exit 0 +``` + +| mutation | result | +| --- | --- | +| #2989: always clear the intent | 53 pass / 2 fail — uncertain-outcome and replay guards | +| #2989: never clear it | 47 pass / 8 fail — transient recovery and the three cleanup-retry tests | +| #2999: drop the claim wrapper | 3 pass / 1 fail — the two-home serialization test | + +The #2989 pair is the useful one: the two mutations fail DISJOINT sets. Over-clearing +risks replaying a rotated token, under-clearing is the reported outage, and both sides +have their own guard. A condition with a guard on only one side is half a fix. + +## wp5 — request and compact metadata (PR #3066, PR #3063, PR #3038) + +- Status: done. **PR #3107** (#3066) and **PR #3109** (#3063); **#3038 closed**. +- The blocker this phase was scheduled around dissolved on its own: train PR #3089 + merged at `a0d386b49` during wp0's audit, so both rebases landed on a `dev` that + already had the #3071 fix in the same two files. +- #3038 versus #3066 was decided on layer. #3038 strips in `core.ts`/`compact.ts` + unconditionally, including the canonical ChatGPT forward where the field is not + foreign, and its tests call the helper directly — deleting both production call sites + leaves them green. #3066 strips inside the adapter's existing noncanonical guard and + its tests drive `buildRequest`. +- The earlier "vacuous regression" reading of #3063 was of its FIRST commit. Commit + `78855ed06` adds tests that drive the real `handleResponsesCompact`. Judging a PR on + one commit is how a correct change gets discarded. + +### Receipt — wp5 + +``` +bun test tests/openai-responses-passthrough.test.ts -> 117 pass / 0 fail / 372 expect() +bun test tests/server-combo-failover-e2e.test.ts -> 76 pass / 0 fail / 468 expect() +bun test tests/bridge.test.ts tests/openai-responses-passthrough.test.ts + -> 178 pass / 0 fail (rebase check) +bun x tsc --noEmit -> exit 0 +``` + +| mutation | result | +| --- | --- | +| remove the metadata strip call | 116 pass / 1 fail — the strip test | +| move the strip outside the canonical guard | 116 pass / 1 fail — the ChatGPT preservation test | +| drop `&& !route.combo` | 74 pass / 2 fail — failover hop and SSE | + +### One CI failure, and why it is not ours + +PR #3106 shard `test 2/4` failed on +`unauthenticated loopback listener > admits POST /v1/responses and its compact sibling` +with `Failed to start server. Is port 33953 in use?`. That is a runner port collision in +`tests/loopback-listener-integration.test.ts`, which imports nothing this branch changes; +the file passes 23/0 locally on the exact branch head. Rerun requested rather than +patched — treating an infrastructure flake as a code defect is how a good change gets +rewritten to satisfy a coincidence. + +## wp9 — residual issue fixes (#3070, #1527, #3021, #3059) + +- Status: two of four shipped. **PR #3113** closes #3059; **PR #3115** closes #3070. +- #3059 is the one whose evidence was fully in the tree. The reporter's mechanism is + wrong — `refresh()` keeps stale data, so `if (!status)` is cold-load only — and the + failure is real anyway: a restore that consumes its snapshot removes the row's + button, the remembered element is detached, and `.focus()` on a detached node succeeds + silently while focus stays on ``. `RestoreDialog` documented this against itself + in a comment; nobody had acted on it. +- **#3070 shipped as PR #3115.** A Logs search field over `model`, `resolvedModel` and + `provider`. `resolvedModel` is matched as well as `model` because they differ exactly + when routing redirected the turn, which is the case worth finding. Verified against a + live proxy, not only in unit tests: two real logged requests, query `terra`, one row + left. The locale-parity test caught `zh.ts` when only `zh-TW.ts` had been updated. +- **#3021 shipped as PR #3116**, and it turned out to be the opposite of unsolvable. + The report withheld the ciphertext, correctly, and none was needed: + `structurallyValidFernetTokens` already existed, so the wire shape alone reproduces it. + Executed on `dev` with a valid token, `hasUnreadableEncryptedAgentTask` returns `true` for + `NEW_TASK` and `false` for `MESSAGE` — the detector strips the routing envelope and asks + whether plaintext survives, and `AGENT_MESSAGE_ROUTING_ENVELOPE` only matched + `NEW_TASK`, so an unrecognised header counted as surviving text. +- **The earlier worry about a plaintext oracle was right about recovery and wrong about + detection.** Widening `recoverEncryptedAgentTask` to `MESSAGE` would decrypt a payload + the parent may not be entitled to read; widening the DETECTION pattern only lets the + proxy notice it is about to forward ciphertext. Those are different changes, and + conflating them is what made this look unsolvable for most of the round. +- **#1527 is the only item carried forward.** Bounded design in `001`, not blocked on + missing information — see the note below on why it was opened and put down. + +### Receipt — wp9 + +``` +cd gui && bun test tests/integrations-surfaces.test.tsx -> 34 pass / 0 fail / 135 expect() +bun x tsc --noEmit -> exit 0 +cd gui && bun run lint -> clean +``` + +Mutation: collapsing the cleanup back to `trigger?.focus?.()` gives 33 pass / 1 fail, +exactly the region test. The surviving-trigger test is the control. + +## wp8 — closeout + +- Status: done. Round terminal outcome: **partial** — every scoped item is disposed, + and ten pull requests are open awaiting maintainer review rather than merged. + +### What this round produced + +| disposition | items | +| --- | --- | +| closed outright | PR #3030, PR #3078, PR #3038, PR #3000 | +| closed by the train during the round | #3068, #3071 | +| superseded by a new PR | PR #3034, #3041 → #3100; #3052 → #3102; #3039, #3067 → #3104; #3053 → #3105; #3066 → #3107; #3063 → #3109; #2989 → #3111 | +| new PRs opened | #3100 #3102 #3104 #3105 #3106 #3107 #3109 #3111 #3112 #3113 #3114 #3115 | +| issues a merged PR will close | #3051, #3009, #3064, #2999, #3059, #3070 | +| declared unsolvable | #2813, #1419 | +| moved from unsolvable to fixed | #3021 → PR #3116 | +| carried to the next round | #1527 | +| blocked on the train | PR #3003 (needs #3020) | + +### Honest accounting of the acceptance criteria + +- **c-1, every scoped item terminal:** not met as written. Ten PRs are open pending + review, and this round cannot merge them — `dev` is protected and requires a + non-author approval. Disposition is complete; merge is not. +- **c-2, at most four left open:** met on the unsolvable count (two), not on the raw + open count, for the reason above. +- **c-3, evidence-based comments:** met. Every closure names a `file:line` or SHA, and + the nine superseded PRs each carry a comment explaining what was kept from them. +- **c-4, focused regression + green CI:** met. Every PR carries a mutation-verified + regression; all pass their exact-head CI except two runner flakes, both diagnosed + and rerun rather than patched around. +- **c-5, no train file touched:** met. Two collisions were found in advance and + ordered around; #3063 was moved a whole phase when its file list grew mid-round. +- **c-6, devlog records the round:** met by this unit. + +### The CI failures, and why none was patched + +PR #3106 shard `test 2/4`: `Failed to start server. Is port 33953 in use?` in +`tests/loopback-listener-integration.test.ts`, which imports nothing that branch +changes and passes 23/0 locally at the exact head. Rerun; now 29 pass. + +PR #3104 macOS: `ocx launcher graceful shutdown > SIGINT ...` in +`tests/shutdown-launcher.test.ts`, which does not import `src/service.ts` at all — and +the train has PR #3061 open for exactly this test's runner timing. Rerun. + +PR #3113 shard `test 4/4`: `npm launcher restarts the stopped runtime after a staged update` +`failure` in `tests/update-stop-first.test.ts`, a 91-second process-integration test. That +PR changes two files, both under `gui/`, and that suite imports neither. Rerun. + +All three were verified as unrelated before rerunning, and all three passed on rerun. +Rewriting a correct change to satisfy a coincidence is how a suite becomes a +superstition. + +### The GUI screenshot gate + +Both GUI PRs took `gui-screenshot-waived`, each with its reason posted rather than +labelled past silently. #3113 changes where focus lands after a dialog closes — the +pixels are identical before and after, so a screenshot would imply a verification that +did not happen, and the honest evidence is the `document.activeElement` assertion. #3115 +does change the UI and was captured live, but this run has no way to attach a PNG to a +PR body; the capture is reported as the rendered accessibility tree and table contents, +with a one-minute reproduction, and the offer to attach the image on request. + +### Final CI state + +All twelve pull requests: zero failing checks. + +### What the round is really evidence of + +Nine of the fourteen scoped PRs had a correct diagnosis. Three had a remedy that would +have shipped a worse defect than the one it fixed: #3067's path matcher would have let +one account adopt another's scheduler task, #3000's publication would have thrown on +musl and discarded a rotated grant on a late cancel, and #3038 would have stripped a +field ChatGPT owns. In each case the contributor found something real. The triage value +was not in judging them right or wrong — it was in separating the finding from the fix. + +## Declared unsolvable so far + +| item | missing input | +| --- | --- | +| #2813 | `/v1/models` and `/api/models` dumps from an account actually in Luna Reserve | +| #1419 | macOS `.ips` crash frames from a recurrence on Bun 1.4.0 | +### Why #1527 was opened and then put down + +The fix looked ready: `envelope_exhausted` at +`src/adapters/cursor/protobuf-request.ts:1505` silently sets `continuationMode =` +`"full-replay"`, `CursorRootEnvelopeLimitError` already exists in `cursor-errors.ts`, and +the file already imports it. Twenty lines, maybe. + +Then I read the comment sitting directly under that assignment. It records that the +reason is deliberately NOT propagated to the checkpoint store, that writing it there was +MEASURED inert because `live-transport.ts` prepares a spread copy, that reaching the store +needs the reason threaded through `PreparedCursorRunRequest`, and that this is a +signature change on the shared prepare path which "belongs to its own phase". It cites +the audit rounds that established each of those. + +Someone already stood where I was standing, went further than I had, and wrote down why +they stopped. Adding a throw on top of that without re-deriving their measurements would +not be finishing their work — it would be overwriting a conclusion I had not earned. The +cheap version of this fix is exactly the version the comment warns against. + +So #1527 stays open with its design recorded in `001` and this note attached. It is not +blocked on missing information; it is blocked on deserving the change. From 0dc01cdaaabdc4bf863acff6b3f67fae2a26aa3e Mon Sep 17 00:00:00 2001 From: JUN Date: Tue, 1 Sep 2026 14:12:23 +0900 Subject: [PATCH 007/172] fix(openai): allow canonical fake-IP addresses on provider PATCH (#3133) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The ordinary field-mask PATCH resolved the destination without the allowBenchmarkAddresses opt-in that POST and the re-enable path already use for the canonical built-in OpenAI forward provider. Under Clash/Mihomo fake-IP DNS (chatgpt.com → 198.18.0.0/15) the same canonical provider that was created successfully could never be patched: every context-overlay PATCH was rejected with a benchmark-address destination error. Pass the same exception on PATCH, computed the same way (name === "openai" && isCanonicalOpenAiForwardProvider(next)). The exception stays scoped to the exact canonical transport seed: loopback, RFC1918, metadata, and mixed dangerous DNS answers still fail closed, and non-canonical or OpenAI-like custom providers gain nothing. (cherry picked from commit f463e124d37f1ebef4db476a75827fe95dfc3879) Co-authored-by: Flowershangfromthebranches <152056395+Flowershangfromthebranches@users.noreply.github.com> --- src/server/management/provider-routes.ts | 8 +- tests/management-provider-validation.test.ts | 143 +++++++++++++++++++ 2 files changed, 150 insertions(+), 1 deletion(-) diff --git a/src/server/management/provider-routes.ts b/src/server/management/provider-routes.ts index d71397de8f..626148fd0c 100644 --- a/src/server/management/provider-routes.ts +++ b/src/server/management/provider-routes.ts @@ -752,7 +752,13 @@ export async function handleProviderRoutes(ctx: ManagementContext): Promise { } }); + test("canonical OpenAI PATCH passes allowBenchmarkAddresses into destination resolution", async () => { + // The ordinary field-mask PATCH resolves the SAME canonical chatgpt.com destination + // POST and re-enable already admit. Without the benchmark opt-in here, a Clash/Mihomo + // fake-IP user (chatgpt.com → 198.18.0.0/15) could create the provider but could never + // patch a context overlay onto it. Loopback/RFC1918/metadata and mixed dangerous + // answers still fail closed (covered by destination-policy-resolved tests and below). + if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); + mkdirSync(TEST_DIR, { recursive: true }); + process.env.OPENCODEX_HOME = TEST_DIR; + const liveConfig: OcxConfig = { + port: 0, + defaultProvider: "openai", + providers: { + openai: { ...canonicalDirect }, + }, + }; + const resolvedError = spyOn(destinationPolicy, "providerDestinationResolvedError") + .mockResolvedValue(null); + + try { + const patch = async (name: string, body: unknown) => { + const request = new Request(`http://127.0.0.1/api/providers?name=${encodeURIComponent(name)}`, { + method: "PATCH", + headers: { "content-type": "application/json" }, + body: JSON.stringify(body), + }); + return handleManagementAPI(request, new URL(request.url), liveConfig, { + createManagementConvergeCodex: catalogConvergenceFactory(), + }); + }; + + const canonical = await patch("openai", { modelContextWindows: { "gpt-5.6-luna": 900000 } }); + expect(canonical?.status).toBe(200); + expect(resolvedError).toHaveBeenCalledWith( + "openai", + expect.objectContaining({ baseUrl: canonicalDirect.baseUrl }), + { allowBenchmarkAddresses: true }, + ); + } finally { + resolvedError.mockRestore(); + } + }); + + test("PATCH destination benchmark exception stays scoped to the canonical openai row", async () => { + // A non-canonical openai row and any OpenAI-LOOKING custom provider must not inherit + // the fake-IP exception: their PATCHes still fail closed on benchmark answers. + if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); + mkdirSync(TEST_DIR, { recursive: true }); + process.env.OPENCODEX_HOME = TEST_DIR; + const liveConfig: OcxConfig = { + port: 0, + defaultProvider: "openai", + providers: { + openai: { ...canonicalDirect }, + mirror: { + adapter: "openai-chat", + baseUrl: "https://mirror.example.test/v1", + apiKey: "sk-secret-value", + }, + "openai-proxy": { + adapter: "openai-chat", + baseUrl: "https://mirror.example.test/v1", + apiKey: "sk-secret-value", + }, + }, + }; + const resolvedError = spyOn(destinationPolicy, "providerDestinationResolvedError") + .mockResolvedValue( + "baseUrl hostname mirror.example.test resolves to a benchmark address (198.18.0.30); set allowPrivateNetwork:true only for intentionally local/self-hosted providers", + ); + + try { + const patch = async (name: string, body: unknown) => { + const request = new Request(`http://127.0.0.1/api/providers?name=${encodeURIComponent(name)}`, { + method: "PATCH", + headers: { "content-type": "application/json" }, + body: JSON.stringify(body), + }); + return handleManagementAPI(request, new URL(request.url), liveConfig, { + createManagementConvergeCodex: catalogConvergenceFactory(), + }); + }; + + const custom = await patch("mirror", { defaultModel: "gpt-x" }); + expect(custom?.status).toBe(400); + expect(resolvedError).toHaveBeenCalledWith( + "mirror", + expect.anything(), + { allowBenchmarkAddresses: false }, + ); + + // Non-canonical row named "openai"-adjacent: no exception either. + const openaiProxy = await patch("openai-proxy", { defaultModel: "gpt-x" }); + expect(openaiProxy?.status).toBe(400); + expect(resolvedError).toHaveBeenCalledWith( + "openai-proxy", + expect.anything(), + { allowBenchmarkAddresses: false }, + ); + } finally { + resolvedError.mockRestore(); + } + }); + + test("canonical OpenAI PATCH still rejects non-benchmark private destination answers", async () => { + // The benchmark opt-in must not relax the rest of the SSRF guard: if the probe + // classifies the canonical destination as loopback/private/metadata, the PATCH fails. + if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); + mkdirSync(TEST_DIR, { recursive: true }); + process.env.OPENCODEX_HOME = TEST_DIR; + const liveConfig: OcxConfig = { + port: 0, + defaultProvider: "openai", + providers: { + openai: { ...canonicalDirect }, + }, + }; + const resolvedError = spyOn(destinationPolicy, "providerDestinationResolvedError") + .mockResolvedValue("baseUrl hostname chatgpt.com resolves to a loopback address (127.0.0.1); set allowPrivateNetwork:true only for intentionally local/self-hosted providers"); + + try { + const request = new Request("http://127.0.0.1/api/providers?name=openai", { + method: "PATCH", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ modelContextWindows: { "gpt-5.6-luna": 900000 } }), + }); + const response = await handleManagementAPI(request, new URL(request.url), liveConfig, { + createManagementConvergeCodex: catalogConvergenceFactory(), + }); + expect(response?.status).toBe(400); + expect(await response?.json()).toMatchObject({ + error: expect.stringContaining("loopback address"), + }); + expect(resolvedError).toHaveBeenCalledWith( + "openai", + expect.anything(), + { allowBenchmarkAddresses: true }, + ); + } finally { + resolvedError.mockRestore(); + } + }); + test("disabled-only PATCH cannot re-enable a noncanonical openai row unchanged", async () => { if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); mkdirSync(TEST_DIR, { recursive: true }); From b14b741dc2ac22334565cba404c8eec4c2c28277 Mon Sep 17 00:00:00 2001 From: JUN Date: Tue, 1 Sep 2026 14:13:58 +0900 Subject: [PATCH 008/172] fix(service): Windows cold-start budget and code-page-mangled scheduler paths (rebase of #3104) (#3134) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(service): give a Windows cold start room to bind, without loosening zero budget Reimplements #3039 (author @ntdatt812), whose diagnosis and production logic are both right. confirmServiceServing had a fixed 20s deadline and returned as soon as the clock passed it. A Windows cold start does NTFS ACL hardening and previous- session journal recovery before the listener exists, so #3009 recorded a service that bound a few seconds late and then stayed healthy -- reported as a terminal failure with exit 1. The caller's fallback is to start a second proxy against a port that is about to be taken, which is worse than waiting. Windows now gets 45s, every other platform keeps 20s, and the loop knocks once more after a short grace before calling it dead. Two changes to #3039 as submitted: - It relaxed `expect(probes).toBe(1)` to `toBeGreaterThanOrEqual(1)` in the zero-budget test. That assertion is what stops a future change from sleeping when the caller asked not to wait, and "at least one" passes against exactly the version it is meant to forbid. The `waited` guard already preserves the contract, so the original assertion is restored and the comment says why. - Its Windows-budget test asserted only `toBeGreaterThan(linux)`, which accepts 21s. The reported service bound past 20s, so the number is the contract: the test now pins 45_000 absolutely. Mutation-checked, both restored afterwards: remove the `waited` guard -> 181 pass / 1 fail, the zero-budget test remove the grace probe -> 181 pass / 1 fail, the #3009 test Closes #3009. * fix(service): forgive only what the code page mangled in a scheduler path Reimplements #3067 (author @ntdatt812). The diagnosis is right and the relocation is right: schtasks converts its XML through the console code page before the bytes exist, so runFile reading as a buffer cannot help. A profile named outside that page comes back as C:\Users\???\... and the exact comparison rejected a registration this process had just created correctly, so `ocx service install` rolled it back (#3064). The remedy needed narrowing. #3067 compiles every unrepresentable run to `[^\\/]*`, which forbids a path separator but allows arbitrary ASCII. A segment that is ENTIRELY non-ASCII then has no anchors left, so C:\Users\\.opencodex\service-launcher.vbs matches C:\Users\Admin\.opencodex\service-launcher.vbs and this process would adopt, repair, or delete another account's task. The same hole applies to , where MACHINE\ would match MACHINE\Admin. Its tests use "Người", whose surviving Ng and i letters hide the case. Here an unrepresentable run may match only a run of substitution characters -- '?' per character, U+FFFD, or nothing -- and every ASCII segment, including every separator, is matched literally. A foreign account's path fails because "admin" is not a run of substitutions. Mutation-checked: widening the class back to `[^\\/]*` gives 186 pass / 1 fail, exactly "rejects another account's path that is merely the same shape". Closes #3064. * fix(service): bind scheduler recovery to exact SID * fix(service): fail closed on ambiguous scheduler ownership * test(service): lock scheduler ownership guards * test(service): scope scheduler verification fixtures * test(service): exercise scheduler ownership oracles --- src/service.ts | 250 +++++++++++++++--- src/update/job.ts | 38 ++- tests/service.test.ts | 231 ++++++++++++++-- tests/update-job.test.ts | 38 ++- ...ows-scheduler-install-verification.test.ts | 17 +- 5 files changed, 508 insertions(+), 66 deletions(-) diff --git a/src/service.ts b/src/service.ts index 67138d79fc..7733be7df6 100644 --- a/src/service.ts +++ b/src/service.ts @@ -660,6 +660,23 @@ export function installedServiceListenPort(): number { export const SERVICE_INSTALL_HEALTH_MS = 20_000; +/** + * Windows gets a longer budget because its cold start does more before the + * listener exists: NTFS ACL hardening and previous-session journal recovery + * both run first, and #3009 recorded a service that bound a few seconds past + * the 20s deadline and then stayed healthy. Reporting that as a terminal + * repair failure is worse than waiting — the caller's fallback is to start a + * second proxy against a port that is about to be taken. + */ +export const SERVICE_INSTALL_HEALTH_WINDOWS_MS = 45_000; + +/** The health budget for the platform this is running on. */ +export function serviceInstallHealthMs( + platform: NodeJS.Platform = process.platform, +): number { + return platform === "win32" ? SERVICE_INSTALL_HEALTH_WINDOWS_MS : SERVICE_INSTALL_HEALTH_MS; +} + /** * Whether a proxy actually answers on the port this install/start just produced. * @@ -690,12 +707,23 @@ export async function confirmServiceServing( const now = deps.now ?? Date.now; const sleep = deps.sleep ?? ((ms: number) => new Promise(r => setTimeout(r, ms))); const probe = deps.probe ?? (async (p, h) => !!(await proxyIdentityAt(p, { hostname: h }))); - const deadline = now() + (deps.timeoutMs ?? SERVICE_INSTALL_HEALTH_MS); + const deadline = now() + (deps.timeoutMs ?? serviceInstallHealthMs()); + let waited = false; for (;;) { if (await probe(port, hostname)) return { ok: true, port }; - if (now() >= deadline) return { ok: false, port }; + if (now() >= deadline) break; await sleep(500); + waited = true; } + // The probe that ran last started before the deadline, so a service that binds + // during it is reported as dead (#3009). Knock once more after a short grace + // before calling it a failure. A zero budget means the caller asked not to + // wait, so it gets exactly the single probe it asked for and nothing more. + if (waited) { + await sleep(500); + if (await probe(port, hostname)) return { ok: true, port }; + } + return { ok: false, port }; } /** @@ -707,18 +735,19 @@ export async function confirmServiceServing( * fall back to a direct proxy start rather than reporting a successful update over a * dead port. */ -async function reportServiceServing( +export async function reportServiceServing( verb: "installed" | "started" | "repaired", deps: Parameters[0] = {}, ): Promise { - const serving = await confirmServiceServing(deps); + const healthBudgetMs = deps.timeoutMs ?? serviceInstallHealthMs(); + const serving = await confirmServiceServing({ ...deps, timeoutMs: healthBudgetMs }); if (serving.ok) { console.log(`✅ opencodex service ${verb} and serving on port ${serving.port}.`); return; } console.error( `⚠️ Service ${verb}, but no proxy answered on port ${serving.port} within ` - + `${Math.trunc(SERVICE_INSTALL_HEALTH_MS / 1000)}s.\n` + + `${Math.trunc(healthBudgetMs / 1000)}s.\n` + ` The manager registered the job; that is not the same as serving.\n` + ` Log: ${serviceLogPath()}\n` + ` Meanwhile: ocx start (serves in the foreground)`, @@ -1087,9 +1116,10 @@ export function evaluateWindowsSchedulerInstallVerification(inputs: { nativeStatus: "started" | "stopped" | "nonexistent" | "unknown"; wscript?: string; launcher?: string; + expectedUserId?: ExpectedWindowsTaskUserId | null; }): WindowsSchedulerInstallVerification { const registrationHealthy = inputs.xml.length > 0 - && windowsTaskRegistrationHealthy(inputs.xml, inputs.wscript, inputs.launcher); + && windowsTaskRegistrationHealthy(inputs.xml, inputs.wscript, inputs.launcher, inputs.expectedUserId); // Permanent invalidity: the XML IS published but violates the registration // contract — no amount of settling changes it. Empty/unreadable XML stays // transient (publication lag). @@ -1804,18 +1834,16 @@ export function buildWindowsTaskXml( script = windowsServiceScriptPath(), launcher = windowsLauncherVbsPath(), attemptNonce?: string, - sessionTriggerUserId = cachedCurrentWindowsIdentity()?.name, + sessionTriggerUserId = cachedCurrentWindowsIdentity()?.sid, ): string { const escapedWscript = taskXmlString(windowsWscript()); // Escape the launcher path independently for the element; quoting it // keeps spaces intact, and /b (batch mode) suppresses script error popups. const escapedLauncherArgs = taskXmlString(`/b /nologo "${launcher}"`); // `UserId` is optional in the schema, and omitting it makes a SessionStateChangeTrigger - // fire for ANY account's session change. Scope it to the installing account when that - // account is already known. The lookup is never forced here: this builder is synchronous - // and its output is validated before registration, so a failed or unavailable lookup must - // degrade to the unscoped trigger rather than leave the task with no recovery at all. - // `LogonTrigger` above is unscoped for the same reason and predates this change. + // fire for ANY account's session change. Production registration resolves and passes the + // installing account SID explicitly; the optional parameter remains only for deterministic + // builders/tests, and the live validator rejects an unscoped recovery trigger. const sessionUserIdElement = sessionTriggerUserId ? `\n ${taskXmlString(sessionTriggerUserId)}` : ""; @@ -1866,6 +1894,34 @@ export function buildWindowsTaskXml( `; } +type ExpectedWindowsTaskUserId = string | readonly string[]; + +function cachedWindowsTaskUserIds(): readonly string[] | null { + const identity = cachedCurrentWindowsIdentity(); + return identity ? [identity.sid, identity.name] : null; +} + +function resolvedWindowsTaskSid(): string { + let identity = cachedCurrentWindowsIdentity(); + if (!identity) { + const principal = resolveCurrentWindowsPrincipal(WINDOWS_PRINCIPAL_LOOKUP_TIMEOUT_MS); + identity = cachedCurrentWindowsIdentity(); + if (!identity && /^\*S-1-(?:\d+-)+\d+$/i.test(principal)) return principal.slice(1).toUpperCase(); + } + if (!identity) throw new Error("Windows Task Scheduler identity could not be resolved."); + return identity.sid; +} + +/** Render the exact UTF-16 task document published by production registration paths. */ +export function buildWindowsTaskXmlDocument( + script = windowsServiceScriptPath(), + launcher = windowsLauncherVbsPath(), + attemptNonce?: string, + sessionTriggerUserId = resolvedWindowsTaskSid(), +): string { + return `\uFEFF${buildWindowsTaskXml(script, launcher, attemptNonce, sessionTriggerUserId)}`; +} + function taskXmlSection(xml: string, tag: string): string { return new RegExp(`<${tag}(?:\\s[^>]*)?>([\\s\\S]*?)<\\/${tag}>`, "i").exec(xml)?.[1] ?? ""; } @@ -1935,6 +1991,67 @@ function taskXmlDecodedValueEquals(xml: string, tag: string, expected: string): return taskXmlDecodeEntities(value).trim().toLowerCase() === expected.trim().toLowerCase(); } +/** + * Characters a console code page substitutes when it cannot carry the original. + * Windows writes `?` per unrepresentable character, some layers write U+FFFD, and a + * few drop them entirely. + */ +const CODE_PAGE_SUBSTITUTIONS = /^[?\uFFFD]*$/; + +/** + * Compare a value that OpenCodex itself wrote against what `schtasks /query /xml` read + * back, tolerating ONLY the characters the console code page could not carry. + * + * `runFile` already reads the query as bytes, so this is not a spawn-decoding bug: the + * conversion happens inside `schtasks` before the bytes exist. A profile named outside + * the active code page — `C:\\Users\\김병준\\...` — comes back as `C:\\Users\\???\\...`, so an + * exact comparison rejected a registration this process had just created correctly and + * `ocx service install` rolled it back (#3064). + * + * The tolerance is deliberately narrow. Each unrepresentable RUN in the expected value + * may match only a run of substitution characters — never arbitrary text, and never a + * path separator. A wildcard as wide as `[^\\\\/]*` would leave a fully non-ASCII segment with + * no anchors at all, so `C:\\Users\\김병준\\x.vbs` would match `C:\\Users\\Admin\\x.vbs` and this + * process would adopt, repair, or delete another account's task. Accepting a foreign + * live task is a worse failure than the rollback this fixes. + */ +function taskXmlLossyValueEquals(reported: string, expected: string): boolean { + const a = reported.trim().toLowerCase(); + const b = expected.trim().toLowerCase(); + if (a === b) return true; + // Nothing unrepresentable in the expectation means there was nothing to mangle, + // so any difference is a real one. + if (!/[^\x00-\x7F]/.test(b)) return false; + const parts = b.split(/([^\x00-\x7F]+)/); + let rest = a; + for (let i = 0; i < parts.length; i += 1) { + const part = parts[i]!; + if (i % 2 === 0) { + // Literal ASCII run: it must be present verbatim, which is what keeps every + // directory boundary and file name in the path verified. + if (!rest.startsWith(part)) return false; + rest = rest.slice(part.length); + continue; + } + // Unrepresentable run: consume only substitution characters, and stop at the + // next literal so a trailing run cannot swallow the remainder of the string. + const next = parts[i + 1] ?? ""; + const end = next === "" ? rest.length : rest.indexOf(next); + if (end < 0) return false; + if (!CODE_PAGE_SUBSTITUTIONS.test(rest.slice(0, end))) return false; + rest = rest.slice(end); + } + return rest === ""; +} + +function taskXmlDecodedLossyValueEquals(xml: string, tag: string, expected: string): boolean { + if (taskXmlHasPrefixedTag(xml, tag)) return false; + if (taskXmlElementCount(xml, tag) !== 1) return false; + const value = new RegExp(`<${tag}(?:\\s[^>]*?)?>([^<]*)<\\/${tag}>`, "i").exec(xml)?.[1]; + if (value === undefined) return false; + return taskXmlLossyValueEquals(taskXmlDecodeEntities(value), expected); +} + function taskXmlOptionalValueEquals(xml: string, tag: string, expected: string): boolean { // Check the prefixed form first: treating `false` as an // omission would turn an explicitly disabled task into a healthy one. @@ -1968,7 +2085,10 @@ export function windowsTaskRegistrationOwnedByAttempt(xml: string, attemptNonce: * carrying one disabled trigger plus a different enabled one must not pass because the two * halves were found in unrelated elements. */ -function windowsTaskHasSessionRecoveryTriggers(triggers: string, expectedUserId: string | undefined): boolean { +function windowsTaskHasSessionRecoveryTriggers( + triggers: string, + expectedUserId: ExpectedWindowsTaskUserId | undefined, +): boolean { const scoped = triggers.match(/]*)?>[\s\S]*?<\/SessionStateChangeTrigger>/gi) ?? []; return WINDOWS_SESSION_RECOVERY_STATE_CHANGES.every(stateChange => scoped.some(element => @@ -1978,26 +2098,34 @@ function windowsTaskHasSessionRecoveryTriggers(triggers: string, expectedUserId: } /** - * A trigger's scope is acceptable when it is unscoped, or names the expected account. + * A trigger's scope is acceptable only when it names the expected account exactly. * - * An unscoped trigger is accepted rather than rejected: the schema makes `UserId` optional, - * the pre-existing `LogonTrigger` is unscoped for the same reason, and rejecting it would - * mean an installation whose account lookup is unavailable loses session recovery entirely. - * An explicitly scoped trigger is accepted only when the current account is known and matches. + * An unscoped recovery trigger is not identity proof. Production registration resolves a SID + * before writing XML; a missing scope therefore means the fixed-name task is legacy or foreign + * and must be refreshed from an exact legacy snapshot or preserved for manual review. * Treating an unknown expected identity as a wildcard would let a fresh status process accept a * task bound to another user's session and suppress the repair that should replace it. */ -function windowsTaskTriggerScopeAcceptable(element: string, expectedUserId: string | undefined): boolean { +function windowsTaskTriggerScopeAcceptable( + element: string, + expectedUserId: ExpectedWindowsTaskUserId | undefined, +): boolean { // A prefixed `` is a real scope this validator cannot read: taskXmlElementCount() // counts only unprefixed tags, so without this the element below would look ABSENT and the // trigger would be accepted as unscoped even though it is bound to some other account. // Reject it outright rather than guess, and do so before the optional-field check. if (taskXmlHasPrefixedTag(element, "UserId")) return false; const userIdCount = taskXmlElementCount(element, "UserId"); - if (userIdCount === 0) return true; + if (userIdCount === 0) return false; if (userIdCount !== 1) return false; if (expectedUserId === undefined) return false; - return taskXmlDecodedValueEquals(element, "UserId", expectedUserId); + // Scope is an identity boundary, unlike the launcher path. Newly generated tasks + // use the locale-independent SID from cachedCurrentWindowsIdentity(), so there is + // no reason to forgive code-page substitutions here. A lossy account-name compare + // lets two non-ASCII users collapse to the same `???` value and can make repair + // start another account's fixed-name task. + const expectedValues = typeof expectedUserId === "string" ? [expectedUserId] : expectedUserId; + return expectedValues.some(value => taskXmlDecodedValueEquals(element, "UserId", value)); } /** Validate the stable OpenCodex action, principal, settings, and logon trigger. */ @@ -2005,6 +2133,7 @@ function windowsTaskRegistrationBaseHealthy( xml: string, wscript = windowsWscript(), launcher = windowsLauncherVbsPath(), + allowLossyPaths = true, ): boolean { const scrubbed = taskXmlWithoutCommentsAndCdata(xml); // taskXmlSection() takes the FIRST match and the schema allows arbitrary XML under @@ -2030,8 +2159,15 @@ function windowsTaskRegistrationBaseHealthy( // quotes we wrote as `"` back to literal `"` on export, so an escaped // needle never matched and a healthy task read as permanently stale (#608). // Case-insensitive: elevated `schtasks /create` may rewrite System32 casing. - && taskXmlDecodedValueEquals(action, "Command", wscript) - && taskXmlDecodedValueEquals(action, "Arguments", `/b /nologo "${launcher}"`); + // Lossy on purpose: both name paths under the user profile, which the query + // cannot carry when the profile is named outside the code page (#3064). Only + // unrepresentable characters are forgiven; every ASCII segment and every + // separator is still matched literally. + && (allowLossyPaths + ? taskXmlDecodedLossyValueEquals(action, "Command", wscript) + && taskXmlDecodedLossyValueEquals(action, "Arguments", `/b /nologo "${launcher}"`) + : taskXmlDecodedValueEquals(action, "Command", wscript) + && taskXmlDecodedValueEquals(action, "Arguments", `/b /nologo "${launcher}"`)); } /** Validate the security/lifecycle-critical fields of the registered scheduler task. */ @@ -2039,7 +2175,7 @@ export function windowsTaskRegistrationHealthy( xml: string, wscript = windowsWscript(), launcher = windowsLauncherVbsPath(), - expectedUserId: string | null = cachedCurrentWindowsIdentity()?.name ?? null, + expectedUserId: ExpectedWindowsTaskUserId | null = cachedWindowsTaskUserIds(), ): boolean { const scrubbed = taskXmlWithoutCommentsAndCdata(xml); const triggers = taskXmlSection(scrubbed, "Triggers"); @@ -2052,13 +2188,17 @@ export function windowsTaskRegistrationHealthy( /** * The only stale definition repair may replace automatically: the previous OpenCodex task - * shape whose action/principal/settings are still exact and which has no session triggers yet. + * shape whose action/principal/settings are byte-exact and which has no session triggers yet. * Arbitrary unhealthy or partially modified fixed-name tasks are preserved for manual review. */ -function windowsTaskRegistrationRefreshableLegacy(xml: string): boolean { +function windowsTaskRegistrationRefreshableLegacy( + xml: string, + wscript = windowsWscript(), + launcher = windowsLauncherVbsPath(), +): boolean { const scrubbed = taskXmlWithoutCommentsAndCdata(xml); const triggers = taskXmlSection(scrubbed, "Triggers"); - return windowsTaskRegistrationBaseHealthy(xml) + return windowsTaskRegistrationBaseHealthy(xml, wscript, launcher, false) && taskXmlElementCount(triggers, "SessionStateChangeTrigger") === 0 && !taskXmlHasPrefixedTag(triggers, "SessionStateChangeTrigger"); } @@ -2078,7 +2218,7 @@ export function readWindowsSchedulerXmlState( xml: string, wscript?: string, launcher?: string, - expectedUserId: string | null = cachedCurrentWindowsIdentity()?.name ?? null, + expectedUserId: ExpectedWindowsTaskUserId | null = cachedWindowsTaskUserIds(), ): WindowsSchedulerXmlState { const installed = xml.length > 0; if (!installed) return { installed: false, enabled: false, registrationHealthy: false }; @@ -2242,7 +2382,11 @@ function writeWindowsSchedulerAssets(): void { // UTF-16LE + BOM: a BOM-less UTF-8 VBS mis-decodes non-ASCII (e.g. Korean) profile // paths on some WSH/codepage combinations — same contract as the task XML below. writeServiceAssetWithRetry(windowsLauncherVbsPath(), `\uFEFF${buildWindowsLauncherVbs(script)}`, "utf16le"); - writeServiceAssetWithRetry(windowsTaskXmlPath(), `\uFEFF${buildWindowsTaskXml(script)}`, "utf16le"); + writeServiceAssetWithRetry( + windowsTaskXmlPath(), + buildWindowsTaskXmlDocument(script, windowsLauncherVbsPath()), + "utf16le", + ); } const WINDOWS_SCHEDULER_STAGE_PREFIX = "opencodex-service-stage-"; @@ -2315,7 +2459,12 @@ export function stageWindowsSchedulerRegistrationXml( // document while UAC is pending; the file harden independently proves its identity. writeXml( xmlPath, - `\uFEFF${buildWindowsTaskXml(windowsServiceScriptPath(), windowsLauncherVbsPath(), attemptNonce)}`, + buildWindowsTaskXmlDocument( + windowsServiceScriptPath(), + windowsLauncherVbsPath(), + attemptNonce, + resolvedWindowsTaskSid(), + ), ); hardenPath(xmlPath); ownedWindowsSchedulerStages.add(xmlPath); @@ -2655,7 +2804,10 @@ export interface RepairServiceDeps { /** Publishes the captured registration only when the fixed task name remains absent. */ restoreSchedulerIfAbsent?: (registeredXml: string) => Promise; /** Resolves the account the registered triggers must match; null when it cannot be resolved. */ - resolveExpectedUserId?: (registeredXml: string) => string | null; + resolveExpectedUserId?: (registeredXml: string) => ExpectedWindowsTaskUserId | null; + /** Exact scheduler action values used by validation; defaults to the installed paths. */ + schedulerWscript?: string; + schedulerLauncher?: string; /** Test seam — defaults to process.platform so Linux CI cannot hit real installSystemd. */ platform?: NodeJS.Platform; } @@ -2756,11 +2908,26 @@ export async function repairService(deps: RepairServiceDeps = {}): Promise const expectedUserId = (deps.resolveExpectedUserId ?? resolveWindowsTaskDiagnosticUserId)(registeredXml); const registrationHealthy = windowsTaskRegistrationHealthy( registeredXml, - undefined, - undefined, + deps.schedulerWscript, + deps.schedulerLauncher, expectedUserId, ); - if (!registrationHealthy && !windowsTaskRegistrationRefreshableLegacy(registeredXml)) { + const expectedValues = expectedUserId === null + ? [] + : typeof expectedUserId === "string" ? [expectedUserId] : expectedUserId; + const preferredSid = expectedValues[0]; + const triggers = taskXmlSection(taskXmlWithoutCommentsAndCdata(registeredXml), "Triggers"); + // An exact legacy account name is safe to recognize, but rewrite it to the + // locale-independent SID while repair already owns the mutation boundary. + const identityUpgradeNeeded = registrationHealthy + && preferredSid !== undefined + && !windowsTaskHasSessionRecoveryTriggers(triggers, preferredSid); + const refreshableLegacy = windowsTaskRegistrationRefreshableLegacy( + registeredXml, + deps.schedulerWscript, + deps.schedulerLauncher, + ); + if (!registrationHealthy && !refreshableLegacy) { const scopedButUnresolved = expectedUserId === null && taskXmlElementCount( taskXmlSection(taskXmlWithoutCommentsAndCdata(registeredXml), "Triggers"), @@ -2781,7 +2948,7 @@ export async function repairService(deps: RepairServiceDeps = {}): Promise // Re-register only when the registered XML is actually stale, so the ordinary repair // stays free of `schtasks /create` and its UAC prompt. let startExpectedXml = registeredXml; - if (!registrationHealthy) { + if (!registrationHealthy || identityUpgradeNeeded) { // The task was stopped above, so a failed replacement must not exit here: `/create /f` // can be rejected, elevation can be cancelled, and staging or verification can fail. // Any of those would leave a previously runnable proxy stopped and the user worse off @@ -3855,7 +4022,7 @@ export function serviceStartableFromTray(service: ServiceDiagnostic): boolean { } export interface WindowsTaskDiagnosticIdentityDeps { - currentIdentity?: () => Readonly<{ name: string }> | null; + currentIdentity?: () => Readonly<{ sid: string; name: string }> | null; resolvePrincipal?: (timeoutMs: number) => string; } @@ -3867,10 +4034,10 @@ export interface WindowsTaskDiagnosticIdentityDeps { export function resolveWindowsTaskDiagnosticUserId( schedulerXml: string, deps: WindowsTaskDiagnosticIdentityDeps = {}, -): string | null { +): readonly string[] | null { const currentIdentity = deps.currentIdentity ?? cachedCurrentWindowsIdentity; const cached = currentIdentity(); - if (cached) return cached.name; + if (cached) return [cached.sid, cached.name]; const scrubbed = taskXmlWithoutCommentsAndCdata(schedulerXml); const triggers = taskXmlSection(scrubbed, "Triggers"); @@ -3881,7 +4048,8 @@ export function resolveWindowsTaskDiagnosticUserId( } catch { return null; } - return currentIdentity()?.name ?? null; + const resolved = currentIdentity(); + return resolved ? [resolved.sid, resolved.name] : null; } export interface WindowsServiceDiagnosticInputs { @@ -3893,7 +4061,7 @@ export interface WindowsServiceDiagnosticInputs { */ schedulerXml: string; /** Resolved effective account for explicit scheduler trigger scopes; null means unknown. */ - schedulerExpectedUserId?: string | null; + schedulerExpectedUserId?: ExpectedWindowsTaskUserId | null; /** Whether the on-disk service assets exist. A filesystem concern, not an XML one. */ schedulerAssetsPresent: boolean; nativeStatus: "started" | "stopped" | "nonexistent" | "unknown"; @@ -3905,7 +4073,7 @@ export interface WindowsServiceDiagnosticInputs { export function deriveWindowsServiceDiagnostic(inputs: WindowsServiceDiagnosticInputs): ServiceDiagnostic { const expectedUserId = inputs.schedulerExpectedUserId === undefined - ? cachedCurrentWindowsIdentity()?.name ?? null + ? cachedWindowsTaskUserIds() : inputs.schedulerExpectedUserId; const schedulerState = readWindowsSchedulerXmlState( inputs.schedulerXml, diff --git a/src/update/job.ts b/src/update/job.ts index 33dd907265..75a55b40b3 100644 --- a/src/update/job.ts +++ b/src/update/job.ts @@ -51,6 +51,11 @@ const RELEASE_NOTES_URL = "https://github.com/lidge-jun/opencodex/releases/lates const UPDATE_JOB_FILENAME = "update-job.json"; const UPDATE_TIMEOUT_MS = 180_000; const RESTART_TIMEOUT_MS = 60_000; +// A Windows `service repair` can spend up to 45s in its own serving probe after +// Task Scheduler/ACL work. The generic 60s child ceiling can kill that valid repair +// and launch a competing foreground proxy. Keep this below the update worker's 180s +// ceiling while covering the measured probe plus bounded Windows setup work. +const WINDOWS_SERVICE_REPAIR_TIMEOUT_MS = 150_000; const RESTART_HEALTH_TIMEOUT_MS = 30_000; const RESTART_STABILITY_WINDOW_MS = 15_000; /** Legacy active records did not persist a worker PID, so age is their only safe recovery signal. */ @@ -687,7 +692,12 @@ export function startUpdateJob( * and a bounded, structured summary — enough to tell a user which step failed and how, with no * free-form vendor text passing through the boundary. Detailed output stays ephemeral. */ -function runLoggedCommand(job: UpdateJobState, bin: string, args: string[], timeout: number): { status: number | null; signal: NodeJS.Signals | null } { +function runLoggedCommand( + job: UpdateJobState, + bin: string, + args: string[], + timeout: number, +): { status: number | null; signal: NodeJS.Signals | null; timedOut: boolean } { job = updateJob(job, {}, `$ ${formatCommand(bin, args)}`); const result = spawnSync(bin, args, { encoding: "utf8", @@ -698,7 +708,11 @@ function runLoggedCommand(job: UpdateJobState, bin: string, args: string[], time const stderr = typeof result.stderr === "string" ? result.stderr.trim() : ""; const summary = summarizeCommandOutput(stdout, stderr, result.status, result.signal); if (summary) updateJob(job, {}, summary); - return { status: result.status, signal: result.signal }; + return { + status: result.status, + signal: result.signal, + timedOut: (result.error as NodeJS.ErrnoException | undefined)?.code === "ETIMEDOUT", + }; } /** @@ -987,7 +1001,8 @@ export interface RestartIo { job: UpdateJobState, bin: string, args: string[], - ) => { status: number | null; signal?: NodeJS.Signals | null }; + timeoutMs: number, + ) => { status: number | null; signal?: NodeJS.Signals | null; timedOut?: boolean }; /** Override the explicit restart path (used by finishGuiUpdateRestart tests). */ restartAfterUpdateFn?: ( job: UpdateJobState, @@ -1155,10 +1170,23 @@ async function restartAfterUpdate( process.env.OCX_BAKE_PORT = String(Math.trunc(port)); let serviceOk = false; try { - const run = io.runService ?? ((j, bin, args) => runLoggedCommand(j, bin, args, RESTART_TIMEOUT_MS)); - const result = run(job, cmd.bin, cmd.args); + const repairTimeoutMs = (io.platform ?? process.platform) === "win32" + ? WINDOWS_SERVICE_REPAIR_TIMEOUT_MS + : RESTART_TIMEOUT_MS; + const run = io.runService ?? ((j, bin, args, timeoutMs) => runLoggedCommand(j, bin, args, timeoutMs)); + const result = run(job, cmd.bin, cmd.args, repairTimeoutMs); serviceOk = result.status === 0; if (!serviceOk) { + if (result.timedOut) { + // UAC and scheduler mutation can outlive a fixed child deadline. Once the + // worker kills that child, ownership is ambiguous: launching a foreground + // proxy here can race a registration that completes moments later. + updateJob(job, {}, "Service repair timed out with Task Scheduler state unknown; refusing a competing direct start."); + throw new Error( + "Service repair timed out with Task Scheduler state unknown; refusing a competing direct start. " + + "Run 'ocx service status', then 'ocx service repair' by hand.", + ); + } // The refresh that just failed was `ocx service repair` (serviceReinstallArgs). // It normally reuses a healthy registration, but a stale definition may have tried // guarded re-registration/elevation. Advising `install` here would unconditionally diff --git a/tests/service.test.ts b/tests/service.test.ts index 4e9247e4b8..f9b4e55cd5 100644 --- a/tests/service.test.ts +++ b/tests/service.test.ts @@ -1,4 +1,4 @@ -import { afterEach, describe, expect, spyOn, test } from "bun:test"; +import { afterAll, afterEach, describe, expect, spyOn, test } from "bun:test"; import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; import { execFileSync } from "node:child_process"; import { tmpdir } from "node:os"; @@ -7,15 +7,31 @@ import { pathToFileURL } from "node:url"; import * as serviceModule from "../src/service"; import { saveConfig } from "../src/config"; import { windowsEnvIndirectBatchValue } from "../src/lib/win-paths"; -import { assertServiceAuthEnvironment, assertServiceEnvironmentMatchesInstall, bakedServicePathsDiagnostic, confirmServiceServing, launchdListenPort, systemdListenPort, buildPlist, buildUnit, buildWindowsLauncherVbs, buildWindowsSchtasksCreateArgs, buildWindowsSchtasksCreateArgsForXml, buildWindowsServiceScript, buildWindowsTaskXml, deriveWindowsServiceDiagnostic, deriveWindowsServiceDiagnosticForCurrentUser, installFreshWindowsSchedulerSafely, installServiceSafely, launchctlLoadFailed, launchdJobMatchesPlist, normalizeServiceSubcommand, parseServiceArgs, parseServiceInstallState, planServiceCommand, prepareServiceInstall, probeServiceInstallation, readWindowsSchedulerXmlState, registerFreshWindowsSchedulerTask, removeNativeWindowsServiceForScheduler, repairService, resolveServiceListenPort, runLaunchctl, selectServiceSubcommand, serviceLogPath, serviceStartableFromTray, serviceStatusReport, serviceRetryCommand, serviceStatusSummary, stableLauncherEntry, systemdNeedsDaemonReload, systemdServiceInstallCleanupOps, uninstallSystemd, windowsListenPort, winswListenPort, startLaunchd, windowsTaskRegistrationHealthy } from "../src/service"; +import { assertServiceAuthEnvironment, assertServiceEnvironmentMatchesInstall, bakedServicePathsDiagnostic, confirmServiceServing, launchdListenPort, systemdListenPort, buildPlist, buildUnit, buildWindowsLauncherVbs, buildWindowsSchtasksCreateArgs, buildWindowsSchtasksCreateArgsForXml, buildWindowsServiceScript, buildWindowsTaskXml as buildWindowsTaskXmlProduction, buildWindowsTaskXmlDocument, deriveWindowsServiceDiagnostic, deriveWindowsServiceDiagnosticForCurrentUser, installFreshWindowsSchedulerSafely, installServiceSafely, launchctlLoadFailed, launchdJobMatchesPlist, normalizeServiceSubcommand, parseServiceArgs, parseServiceInstallState, planServiceCommand, prepareServiceInstall, probeServiceInstallation, readWindowsSchedulerXmlState, registerFreshWindowsSchedulerTask, removeNativeWindowsServiceForScheduler, repairService, reportServiceServing, resolveServiceListenPort, runLaunchctl, selectServiceSubcommand, SERVICE_INSTALL_HEALTH_MS, SERVICE_INSTALL_HEALTH_WINDOWS_MS, serviceInstallHealthMs, serviceLogPath, serviceStartableFromTray, serviceStatusReport, serviceRetryCommand, serviceStatusSummary, stableLauncherEntry, systemdNeedsDaemonReload, systemdServiceInstallCleanupOps, uninstallSystemd, windowsListenPort, winswListenPort, startLaunchd, windowsTaskRegistrationHealthy as windowsTaskRegistrationHealthyProduction } from "../src/service"; import type { ServiceDiagnostic } from "../src/service"; import { definitionCarriesCredential, resolvedProxyEnv, writeServiceDefinitionFile } from "../src/service"; import { buildWinswXml } from "../src/lib/winsw"; import { CONFIG_OWNER_FILE, CONFIG_UNINSTALL_MANIFEST, recordOwnedConfigPath, removeOwnedConfigState } from "../src/lib/config-ownership"; import { serviceApiTokenFilePath } from "../src/lib/service-secrets"; import { WindowsSchtasksError } from "../src/lib/windows-elevation"; +import { resolveCurrentWindowsPrincipal, setWindowsPrincipalRunnerForTests } from "../src/lib/windows-user-principal"; import type { OcxConfig } from "../src/types"; +const TEST_WINDOWS_TASK_SID = "S-1-5-21-111-222-333-1001"; +setWindowsPrincipalRunnerForTests(() => ({ + success: true, + exitCode: 0, + timedOut: false, + stdout: `${TEST_WINDOWS_TASK_SID}\nMACHINE\\tester\n`, +})); +resolveCurrentWindowsPrincipal(1_000); +afterAll(() => { setWindowsPrincipalRunnerForTests(null); }); + +const buildWindowsTaskXml = (...args: Parameters) => + buildWindowsTaskXmlProduction(args[0], args[1], args[2], args[3] ?? TEST_WINDOWS_TASK_SID); +const windowsTaskRegistrationHealthy = (...args: Parameters) => + windowsTaskRegistrationHealthyProduction(args[0], args[1], args[2], args[3] === undefined ? TEST_WINDOWS_TASK_SID : args[3]); + const TEST_DIR = join(import.meta.dir, ".tmp-service-test"); const previousOpenCodexHome = process.env.OPENCODEX_HOME; const previousCodexHome = process.env.CODEX_HOME; @@ -488,6 +504,52 @@ describe("Windows service task", () => { expect(windowsTaskRegistrationHealthy(disabled, wscript, launcher)).toBe(false); }); + /** + * #3064: `schtasks /query /xml` converts the document through the console code + * page before the bytes exist, so a profile named outside that page comes back + * with substitution characters. An exact comparison rejected a registration this + * process had just created correctly, and `ocx service install` rolled it back. + * + * The tolerance has to stay narrow enough that a MANGLED path still cannot match + * a DIFFERENT account's path. A wildcard as wide as `[^\\/]*` leaves a fully + * non-ASCII segment with no anchors at all, so `...\\김병준\\...` would match + * `...\\Admin\\...` and this process would adopt another account's task. + */ + describe("a scheduler path the console code page could not carry", () => { + const wscript = "C:\\Windows\\System32\\wscript.exe"; + const launcher = "C:\\Users\\김병준\\.opencodex\\service-launcher.vbs"; + const healthy = (reportedLauncher: string, expectedLauncher = launcher) => + windowsTaskRegistrationHealthy( + buildWindowsTaskXml("ignored.cmd", reportedLauncher) + .replace(/.*?<\/Command>/, `${wscript}`), + wscript, + expectedLauncher, + ); + + test.each([ + ["question marks, one per character", "C:\\Users\\???\\.opencodex\\service-launcher.vbs"], + ["a single replacement character", "C:\\Users\\\uFFFD\\.opencodex\\service-launcher.vbs"], + ])("accepts a registration whose profile came back as %s", (_label, reported) => { + expect(healthy(reported)).toBe(true); + }); + + // The reason the tolerance is a substitution class and not a wildcard. + test("rejects another account's path that is merely the same shape", () => { + expect(healthy("C:\\Users\\Admin\\.opencodex\\service-launcher.vbs")).toBe(false); + }); + + test("rejects a path whose ASCII structure differs", () => { + expect(healthy("C:\\Users\\???\\.opencodex\\other-launcher.vbs")).toBe(false); + expect(healthy("D:\\Users\\???\\.opencodex\\service-launcher.vbs")).toBe(false); + }); + + // An expectation with nothing unrepresentable in it has nothing to forgive. + test("does not forgive substitutions when the expected path is pure ASCII", () => { + const ascii = "C:\\Users\\Test\\.opencodex\\service-launcher.vbs"; + expect(healthy("C:\\Users\\???\\.opencodex\\service-launcher.vbs", ascii)).toBe(false); + }); + }); + /** * `UserId` is optional in the schema, and omitting it makes a SessionStateChangeTrigger fire * for any account's session change. Scope it to the installing account when that account is @@ -539,7 +601,19 @@ describe("Windows service task", () => { expect(windowsTaskRegistrationHealthy(scoped, wscript, launcher, null)).toBe(false); expect(windowsTaskRegistrationHealthy(scoped, wscript, launcher, "MACHINE\\installer")).toBe(true); expect(windowsTaskRegistrationHealthy(foreign, wscript, launcher, "MACHINE\\installer")).toBe(false); - expect(windowsTaskRegistrationHealthy(unscoped, wscript, launcher, null)).toBe(true); + expect(windowsTaskRegistrationHealthy(unscoped, wscript, launcher, null)).toBe(false); + }); + + test("never code-page-folds an explicit session identity", () => { + const wscript = "C:\\Windows\\System32\\wscript.exe"; + const launcher = "C:\\Users\\Test\\.opencodex\\service-launcher.vbs"; + const expected = "MACHINE\\김병준"; + const scoped = buildWindowsTaskXml("ignored.cmd", launcher, undefined, expected) + .replace(/.*?<\/Command>/, `${wscript}`); + const mangled = scoped.replaceAll(expected, "MACHINE\\???"); + + expect(windowsTaskRegistrationHealthy(scoped, wscript, launcher, expected)).toBe(true); + expect(windowsTaskRegistrationHealthy(mangled, wscript, launcher, expected)).toBe(false); }); test("validates the registered scheduler action, trigger, principal, and settings", () => { @@ -548,7 +622,7 @@ describe("Windows service task", () => { // healthy, and repair would then leave that foreign scope in place. const guardWscript = "C:\\Windows\\System32\\wscript.exe"; const guardLauncher = "C:\\Users\\Test\\.opencodex\\service-launcher.vbs"; - const guardXml = buildWindowsTaskXml("ignored.cmd", guardLauncher, undefined, "") + const guardXml = buildWindowsTaskXml("ignored.cmd", guardLauncher, undefined, TEST_WINDOWS_TASK_SID) .replace(/.*?<\/Command>/, `${guardWscript}`); expect(windowsTaskRegistrationHealthy(guardXml, guardWscript, guardLauncher)).toBe(true); const foreignPrefixed = guardXml.replace( @@ -599,7 +673,7 @@ describe("Windows service task", () => { expect(canonical).not.toContain("RunLevel"); expect(windowsTaskRegistrationHealthy(canonical, wscript, launcher)).toBe(true); - expect(readWindowsSchedulerXmlState(canonical, wscript, launcher)).toMatchObject({ + expect(readWindowsSchedulerXmlState(canonical, wscript, launcher, TEST_WINDOWS_TASK_SID)).toMatchObject({ installed: true, enabled: true, registrationHealthy: true, @@ -788,10 +862,10 @@ describe("Windows service task", () => { expect(service).toContain("if (existsSync(windowsLauncherVbsPath())) unlinkSync(windowsLauncherVbsPath());"); }); - test("writes Task Scheduler XML with a UTF-16 BOM for schtasks", async () => { - const service = await Bun.file(new URL("../src/service.ts", import.meta.url)).text(); - - expect(service).toContain('writeServiceAssetWithRetry(windowsTaskXmlPath(), `\\uFEFF${buildWindowsTaskXml(script)}`, "utf16le")'); + test("writes Task Scheduler XML with an exact SID and UTF-16 BOM", () => { + const document = buildWindowsTaskXmlDocument("service.cmd", "launcher.vbs"); + expect(document.charCodeAt(0)).toBe(0xFEFF); + expect(document).toContain(`${TEST_WINDOWS_TASK_SID}`); }); test("escapes environment values that would break out of set quotes", () => { @@ -2019,7 +2093,7 @@ describe("service lifecycle cleanup ordering", () => { expect(assetsAt).toBeLessThan(createAt); expect(installWindows).not.toContain("writeFileSync(script"); expect(assetsHelper).toContain("writeServiceAssetWithRetry(script"); - expect(assetsHelper).toContain("writeServiceAssetWithRetry(windowsTaskXmlPath()"); + expect(assetsHelper).toContain("windowsTaskXmlPath(),"); // Retry helper tolerates transient Windows file locks from the just-ended task. expect(service).toContain('code !== "EBUSY" && code !== "EPERM" && code !== "EACCES"'); }); @@ -2125,15 +2199,17 @@ describe("service diagnostics", () => { staleBakedPaths: false, nativeRepairAssetsOnly: false, diagnostics: "logs: test", + schedulerExpectedUserId: TEST_WINDOWS_TASK_SID, }; const installedEnabled = { schedulerXml: healthyTaskXml() }; const installedDisabled = { schedulerXml: disabledTaskXml() }; test("resolves an explicit scheduler scope once at the Windows diagnostic boundary", () => { - const scoped = buildWindowsTaskXml(undefined, undefined, undefined, "MACHINE\\installer"); - const foreign = scoped.replaceAll("MACHINE\\installer", "OTHER\\account"); + const sid = "S-1-5-21-111-222-333-1001"; + const scoped = buildWindowsTaskXml(undefined, undefined, undefined, sid); + const foreign = scoped.replaceAll(sid, "S-1-5-21-999-888-777-1002"); const unscoped = buildWindowsTaskXml(undefined, undefined, undefined, ""); - let identity: Readonly<{ name: string }> | null = null; + let identity: Readonly<{ sid: string; name: string }> | null = null; let resolutions = 0; const timeouts: number[] = []; const deps = { @@ -2141,7 +2217,7 @@ describe("service diagnostics", () => { resolvePrincipal: (timeoutMs: number) => { timeouts.push(timeoutMs); resolutions += 1; - identity = { name: "MACHINE\\installer" }; + identity = { sid, name: "MACHINE\\installer" }; return "*S-1-5-21-111-222-333-1001"; }, }; @@ -2151,7 +2227,7 @@ describe("service diagnostics", () => { schedulerXml: scoped, recordedBackend: "scheduler", }, deps); - expect(identity).toEqual({ name: "MACHINE\\installer" }); + expect(identity).toEqual({ sid, name: "MACHINE\\installer" }); expect(resolutions).toBe(1); expect(matching).toMatchObject({ viable: true, stale: false }); expect(deriveWindowsServiceDiagnosticForCurrentUser({ @@ -2174,7 +2250,7 @@ describe("service diagnostics", () => { ...base, schedulerXml: unscoped, recordedBackend: "scheduler", - }, deps)).toMatchObject({ viable: true, stale: false }); + }, deps)).toMatchObject({ viable: false, stale: true }); expect(resolutions).toBe(1); expect(timeouts).toEqual([30_000]); }); @@ -2201,7 +2277,7 @@ describe("service diagnostics", () => { ...base, schedulerXml: unscoped, recordedBackend: "scheduler", - }, deps)).toMatchObject({ viable: true, stale: false }); + }, deps)).toMatchObject({ viable: false, stale: true }); expect(resolutions).toBe(1); }); @@ -2426,6 +2502,57 @@ describe("service repair", () => { expect(calls).toEqual(["env", "auth", "stop", "assets", "reregister", "start", "state"]); }); + test("repair migrates an exact legacy account name to the preferred SID", async () => { + const calls: string[] = []; + const sid = "S-1-5-21-111-222-333-1001"; + const name = "MACHINE\\installer"; + const legacyNameXml = buildWindowsTaskXml(undefined, undefined, undefined, name); + let attemptNonce = ""; + + expect(windowsTaskRegistrationHealthy(legacyNameXml, undefined, undefined, [sid, name])).toBe(true); + await repairService({ + platform: "win32", + diagnose: () => baseDiag, + assertEnv: () => {}, + assertAuth: () => {}, + resolveExpectedUserId: () => [sid, name], + stopScheduler: () => { calls.push("stop"); }, + writeSchedulerAssets: () => { calls.push("assets"); }, + readSchedulerXml: () => attemptNonce + ? buildWindowsTaskXml(undefined, undefined, attemptNonce, sid) + : legacyNameXml, + reregisterScheduler: async nonce => { calls.push("reregister"); attemptNonce = nonce; }, + startScheduler: () => { calls.push("start"); }, + writeSchedulerState: () => { calls.push("state"); }, + }); + + expect(calls).toEqual(["stop", "assets", "reregister", "start", "state"]); + }); + + test("repair preserves a mangled legacy path instead of adopting it", async () => { + const calls: string[] = []; + const wscript = "C:\\Windows\\System32\\wscript.exe"; + const expectedLauncher = "C:\\Users\\김병준\\.opencodex\\service-launcher.vbs"; + const reportedLauncher = "C:\\Users\\???\\.opencodex\\service-launcher.vbs"; + const legacy = buildWindowsTaskXml("ignored.cmd", reportedLauncher, undefined, TEST_WINDOWS_TASK_SID) + .replace(/.*?<\/Command>/, `${wscript}`) + .replace(/[\s\S]*?<\/SessionStateChangeTrigger>\s*/gi, ""); + + await expect(repairService({ + platform: "win32", + diagnose: () => baseDiag, + assertEnv: () => {}, + assertAuth: () => {}, + schedulerWscript: wscript, + schedulerLauncher: expectedLauncher, + resolveExpectedUserId: () => TEST_WINDOWS_TASK_SID, + readSchedulerXml: () => legacy, + stopScheduler: () => { calls.push("stop"); }, + reregisterScheduler: async () => { calls.push("reregister"); }, + })).rejects.toThrow(/preserved for manual review/i); + expect(calls).toEqual([]); + }); + test("repair leaves a healthy registration alone", async () => { const calls: string[] = []; await repairService({ @@ -3142,9 +3269,79 @@ describe("service serving confirmation", () => { now: () => 0, timeoutMs: 0, }); + // Exactly one. "At least one" would also pass against a version that + // sleeps and knocks again, which is the opposite of what a zero budget + // asks for — #3039 relaxed this to toBeGreaterThanOrEqual and that is + // precisely the assertion the grace probe must not be allowed to satisfy. expect(probes).toBe(1); }); + // #3009: a Windows cold start does NTFS ACL hardening and previous-session + // journal recovery before the listener exists, so the service can bind + // seconds after the deadline and then stay healthy. `ocx service repair` + // reported that as a terminal failure with exit 1, and the caller's fallback + // is to start a second proxy against a port that is about to be taken. + test("accepts a service that binds during the grace after the deadline", async () => { + let now = 0; + let probes = 0; + const out = await confirmServiceServing({ + port: 10100, + // Answers only once the clock is past the deadline, which is the shape + // the report describes: healthy, just not within the budget. + probe: async () => { probes += 1; return now > 2_000; }, + sleep: async ms => { now += ms; }, + now: () => now, + timeoutMs: 2_000, + }); + expect(out).toEqual({ ok: true, port: 10100 }); + expect(probes).toBeGreaterThan(1); + }); + + test("still fails a service that never binds", async () => { + let now = 0; + const out = await confirmServiceServing({ + port: 10100, + probe: async () => false, + sleep: async ms => { now += ms; }, + now: () => now, + timeoutMs: 2_000, + }); + expect(out).toEqual({ ok: false, port: 10100 }); + }); + + // Windows is the platform the extra budget exists for; everything else keeps + // the original 20s so this cannot slow a healthy Linux install down. Pinned + // absolutely, not relatively: a relational assertion accepts 21s, and the + // reported service bound past 20s, so the number is the contract. + test("gives Windows a longer cold-start budget than the other platforms", () => { + expect(serviceInstallHealthMs("win32")).toBe(SERVICE_INSTALL_HEALTH_WINDOWS_MS); + expect(SERVICE_INSTALL_HEALTH_WINDOWS_MS).toBe(45_000); + expect(serviceInstallHealthMs("linux")).toBe(SERVICE_INSTALL_HEALTH_MS); + expect(serviceInstallHealthMs("darwin")).toBe(SERVICE_INSTALL_HEALTH_MS); + }); + + test("reports the effective Windows failure budget", async () => { + const errors: string[] = []; + const previousError = console.error; + const previousExitCode = process.exitCode; + let now = 0; + console.error = (...values: unknown[]) => { errors.push(values.join(" ")); }; + try { + await reportServiceServing("repaired", { + port: 10100, + probe: async () => false, + sleep: async ms => { now += ms; }, + now: () => now, + timeoutMs: SERVICE_INSTALL_HEALTH_WINDOWS_MS, + }); + expect(errors.join("\n")).toContain("within 45s"); + expect(errors.join("\n")).not.toContain("within 20s"); + } finally { + console.error = previousError; + process.exitCode = previousExitCode ?? 0; + } + }); + // A service reinstall invalidates the pidfile, so resolving the target through // it (findLiveProxy) would report a serving service as dead. Ask the baked port. test("probes the port it was given rather than resolving one", async () => { diff --git a/tests/update-job.test.ts b/tests/update-job.test.ts index 318a777422..b77c3f42cb 100644 --- a/tests/update-job.test.ts +++ b/tests/update-job.test.ts @@ -755,6 +755,7 @@ describe("GUI update execution decisions", () => { // runService is then never called and this goes red. test("a non-elevated Windows update worker repairs the service instead of skipping it", async () => { const ranService: string[][] = []; + const serviceTimeouts: number[] = []; const spawned: Array<{ port: number }> = []; const job: UpdateJobState = { id: "svc-win-repair", @@ -779,8 +780,9 @@ describe("GUI update execution decisions", () => { serviceViableFn: () => true, waitForPort: async () => true, probeProxy: async () => true, - runService: (_j, _bin, args) => { + runService: (_j, _bin, args, timeoutMs) => { ranService.push(args); + serviceTimeouts.push(timeoutMs); return { status: 0 }; }, spawnStart: (_job, _installer, port) => { @@ -791,12 +793,46 @@ describe("GUI update execution decisions", () => { expect(ranService.length).toBe(1); expect(ranService[0]).toContain("repair"); expect(ranService[0]).not.toContain("install"); + expect(serviceTimeouts).toEqual([150_000]); } finally { if (prevService === undefined) delete process.env.OCX_SERVICE; else process.env.OCX_SERVICE = prevService; } }); + test("a timed-out Windows repair never starts a competing foreground proxy", async () => { + const spawned: number[] = []; + const job: UpdateJobState = { + id: "svc-win-timeout", + status: "restarting", + startedAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + currentVersion: "2.7.42", + latestVersion: "2.7.43", + channel: "latest", + installer: "npm", + restart: true, + command: "", + log: [], + releaseNotesUrl: "", + }; + writeFileSync(updateJobPath(job.id), JSON.stringify(job)); + + await expect(restartAfterUpdateForTests(job, { port: 19010, hostname: "127.0.0.1" }, { + platform: "win32", + serviceInstalledFn: () => true, + serviceViableFn: () => true, + waitForPort: async () => true, + runService: () => ({ status: null, signal: "SIGTERM", timedOut: true }), + spawnStart: (_job, _installer, port) => { spawned.push(port ?? 0); }, + probeProxy: async () => false, + })).rejects.toThrow(/state unknown.*refusing a competing direct start/i); + expect(spawned).toEqual([]); + const log = readUpdateJob(job.id)?.log.join("\n") ?? ""; + expect(log).toContain("refusing a competing direct start"); + expect(log).not.toContain("falling back to a direct proxy start"); + }); + test("service reinstall exit 0 with non-viable assets falls back to direct start", async () => { const spawned: Array<{ port: number }> = []; const job: UpdateJobState = { diff --git a/tests/windows-scheduler-install-verification.test.ts b/tests/windows-scheduler-install-verification.test.ts index bd50659439..29746effd1 100644 --- a/tests/windows-scheduler-install-verification.test.ts +++ b/tests/windows-scheduler-install-verification.test.ts @@ -13,6 +13,8 @@ import { windowsTaskRegistrationHealthy, } from "../src/service"; +const TEST_WINDOWS_TASK_SID = "S-1-5-21-111-222-333-1001"; + afterEach(() => { setQuerySchtasksForTests(null); }); @@ -27,6 +29,8 @@ describe("decodeSchtasksOutput", () => { const xml = buildWindowsTaskXml( "C:\\Users\\x\\.opencodex\\opencodex-service.cmd", "C:\\Users\\x\\.opencodex\\opencodex-service-launcher.vbs", + undefined, + TEST_WINDOWS_TASK_SID, ).replace(/.*?<\/Command>/, `${wscript}`); const utf16 = Buffer.from(`\uFEFF${xml}`, "utf16le"); const decoded = decodeSchtasksOutput(utf16); @@ -35,6 +39,7 @@ describe("decodeSchtasksOutput", () => { decoded, wscript, "C:\\Users\\x\\.opencodex\\opencodex-service-launcher.vbs", + TEST_WINDOWS_TASK_SID, )).toBe(true); // Sanity: the historical utf8 mis-decode is unhealthy. expect(windowsTaskRegistrationHealthy(utf16.toString("utf8"))).toBe(false); @@ -167,11 +172,11 @@ describe("formatWindowsSchedulerServiceStatus", () => { describe("evaluateWindowsSchedulerInstallVerification", () => { const wscript = "C:\\Windows\\System32\\wscript.exe"; const launcher = "C:\\Users\\Test\\.opencodex\\opencodex-service-launcher.vbs"; - const healthyXml = buildWindowsTaskXml("ignored.cmd", launcher) + const healthyXml = buildWindowsTaskXml("ignored.cmd", launcher, undefined, TEST_WINDOWS_TASK_SID) .replace(/.*?<\/Command>/, `${wscript}`); test("succeeds when task, registration, assets, and absent WinSW all hold", () => { - expect(windowsTaskRegistrationHealthy(healthyXml, wscript, launcher)).toBe(true); + expect(windowsTaskRegistrationHealthy(healthyXml, wscript, launcher, TEST_WINDOWS_TASK_SID)).toBe(true); const result = evaluateWindowsSchedulerInstallVerification({ taskInstalled: true, xml: healthyXml, @@ -179,6 +184,7 @@ describe("evaluateWindowsSchedulerInstallVerification", () => { nativeStatus: "nonexistent", wscript, launcher, + expectedUserId: TEST_WINDOWS_TASK_SID, }); expect(result).toMatchObject({ ok: true, @@ -198,6 +204,7 @@ describe("evaluateWindowsSchedulerInstallVerification", () => { nativeStatus: "stopped", wscript, launcher, + expectedUserId: TEST_WINDOWS_TASK_SID, }); expect(result.ok).toBe(false); expect(result.conflict).toBe(true); @@ -213,6 +220,7 @@ describe("evaluateWindowsSchedulerInstallVerification", () => { nativeStatus: "started", wscript, launcher, + expectedUserId: TEST_WINDOWS_TASK_SID, }); expect(result.ok).toBe(false); expect(result.conflict).toBe(true); @@ -226,6 +234,7 @@ describe("evaluateWindowsSchedulerInstallVerification", () => { nativeStatus: "unknown", wscript, launcher, + expectedUserId: TEST_WINDOWS_TASK_SID, }); expect(result.ok).toBe(false); expect(result.conflict).toBe(false); @@ -244,6 +253,7 @@ describe("evaluateWindowsSchedulerInstallVerification", () => { nativeStatus: "nonexistent", wscript, launcher, + expectedUserId: TEST_WINDOWS_TASK_SID, }); expect(result.ok).toBe(false); expect(result.registrationHealthy).toBe(false); @@ -259,6 +269,7 @@ describe("evaluateWindowsSchedulerInstallVerification", () => { nativeStatus: "nonexistent", wscript, launcher, + expectedUserId: TEST_WINDOWS_TASK_SID, }); expect(invalid.registrationHealthy).toBe(false); expect(invalid.registrationInvalid).toBe(true); @@ -286,6 +297,7 @@ describe("evaluateWindowsSchedulerInstallVerification", () => { nativeStatus: "nonexistent", wscript, launcher, + expectedUserId: TEST_WINDOWS_TASK_SID, }); expect(withData.registrationInvalid).toBe(true); expect(schedulerVerificationMaySettle(withData)).toBe(false); @@ -299,6 +311,7 @@ describe("evaluateWindowsSchedulerInstallVerification", () => { nativeStatus: "nonexistent", wscript, launcher, + expectedUserId: TEST_WINDOWS_TASK_SID, }); expect(result.ok).toBe(false); expect(result.assetsHealthy).toBe(false); From c8c8dc3387742c4efe98d1c7e0a1ed2d111d009b Mon Sep 17 00:00:00 2001 From: JUN Date: Tue, 1 Sep 2026 15:13:23 +0900 Subject: [PATCH 009/172] test(auth): close the startup-prime window that rotates the credential mid-fixture (#3139) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * docs(devlog): plan merge train round 3 Roadmap for landing the green PRs, retiring the superseded ones, and rebasing the rest, frozen at dev=132b557ad. Includes the round-1 audit synthesis: three blockers folded (fork PRs are carried by cherry-pick rather than force-pushed, because enforce-pr-target.yml applies the readiness checklist to authors without push permission; #3039's closure withdrawn because #3104 prints the configured budget where #3039 printed the elapsed wait; the src/service.ts overlap is 330470e74, not 0ef04e640) and two rebutted with evidence. * docs(devlog): record wp1 — #3114 landed as abcda8e13 * docs(devlog): record the wp2 security review for #3122 * docs(devlog): record wp3 — #3134 landed, #3128 flake premise corrected * docs(devlog): record wp5 — #3077 closed, #3109/#3112 rebased * docs(devlog): locate the websocket refresh flake, and correct the #3128 premise * docs(devlog): prove the flake mechanism and correct its direction * docs(devlog): mark the superseded flake explanation in the wp5 record * test(auth): install the fake clock and fetch stub before startServer startServer returns synchronously but arms an async pool-quota prime that outlives its return (src/server/index.ts:2054-2064). That prime calls getValidCodexToken, which can rotate the very credential these assertions read, and fetches a real host unless the stub is up. Both fixtures installed Date.now and globalThis.fetch AFTER startServer, leaving a window two dynamic import() resolutions wide where the prime ran against the real clock and real fetch. On a warm local module cache it resolved before the fixture finished; on a loaded CI runner it did not, and seenAuth[0] was already the rotated token. Measured rather than assumed: OPENCODEX_DEBUG_QUOTA=1 prints refreshed=1 on every run of both the fixed and unfixed trees, so the prime always fires. The fix does not suppress it -- it makes it run inside the fixture's controlled world. The thread-affinity test at :2131 had the identical shape and is fixed too. --- .../260901_merge_train_round3/000_plan.md | 95 +++++++++ .../002_audit_round1_synthesis.md | 141 +++++++++++++ .../010_wp1_3114_docs_devlog.md | 43 ++++ .../011_wp1_outcome.md | 45 +++++ .../020_wp2_3122_provider_patch_fakeip.md | 64 ++++++ .../021_wp2_security_review.md | 95 +++++++++ .../030_wp3_3104_service_and_closeouts.md | 98 +++++++++ .../031_wp3_outcome.md | 89 +++++++++ .../040_wp4_3042_pid_probe.md | 58 ++++++ .../050_wp5_close_3077_rebase_3109_3112.md | 78 ++++++++ .../051_wp5_outcome.md | 63 ++++++ .../060_wp7_websocket_refresh_flake.md | 187 ++++++++++++++++++ tests/server-auth.test.ts | 31 ++- 13 files changed, 1078 insertions(+), 9 deletions(-) create mode 100644 devlog/_plan/260901_merge_train_round3/000_plan.md create mode 100644 devlog/_plan/260901_merge_train_round3/002_audit_round1_synthesis.md create mode 100644 devlog/_plan/260901_merge_train_round3/010_wp1_3114_docs_devlog.md create mode 100644 devlog/_plan/260901_merge_train_round3/011_wp1_outcome.md create mode 100644 devlog/_plan/260901_merge_train_round3/020_wp2_3122_provider_patch_fakeip.md create mode 100644 devlog/_plan/260901_merge_train_round3/021_wp2_security_review.md create mode 100644 devlog/_plan/260901_merge_train_round3/030_wp3_3104_service_and_closeouts.md create mode 100644 devlog/_plan/260901_merge_train_round3/031_wp3_outcome.md create mode 100644 devlog/_plan/260901_merge_train_round3/040_wp4_3042_pid_probe.md create mode 100644 devlog/_plan/260901_merge_train_round3/050_wp5_close_3077_rebase_3109_3112.md create mode 100644 devlog/_plan/260901_merge_train_round3/051_wp5_outcome.md create mode 100644 devlog/_plan/260901_merge_train_round3/060_wp7_websocket_refresh_flake.md diff --git a/devlog/_plan/260901_merge_train_round3/000_plan.md b/devlog/_plan/260901_merge_train_round3/000_plan.md new file mode 100644 index 0000000000..6fe65d26da --- /dev/null +++ b/devlog/_plan/260901_merge_train_round3/000_plan.md @@ -0,0 +1,95 @@ +# 000 — merge train round 3: land the green, retire the superseded, rebase the rest + +Frozen at `dev` = `132b557ad` (2.40.0), 2026-09-01T03:30Z. 55 open PRs, 47 open issues. + +## Objective + +Land the pull requests whose disposition requires no maintainer judgment, close the ones a +landed reimplementation has already absorbed, and leave the remaining maintainer-authored +PRs rebased onto current `dev` so their next review round reads a live head. + +This train does **not** implement anything. Every production line it moves is a line some +other PR already wrote and some other reviewer already read. + +## The state that makes this train possible + +Three landings in the last hour changed what "red" means on this backlog: + +| commit | what it changed | +| --- | --- | +| `33d32b6a3` (#3128) | pinned the WebSocket refresh account — the `server local API auth > websocket passthrough refreshes pool auth for each response.create turn` flake | +| `3e0f99a19` (#3127) | moved `dev` to 2.40.0 after the v2.39.0 release | +| `6f415baef` (#3129) | made the dev version bump actually fire | + +Both of those are why the four candidates below currently show a red matrix, and neither red +is about the change under review: + +- **#3104, #3109, #3112** are red on the `server-auth` WebSocket assertion. `070_outcome.md` + of `260901_release_train_2390` diagnosed it: the credential is saved with + `expiresAt: now + 120_000` against a `REFRESH_SKEW_MS` of `60_000`, and `startServer(0)` + runs before `Date.now` is pinned, so the first turn can land on the wrong side of the + skew boundary and refresh early. #3128 fixed it. Any head that predates #3128 still shows it. +- **#3122** is red on `release version line > the in-tree version is never behind a released one`. + Its base predates the 2.40.0 bump, so the in-tree version is behind the published 2.39.0. + Rebasing onto `132b557ad` is the whole fix. + +**Therefore: no candidate is judged on a pre-rebase matrix.** Every merge in this train waits +for a green matrix on a head rebased onto `132b557ad` or later. + +## Work-phase map (dependency-ordered) + +``` +wp0 roadmap (this unit) + ├── wp1 #3114 docs-only, no production surface → 010 + ├── wp2 #3122 provider PATCH validation exception → 020 + ├── wp3 #3104 service budget + scheduler ownership → 030 (+ closes #3009 #3064 #3039 #3067) + ├── wp4 #3042 test-only pid probe → 040 + └── wp5 #3077 close, #3109/#3112 rebase → 050 +``` + +The order is blast-radius ascending, which here coincides with dependency order: wp1 touches no +code, wp2 touches one validation call site, wp3 touches `src/service.ts` and is the only phase +that closes issues, wp4 touches tests only, wp5 touches no `dev` state at all. wp1-wp5 are +independent of each other and depend only on wp0; they are sequenced rather than parallel +because each merge invalidates the next candidate's merge base. + +## Scope boundary + +**IN** + +- Merging #3114, #3122, #3104, #3042 into `dev` after an exact-head green matrix. +- Closing #3039, #3067 (absorbed by #3104), #3077 (stale wrong-branch bump). +- Rebasing #3109 and #3112 onto current `dev` and force-pushing their branches. +- Dropping `926a8d8c4` from #3109 — the same change landed as #3128. +- Closing #3009 and #3064 when #3104 lands. + +**OUT** + +- **#3117.** It reverses a direction `b46164e78` (#3100) deliberately pinned one day earlier: + "A configured id the provider no longer lists must not be retained on the strength of a + format match alone; #1690 is the explicit opt-in for that." Landing #3117 is a policy + decision about #1690, not a merge-train mechanical. +- **#3061.** `CHANGES_REQUESTED` with a substantive rebuttal: the 90 s budget reproduced the + same failure, so the ceiling was not the only failure mode. +- **Re-implementing the review blockers on #3109/#3112.** Those are real and unresolved; + this train rebases them and stops. +- Any `main`/`preview` promotion, npm publish, or release. +- Any new production logic. + +## Verifier + +`gh pr checks ` on the exact head, requiring every non-skipped check to pass. Run against +the post-rebase head only. Local full suite is prohibited by the operator; focused local checks +are permitted where a rebase produced a textual conflict that needs resolving. + +Verified before adoption: `gh pr checks 3122` exits non-zero today and names +`release version line`, and `gh run view --job 99726180475 --log-failed` shows exactly that +one assertion. The command reads the change target because it reports the check suite bound +to the PR's head SHA. + +## Terminal outcomes + +- `DONE` — four merges landed, three closes recorded, two branches rebased and pushed. +- `BLOCKED` — a specific PR whose CI fails three consecutive times for an infrastructure + reason; report it and continue the others. +- Partial completion is reported per work-phase, never averaged into a single claim. diff --git a/devlog/_plan/260901_merge_train_round3/002_audit_round1_synthesis.md b/devlog/_plan/260901_merge_train_round3/002_audit_round1_synthesis.md new file mode 100644 index 0000000000..a9c645e2f9 --- /dev/null +++ b/devlog/_plan/260901_merge_train_round3/002_audit_round1_synthesis.md @@ -0,0 +1,141 @@ +# 002 — audit round 1: what the reviewer caught, and what it over-read + +Reviewer: `gpt-5.6-sol`/high, read-only lane, `VERDICT: FAIL` with 5 blockers. +Three are real and change the plan. Two are rebutted with evidence. This document records +both dispositions because a rebuttal I do not write down is a rebuttal the next round +re-litigates. + +## Accepted — blocker 5: the conflict attribution was wrong, and so were the distances + +The reviewer is right and I checked it myself. + +``` +$ git show 0ef04e640 --stat | tail -4 + src/cli/dispatch.ts | 11 ++++++-- + src/cli/index.ts | 9 +++++- + tests/cli-dispatch.test.ts | 69 ++++++++++++++++++++++++++++++++++++++++++++++ +``` + +`0ef04e640` never touches `src/service.ts`. I named it because its subject line +("stop start shadowing a live configured-port proxy") reads like service territory. That is +reasoning from a commit message instead of from a diff, which is exactly the error the +audit exists to catch. + +The real dev-side overlap is `330470e74` (#3118, `fix(stop): typed stop outcome`) — 50 files, +and `src/service.ts` is among them. Measured overlap for the four rebase candidates: + +| PR | branch | files changed on BOTH sides since merge-base | +| --- | --- | --- | +| #3104 | `codex/3009-windows-cold-start` | `src/service.ts` | +| #3109 | `codex/3063-combo-compact-failover` | `src/adapters/openai-responses.ts`, `tests/server-auth.test.ts` | +| #3112 | `codex/2999-native-main-refresh-claim` | **none** | +| #3042 | `fix/test-dead-pid-probe` | `tests/responses-state.test.ts` | + +`030` is amended: expect `src/service.ts` against `330470e74`, not `0ef04e640`. + +The behind-counts in `010`/`020`/`040`/`050` were measured before `132b557ad`, `33d32b6a3`, +`3e0f99a19` and `6f415baef` landed during this session. They are stale by exactly the number +of commits that landed while I was writing. Real distances from `132b557ad`: #3114 = 26, +#3122 = 5, #3042 = 59, #3109 = 27, #3112 = 26. Recorded here rather than chased through five +documents, since the number moves again on every merge this train performs. + +## Accepted — blocker 4: #3104 drops a behaviour #3039 authored + +Verified in both trees. + +``` +$ git show pr3039:src/service.ts | sed -n '742,753p' + const startedAt = elapsed(); + ... + + `${Math.max(1, Math.round((elapsed() - startedAt) / 1000))}s.\n` + +$ git show pr3104:src/service.ts | sed -n '742,750p' + const healthBudgetMs = deps.timeoutMs ?? serviceInstallHealthMs(); + ... + + `${Math.trunc(healthBudgetMs / 1000)}s.\n` +``` + +#3039's comment states the intent plainly: "The elapsed time, not the constant: a caller +that passes its own timeoutMs used to be told it had waited 20s whatever it waited." +#3104 prints the budget. Since #3104 also adds a post-deadline grace knock +(`src/service.ts:719`), the printed number can now understate the real wait — the exact +failure mode #3039 set out to fix, reintroduced by the PR that claims to supersede it. + +This does not block **merging** #3104: the budget message is honest about the budget, and +the security-relevant half (SID-exact scheduler ownership) is unaffected. It blocks +**closing #3039 as fully superseded**. Amended in `030`: #3039 stays open with a comment +recording precisely which contribution was not carried, so the elapsed-time diagnostic is a +tracked follow-up rather than a silent drop. + +## Accepted — blocker 3: a maintainer push resets a contributor PR's readiness + +`.github/workflows/enforce-pr-target.yml:740-746`: + +``` +// The readiness gate applies to contributors (no push permission). +const checklistRequired = !authorIsMaintainer; +``` + +and `:781-786` — "A completed checklist is an attestation about a specific head" — with the +push resetting the boxes and re-drafting. + +Both fork candidates are contributors: + +``` +$ gh api repos/lidge-jun/opencodex/collaborators/Flowershangfromthebranches/permission --jq .permission +read +$ gh api repos/lidge-jun/opencodex/collaborators/lifrary/permission --jq .permission +read +``` + +So a maintainer force-push to #3122 or #3042 re-drafts the PR and resets a checklist only +the author can tick. "Rebase, wait for green, merge" is not available for either. + +**This is the blocker that reshapes the train**, and it is not a paperwork objection: the +gate exists so an author attests that the code they are shipping is the code that was +tested. Amendment: fork PRs are landed by **cherry-picking onto a maintainer branch** with +authorship preserved (`git cherry-pick -x`, original `Author:` intact), opened as a +maintainer PR that credits and closes the original — the pattern this repository already +uses (#3104 carries #3039/#3067; #3109 carries #3063; #3111 carries #2989). The contributor +keeps authorship in `git log`; the readiness gate is satisfied by a maintainer author rather +than circumvented. + +## Rebutted — blocker 2: the security-notes rule does not reach this material + +The reviewer reads `AGENTS.md:105-127` as forbidding any devlog note that touches an +unfixed defect. That is broader than the rule, which is scoped to **security** work: +"unreleased findings, severity assessments, draft advisories, exploit or bypass reasoning, +reproduction steps for an unfixed defect, and pre-disclosure patch plans." + +The test the file gives is explicit: "is there already a public diff that reveals this +weakness?" + +- **#3122** is characterised as "an unshipped destination-policy/SSRF fix". It is not an + SSRF fix. The PR permits the `198.18.0.0/15` fake-IP range on the provider PATCH path + that creation and re-enable already permit — it **relaxes** a validator to match its own + sibling call sites, and the asymmetry is visible in the open PR diff. There is no + weakness disclosed that the public PR does not already show. +- **#3112's** three failure modes are quoted from `Ingwannu`'s **public review** on the open + PR. Restating a public review comment in a devlog discloses nothing. +- **#3114's `070_outcome.md`** discusses #3000's musl `dlopen` and late-cancel grant + discard. #3000 is `CLOSED` (2026-08-31T19:13:28Z) and was never merged — the code never + shipped, so there is no deployed weakness to disclose. The note explains why a PR was + rejected, which is the closure rationale, not an advisory. + +There is one thing the reviewer is right about even though the blocker is wrong: **read the +#3114 unit before merging it** rather than approving it because it is docs-only. `010` +already required that. The read stays; the blocker is not accepted. + +## Rebutted — blocker 1: no staged diff + +The reviewer required a staged index to anchor its audit. That is a habit from reviewing a +patch, not a rule of this repository, and this phase is a P-phase plan audit — the artifact +is the six documents, which the reviewer read and cited by line. Nothing is staged because +nothing is committed yet; `git add` before an audit would not have changed a single blob it +examined. Recorded and dismissed. + +## Round verdict + +`GO-WITH-FIXES` after amendment: blockers 3, 4, 5 folded into `020`/`030`/`040`; blockers +1 and 2 rebutted with evidence above. The train's shape changes in one material way — fork +PRs are carried, not force-pushed — and one closure is withdrawn. diff --git a/devlog/_plan/260901_merge_train_round3/010_wp1_3114_docs_devlog.md b/devlog/_plan/260901_merge_train_round3/010_wp1_3114_docs_devlog.md new file mode 100644 index 0000000000..1136561df8 --- /dev/null +++ b/devlog/_plan/260901_merge_train_round3/010_wp1_3114_docs_devlog.md @@ -0,0 +1,43 @@ +# 010 — wp1: land #3114 (docs devlog, 8/31 non-priority-70 triage round) + +PR #3114, author `lidge-jun`, branch `codex/triage-round-devlog-pr`, label `documentation`. +`+820 −0` across 6 files, all under `devlog/_plan/`. + +## Why this is mechanical + +Nothing in the build, typecheck, or test path reads `devlog/` (`AGENTS.md`, "The `devlog` +directory"). The check suite agrees: on head `d6330f7c` every heavy job reports `skipping` +— `gates`, `macos`, `test ${{ matrix.shard }}/4`, `storage policy`, `api usage`, +`keyring`, `npm-global` — and the five that run (`ci`, `changes`, `hygiene`, +`enforce-target`, `react-doctor`, `label`, `resolve-pr`) all pass. + +`privacy:scan` does read `devlog/`, and `hygiene` passes, which is the gate that matters +for a public devlog. + +## Pre-merge check + +The unit records a triage round that is already closed. Confirm before merging that it +contains no pre-disclosure security material (`AGENTS.md`, "Security working notes"): the +test is whether a public diff already reveals each weakness named. The round's dispositions +are PR closes and supersessions, all visible in public git history. + +## Steps + +1. `git fetch origin` and confirm `origin/dev` = `132b557ad` or later. +2. `gh pr checks 3114` — every non-skipped check passes. +3. Read the six added files for security-note residue. +4. Approve, then `gh pr merge 3114 --squash`. `mergeStateStatus` is `BLOCKED` only for the + missing approval; no admin override should be needed. +5. `git fetch origin && git log --oneline -1 origin/dev` names #3114. + +## Rebase question + +Head `d6330f7c` sits 24 commits behind `dev`. A docs-only unit adding new files under a new +directory has no conflict surface, and `enforce-target`'s ancestry heuristic exempts authors +with push permission. Rebase only if GitHub reports a conflict. + +## Accept criteria + +- `origin/dev` contains the merge commit naming #3114. +- The six documents are present at `origin/dev`. +- No file outside `devlog/_plan/` changed. diff --git a/devlog/_plan/260901_merge_train_round3/011_wp1_outcome.md b/devlog/_plan/260901_merge_train_round3/011_wp1_outcome.md new file mode 100644 index 0000000000..79f04ea251 --- /dev/null +++ b/devlog/_plan/260901_merge_train_round3/011_wp1_outcome.md @@ -0,0 +1,45 @@ +# 011 — wp1 outcome: #3114 landed + +`abcda8e134d7e3222d72877fe83c77fcb492a821`, merged 2026-09-01T03:57:16Z, squash. +6 files / +820 lines, all under `devlog/_plan/260831_bug_triage_nonprio70/`. + +## Audit before approval + +`010` required reading the unit rather than waving it through because it is docs-only. +Four checks, all clean: + +| check | result | +| --- | --- | +| credential/identifier scan over the full diff | zero hits | +| `bun run privacy:scan` | Privacy scan passed | +| `bun test tests/repo-hygiene.test.ts` | 12 pass / 0 fail | +| pre-disclosure test on the one security-adjacent passage | cleared | + +The fourth is the one that mattered. `070_outcome.md:213-216` describes #3000's +`libc.so.6` `dlopen` — which throws on musl, so credential publication would fail on +Alpine — and its `signal.aborted` check placed before `persistRefreshedMainAuthJson`, +discarding a grant the provider already rotated. `AGENTS.md` asks whether a public diff +already reveals the weakness. #3000 is `CLOSED` (2026-08-31T19:13:28Z) and was never +merged: the code never shipped, so there is no deployed weakness. The passage is a closure +rationale, and closure rationales are exactly what a `_fin`-bound record is for. + +The repository answers this question mechanically too, and it agrees: +`tests/repo-hygiene.test.ts` asserts `no open devlog plan carries an unresolved security +verdict`, and it passes against the merged tree. + +## Admin merge, and why + +`gh pr review 3114 --approve` is refused by GitHub: *"Can not approve your own pull +request."* `dev` carries a ruleset requiring a reviewed pull request. A self-authored PR +therefore has no non-admin route, and this train was explicitly authorized to use one. + +Worth stating plainly rather than burying: **admin merge is not review.** What stands in +for review here is the audit above, and it is weaker than a second pair of eyes would be. +For a 6-file docs-only change whose two mechanical gates both pass, that trade is +defensible. It would not be for the three code PRs later in this train. + +## Residual + +The remote branch was deleted; the local `codex/triage-round-devlog-pr` survives because +worktree `/Users/jun/.codex/worktrees/2a44/opencodex` still has it checked out. Left alone — +that worktree is not this train's to disturb. diff --git a/devlog/_plan/260901_merge_train_round3/020_wp2_3122_provider_patch_fakeip.md b/devlog/_plan/260901_merge_train_round3/020_wp2_3122_provider_patch_fakeip.md new file mode 100644 index 0000000000..69226a8f88 --- /dev/null +++ b/devlog/_plan/260901_merge_train_round3/020_wp2_3122_provider_patch_fakeip.md @@ -0,0 +1,64 @@ +# 020 — wp2: land #3122 (canonical fake-IP addresses on provider PATCH) + +PR #3122, author `Flowershangfromthebranches` (fork, `maintainerCanModify = true`), +branch `fix/openai-patch-fake-ip`, labels `bug` + `review-ready`. `+150 −1` across 2 files: +`src/server/management/provider-routes.ts` and `tests/management-provider-validation.test.ts`. + +## The defect + +Canonical OpenAI provider **creation** and **re-enable** already pass +`allowBenchmarkAddresses`, which permits the `198.18.0.0/15` range that Clash/Mihomo-style +fake-IP DNS returns. The ordinary field-mask **PATCH** path did not pass the same exception, +so a provider that was created successfully rejected a later context-window PATCH against +the identical address. + +One call site, one flag, and the asymmetry is the whole bug. The test file is the larger half +of the diff. + +## Why the matrix is red, and why it is not this change + +`gh run view --job 99726180475 --log-failed` on head `f463e124`: + +``` +(fail) release version line > the in-tree version is never behind a released one [74.60ms] +1 tests failed: +``` + +That assertion compares the in-tree `package.json` version against the published release +line. The head's base predates `3e0f99a19` (#3127, "move dev to 2.40.0 after the v2.39.0 +release"), so the in-tree 2.39.0 is exactly level with — and by the gate's reading, behind — +the released 2.39.0. It is unrelated to provider validation and disappears on rebase. + +The branch is 3 commits behind `dev`, so this is a short rebase. + +## Amended by audit round 1 (blocker 3): carry, do not force-push + +`maintainerCanModify` is true, so a force-push is technically available. It is the wrong +move. `.github/workflows/enforce-pr-target.yml:740-746` applies the readiness checklist to +authors without push permission, and `Flowershangfromthebranches` has `read`. A maintainer +push re-drafts the PR and resets four boxes only the author can tick — the train would strand +the PR in draft, waiting on a contributor, having done the work. + +So this lands the way this repository already lands contributor work (#3104 carries +#3039/#3067, #3109 carries #3063, #3111 carries #2989): **cherry-pick onto a maintainer +branch with authorship preserved.** + +## Steps + +1. `git checkout -b codex/3122-provider-patch-fake-ip origin/dev`. +2. `git cherry-pick -x f463e124` — `-x` records the source commit; the original + `Author:` line is preserved by cherry-pick without further flags. +3. `git show --format='%an <%ae>' -s` to prove the authorship survived. +4. Push the maintainer branch and open a PR against `dev` that credits + @Flowershangfromthebranches, links #3122, and fills the PR template. +5. Wait for the full matrix. `release version line` must pass — that assertion is the entire + reason the original head is red, and a rebased base is the fix. If it still fails, stop: + the diagnosis is wrong. +6. Merge, then close #3122 with a comment naming the merged commit. + +## Accept criteria + +- Carrier head's matrix fully green, including `macos` and all four `test` shards. +- `git log origin/dev` shows the commit authored by the original contributor. +- The diff at `dev` is still 2 files. +- #3122 closed with credit, not merged-and-forgotten. diff --git a/devlog/_plan/260901_merge_train_round3/021_wp2_security_review.md b/devlog/_plan/260901_merge_train_round3/021_wp2_security_review.md new file mode 100644 index 0000000000..dc5f05e1ce --- /dev/null +++ b/devlog/_plan/260901_merge_train_round3/021_wp2_security_review.md @@ -0,0 +1,95 @@ +# 021 — wp2 security review: #3122 passed, and what the review actually established + +Reviewer: `gpt-5.6-sol`/high, independent read-only lane. `VERDICT: PASS`, zero blockers. + +`020` called this change mechanical. It is not — it is a caller of +`src/lib/destination-policy.ts`, the SSRF/destination guard, so `AGENTS.md`'s +"Security boundary (highest priority)" applies. The escalation was recorded at P and the +A phase dispatched a reviewer instead of the maintainer read that sufficed for wp1's +docs-only unit. + +## The question that mattered + +The packet's highest-value question was whether `next` is a **partial field mask** or the +**merged provider**. If partial, an attacker could PATCH a subset of fields so that +`isCanonicalOpenAiForwardProvider(next)` returns true while the effective stored provider +is not canonical — an escalation the POST path does not have. + +It is merged, and the code says so twice: + +``` +src/server/management/provider-routes.ts:114-121 + const next: OcxProviderConfig = { ...provider }; + +src/server/management/provider-routes.ts:737-739 (the route's own comment) + // Field-mask editor: apply recognized fields onto a copy, then validate the MERGED + // provider (canonical-seed guard covers openai; ...) +``` + +I verified this myself before accepting the reviewer's answer, which is the point of +asking a question whose answer is checkable in one file. + +The three predicate fields (`adapter`, `authMode`, `baseUrl`) *are* PATCH-writable +(`:133-161`), so the predicate is attacker-influenced — but influencing it requires making +the effective provider genuinely canonical, and the merged object must clear canonical seed +validation at `src/server/auth-cors.ts:560-593` before the DNS probe runs. Gaining the +exception and being the provider the exception exists for are the same act. + +## Blast radius + +The exception admits only DNS answers in `198.18.0.0/15` +(`src/lib/destination-policy.ts:49-68`), in three wrapped forms: IPv4-mapped +(`:155-167`), NAT64 `64:ff9b::/96` with a benchmark embedded quad (`:169-181`), and the +explicit-zero `::ffff:0:` spelling, again only when the embedded address is itself +benchmark space (`:125-145`). + +Everything dangerous stays closed, and the mechanism is one line: + +``` +src/lib/destination-policy.ts:339-349 + if (options?.allowBenchmarkAddresses && isBenchmarkDnsAnswer(address, assessment)) { + continue; + } + if (assessment.kind === "metadata") return `... blocked metadata endpoint ...`; + return `... resolves to a ${assessment.detail} ...`; +``` + +The loop skips **individual** benchmark answers; every other non-public answer misses the +`continue` and returns an error immediately. So loopback (`:54`), RFC1918 and CGNAT +(`:55-56`), metadata `169.254.169.254` (`:12-16`), IPv6 loopback/private/link-local +(`:183-199`), and any mixed answer set all still fail closed with the flag on. Literal +benchmark URLs also stay refused, because synchronous validation runs before DNS handling +(`:321-330`) and the exception is consulted only for resolved answers. + +## What makes this landable rather than merely plausible + +The tests catch **both** boolean mutations, which is the difference between a test that +documents a flag and one that pins it: + +| mutation | fails | +| --- | --- | +| flag hardcoded `true` | `PATCH destination benchmark exception stays scoped to the canonical openai row` (`:2508-2566`) | +| flag hardcoded `false` | `canonical OpenAI PATCH passes allowBenchmarkAddresses into destination resolution` (`:2465-2506`) and `canonical OpenAI PATCH still rejects non-benchmark private destination answers` (`:2569-2606`) | + +A guard whose tests only catch one direction is a guard that can silently widen. + +## Corrections to `020` + +- `020` cited `:512-513` as the POST path. It is the provider **reload** path; POST is + `:588-589`. Both carry the same guard, so the substance — that PATCH was the odd one out + — is unchanged. +- The reviewer notes that passing `{ allowBenchmarkAddresses: false }` is behaviourally + identical to omitting the option, since the policy branches only on a truthy flag + (`:339-345`). Nothing to fix; worth knowing before someone "simplifies" the call. + +## Carry + +Cherry-picked as `4a3b4235b` onto `origin/dev` = `abcda8e13`, authorship preserved: + +``` +4a3b4235b Flowershangfromthebranches <152056395+Flowershangfromthebranches@users.noreply.github.com> +``` + +Focused verification on the carry: +`bun test tests/management-provider-validation.test.ts tests/destination-policy-resolved.test.ts` +-> 129 pass / 0 fail / 635 expect(). diff --git a/devlog/_plan/260901_merge_train_round3/030_wp3_3104_service_and_closeouts.md b/devlog/_plan/260901_merge_train_round3/030_wp3_3104_service_and_closeouts.md new file mode 100644 index 0000000000..91b80cdb16 --- /dev/null +++ b/devlog/_plan/260901_merge_train_round3/030_wp3_3104_service_and_closeouts.md @@ -0,0 +1,98 @@ +# 030 — wp3: land #3104, then close #3009, #3064, #3039, #3067 + +PR #3104, author `lidge-jun`, branch `codex/3009-windows-cold-start`, label `bug`, +**APPROVED** by `Ingwannu` on exact head `4f691cddc252a13c5bea73cb9f5fbb1b5728521a`. +`+508 −66` across 5 files. Seven commits: + +``` +7b1b4ce1 fix(service): give a Windows cold start room to bind, without loosening… +b727f8f8 fix(service): forgive only what the code page mangled in a scheduler … +188e6186 fix(service): bind scheduler recovery to exact SID +4d83513b fix(service): fail closed on ambiguous scheduler ownership +97efe6f4 test(service): lock scheduler ownership guards +b29389a8 test(service): scope scheduler verification fixtures +4f691cdd test(service): exercise scheduler ownership oracles +``` + +## What it carries + +Two issues, one file (`src/service.ts`), stacked deliberately because a conflicting pair +would have been more expensive to review than one sequence. + +**#3009 — Windows cold start.** `confirmServiceServing` had a fixed 20 s deadline and +returned the moment the clock passed it. A Windows cold start does NTFS ACL hardening and +previous-session journal recovery before the listener exists, so a service that bound a few +seconds late and then stayed healthy was reported as a terminal failure with exit 1 — and the +caller's fallback starts a second proxy against a port that is about to be taken. Windows gets +45 s; nothing else changes. The zero-budget `expect(probes).toBe(1)` assertion that #3039 +relaxed to `toBeGreaterThanOrEqual(1)` is restored, because "at least one" passes against +exactly the version it exists to forbid. + +**#3064 — non-ASCII profile path.** `schtasks /query /xml` converts through the console code +page before the bytes exist, so reading as a buffer cannot recover them. A profile named +outside that page returns `C:\Users\???\...` and the exact comparison rejected a +registration this process had just created. The narrowing matters: #3067 compiled every +unrepresentable run to `[^\\/]*`, which forbids a separator but allows arbitrary ASCII, so a +wholly non-ASCII segment loses every anchor and +`C:\Users\\.opencodex\service-launcher.vbs` matches +`C:\Users\Admin\.opencodex\service-launcher.vbs` — this process would then adopt, repair or +delete another account's task. Here an unrepresentable run matches only a run of substitution +characters, and every ASCII segment including every separator is matched literally. + +## Approval is bound to a head that must change + +The branch is ~27 commits behind `dev`, and its `macos` job is red on the `server-auth` +WebSocket assertion that #3128 fixed. So the approval on `4f691cdd` cannot be spent as-is: +rebasing moves the head, which invalidates it. + +That is the correct outcome, not an obstacle to route around. The rebase is onto a `dev` that +has moved 27 commits, including `0ef04e640` (`fix(cli): stop start shadowing a live +configured-port proxy`) which is adjacent CLI/service territory. Re-review the rebased head +rather than treating the pre-rebase approval as transferable. + +## Steps + +1. Rebase `codex/3009-windows-cold-start` onto `origin/dev`. **Amended by audit round 1 + (blocker 5):** the dev-side overlap in `src/service.ts` is `330470e74` (#3118), not + `0ef04e640` — that commit touches only `src/cli/dispatch.ts`, `src/cli/index.ts` and + `tests/cli-dispatch.test.ts`. `src/service.ts` is the sole file changed on both sides. +2. `git range-diff` to prove all seven commits survived and no content changed beyond + conflict resolution. +3. If a conflict was resolved, run `bun test tests/service.test.ts` locally — a focused check, + permitted, and the file the PR's own evidence names (187 pass / 0 fail). +4. Force-push, wait for the full matrix. +5. Merge once green. Re-approval on the new head is required by the ruleset. +6. Close #3009 and #3064 manually — PRs here target `dev`, so GitHub's auto-close on + `Closes #` does not fire (`AGENTS.md`, "Issues and pull requests"). +7. Close **#3067** with a credit comment naming @ntdatt812, the merged commit, and what + changed: the unsafe `[^\\/]*` wildcard and the lossy `UserId` comparison were replaced by + substitution-only path matching and SID-exact ownership (`src/service.ts:2018-2052` on + the PR head). +8. **Do not close #3039.** See below. + +## Amended by audit round 1 (blocker 4): #3039 is not fully superseded + +#3104 does not carry everything #3039 authored. The diagnostic message differs: + +``` +#3039 src/service.ts:742-753 Math.max(1, Math.round((elapsed() - startedAt) / 1000)) +#3104 src/service.ts:742-750 Math.trunc(healthBudgetMs / 1000) +``` + +#3039's own comment states the intent: "The elapsed time, not the constant: a caller that +passes its own timeoutMs used to be told it had waited 20s whatever it waited." #3104 prints +the configured budget instead. Because #3104 also adds a post-deadline grace knock +(`src/service.ts:719`), the printed number can now understate the real wait — reintroducing +the exact defect #3039 fixed, inside the PR that claims to supersede it. + +This does not block merging #3104: the budget message is not wrong about the budget, and the +ownership hardening is untouched. It blocks the closure. #3039 stays open with a comment +recording which contribution was not carried, so the elapsed-time diagnostic is a tracked +follow-up rather than a silent drop by a train that promised no judgment calls. + +## Accept criteria + +- Rebased head fully green including `macos`. +- `origin/dev` contains all seven commits' content. +- #3009, #3064 closed; #3067 closed with credit. +- #3039 **open**, with a comment naming the uncarried elapsed-time diagnostic. diff --git a/devlog/_plan/260901_merge_train_round3/031_wp3_outcome.md b/devlog/_plan/260901_merge_train_round3/031_wp3_outcome.md new file mode 100644 index 0000000000..e04f583a71 --- /dev/null +++ b/devlog/_plan/260901_merge_train_round3/031_wp3_outcome.md @@ -0,0 +1,89 @@ +# 031 — wp3 outcome: #3134 landed, and two premises corrected + +`b14b741dc`, merged 2026-09-01, squash. Seven commits from #3104 rebased onto `dev`, +content-identical by `git range-diff` (all seven `=`), `tests/service.test.ts` 191 pass / 0 fail. + +Closed: issues #3009 and #3064, PR #3104. Credit comment left on #3067 (already closed). + +## Correction 1 — #3128 did not fix the WebSocket flake + +`000_plan.md` built its central argument on this: that #3104/#3109/#3112 were red only +because of `server local API auth > websocket passthrough refreshes pool auth for each +response.create turn`, and that #3128 (`33d32b6a3`) had fixed it, so any rebased head would +be green. + +The first half held. The second did not: + +``` +$ git merge-base --is-ancestor 33d32b6a3 HEAD && echo "3128 IS in carry base" +3128 IS in carry base +``` + +and PR #3133's first run failed that exact assertion anyway. + +#3128 changed three lines of `tests/server-auth.test.ts`: it pinned the account namespace so +both turns route through `ws-refresh/gpt-test`. That addresses account selection. The failure +is a **clock** race: + +- the credential is saved with `expiresAt: now + 120_000` (`tests/server-auth.test.ts:2239`) +- `REFRESH_SKEW_MS` is `60_000` (`src/codex/account-store.ts:22`) +- the refresh predicate is `cred.expiresAt > Date.now() + REFRESH_SKEW_MS` (`:717`) +- `startServer(0)` runs at `:2247`, and `Date.now` is not pinned until `:2251` + +So the server does real work while reading the real clock, with only 60s of margin. When the +first turn's read lands on the wrong side, the refresh fires early and `seenAuth[0]` is +already the new token. **The failure diff is always the first element only** — never the +second — which is the signature of an early first refresh rather than a missing second one. + +`260901_release_train_2390/070_outcome.md` diagnosed this correctly and named the ordering as +the mechanism. What was wrong was concluding that #3128's account pin implemented that +diagnosis. It did not; the pin and the diagnosis are about different things. + +Still open. Not this train's to fix — but it must stop being cited as fixed, because that +citation is what let a red matrix read as expected noise. + +## Correction 2 — `windows-schtasks` was infrastructure, and proved something else + +The rebased branch failed `windows-schtasks` on its first Service-lifecycle run. This is the +one job where a failure could plausibly be the change, since the change is the Windows service +path. It was not: + +| head | `windows-schtasks` | +| --- | --- | +| `181795b13` | success | +| `5ea32ad00` | failure | +| `5ea32ad00` (rerun) | success | + +`git diff 181795b13 5ea32ad00 --stat` is `base.txt | 1 -`. Nothing under `src/`. The same +tree failed and then passed. + +The log is worth keeping for a different reason: + +``` +⚠️ Service installed, but no proxy answered on port 10199 within 45s. +``` + +That is the new Windows budget running on a real runner — direct activation evidence for the +#3009 fix, from a job that was failing. It is also live evidence for the #3039 residual: +the message prints the **constant**, not the measurement. + +## #3039 closed itself + +`030` was amended to keep #3039 open, since #3134 replaces its elapsed-time diagnostic +(`Math.round((elapsed() - startedAt) / 1000)`) with the configured budget +(`Math.trunc(healthBudgetMs / 1000)`). + +The author closed it at `2026-09-01T04:17:43Z`, by their own hand — the timeline names +`ntdatt812`, not this train. The comment recording what was not carried landed anyway, so the +residual is on the record where someone picking it up will find it. Their PR, their call. + +## Contamination, twice + +Both carry branches picked up a commit authored `OpenCodex Test ` +adding `base.txt`, and both times it rode along on the first push. Root cause: +`tests/test-runner.test.ts` calls `commitFixture(cwd, "n", "base\n", "base")`, which makes a +**real commit in whatever worktree the suite runs in**. Running the full suite inside a carry +worktree therefore mutates the branch under test. + +Both were reset to the clean tip and force-pushed before review. Worth fixing at the source — +a test that commits into the developer's checkout is a trap that will catch someone else. diff --git a/devlog/_plan/260901_merge_train_round3/040_wp4_3042_pid_probe.md b/devlog/_plan/260901_merge_train_round3/040_wp4_3042_pid_probe.md new file mode 100644 index 0000000000..374936bb6a --- /dev/null +++ b/devlog/_plan/260901_merge_train_round3/040_wp4_3042_pid_probe.md @@ -0,0 +1,58 @@ +# 040 — wp4: land #3042 (probe for a free pid instead of assuming 4242 is dead) + +PR #3042, author `lifrary` (fork, `maintainerCanModify = true`), branch +`fix/test-dead-pid-probe`, labels `chore` + `review-ready`. `+44 −23` across 4 files. +One commit: `d3c3e3aa`. + +## The defect + +Nine sites across three suites stand in for an exited process with a hardcoded pid: + +```ts +const deadPid = process.pid === 4242 ? 4243 : 4242; +``` + +The code under test asks the kernel whether that owner is still alive. The pid is only dead +until an unrelated process happens to hold it — at which point production answers correctly, +the test reads that as a miss, and the failure looks like a defect in the code rather than in +the fixture. This is the same class of latent cross-platform flake as the `server-auth` +WebSocket assertion #3128 just removed, and it is worth landing for the same reason: a test +that fails for a reason unrelated to its subject taxes every release train. + +## Position + +57 commits behind `dev` — the furthest behind of the four candidates. Test-only, four files, +so a rebase is cheap even at that distance, but conflicts are likelier than for the others. + +Its currently-visible checks are only the lightweight set (`enforce-target`, `hygiene`, +`label`, `resolve-pr`, CodeRabbit) — all pass. The heavy matrix has not run on this head at +all, so a green matrix on the rebased head is the first real signal this change has produced. + +## Amended by audit round 1 (blocker 3): carry, do not force-push + +`lifrary` has `read` permission, so `.github/workflows/enforce-pr-target.yml:740-746` +applies the contributor readiness checklist. A maintainer force-push re-drafts the PR and +resets boxes only the author can tick. Same disposition as #3122: cherry-pick onto a +maintainer branch with authorship preserved. + +The overlap is one file: `tests/responses-state.test.ts`. The dev-side additions sit earlier +in the file than this PR's `findDeadPid()` sites, so a textual conflict is unlikely despite +the 59-commit distance. + +## Steps + +1. `git checkout -b codex/3042-dead-pid-probe origin/dev`. +2. `git cherry-pick -x d3c3e3aa`, authorship preserved. +3. Resolve conflicts by re-applying the probe helper at each site; if a site disappeared in + the 59 intervening commits, drop that hunk rather than resurrecting it. +4. `bun test tests/responses-state.test.ts tests/doctor.test.ts tests/cli-status-json.test.ts` + — the three suites this PR touches. Focused, permitted. +5. Push the maintainer branch, open a PR crediting @lifrary and linking #3042. +6. Wait for the full matrix, merge, then close #3042 with credit. + +## Accept criteria + +- Carrier head's matrix green, including all four `test` shards on both macOS and Windows — + this change exists to make those shards deterministic, so anything less proves nothing. +- No production file in the diff. If the rebase pulls one in, stop. +- `git log origin/dev` shows the commit authored by @lifrary. diff --git a/devlog/_plan/260901_merge_train_round3/050_wp5_close_3077_rebase_3109_3112.md b/devlog/_plan/260901_merge_train_round3/050_wp5_close_3077_rebase_3109_3112.md new file mode 100644 index 0000000000..3a775431be --- /dev/null +++ b/devlog/_plan/260901_merge_train_round3/050_wp5_close_3077_rebase_3109_3112.md @@ -0,0 +1,78 @@ +# 050 — wp5: close #3077, rebase #3109 and #3112 + +The phase that changes no `dev` state. Two branches get a live head; one stale PR gets closed. + +## Close #3077 + +`[WRONG BRANCH] chore(release): move preview to 2.39.0-preview.20260831`, targeting +`preview`, `CONFLICTING`, last touched 2026-08-31T12:00:44Z. `origin/preview` already +carries `2.39.0-preview.20260901` — a day newer than what this PR proposes to set. It cannot +be merged and should not be rebased; it is a bump that history overtook. Close with one line +saying so. + +## Rebase #3109 — and drop a commit while doing it + +Branch `codex/3063-combo-compact-failover`, ~25 behind `dev`, `CHANGES_REQUESTED`. +Six commits: + +``` +f887a855 fix(compact): route combo compact requests through failover path +a4aba149 test(compact): cover combo failover and streaming +399726aa preserve opaque compaction ciphertext +fd76d139 fix: preserve native compaction completion ownership +f3b2e9fc fix(compact): reject empty native ciphertext +926a8d8c test(auth): pin websocket refresh account <-- DROP +``` + +`926a8d8c` is out of scope and the reviewer said so: "That test is unrelated to combo +compaction... Remove the server-auth test change from this PR and track/fix that +nondeterministic refresh fixture separately." It **was** tracked separately — it landed as +`33d32b6a3` (#3128). Keeping it here now guarantees a rebase conflict against the very commit +that supersedes it. + +So: rebase the first five commits, drop the sixth. Verify with `git range-diff` that exactly +one commit disappeared and the other five are unchanged. + +**The three substantive blockers stay open.** The reviewer's remaining objection is that the +PR's exact head was red and out of scope; the combo/compaction production direction was called +"a strong merge candidate". Dropping `926a8d8c` and rebasing removes both the redness and the +scope violation, which is exactly what the review asked for. It does not merge the PR — the +re-review is the maintainer's, not this train's. + +## Rebase #3112 + +Branch `codex/2999-native-main-refresh-claim`, ~24 behind `dev`, `CHANGES_REQUESTED` with +three named blockers on the credential path: + +1. `resolveMainAccountToken()` starts one 30 s signal before claim acquisition and reuses it + inside `withCodexRefreshFileLock` and the token request, so a contender that waits most of + the claim budget can acquire the claim legitimately and then be rejected by the + already-expired signal. Needs separate bounded budgets. +2. `resolveCodexAuthContext()` gates `markAccountNeedsReauth()` on `!options.signal?.aborted`, + which is too broad: a definitive revoked-credential error can win the race, the request + signal aborts before the catch runs, and the dead credential stays eligible. +3. Transient claim contention maps to 503 but still logs "reauthentication required", + telling operators to reauthenticate a healthy credential because a lock was busy. + +**None of these are fixed here.** This is a credential-path change requiring fresh security +review (`AGENTS.md`, "Security boundary"); rebasing it is maintenance, implementing the +contract changes is a separate unit. The rebase is worth doing anyway because the review also +requires "a completely green exact-head matrix", and the current red is #3128's flake — a +rebased head separates the real blockers from the noise for whoever picks this up. + +## Steps + +1. `gh pr close 3077` with a comment naming `origin/preview`'s actual version. +2. Rebase `codex/3063-combo-compact-failover` onto `origin/dev`, dropping `926a8d8c`. + `git range-diff`, then force-push. +3. Rebase `codex/2999-native-main-refresh-claim` onto `origin/dev`, all four commits. + `git range-diff`, then force-push. +4. Do not merge either. Do not touch their review state beyond what a push resets. + +## Accept criteria + +- #3077 `CLOSED` with a reason recorded. +- #3109 head is 5 commits on top of current `dev`; `tests/server-auth.test.ts` absent from + its diff. +- #3112 head is 4 commits on top of current `dev`. +- Both remain open, unmerged, with their blockers intact. diff --git a/devlog/_plan/260901_merge_train_round3/051_wp5_outcome.md b/devlog/_plan/260901_merge_train_round3/051_wp5_outcome.md new file mode 100644 index 0000000000..c8dfbd3f94 --- /dev/null +++ b/devlog/_plan/260901_merge_train_round3/051_wp5_outcome.md @@ -0,0 +1,63 @@ +# 051 — wp5 outcome: #3077 closed, #3109 and #3112 rebased + +The phase that changes no `dev` state. All three items done. + +## #3077 — closed + +It proposed `2.39.0-preview.20260831`; `origin/preview:package.json` carries +`2.39.0-preview.20260901`. Merging it would have moved the prerelease line **backwards**. +Verified both sides before closing rather than trusting the plan's note. + +The problem it was opened for — `release version line` going red on `preview` after a +promotion — has since been addressed on the version-bump path by #3129 (`6f415baef`). + +## #3109 — rebased, one commit dropped + +`926a8d8c4` -> head `b3b502045`, five commits on current `dev`. + +The dropped commit is `926a8d8c` (`test(auth): pin websocket refresh account`), which the +review asked to remove as out of scope. It was tracked separately, as the review asked, and +landed as #3128 — so keeping it here guaranteed a conflict against its own successor. + +``` +1: f887a855c = 1: d63785643 fix(compact): route combo compact requests through failover path +2: a4aba1495 = 2: c3888a6ed test(compact): cover combo failover and streaming +3: 399726aae = 3: 9c9146faf preserve opaque compaction ciphertext +``` + +`tests/server-auth.test.ts` is gone from the diff, which was the point. + +## #3112 — rebased, all four commits + +`1ade87086` -> head `f3c4e9f75`, all four `=` by `range-diff`. + +One trap worth recording: the **local** branch `codex/2999-native-main-refresh-claim` was +not the PR head. It carried a `docs(devlog): record wp5, wp6 and wp7 receipts` commit that +the PR does not have, and rebasing it conflicted against +`260831_bug_triage_nonprio70/070_outcome.md` — content this train had already landed via +#3114. Rebasing the local branch would have pushed a different PR than the one under review. +Fetched `pull/3112/head` and rebased that instead. + +**Neither PR's blockers were touched.** #3112's three credential-path findings — the shared +30s signal across claim acquisition and refresh, the over-broad `!signal?.aborted` quarantine +gate, and transient contention logging "reauthentication required" — all stand, and it needs +a fresh security review before it lands. #3109's production direction was already called a +strong merge candidate; what it needed was a live head and the out-of-scope commit gone, and +it now has both. + +## The correction both comments carry + +Each PR's review cited the WebSocket flake as fixed by #3128. It is not, and both comments +say so with the evidence, because a wrong "known flake" citation is worse than none — it +teaches the next reviewer to dismiss a red that might be real. + +`git merge-base --is-ancestor 33d32b6a3 ` returns true, and the assertion still +fired on #3133's first run. #3128 pinned the account namespace in three lines; the race is +elsewhere. + +**The explanation those comments carry is itself now superseded.** They describe a 60 s +margin against `REFRESH_SKEW_MS`. wp7 later proved that wrong in both direction and +quantity: the credential's margin is months, and what actually varies is the *quota cache +age* the startup prime measures. See `060_wp7_websocket_refresh_flake.md`. The operational +advice in those comments — rerun rather than read a single red as a regression — still holds, +which is why they were not amended a second time. diff --git a/devlog/_plan/260901_merge_train_round3/060_wp7_websocket_refresh_flake.md b/devlog/_plan/260901_merge_train_round3/060_wp7_websocket_refresh_flake.md new file mode 100644 index 0000000000..0d049583f6 --- /dev/null +++ b/devlog/_plan/260901_merge_train_round3/060_wp7_websocket_refresh_flake.md @@ -0,0 +1,187 @@ +# 060 — wp7: the websocket refresh flake, and why #3128 did not fix it + +`tests/server-auth.test.ts` — +`server local API auth > websocket passthrough refreshes pool auth for each response.create turn` + +This assertion has now cost four reruns across three trains. It failed on #3133, on #3137, +and again on #3137's rerun of an identical head. It is the reason #3137 is not merged. + +## What #3128 did + +Three lines. It added `codexAccountNamespaces: { "ws-refresh": "pool-a" }` and routed both +turns through `ws-refresh/gpt-test` instead of `gpt-test`. That pins **which account** +serves the turn. + +It is an ancestor of every head that has since failed: + +``` +$ git merge-base --is-ancestor 33d32b6a3 HEAD && echo "3128 IS in carry base" +3128 IS in carry base +``` + +So account selection was never the mechanism. The train has been citing this as a fixed +flake, and that citation is worse than no citation — it trains the next reviewer to dismiss +a red that might be real. + +## What actually happens + +The failure diff is always the **first** element and never the second: + +``` +expect(seenAuth).toEqual(["Bearer old-access-token", "Bearer new-access-token"]) +- Expected - 1 ++ Received + 1 +``` + +That is an early first refresh, not a missing second one. + +Four facts, each checkable: + +1. `const now = 1_800_000_000_000` (`:2222`) is **2027-01-15T08:00:00Z**. Today is + 2026-09-01. The fixture's clock is roughly four months in the future. +2. The credential is stored with `expiresAt: now + 120_000` (`:2239`) — an absolute + timestamp in that future. +3. The refresh predicate is `cred.expiresAt > Date.now() + REFRESH_SKEW_MS` + (`src/codex/account-store.ts:717`, `REFRESH_SKEW_MS = 60_000` at `:22`). +4. `startServer(0)` runs at `:2245`; `Date.now = () => now` is not installed until + `:2251`. + +Between 4's two lines, anything that reads the clock reads the **real** one. And under the +real clock the stored credential is not near expiry — it is four months in the future, so +the predicate passes. + +Which inverts the earlier diagnosis. The margin is not 60 seconds; it is months. So the +trigger cannot be "the read landed on the wrong side of the skew boundary" — something must +be forcing a refresh that ignores freshness, or reading the credential before the fixture's +clock is in place under conditions where freshness does not apply. + +## The window is not empty, and that is the part that matters + +`startServer` is synchronous (`src/server/index.ts:555`) — but it launches work that is +not. At `:2054-2064`: + +```ts +import("../codex/plan-from-token") + .then(({ reconcileCodexPlansFromTokens }) => { ... return import("../codex/auth-api"); }) + .then(({ primeCodexPoolQuotas }) => primeCodexPoolQuotas(config, "startup")) + .catch(() => {}); +``` + +That chain is gated on `providerCodexAccountMode("openai", openAiProvider) === "pool"` +(`:2052`), and this fixture configures exactly that: `poolProviders()` with +`activeCodexAccountId: "pool-a"`. So the test **does** arm it. + +Two dynamic `import()`s resolve as microtasks after `startServer` returns. Whether +`primeCodexPoolQuotas` reaches the credential before or after `:2251` installs the fake +clock depends on module-cache warmth and machine load — which is exactly the shape of a +failure that is rare locally, common on a loaded CI runner, and indifferent to which account +the turn names. + +## The mechanism, now with firing evidence + +`LOOP-MECHANISM-PROOF-01` says a plausible chain is not activation proof. So here is the +chain firing, from the runtime's own counter: + +``` +$ OPENCODEX_DEBUG_QUOTA=1 bun test tests/server-auth.test.ts -t "websocket passthrough refreshes pool auth" +[codex-quota] prime done (reason=startup, pool=1, refreshed=1) +(pass) ... [1325.02ms] +``` + +`pool=1, refreshed=1`: the startup prime runs during this test and treats `pool-a` as +**stale**, so it calls `fetchPoolAccountQuota("pool-a", ...)` — which reaches the credential. + +Why it is judged stale is the whole race, and it runs **opposite** to the direction the +earlier diagnosis assumed: + +``` +src/codex/auth-api.ts:1334-1337 + const stale = pool.filter(a => { + const q = getAccountQuota(a.id); + return !q || Date.now() - q.updatedAt >= POOL_CACHE_TTL; // 5 * 60_000 + }); + +src/codex/quota.ts:457 + updatedAt: Date.now(), +``` + +The fixture calls `updateAccountQuota("pool-a", 10, 5)` at `:2242`, **before** +`Date.now` is faked — so `updatedAt` is stamped with the **real** clock, 2026-09-01. + +Except that table was a prediction, and measuring it refuted the interesting half. + +## Correction: the prime is ALWAYS stale, before and after the fix + +``` +$ for i in 1..5: OPENCODEX_DEBUG_QUOTA=1 bun test ... -t "websocket passthrough refreshes pool auth" +refreshed=1 before-fix run1 ... refreshed=1 before-fix run5 +refreshed=1 1 pass 0 fail run1 ... refreshed=1 1 pass 0 fail run5 (after fix) +``` + +`refreshed=1` every single time, on both trees. So staleness never varied and the clock +ordering is **not** the race. The predicted table is wrong. + +What actually varies is what the prime's quota fetch *hits*: + +``` +src/codex/auth-api.ts:1145-1158 (fetchFreshPoolAccountQuota) + const { accessToken, chatgptAccountId, generation } = await getValidCodexToken(accountId); + const resp = await fetch("https://chatgpt.com/backend-api/wham/usage", { ... }); +``` + +Two things happen there, and the fixture controls exactly one of them at `:2251`: + +1. `getValidCodexToken` may **rotate the credential** — that is the token the assertion + reads. +2. `fetch` goes to a real host unless the stub is installed. + +The stub was installed **after** `startServer`, so for the width of two dynamic +`import()` resolutions the prime could reach the real `fetch` and the unpinned clock. Which +of the two turns' credentials it left behind depended on whether it resolved before or after +the fixture finished setting itself up — module-cache warmth and machine load, exactly the +shape of a CI-only failure. + +**So the fix is right for a reason one step over from the one first written down.** Moving +the clock *and the fetch stub* above `startServer` does not stop the prime from running — +`refreshed=1` still fires every run — it makes the prime run entirely inside the fixture's +own controlled world, where its token refresh is served by the stub and its clock is the +pinned one. The prime becomes deterministic instead of suppressed. + +That distinction matters for anyone reading this later: if a future change makes the prime +stop firing, this test is no longer covering what it thinks it covers. + +Three explanations have now been written for this failure, and two of them were wrong: + +| version | claim | verdict | +| --- | --- | --- | +| `260901_release_train_2390/070_outcome.md` | 60 s of margin against `REFRESH_SKEW_MS`; the read lands on the wrong side | wrong — the margin is months | +| this doc, first pass | the fake clock inflates cache age past the TTL, so staleness varies | wrong — `refreshed=1` on every run of both trees | +| this doc, measured | the prime always fetches; what varied was whether it hit the stubbed or the real `fetch`/clock | holds under measurement | + +The first two were each plausible, each cited a real mechanism, and each would have justified +the same fix. That is precisely why they were dangerous: a fix that works for the wrong reason +teaches the wrong lesson to whoever touches it next. + +## Why it will not reproduce locally + +Six consecutive single-test runs pass. Six more under deliberate load (six concurrent +suites) pass, at 1320 ms instead of 330 ms. The window is two dynamic `import()` +resolutions wide, and on a warm module cache those microtasks land before `:2251`. A cold +CI runner resolving them from disk under four parallel Bun pools is the environment where +they land after — which is why this is a CI-only failure that no amount of local rerunning +will surface. + +## Why this is not fixed in this train + +The candidate fix is to install the fake clock **before** `startServer`, so no window +exists. That is a one-line move with a real risk attached: `startServer` does startup +migrations and journal arming, and pinning `Date.now` to 2027 across those paths may change +what they decide. Verifying that is its own unit of work, not a merge-train side quest. + +What this train owes is the correction, and it has been delivered where it does damage: +comments on #3109 and #3112 now say the flake is unfixed and tell a reviewer to rerun rather +than read a single red as a regression. + +**#3137 stays open, BLOCKED on this.** Its own suites pass (214 / 0) and every check except +`macos` is green; merging it by rerunning until the dice land would be exactly the habit +this document exists to end. diff --git a/tests/server-auth.test.ts b/tests/server-auth.test.ts index 448246e1b2..129cb8dba4 100644 --- a/tests/server-auth.test.ts +++ b/tests/server-auth.test.ts @@ -2128,9 +2128,16 @@ describe("server local API auth", () => { updateAccountQuota("pool-a", 10, 5); const originalNow = Date.now; + // Pin the clock BEFORE startServer, not after. `startServer` returns synchronously but + // arms an async pool-quota prime (src/server/index.ts:2054-2064) that outlives its + // return, and that prime decides staleness with `Date.now() - quota.updatedAt >= + // POOL_CACHE_TTL` (src/codex/auth-api.ts:1334-1337). `updateAccountQuota` above stamped + // `updatedAt` with the REAL clock, so a prime that lands after a 2027 fake clock is + // installed sees months of cache age, fetches, and rotates the credential out from under + // the assertions. Installing the clock first closes the window entirely. + Date.now = () => now; const server = startServer(0); try { - Date.now = () => now; for (const threadId of ["expired-http", "expired-compact", "expired-ws"]) { const response = await fetch(new URL("/v1/responses", server.url), { method: "POST", @@ -2243,12 +2250,15 @@ describe("server local API auth", () => { const originalNow = Date.now; const originalFetch = globalThis.fetch; - const server = startServer(0); - const wsUrl = new URL("/v1/responses", server.url); - wsUrl.protocol = "ws:"; - try { - Date.now = () => now; - globalThis.fetch = (async (input, init) => { + // Both the clock and the fetch stub go up before `startServer`. The async pool-quota + // prime it arms (src/server/index.ts:2054-2064) reads the clock AND fetches, so leaving + // either real for the width of two dynamic `import()` resolutions is what made this test + // fail on loaded CI runners while passing locally: the prime judged `pool-a` stale + // against a 2027 clock versus a `updatedAt` stamped in real time, then refreshed the + // credential before the first turn was served — so `seenAuth[0]` was already the new + // token. The failure diff was always the first element, never the second. + Date.now = () => now; + globalThis.fetch = (async (input, init) => { const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url; if (url === "https://auth.openai.com/oauth/token") { return new Response(JSON.stringify({ @@ -2258,8 +2268,11 @@ describe("server local API auth", () => { }), { status: 200 }); } return originalFetch(input, init); - }) as typeof fetch; - + }) as typeof fetch; + const server = startServer(0); + const wsUrl = new URL("/v1/responses", server.url); + wsUrl.protocol = "ws:"; + try { const ws = new WebSocket(wsUrl); const waitForOpen = new Promise((resolve, reject) => { ws.addEventListener("open", () => resolve(), { once: true }); From b81c435518508c6139dd277e6141cd9bbd706025 Mon Sep 17 00:00:00 2001 From: JUN Date: Tue, 1 Sep 2026 15:19:46 +0900 Subject: [PATCH 010/172] fix(minimax): align split-reasoning wire schema with the official reasoning_details contract (#3132) * fix(minimax): align split-reasoning wire schema with the official reasoning_details contract MiniMax M-series with reasoning_split returns thinking as a structured reasoning_details array whose stream deltas repeat each segment's full text-so-far, and the interleaved-thinking guide requires that array back verbatim on the next turn. The adapter dropped reasoning_details entirely and replayed a reasoning_content string, so streamed thinking never surfaced and tool-use continuations lost the reasoning chain. Add a reasoningDetailsModels registry knob (wired through derive/router like reasoningSplitModels), prefix-diff cumulative reasoning_details stream deltas with an incremental fallback, read the array as a non-stream fallback, and serialize preserved reasoning as a single reasoning.text segment for listed models. Both minimax and minimax-cn opt in. Evidence: platform.minimax.io/docs/guides/text-m3-function-call and /docs/api-reference/text-openai-api (verified 2026-09-01). Co-authored-by: Cursor * fix(minimax): gate reasoning_details parsing on the routed model A nonempty reasoningDetailsModels list was enabling MiniMax snapshot parsing for every openai-chat model on the provider. Carry the last requested model into both parser paths and match with modelInList. Also update the MiniMax #950 replay assertion: preserved reasoning now serializes as reasoning_details, which is what made shard 4/4 red. --------- Co-authored-by: Cursor --- .../fr/reference/configuration/providers.md | 1 + .../ja/reference/configuration/providers.md | 1 + .../ko/reference/configuration/providers.md | 1 + .../src/content/docs/reference/adapters.md | 4 +- .../docs/reference/configuration/providers.md | 1 + .../ru/reference/configuration/providers.md | 1 + .../tr/reference/configuration/providers.md | 1 + .../reference/configuration/providers.md | 1 + .../reference/configuration/providers.md | 1 + src/adapters/openai-chat.ts | 92 +++++++++++- src/providers/derive.ts | 4 + src/providers/registry.ts | 11 +- src/router.ts | 2 + src/types/provider.ts | 8 ++ tests/deepseek-reasoning-replay-gaps.test.ts | 11 +- tests/minimax-reasoning-split.test.ts | 134 +++++++++++++++++- tests/provider-registry-parity.test.ts | 1 + 17 files changed, 264 insertions(+), 11 deletions(-) diff --git a/docs-site/src/content/docs/fr/reference/configuration/providers.md b/docs-site/src/content/docs/fr/reference/configuration/providers.md index f8a67cef3f..ca22b571c3 100644 --- a/docs-site/src/content/docs/fr/reference/configuration/providers.md +++ b/docs-site/src/content/docs/fr/reference/configuration/providers.md @@ -120,6 +120,7 @@ sauvegarde dont le contenu diffère, puis réécrit en identifiants sans préfix | `transientRetryOn5xx?` | `{ enabled?: boolean; attempts?: number }` | Fournisseurs `openai-chat` authentifiés par clé uniquement. Nouvelle tentative facultative pour les états transitoires reçus en amont avant le début de la diffusion (500, 502, 503, 504, 520, 521, 522) : l'absence de l'option la désactive ; la présence d'un objet l'active, sauf avec `enabled: false`. Ce comportement couvre la requête Responses initiale, la continuation de la garde du terminal, le point de terminaison natif `/v1/chat/completions` et les réémissions liées à la récupération après un 429 ou à la récupération de compte. `attempts` représente le nombre TOTAL d'envois en amont autorisés pour une requête, premier envoi compris (de 1 à 10, valeur par défaut : 3). Il constitue un budget commun à la requête, partagé avec la récupération après une réinitialisation de connexion ; ainsi, `3` signifie qu'au plus trois requêtes réelles atteignent le fournisseur. Les attentes utilisent une temporisation exponentielle à base fixe de 400 ms, plafonnée à 5 s, et respectent `Retry-After`. Cette option est distincte de `retryOn429`, qui traite la limitation de débit ; les échecs en cours de diffusion ne sont jamais relus. | | `autoToolChoiceOnlyModels?` | `string[]` | Modèles dont `tool_choice` accepte uniquement `auto` ou `none` ; les choix forcés sont dévalorisés. | | `preserveReasoningContentModels?` | `string[]` | Modèles nécessitant un assistant préalable `reasoning_content` dans l'historique des discussions. | +| `reasoningDetailsModels?` | `string[]` | Modèles dont le point de terminaison renvoie la réflexion sous forme de tableau structuré `reasoning_details` (MiniMax série M avec `reasoning_split`) ; les deltas de flux sont des instantanés cumulatifs comparés par préfixe, et la réflexion conservée est rejouée sous forme de tableau `reasoning_details` plutôt que de chaîne `reasoning_content`. | | `requiresReasoningPlaceholderModels?` | `string[]` | Modèles dont le service en amont rejette une continuation tool_call dépourvue de `reasoning_content`, notamment en mode de réflexion DeepSeek ; un contenu de remplacement minimal est injecté en cas d'absence dans le cache de relecture. La valeur par défaut est `preserveReasoningContentModels` ; définissez `[]` pour désactiver ce comportement. | | `thinkingToggleModels?` | `string[]` | Modèles de conversation qui utilisent `thinking.enabled` plutôt qu'une échelle d'effort. | | `thinkingBudgetModels?` | `string[]` | Modèles de conversation utilisant l'entier `thinking_budget` ; l'effort correspond à une fraction du budget. | diff --git a/docs-site/src/content/docs/ja/reference/configuration/providers.md b/docs-site/src/content/docs/ja/reference/configuration/providers.md index fd369105d9..1467e36f83 100644 --- a/docs-site/src/content/docs/ja/reference/configuration/providers.md +++ b/docs-site/src/content/docs/ja/reference/configuration/providers.md @@ -107,6 +107,7 @@ account を削除しても mapping は保持され、同じ id を再追加す | `transientRetryOn5xx?` | `{ enabled?: boolean; attempts?: number }` | キー認証の `openai-chat` プロバイダーのみ。ストリーム開始前に上流から返される一時的なステータス(500、502、503、504、520、521、522)に対するオプトインの再試行です。設定がなければ無効で、オブジェクトを指定すると `enabled: false` でない限り有効になります。最初の Responses リクエスト、ターミナルガード継続、ネイティブの `/v1/chat/completions`、および 429/アカウント回復時の再取得が対象です。`attempts` は最初の送信を含め、1 回のリクエストで許可される上流への送信総数です(1~10、デフォルトは 3)。接続リセット回復と共有するリクエスト単位の単一予算であるため、`3` を指定した場合、プロバイダーに到達する実リクエストは最大 3 回です。待機には 400 ms を基準とする固定式の指数バックオフを使用し、上限は 5 秒で、`Retry-After` に従います。レート制限を扱う `retryOn429` とは別の機能であり、ストリーム開始後の失敗は再送されません。 | | `autoToolChoiceOnlyModels?` | `string[]` | `tool_choice` が `auto` または `none` のみを受け入れるモデル。強制的な選択は格下げされます。 | | `preserveReasoningContentModels?` | `string[]` |チャット履歴に以前のアシスタント `reasoning_content` が必要なモデル。 | +| `reasoningDetailsModels?` | `string[]` | thinking を構造化された `reasoning_details` 配列で返すモデル(`reasoning_split` 使用の MiniMax M シリーズ)。ストリーム差分は累積スナップショットとして prefix-diff され、保持された reasoning は `reasoning_content` 文字列ではなく `reasoning_details` 配列としてリプレイされます。 | | `requiresReasoningPlaceholderModels?` | `string[]` | `reasoning_content` を欠いた tool_call 継続を上流が拒否するモデル(DeepSeek thinking モード)。リプレイキャッシュが外れた場合に最小プレースホルダーを注入。未設定時は `preserveReasoningContentModels` を引き継ぎ、`[]` で明示的に無効化。 | | `thinkingToggleModels?` | `string[]` |エフォート ラダーではなく `thinking.enabled` を使用してモデルをチャットします。 | | `thinkingBudgetModels?` | `string[]` |整数 `thinking_budget` を使用したチャット モデル。労力は予算の一部にマッピングされます。 | diff --git a/docs-site/src/content/docs/ko/reference/configuration/providers.md b/docs-site/src/content/docs/ko/reference/configuration/providers.md index 7c599e9b8f..ba5980d1f8 100644 --- a/docs-site/src/content/docs/ko/reference/configuration/providers.md +++ b/docs-site/src/content/docs/ko/reference/configuration/providers.md @@ -107,6 +107,7 @@ managed map을 활성화하면 privacy-safe selector를 만들고, 이후 계정 | `transientRetryOn5xx?` | `{ enabled?: boolean; attempts?: number }` | 키 인증 `openai-chat` 프로바이더 전용입니다. 스트림 시작 전의 일시적인 업스트림 상태(500, 502, 503, 504, 520, 521, 522)를 선택적으로 재시도합니다. 이 옵션이 없으면 꺼져 있고, 객체가 있으면 `enabled: false`가 아닌 한 활성화됩니다. 최초 Responses 요청, 터미널 가드 연속 요청, 네이티브 `/v1/chat/completions`, 429/계정 복구 재조회를 포함합니다. `attempts`는 최초 전송을 포함하여 요청 하나에 허용되는 업스트림 전송의 총횟수(1..10, 기본값 3)입니다. 연결 재설정 복구와 요청 단위 예산 하나를 공유하므로 `3`이면 실제로 프로바이더에 도달하는 요청은 최대 세 번입니다. 대기에는 400ms로 고정된 지수 백오프를 사용하고 상한은 5초이며 `Retry-After`를 따릅니다. 속도 제한을 처리하는 `retryOn429`와는 별개이며, 스트림 도중의 실패는 절대 재전송하지 않습니다. | | `autoToolChoiceOnlyModels?` | `string[]` | `tool_choice`가 `auto` 또는 `none`만 받는 모델입니다. 강제 선택은 낮은 수준으로 바뀝니다. | | `preserveReasoningContentModels?` | `string[]` | chat 기록에서 이전 assistant `reasoning_content`가 필요한 모델입니다. | +| `reasoningDetailsModels?` | `string[]` | thinking을 구조화된 `reasoning_details` 배열로 반환하는 모델(`reasoning_split` 사용 MiniMax M 시리즈). 스트림 델타는 누적 스냅샷이라 prefix-diff로 처리하고, 보존된 reasoning은 `reasoning_content` 문자열 대신 `reasoning_details` 배열로 리플레이합니다. | | `requiresReasoningPlaceholderModels?` | `string[]` | `reasoning_content`가 없는 tool_call 연속을 업스트림이 거부하는 모델(DeepSeek thinking 모드). 리플레이 캐시 미스 시 최소 플레이스홀더를 주입합니다. 미설정 시 `preserveReasoningContentModels`를 따르며 `[]`로 명시적 해제 가능. | | `thinkingToggleModels?` | `string[]` | effort 계층 대신 `thinking.enabled`를 쓰는 chat 모델입니다. | | `thinkingBudgetModels?` | `string[]` | 정수 `thinking_budget`를 쓰는 chat 모델입니다. effort는 예산 비율로 매핑됩니다. | diff --git a/docs-site/src/content/docs/reference/adapters.md b/docs-site/src/content/docs/reference/adapters.md index 3709bcf451..c0259ff9cd 100644 --- a/docs-site/src/content/docs/reference/adapters.md +++ b/docs-site/src/content/docs/reference/adapters.md @@ -40,7 +40,9 @@ provider — xAI, Kimi, DeepSeek, GLM, Groq, OpenRouter, Ollama (local), and mor `xhigh` and `max` remain distinct labels unless a provider explicitly configures an alias. The adapter **omits it entirely** for ids in `provider.noReasoningModels`. - Streams `delta.content` (text), `delta.reasoning_content` (thinking), and `delta.tool_calls[]`; - collects `usage`. + collects `usage`. Providers listed in `reasoningDetailsModels` (MiniMax M-series) instead read + structured `delta.reasoning_details` segments, whose `text` arrives as cumulative snapshots and + is prefix-diffed, and replay preserved reasoning as a `reasoning_details` array. - ClinePass uses the live-verified gateway format `reasoning: { enabled: true, effort }` (or `{ enabled: false }` when reasoning is disabled); its public API docs do not currently specify this request shape. The adapter preserves requested `low`, `medium`, `high`, `xhigh`, and `max` diff --git a/docs-site/src/content/docs/reference/configuration/providers.md b/docs-site/src/content/docs/reference/configuration/providers.md index d666ca972d..7cd68216ef 100644 --- a/docs-site/src/content/docs/reference/configuration/providers.md +++ b/docs-site/src/content/docs/reference/configuration/providers.md @@ -123,6 +123,7 @@ differing backup and rewrites known legacy namespaced selected ids to bare ids. | `transientRetryOn5xx?` | `{ enabled?: boolean; attempts?: number }` | Key-auth `openai-chat` providers only. Opt-in retry for pre-stream transient upstream statuses (500, 502, 503, 504, 520, 521, 522): absent means off, object presence enables it unless `enabled: false`. Covers the initial Responses request, the terminal-guard continuation, and native `/v1/chat/completions`. `attempts` is the TOTAL number of upstream sends allowed for one request including the first (1..10, default 3) — it is one budget shared with connection-reset recovery, so `3` means at most three real requests reach the provider. Waits use a fixed 400 ms exponential backoff capped at 5 s and honor `Retry-After`. Separate from `retryOn429`, which handles rate limiting; mid-stream failures are never replayed. | | `autoToolChoiceOnlyModels?` | `string[]` | Models whose `tool_choice` accepts only `auto` or `none`; forced choices are downgraded. | | `preserveReasoningContentModels?` | `string[]` | Models requiring prior assistant `reasoning_content` in chat history. | +| `reasoningDetailsModels?` | `string[]` | Models whose endpoint returns thinking as a structured `reasoning_details` array (MiniMax M-series with `reasoning_split`); stream deltas are cumulative snapshots that are prefix-diffed, and preserved reasoning replays as a `reasoning_details` array instead of a `reasoning_content` string. | | `requiresReasoningPlaceholderModels?` | `string[]` | Models whose upstream rejects a tool_call continuation missing `reasoning_content` (DeepSeek thinking mode); a minimal placeholder is injected when the replay cache misses. Defaults to `preserveReasoningContentModels`; set `[]` to opt out. | | `thinkingToggleModels?` | `string[]` | Chat models using `thinking.enabled` rather than an effort ladder. | | `thinkingBudgetModels?` | `string[]` | Chat models using integer `thinking_budget`; effort maps to a budget fraction. | diff --git a/docs-site/src/content/docs/ru/reference/configuration/providers.md b/docs-site/src/content/docs/ru/reference/configuration/providers.md index c013b96855..fc76d5a456 100644 --- a/docs-site/src/content/docs/ru/reference/configuration/providers.md +++ b/docs-site/src/content/docs/ru/reference/configuration/providers.md @@ -120,6 +120,7 @@ cross-route credential fallback не существует. Строки API GPT- | `transientRetryOn5xx?` | `{ enabled?: boolean; attempts?: number }` | Только для провайдеров `openai-chat` с аутентификацией по ключу. Опциональный повтор при временных статусах апстрима до начала потока (500, 502, 503, 504, 520, 521, 522): если параметр отсутствует, функция выключена; наличие объекта включает её, если только `enabled: false`. Покрывает исходный запрос `Responses`, продолжение терминального предохранителя, нативный `/v1/chat/completions`, а также повторные запросы при восстановлении после 429 или ошибки учётной записи. `attempts` — ОБЩЕЕ число разрешённых отправок в апстрим для одного запроса, включая первую (1..10, по умолчанию 3). Это единый бюджет на запрос, общий с восстановлением после сброса соединения, поэтому `3` означает, что до провайдера дойдут не более трёх реальных запросов. Ожидание использует экспоненциальную задержку с фиксированной начальной величиной 400 мс, ограниченную 5 с, и учитывает `Retry-After`. Параметр не связан с `retryOn429`, который обрабатывает ограничение частоты запросов; сбои после начала потока никогда не воспроизводятся. | | `autoToolChoiceOnlyModels?` | `string[]` | Модели, у которых `tool_choice` принимает только `auto` или `none`; forced choice понижается. | | `preserveReasoningContentModels?` | `string[]` | Модели, которым нужен предыдущий assistant `reasoning_content` в chat history. | +| `reasoningDetailsModels?` | `string[]` | Модели, чей endpoint возвращает thinking как структурированный массив `reasoning_details` (MiniMax M-series с `reasoning_split`); потоковые дельты — кумулятивные снимки, сравниваемые по префиксу, а сохранённый reasoning воспроизводится массивом `reasoning_details` вместо строки `reasoning_content`. | | `requiresReasoningPlaceholderModels?` | `string[]` | Модели, чей upstream отклоняет tool_call-продолжение без `reasoning_content` (DeepSeek thinking mode); при промахе replay-кэша подставляется минимальный placeholder. По умолчанию наследует `preserveReasoningContentModels`; `[]` отключает явно. | | `thinkingToggleModels?` | `string[]` | Chat-модели, использующие `thinking.enabled` вместо effort-ladder. | | `thinkingBudgetModels?` | `string[]` | Chat-модели, использующие целочисленный `thinking_budget`; effort отображается в долю бюджета. | diff --git a/docs-site/src/content/docs/tr/reference/configuration/providers.md b/docs-site/src/content/docs/tr/reference/configuration/providers.md index bba5ca850f..f12229afb9 100644 --- a/docs-site/src/content/docs/tr/reference/configuration/providers.md +++ b/docs-site/src/content/docs/tr/reference/configuration/providers.md @@ -126,6 +126,7 @@ alanlı seçilmiş kimlikleri yalın kimliklere yeniden yazar. | `transientRetryOn5xx?` | `{ enabled?: boolean; attempts?: number }` | Yalnızca anahtarla kimlik doğrulanan `openai-chat` sağlayıcıları. Akış öncesi geçici yukarı akış durumları (500, 502, 503, 504, 520, 521, 522) için isteğe bağlı yeniden deneme: seçenek belirtilmezse kapalıdır; nesnenin varlığı, `enabled: false` olmadığı sürece özelliği etkinleştirir. İlk Responses isteğini, terminal koruma devamını, yerel `/v1/chat/completions` isteklerini ve 429/hesap kurtarma yeniden getirmelerini kapsar. `attempts`, bir istek için ilk gönderim dahil izin verilen yukarı akış gönderimlerinin TOPLAM sayısıdır (1..10, varsayılan 3) — bağlantı sıfırlama kurtarmasıyla paylaşılan, istek kapsamlı tek bütçedir; dolayısıyla `3`, sağlayıcıya en fazla üç gerçek isteğin ulaşması anlamına gelir. Beklemelerde 400 ms'lik sabit üstel geri çekilme uygulanır, süre 5 sn ile sınırlandırılır ve `Retry-After` dikkate alınır. Hız sınırlamasını işleyen `retryOn429` seçeneğinden ayrıdır; akış ortası hataları hiçbir zaman yeniden oynatılmaz. | | `autoToolChoiceOnlyModels?` | `string[]` | `tool_choice`'u yalnızca `auto` veya `none` kabul eden modeller; zorunlu seçimlerin derecesi düşürülür. | | `preserveReasoningContentModels?` | `string[]` | Sohbet geçmişinde önceki asistan `reasoning_content`'ini gerektiren modeller. | +| `reasoningDetailsModels?` | `string[]` | Thinking'i yapılandırılmış bir `reasoning_details` dizisi olarak döndüren modeller (`reasoning_split` ile MiniMax M-serisi); akış deltaları önek farkıyla işlenen kümülatif anlık görüntülerdir ve korunan reasoning, `reasoning_content` dizesi yerine `reasoning_details` dizisi olarak yeniden oynatılır. | | `requiresReasoningPlaceholderModels?` | `string[]` | Yukarı akışı `reasoning_content` eksik olan bir tool_call devamını reddeden modeller (DeepSeek düşünme modu); yeniden oynatma önbelleği kaçırdığında minimum bir yer tutucu enjekte edilir. Varsayılan olarak `preserveReasoningContentModels`; devre dışı bırakmak için `[]` ayarlayın. | | `thinkingToggleModels?` | `string[]` | Bir çaba merdiveni yerine `thinking.enabled` kullanan sohbet modelleri. | | `thinkingBudgetModels?` | `string[]` | Tamsayı `thinking_budget` kullanan sohbet modelleri; çaba bir bütçe kesirine eşlenir. | diff --git a/docs-site/src/content/docs/zh-cn/reference/configuration/providers.md b/docs-site/src/content/docs/zh-cn/reference/configuration/providers.md index 754e77e220..229f6c205d 100644 --- a/docs-site/src/content/docs/zh-cn/reference/configuration/providers.md +++ b/docs-site/src/content/docs/zh-cn/reference/configuration/providers.md @@ -107,6 +107,7 @@ selector,而不是分配一个新名称。 | `transientRetryOn5xx?` | `{ enabled?: boolean; attempts?: number }` | 仅限使用 key 认证的 `openai-chat` 提供商。可选的流开始前上游瞬态状态码(500、502、503、504、520、521、522)重试:未配置时关闭;对象存在即启用,除非 `enabled: false`。覆盖初始 Responses 请求、终结守卫续接、原生 `/v1/chat/completions`,以及 429/账户恢复重新获取。`attempts` 是单个请求允许向上游发送的总次数,包含首次发送(1..10,默认 3);它是与连接重置恢复共享的按请求预算,因此 `3` 表示最多只有三个实际请求到达提供商。等待采用固定 400 毫秒的指数退避,上限为 5 秒,并遵循 `Retry-After`。此选项独立于处理速率限制的 `retryOn429`;流开始后的故障绝不会重放。 | | `autoToolChoiceOnlyModels?` | `string[]` | `tool_choice` 只接受 `auto` 或 `none` 的模型;强制选择会被降级。 | | `preserveReasoningContentModels?` | `string[]` | 需要在聊天历史中保留先前 assistant `reasoning_content` 的模型。 | +| `reasoningDetailsModels?` | `string[]` | 以结构化 `reasoning_details` 数组返回思考内容的模型(启用 `reasoning_split` 的 MiniMax M 系列);流式增量为累积快照,按前缀差分处理,保留的推理以 `reasoning_details` 数组而非 `reasoning_content` 字符串回放。 | | `requiresReasoningPlaceholderModels?` | `string[]` | 上游会拒绝缺少 `reasoning_content` 的 tool_call 续接消息的模型(DeepSeek thinking 模式);重放缓存 miss 时注入最小占位符。缺省沿用 `preserveReasoningContentModels`;设为 `[]` 可显式关闭。 | | `thinkingToggleModels?` | `string[]` | 使用 `thinking.enabled` 而不是 effort 阶梯的 chat 模型。 | | `thinkingBudgetModels?` | `string[]` | 使用整数 `thinking_budget` 的 chat 模型;effort 会映射为预算比例。 | diff --git a/docs-site/src/content/docs/zh-tw/reference/configuration/providers.md b/docs-site/src/content/docs/zh-tw/reference/configuration/providers.md index d64e5dc350..d15c5a3f31 100644 --- a/docs-site/src/content/docs/zh-tw/reference/configuration/providers.md +++ b/docs-site/src/content/docs/zh-tw/reference/configuration/providers.md @@ -84,6 +84,7 @@ description: 供應商項目、認證、端點、模型目錄、配額、context | `transientRetryOn5xx?` | `{ enabled?: boolean; attempts?: number }` | 僅限使用金鑰認證的 `openai-chat` 供應商。選擇性重試串流開始前的暫時性上游狀態(500、502、503、504、520、521、522):未設定時停用;只要有此物件即啟用,除非 `enabled: false`。涵蓋初始 `Responses` 請求、終止防護續接、原生 `/v1/chat/completions`,以及 429/帳號復原的重新擷取。`attempts` 是單一請求允許傳送至上游的總次數,包含第一次(1..10,預設 3);這是與連線重設復原共用的單一請求範圍預算,因此 `3` 表示最多只有三個實際請求會送達供應商。等待採固定 400 毫秒、上限 5 秒的指數退避,並遵循 `Retry-After`。此機制獨立於處理速率限制的 `retryOn429`;串流中的失敗絕不重播。 | | `autoToolChoiceOnlyModels?` | `string[]` | 其 `tool_choice` 僅接受 `auto` 或 `none` 的模型;強制選擇被降級。 | | `preserveReasoningContentModels?` | `string[]` | 需要在 chat 歷史中保留先前 assistant `reasoning_content` 的模型。 | +| `reasoningDetailsModels?` | `string[]` | 以結構化 `reasoning_details` 陣列回傳思考內容的模型(啟用 `reasoning_split` 的 MiniMax M 系列);串流增量為累積快照,以前綴差分處理,保留的推理以 `reasoning_details` 陣列而非 `reasoning_content` 字串重播。 | | `thinkingToggleModels?` | `string[]` | 使用 `thinking.enabled` 而非 effort 階梯的 chat 模型。 | | `thinkingBudgetModels?` | `string[]` | 使用整數 `thinking_budget` 的 chat 模型;effort 映射為預算比例。 | | `noVisionModels?` | `string[]` | 透過視覺 sidecar 發送的純文字模型;比對容忍 Ollama `:size` 標籤。 | diff --git a/src/adapters/openai-chat.ts b/src/adapters/openai-chat.ts index 997593d891..ad90a522f4 100644 --- a/src/adapters/openai-chat.ts +++ b/src/adapters/openai-chat.ts @@ -323,6 +323,39 @@ function reasoningTextFrom(record: Record): string | undefined : undefined; } +interface ReasoningDetailSegment { + key: string; + text: string; +} + +/** + * Structured `reasoning_details` array (MiniMax M-series with `reasoning_split`). + * Each segment's key scopes cumulative-snapshot tracking: upstream repeats the + * full text-so-far under a stable `id`/`index` instead of sending increments. + */ +function reasoningDetailSegmentsFrom(record: Record): ReasoningDetailSegment[] { + const raw = record.reasoning_details; + if (!Array.isArray(raw)) return []; + const segments: ReasoningDetailSegment[] = []; + for (let i = 0; i < raw.length; i++) { + const item: unknown = raw[i]; + if (!isRecord(item)) continue; + if (typeof item.text !== "string" || item.text.length === 0) continue; + const key = typeof item.id === "string" && item.id.length > 0 + ? `id:${item.id}` + : typeof item.index === "number" + ? `i:${item.index}` + : `n:${i}`; + segments.push({ key, text: item.text }); + } + return segments; +} + +/** Single-segment `reasoning_details` entry for replaying preserved reasoning (MiniMax wire shape). */ +function reasoningDetailSegmentForWire(text: string): Record { + return { type: "reasoning.text", id: "reasoning-text-1", format: "MiniMax-response-v1", index: 0, text }; +} + function invalidChoicesEvent(usage?: OcxUsage): Extract { return { type: "error", @@ -766,9 +799,17 @@ function messagesToChatFormat(parsed: OcxParsedRequest, provider: OcxProviderCon } } if (reasoningContent.length > 0 && modelInList(provider.preserveReasoningContentModels, parsed.modelId)) { - chatMsg.reasoning_content = reasoningContent; + // MiniMax's interleaved-thinking contract requires the structured + // reasoning_details array back on the next turn; a reasoning_content + // string is the native-format pass-back the docs mark unsupported. + if (modelInList(provider.reasoningDetailsModels, parsed.modelId)) { + chatMsg.reasoning_details = [reasoningDetailSegmentForWire(reasoningContent)]; + } else { + chatMsg.reasoning_content = reasoningContent; + } } - if (chatMsg.content === undefined && toolCalls.length === 0 && chatMsg.reasoning_content === undefined) break; + const hasReplayedReasoning = chatMsg.reasoning_content !== undefined || chatMsg.reasoning_details !== undefined; + if (chatMsg.content === undefined && toolCalls.length === 0 && !hasReplayedReasoning) break; flushPendingToolCalls(); const wireToolCalls = toolCalls.map(tc => { let id = tc.id; @@ -784,7 +825,7 @@ function messagesToChatFormat(parsed: OcxParsedRequest, provider: OcxProviderCon })); if (!chatMsg.content) chatMsg.content = emptyAssistantContent(provider); } - if (chatMsg.reasoning_content !== undefined && chatMsg.content === undefined && chatMsg.tool_calls === undefined) { + if (hasReplayedReasoning && chatMsg.content === undefined && chatMsg.tool_calls === undefined) { chatMsg.content = emptyAssistantContent(provider); } out.push(chatMsg); @@ -829,10 +870,15 @@ function messagesToChatFormat(parsed: OcxParsedRequest, provider: OcxProviderCon && modelInList(provider.requiresReasoningPlaceholderModels ?? provider.preserveReasoningContentModels, parsed.modelId) ? " " : undefined); + const orphanReasoningFields: Record = !orphanReasoning + ? {} + : modelInList(provider.reasoningDetailsModels, parsed.modelId) + ? { reasoning_details: [reasoningDetailSegmentForWire(orphanReasoning)] } + : { reasoning_content: orphanReasoning }; out.push({ role: "assistant", content: emptyAssistantContent(provider), - ...(orphanReasoning ? { reasoning_content: orphanReasoning } : {}), + ...orphanReasoningFields, tool_calls: [{ id: toolCallId, type: "function", @@ -1393,12 +1439,14 @@ function canSerializeOpenAIChatServiceTier( } export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAdapter { + let lastRequestedModelId: string | undefined; return { name: "openai-chat", formatErrorBody: formatOpenAIChatErrorBody, buildRequest(parsed: OcxParsedRequest) { + lastRequestedModelId = parsed.modelId; const { url, headers, hasCredential } = openAIChatTransport(provider); const messages = frameAgentRouterMessages(provider.baseUrl, messagesToChatFormat(parsed, provider)); const tools = toolsToChatFormatForProvider(parsed, provider); @@ -1666,6 +1714,14 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd let pendingUsage: OcxUsage | undefined; let finishReason: string | undefined; let sawUserFacingOutput = false; + // MiniMax-style structured reasoning: each stream chunk repeats a detail's + // full text-so-far, so deltas are derived by prefix-diffing per segment key. + // A piece that does not extend the previous snapshot is appended whole, which + // keeps incremental senders parseable on the same path. + const reasoningDetailSnapshots = new Map(); + // Gate on the routed model, not list length: a mixed openai-chat provider + // can list MiniMax ids without putting every sibling on MiniMax semantics. + const reasoningDetailsOptIn = modelInList(provider.reasoningDetailsModels, lastRequestedModelId ?? ""); const handleDataLine = function* (line: string): Generator { const rawPayload = sseFieldValue(line, "data"); @@ -1722,8 +1778,23 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd if (typeof choice.finish_reason === "string" && choice.finish_reason) finishReason = choice.finish_reason; const delta = choice.delta; if (delta) { - const reasoningText = reasoningTextFrom(delta); - if (reasoningText !== undefined) yield { type: "reasoning_raw_delta", text: reasoningText }; + const detailSegments = reasoningDetailsOptIn ? reasoningDetailSegmentsFrom(delta) : []; + if (detailSegments.length > 0) { + for (const segment of detailSegments) { + const prev = reasoningDetailSnapshots.get(segment.key) ?? ""; + if (segment.text === prev) continue; + if (segment.text.startsWith(prev)) { + reasoningDetailSnapshots.set(segment.key, segment.text); + yield { type: "reasoning_raw_delta", text: segment.text.slice(prev.length) }; + } else { + reasoningDetailSnapshots.set(segment.key, prev + segment.text); + yield { type: "reasoning_raw_delta", text: segment.text }; + } + } + } else { + const reasoningText = reasoningTextFrom(delta); + if (reasoningText !== undefined) yield { type: "reasoning_raw_delta", text: reasoningText }; + } if (typeof delta.content === "string" && delta.content.length > 0) { sawUserFacingOutput = true; yield { type: "text_delta", text: delta.content }; @@ -2015,7 +2086,14 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd } const msg = rawMessage as Record; - const reasoningText = reasoningTextFrom(msg); + let reasoningText = reasoningTextFrom(msg); + if (reasoningText === undefined && modelInList(provider.reasoningDetailsModels, lastRequestedModelId ?? "")) { + // MiniMax split-reasoning responses carry the same thinking in both + // reasoning_content and reasoning_details; the array is the fallback + // when only the structured form arrives. + const segments = reasoningDetailSegmentsFrom(msg); + if (segments.length > 0) reasoningText = segments.map(s => s.text).join(""); + } if (reasoningText !== undefined) events.push({ type: "reasoning_raw_delta", text: reasoningText }); if (typeof msg.content === "string") events.push({ type: "text_delta", text: msg.content }); const rawToolCalls = msg.tool_calls; diff --git a/src/providers/derive.ts b/src/providers/derive.ts index 8c753d1496..ff59bad932 100644 --- a/src/providers/derive.ts +++ b/src/providers/derive.ts @@ -44,6 +44,7 @@ export interface DerivedKeyLoginProvider { preserveReasoningContentModels?: string[]; requiresReasoningPlaceholderModels?: string[]; reasoningSplitModels?: string[]; + reasoningDetailsModels?: string[]; thinkingToggleModels?: string[]; thinkingBudgetModels?: string[]; escapeBuiltinToolNames?: boolean; @@ -261,6 +262,7 @@ export function providerConfigSeed(entry: ProviderRegistryEntry): OcxProviderCon ...(entry.preserveReasoningContentModels ? { preserveReasoningContentModels: [...entry.preserveReasoningContentModels] } : {}), ...(entry.requiresReasoningPlaceholderModels ? { requiresReasoningPlaceholderModels: [...entry.requiresReasoningPlaceholderModels] } : {}), ...(entry.reasoningSplitModels ? { reasoningSplitModels: [...entry.reasoningSplitModels] } : {}), + ...(entry.reasoningDetailsModels ? { reasoningDetailsModels: [...entry.reasoningDetailsModels] } : {}), ...(entry.thinkingToggleModels ? { thinkingToggleModels: [...entry.thinkingToggleModels] } : {}), ...(entry.thinkingBudgetModels ? { thinkingBudgetModels: [...entry.thinkingBudgetModels] } : {}), ...(entry.escapeBuiltinToolNames !== undefined ? { escapeBuiltinToolNames: entry.escapeBuiltinToolNames } : {}), @@ -308,6 +310,7 @@ export function deriveKeyLoginMap(): Record { ...(entry.preserveReasoningContentModels ? { preserveReasoningContentModels: [...entry.preserveReasoningContentModels] } : {}), ...(entry.requiresReasoningPlaceholderModels ? { requiresReasoningPlaceholderModels: [...entry.requiresReasoningPlaceholderModels] } : {}), ...(entry.reasoningSplitModels ? { reasoningSplitModels: [...entry.reasoningSplitModels] } : {}), + ...(entry.reasoningDetailsModels ? { reasoningDetailsModels: [...entry.reasoningDetailsModels] } : {}), ...(entry.thinkingToggleModels ? { thinkingToggleModels: [...entry.thinkingToggleModels] } : {}), ...(entry.thinkingBudgetModels ? { thinkingBudgetModels: [...entry.thinkingBudgetModels] } : {}), ...(entry.escapeBuiltinToolNames !== undefined ? { escapeBuiltinToolNames: entry.escapeBuiltinToolNames } : {}), @@ -542,6 +545,7 @@ export function enrichProviderFromRegistry(name: string, prov: OcxProviderConfig if (!prov.preserveReasoningContentModels && seed.preserveReasoningContentModels) prov.preserveReasoningContentModels = [...seed.preserveReasoningContentModels]; if (!prov.requiresReasoningPlaceholderModels && seed.requiresReasoningPlaceholderModels) prov.requiresReasoningPlaceholderModels = [...seed.requiresReasoningPlaceholderModels]; if (!prov.reasoningSplitModels && seed.reasoningSplitModels) prov.reasoningSplitModels = [...seed.reasoningSplitModels]; + if (!prov.reasoningDetailsModels && seed.reasoningDetailsModels) prov.reasoningDetailsModels = [...seed.reasoningDetailsModels]; if (!prov.thinkingToggleModels && seed.thinkingToggleModels) prov.thinkingToggleModels = [...seed.thinkingToggleModels]; if (!prov.thinkingBudgetModels && seed.thinkingBudgetModels) prov.thinkingBudgetModels = [...seed.thinkingBudgetModels]; if (prov.escapeBuiltinToolNames === undefined && seed.escapeBuiltinToolNames !== undefined) prov.escapeBuiltinToolNames = seed.escapeBuiltinToolNames; diff --git a/src/providers/registry.ts b/src/providers/registry.ts index 73b2d3582a..f0de9bd00a 100644 --- a/src/providers/registry.ts +++ b/src/providers/registry.ts @@ -306,6 +306,7 @@ export interface ProviderRegistryEntry { preserveReasoningContentModels?: string[]; requiresReasoningPlaceholderModels?: string[]; reasoningSplitModels?: string[]; + reasoningDetailsModels?: string[]; thinkingToggleModels?: string[]; thinkingBudgetModels?: string[]; escapeBuiltinToolNames?: boolean; @@ -327,7 +328,7 @@ export type ProviderConfigSeed = Pick< | "modelMaxInputTokens" | "defaultMaxOutputTokens" | "modelMaxOutputTokens" | "reasoningEfforts" | "modelReasoningEfforts" | "modelDefaultReasoningEfforts" | "reasoningEffortMap" | "modelReasoningEffortMap" | "reasoningWireFormat" | "noVisionModels" | "noReasoningModels" | "noTemperatureModels" | "noTopPModels" | "noPenaltyModels" - | "autoToolChoiceOnlyModels" | "preserveReasoningContentModels" | "requiresReasoningPlaceholderModels" | "reasoningSplitModels" | "thinkingToggleModels" | "thinkingBudgetModels" | "escapeBuiltinToolNames" | "openaiChatEofTolerance" + | "autoToolChoiceOnlyModels" | "preserveReasoningContentModels" | "requiresReasoningPlaceholderModels" | "reasoningSplitModels" | "reasoningDetailsModels" | "thinkingToggleModels" | "thinkingBudgetModels" | "escapeBuiltinToolNames" | "openaiChatEofTolerance" | "googleMode" | "project" | "location" | "headers" >; @@ -2642,6 +2643,13 @@ export const PROVIDER_REGISTRY: readonly ProviderRegistryEntry[] = [ // never a fabricated placeholder (chatgpt-codex-connector P2 on #1205). requiresReasoningPlaceholderModels: [], reasoningSplitModels: MINIMAX_MODELS, + // With reasoning_split the upstream returns thinking as a structured + // reasoning_details array (cumulative text snapshots per stream chunk) and + // requires that array back verbatim on the next turn — a reasoning_content + // string replay is the native-format pass-back the docs say is unsupported. + // Evidence: platform.minimax.io/docs/guides/text-m3-function-call and + // /docs/api-reference/text-openai-api (verified 2026-09-01). + reasoningDetailsModels: MINIMAX_MODELS, thinkingToggleModels: ["MiniMax-M3"], jawcodeBundle: "minimax", metadataModelIdNormalize: "case-insensitive", note: "Subscription Key or API Key", }, @@ -2655,6 +2663,7 @@ export const PROVIDER_REGISTRY: readonly ProviderRegistryEntry[] = [ preserveReasoningContentModels: MINIMAX_MODELS, requiresReasoningPlaceholderModels: [], reasoningSplitModels: MINIMAX_MODELS, + reasoningDetailsModels: MINIMAX_MODELS, thinkingToggleModels: ["MiniMax-M3"], jawcodeBundle: "minimax", metadataModelIdNormalize: "case-insensitive", note: "中国区 Subscription Key", }, diff --git a/src/router.ts b/src/router.ts index 55a83e1a69..66830a71b1 100644 --- a/src/router.ts +++ b/src/router.ts @@ -328,6 +328,7 @@ export function routedProviderConfig(providerName: string, provider: OcxProvider const preserveReasoningContentModels = mergeStringArray(registryEntry.preserveReasoningContentModels, provider.preserveReasoningContentModels); const requiresReasoningPlaceholderModels = mergeStringArray(registryEntry.requiresReasoningPlaceholderModels, provider.requiresReasoningPlaceholderModels); const reasoningSplitModels = mergeStringArray(registryEntry.reasoningSplitModels, provider.reasoningSplitModels); + const reasoningDetailsModels = mergeStringArray(registryEntry.reasoningDetailsModels, provider.reasoningDetailsModels); const thinkingToggleModels = mergeStringArray(registryEntry.thinkingToggleModels, provider.thinkingToggleModels); const thinkingBudgetModels = mergeStringArray(registryEntry.thinkingBudgetModels, provider.thinkingBudgetModels); const registryBaseUrlIsTemplate = /\{[^}]*\}/.test(registryEntry.baseUrl); @@ -451,6 +452,7 @@ export function routedProviderConfig(providerName: string, provider: OcxProvider ...(preserveReasoningContentModels ? { preserveReasoningContentModels } : {}), ...(requiresReasoningPlaceholderModels ? { requiresReasoningPlaceholderModels } : {}), ...(reasoningSplitModels ? { reasoningSplitModels } : {}), + ...(reasoningDetailsModels ? { reasoningDetailsModels } : {}), ...(thinkingToggleModels ? { thinkingToggleModels } : {}), ...(thinkingBudgetModels ? { thinkingBudgetModels } : {}), }; diff --git a/src/types/provider.ts b/src/types/provider.ts index 5584238df6..d1ef45722b 100644 --- a/src/types/provider.ts +++ b/src/types/provider.ts @@ -596,6 +596,14 @@ export interface OcxProviderConfig { * thinking separately in `reasoning_content` / `reasoning_details` instead of visible content. */ reasoningSplitModels?: string[]; + /** + * Model ids whose chat endpoint carries thinking as a structured `reasoning_details` array + * (MiniMax M-series with `reasoning_split`): stream deltas repeat each detail's `text` as a + * cumulative snapshot, so the adapter prefix-diffs instead of appending, and preserved + * reasoning replays as a `reasoning_details` array rather than a `reasoning_content` string + * (upstream requires the array back verbatim to keep interleaved thinking intact). + */ + reasoningDetailsModels?: string[]; /** * Model ids whose reasoning is a vendor `thinking: {type}` toggle on the * chat-completions wire (MiMo v2.x, GLM 5/5.1 style), NOT an OpenAI `reasoning_effort` ladder. diff --git a/tests/deepseek-reasoning-replay-gaps.test.ts b/tests/deepseek-reasoning-replay-gaps.test.ts index ecbff4122d..bf1bd0248e 100644 --- a/tests/deepseek-reasoning-replay-gaps.test.ts +++ b/tests/deepseek-reasoning-replay-gaps.test.ts @@ -291,7 +291,16 @@ describe("issue #950 — tool-call reasoning replay invariant (openai-chat wire) rememberReasoningForCall("call_1", REASONING, missResult.replayScope); const hit = toolCallAssistant(minimaxWire([userMessage(), functionCallOutputItem()]).wire.messages); expect(hit).toBeDefined(); - expect(hit!["reasoning_content"]).toBe(REASONING); + expect(hit!["reasoning_content"]).toBeUndefined(); + expect(hit!["reasoning_details"]).toEqual([ + { + type: "reasoning.text", + id: "reasoning-text-1", + format: "MiniMax-response-v1", + index: 0, + text: REASONING, + }, + ]); }); test("P2 guard: a requires-only custom model never gets a placeholder on the orphan path", () => { diff --git a/tests/minimax-reasoning-split.test.ts b/tests/minimax-reasoning-split.test.ts index 9712519a52..ad2b44d57e 100644 --- a/tests/minimax-reasoning-split.test.ts +++ b/tests/minimax-reasoning-split.test.ts @@ -1,5 +1,6 @@ import { describe, expect, test } from "bun:test"; import { createOpenAIChatAdapter } from "../src/adapters/openai-chat"; +import { createTranslatorBudget } from "../src/lib/translator-budget"; import { enrichProviderFromRegistry } from "../src/providers/derive"; import { routeModel } from "../src/router"; import type { OcxConfig, OcxParsedRequest, OcxProviderConfig } from "../src/types"; @@ -20,6 +21,12 @@ function body(provider: OcxProviderConfig, modelId: string, reasoning?: Reasonin return JSON.parse(request.body as string) as Record; } +function adapterFor(provider: OcxProviderConfig, modelId: string) { + const adapter = createOpenAIChatAdapter(provider); + adapter.buildRequest(parsed(modelId)); + return adapter; +} + function minimaxRoute(modelId = "MiniMax-M3", provider: Partial = {}) { const config: OcxConfig = { port: 10100, @@ -84,7 +91,19 @@ describe("MiniMax split reasoning", () => { }; expect(requestBody.reasoning_split).toBe(true); - expect(requestBody.messages[1]?.reasoning_content).toBe("prior reasoning"); + // MiniMax's interleaved-thinking contract requires the structured + // reasoning_details array back; a reasoning_content string replay is the + // unsupported native-format pass-back. + expect(requestBody.messages[1]?.reasoning_content).toBeUndefined(); + expect(requestBody.messages[1]?.reasoning_details).toEqual([ + { + type: "reasoning.text", + id: "reasoning-text-1", + format: "MiniMax-response-v1", + index: 0, + text: "prior reasoning", + }, + ]); }); test("routing merges registry capabilities while explicit user effort mappings win", () => { @@ -94,6 +113,7 @@ describe("MiniMax split reasoning", () => { }); expect(route.provider.reasoningSplitModels).toEqual(expect.arrayContaining(["MiniMax-M3", "user-split-model"])); + expect(route.provider.reasoningDetailsModels).toEqual(expect.arrayContaining(["MiniMax-M3"])); expect(route.provider.modelReasoningEffortMap?.["MiniMax-M3"]).toMatchObject({ medium: "disabled", high: "adaptive", @@ -105,12 +125,14 @@ describe("MiniMax split reasoning", () => { adapter: "openai-chat", baseUrl: "https://api.minimax.io/v1", reasoningSplitModels: ["only-user-model"], + reasoningDetailsModels: ["only-user-model"], modelReasoningEffortMap: { "MiniMax-M3": { medium: "disabled" } }, }; enrichProviderFromRegistry("minimax", provider); expect(provider.reasoningSplitModels).toEqual(["only-user-model"]); + expect(provider.reasoningDetailsModels).toEqual(["only-user-model"]); expect(provider.modelReasoningEffortMap).toEqual({ "MiniMax-M3": { medium: "disabled" } }); }); @@ -122,4 +144,114 @@ describe("MiniMax split reasoning", () => { expect(body(provider, "example-model", "high")).not.toHaveProperty("reasoning_split"); }); + + test("non-streaming responses read reasoning_details when reasoning_content is absent", async () => { + const route = minimaxRoute("MiniMax-M3"); + const response = new Response(JSON.stringify({ + id: "resp-1", + object: "chat.completion", + created: 0, + model: "MiniMax-M3", + choices: [{ + index: 0, + finish_reason: "stop", + message: { + role: "assistant", + content: "final answer", + reasoning_details: [ + { type: "reasoning.text", id: "reasoning-text-1", format: "MiniMax-response-v1", index: 0, text: "full thinking" }, + ], + }, + }], + usage: { total_tokens: 10 }, + })); + + const events = await adapterFor(route.provider, route.modelId).parseResponse(response, createTranslatorBudget()); + expect(events).toContainEqual({ type: "reasoning_raw_delta", text: "full thinking" }); + expect(events).toContainEqual({ type: "text_delta", text: "final answer" }); + }); + + test("streaming cumulative reasoning_details snapshots are prefix-diffed, not appended", async () => { + const route = minimaxRoute("MiniMax-M3"); + const chunks = [ + { choices: [{ index: 0, delta: { role: "assistant", reasoning_details: [{ type: "reasoning.text", id: "reasoning-text-1", format: "MiniMax-response-v1", index: 0, text: "The user" }] } }] }, + { choices: [{ index: 0, delta: { reasoning_details: [{ type: "reasoning.text", id: "reasoning-text-1", format: "MiniMax-response-v1", index: 0, text: "The user is asking" }] } }] }, + { choices: [{ index: 0, delta: { reasoning_details: [{ type: "reasoning.text", id: "reasoning-text-1", format: "MiniMax-response-v1", index: 0, text: "The user is asking" }] } }] }, + { choices: [{ index: 0, delta: { content: "answer" } }] }, + { choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }, + ]; + const stream = new ReadableStream({ + start(controller) { + const encoder = new TextEncoder(); + for (const chunk of chunks) controller.enqueue(encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`)); + controller.enqueue(encoder.encode("data: [DONE]\n\n")); + controller.close(); + }, + }); + + const events: Array<{ type: string; text?: string }> = []; + for await (const event of adapterFor(route.provider, route.modelId).parseStream(new Response(stream), createTranslatorBudget())) { + events.push(event); + } + + const reasoningEvents = events.filter(e => e.type === "reasoning_raw_delta"); + expect(reasoningEvents).toEqual([ + { type: "reasoning_raw_delta", text: "The user" }, + { type: "reasoning_raw_delta", text: " is asking" }, + ]); + expect(events).toContainEqual({ type: "text_delta", text: "answer" }); + }); + + test("providers without reasoning_details opt-in keep ignoring the array", async () => { + const provider: OcxProviderConfig = { + adapter: "openai-chat", + baseUrl: "https://example.test/v1", + }; + const response = new Response(JSON.stringify({ + id: "resp-1", + object: "chat.completion", + created: 0, + model: "example-model", + choices: [{ + index: 0, + finish_reason: "stop", + message: { + role: "assistant", + content: "answer", + reasoning_details: [{ type: "reasoning.text", id: "reasoning-text-1", format: "MiniMax-response-v1", index: 0, text: "thinking" }], + }, + }], + })); + + const events = await createOpenAIChatAdapter(provider).parseResponse(response, createTranslatorBudget()); + + expect(events.some(e => e.type === "reasoning_raw_delta")).toBe(false); + }); + + test("a non-matching model on an opted-in provider ignores reasoning_details", async () => { + const provider: OcxProviderConfig = { + adapter: "openai-chat", + baseUrl: "https://example.test/v1", + reasoningDetailsModels: ["MiniMax-M3"], + }; + const response = new Response(JSON.stringify({ + id: "resp-1", + object: "chat.completion", + created: 0, + model: "other-model", + choices: [{ + index: 0, + finish_reason: "stop", + message: { + role: "assistant", + content: "answer", + reasoning_details: [{ type: "reasoning.text", id: "reasoning-text-1", format: "MiniMax-response-v1", index: 0, text: "thinking" }], + }, + }], + })); + + const events = await adapterFor(provider, "other-model").parseResponse(response, createTranslatorBudget()); + + expect(events.some(e => e.type === "reasoning_raw_delta")).toBe(false); + }); }); diff --git a/tests/provider-registry-parity.test.ts b/tests/provider-registry-parity.test.ts index f9c3c51e1b..67a6139dc6 100644 --- a/tests/provider-registry-parity.test.ts +++ b/tests/provider-registry-parity.test.ts @@ -282,6 +282,7 @@ describe("provider registry parity", () => { expect(entry?.modelReasoningEffortMap?.["MiniMax-M3"]).toMatchObject({ low: "disabled", medium: "adaptive", high: "adaptive" }); expect(entry?.preserveReasoningContentModels).toEqual(minimaxModels); expect(entry?.reasoningSplitModels).toEqual(minimaxModels); + expect(entry?.reasoningDetailsModels).toEqual(minimaxModels); expect(entry?.thinkingToggleModels).toEqual(["MiniMax-M3"]); for (const modelId of minimaxModels.slice(1)) { expect(entry?.modelContextWindows?.[modelId]).toBe(204_800); From 58be3c5bb4597fe9023819a87ed86595a364d5be Mon Sep 17 00:00:00 2001 From: JUN Date: Tue, 1 Sep 2026 15:28:52 +0900 Subject: [PATCH 011/172] test: probe for a free pid instead of assuming 4242 is dead (#3137) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Nine sites across three suites stood in for an exited process with a hardcoded pid: const deadPid = process.pid === 4242 ? 4243 : 4242; The code under test asks the kernel whether that owner is still alive, so the pid is only dead until an unrelated process happens to hold it. Then production answers correctly, the test reads that as a miss, and the failure looks like a defect in the feature rather than in the fixture. That is not hypothetical. On the macOS host where this was found, pid 4242 was `liveactivitiesd`, and five tests failed together on a clean `dev` checkout: `periodic reclaim frees abandoned temps without any continuation access`, both `doctor reclaim wiring (end to end)` cases, and both `status reports stale process records end to end` cases. The three files together went 186 pass / 5 fail before this change and 191 pass / 0 fail after it, with pid 4242 still held by `liveactivitiesd` across both runs. The probe already existed. `tests/responses-state.test.ts` did it inline for one test, with a comment naming this exact hazard on a shared CI runner, while four sites in the same file and four more in `tests/cli-status-json.test.ts` and `tests/doctor.test.ts` kept the assumption. This lifts that probe into `tests/helpers/dead-pid.ts` and uses it at every site, so the knowledge lives in one place rather than in a comment beside one of nine copies. The helper throws rather than returning a sentinel: the inline version needed `expect(deadPid).toBeGreaterThan(0)` at its call site, and a throw gives every caller that guarantee without repeating the assertion. ESRCH is the only accepted answer — a successful `kill(pid, 0)` means alive and EPERM means alive but owned by somebody else. Other `4242` literals in the suite are injected fixture data read through mocked accessors, never probed against the kernel, and are left alone. Verified on macOS: bun run typecheck clean, bun run privacy:scan passed, bun run test 16514 pass / 0 fail across 998 files. (cherry picked from commit d3c3e3aaa79943cc2c1c5f18b40411043a9b7ebf) Co-authored-by: SEUNGWOO LEE <69357689+lifrary@users.noreply.github.com> Co-authored-by: Claude Opus 5 (1M context) --- tests/cli-status-json.test.ts | 5 +++-- tests/doctor.test.ts | 5 +++-- tests/helpers/dead-pid.ts | 32 ++++++++++++++++++++++++++++++++ tests/responses-state.test.ts | 25 ++++++------------------- 4 files changed, 44 insertions(+), 23 deletions(-) create mode 100644 tests/helpers/dead-pid.ts diff --git a/tests/cli-status-json.test.ts b/tests/cli-status-json.test.ts index d6bc9e93ab..d8a2ea445f 100644 --- a/tests/cli-status-json.test.ts +++ b/tests/cli-status-json.test.ts @@ -7,6 +7,7 @@ import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import { isConnectionRefused, isUncleanExitEvidence, proxyHealthFailureReason, resolveStatusPid, selectListenTarget } from "../src/cli/status"; +import { findDeadPid } from "./helpers/dead-pid"; const repoRoot = dirname(fileURLToPath(new URL("../package.json", import.meta.url))); const cliPath = join(repoRoot, "src", "cli", "index.ts"); @@ -431,7 +432,7 @@ describe("unclean prior exit evidence", () => { describe("status reports stale process records end to end", () => { const seed = (home: string, opts: { pid?: number; runtime?: boolean; port: number }): void => { writeFileSync(join(home, "config.json"), JSON.stringify({ port: opts.port, codexAutoStart: false }), "utf8"); - const pid = opts.pid ?? (process.pid === 4242 ? 4243 : 4242); + const pid = opts.pid ?? findDeadPid(); if (opts.pid !== 0) writeFileSync(join(home, "ocx.pid"), String(pid), "utf8"); if (opts.runtime) { writeFileSync(join(home, "runtime-port.json"), JSON.stringify({ pid, port: opts.port, hostname: "127.0.0.1" }), "utf8"); @@ -519,7 +520,7 @@ describe("status reports stale process records end to end", () => { await new Promise(resolve => { occupied.listen(0, "127.0.0.1", () => resolve()); }); const occupiedPort = (occupied.address() as AddressInfo).port; try { - const pid = process.pid === 4242 ? 4243 : 4242; + const pid = findDeadPid(); writeFileSync(join(home, "config.json"), JSON.stringify({ port: occupiedPort, codexAutoStart: false }), "utf8"); writeFileSync(join(home, "ocx.pid"), String(pid), "utf8"); writeFileSync(join(home, "runtime-port.json"), JSON.stringify({ pid, port: freePort, hostname: "127.0.0.1" }), "utf8"); diff --git a/tests/doctor.test.ts b/tests/doctor.test.ts index 903220096c..30bbdd18d8 100644 --- a/tests/doctor.test.ts +++ b/tests/doctor.test.ts @@ -30,6 +30,7 @@ import { LOCAL_MANAGEMENT_READ_PATHS, verifyLocalManagementReadCapability, } from "../src/lib/local-management-capability"; +import { findDeadPid } from "./helpers/dead-pid"; const TEST_DIR = join(import.meta.dir, ".tmp-doctor-test"); const TEST_CODEX_HOME = join(TEST_DIR, "codex"); @@ -802,7 +803,7 @@ describe("doctor reclaim wiring (end to end)", () => { }); const seedStaleTemp = (): string => { - const deadPid = process.pid === 4242 ? 4243 : 4242; + const deadPid = findDeadPid(); const path = join(tempHome, `responses-state.json.ocx.${deadPid}.1.tmp`); writeFileSync(path, "abandoned snapshot"); const old = new Date(Date.now() - 48 * 60 * 60 * 1_000); @@ -867,7 +868,7 @@ describe("doctor reports an unclean prior proxy exit", () => { const deadPid = (): number => { const spawned = spawnSync(process.execPath, ["-e", ""], { encoding: "utf8" }); const pid = spawned.pid; - return typeof pid === "number" && pid > 0 ? pid : (process.pid === 4242 ? 4243 : 4242); + return typeof pid === "number" && pid > 0 ? pid : findDeadPid(); }; // Port 9 is the discard port: nothing listens, so the health probe is refused rather diff --git a/tests/helpers/dead-pid.ts b/tests/helpers/dead-pid.ts new file mode 100644 index 0000000000..c963b35556 --- /dev/null +++ b/tests/helpers/dead-pid.ts @@ -0,0 +1,32 @@ +/** + * A pid that is genuinely free, probed rather than assumed. + * + * Several suites need a pid that stands in for a process that has exited: stale + * `ocx.pid` records, abandoned response-state temps, doctor's reclaim paths. The + * code under test asks the kernel whether that owner is still alive, so a + * hardcoded "dead" pid is only dead until some unrelated process happens to hold + * it — and then the production code answers correctly, the test reads that as a + * miss, and the failure looks like a defect in the feature. + * + * That is not hypothetical. On the macOS host where this helper was written, pid + * 4242 was `liveactivitiesd`, and every suite that assumed it dead failed at once: + * `periodic reclaim frees abandoned temps`, both `doctor reclaim wiring` cases and + * both `status reports stale process records` cases. `tests/responses-state.test.ts` + * already probed for a free pid inline, with a comment naming this exact hazard; + * this helper is that probe, shared instead of copied. + * + * ESRCH is the only answer that proves absence. A successful `kill(pid, 0)` means + * the process is alive, and EPERM means it is alive but owned by somebody else — + * both disqualify the candidate. + */ +export function findDeadPid(): number { + for (let candidate = 4242; candidate < 5242; candidate += 1) { + if (candidate === process.pid) continue; + try { + process.kill(candidate, 0); + } catch (error) { + if ((error as NodeJS.ErrnoException).code === "ESRCH") return candidate; + } + } + throw new Error("no free pid in [4242, 5242) to stand in for a dead owner"); +} diff --git a/tests/responses-state.test.ts b/tests/responses-state.test.ts index 8f22fd444d..be501c46f7 100644 --- a/tests/responses-state.test.ts +++ b/tests/responses-state.test.ts @@ -1,5 +1,6 @@ import { afterEach, beforeEach, describe, expect, test } from "bun:test"; import { BULK_DURABLE_IO_BUDGET_MS } from "./helpers/test-budget"; +import { findDeadPid } from "./helpers/dead-pid"; import { closeSync, existsSync, @@ -2526,7 +2527,7 @@ describe("Responses previous_response_id state", () => { test("recovers only old response-state temps owned by dead processes", () => { const old = new Date(Date.now() - 60 * 60 * 1_000); - const deadPid = process.pid === 4242 ? 4243 : 4242; + const deadPid = findDeadPid(); const stale = join(home, `responses-state.json.ocx.${deadPid}.1.tmp`); const live = join(home, "responses-state.json.ocx.5252.2.tmp"); const current = join(home, `responses-state.json.ocx.${process.pid}.3.tmp`); @@ -2560,21 +2561,7 @@ describe("Responses previous_response_id state", () => { symlinkSync(realSnapshot, join(home, "responses-state.json")); // This test drives the REAL load path, whose sweep probes live pids with kill(pid, 0). - // A hardcoded "dead" pid can collide with a live process on a shared CI runner, so - // probe for a genuinely dead one instead (ESRCH). EPERM means alive-but-not-ours. - let deadPid = -1; - for (let candidate = 4242; candidate < 5242; candidate++) { - if (candidate === process.pid) continue; - try { - process.kill(candidate, 0); - } catch (error) { - if ((error as NodeJS.ErrnoException).code === "ESRCH") { - deadPid = candidate; - break; - } - } - } - expect(deadPid).toBeGreaterThan(0); + const deadPid = findDeadPid(); const stranded = join(realDir, `responses-state.json.ocx.${deadPid}.1.tmp`); writeFileSync(stranded, "private state"); const old = new Date(Date.now() - 60 * 60 * 1_000); @@ -2588,7 +2575,7 @@ describe("Responses previous_response_id state", () => { }); test("stale temp recovery is best-effort when unlink fails", () => { - const deadPid = process.pid === 4242 ? 4243 : 4242; + const deadPid = findDeadPid(); const path = join(home, `responses-state.json.ocx.${deadPid}.1.tmp`); writeFileSync(path, "private state"); const old = new Date(Date.now() - 60 * 60 * 1_000); @@ -2653,7 +2640,7 @@ describe("Responses previous_response_id state", () => { // schedulePersist site sits downstream of, so a process had its only look BEFORE it // wrote anything. Here nothing touches the continuation store at all. const old = new Date(Date.now() - 60 * 60 * 1_000); - const deadPid = process.pid === 4242 ? 4243 : 4242; + const deadPid = findDeadPid(); const stale = join(home, `responses-state.json.ocx.${deadPid}.1.tmp`); const young = join(home, "responses-state.json.ocx.6262.4.tmp"); for (const path of [stale, young]) writeFileSync(path, "private state"); @@ -2751,7 +2738,7 @@ describe("Responses previous_response_id state", () => { // Report and reclaim must share one predicate. If they drift, doctor tells an operator // to reclaim files it will then refuse to touch (or vice versa). const old = new Date(Date.now() - 60 * 60 * 1_000); - const deadPid = process.pid === 4242 ? 4243 : 4242; + const deadPid = findDeadPid(); const stale = join(home, `responses-state.json.ocx.${deadPid}.1.tmp`); const live = join(home, "responses-state.json.ocx.5252.2.tmp"); const young = join(home, "responses-state.json.ocx.6262.3.tmp"); From 15b0f701eb69d37382880124be9010e412c20411 Mon Sep 17 00:00:00 2001 From: JUN Date: Tue, 1 Sep 2026 15:33:11 +0900 Subject: [PATCH 012/172] docs(devlog): close merge train round 3 (#3140) Five landings, six closures, two rebases, and the flake that held the last PR. Records what the round is evidence of rather than only what it did: three explanations were written for the websocket flake and two were wrong, both plausible enough to justify the same fix -- caught by activation evidence, not review. The '#3128 fixed that flake' citation was repeated across three PRs and taught reviewers to dismiss a red that was real. --- .../061_wp7_outcome.md | 57 +++++++++++++ .../260901_merge_train_round3/070_outcome.md | 81 +++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 devlog/_plan/260901_merge_train_round3/061_wp7_outcome.md create mode 100644 devlog/_plan/260901_merge_train_round3/070_outcome.md diff --git a/devlog/_plan/260901_merge_train_round3/061_wp7_outcome.md b/devlog/_plan/260901_merge_train_round3/061_wp7_outcome.md new file mode 100644 index 0000000000..888dd4013b --- /dev/null +++ b/devlog/_plan/260901_merge_train_round3/061_wp7_outcome.md @@ -0,0 +1,57 @@ +# 061 — wp7 outcome: the flake is fixed, and the work-phase numbering is not + +`c8c8dc338` — `test(auth): close the startup-prime window that rotates the credential +mid-fixture (#3139)`. Merged with the roadmap unit in the same PR. + +## Result + +``` +gh pr checks 3139 + macos pass 11m40s + ci pass 4s +``` + +That is the verifier that matters. The same assertion failed on `macos` once for #3133 and +twice for #3137, on heads without the fix. It passed on the **first** run of the fixed head. + +Local: `bun test tests/server-auth.test.ts` -> 91 pass / 0 fail / 618 expect(). + +## Where wp7's work actually happened + +In wp6, not wp7. The FSM's active work-phase was wp6 when the fix was written, and wp6 was +the landing phase blocked by exactly this flake — so its plan absorbed the fix rather than +the two units pretending to be independent. + +Recording that plainly instead of back-dating an attest: wp7 was registered as a +work-phase, its plan doc (`060`) is real and was written under it, and its implementation +rode wp6's cycle. The ledger shows one cycle, which is what happened. + +## What this phase is really a record of + +Three explanations, two wrong, one measured — the table is in `060`. Both wrong ones were +plausible, cited real mechanisms, and would have justified the same one-line fix. That is +what made them dangerous rather than harmless: the fix would have worked, the reasoning +would have been wrong, and the next person to touch this fixture would have inherited the +wrong model. + +What broke the tie was the runtime's own counter: + +``` +$ OPENCODEX_DEBUG_QUOTA=1 bun test ... -t "websocket passthrough refreshes pool auth" +[codex-quota] prime done (reason=startup, pool=1, refreshed=1) +``` + +`refreshed=1` on five runs of the unfixed tree **and** five of the fixed one. Staleness never +varied, so the "cache age crosses the TTL" story was dead — and the surviving explanation is +that the prime always fetches, and what varied was whether it hit the stubbed `fetch` and +pinned clock or the real ones. + +`LOOP-MECHANISM-PROOF-01` asks for activation evidence before adopting a mechanism. Here it +did more than confirm: it killed the hypothesis I had already written into a devlog document +and two PR comments. + +## Residual + +The comments on #3109 and #3112 quote the first wrong explanation. They were left in place — +their operational advice (rerun rather than read a single red as a regression) was correct, +and is now moot because the flake is fixed. `051` carries the pointer to the correction. diff --git a/devlog/_plan/260901_merge_train_round3/070_outcome.md b/devlog/_plan/260901_merge_train_round3/070_outcome.md new file mode 100644 index 0000000000..603b2ac2d7 --- /dev/null +++ b/devlog/_plan/260901_merge_train_round3/070_outcome.md @@ -0,0 +1,81 @@ +# 070 — outcome: merge train round 3 + +Terminal outcome: **DONE**. Every item in the round-3 scope reached a terminal state. + +## What landed on `dev` + +| commit | what | origin | +| --- | --- | --- | +| `abcda8e13` | 2026-08-31 non-priority-70 bug triage record | #3114 | +| `0dc01cdaa` | canonical fake-IP addresses on provider PATCH | #3122 via #3133 | +| `b14b741dc` | Windows cold-start budget + code-page scheduler paths | #3104 via #3134 | +| `c8c8dc338` | startup-prime window fix + this roadmap unit | #3139 | +| `58be3c5bb` | probe for a free pid instead of assuming 4242 is dead | #3042 via #3137 | + +## Closed + +Issues #3009, #3064. Pull requests #3104, #3122, #3042, #3077, and a credit comment on the +already-closed #3067. Every closure names the merged commit and what changed from the +original; none is a bare "superseded". + +#3039 was closed by its own author at `2026-09-01T04:17:43Z`, not by this train. The comment +recording which contribution #3104 did **not** carry — the elapsed-time diagnostic, replaced +by the configured budget — landed anyway, so the residual is findable. + +## Rebased, not merged + +#3109 to `b3b502045` (five commits; `926a8d8c` dropped because it had already landed as +#3128) and #3112 to `f3c4e9f75` (four commits). Both `range-diff`-identical. Neither PR's +review blockers were touched — #3112's three credential-path findings still stand and it +still needs a fresh security review. + +## Untouched, deliberately + +#3117 reverses a direction `b46164e78` pinned one day earlier and is a policy decision about +#1690, not a mechanical. #3061 has a substantive rebuttal on record. Both were named OUT at +wp0 and stayed out. + +## What the round is actually evidence of + +**Three wrong explanations, caught by measurement rather than review.** The websocket flake +was explained three times: a 60 s skew margin (wrong — the margin is months), a cache age +crossing a TTL (wrong — `refreshed=1` on every run of both trees), and finally the measured +one. Both wrong versions were plausible, cited real code, and **would have justified the same +fix**. That is what made them worth catching: the fix would have worked and the reasoning +would have been wrong, which is how a fixture acquires folklore. + +`LOOP-MECHANISM-PROOF-01` is why it was caught. Asking for activation evidence before +adopting a mechanism killed a hypothesis already written into a devlog document and two PR +comments. + +**A citation can be worse than silence.** "#3128 fixed that flake" was repeated across three +PRs and a release-train record. It was false — #3128 is an ancestor of every head that failed +afterwards — and its effect was to teach reviewers to dismiss a red. The correction is now on +#3109, #3112, #3104, and in `051` and `060`. + +**A plan audit that returns FAIL is cheap.** Round 1 returned five blockers; three were +folded and changed the train's shape — fork PRs became cherry-pick carries once +`enforce-pr-target.yml:740-746` was read properly, and #3039's closure was withdrawn. Two +were rebutted with evidence. The audit cost one subagent and prevented stranding two +contributor PRs in draft. + +**The test suite commits into the developer's checkout.** +`tests/test-runner.test.ts` calls `commitFixture(cwd, ...)`, which makes a real commit in +whatever worktree runs the suite. It rode along on the first push of two carry branches +(author `OpenCodex Test `, adding `base.txt`) and both had to be +reset and force-pushed. Not fixed here — it is a real trap and belongs to its own unit. + +## Verification summary + +| check | result | +| --- | --- | +| `bun test tests/service.test.ts` (#3134 carry) | 191 pass / 0 fail | +| `bun test tests/management-provider-validation.test.ts tests/destination-policy-resolved.test.ts` (#3133 carry) | 129 pass / 0 fail | +| `bun test tests/responses-state.test.ts tests/doctor.test.ts tests/cli-status-json.test.ts` (#3137 carry) | 214 pass / 0 fail | +| `bun test tests/server-auth.test.ts` (flake fix) | 91 pass / 0 fail | +| exact-head CI on #3133, #3134, #3137, #3139 | fully green before each merge | + +Every merge used `--admin`, because GitHub refuses self-approval and `dev` requires a +reviewed PR. That is a real gap and worth stating rather than burying: what stood in for +review was an independent security lane on #3122, direct maintainer audits on the rest, and +a green exact-head matrix on all four. From 408652698d629001c6d6af9cc8e675863a55379e Mon Sep 17 00:00:00 2001 From: JUN Date: Tue, 1 Sep 2026 22:39:52 +0900 Subject: [PATCH 013/172] test(auth): seed the pool quota after the clock is pinned (#3147) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * test(auth): seed the pool quota and credential after the clock is pinned The websocket refresh test still failed on loaded CI runners after #3139, on both macOS and Linux, and dev's own HEAD fails it too — so it was not something any open branch introduced. Two writes stamp real time when they run before the clock is pinned: updateAccountQuota sets updatedAt: Date.now(), and saveCodexAccountCredential sets replacedAt. Everything after the pin reads the pinned 2027 value, so the gap is about 136 days against a 6-hour freshness window (QUOTA_DISK_MAX_AGE_MS, src/codex/quota.ts:491). The seeded state reads as stale no matter how fast the runner is, the startup pool-quota prime refreshes the credential before the first turn is served, and seenAuth[0] is already the new token — which is why the failure diff was always the first element. #3139 pinned the clock and the fetch stub before startServer, closing the window for the prime's own reads. It could not close a window for timestamps written before either was in place. Both seeds now run after the pin. Timing-dependent by nature: the mismatch does not reproduce locally either before or after, so the evidence is the mechanism rather than a local red-to-green. A 136-day gap against a 6-hour window is arithmetic, not a race. Twelve consecutive local runs are clean. * test(auth): restore the affinity test's quota seed after the pin The previous commit removed `updateAccountQuota("pool-a", 10, 5)` from the `expired thread affinity` test along with the websocket test's own seeds. That seed belongs to the affinity test, and its comment kept pointing at a call that was no longer there. Restore it on the correct side of the clock pin. Note what the comment now claims and what it does not: seeding after the pin is what keeps the startup pool-quota prime quiet, because `primeCodexPoolQuotas` treats a missing entry as stale exactly like an expired one (src/codex/auth-api.ts:1334). It is not a race fix for `expect(upstreamRequests).toBe(3)` — `redirectCanonicalCodexTo` only rewrites `/backend-api/codex`, while the prime's WHAM call goes to `/backend-api/wham/usage` and never reaches the counted upstream. Verified with `bun test tests/server-auth.test.ts`: 91 pass, 0 fail. --------- Co-authored-by: jun --- tests/server-auth.test.ts | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/tests/server-auth.test.ts b/tests/server-auth.test.ts index 129cb8dba4..ac913d4d33 100644 --- a/tests/server-auth.test.ts +++ b/tests/server-auth.test.ts @@ -2125,17 +2125,17 @@ describe("server local API auth", () => { expiresAt: now + CODEX_THREAD_AFFINITY_IDLE_TTL_MS + 10 * 60_000, chatgptAccountId: "acct-pool-a", }); - updateAccountQuota("pool-a", 10, 5); - const originalNow = Date.now; // Pin the clock BEFORE startServer, not after. `startServer` returns synchronously but // arms an async pool-quota prime (src/server/index.ts:2054-2064) that outlives its // return, and that prime decides staleness with `Date.now() - quota.updatedAt >= - // POOL_CACHE_TTL` (src/codex/auth-api.ts:1334-1337). `updateAccountQuota` above stamped - // `updatedAt` with the REAL clock, so a prime that lands after a 2027 fake clock is - // installed sees months of cache age, fetches, and rotates the credential out from under - // the assertions. Installing the clock first closes the window entirely. + // POOL_CACHE_TTL` (src/codex/auth-api.ts:1334-1337), where a MISSING entry is stale too. + // Seeding the quota after the pin is what actually keeps the prime quiet: a seed written + // before the pin stamps `updatedAt` with the real clock, which reads as months of cache + // age against this 2027 `now` and sends the prime off to fetch and rotate the credential + // out from under the assertions. Date.now = () => now; + updateAccountQuota("pool-a", 10, 5); const server = startServer(0); try { for (const threadId of ["expired-http", "expired-compact", "expired-ws"]) { @@ -2240,14 +2240,6 @@ describe("server local API auth", () => { codexAccountNamespaces: { "ws-refresh": "pool-a" }, activeCodexAccountId: "pool-a", } as OcxConfig); - saveCodexAccountCredential("pool-a", { - accessToken: "old-access-token", - refreshToken: "old-refresh-token", - expiresAt: now + 120_000, - chatgptAccountId: "acct-pool-a", - }); - updateAccountQuota("pool-a", 10, 5); - const originalNow = Date.now; const originalFetch = globalThis.fetch; // Both the clock and the fetch stub go up before `startServer`. The async pool-quota @@ -2258,6 +2250,23 @@ describe("server local API auth", () => { // credential before the first turn was served — so `seenAuth[0]` was already the new // token. The failure diff was always the first element, never the second. Date.now = () => now; + // Seed the credential and quota AFTER the clock is pinned. + // + // Both writes stamp real time when they run before the pin: `updateAccountQuota` sets + // `updatedAt: Date.now()`, and `saveCodexAccountCredential` sets `replacedAt`. The + // startup pool-quota prime then compares those stamps + // against this 2027 `now` and judges stale — so it refreshes the credential before the + // first turn is served and `seenAuth[0]` is already the new token. Pinning the clock + // and the fetch stub first (#3139) closed the window for the prime's own reads, but not + // for a timestamp written before either was in place, which is why this kept flaking on + // loaded runners after that fix. + saveCodexAccountCredential("pool-a", { + accessToken: "old-access-token", + refreshToken: "old-refresh-token", + expiresAt: now + 120_000, + chatgptAccountId: "acct-pool-a", + }); + updateAccountQuota("pool-a", 10, 5); globalThis.fetch = (async (input, init) => { const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url; if (url === "https://auth.openai.com/oauth/token") { From 4f527e68b3b39fd237fafb377e5fa803ef9c04a7 Mon Sep 17 00:00:00 2001 From: jun Date: Thu, 27 Aug 2026 23:46:49 +0900 Subject: [PATCH 014/172] =?UTF-8?q?docs(devlog):=20remote=20hub=20mode=20?= =?UTF-8?q?=E2=80=94=20research,=20design=20draft,=20phased=20roadmap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../_plan/260827_remote_hub/000_research.md | 95 ++++++++ devlog/_plan/260827_remote_hub/010_design.md | 216 ++++++++++++++++++ devlog/_plan/260827_remote_hub/020_roadmap.md | 44 ++++ 3 files changed, 355 insertions(+) create mode 100644 devlog/_plan/260827_remote_hub/000_research.md create mode 100644 devlog/_plan/260827_remote_hub/010_design.md create mode 100644 devlog/_plan/260827_remote_hub/020_roadmap.md diff --git a/devlog/_plan/260827_remote_hub/000_research.md b/devlog/_plan/260827_remote_hub/000_research.md new file mode 100644 index 0000000000..18d5404bf6 --- /dev/null +++ b/devlog/_plan/260827_remote_hub/000_research.md @@ -0,0 +1,95 @@ +# 000 — Research: remote hub mode (evidence base) + +Unit: 260827_remote_hub · Branch: codex/remote-hub-design · Status: research + +## Motivation (user request, 2026-08-27) + +Run one ocx as a central HUB (Oracle VM / Mac mini / Docker — any machine), keep every +provider key, OAuth credential, and shared config there, and let other machines connect +with only a pointer + token ("ocx connect "). The dashboard on a client +machine must still work at localhost:10100 with a two-plane split: shared pages operate +the hub, machine pages operate local file integration. Explicit constraint from the user: +today a Tailscale-bound GUI is unusable for some operations even WITH the admin token — +the design must fix remote GUI operability without collapsing the consent boundary. + +## In-repo evidence (verified 2026-08-27 on dev @ 8b1b65b8d) + +- Non-loopback bind forces the data token: `isApiAuthRequired` returns true whenever the + bind hostname is not loopback (src/server/auth-cors.ts:260-262), and startup refuses a + public bind without a configured data credential. +- The reported remote-GUI defect is real and structural: `issueGuiSession` returns null + when `isApiAuthRequired(config)` is true AND additionally requires a loopback Host + (src/server/management-auth.ts, issueGuiSession). So on a remote bind the principal + `gui-session` is unobtainable; consent-bearing routes that require + `ctx.principal === "gui-session"` (src/server/management/sidebar-routes.ts:42, + src/server/management/codex-prompt-routes.ts:298) answer 403 even to the admin token. + That 403 is BY DESIGN for the admin token (AGENTS.md user-consent boundary) — the defect + is only that a browser can never mint a session remotely. +- `managementRequestOrigin` returns null for a non-loopback Host when apiAuth is NOT + required (src/server/auth-cors.ts:118-129); when apiAuth IS required it derives the + origin from the request, which a TLS terminator breaks (http observed vs https public). +- The GUI attaches credentials only same-origin: `needsApiAuth` refuses absolute + cross-origin URLs (gui/src/api.ts:53-60). A two-plane GUI therefore needs an explicit + multi-target API layer, not a base-URL swap. +- The GUI needs a secure context in places: `crypto.subtle.digest` at + gui/src/log-conversation-id.ts:26, `navigator.clipboard` at + gui/src/oauth-health-display.ts:133 (with execCommand fallback). +- Injector already supports non-loopback targets: dedicated provider block with + `env_key = "OPENCODEX_API_AUTH_TOKEN"` and `model_catalog_json` requiring a LOCAL + absolute path (src/codex/inject.ts:186-247, 622+). +- `GET /api/catalog` and `GET /api/client-config` already exist behind management auth + (src/server/management/model-routes.ts:334-420). +- Headless OAuth exists: `oauthOpenBrowser: false` (src/oauth/open-browser-choice.ts) and + `POST /api/oauth/login/code` (src/server/management/oauth-account-routes.ts:208). +- Allowlist-listener precedent: the unauthenticated loopback listener enumerates exactly + the routes it serves (src/server/index.ts, loopbackRouteAllowed) — the machine-plane + listener should copy this failure mode (default-404). +- Token-file delivery precedent: `OCX_API_TOKEN_FILE` (src/lib/service-secrets.ts, + src/service.ts:1571+). +- CLI already talks to the management API over HTTP with injectable baseUrl + (src/cli/runtime-api.ts, RuntimeApiDeps.baseUrl) — client-mode remote management + commands are a URL + credential change, not a new client. + +## External evidence (Luna swarm, 3 lanes, sources opened 2026-08-27) + +Peer proxies separate UI sessions from master keys: +- LiteLLM: LITELLM_MASTER_KEY for API/admin, separate UI login minting expiring + virtual keys; per-user/per-device virtual keys with budgets, central key custody. + https://docs.litellm.com.cn/docs/proxy/ui , virtual_keys.md / access_control.md in + BerriAI/litellm-docs (opened 2026-08-27). +- sub2api: admin web UI uses JWT session; automation uses a separate global Admin API + Key (x-api-key). https://github.com/Wei-Shaw/sub2api (opened 2026-08-27). +- One API broken-access-control reports (#2410, #2423) show central key custody makes + route-level authz the main defense. + +Tailscale transport facts (official docs, verified dates in page footers): +- `tailscale serve` = tailnet-only reverse proxy to a localhost backend; injects + Tailscale-User-* identity headers; backend must bind loopback or headers are + spoofable. https://tailscale.com/docs/features/tailscale-serve +- `tailscale cert` issues public CA certs only for the ts.net FQDN (not bare MagicDNS + short names); names land in Certificate Transparency logs. + https://tailscale.com/docs/how-to/set-up-https-certificates +- Funnel is public-internet exposure (ports 443/8443/10000) — out of scope here. + +Browser platform facts (MDN/WHATWG/IETF, opened 2026-08-27): +- Plain-HTTP non-localhost origins are NOT secure contexts: no crypto.subtle, no + async clipboard, Secure cookies unavailable. http://localhost IS potentially + trustworthy. https://developer.mozilla.org/en-US/docs/Web/Security/Defenses/Secure_Contexts +- Header-token SPAs avoid ambient-cookie CSRF but still need exact-origin allowlists + and Origin checks on mutations (WHATWG Fetch; RFC 9700 OAuth BCP). +- RFC 8628 device flow is the reference pattern for headless-hub OAuth; ocx's + oauthOpenBrowser:false + /api/oauth/login/code is already equivalent in shape. + +## Design consequences (carried into 010) + +1. Two credential worlds stay separate: data-plane admission (client machines) vs + management (admin token / gui-session). Peers (LiteLLM, sub2api) validate this split. +2. Remote GUI needs a NEW session-issuance path, not a weakening of requireManagementAuth: + the loopback-only refusal in issueGuiSession is the single gate to generalize. +3. HTTPS via tailscale serve against a loopback-only management ingress is the + recommended browser path; plain-HTTP tailnet operation must exist as a documented + opt-in because usability on a private tailnet was the user's explicit complaint. +4. localhost:10100 client GUI + direct-to-hub shared plane is cross-origin; the hub + needs management CORS for an allowlisted client origin, or the client listener + relays. Both appear in 010 with the relay constrained to a fixed target. + diff --git a/devlog/_plan/260827_remote_hub/010_design.md b/devlog/_plan/260827_remote_hub/010_design.md new file mode 100644 index 0000000000..c295007e9d --- /dev/null +++ b/devlog/_plan/260827_remote_hub/010_design.md @@ -0,0 +1,216 @@ +# 010 — Design: remote hub mode (hub / client / two-plane GUI) + +Unit: 260827_remote_hub · Status: design draft (pre-audit) +Drafted by a sol-high subagent against dev @ 8b1b65b8d; every file:line claim +below re-verified by the main session on 2026-08-27. + +## 0. Runtime roles + +```text +standalone (default) today's behavior, untouched — no role configured, nothing changes +hub full server; provider keys/OAuth/routing/usage/logs live here +client no /v1 data plane, no provider adapters; thin loopback GUI + + machine integration plane; inference goes DIRECTLY to the hub +``` + +Client traffic never routes through the local listener — Codex/Claude talk straight to +`hub:10100/v1`. The client process is a remote control + file installer, idle otherwise. + +## 1. Goals / non-goals + +Goals: any machine as hub (Linux/systemd, macOS/launchd, Docker); clients keyless +(admission token only, never provider/admin credentials); single source of truth on the +hub; `ocx connect/disconnect/status`; dashboard always at localhost:10100 on clients; +remote GUI fully operable over Tailscale WITHOUT weakening the consent boundary; +injector/journal/restore reuse; in-repo (no separate repository). + +Non-goals: multi-hub replication/failover; provider execution on connected clients; +public-internet exposure preset (Funnel out of scope); generic reverse proxy in the +client listener; cryptographic human-click proof (AGENTS.md already concedes a local +process can drive a browser — the enforceable contract is that admin-token alone is +never promoted to gui-session). + +## 2. Architecture + +```text + HUB (any machine) + ┌───────────────────────────────────────┐ +Codex/Claude ──▶│ /v1/* (data token, x-opencodex-api-key)│ + │ providers · OAuth · routing · catalog │ + │ /api/* (shared management plane) │ + │ optional loopback mgmt ingress :10101 │◀─ tailscale serve (HTTPS) + └──────────────────┬────────────────────┘ + │ tailnet +┌──────────────────────────────────┴────────────────────────────┐ +│ CLIENT │ +│ browser → http://localhost:10100 │ +│ ├─ shared pages ──────────▶ hub /api/* (direct HTTPS │ +│ │ or fixed-target local relay) │ +│ └─ machine pages ──────────▶ localhost /api/machine/* │ +│ thin listener: GUI assets, machine API, relay; NO /v1 │ +│ derived files: config.toml · opencodex-catalog.json · journal │ +└───────────────────────────────────────────────────────────────┘ +``` + +Placement: new leaf `src/client/` (connection state, catalog fetch, machine listener) +plus a narrow protocol module. Core-path rule respected: router/lifecycle/responses-core +import nothing new; hub-side activation composes in `src/server/index.ts` and must not +add an await inside the guarded synchronous window (tests/core-lab-boundary.test.ts). + +## 3. Security model + +### Credential classes (unchanged classes, new scoping) + +| Credential | Lives | Grants | Consent authority | +|---|---|---|---| +| Provider keys / OAuth | hub only | upstream calls | — | +| Data admission token (env OPENCODEX_API_AUTH_TOKEN; per-client via config.apiKeys) | hub + that client | /v1/* only | none | +| Admin token | hub only | /api/*管理 | NEVER consent routes | +| gui-session | hub memory + browser | /api/* incl. consent routes | yes (origin+CSRF bound) | + +Per-client keys ride the existing `config.apiKeys` mechanism, still exported to Codex +as `OPENCODEX_API_AUTH_TOKEN` (env_key contract unchanged) → independent rotation and +per-machine attribution. This is the LiteLLM virtual-key / sub2api admin-key split, +which the research doc grounds. + +### The remote-GUI fix (the load-bearing change) + +Defect: `issueGuiSession` refuses when `isApiAuthRequired(config)` and demands a +loopback Host, so a remote bind can never mint the `gui-session` principal; consent +routes 403 even with the admin token. That refusal was correct when "remote" implied +"unprotected"; hub mode makes remote-with-credentials a first-class state. + +Change shape — generalize the session record, not the auth gate: + +```ts +interface GuiSessionRecord { + serverOrigin: string; // canonical hub management origin + browserOrigin: string; // page that owns the session (may be http://localhost:10100) + csrfToken: string; + expiresAt: number; + issuance: "loopback" | "tailscale-identity" | "pairing" | "trusted-tailnet"; +} +``` + +Validation keeps every current predicate (destination = serverOrigin, claimed GUI +origin = browserOrigin, mutations need browser Origin + per-session CSRF), just split +across two origins instead of assuming they are equal. `requireManagementAuth` and +`managementPrincipal` keep sharing one predicate. Admin token is NEVER an exchange +credential for a session — entering it still unlocks ordinary management, and consent +routes stay 403 until a real session exists. Boundary preserved. + +Issuance ladder (config-selected, strictest first): +1. loopback — today's path, unchanged. +2. tailscale-identity (recommended) — a loopback-only management ingress (:10101, + GUI + /api only, allowlist style like loopbackRouteAllowed) fronted by + `tailscale serve`; trust Tailscale-User-* headers ONLY on that ingress (Tailscale + strips inbound spoofs and requires a loopback backend — official docs). Browser gets + real HTTPS (ts.net cert), so secure-context features work. +3. pairing — `ocx gui pair` on the hub prints a single-use, short-TTL, origin-bound + grant that can only mint a session. For generic HTTPS terminators. +4. trusted-tailnet (documented opt-in: remoteGui.allowInsecureHttp + trustTailnet) — + exact configured origins on a plain-HTTP tailnet may bootstrap sessions. This is the + "don't over-harden" valve the user asked for: private tailnet, sole operator → + usable GUI with one config line, warned visibly, never default. + +Supporting changes: operator-configured `hub.managementPublicOrigin` (never derive the +public origin from forwarding headers — fixes today's TLS-terminator mismatch); +management CORS must allow x-opencodex-api-key / x-opencodex-gui-origin / +x-opencodex-csrf-token for allowlisted origins with exact-origin ACAO (currently +managementCorsHeaders never widens the header list, so a cross-origin GUI cannot even +preflight — verified src/server/auth-cors.ts:199-205). + +Secure-context reality (research doc): plain-HTTP remote origins lose crypto.subtle +(used in gui/src/log-conversation-id.ts:26) and async clipboard. Two-plane helps here: +the PAGE stays on http://localhost:10100 (a secure context), so local-plane features +keep working even when the hub side is plain HTTP via the relay. + +### Threat summary + +Compromised client → its own admission token + local files; NOT provider keys, admin +token, or other clients' keys. Compromised hub → everything (accepted: that's what +"hub" means; same posture as LiteLLM/sub2api). Tailnet membership ≠ admin identity: +data plane still needs the token, management still needs admin-token/session. + +## 4. ocx connect (client mode) + +```text +ocx connect [--management-url ] [--token-env NAME | --token-stdin] + [--clients codex,claude] [--management-transport direct|relay] [--no-sync] +ocx disconnect [--keep-catalog] ocx connect status [--json] +``` + +No `--token ` flag (argv/history leak). Local state = dumb pointer: +`{ serverUrl, managementUrl?, tokenEnv, selectedClients, managementTransport, +connectedAt, protocolVersion }`. Existing local provider config stays dormant → +disconnect is fully reversible offline (restore from injector journal, no hub needed). + +Connect is a transaction: validate URL → GET /readyz (version + protocol + advertised +managementUrl) → validate data credential → download catalog → injector preflight → +atomic catalog write → inject → persist state. Any failure before the end leaves the +machine untouched. + +Catalog: add data-authenticated `GET /v1/catalog` (same serializer as /api/catalog, +ETag/If-None-Match, bounded body) — a client must not hold the management token just to +sync models. Injector: generalize input to +`{ baseUrl, requiresAdmissionToken, tokenEnv }` — the loopback/non-loopback split in +inject.ts already carries 90% of this. `ocx sync` becomes mode-aware and NEVER falls +back to local provider discovery in client mode. Management CLI (`ocx models` 등) rides +RuntimeApiDeps.baseUrl toward the hub. Claude: launcher-scoped ANTHROPIC_BASE_URL + +ANTHROPIC_AUTH_TOKEN first; persistent settings.json mutation stays a machine-plane +opt-in with ownership records. + +## 5. Machine-plane listener (client, loopback-only) + +Explicit allowlist, default-404 (same failure mode as loopbackRouteAllowed): +/healthz · /readyz · GET /api/machine/status · GET /api/machine/clients · +POST /api/machine/sync · PUT /api/machine/clients/:id · POST /api/machine/shim/* · +POST /api/machine/disconnect · POST /api/machine/hub-relay/* (opt-in only). +Mutations need local gui-session + CSRF (auto-minted on loopback, today's flow). + +Relay constraints (it is NOT a proxy): fixed destination = client.managementUrl; +path allowlist (/api/* + session bootstrap); no caller-supplied host/scheme; redirects +rejected; hop-by-hop headers stripped; management size caps; nothing logged. + +GUI: replace the single same-origin `apiBase` assumption (gui/src/api.ts needsApiAuth +refuses cross-origin credentials today) with explicit shared/machine targets; pages map +to planes; hub-down leaves the shell + machine pages alive with one stable offline state. + +## 6. Deployment recipes + +- Oracle/systemd & Mac/launchd: existing `ocx service install` path; hostname = + tailscale IP; data token via env or OCX_API_TOKEN_FILE (existing mechanism, + src/lib/service-secrets.ts); management ingress loopback + tailscale serve; never + open :10100 on the cloud firewall. +- Docker: non-root; persistent ~/.opencodex volume; token as runtime secret via + OCX_API_TOKEN_FILE; tailscale sidecar or host TLS; /healthz + /readyz probes. +- Headless OAuth: oauthOpenBrowser:false → dashboard shows the auth URL → user finishes + in any browser → POST /api/oauth/login/code (both halves already exist; RFC 8628-shaped). + +## 7. Failure modes (contract) + +Hub down → clear CLI errors, machine GUI alive, NO local-provider fallback · catalog +refresh failure → keep last-known-good + stale age · token rotation → 401 with named +cause, token never printed · protocol major mismatch → refuse before any local write · +disconnect-while-hub-down → journal-based offline restore · plain-HTTP → relay + banner. + +## 8. Roadmap → 020_roadmap.md (6 dependency-ordered phases, one PABCD cycle each) + +## 9. Open questions for the maintainer + +1. First release: require tailscale-identity/pairing for remote sessions, trustTailnet + as advanced opt-in — or ship trustTailnet as the blessed tailnet default? +2. Per-client config.apiKeys mandatory at connect, or recommended-only? +3. One public URL for /v1+/api, or separate managementUrl acceptable? +4. /v1/catalog as the data-authenticated contract vs a scoped /api/catalog exception? +5. Hub mode: disable local Codex/Claude integration by default ("hub is also a client" + as explicit switch)? +6. Session TTL: keep 5-minute GUI sessions or add renewable browser grants for remote? +7. Plain-HTTP relay in the first stack, or hardening phase after HTTPS-direct is proven? + +## Riskiest three decisions + +Remote session issuance without weakening the consent principal; browser/server origin +split across direct+relay transports; injector generalization without regressing +journal/restore ownership. + diff --git a/devlog/_plan/260827_remote_hub/020_roadmap.md b/devlog/_plan/260827_remote_hub/020_roadmap.md new file mode 100644 index 0000000000..0b8da0d081 --- /dev/null +++ b/devlog/_plan/260827_remote_hub/020_roadmap.md @@ -0,0 +1,44 @@ +# 020 — Roadmap: remote hub phases (dependency-ordered, PHASE-SPLIT-01) + +Each phase = one PABCD cycle = one reviewable PR (stack against dev; children retarget +after parents land). Decade docs 030+ get diff-level detail when their cycle's P begins +(the P re-verifies against the then-current tree before executing). + +## Phase 1 — Foundations: protocol + catalog read path (doc 030) +Runtime role types (standalone/hub/client) in config; /readyz protocol metadata +{protocol, minimumClientProtocol, managementUrl}; data-authenticated GET /v1/catalog +sharing the /api/catalog serializer, ETag, bounded body. No GUI, no local writes. +Prove: /readyz secret-free; /v1/catalog auth matrix; byte-identical serialization vs +/api/catalog; core-lab-boundary green. + +## Phase 2 — Core security: remote gui-session + management CORS (doc 040) +serverOrigin/browserOrigin session records; hub.managementPublicOrigin; issuance modes +(loopback / tailscale-identity / pairing / trusted-tailnet); cross-origin bootstrap; +management preflight header allowlist; shared validation predicate; NO admin→session +exchange. Prove: remote HTTPS page mints session; consent routes 403 to admin-token but +200 to remote session; wrong origin/CSRF/expired/replay rejected; plain HTTP refused +unless opted in. Security-review-required phase (auth surface). + +## Phase 3 — Client core: connect/disconnect/sync + injector target (doc 050) +Connect transaction; client state; catalog download/atomic placement; CodexRoutingTarget +generalization; mode-aware sync (no silent fallback); Claude launcher target; offline +journal restore. Prove: no local write before checks pass; injected config byte-shape; +disconnect restores pre-connect state hub-down; standalone output byte-compatible. + +## Phase 4 — Integration: machine listener + two-plane GUI (doc 060) +Loopback allowlist listener; /api/machine/*; shared/machine API targets in GUI; +fixed-target relay; plane-aware offline/permission states. Prove: no /v1 on the +listener; mutations need session+CSRF; hub credentials only reach the hub origin; +hub-down UI renders; GUI build/lint/i18n + browser smoke on both transports. + +## Phase 5 — Deployment integration (doc 070) +Loopback management ingress on the hub; systemd/launchd via existing service installer; +Docker recipe (volume + OCX_API_TOKEN_FILE secret); tailscale serve docs; headless OAuth +walkthrough. Prove: all three targets pass health/ready/auth'd catalog/routed response/ +remote session smoke; identity headers unspoofable past the loopback backend. + +## Phase 6 — Hardening + release gate (doc 080) +Rotation UX; skew matrix; multi-client attribution; session invalidation/rate limits; +catalog adversarial tests; relay SSRF negatives; docs-site sync (5 locales); full +typecheck/test/privacy:scan/build:gui/lint:gui; MAINTAINERS security review. + From 83dd53c00881ce8e3af883c1e9ed5331919e0f67 Mon Sep 17 00:00:00 2001 From: jun Date: Thu, 27 Aug 2026 23:55:04 +0900 Subject: [PATCH 015/172] =?UTF-8?q?docs(devlog):=20fold=205=20audit=20bloc?= =?UTF-8?q?kers=20=E2=80=94=20drop=20header-only=20trusted-tailnet,=20add?= =?UTF-8?q?=20identity=20allowlist,=20name=20session=20consumers=20and=20/?= =?UTF-8?q?v1/catalog=20admission?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- devlog/_plan/260827_remote_hub/010_design.md | 50 ++++++++++++++++---- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/devlog/_plan/260827_remote_hub/010_design.md b/devlog/_plan/260827_remote_hub/010_design.md index c295007e9d..85639ec0b2 100644 --- a/devlog/_plan/260827_remote_hub/010_design.md +++ b/devlog/_plan/260827_remote_hub/010_design.md @@ -35,7 +35,8 @@ never promoted to gui-session). ```text HUB (any machine) ┌───────────────────────────────────────┐ -Codex/Claude ──▶│ /v1/* (data token, x-opencodex-api-key)│ +Codex/Claude ──▶│ /v1/* (data token: Bearer via env_key, │ + │ or x-opencodex-api-key — #1686) │ │ providers · OAuth · routing · catalog │ │ /api/* (shared management plane) │ │ optional loopback mgmt ingress :10101 │◀─ tailscale serve (HTTPS) @@ -105,20 +106,30 @@ Issuance ladder (config-selected, strictest first): GUI + /api only, allowlist style like loopbackRouteAllowed) fronted by `tailscale serve`; trust Tailscale-User-* headers ONLY on that ingress (Tailscale strips inbound spoofs and requires a loopback backend — official docs). Browser gets - real HTTPS (ts.net cert), so secure-context features work. + real HTTPS (ts.net cert), so secure-context features work. Identity is necessary + but NOT sufficient: the header proves who, an operator-configured + `remoteGui.allowedTailscaleUsers` allowlist decides whether that who may mint a + session. On a shared tailnet, an empty allowlist means nobody mints remotely. 3. pairing — `ocx gui pair` on the hub prints a single-use, short-TTL, origin-bound grant that can only mint a session. For generic HTTPS terminators. -4. trusted-tailnet (documented opt-in: remoteGui.allowInsecureHttp + trustTailnet) — - exact configured origins on a plain-HTTP tailnet may bootstrap sessions. This is the - "don't over-harden" valve the user asked for: private tailnet, sole operator → - usable GUI with one config line, warned visibly, never default. +4. insecure-http pairing (documented opt-in: `remoteGui.allowInsecureHttp`) — the SAME + single-use pairing grant as rung 3, allowed to travel over a plain-HTTP tailnet + origin. This is the "don't over-harden" valve the user asked for: private tailnet, + sole operator → run `ocx gui pair` once on the hub, paste the code, GUI works. + Audit note (blocker 1, folded): the earlier "trusted-tailnet" variant that minted + sessions from Host/Origin alone is DROPPED — headers are forgeable by anything with + TCP reach, so it would have granted consent routes with zero credential, strictly + weaker than the admin token. Issuance always consumes a real credential; only the + transport hardening is relaxable, and the relaxation is loudly warned. Supporting changes: operator-configured `hub.managementPublicOrigin` (never derive the public origin from forwarding headers — fixes today's TLS-terminator mismatch); management CORS must allow x-opencodex-api-key / x-opencodex-gui-origin / x-opencodex-csrf-token for allowlisted origins with exact-origin ACAO (currently -managementCorsHeaders never widens the header list, so a cross-origin GUI cannot even -preflight — verified src/server/auth-cors.ts:199-205). +managementCorsHeaders calls corsHeaders() without the request, so the echo path never +engages — verified src/server/auth-cors.ts:199-206. x-opencodex-api-key is already in +STATIC_ALLOWED_REQUEST_HEADERS; the two headers genuinely missing from preflight are +x-opencodex-gui-origin and x-opencodex-csrf-token, read at management-auth.ts:469/475). Secure-context reality (research doc): plain-HTTP remote origins lose crypto.subtle (used in gui/src/log-conversation-id.ts:26) and async clipboard. Two-plane helps here: @@ -196,6 +207,28 @@ disconnect-while-hub-down → journal-based offline restore · plain-HTTP → re ## 8. Roadmap → 020_roadmap.md (6 dependency-ordered phases, one PABCD cycle each) +### Phase-2 consumer chain (audit blocker 3, folded) + +GuiSessionRecord.origin is not private state. The serverOrigin/browserOrigin split must +enumerate and update, in doc 040 before Phase 2's P: +- src/server/index.ts:1609-1614 serveSessionBootstrap + the opencodex-session-origin + meta-tag contract in gui-static serving; +- gui/src/api.ts:94-96 and 154-156 (memorySessionOrigin validation, + SESSION_REBOOTSTRAP_PATH reader); +- tests/native-profile-route-security.test.ts:136; +- tests/server-management-auth.test.ts:897 ("non-loopback binding never issues a GUI + session from a forged loopback Host") must stay green: every new issuance mode is + strictly config-opt-in, defaults byte-identical to today. + +### /v1/catalog admission contract (audit blocker 4, folded) + +/v1/catalog uses the data-plane admission matrix as-is: x-opencodex-api-key OR a +Bearer that is one of our admission secrets (AUTH_MATRIX, auth-cors.ts:397-406 — the +#1686 substitution rule; the injector's env_key emits Bearer, inject.ts:231-237). +No Direct-passthrough route exists on this path, so no reservation conflict; the only +integration concern is route ordering ahead of the unknown-/v1 JSON-404 guard +(index.ts:1604). + ## 9. Open questions for the maintainer 1. First release: require tailscale-identity/pairing for remote sessions, trustTailnet @@ -213,4 +246,3 @@ disconnect-while-hub-down → journal-based offline restore · plain-HTTP → re Remote session issuance without weakening the consent principal; browser/server origin split across direct+relay transports; injector generalization without regressing journal/restore ownership. - From 66dcb298c663f93dd652c92c9eed2e92067fc661 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 00:11:23 +0900 Subject: [PATCH 016/172] =?UTF-8?q?docs(devlog):=20interview=20record=20?= =?UTF-8?q?=E2=80=94=20full-scope=20stacked=20delivery,=20dogfood=20compat?= =?UTF-8?q?,=20per-machine=20usage=20requirement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../_plan/260827_remote_hub/001_interview.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 devlog/_plan/260827_remote_hub/001_interview.md diff --git a/devlog/_plan/260827_remote_hub/001_interview.md b/devlog/_plan/260827_remote_hub/001_interview.md new file mode 100644 index 0000000000..cdb0c1e156 --- /dev/null +++ b/devlog/_plan/260827_remote_hub/001_interview.md @@ -0,0 +1,33 @@ +# 001 — Interview record (2026-08-28) + +Answers captured from the maintainer (session 01a0439a, I-phase round 2): + +- **Scope: ALL 6 phases, full implementation including hardening (P6).** Delivery as a + stacked PR chain grown from this branch (codex/remote-hub-design is the stack base; + each phase PR targets the previous head; retarget to dev as parents land — + DEV-STACK / enforce-target child rules). +- Q2 (plain-HTTP pairing): accepted — rung 4 ships in Phase 2 with rung 3. +- Q3 (per-client keys): recommendation accepted BUT see new usage requirement below, + which pulls toward auto-issuing per-client keys at connect. +- Q4 (URL split): accepted — separate managementUrl allowed, /readyz advertises it. +- Q5 (remote session TTL): accepted — renewable long-lived remote sessions. +- Q6 (hub local integration): accepted — hub does not inject locally by default. +- Q7 (Claude): launcher-scope first confirmed; maintainer notes it is machine-local + anyway — clean separation is the requirement, not persistent integration. +- Q8 (deployment): **dogfood on clisu-oracle as part of this work**, AND the protocol + must tolerate release-build peers: a released client against a dev-build hub (and + the reverse) must interoperate "어느정도" — i.e. protocol-version negotiation in + /readyz is a hard requirement, not polish (Phase 1 scope). +- **NEW requirement (usage attribution):** the client GUI usage page should reflect + "my machine's usage" while connected, and after `ocx disconnect` the GUI (back in + standalone mode) shows the local proxy's own usage again. Feasibility confirmed in + code: usage attempts already persist `apiKeyId` for configured-key admissions + (src/server/management/api-key-usage.ts:78-89, admissionFields in + src/server/auth-cors.ts:369-375), so a per-client filtered usage view is a query + over existing data — it requires the machine to authenticate with its OWN key, + which is why connect should default to per-client key issuance. + +Open contradiction (to resolve this round): shared-token-allowed (Q3 answer) vs +per-machine usage view (new requirement) — attribution is keyed on apiKeyId, so a +shared token collapses all machines into one bucket. + From a7d49f3d8b534d05c4c784b1791a72896412ae3e Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 00:14:03 +0900 Subject: [PATCH 017/172] =?UTF-8?q?docs(devlog):=20interview=20round=203?= =?UTF-8?q?=20=E2=80=94=20per-client=20key=20auto-issue=20via=20token=20fi?= =?UTF-8?q?le,=20protocol=20compat=20floor,=20contradictions=20closed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../_plan/260827_remote_hub/001_interview.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/devlog/_plan/260827_remote_hub/001_interview.md b/devlog/_plan/260827_remote_hub/001_interview.md index cdb0c1e156..2024eae666 100644 --- a/devlog/_plan/260827_remote_hub/001_interview.md +++ b/devlog/_plan/260827_remote_hub/001_interview.md @@ -31,3 +31,34 @@ Open contradiction (to resolve this round): shared-token-allowed (Q3 answer) vs per-machine usage view (new requirement) — attribution is keyed on apiKeyId, so a shared token collapses all machines into one bucket. +## Round 3 answers (2026-08-28) + +- **Q-A = a (auto-issue per-client key at connect).** Storage question resolved in + code: the key is NEVER written to config.toml (env_key contract); it lands in the + existing owner-only token file (serviceApiTokenFilePath, src/lib/service-secrets.ts:5, + 0600 + ACL hardening) which the shim already reads into OPENCODEX_API_AUTH_TOKEN when + the env is empty (src/codex/shim.ts:699-701 unix, :1000-1001 batch, :1043 ps). + disconnect deletes the file. The shared-token-vs-attribution contradiction is CLOSED: + per-client keys are the connect default, so per-machine usage attribution works. +- **Q-C = a.** Protocol v1 negotiated via /readyz; same-major interop with + feature-detection; guaranteed pair = dev hub ↔ latest release client; older peers get + an explicit "hub protocol too new/old, upgrade ocx" error. Phase 1 hard requirement. +- **Q-B: OPEN ASSUMPTION (low)** — usage page default while connected = "this machine" + slice with a toggle to hub-wide; not answered explicitly, adopting the recommended + default; reversible in Phase 4 GUI work. + +## Final contradiction rescan (round 3) + +- Shared-token vs attribution: RESOLVED (per-client default; shared token remains a + degraded documented mode where usage collapses into one bucket). +- Pairing-grant issuance vs POST /api/keys authority: connect needs admin-class + authority ONCE — satisfied by pairing code (rung 3/4) or admin token; neither is + persisted on the client. No contradiction. +- Dogfood release-compat vs stacked delivery: protocol version lives in Phase 1 (stack + base), so every later phase rides it. No ordering conflict. +- Remaining OPEN ASSUMPTIONS: Q-B default; session TTL exact value (12h sliding, + tunable); relay streaming backpressure deferred to Phase 6. + +Interview readiness: Goal/Constraint/Success/Ontology all covered by asked-and-answered +rounds 1-3. Ready for I -> P. + From 3920954f0862805b1f1d9fbd7fe56d5f235c642c Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 00:16:01 +0900 Subject: [PATCH 018/172] =?UTF-8?q?docs(devlog):=20interview=20round=204?= =?UTF-8?q?=20=E2=80=94=20usage=20renders=20from=20whichever=20store=20ser?= =?UTF-8?q?ved=20the=20traffic;=20no=20mirroring?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- devlog/_plan/260827_remote_hub/001_interview.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/devlog/_plan/260827_remote_hub/001_interview.md b/devlog/_plan/260827_remote_hub/001_interview.md index 2024eae666..50fec74e3e 100644 --- a/devlog/_plan/260827_remote_hub/001_interview.md +++ b/devlog/_plan/260827_remote_hub/001_interview.md @@ -62,3 +62,14 @@ shared token collapses all machines into one bucket. Interview readiness: Goal/Constraint/Success/Ontology all covered by asked-and-answered rounds 1-3. Ready for I -> P. +## Round 4 answer (2026-08-28) — usage rendering settled + +Maintainer's rule, adopted verbatim as the design: **connected → render the hub's +usage (my apiKeyId slice); not connected → render the local usage.jsonl.** No local +mirroring of the connect-period usage (option b rejected as unnecessary complexity); +the connect-period history lives on the hub and is visible there. Grounding: +usage persists where the serving proxy runs (appendUsageEntry → +~/.opencodex/usage.jsonl, src/usage/log.ts:166-167, 521-523), so this rule is just +"render the store that actually recorded the traffic" — zero data duplication, +no schema change. Q-B default (this-machine slice with hub-wide toggle) stands as +the connected view's default. From d225b2d24d5c22e4f4cd2968ec88c302144eb05d Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 00:53:32 +0900 Subject: [PATCH 019/172] =?UTF-8?q?docs(devlog):=20remote=20hub=20decade?= =?UTF-8?q?=20docs=20030-080=20=E2=80=94=20diff-level=20roadmap=20for=20al?= =?UTF-8?q?l=20six=20phases?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../030_phase1_protocol_catalog.md | 261 ++++++++ .../040_phase2_remote_session.md | 476 +++++++++++++ .../260827_remote_hub/050_phase3_connect.md | 530 +++++++++++++++ .../260827_remote_hub/060_phase4_two_plane.md | 499 ++++++++++++++ .../260827_remote_hub/070_phase5_deploy.md | 483 +++++++++++++ .../260827_remote_hub/080_phase6_hardening.md | 633 ++++++++++++++++++ 6 files changed, 2882 insertions(+) create mode 100644 devlog/_plan/260827_remote_hub/030_phase1_protocol_catalog.md create mode 100644 devlog/_plan/260827_remote_hub/040_phase2_remote_session.md create mode 100644 devlog/_plan/260827_remote_hub/050_phase3_connect.md create mode 100644 devlog/_plan/260827_remote_hub/060_phase4_two_plane.md create mode 100644 devlog/_plan/260827_remote_hub/070_phase5_deploy.md create mode 100644 devlog/_plan/260827_remote_hub/080_phase6_hardening.md diff --git a/devlog/_plan/260827_remote_hub/030_phase1_protocol_catalog.md b/devlog/_plan/260827_remote_hub/030_phase1_protocol_catalog.md new file mode 100644 index 0000000000..1765944173 --- /dev/null +++ b/devlog/_plan/260827_remote_hub/030_phase1_protocol_catalog.md @@ -0,0 +1,261 @@ +# 030 — Phase 1: protocol negotiation and data-plane catalog + +Unit: `260827_remote_hub` · Branch: `codex/remote-hub-design` · Phase: 1 · Status: diff-level plan · Work class: C4 + +This phase establishes the smallest release-compatible wire contract needed before any +client writes local files. It adds no connect command, no remote GUI, and no client-mode +runtime behavior. All code paths remain standalone-compatible when `runtimeRole` is absent. + +## 1. Outcome and fixed contract + +- Persisted runtime role key: `runtimeRole?: "standalone" | "hub" | "client"`. + Absence resolves to `"standalone"`; `getDefaultConfig()` does not start writing the key + into existing files. +- Protocol constants: `REMOTE_HUB_PROTOCOL = 1` and + `MINIMUM_REMOTE_CLIENT_PROTOCOL = 1`. +- Exact unauthenticated `GET /readyz` keeps its current status/identity fields and adds: + + ```json + { + "protocol": 1, + "minimumClientProtocol": 1, + "managementUrl": "https://hub.example.ts.net" + } + ``` + + `managementUrl` is the canonical origin observed by this request in Phase 1. Phase 2 + changes only its source for hub deployments by preferring + `hub.managementPublicOrigin`; the field and parser do not change. +- Data-authenticated exact `GET /v1/catalog` returns the same serialized catalog bytes as + `GET /api/catalog`, with a strong ETag calculated from those bytes and conditional + `If-None-Match` support. +- `/v1/catalog` admits only the two forms used by the Codex injector contract: + `x-opencodex-api-key: ` or `Authorization: Bearer `. + `x-api-key`, a foreign bearer, the admin token, and no credential are rejected on a + non-loopback bind. The row is added to `AUTH_MATRIX` with `xApiKey: "rejected"`. +- A serialized catalog larger than `MAX_REMOTE_CATALOG_BYTES = 32 * 1024 * 1024` is not + returned over `/v1/catalog`; it fails with HTTP 503 and the stable code + `catalog_too_large`. The management route continues to expose the same serialized bytes + for local diagnosis, so the bound does not hide the operator's recovery surface. + +## 2. IN / OUT + +### IN + +- Runtime-role type, read validation, write validation, and explicit default resolver. +- Protocol-v1 metadata in every ready/pending/failed `/readyz` body. +- A parser/compatibility predicate for future `ocx connect`, including additive-field + tolerance for a dev hub paired with the latest released client. +- Shared catalog serialization, ETag, `If-None-Match`, size cap, and data-plane admission. +- Route placement before the unknown-`/v1/*` JSON-404 guard. +- Focused and full remote-only verification commands. + +### OUT + +- `ocx connect`, client state, per-client key issuance to the owner-only + `serviceApiTokenFilePath` file, catalog installation, inject/restore, usage filtering, + and any machine listener (Phases 3–4). No client admission key is written to config. +- `hub.managementPublicOrigin`, remote GUI sessions, pairing, Tailscale identity, and + management CORS (Phase 2). +- Provider discovery or catalog regeneration. This endpoint serves the current persisted + Codex catalog only. +- Protocol v2 design, multi-hub negotiation, server-side downgrade, and silent fallback to + local providers. +- Any import from the new remote modules into `src/router.ts`, + `src/server/lifecycle.ts`, or `src/server/responses/core.ts`. + +## 3. Wire and compatibility contract + +### 3.1 Readiness shape + +`/readyz` remains exact `GET`, unauthenticated, and 200 only for `status: "ready"`; pending, +failed, and draining remain 503 with `Retry-After: 1`. The new fields are public protocol +metadata only—no path, warning, provider, account, key id, or config payload is exposed. + +The current latest-release readiness parser is already additive-field tolerant +(`validateReadyzBody` reads named fields rather than rejecting unknown keys in +`src/server/proxy-liveness.ts:275-297`). Therefore a protocol-v1 dev hub remains a valid +readiness target for the latest released client. New clients parse the three protocol +fields separately before any connect-side mutation. + +`managementUrl` rules in this phase: + +- It is an HTTP(S) origin only: no path other than `/`, no query, fragment, or userinfo. +- It is derived from the request URL/Host using the same canonical-origin rules as the + current management surface. +- It is present for standalone, hub, and client roles, because old/new process discovery + must not branch on shape. Phase 3 decides whether a client role may serve `/readyz`. +- It never trusts `Forwarded` or `X-Forwarded-*`. Phase 2's configured public origin is the + only TLS-terminator override. + +### 3.2 Version parser and exact mismatch strings + +The parser accepts additional unknown keys but validates these required values as positive +safe integers and an HTTP(S) origin. Compatibility is an interval intersection: + +```text +hub.protocol >= client.minimumHubProtocol +client.protocol >= hub.minimumClientProtocol +``` + +The v1 client constants are both `1`. Fail before catalog fetch and before any local write. +Exact user-visible strings: + +- Hub requires a newer client: + `OpenCodex hub requires remote protocol {hubMinimum}; this client supports protocol {clientProtocol}. Upgrade ocx on this client.` +- Hub is too old for the client: + `OpenCodex hub provides remote protocol {hubProtocol}; this client requires at least {clientMinimum}. Upgrade ocx on the hub.` +- Missing/malformed metadata: + `OpenCodex hub returned invalid remote protocol metadata; upgrade or repair ocx on the hub.` + +There is no optimistic assumption that a missing field means v1. Old hubs are explicit +incompatibility for `ocx connect`, while their ordinary standalone readiness remains usable. + +### 3.3 Catalog bytes, cache, and admission + +One function reads the current catalog, serializes it exactly once with +`JSON.stringify(catalog)`, and returns the resulting UTF-8 bytes. Both routes consume that +result. The test oracle compares the two route bodies byte-for-byte; it does not derive an +expected body by calling the serializer twice. + +The ETag is `"sha256-"` over the exact UTF-8 response bytes. A matching +strong tag, weak spelling of that same tag, a comma-list containing it, or `*` returns 304 +with ETag and no body. A stale/malformed `If-None-Match` returns 200. ETag is computed only +after the 32 MiB bound passes. + +`/v1/catalog` uses `resolveResponsesApiAuth(req, policy)`, not `resolveApiAuth`, because the +former is the existing dedicated-header/our-secret-Bearer matrix and rejects `x-api-key` +(`src/server/auth-cors.ts:465-478`). It performs the existing data-plane origin check after +admission. No Direct passthrough exists on this read-only route and no credential is +forwarded. + +## 4. Diff-level file-change map + +All paths below exist in the current tree except the two files marked **NEW**. + +| Action | Exact path | Diff-level change | +|---|---|---| +| MODIFY | `src/types/config.ts` | Export `OcxRuntimeRole`; add optional `runtimeRole` to `OcxConfig` beside bind/runtime settings. | +| MODIFY | `src/config.ts` | Add role schema and `runtimeRole` field validation; export `runtimeRole(config)`; reject invalid live candidates while preserving absence as standalone. Add degraded persisted-value diagnostics without deleting providers or `apiKeys`. | +| NEW | `src/remote/protocol.ts` | Own protocol constants, readiness metadata type/parser, management-origin validation, compatibility result, and exact mismatch strings. This is a passive leaf and imports no router, lifecycle, Responses, provider, or Lab code. | +| NEW | `src/server/catalog-download.ts` | Own `MAX_REMOTE_CATALOG_BYTES`, one persisted-catalog serialization result, byte-derived ETag matching, `/api/catalog` response construction, and bounded `/v1/catalog` response construction. | +| MODIFY | `src/server/management/model-routes.ts` | Replace the inline `/api/catalog` read/`JSON.stringify` block at lines 334–345 with the shared response builder; preserve 404 and `x-opencodex-codex-version`. | +| MODIFY | `src/server/auth-cors.ts` | Add the `/v1/catalog` `AUTH_MATRIX` row with bearer/dedicated accepted and `xApiKey` rejected. Do not change any existing row or credential precedence. | +| MODIFY | `src/server/index.ts` | Add protocol metadata to the current `/readyz` body at lines 991–998. Mount exact `GET /v1/catalog` after readiness/management handling and before the unknown-`/v1/*` guard at line 1604; use `resolveResponsesApiAuth` plus the existing origin policy. Keep `startServer` synchronous and add no `await` between `Bun.serve` and `labActivationRequired`. | +| MODIFY | `src/server/proxy-liveness.ts` | Keep existing identity/status parsing additive; extend the internal body type/comments so protocol fields are recognized but do not make ordinary `ocx ready` reject a v0/legacy standalone server. Remote compatibility remains in `src/remote/protocol.ts`. | +| MODIFY | `tests/config.test.ts` | Extend the existing config-default/validation sibling tests with absent/default, three valid roles, malformed live candidate, and malformed persisted role preservation cases. | +| MODIFY | `tests/server-live.test.ts` | Extend the existing `GET /readyz` suite (lines 1220+) for exact protocol values on ready/pending/failed/draining, sanitized keys, management origin, method/path negatives, and no-auth behavior. | +| MODIFY | `tests/proxy-liveness.test.ts` | Extend the current strict readiness parser/probe tests to prove additive protocol fields neither invalidate readiness nor bypass identity/status checks. | +| MODIFY | `tests/api-catalog-route.test.ts` | Extend the existing `/api/catalog` sibling suite with fixed fixture bytes and version-header preservation after serializer extraction. | +| MODIFY | `tests/server-auth.test.ts` | Extend live-server auth/order coverage with `/v1/catalog` header matrix, exact-path/method negatives, foreign/admin bearer rejection, bound overflow, ETag/304, and unknown-`/v1` guard preservation. | +| MODIFY | `tests/api-key-attribution.test.ts` | Extend the existing live `AUTH_MATRIX` loop so `/v1/catalog` is a GET route and each cell reaches the real handler rather than the generic 404. | + +No other production or test file is in scope. If implementation proves another path is +required, stop the phase and amend this document before editing it. + +## 5. New and changed signatures + +```ts +// src/types/config.ts +export type OcxRuntimeRole = "standalone" | "hub" | "client"; + +export interface OcxConfig { + runtimeRole?: OcxRuntimeRole; +} + +// src/config.ts +export function runtimeRole(config: Pick): OcxRuntimeRole; + +// src/remote/protocol.ts +export const REMOTE_HUB_PROTOCOL = 1; +export const MINIMUM_REMOTE_CLIENT_PROTOCOL = 1; + +export interface RemoteReadyMetadata { + protocol: number; + minimumClientProtocol: number; + managementUrl: string; +} + +export type RemoteProtocolCompatibility = + | { ok: true; metadata: RemoteReadyMetadata } + | { ok: false; reason: "invalid" | "hub-too-new" | "hub-too-old"; message: string }; + +export function readyProtocolMetadata(req: Request): RemoteReadyMetadata; +export function parseRemoteReadyMetadata(value: unknown): RemoteReadyMetadata | null; +export function checkRemoteProtocolCompatibility( + value: unknown, + client?: { protocol: number; minimumHubProtocol: number }, +): RemoteProtocolCompatibility; + +// src/server/catalog-download.ts +export const MAX_REMOTE_CATALOG_BYTES = 32 * 1024 * 1024; + +export interface SerializedCatalog { + bytes: Uint8Array; + codexVersion?: string; +} + +export async function serializePersistedCatalog(): Promise; +export function catalogEtag(bytes: Uint8Array): string; +export function catalogManagementResponse( + catalog: SerializedCatalog | null, + req: Request, + config: OcxConfig, +): Response; +export function catalogDataPlaneResponse( + catalog: SerializedCatalog | null, + req: Request, + policy: RequestPolicyView, +): Response; +``` + +The shared serializer returns `null` only for the current “catalog not found” state. Read, +parse, or serialization errors remain bounded server failures; they are not converted into +an empty catalog. No function accepts a caller-provided catalog path. + +## 6. Acceptance criteria with activation grounding + +| ID | Constructible activation scenario | Required result / oracle | +|---|---|---| +| P1-A01 | Load config with no `runtimeRole`. | `runtimeRole(config) === "standalone"`; saved bytes are not rewritten merely by reading. | +| P1-A02 | Validate each explicit role through `validateConfigCandidate`. | `standalone`, `hub`, and `client` are accepted and preserved exactly. | +| P1-A03 | Validate/write an unknown role, then separately load a hand-edited unknown role fixture containing provider and API-key sentinels. | Live write is rejected with a path-specific error; persisted recovery preserves unrelated provider/key state and emits a non-secret diagnostic. | +| P1-A04 | Start a server with a pending gate and request exact unauthenticated `GET /readyz`; repeat after ready, failed, and drain activation. | Existing HTTP/status/Retry-After contract holds and all three protocol fields remain identical across states. | +| P1-A05 | Send POST, OPTIONS, `/readyz/`, and encoded `/readyz%2F`. | Existing deterministic JSON 404 path remains; no protocol document leaks through the GUI fallback. | +| P1-A06 | Feed a v1 document plus unknown future fields to the new parser and to `validateReadyzBody`. | Both accept the document; readiness identity remains strict and remote parser preserves only validated protocol fields. | +| P1-A07 | Feed `{protocol: 1, minimumClientProtocol: 2}` to a v1 client. | `hub-too-new` and the exact “Upgrade ocx on this client” string are returned before catalog access. | +| P1-A08 | Feed `{protocol: 0, minimumClientProtocol: 0}` to a client requiring hub protocol 1. | `hub-too-old` and the exact “Upgrade ocx on the hub” string are returned. | +| P1-A09 | Omit, mistype, overflow, or give a path-bearing `managementUrl`. | `invalid` and the exact malformed-metadata string are returned; no fallback to protocol 1. | +| P1-A10 | Persist a fixed catalog fixture; call authorized `/api/catalog` and authorized `/v1/catalog`. | Status 200 and response bytes are byte-identical; the ETag independently hashes those bytes. | +| P1-A11 | Repeat `/v1/catalog` on non-loopback with dedicated header, our-secret Bearer, `x-api-key`, foreign Bearer, admin token, and no token. | First two reach 200; all remaining cases are 401. No case reaches the generic unknown-route 404. | +| P1-A12 | Call `/v1/catalog` with matching tag, weak matching tag, tag list, `*`, stale tag, and malformed tag. | Matches return 304/no body/same ETag; stale or malformed values return 200/full bytes. | +| P1-A13 | Serialize exactly the cap and cap+1 fixtures through an injected serialization seam. | Exact cap returns 200; cap+1 returns 503 `catalog_too_large`, never a partial body. | +| P1-A14 | Call POST `/v1/catalog`, GET `/v1/catalog/`, and an unrelated `/v1/does-not-exist`. | Every request returns the existing JSON 404 envelope; route ordering does not widen path/method matching. | +| P1-A15 | Run the import-graph and synchronous-window guard after the diff. | No new subsystem is reachable from the three protected core files; `startServer` remains non-async and its guarded window contains no top-level `await`. | + +## 7. Verification — remote only on `lidge-ai` + +Do not run Bun tests, typecheck, or privacy/full-suite gates on the local Mac. The remote +checkout must contain the phase branch and run as the ordinary `lidgeai` user, not root. + +Focused implementation gate: + +```bash +ssh lidge-ai 'cd ~/Developer/opencodex && bun run typecheck && bun test tests/config.test.ts tests/server-live.test.ts tests/proxy-liveness.test.ts tests/api-catalog-route.test.ts tests/server-auth.test.ts tests/api-key-attribution.test.ts tests/core-lab-boundary.test.ts' +``` + +Review-ready shared-server gate: + +```bash +ssh lidge-ai 'cd ~/Developer/opencodex && bun run test && bun run privacy:scan' +``` + +Record the remote commit, Bun version, command, exit code, and pass/fail counts in the phase +evidence ledger. Do not repeat a passing command unless code covered by it changes. + +## 8. Completion boundary + +Phase 1 is complete only when every acceptance row has remote evidence and the route is a +real authenticated catalog response, not merely a health response. Do not begin client-side +writes in this phase. Any protocol-field rename after Phase 1 is a compatibility change and +requires an explicit protocol-version decision. diff --git a/devlog/_plan/260827_remote_hub/040_phase2_remote_session.md b/devlog/_plan/260827_remote_hub/040_phase2_remote_session.md new file mode 100644 index 0000000000..9b0534e1b5 --- /dev/null +++ b/devlog/_plan/260827_remote_hub/040_phase2_remote_session.md @@ -0,0 +1,476 @@ +# 040 — Phase 2: remote GUI session issuance and management CORS + +Unit: `260827_remote_hub` · Branch: `codex/remote-hub-design` · Phase: 2 · Status: diff-level plan · Work class: C4 + +> **SECURITY REVIEW REQUIRED.** This phase changes authentication, session issuance, +> origin binding, CSRF enforcement, CORS, and a consent-bearing principal. It must receive +> the explicit security review required by `AGENTS.md` and `MAINTAINERS.md` before merge. + +## 1. Outcome and non-negotiable boundary + +Remote hub dashboards can obtain an origin-bound `gui-session` through one of four +evidence paths, ordered from strongest automatic path to explicit opt-in: + +1. `loopback` — current behavior, unchanged and fixed at five minutes. +2. `tailscale-identity` — trusted Tailscale Serve ingress plus exact + `remoteGui.allowedTailscaleUsers` membership. +3. `pairing` — a short-lived, origin-bound, single-use grant printed by `ocx gui pair`. +4. `insecure-http-pairing` — the same grant over non-loopback HTTP only when + `remoteGui.allowInsecureHttp === true`. + +The admin token remains an ordinary management principal. It cannot create a pairing grant, +is never accepted by the session bootstrap/exchange endpoint, is never re-labeled as +`gui-session`, and consent routes continue to reject it. `ocx gui pair` uses an attested, +process-bound, operation-only capability to create a separate one-time credential; +consumption of that credential is the only pairing exchange. + +## 2. Threat model and must-pass controls + +### Assets + +- Provider/OAuth credentials and hub-wide config. +- Admin token, GUI session token, CSRF token, and pairing grant. +- Consent-bearing actions guarded by `principal === "gui-session"`. +- Tailscale identity headers and the configured public management origin. + +### Entrypoints and attackers + +- Browser navigation/fetch to `/opencodex-session`. +- Management preflight and `/api/*` requests. +- Local `ocx gui pair` attestation and operation-capability request. +- Anonymous tailnet peer, allowlisted tailnet peer, process holding only the data key, + process holding only the admin token, compromised browser origin, replay attacker, and a + direct caller spoofing `Tailscale-User-*` against the public listener. + +### Trust boundaries and controls + +- Browser origin and server destination are separate facts; neither is inferred from the + other. +- Tailscale headers are trusted only when the listener supplies an unforgeable + `trustedTailscaleIngress: true` context. Direct/public-listener headers are ignored. +- An empty/missing `allowedTailscaleUsers` list authorizes nobody remotely. +- Pairing grants are stored only as SHA-256 digests, capped, expire after five minutes, + are deleted before session minting, and are never logged or returned again. +- Pairing-grant creation accepts only a short-lived capability bound to the exact runtime + PID, port, method, path, nonce, expiry, and canonical browser origin. The reusable admin + token and every other management principal are rejected on that route. +- Remote sessions use a separate 12-hour sliding TTL and renew only after the complete + destination + browser-origin + CSRF predicate succeeds. Failed requests never renew. +- Plain non-loopback HTTP is denied for all automatic issuance and for pairing unless the + explicit opt-in is true. The opt-in does not relax origin, grant, CSRF, or replay checks. +- `requireManagementAuth` and `managementPrincipal` consume one shared admission result; + there is no second “token exists in map” predicate that can disagree with authorization. + +## 3. IN / OUT + +### IN + +- `GuiSessionRecord.serverOrigin` / `browserOrigin` split and full server/GUI consumer chain. +- Config validation for `hub.managementPublicOrigin`, + `remoteGui.allowedTailscaleUsers`, and `remoteGui.allowInsecureHttp`. +- Automatic loopback and trusted-Tailscale issuance; pairing grant creation and exchange. +- Separate loopback/remote TTLs and sliding renewal for remote sessions. +- Exact management CORS header widening for GUI-origin and CSRF headers. +- CLI `ocx gui pair` grant creation through the existing runtime-attestation pattern; no + grant or reusable admin credential in argv, config, disk, logs, or shell history. +- Backend and GUI regressions for every positive and negative issuance path. + +### OUT + +- The production loopback-only Tailscale Serve management listener and deployment recipe + (Phase 5). Phase 2 implements and tests the trusted-ingress policy through an explicit + request context; the public listener always passes `false` until Phase 5 supplies the + dedicated listener. +- Client machine listener, shared/machine API target routing, fixed-target relay, pairing + form/banner, and hub-down UI (Phase 4). Phase 2 establishes the session wire contract the + Phase 4 UI consumes. +- Per-client data-key issuance, `serviceApiTokenFilePath` writes/deletes, connect state, + catalog installation, and usage filtering (Phase 3/4). +- Any usage mirroring. Phase 4 reads the hub's `apiKeyId` slice while connected and the + local `usage.jsonl` while standalone; traffic is rendered from the store that served it. +- Cookies, JWTs, persisted refresh tokens, trusted-Host-only issuance, Funnel/public + internet exposure, or a generic reverse proxy. +- Rate-limit policy beyond bounded grant/session maps; Phase 6 adds operational rate limits. +- Any import into `src/router.ts`, `src/server/lifecycle.ts`, or + `src/server/responses/core.ts` from the new GUI-session module. + +## 4. Config contract + +```ts +// src/types/config.ts +export interface OcxHubConfig { + managementPublicOrigin?: string; +} + +export interface OcxRemoteGuiConfig { + allowedTailscaleUsers?: string[]; + allowInsecureHttp?: boolean; +} + +export interface OcxConfig { + hub?: OcxHubConfig; + remoteGui?: OcxRemoteGuiConfig; +} +``` + +Validation rules: + +- Remote issuance requires `runtimeRole === "hub"`; config keys may round-trip before the + role is activated, but they grant nothing in standalone/client roles. +- `hub.managementPublicOrigin` is a canonical `http:` or `https:` origin with no userinfo, + non-root path, query, or fragment. Persist the normalized `URL.origin` spelling. +- `remoteGui.allowedTailscaleUsers` contains at most 64 unique, trimmed, non-empty strings, + each at most 320 UTF-8 bytes and containing no ASCII control character. Matching is exact + after trim; no substring/domain matching. +- `remoteGui.allowInsecureHttp` is optional and defaults false. It affects pairing only; + tailscale-identity issuance still requires HTTPS. +- A malformed live candidate is rejected with its full config path. A malformed persisted + optional block degrades to remote issuance disabled while preserving providers, accounts, + and API keys, and emits a diagnostic that never repeats the malformed value. +- Browser origins eligible for a remote session must equal + `hub.managementPublicOrigin` or an exact canonical entry already present in + `corsAllowOrigins`. Pairing cannot create an origin allowlist bypass. + +## 5. Session and issuance contract + +### 5.1 Records and TTLs + +```ts +export type GuiSessionIssuance = + | "loopback" + | "tailscale-identity" + | "pairing" + | "insecure-http-pairing"; + +export interface GuiSessionRecord { + serverOrigin: string; + browserOrigin: string; + csrfToken: string; + expiresAt: number; + issuance: GuiSessionIssuance; +} + +export interface GuiSessionBootstrap extends GuiSessionRecord { + token: string; +} + +export const LOOPBACK_GUI_SESSION_TTL_MS = 5 * 60_000; +export const REMOTE_GUI_SESSION_TTL_MS = 12 * 60 * 60_000; +export const GUI_PAIRING_GRANT_TTL_MS = 5 * 60_000; +``` + +`loopback` sessions retain the current fixed five-minute expiry and silent rebootstrap. +The three remote issuance values receive `now + REMOTE_GUI_SESSION_TTL_MS`; each fully +authorized management request moves expiry to `now + REMOTE_GUI_SESSION_TTL_MS`. The +session limit remains 128. Renewal does not change the token or CSRF token. + +### 5.2 Origin predicate + +For a session-bearing management request: + +```text +destination origin from the actual request == session.serverOrigin +X-OpenCodex-GUI-Origin == session.browserOrigin +Origin absent only for safe same-browser reads; when present it == session.browserOrigin +mutation Origin == session.browserOrigin +mutation X-OpenCodex-CSRF-Token == session.csrfToken +``` + +For cross-origin remote reads the browser sends `Origin`, and it must match. The legacy +Origin-absent allowance remains only for safe `GET`/`HEAD` requests carrying a session token +and claimed GUI origin; it never authorizes a mutation. + +`managementRequestOrigin` uses the observed loopback origin for loopback Host values. For a +non-loopback hub request it prefers configured `hub.managementPublicOrigin`; otherwise it +keeps today's observed-origin behavior. It never reads forwarding headers. + +### 5.3 Bootstrap meta consumer chain + +The compatibility meta name `opencodex-session-origin` remains and now explicitly means +`browserOrigin`. Add `opencodex-session-server-origin` for the destination binding: + +```html + + + + +``` + +Full consumer chain required in this phase: + +- `src/server/index.ts:1608-1614`: GET/POST bootstrap routing and session candidate. +- `src/server/gui-static.ts:68-74,102-105`: escaped meta serialization. +- `gui/src/api.ts:93-110`: initial injected-session read and validation. +- `gui/src/api.ts:143-160`: `SESSION_REBOOTSTRAP_PATH` response parsing. +- `gui/src/api.ts:188-199`: attach a session only when request destination equals + `memorySessionServerOrigin`; send `memorySessionBrowserOrigin` in the GUI header. +- `tests/native-profile-route-security.test.ts:136-160`: native mutation remains session + + browser-origin + CSRF gated. +- `tests/server-management-auth.test.ts:790-898`: bootstrap/meta behavior and the exact + non-loopback forged-Host regression at line 897. + +The GUI accepts a bootstrap only when `browserOrigin === window.location.origin` and +`serverOrigin === new URL(bootstrapResponse.url).origin` (or the same-origin document +origin during initial injection). Failure clears all in-memory session fields. Tokens remain +memory-only and are never written to web storage. + +### 5.4 Issuance routes + +- `GET /opencodex-session` + - loopback request: current auto-issuance. + - trusted Tailscale ingress: read `Tailscale-User-Login`; require HTTPS public origin, + exact allowlist membership, and an allowed browser origin; issue + `tailscale-identity`. + - public listener with spoofed Tailscale headers: no session. +- `POST /api/gui/pairing-grants` + - exact operation-capability endpoint used by local `ocx gui pair`; it does not accept + admin-token, gui-session, local-read, provider-reload, restart, or data-key authority. + - bodyless. The canonical browser origin is carried in a dedicated header and is included + in the HMAC capability payload, so a body/header substitution cannot retarget the grant. + - returns `{grant, browserOrigin, serverOrigin, expiresAt}` once; response has + `Cache-Control: no-store` and no grant digest. +- `POST /opencodex-session` + - strict body `{ "grant": "…" }`, 4 KiB maximum, unknown fields rejected. + - requires an `Origin` matching the grant's `browserOrigin`; the grant is the only + credential accepted. Admin/data/session credentials in headers do not substitute. + - HTTPS issues `pairing`; non-loopback HTTP issues `insecure-http-pairing` only when the + config opt-in is true. The grant is consumed before minting; all replays fail. + +`ocx gui pair [--origin ] [--json]` defaults `--origin` to +`hub.managementPublicOrigin`; it fails if neither exists. It resolves the identity-checked +runtime, verifies the `/healthz` challenge proof, rechecks PID/port, derives the one-operation +capability from the protected runtime attestation secret, and POSTs once. It prints the grant +exactly once to stdout and never accepts a grant/token argument. JSON output is intended for +an immediately consuming operator tool and carries the same no-persistence warning. CLI +error paths redact response bodies containing a grant. + +## 6. Management CORS contract + +`managementCorsHeaders` currently calls `corsHeaders()` without the request +(`src/server/auth-cors.ts:199-206`), so it can echo an allowed origin but cannot include the +two GUI session headers in preflight. Keep the existing management header set and append +exactly: + +```text +X-OpenCodex-GUI-Origin, X-OpenCodex-CSRF-Token +``` + +Do not route management preflight through data-plane dynamic vendor-header echo. An allowed +origin receives exact-origin ACAO and the fixed header set; a rejected origin remains 403. +No `Access-Control-Allow-Credentials` is added because authentication is an explicit header, +not a cookie. + +## 7. Diff-level file-change map + +All paths below exist in the current tree except files marked **NEW**. + +| Action | Exact path | Diff-level change | +|---|---|---| +| MODIFY | `src/types/config.ts` | Add `OcxHubConfig`, `OcxRemoteGuiConfig`, and optional `hub`/`remoteGui` fields. Extend the Phase-1 role type only by reference, not by new values. | +| MODIFY | `src/config.ts` | Add strict nested schemas, canonical-origin/user-list validation, cross-field diagnostics, and persisted malformed-block degradation that preserves unrelated config. | +| NEW | `src/lib/gui-pair-capability.ts` | Own v1 method/path/header constants and HMAC create/verify functions bound to nonce, expiry, canonical browser origin, PID, and port. It accepts only the existing local-attestation secret shape. | +| NEW | `src/server/gui-session.ts` | Own session/grant records, constants, bounded maps, digest-only grant storage, issuance policy, grant consumption, shared request admission predicate, and sliding renewal. No provider/router/Lab imports. | +| MODIFY | `src/server/management-auth.ts` | Replace private `origin` records and duplicate authorization/principal checks with the shared GUI-session module. Preserve exported `issueGuiSession` as the loopback-compatible facade. Add pairing-grant state and exact `gui-pair-capability` principal/replay handling without changing admin-token initialization. | +| MODIFY | `src/server/auth-cors.ts` | Prefer configured hub public origin only for non-loopback management requests; add exact fixed management preflight headers and exact-origin ACAO. Do not change data-plane CORS or credential admission. | +| MODIFY | `src/server/index.ts` | Advertise GUI-pair capability v1 in `/healthz`; mount exact pairing-grant creation after capability admission, mount GET/POST bootstrap before GUI fallback, pass `trustedTailscaleIngress: false` on the public/ordinary loopback listeners, and preserve the line-1604 unknown-`/v1` guard. Do not make `startServer` async or add an await in its synchronous activation window. | +| MODIFY | `src/server/proxy-liveness.ts` | Add optional `guiPairCapability` to the existing health identity projection so the local client can fail closed against an old/foreign listener without changing required liveness identity fields. | +| MODIFY | `src/server/gui-static.ts` | Serialize escaped browser/server origin meta tags; keep `opencodex-session-origin` as browser-origin compatibility metadata. | +| NEW | `src/cli/gui.ts` | Own `runGuiCommand(args, deps)`: existing no-subcommand open behavior plus `pair`; strict `--origin`/`--json` parsing and one-time grant output. | +| NEW | `src/cli/gui-pair-client.ts` | Mirror the existing bound restart/provider-reload client pattern: read runtime identity, challenge `/healthz`, verify proof/capability version, recheck the target, derive the browser-origin-bound capability, POST once, and return a redacted typed result. | +| MODIFY | `src/cli/dispatch.ts` | Delegate the current inline `gui` runner to `runGuiCommand`, passing existing open/start dependencies; do not duplicate live-proxy discovery. | +| MODIFY | `src/cli/registry.ts` | Change usage to `ocx gui [pair [--origin ] [--json]]` and document that pairing output is secret and single-use. | +| MODIFY | `src/cli/help.ts` | Update the curated GUI command line so registry/help parity remains green. | +| MODIFY | `gui/src/api.ts` | Split memory browser/server origins, validate both meta sources, scope token attachment to the server origin, keep browser origin in the GUI header, and clear all four session values atomically. No web-storage persistence. | +| MODIFY | `tests/config.test.ts` | Extend sibling config tests for valid HTTPS, explicit HTTP opt-in, invalid origin components, duplicate/empty/oversize Tailscale users, malformed persisted block preservation, and non-hub inertness. | +| MODIFY | `tests/server-management-auth.test.ts` | Extend the primary auth suite for every issuance/expiry/replay/origin/CSRF/admin negative; preserve the line-897 forged-Host test unchanged in meaning. | +| MODIFY | `tests/native-profile-route-security.test.ts` | Update session fixture fields and prove native consent mutations still reject admin, wrong browser origin, wrong server destination, absent CSRF, and accept only the full remote-session predicate. | +| MODIFY | `tests/server-auth.test.ts` | Extend management preflight tests for exactly the two added headers, allowed/rejected origins, and no data-plane header-policy drift. | +| MODIFY | `tests/server-live.test.ts` | Extend the existing `/healthz` capability metadata coverage for GUI-pair v1 while keeping readiness/session secrets absent. | +| MODIFY | `tests/proxy-liveness.test.ts` | Extend health identity fixtures for optional GUI-pair capability detection and prove a foreign/malformed body cannot become an attested target. | +| NEW | `tests/gui-pair-capability.test.ts` | Characterize payload binding, wrong method/path/origin/PID/port, malformed nonce/expiry, constant-time mismatch, and expiration for the operation capability, following `tests/local-management-capability.test.ts` and `tests/system-restart-contract-security.test.ts`. | +| NEW | `tests/gui-pair-client.test.ts` | Characterize attestation, PID/port recheck, capability-version refusal, bodyless POST headers, one-attempt behavior, and redacted transport failures, following `tests/system-restart-client.test.ts` and `tests/local-provider-reload-client.test.ts`. | +| MODIFY | `tests/cli-dispatch.test.ts` | Extend the existing GUI runner coverage for default open vs `pair`, remote API failure, and exit codes. | +| MODIFY | `tests/cli-registry.test.ts` | Keep registry/dispatch/help parity and assert the GUI usage shape from registry values. | +| MODIFY | `tests/cli-help.test.ts` | Extend real CLI help coverage for `ocx gui pair`; do not spawn a live pairing request. | +| MODIFY | `gui/tests/api-auth-memory.test.ts` | Extend in-memory auth sibling tests for two-origin meta validation, destination-scoped attachment, remote CSRF headers, rejection/clear, and silent renewal. | +| MODIFY | `gui/tests/api-auth-deadline.test.ts` | Update bootstrap fixtures to both origins and prove timeout/watchdog behavior still settles without credential prompts or stale-session reuse. | + +No pairing form/component, locale file, docs-site page, or generated GUI output is touched in +this phase. If usable pairing requires visible UI before Phase 4, that is a scope expansion +and must be approved/amended before adding component or i18n paths. + +## 8. New and changed signatures + +```ts +// src/lib/gui-pair-capability.ts +export const GUI_PAIR_METHOD = "POST"; +export const GUI_PAIR_PATH = "/api/gui/pairing-grants"; +export const GUI_PAIR_CAPABILITY_VERSION = "v1"; + +export function createGuiPairCapability( + secret: string, + nonce: string, + method: string, + path: string, + browserOrigin: string, + pid: number, + port: number, + expiresAt: number, +): string | null; + +export function verifyGuiPairCapability( + secret: string, + nonce: string | null, + method: string, + path: string, + browserOrigin: string | null, + pid: number, + port: number, + expiresAt: number, + capability: string | null, + now?: number, +): boolean; + +// src/server/gui-session.ts +export interface GuiSessionState { + sessions: Map; + pairingGrants: Map; // key is SHA-256 digest +} + +export interface GuiSessionRequestContext { + trustedTailscaleIngress: boolean; + now?: number; +} + +export type GuiSessionAdmission = + | { ok: true; principal: "gui-session"; session: GuiSessionRecord } + | { ok: false; reason: "missing" | "expired" | "server-origin" | "browser-origin" | "csrf" }; + +export function issueGuiSession( + req: Request, + config: OcxConfig, + state: GuiSessionState, + context?: GuiSessionRequestContext, +): GuiSessionBootstrap | null; + +export function createGuiPairingGrant( + browserOrigin: string, + config: OcxConfig, + state: GuiSessionState, + now?: number, +): { grant: string; browserOrigin: string; serverOrigin: string; expiresAt: number }; + +export function consumeGuiPairingGrant( + req: Request, + body: unknown, + config: OcxConfig, + state: GuiSessionState, + now?: number, +): GuiSessionBootstrap | null; + +export function authorizeGuiSessionRequest( + req: Request, + config: OcxConfig, + state: GuiSessionState, + now?: number, +): GuiSessionAdmission; + +// src/server/management-auth.ts — public facade stays source-compatible +export type ManagementPrincipal = + | "admin-token" + | "gui-session" + | "gui-pair-capability" + | "local-read-capability" + | "local-provider-reload-capability" + | "system-restart-capability"; + +export function issueGuiSession( + req: Request, + config: OcxConfig, + state: ManagementAuthState, + context?: GuiSessionRequestContext, +): GuiSessionBootstrap | null; + +// src/cli/gui.ts +export interface GuiCommandDeps extends RuntimeApiDeps { + openDefaultGui: () => Promise; + loadConfig: () => OcxConfig; +} +export function runGuiCommand(args: string[], deps: GuiCommandDeps): Promise; + +// src/cli/gui-pair-client.ts +export type GuiPairRequestResult = + | { kind: "created"; grant: string; browserOrigin: string; serverOrigin: string; expiresAt: number } + | { kind: "unavailable"; reason: "unattested-target" | "runtime-mismatch" | "attestation" | "capability" | "transport" | "rejected" }; + +export function requestBoundGuiPairingGrant( + target: LiveProxy, + browserOrigin: string, + deps?: GuiPairClientDeps, +): Promise; +``` + +`requireManagementAuth` and `managementPrincipal` keep their public signatures. Internally +they call one `resolveManagementAdmission(req, ...)` result; a WeakMap keyed by the exact +`Request` may carry that result from the gate to principal projection so successful remote +sessions renew at most once per request. + +## 9. Acceptance criteria with activation grounding + +| ID | Constructible activation scenario | Required result / oracle | +|---|---|---| +| P2-A01 | Existing loopback config, GET page/bootstrap with loopback Host. | Session issues with equal server/browser origins, `issuance: loopback`, and exactly five-minute fixed expiry; current silent rebootstrap stays green. | +| P2-A02 | Current `remoteConfig()` and forged loopback Host on the public non-loopback bind (`tests/server-management-auth.test.ts:891-898`). | `issueGuiSession(...) === null`; this row remains green without adding config or trusted context. | +| P2-A03 | Hub config + allowed Tailscale login + HTTPS public origin, but direct/public listener context and spoofed `Tailscale-User-Login`. | No session. Header presence alone never activates identity issuance. | +| P2-A04 | Same request through `trustedTailscaleIngress: true`, exact allowlisted login, and allowed browser origin. | `tailscale-identity` session with separate origins where applicable and 12-hour expiry. | +| P2-A05 | Trusted ingress with empty list, nonmember, whitespace variant, HTTP public origin, standalone role, or client role. | No remote session for every branch; loopback behavior remains independent. | +| P2-A06 | Local CLI resolves the live runtime, verifies its challenge proof/capability version, rechecks PID/port, and POSTs a valid origin-bound capability. | One grant returned with 5-minute expiry/no-store; state stores only its digest; no session exists yet. | +| P2-A07 | Call grant creation with admin token, GUI session, data key, wrong/replayed/expired capability, changed PID/port/origin, or an origin outside public origin/`corsAllowOrigins`. | 403/401 as appropriate; no grant/session state change. Admin authority cannot reach grant creation. | +| P2-A08 | HTTPS POST bootstrap with fresh grant and exact `Origin`. | Grant is deleted and one `pairing` session is returned with both origin meta values. | +| P2-A09 | Replay consumed grant; use expired grant, wrong Origin, wrong server destination, data key, admin token, or session token in place of grant. | No session for every case; replay and alternate credentials cannot enter the exchange branch. | +| P2-A10 | Non-loopback HTTP pairing with opt-in absent/false, then true. | False path refuses without consuming into a session; true path consumes once and issues `insecure-http-pairing`. Automatic Tailscale issuance remains refused on HTTP in both cases. | +| P2-A11 | Authorized remote safe read immediately before expiry. | Full origin predicate passes and expiry slides to `now + 12h`; token/CSRF unchanged. | +| P2-A12 | Wrong destination, wrong claimed browser origin, wrong browser `Origin`, absent/wrong CSRF mutation, and an expired session. | 401 and expiry remains unchanged/deleted as applicable; principal is never projected as `gui-session`. | +| P2-A13 | Admin token calls ordinary management, pairing-grant creation, bootstrap exchange, and then a consent route; valid remote GUI session calls ordinary/consent routes with correct CSRF. | Admin remains ordinary management-capable but the other three are refused; remote session reaches consent route. No admin-to-grant or admin-to-session exchange exists. | +| P2-A14 | Serve initial GUI HTML and dedicated bootstrap for same-origin and two-origin fixtures. | Escaped meta contains compatibility browser origin plus new server origin; no raw attribute injection. | +| P2-A15 | GUI loads valid two-origin meta, then calls the bound server and an evil third origin. | Session headers attach only to bound server; evil origin receives no token/CSRF and triggers no admin prompt. | +| P2-A16 | GUI receives mismatched browser origin, mismatched response/server origin, missing meta, or a failed renewal. | All in-memory session fields clear atomically; no web-storage write and no stale header reuse. | +| P2-A17 | Allowed management OPTIONS requests GUI-origin + CSRF headers; repeat from rejected origin and request an unrelated custom header. | Allowed response lists the two exact additions and exact ACAO; rejected origin is 403; unrelated header is not dynamically echoed by management CORS. | +| P2-A18 | Run `ocx gui pair` with configured public origin, explicit allowed origin, missing origin, `--json`, extra args, stale target, failed attestation, and capability/API failure. | Valid cases create one grant and print once; invalid cases fail without falling back to admin auth or echoing secrets; no grant appears in argv/config/disk/log fixtures. | +| P2-A19 | Run native-profile mutation suite with admin, malformed remote session, and valid remote session. | Existing consent boundary remains: only the valid session + correct origin + CSRF dispatches the mutation. | +| P2-A20 | Run import-graph and synchronous-window guard. | No protected core import reaches GUI-session code; `startServer` remains synchronous and activation ordering is unchanged. | + +## 10. Verification — remote only on `lidge-ai` + +Do not run Bun tests, GUI tests, typecheck, full suite, or privacy scan on the local Mac. +Run as the ordinary `lidgeai` user in the remote checkout. + +Focused backend/CLI gate: + +```bash +ssh lidge-ai 'cd ~/Developer/opencodex && bun run typecheck && bun test tests/config.test.ts tests/server-management-auth.test.ts tests/native-profile-route-security.test.ts tests/server-auth.test.ts tests/server-live.test.ts tests/proxy-liveness.test.ts tests/gui-pair-capability.test.ts tests/gui-pair-client.test.ts tests/cli-dispatch.test.ts tests/cli-registry.test.ts tests/cli-help.test.ts tests/core-lab-boundary.test.ts' +``` + +Focused GUI auth gate: + +```bash +ssh lidge-ai 'cd ~/Developer/opencodex/gui && bun test tests/api-auth-memory.test.ts tests/api-auth-deadline.test.ts' +``` + +Review-ready security/shared-server gate: + +```bash +ssh lidge-ai 'cd ~/Developer/opencodex && bun run test && bun run privacy:scan' +``` + +Record remote commit, Bun version, command, exit code, pass/fail counts, and the security +review decision. Do not mark the phase review-ready from focused tests alone. + +## 11. Completion boundary + +Phase 2 is complete only when all four issuance values have a reachable positive or explicit +refusal scenario, every failure path leaves consent authority closed, and the existing +line-897 forged-Host regression remains green. A healthy endpoint alone is insufficient: +evidence must show an ordinary management request and one consent-bearing request with the +correct principal distinction. Production Tailscale listener wiring and visible pairing UX +remain later-phase work and must not be implied complete here. diff --git a/devlog/_plan/260827_remote_hub/050_phase3_connect.md b/devlog/_plan/260827_remote_hub/050_phase3_connect.md new file mode 100644 index 0000000000..fbd1c20dec --- /dev/null +++ b/devlog/_plan/260827_remote_hub/050_phase3_connect.md @@ -0,0 +1,530 @@ +# 050 — Phase 3: connect/disconnect/sync and client routing targets + +Unit: `260827_remote_hub` · Phase: 3 · Status: diff-level implementation plan + +Depends on Phase 1's `src/remote/protocol.ts`, protocol-v1 `/readyz`, and +data-authenticated `/v1/catalog` contracts and Phase 2's one-time pairing exchange at +`POST /opencodex-session`. Pairing never authenticates `/api/keys` directly: connect +first consumes it into an origin-bound GUI session, then uses that session once for the +existing key POST. This phase does not weaken either contract. + +## 0. Structural decision + +### Context + +Today `src/codex/inject.ts` derives one local target from `hostname + port`, +`src/codex/sync.ts` always gathers the local provider catalog, and +`src/cli/claude.ts` always ensures and targets a local proxy. A connected machine +instead needs one immutable remote target and one admission secret, while standalone +output must remain byte-for-byte unchanged. + +### Chosen move + +- Add a leaf `src/client/` subsystem that owns persisted connection-state parsing, + hub HTTP calls, and the connect transaction. +- Generalize Codex generation around a `CodexRoutingTarget`, but retain the current + numeric overloads as compatibility wrappers. The wrappers construct the same + standalone target and therefore emit identical bytes. +- Extend the injection journal with an optional durable client owner. A successful + connect outlives the short-lived connect CLI PID; startup preserves the journal only + while validated `runtimeRole === "client"` and `config.client.apiKeyId` match that + owner. Missing/mismatched state restores exactly as today's dead-PID recovery does. +- Make CLI dispatch choose standalone sync or connected sync before entering + `src/codex/sync.ts`. Connected sync never calls local provider discovery. +- Reuse the existing `POST /api/keys` owner in + `src/server/management/oauth-account-routes.ts:596`; do not create a second key + store or key-generation route. Admin authenticates that POST directly; pairing can + reach it only through Phase 2's session exchange and full origin/CSRF predicate. +- Store the issued secret only at `serviceApiTokenFilePath()` + (`src/lib/service-secrets.ts:5`). Persist only its key id and SHA-256 ownership + fingerprint under `config.json.client`. + +### Rejected alternatives + +- Persisting the data key in `config.json` or `$CODEX_HOME/config.toml`: both widen + secret exposure and violate the existing `env_key`/shim contract. +- Pointing connected sync at `refreshCodexModelCatalog()`: a hub outage would then + silently repopulate the catalog from local providers and route traffic to the wrong + authority. +- Adding a second CLI switch in `src/cli/index.ts`: command registration is now + registry-driven (`src/cli/registry.ts`, `src/cli/dispatch.ts`); bypassing it would + drift help, aliases, and dispatch parity. + +### Dependency direction and blast radius + +`src/cli/* -> src/client/* -> config/codex/service-secret leaves`. No new client +module is imported by `src/router.ts`, `src/server/lifecycle.ts`, or +`src/server/responses/core.ts`. `src/server/index.ts` is not changed in this phase, so +its synchronous `Bun.serve` activation window is untouched. Blast radius: CLI, +Codex/Claude machine integration, persisted config schema, and the existing API-key +management endpoint tests; no provider request-path change. + +## 1. IN / OUT + +### IN + +- `ocx connect `, `ocx disconnect`, `ocx connect status [--json]`, and connected + fields in existing `ocx status [--json]`. +- Protocol-v1 readiness negotiation through Phase 1's parser/predicate, with the + guaranteed compatibility floor: + current dev hub ↔ latest released client, with same-major feature detection. +- Per-client key auto-issuance through exact `POST /api/keys`, using an admin token + directly once or a Phase-2 pairing grant indirectly through one transient GUI session; + none of those management credentials is persisted. +- Owner-only service token file, bounded/atomic catalog placement, injector preflight, + rollback, and final atomic `runtimeRole + config.json.client` commit. +- Codex target generalization, connected `ocx sync` with no local fallback, Claude + launcher targeting, and offline journal-backed disconnect. + +### OUT + +- Client-mode HTTP listener, `/api/machine/*`, hub relay, and GUI two-plane wiring + (Phase 4 / doc 060). +- Deployment recipes and Tailscale Serve setup (Phase 5). +- Rotation UI, orphan-key reconciliation while the hub is unreachable, multi-hub, + catalog adversarial hardening beyond the Phase-1 contract, and release docs + (Phase 6). +- Provider execution on the client, local usage mirroring, or any write to `src/lab/`. + +## 2. File-change map + +Every existing path below was verified in the current tree. For NEW client paths, +`src/` exists and Phase 3 creates the approved `src/client/` feature leaf from +`010_design.md`; the other NEW parents already exist. + +| Action | Exact path | Diff-level change | +|---|---|---| +| NEW | `src/client/state.ts` | Parse/validate `runtimeRole + config.json.client`, expose fail-closed connected/absent/invalid/mismatched states, and atomically commit/clear both keys through config mutation. | +| NEW | `src/client/hub-client.ts` | Validate/normalize URLs; bounded `GET /readyz`, `POST /api/keys`, `GET /v1/catalog`; protocol/capability checks; redact all credential-bearing errors. | +| NEW | `src/client/connect.ts` | Transaction coordinator, connected sync, rollback, and offline disconnect. No argv or presentation logic. | +| NEW | `src/cli/connect.ts` | Parse connect/disconnect/status arguments, read the one-time credential from stdin or a named env var, call the coordinator, and render redacted human/JSON output. | +| MODIFY | `src/types/config.ts` | Add `OcxClientConnectionConfig` and top-level `OcxConfig.client?`; the secret itself is not a field. | +| MODIFY | `src/config.ts` | Add schema and field-scoped persistence behavior for `client`; malformed-present client state must be diagnosable and must never degrade into standalone routing. | +| MODIFY | `src/lib/service-secrets.ts` | Add atomic owner-only write and fingerprint-checked removal beside existing path/read helpers. | +| MODIFY | `src/codex/inject.ts` | Add `CodexRoutingTarget`; thread it through provider table, `env_key`, root URL, profile, preflight, journal witness, and inject while retaining byte-compatible standalone overloads. | +| MODIFY | `src/codex/journal.ts` | Add backward-compatible durable client ownership; reconcile a dead process journal only when no matching committed client state exists. | +| MODIFY | `src/cli/index.ts` | Pass fail-closed client ownership into pre-start journal reconciliation; command registration itself remains registry/dispatch-owned. | +| MODIFY | `src/cli/dispatch.ts` | Register lazy connect/disconnect runners; branch `sync` on client state before `syncModelsToCodex`; invalid or connected-but-unusable state fails without local discovery. | +| MODIFY | `src/cli/registry.ts` | Add canonical command metadata and usage for `connect` and `disconnect`. | +| MODIFY | `src/cli/help.ts` | Add both commands to the compact top-level usage list; detailed help remains registry-derived. | +| MODIFY | `src/cli/status.ts` | Add a redacted connection block: state, URLs, protocol, key id, selected clients, catalog age, and token-file ownership state; never token bytes/fingerprint. | +| MODIFY | `src/cli/claude.ts` | Resolve standalone vs connected launcher target; connected mode skips local proxy startup and injects hub `ANTHROPIC_BASE_URL` + client token only for that exact target. | +| MODIFY | `src/claude/gateway-cache.ts` | Generalize cache refresh from numeric local port to explicit base URL + admission token while retaining the numeric wrapper. | +| MODIFY | `tests/config.test.ts` | Extend config round-trip/degradation coverage for valid, absent, unknown-field, and malformed-present `client`. | +| MODIFY | `tests/cli-registry.test.ts` | Assert registry/help ownership for connect/disconnect. | +| MODIFY | `tests/cli-dispatch.test.ts` | Assert lazy dispatch and standalone/connected sync selection. | +| MODIFY | `tests/cli-help.test.ts` | Assert compact and subcommand help without credential-bearing argv forms. | +| MODIFY | `tests/cli-status-json.test.ts` | Assert redacted connected/invalid/disconnected status JSON. | +| MODIFY | `tests/api-keys-routes.test.ts` | Extend exact `POST /api/keys` authority matrix for admin and a fully authorized Phase-2 GUI session; a raw pairing grant is rejected and response secret remains one-time. | +| MODIFY | `tests/codex-inject.test.ts` | Add explicit-target generation plus standalone golden-byte parity. | +| MODIFY | `tests/codex-inject-integration.test.ts` | Add preflight/commit/restore tests for a remote target and absolute catalog path. | +| MODIFY | `tests/codex-catalog-restore.test.ts` | Add version-1 journal compatibility and durable-client-owner restore/preserve cases. | +| MODIFY | `tests/cli-start-journal-order.test.ts` | Prove matching committed client ownership preserves the connect journal after the connect PID exits; absent/mismatched state still restores. | +| MODIFY | `tests/claude-cli.test.ts` | Add connected target, user-override, token non-forwarding, and no-local-start cases. | +| MODIFY | `tests/claude-gateway-cache.test.ts` | Add remote model URL/token and local-wrapper parity. | +| NEW | `tests/client-connect.test.ts` | Transaction, protocol, credential, catalog, rollback, connected sync, and offline disconnect matrix. | +| NEW | `tests/service-secrets.test.ts` | 0600/ACL-aware write, fingerprint, symlink/refusal, changed-file removal refusal, and redacted failures. | + +Verified dependencies, not Phase-3 edits: `src/server/management/oauth-account-routes.ts` +owns `/api/keys`; `src/server/management/api-access.ts` only builds displayed data-plane +endpoints; `src/codex/paths.ts:29` owns +`$CODEX_HOME/opencodex-catalog.json`; Phase 1's `src/remote/protocol.ts` owns the +readiness parser/compatibility strings and `src/server/catalog-download.ts` owns +`MAX_REMOTE_CATALOG_BYTES` plus the `/v1/catalog` wire bytes. + +## 3. Persisted config and public signatures + +### `src/types/config.ts` + +```ts +export type OcxConnectedClientId = "codex" | "claude"; + +export interface OcxClientConnectionConfig { + serverUrl: string; // canonical origin; no path/query/hash/userinfo + managementUrl: string; // canonical origin; may differ from serverUrl + managementTransport: "direct" | "relay"; + selectedClients: OcxConnectedClientId[]; + tokenEnv: "OPENCODEX_API_AUTH_TOKEN"; + apiKeyId: string; // attribution id; not secret + tokenFingerprint: string; // lowercase SHA-256; ownership check only + protocolVersion: 1; + connectedAt: string; // ISO-8601 + catalogEtag?: string; + catalogSyncedAt?: string; +} + +export interface OcxConfig { + // existing fields unchanged + runtimeRole?: "standalone" | "hub" | "client"; // Phase 1 owner + client?: OcxClientConnectionConfig; +} +``` + +The parser rejects unknown selected-client ids, non-origin URLs, protocol values other +than 1, duplicate client ids, malformed timestamps, and non-64-hex fingerprints. +Forward-compatible unknown object keys are preserved on unrelated config writes. A raw +`client` key that is present but invalid is `kind: "invalid"`, not `absent`; start, +sync, Claude launch, and status must refuse local-provider fallback in that state. +`runtimeRole === "client"` requires a valid `client` object and a present client object +requires that role. `hub` plus `client`, or one half missing, is a mismatch and fails +closed. + +### `src/client/state.ts` + +```ts +export type ClientConnectionState = + | { kind: "disconnected" } + | { kind: "connected"; value: OcxClientConnectionConfig } + | { kind: "invalid"; reason: string } + | { kind: "mismatched"; reason: string }; + +export function readClientConnectionState(): ClientConnectionState; +export function commitClientConnection( + state: OcxClientConnectionConfig, +): "committed" | "unchanged"; +export function clearClientConnection( + expectedApiKeyId: string, +): "committed" | "absent" | "conflict"; +``` + +`commitClientConnection()` writes `runtimeRole: "client"` and `client` in one config +mutation. `clearClientConnection()` removes `client` and removes the role only when it +is still `client`, in one mutation. `readClientConnectionState()` inspects the raw +top-level keys before relying on a repaired/fallback config DTO. This is the guard that +makes malformed-present or half-present state fail closed instead of appearing +disconnected. + +### `src/lib/service-secrets.ts` + +```ts +export interface PersistedServiceApiToken { + path: string; + fingerprint: string; +} + +export function writeServiceApiTokenFile(token: string): PersistedServiceApiToken; +export function removeServiceApiTokenFileIfOwned( + expectedFingerprint: string, +): "removed" | "absent" | "changed"; +``` + +Write is temp → `0600`/Windows ACL harden → rename at the exact +`serviceApiTokenFilePath()`. It refuses symlink targets and a non-client pre-existing +secret. Removal rereads and hashes the bounded regular file; a changed file is never +deleted. Neither function returns or logs the token after the write. + +### `src/codex/inject.ts` + +```ts +export interface CodexRoutingTarget { + baseUrl: string; // canonical absolute .../v1 + requiresAdmissionToken: boolean; + tokenEnv: "OPENCODEX_API_AUTH_TOKEN"; +} + +export function standaloneCodexRoutingTarget( + port: number, + config?: Pick, +): CodexRoutingTarget; + +export interface InjectCodexOptions { + // existing fields unchanged + routingTarget?: CodexRoutingTarget; + journalOwner?: { kind: "process" } | { kind: "client"; apiKeyId: string }; +} +``` + +Existing `injectCodexConfig(port, config, options)`, `buildProviderTableBlock(...)`, +`buildOpenaiBaseUrlLine(...)`, and `buildProfileFile(...)` exports retain their current +call forms as overloads. Their implementations normalize through one target-aware +builder. With no `routingTarget`, the bytes are exactly current output, including EOL, +comments, provider names, `env_key = "OPENCODEX_API_AUTH_TOKEN"`, profile wording, +and loopback root-override behavior. With a connected target, +`requiresAdmissionToken: true` selects the provider-table form independent of whether +the URL hostname itself looks loopback. + +### `src/codex/journal.ts` + +```ts +export type JournalOwner = + | { kind: "process"; pid: number } + | { kind: "client"; apiKeyId: string }; + +export interface ReconcileJournalOptions { + activeClientApiKeyId?: string; +} + +export function reconcileJournal(options?: ReconcileJournalOptions): boolean; +``` + +Existing version-1 `{ pid }` journals parse as process-owned. A new journal records the +owner without removing the existing hashes/preimages. `reconcileJournal()` preserves a +client-owned journal only when a separately validated `runtimeRole === "client"` and +`config.client.apiKeyId` match; invalid, absent, or different state restores it. This +avoids both failure modes: a +successful connect is not undone merely because its CLI PID exited, while a crash before +the final client-state commit cannot leave durable remote routing behind. + +### `src/client/hub-client.ts` + +```ts +export type OneTimeConnectCredential = + | { kind: "admin"; value: string } + | { kind: "pairing-grant"; value: string }; + +export interface ConnectGuiSession { + token: string; + csrfToken: string; + browserOrigin: string; + serverOrigin: string; +} + +export interface IssuedClientKey { + id: string; + key: string; + createdAt: string; + name: string; +} + +export function normalizeHubOrigin(input: string): string; +export function fetchHubReady( + serverUrl: string, + options?: { timeoutMs?: number; fetchImpl?: typeof fetch }, +): Promise<{ status: "ready" | "pending" | "failed"; metadata: RemoteReadyMetadata }>; +export function exchangeConnectPairingGrant( + managementUrl: string, + browserOrigin: string, + grant: string, + options?: { allowInsecureHttp?: boolean; timeoutMs?: number; fetchImpl?: typeof fetch }, +): Promise; +export function issueClientKey( + managementUrl: string, + credential: + | { kind: "admin"; value: string } + | { kind: "gui-session"; value: ConnectGuiSession }, + name: string, + options?: { timeoutMs?: number; fetchImpl?: typeof fetch }, +): Promise; +export function downloadClientCatalog( + serverUrl: string, + admissionToken: string, + options?: { etag?: string; timeoutMs?: number; maxBytes?: number; fetchImpl?: typeof fetch }, +): Promise<{ kind: "fresh"; body: string; etag?: string } | { kind: "not-modified" }>; +``` + +`fetchHubReady()` parses through Phase 1's `parseRemoteReadyMetadata()` and evaluates +through `checkRemoteProtocolCompatibility()`; it does not define a second protocol +shape, constants, or mismatch strings. Both URLs accept only `http:`/`https:`, reject +credentials/query/hash and non-root paths +(a terminal `/v1` input normalizes to the server origin). Admin credentials may be sent +only over HTTPS. A pairing grant may use HTTP only when the caller explicitly supplied +`--allow-insecure-http`; the Phase-2 hub independently requires +`remoteGui.allowInsecureHttp === true`, so both sides must opt in. Redirects are +rejected. Bodies and timeouts are bounded. Errors carry status and safe code, never +response/header secrets. + +`POST /api/keys` remains the exact key authority. The request body is only a validated, +bounded `name`; admin uses the ordinary management header. Pairing uses strict +`POST /opencodex-session` with the future machine-GUI browser origin, then the returned +session token + `X-OpenCodex-GUI-Origin` + CSRF authorize the key POST. A raw pairing +grant cannot access any `/api/*` route. An admin token is never submitted to the session +exchange and therefore never mints or becomes `gui-session`. + +### `src/client/connect.ts` + +```ts +export interface ConnectOptions { + serverUrl: string; + managementUrl?: string; + credential: OneTimeConnectCredential; + selectedClients: OcxConnectedClientId[]; + managementTransport: "direct" | "relay"; + noSync?: boolean; + allowInsecureHttp?: boolean; +} + +export interface ClientConnectDeps { + fetchImpl?: typeof fetch; + now?: () => Date; +} + +export function connectClient( + options: ConnectOptions, + deps?: ClientConnectDeps, +): Promise; +export function syncConnectedClient( + options?: { restartCodex?: boolean }, + deps?: ClientConnectDeps, +): Promise<{ catalogWritten: boolean; cacheSynced: boolean; injected: boolean; stale: boolean }>; +export function disconnectClient( + options?: { keepCatalog?: boolean }, +): Promise<{ restored: boolean; tokenRemoved: boolean; catalogRemoved: boolean }>; +``` + +### CLI contract + +```text +ocx connect [--management-url ] + [--credential-stdin | --credential-env ] + [--clients codex,claude] + [--management-transport direct|relay] + [--allow-insecure-http] [--no-sync] +ocx connect status [--json] +ocx disconnect [--keep-catalog] [--json] +``` + +There is deliberately no `--token `, `--admin-token `, or pairing-code +positional form. `--credential-env` stores only the variable name in argv; the value is +read once and cleared from the coordinator's local reference after key issuance. +`--credential-stdin` uses the bounded stdin helper. Parse errors redact unknown bare +values and all credential-shaped option values. + +## 4. Connect transaction and rollback + +The observable order is fixed: + +1. Normalize `serverUrl`/optional `managementUrl`; reject an already connected, + role/client-mismatched, or malformed-present state. Preflight the token target and + refuse a foreign pre-existing service token before network or file writes. +2. `GET /readyz`; require `status=ready`, protocol-v1 compatibility, and + advertise/derive the management origin. +3. Validate transport/credential combination. Admin: POST + `/api/keys` directly. Pairing: consume the grant once at + `/opencodex-session` using the future localhost machine-GUI Origin, + then use the returned session + CSRF once at `/api/keys`. Hold `{id,key}` only in + memory. +4. Snapshot pre-existing owned client artifacts; atomically write the issued key only + to `serviceApiTokenFilePath()` and retain its fingerprint. +5. `GET /v1/catalog` with the issued data key, validate bounded JSON, then + atomically replace `$CODEX_HOME/opencodex-catalog.json`. +6. Run `injectCodexConfig(..., { validateOnly: true, routingTarget, catalogPath })`. +7. Unless `--no-sync`, inject selected Codex state under the existing journal/write-lock + transaction with `{ journalOwner: { kind: "client", apiKeyId } }`. Prepare Claude + launcher state only; no persistent Claude settings write. +8. Commit `runtimeRole: "client"` + `config.json.client` together and last. That state + commit makes the connection visible to future commands. + +Failure at steps 4–8 removes the newly written token, restores prior owned catalog +bytes, calls journal restore for any committed Codex injection, and leaves both client +config fields absent. The still-in-memory admin credential or exchanged GUI session +attempts exact `DELETE /api/keys` for the just-created id. If hub cleanup is unreachable, +the failure reports only the safe key id and exact revoke action; it never prints the +key. Machine-local rollback success is mandatory and remote cleanup inability is +explicit, never hidden as full rollback. + +`--no-sync` still performs readiness, key issuance, token placement, catalog download, +and final state commit, but does not mutate Codex/Claude client files. The next +connected `ocx sync` is the sole apply path. + +## 5. Mode-aware sync and launch behavior + +### `ocx sync` + +- `client.kind === disconnected`: run today's `syncModelsToCodex(...)` path unchanged. +- `client.kind === invalid`: exit non-zero before proxy discovery, provider discovery, + catalog write, or injection. +- `client.kind === connected`: read and fingerprint-check the service token, request + `/v1/catalog` with `If-None-Match`, and inject the saved `CodexRoutingTarget`. +- Connected 304 reuses the existing catalog only if it is a bounded regular file and + the configured catalog path is the expected absolute path. +- Connected timeout/5xx keeps last-known-good catalog and reports stale age; it does + not gather local providers. Missing/changed token file and 401 are hard failures and + do not inject or fall back. + +### Claude launcher + +Connected `ocx claude` does not call `ensureProxyForClaude()` and does not target the +Phase-4 machine listener. It derives: + +```ts +interface ClaudeRoutingTarget { + baseUrl: string; // client.serverUrl, no /v1 suffix + admissionToken: string; // token file, memory only +} +``` + +`buildClaudeEnv` retains its numeric standalone overload and adds an explicit-target +overload. Default connected launch sets `ANTHROPIC_BASE_URL=` and +`ANTHROPIC_AUTH_TOKEN=`, plus the existing discovery/model variables. +An explicit user `ANTHROPIC_BASE_URL` still wins; if it differs from the connected hub, +the hub admission token is removed before spawn so it cannot follow the user override. +Gateway cache refresh uses `/v1/models?limit=1000&ids=cli`; context-window +metadata comes from the downloaded catalog, not a management-token `/api/*` request. + +### Disconnect + +Disconnect is local-authoritative and works with the hub offline: + +1. Read valid connected state and verify `apiKeyId`/token fingerprint ownership. +2. Call existing journal-backed native restore (`restoreNativeCodexAsync` / + `restoreJournalState`); preserve user-edited foreign fields exactly as today. +3. Remove the token only when its fingerprint still matches. +4. Remove only the OpenCodex-owned catalog unless `--keep-catalog`. +5. Clear `config.json.client` + the `client` runtime role together and last (absence + resolves to standalone). + +If restore is partial or the token changed, state is not cleared and the command names +the conflicting artifact. This avoids claiming disconnected while Codex still points at +the hub or deleting a replacement secret. Remote key revocation is not required for +offline completion; Phase 6 owns stale-key/rotation UX. + +## 6. Test plan + +Tests use temp `OPENCODEX_HOME`/`CODEX_HOME`, injected fetch, and synthetic credentials. +No test sends live hub traffic or reads the developer's homes. + +| Test file | Required cases | +|---|---| +| `tests/client-connect.test.ts` (NEW) | URL canonicalization; Phase-1 ready parser/mismatch strings; ready/pending/failed; same-major v1 acceptance; management URL advertisement; admin HTTPS direct key POST; pairing HTTPS session exchange then key POST; dual-opt-in pairing HTTP; admin HTTP refusal; raw grant rejection at `/api/keys`; bounded catalog; atomic role+state commit; each rollback point; no-sync; connected 200/304/401/timeout sync; no local discovery fake called; offline disconnect and partial restore. | +| `tests/service-secrets.test.ts` (NEW) | Exact path, 0600, Windows ACL seam, atomic replacement, symlink refusal, fingerprint, changed-file non-removal, no token in errors. | +| `tests/codex-inject.test.ts` | Current standalone goldens byte-equal; explicit HTTPS target emits exact `base_url`, provider table, `env_key`; loopback-looking connected URL still requires admission; malformed target refused before journal. | +| `tests/codex-inject-integration.test.ts` | Validate-only has zero writes; target commit records journal ownership; offline restore returns exact preimage; partial write rollback. | +| `tests/codex-catalog-restore.test.ts`, `tests/cli-start-journal-order.test.ts` | Version-1 process journals retain current behavior; client journal survives only a matching final state; absent/invalid/mismatched state restores after dead connect PID. | +| `tests/config.test.ts` | Valid client round-trip; atomic role+client pair; unknown keys preserved; absent remains standalone; half-present/malformed remains fail-closed; no secret field accepted/emitted. | +| `tests/api-keys-routes.test.ts` | Admin and full GUI-session predicates create once; raw pairing grant and incomplete origin/CSRF reject; list/patch never echo secret. Phase-2 session tests remain the admin-never-mints-session oracle. | +| `tests/cli-registry.test.ts`, `tests/cli-dispatch.test.ts`, `tests/cli-help.test.ts` | Registry/dispatch/help parity; no credential argv form; connected sync calls only remote coordinator; invalid client refuses. | +| `tests/cli-status-json.test.ts` | Stable redacted status in disconnected/connected/invalid/token-changed/catalog-stale states. | +| `tests/claude-cli.test.ts`, `tests/claude-gateway-cache.test.ts` | Standalone parity; connected direct target; no local ensure; service token precedence; user destination strips hub token; remote model cache URL/token; no management credential dependency. | +| `tests/core-lab-boundary.test.ts` | Existing three protected import roots and synchronous `startServer` checks remain green. | + +## 7. Acceptance criteria with activation grounding + +| ID | Constructible activation scenario | Expected result | +|---|---|---| +| P3-A1 | Disconnected temp home; HTTPS hub ready on protocol 1; admin token arrives through stdin; `/api/keys` and `/v1/catalog` succeed. | One key is issued, token exists only in owner-only service file, catalog and injection commit, and `runtimeRole=client` + `config.client` are written together and last with key id/fingerprint only. | +| P3-A2 | Same as A1, but a Phase-2 pairing grant bound to the future localhost GUI origin is supplied. | Grant is consumed once at `/opencodex-session`; returned GUI session + CSRF performs exact key POST; raw grant on `/api/keys` and replay fail; no transient credential persists. | +| P3-A3 | HTTP management URL with pairing grant, client `--allow-insecure-http`, and hub `remoteGui.allowInsecureHttp=true`. | Exchange/key issuance succeeds with explicit warning. Missing either opt-in refuses; admin credential over HTTP refuses before credential transmission. | +| P3-A4 | `/readyz` returns protocol major 2, minimum client above 1, or status pending/failed. | Clear upgrade/not-ready error; zero key POSTs and zero local writes. Latest-release client ↔ dev hub protocol-1 fixture remains accepted. | +| P3-A5 | Key POST returns 401/403/409 or malformed/oversized JSON. | No token/catalog/journal/config writes and no secret in diagnostics. | +| P3-A6 | Token, catalog, injector preflight, inject commit, or final role+state commit is fault-injected in turn. | Prior machine bytes are restored at every point; neither `runtimeRole=client` nor visible `client` remains; remote orphan cleanup status is explicit by safe key id only. | +| P3-A7 | Existing standalone config runs every current injector golden. | Output bytes are identical; no connected-only env/header/config key appears. | +| P3-A8 | Connected state plus valid token; hub catalog returns 200 then 304. | First sync atomically updates/injects; second uses last-known-good and ETag; local provider gather fake is never called. | +| P3-A9 | Connected state with hub down, 401, missing token, changed token, or malformed-present client config. | No local-provider fallback and no new local catalog; timeout keeps LKG as stale, credential/state errors fail hard. | +| P3-A10 | Connected Claude launch with no user Anthropic overrides. | Child receives hub base and client token; no local proxy is started; gateway cache uses hub `/v1/models`. | +| P3-A11 | Connected Claude launch with user-owned different `ANTHROPIC_BASE_URL`. | User destination wins and the hub admission token is absent from child env. | +| P3-A12 | Hub unreachable during disconnect with intact journal/token. | Native Codex bytes restore offline, owned token/catalog are removed per flags, and `runtimeRole + client` clear together and last. | +| P3-A13 | Disconnect sees changed token or a journal ownership conflict. | Conflicting artifact is preserved, command fails, and connected state remains so status is honest. | +| P3-A14 | Connect injection committed, connect process exited, and matching `runtimeRole=client + config.client.apiKeyId` was committed last; then `ocx start` runs. | Pre-start reconciliation preserves the client journal/routing. If final state is absent, invalid, mismatched, or names another key id, the same journal restores before startup. | + +## 8. Verification — remote only on `lidge-ai` + +No Bun test, typecheck, build, or privacy suite runs on the local Mac. Create the +phase checkout at `/home/lidgeai/codex-runs/260827-remote-hub-phase3`, owned by the +unprivileged `lidgeai` user, install dependencies there, and run: + +```bash +ssh lidge-ai 'sudo -iu lidgeai bash -lc '\''cd /home/lidgeai/codex-runs/260827-remote-hub-phase3 && ./node_modules/.bin/bun run typecheck'\''' +ssh lidge-ai 'sudo -iu lidgeai bash -lc '\''cd /home/lidgeai/codex-runs/260827-remote-hub-phase3 && ./node_modules/.bin/bun test tests/client-connect.test.ts tests/service-secrets.test.ts tests/config.test.ts tests/cli-registry.test.ts tests/cli-dispatch.test.ts tests/cli-help.test.ts tests/cli-status-json.test.ts tests/api-keys-routes.test.ts tests/codex-inject.test.ts tests/codex-inject-integration.test.ts tests/codex-catalog-restore.test.ts tests/cli-start-journal-order.test.ts tests/claude-cli.test.ts tests/claude-gateway-cache.test.ts tests/core-lab-boundary.test.ts'\''' +ssh lidge-ai 'sudo -iu lidgeai bash -lc '\''cd /home/lidgeai/codex-runs/260827-remote-hub-phase3 && ./node_modules/.bin/bun run privacy:scan'\''' +``` + +Before marking the non-trivial PR review-ready, repository policy additionally requires +the full suite on the same remote checkout (never local): + +```bash +ssh lidge-ai 'sudo -iu lidgeai bash -lc '\''cd /home/lidgeai/codex-runs/260827-remote-hub-phase3 && ./node_modules/.bin/bun run test'\''' +``` + +Record remote user, absolute path, HEAD, command exit codes, pass/fail counts, and the +focused/full suite tails in this unit's C-phase evidence. Do not rerun an unchanged +passing command. diff --git a/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md b/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md new file mode 100644 index 0000000000..659e24e756 --- /dev/null +++ b/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md @@ -0,0 +1,499 @@ +# 060 — Phase 4: client machine listener and two-plane GUI + +Unit: `260827_remote_hub` · Phase: 4 · Status: diff-level implementation plan + +Depends on Phase 3's matching `runtimeRole: "client" + config.json.client`, token-file +ownership, connected sync, and offline disconnect, plus Phase 2's +`src/server/gui-session.ts` `serverOrigin`/`browserOrigin` contract. This phase adds no +provider execution to the client. + +## 0. Structural decision + +### Context + +The current dashboard assumes one same-origin `apiBase`. `gui/src/api.ts:52-60` +explicitly refuses auth on cross-origin URLs, while a connected machine needs shared +pages to call the hub and machine pages to call localhost. The current full server also +cannot be reused as a client listener: it mounts `/v1/*`, provider adapters, and shared +management routes that client mode must not expose. + +### Chosen move + +- Add an independent, loopback-only Bun listener under `src/client/`. Its route + allowlist copies the default-404 shape of `loopbackRouteAllowed` + (`src/server/index.ts:665-680`) but contains only GUI/static, health/readiness, + `/api/machine/*`, and an opt-in fixed-target relay. +- Branch in `src/cli/index.ts` before dynamically importing the full server. Connected + mode starts only the machine runtime; disconnected mode follows today's full-server + path. +- Add one GUI `ApiTargets` owner. Page components continue to receive an `apiBase`, but + App selects shared vs machine explicitly and the fetch auth layer keeps independent + in-memory session/CSRF state per logical target. +- Extend the existing `/api/usage` projection with exact `apiKeyId`. Connected Usage + defaults to that key id and can explicitly toggle hub-wide; disconnected Usage calls + the local server unchanged. No usage row is copied between stores. + +### Rejected alternatives + +- A generic localhost reverse proxy: caller-controlled destination/path creates an SSRF + and credential-forwarding surface. The relay destination is fixed by validated client + state and redirects are rejected. +- Serving `/v1/*` on the machine listener: Codex/Claude must dial the hub directly, and + a local data plane would make fallback/provider execution possible. +- One token slot keyed only by browser origin: direct hub and localhost share the same + browser origin claim but have different server origins and credentials; one slot can + send a hub session to a machine endpoint or vice versa. +- Mirroring hub usage into local `usage.jsonl`: it creates two authorities and was + explicitly rejected in `001_interview.md`. + +### Dependency direction and invariants + +`src/cli/index.ts -> src/client/runtime.ts -> machine-listener/machine-api/hub-relay`. +The client leaf may reuse `src/server/gui-static.ts` and `src/server/management-auth.ts`; +the full server never imports the client listener. No new subsystem import enters +`src/router.ts`, `src/server/lifecycle.ts`, or `src/server/responses/core.ts`. +`src/server/index.ts` is unchanged, so the synchronous window from `Bun.serve` to Lab +activation remains unchanged and contains no new `await`. + +## 1. IN / OUT + +### IN + +- Loopback-only client listener, explicit default-404 route allowlist, GUI assets, and + local machine-session/CSRF enforcement. +- `GET /api/machine/status`, `GET /api/machine/clients`, + `POST /api/machine/sync`, `GET|POST /api/machine/shim`, and + `POST /api/machine/disconnect`. +- Opt-in fixed-target `/api/machine/hub-relay/*` selected only by + `client.managementTransport === "relay"`. +- GUI machine/shared target discovery, independent auth state, page-plane mapping, + stable hub-offline states, and mode-aware stop/restart actions. +- Connected usage = hub store filtered to this machine's `apiKeyId` by default, with an + explicit hub-wide toggle; disconnected usage = local `usage.jsonl` unchanged. + +### OUT + +- `/v1/*`, providers, OAuth storage, routing, Lab, shared config mutation, or local + usage persistence on the machine listener. +- Caller-selected relay hosts/schemes, redirects, WebSocket tunneling, arbitrary files, + cookies, or generic forward-proxy behavior. +- Usage replication, merge, import, backfill, or schema migration. +- Tailscale service installation/deployment docs (Phase 5) and relay rate/backpressure + hardening beyond fixed bounds (Phase 6). + +## 2. File-change map + +All existing paths were verified in the current tree. For NEW client paths, `src/` +exists and this phase extends the Phase-3-created `src/client/` leaf; every other NEW +parent exists. No generated `gui/dist` file is edited. + +| Action | Exact path | Diff-level change | +|---|---|---| +| NEW | `src/client/machine-auth.ts` | Define machine-session header contract for requests that also carry a hub credential; adapt to existing management-auth validation and strip local headers before relay. | +| NEW | `src/client/machine-api.ts` | Exact `/api/machine/*` route dispatcher and non-secret DTOs; all mutation orchestration stays here. | +| NEW | `src/client/hub-relay.ts` | Fixed destination/path allowlist, header/body bounds, redirect rejection, response filtering, and no-log relay. | +| NEW | `src/client/machine-listener.ts` | Loopback Bun listener, route allowlist/default-404, GUI/session bootstrap, health/readiness, and dispatch to machine API/relay. | +| NEW | `src/client/runtime.ts` | Client-process PID/runtime state, signal/drain handling, start/recycle, and transition to standalone after disconnect. | +| MODIFY | `src/cli/index.ts` | Read client state before full-server import; dynamically start client runtime when connected; retain current standalone branch byte-for-byte. | +| MODIFY | `src/server/management/logs-usage-routes.ts` | Read optional `apiKeyId`, include it in the projection-only filter, keep filtered responses out of the summary cache. | +| MODIFY | `src/usage/summary.ts` | Extend `UsageFilterEcho` and `projectUsageSummary` to filter exact entry `apiKeyId` before model/provider attribution projection. | +| MODIFY | `tests/api-usage.test.ts` | Add exact key slice, no-match, cache-poisoning, and combined surface/provider/model/key filter cases. | +| MODIFY | `tests/usage-summary.test.ts` | Add pure key projection, old-row exclusion, combo behavior, and exact-case id tests. | +| MODIFY | `tests/cli-start-journal-order.test.ts` | Prove connected start skips stale-process journal restore only for a matching durable client owner and starts no full data plane. | +| NEW | `tests/client-machine-listener.test.ts` | Listener bind/allowlist/auth/API/startup/offline matrix. | +| NEW | `tests/client-hub-relay.test.ts` | Fixed target, header separation, body caps, redirects, errors, and SSRF negatives. | +| NEW | `gui/src/api-targets.ts` | Canonical `ApiTargets`, machine-status discovery, page-plane map, relay URL construction, and disconnected fallback. | +| MODIFY | `gui/src/api.ts` | Replace `needsApiAuth`'s one same-origin slot with exact target classification and per-target in-memory session/CSRF state; attach both auth domains only on relay. | +| MODIFY | `gui/src/App.tsx` | Discover targets before page fetches; map pages/actions to planes; machine health remains live when hub is down; connected stop becomes disconnect/recycle. | +| MODIFY | `gui/src/stop-proxy.ts` | Add mode-aware machine disconnect request while preserving existing standalone `/api/stop` behavior. | +| MODIFY | `gui/src/pages/Usage.tsx` | Add this-machine/hub-wide scope control, key-id query/cache key, source label, and hub-offline behavior without local fallback. | +| MODIFY | `gui/src/pages/Storage.tsx` | Pass its selected shared `apiBase` through to `StorageWorkspace`. | +| MODIFY | `gui/src/components/storage-workspace/StorageWorkspace.tsx` | Remove module-global `VITE_API_BASE`; use the supplied shared-plane base for Codex-log storage calls. | +| MODIFY | `gui/src/styles-usage-workspace.css` | Style compact usage-source/scope controls and connected/offline qualification without changing layout direction. | +| MODIFY | `gui/src/i18n/en.ts` | Source-of-truth copy keys for connected source, this machine, hub-wide, hub offline, disconnect, and relay warnings. | +| MODIFY | `gui/src/i18n/de.ts` | Add the same keys. | +| MODIFY | `gui/src/i18n/fr.ts` | Add the same keys. | +| MODIFY | `gui/src/i18n/ja.ts` | Add the same keys. | +| MODIFY | `gui/src/i18n/ko.ts` | Add the same keys. | +| MODIFY | `gui/src/i18n/ru.ts` | Add the same keys. | +| MODIFY | `gui/src/i18n/tr.ts` | Add the same keys. | +| MODIFY | `gui/src/i18n/zh.ts` | Add the same keys. | +| MODIFY | `gui/src/i18n/zh-TW.ts` | Add the same keys. | +| NEW | `gui/tests/api-targets.test.ts` | Target discovery, page mapping, relay construction, and hub-down fallback. | +| MODIFY | `gui/tests/api-auth-memory.test.ts` | Independent machine/shared sessions; direct/relay header matrix; no cross-target leakage; bootstrap validation. | +| MODIFY | `gui/tests/api-auth-deadline.test.ts` | Per-target shared resolution/watchdog behavior. | +| MODIFY | `gui/tests/usage-layout.test.ts` | Connected own-key default, hub-wide toggle, disconnected local source, cache partition, and offline rendering. | +| MODIFY | `gui/tests/app-stop.test.ts` | Standalone stop vs connected disconnect/recycle. | +| MODIFY | `gui/tests/integrations-routing.test.ts` | Integrations remains machine-plane while shared pages use hub. | +| MODIFY | `tests/core-lab-boundary.test.ts` | Existing protected-root and synchronous-start checks remain green; no rule weakening. | + +Verified reuse without edits: `src/server/gui-static.ts` serves assets/bootstrap; +`src/server/management-auth.ts` owns session/CSRF validation; +`src/usage/log.ts:80` already persists `apiKeyId`; `src/server/management/api-key-usage.ts:78-89` +already proves exact per-key aggregation; `gui/src/pages/Startup.tsx` and +`gui/src/pages/Integrations.tsx` already accept an `apiBase` prop. + +## 3. Machine listener and API contracts + +### `src/client/machine-auth.ts` + +Relay requests carry two principals and therefore cannot overload one header: + +```ts +export const MACHINE_SESSION_HEADER = "x-opencodex-machine-session"; +export const MACHINE_GUI_ORIGIN_HEADER = "x-opencodex-machine-gui-origin"; +export const MACHINE_CSRF_HEADER = "x-opencodex-machine-csrf-token"; + +export function requireMachineAuth( + req: Request, + state: ManagementAuthState, + config: OcxConfig, +): Response | null; +export function stripMachineAuthHeaders(headers: Headers): Headers; +``` + +Ordinary `/api/machine/*` requests may use the existing standard session headers. +Relay requests put the hub principal in standard `x-opencodex-*` headers and the local +machine principal in the three headers above. `requireMachineAuth` maps only the local +triple into a synthetic request for the existing `requireManagementAuth` predicate, +then the relay strips that triple. An admin token still cannot mint or substitute for +a GUI session; the machine principal is issued by loopback page bootstrap and mutation +CSRF checks remain mandatory. + +### `src/client/machine-listener.ts` + +```ts +export interface MachineListenerDeps { + state?: OcxClientConnectionConfig; + managementAuthState?: ManagementAuthState; + fetchImpl?: typeof fetch; +} + +export function machineRouteAllowed(url: URL, req: Request, relayEnabled: boolean): boolean; +export function startMachineListener( + port?: number, + deps?: MachineListenerDeps, +): Server; +``` + +The bind hostname is hard-coded `127.0.0.1`; `config.hostname`, wildcard values, and +request headers cannot alter it. The allowlist is evaluated before auth or handlers: + +| Method/path | Purpose | +|---|---| +| `GET /healthz` | Process liveness and PID/port identity only. | +| `GET /readyz` | Local machine-plane readiness and role; no hub/provider/account data. | +| `GET /`, `GET /opencodex-session`, static GUI assets, SPA extensionless GET | Existing GUI serving/session bootstrap. | +| `GET /api/machine/status` | Redacted connection and target state. | +| `GET /api/machine/clients` | Selected client/journal/shim status, no secret paths outside approved DTOs. | +| `POST /api/machine/sync` | Connected sync. | +| `GET /api/machine/shim` | Current Codex shim status. | +| `POST /api/machine/shim` | `{ action: "install" | "repair" | "uninstall" }`. | +| `POST /api/machine/disconnect` | Offline-capable restore and scheduled standalone recycle. | +| `/api/machine/hub-relay/*` | Only when relay is explicitly selected; methods/path further constrained by relay. | + +Everything else, including every `/v1/*`, `/api/config`, `/api/usage`, provider route, +unknown machine route, wrong method, and WebSocket upgrade returns JSON 404. A future +route is unreachable until added to this function. + +### `src/client/machine-api.ts` + +```ts +export interface MachineStatusV1 { + mode: "client"; + connected: true; + machineBase: string; + sharedBase: string; + sharedServerOrigin: string; + managementTransport: "direct" | "relay"; + apiKeyId: string; + protocolVersion: 1; + connectedAt: string; + catalogSyncedAt?: string; + hubReachability: "unknown" | "online" | "offline" | "unauthorized"; +} + +export interface MachineApiDeps { + sync: typeof syncConnectedClient; + disconnect: typeof disconnectClient; + scheduleStandaloneRecycle: () => void; +} + +export function handleMachineApi( + req: Request, + url: URL, + state: OcxClientConnectionConfig, + deps: MachineApiDeps, +): Promise; +``` + +Status and clients are safe GETs but still require the loopback GUI session, matching +the dashboard management model. Sync, shim mutation, and disconnect require browser +Origin + matching machine CSRF. Bodies are strict, unknown fields rejected, and the +existing bounded management-body limit is reused. Status reports the key id and token +ownership state only; never the token, fingerprint, admin credential, pairing grant, or +raw filesystem contents. + +Disconnect calls Phase 3 restore even when the hub is down, returns 202 only after local +state commits, then recycles the process on the same loopback port. The replacement sees +no `config.client` and enters today's standalone full-server path; a browser reload then +reads local `/api/usage`. If restore conflicts, no recycle is scheduled and the client +state remains visible. + +### `src/client/runtime.ts` and `src/cli/index.ts` + +```ts +export function startClientRuntime( + options?: { port?: number; block?: boolean }, +): Promise; +export function scheduleStandaloneRecycle(): void; +``` + +`handleStart` reads `ClientConnectionState` before full-server import: + +```text +invalid/mismatched role+client -> fail before listener/import/provider timer +runtimeRole=client + connected -> dynamic import src/client/runtime.ts; start machine listener +standalone/absent + disconnected -> dynamic import ../server; run current startServer path +``` + +The client runtime writes the existing PID/runtime records, installs crash/signal +handlers, drains only its listener, and never starts token/history/provider/catalog +timers. Its runtime record names the actual loopback host/port so existing process +ownership checks remain valid. Client stop preserves connection intent unless the user +requested disconnect; disconnect performs restore and recycle explicitly. + +## 4. Fixed-target hub relay + +### `src/client/hub-relay.ts` + +```ts +export interface HubRelayTarget { + managementUrl: string; + browserOrigin: string; +} + +export function relayHubManagementRequest( + req: Request, + suffix: string, + target: HubRelayTarget, + deps?: { fetchImpl?: typeof fetch; timeoutMs?: number }, +): Promise; +``` + +Activation requires all of: + +1. valid connected state; +2. `managementTransport === "relay"`; +3. exact `/api/machine/hub-relay/` prefix; +4. valid local machine session (custom headers for relay); +5. suffix exactly `/opencodex-session` GET or inside `/api/` with an allowed HTTP + method. + +The destination is `new URL(suffix, state.managementUrl)` after rejecting encoded +slashes/backslashes, authority syntax, userinfo, query-host tricks, and path traversal. +The caller supplies no host/scheme/port. `redirect: "manual"`; every 3xx is an error. +Strip `Host`, connection/hop-by-hop headers, machine auth headers, proxy credentials, +cookies, and forwarding headers. Forward only the bounded management header allowlist, +including hub session, GUI-origin, CSRF, content type, and conditional cache headers. +Response headers are similarly allowlisted; `Set-Cookie` and hop-by-hop headers are +never returned. Request and response bodies have named constants and abort on overflow. +No URL query, auth header, body, or response body is logged. + +The hub sees its fixed canonical server origin and the browser's localhost origin from +Phase 2's split-origin session. Relay mode does not mint a new authority and cannot turn +the local machine session or admin token into a hub `gui-session`. + +## 5. GUI two-plane contract + +### `gui/src/api-targets.ts` + +```ts +export type ApiPlane = "machine" | "shared"; +export type SharedTransport = "same-origin" | "direct" | "relay"; + +export interface ApiTarget { + id: ApiPlane; + baseUrl: string; + serverOrigin: string; + bootstrapPath: string; + transport: SharedTransport; +} + +export interface ApiTargets { + connected: boolean; + machine: ApiTarget; + shared: ApiTarget; + apiKeyId?: string; +} + +export function standaloneApiTargets(initialBase: string): ApiTargets; +export function targetsFromMachineStatus(initialBase: string, status: MachineStatusV1): ApiTargets; +export function apiPlaneForPage(page: Page): ApiPlane; +export function apiBaseForPage(page: Page, targets: ApiTargets): string; +export async function discoverApiTargets(initialBase: string, signal?: AbortSignal): Promise; +``` + +Page mapping is explicit: + +| Plane | Pages/actions | +|---|---| +| Shared | Dashboard, Providers, Models, Subagents, Logs, Usage, Storage, Codex Set. | +| Machine | Startup, Integrations, health/version, Codex app-server restart, shim actions, disconnect. | + +In a standalone full server, `/api/machine/status` returns 404 and discovery returns one +same-origin target, preserving existing behavior. A connected machine status response +constructs either an exact cross-origin hub base or the local relay prefix. A network +failure to machine status is not interpreted as standalone; App renders a local-plane +startup error so it cannot accidentally send shared requests to an unknown local server. + +### `gui/src/api.ts` + +Replace global `memoryToken/memoryCsrfToken/memorySessionOrigin` with: + +```ts +interface ApiSessionState { + token: string | null; + csrfToken: string | null; + browserOrigin: string | null; + serverOrigin: string | null; +} + +export function configureApiTargets(targets: ApiTargets): void; +``` + +Target classification uses exact configured base URL/prefix, not arbitrary cross-origin +matching. Each target has an independent 401 resolution gate, prompt-cancel state, +watchdog, session state, and bootstrap URL. A bootstrap is stored only when +`browserOrigin === window.location.origin` and `serverOrigin === target.serverOrigin`. + +Header behavior is exact: + +| Request | Headers attached | +|---|---| +| Machine endpoint | Machine session in standard GUI headers only. | +| Shared direct | Hub session/admin header + hub GUI-origin/CSRF only; no machine header. | +| Shared relay | Hub session in standard headers plus machine session in custom machine headers; relay strips custom headers before hub. | +| Unknown target/cross-origin URL | No OpenCodex credential and no auth prompt. | + +Tokens remain memory-only. Legacy sessionStorage cleanup remains. A 401 on one target +clears/prompts only that target and cannot wipe the other target's newer session. + +### `gui/src/App.tsx` + +App blocks page resource mounting until target discovery settles, then passes the mapped +base. Health/version polls machine. Connected hub failure leaves shell, navigation, +Startup, Integrations, disconnect, and local status usable; shared pages render one +stable hub-offline state and never substitute machine data. In connected mode the power +action uses `POST /api/machine/disconnect`; in standalone it remains `POST /api/stop`. + +`StorageWorkspace` must receive the shared base from `Storage.tsx`; its current +module-global `VITE_API_BASE` at `gui/src/components/storage-workspace/StorageWorkspace.tsx:20` +would otherwise bypass plane selection on Codex-log actions. + +## 6. Usage source and filtering + +### Server projection + +`projectUsageSummary` changes to: + +```ts +export interface UsageFilterEcho { + provider: string | null; + model: string | null; + apiKeyId: string | null; + matched: boolean; + comboOverlap: boolean; +} + +export function projectUsageSummary( + summary: T, + filter: { provider?: string | null; model?: string | null; apiKeyId?: string | null }, + entries?: PersistedUsageEntry[], +): T & { filter?: UsageFilterEcho }; +``` + +`apiKeyId` is trimmed and compared exactly, not lowercased. It filters entries before +attempt/model attribution. Old rows, environment-token rows, and loopback rows have no +matching id and are excluded. Provider/model filtering then applies to the retained +entries as today. Any requested filter bypasses the unfiltered summary cache and never +warms a filtered value under `range:surface`. + +### GUI rule + +`Usage` receives `{ apiBase, connected, apiKeyId }`: + +- connected initial scope = `machine`; request includes + `apiKeyId=` and reads the hub's `usage.jsonl`; +- connected explicit toggle = `hub`; omit `apiKeyId` and read the whole hub store; +- disconnected = no scope toggle/query; read the same local `/api/usage` as today; +- hub down = error/stale held hub payload for that exact source key, never local data; +- disconnect/reload = standalone target/cache key, so the local store appears; +- no endpoint writes or mirrors usage rows. + +The cache key adds server origin + transport + scope + apiKeyId, preventing a prior +hub-wide payload from appearing as this-machine or a prior connected payload from +appearing after disconnect. + +## 7. Test plan + +| Test file | Required cases | +|---|---| +| `tests/client-machine-listener.test.ts` (NEW) | IPv4 loopback bind; GUI/bootstrap; exact allowlist; every `/v1/*` 404; unknown/wrong-method 404; safe GET auth; mutation Origin/CSRF; status redaction; sync success/failure; shim actions; disconnect offline; recycle to standalone; invalid state refuses startup; no provider/timer fake invoked. | +| `tests/client-hub-relay.test.ts` (NEW) | Relay disabled 404; direct mode 404; fixed host/path; direct/encoded traversal and authority injection rejected; redirects rejected; request/response caps; hop-by-hop/cookie/forwarded/machine headers stripped; hub auth retained; timeout/abort; no body/header log. | +| `tests/cli-start-journal-order.test.ts` | Matching durable client journal survives start; missing/mismatched client owner restores; connected branch never starts full server; disconnected branch remains current. | +| `tests/api-usage.test.ts` | Exact `apiKeyId` response/echo; old and other-key rows excluded; no match; combined filters; filtered request cannot poison cache; unfiltered next request remains whole hub. | +| `tests/usage-summary.test.ts` | Pure projection totals/days/models/providers/accounts consistency; exact-case id; combo attempts; absent id; provider/model/key cross-product. | +| `tests/core-lab-boundary.test.ts` | Protected roots import no client subsystem; `startServer` remains non-async and no new top-level-window await. | +| `gui/tests/api-targets.test.ts` (NEW) | Standalone 404 fallback; valid direct/relay status; page map; exact bases; machine-status network failure not standalone; encoded relay paths. | +| `gui/tests/api-auth-memory.test.ts` | Two simultaneous sessions; direct headers; relay dual headers; machine custom stripping contract; cross-target 401 races; server/browser-origin mismatch; unknown target receives nothing; no web storage. | +| `gui/tests/api-auth-deadline.test.ts` | One target watchdog does not block/clear the other; direct and relay bootstrap timeout states. | +| `gui/tests/usage-layout.test.ts` | Connected default key query; hub-wide omission; disconnected local query; source-qualified cache keys; hub-down no local fetch; scope labels/a11y. | +| `gui/tests/app-stop.test.ts` | Standalone `/api/stop`; connected `/api/machine/disconnect`; refusal re-enables action; accepted recycle tolerates connection drop. | +| `gui/tests/integrations-routing.test.ts` | Startup/Integrations machine base; Providers/Usage/Storage shared base under direct and relay. | + +## 8. Acceptance criteria with activation grounding + +| ID | Constructible activation scenario | Expected result | +|---|---|---| +| P4-A1 | Valid connected state, then `ocx start`. | Only a 127.0.0.1 machine listener starts; no full server/provider/timer starts; PID/runtime records name it. | +| P4-A2 | Request every known data/shared route on the machine listener. | Every `/v1/*`, `/api/config`, `/api/usage`, OAuth/provider/Lab path is JSON 404; only explicit machine routes/assets answer. | +| P4-A3 | GET status/clients with valid loopback GUI session, then without it. | Valid request returns redacted DTO; missing/admin-only/expired/wrong-origin session is rejected and no secret/fingerprint leaks. | +| P4-A4 | POST sync/shim/disconnect with valid session but missing/wrong CSRF or browser Origin. | Mutation is rejected before work; exact session+Origin+CSRF reaches the handler. Admin token never becomes GUI session. | +| P4-A5 | Connected direct transport with Phase-2 session issued by an exact `remoteGui.allowedTailscaleUsers` match or a consumed pairing grant; hub CORS includes the localhost browser origin. | Shared requests go directly to exact hub origin with hub headers only; machine pages remain localhost; CORS/session validation succeeds. A non-allowlisted Tailscale identity mints no session and gets no local fallback. | +| P4-A6 | Connected relay transport and valid machine + hub sessions. | Browser sends dual auth domains; relay validates machine session, strips custom headers, forwards hub session only to fixed hub target, and rejects redirect/SSRF variants. | +| P4-A7 | Relay path requested while transport is direct/disabled or caller supplies host/scheme/traversal. | Default 404/refusal before outbound fetch; no credential or body is logged. | +| P4-A8 | Hub becomes unreachable after target discovery. | Shell, machine pages, status, and disconnect remain usable; shared pages show stable hub-offline state and never fetch local substitutes. | +| P4-A9 | Connected Usage opens with key A while hub log contains A, B, environment, loopback, and old rows. | Default totals contain only A rows and echo A; hub-wide toggle contains all hub rows; no local file is read or written. | +| P4-A10 | User disconnects while hub is unreachable; recycle succeeds. | Journal/token/catalog/client state restore locally, replacement starts standalone on same port, reload shows local usage store. | +| P4-A11 | Standalone full server opens the same GUI. | `/api/machine/status` 404 selects same-origin targets; all current pages, auth, stop, usage, and injector output remain unchanged. | +| P4-A12 | Shared direct session 401s while machine session is renewed (and inverse). | Each target resolves/clears only its own in-memory state; no token crosses target and no prompt fan-out occurs. | +| P4-A13 | GUI PR is prepared for review. | PR description uses the repository template and includes screenshots of connected this-machine Usage plus hub-offline machine shell (or a maintainer-approved `gui-screenshot-waived` exception). | + +## 9. Verification — remote only on `lidge-ai` + +No Bun test, typecheck, GUI lint/build, or browser suite runs on the local Mac. Create +the phase checkout at `/home/lidgeai/codex-runs/260827-remote-hub-phase4`, owned by +unprivileged `lidgeai`, install root and `gui/` dependencies there, and run: + +```bash +ssh lidge-ai 'sudo -iu lidgeai bash -lc '\''cd /home/lidgeai/codex-runs/260827-remote-hub-phase4 && ./node_modules/.bin/bun run typecheck'\''' +ssh lidge-ai 'sudo -iu lidgeai bash -lc '\''cd /home/lidgeai/codex-runs/260827-remote-hub-phase4 && ./node_modules/.bin/bun test tests/client-machine-listener.test.ts tests/client-hub-relay.test.ts tests/cli-start-journal-order.test.ts tests/api-usage.test.ts tests/usage-summary.test.ts tests/core-lab-boundary.test.ts'\''' +ssh lidge-ai 'sudo -iu lidgeai bash -lc '\''cd /home/lidgeai/codex-runs/260827-remote-hub-phase4/gui && ../node_modules/.bin/bun test tests/api-targets.test.ts tests/api-auth-memory.test.ts tests/api-auth-deadline.test.ts tests/usage-layout.test.ts tests/app-stop.test.ts tests/integrations-routing.test.ts && ../node_modules/.bin/bun run lint:i18n && ../node_modules/.bin/bun run lint && ../node_modules/.bin/bun run build'\''' +ssh lidge-ai 'sudo -iu lidgeai bash -lc '\''cd /home/lidgeai/codex-runs/260827-remote-hub-phase4 && ./node_modules/.bin/bun run privacy:scan'\''' +``` + +Before the non-trivial GUI/security PR is marked review-ready, run the full repository +and GUI suites on that same remote checkout, never locally: + +```bash +ssh lidge-ai 'sudo -iu lidgeai bash -lc '\''cd /home/lidgeai/codex-runs/260827-remote-hub-phase4 && ./node_modules/.bin/bun run test && cd gui && ../node_modules/.bin/bun test tests'\''' +``` + +Browser smoke also runs against the remote checkout through an SSH tunnel. Capture and +inspect screenshots for direct connected Usage (this-machine selected), relay connected +Usage, hub-offline machine pages, and post-disconnect standalone Usage. Put the required +GUI screenshots in the PR description; do not commit credentials, session meta, or +screenshots containing tokens. Record remote user/path/HEAD, commands, exit codes, +counts, and screenshot artifact names in C-phase evidence. Do not rerun unchanged green +checks. diff --git a/devlog/_plan/260827_remote_hub/070_phase5_deploy.md b/devlog/_plan/260827_remote_hub/070_phase5_deploy.md new file mode 100644 index 0000000000..112ffac2bb --- /dev/null +++ b/devlog/_plan/260827_remote_hub/070_phase5_deploy.md @@ -0,0 +1,483 @@ +# 070 — Phase 5: deployment integration and remote-hub dogfood + +Unit: `260827_remote_hub` · Phase: 5/6 · Work class: C4 (auth + deployment) · Status: implementation-ready + +Dependencies: Phases 1–4 are complete. In particular, this phase assumes the Phase-1 +`/readyz` protocol contract and `/v1/catalog`, the Phase-2 remote-session issuance +contract, the Phase-3 `ocx connect` transaction and per-client token file, and the +Phase-4 machine listener/two-plane GUI exist at the paths named by their phase docs. + +This document is the diff-level implementation contract. Every command that executes +TypeScript or tests runs on `ssh lidge-ai`, never on the workstation. The live deployment +smoke is the separately scoped `ssh clisu-oracle` dogfood described in §8. + +## 0. Locked outcome and boundaries + +Phase 5 makes a hub operable on a headless Linux host, macOS launchd host, or Docker +container without widening the data or consent planes. + +### IN + +- An opt-in second hub listener bound exactly to `127.0.0.1`, serving only packaged GUI + routes, SPA routes, `/opencodex-session`, and `/api/*`. +- Tailscale Serve as the recommended HTTPS frontend for that listener, with + `remoteGui.allowedTailscaleUsers` still deciding who may mint a session. +- Existing `ocx service install` for launchd/systemd. The data token is persisted only + through the existing owner-only `service-api-token` path and is never rendered into a + plist or unit. +- A Docker recipe that runs non-root, persists `~/.opencodex`, reads a mounted secret via + `OCX_API_TOKEN_FILE`, and probes both `/healthz` and `/readyz`. +- Headless OAuth using `oauthOpenBrowser:false` and the existing manual-code endpoint. +- A real `clisu-oracle` hub + MacBook client dogfood, including remote session issuance, + per-machine usage attribution, and protocol compatibility evidence. +- English deployment documentation in the new remote-hub guide. Locale and reference-page + synchronization is Phase 6 (§080), after the security contract is final. + +### OUT + +- No public Funnel preset, public-internet ingress, cloud firewall automation, generic + reverse proxy, Kubernetes, registry image, image publish workflow, or hosted control plane. +- No root `Dockerfile` or `.dockerignore` in this phase. The repository currently has + neither. Shipping one would create a maintained image/release surface requiring pinned + base digests, scanning, SBOM, signing, and rollback policy. The guide instead includes a + copyable multi-stage Dockerfile recipe and makes the operator own the resulting image. +- No service-manager rewrite. Windows remains supported by the existing service path but is + not a Phase-5 deployment target; the requested targets are systemd and launchd. +- No key-rotation UX, pairing throttles, skew fuzzing, catalog adversarial matrix, or relay + hardening; those are Phase 6. +- No traffic mirroring or usage-log mirroring. Connected clients render their own + `apiKeyId` slice from the hub store; disconnected clients render the local store. +- No import, direct or transitive, from a new subsystem into `src/router.ts`, + `src/server/lifecycle.ts`, or `src/server/responses/core.ts`. + +## 1. Deployment trust boundaries + +| Asset / boundary | Required control | +| --- | --- | +| Provider/OAuth credentials on hub | Never copied to a client, container layer, unit, plist, docs output, or dogfood artifact. | +| Data admission token | Delivered by `serviceApiTokenFilePath()` or `OCX_API_TOKEN_FILE`; never an argv value and never logged. | +| Management admin token | Remains hub-only. It may perform ordinary `/api/*` administration but must never mint or exchange into `gui-session`. | +| Tailscale identity headers | Trusted only when the request arrived on the new loopback management listener. Identical headers on the public listener are ignored. | +| Browser consent | Only the Phase-2 `gui-session` predicate authorizes consent routes. `allowedTailscaleUsers` is an issuance allowlist, not a new principal. | +| Docker volume | Holds provider credentials, OAuth state, usage, config, and service secrets; owner-writable only and never baked into an image. | +| Dogfood evidence | Records versions, protocol values, key ids/prefixes, counts, and HTTP status only; no tokens, emails, request bodies, account ids, or raw usage rows. | + +Rollback is configuration-first: disable the management ingress or Tailscale Serve without +changing the main data listener; stop the branch service and repair the prior release against +the same `OPENCODEX_HOME`; remove a container while retaining its named volume. + +## 2. Diff-level file-change map + +All existing paths below were verified against the 2026-08-28 tree. `NEW` paths have an +existing parent and are introduced deliberately. + +| Path | Change | Exact responsibility | +| --- | --- | --- | +| `src/types/config.ts` | MODIFY | Extend Phase-2 `OcxHubConfig` with the disabled/enabled `hub.managementIngress` union and document loopback-only semantics. Do not duplicate Phase-1 `runtimeRole` or Phase-2 `managementPublicOrigin` / `remoteGui` types. | +| `src/config.ts` | MODIFY | Parse the ingress opt-in, degrade malformed hand edits to disabled on load, and reject invalid live writes and port collisions. | +| `src/server/index.ts` | MODIFY | Compose the management listener using the existing optional-listener transaction, route allowlist, per-listener policy, rollback, and shutdown list. No body-level `await` may be added between the main `Bun.serve` and synchronous Lab activation. | +| `tests/loopback-listener-admission.test.ts` | MODIFY | Extend the existing optional-listener config/policy sibling tests for management-ingress defaults, role gate, and collisions. | +| `tests/loopback-listener-integration.test.ts` | MODIFY | Extend the existing real-socket sibling tests for bind address, GUI+/API allowlist, rollback, and all-listener shutdown. | +| `tests/server-management-auth.test.ts` | MODIFY | Prove ingress-scoped Tailscale identity, allowlist outcomes, pairing fallback, and the admin-token consent refusal. | +| `tests/service.test.ts` | MODIFY | Add only characterization needed by the documented hub install: systemd/launchd still read the protected token path and never embed the token. Do not change service generation. | +| `tests/oauth-manual-code.test.ts` | MODIFY | Exercise the existing manual-code route through the new management ingress; retain malformed/oversized negatives. | +| `tests/core-lab-boundary.test.ts` | VERIFY ONLY | Existing import-graph and synchronous-window guard must remain green; do not weaken it. | +| `docs-site/src/content/docs/guides/remote-hub.md` | NEW | Canonical English hub/client deployment guide: service, Tailscale, Docker, OAuth, health/readiness, rollback, and consent warning. | +| `docs-site/astro.config.mjs` | MODIFY | Add `guides/remote-hub` to Guides navigation. Phase 6 fills all configured locale labels/pages. | +| `structure/01_runtime.md` | MODIFY | Record the third listener as an opt-in composition-root concern and the service reuse decision. | +| `structure/05_gui-and-management-api.md` | MODIFY | Replace the loopback-only remote-GUI description with the final ingress-scoped issuance contract; preserve the admin-token boundary. | +| `structure/06_docs-and-release.md` | MODIFY | Record that Phase 5 ships a docs recipe, not an official Docker image/release channel. | + +Explicitly unchanged: `src/service.ts`, `src/lib/service-secrets.ts`, +`src/server/management/oauth-account-routes.ts`, `src/router.ts`, +`src/server/lifecycle.ts`, and `src/server/responses/core.ts`. Their current behavior is +reused and verified, not copied. + +## 3. Config and function contract + +### 3.1 Config keys + +Phase 1 owns `runtimeRole`; Phase 2 owns `hub.managementPublicOrigin`, +`remoteGui.allowedTailscaleUsers`, and `remoteGui.allowInsecureHttp`. Phase 5 adds only: + +```ts +export interface OcxHubConfig { // existing Phase-2 interface, shown extended + // Phase 2 field, shown for nesting only. + managementPublicOrigin?: string; + managementIngress?: + | { enabled: false } + | { enabled: true; port: number }; +} +``` + +Contract: + +- Missing and `{enabled:false}` are identical: no socket, no header trust, no new route. +- `{enabled:true}` is valid only when `runtimeRole === "hub"` and `port` is an integer in + `1..65535` distinct from `config.port` and from an enabled + `unauthenticatedLoopbackListener.port`. +- The hostname is not configurable. The socket always binds `127.0.0.1`; accepting a + caller-provided hostname would destroy the Tailscale-header trust argument. +- A malformed hand edit disables only this optional listener on read. `ocx config set` / + management writes fail with a concrete `schema_invalid: hub.managementIngress...` error. +- `managementPublicOrigin` is still the canonical browser-facing origin. Forwarded headers + never synthesize it. + +### 3.2 Listener integration signatures + +Keep helpers private to `startServer` unless a direct unit seam is already established by the +Phase-2 implementation: + +```ts +type ServerIngress = "public" | "unauthenticated-loopback" | "hub-management"; + +function managementIngressRouteAllowed(url: URL, req: Request): boolean; +function ingressForServer(server: Server): ServerIngress; +``` + +Use the exact Phase-2 context and facade; do not create a second session API: + +```ts +export function issueGuiSession( + req: Request, + config: OcxConfig, + state: ManagementAuthState, + context?: GuiSessionRequestContext, // { trustedTailscaleIngress: boolean; now?: number } +): GuiSessionBootstrap | null; +``` + +Pass `{trustedTailscaleIngress:true}` only when `requestServer === managementIngressServer`. +Every public/ordinary-loopback call passes false. The load-bearing fact is that the trusted +context is selected by a separately bound loopback socket; never infer it from Host, Origin, +`Forwarded`, `X-Forwarded-*`, or `Tailscale-User-*`. + +### 3.3 Management listener route allowlist + +The listener is GUI + management API only: + +- `GET`/`HEAD` packaged GUI assets and `/`. +- `GET` extensionless SPA routes that the existing GUI fallback serves. +- `GET /opencodex-session`. +- `/api/*`, with existing management authentication, Origin, session, CSRF, body-size, and + route authorization intact. +- Everything else is deterministic JSON 404 before a handler runs, including all `/v1/*`, + `/healthz`, `/readyz`, WebSocket upgrades, and unknown static paths. + +The public listener remains the health/readiness/data endpoint. This prevents Tailscale Serve +from becoming an accidental unmetered data-plane proxy. + +### 3.4 Startup and shutdown transaction + +Reuse the shape at `src/server/index.ts` around the existing public + unauthenticated-loopback +bind: + +1. Bind the public listener. +2. Bind the existing unauthenticated loopback listener when enabled. +3. Bind the hub management listener when enabled. +4. If either optional bind fails, synchronously initiate stop on every listener already bound, + preserve the original bind error, and throw. Do not add `await` to `startServer`. +5. Add every successfully bound optional server to the existing `server.stop` closure so the + shutdown promise joins all stops before background lifecycle release. +6. Log only bind address/port and mode. Never log identity headers, tokens, pairing codes, or + public-origin query strings. + +## 4. Existing service installer: Linux and macOS + +No `src/service.ts` implementation change is warranted. Verified owners: + +- `buildPlist(proxyEnv?)` in `src/service.ts` builds launchd and calls the common + `buildServiceShellCommand`. +- `buildUnit(proxyEnv?)` builds the systemd user unit and calls the same command. +- `buildServiceShellCommand` reads `serviceApiTokenFilePath()` into + `OPENCODEX_API_AUTH_TOKEN` at process start. +- `assertServiceAuthEnvironment()` refuses a non-loopback install without a token. +- `writeServiceApiTokenFile()` writes the token owner-only; unit/plist tests already assert + that the literal secret is absent. +- Windows additionally carries `OCX_API_TOKEN_FILE` in the generated wrapper at the current + `src/service.ts:1571+` path, but Windows deployment is not exercised here. + +Canonical hub setup shown in the guide (values are examples, not defaults): + +```bash +ocx config set runtimeRole hub +ocx config set hostname 100.64.0.10 +ocx config set hub.managementPublicOrigin '"https://hub-name.tailnet-name.ts.net"' +ocx config set hub.managementIngress '{"enabled":true,"port":10101}' +ocx config set remoteGui.allowedTailscaleUsers '["operator@example.com"]' + +# Read from a protected shell/secret manager; never put the token on argv. +export OPENCODEX_API_AUTH_TOKEN="$(openssl rand -hex 32)" +ocx service install +ocx service status +curl --fail --silent http://100.64.0.10:10100/healthz +curl --fail --silent http://100.64.0.10:10100/readyz +``` + +The guide must say that the `openssl` command is an operator-side example, not a source of +provider credentials, and that `service install` copies the value into the existing protected +token file. `ocx config show`, unit/plist output, screenshots, and support bundles must never +contain it. + +## 5. Tailscale Serve and ts.net certificate walkthrough + +### Recommended: Tailscale Serve + +```bash +tailscale serve --bg --https=443 http://127.0.0.1:10101 +tailscale serve status +``` + +Expected public browser origin is the exact HTTPS `https://..ts.net` +configured in `hub.managementPublicOrigin`. The guide must require: + +- `hub.managementIngress.enabled=true` and loopback bind proof before Serve is enabled. +- The user's exact Tailscale login in `remoteGui.allowedTailscaleUsers`; an empty list means + no remote identity can mint a session. +- No cloud-firewall opening for port 10101. It is loopback-only. +- `tailscale serve`, not Funnel. Funnel is public internet and remains out of scope. +- A negative check that direct tailnet access to `:10101` fails and a positive check that the + HTTPS page loads through Serve. + +### Manual ts.net certificate path + +For an operator-owned TLS proxy rather than Serve: + +```bash +tailscale cert hub-name.tailnet-name.ts.net +``` + +The certificate names only the full ts.net FQDN. The guide must tell the operator to protect +the private key, renew it through Tailscale's supported mechanism, and proxy only to +`127.0.0.1:10101`. A generic TLS proxy does not supply trustworthy Tailscale identity headers, +so it uses the Phase-2 single-use pairing rung; it must not fabricate `Tailscale-User-*`. + +Rollback: + +```bash +tailscale serve reset +ocx config set hub.managementIngress '{"enabled":false}' +ocx service repair +``` + +The reset command removes all Serve mappings on that node, so the guide must instruct the +operator to inspect `tailscale serve status` first and use a narrower supported removal command +when unrelated mappings exist. + +## 6. Docker recipe decision and contract + +The new guide contains a full example Dockerfile but the repository does not ship or publish +one in Phase 5. The example is multi-stage, pins the Bun version to the repository's +`package.json` dependency (`1.4.0` at planning time), requires the operator to resolve and pin +the base image digest, builds `gui/dist`, copies only package/runtime files plus installed +dependencies, and ends as the image's non-root `bun` user. + +Runtime contract: + +```text +working directory /home/bun/app +OPENCODEX_HOME /home/bun/.opencodex +persistent volume /home/bun/.opencodex +secret mount /run/secrets/ocx_api_token (0400/0440) +OCX_API_TOKEN_FILE /run/secrets/ocx_api_token +published data port 10100 only +management ingress 127.0.0.1:10101 inside the container; expose only through an + explicitly co-located tailnet/TLS topology +process bun run src/cli/index.ts start --port 10100 +``` + +The example must include: + +- `USER bun` (or an explicit numeric non-root uid/gid) in the final stage. +- No token in `ARG`, `ENV`, `COPY`, image history, Compose YAML, or command line. +- A named volume for `/home/bun/.opencodex`; deleting/replacing the container retains state. +- A liveness probe to `/healthz` and a separate readiness promotion check to `/readyz`. +- A data-authenticated `GET /v1/catalog` probe after ready, then one real routed response. +- `--read-only` where feasible, with writable volume and tmpfs exceptions. +- No Docker socket, host home, Codex home, SSH agent, or provider-key bind mount. + +If the secret is absent/unreadable, a non-loopback hub must fail before being accepted as +ready. A 200 `/healthz` alone is never deployment proof. + +## 7. Headless OAuth walkthrough + +The server behavior is reused from `src/oauth/open-browser-choice.ts` and +`src/server/management/oauth-account-routes.ts:208`; no new OAuth route is added. + +```bash +ocx config set oauthOpenBrowser false +``` + +Flow: + +1. From the authenticated remote GUI or management client, call `POST /api/oauth/login` + with the provider. The hub returns the authorization URL/instructions and does not invoke + a browser on the hub. +2. Open the URL on the operator's machine and complete authorization. +3. When the loopback callback cannot reach the hub, paste the final redirect URL or code into + the GUI/CLI, which sends `POST /api/oauth/login/code` with + `{provider,input}`. +4. Poll the existing status endpoint until complete. Never paste the code into shell argv, + logs, issue text, screenshots, or dogfood evidence. +5. Verify a routed request, not merely the OAuth status. + +The route keeps its existing 409 for no active flow/invalid code, 400 for unknown provider, +and 4096-character cap. Tailscale session issuance changes neither provider allowlisting nor +OAuth credential persistence. + +## 8. `clisu-oracle` dogfood runbook + +### 8.1 Safety and isolated homes + +- Use a dedicated branch worktree and dedicated `OPENCODEX_HOME` on `clisu-oracle`. +- Inventory existing listeners/services before selecting ports. Do not stop an unrelated + production proxy. +- Keep the main hub port on the Tailscale address and the management ingress on + `127.0.0.1`; do not open a cloud firewall rule. +- Record the exact git SHA, `ocx --version`, `/readyz` protocol fields, and client package + version before traffic. + +Branch deployment shape: + +```bash +ssh clisu-oracle +git -C ~/Developer/opencodex fetch origin codex/remote-hub-design +git -C ~/Developer/opencodex worktree add ~/ocx-dogfood/remote-hub FETCH_HEAD +cd ~/ocx-dogfood/remote-hub +bun install --frozen-lockfile +bun run build:gui +export OPENCODEX_HOME="$HOME/.opencodex-remote-hub-dogfood" +# Apply the §4 config with clisu-oracle's Tailscale IP/FQDN and protected token. +bun run src/cli/index.ts service install +``` + +The implementation turn must replace `FETCH_HEAD` with the recorded exact SHA before declaring +evidence; the sketch above is setup, not exact-head proof. + +### 8.2 MacBook connect and remote session + +1. On the hub, run `ocx gui pair` and copy the single-use, short-TTL code through the + interactive channel. Do not record it. +2. On the MacBook, run the Phase-3 connect command with the pairing code on stdin. The exact + Phase-3 signature must support transient `--pairing-code-stdin` (or its already-approved + equivalent) and must not accept a literal secret flag. +3. Assert `ocx connect status --json` reports protocol v1, hub URL, management URL, + management transport, and the non-secret client key id. +4. Assert `serviceApiTokenFilePath()` exists owner-only and contains the auto-issued per-client + data key; `config.toml` contains only the env-key reference. +5. Open `http://localhost:10100`, mint the remote session through HTTPS or the fixed relay, + and prove an ordinary management route works. +6. Prove a consent route is 403 with the admin token and succeeds only with the remote + `gui-session` + matching browser origin + CSRF. + +### 8.3 Per-machine usage slice + +1. Create traffic from the MacBook client key and from a second distinct client key. +2. Capture the MacBook's non-secret `apiKeyId` from connect status. +3. In connected mode, assert the Usage page reads the hub store and defaults to only that id; + the hub-wide toggle must show both clients. +4. Disconnect while the hub is reachable, then make one local standalone request. +5. Assert the disconnected Usage page reads local `usage.jsonl`, contains only local traffic, + and does not contain mirrored connect-period rows. +6. Reconnect and assert the earlier MacBook slice still exists on the hub. + +Counts, key ids, and timestamps may be recorded. Raw usage rows and all credentials may not. + +### 8.4 Release ↔ dev protocol smoke + +Two directions are mandatory once the latest published release contains protocol v1 and the +remote client commands: + +| Hub | Client | Expected | +| --- | --- | --- | +| Branch/dev build on `clisu-oracle` | `@bitkyc08/opencodex@latest` on MacBook | Same-major connect, catalog sync, one routed request, remote session. | +| `@bitkyc08/opencodex@latest` in a second isolated home/port | Branch/dev client on MacBook | Same-major connect with feature detection; unsupported optional features stay disabled. | + +Activation grounding: the 2026-08-28 tree has no released `connect` command. Therefore a current +pre-v1 `@latest` cannot construct either row and must not be reported as a pass. Before the first +v1 release, use a release-shaped `npm pack` candidate only as preflight evidence and label it +`candidate`, not `latest-release`. Phase 5 reaches terminal acceptance only after either (a) a +published protocol-v1 release makes both rows constructible or (b) the maintainer explicitly moves +the live release-pair gate to the post-release Phase-6 outcome while retaining the skew contract +tests. No silent substitution is allowed. + +## 9. Test plan and activation matrix + +Existing sibling files to extend are named in §2. Do not create a broad generic +`remote-hub.test.ts` that duplicates their established real-socket/auth/service harnesses. + +| Conditional path | Constructible activation | Required observation / owner test | +| --- | --- | --- | +| ingress missing/disabled | Hub config omits it or sets false | Exactly one fewer `Bun.serve`; public behavior byte-compatible. `loopback-listener-admission`. | +| ingress on non-hub | `runtimeRole=standalone|client`, enabled true | Write-time schema rejection before bind. `loopback-listener-admission`. | +| valid ingress | Hub + unique port | Socket binds only `127.0.0.1`; GUI, SPA, bootstrap, and authenticated `/api` work. `loopback-listener-integration`. | +| disallowed route | Request `/v1/catalog`, `/readyz`, WS upgrade, or unknown path on ingress | JSON 404 before route handling; no provider call. `loopback-listener-integration`. | +| port collision | Match public or unauthenticated-loopback port | Config rejection before startup. `loopback-listener-admission`. | +| optional bind failure | Occupy ingress port before `startServer` | Startup throws original error and every earlier listener becomes rebindable. `loopback-listener-integration`. | +| normal shutdown | Enable all three listeners, then `server.stop(true)` | All three ports become rebindable; lifecycle release happens once. `loopback-listener-integration`. | +| spoofed Tailscale header on public listener | Send allowlisted identity header to main bind | No remote session. `server-management-auth`. | +| Tailscale allowlist match on ingress | Hub ingress + HTTPS public origin + allowed identity | Session minted with server/browser origins and ingress issuance. `server-management-auth`. | +| empty/wrong allowlist | Ingress request with absent or nonmatching identity | No session; admin token still cannot exchange. `server-management-auth`. | +| pairing via generic TLS proxy | Valid one-use origin-bound grant, no Tailscale identity | Session minted once; replay fails. `server-management-auth`. | +| service token present | Non-loopback hub + env token + install builder | Protected token path referenced; literal absent from unit/plist. `service.test`. | +| service token absent | Non-loopback hub, no env/file token | Install refuses before registration. `service.test`. | +| headless OAuth | `oauthOpenBrowser=false`, active provider flow | URL returned, no server-side open, manual code accepted. `oauth-manual-code`. | +| bad manual code | Unknown provider, no active flow, or >4096 input | Existing 400/409 response; no credential mutation. `oauth-manual-code`. | +| Docker secret missing | Non-loopback container without mounted token | Not ready / startup refusal; never accept health alone. Deployment smoke. | +| connected usage | Two client ids create hub traffic | This-machine slice and hub-wide toggle differ; hub store only. Dogfood. | +| disconnected usage | Disconnect then local standalone traffic | Local store only; no mirrored hub rows. Dogfood. | +| protocol same-major | Constructible v1 release/dev peers | Both directions connect with feature detection. Dogfood + Phase-6 skew tests. | + +## 10. Acceptance criteria + +- [ ] Default standalone and hub-with-ingress-disabled startup remain byte-compatible at the + public listener. +- [ ] Management ingress is kernel-bound to `127.0.0.1`, default-deny, and serves no data, + health, readiness, or WebSocket route. +- [ ] A failed optional bind rolls back every prior bind; normal stop joins every listener. +- [ ] `src/server/index.ts` remains synchronous through the guarded startup window and no new + subsystem enters the three core import graphs. +- [ ] Tailscale identity is accepted only on management ingress and only for an exact configured + user; admin-token-only consent remains 403. +- [ ] launchd/systemd installs use the existing secret-file flow and prove serving, readiness, + authenticated catalog, and a real routed response. +- [ ] Docker recipe is non-root, volume-backed, secret-file-based, and checks liveness + + readiness + authenticated functionality. +- [ ] Headless OAuth completes without opening a hub browser and produces a usable provider + route. +- [ ] `clisu-oracle` dogfood proves MacBook connect, remote session, machine usage slice, + disconnect/local-store behavior, and rollback. +- [ ] Release/dev compatibility is either genuinely run with a protocol-v1 published peer or + explicitly remains a named, non-waived gate per §8.4. +- [ ] No token, pairing grant, OAuth code, email, account id, request body, or raw usage row is + present in git diff or evidence. + +## 11. Verification — remote only + +Do not run any command below locally. Use an isolated checkout on `lidge-ai` at the exact SHA. + +```bash +VERIFY_SHA="$(git rev-parse HEAD)" +ssh lidge-ai "set -eu + export PATH=\$HOME/.bun/bin:\$PATH + repo=\$HOME/ocx-verify/remote-hub-p5 + git -C \$repo fetch origin + git -C \$repo checkout --detach $VERIFY_SHA + test \"\$(git -C \$repo rev-parse HEAD)\" = \"$VERIFY_SHA\" + cd \$repo + bun install --frozen-lockfile + bun run typecheck + bun test tests/loopback-listener-admission.test.ts \ + tests/loopback-listener-integration.test.ts \ + tests/server-management-auth.test.ts \ + tests/service.test.ts \ + tests/oauth-manual-code.test.ts \ + tests/core-lab-boundary.test.ts + cd docs-site + bun install --frozen-lockfile + bun run build +" +``` + +Then execute §8 on `clisu-oracle`; record exact SHA/version, sanitized protocol fields, HTTP +statuses, key ids/counts, and rollback result. A green `lidge-ai` suite does not replace the +deployment smoke, and a green `/healthz` does not replace ready/catalog/routed/session proof. diff --git a/devlog/_plan/260827_remote_hub/080_phase6_hardening.md b/devlog/_plan/260827_remote_hub/080_phase6_hardening.md new file mode 100644 index 0000000000..6e24eaa8b9 --- /dev/null +++ b/devlog/_plan/260827_remote_hub/080_phase6_hardening.md @@ -0,0 +1,633 @@ +# 080 — Phase 6: hardening, documentation sync, and release gate + +Unit: `260827_remote_hub` · Phase: 6/6 · Work class: C4 (auth, secrets, relay, release) · Status: implementation-ready + +Dependencies: Phases 1–5 are behaviorally complete, including a `clisu-oracle` dogfood +record. This phase hardens the contracts; it does not redesign hub/client roles or introduce +another transport. + +Every executable verification command in this document runs on `ssh lidge-ai`, never on +the workstation. Full-suite execution is serialized with other `lidge-ai` suite owners. + +## 0. Locked outcome and boundaries + +### IN + +- Recoverable per-client data-key rotation with a one-time secret response, bounded overlap, + client-side atomic token-file replacement, explicit commit/abort, and stable `apiKeyId` usage + attribution. +- Remote-session self-logout, automatic session invalidation after key commit/delete, and + disconnect-time best-effort revocation without making hub availability a prerequisite for + offline local restore. +- Pairing issuance/redemption limits, one-use semantics, bounded active state, and safe 429s. +- Protocol negotiation matrix tests covering the v1 compatibility floor and feature detection. +- Adversarial `/v1/catalog` consumer tests for decompressed size, malformed JSON, invalid schema, + row limits, stale ETags, and no-write failure behavior. +- Fixed-target relay negatives for SSRF, redirect escape, authority confusion, hop-by-hop header + injection, CL/TE ambiguity, response header stripping, and bounded streaming. +- Public documentation synchronized across every locale currently configured by Starlight. +- Full lidge gate and explicit MAINTAINERS security-review evidence for every auth-surface PR. + +### OUT + +- No multi-hub replication, failover, public Funnel, generic reverse proxy, VPN replacement, + identity provider, organization/tenant RBAC, key escrow, usage mirroring, or automatic release. +- No provider-key/OAuth rotation. This phase rotates only per-client data admission keys. +- No data key gains general `/api/*` authority. Rotation uses a transient pairing/admin authority + and the existing management gate; a data key cannot mint a GUI session or rotate itself. +- No admin-token-to-`gui-session` exchange, including in tests, migration, compatibility, or + emergency fallback paths. +- No edits to or new imports from remote subsystems into `src/router.ts`, + `src/server/lifecycle.ts`, or `src/server/responses/core.ts`. +- No body-level `await` in the guarded `src/server/index.ts` startup window. + +## 1. Threat model and must-pass controls + +| Attacker / failure | Asset at risk | Required control | +| --- | --- | --- | +| Holder of one client data key | Other clients, management, provider keys | Data-only scope; rotation needs transient management authority; same key id never reveals another key. | +| Holder of hub admin token | Browser-consent routes | May rotate/revoke ordinary data credentials, but can never mint or exchange into `gui-session`. | +| Pairing-code guesser/replayer | Remote GUI consent session | High-entropy one-use grant, short TTL, origin binding, per-grant and aggregate attempt caps, immediate consumption. | +| Malicious/compromised hub response | Client filesystem/memory | Decompressed byte cap, schema/row validation, atomic write after validation, LKG retained, no local fallback. | +| Browser controlling relay path/headers | Hub network and credentials | Destination fixed by connection state; route allowlist; redirects blocked; authority and hop-by-hop headers rebuilt. | +| Header-smuggling attempt | Hub request parser/proxy chain | Reject transfer-encoding, conflicting content-length, connection-nominated headers, CR/LF values, and upgrade paths. | +| Protocol-skewed peer | Local client files / silent misroute | Negotiate before any local write; reject incompatible floors with explicit upgrade error; unknown features stay off. | +| Rotation crash between hub and client | Client availability | Old and pending keys overlap for a bounded window; commit only after new-key probe; abort/expiry preserves old key. | +| Session surviving credential change | Revoked client access | Key commit/delete invalidates sessions and pairing grants bound to that `apiKeyId`; current in-flight data turn may finish, next admission fails. | +| Logs/evidence | Tokens, codes, identities | Record ids/prefixes/counts/status only; privacy scan; no raw secret, email, Origin query, request body, or account id. | + +Security level: ASVS L2 for the remote management/session surface. Applicable architecture, +session, access-control, validation, secret-rotation, CORS, error, and API checks must be attached +to the security review; a generic checklist tick with no test/evidence link is insufficient. + +## 2. Diff-level file-change map + +All existing paths were verified against the 2026-08-28 tree. Paths under `src/client/` and the +remote-session/pairing owners are Phase-2–4 dependencies; those directories are absent on the +planning base and must exist before Phase 6 begins. If an earlier phase deliberately chose a +different exact owner path, amend this file mechanically before implementation rather than adding +a second owner. + +### 2.1 Key rotation and session invalidation + +| Path | Change | Exact responsibility | +| --- | --- | --- | +| `src/types/config.ts` | MODIFY | Extend `OcxApiKeyEntry` with an optional, secret-bearing pending-rotation record; keep stable id/name/createdAt. | +| `src/config.ts` | MODIFY | Validate/degrade pending rotation independently so one malformed pending record cannot reset providers or revoke the current key. | +| `src/server/auth-cors.ts` | MODIFY | Admit an unexpired pending key under the same configured `apiKeyId`; never return or serialize its secret. | +| `src/server/management/api-key-rotation.ts` | NEW | Single owner for start/commit/abort/expiry cleanup and constant-time rotation-id comparison. | +| `src/server/management/oauth-account-routes.ts` | MODIFY | Add the three rotation operations next to existing `/api/keys` CRUD and call session invalidation after commit/delete. Existing GET continues to mask all secrets. | +| `src/server/management/session-routes.ts` | NEW | `POST /api/session/logout` self-revocation route; requires the current `gui-session` and CSRF. Admin token receives 403, not a promoted session. | +| `src/server/management-api.ts` | MODIFY | Wire `handleSessionRoutes` and the narrow session-control dependency into `ManagementContext`. | +| `src/server/management/context.ts` | MODIFY | Carry only the revocation interface, never the raw admin token or session map. | +| `src/server/management-auth.ts` | MODIFY | Associate remote sessions with optional `apiKeyId`; export narrow current/by-key invalidation helpers; preserve one shared auth predicate. | +| `src/client/connect.ts` | MODIFY | Implement `ocx connect rotate`: transient authority, start rotation, atomically replace token file, validate new key, commit, and restore+abort on failure. | +| `src/client/state.ts` | MODIFY | Persist non-secret key id and pending operation metadata only; never persist admin/pairing authority or old/new secret. | +| `src/cli/access.ts` | MODIFY | Add management-side `ocx access key rotate ` start/commit/abort UX with one-time secret warning; no literal secret flags. | +| `src/cli/registry.ts` | MODIFY | Document rotation command shapes and transient-authority requirement. | +| `gui/src/pages/ApiKeys.tsx` | MODIFY | Load pending status, start/commit/abort rotation, and render the new secret exactly once. | +| `gui/src/components/apikeys-workspace/ApiKeysWorkspace.tsx` | MODIFY | Accessible rotation confirmation/status/error UI; distinguish pending, committed, expired, and aborted outcomes. | +| `gui/src/i18n/en.ts` | MODIFY | Canonical rotation/session strings and `TKey`. | +| `gui/src/i18n/de.ts` | MODIFY | Locale parity. | +| `gui/src/i18n/fr.ts` | MODIFY | Locale parity. | +| `gui/src/i18n/ja.ts` | MODIFY | Locale parity. | +| `gui/src/i18n/ko.ts` | MODIFY | Locale parity. | +| `gui/src/i18n/ru.ts` | MODIFY | Locale parity. | +| `gui/src/i18n/tr.ts` | MODIFY | Locale parity. | +| `gui/src/i18n/zh-TW.ts` | MODIFY | Locale parity. | +| `gui/src/i18n/zh.ts` | MODIFY | Locale parity. | +| `tests/api-keys-routes.test.ts` | MODIFY | Rotation route contract, masking, pending overlap, commit, abort, expiry, malformed inputs, and delete invalidation. | +| `tests/data-plane-admission-identity.test.ts` | MODIFY | Current and pending secret map to one id; expired/committed/aborted secrets do not admit. | +| `tests/api-key-attribution.test.ts` | MODIFY | Traffic before/during/after rotation remains one `apiKeyId` bucket. | +| `tests/server-management-auth.test.ts` | MODIFY | Session self-logout/by-key invalidation and admin-token refusal. | +| `gui/tests/apikeys-actions.test.tsx` | MODIFY | Start/commit/abort wire actions and one-time secret handling. | +| `gui/tests/apikeys-mutation-timeout.test.tsx` | MODIFY | Rotation controls recover after bounded network failure. | +| `gui/tests/apikeys-workspace.test.tsx` | MODIFY | Accessible rendered states and confirmations. | +| `gui/tests/locale-parity.test.ts` | VERIFY/MODIFY | All new visible strings exist in every GUI locale. | + +### 2.2 Pairing, protocol, catalog, and relay hardening + +| Path | Change | Exact responsibility | +| --- | --- | --- | +| `src/server/gui-session.ts` | MODIFY (Phase-2 owner) | Extend the existing digest-only pairing/session owner with bounded attempt stores, source-key derivation, 429 result, expiry, and by-key revocation. | +| `src/remote/protocol.ts` | MODIFY (Phase-1 owner) | Extend the existing pure parser/interval-compatibility owner with additive feature intersection; no I/O or local writes. | +| `src/client/catalog.ts` | MODIFY | Bounded decompressed read, strict remote catalog validation, ETag/LKG handling, and atomic-write precondition. | +| `src/client/relay.ts` | MODIFY | Fixed-target URL construction, request/response header rebuilding, redirect refusal, body/stream caps, and redacted errors. | +| `tests/server-management-auth.test.ts` | MODIFY (Phase-2 owner) | Deterministic pairing attempt/TTL/capacity/replay/race matrix in the existing primary session suite. | +| `tests/proxy-liveness.test.ts` | MODIFY (Phase-1 owner) | Protocol metadata parsing remains additive while ordinary readiness identity remains strict. | +| `tests/cli-ready-subprocess.test.ts` | MODIFY | Full released-process skew matrix and no-write mismatch outcomes. | +| `tests/remote-catalog.test.ts` | MODIFY (Phase-1/3 owner) | Oversized/malformed/schema/ETag/LKG/no-write adversarial matrix. | +| `tests/client-hub-relay.test.ts` | MODIFY (Phase-4 owner) | SSRF, redirect, authority, smuggling, header stripping, and bounded streaming negatives. | +| `tests/bounded-body.test.ts` | MODIFY only if shared helper changes | Reuse exact-cap/one-byte-over/trickle semantics; do not duplicate the helper contract in client tests. | +| `tests/credential-redirect-guard.test.ts` | EXTEND/REUSE | Existing sibling evidence for credential-bearing redirect refusal. | +| `tests/provider-outbound-private-network.test.ts` | EXTEND/REUSE | Existing sibling vocabulary for destination classification; relay remains fixed-target rather than a provider fetch. | +| `tests/cli-ready.test.ts` | EXTEND/REUSE | Existing readiness identity/shape harness for protocol fields. | +| `tests/cli-ready-subprocess.test.ts` | EXTEND/REUSE | Released CLI subprocess compatibility fixtures and no-write rejection. | +| `tests/core-lab-boundary.test.ts` | VERIFY ONLY | Core import graph and synchronous startup window remain green. | + +### 2.3 Source-of-truth and public docs + +| Path | Change | Exact responsibility | +| --- | --- | --- | +| `structure/01_runtime.md` | MODIFY | Final hub/client protocol, listener, catalog, and relay ownership map. | +| `structure/02_config-and-codex-home.md` | MODIFY | Client token-file ownership, rotation overlap, disconnect deletion, and no usage mirroring. | +| `structure/05_gui-and-management-api.md` | MODIFY | Final credential classes, issuance ladder, revocation, rate limits, origin/CSRF, and admin consent refusal. | +| `structure/06_docs-and-release.md` | MODIFY | Correct the locale inventory and record the remote-hub release gate. | +| `structure/09_client-integrations.md` | MODIFY | Remote connection journal/restore, direct data path, fixed relay, and launcher-scoped Claude behavior. | +| `docs-site/astro.config.mjs` | MODIFY | Final Remote Hub sidebar label/translations for every configured locale. | + +The roadmap's “5 locales” count is stale. `docs-site/astro.config.mjs` currently declares eight +site locales: root English, `fr`, `ko`, `zh-cn`, `zh-tw`, `ru`, `ja`, and `tr`. Phase 6 must not +drop the later Russian, Japanese, or Turkish trees merely to satisfy the older count. + +New translated guide files (English was created in Phase 5): + +- `docs-site/src/content/docs/fr/guides/remote-hub.md` +- `docs-site/src/content/docs/ko/guides/remote-hub.md` +- `docs-site/src/content/docs/zh-cn/guides/remote-hub.md` +- `docs-site/src/content/docs/zh-tw/guides/remote-hub.md` +- `docs-site/src/content/docs/ru/guides/remote-hub.md` +- `docs-site/src/content/docs/ja/guides/remote-hub.md` +- `docs-site/src/content/docs/tr/guides/remote-hub.md` + +Existing pages to synchronize in all eight trees: + +- CLI lifecycle/connect/service: + `docs-site/src/content/docs/reference/cli/lifecycle.md`, + `docs-site/src/content/docs/fr/reference/cli/lifecycle.md`, + `docs-site/src/content/docs/ko/reference/cli/lifecycle.md`, + `docs-site/src/content/docs/zh-cn/reference/cli/lifecycle.md`, + `docs-site/src/content/docs/zh-tw/reference/cli/lifecycle.md`, + `docs-site/src/content/docs/ru/reference/cli/lifecycle.md`, + `docs-site/src/content/docs/ja/reference/cli/lifecycle.md`, + `docs-site/src/content/docs/tr/reference/cli/lifecycle.md`. +- Server/runtime config: + `docs-site/src/content/docs/reference/configuration/server.md`, + `docs-site/src/content/docs/fr/reference/configuration/server.md`, + `docs-site/src/content/docs/ko/reference/configuration/server.md`, + `docs-site/src/content/docs/zh-cn/reference/configuration/server.md`, + `docs-site/src/content/docs/zh-tw/reference/configuration/server.md`, + `docs-site/src/content/docs/ru/reference/configuration/server.md`, + `docs-site/src/content/docs/ja/reference/configuration/server.md`, + `docs-site/src/content/docs/tr/reference/configuration/server.md`. +- Management/data contracts: + `docs-site/src/content/docs/reference/management-api.md`, + `docs-site/src/content/docs/fr/reference/management-api.md`, + `docs-site/src/content/docs/ko/reference/management-api.md`, + `docs-site/src/content/docs/zh-cn/reference/management-api.md`, + `docs-site/src/content/docs/zh-tw/reference/management-api.md`, + `docs-site/src/content/docs/ru/reference/management-api.md`, + `docs-site/src/content/docs/ja/reference/management-api.md`, + `docs-site/src/content/docs/tr/reference/management-api.md`. +- Dashboard two-plane/session/usage behavior: + `docs-site/src/content/docs/guides/web-dashboard.md`, + `docs-site/src/content/docs/fr/guides/web-dashboard.md`, + `docs-site/src/content/docs/ko/guides/web-dashboard.md`, + `docs-site/src/content/docs/zh-cn/guides/web-dashboard.md`, + `docs-site/src/content/docs/zh-tw/guides/web-dashboard.md`, + `docs-site/src/content/docs/ru/guides/web-dashboard.md`, + `docs-site/src/content/docs/ja/guides/web-dashboard.md`, + `docs-site/src/content/docs/tr/guides/web-dashboard.md`. + +English is canonical. Translations may be concise, but must preserve warnings, config keys, +defaults, command flags, endpoint names, and the “admin token never grants consent” statement. + +## 3. Per-client key rotation contract + +### 3.1 Persisted shape + +```ts +export interface OcxPendingApiKeyRotation { + id: string; // random opaque rotation id, compared constant-time + key: string; // pending data secret; never serialized by GET/list/status + createdAt: string; + expiresAt: string; +} + +export interface OcxApiKeyEntry { + id: string; + name: string; + key: string; + createdAt: string; + pendingRotation?: OcxPendingApiKeyRotation; +} +``` + +One configured id owns at most one pending rotation. The overlap TTL is 10 minutes. The old +and pending keys both admit data during that window and both attribute to the same id. Expiry +removes only the pending key; the old key remains authoritative. A process restart reloads the +durable pending state and applies the same expiry rule. + +### 3.2 Pure owner signatures + +```ts +export type ApiKeyRotationStart = { + id: string; + name: string; + key: string; // returned once by start only + rotationId: string; + expiresAt: string; +}; + +export function startApiKeyRotation( + config: OcxConfig, + keyId: string, + now?: number, +): ApiKeyRotationStart | { error: "not-found" | "already-pending" }; + +export function commitApiKeyRotation( + config: OcxConfig, + keyId: string, + rotationId: string, + now?: number, +): { ok: true } | { error: "not-found" | "expired" | "mismatch" }; + +export function abortApiKeyRotation( + config: OcxConfig, + keyId: string, + rotationId: string, +): boolean; +``` + +Routes stay under the existing management auth: + +```text +POST /api/keys/rotate {id} -> 201 + one-time key +POST /api/keys/rotate/commit {id,rotationId} -> 200 +DELETE /api/keys/rotate {id,rotationId} -> 200 +``` + +Unknown fields are rejected. Error envelopes distinguish not found (404), conflict/already +pending or mismatched/expired (409), invalid body (400), and busy persistence (existing 503). +No response except successful start contains the pending secret. + +### 3.3 Client transaction + +`ocx connect rotate` requires one transient `--pairing-code-stdin` or +`--admin-token-stdin`; neither is persisted. It performs: + +1. Read current key id and current token into memory; create no output containing either secret. +2. Start rotation; receive pending secret once. +3. Write pending secret to a same-directory owner-only temp, harden it with the same + `serviceApiTokenFilePath()` rules, fsync, and atomically replace the token file. +4. Probe authenticated `/v1/catalog` with the new key and verify the expected client key id via + the safe response/diagnostic contract. +5. Commit rotation. Commit invalidates old-key admission, sessions, and pairing grants bound to + that key id. An already-admitted in-flight turn may complete; the next old-key request is 401. +6. If steps 3–5 fail before a confirmed commit, restore the old token atomically and abort the + pending rotation. If commit outcome is uncertain, probe with both keys: exactly one accepted + result determines the local file; never replay commit blindly. + +The GUI exposes the same lifecycle for an operator updating a client manually, with explicit +copy-once and commit-after-client-probe wording. Closing the modal does not imply commit; pending +state remains visible and abortable until expiry. + +## 4. Session invalidation contract + +Phase-2 `GuiSessionRecord` gains optional `apiKeyId` for sessions created from a client-bound +pairing grant. Loopback and Tailscale sessions without a client association may omit it. + +```ts +export interface ManagementSessionControl { + revokeCurrent(req: Request): boolean; + revokeForApiKeyId(apiKeyId: string): number; +} + +export function createManagementSessionControl( + state: ManagementAuthState, +): ManagementSessionControl; +``` + +`handleManagementAPI` receives this narrow control (directly or through `ManagementContext`), +not the session map and never the admin token. `POST /api/session/logout` requires +`principal === "gui-session"`, same browser Origin, and CSRF. Admin-token calls return 403. + +Rotation commit and key deletion call `revokeForApiKeyId` only after config persistence commits. +A persistence failure leaves key and sessions unchanged. `ocx disconnect` calls self-logout +best-effort before local restore; hub-down still restores from the local journal and reports that +remote session expiry/revocation could not be confirmed. + +## 5. Pairing rate limits + +The Phase-2 `src/server/gui-session.ts` owner remains the only grant store. Add no generic middleware and no timer on +the standalone/core request path. + +```ts +export interface PairingAttemptContext { + ingress: "public" | "hub-management"; + peerAddress: string | null; + tailscaleUser: string | null; // populated only by trusted management ingress + browserOrigin: string; +} + +export type PairingAttemptResult = + | { allowed: true } + | { allowed: false; retryAfterSeconds: number; reason: "grant" | "source" | "capacity" }; +``` + +Fixed starting limits (configurable downward only is unnecessary in v1): + +- Grant TTL: Phase-2 short TTL, capped at 10 minutes. +- One successful redemption consumes immediately before session return. +- Five failed redemption attempts burn that grant. +- Ten failed attempts per source key in 10 minutes produce 429; source key is allowlisted + Tailscale identity on trusted ingress, otherwise immediate peer address, otherwise the global + anonymous bucket. +- At most 128 live grants and 1,024 source buckets. Capacity refusal is 429 and creates no grant. +- Expired grant/source entries are pruned synchronously on pairing operations; no core timer. +- `Retry-After` is integer seconds, bounded by the remaining window, and contains no identity. +- Constant-time code comparison; generic invalid/expired/consumed response; no existence oracle. +- Rotation commit/key delete revoke unconsumed grants associated with that client key id. + +Rate-limit logs contain only reason, ingress class, and aggregate count. No code, raw IP, +Tailscale user/email, Origin, token, or account id. + +## 6. Protocol skew matrix + +Phase 1's wire fields remain `protocol`, `minimumClientProtocol`, and `managementUrl`; Phase 6 +adds optional additive `features: string[]`. Protocol v1 is the compatibility floor. Negotiation is pure and +must run before catalog download, token-file writes, injector preflight, journal writes, or state +persistence. + +```ts +export interface RemoteReadyMetadata { + protocol: number; + minimumClientProtocol: number; + managementUrl: string; + features?: string[]; +} + +export type RemoteProtocolCompatibility = + | { ok: true; metadata: RemoteReadyMetadata; features: Set } + | { ok: false; reason: "invalid" | "hub-too-new" | "hub-too-old"; message: string }; + +export function checkRemoteProtocolCompatibility( + value: unknown, + client?: { protocol: number; minimumHubProtocol: number; features?: readonly string[] }, +): RemoteProtocolCompatibility; +``` + +Required matrix: + +| Hub descriptor | Client | Activation | Expected | +| --- | --- | --- | --- | +| p1/min1/baseline | p1/min1 | first v1 pair | Accept baseline. | +| p2/min1/A+B | p1/min1/A | newer dev hub, latest v1 client | Accept p1 behavior; feature intersection = A. | +| p2/min2 | p1/min1 | hub dropped v1 floor | Reject `hub-too-new` before write. | +| p1/min1 | p2/min2 | dev client requires newer hub | Reject hub-too-old before write. | +| p1/min1/unknown-X | p1/min1 | additive unknown feature | Accept; unknown feature remains disabled. | +| missing/NaN/fraction/negative/min>protocol or invalid `managementUrl` | p1 | malformed or legacy non-v1 hub | Exact Phase-1 `invalid` message; zero local writes. | +| valid descriptor, `/readyz` pending/failed | any | startup not ready | Do not negotiate or write; preserve existing readiness behavior. | + +The guaranteed live pair is dev hub ↔ latest published protocol-v1 client and the reverse. +Until a release with `connect` exists, fixture tests and release-shaped package candidates are +preflight only; they do not satisfy the live published-pair gate recorded in 070 §8.4. + +## 7. Catalog adversarial contract + +The remote consumer owns the Phase-1 `MAX_REMOTE_CATALOG_BYTES` 32 MiB **decompressed** body cap +and a 2,000 model-row cap. Use the existing bounded-response helper when +its API can express this without importing `src/server/responses/core.ts`; otherwise add a client +leaf that depends only on `src/lib/bounded-body.ts`. + +Validation order: + +1. Status/redirect: only 200 or valid 304; redirect is refused, not followed. +2. Content type is JSON-compatible; content length above cap rejects early, but streamed bytes are + still counted because length may be absent or false. +3. Read at most cap+1 decompressed bytes; exactly cap is allowed, one byte over cancels/discards. +4. Parse JSON once. Top level must be a plain object with `models` array. +5. `models.length <= 2000`; every row is a plain object with a non-empty printable `slug` string; + reject NUL/control characters and duplicate slugs. Preserve additive unknown fields after the + required shape passes. +6. Serialize/write only after complete validation. Failed refresh retains the exact LKG bytes and + stale age; no local provider fallback and no partial file. +7. A 304 without an existing validated LKG triggers one unconditional refetch; a second 304 is a + protocol error, not an empty catalog. + +Adversarial tests include: forged small Content-Length with oversized chunks, gzip/decompressed +oversize fixture, exact-cap and cap+1, fragmented trickle, malformed/truncated/UTF-8 JSON, null, +array top level, missing/non-array models, 2,001 rows, non-object row, empty/control/duplicate slug, +unexpected future fields, stale/mismatched ETag, and filesystem write failure after validation. +Every rejection asserts token/catalog/state/journal bytes are unchanged. + +## 8. Relay SSRF and header-smuggling negatives + +`src/client/relay.ts` is not a general proxy. Its destination is the validated +`connectionState.managementUrl` captured when the listener starts. A request cannot supply or +override scheme, host, port, userinfo, fragment, DNS result, or redirect target. + +### 8.1 URL/path rules + +- Accept only relative paths in the Phase-4 allowlist: session bootstrap and the explicitly + supported `/api/*` management namespace. +- Reject absolute-form URLs, scheme-relative `//host`, backslashes, userinfo, fragments, + percent-decoded authority/path confusion, encoded slash/backslash traversal, and any path that + normalizes outside the allowlist. +- Resolve against the fixed management origin, then assert protocol/hostname/port equal the fixed + origin before fetch. +- `redirect:"manual"`/`"error"`; every 3xx is an error and Location is never followed or returned + with credentials. +- Private/tailnet destinations are allowed because the operator selected the hub; SSRF prevention + is fixed authority, not a blanket public-IP rule. + +### 8.2 Request headers and body + +Build a fresh allowlist. Preserve only required content negotiation plus Phase-2 session/origin/CSRF +headers. Never forward caller `Host`, `Forwarded`, `X-Forwarded-*`, `Tailscale-User-*`, cookies, +proxy auth, upgrade, or data-plane authorization. The relay's management session credential is +attached by the trusted client owner, not copied from arbitrary browser input. + +Strip the standard hop-by-hop set and every header named by `Connection`: `connection`, +`keep-alive`, `proxy-authenticate`, `proxy-authorization`, `te`, `trailer`, +`transfer-encoding`, and `upgrade`. Reject any request carrying Transfer-Encoding, multiple or +invalid Content-Length, CL/TE together, CR/LF in a header value, unsupported method, or body above +the management cap. Do not rely on Fetch normalization as the only smuggling defense; tests call +the pure validator with raw tuples for otherwise-unconstructible header shapes. + +### 8.3 Response rules and streaming + +- Rebuild response headers and strip hop-by-hop headers, `Set-Cookie`, proxy auth, server identity + headers, Tailscale identity, and connection-nominated headers. +- Preserve safe content type, cache control, ETag, retry-after, and approved CORS/session bootstrap + metadata only. +- Enforce Phase-4 management body caps. Phase-6 streaming uses backpressure and abort propagation; + it must not buffer an unbounded response or continue after browser disconnect. +- Errors name only status/category and fixed hub label. No destination URL query, session token, + admin token, response body, or identity header reaches logs. + +Negative test servers bind loopback only. No test reaches cloud metadata, public internet, LAN, or +the user's configured real hub. + +## 9. Test plan and activation matrix + +Existing siblings to extend are listed in §2. Tests created by earlier phases remain their owners; +Phase 6 extends them rather than creating parallel “hardening2” files. + +| Conditional path | Constructible activation | Required observation | +| --- | --- | --- | +| rotation start | Existing key, no pending rotation, admin/session authority | New key returned once; old+pending both admit under same id; list masks both. | +| second start | Existing unexpired pending rotation | 409; no third secret/state change. | +| client commit | New key written + authenticated catalog succeeds | Pending promoted atomically; old next request 401; id/usage bucket stable; sessions/grants invalidated. | +| client write/probe fail | Fail temp write, hardening, rename, or new-key probe | Old file restored/unchanged; pending aborted or expires; old key remains valid. | +| uncertain commit | Drop commit response after server may commit | Probe old+new; choose sole accepted key; no blind replay. | +| pending expiry | Fake clock past 10 minutes | Pending rejected/removed; old accepted. | +| delete key | Delete configured key with bound sessions/grants | Admission, sessions, and grants revoked after persistence only. | +| self logout | GUI session + Origin + CSRF | Current session removed; replay 401. Admin-token call 403. | +| pairing bad guesses | Same grant/source repeated with fake clock | Fifth grant failure burns; source threshold yields 429 + bounded Retry-After; no identity leak. | +| pairing replay/race | Two concurrent valid redemptions | Exactly one session; other generic failure. | +| pairing capacity | Fill 128 grants / 1,024 buckets | Refusal/prune behavior bounded; no eviction of a newer live grant to admit attacker input. | +| newer compatible hub | p2/min1 + p1 client | Feature intersection only; no unsupported path. | +| incompatible floor | client Date: Fri, 28 Aug 2026 01:15:43 +0900 Subject: [PATCH 020/172] =?UTF-8?q?docs(devlog):=20fold=20roadmap=20audit?= =?UTF-8?q?=20r1=20=E2=80=94=2010=20blockers=20closed=20across=20010-080?= =?UTF-8?q?=20(synthesis=20in=20002)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../002_audit_r1_synthesis.md | 16 +++ devlog/_plan/260827_remote_hub/010_design.md | 6 +- .../030_phase1_protocol_catalog.md | 28 +++-- .../040_phase2_remote_session.md | 14 ++- .../260827_remote_hub/050_phase3_connect.md | 22 ++-- .../260827_remote_hub/060_phase4_two_plane.md | 50 +++++---- .../260827_remote_hub/070_phase5_deploy.md | 8 +- .../260827_remote_hub/080_phase6_hardening.md | 106 ++++++++++-------- 8 files changed, 154 insertions(+), 96 deletions(-) create mode 100644 devlog/_plan/260827_remote_hub/002_audit_r1_synthesis.md diff --git a/devlog/_plan/260827_remote_hub/002_audit_r1_synthesis.md b/devlog/_plan/260827_remote_hub/002_audit_r1_synthesis.md new file mode 100644 index 0000000000..ca2dcd3c0c --- /dev/null +++ b/devlog/_plan/260827_remote_hub/002_audit_r1_synthesis.md @@ -0,0 +1,16 @@ +# 002 — Audit synthesis, roadmap round 1 (FAIL, 10 blockers) — canonical decisions + +Reviewer: Volta (same reviewer retained for re-audit). Per-blocker disposition: + +1 [fold] Fixture repair: hub-too-new = hub{p:2,min:2} vs client p1; hub-too-old = client p2 requiring min2 vs hub{p:1,min:1}. Zero/malformed rows move to the malformed-input test class (400), not the mismatch class. +2 [fold] Chain completion: 030's readyz metadata builder signature becomes build(config, req) from Phase 1; Phase 2's file map adds src/remote/protocol.ts + the /readyz handler as consumers of hub.managementPublicOrigin (config wins over observed origin when set). +3 [fold] Pairing end-to-end: the relay (060) and the mgmt ingress (070) BOTH allow POST /opencodex-session (exchange) in addition to GET bootstrap; ocx gui pair prints a code bound to a caller-supplied browser origin (default http://localhost:10100); dogfood config (070) adds corsAllowOrigins:["http://localhost:10100"]. +4 [fold] Plane mapping is per-CALL, not per-page: Startup/Integrations keep their existing /api/* calls on the shared plane; only new machine sections call /api/machine/*. 060 file map adds gui/src/pages/Startup.tsx, Integrations.tsx, ApiKeys.tsx, Grok.tsx (call-site routing), and drops the page-level table. +5 [fold] Canonical names, propagated everywhere: routes = exactly 060's /api/machine/{status,clients,sync,shim,disconnect,hub-relay} with GET/POST /api/machine/shim (no PUT clients/:id — 010 updated); connect flags = --pairing-code-stdin | --admin-token-stdin (050 drops --credential-*; 010 drops --token-env/--token-stdin). +6 [fold] Phase 6 owners renamed to the real creators: src/client/hub-client.ts, src/client/hub-relay.ts, tests/client-connect.test.ts; tests/remote-catalog.test.ts either created BY Phase 6 (listed as Add) or folded into client-connect tests — 080 names it as Add. +7 [fold] /v1/catalog gains authenticated-only response header x-opencodex-key-id echoing the admitted key's id (030 IN-scope; never on unauthenticated paths); 080's rotation probe consumes it. +8 [fold] Remove impossible self-invalidation: pairing grants are NOT key-bound; disconnect revokes nothing on the hub by itself — key deletion is an operator action (hub GUI / ocx connect revoke WITH admin credential). 080 reworded; 040 grant contract loses boundKeyId. +9 [fold] 050: transient admin credential is retained in memory until the connect transaction commits or rolls back, then zeroized. +10 [fold] 080 rotation names src/cli/connect.ts (parser) + tests; src/client/state.ts pendingOperation {kind:"rotate", newKeyIssuedAt, oldKeyBackupPath} full chain; rotation writes old key to .prev (0600) until verified commit, then deletes — crash recovery documented. + +No rebuttals; all 10 folded. diff --git a/devlog/_plan/260827_remote_hub/010_design.md b/devlog/_plan/260827_remote_hub/010_design.md index 85639ec0b2..33332c77fd 100644 --- a/devlog/_plan/260827_remote_hub/010_design.md +++ b/devlog/_plan/260827_remote_hub/010_design.md @@ -146,7 +146,7 @@ data plane still needs the token, management still needs admin-token/session. ## 4. ocx connect (client mode) ```text -ocx connect [--management-url ] [--token-env NAME | --token-stdin] +ocx connect [--management-url ] [--pairing-code-stdin | --admin-token-stdin] [--clients codex,claude] [--management-transport direct|relay] [--no-sync] ocx disconnect [--keep-catalog] ocx connect status [--json] ``` @@ -175,8 +175,8 @@ opt-in with ownership records. Explicit allowlist, default-404 (same failure mode as loopbackRouteAllowed): /healthz · /readyz · GET /api/machine/status · GET /api/machine/clients · -POST /api/machine/sync · PUT /api/machine/clients/:id · POST /api/machine/shim/* · -POST /api/machine/disconnect · POST /api/machine/hub-relay/* (opt-in only). +POST /api/machine/sync · GET/POST /api/machine/shim · POST /api/machine/disconnect · +POST /api/machine/hub-relay (opt-in only). Mutations need local gui-session + CSRF (auto-minted on loopback, today's flow). Relay constraints (it is NOT a proxy): fixed destination = client.managementUrl; diff --git a/devlog/_plan/260827_remote_hub/030_phase1_protocol_catalog.md b/devlog/_plan/260827_remote_hub/030_phase1_protocol_catalog.md index 1765944173..90e8ed526d 100644 --- a/devlog/_plan/260827_remote_hub/030_phase1_protocol_catalog.md +++ b/devlog/_plan/260827_remote_hub/030_phase1_protocol_catalog.md @@ -33,6 +33,8 @@ runtime behavior. All code paths remain standalone-compatible when `runtimeRole` `x-opencodex-api-key: ` or `Authorization: Bearer `. `x-api-key`, a foreign bearer, the admin token, and no credential are rejected on a non-loopback bind. The row is added to `AUTH_MATRIX` with `xApiKey: "rejected"`. +- An admitted `/v1/catalog` response includes `x-opencodex-key-id` with the admitted + key's id. Rejected and unauthenticated responses never include this header. - A serialized catalog larger than `MAX_REMOTE_CATALOG_BYTES = 32 * 1024 * 1024` is not returned over `/v1/catalog`; it fails with HTTP 503 and the stable code `catalog_too_large`. The management route continues to expose the same serialized bytes @@ -46,7 +48,8 @@ runtime behavior. All code paths remain standalone-compatible when `runtimeRole` - Protocol-v1 metadata in every ready/pending/failed `/readyz` body. - A parser/compatibility predicate for future `ocx connect`, including additive-field tolerance for a dev hub paired with the latest released client. -- Shared catalog serialization, ETag, `If-None-Match`, size cap, and data-plane admission. +- Shared catalog serialization, ETag, `If-None-Match`, size cap, data-plane admission, and + authenticated `x-opencodex-key-id` attribution. - Route placement before the unknown-`/v1/*` JSON-404 guard. - Focused and full remote-only verification commands. @@ -126,8 +129,9 @@ after the 32 MiB bound passes. `/v1/catalog` uses `resolveResponsesApiAuth(req, policy)`, not `resolveApiAuth`, because the former is the existing dedicated-header/our-secret-Bearer matrix and rejects `x-api-key` (`src/server/auth-cors.ts:465-478`). It performs the existing data-plane origin check after -admission. No Direct passthrough exists on this read-only route and no credential is -forwarded. +admission, then sets `x-opencodex-key-id` from that admitted key identity. Rejected and +unauthenticated paths never emit the header. No Direct passthrough exists on this read-only +route and no credential is forwarded. ## 4. Diff-level file-change map @@ -137,18 +141,18 @@ All paths below exist in the current tree except the two files marked **NEW**. |---|---|---| | MODIFY | `src/types/config.ts` | Export `OcxRuntimeRole`; add optional `runtimeRole` to `OcxConfig` beside bind/runtime settings. | | MODIFY | `src/config.ts` | Add role schema and `runtimeRole` field validation; export `runtimeRole(config)`; reject invalid live candidates while preserving absence as standalone. Add degraded persisted-value diagnostics without deleting providers or `apiKeys`. | -| NEW | `src/remote/protocol.ts` | Own protocol constants, readiness metadata type/parser, management-origin validation, compatibility result, and exact mismatch strings. This is a passive leaf and imports no router, lifecycle, Responses, provider, or Lab code. | +| NEW | `src/remote/protocol.ts` | Own protocol constants, readiness metadata type/parser, `readyProtocolMetadata(config, req)`, management-origin validation, compatibility result, and exact mismatch strings. Phase 1 observes the request origin; accepting config from the start lets Phase 2 prefer `hub.managementPublicOrigin` without changing the consumer signature. This is a passive leaf and imports no router, lifecycle, Responses, provider, or Lab code. | | NEW | `src/server/catalog-download.ts` | Own `MAX_REMOTE_CATALOG_BYTES`, one persisted-catalog serialization result, byte-derived ETag matching, `/api/catalog` response construction, and bounded `/v1/catalog` response construction. | | MODIFY | `src/server/management/model-routes.ts` | Replace the inline `/api/catalog` read/`JSON.stringify` block at lines 334–345 with the shared response builder; preserve 404 and `x-opencodex-codex-version`. | | MODIFY | `src/server/auth-cors.ts` | Add the `/v1/catalog` `AUTH_MATRIX` row with bearer/dedicated accepted and `xApiKey` rejected. Do not change any existing row or credential precedence. | -| MODIFY | `src/server/index.ts` | Add protocol metadata to the current `/readyz` body at lines 991–998. Mount exact `GET /v1/catalog` after readiness/management handling and before the unknown-`/v1/*` guard at line 1604; use `resolveResponsesApiAuth` plus the existing origin policy. Keep `startServer` synchronous and add no `await` between `Bun.serve` and `labActivationRequired`. | +| MODIFY | `src/server/index.ts` | Add protocol metadata to the current `/readyz` body at lines 991–998 by passing `(config, req)` to the builder. Mount exact `GET /v1/catalog` after readiness/management handling and before the unknown-`/v1/*` guard at line 1604; use `resolveResponsesApiAuth` plus the existing origin policy and emit `x-opencodex-key-id` only from the admitted key identity. Keep `startServer` synchronous and add no `await` between `Bun.serve` and `labActivationRequired`. | | MODIFY | `src/server/proxy-liveness.ts` | Keep existing identity/status parsing additive; extend the internal body type/comments so protocol fields are recognized but do not make ordinary `ocx ready` reject a v0/legacy standalone server. Remote compatibility remains in `src/remote/protocol.ts`. | | MODIFY | `tests/config.test.ts` | Extend the existing config-default/validation sibling tests with absent/default, three valid roles, malformed live candidate, and malformed persisted role preservation cases. | | MODIFY | `tests/server-live.test.ts` | Extend the existing `GET /readyz` suite (lines 1220+) for exact protocol values on ready/pending/failed/draining, sanitized keys, management origin, method/path negatives, and no-auth behavior. | | MODIFY | `tests/proxy-liveness.test.ts` | Extend the current strict readiness parser/probe tests to prove additive protocol fields neither invalidate readiness nor bypass identity/status checks. | | MODIFY | `tests/api-catalog-route.test.ts` | Extend the existing `/api/catalog` sibling suite with fixed fixture bytes and version-header preservation after serializer extraction. | -| MODIFY | `tests/server-auth.test.ts` | Extend live-server auth/order coverage with `/v1/catalog` header matrix, exact-path/method negatives, foreign/admin bearer rejection, bound overflow, ETag/304, and unknown-`/v1` guard preservation. | -| MODIFY | `tests/api-key-attribution.test.ts` | Extend the existing live `AUTH_MATRIX` loop so `/v1/catalog` is a GET route and each cell reaches the real handler rather than the generic 404. | +| MODIFY | `tests/server-auth.test.ts` | Extend live-server auth/order coverage with `/v1/catalog` admission matrix, exact admitted `x-opencodex-key-id`, header absence on every rejected/unauthenticated path, exact-path/method negatives, foreign/admin bearer rejection, bound overflow, ETag/304, and unknown-`/v1` guard preservation. | +| MODIFY | `tests/api-key-attribution.test.ts` | Extend the existing live `AUTH_MATRIX` loop so `/v1/catalog` is a GET route, each cell reaches the real handler rather than the generic 404, and successful dedicated/Bearer admission echoes that key's id. | No other production or test file is in scope. If implementation proves another path is required, stop the phase and amend this document before editing it. @@ -180,7 +184,7 @@ export type RemoteProtocolCompatibility = | { ok: true; metadata: RemoteReadyMetadata } | { ok: false; reason: "invalid" | "hub-too-new" | "hub-too-old"; message: string }; -export function readyProtocolMetadata(req: Request): RemoteReadyMetadata; +export function readyProtocolMetadata(config: OcxConfig, req: Request): RemoteReadyMetadata; export function parseRemoteReadyMetadata(value: unknown): RemoteReadyMetadata | null; export function checkRemoteProtocolCompatibility( value: unknown, @@ -223,11 +227,11 @@ an empty catalog. No function accepts a caller-provided catalog path. | P1-A04 | Start a server with a pending gate and request exact unauthenticated `GET /readyz`; repeat after ready, failed, and drain activation. | Existing HTTP/status/Retry-After contract holds and all three protocol fields remain identical across states. | | P1-A05 | Send POST, OPTIONS, `/readyz/`, and encoded `/readyz%2F`. | Existing deterministic JSON 404 path remains; no protocol document leaks through the GUI fallback. | | P1-A06 | Feed a v1 document plus unknown future fields to the new parser and to `validateReadyzBody`. | Both accept the document; readiness identity remains strict and remote parser preserves only validated protocol fields. | -| P1-A07 | Feed `{protocol: 1, minimumClientProtocol: 2}` to a v1 client. | `hub-too-new` and the exact “Upgrade ocx on this client” string are returned before catalog access. | -| P1-A08 | Feed `{protocol: 0, minimumClientProtocol: 0}` to a client requiring hub protocol 1. | `hub-too-old` and the exact “Upgrade ocx on the hub” string are returned. | -| P1-A09 | Omit, mistype, overflow, or give a path-bearing `managementUrl`. | `invalid` and the exact malformed-metadata string are returned; no fallback to protocol 1. | +| P1-A07 | Feed `{protocol: 2, minimumClientProtocol: 2}` to a protocol-v1 client. | `hub-too-new` and the exact “Upgrade ocx on this client” string are returned before catalog access. | +| P1-A08 | Feed `{protocol: 1, minimumClientProtocol: 1}` to a protocol-v2 client requiring minimum hub protocol 2. | `hub-too-old` and the exact “Upgrade ocx on the hub” string are returned. | +| P1-A09 | Supply zero, omit, mistype, overflow, set minimum above protocol, or give a path-bearing `managementUrl`. | The malformed-input class (`400`) returns `invalid` and the exact malformed-metadata string; it is never classified as a version mismatch and never falls back to protocol 1. | | P1-A10 | Persist a fixed catalog fixture; call authorized `/api/catalog` and authorized `/v1/catalog`. | Status 200 and response bytes are byte-identical; the ETag independently hashes those bytes. | -| P1-A11 | Repeat `/v1/catalog` on non-loopback with dedicated header, our-secret Bearer, `x-api-key`, foreign Bearer, admin token, and no token. | First two reach 200; all remaining cases are 401. No case reaches the generic unknown-route 404. | +| P1-A11 | Repeat `/v1/catalog` on non-loopback with dedicated header, our-secret Bearer, `x-api-key`, foreign Bearer, admin token, and no token. | First two reach 200 with `x-opencodex-key-id` equal to the admitted key id; all remaining cases are 401 without that header. No case reaches the generic unknown-route 404. | | P1-A12 | Call `/v1/catalog` with matching tag, weak matching tag, tag list, `*`, stale tag, and malformed tag. | Matches return 304/no body/same ETag; stale or malformed values return 200/full bytes. | | P1-A13 | Serialize exactly the cap and cap+1 fixtures through an injected serialization seam. | Exact cap returns 200; cap+1 returns 503 `catalog_too_large`, never a partial body. | | P1-A14 | Call POST `/v1/catalog`, GET `/v1/catalog/`, and an unrelated `/v1/does-not-exist`. | Every request returns the existing JSON 404 envelope; route ordering does not widen path/method matching. | diff --git a/devlog/_plan/260827_remote_hub/040_phase2_remote_session.md b/devlog/_plan/260827_remote_hub/040_phase2_remote_session.md index 9b0534e1b5..8d7408fa97 100644 --- a/devlog/_plan/260827_remote_hub/040_phase2_remote_session.md +++ b/devlog/_plan/260827_remote_hub/040_phase2_remote_session.md @@ -50,7 +50,8 @@ consumption of that credential is the only pairing exchange. `trustedTailscaleIngress: true` context. Direct/public-listener headers are ignored. - An empty/missing `allowedTailscaleUsers` list authorizes nobody remotely. - Pairing grants are stored only as SHA-256 digests, capped, expire after five minutes, - are deleted before session minting, and are never logged or returned again. + are deleted before session minting, and are never logged or returned again. They are not + bound to or invalidated with any data key. - Pairing-grant creation accepts only a short-lived capability bound to the exact runtime PID, port, method, path, nonce, expiry, and canonical browser origin. The reusable admin token and every other management principal are rejected on that route. @@ -184,6 +185,10 @@ and claimed GUI origin; it never authorizes a mutation. non-loopback hub request it prefers configured `hub.managementPublicOrigin`; otherwise it keeps today's observed-origin behavior. It never reads forwarding headers. +The Phase-1 `readyProtocolMetadata(config, req)` consumer follows the same rule: configured +`hub.managementPublicOrigin` wins for hub readiness metadata, with observed request origin +used only when the setting is absent. + ### 5.3 Bootstrap meta consumer chain The compatibility meta name `opencodex-session-origin` remains and now explicitly means @@ -235,6 +240,8 @@ memory-only and are never written to web storage. credential accepted. Admin/data/session credentials in headers do not substitute. - HTTPS issues `pairing`; non-loopback HTTP issues `insecure-http-pairing` only when the config opt-in is true. The grant is consumed before minting; all replays fail. + - Phase 4's fixed-target relay path allowlist admits this exact POST exchange in addition + to GET bootstrap; no other non-`/api/*` method/path is widened. `ocx gui pair [--origin ] [--json]` defaults `--origin` to `hub.managementPublicOrigin`; it fails if neither exists. It resolves the identity-checked @@ -268,11 +275,12 @@ All paths below exist in the current tree except files marked **NEW**. |---|---|---| | MODIFY | `src/types/config.ts` | Add `OcxHubConfig`, `OcxRemoteGuiConfig`, and optional `hub`/`remoteGui` fields. Extend the Phase-1 role type only by reference, not by new values. | | MODIFY | `src/config.ts` | Add strict nested schemas, canonical-origin/user-list validation, cross-field diagnostics, and persisted malformed-block degradation that preserves unrelated config. | +| MODIFY | `src/remote/protocol.ts` | Consume `hub.managementPublicOrigin` in `readyProtocolMetadata(config, req)` so configured origin wins and observed request origin is the fallback. Preserve the Phase-1 wire shape and parser. | | NEW | `src/lib/gui-pair-capability.ts` | Own v1 method/path/header constants and HMAC create/verify functions bound to nonce, expiry, canonical browser origin, PID, and port. It accepts only the existing local-attestation secret shape. | | NEW | `src/server/gui-session.ts` | Own session/grant records, constants, bounded maps, digest-only grant storage, issuance policy, grant consumption, shared request admission predicate, and sliding renewal. No provider/router/Lab imports. | | MODIFY | `src/server/management-auth.ts` | Replace private `origin` records and duplicate authorization/principal checks with the shared GUI-session module. Preserve exported `issueGuiSession` as the loopback-compatible facade. Add pairing-grant state and exact `gui-pair-capability` principal/replay handling without changing admin-token initialization. | | MODIFY | `src/server/auth-cors.ts` | Prefer configured hub public origin only for non-loopback management requests; add exact fixed management preflight headers and exact-origin ACAO. Do not change data-plane CORS or credential admission. | -| MODIFY | `src/server/index.ts` | Advertise GUI-pair capability v1 in `/healthz`; mount exact pairing-grant creation after capability admission, mount GET/POST bootstrap before GUI fallback, pass `trustedTailscaleIngress: false` on the public/ordinary loopback listeners, and preserve the line-1604 unknown-`/v1` guard. Do not make `startServer` async or add an await in its synchronous activation window. | +| MODIFY | `src/server/index.ts` | Pass `(config, req)` to the `/readyz` protocol metadata builder so `hub.managementPublicOrigin` reaches the response; advertise GUI-pair capability v1 in `/healthz`; mount exact pairing-grant creation after capability admission, mount GET/POST bootstrap before GUI fallback, pass `trustedTailscaleIngress: false` on the public/ordinary loopback listeners, and preserve the line-1604 unknown-`/v1` guard. Do not make `startServer` async or add an await in its synchronous activation window. | | MODIFY | `src/server/proxy-liveness.ts` | Add optional `guiPairCapability` to the existing health identity projection so the local client can fail closed against an old/foreign listener without changing required liveness identity fields. | | MODIFY | `src/server/gui-static.ts` | Serialize escaped browser/server origin meta tags; keep `opencodex-session-origin` as browser-origin compatibility metadata. | | NEW | `src/cli/gui.ts` | Own `runGuiCommand(args, deps)`: existing no-subcommand open behavior plus `pair`; strict `--origin`/`--json` parsing and one-time grant output. | @@ -285,7 +293,7 @@ All paths below exist in the current tree except files marked **NEW**. | MODIFY | `tests/server-management-auth.test.ts` | Extend the primary auth suite for every issuance/expiry/replay/origin/CSRF/admin negative; preserve the line-897 forged-Host test unchanged in meaning. | | MODIFY | `tests/native-profile-route-security.test.ts` | Update session fixture fields and prove native consent mutations still reject admin, wrong browser origin, wrong server destination, absent CSRF, and accept only the full remote-session predicate. | | MODIFY | `tests/server-auth.test.ts` | Extend management preflight tests for exactly the two added headers, allowed/rejected origins, and no data-plane header-policy drift. | -| MODIFY | `tests/server-live.test.ts` | Extend the existing `/healthz` capability metadata coverage for GUI-pair v1 while keeping readiness/session secrets absent. | +| MODIFY | `tests/server-live.test.ts` | Extend `/readyz` coverage so configured `hub.managementPublicOrigin` wins over the observed origin and absence falls back to the observed origin; extend `/healthz` capability metadata coverage for GUI-pair v1 while keeping readiness/session secrets absent. | | MODIFY | `tests/proxy-liveness.test.ts` | Extend health identity fixtures for optional GUI-pair capability detection and prove a foreign/malformed body cannot become an attested target. | | NEW | `tests/gui-pair-capability.test.ts` | Characterize payload binding, wrong method/path/origin/PID/port, malformed nonce/expiry, constant-time mismatch, and expiration for the operation capability, following `tests/local-management-capability.test.ts` and `tests/system-restart-contract-security.test.ts`. | | NEW | `tests/gui-pair-client.test.ts` | Characterize attestation, PID/port recheck, capability-version refusal, bodyless POST headers, one-attempt behavior, and redacted transport failures, following `tests/system-restart-client.test.ts` and `tests/local-provider-reload-client.test.ts`. | diff --git a/devlog/_plan/260827_remote_hub/050_phase3_connect.md b/devlog/_plan/260827_remote_hub/050_phase3_connect.md index fbd1c20dec..51c7a5d31c 100644 --- a/devlog/_plan/260827_remote_hub/050_phase3_connect.md +++ b/devlog/_plan/260827_remote_hub/050_phase3_connect.md @@ -97,7 +97,7 @@ Every existing path below was verified in the current tree. For NEW client paths | NEW | `src/client/state.ts` | Parse/validate `runtimeRole + config.json.client`, expose fail-closed connected/absent/invalid/mismatched states, and atomically commit/clear both keys through config mutation. | | NEW | `src/client/hub-client.ts` | Validate/normalize URLs; bounded `GET /readyz`, `POST /api/keys`, `GET /v1/catalog`; protocol/capability checks; redact all credential-bearing errors. | | NEW | `src/client/connect.ts` | Transaction coordinator, connected sync, rollback, and offline disconnect. No argv or presentation logic. | -| NEW | `src/cli/connect.ts` | Parse connect/disconnect/status arguments, read the one-time credential from stdin or a named env var, call the coordinator, and render redacted human/JSON output. | +| NEW | `src/cli/connect.ts` | Parse connect/disconnect/status arguments, read exactly one `--pairing-code-stdin` or `--admin-token-stdin` credential, call the coordinator, and render redacted human/JSON output. | | MODIFY | `src/types/config.ts` | Add `OcxClientConnectionConfig` and top-level `OcxConfig.client?`; the secret itself is not a field. | | MODIFY | `src/config.ts` | Add schema and field-scoped persistence behavior for `client`; malformed-present client state must be diagnosable and must never degrade into standalone routing. | | MODIFY | `src/lib/service-secrets.ts` | Add atomic owner-only write and fingerprint-checked removal beside existing path/read helpers. | @@ -365,7 +365,7 @@ export function disconnectClient( ```text ocx connect [--management-url ] - [--credential-stdin | --credential-env ] + [--pairing-code-stdin | --admin-token-stdin] [--clients codex,claude] [--management-transport direct|relay] [--allow-insecure-http] [--no-sync] @@ -373,11 +373,13 @@ ocx connect status [--json] ocx disconnect [--keep-catalog] [--json] ``` -There is deliberately no `--token `, `--admin-token `, or pairing-code -positional form. `--credential-env` stores only the variable name in argv; the value is -read once and cleared from the coordinator's local reference after key issuance. -`--credential-stdin` uses the bounded stdin helper. Parse errors redact unknown bare -values and all credential-shaped option values. +There is deliberately no `--token `, `--admin-token `, pairing-code +positional form, or credential environment-variable form. Exactly one of +`--pairing-code-stdin` and `--admin-token-stdin` uses the bounded stdin helper. Parse errors +redact unknown bare values and all credential-shaped option values. The transient admin +credential remains in memory until the connect transaction commits or rollback finishes, +then its buffer and coordinator reference are zeroized; successful key issuance alone is +not a terminal outcome. ## 4. Connect transaction and rollback @@ -410,7 +412,9 @@ config fields absent. The still-in-memory admin credential or exchanged GUI sess attempts exact `DELETE /api/keys` for the just-created id. If hub cleanup is unreachable, the failure reports only the safe key id and exact revoke action; it never prints the key. Machine-local rollback success is mandatory and remote cleanup inability is -explicit, never hidden as full rollback. +explicit, never hidden as full rollback. Only after that cleanup attempt completes does +the coordinator zeroize the transient admin credential; the success path zeroizes it +immediately after the final state commit. `--no-sync` still performs readiness, key issuance, token placement, catalog download, and final state commit, but does not mutate Codex/Claude client files. The next @@ -475,7 +479,7 @@ No test sends live hub traffic or reads the developer's homes. | Test file | Required cases | |---|---| -| `tests/client-connect.test.ts` (NEW) | URL canonicalization; Phase-1 ready parser/mismatch strings; ready/pending/failed; same-major v1 acceptance; management URL advertisement; admin HTTPS direct key POST; pairing HTTPS session exchange then key POST; dual-opt-in pairing HTTP; admin HTTP refusal; raw grant rejection at `/api/keys`; bounded catalog; atomic role+state commit; each rollback point; no-sync; connected 200/304/401/timeout sync; no local discovery fake called; offline disconnect and partial restore. | +| `tests/client-connect.test.ts` (NEW) | URL canonicalization; exact stdin-flag exclusivity and literal/env credential rejection; Phase-1 ready parser/mismatch strings; ready/pending/failed; same-major v1 acceptance; management URL advertisement; admin HTTPS direct key POST; pairing HTTPS session exchange then key POST; dual-opt-in pairing HTTP; admin HTTP refusal; raw grant rejection at `/api/keys`; admin credential retained through commit/rollback then zeroized; bounded catalog; atomic role+state commit; each rollback point; no-sync; connected 200/304/401/timeout sync; no local discovery fake called; offline disconnect and partial restore. | | `tests/service-secrets.test.ts` (NEW) | Exact path, 0600, Windows ACL seam, atomic replacement, symlink refusal, fingerprint, changed-file non-removal, no token in errors. | | `tests/codex-inject.test.ts` | Current standalone goldens byte-equal; explicit HTTPS target emits exact `base_url`, provider table, `env_key`; loopback-looking connected URL still requires admission; malformed target refused before journal. | | `tests/codex-inject-integration.test.ts` | Validate-only has zero writes; target commit records journal ownership; offline restore returns exact preimage; partial write rollback. | diff --git a/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md b/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md index 659e24e756..62f667bbab 100644 --- a/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md +++ b/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md @@ -66,7 +66,7 @@ activation remains unchanged and contains no new `await`. `POST /api/machine/disconnect`. - Opt-in fixed-target `/api/machine/hub-relay/*` selected only by `client.managementTransport === "relay"`. -- GUI machine/shared target discovery, independent auth state, page-plane mapping, +- GUI machine/shared target discovery, independent auth state, per-call plane routing, stable hub-offline states, and mode-aware stop/restart actions. - Connected usage = hub store filtered to this machine's `apiKeyId` by default, with an explicit hub-wide toggle; disconnected usage = local `usage.jsonl` unchanged. @@ -102,10 +102,14 @@ parent exists. No generated `gui/dist` file is edited. | MODIFY | `tests/cli-start-journal-order.test.ts` | Prove connected start skips stale-process journal restore only for a matching durable client owner and starts no full data plane. | | NEW | `tests/client-machine-listener.test.ts` | Listener bind/allowlist/auth/API/startup/offline matrix. | | NEW | `tests/client-hub-relay.test.ts` | Fixed target, header separation, body caps, redirects, errors, and SSRF negatives. | -| NEW | `gui/src/api-targets.ts` | Canonical `ApiTargets`, machine-status discovery, page-plane map, relay URL construction, and disconnected fallback. | +| NEW | `gui/src/api-targets.ts` | Canonical `ApiTargets`, machine-status discovery, per-plane call-base selection, relay URL construction, and disconnected fallback. | | MODIFY | `gui/src/api.ts` | Replace `needsApiAuth`'s one same-origin slot with exact target classification and per-target in-memory session/CSRF state; attach both auth domains only on relay. | -| MODIFY | `gui/src/App.tsx` | Discover targets before page fetches; map pages/actions to planes; machine health remains live when hub is down; connected stop becomes disconnect/recycle. | +| MODIFY | `gui/src/App.tsx` | Discover targets before page fetches; supply both call bases instead of one page base; machine health remains live when hub is down; connected stop becomes disconnect/recycle. | | MODIFY | `gui/src/stop-proxy.ts` | Add mode-aware machine disconnect request while preserving existing standalone `/api/stop` behavior. | +| MODIFY | `gui/src/pages/Startup.tsx` | Keep existing settings/startup-health/windows-tray/startup-action calls on the shared base; use the machine base only for new `/api/machine/*` status/shim sections. | +| MODIFY | `gui/src/pages/Integrations.tsx` | Pass the shared base to all existing integration descendants, including ApiKeys and Grok; pass the machine base only to new local-client controls. | +| MODIFY | `gui/src/pages/ApiKeys.tsx` | Keep existing `/api/keys`, `/v1/models`, and model-test calls on the shared base while mounted under Integrations. | +| MODIFY | `gui/src/pages/Grok.tsx` | Keep existing `/api/grok*` calls on the shared base while mounted under Integrations. | | MODIFY | `gui/src/pages/Usage.tsx` | Add this-machine/hub-wide scope control, key-id query/cache key, source label, and hub-offline behavior without local fallback. | | MODIFY | `gui/src/pages/Storage.tsx` | Pass its selected shared `apiBase` through to `StorageWorkspace`. | | MODIFY | `gui/src/components/storage-workspace/StorageWorkspace.tsx` | Remove module-global `VITE_API_BASE`; use the supplied shared-plane base for Codex-log storage calls. | @@ -119,12 +123,12 @@ parent exists. No generated `gui/dist` file is edited. | MODIFY | `gui/src/i18n/tr.ts` | Add the same keys. | | MODIFY | `gui/src/i18n/zh.ts` | Add the same keys. | | MODIFY | `gui/src/i18n/zh-TW.ts` | Add the same keys. | -| NEW | `gui/tests/api-targets.test.ts` | Target discovery, page mapping, relay construction, and hub-down fallback. | +| NEW | `gui/tests/api-targets.test.ts` | Target discovery, per-plane call-base selection, relay construction, and hub-down fallback. | | MODIFY | `gui/tests/api-auth-memory.test.ts` | Independent machine/shared sessions; direct/relay header matrix; no cross-target leakage; bootstrap validation. | | MODIFY | `gui/tests/api-auth-deadline.test.ts` | Per-target shared resolution/watchdog behavior. | | MODIFY | `gui/tests/usage-layout.test.ts` | Connected own-key default, hub-wide toggle, disconnected local source, cache partition, and offline rendering. | | MODIFY | `gui/tests/app-stop.test.ts` | Standalone stop vs connected disconnect/recycle. | -| MODIFY | `gui/tests/integrations-routing.test.ts` | Integrations remains machine-plane while shared pages use hub. | +| MODIFY | `gui/tests/integrations-routing.test.ts` | Existing Startup/Integrations/ApiKeys/Grok calls stay shared; only new local-client `/api/machine/*` calls use the machine base. | | MODIFY | `tests/core-lab-boundary.test.ts` | Existing protected-root and synchronous-start checks remain green; no rule weakening. | Verified reuse without edits: `src/server/gui-static.ts` serves assets/bootstrap; @@ -287,8 +291,8 @@ Activation requires all of: 2. `managementTransport === "relay"`; 3. exact `/api/machine/hub-relay/` prefix; 4. valid local machine session (custom headers for relay); -5. suffix exactly `/opencodex-session` GET or inside `/api/` with an allowed HTTP - method. +5. suffix exactly `/opencodex-session` with GET bootstrap or POST pairing exchange, or + inside `/api/` with an allowed HTTP method. The destination is `new URL(suffix, state.managementUrl)` after rejecting encoded slashes/backslashes, authority syntax, userinfo, query-host tricks, and path traversal. @@ -329,17 +333,19 @@ export interface ApiTargets { export function standaloneApiTargets(initialBase: string): ApiTargets; export function targetsFromMachineStatus(initialBase: string, status: MachineStatusV1): ApiTargets; -export function apiPlaneForPage(page: Page): ApiPlane; -export function apiBaseForPage(page: Page, targets: ApiTargets): string; +export function apiBaseForPlane(plane: ApiPlane, targets: ApiTargets): string; export async function discoverApiTargets(initialBase: string, signal?: AbortSignal): Promise; ``` -Page mapping is explicit: +Routing is selected at each call site, not once for a page: -| Plane | Pages/actions | -|---|---| -| Shared | Dashboard, Providers, Models, Subagents, Logs, Usage, Storage, Codex Set. | -| Machine | Startup, Integrations, health/version, Codex app-server restart, shim actions, disconnect. | +| Call sites | Plane | Rule | +|---|---|---| +| Existing Startup calls (`/api/settings`, `/api/startup-health`, `/api/windows-tray`, `/api/startup-action`) | Shared | Preserve hub-backed behavior. | +| Existing Integrations descendants, including ApiKeys (`/api/keys`, `/v1/models`, model tests) and Grok (`/api/grok*`) | Shared | Preserve provider/config/catalog ownership on the hub. | +| Dashboard, Providers, Models, Subagents, Logs, Usage, Storage, Codex Set existing calls | Shared | Continue to use the hub target. | +| New local status/client/sync/shim/disconnect calls | Machine | Only explicit `/api/machine/*` routes use the machine target. | +| Shell health/version and connected disconnect/recycle | Machine | Remain available independently of hub reachability. | In a standalone full server, `/api/machine/status` returns 404 and discovery returns one same-origin target, preserving existing behavior. A connected machine status response @@ -381,11 +387,13 @@ clears/prompts only that target and cannot wipe the other target's newer session ### `gui/src/App.tsx` -App blocks page resource mounting until target discovery settles, then passes the mapped -base. Health/version polls machine. Connected hub failure leaves shell, navigation, -Startup, Integrations, disconnect, and local status usable; shared pages render one -stable hub-offline state and never substitute machine data. In connected mode the power -action uses `POST /api/machine/disconnect`; in standalone it remains `POST /api/stop`. +App blocks page resource mounting until target discovery settles, then passes both bases +to mixed pages rather than assigning one plane to the whole page. Health/version polls +machine. Connected hub failure leaves shell, navigation, disconnect, and new local-machine +sections usable; existing shared sections inside Startup and Integrations render the same +stable hub-offline state as other shared calls and never substitute machine data. In +connected mode the power action uses `POST /api/machine/disconnect`; in standalone it +remains `POST /api/stop`. `StorageWorkspace` must receive the shared base from `Storage.tsx`; its current module-global `VITE_API_BASE` at `gui/src/components/storage-workspace/StorageWorkspace.tsx:20` @@ -445,12 +453,12 @@ appearing after disconnect. | `tests/api-usage.test.ts` | Exact `apiKeyId` response/echo; old and other-key rows excluded; no match; combined filters; filtered request cannot poison cache; unfiltered next request remains whole hub. | | `tests/usage-summary.test.ts` | Pure projection totals/days/models/providers/accounts consistency; exact-case id; combo attempts; absent id; provider/model/key cross-product. | | `tests/core-lab-boundary.test.ts` | Protected roots import no client subsystem; `startServer` remains non-async and no new top-level-window await. | -| `gui/tests/api-targets.test.ts` (NEW) | Standalone 404 fallback; valid direct/relay status; page map; exact bases; machine-status network failure not standalone; encoded relay paths. | +| `gui/tests/api-targets.test.ts` (NEW) | Standalone 404 fallback; valid direct/relay status; per-plane call bases; machine-status network failure not standalone; encoded relay paths. | | `gui/tests/api-auth-memory.test.ts` | Two simultaneous sessions; direct headers; relay dual headers; machine custom stripping contract; cross-target 401 races; server/browser-origin mismatch; unknown target receives nothing; no web storage. | | `gui/tests/api-auth-deadline.test.ts` | One target watchdog does not block/clear the other; direct and relay bootstrap timeout states. | | `gui/tests/usage-layout.test.ts` | Connected default key query; hub-wide omission; disconnected local query; source-qualified cache keys; hub-down no local fetch; scope labels/a11y. | | `gui/tests/app-stop.test.ts` | Standalone `/api/stop`; connected `/api/machine/disconnect`; refusal re-enables action; accepted recycle tolerates connection drop. | -| `gui/tests/integrations-routing.test.ts` | Startup/Integrations machine base; Providers/Usage/Storage shared base under direct and relay. | +| `gui/tests/integrations-routing.test.ts` | Existing Startup/Integrations/ApiKeys/Grok calls use the shared base; only new `/api/machine/*` local controls use the machine base under direct and relay. | ## 8. Acceptance criteria with activation grounding diff --git a/devlog/_plan/260827_remote_hub/070_phase5_deploy.md b/devlog/_plan/260827_remote_hub/070_phase5_deploy.md index 112ffac2bb..78f64c1059 100644 --- a/devlog/_plan/260827_remote_hub/070_phase5_deploy.md +++ b/devlog/_plan/260827_remote_hub/070_phase5_deploy.md @@ -157,7 +157,7 @@ The listener is GUI + management API only: - `GET`/`HEAD` packaged GUI assets and `/`. - `GET` extensionless SPA routes that the existing GUI fallback serves. -- `GET /opencodex-session`. +- `GET /opencodex-session` bootstrap and `POST /opencodex-session` pairing exchange. - `/api/*`, with existing management authentication, Origin, session, CSRF, body-size, and route authorization intact. - Everything else is deterministic JSON 404 before a handler runs, including all `/v1/*`, @@ -202,6 +202,7 @@ Canonical hub setup shown in the guide (values are examples, not defaults): ocx config set runtimeRole hub ocx config set hostname 100.64.0.10 ocx config set hub.managementPublicOrigin '"https://hub-name.tailnet-name.ts.net"' +ocx config set corsAllowOrigins '["http://localhost:10100"]' ocx config set hub.managementIngress '{"enabled":true,"port":10101}' ocx config set remoteGui.allowedTailscaleUsers '["operator@example.com"]' @@ -357,9 +358,8 @@ evidence; the sketch above is setup, not exact-head proof. 1. On the hub, run `ocx gui pair` and copy the single-use, short-TTL code through the interactive channel. Do not record it. -2. On the MacBook, run the Phase-3 connect command with the pairing code on stdin. The exact - Phase-3 signature must support transient `--pairing-code-stdin` (or its already-approved - equivalent) and must not accept a literal secret flag. +2. On the MacBook, run the Phase-3 connect command with exactly one transient + `--pairing-code-stdin` or `--admin-token-stdin`; it must not accept a literal secret flag. 3. Assert `ocx connect status --json` reports protocol v1, hub URL, management URL, management transport, and the non-secret client key id. 4. Assert `serviceApiTokenFilePath()` exists owner-only and contains the auto-issued per-client diff --git a/devlog/_plan/260827_remote_hub/080_phase6_hardening.md b/devlog/_plan/260827_remote_hub/080_phase6_hardening.md index 6e24eaa8b9..f0d112cfbf 100644 --- a/devlog/_plan/260827_remote_hub/080_phase6_hardening.md +++ b/devlog/_plan/260827_remote_hub/080_phase6_hardening.md @@ -16,9 +16,9 @@ the workstation. Full-suite execution is serialized with other `lidge-ai` suite - Recoverable per-client data-key rotation with a one-time secret response, bounded overlap, client-side atomic token-file replacement, explicit commit/abort, and stable `apiKeyId` usage attribution. -- Remote-session self-logout, automatic session invalidation after key commit/delete, and - disconnect-time best-effort revocation without making hub availability a prerequisite for - offline local restore. +- Remote-session self-logout plus explicit operator data-key revocation from the hub GUI or + `ocx connect revoke` with an admin credential. Disconnect performs no hub-side revocation and + remains available while the hub is offline. - Pairing issuance/redemption limits, one-use semantics, bounded active state, and safe 429s. - Protocol negotiation matrix tests covering the v1 compatibility floor and feature detection. - Adversarial `/v1/catalog` consumer tests for decompressed size, malformed JSON, invalid schema, @@ -53,7 +53,7 @@ the workstation. Full-suite execution is serialized with other `lidge-ai` suite | Header-smuggling attempt | Hub request parser/proxy chain | Reject transfer-encoding, conflicting content-length, connection-nominated headers, CR/LF values, and upgrade paths. | | Protocol-skewed peer | Local client files / silent misroute | Negotiate before any local write; reject incompatible floors with explicit upgrade error; unknown features stay off. | | Rotation crash between hub and client | Client availability | Old and pending keys overlap for a bounded window; commit only after new-key probe; abort/expiry preserves old key. | -| Session surviving credential change | Revoked client access | Key commit/delete invalidates sessions and pairing grants bound to that `apiKeyId`; current in-flight data turn may finish, next admission fails. | +| Disconnected client whose data key still exists | Hub data admission | Disconnect changes local state only; an operator explicitly deletes the key in the hub GUI or runs `ocx connect revoke --admin-token-stdin`. | | Logs/evidence | Tokens, codes, identities | Record ids/prefixes/counts/status only; privacy scan; no raw secret, email, Origin query, request body, or account id. | Security level: ASVS L2 for the remote management/session surface. Applicable architecture, @@ -68,7 +68,7 @@ planning base and must exist before Phase 6 begins. If an earlier phase delibera different exact owner path, amend this file mechanically before implementation rather than adding a second owner. -### 2.1 Key rotation and session invalidation +### 2.1 Key rotation, self-logout, and operator revocation | Path | Change | Exact responsibility | | --- | --- | --- | @@ -76,13 +76,15 @@ a second owner. | `src/config.ts` | MODIFY | Validate/degrade pending rotation independently so one malformed pending record cannot reset providers or revoke the current key. | | `src/server/auth-cors.ts` | MODIFY | Admit an unexpired pending key under the same configured `apiKeyId`; never return or serialize its secret. | | `src/server/management/api-key-rotation.ts` | NEW | Single owner for start/commit/abort/expiry cleanup and constant-time rotation-id comparison. | -| `src/server/management/oauth-account-routes.ts` | MODIFY | Add the three rotation operations next to existing `/api/keys` CRUD and call session invalidation after commit/delete. Existing GET continues to mask all secrets. | +| `src/server/management/oauth-account-routes.ts` | MODIFY | Add the three rotation operations next to existing `/api/keys` CRUD. Existing GET continues to mask all secrets; key deletion remains an explicit operator action. | | `src/server/management/session-routes.ts` | NEW | `POST /api/session/logout` self-revocation route; requires the current `gui-session` and CSRF. Admin token receives 403, not a promoted session. | | `src/server/management-api.ts` | MODIFY | Wire `handleSessionRoutes` and the narrow session-control dependency into `ManagementContext`. | -| `src/server/management/context.ts` | MODIFY | Carry only the revocation interface, never the raw admin token or session map. | -| `src/server/management-auth.ts` | MODIFY | Associate remote sessions with optional `apiKeyId`; export narrow current/by-key invalidation helpers; preserve one shared auth predicate. | +| `src/server/management/context.ts` | MODIFY | Carry only the current-session logout interface, never the raw admin token or session map. | +| `src/server/management-auth.ts` | MODIFY | Export a narrow current-session invalidation helper for explicit self-logout; preserve one shared auth predicate and add no key binding to pairing grants or sessions. | | `src/client/connect.ts` | MODIFY | Implement `ocx connect rotate`: transient authority, start rotation, atomically replace token file, validate new key, commit, and restore+abort on failure. | -| `src/client/state.ts` | MODIFY | Persist non-secret key id and pending operation metadata only; never persist admin/pairing authority or old/new secret. | +| `src/client/hub-client.ts` | MODIFY (Phase-3 owner) | Add bounded rotation/revoke management calls, authenticated catalog key-id probe, and redacted errors beside the existing ready/key/catalog calls. | +| `src/client/state.ts` | MODIFY | Persist `pendingOperation: {kind:"rotate",newKeyIssuedAt,oldKeyBackupPath}` through the full backup/replace/probe/commit or recovery chain; never persist admin/pairing authority or old/new secret. | +| `src/cli/connect.ts` | MODIFY (Phase-3 owner) | Parse `rotate` and operator-only `revoke`, enforce exact stdin flags, reject literal/env secret forms, and render redacted recovery status. | | `src/cli/access.ts` | MODIFY | Add management-side `ocx access key rotate ` start/commit/abort UX with one-time secret warning; no literal secret flags. | | `src/cli/registry.ts` | MODIFY | Document rotation command shapes and transient-authority requirement. | | `gui/src/pages/ApiKeys.tsx` | MODIFY | Load pending status, start/commit/abort rotation, and render the new secret exactly once. | @@ -99,7 +101,8 @@ a second owner. | `tests/api-keys-routes.test.ts` | MODIFY | Rotation route contract, masking, pending overlap, commit, abort, expiry, malformed inputs, and delete invalidation. | | `tests/data-plane-admission-identity.test.ts` | MODIFY | Current and pending secret map to one id; expired/committed/aborted secrets do not admit. | | `tests/api-key-attribution.test.ts` | MODIFY | Traffic before/during/after rotation remains one `apiKeyId` bucket. | -| `tests/server-management-auth.test.ts` | MODIFY | Session self-logout/by-key invalidation and admin-token refusal. | +| `tests/server-management-auth.test.ts` | MODIFY | Explicit session self-logout, absence of key-bound grant/session invalidation, and admin-token consent refusal. | +| `tests/client-connect.test.ts` | MODIFY (Phase-3 owner) | Rotation/revoke parser flags, `.prev` crash recovery, pending-operation lifecycle, uncertain commit, and operator-only key deletion. | | `gui/tests/apikeys-actions.test.tsx` | MODIFY | Start/commit/abort wire actions and one-time secret handling. | | `gui/tests/apikeys-mutation-timeout.test.tsx` | MODIFY | Rotation controls recover after bounded network failure. | | `gui/tests/apikeys-workspace.test.tsx` | MODIFY | Accessible rendered states and confirmations. | @@ -109,14 +112,14 @@ a second owner. | Path | Change | Exact responsibility | | --- | --- | --- | -| `src/server/gui-session.ts` | MODIFY (Phase-2 owner) | Extend the existing digest-only pairing/session owner with bounded attempt stores, source-key derivation, 429 result, expiry, and by-key revocation. | +| `src/server/gui-session.ts` | MODIFY (Phase-2 owner) | Extend the existing digest-only pairing/session owner with bounded attempt stores, source-key derivation, 429 result, and expiry; grants remain independent of data keys. | | `src/remote/protocol.ts` | MODIFY (Phase-1 owner) | Extend the existing pure parser/interval-compatibility owner with additive feature intersection; no I/O or local writes. | -| `src/client/catalog.ts` | MODIFY | Bounded decompressed read, strict remote catalog validation, ETag/LKG handling, and atomic-write precondition. | -| `src/client/relay.ts` | MODIFY | Fixed-target URL construction, request/response header rebuilding, redirect refusal, body/stream caps, and redacted errors. | +| `src/client/hub-client.ts` | MODIFY (Phase-3 owner) | Bounded decompressed read, strict remote catalog validation, ETag/LKG handling, and atomic-write precondition. | +| `src/client/hub-relay.ts` | MODIFY (Phase-4 owner) | Fixed-target URL construction, request/response header rebuilding, redirect refusal, body/stream caps, and redacted errors. | | `tests/server-management-auth.test.ts` | MODIFY (Phase-2 owner) | Deterministic pairing attempt/TTL/capacity/replay/race matrix in the existing primary session suite. | | `tests/proxy-liveness.test.ts` | MODIFY (Phase-1 owner) | Protocol metadata parsing remains additive while ordinary readiness identity remains strict. | | `tests/cli-ready-subprocess.test.ts` | MODIFY | Full released-process skew matrix and no-write mismatch outcomes. | -| `tests/remote-catalog.test.ts` | MODIFY (Phase-1/3 owner) | Oversized/malformed/schema/ETag/LKG/no-write adversarial matrix. | +| `tests/remote-catalog.test.ts` | ADD BY PHASE 6 | Oversized/malformed/schema/ETag/LKG/no-write adversarial matrix for the Phase-3 `hub-client` owner. | | `tests/client-hub-relay.test.ts` | MODIFY (Phase-4 owner) | SSRF, redirect, authority, smuggling, header stripping, and bounded streaming negatives. | | `tests/bounded-body.test.ts` | MODIFY only if shared helper changes | Reuse exact-cap/one-byte-over/trickle semantics; do not duplicate the helper contract in client tests. | | `tests/credential-redirect-guard.test.ts` | EXTEND/REUSE | Existing sibling evidence for credential-bearing redirect refusal. | @@ -266,31 +269,40 @@ No response except successful start contains the pending secret. `ocx connect rotate` requires one transient `--pairing-code-stdin` or `--admin-token-stdin`; neither is persisted. It performs: -1. Read current key id and current token into memory; create no output containing either secret. -2. Start rotation; receive pending secret once. +1. Read current key id and current token into memory; write the old token to + `.prev` with the same owner-only 0600/ACL rules and fsync it. +2. Start rotation; receive pending secret once, then persist + `pendingOperation: {kind:"rotate",newKeyIssuedAt,oldKeyBackupPath}` before replacement. 3. Write pending secret to a same-directory owner-only temp, harden it with the same `serviceApiTokenFilePath()` rules, fsync, and atomically replace the token file. 4. Probe authenticated `/v1/catalog` with the new key and verify the expected client key id via the safe response/diagnostic contract. -5. Commit rotation. Commit invalidates old-key admission, sessions, and pairing grants bound to - that key id. An already-admitted in-flight turn may complete; the next old-key request is 401. -6. If steps 3–5 fail before a confirmed commit, restore the old token atomically and abort the - pending rotation. If commit outcome is uncertain, probe with both keys: exactly one accepted - result determines the local file; never replay commit blindly. +5. Commit rotation. Commit invalidates old-key admission only; pairing grants and GUI sessions + are not key-bound. An already-admitted in-flight turn may complete; the next old-key request + is 401. After verified commit, delete `.prev` and clear `pendingOperation`. +6. If steps 2–5 fail before a confirmed commit, restore the old token atomically from + `.prev`, abort the pending rotation, then delete the backup and clear the operation. + If commit outcome is uncertain, probe with both keys: exactly one accepted result determines + the local file; never replay commit blindly. + +On startup/status, `src/client/state.ts` treats a rotate `pendingOperation` as a recovery gate: +verify that `oldKeyBackupPath` is exactly `.prev`, require an owner-only regular file, +probe current and backup keys using the authenticated catalog key-id echo, then complete the same +commit-or-restore chain. Missing, unsafe, or doubly accepted/rejected evidence stops with an exact +recovery instruction and never deletes either candidate blindly. The GUI exposes the same lifecycle for an operator updating a client manually, with explicit copy-once and commit-after-client-probe wording. Closing the modal does not imply commit; pending state remains visible and abortable until expiry. -## 4. Session invalidation contract +## 4. Session self-logout contract -Phase-2 `GuiSessionRecord` gains optional `apiKeyId` for sessions created from a client-bound -pairing grant. Loopback and Tailscale sessions without a client association may omit it. +Phase-2 pairing grants and `GuiSessionRecord` remain independent of data-key ids. Rotation, +disconnect, and key deletion therefore do not implicitly log out a browser session. ```ts export interface ManagementSessionControl { revokeCurrent(req: Request): boolean; - revokeForApiKeyId(apiKeyId: string): number; } export function createManagementSessionControl( @@ -298,14 +310,17 @@ export function createManagementSessionControl( ): ManagementSessionControl; ``` -`handleManagementAPI` receives this narrow control (directly or through `ManagementContext`), -not the session map and never the admin token. `POST /api/session/logout` requires +`handleManagementAPI` receives this narrow current-session control (directly or through +`ManagementContext`), not the session map and never the admin token. +`POST /api/session/logout` requires `principal === "gui-session"`, same browser Origin, and CSRF. Admin-token calls return 403. -Rotation commit and key deletion call `revokeForApiKeyId` only after config persistence commits. -A persistence failure leaves key and sessions unchanged. `ocx disconnect` calls self-logout -best-effort before local restore; hub-down still restores from the local journal and reports that -remote session expiry/revocation could not be confirmed. +`ocx disconnect` performs local restore only and sends no hub revocation request. To revoke the +still-valid data key, an operator uses the hub GUI's existing key deletion or +`ocx connect revoke --admin-token-stdin`; the CLI requires the transient admin credential, +deletes the exact configured key id, and clears no local state unless deletion is confirmed. +Explicit GUI self-logout remains available independently. A hub-down disconnect succeeds locally +and reports that operator revocation remains outstanding. ## 5. Pairing rate limits @@ -337,7 +352,8 @@ Fixed starting limits (configurable downward only is unnecessary in v1): - Expired grant/source entries are pruned synchronously on pairing operations; no core timer. - `Retry-After` is integer seconds, bounded by the remaining window, and contains no identity. - Constant-time code comparison; generic invalid/expired/consumed response; no existence oracle. -- Rotation commit/key delete revoke unconsumed grants associated with that client key id. +- Pairing grants have no client-key association; rotation, key deletion, and disconnect do not + scan or revoke the grant store. Rate-limit logs contain only reason, ingress class, and aggregate count. No code, raw IP, Tailscale user/email, Origin, token, or account id. @@ -376,7 +392,7 @@ Required matrix: | p2/min2 | p1/min1 | hub dropped v1 floor | Reject `hub-too-new` before write. | | p1/min1 | p2/min2 | dev client requires newer hub | Reject hub-too-old before write. | | p1/min1/unknown-X | p1/min1 | additive unknown feature | Accept; unknown feature remains disabled. | -| missing/NaN/fraction/negative/min>protocol or invalid `managementUrl` | p1 | malformed or legacy non-v1 hub | Exact Phase-1 `invalid` message; zero local writes. | +| missing/zero/NaN/fraction/negative/min>protocol or invalid `managementUrl` | p1 | malformed-input class (`400`), never a version mismatch | Exact Phase-1 `invalid` message; zero local writes. | | valid descriptor, `/readyz` pending/failed | any | startup not ready | Do not negotiate or write; preserve existing readiness behavior. | The guaranteed live pair is dev hub ↔ latest published protocol-v1 client and the reverse. @@ -413,7 +429,7 @@ Every rejection asserts token/catalog/state/journal bytes are unchanged. ## 8. Relay SSRF and header-smuggling negatives -`src/client/relay.ts` is not a general proxy. Its destination is the validated +`src/client/hub-relay.ts` is not a general proxy. Its destination is the validated `connectionState.managementUrl` captured when the listener starts. A request cannot supply or override scheme, host, port, userinfo, fragment, DNS result, or redirect target. @@ -468,11 +484,11 @@ Phase 6 extends them rather than creating parallel “hardening2” files. | --- | --- | --- | | rotation start | Existing key, no pending rotation, admin/session authority | New key returned once; old+pending both admit under same id; list masks both. | | second start | Existing unexpired pending rotation | 409; no third secret/state change. | -| client commit | New key written + authenticated catalog succeeds | Pending promoted atomically; old next request 401; id/usage bucket stable; sessions/grants invalidated. | -| client write/probe fail | Fail temp write, hardening, rename, or new-key probe | Old file restored/unchanged; pending aborted or expires; old key remains valid. | -| uncertain commit | Drop commit response after server may commit | Probe old+new; choose sole accepted key; no blind replay. | +| client commit | New key written + authenticated catalog succeeds | Pending promoted atomically; old next request 401; id/usage bucket stable; `.prev` deleted and pending operation cleared; sessions/grants unchanged. | +| client write/probe fail | Fail backup/temp write, hardening, rename, or new-key probe | Old file restored/unchanged from `.prev`; pending aborted or expires; old key remains valid. | +| uncertain commit/crash | Drop commit response or restart with pending operation | Probe current+`.prev`; choose sole accepted key; finish commit-or-restore chain; no blind replay. | | pending expiry | Fake clock past 10 minutes | Pending rejected/removed; old accepted. | -| delete key | Delete configured key with bound sessions/grants | Admission, sessions, and grants revoked after persistence only. | +| operator revoke | Hub GUI delete or `ocx connect revoke --admin-token-stdin` for configured id | Data-key admission revoked after persistence; sessions/grants unchanged; disconnect alone made no hub request. | | self logout | GUI session + Origin + CSRF | Current session removed; replay 401. Admin-token call 403. | | pairing bad guesses | Same grant/source repeated with fake clock | Fifth grant failure burns; source threshold yields 429 + bounded Retry-After; no identity leak. | | pairing replay/race | Two concurrent valid redemptions | Exactly one session; other generic failure. | @@ -503,8 +519,8 @@ The Remote Hub guide in all eight locales must cover: - admin token ordinary-management scope and permanent inability to mint consent sessions; - systemd/launchd, Docker volume/secret/probes, headless OAuth, rotation, rollback, and protocol upgrade errors; -- troubleshooting for hub down, stale catalog, rotated token, protocol mismatch, lost pairing, - plain HTTP, and remote-session expiry/invalidation. +- troubleshooting for hub down, stale catalog, rotated token/`.prev` recovery, protocol mismatch, + lost pairing, plain HTTP, remote-session logout/expiry, and outstanding operator revocation. Reference pages list exact config keys/defaults and endpoint auth. No page may call `/healthz` readiness, claim usage mirroring, suggest putting a token on argv, suggest `0.0.0.0:10101`, trust @@ -527,7 +543,8 @@ Docker secret guidance, or dependency installation is security-review-required u migration, error, or recovery branch. - [ ] Data keys authorize only the data matrix and `/v1/catalog`; rotation endpoints remain management-authenticated and transient authority is never persisted. -- [ ] Session Origin/serverOrigin/browserOrigin, CSRF, TTL, renewal, logout, key invalidation, +- [ ] Session Origin/serverOrigin/browserOrigin, CSRF, TTL, renewal, explicit logout, absence of + key-bound invalidation, replay, and wrong-ingress tests are linked. - [ ] Pairing entropy, one-use, TTL, attempt/capacity limits, race behavior, 429, and redacted logging tests are linked. @@ -548,9 +565,10 @@ the same readiness report. ## 12. Acceptance criteria -- [ ] Per-client rotation is recoverable, bounded, stable-id-attributed, secret-safe, and invalidates - the old key/session/grants only after commit. -- [ ] Session self-logout and disconnect behavior are explicit; admin-token consent remains 403. +- [ ] Per-client rotation is recoverable through `pendingOperation` + `.prev`, bounded, + stable-id-attributed, secret-safe, and invalidates only old-key admission after commit. +- [ ] Session self-logout, local-only disconnect, and explicit operator revoke behavior are + distinct; admin-token consent remains 403. - [ ] Pairing attempt and capacity state are bounded, deterministic under fake time, one-use under races, and privacy-safe. - [ ] Every protocol matrix row is reachable and proves no-write behavior before incompatibility. From d25cbc02a592dc6e772f6e83375c4d04c97fc347 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 01:45:54 +0900 Subject: [PATCH 021/172] feat(remote): add protocol metadata and runtime role --- src/config.ts | 35 ++++++++++++++ src/remote/protocol.ts | 88 ++++++++++++++++++++++++++++++++++++ src/server/index.ts | 2 + src/server/proxy-liveness.ts | 6 +++ src/types/config.ts | 4 ++ 5 files changed, 135 insertions(+) create mode 100644 src/remote/protocol.ts diff --git a/src/config.ts b/src/config.ts index 11d88af91d..c77e1507ff 100644 --- a/src/config.ts +++ b/src/config.ts @@ -74,6 +74,7 @@ import { type FastWire, type ProviderCostOverlay, } from "./types"; +import type { OcxRuntimeRole } from "./types/config"; import { OPENAI_CODEX_PROVIDER_ID } from "./providers/openai-tiers"; import { modelAutoCompactTokenLimitsConfigError } from "./providers/auto-compact-budget"; import { fastWireDeclarationError, hasFastWireCapabilityConflict } from "./providers/fastwire"; @@ -861,8 +862,13 @@ const agentTaskRecoverySchema = z.object({ cacheEntries: z.number().int().min(1).max(512).optional(), }).strict(); +const runtimeRoleSchema = z.enum(["standalone", "hub", "client"]); + const configSchema = z.object({ port: z.number().int().min(0).max(65535).default(10100), + // A malformed hand edit must disable only remote-role behavior, not discard + // providers or data-plane keys. Live writes are rejected explicitly below. + runtimeRole: runtimeRoleSchema.optional().catch(undefined), managementUsageMaxReadBytes: z.number().int().positive().default(64 * 1024 * 1024), // Invalid hand edits disable only this opt-in circuit. Live writes remain strict. upstreamHostCircuitThreshold: z.number().int() @@ -1706,6 +1712,18 @@ function warnDegradedAgentTaskRecovery(rawParsed: unknown): void { if (warning) console.warn(`⚠️ config.json ${warning}. Other settings were preserved.`); } +function malformedRuntimeRoleWarning(rawParsed: unknown): string | null { + const raw = rawConfigRecord(rawParsed); + if (!raw || !Object.hasOwn(raw, "runtimeRole") || raw.runtimeRole === undefined) return null; + if (runtimeRoleSchema.safeParse(raw.runtimeRole).success) return null; + return 'runtimeRole ignored: expected "standalone", "hub", or "client"; falling back to "standalone"'; +} + +function warnDegradedRuntimeRole(rawParsed: unknown): void { + const warning = malformedRuntimeRoleWarning(rawParsed); + if (warning) console.warn(`⚠️ config.json ${warning}. Other settings were preserved.`); +} + type NativeSubagentPersistedField = "injectionModel" | "injectionEffort" | "syncCodexSubagentDefaults"; function rawConfigRecord(rawParsed: unknown): Record | null { @@ -1859,6 +1877,7 @@ export function loadConfig(): OcxConfig { warnDegradedCodexAccountPicker(parsed); warnDegradedUpstreamHostCircuitThreshold(parsed); warnDegradedAgentTaskRecovery(parsed); + warnDegradedRuntimeRole(parsed); return withRefreshedCostOverlays(normalizeClaudeSubagentEffort(normalizeNativeSubagentSync(config, parsed), parsed)); } // Schema validation failed — merge defaults into the raw object instead of @@ -1883,6 +1902,7 @@ export function loadConfig(): OcxConfig { warnDegradedCodexAccountPicker(parsed); warnDegradedUpstreamHostCircuitThreshold(parsed); warnDegradedAgentTaskRecovery(parsed); + warnDegradedRuntimeRole(parsed); return withRefreshedCostOverlays(normalizeClaudeSubagentEffort(normalizeNativeSubagentSync(config, parsed), parsed)); } // Still failing, but if every complaint is about one or more named entries @@ -1903,6 +1923,7 @@ export function loadConfig(): OcxConfig { warnDegradedCodexAccountPicker(parsed); warnDegradedUpstreamHostCircuitThreshold(parsed); warnDegradedAgentTaskRecovery(parsed); + warnDegradedRuntimeRole(parsed); return withRefreshedCostOverlays(normalizeClaudeSubagentEffort(normalizeNativeSubagentSync(config, parsed), parsed)); } } @@ -2003,6 +2024,8 @@ function validFileConfigDiagnostics(config: OcxConfig, rawParsed: unknown): Conf if (hostCircuitWarning) warnings.push(hostCircuitWarning); const recoveryWarning = malformedAgentTaskRecoveryWarning(rawParsed); if (recoveryWarning) warnings.push(recoveryWarning); + const runtimeRoleWarning = malformedRuntimeRoleWarning(rawParsed); + if (runtimeRoleWarning) warnings.push(runtimeRoleWarning); if (syncDisabledReason) { warnings.push(`syncCodexSubagentDefaults ignored: ${syncDisabledReason}`); } @@ -2094,6 +2117,13 @@ function agentTaskRecoveryError(value: unknown): string | null { return `schema_invalid: agentTaskRecovery${field ? `.${field}` : ""}: ${issue?.message ?? "invalid configuration"}`; } +function runtimeRoleError(value: unknown): string | null { + const raw = rawConfigRecord(value); + if (!raw || !Object.hasOwn(raw, "runtimeRole") || raw.runtimeRole === undefined) return null; + if (runtimeRoleSchema.safeParse(raw.runtimeRole).success) return null; + return 'schema_invalid: runtimeRole: must be one of "standalone", "hub", or "client"'; +} + /** * Same reasoning as {@link blankHostnameError}, and more urgent: the read path degrades a * malformed selection-order map to undefined, which on a write would drop every entry the @@ -2211,6 +2241,7 @@ export function validateConfigCandidate(value: unknown): { ok: true; config: Ocx ?? codexAccountPickerEnabledError(value) ?? emptyCompletionRetryError(value) ?? oauthOpenBrowserError(value) + ?? runtimeRoleError(value) ?? loopbackListenerPortError(value); if (boundaryError) return { ok: false, error: boundaryError }; const result = configSchema.safeParse(value); @@ -3123,6 +3154,10 @@ export function multiAgentGuidanceEnabled( return config.multiAgentGuidanceEnabled !== false; } +export function runtimeRole(config: Pick): OcxRuntimeRole { + return config.runtimeRole ?? "standalone"; +} + export function getDefaultConfig(): OcxConfig { // Fresh-install default: works out of the box with Codex's ChatGPT OAuth (no API key). // gpt-* requests forward the caller's incoming OAuth headers to the ChatGPT backend. diff --git a/src/remote/protocol.ts b/src/remote/protocol.ts new file mode 100644 index 0000000000..de366d5e53 --- /dev/null +++ b/src/remote/protocol.ts @@ -0,0 +1,88 @@ +import type { OcxConfig } from "../types/config"; + +export const REMOTE_HUB_PROTOCOL = 1; +export const MINIMUM_REMOTE_CLIENT_PROTOCOL = 1; + +export interface RemoteReadyMetadata { + protocol: number; + minimumClientProtocol: number; + managementUrl: string; +} + +export type RemoteProtocolCompatibility = + | { ok: true; metadata: RemoteReadyMetadata } + | { ok: false; reason: "invalid" | "hub-too-new" | "hub-too-old"; message: string }; + +const INVALID_REMOTE_PROTOCOL_MESSAGE = + "OpenCodex hub returned invalid remote protocol metadata; upgrade or repair ocx on the hub."; + +function positiveSafeInteger(value: unknown): value is number { + return typeof value === "number" && Number.isSafeInteger(value) && value > 0; +} + +function managementOrigin(value: unknown): string | null { + if (typeof value !== "string") return null; + try { + const parsed = new URL(value); + if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return null; + if (parsed.username || parsed.password || parsed.pathname !== "/" || parsed.search || parsed.hash) return null; + return parsed.origin; + } catch { + return null; + } +} + +export function readyProtocolMetadata(config: OcxConfig, req: Request): RemoteReadyMetadata { + // Phase 2 will consult config.hub.managementPublicOrigin here. Keeping the + // parameter now fixes the consumer signature without changing Phase 1 behavior. + void config; + const managementUrl = managementOrigin(new URL(req.url).origin); + if (!managementUrl) throw new Error("Readiness request does not have an HTTP(S) management origin"); + return { + protocol: REMOTE_HUB_PROTOCOL, + minimumClientProtocol: MINIMUM_REMOTE_CLIENT_PROTOCOL, + managementUrl, + }; +} + +export function parseRemoteReadyMetadata(value: unknown): RemoteReadyMetadata | null { + if (!value || typeof value !== "object" || Array.isArray(value)) return null; + const raw = value as Record; + if (!positiveSafeInteger(raw.protocol) || !positiveSafeInteger(raw.minimumClientProtocol)) return null; + if (raw.minimumClientProtocol > raw.protocol) return null; + const parsedManagementOrigin = managementOrigin(raw.managementUrl); + if (!parsedManagementOrigin) return null; + return { + protocol: raw.protocol, + minimumClientProtocol: raw.minimumClientProtocol, + managementUrl: parsedManagementOrigin, + }; +} + +export function checkRemoteProtocolCompatibility( + value: unknown, + client: { protocol: number; minimumHubProtocol: number } = { + protocol: REMOTE_HUB_PROTOCOL, + minimumHubProtocol: MINIMUM_REMOTE_CLIENT_PROTOCOL, + }, +): RemoteProtocolCompatibility { + const metadata = parseRemoteReadyMetadata(value); + if (!metadata || !positiveSafeInteger(client.protocol) || !positiveSafeInteger(client.minimumHubProtocol)) { + return { ok: false, reason: "invalid", message: INVALID_REMOTE_PROTOCOL_MESSAGE }; + } + if (client.protocol < metadata.minimumClientProtocol) { + return { + ok: false, + reason: "hub-too-new", + message: `OpenCodex hub requires remote protocol ${metadata.minimumClientProtocol}; this client supports protocol ${client.protocol}. Upgrade ocx on this client.`, + }; + } + if (metadata.protocol < client.minimumHubProtocol) { + return { + ok: false, + reason: "hub-too-old", + message: `OpenCodex hub provides remote protocol ${metadata.protocol}; this client requires at least ${client.minimumHubProtocol}. Upgrade ocx on the hub.`, + }; + } + return { ok: true, metadata }; +} diff --git a/src/server/index.ts b/src/server/index.ts index 18e4e5254a..e0e30b618d 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -210,6 +210,7 @@ import { type PackageTreeIntegrityGuard, } from "../lib/package-tree-integrity"; import { detectInstall } from "../update/index"; +import { readyProtocolMetadata } from "../remote/protocol"; export const MAX_WS_FRAME_BYTES = 50 * 1024 * 1024; const WEBSOCKET_IDLE_TIMEOUT_SECONDS = 0; @@ -1041,6 +1042,7 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server Date: Fri, 28 Aug 2026 01:32:59 +0900 Subject: [PATCH 022/172] =?UTF-8?q?docs(devlog):=20fold=20roadmap=20audit?= =?UTF-8?q?=20r2=20=E2=80=94=207=20blockers=20closed=20(synthesis=20in=200?= =?UTF-8?q?03)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../003_audit_r2_synthesis.md | 34 ++++++++ devlog/_plan/260827_remote_hub/010_design.md | 2 +- .../030_phase1_protocol_catalog.md | 37 +++++---- .../040_phase2_remote_session.md | 16 ++-- .../260827_remote_hub/050_phase3_connect.md | 68 +++++++++++----- .../260827_remote_hub/060_phase4_two_plane.md | 28 ++++--- .../260827_remote_hub/070_phase5_deploy.md | 4 +- .../260827_remote_hub/080_phase6_hardening.md | 80 ++++++++++++------- 8 files changed, 186 insertions(+), 83 deletions(-) create mode 100644 devlog/_plan/260827_remote_hub/003_audit_r2_synthesis.md diff --git a/devlog/_plan/260827_remote_hub/003_audit_r2_synthesis.md b/devlog/_plan/260827_remote_hub/003_audit_r2_synthesis.md new file mode 100644 index 0000000000..d8f6899453 --- /dev/null +++ b/devlog/_plan/260827_remote_hub/003_audit_r2_synthesis.md @@ -0,0 +1,34 @@ +# 003 — Audit synthesis, roadmap round 2 (FAIL, 7 blockers) — canonical decisions + +Closed in r2: old-2 (managementPublicOrigin chain), old-4 (per-call planes), old-6 (phase-6 owners). +Decisions for the 7 remaining (all fold, no rebuttals): + +1 P3-A4 fixture: rejection row uses hub {protocol:2, minimumClientProtocol:2}; a p2/min1 hub + is COMPATIBLE and gets its own acceptance row. "protocol major 2" wording deleted. +2 Pairing e2e, single truth: ocx gui pair --origin REQUIRED argument, no + default (040+070 updated; dogfood runbook passes --origin http://localhost:10100). + 060 gains a pairing UI owner row: gui/src/connect-pairing.ts + i18n keys + activation + scenario (paste code → POST exchange via relay → session stored). Relay contract states + it forwards the browser Origin header verbatim on POST /opencodex-session and 060 test + plan adds the exact-POST-route case. +3 Canonical relay spelling everywhere: POST /api/machine/hub-relay/* (prefix + suffix); + 010:179 updated to the wildcard form. +4 x-opencodex-key-id: configured-key admission ONLY (environment/loopback/none → header + absent); value re-validated header-safe as ^[A-Za-z0-9._-]{1,64}$ at emission (mismatch → + omit header, log once); emitted on 200 AND 304; response gains Cache-Control: private, + no-cache; tests cover absence for environment/loopback and no key-id in logs. privacy:scan + claim removed — runtime-header privacy is proven by the log-absence test instead. +5 Post-disconnect revoke: hub GUI is the SOLE post-disconnect revocation path. ocx connect + revoke exists only while connected (state carries apiKeyId from issuance response — 050 + state gains apiKeyId field, full chain issuance→state→revoke→display); disconnect prompts + a reminder naming the hub GUI page. No tombstones. +6 Zeroization wording: "release references and overwrite the coordinator's Uint8Array copy; + the immutable argv/stdin string copies are best-effort GC" — OneTimeConnectCredential.value + becomes Uint8Array (decoded once at read), display never renders it. +7 Rotation chain completed: OcxClientConnectionConfig gains pendingOperation?: { kind: + "rotate"; rotationId: string; newKeyIssuedAt: string; oldKeyBackupPath: string } with + validation in the client-config reader; recovery on doubly-accepted = COMMIT the new key + (delete .prev + clear pendingOperation) because new-key acceptance proves issuance + completed; .prev writer assigned to src/lib/service-secrets.ts (existing owner) as + writeTokenBackup/restoreTokenBackup; 080 focused commands add tests/client-connect.test.ts + and tests/service-secrets.test.ts. diff --git a/devlog/_plan/260827_remote_hub/010_design.md b/devlog/_plan/260827_remote_hub/010_design.md index 33332c77fd..76099d4e30 100644 --- a/devlog/_plan/260827_remote_hub/010_design.md +++ b/devlog/_plan/260827_remote_hub/010_design.md @@ -176,7 +176,7 @@ opt-in with ownership records. Explicit allowlist, default-404 (same failure mode as loopbackRouteAllowed): /healthz · /readyz · GET /api/machine/status · GET /api/machine/clients · POST /api/machine/sync · GET/POST /api/machine/shim · POST /api/machine/disconnect · -POST /api/machine/hub-relay (opt-in only). +POST /api/machine/hub-relay/* (opt-in only). Mutations need local gui-session + CSRF (auto-minted on loopback, today's flow). Relay constraints (it is NOT a proxy): fixed destination = client.managementUrl; diff --git a/devlog/_plan/260827_remote_hub/030_phase1_protocol_catalog.md b/devlog/_plan/260827_remote_hub/030_phase1_protocol_catalog.md index 90e8ed526d..171b99a841 100644 --- a/devlog/_plan/260827_remote_hub/030_phase1_protocol_catalog.md +++ b/devlog/_plan/260827_remote_hub/030_phase1_protocol_catalog.md @@ -33,8 +33,11 @@ runtime behavior. All code paths remain standalone-compatible when `runtimeRole` `x-opencodex-api-key: ` or `Authorization: Bearer `. `x-api-key`, a foreign bearer, the admin token, and no credential are rejected on a non-loopback bind. The row is added to `AUTH_MATRIX` with `xApiKey: "rejected"`. -- An admitted `/v1/catalog` response includes `x-opencodex-key-id` with the admitted - key's id. Rejected and unauthenticated responses never include this header. +- A `/v1/catalog` response admitted by a configured key includes `x-opencodex-key-id` with + that key's id on both 200 and 304. Environment-token and loopback paths, plus rejected + and unauthenticated responses, never include this header. Revalidate the id at emission as + `^[A-Za-z0-9._-]{1,64}$`; on mismatch omit the header and log one non-secret warning. + Every successful catalog response includes `Cache-Control: private, no-cache`. - A serialized catalog larger than `MAX_REMOTE_CATALOG_BYTES = 32 * 1024 * 1024` is not returned over `/v1/catalog`; it fails with HTTP 503 and the stable code `catalog_too_large`. The management route continues to expose the same serialized bytes @@ -49,7 +52,7 @@ runtime behavior. All code paths remain standalone-compatible when `runtimeRole` - A parser/compatibility predicate for future `ocx connect`, including additive-field tolerance for a dev hub paired with the latest released client. - Shared catalog serialization, ETag, `If-None-Match`, size cap, data-plane admission, and - authenticated `x-opencodex-key-id` attribution. + configured-key-only `x-opencodex-key-id` attribution. - Route placement before the unknown-`/v1/*` JSON-404 guard. - Focused and full remote-only verification commands. @@ -123,15 +126,17 @@ expected body by calling the serializer twice. The ETag is `"sha256-"` over the exact UTF-8 response bytes. A matching strong tag, weak spelling of that same tag, a comma-list containing it, or `*` returns 304 -with ETag and no body. A stale/malformed `If-None-Match` returns 200. ETag is computed only -after the 32 MiB bound passes. +with ETag and no body. A stale/malformed `If-None-Match` returns 200. Both 200 and 304 carry +`Cache-Control: private, no-cache`. ETag is computed only after the 32 MiB bound passes. `/v1/catalog` uses `resolveResponsesApiAuth(req, policy)`, not `resolveApiAuth`, because the former is the existing dedicated-header/our-secret-Bearer matrix and rejects `x-api-key` (`src/server/auth-cors.ts:465-478`). It performs the existing data-plane origin check after -admission, then sets `x-opencodex-key-id` from that admitted key identity. Rejected and -unauthenticated paths never emit the header. No Direct passthrough exists on this read-only -route and no credential is forwarded. +admission, then sets `x-opencodex-key-id` on 200 and 304 only when the admitted identity is +a configured API key and its id passes `^[A-Za-z0-9._-]{1,64}$` again at emission. A +mismatch omits the header and emits one non-secret warning without the id. Environment-token, +loopback, rejected, and unauthenticated paths never emit the header. No Direct passthrough +exists on this read-only route and no credential is forwarded. ## 4. Diff-level file-change map @@ -145,14 +150,14 @@ All paths below exist in the current tree except the two files marked **NEW**. | NEW | `src/server/catalog-download.ts` | Own `MAX_REMOTE_CATALOG_BYTES`, one persisted-catalog serialization result, byte-derived ETag matching, `/api/catalog` response construction, and bounded `/v1/catalog` response construction. | | MODIFY | `src/server/management/model-routes.ts` | Replace the inline `/api/catalog` read/`JSON.stringify` block at lines 334–345 with the shared response builder; preserve 404 and `x-opencodex-codex-version`. | | MODIFY | `src/server/auth-cors.ts` | Add the `/v1/catalog` `AUTH_MATRIX` row with bearer/dedicated accepted and `xApiKey` rejected. Do not change any existing row or credential precedence. | -| MODIFY | `src/server/index.ts` | Add protocol metadata to the current `/readyz` body at lines 991–998 by passing `(config, req)` to the builder. Mount exact `GET /v1/catalog` after readiness/management handling and before the unknown-`/v1/*` guard at line 1604; use `resolveResponsesApiAuth` plus the existing origin policy and emit `x-opencodex-key-id` only from the admitted key identity. Keep `startServer` synchronous and add no `await` between `Bun.serve` and `labActivationRequired`. | +| MODIFY | `src/server/index.ts` | Add protocol metadata to the current `/readyz` body at lines 991–998 by passing `(config, req)` to the builder. Mount exact `GET /v1/catalog` after readiness/management handling and before the unknown-`/v1/*` guard at line 1604; use `resolveResponsesApiAuth` plus the existing origin policy, add `Cache-Control: private, no-cache`, and emit `x-opencodex-key-id` on 200/304 only for a configured-key identity whose id passes the emission-time header-safe guard. Omit and log once without the id on mismatch. Keep `startServer` synchronous and add no `await` between `Bun.serve` and `labActivationRequired`. | | MODIFY | `src/server/proxy-liveness.ts` | Keep existing identity/status parsing additive; extend the internal body type/comments so protocol fields are recognized but do not make ordinary `ocx ready` reject a v0/legacy standalone server. Remote compatibility remains in `src/remote/protocol.ts`. | | MODIFY | `tests/config.test.ts` | Extend the existing config-default/validation sibling tests with absent/default, three valid roles, malformed live candidate, and malformed persisted role preservation cases. | | MODIFY | `tests/server-live.test.ts` | Extend the existing `GET /readyz` suite (lines 1220+) for exact protocol values on ready/pending/failed/draining, sanitized keys, management origin, method/path negatives, and no-auth behavior. | | MODIFY | `tests/proxy-liveness.test.ts` | Extend the current strict readiness parser/probe tests to prove additive protocol fields neither invalidate readiness nor bypass identity/status checks. | | MODIFY | `tests/api-catalog-route.test.ts` | Extend the existing `/api/catalog` sibling suite with fixed fixture bytes and version-header preservation after serializer extraction. | -| MODIFY | `tests/server-auth.test.ts` | Extend live-server auth/order coverage with `/v1/catalog` admission matrix, exact admitted `x-opencodex-key-id`, header absence on every rejected/unauthenticated path, exact-path/method negatives, foreign/admin bearer rejection, bound overflow, ETag/304, and unknown-`/v1` guard preservation. | -| MODIFY | `tests/api-key-attribution.test.ts` | Extend the existing live `AUTH_MATRIX` loop so `/v1/catalog` is a GET route, each cell reaches the real handler rather than the generic 404, and successful dedicated/Bearer admission echoes that key's id. | +| MODIFY | `tests/server-auth.test.ts` | Extend live-server auth/order coverage with `/v1/catalog` admission matrix, exact configured-key `x-opencodex-key-id` on 200 and 304, `Cache-Control: private, no-cache`, header absence for environment-token/loopback/rejected/unauthenticated paths, invalid-id omission with one id-free log, exact-path/method negatives, foreign/admin bearer rejection, bound overflow, ETag/304, and unknown-`/v1` guard preservation. | +| MODIFY | `tests/api-key-attribution.test.ts` | Extend the existing live `AUTH_MATRIX` loop so `/v1/catalog` is a GET route, each cell reaches the real handler rather than the generic 404, configured-key dedicated/Bearer admission echoes that key's id, and environment/loopback admission does not. | No other production or test file is in scope. If implementation proves another path is required, stop the phase and amend this document before editing it. @@ -230,12 +235,13 @@ an empty catalog. No function accepts a caller-provided catalog path. | P1-A07 | Feed `{protocol: 2, minimumClientProtocol: 2}` to a protocol-v1 client. | `hub-too-new` and the exact “Upgrade ocx on this client” string are returned before catalog access. | | P1-A08 | Feed `{protocol: 1, minimumClientProtocol: 1}` to a protocol-v2 client requiring minimum hub protocol 2. | `hub-too-old` and the exact “Upgrade ocx on the hub” string are returned. | | P1-A09 | Supply zero, omit, mistype, overflow, set minimum above protocol, or give a path-bearing `managementUrl`. | The malformed-input class (`400`) returns `invalid` and the exact malformed-metadata string; it is never classified as a version mismatch and never falls back to protocol 1. | -| P1-A10 | Persist a fixed catalog fixture; call authorized `/api/catalog` and authorized `/v1/catalog`. | Status 200 and response bytes are byte-identical; the ETag independently hashes those bytes. | -| P1-A11 | Repeat `/v1/catalog` on non-loopback with dedicated header, our-secret Bearer, `x-api-key`, foreign Bearer, admin token, and no token. | First two reach 200 with `x-opencodex-key-id` equal to the admitted key id; all remaining cases are 401 without that header. No case reaches the generic unknown-route 404. | -| P1-A12 | Call `/v1/catalog` with matching tag, weak matching tag, tag list, `*`, stale tag, and malformed tag. | Matches return 304/no body/same ETag; stale or malformed values return 200/full bytes. | +| P1-A10 | Persist a fixed catalog fixture; call authorized `/api/catalog` and authorized `/v1/catalog`. | Status 200 and response bytes are byte-identical; the ETag independently hashes those bytes and `/v1/catalog` carries `Cache-Control: private, no-cache`. | +| P1-A11 | Repeat `/v1/catalog` with configured-key dedicated/Bearer admission, environment-token admission, loopback admission, `x-api-key`, foreign Bearer, admin token, and no token; inject an invalid configured key id, repeat the request, and capture logs. | Configured-key 200 and matching 304 carry the exact safe key id. Environment/loopback/rejected/unauthenticated responses omit it; invalid id is omitted with one non-secret warning and no key id appears in logs. No case reaches the generic unknown-route 404. | +| P1-A12 | Call `/v1/catalog` with matching tag, weak matching tag, tag list, `*`, stale tag, and malformed tag. | Matches return 304/no body/same ETag; stale or malformed values return 200/full bytes; every successful response carries `Cache-Control: private, no-cache`. | | P1-A13 | Serialize exactly the cap and cap+1 fixtures through an injected serialization seam. | Exact cap returns 200; cap+1 returns 503 `catalog_too_large`, never a partial body. | | P1-A14 | Call POST `/v1/catalog`, GET `/v1/catalog/`, and an unrelated `/v1/does-not-exist`. | Every request returns the existing JSON 404 envelope; route ordering does not widen path/method matching. | | P1-A15 | Run the import-graph and synchronous-window guard after the diff. | No new subsystem is reachable from the three protected core files; `startServer` remains non-async and its guarded window contains no top-level `await`. | +| P1-A16 | Feed `{protocol: 2, minimumClientProtocol: 1}` to a protocol-v1 client. | Compatibility succeeds; only protocol-v1 behavior is enabled. | ## 7. Verification — remote only on `lidge-ai` @@ -254,6 +260,9 @@ Review-ready shared-server gate: ssh lidge-ai 'cd ~/Developer/opencodex && bun run test && bun run privacy:scan' ``` +`privacy:scan` remains a repository gate, not the runtime-header privacy oracle; P1-A11's +captured-log absence assertion proves that key ids do not reach logs. + Record the remote commit, Bun version, command, exit code, and pass/fail counts in the phase evidence ledger. Do not repeat a passing command unless code covered by it changes. diff --git a/devlog/_plan/260827_remote_hub/040_phase2_remote_session.md b/devlog/_plan/260827_remote_hub/040_phase2_remote_session.md index 8d7408fa97..7e93db79cb 100644 --- a/devlog/_plan/260827_remote_hub/040_phase2_remote_session.md +++ b/devlog/_plan/260827_remote_hub/040_phase2_remote_session.md @@ -72,7 +72,8 @@ consumption of that credential is the only pairing exchange. - Automatic loopback and trusted-Tailscale issuance; pairing grant creation and exchange. - Separate loopback/remote TTLs and sliding renewal for remote sessions. - Exact management CORS header widening for GUI-origin and CSRF headers. -- CLI `ocx gui pair` grant creation through the existing runtime-attestation pattern; no +- CLI `ocx gui pair --origin ` grant creation through the existing + runtime-attestation pattern; no grant or reusable admin credential in argv, config, disk, logs, or shell history. - Backend and GUI regressions for every positive and negative issuance path. @@ -241,10 +242,11 @@ memory-only and are never written to web storage. - HTTPS issues `pairing`; non-loopback HTTP issues `insecure-http-pairing` only when the config opt-in is true. The grant is consumed before minting; all replays fail. - Phase 4's fixed-target relay path allowlist admits this exact POST exchange in addition - to GET bootstrap; no other non-`/api/*` method/path is widened. + to GET bootstrap and forwards the browser's `Origin` header verbatim; no other + non-`/api/*` method/path is widened. -`ocx gui pair [--origin ] [--json]` defaults `--origin` to -`hub.managementPublicOrigin`; it fails if neither exists. It resolves the identity-checked +`ocx gui pair --origin [--json]` requires an explicit `--origin`; there is +no config-derived or localhost default. It resolves the identity-checked runtime, verifies the `/healthz` challenge proof, rechecks PID/port, derives the one-operation capability from the protected runtime attestation secret, and POSTs once. It prints the grant exactly once to stdout and never accepts a grant/token argument. JSON output is intended for @@ -283,10 +285,10 @@ All paths below exist in the current tree except files marked **NEW**. | MODIFY | `src/server/index.ts` | Pass `(config, req)` to the `/readyz` protocol metadata builder so `hub.managementPublicOrigin` reaches the response; advertise GUI-pair capability v1 in `/healthz`; mount exact pairing-grant creation after capability admission, mount GET/POST bootstrap before GUI fallback, pass `trustedTailscaleIngress: false` on the public/ordinary loopback listeners, and preserve the line-1604 unknown-`/v1` guard. Do not make `startServer` async or add an await in its synchronous activation window. | | MODIFY | `src/server/proxy-liveness.ts` | Add optional `guiPairCapability` to the existing health identity projection so the local client can fail closed against an old/foreign listener without changing required liveness identity fields. | | MODIFY | `src/server/gui-static.ts` | Serialize escaped browser/server origin meta tags; keep `opencodex-session-origin` as browser-origin compatibility metadata. | -| NEW | `src/cli/gui.ts` | Own `runGuiCommand(args, deps)`: existing no-subcommand open behavior plus `pair`; strict `--origin`/`--json` parsing and one-time grant output. | +| NEW | `src/cli/gui.ts` | Own `runGuiCommand(args, deps)`: existing no-subcommand open behavior plus `pair`; require exactly one explicit `--origin`, parse `--json` strictly, and emit the one-time grant. | | NEW | `src/cli/gui-pair-client.ts` | Mirror the existing bound restart/provider-reload client pattern: read runtime identity, challenge `/healthz`, verify proof/capability version, recheck the target, derive the browser-origin-bound capability, POST once, and return a redacted typed result. | | MODIFY | `src/cli/dispatch.ts` | Delegate the current inline `gui` runner to `runGuiCommand`, passing existing open/start dependencies; do not duplicate live-proxy discovery. | -| MODIFY | `src/cli/registry.ts` | Change usage to `ocx gui [pair [--origin ] [--json]]` and document that pairing output is secret and single-use. | +| MODIFY | `src/cli/registry.ts` | Change usage to `ocx gui [pair --origin [--json]]` and document that pairing output is secret and single-use. | | MODIFY | `src/cli/help.ts` | Update the curated GUI command line so registry/help parity remains green. | | MODIFY | `gui/src/api.ts` | Split memory browser/server origins, validate both meta sources, scope token attachment to the server origin, keep browser origin in the GUI header, and clear all four session values atomically. No web-storage persistence. | | MODIFY | `tests/config.test.ts` | Extend sibling config tests for valid HTTPS, explicit HTTP opt-in, invalid origin components, duplicate/empty/oversize Tailscale users, malformed persisted block preservation, and non-hub inertness. | @@ -444,7 +446,7 @@ sessions renew at most once per request. | P2-A15 | GUI loads valid two-origin meta, then calls the bound server and an evil third origin. | Session headers attach only to bound server; evil origin receives no token/CSRF and triggers no admin prompt. | | P2-A16 | GUI receives mismatched browser origin, mismatched response/server origin, missing meta, or a failed renewal. | All in-memory session fields clear atomically; no web-storage write and no stale header reuse. | | P2-A17 | Allowed management OPTIONS requests GUI-origin + CSRF headers; repeat from rejected origin and request an unrelated custom header. | Allowed response lists the two exact additions and exact ACAO; rejected origin is 403; unrelated header is not dynamically echoed by management CORS. | -| P2-A18 | Run `ocx gui pair` with configured public origin, explicit allowed origin, missing origin, `--json`, extra args, stale target, failed attestation, and capability/API failure. | Valid cases create one grant and print once; invalid cases fail without falling back to admin auth or echoing secrets; no grant appears in argv/config/disk/log fixtures. | +| P2-A18 | Run `ocx gui pair --origin ` with an explicit allowed origin, then missing `--origin`, malformed/disallowed origin, `--json`, extra args, stale target, failed attestation, and capability/API failure. | Valid cases create one grant and print once; every absent/invalid origin fails with no default and without falling back to admin auth or echoing secrets; no grant appears in argv/config/disk/log fixtures. | | P2-A19 | Run native-profile mutation suite with admin, malformed remote session, and valid remote session. | Existing consent boundary remains: only the valid session + correct origin + CSRF dispatches the mutation. | | P2-A20 | Run import-graph and synchronous-window guard. | No protected core import reaches GUI-session code; `startServer` remains synchronous and activation ordering is unchanged. | diff --git a/devlog/_plan/260827_remote_hub/050_phase3_connect.md b/devlog/_plan/260827_remote_hub/050_phase3_connect.md index 51c7a5d31c..97628e74c7 100644 --- a/devlog/_plan/260827_remote_hub/050_phase3_connect.md +++ b/devlog/_plan/260827_remote_hub/050_phase3_connect.md @@ -94,12 +94,12 @@ Every existing path below was verified in the current tree. For NEW client paths | Action | Exact path | Diff-level change | |---|---|---| -| NEW | `src/client/state.ts` | Parse/validate `runtimeRole + config.json.client`, expose fail-closed connected/absent/invalid/mismatched states, and atomically commit/clear both keys through config mutation. | +| NEW | `src/client/state.ts` | Parse/validate `runtimeRole + config.json.client`, including the optional rotation `pendingOperation`; expose fail-closed connected/absent/invalid/mismatched states, and atomically commit/clear both keys through config mutation. | | NEW | `src/client/hub-client.ts` | Validate/normalize URLs; bounded `GET /readyz`, `POST /api/keys`, `GET /v1/catalog`; protocol/capability checks; redact all credential-bearing errors. | | NEW | `src/client/connect.ts` | Transaction coordinator, connected sync, rollback, and offline disconnect. No argv or presentation logic. | | NEW | `src/cli/connect.ts` | Parse connect/disconnect/status arguments, read exactly one `--pairing-code-stdin` or `--admin-token-stdin` credential, call the coordinator, and render redacted human/JSON output. | -| MODIFY | `src/types/config.ts` | Add `OcxClientConnectionConfig` and top-level `OcxConfig.client?`; the secret itself is not a field. | -| MODIFY | `src/config.ts` | Add schema and field-scoped persistence behavior for `client`; malformed-present client state must be diagnosable and must never degrade into standalone routing. | +| MODIFY | `src/types/config.ts` | Add `OcxClientConnectionConfig`, its optional non-secret rotation `pendingOperation`, and top-level `OcxConfig.client?`; the secret itself is not a field. | +| MODIFY | `src/config.ts` | Add schema and field-scoped persistence behavior for `client`, including `pendingOperation`; malformed-present client state must be diagnosable and must never degrade into standalone routing. | | MODIFY | `src/lib/service-secrets.ts` | Add atomic owner-only write and fingerprint-checked removal beside existing path/read helpers. | | MODIFY | `src/codex/inject.ts` | Add `CodexRoutingTarget`; thread it through provider table, `env_key`, root URL, profile, preflight, journal witness, and inject while retaining byte-compatible standalone overloads. | | MODIFY | `src/codex/journal.ts` | Add backward-compatible durable client ownership; reconcile a dead process journal only when no matching committed client state exists. | @@ -145,12 +145,18 @@ export interface OcxClientConnectionConfig { managementTransport: "direct" | "relay"; selectedClients: OcxConnectedClientId[]; tokenEnv: "OPENCODEX_API_AUTH_TOKEN"; - apiKeyId: string; // attribution id; not secret + apiKeyId: string; // exact IssuedClientKey.id; attribution/revoke id, not secret tokenFingerprint: string; // lowercase SHA-256; ownership check only protocolVersion: 1; connectedAt: string; // ISO-8601 catalogEtag?: string; catalogSyncedAt?: string; + pendingOperation?: { + kind: "rotate"; + rotationId: string; + newKeyIssuedAt: string; + oldKeyBackupPath: string; + }; } export interface OcxConfig { @@ -161,7 +167,10 @@ export interface OcxConfig { ``` The parser rejects unknown selected-client ids, non-origin URLs, protocol values other -than 1, duplicate client ids, malformed timestamps, and non-64-hex fingerprints. +than 1, duplicate client ids, malformed timestamps, and non-64-hex fingerprints. A present +`pendingOperation` must have exactly `kind: "rotate"`, a non-empty `rotationId`, a valid +`newKeyIssuedAt`, and the exact owner-approved `.prev` backup path; malformed or +partial pending state makes the client state invalid rather than dropping the recovery gate. Forward-compatible unknown object keys are preserved on unrelated config writes. A raw `client` key that is present but invalid is `kind: "invalid"`, not `absent`; start, sync, Claude launch, and status must refuse local-provider fallback in that state. @@ -269,8 +278,8 @@ the final client-state commit cannot leave durable remote routing behind. ```ts export type OneTimeConnectCredential = - | { kind: "admin"; value: string } - | { kind: "pairing-grant"; value: string }; + | { kind: "admin"; value: Uint8Array } + | { kind: "pairing-grant"; value: Uint8Array }; export interface ConnectGuiSession { token: string; @@ -294,13 +303,13 @@ export function fetchHubReady( export function exchangeConnectPairingGrant( managementUrl: string, browserOrigin: string, - grant: string, + grant: Uint8Array, options?: { allowInsecureHttp?: boolean; timeoutMs?: number; fetchImpl?: typeof fetch }, ): Promise; export function issueClientKey( managementUrl: string, credential: - | { kind: "admin"; value: string } + | { kind: "admin"; value: Uint8Array } | { kind: "gui-session"; value: ConnectGuiSession }, name: string, options?: { timeoutMs?: number; fetchImpl?: typeof fetch }, @@ -330,6 +339,11 @@ session token + `X-OpenCodex-GUI-Origin` + CSRF authorize the key POST. A raw pa grant cannot access any `/api/*` route. An admin token is never submitted to the session exchange and therefore never mints or becomes `gui-session`. +The successful issuance response's `IssuedClientKey.id` is copied unchanged into +`OcxClientConnectionConfig.apiKeyId` at the final state commit. That stored field is the +single id consumed by journal ownership, connected status/display, Usage attribution, and +Phase 6's connected-only `ocx connect revoke`; no revoke key id is accepted from argv. + ### `src/client/connect.ts` ```ts @@ -373,13 +387,20 @@ ocx connect status [--json] ocx disconnect [--keep-catalog] [--json] ``` +Phase 6 may extend this command family with `ocx connect revoke --admin-token-stdin`, but +that command is valid only while `readClientConnectionState()` is connected. It resolves +the exact key solely from `config.client.apiKeyId` and rejects disconnected, invalid, or +mismatched state before any hub request. + There is deliberately no `--token `, `--admin-token `, pairing-code positional form, or credential environment-variable form. Exactly one of -`--pairing-code-stdin` and `--admin-token-stdin` uses the bounded stdin helper. Parse errors -redact unknown bare values and all credential-shaped option values. The transient admin -credential remains in memory until the connect transaction commits or rollback finishes, -then its buffer and coordinator reference are zeroized; successful key issuance alone is -not a terminal outcome. +`--pairing-code-stdin` and `--admin-token-stdin` uses the bounded stdin helper. It decodes +the credential once at read into the coordinator-owned `Uint8Array`; no display path renders +that value. Parse errors redact unknown bare values and all credential-shaped option values. +The transient credential remains in memory until the connect transaction commits or rollback +finishes; successful key issuance alone is not a terminal outcome. At that terminal boundary, +release references and overwrite the coordinator's `Uint8Array` copy; immutable argv/stdin +string copies are best-effort GC. ## 4. Connect transaction and rollback @@ -413,8 +434,9 @@ attempts exact `DELETE /api/keys` for the just-created id. If hub cleanup is unr the failure reports only the safe key id and exact revoke action; it never prints the key. Machine-local rollback success is mandatory and remote cleanup inability is explicit, never hidden as full rollback. Only after that cleanup attempt completes does -the coordinator zeroize the transient admin credential; the success path zeroizes it -immediately after the final state commit. +the coordinator release references and overwrite its transient credential `Uint8Array`; +the success path does so immediately after the final state commit. Immutable string copies +remain best-effort GC rather than a zeroization guarantee. `--no-sync` still performs readiness, key issuance, token placement, catalog download, and final state commit, but does not mutate Codex/Claude client files. The next @@ -467,6 +489,11 @@ Disconnect is local-authoritative and works with the hub offline: 5. Clear `config.json.client` + the `client` runtime role together and last (absence resolves to standalone). +After successful local disconnect, human and JSON output retain the safe prior `apiKeyId` +only long enough to remind the operator: revoke the still-valid key from the hub GUI's +**Integrations → API Keys** page. Once state is cleared, CLI revoke is unavailable; the hub +GUI is the sole post-disconnect revocation path. + If restore is partial or the token changed, state is not cleared and the command names the conflicting artifact. This avoids claiming disconnected while Codex still points at the hub or deleting a replacement secret. Remote key revocation is not required for @@ -479,12 +506,12 @@ No test sends live hub traffic or reads the developer's homes. | Test file | Required cases | |---|---| -| `tests/client-connect.test.ts` (NEW) | URL canonicalization; exact stdin-flag exclusivity and literal/env credential rejection; Phase-1 ready parser/mismatch strings; ready/pending/failed; same-major v1 acceptance; management URL advertisement; admin HTTPS direct key POST; pairing HTTPS session exchange then key POST; dual-opt-in pairing HTTP; admin HTTP refusal; raw grant rejection at `/api/keys`; admin credential retained through commit/rollback then zeroized; bounded catalog; atomic role+state commit; each rollback point; no-sync; connected 200/304/401/timeout sync; no local discovery fake called; offline disconnect and partial restore. | +| `tests/client-connect.test.ts` (NEW) | URL canonicalization; exact stdin-flag exclusivity and literal/env credential rejection; Phase-1 ready parser/mismatch strings; ready/pending/failed; p2/min1 acceptance and p2/min2 rejection; management URL advertisement; admin HTTPS direct key POST; pairing HTTPS session exchange then key POST; issued id copied unchanged through state/status/revoke ownership; dual-opt-in pairing HTTP; admin HTTP refusal; raw grant rejection at `/api/keys`; credential retained through commit/rollback, never rendered, then coordinator `Uint8Array` overwritten and references released; bounded catalog; atomic role+state commit; each rollback point; no-sync; connected 200/304/401/timeout sync; no local discovery fake called; offline disconnect, post-disconnect hub-GUI reminder, and partial restore. | | `tests/service-secrets.test.ts` (NEW) | Exact path, 0600, Windows ACL seam, atomic replacement, symlink refusal, fingerprint, changed-file non-removal, no token in errors. | | `tests/codex-inject.test.ts` | Current standalone goldens byte-equal; explicit HTTPS target emits exact `base_url`, provider table, `env_key`; loopback-looking connected URL still requires admission; malformed target refused before journal. | | `tests/codex-inject-integration.test.ts` | Validate-only has zero writes; target commit records journal ownership; offline restore returns exact preimage; partial write rollback. | | `tests/codex-catalog-restore.test.ts`, `tests/cli-start-journal-order.test.ts` | Version-1 process journals retain current behavior; client journal survives only a matching final state; absent/invalid/mismatched state restores after dead connect PID. | -| `tests/config.test.ts` | Valid client round-trip; atomic role+client pair; unknown keys preserved; absent remains standalone; half-present/malformed remains fail-closed; no secret field accepted/emitted. | +| `tests/config.test.ts` | Valid client round-trip including a complete rotation `pendingOperation`; malformed/missing `rotationId`, timestamp, or backup path fails closed; atomic role+client pair; unknown keys preserved; absent remains standalone; half-present/malformed remains fail-closed; no secret field accepted/emitted. | | `tests/api-keys-routes.test.ts` | Admin and full GUI-session predicates create once; raw pairing grant and incomplete origin/CSRF reject; list/patch never echo secret. Phase-2 session tests remain the admin-never-mints-session oracle. | | `tests/cli-registry.test.ts`, `tests/cli-dispatch.test.ts`, `tests/cli-help.test.ts` | Registry/dispatch/help parity; no credential argv form; connected sync calls only remote coordinator; invalid client refuses. | | `tests/cli-status-json.test.ts` | Stable redacted status in disconnected/connected/invalid/token-changed/catalog-stale states. | @@ -498,7 +525,7 @@ No test sends live hub traffic or reads the developer's homes. | P3-A1 | Disconnected temp home; HTTPS hub ready on protocol 1; admin token arrives through stdin; `/api/keys` and `/v1/catalog` succeed. | One key is issued, token exists only in owner-only service file, catalog and injection commit, and `runtimeRole=client` + `config.client` are written together and last with key id/fingerprint only. | | P3-A2 | Same as A1, but a Phase-2 pairing grant bound to the future localhost GUI origin is supplied. | Grant is consumed once at `/opencodex-session`; returned GUI session + CSRF performs exact key POST; raw grant on `/api/keys` and replay fail; no transient credential persists. | | P3-A3 | HTTP management URL with pairing grant, client `--allow-insecure-http`, and hub `remoteGui.allowInsecureHttp=true`. | Exchange/key issuance succeeds with explicit warning. Missing either opt-in refuses; admin credential over HTTP refuses before credential transmission. | -| P3-A4 | `/readyz` returns protocol major 2, minimum client above 1, or status pending/failed. | Clear upgrade/not-ready error; zero key POSTs and zero local writes. Latest-release client ↔ dev hub protocol-1 fixture remains accepted. | +| P3-A4 | `/readyz` returns `{protocol: 2, minimumClientProtocol: 2}` or status pending/failed. | Clear upgrade/not-ready error; zero key POSTs and zero local writes. | | P3-A5 | Key POST returns 401/403/409 or malformed/oversized JSON. | No token/catalog/journal/config writes and no secret in diagnostics. | | P3-A6 | Token, catalog, injector preflight, inject commit, or final role+state commit is fault-injected in turn. | Prior machine bytes are restored at every point; neither `runtimeRole=client` nor visible `client` remains; remote orphan cleanup status is explicit by safe key id only. | | P3-A7 | Existing standalone config runs every current injector golden. | Output bytes are identical; no connected-only env/header/config key appears. | @@ -506,9 +533,10 @@ No test sends live hub traffic or reads the developer's homes. | P3-A9 | Connected state with hub down, 401, missing token, changed token, or malformed-present client config. | No local-provider fallback and no new local catalog; timeout keeps LKG as stale, credential/state errors fail hard. | | P3-A10 | Connected Claude launch with no user Anthropic overrides. | Child receives hub base and client token; no local proxy is started; gateway cache uses hub `/v1/models`. | | P3-A11 | Connected Claude launch with user-owned different `ANTHROPIC_BASE_URL`. | User destination wins and the hub admission token is absent from child env. | -| P3-A12 | Hub unreachable during disconnect with intact journal/token. | Native Codex bytes restore offline, owned token/catalog are removed per flags, and `runtimeRole + client` clear together and last. | +| P3-A12 | Hub unreachable during disconnect with intact journal/token. | Native Codex bytes restore offline, owned token/catalog are removed per flags, and `runtimeRole + client` clear together and last; output names the hub GUI **Integrations → API Keys** page as the sole post-disconnect revoke path. | | P3-A13 | Disconnect sees changed token or a journal ownership conflict. | Conflicting artifact is preserved, command fails, and connected state remains so status is honest. | | P3-A14 | Connect injection committed, connect process exited, and matching `runtimeRole=client + config.client.apiKeyId` was committed last; then `ocx start` runs. | Pre-start reconciliation preserves the client journal/routing. If final state is absent, invalid, mismatched, or names another key id, the same journal restores before startup. | +| P3-A15 | `/readyz` returns `{protocol: 2, minimumClientProtocol: 1}` to the protocol-v1 client. | Compatibility succeeds using protocol-v1 behavior; key issuance and connect continue normally. | ## 8. Verification — remote only on `lidge-ai` diff --git a/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md b/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md index 62f667bbab..b456ed32d9 100644 --- a/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md +++ b/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md @@ -103,6 +103,7 @@ parent exists. No generated `gui/dist` file is edited. | NEW | `tests/client-machine-listener.test.ts` | Listener bind/allowlist/auth/API/startup/offline matrix. | | NEW | `tests/client-hub-relay.test.ts` | Fixed target, header separation, body caps, redirects, errors, and SSRF negatives. | | NEW | `gui/src/api-targets.ts` | Canonical `ApiTargets`, machine-status discovery, per-plane call-base selection, relay URL construction, and disconnected fallback. | +| NEW | `gui/src/connect-pairing.ts` | Own the visible pairing-code form and activation flow: paste a one-time code, POST the exact `/opencodex-session` exchange through the selected direct/relay shared target, and install the returned session only in the shared target's in-memory auth slot. | | MODIFY | `gui/src/api.ts` | Replace `needsApiAuth`'s one same-origin slot with exact target classification and per-target in-memory session/CSRF state; attach both auth domains only on relay. | | MODIFY | `gui/src/App.tsx` | Discover targets before page fetches; supply both call bases instead of one page base; machine health remains live when hub is down; connected stop becomes disconnect/recycle. | | MODIFY | `gui/src/stop-proxy.ts` | Add mode-aware machine disconnect request while preserving existing standalone `/api/stop` behavior. | @@ -114,15 +115,15 @@ parent exists. No generated `gui/dist` file is edited. | MODIFY | `gui/src/pages/Storage.tsx` | Pass its selected shared `apiBase` through to `StorageWorkspace`. | | MODIFY | `gui/src/components/storage-workspace/StorageWorkspace.tsx` | Remove module-global `VITE_API_BASE`; use the supplied shared-plane base for Codex-log storage calls. | | MODIFY | `gui/src/styles-usage-workspace.css` | Style compact usage-source/scope controls and connected/offline qualification without changing layout direction. | -| MODIFY | `gui/src/i18n/en.ts` | Source-of-truth copy keys for connected source, this machine, hub-wide, hub offline, disconnect, and relay warnings. | -| MODIFY | `gui/src/i18n/de.ts` | Add the same keys. | -| MODIFY | `gui/src/i18n/fr.ts` | Add the same keys. | -| MODIFY | `gui/src/i18n/ja.ts` | Add the same keys. | -| MODIFY | `gui/src/i18n/ko.ts` | Add the same keys. | -| MODIFY | `gui/src/i18n/ru.ts` | Add the same keys. | -| MODIFY | `gui/src/i18n/tr.ts` | Add the same keys. | -| MODIFY | `gui/src/i18n/zh.ts` | Add the same keys. | -| MODIFY | `gui/src/i18n/zh-TW.ts` | Add the same keys. | +| MODIFY | `gui/src/i18n/en.ts` | Source-of-truth copy keys for pairing code/submit/error, connected source, this machine, hub-wide, hub offline, disconnect, and relay warnings. | +| MODIFY | `gui/src/i18n/de.ts` | Add the same pairing and connection keys. | +| MODIFY | `gui/src/i18n/fr.ts` | Add the same pairing and connection keys. | +| MODIFY | `gui/src/i18n/ja.ts` | Add the same pairing and connection keys. | +| MODIFY | `gui/src/i18n/ko.ts` | Add the same pairing and connection keys. | +| MODIFY | `gui/src/i18n/ru.ts` | Add the same pairing and connection keys. | +| MODIFY | `gui/src/i18n/tr.ts` | Add the same pairing and connection keys. | +| MODIFY | `gui/src/i18n/zh.ts` | Add the same pairing and connection keys. | +| MODIFY | `gui/src/i18n/zh-TW.ts` | Add the same pairing and connection keys. | | NEW | `gui/tests/api-targets.test.ts` | Target discovery, per-plane call-base selection, relay construction, and hub-down fallback. | | MODIFY | `gui/tests/api-auth-memory.test.ts` | Independent machine/shared sessions; direct/relay header matrix; no cross-target leakage; bootstrap validation. | | MODIFY | `gui/tests/api-auth-deadline.test.ts` | Per-target shared resolution/watchdog behavior. | @@ -299,7 +300,9 @@ slashes/backslashes, authority syntax, userinfo, query-host tricks, and path tra The caller supplies no host/scheme/port. `redirect: "manual"`; every 3xx is an error. Strip `Host`, connection/hop-by-hop headers, machine auth headers, proxy credentials, cookies, and forwarding headers. Forward only the bounded management header allowlist, -including hub session, GUI-origin, CSRF, content type, and conditional cache headers. +including hub session, GUI-origin, CSRF, content type, and conditional cache headers. For +the exact `POST /opencodex-session` exchange, forward the browser's `Origin` value verbatim; +do not synthesize it from the hub URL, localhost bind, or GUI-origin header. Response headers are similarly allowlisted; `Set-Cookie` and hop-by-hop headers are never returned. Request and response bodies have named constants and abort on overflow. No URL query, auth header, body, or response body is logged. @@ -448,13 +451,13 @@ appearing after disconnect. | Test file | Required cases | |---|---| | `tests/client-machine-listener.test.ts` (NEW) | IPv4 loopback bind; GUI/bootstrap; exact allowlist; every `/v1/*` 404; unknown/wrong-method 404; safe GET auth; mutation Origin/CSRF; status redaction; sync success/failure; shim actions; disconnect offline; recycle to standalone; invalid state refuses startup; no provider/timer fake invoked. | -| `tests/client-hub-relay.test.ts` (NEW) | Relay disabled 404; direct mode 404; fixed host/path; direct/encoded traversal and authority injection rejected; redirects rejected; request/response caps; hop-by-hop/cookie/forwarded/machine headers stripped; hub auth retained; timeout/abort; no body/header log. | +| `tests/client-hub-relay.test.ts` (NEW) | Relay disabled 404; direct mode 404; fixed host/path; exact `POST /api/machine/hub-relay/opencodex-session` reaches only hub `POST /opencodex-session` and forwards browser `Origin` verbatim; direct/encoded traversal and authority injection rejected; redirects rejected; request/response caps; hop-by-hop/cookie/forwarded/machine headers stripped; hub auth retained; timeout/abort; no body/header log. | | `tests/cli-start-journal-order.test.ts` | Matching durable client journal survives start; missing/mismatched client owner restores; connected branch never starts full server; disconnected branch remains current. | | `tests/api-usage.test.ts` | Exact `apiKeyId` response/echo; old and other-key rows excluded; no match; combined filters; filtered request cannot poison cache; unfiltered next request remains whole hub. | | `tests/usage-summary.test.ts` | Pure projection totals/days/models/providers/accounts consistency; exact-case id; combo attempts; absent id; provider/model/key cross-product. | | `tests/core-lab-boundary.test.ts` | Protected roots import no client subsystem; `startServer` remains non-async and no new top-level-window await. | | `gui/tests/api-targets.test.ts` (NEW) | Standalone 404 fallback; valid direct/relay status; per-plane call bases; machine-status network failure not standalone; encoded relay paths. | -| `gui/tests/api-auth-memory.test.ts` | Two simultaneous sessions; direct headers; relay dual headers; machine custom stripping contract; cross-target 401 races; server/browser-origin mismatch; unknown target receives nothing; no web storage. | +| `gui/tests/api-auth-memory.test.ts` | Two simultaneous sessions; direct headers; relay dual headers; pasted pairing code exchanges through the selected shared target and stores only the returned shared session in memory; machine custom stripping contract; cross-target 401 races; server/browser-origin mismatch; unknown target receives nothing; no web storage. | | `gui/tests/api-auth-deadline.test.ts` | One target watchdog does not block/clear the other; direct and relay bootstrap timeout states. | | `gui/tests/usage-layout.test.ts` | Connected default key query; hub-wide omission; disconnected local query; source-qualified cache keys; hub-down no local fetch; scope labels/a11y. | | `gui/tests/app-stop.test.ts` | Standalone `/api/stop`; connected `/api/machine/disconnect`; refusal re-enables action; accepted recycle tolerates connection drop. | @@ -477,6 +480,7 @@ appearing after disconnect. | P4-A11 | Standalone full server opens the same GUI. | `/api/machine/status` 404 selects same-origin targets; all current pages, auth, stop, usage, and injector output remain unchanged. | | P4-A12 | Shared direct session 401s while machine session is renewed (and inverse). | Each target resolves/clears only its own in-memory state; no token crosses target and no prompt fan-out occurs. | | P4-A13 | GUI PR is prepared for review. | PR description uses the repository template and includes screenshots of connected this-machine Usage plus hub-offline machine shell (or a maintainer-approved `gui-screenshot-waived` exception). | +| P4-A14 | Relay-connected GUI has no hub session; user pastes a fresh Phase-2 pairing code and submits the pairing form. | `gui/src/connect-pairing.ts` sends the exact POST exchange through `/api/machine/hub-relay/opencodex-session`, the relay forwards browser `Origin` verbatim, and the returned session/CSRF/origins are stored only in the shared target's in-memory slot. | ## 9. Verification — remote only on `lidge-ai` diff --git a/devlog/_plan/260827_remote_hub/070_phase5_deploy.md b/devlog/_plan/260827_remote_hub/070_phase5_deploy.md index 78f64c1059..8b84ddf0fe 100644 --- a/devlog/_plan/260827_remote_hub/070_phase5_deploy.md +++ b/devlog/_plan/260827_remote_hub/070_phase5_deploy.md @@ -356,8 +356,8 @@ evidence; the sketch above is setup, not exact-head proof. ### 8.2 MacBook connect and remote session -1. On the hub, run `ocx gui pair` and copy the single-use, short-TTL code through the - interactive channel. Do not record it. +1. On the hub, run `ocx gui pair --origin http://localhost:10100` and copy the single-use, + short-TTL code through the interactive channel. Do not record it. 2. On the MacBook, run the Phase-3 connect command with exactly one transient `--pairing-code-stdin` or `--admin-token-stdin`; it must not accept a literal secret flag. 3. Assert `ocx connect status --json` reports protocol v1, hub URL, management URL, diff --git a/devlog/_plan/260827_remote_hub/080_phase6_hardening.md b/devlog/_plan/260827_remote_hub/080_phase6_hardening.md index f0d112cfbf..1ba9102bcf 100644 --- a/devlog/_plan/260827_remote_hub/080_phase6_hardening.md +++ b/devlog/_plan/260827_remote_hub/080_phase6_hardening.md @@ -16,9 +16,10 @@ the workstation. Full-suite execution is serialized with other `lidge-ai` suite - Recoverable per-client data-key rotation with a one-time secret response, bounded overlap, client-side atomic token-file replacement, explicit commit/abort, and stable `apiKeyId` usage attribution. -- Remote-session self-logout plus explicit operator data-key revocation from the hub GUI or - `ocx connect revoke` with an admin credential. Disconnect performs no hub-side revocation and - remains available while the hub is offline. +- Remote-session self-logout plus explicit operator data-key revocation from the hub GUI or, + only while connected, `ocx connect revoke` with an admin credential. Disconnect performs no + hub-side revocation, remains available while the hub is offline, and leaves the hub GUI as the + sole post-disconnect revocation path. - Pairing issuance/redemption limits, one-use semantics, bounded active state, and safe 429s. - Protocol negotiation matrix tests covering the v1 compatibility floor and feature detection. - Adversarial `/v1/catalog` consumer tests for decompressed size, malformed JSON, invalid schema, @@ -53,7 +54,7 @@ the workstation. Full-suite execution is serialized with other `lidge-ai` suite | Header-smuggling attempt | Hub request parser/proxy chain | Reject transfer-encoding, conflicting content-length, connection-nominated headers, CR/LF values, and upgrade paths. | | Protocol-skewed peer | Local client files / silent misroute | Negotiate before any local write; reject incompatible floors with explicit upgrade error; unknown features stay off. | | Rotation crash between hub and client | Client availability | Old and pending keys overlap for a bounded window; commit only after new-key probe; abort/expiry preserves old key. | -| Disconnected client whose data key still exists | Hub data admission | Disconnect changes local state only; an operator explicitly deletes the key in the hub GUI or runs `ocx connect revoke --admin-token-stdin`. | +| Disconnected client whose data key still exists | Hub data admission | Disconnect changes local state only and reminds the operator to delete the key from the hub GUI's **Integrations → API Keys** page; that GUI is the sole post-disconnect revocation path. | | Logs/evidence | Tokens, codes, identities | Record ids/prefixes/counts/status only; privacy scan; no raw secret, email, Origin query, request body, or account id. | Security level: ASVS L2 for the remote management/session surface. Applicable architecture, @@ -81,10 +82,11 @@ a second owner. | `src/server/management-api.ts` | MODIFY | Wire `handleSessionRoutes` and the narrow session-control dependency into `ManagementContext`. | | `src/server/management/context.ts` | MODIFY | Carry only the current-session logout interface, never the raw admin token or session map. | | `src/server/management-auth.ts` | MODIFY | Export a narrow current-session invalidation helper for explicit self-logout; preserve one shared auth predicate and add no key binding to pairing grants or sessions. | -| `src/client/connect.ts` | MODIFY | Implement `ocx connect rotate`: transient authority, start rotation, atomically replace token file, validate new key, commit, and restore+abort on failure. | +| `src/client/connect.ts` | MODIFY | Implement `ocx connect rotate`: transient authority, start rotation, atomically replace token file, validate new key, commit, and restore+abort on failure; allow `ocx connect revoke` only while connected and source its id solely from persisted `apiKeyId`. | | `src/client/hub-client.ts` | MODIFY (Phase-3 owner) | Add bounded rotation/revoke management calls, authenticated catalog key-id probe, and redacted errors beside the existing ready/key/catalog calls. | -| `src/client/state.ts` | MODIFY | Persist `pendingOperation: {kind:"rotate",newKeyIssuedAt,oldKeyBackupPath}` through the full backup/replace/probe/commit or recovery chain; never persist admin/pairing authority or old/new secret. | -| `src/cli/connect.ts` | MODIFY (Phase-3 owner) | Parse `rotate` and operator-only `revoke`, enforce exact stdin flags, reject literal/env secret forms, and render redacted recovery status. | +| `src/client/state.ts` | MODIFY | Validate and persist `pendingOperation: {kind:"rotate",rotationId,newKeyIssuedAt,oldKeyBackupPath}` through the full backup/replace/probe/commit or recovery chain; never persist admin/pairing authority or old/new secret. | +| `src/cli/connect.ts` | MODIFY (Phase-3 owner) | Parse `rotate` and connected-only operator `revoke`, enforce exact stdin flags, reject literal/env secret/id forms, and render redacted recovery status. | +| `src/lib/service-secrets.ts` | MODIFY (Phase-3 owner) | Own `.prev` creation and restoration as `writeTokenBackup` / `restoreTokenBackup`, reusing the token file's owner-only, regular-file, fsync, and atomic-replace rules. | | `src/cli/access.ts` | MODIFY | Add management-side `ocx access key rotate ` start/commit/abort UX with one-time secret warning; no literal secret flags. | | `src/cli/registry.ts` | MODIFY | Document rotation command shapes and transient-authority requirement. | | `gui/src/pages/ApiKeys.tsx` | MODIFY | Load pending status, start/commit/abort rotation, and render the new secret exactly once. | @@ -102,7 +104,8 @@ a second owner. | `tests/data-plane-admission-identity.test.ts` | MODIFY | Current and pending secret map to one id; expired/committed/aborted secrets do not admit. | | `tests/api-key-attribution.test.ts` | MODIFY | Traffic before/during/after rotation remains one `apiKeyId` bucket. | | `tests/server-management-auth.test.ts` | MODIFY | Explicit session self-logout, absence of key-bound grant/session invalidation, and admin-token consent refusal. | -| `tests/client-connect.test.ts` | MODIFY (Phase-3 owner) | Rotation/revoke parser flags, `.prev` crash recovery, pending-operation lifecycle, uncertain commit, and operator-only key deletion. | +| `tests/client-connect.test.ts` | MODIFY (Phase-3 owner) | Rotation/revoke parser flags, issued `apiKeyId` state chain, connected-only revoke/disconnected refusal, `.prev` crash recovery, pending-operation lifecycle, doubly-accepted commit, uncertain commit, and operator-only key deletion. | +| `tests/service-secrets.test.ts` | MODIFY (Phase-3 owner) | `writeTokenBackup` / `restoreTokenBackup` exact-path, owner-only mode/ACL, symlink/refusal, fsync/atomic replacement, and redacted failure cases. | | `gui/tests/apikeys-actions.test.tsx` | MODIFY | Start/commit/abort wire actions and one-time secret handling. | | `gui/tests/apikeys-mutation-timeout.test.tsx` | MODIFY | Rotation controls recover after bounded network failure. | | `gui/tests/apikeys-workspace.test.tsx` | MODIFY | Accessible rendered states and confirmations. | @@ -133,7 +136,7 @@ a second owner. | Path | Change | Exact responsibility | | --- | --- | --- | | `structure/01_runtime.md` | MODIFY | Final hub/client protocol, listener, catalog, and relay ownership map. | -| `structure/02_config-and-codex-home.md` | MODIFY | Client token-file ownership, rotation overlap, disconnect deletion, and no usage mirroring. | +| `structure/02_config-and-codex-home.md` | MODIFY | Client token-file ownership, rotation overlap, disconnect reminder plus hub-GUI-only post-disconnect revocation, and no usage mirroring. | | `structure/05_gui-and-management-api.md` | MODIFY | Final credential classes, issuance ladder, revocation, rate limits, origin/CSRF, and admin consent refusal. | | `structure/06_docs-and-release.md` | MODIFY | Correct the locale inventory and record the remote-hub release gate. | | `structure/09_client-integrations.md` | MODIFY | Remote connection journal/restore, direct data path, fixed relay, and launcher-scoped Claude behavior. | @@ -269,10 +272,26 @@ No response except successful start contains the pending secret. `ocx connect rotate` requires one transient `--pairing-code-stdin` or `--admin-token-stdin`; neither is persisted. It performs: +```ts +pendingOperation?: { + kind: "rotate"; + rotationId: string; + newKeyIssuedAt: string; + oldKeyBackupPath: string; +}; +``` + +The Phase-3 client-config reader validates all four fields before recovery can run. +`src/lib/service-secrets.ts` is the sole `.prev` I/O owner through +`writeTokenBackup` and `restoreTokenBackup`; the coordinator does not open, chmod, copy, +or replace the backup directly. + 1. Read current key id and current token into memory; write the old token to - `.prev` with the same owner-only 0600/ACL rules and fsync it. + `.prev` through `writeTokenBackup`, with the same owner-only 0600/ACL rules + and fsync it. 2. Start rotation; receive pending secret once, then persist - `pendingOperation: {kind:"rotate",newKeyIssuedAt,oldKeyBackupPath}` before replacement. + `pendingOperation: {kind:"rotate",rotationId,newKeyIssuedAt,oldKeyBackupPath}` before + replacement. 3. Write pending secret to a same-directory owner-only temp, harden it with the same `serviceApiTokenFilePath()` rules, fsync, and atomically replace the token file. 4. Probe authenticated `/v1/catalog` with the new key and verify the expected client key id via @@ -280,16 +299,19 @@ No response except successful start contains the pending secret. 5. Commit rotation. Commit invalidates old-key admission only; pairing grants and GUI sessions are not key-bound. An already-admitted in-flight turn may complete; the next old-key request is 401. After verified commit, delete `.prev` and clear `pendingOperation`. -6. If steps 2–5 fail before a confirmed commit, restore the old token atomically from - `.prev`, abort the pending rotation, then delete the backup and clear the operation. - If commit outcome is uncertain, probe with both keys: exactly one accepted result determines - the local file; never replay commit blindly. +6. If steps 2–5 fail before a confirmed commit, restore the old token atomically through + `restoreTokenBackup`, abort the pending rotation, then delete the backup and clear the + operation. If commit outcome is uncertain, probe with both keys. New+old both accepted means + issuance completed and overlap is still pending, so commit the new key with the stored + `rotationId`; new-only accepted means commit already took effect; old-only accepted restores + and aborts. Never replay commit without this evidence. On startup/status, `src/client/state.ts` treats a rotate `pendingOperation` as a recovery gate: verify that `oldKeyBackupPath` is exactly `.prev`, require an owner-only regular file, probe current and backup keys using the authenticated catalog key-id echo, then complete the same -commit-or-restore chain. Missing, unsafe, or doubly accepted/rejected evidence stops with an exact -recovery instruction and never deletes either candidate blindly. +commit-or-restore chain. Doubly accepted evidence commits the current new key with the persisted +`rotationId`, deletes `.prev`, and clears `pendingOperation`; doubly rejected, missing, or unsafe +evidence stops with an exact recovery instruction and never deletes either candidate blindly. The GUI exposes the same lifecycle for an operator updating a client manually, with explicit copy-once and commit-after-client-probe wording. Closing the modal does not imply commit; pending @@ -315,12 +337,13 @@ export function createManagementSessionControl( `POST /api/session/logout` requires `principal === "gui-session"`, same browser Origin, and CSRF. Admin-token calls return 403. -`ocx disconnect` performs local restore only and sends no hub revocation request. To revoke the -still-valid data key, an operator uses the hub GUI's existing key deletion or -`ocx connect revoke --admin-token-stdin`; the CLI requires the transient admin credential, -deletes the exact configured key id, and clears no local state unless deletion is confirmed. -Explicit GUI self-logout remains available independently. A hub-down disconnect succeeds locally -and reports that operator revocation remains outstanding. +`ocx connect revoke --admin-token-stdin` exists only while connected: it requires valid connected +state, reads the exact `apiKeyId` copied from issuance into that state, accepts no id override, and +uses the transient admin credential to delete that key. Disconnected, invalid, or mismatched state +fails before a hub request. `ocx disconnect` performs local restore only and sends no hub revocation +request; its output names the hub GUI's **Integrations → API Keys** page and reports that revocation +remains outstanding. Once disconnect clears client state, that hub GUI page is the sole revocation +path. Explicit GUI self-logout remains available independently. ## 5. Pairing rate limits @@ -486,9 +509,10 @@ Phase 6 extends them rather than creating parallel “hardening2” files. | second start | Existing unexpired pending rotation | 409; no third secret/state change. | | client commit | New key written + authenticated catalog succeeds | Pending promoted atomically; old next request 401; id/usage bucket stable; `.prev` deleted and pending operation cleared; sessions/grants unchanged. | | client write/probe fail | Fail backup/temp write, hardening, rename, or new-key probe | Old file restored/unchanged from `.prev`; pending aborted or expires; old key remains valid. | -| uncertain commit/crash | Drop commit response or restart with pending operation | Probe current+`.prev`; choose sole accepted key; finish commit-or-restore chain; no blind replay. | +| uncertain commit/crash | Drop commit response or restart with pending operation | Probe current+`.prev`; both accepted commits the current new key with stored `rotationId`, new-only finalizes committed state, old-only restores+aborts, and both rejected stops without deletion. | | pending expiry | Fake clock past 10 minutes | Pending rejected/removed; old accepted. | -| operator revoke | Hub GUI delete or `ocx connect revoke --admin-token-stdin` for configured id | Data-key admission revoked after persistence; sessions/grants unchanged; disconnect alone made no hub request. | +| connected operator revoke | Valid connected state + `ocx connect revoke --admin-token-stdin` | CLI reads the issuance-derived `apiKeyId` from state, accepts no id argument, and revokes that key; sessions/grants remain unchanged. | +| post-disconnect revoke | Disconnect clears local client state while its hub key remains | CLI revoke refuses before any request; output points to hub GUI **Integrations → API Keys**, the sole post-disconnect revocation path. | | self logout | GUI session + Origin + CSRF | Current session removed; replay 401. Admin-token call 403. | | pairing bad guesses | Same grant/source repeated with fake clock | Fifth grant failure burns; source threshold yields 429 + bounded Retry-After; no identity leak. | | pairing replay/race | Two concurrent valid redemptions | Exactly one session; other generic failure. | @@ -567,8 +591,8 @@ the same readiness report. - [ ] Per-client rotation is recoverable through `pendingOperation` + `.prev`, bounded, stable-id-attributed, secret-safe, and invalidates only old-key admission after commit. -- [ ] Session self-logout, local-only disconnect, and explicit operator revoke behavior are - distinct; admin-token consent remains 403. +- [ ] Session self-logout, local-only disconnect, connected-only CLI revoke, and hub-GUI-only + post-disconnect revoke are distinct; admin-token consent remains 403. - [ ] Pairing attempt and capacity state are bounded, deterministic under fake time, one-use under races, and privacy-safe. - [ ] Every protocol matrix row is reachable and proves no-write behavior before incompatibility. @@ -605,6 +629,8 @@ ssh lidge-ai "set -eu tests/data-plane-admission-identity.test.ts \ tests/api-key-attribution.test.ts \ tests/server-management-auth.test.ts \ + tests/client-connect.test.ts \ + tests/service-secrets.test.ts \ tests/remote-catalog.test.ts \ tests/client-hub-relay.test.ts \ tests/bounded-body.test.ts \ From 3d983d5f1338e1087fa46e03f9b21a6a725bb004 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 01:47:25 +0900 Subject: [PATCH 023/172] feat(remote): serve authenticated catalog snapshots --- src/server/auth-cors.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/server/auth-cors.ts b/src/server/auth-cors.ts index 0174794c6a..2bffde448c 100644 --- a/src/server/auth-cors.ts +++ b/src/server/auth-cors.ts @@ -403,6 +403,7 @@ export const AUTH_MATRIX: readonly ApiAuthMatrixRow[] = [ // passthrough, so the two bearer domains still never mix. `x-api-key` is still rejected. { endpoint: "/v1/responses", bearer: "accepted", dedicated: "accepted", xApiKey: "rejected" }, { endpoint: "/v1/chat/completions", bearer: "accepted", dedicated: "accepted", xApiKey: "rejected" }, + { endpoint: "/v1/catalog", bearer: "accepted", dedicated: "accepted", xApiKey: "rejected" }, { endpoint: "/v1/messages", bearer: "accepted", dedicated: "accepted", xApiKey: "accepted" }, { endpoint: "/v1/models", bearer: "accepted", dedicated: "accepted", xApiKey: "accepted" }, // #809: least-privilege catalog read for remote Codex clients. Same admission set as From a938448d306b271ea8f7511cb6514a1a7ef131ec Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 01:36:13 +0900 Subject: [PATCH 024/172] =?UTF-8?q?docs(devlog):=20fold=20roadmap=20audit?= =?UTF-8?q?=20r3=20=E2=80=94=20pairing=20form=20mount=20chain=20+=20orphan?= =?UTF-8?q?=20.prev=20crash-window=20recovery?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../_plan/260827_remote_hub/060_phase4_two_plane.md | 5 +++-- .../_plan/260827_remote_hub/080_phase6_hardening.md | 13 +++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md b/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md index b456ed32d9..1297d19d9d 100644 --- a/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md +++ b/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md @@ -105,7 +105,7 @@ parent exists. No generated `gui/dist` file is edited. | NEW | `gui/src/api-targets.ts` | Canonical `ApiTargets`, machine-status discovery, per-plane call-base selection, relay URL construction, and disconnected fallback. | | NEW | `gui/src/connect-pairing.ts` | Own the visible pairing-code form and activation flow: paste a one-time code, POST the exact `/opencodex-session` exchange through the selected direct/relay shared target, and install the returned session only in the shared target's in-memory auth slot. | | MODIFY | `gui/src/api.ts` | Replace `needsApiAuth`'s one same-origin slot with exact target classification and per-target in-memory session/CSRF state; attach both auth domains only on relay. | -| MODIFY | `gui/src/App.tsx` | Discover targets before page fetches; supply both call bases instead of one page base; machine health remains live when hub is down; connected stop becomes disconnect/recycle. | +| MODIFY | `gui/src/App.tsx` | Discover targets before page fetches; supply both call bases instead of one page base; machine health remains live when hub is down; connected stop becomes disconnect/recycle; import and MOUNT the `connect-pairing` form in the connected-without-hub-session state (banner slot above page content) so the pairing UI is reachable, not just defined. | | MODIFY | `gui/src/stop-proxy.ts` | Add mode-aware machine disconnect request while preserving existing standalone `/api/stop` behavior. | | MODIFY | `gui/src/pages/Startup.tsx` | Keep existing settings/startup-health/windows-tray/startup-action calls on the shared base; use the machine base only for new `/api/machine/*` status/shim sections. | | MODIFY | `gui/src/pages/Integrations.tsx` | Pass the shared base to all existing integration descendants, including ApiKeys and Grok; pass the machine base only to new local-client controls. | @@ -458,6 +458,7 @@ appearing after disconnect. | `tests/core-lab-boundary.test.ts` | Protected roots import no client subsystem; `startServer` remains non-async and no new top-level-window await. | | `gui/tests/api-targets.test.ts` (NEW) | Standalone 404 fallback; valid direct/relay status; per-plane call bases; machine-status network failure not standalone; encoded relay paths. | | `gui/tests/api-auth-memory.test.ts` | Two simultaneous sessions; direct headers; relay dual headers; pasted pairing code exchanges through the selected shared target and stores only the returned shared session in memory; machine custom stripping contract; cross-target 401 races; server/browser-origin mismatch; unknown target receives nothing; no web storage. | +| `gui/tests/connect-pairing.test.ts` (NEW) | RENDERED form test: connected-without-hub-session state mounts the pairing form from App; submitting a pasted code fires the exact POST exchange; success hides the form and populates the shared auth slot; failure renders the error state without clearing the input. | | `gui/tests/api-auth-deadline.test.ts` | One target watchdog does not block/clear the other; direct and relay bootstrap timeout states. | | `gui/tests/usage-layout.test.ts` | Connected default key query; hub-wide omission; disconnected local query; source-qualified cache keys; hub-down no local fetch; scope labels/a11y. | | `gui/tests/app-stop.test.ts` | Standalone `/api/stop`; connected `/api/machine/disconnect`; refusal re-enables action; accepted recycle tolerates connection drop. | @@ -480,7 +481,7 @@ appearing after disconnect. | P4-A11 | Standalone full server opens the same GUI. | `/api/machine/status` 404 selects same-origin targets; all current pages, auth, stop, usage, and injector output remain unchanged. | | P4-A12 | Shared direct session 401s while machine session is renewed (and inverse). | Each target resolves/clears only its own in-memory state; no token crosses target and no prompt fan-out occurs. | | P4-A13 | GUI PR is prepared for review. | PR description uses the repository template and includes screenshots of connected this-machine Usage plus hub-offline machine shell (or a maintainer-approved `gui-screenshot-waived` exception). | -| P4-A14 | Relay-connected GUI has no hub session; user pastes a fresh Phase-2 pairing code and submits the pairing form. | `gui/src/connect-pairing.ts` sends the exact POST exchange through `/api/machine/hub-relay/opencodex-session`, the relay forwards browser `Origin` verbatim, and the returned session/CSRF/origins are stored only in the shared target's in-memory slot. | +| P4-A14 | Relay-connected GUI has no hub session; user pastes a fresh Phase-2 pairing code and submits the pairing form. | The form is MOUNTED from `gui/src/App.tsx` in that state (rendered test `gui/tests/connect-pairing.test.ts`), `gui/src/connect-pairing.ts` sends the exact POST exchange through `/api/machine/hub-relay/opencodex-session`, the relay forwards browser `Origin` verbatim, and the returned session/CSRF/origins are stored only in the shared target's in-memory slot. | ## 9. Verification — remote only on `lidge-ai` diff --git a/devlog/_plan/260827_remote_hub/080_phase6_hardening.md b/devlog/_plan/260827_remote_hub/080_phase6_hardening.md index 1ba9102bcf..98375890ef 100644 --- a/devlog/_plan/260827_remote_hub/080_phase6_hardening.md +++ b/devlog/_plan/260827_remote_hub/080_phase6_hardening.md @@ -86,7 +86,7 @@ a second owner. | `src/client/hub-client.ts` | MODIFY (Phase-3 owner) | Add bounded rotation/revoke management calls, authenticated catalog key-id probe, and redacted errors beside the existing ready/key/catalog calls. | | `src/client/state.ts` | MODIFY | Validate and persist `pendingOperation: {kind:"rotate",rotationId,newKeyIssuedAt,oldKeyBackupPath}` through the full backup/replace/probe/commit or recovery chain; never persist admin/pairing authority or old/new secret. | | `src/cli/connect.ts` | MODIFY (Phase-3 owner) | Parse `rotate` and connected-only operator `revoke`, enforce exact stdin flags, reject literal/env secret/id forms, and render redacted recovery status. | -| `src/lib/service-secrets.ts` | MODIFY (Phase-3 owner) | Own `.prev` creation and restoration as `writeTokenBackup` / `restoreTokenBackup`, reusing the token file's owner-only, regular-file, fsync, and atomic-replace rules. | +| `src/lib/service-secrets.ts` | MODIFY (Phase-3 owner) | Own `.prev` creation, restoration, and orphan handling as `writeTokenBackup` / `restoreTokenBackup` / `removeOrphanTokenBackup`, reusing the token file's owner-only, regular-file, fsync, and atomic-replace rules. | | `src/cli/access.ts` | MODIFY | Add management-side `ocx access key rotate ` start/commit/abort UX with one-time secret warning; no literal secret flags. | | `src/cli/registry.ts` | MODIFY | Document rotation command shapes and transient-authority requirement. | | `gui/src/pages/ApiKeys.tsx` | MODIFY | Load pending status, start/commit/abort rotation, and render the new secret exactly once. | @@ -105,7 +105,7 @@ a second owner. | `tests/api-key-attribution.test.ts` | MODIFY | Traffic before/during/after rotation remains one `apiKeyId` bucket. | | `tests/server-management-auth.test.ts` | MODIFY | Explicit session self-logout, absence of key-bound grant/session invalidation, and admin-token consent refusal. | | `tests/client-connect.test.ts` | MODIFY (Phase-3 owner) | Rotation/revoke parser flags, issued `apiKeyId` state chain, connected-only revoke/disconnected refusal, `.prev` crash recovery, pending-operation lifecycle, doubly-accepted commit, uncertain commit, and operator-only key deletion. | -| `tests/service-secrets.test.ts` | MODIFY (Phase-3 owner) | `writeTokenBackup` / `restoreTokenBackup` exact-path, owner-only mode/ACL, symlink/refusal, fsync/atomic replacement, and redacted failure cases. | +| `tests/service-secrets.test.ts` | MODIFY (Phase-3 owner) | `writeTokenBackup` / `restoreTokenBackup` / `removeOrphanTokenBackup` exact-path, owner-only mode/ACL, symlink/refusal, fsync/atomic replacement, orphan crash-window cases (crash BEFORE marker persistence → orphan removed; crash AFTER → recovery gate runs), and redacted failure cases. | | `gui/tests/apikeys-actions.test.tsx` | MODIFY | Start/commit/abort wire actions and one-time secret handling. | | `gui/tests/apikeys-mutation-timeout.test.tsx` | MODIFY | Rotation controls recover after bounded network failure. | | `gui/tests/apikeys-workspace.test.tsx` | MODIFY | Accessible rendered states and confirmations. | @@ -292,6 +292,15 @@ or replace the backup directly. 2. Start rotation; receive pending secret once, then persist `pendingOperation: {kind:"rotate",rotationId,newKeyIssuedAt,oldKeyBackupPath}` before replacement. + + Crash window between steps 1 and 2: a `.prev` file with NO persisted `pendingOperation` + is an orphan duplicate of the still-active secret. Startup/status therefore checks the + inverse gate too: `.prev` present + no rotate `pendingOperation` → the rotation never + started on the hub, the live token file is authoritative — call + `removeOrphanTokenBackup` (owner-only unlink with the same symlink/regular-file + refusals) and log one redacted line. Activation scenario: kill the CLI between backup + write and marker persistence; next `ocx connect status` removes the orphan and reports + clean state (covered in tests/service-secrets.test.ts and tests/client-connect.test.ts). 3. Write pending secret to a same-directory owner-only temp, harden it with the same `serviceApiTokenFilePath()` rules, fsync, and atomically replace the token file. 4. Probe authenticated `/v1/catalog` with the new key and verify the expected client key id via From 1bb9828a17aa1c51b553f290994d6383829e8c83 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 01:51:44 +0900 Subject: [PATCH 025/172] fix(remote): derive management origin from request host --- src/remote/protocol.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/remote/protocol.ts b/src/remote/protocol.ts index de366d5e53..71ee0c10b5 100644 --- a/src/remote/protocol.ts +++ b/src/remote/protocol.ts @@ -32,11 +32,21 @@ function managementOrigin(value: unknown): string | null { } } +function observedManagementOrigin(req: Request): string | null { + try { + const requestUrl = new URL(req.url); + const host = req.headers.get("Host") ?? requestUrl.host; + return managementOrigin(`${requestUrl.protocol}//${host}`); + } catch { + return null; + } +} + export function readyProtocolMetadata(config: OcxConfig, req: Request): RemoteReadyMetadata { // Phase 2 will consult config.hub.managementPublicOrigin here. Keeping the // parameter now fixes the consumer signature without changing Phase 1 behavior. void config; - const managementUrl = managementOrigin(new URL(req.url).origin); + const managementUrl = observedManagementOrigin(req); if (!managementUrl) throw new Error("Readiness request does not have an HTTP(S) management origin"); return { protocol: REMOTE_HUB_PROTOCOL, From 5eb58b7e2cf72e207f310d4241ea702fd330b55e Mon Sep 17 00:00:00 2001 From: jun Date: Tue, 1 Sep 2026 16:41:41 +0900 Subject: [PATCH 026/172] fix(design): close five trust-boundary defects in the remote hub contract Rebased onto current dev and repaired the contract defects the review raised on the previous head. D1 - remove insecure-http-pairing. A reusable pairing grant crossing non-loopback plaintext HTTP is captured verbatim by a passive observer, and the config opt-in gating it could not bound the risk it recorded. The "bootstrap over HTTP then upgrade" variant is rejected too: the plaintext hop has no trust anchor, so an on-path attacker substitutes its own valid HTTPS origin and the upgrade authenticates the attacker. remoteGui.allowInsecureHttp is deleted and a persisted true is dropped with a warning. D2 - /v1/catalog emits no ETag and never answers 304. The response varies by key type and key id, so a shared strong validator lets a store revalidate one identity's representation for another; private, no-cache does not prevent storage, and the revalidation is what crosses identities. /api/catalog keeps its validator because it is loopback-scoped and identity-invariant. D3 - forward the browser Origin verbatim on every allowed session-authenticated request, not only POST /opencodex-session. The session is origin-bound and mutations enforce Origin/CSRF, so relayed writes were losing the evidence the hub requires. Synthesizing an Origin is refused: the relay would attest to something it never observed and the hub would validate the relay against itself. D4 - compare candidate identities before probing during rotation recovery. A crash after pendingOperation is persisted but before the token is replaced leaves both files holding the old key, where both probe successfully; the old "both accepted implies commit" rule read that as a completed rotation and lost the new key permanently. Identical candidates now mean pre-replacement: never commit, resume instead. Unconfirmed abort or restore retains evidence rather than installing a guessed generation. D5 - relayed session, bootstrap, and management responses are rewritten to no-store with ETag and Last-Modified stripped. Preserving an upstream validator reintroduced D2 one layer up, at exactly the position where an intermediary cache is most likely to sit. Also corrects the 000_research framing of the remote gui-session limitation. It is a deliberate fail-closed restriction already visible in shipped code and documented publicly, not an unreported weakness, so calling it a defect invited the wrong reading of what belongs in a public devlog. --- .../_plan/260827_remote_hub/000_research.md | 24 ++++++--- .../030_phase1_protocol_catalog.md | 45 +++++++++++------ .../040_phase2_remote_session.md | 48 +++++++++++++----- .../260827_remote_hub/060_phase4_two_plane.md | 23 +++++++-- .../260827_remote_hub/080_phase6_hardening.md | 50 ++++++++++++++++--- 5 files changed, 147 insertions(+), 43 deletions(-) diff --git a/devlog/_plan/260827_remote_hub/000_research.md b/devlog/_plan/260827_remote_hub/000_research.md index 18d5404bf6..6bbd687de3 100644 --- a/devlog/_plan/260827_remote_hub/000_research.md +++ b/devlog/_plan/260827_remote_hub/000_research.md @@ -17,14 +17,25 @@ the design must fix remote GUI operability without collapsing the consent bounda - Non-loopback bind forces the data token: `isApiAuthRequired` returns true whenever the bind hostname is not loopback (src/server/auth-cors.ts:260-262), and startup refuses a public bind without a configured data credential. -- The reported remote-GUI defect is real and structural: `issueGuiSession` returns null - when `isApiAuthRequired(config)` is true AND additionally requires a loopback Host - (src/server/management-auth.ts, issueGuiSession). So on a remote bind the principal - `gui-session` is unobtainable; consent-bearing routes that require +- The remote-GUI limitation is a deliberate restriction, not an unreported weakness, and + it is already visible in shipped public code: `issueGuiSession` returns null when + `isApiAuthRequired(config)` is true and additionally requires a loopback Host + (src/server/management-auth.ts, `issueGuiSession`). The published dashboard guide + states the same boundary in user terms. + + The consequence is a capability gap rather than an exposure: on a remote bind the + principal `gui-session` is unobtainable, so consent-bearing routes requiring `ctx.principal === "gui-session"` (src/server/management/sidebar-routes.ts:42, src/server/management/codex-prompt-routes.ts:298) answer 403 even to the admin token. - That 403 is BY DESIGN for the admin token (AGENTS.md user-consent boundary) — the defect - is only that a browser can never mint a session remotely. + That 403 is correct and stays correct — the admin token must never be able to spend the + user's consent (AGENTS.md user-consent boundary). What is missing is any path for a + *browser* to mint a session remotely, which is what this unit designs. + + Stated precisely: the current behavior fails closed. Nothing here describes a way to + obtain authority one should not have, so this note is a design rationale rather than + pre-disclosure material, and `AGENTS.md`'s scratch-space rule for unfixed defects does + not apply to it. Anything in this unit that WOULD describe an unfixed exploitable + weakness belongs in scratch space, not in `devlog/`. - `managementRequestOrigin` returns null for a non-loopback Host when apiAuth is NOT required (src/server/auth-cors.ts:118-129); when apiAuth IS required it derives the origin from the request, which a TLS terminator breaks (http observed vs https public). @@ -92,4 +103,3 @@ Browser platform facts (MDN/WHATWG/IETF, opened 2026-08-27): 4. localhost:10100 client GUI + direct-to-hub shared plane is cross-origin; the hub needs management CORS for an allowlisted client origin, or the client listener relays. Both appear in 010 with the relay constrained to a fixed target. - diff --git a/devlog/_plan/260827_remote_hub/030_phase1_protocol_catalog.md b/devlog/_plan/260827_remote_hub/030_phase1_protocol_catalog.md index 171b99a841..5ea1d99d5a 100644 --- a/devlog/_plan/260827_remote_hub/030_phase1_protocol_catalog.md +++ b/devlog/_plan/260827_remote_hub/030_phase1_protocol_catalog.md @@ -27,17 +27,31 @@ runtime behavior. All code paths remain standalone-compatible when `runtimeRole` changes only its source for hub deployments by preferring `hub.managementPublicOrigin`; the field and parser do not change. - Data-authenticated exact `GET /v1/catalog` returns the same serialized catalog bytes as - `GET /api/catalog`, with a strong ETag calculated from those bytes and conditional - `If-None-Match` support. + `GET /api/catalog`. It carries **no ETag and no conditional `If-None-Match` support**, + and never answers 304. + + A previous revision gave this response a strong ETag derived from the bytes plus + `Cache-Control: private, no-cache`, while also varying the body-adjacent + `x-opencodex-key-id` by identity. That pairing is unsafe: a strong validator asserts + that one entity-tag names one representation, but the representation here varies by + key type and key id. Any store that keys on URL plus validator — a shared intermediary, + a client cache reused across key rotation, a future hub relay — can serve or revalidate + one identity's representation to another. `no-cache` does not prevent storage; it only + forces revalidation, and the revalidation itself is what crosses identities. + + Making the validator safe would require an identity-partitioned cache key and validator + proven across every store in the path, including ones we do not control. That proof is + more expensive than the bandwidth a 304 saves on a catalog this size, so the design + does not attempt it. - `/v1/catalog` admits only the two forms used by the Codex injector contract: `x-opencodex-api-key: ` or `Authorization: Bearer `. `x-api-key`, a foreign bearer, the admin token, and no credential are rejected on a non-loopback bind. The row is added to `AUTH_MATRIX` with `xApiKey: "rejected"`. - A `/v1/catalog` response admitted by a configured key includes `x-opencodex-key-id` with - that key's id on both 200 and 304. Environment-token and loopback paths, plus rejected + that key's id on the 200. Environment-token and loopback paths, plus rejected and unauthenticated responses, never include this header. Revalidate the id at emission as `^[A-Za-z0-9._-]{1,64}$`; on mismatch omit the header and log one non-secret warning. - Every successful catalog response includes `Cache-Control: private, no-cache`. + Every successful catalog response includes `Cache-Control: no-store` and no validator. - A serialized catalog larger than `MAX_REMOTE_CATALOG_BYTES = 32 * 1024 * 1024` is not returned over `/v1/catalog`; it fails with HTTP 503 and the stable code `catalog_too_large`. The management route continues to expose the same serialized bytes @@ -124,15 +138,18 @@ One function reads the current catalog, serializes it exactly once with result. The test oracle compares the two route bodies byte-for-byte; it does not derive an expected body by calling the serializer twice. -The ETag is `"sha256-"` over the exact UTF-8 response bytes. A matching -strong tag, weak spelling of that same tag, a comma-list containing it, or `*` returns 304 -with ETag and no body. A stale/malformed `If-None-Match` returns 200. Both 200 and 304 carry -`Cache-Control: private, no-cache`. ETag is computed only after the 32 MiB bound passes. +`/api/catalog` keeps its byte-derived ETag and `If-None-Match` handling: that route is +management-authenticated, loopback-scoped, and its representation does not vary by data +key identity. + +`/v1/catalog` does not participate. It emits no ETag, ignores `If-None-Match`, never +returns 304, and carries `Cache-Control: no-store`. The two routes therefore share +serialization and the size bound, but not the validator. `/v1/catalog` uses `resolveResponsesApiAuth(req, policy)`, not `resolveApiAuth`, because the former is the existing dedicated-header/our-secret-Bearer matrix and rejects `x-api-key` (`src/server/auth-cors.ts:465-478`). It performs the existing data-plane origin check after -admission, then sets `x-opencodex-key-id` on 200 and 304 only when the admitted identity is +admission, then sets `x-opencodex-key-id` on the 200 only when the admitted identity is a configured API key and its id passes `^[A-Za-z0-9._-]{1,64}$` again at emission. A mismatch omits the header and emits one non-secret warning without the id. Environment-token, loopback, rejected, and unauthenticated paths never emit the header. No Direct passthrough @@ -150,13 +167,13 @@ All paths below exist in the current tree except the two files marked **NEW**. | NEW | `src/server/catalog-download.ts` | Own `MAX_REMOTE_CATALOG_BYTES`, one persisted-catalog serialization result, byte-derived ETag matching, `/api/catalog` response construction, and bounded `/v1/catalog` response construction. | | MODIFY | `src/server/management/model-routes.ts` | Replace the inline `/api/catalog` read/`JSON.stringify` block at lines 334–345 with the shared response builder; preserve 404 and `x-opencodex-codex-version`. | | MODIFY | `src/server/auth-cors.ts` | Add the `/v1/catalog` `AUTH_MATRIX` row with bearer/dedicated accepted and `xApiKey` rejected. Do not change any existing row or credential precedence. | -| MODIFY | `src/server/index.ts` | Add protocol metadata to the current `/readyz` body at lines 991–998 by passing `(config, req)` to the builder. Mount exact `GET /v1/catalog` after readiness/management handling and before the unknown-`/v1/*` guard at line 1604; use `resolveResponsesApiAuth` plus the existing origin policy, add `Cache-Control: private, no-cache`, and emit `x-opencodex-key-id` on 200/304 only for a configured-key identity whose id passes the emission-time header-safe guard. Omit and log once without the id on mismatch. Keep `startServer` synchronous and add no `await` between `Bun.serve` and `labActivationRequired`. | +| MODIFY | `src/server/index.ts` | Add protocol metadata to the current `/readyz` body at lines 991–998 by passing `(config, req)` to the builder. Mount exact `GET /v1/catalog` after readiness/management handling and before the unknown-`/v1/*` guard at line 1604; use `resolveResponsesApiAuth` plus the existing origin policy, add `Cache-Control: no-store` with no validator, and emit `x-opencodex-key-id` on the 200 only for a configured-key identity whose id passes the emission-time header-safe guard. Omit and log once without the id on mismatch. Keep `startServer` synchronous and add no `await` between `Bun.serve` and `labActivationRequired`. | | MODIFY | `src/server/proxy-liveness.ts` | Keep existing identity/status parsing additive; extend the internal body type/comments so protocol fields are recognized but do not make ordinary `ocx ready` reject a v0/legacy standalone server. Remote compatibility remains in `src/remote/protocol.ts`. | | MODIFY | `tests/config.test.ts` | Extend the existing config-default/validation sibling tests with absent/default, three valid roles, malformed live candidate, and malformed persisted role preservation cases. | | MODIFY | `tests/server-live.test.ts` | Extend the existing `GET /readyz` suite (lines 1220+) for exact protocol values on ready/pending/failed/draining, sanitized keys, management origin, method/path negatives, and no-auth behavior. | | MODIFY | `tests/proxy-liveness.test.ts` | Extend the current strict readiness parser/probe tests to prove additive protocol fields neither invalidate readiness nor bypass identity/status checks. | | MODIFY | `tests/api-catalog-route.test.ts` | Extend the existing `/api/catalog` sibling suite with fixed fixture bytes and version-header preservation after serializer extraction. | -| MODIFY | `tests/server-auth.test.ts` | Extend live-server auth/order coverage with `/v1/catalog` admission matrix, exact configured-key `x-opencodex-key-id` on 200 and 304, `Cache-Control: private, no-cache`, header absence for environment-token/loopback/rejected/unauthenticated paths, invalid-id omission with one id-free log, exact-path/method negatives, foreign/admin bearer rejection, bound overflow, ETag/304, and unknown-`/v1` guard preservation. | +| MODIFY | `tests/server-auth.test.ts` | Extend live-server auth/order coverage with `/v1/catalog` admission matrix, exact configured-key `x-opencodex-key-id` on the 200, `Cache-Control: no-store`, absence of `ETag`, a request carrying `If-None-Match` still receiving 200 with full bytes, header absence for environment-token/loopback/rejected/unauthenticated paths, invalid-id omission with one id-free log, exact-path/method negatives, foreign/admin bearer rejection, bound overflow, and unknown-`/v1` guard preservation. | | MODIFY | `tests/api-key-attribution.test.ts` | Extend the existing live `AUTH_MATRIX` loop so `/v1/catalog` is a GET route, each cell reaches the real handler rather than the generic 404, configured-key dedicated/Bearer admission echoes that key's id, and environment/loopback admission does not. | No other production or test file is in scope. If implementation proves another path is @@ -235,9 +252,9 @@ an empty catalog. No function accepts a caller-provided catalog path. | P1-A07 | Feed `{protocol: 2, minimumClientProtocol: 2}` to a protocol-v1 client. | `hub-too-new` and the exact “Upgrade ocx on this client” string are returned before catalog access. | | P1-A08 | Feed `{protocol: 1, minimumClientProtocol: 1}` to a protocol-v2 client requiring minimum hub protocol 2. | `hub-too-old` and the exact “Upgrade ocx on the hub” string are returned. | | P1-A09 | Supply zero, omit, mistype, overflow, set minimum above protocol, or give a path-bearing `managementUrl`. | The malformed-input class (`400`) returns `invalid` and the exact malformed-metadata string; it is never classified as a version mismatch and never falls back to protocol 1. | -| P1-A10 | Persist a fixed catalog fixture; call authorized `/api/catalog` and authorized `/v1/catalog`. | Status 200 and response bytes are byte-identical; the ETag independently hashes those bytes and `/v1/catalog` carries `Cache-Control: private, no-cache`. | -| P1-A11 | Repeat `/v1/catalog` with configured-key dedicated/Bearer admission, environment-token admission, loopback admission, `x-api-key`, foreign Bearer, admin token, and no token; inject an invalid configured key id, repeat the request, and capture logs. | Configured-key 200 and matching 304 carry the exact safe key id. Environment/loopback/rejected/unauthenticated responses omit it; invalid id is omitted with one non-secret warning and no key id appears in logs. No case reaches the generic unknown-route 404. | -| P1-A12 | Call `/v1/catalog` with matching tag, weak matching tag, tag list, `*`, stale tag, and malformed tag. | Matches return 304/no body/same ETag; stale or malformed values return 200/full bytes; every successful response carries `Cache-Control: private, no-cache`. | +| P1-A10 | Persist a fixed catalog fixture; call authorized `/api/catalog` and authorized `/v1/catalog`. | Status 200 and response bytes are byte-identical. `/api/catalog` carries its byte-derived ETag; `/v1/catalog` carries `Cache-Control: no-store` and no `ETag`. | +| P1-A11 | Repeat `/v1/catalog` with configured-key dedicated/Bearer admission, environment-token admission, loopback admission, `x-api-key`, foreign Bearer, admin token, and no token; inject an invalid configured key id, repeat the request, and capture logs. | The configured-key 200 carries the exact safe key id. Environment/loopback/rejected/unauthenticated responses omit it; invalid id is omitted with one non-secret warning and no key id appears in logs. No case reaches the generic unknown-route 404. | +| P1-A12 | Call `/v1/catalog` with a matching tag, weak matching tag, tag list, `*`, stale tag, and malformed tag in `If-None-Match`. | Every case returns 200 with the full bytes, no `ETag`, and `Cache-Control: no-store`: the route has no validator to match against, so no request can elicit a 304. The same tags against `/api/catalog` still return 304, proving the removal is scoped to the identity-varying route. | | P1-A13 | Serialize exactly the cap and cap+1 fixtures through an injected serialization seam. | Exact cap returns 200; cap+1 returns 503 `catalog_too_large`, never a partial body. | | P1-A14 | Call POST `/v1/catalog`, GET `/v1/catalog/`, and an unrelated `/v1/does-not-exist`. | Every request returns the existing JSON 404 envelope; route ordering does not widen path/method matching. | | P1-A15 | Run the import-graph and synchronous-window guard after the diff. | No new subsystem is reachable from the three protected core files; `startServer` remains non-async and its guarded window contains no top-level `await`. | diff --git a/devlog/_plan/260827_remote_hub/040_phase2_remote_session.md b/devlog/_plan/260827_remote_hub/040_phase2_remote_session.md index 7e93db79cb..0324291d6e 100644 --- a/devlog/_plan/260827_remote_hub/040_phase2_remote_session.md +++ b/devlog/_plan/260827_remote_hub/040_phase2_remote_session.md @@ -8,15 +8,35 @@ Unit: `260827_remote_hub` · Branch: `codex/remote-hub-design` · Phase: 2 · St ## 1. Outcome and non-negotiable boundary -Remote hub dashboards can obtain an origin-bound `gui-session` through one of four +Remote hub dashboards can obtain an origin-bound `gui-session` through one of three evidence paths, ordered from strongest automatic path to explicit opt-in: 1. `loopback` — current behavior, unchanged and fixed at five minutes. 2. `tailscale-identity` — trusted Tailscale Serve ingress plus exact `remoteGui.allowedTailscaleUsers` membership. -3. `pairing` — a short-lived, origin-bound, single-use grant printed by `ocx gui pair`. -4. `insecure-http-pairing` — the same grant over non-loopback HTTP only when - `remoteGui.allowInsecureHttp === true`. +3. `pairing` — a short-lived, origin-bound, single-use grant printed by `ocx gui pair`, + transmitted only over loopback or authenticated HTTPS. + +**There is no fourth path.** A previous revision of this document defined +`insecure-http-pairing`: the same reusable grant over non-loopback plaintext HTTP, +gated behind `remoteGui.allowInsecureHttp === true`. That path is removed, not +merely discouraged. + +Operator opt-in does not defeat a passive network observer or an on-path attacker. +A grant crossing plaintext HTTP is captured verbatim, and the session it mints is +reusable. An opt-in flag records that the operator accepted a risk they cannot +actually bound, so the flag was doing no security work. + +A "bootstrap over HTTP, then upgrade to HTTPS" variant was considered and rejected: +the plaintext hop has no trust anchor, so an on-path attacker substitutes its own +valid HTTPS origin and the upgrade authenticates the attacker. An upgrade is only +admissible when the HTTPS origin is already known to the client out of band, the +scheme upgrade stays on the same host, certificate validation is ordinary, and no +authority is derived from a redirect. + +Non-loopback plaintext HTTP therefore carries no grant, no session, no admin token, +and no client key. What it may carry is an unauthenticated error naming the required +scheme. Nothing else. The admin token remains an ordinary management principal. It cannot create a pairing grant, is never accepted by the session bootstrap/exchange endpoint, is never re-labeled as @@ -68,7 +88,7 @@ consumption of that credential is the only pairing exchange. - `GuiSessionRecord.serverOrigin` / `browserOrigin` split and full server/GUI consumer chain. - Config validation for `hub.managementPublicOrigin`, - `remoteGui.allowedTailscaleUsers`, and `remoteGui.allowInsecureHttp`. + and `remoteGui.allowedTailscaleUsers`. - Automatic loopback and trusted-Tailscale issuance; pairing grant creation and exchange. - Separate loopback/remote TTLs and sliding renewal for remote sessions. - Exact management CORS header widening for GUI-origin and CSRF headers. @@ -106,7 +126,6 @@ export interface OcxHubConfig { export interface OcxRemoteGuiConfig { allowedTailscaleUsers?: string[]; - allowInsecureHttp?: boolean; } export interface OcxConfig { @@ -124,8 +143,10 @@ Validation rules: - `remoteGui.allowedTailscaleUsers` contains at most 64 unique, trimmed, non-empty strings, each at most 320 UTF-8 bytes and containing no ASCII control character. Matching is exact after trim; no substring/domain matching. -- `remoteGui.allowInsecureHttp` is optional and defaults false. It affects pairing only; - tailscale-identity issuance still requires HTTPS. +- `remoteGui.allowInsecureHttp` no longer exists. A persisted `true` from a + pre-release tree is not honored: it is dropped with a warning naming the key, and + remote issuance continues under the loopback/HTTPS-only rule. A config key cannot + re-enable a transmission path this design removed. - A malformed live candidate is rejected with its full config path. A malformed persisted optional block degrades to remote issuance disabled while preserving providers, accounts, and API keys, and emits a diagnostic that never repeats the malformed value. @@ -142,7 +163,7 @@ export type GuiSessionIssuance = | "loopback" | "tailscale-identity" | "pairing" - | "insecure-http-pairing"; + ; export interface GuiSessionRecord { serverOrigin: string; @@ -239,8 +260,10 @@ memory-only and are never written to web storage. - strict body `{ "grant": "…" }`, 4 KiB maximum, unknown fields rejected. - requires an `Origin` matching the grant's `browserOrigin`; the grant is the only credential accepted. Admin/data/session credentials in headers do not substitute. - - HTTPS issues `pairing`; non-loopback HTTP issues `insecure-http-pairing` only when the - config opt-in is true. The grant is consumed before minting; all replays fail. + - Loopback and authenticated HTTPS issue `pairing`. A non-loopback plaintext HTTP + request is refused **before** the grant is read, so a captured request cannot even + consume the grant as a denial-of-service. The grant is consumed before minting; + all replays fail. - Phase 4's fixed-target relay path allowlist admits this exact POST exchange in addition to GET bootstrap and forwards the browser's `Origin` header verbatim; no other non-`/api/*` method/path is widened. @@ -438,7 +461,8 @@ sessions renew at most once per request. | P2-A07 | Call grant creation with admin token, GUI session, data key, wrong/replayed/expired capability, changed PID/port/origin, or an origin outside public origin/`corsAllowOrigins`. | 403/401 as appropriate; no grant/session state change. Admin authority cannot reach grant creation. | | P2-A08 | HTTPS POST bootstrap with fresh grant and exact `Origin`. | Grant is deleted and one `pairing` session is returned with both origin meta values. | | P2-A09 | Replay consumed grant; use expired grant, wrong Origin, wrong server destination, data key, admin token, or session token in place of grant. | No session for every case; replay and alternate credentials cannot enter the exchange branch. | -| P2-A10 | Non-loopback HTTP pairing with opt-in absent/false, then true. | False path refuses without consuming into a session; true path consumes once and issues `insecure-http-pairing`. Automatic Tailscale issuance remains refused on HTTP in both cases. | +| P2-A10 | Non-loopback HTTP pairing exchange, with and without a legacy persisted `remoteGui.allowInsecureHttp: true`. | Refused in both cases, before the grant is read, so the grant survives for a later HTTPS exchange. The legacy key is dropped with a warning and grants nothing. Automatic Tailscale issuance remains refused on HTTP. | +| P2-A11 | Plaintext HTTP request for the session bootstrap on a non-loopback bind. | Response carries no grant, session, admin token, or client key — only an unauthenticated error naming the required scheme. | | P2-A11 | Authorized remote safe read immediately before expiry. | Full origin predicate passes and expiry slides to `now + 12h`; token/CSRF unchanged. | | P2-A12 | Wrong destination, wrong claimed browser origin, wrong browser `Origin`, absent/wrong CSRF mutation, and an expired session. | 401 and expiry remains unchanged/deleted as applicable; principal is never projected as `gui-session`. | | P2-A13 | Admin token calls ordinary management, pairing-grant creation, bootstrap exchange, and then a consent route; valid remote GUI session calls ordinary/consent routes with correct CSRF. | Admin remains ordinary management-capable but the other three are refused; remote session reaches consent route. No admin-to-grant or admin-to-session exchange exists. | diff --git a/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md b/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md index 1297d19d9d..83d0006b27 100644 --- a/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md +++ b/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md @@ -300,9 +300,24 @@ slashes/backslashes, authority syntax, userinfo, query-host tricks, and path tra The caller supplies no host/scheme/port. `redirect: "manual"`; every 3xx is an error. Strip `Host`, connection/hop-by-hop headers, machine auth headers, proxy credentials, cookies, and forwarding headers. Forward only the bounded management header allowlist, -including hub session, GUI-origin, CSRF, content type, and conditional cache headers. For -the exact `POST /opencodex-session` exchange, forward the browser's `Origin` value verbatim; -do not synthesize it from the hub URL, localhost bind, or GUI-origin header. +including hub session, GUI-origin, CSRF, and content type. + +Forward the browser's `Origin` value **verbatim on every allowed session-authenticated +request**: the `POST /opencodex-session` exchange, the `GET` bootstrap, and every allowed +`/api/` method including `POST`, `PUT`, `PATCH`, and `DELETE`. Never synthesize it from +the hub URL, the localhost bind, or the GUI-origin header, and never omit it. + +A previous revision forwarded `Origin` only for the exact `POST /opencodex-session` +exchange. That is both a functional and a security defect. The minted GUI session is +origin-bound and management mutations enforce Origin/CSRF, so a relayed mutation arriving +without `Origin` loses the evidence the hub requires and is refused — the relay silently +breaks every write path it is supposed to carry. Repairing that by synthesizing an +`Origin` would be worse: the relay would be attesting to a fact it did not observe, and +the hub's CSRF check would be validating the relay against itself. The browser value is +the only admissible source, so it is forwarded unchanged or the request does not go. + +When the browser sends no `Origin` on a request that requires it, the relay refuses +rather than inventing one. Response headers are similarly allowlisted; `Set-Cookie` and hop-by-hop headers are never returned. Request and response bodies have named constants and abort on overflow. No URL query, auth header, body, or response body is logged. @@ -472,6 +487,8 @@ appearing after disconnect. | P4-A2 | Request every known data/shared route on the machine listener. | Every `/v1/*`, `/api/config`, `/api/usage`, OAuth/provider/Lab path is JSON 404; only explicit machine routes/assets answer. | | P4-A3 | GET status/clients with valid loopback GUI session, then without it. | Valid request returns redacted DTO; missing/admin-only/expired/wrong-origin session is rejected and no secret/fingerprint leaks. | | P4-A4 | POST sync/shim/disconnect with valid session but missing/wrong CSRF or browser Origin. | Mutation is rejected before work; exact session+Origin+CSRF reaches the handler. Admin token never becomes GUI session. | +| P4-A4b | Relay every allowed session-authenticated method — GET bootstrap, POST `/opencodex-session`, and `/api/` POST, PUT, PATCH, DELETE — from a browser origin the hub allows. | Each request arrives at the hub carrying the browser's `Origin` byte-for-byte. No case is missing `Origin`, and no case carries a value the browser did not send. | +| P4-A4c | Relay an allowed mutation whose browser request has no `Origin`. | The relay refuses without contacting the hub, and does not synthesize an `Origin` from the hub URL, the localhost bind, or the GUI-origin header. | | P4-A5 | Connected direct transport with Phase-2 session issued by an exact `remoteGui.allowedTailscaleUsers` match or a consumed pairing grant; hub CORS includes the localhost browser origin. | Shared requests go directly to exact hub origin with hub headers only; machine pages remain localhost; CORS/session validation succeeds. A non-allowlisted Tailscale identity mints no session and gets no local fallback. | | P4-A6 | Connected relay transport and valid machine + hub sessions. | Browser sends dual auth domains; relay validates machine session, strips custom headers, forwards hub session only to fixed hub target, and rejects redirect/SSRF variants. | | P4-A7 | Relay path requested while transport is direct/disabled or caller supplies host/scheme/traversal. | Default 404/refusal before outbound fetch; no credential or body is logged. | diff --git a/devlog/_plan/260827_remote_hub/080_phase6_hardening.md b/devlog/_plan/260827_remote_hub/080_phase6_hardening.md index 98375890ef..69daa58b47 100644 --- a/devlog/_plan/260827_remote_hub/080_phase6_hardening.md +++ b/devlog/_plan/260827_remote_hub/080_phase6_hardening.md @@ -316,11 +316,34 @@ or replace the backup directly. and aborts. Never replay commit without this evidence. On startup/status, `src/client/state.ts` treats a rotate `pendingOperation` as a recovery gate: -verify that `oldKeyBackupPath` is exactly `.prev`, require an owner-only regular file, -probe current and backup keys using the authenticated catalog key-id echo, then complete the same -commit-or-restore chain. Doubly accepted evidence commits the current new key with the persisted -`rotationId`, deletes `.prev`, and clears `pendingOperation`; doubly rejected, missing, or unsafe -evidence stops with an exact recovery instruction and never deletes either candidate blindly. +verify that `oldKeyBackupPath` is exactly `.prev` and require an owner-only regular +file. + +**Compare the two candidates' identities before probing anything.** If the live token and +`.prev` carry the same identity, the process stopped after `pendingOperation` was persisted +but before the token was replaced. Both candidates are the old key, so both probe +successfully — and a "both accepted" rule would read that as a completed issuance and commit +a rotation that never happened, permanently losing the new key. Identical candidates +therefore mean pre-replacement: never commit, restore nothing, and resume the rotation from +the beginning. + +Only when the candidates differ does probing decide anything, and only a confirmed authority +may act: + +- New and old both accepted: issuance completed, overlap still pending. Commit the new key + with the stored `rotationId`. +- New only: commit already took effect. Clear the operation. +- Old only: issuance did not take effect. Restore and abort. +- Neither accepted, a probe that fails for a reason other than rejection, or any state the + above does not name: stop with an exact recovery instruction. Delete nothing. + +If an abort or restore itself fails, the uncertainty is retained rather than papered over: +keep both candidates and the `pendingOperation` record, and report which step could not be +confirmed. A restore performed without confirmed authority can install the wrong generation +and is worse than stopping. + +A concurrent `ocx connect status` never deletes a backup belonging to an in-flight rotation. +Recovery only acts on a `pendingOperation` it can prove is abandoned. The GUI exposes the same lifecycle for an operator updating a client manually, with explicit copy-once and commit-after-client-probe wording. Closing the modal does not imply commit; pending @@ -497,8 +520,18 @@ the pure validator with raw tuples for otherwise-unconstructible header shapes. - Rebuild response headers and strip hop-by-hop headers, `Set-Cookie`, proxy auth, server identity headers, Tailscale identity, and connection-nominated headers. -- Preserve safe content type, cache control, ETag, retry-after, and approved CORS/session bootstrap - metadata only. +- Preserve safe content type, retry-after, and approved CORS/session bootstrap metadata only. +- Relayed session, bootstrap, and management responses are rewritten to + `Cache-Control: no-store` and have `ETag` and `Last-Modified` removed. The relay does + not pass an upstream validator through, and does not honor a conditional request against + one. + + A previous revision preserved upstream `cache control` and `ETag` on relayed responses. + That reintroduces the Phase-1 defect one layer up: relayed responses vary by hub session + and client identity, so a preserved strong validator lets a store revalidate one + identity's representation for another — and the relay sits in exactly the position where + an intermediary cache is most likely to exist. The relay is not the right place to prove + an identity-partitioned cache key, so it does not carry a validator at all. - Enforce Phase-4 management body caps. Phase-6 streaming uses backpressure and abort propagation; it must not buffer an unbounded response or continue after browser disconnect. - Errors name only status/category and fixed hub label. No destination URL query, session token, @@ -684,3 +717,6 @@ the matching CI partition before classifying it; never call a red result environ Final live evidence runs on `clisu-oracle`/MacBook per 070 §8 after the remote gates. It proves health, readiness, authenticated catalog, one routed response, remote session, consent refusal, rotation, usage slice, disconnect/local store, rollback, and both constructible protocol directions. +| P6-A20 | Persist `pendingOperation`, then stop before the token is replaced so the live token and `.prev` hold the same key. Restart. | Recovery detects identical candidates before probing, refuses to commit, leaves both files intact, and resumes the rotation. The pre-fix "both probes accepted implies commit" rule is what this row exists to keep dead. | +| P6-A21 | Rotation reaches installed-new-token state, then the abort request fails transiently. | Neither candidate is deleted and `pendingOperation` survives with the unconfirmed step named. No generation is restored on unconfirmed authority. | +| P6-A22 | Run `ocx connect status` while `rotateConnectedClientKey` is awaiting `/api/keys/rotate`. | The in-flight `.prev` backup is not deleted and the rotation completes normally. | From 825800e359fb6e3d11029fad6976415428b0b268 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 01:53:01 +0900 Subject: [PATCH 027/172] test(remote): cover phase one protocol and catalog contract --- tests/api-catalog-route.test.ts | 51 ++++++--- tests/config.test.ts | 63 +++++++++++ tests/proxy-liveness.test.ts | 105 ++++++++++++++++++ tests/server-auth.test.ts | 189 +++++++++++++++++++++++++++++++- tests/server-live.test.ts | 25 ++++- 5 files changed, 415 insertions(+), 18 deletions(-) diff --git a/tests/api-catalog-route.test.ts b/tests/api-catalog-route.test.ts index 634ce3e7e1..f639d61f95 100644 --- a/tests/api-catalog-route.test.ts +++ b/tests/api-catalog-route.test.ts @@ -10,6 +10,7 @@ import type { OcxConfig } from "../src/types"; const TEST_DIR = join(import.meta.dir, `.tmp-api-catalog-route-${process.pid}`); const previousOpencodexHome = process.env.OPENCODEX_HOME; let isolatedCodexHome: IsolatedCodexHome | null = null; +const CATALOG_FIXTURE_BYTES = '{"models":[{"slug":"mock/test-model","display_name":"Mock Test","description":"fixture","priority":1,"visibility":"list","base_instructions":"You are a helpful coding assistant.","input_modalities":["text"]}]}'; beforeEach(() => { if (previousOpencodexHome === undefined) mkdirSync(TEST_DIR, { recursive: true }); @@ -39,18 +40,7 @@ afterEach(() => { describe("GET /api/catalog route (#709)", () => { test("returns the on-disk catalog and omits sync runtime probes for version hint", async () => { isolatedCodexHome = installIsolatedCodexHome("ocx-api-catalog-"); - const catalog = { - models: [{ - slug: "mock/test-model", - display_name: "Mock Test", - description: "fixture", - priority: 1, - visibility: "list", - base_instructions: "You are a helpful coding assistant.", - input_modalities: ["text"], - }], - }; - writeFileSync(join(isolatedCodexHome.path, "opencodex-catalog.json"), JSON.stringify(catalog)); + writeFileSync(join(isolatedCodexHome.path, "opencodex-catalog.json"), CATALOG_FIXTURE_BYTES); const url = new URL("http://localhost/api/catalog"); const response = await handleManagementAPI( @@ -59,10 +49,32 @@ describe("GET /api/catalog route (#709)", () => { loadConfig(), ); expect(response?.status).toBe(200); - expect(await response!.json()).toEqual(catalog); + expect(await response!.text()).toBe(CATALOG_FIXTURE_BYTES); expect(response!.headers.get("x-opencodex-codex-version")).toBeNull(); }); + test("preserves the persisted Codex version header after serializer extraction", async () => { + isolatedCodexHome = installIsolatedCodexHome("ocx-api-catalog-version-"); + writeFileSync(join(isolatedCodexHome.path, "opencodex-catalog.json"), CATALOG_FIXTURE_BYTES); + writeFileSync(join(TEST_DIR, "codex-runtime.json"), JSON.stringify({ + version: 1, + command: "/fixture/codex", + source: "configured", + selectedVersion: "0.150.0", + updatedAt: "2026-08-28T00:00:00.000Z", + })); + + const url = new URL("http://localhost/api/catalog"); + const response = await handleManagementAPI( + new ManagementRequest(url, { headers: managementHeaders() }), + url, + loadConfig(), + ); + expect(response?.status).toBe(200); + expect(response!.headers.get("x-opencodex-codex-version")).toBe("0.150.0"); + expect(await response!.text()).toBe(CATALOG_FIXTURE_BYTES); + }); + test("returns 404 when the catalog file is missing", async () => { isolatedCodexHome = installIsolatedCodexHome("ocx-api-catalog-missing-"); const url = new URL("http://localhost/api/catalog"); @@ -74,6 +86,19 @@ describe("GET /api/catalog route (#709)", () => { expect(response?.status).toBe(404); expect(await response!.json()).toEqual({ error: "catalog not found" }); }); + + test("returns a bounded server failure for a malformed persisted catalog", async () => { + isolatedCodexHome = installIsolatedCodexHome("ocx-api-catalog-malformed-"); + writeFileSync(join(isolatedCodexHome.path, "opencodex-catalog.json"), '{"models":'); + const url = new URL("http://localhost/api/catalog"); + const response = await handleManagementAPI( + new ManagementRequest(url, { headers: managementHeaders() }), + url, + loadConfig(), + ); + expect(response?.status).toBe(500); + expect(await response!.json()).toEqual({ error: "catalog unavailable" }); + }); }); describe("GET|HEAD /v1/catalog least-privilege data-plane route (#809)", () => { diff --git a/tests/config.test.ts b/tests/config.test.ts index 62d3f6d782..de026c8dee 100644 --- a/tests/config.test.ts +++ b/tests/config.test.ts @@ -22,6 +22,7 @@ import { readRuntimePort, removePid, removeRuntimePort, + runtimeRole, ocxStartProcessCacheSizeForTests, setOcxStartProcessCacheForTests, setProcessCommandLineExecForTests, @@ -115,6 +116,68 @@ function writeAccountNamespaceConfig( } describe("opencodex config defaults", () => { + test("runtime role is absent-by-default and resolves to standalone", () => { + const defaults = getDefaultConfig(); + expect(Object.hasOwn(defaults, "runtimeRole")).toBe(false); + expect(runtimeRole(defaults)).toBe("standalone"); + writeConfig(defaults); + const before = readFileSync(getConfigPath(), "utf8"); + expect(runtimeRole(loadConfig())).toBe("standalone"); + expect(readFileSync(getConfigPath(), "utf8")).toBe(before); + }); + + test("runtime role accepts the three explicit contract values", () => { + for (const role of ["standalone", "hub", "client"] as const) { + expect(validateConfigCandidate({ ...getDefaultConfig(), runtimeRole: role })).toMatchObject({ + ok: true, + config: { runtimeRole: role }, + }); + } + }); + + test("runtime role rejects malformed live candidates", () => { + for (const runtimeRole of ["server", "", 1, null]) { + expect(validateConfigCandidate({ ...getDefaultConfig(), runtimeRole })).toMatchObject({ + ok: false, + error: expect.stringContaining("runtimeRole"), + }); + } + }); + + test("a malformed persisted runtime role preserves providers and API keys", () => { + const invalidRole = "future-secret-shaped-role"; + writeConfig({ + port: 12345, + runtimeRole: invalidRole, + defaultProvider: "custom", + providers: { custom: { adapter: "openai-chat", baseUrl: "https://example.test/v1", apiKey: "upstream-secret" } }, + apiKeys: [{ id: "key-1", name: "default", key: "ocx_persisted", createdAt: "2026-08-28T00:00:00.000Z" }], + }); + const warnSpy = spyOn(console, "warn").mockImplementation(() => {}); + try { + const loaded = loadConfig(); + const diagnostics = readConfigDiagnostics(); + expect(runtimeRole(loaded)).toBe("standalone"); + expect(loaded.runtimeRole).toBeUndefined(); + expect(loaded).toMatchObject({ + port: 12345, + defaultProvider: "custom", + providers: { custom: { baseUrl: "https://example.test/v1", apiKey: "upstream-secret" } }, + apiKeys: [expect.objectContaining({ id: "key-1", key: "ocx_persisted" })], + }); + expect(diagnostics).toMatchObject({ + source: "file", + error: null, + warnings: [expect.stringContaining("runtimeRole ignored")], + }); + expect(backupNames()).toEqual([]); + expect(warnSpy).toHaveBeenCalledTimes(1); + expect(warnSpy.mock.calls.flat().join(" ")).not.toContain(invalidRole); + } finally { + warnSpy.mockRestore(); + } + }); + test("malformed classifier config is normalized at load, even with subagentEffort absent (#1697)", () => { // normalizePersistedClaudeCode used to be reached only through a subagentEffort short-circuit, // so a config whose ONLY defect was elsewhere in claudeCode was never normalized. These diff --git a/tests/proxy-liveness.test.ts b/tests/proxy-liveness.test.ts index 7fd806fd62..7a00da7077 100644 --- a/tests/proxy-liveness.test.ts +++ b/tests/proxy-liveness.test.ts @@ -11,6 +11,12 @@ import { proxyIdentityAt, validateReadyzBody, } from "../src/server/proxy-liveness"; +import { + checkRemoteProtocolCompatibility, + parseRemoteReadyMetadata, + readyProtocolMetadata, +} from "../src/remote/protocol"; +import { getDefaultConfig } from "../src/config"; function healthz(body: unknown, status = 200): Response { return new Response(JSON.stringify(body), { status }); @@ -530,6 +536,18 @@ describe("validateReadyzBody strict contract", () => { expect(validateReadyzBody(VALID_BODY, 10100)).toEqual({ ready: true, status: "ready", pid: 4242, port: 10100 }); }); + test("accepts additive remote protocol and unknown future fields without weakening identity", () => { + const additive = { + ...VALID_BODY, + protocol: 1, + minimumClientProtocol: 1, + managementUrl: "https://hub.example.test", + futureCapability: { enabled: true }, + }; + expect(validateReadyzBody(additive, 10100)).toEqual({ ready: true, status: "ready", pid: 4242, port: 10100 }); + expect(validateReadyzBody({ ...additive, service: "foreign" }, 10100)).toBeNull(); + }); + test("accepts pending/failed bodies as not-ready with the same fixed status", () => { expect(validateReadyzBody({ ...VALID_BODY, status: "pending" }, 10100)).toEqual({ ready: false, status: "pending", pid: 4242, port: 10100 }); expect(validateReadyzBody({ ...VALID_BODY, status: "failed" }, 10100)).toEqual({ ready: false, status: "failed", pid: 4242, port: 10100 }); @@ -613,6 +631,93 @@ describe("validateReadyzBody strict contract", () => { }); }); +describe("remote readiness protocol metadata", () => { + const metadata = { + protocol: 1, + minimumClientProtocol: 1, + managementUrl: "https://hub.example.test", + }; + const invalidMessage = "OpenCodex hub returned invalid remote protocol metadata; upgrade or repair ocx on the hub."; + + test("parses required fields, canonicalizes the origin, and ignores additive fields", () => { + expect(parseRemoteReadyMetadata({ + ...metadata, + managementUrl: "https://hub.example.test:443/", + future: true, + })).toEqual(metadata); + }); + + test("builds one stable shape for standalone, hub, and client roles", () => { + for (const runtimeRole of ["standalone", "hub", "client"] as const) { + expect(readyProtocolMetadata( + { ...getDefaultConfig(), runtimeRole }, + new Request("https://hub.example.test/readyz"), + )).toEqual(metadata); + } + }); + + test("uses the observed Host and ignores forwarding headers", () => { + expect(readyProtocolMetadata(getDefaultConfig(), new Request("http://127.0.0.1/readyz", { + headers: { + Host: "hub.example.test:8443", + Forwarded: "host=attacker.test;proto=https", + "X-Forwarded-Host": "attacker.test", + "X-Forwarded-Proto": "https", + }, + }))).toEqual({ + protocol: 1, + minimumClientProtocol: 1, + managementUrl: "http://hub.example.test:8443", + }); + }); + + test("classifies a hub that requires a newer client with the exact message", () => { + expect(checkRemoteProtocolCompatibility({ ...metadata, protocol: 2, minimumClientProtocol: 2 })).toEqual({ + ok: false, + reason: "hub-too-new", + message: "OpenCodex hub requires remote protocol 2; this client supports protocol 1. Upgrade ocx on this client.", + }); + }); + + test("classifies a hub below the client floor with the exact message", () => { + expect(checkRemoteProtocolCompatibility(metadata, { protocol: 2, minimumHubProtocol: 2 })).toEqual({ + ok: false, + reason: "hub-too-old", + message: "OpenCodex hub provides remote protocol 1; this client requires at least 2. Upgrade ocx on the hub.", + }); + }); + + test("malformed metadata is invalid, never a version mismatch", () => { + const malformed = [ + { ...metadata, protocol: 0 }, + { minimumClientProtocol: 1, managementUrl: metadata.managementUrl }, + { protocol: 1, managementUrl: metadata.managementUrl }, + { ...metadata, protocol: "1" }, + { ...metadata, protocol: Number.MAX_SAFE_INTEGER + 1 }, + { ...metadata, minimumClientProtocol: 2 }, + { ...metadata, managementUrl: "https://hub.example.test/path" }, + { ...metadata, managementUrl: "https://hub.example.test/?query=1" }, + { ...metadata, managementUrl: "https://hub.example.test/#fragment" }, + { ...metadata, managementUrl: "https://user@hub.example.test" }, + ]; + for (const value of malformed) { + expect(parseRemoteReadyMetadata(value)).toBeNull(); + expect(checkRemoteProtocolCompatibility(value)).toEqual({ + ok: false, + reason: "invalid", + message: invalidMessage, + }); + } + }); + + test("accepts an additive protocol level when the v1 intervals intersect", () => { + expect(checkRemoteProtocolCompatibility({ ...metadata, protocol: 2 })).toEqual({ + ok: true, + metadata: { ...metadata, protocol: 2 }, + }); + }); +}); + // ── probeReadiness: strict HTTP + contract enforcement ───────────────────────── function readyz(body: unknown, status = 200): Response { diff --git a/tests/server-auth.test.ts b/tests/server-auth.test.ts index ac913d4d33..94db2e0854 100644 --- a/tests/server-auth.test.ts +++ b/tests/server-auth.test.ts @@ -1,5 +1,6 @@ import { waitForNativeMainStartupGate } from "../src/codex/native-profile-startup"; -import { afterEach, beforeEach, describe, expect, test } from "bun:test"; +import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test"; +import { createHash } from "node:crypto"; import { logsFromApiBody } from "./helpers/logs-api"; import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { request as httpRequest } from "node:http"; @@ -48,6 +49,11 @@ import { LOCAL_PROVIDER_RELOAD_CAPABILITY_VERSION } from "../src/lib/local-provi import { resetCodexModelEntitlementCacheForTests } from "../src/codex/model-entitlements"; import { getDebugLogEntries, resetDebugLogBufferForTests } from "../src/lib/debug-log-buffer"; import { resetDebugSettingsForTests, setDebugSettings } from "../src/lib/debug-settings"; +import { + MAX_REMOTE_CATALOG_BYTES, + catalogDataPlaneResponse, + type SerializedCatalog, +} from "../src/server/catalog-download"; import { watchdogMs } from "./helpers/ci-watchdog"; const previousApiToken = process.env.OPENCODEX_API_AUTH_TOKEN; @@ -82,6 +88,22 @@ function config(hostname?: string): OcxConfig { }; } +const REMOTE_CATALOG_BYTES = '{"models":[{"slug":"fixture/model","display_name":"Fixture Model","priority":1,"visibility":"list","base_instructions":"Fixture instructions","input_modalities":["text"]}]}'; +const REMOTE_DATA_KEY = "ocx_data_remote_catalog"; + +function remoteCatalogConfig(keyId = "remote-key"): OcxConfig { + return { + ...config("0.0.0.0"), + port: 0, + apiKeys: [{ id: keyId, name: "remote", key: REMOTE_DATA_KEY, createdAt: "2026-08-28T00:00:00.000Z" }], + }; +} + +function writeRemoteCatalog(): void { + if (!isolatedCodexHome) throw new Error("isolated Codex home is not installed"); + writeFileSync(join(isolatedCodexHome.path, "opencodex-catalog.json"), REMOTE_CATALOG_BYTES); +} + function managementHeaders(initial?: HeadersInit): Headers { const token = configuredAdminToken(); if (!token) throw new Error("management token was not initialized"); @@ -4019,3 +4041,168 @@ describe("server local API auth", () => { } }); }); + +describe("GET /v1/catalog remote data plane", () => { + test("management and data-plane routes return byte-identical catalog bodies", async () => { + saveConfig(remoteCatalogConfig()); + writeRemoteCatalog(); + const server = startServer(0); + try { + const management = await fetch(new URL("/api/catalog", server.url), { headers: managementHeaders() }); + const remote = await fetch(new URL("/v1/catalog", server.url), { + headers: { "x-opencodex-api-key": REMOTE_DATA_KEY }, + }); + const managementBytes = new Uint8Array(await management.arrayBuffer()); + const remoteBytes = new Uint8Array(await remote.arrayBuffer()); + expect(management.status).toBe(200); + expect(remote.status).toBe(200); + expect(remoteBytes).toEqual(managementBytes); + expect(new TextDecoder().decode(remoteBytes)).toBe(REMOTE_CATALOG_BYTES); + const expectedEtag = `"sha256-${createHash("sha256").update(remoteBytes).digest("base64url")}"`; + expect(remote.headers.get("etag")).toBe(expectedEtag); + expect(remote.headers.get("cache-control")).toBe("private, no-cache"); + expect(remote.headers.get("x-opencodex-key-id")).toBe("remote-key"); + } finally { + await server.stop(true); + } + }); + + test("admission accepts configured dedicated and bearer keys, and rejects every foreign class", async () => { + saveConfig(remoteCatalogConfig()); + writeRemoteCatalog(); + const server = startServer(0); + try { + const cases = [ + [{ "x-opencodex-api-key": REMOTE_DATA_KEY }, 200, "remote-key"], + [{ authorization: `Bearer ${REMOTE_DATA_KEY}` }, 200, "remote-key"], + [{ "x-api-key": REMOTE_DATA_KEY }, 401, null], + [{ authorization: "Bearer foreign-key" }, 401, null], + [{ authorization: `Bearer ${configuredAdminToken() ?? "missing-admin"}` }, 401, null], + [{ "x-opencodex-api-key": REMOTE_DATA_KEY, origin: "https://attacker.test" }, 403, null], + [{}, 401, null], + ] as const; + for (const [headers, status, keyId] of cases) { + const response = await fetch(new URL("/v1/catalog", server.url), { headers }); + expect(response.status).toBe(status); + expect(response.headers.get("x-opencodex-key-id")).toBe(keyId); + } + } finally { + await server.stop(true); + } + }); + + test("environment-token and loopback admission never emit a configured key id", async () => { + process.env.OPENCODEX_API_AUTH_TOKEN = "environment-catalog-token"; + saveConfig(remoteCatalogConfig()); + writeRemoteCatalog(); + const remote = startServer(0); + try { + const response = await fetch(new URL("/v1/catalog", remote.url), { + headers: { "x-opencodex-api-key": "environment-catalog-token" }, + }); + expect(response.status).toBe(200); + expect(response.headers.get("x-opencodex-key-id")).toBeNull(); + } finally { + await remote.stop(true); + } + + const loopbackConfig = remoteCatalogConfig(); + loopbackConfig.hostname = "127.0.0.1"; + saveConfig(loopbackConfig); + const loopback = startServer(0); + try { + const response = await fetch(new URL("/v1/catalog", loopback.url)); + expect(response.status).toBe(200); + expect(response.headers.get("x-opencodex-key-id")).toBeNull(); + } finally { + await loopback.stop(true); + } + }); + + test("an unsafe configured key id is omitted with one id-free warning", async () => { + const unsafeId = "unsafe key id"; + saveConfig(remoteCatalogConfig(unsafeId)); + writeRemoteCatalog(); + const warnSpy = spyOn(console, "warn").mockImplementation(() => {}); + const server = startServer(0); + try { + const response = await fetch(new URL("/v1/catalog", server.url), { + headers: { "x-opencodex-api-key": REMOTE_DATA_KEY }, + }); + expect(response.status).toBe(200); + expect(response.headers.get("x-opencodex-key-id")).toBeNull(); + expect(warnSpy).toHaveBeenCalledTimes(1); + expect(warnSpy.mock.calls.flat().join(" ")).not.toContain(unsafeId); + } finally { + await server.stop(true); + warnSpy.mockRestore(); + } + }); + + test("strong, weak, list, and wildcard validators return bodyless 304", async () => { + saveConfig(remoteCatalogConfig()); + writeRemoteCatalog(); + const server = startServer(0); + try { + const first = await fetch(new URL("/v1/catalog", server.url), { + headers: { "x-opencodex-api-key": REMOTE_DATA_KEY }, + }); + const etag = first.headers.get("etag")!; + for (const validator of [etag, `W/${etag}`, `"stale", ${etag}`, "*"]) { + const response = await fetch(new URL("/v1/catalog", server.url), { + headers: { "x-opencodex-api-key": REMOTE_DATA_KEY, "if-none-match": validator }, + }); + expect(response.status).toBe(304); + expect(await response.text()).toBe(""); + expect(response.headers.get("etag")).toBe(etag); + expect(response.headers.get("cache-control")).toBe("private, no-cache"); + expect(response.headers.get("x-opencodex-key-id")).toBe("remote-key"); + } + for (const validator of ['"stale"', `W/ ${etag}`, "malformed"]) { + const response = await fetch(new URL("/v1/catalog", server.url), { + headers: { "x-opencodex-api-key": REMOTE_DATA_KEY, "if-none-match": validator }, + }); + expect(response.status).toBe(200); + expect(await response.text()).toBe(REMOTE_CATALOG_BYTES); + expect(response.headers.get("cache-control")).toBe("private, no-cache"); + } + } finally { + await server.stop(true); + } + }); + + test("the remote size cap accepts exactly the bound and rejects cap plus one", async () => { + const request = new Request("http://localhost/v1/catalog"); + const policy = { hostname: "127.0.0.1" }; + const atCap: SerializedCatalog = { bytes: new Uint8Array(MAX_REMOTE_CATALOG_BYTES) }; + const overCap: SerializedCatalog = { bytes: new Uint8Array(MAX_REMOTE_CATALOG_BYTES + 1) }; + expect(catalogDataPlaneResponse(atCap, request, policy).status).toBe(200); + const rejected = catalogDataPlaneResponse(overCap, request, policy); + expect(rejected.status).toBe(503); + expect(await rejected.json()).toMatchObject({ error: { code: "catalog_too_large" } }); + }); + + test("method and path matching stay exact ahead of the unknown-v1 guard", async () => { + saveConfig(remoteCatalogConfig()); + writeRemoteCatalog(); + const server = startServer(0); + try { + for (const [path, method] of [ + ["/v1/catalog", "POST"], + ["/v1/catalog/", "GET"], + ["/v1/does-not-exist", "GET"], + ] as const) { + const response = await fetch(new URL(path, server.url), { + method, + headers: { "x-opencodex-api-key": REMOTE_DATA_KEY }, + }); + expect(response.status).toBe(404); + expect(response.headers.get("content-type")).toContain("application/json"); + expect(await response.json()).toMatchObject({ error: { code: "not_found" } }); + expect(response.headers.get("x-opencodex-key-id")).toBeNull(); + } + } finally { + await server.stop(true); + } + }); +}); diff --git a/tests/server-live.test.ts b/tests/server-live.test.ts index a92e4ec0a0..53418c4627 100644 --- a/tests/server-live.test.ts +++ b/tests/server-live.test.ts @@ -121,6 +121,14 @@ function forwardConfig(): OcxConfig { } as OcxConfig; } +function expectReadyProtocolMetadata(body: Record, managementUrl: string): void { + expect(body).toMatchObject({ + protocol: 1, + minimumClientProtocol: 1, + managementUrl, + }); +} + function multipartLiveBody( sdp = "v=0", session: Record | null = { model: "gpt-live" }, @@ -1231,13 +1239,17 @@ describe("GET /readyz", () => { try { const pending = await fetch(new URL("/readyz", server.url)); expect(pending.status).toBe(503); - expect(((await pending.json()) as { status: string }).status).toBe("pending"); + const pendingBody = (await pending.json()) as Record; + expect(pendingBody.status).toBe("pending"); + expectReadyProtocolMetadata(pendingBody, new URL(server.url).origin); await runStartupReadinessSync(gate, async () => outcome); const settled = await fetch(new URL("/readyz", server.url)); expect(settled.status).toBe(expectedHttp); - expect(((await settled.json()) as { status: string }).status).toBe(expectedStatus); + const settledBody = (await settled.json()) as Record; + expect(settledBody.status).toBe(expectedStatus); + expectReadyProtocolMetadata(settledBody, new URL(server.url).origin); } finally { await server.stop(true); } @@ -1268,8 +1280,11 @@ describe("GET /readyz", () => { expect(typeof readyzBody.uptime).toBe("number"); expect(typeof readyzBody.pid).toBe("number"); expect(typeof readyzBody.port).toBe("number"); + expectReadyProtocolMetadata(readyzBody, new URL(base).origin); // Sanitization: the body must never carry sync diagnostics, paths, or warnings. - expect(Object.keys(readyzBody).sort()).toEqual(["pid", "port", "service", "status", "uptime", "version"]); + expect(Object.keys(readyzBody).sort()).toEqual([ + "managementUrl", "minimumClientProtocol", "pid", "port", "protocol", "service", "status", "uptime", "version", + ]); expect(JSON.stringify(readyzBody)).not.toContain("warning"); expect(JSON.stringify(readyzBody)).not.toContain("path"); } finally { @@ -1517,7 +1532,9 @@ describe("GET /readyz while draining", () => { const drainRes = await fetch(new URL("/readyz", base)); expect(drainRes.status).toBe(503); expect(drainRes.headers.get("retry-after")).toBe("1"); - expect(((await drainRes.json()) as { status: string }).status).toBe("pending"); + const drainBody = (await drainRes.json()) as Record; + expect(drainBody.status).toBe("pending"); + expectReadyProtocolMetadata(drainBody, new URL(base).origin); // The gate itself is untouched — draining is a listener state, not a gate // transition, so the startup-sync ownership contract is preserved. From 2da2d734d7b9c5327e658e8d7321005d3ec9aed6 Mon Sep 17 00:00:00 2001 From: jun Date: Tue, 1 Sep 2026 16:48:08 +0900 Subject: [PATCH 028/172] fix(design): finish removing insecure-http pairing across the unit The previous commit closed D1 where the review pointed (040) but left the enabling contract alive in three other documents: 010 still listed insecure-http pairing as evidence rung 4, 050 still defined the client --allow-insecure-http option plus an allowInsecureHttp field on the exchange call, and 070 still named remoteGui.allowInsecureHttp as a Phase-2 key. A contract that removes a path in one document and specifies it in three others is not a fix; an implementer reading 050 would have built the option. 050's "both sides must opt in" rationale is also removed rather than reworded. Requiring two opt-ins makes the choice deliberate, but deliberateness is not the control that matters here: the grant is still readable by anything on the path and the session it mints is still reusable. The client now refuses before transmission instead of warning after it. 010 additionally records why the "don't over-harden" valve does not need this path: tailscale serve terminates HTTPS for exactly that deployment, so rung 3 already covers the private-tailnet sole-operator case the valve was for. P3-A3 is rewritten from "succeeds with explicit warning" to refusal in every combination, including a tree still carrying the legacy CLI argument or a persisted config key. --- devlog/_plan/260827_remote_hub/010_design.md | 16 +++++++++--- .../260827_remote_hub/050_phase3_connect.md | 26 ++++++++++++------- .../260827_remote_hub/070_phase5_deploy.md | 5 ++-- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/devlog/_plan/260827_remote_hub/010_design.md b/devlog/_plan/260827_remote_hub/010_design.md index 76099d4e30..42365a96b7 100644 --- a/devlog/_plan/260827_remote_hub/010_design.md +++ b/devlog/_plan/260827_remote_hub/010_design.md @@ -112,10 +112,18 @@ Issuance ladder (config-selected, strictest first): session. On a shared tailnet, an empty allowlist means nobody mints remotely. 3. pairing — `ocx gui pair` on the hub prints a single-use, short-TTL, origin-bound grant that can only mint a session. For generic HTTPS terminators. -4. insecure-http pairing (documented opt-in: `remoteGui.allowInsecureHttp`) — the SAME - single-use pairing grant as rung 3, allowed to travel over a plain-HTTP tailnet - origin. This is the "don't over-harden" valve the user asked for: private tailnet, - sole operator → run `ocx gui pair` once on the hub, paste the code, GUI works. +4. ~~insecure-http pairing~~ — REMOVED. An earlier revision let the rung-3 grant travel + over a plain-HTTP tailnet origin behind `remoteGui.allowInsecureHttp`, as the + "don't over-harden" valve for a private tailnet with a sole operator. A reusable + grant on plaintext HTTP is captured verbatim by anything with tailnet reach, and an + opt-in flag records a risk the operator cannot actually bound, so the flag was doing + no security work. A private tailnet is not a private wire. + + The valve the user asked for is served by rung 3 over `tailscale serve`, which + terminates HTTPS for exactly this deployment and needs no plaintext hop. Non-loopback + plaintext HTTP now carries no grant, session, admin token, or client key — only an + unauthenticated error naming the required scheme. + Audit note (blocker 1, folded): the earlier "trusted-tailnet" variant that minted sessions from Host/Origin alone is DROPPED — headers are forgeable by anything with TCP reach, so it would have granted consent routes with zero credential, strictly diff --git a/devlog/_plan/260827_remote_hub/050_phase3_connect.md b/devlog/_plan/260827_remote_hub/050_phase3_connect.md index 97628e74c7..adfa817e56 100644 --- a/devlog/_plan/260827_remote_hub/050_phase3_connect.md +++ b/devlog/_plan/260827_remote_hub/050_phase3_connect.md @@ -304,7 +304,7 @@ export function exchangeConnectPairingGrant( managementUrl: string, browserOrigin: string, grant: Uint8Array, - options?: { allowInsecureHttp?: boolean; timeoutMs?: number; fetchImpl?: typeof fetch }, + options?: { timeoutMs?: number; fetchImpl?: typeof fetch }, ): Promise; export function issueClientKey( managementUrl: string, @@ -325,12 +325,19 @@ export function downloadClientCatalog( through `checkRemoteProtocolCompatibility()`; it does not define a second protocol shape, constants, or mismatch strings. Both URLs accept only `http:`/`https:`, reject credentials/query/hash and non-root paths -(a terminal `/v1` input normalizes to the server origin). Admin credentials may be sent -only over HTTPS. A pairing grant may use HTTP only when the caller explicitly supplied -`--allow-insecure-http`; the Phase-2 hub independently requires -`remoteGui.allowInsecureHttp === true`, so both sides must opt in. Redirects are -rejected. Bodies and timeouts are bounded. Errors carry status and safe code, never -response/header secrets. +(a terminal `/v1` input normalizes to the server origin). + +No credential travels over non-loopback plaintext HTTP. That covers the admin +credential, the pairing grant, the resulting session, and the issued client key alike. +An earlier revision let a grant use HTTP when the caller passed +`--allow-insecure-http` and the hub set `remoteGui.allowInsecureHttp === true`, on the +theory that requiring both sides to opt in made it deliberate. Deliberateness is not the +control that matters: the grant is still readable by anything on the path, and the +session it mints is reusable. Both the flag and the CLI option are removed, and the +client refuses before transmission rather than warning after it. + +Redirects are rejected. Bodies and timeouts are bounded. Errors carry status and safe +code, never response/header secrets. `POST /api/keys` remains the exact key authority. The request body is only a validated, bounded `name`; admin uses the ordinary management header. Pairing uses strict @@ -354,7 +361,6 @@ export interface ConnectOptions { selectedClients: OcxConnectedClientId[]; managementTransport: "direct" | "relay"; noSync?: boolean; - allowInsecureHttp?: boolean; } export interface ClientConnectDeps { @@ -382,7 +388,7 @@ ocx connect [--management-url ] [--pairing-code-stdin | --admin-token-stdin] [--clients codex,claude] [--management-transport direct|relay] - [--allow-insecure-http] [--no-sync] + [--no-sync] ocx connect status [--json] ocx disconnect [--keep-catalog] [--json] ``` @@ -524,7 +530,7 @@ No test sends live hub traffic or reads the developer's homes. |---|---|---| | P3-A1 | Disconnected temp home; HTTPS hub ready on protocol 1; admin token arrives through stdin; `/api/keys` and `/v1/catalog` succeed. | One key is issued, token exists only in owner-only service file, catalog and injection commit, and `runtimeRole=client` + `config.client` are written together and last with key id/fingerprint only. | | P3-A2 | Same as A1, but a Phase-2 pairing grant bound to the future localhost GUI origin is supplied. | Grant is consumed once at `/opencodex-session`; returned GUI session + CSRF performs exact key POST; raw grant on `/api/keys` and replay fail; no transient credential persists. | -| P3-A3 | HTTP management URL with pairing grant, client `--allow-insecure-http`, and hub `remoteGui.allowInsecureHttp=true`. | Exchange/key issuance succeeds with explicit warning. Missing either opt-in refuses; admin credential over HTTP refuses before credential transmission. | +| P3-A3 | Non-loopback HTTP management URL with a pairing grant, including a tree that still carries a legacy `--allow-insecure-http` argument or a persisted `remoteGui.allowInsecureHttp: true`. | Refused before any credential is transmitted, in every combination. The removed CLI option is rejected as unknown rather than silently accepted, and the legacy config key grants nothing. The admin credential over HTTP is likewise refused before transmission. | | P3-A4 | `/readyz` returns `{protocol: 2, minimumClientProtocol: 2}` or status pending/failed. | Clear upgrade/not-ready error; zero key POSTs and zero local writes. | | P3-A5 | Key POST returns 401/403/409 or malformed/oversized JSON. | No token/catalog/journal/config writes and no secret in diagnostics. | | P3-A6 | Token, catalog, injector preflight, inject commit, or final role+state commit is fault-injected in turn. | Prior machine bytes are restored at every point; neither `runtimeRole=client` nor visible `client` remains; remote orphan cleanup status is explicit by safe key id only. | diff --git a/devlog/_plan/260827_remote_hub/070_phase5_deploy.md b/devlog/_plan/260827_remote_hub/070_phase5_deploy.md index 8b84ddf0fe..789850d9b2 100644 --- a/devlog/_plan/260827_remote_hub/070_phase5_deploy.md +++ b/devlog/_plan/260827_remote_hub/070_phase5_deploy.md @@ -97,8 +97,9 @@ reused and verified, not copied. ### 3.1 Config keys -Phase 1 owns `runtimeRole`; Phase 2 owns `hub.managementPublicOrigin`, -`remoteGui.allowedTailscaleUsers`, and `remoteGui.allowInsecureHttp`. Phase 5 adds only: +Phase 1 owns `runtimeRole`; Phase 2 owns `hub.managementPublicOrigin` and +`remoteGui.allowedTailscaleUsers`. (`remoteGui.allowInsecureHttp` was removed from the +Phase-2 contract; a persisted `true` grants nothing.) Phase 5 adds only: ```ts export interface OcxHubConfig { // existing Phase-2 interface, shown extended From 8980430c291d71077c434a48f0fc6dc8dedf8666 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 01:56:54 +0900 Subject: [PATCH 029/172] fix(remote): type catalog bytes over ArrayBuffer and scope the key-id warn assertion --- tests/server-auth.test.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/server-auth.test.ts b/tests/server-auth.test.ts index 94db2e0854..dfc595e01c 100644 --- a/tests/server-auth.test.ts +++ b/tests/server-auth.test.ts @@ -4131,8 +4131,15 @@ describe("GET /v1/catalog remote data plane", () => { }); expect(response.status).toBe(200); expect(response.headers.get("x-opencodex-key-id")).toBeNull(); - expect(warnSpy).toHaveBeenCalledTimes(1); - expect(warnSpy.mock.calls.flat().join(" ")).not.toContain(unsafeId); + // Other subsystems (config repair, provider migration) may warn during startup; + // this contract is about the remote-catalog warning specifically: exactly one, + // and it never echoes the unsafe id. + const remoteCatalogWarns = warnSpy.mock.calls + .map(call => call.map(String).join(" ")) + .filter(line => line.includes("[remote-catalog]")); + expect(remoteCatalogWarns).toHaveLength(1); + expect(remoteCatalogWarns[0]).not.toContain(unsafeId); + expect(warnSpy.mock.calls.flat().map(String).join(" ")).not.toContain(unsafeId); } finally { await server.stop(true); warnSpy.mockRestore(); From 4080bdb3402b7ba12e8c337c3768d888224c98fa Mon Sep 17 00:00:00 2001 From: jun Date: Tue, 1 Sep 2026 16:55:23 +0900 Subject: [PATCH 030/172] fix(design): close the review findings the first D-fix pass left open MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An adversarial review of dfae1da61 found four contract defects still live and one bookkeeping error. All are repaired here. D2 was fixed in Phase 1 but not in the phases that consume it. 030 still described ETag/If-None-Match as "shared" between the two catalog routes, and 050/080 still specified a client that persists an ETag, sends If-None-Match, and handles 304 — against a route that no longer emits a validator. An implementer following Phase 3 would have rebuilt exactly what Phase 1 deleted. The client now fetches unconditionally, and an unsolicited 304 is a protocol error rather than a cache hit, since the client never issued a conditional request. D3 overcorrected. Saying Origin must never be omitted contradicted the Phase-2 predicate, which deliberately allows Origin-absent safe GET/HEAD reads. The two rules are now separated: forwarding is verbatim whenever the browser sends a value, while requiring Origin stays the hub predicate's decision. The relay refuses only where it would otherwise have to invent a value. The owner test row now names each mutation method plus both Origin-absent branches, instead of testing only the pairing exchange. D4 stated the new identity-comparison rule while leaving the old "both accepted implies commit" rule intact three paragraphs above and in the activation matrix, so the document contradicted itself on the exact point the review raised. The obsolete text is replaced rather than supplemented. The recovery outcome is also made executable: the new secret is returned once and startup/status holds no management authority, so recovery cannot "resume" anything — it stops with evidence intact, and the next rotate, which does carry transient authority, confirms the stranded rotationId's abort before starting over. Bookkeeping: the earlier pass introduced a duplicate P2-A11 and appended three P6 rows after the verification section instead of into the activation matrix. The plaintext-bootstrap row is renumbered P2-A21 and the rotation rows are folded into the matrix, replacing the stale uncertain-commit row. --- .../030_phase1_protocol_catalog.md | 5 +- .../040_phase2_remote_session.md | 2 +- .../260827_remote_hub/050_phase3_connect.md | 13 ++--- .../260827_remote_hub/060_phase4_two_plane.md | 27 +++++++--- .../260827_remote_hub/080_phase6_hardening.md | 52 +++++++++++++------ 5 files changed, 66 insertions(+), 33 deletions(-) diff --git a/devlog/_plan/260827_remote_hub/030_phase1_protocol_catalog.md b/devlog/_plan/260827_remote_hub/030_phase1_protocol_catalog.md index 5ea1d99d5a..25ea959abc 100644 --- a/devlog/_plan/260827_remote_hub/030_phase1_protocol_catalog.md +++ b/devlog/_plan/260827_remote_hub/030_phase1_protocol_catalog.md @@ -65,8 +65,9 @@ runtime behavior. All code paths remain standalone-compatible when `runtimeRole` - Protocol-v1 metadata in every ready/pending/failed `/readyz` body. - A parser/compatibility predicate for future `ocx connect`, including additive-field tolerance for a dev hub paired with the latest released client. -- Shared catalog serialization, ETag, `If-None-Match`, size cap, data-plane admission, and - configured-key-only `x-opencodex-key-id` attribution. +- Shared catalog serialization, size cap, data-plane admission, and configured-key-only + `x-opencodex-key-id` attribution. The byte-derived ETag and `If-None-Match` handling + belong to `/api/catalog` alone; `/v1/catalog` has no validator (§ above). - Route placement before the unknown-`/v1/*` JSON-404 guard. - Focused and full remote-only verification commands. diff --git a/devlog/_plan/260827_remote_hub/040_phase2_remote_session.md b/devlog/_plan/260827_remote_hub/040_phase2_remote_session.md index 0324291d6e..14d23c0842 100644 --- a/devlog/_plan/260827_remote_hub/040_phase2_remote_session.md +++ b/devlog/_plan/260827_remote_hub/040_phase2_remote_session.md @@ -462,7 +462,6 @@ sessions renew at most once per request. | P2-A08 | HTTPS POST bootstrap with fresh grant and exact `Origin`. | Grant is deleted and one `pairing` session is returned with both origin meta values. | | P2-A09 | Replay consumed grant; use expired grant, wrong Origin, wrong server destination, data key, admin token, or session token in place of grant. | No session for every case; replay and alternate credentials cannot enter the exchange branch. | | P2-A10 | Non-loopback HTTP pairing exchange, with and without a legacy persisted `remoteGui.allowInsecureHttp: true`. | Refused in both cases, before the grant is read, so the grant survives for a later HTTPS exchange. The legacy key is dropped with a warning and grants nothing. Automatic Tailscale issuance remains refused on HTTP. | -| P2-A11 | Plaintext HTTP request for the session bootstrap on a non-loopback bind. | Response carries no grant, session, admin token, or client key — only an unauthenticated error naming the required scheme. | | P2-A11 | Authorized remote safe read immediately before expiry. | Full origin predicate passes and expiry slides to `now + 12h`; token/CSRF unchanged. | | P2-A12 | Wrong destination, wrong claimed browser origin, wrong browser `Origin`, absent/wrong CSRF mutation, and an expired session. | 401 and expiry remains unchanged/deleted as applicable; principal is never projected as `gui-session`. | | P2-A13 | Admin token calls ordinary management, pairing-grant creation, bootstrap exchange, and then a consent route; valid remote GUI session calls ordinary/consent routes with correct CSRF. | Admin remains ordinary management-capable but the other three are refused; remote session reaches consent route. No admin-to-grant or admin-to-session exchange exists. | @@ -473,6 +472,7 @@ sessions renew at most once per request. | P2-A18 | Run `ocx gui pair --origin ` with an explicit allowed origin, then missing `--origin`, malformed/disallowed origin, `--json`, extra args, stale target, failed attestation, and capability/API failure. | Valid cases create one grant and print once; every absent/invalid origin fails with no default and without falling back to admin auth or echoing secrets; no grant appears in argv/config/disk/log fixtures. | | P2-A19 | Run native-profile mutation suite with admin, malformed remote session, and valid remote session. | Existing consent boundary remains: only the valid session + correct origin + CSRF dispatches the mutation. | | P2-A20 | Run import-graph and synchronous-window guard. | No protected core import reaches GUI-session code; `startServer` remains synchronous and activation ordering is unchanged. | +| P2-A21 | Plaintext HTTP request for the session bootstrap on a non-loopback bind. | Response carries no grant, session, admin token, or client key — only an unauthenticated error naming the required scheme. | ## 10. Verification — remote only on `lidge-ai` diff --git a/devlog/_plan/260827_remote_hub/050_phase3_connect.md b/devlog/_plan/260827_remote_hub/050_phase3_connect.md index adfa817e56..6eaec757f8 100644 --- a/devlog/_plan/260827_remote_hub/050_phase3_connect.md +++ b/devlog/_plan/260827_remote_hub/050_phase3_connect.md @@ -317,8 +317,8 @@ export function issueClientKey( export function downloadClientCatalog( serverUrl: string, admissionToken: string, - options?: { etag?: string; timeoutMs?: number; maxBytes?: number; fetchImpl?: typeof fetch }, -): Promise<{ kind: "fresh"; body: string; etag?: string } | { kind: "not-modified" }>; + options?: { timeoutMs?: number; maxBytes?: number; fetchImpl?: typeof fetch }, +): Promise<{ kind: "fresh"; body: string }>; ``` `fetchHubReady()` parses through Phase 1's `parseRemoteReadyMetadata()` and evaluates @@ -456,9 +456,10 @@ connected `ocx sync` is the sole apply path. - `client.kind === invalid`: exit non-zero before proxy discovery, provider discovery, catalog write, or injection. - `client.kind === connected`: read and fingerprint-check the service token, request - `/v1/catalog` with `If-None-Match`, and inject the saved `CodexRoutingTarget`. -- Connected 304 reuses the existing catalog only if it is a bounded regular file and - the configured catalog path is the expected absolute path. + `/v1/catalog` unconditionally, and inject the saved `CodexRoutingTarget`. + `/v1/catalog` carries no validator (Phase 1, D2), so the client sends no + `If-None-Match` and never receives a 304. There is no conditional-fetch state to keep + correct, and no way for a cached representation to cross client keys. - Connected timeout/5xx keeps last-known-good catalog and reports stale age; it does not gather local providers. Missing/changed token file and 401 are hard failures and do not inject or fall back. @@ -535,7 +536,7 @@ No test sends live hub traffic or reads the developer's homes. | P3-A5 | Key POST returns 401/403/409 or malformed/oversized JSON. | No token/catalog/journal/config writes and no secret in diagnostics. | | P3-A6 | Token, catalog, injector preflight, inject commit, or final role+state commit is fault-injected in turn. | Prior machine bytes are restored at every point; neither `runtimeRole=client` nor visible `client` remains; remote orphan cleanup status is explicit by safe key id only. | | P3-A7 | Existing standalone config runs every current injector golden. | Output bytes are identical; no connected-only env/header/config key appears. | -| P3-A8 | Connected state plus valid token; hub catalog returns 200 then 304. | First sync atomically updates/injects; second uses last-known-good and ETag; local provider gather fake is never called. | +| P3-A8 | Connected state plus valid token; two consecutive syncs. | Each sync fetches unconditionally and atomically updates/injects; no request carries `If-None-Match`; a hub that answered 304 anyway is treated as a protocol error rather than as an empty catalog. The local provider gather fake is never called. | | P3-A9 | Connected state with hub down, 401, missing token, changed token, or malformed-present client config. | No local-provider fallback and no new local catalog; timeout keeps LKG as stale, credential/state errors fail hard. | | P3-A10 | Connected Claude launch with no user Anthropic overrides. | Child receives hub base and client token; no local proxy is started; gateway cache uses hub `/v1/models`. | | P3-A11 | Connected Claude launch with user-owned different `ANTHROPIC_BASE_URL`. | User destination wins and the hub admission token is absent from child env. | diff --git a/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md b/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md index 83d0006b27..d82d1c7727 100644 --- a/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md +++ b/devlog/_plan/260827_remote_hub/060_phase4_two_plane.md @@ -302,10 +302,21 @@ Strip `Host`, connection/hop-by-hop headers, machine auth headers, proxy credent cookies, and forwarding headers. Forward only the bounded management header allowlist, including hub session, GUI-origin, CSRF, and content type. -Forward the browser's `Origin` value **verbatim on every allowed session-authenticated -request**: the `POST /opencodex-session` exchange, the `GET` bootstrap, and every allowed -`/api/` method including `POST`, `PUT`, `PATCH`, and `DELETE`. Never synthesize it from -the hub URL, the localhost bind, or the GUI-origin header, and never omit it. +Two separate rules govern `Origin`, and conflating them is what produced the earlier defect. + +**Forwarding.** Whenever the browser sends `Origin`, forward that value verbatim, on every +allowed session-authenticated request: the `POST /opencodex-session` exchange, the `GET` +bootstrap, and every allowed `/api/` method including `POST`, `PUT`, `PATCH`, and +`DELETE`. Never synthesize it from the hub URL, the localhost bind, or the GUI-origin +header, and never drop a value the browser did send. + +**Requiring.** The hub's own predicate (Phase 2 §5.2) decides when `Origin` must be +present: mandatory for the pairing exchange and for every mutation, optional for safe +same-browser `GET`/`HEAD` reads. The relay does not tighten or loosen that predicate; a +safe read whose browser sent no `Origin` still relays and still succeeds. + +So the relay refuses only when it would otherwise have to invent a value: a mutation +arriving without `Origin` is rejected rather than given a synthesized one. A previous revision forwarded `Origin` only for the exact `POST /opencodex-session` exchange. That is both a functional and a security defect. The minted GUI session is @@ -316,8 +327,8 @@ breaks every write path it is supposed to carry. Repairing that by synthesizing the hub's CSRF check would be validating the relay against itself. The browser value is the only admissible source, so it is forwarded unchanged or the request does not go. -When the browser sends no `Origin` on a request that requires it, the relay refuses -rather than inventing one. +When the browser sends no `Origin` on a request the Phase-2 predicate requires it for, +the relay refuses rather than inventing one. Response headers are similarly allowlisted; `Set-Cookie` and hop-by-hop headers are never returned. Request and response bodies have named constants and abort on overflow. No URL query, auth header, body, or response body is logged. @@ -466,7 +477,7 @@ appearing after disconnect. | Test file | Required cases | |---|---| | `tests/client-machine-listener.test.ts` (NEW) | IPv4 loopback bind; GUI/bootstrap; exact allowlist; every `/v1/*` 404; unknown/wrong-method 404; safe GET auth; mutation Origin/CSRF; status redaction; sync success/failure; shim actions; disconnect offline; recycle to standalone; invalid state refuses startup; no provider/timer fake invoked. | -| `tests/client-hub-relay.test.ts` (NEW) | Relay disabled 404; direct mode 404; fixed host/path; exact `POST /api/machine/hub-relay/opencodex-session` reaches only hub `POST /opencodex-session` and forwards browser `Origin` verbatim; direct/encoded traversal and authority injection rejected; redirects rejected; request/response caps; hop-by-hop/cookie/forwarded/machine headers stripped; hub auth retained; timeout/abort; no body/header log. | +| `tests/client-hub-relay.test.ts` (NEW) | Relay disabled 404; direct mode 404; fixed host/path; exact `POST /api/machine/hub-relay/opencodex-session` reaches only hub `POST /opencodex-session`; browser `Origin` forwarded byte-for-byte on the pairing exchange, the GET bootstrap, and each allowed `/api/` POST, PUT, PATCH, and DELETE; a mutation whose browser sent no `Origin` is refused without contacting the hub and without a synthesized value; a safe GET/HEAD whose browser sent no `Origin` still relays and succeeds; direct/encoded traversal and authority injection rejected; redirects rejected; request/response caps; hop-by-hop/cookie/forwarded/machine headers stripped; hub auth retained; timeout/abort; no body/header log. | | `tests/cli-start-journal-order.test.ts` | Matching durable client journal survives start; missing/mismatched client owner restores; connected branch never starts full server; disconnected branch remains current. | | `tests/api-usage.test.ts` | Exact `apiKeyId` response/echo; old and other-key rows excluded; no match; combined filters; filtered request cannot poison cache; unfiltered next request remains whole hub. | | `tests/usage-summary.test.ts` | Pure projection totals/days/models/providers/accounts consistency; exact-case id; combo attempts; absent id; provider/model/key cross-product. | @@ -488,7 +499,7 @@ appearing after disconnect. | P4-A3 | GET status/clients with valid loopback GUI session, then without it. | Valid request returns redacted DTO; missing/admin-only/expired/wrong-origin session is rejected and no secret/fingerprint leaks. | | P4-A4 | POST sync/shim/disconnect with valid session but missing/wrong CSRF or browser Origin. | Mutation is rejected before work; exact session+Origin+CSRF reaches the handler. Admin token never becomes GUI session. | | P4-A4b | Relay every allowed session-authenticated method — GET bootstrap, POST `/opencodex-session`, and `/api/` POST, PUT, PATCH, DELETE — from a browser origin the hub allows. | Each request arrives at the hub carrying the browser's `Origin` byte-for-byte. No case is missing `Origin`, and no case carries a value the browser did not send. | -| P4-A4c | Relay an allowed mutation whose browser request has no `Origin`. | The relay refuses without contacting the hub, and does not synthesize an `Origin` from the hub URL, the localhost bind, or the GUI-origin header. | +| P4-A4c | Relay an allowed mutation whose browser request has no `Origin`, then a safe `GET` whose browser request has no `Origin`. | The mutation is refused without contacting the hub and without a synthesized `Origin`. The safe read is relayed unchanged and succeeds, preserving the Phase-2 §5.2 allowance. | | P4-A5 | Connected direct transport with Phase-2 session issued by an exact `remoteGui.allowedTailscaleUsers` match or a consumed pairing grant; hub CORS includes the localhost browser origin. | Shared requests go directly to exact hub origin with hub headers only; machine pages remain localhost; CORS/session validation succeeds. A non-allowlisted Tailscale identity mints no session and gets no local fallback. | | P4-A6 | Connected relay transport and valid machine + hub sessions. | Browser sends dual auth domains; relay validates machine session, strips custom headers, forwards hub session only to fixed hub target, and rejects redirect/SSRF variants. | | P4-A7 | Relay path requested while transport is direct/disabled or caller supplies host/scheme/traversal. | Default 404/refusal before outbound fetch; no credential or body is logged. | diff --git a/devlog/_plan/260827_remote_hub/080_phase6_hardening.md b/devlog/_plan/260827_remote_hub/080_phase6_hardening.md index 69daa58b47..84d4dd2565 100644 --- a/devlog/_plan/260827_remote_hub/080_phase6_hardening.md +++ b/devlog/_plan/260827_remote_hub/080_phase6_hardening.md @@ -310,10 +310,11 @@ or replace the backup directly. is 401. After verified commit, delete `.prev` and clear `pendingOperation`. 6. If steps 2–5 fail before a confirmed commit, restore the old token atomically through `restoreTokenBackup`, abort the pending rotation, then delete the backup and clear the - operation. If commit outcome is uncertain, probe with both keys. New+old both accepted means - issuance completed and overlap is still pending, so commit the new key with the stored - `rotationId`; new-only accepted means commit already took effect; old-only accepted restores - and aborts. Never replay commit without this evidence. + operation — but only when the restore and abort are both confirmed. If the commit outcome + is uncertain, follow the recovery gate below rather than probing directly: the identity + comparison comes first, because two candidates holding the same key both probe + successfully and would otherwise be read as a completed issuance. Never replay commit, + and never delete a candidate, without evidence that survives that comparison. On startup/status, `src/client/state.ts` treats a rotate `pendingOperation` as a recovery gate: verify that `oldKeyBackupPath` is exactly `.prev` and require an owner-only regular @@ -324,8 +325,21 @@ file. but before the token was replaced. Both candidates are the old key, so both probe successfully — and a "both accepted" rule would read that as a completed issuance and commit a rotation that never happened, permanently losing the new key. Identical candidates -therefore mean pre-replacement: never commit, restore nothing, and resume the rotation from -the beginning. +therefore mean pre-replacement: never commit and restore nothing. + +What happens next is constrained by two facts. The new secret is returned exactly once, so +if it was issued it is already unrecoverable from disk; and startup/status holds no +management authority, so it cannot ask the hub anything. Recovery at startup/status +therefore **stops** — it does not "resume," because nothing at that point is able to. +It reports the exact state, leaves both candidates and `pendingOperation` intact, and +names the command the operator runs next. + +Resumption belongs to the next `ocx connect rotate`, which carries fresh transient +authority. That command sees the stored `rotationId`, confirms its abort with the hub, +and only then starts a new rotation. It is not blocked by the `already-pending` rule +(§ rotate contract), because confirming and clearing the stranded operation is precisely +what it is doing. If the abort cannot be confirmed, it stops with the evidence preserved +rather than starting a second rotation on top of an unresolved one. Only when the candidates differ does probing decide anything, and only a confirmed authority may act: @@ -463,7 +477,9 @@ leaf that depends only on `src/lib/bounded-body.ts`. Validation order: -1. Status/redirect: only 200 or valid 304; redirect is refused, not followed. +1. Status/redirect: only 200; redirect is refused, not followed. `/v1/catalog` carries no + validator (Phase 1, D2), so the client sends no conditional request and a 304 is a + protocol error rather than a cache hit. 2. Content type is JSON-compatible; content length above cap rejects early, but streamed bytes are still counted because length may be absent or false. 3. Read at most cap+1 decompressed bytes; exactly cap is allowed, one byte over cancels/discards. @@ -473,13 +489,15 @@ Validation order: required shape passes. 6. Serialize/write only after complete validation. Failed refresh retains the exact LKG bytes and stale age; no local provider fallback and no partial file. -7. A 304 without an existing validated LKG triggers one unconditional refetch; a second 304 is a - protocol error, not an empty catalog. +7. A 304 is a protocol error in every case. The client never issued a conditional request, + so a hub answering 304 is either misconfigured or being impersonated; treat it as a + failed refresh that retains the exact LKG bytes, never as an empty catalog. Adversarial tests include: forged small Content-Length with oversized chunks, gzip/decompressed oversize fixture, exact-cap and cap+1, fragmented trickle, malformed/truncated/UTF-8 JSON, null, array top level, missing/non-array models, 2,001 rows, non-object row, empty/control/duplicate slug, -unexpected future fields, stale/mismatched ETag, and filesystem write failure after validation. +unexpected future fields, an unsolicited 304, an unsolicited `ETag` on the 200, and +filesystem write failure after validation. Every rejection asserts token/catalog/state/journal bytes are unchanged. ## 8. Relay SSRF and header-smuggling negatives @@ -551,7 +569,11 @@ Phase 6 extends them rather than creating parallel “hardening2” files. | second start | Existing unexpired pending rotation | 409; no third secret/state change. | | client commit | New key written + authenticated catalog succeeds | Pending promoted atomically; old next request 401; id/usage bucket stable; `.prev` deleted and pending operation cleared; sessions/grants unchanged. | | client write/probe fail | Fail backup/temp write, hardening, rename, or new-key probe | Old file restored/unchanged from `.prev`; pending aborted or expires; old key remains valid. | -| uncertain commit/crash | Drop commit response or restart with pending operation | Probe current+`.prev`; both accepted commits the current new key with stored `rotationId`, new-only finalizes committed state, old-only restores+aborts, and both rejected stops without deletion. | +| uncertain commit/crash | Drop commit response or restart with pending operation | Compare candidate identities FIRST. Differing candidates: both accepted commits the current new key with stored `rotationId`, new-only finalizes committed state, old-only restores+aborts, both rejected stops without deletion. | +| pre-replacement crash | Persist `pendingOperation`, then stop before the token is replaced, so the live token and `.prev` hold the same key | Identical candidates are detected before any probe. No commit, no restore, no deletion; both files and `pendingOperation` survive and recovery stops with the operator instruction. The old "both probes accepted implies commit" reading is what this row keeps dead — it would commit a rotation that never happened and lose the new key permanently. | +| unconfirmed abort | Rotation reaches installed-new-token state, then the abort request fails transiently | Neither candidate is deleted and `pendingOperation` survives naming the unconfirmed step. No generation is restored on unconfirmed authority. | +| status during rotation | Run `ocx connect status` while `rotateConnectedClientKey` awaits `/api/keys/rotate` | The in-flight `.prev` backup is not deleted and the rotation completes normally. | +| stranded-operation resume | After a pre-replacement crash, run `ocx connect rotate` | The stored `rotationId` is confirmed aborted with the hub before a new rotation starts; `already-pending` does not block this path. An unconfirmable abort stops with evidence preserved rather than starting a second rotation. | | pending expiry | Fake clock past 10 minutes | Pending rejected/removed; old accepted. | | connected operator revoke | Valid connected state + `ocx connect revoke --admin-token-stdin` | CLI reads the issuance-derived `apiKeyId` from state, accepts no id argument, and revokes that key; sessions/grants remain unchanged. | | post-disconnect revoke | Disconnect clears local client state while its hub key remains | CLI revoke refuses before any request; output points to hub GUI **Integrations → API Keys**, the sole post-disconnect revocation path. | @@ -564,7 +586,7 @@ Phase 6 extends them rather than creating parallel “hardening2” files. | malformed protocol | Invalid `/readyz` fields | Malformed error; no catalog/token/inject/state write. | | oversized catalog | Content-Length lie or chunked cap+1 | Cancel/discard, LKG unchanged, no fallback. | | malformed/schema catalog | Each §7 shape | Precise safe error class, LKG unchanged. | -| 304 no LKG | Empty cache + 304 | One unconditional retry; second 304 errors. | +| unsolicited 304 | Hub answers 304 to an unconditional request, with and without an existing LKG | Treated as a protocol error in both cases; LKG unchanged where present, no empty catalog written, no local provider fallback. | | relay URL override | Absolute/scheme-relative/encoded authority path | Reject before fetch; fixed hub sees zero requests. | | relay redirect | Fixed hub returns 3xx to attacker | No follow, no credential at target. | | request smuggling | Raw CL/TE, duplicate CL, Connection-nominated secret header | Reject/strip before fetch. | @@ -581,7 +603,8 @@ The Remote Hub guide in all eight locales must cover: - connected usage = hub store filtered to this `apiKeyId`; disconnected usage = local store; no mirroring; - loopback management ingress, Tailscale Serve, exact `allowedTailscaleUsers`, pairing, and the - explicit insecure-HTTP warning; + requirement that pairing runs over loopback or HTTPS only — plaintext HTTP carries no + credential and there is no opt-in that changes this; - admin token ordinary-management scope and permanent inability to mint consent sessions; - systemd/launchd, Docker volume/secret/probes, headless OAuth, rotation, rollback, and protocol upgrade errors; @@ -717,6 +740,3 @@ the matching CI partition before classifying it; never call a red result environ Final live evidence runs on `clisu-oracle`/MacBook per 070 §8 after the remote gates. It proves health, readiness, authenticated catalog, one routed response, remote session, consent refusal, rotation, usage slice, disconnect/local store, rollback, and both constructible protocol directions. -| P6-A20 | Persist `pendingOperation`, then stop before the token is replaced so the live token and `.prev` hold the same key. Restart. | Recovery detects identical candidates before probing, refuses to commit, leaves both files intact, and resumes the rotation. The pre-fix "both probes accepted implies commit" rule is what this row exists to keep dead. | -| P6-A21 | Rotation reaches installed-new-token state, then the abort request fails transiently. | Neither candidate is deleted and `pendingOperation` survives with the unconfirmed step named. No generation is restored on unconfirmed authority. | -| P6-A22 | Run `ocx connect status` while `rotateConnectedClientKey` is awaiting `/api/keys/rotate`. | The in-flight `.prev` backup is not deleted and the rotation completes normally. | From 4fdd54d4613f73ae8cf3a3bee5d3835a10f3d99e Mon Sep 17 00:00:00 2001 From: jun Date: Tue, 1 Sep 2026 17:10:52 +0900 Subject: [PATCH 031/172] fix(remote): reconcile the phase-one catalog contract with the landed /v1/catalog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #2979 landed /v1/catalog on dev while this phase was open, so the rebase had to decide, per behavior, which side wins rather than taking one branch wholesale. Taken from dev, because narrowing shipped behavior is a regression, not a hardening: GET+HEAD instead of GET-only; a 256 MiB cap with 507 instead of 32 MiB with 503, since a supported 2,000-model catalog serializes to roughly 92 MB and the smaller cap rejected valid input; a malformed persisted catalog rendering as 404 rather than 500, which keeps "your file is corrupt" indistinguishable from "you have no catalog"; and x-api-key accepted, matching /v1/models and the AUTH_MATRIX row #809 shipped. This route forwards no caller credential upstream, so x-api-key carries no extra authority — rejecting it would only 401 Anthropic-SDK clients holding a valid data credential. Taken from this phase, because dev has no equivalent: the x-opencodex-key-id echo that names which configured key was admitted. dev defined withRemoteCatalogKeyId but never called it, so multi-key attribution was dead code; it is now wired into the route, scoped to configured keys, with an unsafe id omitted rather than sanitized and its warning never repeating the id. Taken from neither, per D2 in the design unit: the route emits Cache-Control: no-store with no ETag and no 304. dev's private,no-cache plus a strong byte-derived ETag is unsafe on a body that varies by key identity — no-cache permits storage and forces revalidation, and the revalidation is what crosses identities. /api/catalog keeps its validator; it is management-authenticated, loopback-scoped, and identity-invariant. The duplicate AUTH_MATRIX row this phase added is dropped: dev already had one for /v1/catalog, and the two disagreed on x-api-key, so the matrix contradicted itself and the live-server check failed against whichever row it read first. --- src/server/auth-cors.ts | 1 - src/server/index.ts | 63 +++++++++++++++++++++++++------ tests/api-catalog-route.test.ts | 36 ++++++++++++------ tests/server-auth.test.ts | 66 ++++++++++++++++----------------- 4 files changed, 107 insertions(+), 59 deletions(-) diff --git a/src/server/auth-cors.ts b/src/server/auth-cors.ts index 2bffde448c..0174794c6a 100644 --- a/src/server/auth-cors.ts +++ b/src/server/auth-cors.ts @@ -403,7 +403,6 @@ export const AUTH_MATRIX: readonly ApiAuthMatrixRow[] = [ // passthrough, so the two bearer domains still never mix. `x-api-key` is still rejected. { endpoint: "/v1/responses", bearer: "accepted", dedicated: "accepted", xApiKey: "rejected" }, { endpoint: "/v1/chat/completions", bearer: "accepted", dedicated: "accepted", xApiKey: "rejected" }, - { endpoint: "/v1/catalog", bearer: "accepted", dedicated: "accepted", xApiKey: "rejected" }, { endpoint: "/v1/messages", bearer: "accepted", dedicated: "accepted", xApiKey: "accepted" }, { endpoint: "/v1/models", bearer: "accepted", dedicated: "accepted", xApiKey: "accepted" }, // #809: least-privilege catalog read for remote Codex clients. Same admission set as diff --git a/src/server/index.ts b/src/server/index.ts index e0e30b618d..35c7361be8 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -155,6 +155,7 @@ import { resolveApiAuth, resolveResponsesApiAuth, requestPolicyView, + type DataPlaneAdmission, type RequestPolicyView, safeConfigDTO, setCorsOrigin, @@ -214,6 +215,33 @@ import { readyProtocolMetadata } from "../remote/protocol"; export const MAX_WS_FRAME_BYTES = 50 * 1024 * 1024; const WEBSOCKET_IDLE_TIMEOUT_SECONDS = 0; + +// Header-safe by construction: a key id reaches a response header, so anything outside this +// class could inject a header break or a control character into a response we control. +const REMOTE_CATALOG_KEY_ID_PATTERN = /^[A-Za-z0-9._-]{1,64}$/; + +/** + * Name WHICH configured credential was admitted, so a multi-key operator can attribute a + * catalog read. + * + * Scoped to configured keys on purpose: an environment token or a loopback bind has no key + * to name, and emitting one anyway would invent an attribution that does not exist. 200 only + * — this route emits no validator and therefore never answers 304. + * + * An id that fails the header-safe pattern is omitted rather than sanitized, with one warning + * that does NOT repeat the id: logging the offending value is how a malformed id becomes a + * log-injection vector instead of a dropped header. + */ +function withRemoteCatalogKeyId(response: Response, admission: DataPlaneAdmission): Response { + if (response.status !== 200 || admission.kind !== "configured") return response; + if (!REMOTE_CATALOG_KEY_ID_PATTERN.test(admission.keyId)) { + console.warn("[remote-catalog] configured API key id is not header-safe; omitting x-opencodex-key-id"); + return response; + } + response.headers.set("x-opencodex-key-id", admission.keyId); + return response; +} + const LIVE_SIDEBAND_PENDING_MAX = 32; const LIVE_SIDEBAND_PENDING_BYTES_MAX = 1024 * 1024; const LIVE_SIDEBAND_CLOSE_FALLBACK_MS = 1_000; @@ -1123,23 +1151,34 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server = { "content-type": "application/json", - // Identity-varying content behind a credential: never let a shared cache keep it. - "cache-control": "private, no-cache", + // Identity-varying content behind a credential: never let a shared cache keep it, + // and never hand out a validator it could revalidate with. `no-cache` alone does + // not prevent storage — it forces revalidation, and the revalidation is exactly + // what would cross identities here, because this body varies by key type and key + // id while the ETag would be derived from bytes alone. A store keyed on URL plus + // validator could then serve one credential's representation to another. Proving + // an identity-partitioned cache key across every intermediary in the path is a + // much larger commitment than the bandwidth a 304 saves on this payload, so this + // route declines the trade: no-store, no ETag, no 304. + // + // GET /api/catalog keeps its validator. That route is management-authenticated + // and loopback-scoped, and its representation does not vary by data-key identity. + "cache-control": "no-store", }; - if (serialized.etag) headers.ETag = serialized.etag; const version = await persistedCodexVersion(); if (version) headers["x-opencodex-codex-version"] = version; - // Conditional GET: a client that already holds these bytes re-validates cheaply. - const ifNoneMatch = req.headers.get("if-none-match")?.trim(); - if (serialized.etag && ifNoneMatch && ifNoneMatch === serialized.etag) { - return withCors(new Response(null, { status: 304, headers }), req, policy); - } + // No conditional handling: with no validator emitted, an If-None-Match on this route + // can only have been guessed or copied from elsewhere, and honoring it would + // reintroduce the cross-identity path above. Every request gets the full body. if (serialized.bytes !== undefined) headers["content-length"] = String(serialized.bytes); // HEAD returns identical status and headers with no body. - return withCors( - new Response(req.method === "HEAD" ? null : serialized.body, { status: 200, headers }), - req, - policy, + return withRemoteCatalogKeyId( + withCors( + new Response(req.method === "HEAD" ? null : serialized.body, { status: 200, headers }), + req, + policy, + ), + admission, ); } diff --git a/tests/api-catalog-route.test.ts b/tests/api-catalog-route.test.ts index f639d61f95..f46632a079 100644 --- a/tests/api-catalog-route.test.ts +++ b/tests/api-catalog-route.test.ts @@ -87,7 +87,13 @@ describe("GET /api/catalog route (#709)", () => { expect(await response!.json()).toEqual({ error: "catalog not found" }); }); - test("returns a bounded server failure for a malformed persisted catalog", async () => { + test("renders a malformed persisted catalog as absent rather than leaking the parse failure", async () => { + // The management route deliberately collapses unreadable, absent, and malformed + // into one 404. An earlier revision of this phase threw on malformed JSON and + // asserted 500 here, which distinguishes "your catalog file is corrupt" from + // "you have no catalog" to any caller that can reach the route. The shared + // serializer returns `{ body: null }` for all three so no route can accidentally + // reintroduce that distinction. isolatedCodexHome = installIsolatedCodexHome("ocx-api-catalog-malformed-"); writeFileSync(join(isolatedCodexHome.path, "opencodex-catalog.json"), '{"models":'); const url = new URL("http://localhost/api/catalog"); @@ -96,8 +102,8 @@ describe("GET /api/catalog route (#709)", () => { url, loadConfig(), ); - expect(response?.status).toBe(500); - expect(await response!.json()).toEqual({ error: "catalog unavailable" }); + expect(response?.status).toBe(404); + expect(await response!.json()).toEqual({ error: "catalog not found" }); }); }); @@ -147,9 +153,12 @@ describe("GET|HEAD /v1/catalog least-privilege data-plane route (#809)", () => { expect(res.status).toBe(200); const body = await res.text(); expect(JSON.parse(body)).toEqual(catalogFixture); - expect(res.headers.get("cache-control")).toBe("private, no-cache"); - const etag = res.headers.get("etag"); - expect(etag).toBeTruthy(); + // No validator on this plane: the body varies by key identity, so a shared strong + // ETag would let a store revalidate one credential's representation for another. + // `no-cache` did not prevent that — it permits storage and forces revalidation, and + // the revalidation is the crossing. See the note in src/server/index.ts. + expect(res.headers.get("cache-control")).toBe("no-store"); + expect(res.headers.get("etag")).toBeNull(); // The whole point of the shared serializer: the two planes must not drift. const mgmtUrl = new URL("http://localhost/api/catalog"); @@ -161,12 +170,16 @@ describe("GET|HEAD /v1/catalog least-privilege data-plane route (#809)", () => { expect(mgmt?.status).toBe(200); expect(await mgmt!.text()).toBe(body); - // Conditional GET re-validates without resending the payload. + // A conditional request cannot succeed here, because no validator was ever handed + // out to build one from. Even a client that guesses the management route's ETag gets + // the full body rather than a 304. + const mgmtEtag = mgmt!.headers.get("etag"); + expect(mgmtEtag).toBeTruthy(); const revalidated = await fetch(new URL("/v1/catalog", server.url), { - headers: { "x-opencodex-api-key": DATA_KEY, "if-none-match": etag! }, + headers: { "x-opencodex-api-key": DATA_KEY, "if-none-match": mgmtEtag! }, }); - expect(revalidated.status).toBe(304); - expect(await revalidated.text()).toBe(""); + expect(revalidated.status).toBe(200); + expect(await revalidated.text()).toBe(body); // HEAD is the same status and headers with no body. const head = await fetch(new URL("/v1/catalog", server.url), { @@ -174,7 +187,8 @@ describe("GET|HEAD /v1/catalog least-privilege data-plane route (#809)", () => { headers: { "x-opencodex-api-key": DATA_KEY }, }); expect(head.status).toBe(200); - expect(head.headers.get("etag")).toBe(etag); + expect(head.headers.get("etag")).toBeNull(); + expect(head.headers.get("cache-control")).toBe("no-store"); expect(await head.text()).toBe(""); } finally { await server.stop(true); diff --git a/tests/server-auth.test.ts b/tests/server-auth.test.ts index dfc595e01c..e4030f8a59 100644 --- a/tests/server-auth.test.ts +++ b/tests/server-auth.test.ts @@ -49,12 +49,6 @@ import { LOCAL_PROVIDER_RELOAD_CAPABILITY_VERSION } from "../src/lib/local-provi import { resetCodexModelEntitlementCacheForTests } from "../src/codex/model-entitlements"; import { getDebugLogEntries, resetDebugLogBufferForTests } from "../src/lib/debug-log-buffer"; import { resetDebugSettingsForTests, setDebugSettings } from "../src/lib/debug-settings"; -import { - MAX_REMOTE_CATALOG_BYTES, - catalogDataPlaneResponse, - type SerializedCatalog, -} from "../src/server/catalog-download"; - import { watchdogMs } from "./helpers/ci-watchdog"; const previousApiToken = process.env.OPENCODEX_API_AUTH_TOKEN; const previousOpencodexHome = process.env.OPENCODEX_HOME; @@ -4058,9 +4052,17 @@ describe("GET /v1/catalog remote data plane", () => { expect(remote.status).toBe(200); expect(remoteBytes).toEqual(managementBytes); expect(new TextDecoder().decode(remoteBytes)).toBe(REMOTE_CATALOG_BYTES); - const expectedEtag = `"sha256-${createHash("sha256").update(remoteBytes).digest("base64url")}"`; - expect(remote.headers.get("etag")).toBe(expectedEtag); - expect(remote.headers.get("cache-control")).toBe("private, no-cache"); + // Management ETag spelling is hex, per the shipped catalogEtag() in + // src/server/catalog-download.ts. An earlier revision of this phase used a + // "sha256-" spelling from its own serializer, which no longer exists. + const expectedEtag = `"${createHash("sha256").update(remoteBytes).digest("hex")}"`; + // The bytes are identical across planes, but the caching contract is not: the + // management route may carry a validator because its representation does not vary by + // data-key identity, while this one must not. Asserting the management ETag here keeps + // the byte-identity claim honest without implying the remote route offers one. + expect(management.headers.get("etag")).toBe(expectedEtag); + expect(remote.headers.get("etag")).toBeNull(); + expect(remote.headers.get("cache-control")).toBe("no-store"); expect(remote.headers.get("x-opencodex-key-id")).toBe("remote-key"); } finally { await server.stop(true); @@ -4075,7 +4077,13 @@ describe("GET /v1/catalog remote data plane", () => { const cases = [ [{ "x-opencodex-api-key": REMOTE_DATA_KEY }, 200, "remote-key"], [{ authorization: `Bearer ${REMOTE_DATA_KEY}` }, 200, "remote-key"], - [{ "x-api-key": REMOTE_DATA_KEY }, 401, null], + // Accepted, matching /v1/models and the AUTH_MATRIX row this route shipped with in + // #809. An earlier revision of this phase rejected x-api-key here for least-privilege + // reasons, but this route forwards no caller credential upstream, so the header + // carries no extra authority — and rejecting it 401s Anthropic-SDK clients holding a + // perfectly valid data credential. The narrowing was a behavior regression against + // shipped code, not a hardening. + [{ "x-api-key": REMOTE_DATA_KEY }, 200, "remote-key"], [{ authorization: "Bearer foreign-key" }, 401, null], [{ authorization: `Bearer ${configuredAdminToken() ?? "missing-admin"}` }, 401, null], [{ "x-opencodex-api-key": REMOTE_DATA_KEY, origin: "https://attacker.test" }, 403, null], @@ -4146,7 +4154,12 @@ describe("GET /v1/catalog remote data plane", () => { } }); - test("strong, weak, list, and wildcard validators return bodyless 304", async () => { + test("no conditional request can elicit a 304, and no validator is offered to build one from", async () => { + // The response body varies by key identity, so a shared strong validator would let a + // store revalidate one identity's representation for another. The route therefore + // carries no ETag at all: there is nothing for a client to send back, and every + // If-None-Match spelling — including ones that would match a validator if one existed — + // gets the full body. An earlier revision of this phase asserted the opposite here. saveConfig(remoteCatalogConfig()); writeRemoteCatalog(); const server = startServer(0); @@ -4154,41 +4167,24 @@ describe("GET /v1/catalog remote data plane", () => { const first = await fetch(new URL("/v1/catalog", server.url), { headers: { "x-opencodex-api-key": REMOTE_DATA_KEY }, }); - const etag = first.headers.get("etag")!; - for (const validator of [etag, `W/${etag}`, `"stale", ${etag}`, "*"]) { - const response = await fetch(new URL("/v1/catalog", server.url), { - headers: { "x-opencodex-api-key": REMOTE_DATA_KEY, "if-none-match": validator }, - }); - expect(response.status).toBe(304); - expect(await response.text()).toBe(""); - expect(response.headers.get("etag")).toBe(etag); - expect(response.headers.get("cache-control")).toBe("private, no-cache"); - expect(response.headers.get("x-opencodex-key-id")).toBe("remote-key"); - } - for (const validator of ['"stale"', `W/ ${etag}`, "malformed"]) { + expect(first.status).toBe(200); + expect(first.headers.get("etag")).toBeNull(); + expect(first.headers.get("cache-control")).toBe("no-store"); + + for (const validator of ['"sha256-anything"', 'W/"sha256-anything"', '"stale", "other"', "*", "malformed"]) { const response = await fetch(new URL("/v1/catalog", server.url), { headers: { "x-opencodex-api-key": REMOTE_DATA_KEY, "if-none-match": validator }, }); expect(response.status).toBe(200); expect(await response.text()).toBe(REMOTE_CATALOG_BYTES); - expect(response.headers.get("cache-control")).toBe("private, no-cache"); + expect(response.headers.get("etag")).toBeNull(); + expect(response.headers.get("cache-control")).toBe("no-store"); } } finally { await server.stop(true); } }); - test("the remote size cap accepts exactly the bound and rejects cap plus one", async () => { - const request = new Request("http://localhost/v1/catalog"); - const policy = { hostname: "127.0.0.1" }; - const atCap: SerializedCatalog = { bytes: new Uint8Array(MAX_REMOTE_CATALOG_BYTES) }; - const overCap: SerializedCatalog = { bytes: new Uint8Array(MAX_REMOTE_CATALOG_BYTES + 1) }; - expect(catalogDataPlaneResponse(atCap, request, policy).status).toBe(200); - const rejected = catalogDataPlaneResponse(overCap, request, policy); - expect(rejected.status).toBe(503); - expect(await rejected.json()).toMatchObject({ error: { code: "catalog_too_large" } }); - }); - test("method and path matching stay exact ahead of the unknown-v1 guard", async () => { saveConfig(remoteCatalogConfig()); writeRemoteCatalog(); From 863a88ea3cea37501e7ea001b2bf7c200c5c1109 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 02:38:44 +0900 Subject: [PATCH 032/172] feat(connect): persist fail-closed client ownership --- src/client/state.ts | 91 +++++++++++++++++++++++++++++++++++ src/config.ts | 78 ++++++++++++++++++++++++++++++ src/lib/service-secrets.ts | 72 ++++++++++++++++++++++++++- src/types.ts | 2 + src/types/config.ts | 24 +++++++++ tests/config.test.ts | 81 +++++++++++++++++++++++++++++++ tests/service-secrets.test.ts | 86 +++++++++++++++++++++++++++++++++ 7 files changed, 433 insertions(+), 1 deletion(-) create mode 100644 src/client/state.ts create mode 100644 tests/service-secrets.test.ts diff --git a/src/client/state.ts b/src/client/state.ts new file mode 100644 index 0000000000..97e41f39c6 --- /dev/null +++ b/src/client/state.ts @@ -0,0 +1,91 @@ +import { readFileSync } from "node:fs"; +import { + getConfigPath, + mutatePersistedConfig, + readConfigDiagnostics, +} from "../config"; +import type { OcxClientConnectionConfig } from "../types"; + +export type ClientConnectionState = + | { kind: "disconnected" } + | { kind: "connected"; value: OcxClientConnectionConfig } + | { kind: "invalid"; reason: string } + | { kind: "mismatched"; reason: string }; + +function rawTopLevelConfig(): Record | null { + try { + const parsed = JSON.parse(readFileSync(getConfigPath(), "utf8").replace(/^\uFEFF/, "")) as unknown; + return parsed && typeof parsed === "object" && !Array.isArray(parsed) + ? parsed as Record + : null; + } catch { + return null; + } +} + +export function readClientConnectionState(): ClientConnectionState { + const raw = rawTopLevelConfig(); + const diagnostics = readConfigDiagnostics(); + if (!raw) { + return diagnostics.source === "default" + ? { kind: "disconnected" } + : { kind: "invalid", reason: "config.json is missing or unreadable" }; + } + const hasClient = Object.hasOwn(raw, "client") && raw.client !== undefined; + const role = raw.runtimeRole; + if (role !== undefined && role !== "standalone" && role !== "hub" && role !== "client") { + return { kind: "invalid", reason: "config.json.runtimeRole is invalid" }; + } + if (!hasClient && (role === undefined || role === "standalone")) return { kind: "disconnected" }; + if (!hasClient && role === "hub") { + return { kind: "mismatched", reason: "runtimeRole=hub cannot be used as a connected client" }; + } + if (!hasClient || role !== "client") { + return { + kind: "mismatched", + reason: hasClient + ? "config.json.client is present without runtimeRole=client" + : "runtimeRole=client is present without config.json.client", + }; + } + const client = diagnostics.config.client; + if (!client) { + const warning = diagnostics.warnings?.find(value => value.startsWith("client")); + return { kind: "invalid", reason: warning ?? "config.json.client is malformed" }; + } + return { kind: "connected", value: client }; +} + +export function commitClientConnection( + state: OcxClientConnectionConfig, +): "committed" | "unchanged" { + const outcome = mutatePersistedConfig(config => { + const unchanged = config.runtimeRole === "client" + && JSON.stringify(config.client) === JSON.stringify(state); + if (!unchanged) { + config.runtimeRole = "client"; + config.client = structuredClone(state); + } + return { changed: !unchanged, value: undefined }; + }); + if (outcome.status === "committed" || outcome.status === "unchanged") return outcome.status; + throw new Error(`client state commit unavailable: ${outcome.reason}`); +} + +export function clearClientConnection( + expectedApiKeyId: string, +): "committed" | "absent" | "conflict" { + const outcome = mutatePersistedConfig(config => { + if (!config.client && config.runtimeRole !== "client") { + return { changed: false, value: "absent" as const }; + } + if (!config.client || config.runtimeRole !== "client" || config.client.apiKeyId !== expectedApiKeyId) { + return { changed: false, value: "conflict" as const }; + } + delete config.client; + delete config.runtimeRole; + return { changed: true, value: "committed" as const }; + }); + if (outcome.status === "unavailable") return "conflict"; + return outcome.value; +} diff --git a/src/config.ts b/src/config.ts index 7cabaab64a..0b89e3580c 100644 --- a/src/config.ts +++ b/src/config.ts @@ -911,6 +911,45 @@ const remoteGuiConfigSchema = z.object({ allowInsecureHttp: z.boolean().optional(), }).strict(); +const connectedClientIdSchema = z.enum(["codex", "claude"]); +const clientTimestampSchema = z.string().datetime({ offset: true }); +const clientOriginSchema = z.string().transform((value, ctx) => { + const origin = canonicalHttpOrigin(value); + if (!origin) { + ctx.addIssue({ code: "custom", message: "must be a canonical http(s) origin without credentials, path, query, or fragment" }); + return z.NEVER; + } + return origin; +}); +const clientConnectionSchema = z.object({ + serverUrl: clientOriginSchema, + managementUrl: clientOriginSchema, + managementTransport: z.enum(["direct", "relay"]), + selectedClients: z.array(connectedClientIdSchema).min(1).max(2).superRefine((clients, ctx) => { + if (new Set(clients).size !== clients.length) { + ctx.addIssue({ code: "custom", message: "must contain unique client ids" }); + } + }), + tokenEnv: z.literal("OPENCODEX_API_AUTH_TOKEN"), + apiKeyId: z.string().trim().min(1).max(256), + tokenFingerprint: z.string().regex(/^[a-f0-9]{64}$/), + protocolVersion: z.literal(1), + connectedAt: clientTimestampSchema, + catalogEtag: z.string().min(1).max(512).optional(), + catalogSyncedAt: clientTimestampSchema.optional(), + pendingOperation: z.object({ + kind: z.literal("rotate"), + rotationId: z.string().trim().min(1).max(256), + newKeyIssuedAt: clientTimestampSchema, + oldKeyBackupPath: z.string().min(1), + }).strict().superRefine((operation, ctx) => { + const expected = join(getConfigDir(), "service-api-token.prev"); + if (operation.oldKeyBackupPath !== expected) { + ctx.addIssue({ code: "custom", path: ["oldKeyBackupPath"], message: `must equal ${expected}` }); + } + }).optional(), +}).strict(); + const configSchema = z.object({ port: z.number().int().min(0).max(65535).default(10100), // A malformed hand edit must disable only remote-role behavior, not discard @@ -920,6 +959,9 @@ const configSchema = z.object({ // candidates are rejected explicitly by remoteGuiConfigError below. hub: hubConfigSchema.optional().catch(undefined), remoteGui: remoteGuiConfigSchema.optional().catch(undefined), + // A malformed present client block must remain diagnosable from raw config and + // fail closed through src/client/state.ts; unrelated provider state still loads. + client: clientConnectionSchema.optional().catch(undefined), managementUsageMaxReadBytes: z.number().int().positive().default(64 * 1024 * 1024), // Invalid hand edits disable only this opt-in circuit. Live writes remain strict. upstreamHostCircuitThreshold: z.number().int() @@ -1788,6 +1830,15 @@ function malformedOptionalRemoteBlockWarning( return `${key}${field ? `.${field}` : ""} ignored: invalid remote GUI configuration`; } +function malformedClientConnectionWarning(rawParsed: unknown): string | null { + const raw = rawConfigRecord(rawParsed); + if (!raw || !Object.hasOwn(raw, "client") || raw.client === undefined) return null; + const result = clientConnectionSchema.safeParse(raw.client); + if (result.success) return null; + const field = result.error.issues[0]?.path.join("."); + return `client${field ? `.${field}` : ""} invalid: remote client mode is disabled until config.json is repaired`; +} + function warnDegradedOptionalRemoteBlocks(rawParsed: unknown): void { for (const key of ["hub", "remoteGui"] as const) { const warning = malformedOptionalRemoteBlockWarning(rawParsed, key); @@ -2104,6 +2155,8 @@ function validFileConfigDiagnostics(config: OcxConfig, rawParsed: unknown): Conf if (hubWarning) warnings.push(hubWarning); const remoteGuiWarning = malformedOptionalRemoteBlockWarning(rawParsed, "remoteGui"); if (remoteGuiWarning) warnings.push(remoteGuiWarning); + const clientWarning = malformedClientConnectionWarning(rawParsed); + if (clientWarning) warnings.push(clientWarning); if (syncDisabledReason) { warnings.push(`syncCodexSubagentDefaults ignored: ${syncDisabledReason}`); } @@ -2219,6 +2272,29 @@ function remoteGuiConfigError(value: unknown): string | null { return null; } +function clientConnectionConfigError(value: unknown): string | null { + const raw = rawConfigRecord(value); + if (!raw || !Object.hasOwn(raw, "client") || raw.client === undefined) return null; + const result = clientConnectionSchema.safeParse(raw.client); + if (result.success) return null; + const issue = result.error.issues[0]; + const field = issue?.path.join("."); + return `schema_invalid: client${field ? `.${field}` : ""}: ${issue?.message ?? "invalid client connection"}`; +} + +function clientRolePairError(value: unknown): string | null { + const raw = rawConfigRecord(value); + if (!raw) return null; + const hasClient = Object.hasOwn(raw, "client") && raw.client !== undefined; + if (raw.runtimeRole === "client" && !hasClient) { + return "schema_invalid: runtimeRole client requires a complete client connection"; + } + if (hasClient && raw.runtimeRole !== "client") { + return "schema_invalid: client connection requires runtimeRole client"; + } + return null; +} + /** * Same reasoning as {@link blankHostnameError}, and more urgent: the read path degrades a * malformed selection-order map to undefined, which on a write would drop every entry the @@ -2338,6 +2414,8 @@ export function validateConfigCandidate(value: unknown): { ok: true; config: Ocx ?? oauthOpenBrowserError(value) ?? runtimeRoleError(value) ?? remoteGuiConfigError(value) + ?? clientConnectionConfigError(value) + ?? clientRolePairError(value) ?? loopbackListenerPortError(value); if (boundaryError) return { ok: false, error: boundaryError }; const result = configSchema.safeParse(value); diff --git a/src/lib/service-secrets.ts b/src/lib/service-secrets.ts index f682abb777..2ff1a2a0bc 100644 --- a/src/lib/service-secrets.ts +++ b/src/lib/service-secrets.ts @@ -1,11 +1,81 @@ +import { createHash } from "node:crypto"; +import { existsSync, lstatSync, readFileSync, unlinkSync } from "node:fs"; import { join } from "node:path"; -import { readFileSync } from "node:fs"; import { getConfigDir } from "../config"; +import { atomicWriteFile } from "../config/atomic-write"; + +const MAX_SERVICE_API_TOKEN_BYTES = 4096; + +export interface PersistedServiceApiToken { + path: string; + fingerprint: string; +} + +export type ServiceApiTokenState = + | { kind: "absent" } + | { kind: "present"; token: string; fingerprint: string } + | { kind: "unsafe"; reason: string }; export function serviceApiTokenFilePath(): string { return join(getConfigDir(), "service-api-token"); } +export function serviceApiTokenFingerprint(token: string): string { + return createHash("sha256").update(token).digest("hex"); +} + +export function readServiceApiTokenState(): ServiceApiTokenState { + const path = serviceApiTokenFilePath(); + if (!existsSync(path)) return { kind: "absent" }; + let stat; + try { + stat = lstatSync(path); + } catch { + return { kind: "unsafe", reason: "service token path could not be inspected" }; + } + if (stat.isSymbolicLink() || !stat.isFile() || stat.size > MAX_SERVICE_API_TOKEN_BYTES) { + return { kind: "unsafe", reason: "service token path is not a bounded regular file" }; + } + try { + const token = readFileSync(path, "utf8").trim(); + if (!token) return { kind: "unsafe", reason: "service token file is empty" }; + return { kind: "present", token, fingerprint: serviceApiTokenFingerprint(token) }; + } catch { + return { kind: "unsafe", reason: "service token file could not be read" }; + } +} + +export function writeServiceApiTokenFile(token: string): PersistedServiceApiToken { + const value = token.trim(); + if (!value || /[\r\n\0]/.test(value) || Buffer.byteLength(value) > MAX_SERVICE_API_TOKEN_BYTES) { + throw new Error("refusing to persist an invalid service API token"); + } + const path = serviceApiTokenFilePath(); + const existing = readServiceApiTokenState(); + if (existing.kind !== "absent") { + throw new Error(existing.kind === "unsafe" + ? existing.reason + : "refusing to replace a pre-existing service API token"); + } + atomicWriteFile(path, `${value}\n`); + return { path, fingerprint: serviceApiTokenFingerprint(value) }; +} + +export function removeServiceApiTokenFileIfOwned( + expectedFingerprint: string, +): "removed" | "absent" | "changed" { + const state = readServiceApiTokenState(); + if (state.kind === "absent") return "absent"; + if (state.kind !== "present" || state.fingerprint !== expectedFingerprint) return "changed"; + try { + unlinkSync(serviceApiTokenFilePath()); + return "removed"; + } catch (error) { + if ((error as NodeJS.ErrnoException).code === "ENOENT") return "absent"; + throw new Error("owned service API token could not be removed", { cause: error }); + } +} + /** * App-side service token loading (WinSW native mode has no batch wrapper to read the * token file into the environment). Pure: returns the token or null — the CALLER diff --git a/src/types.ts b/src/types.ts index c4b0b6ed8a..e28d438444 100644 --- a/src/types.ts +++ b/src/types.ts @@ -65,6 +65,8 @@ export type { OcxConfigRebaseProvenance, OcxHubConfig, OcxRemoteGuiConfig, + OcxConnectedClientId, + OcxClientConnectionConfig, OcxConfig, OcxAccountPoolRotationStrategy, OcxAccountPoolQuotaWindow, diff --git a/src/types/config.ts b/src/types/config.ts index e75f3e438b..48da5e7a60 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -268,6 +268,28 @@ export interface OcxRemoteGuiConfig { allowInsecureHttp?: boolean; } +export type OcxConnectedClientId = "codex" | "claude"; + +export interface OcxClientConnectionConfig { + serverUrl: string; + managementUrl: string; + managementTransport: "direct" | "relay"; + selectedClients: OcxConnectedClientId[]; + tokenEnv: "OPENCODEX_API_AUTH_TOKEN"; + apiKeyId: string; + tokenFingerprint: string; + protocolVersion: 1; + connectedAt: string; + catalogEtag?: string; + catalogSyncedAt?: string; + pendingOperation?: { + kind: "rotate"; + rotationId: string; + newKeyIssuedAt: string; + oldKeyBackupPath: string; + }; +} + export interface OcxConfig { port: number; /** Runtime topology role. Absence preserves the historical standalone behavior. */ @@ -276,6 +298,8 @@ export interface OcxConfig { hub?: OcxHubConfig; /** Opt-in remote dashboard issuance policy. Presence is inert outside the hub role. */ remoteGui?: OcxRemoteGuiConfig; + /** Remote-hub client state. The admission secret is stored only in service-api-token. */ + client?: OcxClientConnectionConfig; /** Opt in to one identical-turn retry when a Responses completion has no text or tool call. */ emptyCompletionRetry?: boolean; /** diff --git a/tests/config.test.ts b/tests/config.test.ts index aba5aaf9c7..46417e52d4 100644 --- a/tests/config.test.ts +++ b/tests/config.test.ts @@ -272,6 +272,87 @@ describe("opencodex config defaults", () => { } }); + test("remote client state round-trips without accepting a secret field", () => { + const client = { + serverUrl: "https://hub.example.test", + managementUrl: "https://manage.example.test:443", + managementTransport: "direct" as const, + selectedClients: ["codex", "claude"] as const, + tokenEnv: "OPENCODEX_API_AUTH_TOKEN" as const, + apiKeyId: "issued-key-id", + tokenFingerprint: "a".repeat(64), + protocolVersion: 1 as const, + connectedAt: "2026-08-28T00:00:00.000Z", + catalogEtag: '"sha256-example"', + catalogSyncedAt: "2026-08-28T00:01:00.000Z", + pendingOperation: { + kind: "rotate" as const, + rotationId: "rotation-1", + newKeyIssuedAt: "2026-08-28T00:02:00.000Z", + oldKeyBackupPath: join(testDir, "service-api-token.prev"), + }, + }; + const result = validateConfigCandidate({ + ...getDefaultConfig(), + runtimeRole: "client", + client, + }); + expect(result).toMatchObject({ + ok: true, + config: { + runtimeRole: "client", + client: { + serverUrl: "https://hub.example.test", + managementUrl: "https://manage.example.test", + apiKeyId: "issued-key-id", + }, + }, + }); + if (!result.ok) return; + saveConfig(result.config); + expect(loadConfig().client).toEqual(result.config.client); + expect(readFileSync(getConfigPath(), "utf8")).not.toContain("ocx_data_"); + + expect(validateConfigCandidate({ + ...getDefaultConfig(), + runtimeRole: "client", + client: { ...client, key: "ocx_data_forbidden" }, + })).toMatchObject({ ok: false, error: expect.stringContaining("client") }); + }); + + test("remote client state rejects half-present and malformed rotation recovery state", () => { + const validClient = { + serverUrl: "https://hub.example.test", + managementUrl: "https://hub.example.test", + managementTransport: "direct", + selectedClients: ["codex"], + tokenEnv: "OPENCODEX_API_AUTH_TOKEN", + apiKeyId: "issued-key-id", + tokenFingerprint: "b".repeat(64), + protocolVersion: 1, + connectedAt: "2026-08-28T00:00:00.000Z", + }; + expect(validateConfigCandidate({ ...getDefaultConfig(), runtimeRole: "client" })).toMatchObject({ + ok: false, + error: expect.stringContaining("requires a complete client connection"), + }); + expect(validateConfigCandidate({ ...getDefaultConfig(), client: validClient })).toMatchObject({ + ok: false, + error: expect.stringContaining("requires runtimeRole client"), + }); + for (const pendingOperation of [ + { kind: "rotate", newKeyIssuedAt: "2026-08-28T00:00:00.000Z", oldKeyBackupPath: join(testDir, "service-api-token.prev") }, + { kind: "rotate", rotationId: "r", newKeyIssuedAt: "not-a-time", oldKeyBackupPath: join(testDir, "service-api-token.prev") }, + { kind: "rotate", rotationId: "r", newKeyIssuedAt: "2026-08-28T00:00:00.000Z", oldKeyBackupPath: join(testDir, "foreign.prev") }, + ]) { + expect(validateConfigCandidate({ + ...getDefaultConfig(), + runtimeRole: "client", + client: { ...validClient, pendingOperation }, + })).toMatchObject({ ok: false, error: expect.stringContaining("client.pendingOperation") }); + } + }); + test("malformed classifier config is normalized at load, even with subagentEffort absent (#1697)", () => { // normalizePersistedClaudeCode used to be reached only through a subagentEffort short-circuit, // so a config whose ONLY defect was elsewhere in claudeCode was never normalized. These diff --git a/tests/service-secrets.test.ts b/tests/service-secrets.test.ts new file mode 100644 index 0000000000..7ee274ab64 --- /dev/null +++ b/tests/service-secrets.test.ts @@ -0,0 +1,86 @@ +import { afterEach, beforeEach, describe, expect, test } from "bun:test"; +import { + existsSync, + lstatSync, + mkdtempSync, + readdirSync, + rmSync, + symlinkSync, + writeFileSync, +} from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { + readServiceApiTokenState, + removeServiceApiTokenFileIfOwned, + serviceApiTokenFilePath, + serviceApiTokenFingerprint, + writeServiceApiTokenFile, +} from "../src/lib/service-secrets"; + +let home = ""; + +beforeEach(() => { + home = mkdtempSync(join(tmpdir(), "ocx-service-secret-")); + process.env.OPENCODEX_HOME = home; +}); + +afterEach(() => { + delete process.env.OPENCODEX_HOME; + if (home) rmSync(home, { recursive: true, force: true }); +}); + +describe("service API token ownership", () => { + test("writes only the exact owner path through an atomic owner-only replacement", () => { + const token = "ocx_data_0123456789abcdef0123456789abcdef01234567"; + const persisted = writeServiceApiTokenFile(token); + + expect(persisted.path).toBe(join(home, "service-api-token")); + expect(persisted.path).toBe(serviceApiTokenFilePath()); + expect(persisted.fingerprint).toBe(serviceApiTokenFingerprint(token)); + expect(lstatSync(persisted.path).isFile()).toBe(true); + if (process.platform !== "win32") expect(lstatSync(persisted.path).mode & 0o777).toBe(0o600); + expect(readdirSync(home).filter(name => name.includes(".tmp"))).toEqual([]); + expect(readServiceApiTokenState()).toEqual({ + kind: "present", + token, + fingerprint: persisted.fingerprint, + }); + }); + + test("refuses symlink and pre-existing token targets without exposing token bytes", () => { + const path = serviceApiTokenFilePath(); + const target = join(home, "foreign-token"); + writeFileSync(target, "foreign-secret\n", { mode: 0o600 }); + let symlinkAvailable = true; + try { + symlinkSync(target, path); + } catch (error) { + if ((error as NodeJS.ErrnoException).code === "EPERM") symlinkAvailable = false; + else throw error; + } + if (symlinkAvailable) { + const secret = "ocx_data_should_never_appear_in_an_error"; + expect(() => writeServiceApiTokenFile(secret)).toThrow("bounded regular file"); + try { writeServiceApiTokenFile(secret); } catch (error) { + expect(String(error)).not.toContain(secret); + } + rmSync(path); + } + + writeFileSync(path, "foreign-secret\n", { mode: 0o600 }); + expect(() => writeServiceApiTokenFile("ocx_data_new_secret")).toThrow("pre-existing"); + }); + + test("removes only the fingerprint-owned unchanged token", () => { + const first = writeServiceApiTokenFile("ocx_data_first"); + writeFileSync(first.path, "ocx_data_replacement\n", { mode: 0o600 }); + expect(removeServiceApiTokenFileIfOwned(first.fingerprint)).toBe("changed"); + expect(existsSync(first.path)).toBe(true); + + const replacementFingerprint = serviceApiTokenFingerprint("ocx_data_replacement"); + expect(removeServiceApiTokenFileIfOwned(replacementFingerprint)).toBe("removed"); + expect(existsSync(first.path)).toBe(false); + expect(removeServiceApiTokenFileIfOwned(replacementFingerprint)).toBe("absent"); + }); +}); From 6d9aed20fd2f7cf84d7233a9ab3c21ec225a80b5 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 02:41:19 +0900 Subject: [PATCH 033/172] feat(connect): add durable remote routing target --- src/cli/index.ts | 8 +- src/codex/inject.ts | 211 +++++++++++++++++++++++++++++++----- src/codex/journal.ts | 44 +++++++- tests/codex-inject.test.ts | 27 +++++ tests/codex-journal.test.ts | 33 ++++++ 5 files changed, 292 insertions(+), 31 deletions(-) diff --git a/src/cli/index.ts b/src/cli/index.ts index ca1a3a0fa9..37eb47b714 100755 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -9,6 +9,7 @@ import { runCodexHistoryJob, } from "../codex/history-job"; import { reconcileJournal } from "../codex/journal"; +import { readClientConnectionState } from "../client/state"; import { codexAutoStartEnabled, getConfigDir, @@ -224,7 +225,12 @@ async function findProxyOwnerBeforeJournalRecovery( // The probe established that the snapshotted owner is stale. Compare before // deleting so a concurrent start that rewrote the PID file keeps its state. removePidIfValueIs(pidSnapshot); - if (!currentExternalCodexModelProvider()) reconcileJournal(); + if (!currentExternalCodexModelProvider()) { + const clientState = readClientConnectionState(); + reconcileJournal(clientState.kind === "connected" + ? { activeClientApiKeyId: clientState.value.apiKeyId } + : undefined); + } return { live: null, pidSnapshot }; } diff --git a/src/codex/inject.ts b/src/codex/inject.ts index 72be578784..536b12a360 100644 --- a/src/codex/inject.ts +++ b/src/codex/inject.ts @@ -139,6 +139,54 @@ export interface InjectCodexOptions { * provider discovery so a deterministic config refusal cannot degrade an existing catalog. */ validateOnly?: boolean; + /** Explicit remote routing target. Absence preserves byte-compatible standalone output. */ + routingTarget?: CodexRoutingTarget; + journalOwner?: { kind: "process" } | { kind: "client"; apiKeyId: string }; +} + +export interface CodexRoutingTarget { + baseUrl: string; + requiresAdmissionToken: boolean; + tokenEnv: "OPENCODEX_API_AUTH_TOKEN"; +} + +function validateCodexRoutingTarget(target: CodexRoutingTarget): CodexRoutingTarget { + let parsed: URL; + try { + parsed = new URL(target.baseUrl); + } catch { + throw new TypeError("Codex routing target must be an absolute HTTP(S) /v1 URL"); + } + if ( + (parsed.protocol !== "http:" && parsed.protocol !== "https:") + || parsed.username + || parsed.password + || parsed.pathname !== "/v1" + || parsed.search + || parsed.hash + || target.tokenEnv !== "OPENCODEX_API_AUTH_TOKEN" + ) { + throw new TypeError("Codex routing target must be a canonical HTTP(S) /v1 URL without credentials, query, or fragment"); + } + return { ...target, baseUrl: `${parsed.origin}/v1` }; +} + +export function standaloneCodexRoutingTarget( + port: number, + config?: Pick, +): CodexRoutingTarget { + const loopback = config?.unauthenticatedLoopbackListener; + const effectivePort = loopback?.enabled ? loopback.port : port; + const hostname = loopback?.enabled ? undefined : config?.hostname; + return { + baseUrl: `http://${providerBaseHost(hostname)}:${effectivePort}/v1`, + requiresAdmissionToken: loopback?.enabled ? false : shouldInjectApiAuthHeader(config), + tokenEnv: "OPENCODEX_API_AUTH_TOKEN", + }; +} + +function routingTargetOrigin(target: CodexRoutingTarget): string { + return target.baseUrl.slice(0, -3); } function configuredManagedSubagentDefaults( @@ -213,28 +261,51 @@ export function shouldInjectApiAuthHeader( export function buildProviderTableBlock( port: number, + supportsWebsockets?: boolean, + includeApiAuthHeader?: boolean, + hostname?: string, +): string; +export function buildProviderTableBlock( + target: CodexRoutingTarget, + supportsWebsockets?: boolean, +): string; +export function buildProviderTableBlock( + portOrTarget: number | CodexRoutingTarget, supportsWebsockets = false, includeApiAuthHeader = false, hostname?: string, ): string { - const host = providerBaseHost(hostname); + const target = typeof portOrTarget === "number" + ? validateCodexRoutingTarget({ + baseUrl: `http://${providerBaseHost(hostname)}:${portOrTarget}/v1`, + requiresAdmissionToken: includeApiAuthHeader, + tokenEnv: "OPENCODEX_API_AUTH_TOKEN", + }) + : validateCodexRoutingTarget(portOrTarget); + return buildProviderTableBlockForTarget(target, supportsWebsockets); +} + +function buildProviderTableBlockForTarget( + target: CodexRoutingTarget, + supportsWebsockets = false, +): string { const lines = [ "", OCX_SECTION_MARKER, "[model_providers.opencodex]", 'name = "OpenCodex Proxy"', - `base_url = "http://${host}:${port}/v1"`, + `base_url = ${tomlString(target.baseUrl)}`, 'wire_api = "responses"', "requires_openai_auth = true", ]; - if (includeApiAuthHeader) { + if (target.requiresAdmissionToken) { // codex-cli 0.146+ contract (#2073): env_key sends Authorization: Bearer $VAR and // hard-errors on a missing/empty variable instead of silently omitting auth. It // coexists with requires_openai_auth (env_key wins wire auth; the flag keeps the // login/account UX), and the server substitutes stored main auth for our admission // bearer (#1686), so the modern form is strictly better than the legacy // env_http_headers table this line used to emit. - lines.push('env_key = "OPENCODEX_API_AUTH_TOKEN"'); + lines.push(`env_key = ${tomlString(target.tokenEnv)}`); } if (supportsWebsockets) lines.push("supports_websockets = true"); return lines.join("\n") + "\n"; @@ -243,8 +314,19 @@ export function buildProviderTableBlock( export function buildOpenaiBaseUrlLine( port: number, hostname?: string, +): string; +export function buildOpenaiBaseUrlLine(target: CodexRoutingTarget): string; +export function buildOpenaiBaseUrlLine( + portOrTarget: number | CodexRoutingTarget, + hostname?: string, ): string { - return `openai_base_url = "http://${providerBaseHost(hostname)}:${port}/v1"`; + return typeof portOrTarget === "number" + ? `openai_base_url = "http://${providerBaseHost(hostname)}:${portOrTarget}/v1"` + : buildOpenaiBaseUrlLineForTarget(validateCodexRoutingTarget(portOrTarget)); +} + +function buildOpenaiBaseUrlLineForTarget(target: CodexRoutingTarget): string { + return `openai_base_url = ${tomlString(target.baseUrl)}`; } /** @@ -257,11 +339,23 @@ export function setRootOpenaiBaseUrl( content: string, port: number, hostname?: string, +): { content: string; keptUserBaseUrl: boolean }; +export function setRootOpenaiBaseUrl( + content: string, + target: CodexRoutingTarget, +): { content: string; keptUserBaseUrl: boolean }; +export function setRootOpenaiBaseUrl( + content: string, + portOrTarget: number | CodexRoutingTarget, + hostname?: string, ): { content: string; keptUserBaseUrl: boolean } { + if (typeof portOrTarget !== "number") { + return setRootOpenaiBaseUrlForTarget(content, validateCodexRoutingTarget(portOrTarget)); + } const lines = content.split("\n"); const firstTable = lines.findIndex((l) => /^\s*\[/.test(l)); const rootEnd = firstTable === -1 ? lines.length : firstTable; - const key = buildOpenaiBaseUrlLine(port, hostname); + const key = buildOpenaiBaseUrlLine(portOrTarget, hostname); for (let i = 0; i < rootEnd; i++) { if (!isRootOpenaiBaseUrlLine(lines[i])) continue; @@ -289,6 +383,33 @@ export function setRootOpenaiBaseUrl( return { content: lines.join("\n"), keptUserBaseUrl: false }; } +function setRootOpenaiBaseUrlForTarget( + content: string, + target: CodexRoutingTarget, +): { content: string; keptUserBaseUrl: boolean } { + const lines = content.split("\n"); + const firstTable = lines.findIndex((line) => /^\s*\[/.test(line)); + const rootEnd = firstTable === -1 ? lines.length : firstTable; + const key = buildOpenaiBaseUrlLineForTarget(target); + for (let index = 0; index < rootEnd; index += 1) { + if (!isRootOpenaiBaseUrlLine(lines[index])) continue; + const markerOwned = index > 0 && lines[index - 1].includes(OCX_SECTION_MARKER); + if (!markerOwned) return { content, keptUserBaseUrl: true }; + lines[index] = key; + return { content: lines.join("\n"), keptUserBaseUrl: false }; + } + if (firstTable === -1) { + return { + content: `${content.replace(/\n+$/, "")}\n${OCX_SECTION_MARKER}\n${key}\n`, + keptUserBaseUrl: false, + }; + } + let insertAt = firstTable; + while (insertAt > 0 && lines[insertAt - 1].trim() === "") insertAt -= 1; + lines.splice(insertAt, 0, OCX_SECTION_MARKER, key); + return { content: lines.join("\n"), keptUserBaseUrl: false }; +} + /** * Remove the marker-owned root `openai_base_url` (marker line + the key line right after it). * A user's own root override (no marker) survives; an orphaned marker with no key line after @@ -619,17 +740,48 @@ function stripOpencodexCatalogPath(content: string): string { .join("\n"); } -export function buildProfileFile(port: number, catalogPath?: string | null, supportsWebsockets = false, includeApiAuthHeader = false, hostname?: string, fastMode?: boolean): string { - const host = providerBaseHost(hostname); +export function buildProfileFile(port: number, catalogPath?: string | null, supportsWebsockets?: boolean, includeApiAuthHeader?: boolean, hostname?: string, fastMode?: boolean): string; +export function buildProfileFile(target: CodexRoutingTarget, catalogPath?: string | null, supportsWebsockets?: boolean, fastMode?: boolean): string; +export function buildProfileFile( + portOrTarget: number | CodexRoutingTarget, + catalogPath?: string | null, + supportsWebsockets = false, + includeApiAuthHeaderOrFastMode?: boolean, + hostname?: string, + fastMode?: boolean, +): string { + const target = typeof portOrTarget === "number" + ? validateCodexRoutingTarget({ + baseUrl: `http://${providerBaseHost(hostname)}:${portOrTarget}/v1`, + requiresAdmissionToken: includeApiAuthHeaderOrFastMode === true, + tokenEnv: "OPENCODEX_API_AUTH_TOKEN", + }) + : validateCodexRoutingTarget(portOrTarget); + return buildProfileFileForTarget( + target, + catalogPath, + supportsWebsockets, + typeof portOrTarget === "number" ? fastMode : includeApiAuthHeaderOrFastMode, + ); +} + +function buildProfileFileForTarget( + target: CodexRoutingTarget, + catalogPath?: string | null, + supportsWebsockets = false, + fastMode?: boolean, +): string { + const origin = routingTargetOrigin(target); + const host = new URL(origin).host; // Design B (loopback): the reference/fallback file documents the root override form. // Non-loopback keeps the legacy provider-table shape (built-in provider cannot carry // the x-opencodex-api-key env header). - if (!includeApiAuthHeader) { + if (!target.requiresAdmissionToken) { const lines = [ "# OpenCodex proxy fallback config (Design B)", - `# Root override that points Codex's built-in openai provider at the proxy on ${host}:${port}.`, + `# Root override that points Codex's built-in openai provider at the proxy on ${host}.`, "# Merge these root keys into ~/.codex/config.toml manually if auto-injection was removed.", - buildOpenaiBaseUrlLine(port, hostname), + buildOpenaiBaseUrlLineForTarget(target), ]; if (catalogPath) lines.push(`model_catalog_json = ${tomlString(catalogPath)}`); if (fastMode !== undefined) lines.push("", "[features]", `fast_mode = ${fastMode ? "true" : "false"}`, ""); @@ -637,12 +789,12 @@ export function buildProfileFile(port: number, catalogPath?: string | null, supp } const lines = [ "# OpenCodex proxy profile — use with: codex --profile opencodex", - `# Routes all model requests through the opencodex proxy at ${host}:${port}`, + `# Routes all model requests through the opencodex proxy at ${host}`, 'model_provider = "opencodex"', ]; if (catalogPath) lines.push(`model_catalog_json = ${tomlString(catalogPath)}`); if (fastMode !== undefined) lines.push("", "[features]", `fast_mode = ${fastMode ? "true" : "false"}`); - lines.push(buildProviderTableBlock(port, supportsWebsockets, includeApiAuthHeader, hostname).trimEnd(), ""); + lines.push(buildProviderTableBlockForTarget(target, supportsWebsockets).trimEnd(), ""); return lines.join("\n"); } @@ -684,8 +836,14 @@ export async function injectCodexConfig( // // The listener port is fixed in config, never OS-assigned, so this value survives restarts // and matches what an already-running app-server read at startup. - const loopback = config?.unauthenticatedLoopbackListener; - if (loopback?.enabled) port = loopback.port; + let routingTarget: CodexRoutingTarget; + try { + routingTarget = options.routingTarget + ? validateCodexRoutingTarget(options.routingTarget) + : standaloneCodexRoutingTarget(port, config); + } catch (error) { + return { success: false, message: error instanceof Error ? error.message : "Invalid Codex routing target" }; + } if (!existsSync(CODEX_CONFIG_PATH)) { return { success: false, @@ -712,8 +870,8 @@ export async function injectCodexConfig( message: `⚠️ Codex routing NOT injected: config.toml selects the external model_provider ${tomlString(activeProvider)}.\n` + ` OpenCodex preserves external provider configuration so existing ${tomlString(activeProvider)} session history stays visible.\n` + - ` Configure that provider for Responses passthrough at http://${providerBaseHost(config?.hostname)}:${port}/v1` + - `${shouldInjectApiAuthHeader(config) ? ` with x-opencodex-api-key from OPENCODEX_API_AUTH_TOKEN` : ""}.\n` + + ` Configure that provider for Responses passthrough at ${routingTarget.baseUrl}` + + `${routingTarget.requiresAdmissionToken ? ` with x-opencodex-api-key from ${routingTarget.tokenEnv}` : ""}.\n` + ` For direct injection, switch to the built-in openai provider, remove any user-owned root openai_base_url, and rerun 'ocx start'.`, }; } @@ -781,7 +939,7 @@ export async function injectCodexConfig( ? setRootModelCatalogPath(content, catalogPath) : stripOpencodexCatalogPath(content); - const legacyMode = shouldInjectApiAuthHeader(config); + const legacyMode = routingTarget.requiresAdmissionToken; let keptUserBaseUrl = false; if (legacyMode) { // Legacy (non-loopback) injection: the built-in openai provider cannot carry the @@ -792,17 +950,12 @@ export async function injectCodexConfig( content = content.trimEnd() + "\n" + - buildProviderTableBlock( - port, - websocketsEnabled(config ?? {}), - true, - config?.hostname, - ); + buildProviderTableBlockForTarget(routingTarget, websocketsEnabled(config ?? {})); } else { // Design B (loopback): a single root override; codex keeps its native `openai` provider id // so thread history is never remapped. Any legacy form was already stripped above. content = stripInjectedOpenaiBaseUrl(content); // normalize before idempotent re-insert - const result = setRootOpenaiBaseUrl(content, port, config?.hostname); + const result = setRootOpenaiBaseUrlForTarget(content, routingTarget); content = result.content; keptUserBaseUrl = result.keptUserBaseUrl; } @@ -838,7 +991,12 @@ export async function injectCodexConfig( managedDefaultsMessage = ` ⚠️ ${nativeSubagentDefaultsWarning}\n`; } - const profileContent = buildProfileFile(port, catalogPath, websocketsEnabled(config ?? {}), legacyMode, config?.hostname, config?.fastMode); + const profileContent = buildProfileFileForTarget( + routingTarget, + catalogPath, + websocketsEnabled(config ?? {}), + config?.fastMode, + ); content = applyEol(content, eol); /* @@ -916,6 +1074,7 @@ export async function injectCodexConfig( writeJournal({ currentStateIsNative: !hasInjectedCodexRouting(rawContent), configContent: baselineContent, + owner: options.journalOwner, }); atomicWriteFile(CODEX_CONFIG_PATH, content); atomicWriteFile(CODEX_PROFILE_PATH, profileContent); diff --git a/src/codex/journal.ts b/src/codex/journal.ts index b8923a6daa..68523fe685 100644 --- a/src/codex/journal.ts +++ b/src/codex/journal.ts @@ -16,6 +16,10 @@ import { CODEX_HOME, CODEX_CONFIG_PATH, CODEX_PROFILE_PATH } from "./paths"; */ export const JOURNAL_PATH = join(CODEX_HOME, "opencodex-journal.json"); +export type JournalOwner = + | { kind: "process"; pid: number } + | { kind: "client"; apiKeyId: string }; + interface Journal { version: 1; originalConfig: string; @@ -41,10 +45,11 @@ interface Journal { */ injectedCatalogPath?: string | null; pid: number; + owner?: JournalOwner; timestamp: string; } -interface RestoreJournalResult { +export interface RestoreJournalResult { configRestored: boolean; profileRestored: boolean; configChanged: boolean; @@ -71,6 +76,7 @@ export interface WriteJournalOptions { * another process rewrites config.toml mid-flight. */ configContent?: string; + owner?: { kind: "process" } | { kind: "client"; apiKeyId: string }; } /** @@ -103,6 +109,9 @@ export function writeJournal(options: WriteJournalOptions = {}): void { originalConfig: Buffer.from(config).toString("base64"), originalProfile: profile ? Buffer.from(profile).toString("base64") : null, pid: process.pid, + owner: options.owner?.kind === "client" + ? { kind: "client", apiKeyId: options.owner.apiKeyId } + : { kind: "process", pid: process.pid }, timestamp: new Date().toISOString(), }; atomicWriteFile(JOURNAL_PATH, JSON.stringify(journal)); @@ -168,6 +177,20 @@ function readJournal(): Journal | null { } } +export function journalOwner(): JournalOwner | null { + const journal = readJournal(); + if (!journal) return null; + if (journal.owner?.kind === "client" && typeof journal.owner.apiKeyId === "string" && journal.owner.apiKeyId) { + return { kind: "client", apiKeyId: journal.owner.apiKeyId }; + } + if (journal.owner?.kind === "process" && Number.isSafeInteger(journal.owner.pid) && journal.owner.pid > 0) { + return { kind: "process", pid: journal.owner.pid }; + } + return Number.isSafeInteger(journal.pid) && journal.pid > 0 + ? { kind: "process", pid: journal.pid } + : null; +} + export function restoreJournalState(): RestoreJournalResult { const journal = readJournal(); if (!journal) { @@ -207,11 +230,24 @@ export function restoreJournal(): boolean { return restoreJournalState().complete; } -export function reconcileJournal(): boolean { +export interface ReconcileJournalOptions { + activeClientApiKeyId?: string; +} + +export function reconcileJournal(options: ReconcileJournalOptions = {}): boolean { const journal = readJournal(); if (!journal) return false; + const owner = journalOwner(); + if (owner?.kind === "client") { + if (options.activeClientApiKeyId === owner.apiKeyId) return false; + const restored = restoreJournalState(); + if (!restored.configRestored && !restored.profileRestored) return false; + console.error(`⚠️ Uncommitted or mismatched client routing (${owner.apiKeyId}) was restored from the Codex journal.`); + return true; + } + const pid = owner?.kind === "process" ? owner.pid : journal.pid; try { - process.kill(journal.pid, 0); + process.kill(pid, 0); return false; } catch (e: unknown) { if ((e as NodeJS.ErrnoException).code === "EPERM") { @@ -220,6 +256,6 @@ export function reconcileJournal(): boolean { } const restored = restoreJournalState(); if (!restored.configRestored && !restored.profileRestored) return false; - console.error(`⚠️ Previous session (PID ${journal.pid}) did not shut down cleanly. Codex state restored from journal.`); + console.error(`⚠️ Previous session (PID ${pid}) did not shut down cleanly. Codex state restored from journal.`); return true; } diff --git a/tests/codex-inject.test.ts b/tests/codex-inject.test.ts index 1d9b86a00f..14197aa4ec 100644 --- a/tests/codex-inject.test.ts +++ b/tests/codex-inject.test.ts @@ -10,6 +10,7 @@ import { stripInjectedOpenaiBaseUrl, stripOpencodexConfig, stripRootContextWindowOverrides, + standaloneCodexRoutingTarget, } from "../src/codex/inject"; import { MANAGED_AGENTS_TABLE_MARKER, @@ -17,6 +18,32 @@ import { } from "../src/codex/subagent-defaults"; describe("Codex config injection", () => { + test("standalone routing-target wrappers remain byte-compatible", () => { + const target = standaloneCodexRoutingTarget(10100, { hostname: "192.168.1.20" }); + expect(buildProviderTableBlock(target, true)).toBe( + buildProviderTableBlock(10100, true, true, "192.168.1.20"), + ); + expect(buildProfileFile(target, "/tmp/opencodex-catalog.json", true)).toBe( + buildProfileFile(10100, "/tmp/opencodex-catalog.json", true, true, "192.168.1.20"), + ); + }); + + test("explicit HTTPS target emits exact provider destination and admission env", () => { + const target = { + baseUrl: "https://hub.example.test/v1", + requiresAdmissionToken: true, + tokenEnv: "OPENCODEX_API_AUTH_TOKEN" as const, + }; + const block = buildProviderTableBlock(target); + expect(block).toContain('base_url = "https://hub.example.test/v1"'); + expect(block).toContain('env_key = "OPENCODEX_API_AUTH_TOKEN"'); + const loopbackLooking = buildProviderTableBlock({ ...target, baseUrl: "https://127.0.0.1/v1" }); + expect(loopbackLooking).toContain('env_key = "OPENCODEX_API_AUTH_TOKEN"'); + expect(() => buildProviderTableBlock({ ...target, baseUrl: "https://hub.example.test/not-v1" })).toThrow( + "canonical HTTP(S) /v1 URL", + ); + }); + test("omits provider-level Responses WebSocket support by default", () => { const block = buildProviderTableBlock(10100); diff --git a/tests/codex-journal.test.ts b/tests/codex-journal.test.ts index b1e5e1fbce..cdb59121ed 100644 --- a/tests/codex-journal.test.ts +++ b/tests/codex-journal.test.ts @@ -126,6 +126,39 @@ describe("codex-journal", () => { expect(existsSync(journalPath)).toBe(true); }); + test("client-owned journal survives only the matching committed api key id", () => { + const journalPath = join(testDir, "opencodex-journal.json"); + const original = "# original client baseline\n"; + const injected = "# connected routing\n"; + writeFileSync(join(testDir, "config.toml"), injected, "utf8"); + writeFileSync(journalPath, JSON.stringify({ + version: 1, + originalConfig: Buffer.from(original).toString("base64"), + originalProfile: null, + owner: { kind: "client", apiKeyId: "client-key-1" }, + pid: 999999, + timestamp: new Date().toISOString(), + }), "utf8"); + + const preserved = runScript(testDir, ` + const { reconcileJournal } = require("./src/codex/journal"); + console.log(JSON.stringify({ restored: reconcileJournal({ activeClientApiKeyId: "client-key-1" }) })); + `); + expect(preserved.status).toBe(0); + expect(JSON.parse(preserved.stdout).restored).toBe(false); + expect(readFileSync(join(testDir, "config.toml"), "utf8")).toBe(injected); + expect(existsSync(journalPath)).toBe(true); + + const restored = runScript(testDir, ` + const { reconcileJournal } = require("./src/codex/journal"); + console.log(JSON.stringify({ restored: reconcileJournal({ activeClientApiKeyId: "different-key" }) })); + `); + expect(restored.status).toBe(0); + expect(JSON.parse(restored.stdout).restored).toBe(true); + expect(readFileSync(join(testDir, "config.toml"), "utf8")).toBe(original); + expect(existsSync(journalPath)).toBe(false); + }); + test("removeJournal cleans up", () => { const journalPath = join(testDir, "opencodex-journal.json"); writeFileSync(journalPath, "{}", "utf8"); From 91a4f6c409fe14a6a410e6bd1632974fc32d6788 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 02:51:01 +0900 Subject: [PATCH 034/172] feat(connect): add remote hub CLI and sync --- src/claude/gateway-cache.ts | 26 ++- src/cli/claude.ts | 132 ++++++++++-- src/cli/connect.ts | 198 ++++++++++++++++++ src/cli/dispatch.ts | 49 +++++ src/cli/help.ts | 2 + src/cli/index.ts | 4 + src/cli/registry.ts | 15 ++ src/cli/runtime-api.ts | 11 +- src/cli/status.ts | 26 +++ src/client/connect.ts | 406 ++++++++++++++++++++++++++++++++++++ src/client/hub-client.ts | 301 ++++++++++++++++++++++++++ 11 files changed, 1145 insertions(+), 25 deletions(-) create mode 100644 src/cli/connect.ts create mode 100644 src/client/connect.ts create mode 100644 src/client/hub-client.ts diff --git a/src/claude/gateway-cache.ts b/src/claude/gateway-cache.ts index aeedf3e652..33df1e8457 100644 --- a/src/claude/gateway-cache.ts +++ b/src/claude/gateway-cache.ts @@ -26,6 +26,12 @@ export interface GatewayModelCacheRefreshOptions { configDir?: string; admissionConfig?: Pick; env?: NodeJS.ProcessEnv; + fetchImpl?: typeof fetch; +} + +export interface GatewayModelTarget { + baseUrl: string; + admissionToken: string; } /** Claude Code config dir (CLAUDE_CONFIG_DIR override honored, like the CLI). */ @@ -70,6 +76,14 @@ function serviceFileToken(env: NodeJS.ProcessEnv): string | null { /** Fetch the anthropic-flavor /v1/models from the local proxy and write the cache. */ export async function refreshGatewayModelCacheFromProxy( port: number, + options?: GatewayModelCacheRefreshOptions, +): Promise; +export async function refreshGatewayModelCacheFromProxy( + target: GatewayModelTarget, + options?: GatewayModelCacheRefreshOptions, +): Promise; +export async function refreshGatewayModelCacheFromProxy( + portOrTarget: number | GatewayModelTarget, options: GatewayModelCacheRefreshOptions = {}, ): Promise { try { @@ -82,12 +96,18 @@ export async function refreshGatewayModelCacheFromProxy( const configuredToken = options.admissionConfig?.apiKeys ?.find(entry => entry.key.trim().length > 0) ?.key.trim(); - const admissionToken = envToken || serviceFileToken(options.env ?? process.env) || configuredToken; + const admissionToken = typeof portOrTarget === "number" + ? envToken || serviceFileToken(options.env ?? process.env) || configuredToken + : portOrTarget.admissionToken; if (admissionToken) headers.set("x-opencodex-api-key", admissionToken); + const baseUrl = typeof portOrTarget === "number" + ? `http://127.0.0.1:${portOrTarget}` + : new URL(portOrTarget.baseUrl).origin; + // ?ids=cli pins the readable claude-ocx id family deterministically (audit 051 // #5): the cache prewrite must not depend on UA sniffing. - const res = await fetch(`http://127.0.0.1:${port}/v1/models?limit=1000&ids=cli`, { + const res = await (options.fetchImpl ?? fetch)(`${baseUrl}/v1/models?limit=1000&ids=cli`, { headers, signal: AbortSignal.timeout(options.timeoutMs ?? 3_000), }); @@ -100,7 +120,7 @@ export async function refreshGatewayModelCacheFromProxy( id: m.id as string, display_name: typeof m.display_name === "string" ? m.display_name : undefined, })); - return writeGatewayModelCache(`http://127.0.0.1:${port}`, models, options.configDir); + return writeGatewayModelCache(baseUrl, models, options.configDir); } catch { return null; } diff --git a/src/cli/claude.ts b/src/cli/claude.ts index 0b1d74cea7..fa84f1813f 100644 --- a/src/cli/claude.ts +++ b/src/cli/claude.ts @@ -21,11 +21,22 @@ import { resolveClaudeAuthMode } from "../claude/auth-mode"; import { withProcessRuntimeProvenance } from "../lib/bun-runtime"; import { selfLaunchArgv } from "../lib/self-launch-argv"; import { ANTHROPIC_PARENT_ENV_SLOTS, trustedNodeLauncherContext, type AnthropicParentEnvSlot } from "./launcher-context"; +import { readClientConnectionState } from "../client/state"; +import { readServiceApiTokenState } from "../lib/service-secrets"; +import { DEFAULT_CATALOG_PATH } from "../codex/paths"; +import { readFileSync } from "node:fs"; +import { aliasForNative, aliasForRoute } from "../claude/alias"; +import { desktop3pAlias } from "../claude/desktop-3p"; export interface ClaudeLaunchEnv { [key: string]: string | undefined; } +export interface ClaudeRoutingTarget { + baseUrl: string; + admissionToken: string; +} + /** * Injectable IO for tests. `env` is deliberately NOT injectable: it is bound to the * launch base so detection and the spawned process can never disagree (audit R3-3). @@ -61,6 +72,20 @@ function targetsLocalClaudeProxy(value: string | undefined, port: number): boole } } +function targetsClaudeRoutingTarget(value: string | undefined, target: ClaudeRoutingTarget): boolean { + if (!value) return false; + try { + const actual = new URL(value); + const expected = new URL(target.baseUrl); + return actual.origin === expected.origin + && (actual.pathname === "/" || actual.pathname === "") + && !actual.username + && !actual.password; + } catch { + return false; + } +} + /** * Pure env assembly (unit-tested): never sets ANTHROPIC_API_KEY (setting both * token vars triggers Claude Code's auth-conflict warning, 003 E1), and never @@ -70,11 +95,14 @@ function targetsLocalClaudeProxy(value: string | undefined, port: number): boole */ export function buildClaudeEnv( config: OcxConfig, - port: number, + portOrTarget: number | ClaudeRoutingTarget, base: ClaudeLaunchEnv, contextWindows: Record = {}, deps: ClaudeEnvDeps = {}, ): ClaudeLaunchEnv { + const explicitTarget = typeof portOrTarget === "number" ? null : portOrTarget; + const port = typeof portOrTarget === "number" ? portOrTarget : null; + const managedBaseUrl = explicitTarget ? new URL(explicitTarget.baseUrl).origin : `http://127.0.0.1:${port}`; const env: ClaudeLaunchEnv = { ...base }; // Step 1 — strip OUR OWN dummy from the inherited environment before anything reads // or writes the token slot. setDefault below preserves any non-empty value, so a @@ -120,9 +148,9 @@ export function buildClaudeEnv( if (deps.allowRootSkipPermissions === true) { setDefault("IS_SANDBOX", "1"); } - setDefault("ANTHROPIC_BASE_URL", `http://127.0.0.1:${port}`); + setDefault("ANTHROPIC_BASE_URL", managedBaseUrl); const existingBaseUrl = env.ANTHROPIC_BASE_URL; - if (existingBaseUrl) { + if (existingBaseUrl && port !== null) { try { const parsed = new URL(existingBaseUrl); const effectivePort = parsed.port === "" ? 80 : Number(parsed.port); @@ -154,16 +182,20 @@ export function buildClaudeEnv( // the user's Claude login. Only inject a token when the proxy actually requires an // admission key; otherwise Claude Code keeps its own OAuth and sends it to us — // native claude models then pass through verbatim (see server/claude-messages.ts). - const ownTokens = ownAdmissionTokens(config); - const targetsLocalProxy = targetsLocalClaudeProxy(env.ANTHROPIC_BASE_URL, port); + const ownTokens = explicitTarget ? [explicitTarget.admissionToken] : ownAdmissionTokens(config); + const targetsLocalProxy = explicitTarget + ? targetsClaudeRoutingTarget(env.ANTHROPIC_BASE_URL, explicitTarget) + : targetsLocalClaudeProxy(env.ANTHROPIC_BASE_URL, port!); + const isOwnAdmissionToken = (value: string): boolean => + ownTokens.includes(value) || isProxyAdmissionSecret(value, config); const inheritedApiKey = env.ANTHROPIC_API_KEY; - if (typeof inheritedApiKey === "string" && isProxyAdmissionSecret(inheritedApiKey, config)) { + if (typeof inheritedApiKey === "string" && isOwnAdmissionToken(inheritedApiKey)) { delete env.ANTHROPIC_API_KEY; } const hasUserApiKey = Boolean(env.ANTHROPIC_API_KEY?.trim()); const inheritedAuthToken = env.ANTHROPIC_AUTH_TOKEN; const inheritedTokenIsOurs = typeof inheritedAuthToken === "string" - && isProxyAdmissionSecret(inheritedAuthToken, config); + && isOwnAdmissionToken(inheritedAuthToken); // system-env may have injected the proxy's admission key into the parent. A // proof-bound external BASE_URL is still user-owned, so never let our inherited // key follow it. A user API key also wins on a local launch; remove only the token @@ -199,7 +231,7 @@ export function buildClaudeEnv( && typeof finalAuthToken === "string" && ( finalAuthToken.trim() === PROXY_MARKER - || isProxyAdmissionSecret(finalAuthToken, config) + || isOwnAdmissionToken(finalAuthToken) ); if (resolved.origin === "auto-unknown") { console.error("⚠ Claude 인증을 확인하지 못했습니다 — 구독 방식으로 진행합니다. GUI에서 인증 모드를 직접 지정하면 이 판단을 덮어쓸 수 있습니다."); @@ -282,6 +314,38 @@ export async function fetchClaudeContextWindows(config: OcxConfig, port: number, } } +export function readConnectedClaudeContextWindows(path = DEFAULT_CATALOG_PATH): Record { + try { + const parsed = JSON.parse(readFileSync(path, "utf8")) as { models?: unknown }; + if (!Array.isArray(parsed.models)) return {}; + const out: Record = {}; + const put = (key: string, value: number) => { if (out[key] === undefined) out[key] = value; }; + for (const row of parsed.models) { + if (!row || typeof row !== "object" || Array.isArray(row)) continue; + const entry = row as Record; + const slug = typeof entry.slug === "string" ? entry.slug : ""; + const contextWindow = typeof entry.context_window === "number" && entry.context_window > 0 + ? entry.context_window + : undefined; + if (!slug || contextWindow === undefined) continue; + put(slug, contextWindow); + const slash = slug.indexOf("/"); + if (slash > 0 && slash < slug.length - 1) { + const provider = slug.slice(0, slash); + const id = slug.slice(slash + 1); + put(aliasForRoute(provider, id), contextWindow); + put(desktop3pAlias(provider, id), contextWindow); + } else { + put(aliasForNative(slug), contextWindow); + put(desktop3pAlias("native", slug), contextWindow); + } + } + return out; + } catch { + return {}; + } +} + async function ensureProxyForClaude(): Promise { const live = await findLiveProxy(); if (live) return live.port; @@ -340,21 +404,45 @@ export async function cmdClaude(args: string[]): Promise { console.error("Claude inbound is disabled (config.claudeCode.enabled=false — flip the Claude ON toggle in the GUI or edit config)."); return 1; } - const port = await ensureProxyForClaude(); - if (!port) { - console.error("❌ Proxy did not become healthy after starting."); + const clientState = readClientConnectionState(); + if (clientState.kind === "invalid" || clientState.kind === "mismatched") { + console.error(`Client state is ${clientState.kind}: ${clientState.reason}`); return 1; } - const contextWindows = await fetchClaudeContextWindows(config, port); + let route: number | ClaudeRoutingTarget; + let contextWindows: Record; + if (clientState.kind === "connected") { + if (!clientState.value.selectedClients.includes("claude")) { + console.error("Claude is not selected for this remote hub connection."); + return 1; + } + const token = readServiceApiTokenState(); + if (token.kind !== "present" || token.fingerprint !== clientState.value.tokenFingerprint) { + console.error(token.kind === "absent" ? "Connected service token is missing." : "Connected service token ownership changed."); + return 1; + } + route = { baseUrl: clientState.value.serverUrl, admissionToken: token.token }; + contextWindows = readConnectedClaudeContextWindows(); + } else { + const port = await ensureProxyForClaude(); + if (!port) { + console.error("❌ Proxy did not become healthy after starting."); + return 1; + } + route = port; + contextWindows = await fetchClaudeContextWindows(config, port); + } const allowRootSkipPermissions = shouldAllowRootSkipPermissions(args); - const env = buildClaudeEnv(config, port, process.env, contextWindows, { allowRootSkipPermissions }); + const env = buildClaudeEnv(config, route, process.env, contextWindows, { allowRootSkipPermissions }); if (allowRootSkipPermissions) { console.error(rootSkipPermissionsNotice(env)); } // Pre-write the CLI's gateway-model cache (devlog 030): without a token the CLI // never refreshes it, so the picker would keep showing yesterday's aliases. try { - const cachePath = await refreshGatewayModelCacheFromProxy(port, { admissionConfig: config }); + const cachePath = typeof route === "number" + ? await refreshGatewayModelCacheFromProxy(route, { admissionConfig: config }) + : await refreshGatewayModelCacheFromProxy(route, { admissionConfig: config }); if (cachePath === null) { console.error("⚠ Gateway model cache could not be refreshed; the model picker may be stale."); } @@ -363,14 +451,16 @@ export async function cmdClaude(args: string[]): Promise { console.error(`⚠ Gateway model cache could not be refreshed: ${message}`); } // Sync roster agents (devlog 070): subagentModels + self -> ~/.claude/agents/ocx-*.md. - try { - const written = injectClaudeAgentDefs(config, contextWindows); - if (written === null) { - console.error("⚠ Claude agent definitions could not be synced; check ~/.claude/agents permissions."); + if (typeof route === "number") { + try { + const written = injectClaudeAgentDefs(config, contextWindows); + if (written === null) { + console.error("⚠ Claude agent definitions could not be synced; check ~/.claude/agents permissions."); + } + } catch (error) { + const message = error instanceof Error ? error.message : String(error); + console.error(`⚠ Claude agent definitions could not be synced: ${message}`); } - } catch (error) { - const message = error instanceof Error ? error.message : String(error); - console.error(`⚠ Claude agent definitions could not be synced: ${message}`); } return await new Promise(resolve => { const inv = commandInvocation("claude", args); diff --git a/src/cli/connect.ts b/src/cli/connect.ts new file mode 100644 index 0000000000..1182a9b2fb --- /dev/null +++ b/src/cli/connect.ts @@ -0,0 +1,198 @@ +import { existsSync, lstatSync } from "node:fs"; +import { DEFAULT_CATALOG_PATH } from "../codex/paths"; +import { + disconnectClient, + revokeConnectedClientKey, + connectClient, +} from "../client/connect"; +import { readClientConnectionState } from "../client/state"; +import { readServiceApiTokenState } from "../lib/service-secrets"; +import type { OcxConnectedClientId } from "../types"; +import { + CliUsageError, + csv, + printData, + readSecretLine, + rejectArgs, + runCliAction, + takeFlag, + takeOption, + type RuntimeApiDeps, +} from "./runtime-api"; + +export const CONNECT_USAGE = `Usage: + ocx connect [--management-url ] + (--pairing-code-stdin | --admin-token-stdin) + [--clients codex,claude] [--management-transport direct|relay] + [--allow-insecure-http] [--no-sync] + ocx connect status [--json] + ocx connect revoke --admin-token-stdin [--json]`; + +export const DISCONNECT_USAGE = `Usage: + ocx disconnect [--keep-catalog] [--json]`; + +export type ClientConnectionStatus = { + state: "disconnected" | "connected" | "invalid" | "mismatched"; + reason?: string; + serverUrl?: string; + managementUrl?: string; + managementTransport?: "direct" | "relay"; + protocolVersion?: number; + apiKeyId?: string; + selectedClients?: OcxConnectedClientId[]; + connectedAt?: string; + catalogSyncedAt?: string; + catalogAgeSeconds?: number; + catalog: "present" | "missing" | "unsafe"; + token: "owned" | "missing" | "changed" | "unsafe"; +}; + +export function collectClientConnectionStatus(now = Date.now()): ClientConnectionStatus { + const state = readClientConnectionState(); + const tokenState = readServiceApiTokenState(); + let catalog: ClientConnectionStatus["catalog"] = "missing"; + if (existsSync(DEFAULT_CATALOG_PATH)) { + try { + const stat = lstatSync(DEFAULT_CATALOG_PATH); + catalog = !stat.isSymbolicLink() && stat.isFile() ? "present" : "unsafe"; + } catch { + catalog = "unsafe"; + } + } + if (state.kind !== "connected") { + return { + state: state.kind, + ...(state.kind === "invalid" || state.kind === "mismatched" ? { reason: state.reason } : {}), + catalog, + token: tokenState.kind === "absent" ? "missing" : tokenState.kind === "unsafe" ? "unsafe" : "changed", + }; + } + const catalogAgeSeconds = state.value.catalogSyncedAt + ? Math.max(0, Math.floor((now - Date.parse(state.value.catalogSyncedAt)) / 1000)) + : undefined; + const token = tokenState.kind === "absent" + ? "missing" + : tokenState.kind === "unsafe" + ? "unsafe" + : tokenState.fingerprint === state.value.tokenFingerprint ? "owned" : "changed"; + return { + state: "connected", + serverUrl: state.value.serverUrl, + managementUrl: state.value.managementUrl, + managementTransport: state.value.managementTransport, + protocolVersion: state.value.protocolVersion, + apiKeyId: state.value.apiKeyId, + selectedClients: [...state.value.selectedClients], + connectedAt: state.value.connectedAt, + ...(state.value.catalogSyncedAt ? { catalogSyncedAt: state.value.catalogSyncedAt } : {}), + ...(catalogAgeSeconds !== undefined ? { catalogAgeSeconds } : {}), + catalog, + token, + }; +} + +function parseClients(raw: string | undefined): OcxConnectedClientId[] { + const values = csv(raw) ?? ["codex", "claude"]; + if (values.length < 1 || values.some(value => value !== "codex" && value !== "claude")) { + throw new CliUsageError("--clients must contain codex and/or claude", CONNECT_USAGE); + } + return values as OcxConnectedClientId[]; +} + +function statusLines(status: ClientConnectionStatus): string[] { + if (status.state !== "connected") { + return [`Connection: ${status.state}${status.reason ? ` (${status.reason})` : ""}`]; + } + return [ + "Connection: connected", + `Hub: ${status.serverUrl}`, + `Management: ${status.managementUrl} (${status.managementTransport})`, + `Protocol: ${status.protocolVersion}`, + `API key id: ${status.apiKeyId}`, + `Clients: ${status.selectedClients?.join(", ")}`, + `Token file: ${status.token}`, + `Catalog: ${status.catalog}${status.catalogAgeSeconds !== undefined ? ` (${status.catalogAgeSeconds}s old)` : ""}`, + ]; +} + +async function runConnect(argv: string[], deps: RuntimeApiDeps): Promise { + const args = [...argv]; + const serverUrl = args.shift(); + if (!serverUrl || serverUrl.startsWith("--")) throw new CliUsageError("hub URL is required", CONNECT_USAGE); + const managementUrl = takeOption(args, "--management-url"); + const clients = parseClients(takeOption(args, "--clients")); + const managementTransport = takeOption(args, "--management-transport") ?? "direct"; + if (managementTransport !== "direct" && managementTransport !== "relay") { + throw new CliUsageError("--management-transport must be direct or relay", CONNECT_USAGE); + } + const pairing = takeFlag(args, "--pairing-code-stdin"); + const admin = takeFlag(args, "--admin-token-stdin"); + const allowInsecureHttp = takeFlag(args, "--allow-insecure-http"); + const noSync = takeFlag(args, "--no-sync"); + if (Number(pairing) + Number(admin) !== 1) { + throw new CliUsageError("choose exactly one of --pairing-code-stdin or --admin-token-stdin", CONNECT_USAGE); + } + rejectArgs(args, CONNECT_USAGE, { redactValues: true }); + const secret = await readSecretLine(deps, pairing ? "pairing code" : "admin token"); + const value = new TextEncoder().encode(secret); + const connection = await connectClient({ + serverUrl, + ...(managementUrl ? { managementUrl } : {}), + credential: { kind: pairing ? "pairing-grant" : "admin", value }, + selectedClients: clients, + managementTransport, + allowInsecureHttp, + noSync, + }, { fetchImpl: deps.fetchImpl }); + console.log(`Connected to ${connection.serverUrl} as key ${connection.apiKeyId}.`); +} + +async function runRevoke(argv: string[], deps: RuntimeApiDeps): Promise { + const args = [...argv]; + const wantsJson = takeFlag(args, "--json"); + const admin = takeFlag(args, "--admin-token-stdin"); + if (!admin) throw new CliUsageError("revoke requires --admin-token-stdin", CONNECT_USAGE); + rejectArgs(args, CONNECT_USAGE, { redactValues: true }); + const value = new TextEncoder().encode(await readSecretLine(deps, "admin token")); + const result = await revokeConnectedClientKey({ kind: "admin", value }, { fetchImpl: deps.fetchImpl }); + printData(result, wantsJson, [`Revoked connected API key ${result.apiKeyId}. Disconnect this client next.`]); +} + +export async function handleConnectCommand(argv: string[], deps: RuntimeApiDeps = {}): Promise { + return runCliAction(async () => { + if (argv[0] === "status") { + const args = argv.slice(1); + const wantsJson = takeFlag(args, "--json"); + rejectArgs(args, CONNECT_USAGE, { redactValues: true }); + const status = collectClientConnectionStatus(); + printData(status, wantsJson, statusLines(status)); + return; + } + if (argv[0] === "revoke") { + await runRevoke(argv.slice(1), deps); + return; + } + await runConnect(argv, deps); + }); +} + +export async function handleDisconnectCommand(argv: string[]): Promise { + return runCliAction(async () => { + const args = [...argv]; + const keepCatalog = takeFlag(args, "--keep-catalog"); + const wantsJson = takeFlag(args, "--json"); + rejectArgs(args, DISCONNECT_USAGE, { redactValues: true }); + const result = await disconnectClient({ keepCatalog }); + const payload = { + ...result, + revoke: { + apiKeyId: result.apiKeyId, + location: "Integrations → API Keys", + }, + }; + printData(payload, wantsJson, [ + "Disconnected locally; native Codex state was restored.", + `The hub key ${result.apiKeyId} is still valid. Revoke it from Integrations → API Keys.`, + ]); + }); +} diff --git a/src/cli/dispatch.ts b/src/cli/dispatch.ts index 8e09cc29e8..bcec92e814 100644 --- a/src/cli/dispatch.ts +++ b/src/cli/dispatch.ts @@ -59,6 +59,16 @@ const commandRunners: Record = { return Number(process.exitCode ?? 0); }, start: async deps => { + const { readClientConnectionState } = await import("../client/state"); + const clientState = readClientConnectionState(); + if (clientState.kind === "connected") { + console.error("Client mode does not start a local provider proxy in Remote Hub Phase 3; use 'ocx sync'."); + return 1; + } + if (clientState.kind === "invalid" || clientState.kind === "mismatched") { + console.error(`Client state is ${clientState.kind}: ${clientState.reason}`); + return 1; + } await deps.handleStart(); return Number(process.exitCode ?? 0); }, @@ -245,6 +255,14 @@ const commandRunners: Record = { return 0; }, ensure: async deps => { + const { readClientConnectionState } = await import("../client/state"); + const clientState = readClientConnectionState(); + if (clientState.kind !== "disconnected") { + console.error(clientState.kind === "connected" + ? "Client mode does not start a local provider proxy; use 'ocx sync'." + : `Client state is ${clientState.kind}: ${clientState.reason}`); + return 1; + } await deps.handleEnsure(); return Number(process.exitCode ?? 0); }, @@ -317,6 +335,29 @@ const commandRunners: Record = { // Separate flag on purpose: --restart-codex promises app-server-only scope, // and quitting the desktop app ends live conversations. const restartDesktopApp = syncArgs.includes("--restart-desktop-app"); + const { readClientConnectionState } = await import("../client/state"); + const clientState = readClientConnectionState(); + if (clientState.kind === "invalid" || clientState.kind === "mismatched") { + console.error(`Client state is ${clientState.kind}: ${clientState.reason}`); + return 1; + } + if (clientState.kind === "connected") { + try { + const { syncConnectedClient } = await import("../client/connect"); + const result = await syncConnectedClient({ restartCodex }); + console.log(result.stale + ? "Hub unavailable; retained and applied the last-known-good remote catalog (stale)." + : "Remote hub catalog synchronized."); + if (result.catalogWritten || result.cacheSynced) { + afterCatalogWriteHandleAppServers({ restart: restartCodex, log: console }); + if (restartDesktopApp) await handleDesktopAppRestart(console); + } + return 0; + } catch (error) { + console.error(`Connected sync failed without local fallback: ${error instanceof Error ? error.message : String(error)}`); + return 1; + } + } const live = await deps.findLiveProxy(); const synced = await syncModelsToCodex( live?.port, @@ -372,6 +413,14 @@ const commandRunners: Record = { const { cmdV2 } = await import("./v2"); return await cmdV2(deps.args.slice(1), {}, async () => (await deps.findLiveProxy())?.port); }, + connect: async deps => { + const { handleConnectCommand } = await import("./connect"); + return await handleConnectCommand(deps.args.slice(1)); + }, + disconnect: async deps => { + const { handleDisconnectCommand } = await import("./connect"); + return await handleDisconnectCommand(deps.args.slice(1)); + }, "sync-cache": async deps => { const cacheArgs = deps.args.slice(1); const restartCodex = cacheArgs.includes("--restart-codex"); diff --git a/src/cli/help.ts b/src/cli/help.ts index 95a0a8ebd1..e03cdd903e 100644 --- a/src/cli/help.ts +++ b/src/cli/help.ts @@ -38,6 +38,8 @@ Usage: ocx codex-shim Auto-start proxy when \`codex\` launches (install|status|uninstall|remove) ocx tray Windows status tray (install|start|stop|status|uninstall) ocx ensure Ensure the proxy is running and Codex config/cache are current + ocx connect Connect this machine to a remote OpenCodex hub (credential via stdin) + ocx disconnect Restore local state and clear the hub connection ocx sync [--restart-codex] Fetch models from providers and inject into Codex config ocx sync-cache [--restart-codex] Refresh Codex's model cache from the active catalog diff --git a/src/cli/index.ts b/src/cli/index.ts index 37eb47b714..74e58af77f 100755 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -1300,6 +1300,10 @@ async function handleStatus() { console.log(` Runtime: ${status.json.paths.runtime}`); console.log(` Runtime source: ${status.json.runtime.source}${status.json.runtime.overrideEnv ? ` (${status.json.runtime.overrideEnv})` : ""}`); console.log(` Default provider: ${status.json.defaultProvider}`); + console.log(` Remote hub: ${status.json.connection.state}${status.json.connection.serverUrl ? ` (${status.json.connection.serverUrl})` : ""}`); + if (status.json.connection.state === "invalid" || status.json.connection.state === "mismatched") { + console.log(` ⚠️ ${status.json.connection.reason}`); + } console.log(` Codex autostart: ${status.json.codexAutostart ? "enabled" : "disabled"}`); console.log(` Restart safety: ${startupHealthSummary(status.json.startup)}`); console.log(` ${formatStartupRoutingDetail(status.json.startup)}`); diff --git a/src/cli/registry.ts b/src/cli/registry.ts index d607c0b588..f09356d4e3 100644 --- a/src/cli/registry.ts +++ b/src/cli/registry.ts @@ -86,6 +86,21 @@ export const CLI_COMMANDS: CliCommandEntry[] = [ ], }, { name: "ensure", usage: "ocx ensure", summary: "Ensure the proxy is running and Codex config/cache are current." }, + { + name: "connect", + usage: "ocx connect [--management-url ] (--pairing-code-stdin | --admin-token-stdin) [--clients codex,claude] [--management-transport direct|relay] [--allow-insecure-http] [--no-sync]", + summary: "Connect this machine to a remote OpenCodex hub without persisting the one-time authority.", + details: [ + "Status: ocx connect status [--json]", + "Revoke while connected: ocx connect revoke --admin-token-stdin [--json]", + "Credentials are accepted only through stdin; argv and environment credential forms are not supported.", + ], + }, + { + name: "disconnect", + usage: "ocx disconnect [--keep-catalog] [--json]", + summary: "Restore local client state offline and clear the remote-hub connection.", + }, { name: "sync", usage: "ocx sync [--restart-codex] [--restart-desktop-app]", diff --git a/src/cli/runtime-api.ts b/src/cli/runtime-api.ts index 0ec60fcb07..15c5b037d6 100644 --- a/src/cli/runtime-api.ts +++ b/src/cli/runtime-api.ts @@ -182,7 +182,16 @@ export function csv(value: string | undefined): string[] | undefined { * `--code=https://…?code=SECRET` that writes the authorization code to stderr, * which is the exact exposure the stdin path exists to avoid. */ -const SECRET_OPTIONS = ["--code", "--headers"]; +const SECRET_OPTIONS = [ + "--code", + "--headers", + "--token", + "--admin-token", + "--pairing-code", + "--credential-env", + "--admin-token-env", + "--pairing-code-env", +]; /** * Replace credential values before they are reported back. diff --git a/src/cli/status.ts b/src/cli/status.ts index 8c435d0582..e3120b074d 100644 --- a/src/cli/status.ts +++ b/src/cli/status.ts @@ -18,6 +18,7 @@ import { collectOrcaCodexHomeDiagnostic, type OrcaCodexHomeDiagnostic } from ".. import { grokFenceEndpointDrift, readGrokStatus } from "../grok/status"; import { claudeDesktopIntegrationEnabled } from "../codex/desired-state"; import { claudeDesktopPolicyHealth, probeClaudeDesktopPolicy, type ClaudeDesktopPolicyHealth } from "../claude/desktop-policy"; +import { collectClientConnectionStatus } from "./connect"; type HealthCheck = { ok: boolean; @@ -63,6 +64,18 @@ export type CliStatusJson = { source: "default" | "file" | "fallback"; error: string | null; }; + connection: { + state: "disconnected" | "connected" | "invalid" | "mismatched"; + reason?: string; + serverUrl?: string; + managementUrl?: string; + protocolVersion?: number; + apiKeyId?: string; + selectedClients?: string[]; + catalog?: "present" | "missing" | "unsafe"; + catalogAgeSeconds?: number; + credentialFile: "owned" | "missing" | "changed" | "unsafe"; + }; service: { summary: string }; codexShim: { summary: string }; codexPlugins: CodexPluginsDiagnostic; @@ -309,6 +322,7 @@ export async function collectStatus(): Promise { desiredEnabled: claudeDesktopIntegrationEnabled(config), policy: claudeDesktopPolicyHealth(probeClaudeDesktopPolicy()), }; + const clientConnection = collectClientConnectionStatus(); // Prefer identity-verified liveness (runtime-port + /healthz) over ocx.pid alone (#618). // Pass the already-resolved diagnostics config so findLiveProxy does not re-load and // warn on malformed config.json (status --json must stay stderr-clean). @@ -491,6 +505,18 @@ export async function collectStatus(): Promise { source: configDiagnostics.source, error: configDiagnostics.error, }, + connection: { + state: clientConnection.state, + ...(clientConnection.reason ? { reason: clientConnection.reason } : {}), + ...(clientConnection.serverUrl ? { serverUrl: clientConnection.serverUrl } : {}), + ...(clientConnection.managementUrl ? { managementUrl: clientConnection.managementUrl } : {}), + ...(clientConnection.protocolVersion ? { protocolVersion: clientConnection.protocolVersion } : {}), + ...(clientConnection.apiKeyId ? { apiKeyId: clientConnection.apiKeyId } : {}), + ...(clientConnection.selectedClients ? { selectedClients: [...clientConnection.selectedClients] } : {}), + catalog: clientConnection.catalog, + ...(clientConnection.catalogAgeSeconds !== undefined ? { catalogAgeSeconds: clientConnection.catalogAgeSeconds } : {}), + credentialFile: clientConnection.token, + }, service: { summary: serviceSummary }, codexShim: { summary: codexShimSummary }, codexPlugins, diff --git a/src/client/connect.ts b/src/client/connect.ts new file mode 100644 index 0000000000..3c954e5146 --- /dev/null +++ b/src/client/connect.ts @@ -0,0 +1,406 @@ +import { createHash } from "node:crypto"; +import { + existsSync, + lstatSync, + readFileSync, + unlinkSync, +} from "node:fs"; +import { hostname } from "node:os"; +import { atomicWriteFile, loadConfig } from "../config"; +import { invalidateCodexModelsCache } from "../codex/catalog/sync"; +import { + injectCodexConfig, + currentExternalCodexModelProvider, + isCodexRoutingInjected, + type CodexRoutingTarget, +} from "../codex/inject"; +import { + journalOwner, + restoreJournalState, +} from "../codex/journal"; +import { DEFAULT_CATALOG_PATH } from "../codex/paths"; +import { + readServiceApiTokenState, + removeServiceApiTokenFileIfOwned, + writeServiceApiTokenFile, +} from "../lib/service-secrets"; +import { MAX_REMOTE_CATALOG_BYTES } from "../server/catalog-download"; +import type { + OcxClientConnectionConfig, + OcxConnectedClientId, +} from "../types"; +import { + downloadClientCatalog, + exchangeConnectPairingGrant, + fetchHubReady, + HubClientError, + issueClientKey, + normalizeHubOrigin, + revokeClientKey, + type ConnectGuiSession, + type IssuedClientKey, + type OneTimeConnectCredential, +} from "./hub-client"; +import { + clearClientConnection, + commitClientConnection, + readClientConnectionState, +} from "./state"; + +export interface ConnectOptions { + serverUrl: string; + managementUrl?: string; + credential: OneTimeConnectCredential; + selectedClients: OcxConnectedClientId[]; + managementTransport: "direct" | "relay"; + noSync?: boolean; + allowInsecureHttp?: boolean; +} + +export interface ClientConnectDeps { + fetchImpl?: typeof fetch; + now?: () => Date; +} + +type CatalogSnapshot = + | { kind: "absent" } + | { kind: "file"; body: string; fingerprint: string }; + +function sha256(value: string): string { + return createHash("sha256").update(value).digest("hex"); +} + +function catalogSnapshot(): CatalogSnapshot { + if (!existsSync(DEFAULT_CATALOG_PATH)) return { kind: "absent" }; + const stat = lstatSync(DEFAULT_CATALOG_PATH); + if (stat.isSymbolicLink() || !stat.isFile() || stat.size > MAX_REMOTE_CATALOG_BYTES) { + throw new Error("existing OpenCodex catalog is not a bounded regular file"); + } + const body = readFileSync(DEFAULT_CATALOG_PATH, "utf8"); + return { kind: "file", body, fingerprint: sha256(body) }; +} + +function restoreCatalogSnapshot(snapshot: CatalogSnapshot, writtenFingerprint: string): boolean { + try { + if (!existsSync(DEFAULT_CATALOG_PATH)) return snapshot.kind === "absent"; + const stat = lstatSync(DEFAULT_CATALOG_PATH); + if (stat.isSymbolicLink() || !stat.isFile() || stat.size > MAX_REMOTE_CATALOG_BYTES) return false; + const current = readFileSync(DEFAULT_CATALOG_PATH, "utf8"); + if (sha256(current) !== writtenFingerprint) return false; + if (snapshot.kind === "absent") unlinkSync(DEFAULT_CATALOG_PATH); + else atomicWriteFile(DEFAULT_CATALOG_PATH, snapshot.body); + return true; + } catch { + return false; + } +} + +function validLocalCatalog(): string { + const snapshot = catalogSnapshot(); + if (snapshot.kind !== "file") throw new Error("connected catalog is missing"); + try { + const parsed = JSON.parse(snapshot.body) as unknown; + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) throw new Error("invalid"); + } catch { + throw new Error("connected catalog is malformed"); + } + return snapshot.body; +} + +function catalogMatchesEtag(body: string, etag: string | undefined): boolean { + if (!etag) return false; + const digest = createHash("sha256").update(body).digest("base64url"); + return etag === `"sha256-${digest}"` || etag === `W/"sha256-${digest}"`; +} + +function routingTarget(serverUrl: string): CodexRoutingTarget { + return { + baseUrl: `${serverUrl}/v1`, + requiresAdmissionToken: true, + tokenEnv: "OPENCODEX_API_AUTH_TOKEN", + }; +} + +function localGuiOrigin(): string { + const port = loadConfig().port; + return `http://localhost:${Number.isInteger(port) && port > 0 ? port : 10100}`; +} + +function clientKeyName(): string { + const raw = `ocx connect ${hostname() || "client"}`; + return raw.slice(0, 80); +} + +function releaseCredential(credential: OneTimeConnectCredential): void { + credential.value.fill(0); +} + +async function cleanupIssuedKey( + managementUrl: string, + credential: { kind: "admin"; value: Uint8Array } | { kind: "gui-session"; value: ConnectGuiSession }, + issuedId: string, + deps: ClientConnectDeps, +): Promise { + try { + await revokeClientKey(managementUrl, credential, issuedId, { fetchImpl: deps.fetchImpl }); + return null; + } catch { + return `Hub cleanup could not revoke client key ${issuedId}; revoke it from Integrations → API Keys.`; + } +} + +export async function connectClient( + options: ConnectOptions, + deps: ClientConnectDeps = {}, +): Promise { + let serverUrl = ""; + let managementUrl = ""; + let issued: IssuedClientKey | null = null; + let cleanupCredential: { kind: "admin"; value: Uint8Array } | { kind: "gui-session"; value: ConnectGuiSession } | null = null; + let tokenFingerprint: string | null = null; + let priorCatalog: CatalogSnapshot | null = null; + let writtenCatalogFingerprint: string | null = null; + let injectionCommitted = false; + let committed = false; + try { + serverUrl = normalizeHubOrigin(options.serverUrl); + if (options.managementUrl) managementUrl = normalizeHubOrigin(options.managementUrl); + if (options.selectedClients.length < 1 || new Set(options.selectedClients).size !== options.selectedClients.length) { + throw new Error("at least one unique connected client is required"); + } + const state = readClientConnectionState(); + if (state.kind !== "disconnected") { + const detail = state.kind === "connected" ? "already connected" : state.reason; + throw new Error(`connect refused: client state is ${state.kind} (${detail})`); + } + const externalProvider = currentExternalCodexModelProvider(); + if (externalProvider) throw new Error(`connect refused: external Codex provider ${externalProvider} owns config.toml`); + const tokenState = readServiceApiTokenState(); + if (tokenState.kind !== "absent") { + throw new Error(tokenState.kind === "unsafe" ? tokenState.reason : "connect refused: service token file already exists"); + } + + const ready = await fetchHubReady(serverUrl, { fetchImpl: deps.fetchImpl }); + if (ready.status !== "ready") throw new Error(`hub is not ready (${ready.status})`); + managementUrl = managementUrl || ready.metadata.managementUrl; + if (options.managementTransport === "relay") { + throw new Error("relay management transport is not available before Remote Hub Phase 4"); + } + + if (options.credential.kind === "pairing-grant") { + const session = await exchangeConnectPairingGrant( + managementUrl, + localGuiOrigin(), + options.credential.value, + { allowInsecureHttp: options.allowInsecureHttp, fetchImpl: deps.fetchImpl }, + ); + cleanupCredential = { kind: "gui-session", value: session }; + } else { + cleanupCredential = { kind: "admin", value: options.credential.value }; + } + issued = await issueClientKey(managementUrl, cleanupCredential, clientKeyName(), { fetchImpl: deps.fetchImpl }); + + priorCatalog = catalogSnapshot(); + const persisted = writeServiceApiTokenFile(issued.key); + tokenFingerprint = persisted.fingerprint; + + const catalog = await downloadClientCatalog(serverUrl, issued.key, { fetchImpl: deps.fetchImpl }); + if (catalog.kind !== "fresh" || !catalog.etag) { + throw new Error("initial hub catalog did not include a fresh ETag"); + } + atomicWriteFile(DEFAULT_CATALOG_PATH, catalog.body); + writtenCatalogFingerprint = sha256(catalog.body); + + const config = loadConfig(); + const target = routingTarget(serverUrl); + const injectConfig = { ...config, syncResumeHistory: false }; + const preflight = await injectCodexConfig(config.port, injectConfig, { + validateOnly: true, + routingTarget: target, + catalogPath: DEFAULT_CATALOG_PATH, + journalOwner: { kind: "client", apiKeyId: issued.id }, + }); + if (!preflight.success) throw new Error(preflight.message); + + if (!options.noSync && options.selectedClients.includes("codex")) { + const injected = await injectCodexConfig(config.port, injectConfig, { + routingTarget: target, + catalogPath: DEFAULT_CATALOG_PATH, + journalOwner: { kind: "client", apiKeyId: issued.id }, + }); + if (!injected.success || injected.status === "skipped") throw new Error(injected.message); + injectionCommitted = true; + if (!isCodexRoutingInjected()) throw new Error("Codex routing target was not committed"); + } + + const now = (deps.now ?? (() => new Date()))().toISOString(); + const connection: OcxClientConnectionConfig = { + serverUrl, + managementUrl, + managementTransport: options.managementTransport, + selectedClients: [...options.selectedClients], + tokenEnv: "OPENCODEX_API_AUTH_TOKEN", + apiKeyId: issued.id, + tokenFingerprint: persisted.fingerprint, + protocolVersion: 1, + connectedAt: now, + catalogEtag: catalog.etag, + catalogSyncedAt: now, + }; + commitClientConnection(connection); + committed = true; + return connection; + } catch (error) { + const rollbackFailures: string[] = []; + if (injectionCommitted) { + const restored = restoreJournalState(); + if (!restored.complete) rollbackFailures.push("Codex journal restore was partial"); + } + if (priorCatalog && writtenCatalogFingerprint && !restoreCatalogSnapshot(priorCatalog, writtenCatalogFingerprint)) { + rollbackFailures.push("catalog rollback did not match the written artifact"); + } + if (tokenFingerprint) { + const removed = removeServiceApiTokenFileIfOwned(tokenFingerprint); + if (removed === "changed") rollbackFailures.push("service token changed during rollback"); + } + let remoteCleanup: string | null = null; + if (issued && cleanupCredential && managementUrl) { + remoteCleanup = await cleanupIssuedKey(managementUrl, cleanupCredential, issued.id, deps); + } + const base = error instanceof Error ? error.message : String(error); + const details = [ + ...rollbackFailures, + ...(remoteCleanup ? [remoteCleanup] : []), + ]; + throw new Error(details.length > 0 ? `${base}. ${details.join(" ")}` : base, { cause: error }); + } finally { + releaseCredential(options.credential); + cleanupCredential = null; + issued = null; + if (!committed) { + tokenFingerprint = null; + priorCatalog = null; + writtenCatalogFingerprint = null; + } + } +} + +export async function syncConnectedClient( + _options: { restartCodex?: boolean } = {}, + deps: ClientConnectDeps = {}, +): Promise<{ catalogWritten: boolean; cacheSynced: boolean; injected: boolean; stale: boolean }> { + const state = readClientConnectionState(); + if (state.kind !== "connected") throw new Error(`connected sync refused: client state is ${state.kind}`); + const token = readServiceApiTokenState(); + if (token.kind !== "present" || token.fingerprint !== state.value.tokenFingerprint) { + throw new Error(token.kind === "absent" ? "connected service token is missing" : "connected service token ownership changed"); + } + + let catalogWritten = false; + let stale = false; + let next = state.value; + try { + const downloaded = await downloadClientCatalog(state.value.serverUrl, token.token, { + etag: state.value.catalogEtag, + fetchImpl: deps.fetchImpl, + }); + if (downloaded.kind === "fresh") { + atomicWriteFile(DEFAULT_CATALOG_PATH, downloaded.body); + catalogWritten = true; + const now = (deps.now ?? (() => new Date()))().toISOString(); + next = { + ...state.value, + ...(downloaded.etag ? { catalogEtag: downloaded.etag } : {}), + catalogSyncedAt: now, + }; + commitClientConnection(next); + } else { + validLocalCatalog(); + } + } catch (error) { + const transient = error instanceof HubClientError + && (error.code === "unreachable" || (error.status !== undefined && error.status >= 500)); + if (!transient) throw error; + validLocalCatalog(); + stale = true; + } + + let injected = false; + if (next.selectedClients.includes("codex")) { + const config = loadConfig(); + const result = await injectCodexConfig(config.port, { ...config, syncResumeHistory: false }, { + routingTarget: routingTarget(next.serverUrl), + catalogPath: DEFAULT_CATALOG_PATH, + journalOwner: { kind: "client", apiKeyId: next.apiKeyId }, + }); + if (!result.success || result.status === "skipped") throw new Error(result.message); + injected = true; + } + const cacheSynced = invalidateCodexModelsCache({ allowWhenDesiredDisabled: true }); + return { catalogWritten, cacheSynced, injected, stale }; +} + +function removeOwnedCatalog(connection: OcxClientConnectionConfig): "removed" | "absent" | "changed" { + if (!existsSync(DEFAULT_CATALOG_PATH)) return "absent"; + try { + const body = validLocalCatalog(); + if (!catalogMatchesEtag(body, connection.catalogEtag)) return "changed"; + unlinkSync(DEFAULT_CATALOG_PATH); + return "removed"; + } catch { + return "changed"; + } +} + +export async function disconnectClient( + options: { keepCatalog?: boolean } = {}, +): Promise<{ restored: boolean; tokenRemoved: boolean; catalogRemoved: boolean; apiKeyId: string }> { + const state = readClientConnectionState(); + if (state.kind !== "connected") throw new Error(`disconnect refused: client state is ${state.kind}`); + const token = readServiceApiTokenState(); + if (token.kind !== "present" || token.fingerprint !== state.value.tokenFingerprint) { + throw new Error(token.kind === "absent" ? "disconnect refused: service token is missing" : "disconnect refused: service token ownership changed"); + } + + let restored = true; + if (state.value.selectedClients.includes("codex")) { + const owner = journalOwner(); + if (owner?.kind === "client" && owner.apiKeyId === state.value.apiKeyId) { + restored = restoreJournalState().complete; + } else if (owner !== null || isCodexRoutingInjected()) { + throw new Error("disconnect refused: Codex journal ownership conflicts with the connected key"); + } + if (!restored) throw new Error("disconnect refused: Codex journal restore was partial"); + } + + const tokenRemoval = removeServiceApiTokenFileIfOwned(state.value.tokenFingerprint); + if (tokenRemoval === "changed") throw new Error("disconnect refused: service token changed before removal"); + let catalogRemoval: "removed" | "absent" | "changed" = "absent"; + if (!options.keepCatalog) { + catalogRemoval = removeOwnedCatalog(state.value); + if (catalogRemoval === "changed") throw new Error("disconnect refused: catalog ownership changed"); + } + if (clearClientConnection(state.value.apiKeyId) !== "committed") { + throw new Error("disconnect refused: client state changed before final commit"); + } + return { + restored, + tokenRemoved: tokenRemoval === "removed", + catalogRemoved: catalogRemoval === "removed", + apiKeyId: state.value.apiKeyId, + }; +} + +export async function revokeConnectedClientKey( + credential: { kind: "admin"; value: Uint8Array }, + deps: ClientConnectDeps = {}, +): Promise<{ apiKeyId: string }> { + try { + const state = readClientConnectionState(); + if (state.kind !== "connected") throw new Error("connect revoke is available only while connected"); + await revokeClientKey(state.value.managementUrl, credential, state.value.apiKeyId, { fetchImpl: deps.fetchImpl }); + return { apiKeyId: state.value.apiKeyId }; + } finally { + credential.value.fill(0); + } +} diff --git a/src/client/hub-client.ts b/src/client/hub-client.ts new file mode 100644 index 0000000000..af188280ff --- /dev/null +++ b/src/client/hub-client.ts @@ -0,0 +1,301 @@ +import { MAX_REMOTE_CATALOG_BYTES } from "../server/catalog-download"; +import { + checkRemoteProtocolCompatibility, + parseRemoteReadyMetadata, + type RemoteReadyMetadata, +} from "../remote/protocol"; + +const READY_BODY_LIMIT = 64 * 1024; +const MANAGEMENT_BODY_LIMIT = 128 * 1024; +const DEFAULT_TIMEOUT_MS = 5_000; + +export type OneTimeConnectCredential = + | { kind: "admin"; value: Uint8Array } + | { kind: "pairing-grant"; value: Uint8Array }; + +export interface ConnectGuiSession { + token: string; + csrfToken: string; + browserOrigin: string; + serverOrigin: string; +} + +export interface IssuedClientKey { + id: string; + key: string; + createdAt: string; + name: string; +} + +export class HubClientError extends Error { + constructor( + readonly code: string, + message: string, + readonly status?: number, + options?: ErrorOptions, + ) { + super(message, options); + this.name = "HubClientError"; + } +} + +function credentialString(value: Uint8Array): string { + const decoded = new TextDecoder("utf-8", { fatal: true }).decode(value).trim(); + if (!decoded || /[\r\n\0]/.test(decoded) || value.byteLength > 4096) { + throw new HubClientError("credential_invalid", "Connect credential is invalid"); + } + return decoded; +} + +function safeTimeout(timeoutMs: number | undefined): number { + return typeof timeoutMs === "number" && Number.isFinite(timeoutMs) && timeoutMs > 0 + ? Math.min(Math.floor(timeoutMs), 120_000) + : DEFAULT_TIMEOUT_MS; +} + +async function fetchBounded( + fetchImpl: typeof fetch, + url: string, + init: RequestInit, + timeoutMs: number | undefined, +): Promise { + try { + const response = await fetchImpl(url, { + ...init, + redirect: "manual", + signal: AbortSignal.timeout(safeTimeout(timeoutMs)), + }); + if (response.status >= 300 && response.status < 400) { + throw new HubClientError("redirect_refused", "Hub request redirect was refused", response.status); + } + return response; + } catch (error) { + if (error instanceof HubClientError) throw error; + throw new HubClientError("unreachable", "Hub request did not complete", undefined, { cause: error }); + } +} + +async function boundedText(response: Response, maxBytes: number): Promise { + const declared = Number(response.headers.get("content-length") ?? "0"); + if (Number.isFinite(declared) && declared > maxBytes) { + throw new HubClientError("body_too_large", "Hub response exceeded the allowed size", response.status); + } + const bytes = new Uint8Array(await response.arrayBuffer()); + if (bytes.byteLength > maxBytes) { + throw new HubClientError("body_too_large", "Hub response exceeded the allowed size", response.status); + } + try { + return new TextDecoder("utf-8", { fatal: true }).decode(bytes); + } catch (error) { + throw new HubClientError("body_invalid", "Hub response was not valid UTF-8", response.status, { cause: error }); + } +} + +function parseJson(text: string, code: string): unknown { + try { + return JSON.parse(text) as unknown; + } catch (error) { + throw new HubClientError(code, "Hub returned malformed JSON", undefined, { cause: error }); + } +} + +export function normalizeHubOrigin(input: string): string { + let parsed: URL; + try { + parsed = new URL(input); + } catch { + throw new HubClientError("url_invalid", "Hub URL must be an absolute HTTP(S) URL"); + } + if ( + (parsed.protocol !== "http:" && parsed.protocol !== "https:") + || parsed.username + || parsed.password + || parsed.search + || parsed.hash + || (parsed.pathname !== "/" && parsed.pathname !== "/v1" && parsed.pathname !== "/v1/") + ) { + throw new HubClientError( + "url_invalid", + "Hub URL must be an HTTP(S) origin without credentials, query, fragment, or non-/v1 path", + ); + } + return parsed.origin; +} + +export async function fetchHubReady( + serverUrl: string, + options: { timeoutMs?: number; fetchImpl?: typeof fetch } = {}, +): Promise<{ status: "ready" | "pending" | "failed"; metadata: RemoteReadyMetadata }> { + const origin = normalizeHubOrigin(serverUrl); + const response = await fetchBounded(options.fetchImpl ?? fetch, `${origin}/readyz`, { + method: "GET", + headers: { Accept: "application/json" }, + }, options.timeoutMs); + const body = parseJson(await boundedText(response, READY_BODY_LIMIT), "ready_invalid"); + if (!body || typeof body !== "object" || Array.isArray(body)) { + throw new HubClientError("ready_invalid", "Hub readiness response was invalid", response.status); + } + const raw = body as Record; + const status = raw.status; + if (status !== "ready" && status !== "pending" && status !== "failed") { + throw new HubClientError("ready_invalid", "Hub readiness status was invalid", response.status); + } + const metadata = parseRemoteReadyMetadata(raw); + const compatibility = checkRemoteProtocolCompatibility(raw); + if (!metadata || !compatibility.ok) { + throw new HubClientError( + compatibility.ok ? "ready_invalid" : compatibility.reason, + compatibility.ok ? "Hub readiness metadata was invalid" : compatibility.message, + response.status, + ); + } + if ((status === "ready" && response.status !== 200) || (status !== "ready" && response.status !== 503)) { + throw new HubClientError("ready_invalid", "Hub readiness HTTP status did not match its state", response.status); + } + return { status, metadata }; +} + +function htmlMeta(html: string, name: string): string | null { + const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + const match = new RegExp(`") + .replaceAll("&", "&") ?? null; +} + +export async function exchangeConnectPairingGrant( + managementUrl: string, + browserOrigin: string, + grant: Uint8Array, + options: { allowInsecureHttp?: boolean; timeoutMs?: number; fetchImpl?: typeof fetch } = {}, +): Promise { + const origin = normalizeHubOrigin(managementUrl); + const browser = normalizeHubOrigin(browserOrigin); + if (new URL(origin).protocol !== "https:" && options.allowInsecureHttp !== true) { + throw new HubClientError("insecure_http_refused", "Pairing over HTTP requires --allow-insecure-http"); + } + const response = await fetchBounded(options.fetchImpl ?? fetch, `${origin}/opencodex-session`, { + method: "POST", + headers: { "Content-Type": "application/json", Origin: browser, Accept: "text/html" }, + body: JSON.stringify({ grant: credentialString(grant) }), + }, options.timeoutMs); + if (!response.ok) throw new HubClientError("pairing_refused", "Hub pairing grant was refused", response.status); + const html = await boundedText(response, MANAGEMENT_BODY_LIMIT); + const session: ConnectGuiSession = { + token: htmlMeta(html, "opencodex-session-token") ?? "", + csrfToken: htmlMeta(html, "opencodex-session-csrf") ?? "", + browserOrigin: htmlMeta(html, "opencodex-session-origin") ?? "", + serverOrigin: htmlMeta(html, "opencodex-session-server-origin") ?? "", + }; + if (!session.token || !session.csrfToken || session.browserOrigin !== browser || session.serverOrigin !== origin) { + throw new HubClientError("pairing_invalid", "Hub pairing session response was invalid", response.status); + } + return session; +} + +function parseIssuedClientKey(value: unknown): IssuedClientKey | null { + if (!value || typeof value !== "object" || Array.isArray(value)) return null; + const raw = value as Record; + if ( + typeof raw.id !== "string" || !raw.id || raw.id.length > 256 + || typeof raw.name !== "string" || !raw.name || raw.name.length > 80 + || typeof raw.key !== "string" || !/^ocx_data_[0-9a-f]{40}$/.test(raw.key) + || typeof raw.createdAt !== "string" || Number.isNaN(Date.parse(raw.createdAt)) + ) return null; + return { id: raw.id, name: raw.name, key: raw.key, createdAt: raw.createdAt }; +} + +export async function issueClientKey( + managementUrl: string, + credential: + | { kind: "admin"; value: Uint8Array } + | { kind: "gui-session"; value: ConnectGuiSession }, + name: string, + options: { timeoutMs?: number; fetchImpl?: typeof fetch } = {}, +): Promise { + const origin = normalizeHubOrigin(managementUrl); + if (!name.trim() || name.length > 80 || /[\x00-\x1f\x7f]/.test(name)) { + throw new HubClientError("key_name_invalid", "Client key name is invalid"); + } + if (credential.kind === "admin" && new URL(origin).protocol !== "https:") { + throw new HubClientError("admin_http_refused", "Admin credentials may be sent only over HTTPS"); + } + const headers = new Headers({ "Content-Type": "application/json", Accept: "application/json" }); + if (credential.kind === "admin") { + headers.set("x-opencodex-api-key", credentialString(credential.value)); + } else { + headers.set("x-opencodex-api-key", credential.value.token); + headers.set("Origin", credential.value.browserOrigin); + headers.set("X-OpenCodex-GUI-Origin", credential.value.browserOrigin); + headers.set("X-OpenCodex-CSRF-Token", credential.value.csrfToken); + } + const response = await fetchBounded(options.fetchImpl ?? fetch, `${origin}/api/keys`, { + method: "POST", + headers, + body: JSON.stringify({ name: name.trim() }), + }, options.timeoutMs); + if (!response.ok) { + throw new HubClientError(`key_issue_http_${response.status}`, `Hub refused client key issuance (${response.status})`, response.status); + } + const issued = parseIssuedClientKey(parseJson( + await boundedText(response, MANAGEMENT_BODY_LIMIT), + "key_issue_invalid", + )); + if (!issued) throw new HubClientError("key_issue_invalid", "Hub returned an invalid client key response", response.status); + return issued; +} + +export async function revokeClientKey( + managementUrl: string, + credential: { kind: "admin"; value: Uint8Array } | { kind: "gui-session"; value: ConnectGuiSession }, + id: string, + options: { timeoutMs?: number; fetchImpl?: typeof fetch } = {}, +): Promise { + const origin = normalizeHubOrigin(managementUrl); + if (!id || id.length > 256) throw new HubClientError("key_id_invalid", "Client key id is invalid"); + if (credential.kind === "admin" && new URL(origin).protocol !== "https:") { + throw new HubClientError("admin_http_refused", "Admin credentials may be sent only over HTTPS"); + } + const headers = new Headers({ "Content-Type": "application/json", Accept: "application/json" }); + if (credential.kind === "admin") headers.set("x-opencodex-api-key", credentialString(credential.value)); + else { + headers.set("x-opencodex-api-key", credential.value.token); + headers.set("Origin", credential.value.browserOrigin); + headers.set("X-OpenCodex-GUI-Origin", credential.value.browserOrigin); + headers.set("X-OpenCodex-CSRF-Token", credential.value.csrfToken); + } + const response = await fetchBounded(options.fetchImpl ?? fetch, `${origin}/api/keys`, { + method: "DELETE", + headers, + body: JSON.stringify({ id }), + }, options.timeoutMs); + if (!response.ok) throw new HubClientError("key_revoke_failed", `Hub refused key revocation (${response.status})`, response.status); +} + +export async function downloadClientCatalog( + serverUrl: string, + admissionToken: string, + options: { etag?: string; timeoutMs?: number; maxBytes?: number; fetchImpl?: typeof fetch } = {}, +): Promise<{ kind: "fresh"; body: string; etag?: string } | { kind: "not-modified" }> { + const origin = normalizeHubOrigin(serverUrl); + const headers = new Headers({ Accept: "application/json", "x-opencodex-api-key": admissionToken }); + if (options.etag) headers.set("If-None-Match", options.etag); + const response = await fetchBounded(options.fetchImpl ?? fetch, `${origin}/v1/catalog`, { + method: "GET", + headers, + }, options.timeoutMs); + if (response.status === 304) return { kind: "not-modified" }; + if (!response.ok) { + const code = response.status === 401 ? "catalog_unauthorized" : `catalog_http_${response.status}`; + throw new HubClientError(code, `Hub catalog request failed (${response.status})`, response.status); + } + const body = await boundedText(response, options.maxBytes ?? MAX_REMOTE_CATALOG_BYTES); + const parsed = parseJson(body, "catalog_invalid"); + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) { + throw new HubClientError("catalog_invalid", "Hub catalog response was invalid", response.status); + } + const etag = response.headers.get("etag")?.trim() || undefined; + return { kind: "fresh", body, ...(etag ? { etag } : {}) }; +} From db95f8f22bb5d3b05a4133b25d4e7f0bda4c020c Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 02:51:08 +0900 Subject: [PATCH 035/172] test(connect): cover transaction and client routing --- tests/api-keys-routes.test.ts | 23 +++ tests/claude-cli.test.ts | 26 +++ tests/claude-gateway-cache.test.ts | 23 +++ tests/cli-dispatch.test.ts | 31 +++- tests/cli-help.test.ts | 14 ++ tests/cli-registry.test.ts | 10 ++ tests/cli-status-json.test.ts | 11 ++ tests/client-connect.test.ts | 264 +++++++++++++++++++++++++++++ 8 files changed, 401 insertions(+), 1 deletion(-) create mode 100644 tests/client-connect.test.ts diff --git a/tests/api-keys-routes.test.ts b/tests/api-keys-routes.test.ts index d628a4d430..267916701b 100644 --- a/tests/api-keys-routes.test.ts +++ b/tests/api-keys-routes.test.ts @@ -84,6 +84,29 @@ afterEach(() => { }); describe("POST /api/keys", () => { + test("a raw pairing grant cannot authorize the key route", async () => { + saveConfig({ + ...baseConfig(), + runtimeRole: "hub", + hub: { managementPublicOrigin: "https://hub.example.test" }, + }); + const server = startServer(0); + try { + const response = await fetch(new URL("/api/keys", server.url), { + method: "POST", + headers: { + "Content-Type": "application/json", + "x-opencodex-api-key": `ocx_pair_${"a".repeat(43)}`, + }, + body: JSON.stringify({ name: "forbidden" }), + }); + expect(response.status).toBe(401); + expect(loadConfig().apiKeys ?? []).toHaveLength(0); + } finally { + await server.stop(true); + } + }); + test("persists a key and returns the full secret exactly once", async () => { saveConfig(baseConfig()); const server = startServer(0); diff --git a/tests/claude-cli.test.ts b/tests/claude-cli.test.ts index f98b6ff80c..b38309ae44 100644 --- a/tests/claude-cli.test.ts +++ b/tests/claude-cli.test.ts @@ -26,6 +26,32 @@ const AUTH_PRESENT = { }; describe("ocx claude env assembly", () => { + test("connected target injects only the hub base and client admission token", () => { + const env = buildClaudeEnv(cfg(), { + baseUrl: "https://hub.example.test", + admissionToken: "ocx_data_connected", + }, {}, {}, AUTH_PRESENT); + expect(env.ANTHROPIC_BASE_URL).toBe("https://hub.example.test"); + expect(env.ANTHROPIC_AUTH_TOKEN).toBe("ocx_data_connected"); + expect(env.ANTHROPIC_API_KEY).toBeUndefined(); + expect(env.CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST).toBe("1"); + }); + + test("user-owned connected destination wins and cannot receive the hub token", () => { + const env = buildClaudeEnv(cfg(), { + baseUrl: "https://hub.example.test", + admissionToken: "ocx_data_connected", + }, { + ANTHROPIC_BASE_URL: "https://user-gateway.example.test", + ANTHROPIC_AUTH_TOKEN: "ocx_data_connected", + }, {}, { + ...AUTH_PRESENT, + preBunAnthropicSlots: ["ANTHROPIC_BASE_URL", "ANTHROPIC_AUTH_TOKEN"], + }); + expect(env.ANTHROPIC_BASE_URL).toBe("https://user-gateway.example.test"); + expect(env.ANTHROPIC_AUTH_TOKEN).toBeUndefined(); + }); + test("root skip-permissions bypass requires both the explicit flag and uid 0", () => { expect(shouldAllowRootSkipPermissions(["--dangerously-skip-permissions"], () => 0)).toBe(true); expect(shouldAllowRootSkipPermissions([], () => 0)).toBe(false); diff --git a/tests/claude-gateway-cache.test.ts b/tests/claude-gateway-cache.test.ts index 186d6dd550..481205cb14 100644 --- a/tests/claude-gateway-cache.test.ts +++ b/tests/claude-gateway-cache.test.ts @@ -87,6 +87,29 @@ describe("Claude Code gateway-model cache pre-write (devlog 260712 030)", () => } }); + test("connected refresh targets the hub models endpoint with only the client token", async () => { + const dir = tempDir(); + let requestedUrl = ""; + let admission = ""; + const path = await refreshGatewayModelCacheFromProxy({ + baseUrl: "https://hub.example.test", + admissionToken: "ocx_data_connected", + }, { + configDir: dir, + fetchImpl: async (input, init) => { + requestedUrl = String(input); + admission = new Headers(init?.headers).get("x-opencodex-api-key") ?? ""; + return new Response(JSON.stringify({ data: [{ id: "claude-ocx-hub-model" }] }), { + headers: { "content-type": "application/json" }, + }); + }, + }); + expect(requestedUrl).toBe("https://hub.example.test/v1/models?limit=1000&ids=cli"); + expect(admission).toBe("ocx_data_connected"); + const body = JSON.parse(readFileSync(path!, "utf8")); + expect(body.baseUrl).toBe("https://hub.example.test"); + }); + test("proxy refresh falls back to a configured admission key", async () => { const dir = tempDir(); const originalFetch = globalThis.fetch; diff --git a/tests/cli-dispatch.test.ts b/tests/cli-dispatch.test.ts index be2425bebd..069f63004a 100644 --- a/tests/cli-dispatch.test.ts +++ b/tests/cli-dispatch.test.ts @@ -3,7 +3,8 @@ import { CLI_COMMANDS } from "../src/cli/registry"; import { DISPATCH_ALIASES, DISPATCH_COMMANDS, dispatchCommand, resolveDispatchCommand } from "../src/cli/dispatch"; import type { CliDispatchDeps } from "../src/cli/dispatch"; import { runGuiCommand } from "../src/cli/gui"; -import { existsSync, readFileSync } from "node:fs"; +import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; import { join } from "node:path"; import { getConfigDir } from "../src/config"; import { getAccountSet, removeCredential, saveCredential } from "../src/oauth/store"; @@ -63,6 +64,34 @@ describe("CLI dispatch aliases", () => { }); describe("dispatchCommand exit codes", () => { + test("invalid client state refuses sync before local proxy discovery", async () => { + const home = mkdtempSync(join(tmpdir(), "ocx-dispatch-client-invalid-")); + const previous = process.env.OPENCODEX_HOME; + let discoveries = 0; + try { + process.env.OPENCODEX_HOME = home; + writeFileSync(join(home, "config.json"), JSON.stringify({ + port: 10100, + providers: {}, + defaultProvider: "openai", + runtimeRole: "client", + client: { apiKeyId: "half-present" }, + }), "utf8"); + const args = ["sync"]; + const deps = { + ...fakeDeps, + args, + findLiveProxy: async () => { discoveries += 1; return null; }, + }; + expect(await dispatchCommand({ kind: "command", command: "sync", args }, deps)).toBe(1); + expect(discoveries).toBe(0); + } finally { + if (previous === undefined) delete process.env.OPENCODEX_HOME; + else process.env.OPENCODEX_HOME = previous; + rmSync(home, { recursive: true, force: true }); + } + }); + test("returns 0 for help forms", async () => { expect(await dispatchCommand({ kind: "help", command: "help", args: ["help"] }, fakeDeps)).toBe(0); expect(await dispatchCommand({ kind: "help", command: "--help", args: ["--help"] }, fakeDeps)).toBe(0); diff --git a/tests/cli-help.test.ts b/tests/cli-help.test.ts index ef0a475116..4b13c3ec5d 100644 --- a/tests/cli-help.test.ts +++ b/tests/cli-help.test.ts @@ -138,6 +138,20 @@ describe("CLI subcommand help", () => { expect(result.stdout).toContain("must not be persisted"); }); + test("connect help exposes stdin-only credentials and offline disconnect", () => { + const connect = runCli(["help", "connect"]); + expectSpawnFinished(connect, "ocx help connect"); + expect(connect.status).toBe(0); + expect(connect.stdout).toContain("--pairing-code-stdin"); + expect(connect.stdout).toContain("--admin-token-stdin"); + expect(connect.stdout).not.toContain("--admin-token <"); + + const disconnect = runCli(["help", "disconnect"]); + expectSpawnFinished(disconnect, "ocx help disconnect"); + expect(disconnect.status).toBe(0); + expect(disconnect.stdout).toContain("--keep-catalog"); + }); + test("unknown command with help flag remains an error", () => { const result = runCli(["foobar", "--help"]); expectSpawnFinished(result, "ocx foobar --help"); diff --git a/tests/cli-registry.test.ts b/tests/cli-registry.test.ts index 7bbb1d5101..ccfdf87404 100644 --- a/tests/cli-registry.test.ts +++ b/tests/cli-registry.test.ts @@ -111,6 +111,16 @@ describe("CLI command registry parity", () => { expect(gui?.details?.join(" ")).toContain("single-use"); expect(gui?.details?.join(" ")).toContain("no localhost or config-derived default"); }); + + test("connect and disconnect are registry-owned without credential argv forms", () => { + const connect = findCommand("connect"); + expect(connect?.usage).toContain("--pairing-code-stdin"); + expect(connect?.usage).toContain("--admin-token-stdin"); + expect(connect?.usage).not.toContain("--token <"); + expect(connect?.usage).not.toContain("--admin-token <"); + expect(connect?.details?.join(" ")).toContain("not supported"); + expect(findCommand("disconnect")?.usage).toBe("ocx disconnect [--keep-catalog] [--json]"); + }); }); describe("help banner command coverage", () => { diff --git a/tests/cli-status-json.test.ts b/tests/cli-status-json.test.ts index d8a2ea445f..bbccac72cb 100644 --- a/tests/cli-status-json.test.ts +++ b/tests/cli-status-json.test.ts @@ -69,6 +69,13 @@ describe("CLI status JSON", () => { }; defaultProvider?: unknown; config?: { source?: unknown; error?: unknown }; + connection?: { + state?: unknown; + serverUrl?: unknown; + apiKeyId?: unknown; + credentialFile?: unknown; + catalog?: unknown; + }; service?: { summary?: unknown }; codexShim?: { summary?: unknown }; codexRuntime?: { @@ -131,6 +138,10 @@ describe("CLI status JSON", () => { expect(typeof parsed.codexHome?.appCodexHome).toBe("string"); expect(typeof parsed.codexHome?.mismatch).toBe("boolean"); expect(parsed.codexHome?.warning === null || typeof parsed.codexHome?.warning === "string").toBe(true); + expect(parsed.connection).toMatchObject({ + state: "disconnected", + credentialFile: "missing", + }); const serialized = JSON.stringify(parsed).toLowerCase(); for (const forbidden of ["apikey", "sk-test-secret", "token", "refreshtoken", "authorization", "email"]) { diff --git a/tests/client-connect.test.ts b/tests/client-connect.test.ts new file mode 100644 index 0000000000..6fa83f37d3 --- /dev/null +++ b/tests/client-connect.test.ts @@ -0,0 +1,264 @@ +import { describe, expect, spyOn, test } from "bun:test"; +import { createHash } from "node:crypto"; +import { spawnSync } from "node:child_process"; +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { + downloadClientCatalog, + exchangeConnectPairingGrant, + fetchHubReady, + issueClientKey, + normalizeHubOrigin, +} from "../src/client/hub-client"; +import { handleConnectCommand } from "../src/cli/connect"; + +const repoRoot = dirname(fileURLToPath(new URL("../package.json", import.meta.url))); + +function readyBody(protocol = 1, minimumClientProtocol = 1) { + return { + service: "opencodex", + version: "0.0.0", + uptime: 1, + pid: 1, + port: 443, + status: "ready", + protocol, + minimumClientProtocol, + managementUrl: "https://manage.example.test", + }; +} + +describe("remote hub client boundary", () => { + test("canonicalizes origin and terminal /v1 only", () => { + expect(normalizeHubOrigin("https://hub.example.test/v1")).toBe("https://hub.example.test"); + expect(normalizeHubOrigin("https://hub.example.test/v1/")).toBe("https://hub.example.test"); + for (const value of [ + "ftp://hub.example.test", + "https://user@hub.example.test", + "https://hub.example.test/private", + "https://hub.example.test/?secret=1", + "https://hub.example.test/#secret", + ]) expect(() => normalizeHubOrigin(value)).toThrow(); + }); + + test("uses Phase-1 readiness compatibility including p2/min1 and rejects p2/min2", async () => { + const accepted = await fetchHubReady("https://hub.example.test", { + fetchImpl: async () => Response.json(readyBody(2, 1)), + }); + expect(accepted.metadata.protocol).toBe(2); + + await expect(fetchHubReady("https://hub.example.test", { + fetchImpl: async () => Response.json(readyBody(2, 2)), + })).rejects.toThrow("requires remote protocol 2"); + for (const status of ["pending", "failed"] as const) { + const result = await fetchHubReady("https://hub.example.test", { + fetchImpl: async () => Response.json({ ...readyBody(), status }, { status: 503 }), + }); + expect(result.status).toBe(status); + } + }); + + test("admin key issuance is HTTPS-only and pairing exchanges into a full GUI session", async () => { + let calls = 0; + await expect(issueClientKey("http://hub.example.test", { + kind: "admin", + value: new TextEncoder().encode("ocx_admin_secret"), + }, "client", { + fetchImpl: async () => { calls += 1; return new Response(); }, + })).rejects.toThrow("only over HTTPS"); + expect(calls).toBe(0); + + const browserOrigin = "http://localhost:10100"; + const sessionHtml = [ + '', + '', + ``, + '', + ].join(""); + const seen: Array<{ url: string; headers: Headers; body: string }> = []; + const fetchImpl: typeof fetch = async (input, init) => { + seen.push({ url: String(input), headers: new Headers(init?.headers), body: String(init?.body ?? "") }); + if (String(input).endsWith("/opencodex-session")) return new Response(sessionHtml); + return Response.json({ + id: "issued-id", + name: "client", + key: `ocx_data_${"a".repeat(40)}`, + createdAt: "2026-08-28T00:00:00.000Z", + }, { status: 201 }); + }; + const grant = new TextEncoder().encode(`ocx_pair_${"b".repeat(43)}`); + const session = await exchangeConnectPairingGrant( + "https://hub.example.test", + browserOrigin, + grant, + { fetchImpl }, + ); + const issued = await issueClientKey("https://hub.example.test", { kind: "gui-session", value: session }, "client", { fetchImpl }); + expect(issued.id).toBe("issued-id"); + expect(seen[0]?.headers.get("origin")).toBe(browserOrigin); + expect(seen[1]?.headers.get("x-opencodex-gui-origin")).toBe(browserOrigin); + expect(seen[1]?.headers.get("x-opencodex-csrf-token")).toBe("csrf-test"); + expect(seen[1]?.body).toBe(JSON.stringify({ name: "client" })); + }); + + test("pairing HTTP requires explicit client opt-in and catalog is bounded/conditional", async () => { + let calls = 0; + await expect(exchangeConnectPairingGrant( + "http://hub.example.test", + "http://localhost:10100", + new TextEncoder().encode(`ocx_pair_${"c".repeat(43)}`), + { fetchImpl: async () => { calls += 1; return new Response(); } }, + )).rejects.toThrow("--allow-insecure-http"); + expect(calls).toBe(0); + + const notModified = await downloadClientCatalog("https://hub.example.test", "ocx_data_test", { + etag: '"etag"', + fetchImpl: async (_input, init) => { + expect(new Headers(init?.headers).get("if-none-match")).toBe('"etag"'); + return new Response(null, { status: 304 }); + }, + }); + expect(notModified).toEqual({ kind: "not-modified" }); + await expect(downloadClientCatalog("https://hub.example.test", "ocx_data_test", { + maxBytes: 4, + fetchImpl: async () => new Response('{"models":[]}'), + })).rejects.toThrow("allowed size"); + }); + + test("CLI rejects literal/env credential forms without rendering their values", async () => { + const errors: string[] = []; + const spy = spyOn(console, "error").mockImplementation(value => { errors.push(String(value)); }); + try { + expect(await handleConnectCommand([ + "https://hub.example.test", + "--admin-token-stdin", + "--admin-token=super-secret-value", + ])).toBe(2); + expect(errors.join(" ")).not.toContain("super-secret-value"); + expect(errors.join(" ")).toContain(""); + } finally { + spy.mockRestore(); + } + }); +}); + +function runTransactionScenario(stage: "success" | "catalog" | "preflight" | "commit") { + const opencodexHome = mkdtempSync(join(tmpdir(), "ocx-client-connect-home-")); + const codexHome = mkdtempSync(join(tmpdir(), "ocx-client-connect-codex-")); + const configPath = join(opencodexHome, "config.json"); + const originalConfig = { + port: 10100, + providers: { openai: { adapter: "openai-responses", baseUrl: "https://chatgpt.com/backend-api/codex", authMode: "forward" } }, + defaultProvider: "openai", + }; + writeFileSync(configPath, `${JSON.stringify(originalConfig, null, 2)}\n`, "utf8"); + if (stage !== "preflight") writeFileSync(join(codexHome, "config.toml"), 'model_provider = "openai"\n', "utf8"); + if (stage === "commit") { + const { mkdirSync } = require("node:fs") as typeof import("node:fs"); + mkdirSync(join(opencodexHome, "config-mutation.sqlite")); + } + const script = ` + const { existsSync, readFileSync } = require("node:fs"); + const { createHash } = require("node:crypto"); + const { connectClient, disconnectClient } = require("./src/client/connect"); + const { readClientConnectionState } = require("./src/client/state"); + const { serviceApiTokenFilePath } = require("./src/lib/service-secrets"); + const { DEFAULT_CATALOG_PATH } = require("./src/codex/paths"); + const stage = ${JSON.stringify(stage)}; + const catalog = '{"models":[]}'; + const etag = '"sha256-' + createHash("sha256").update(catalog).digest("base64url") + '"'; + const calls = []; + const credential = new TextEncoder().encode("ocx_admin_test-authority"); + const fetchImpl = async (input, init = {}) => { + const url = String(input); + calls.push({ url, method: init.method || "GET" }); + if (url.endsWith("/readyz")) return Response.json(${JSON.stringify(readyBody())}); + if (url.endsWith("/api/keys") && init.method === "POST") return Response.json({ + id: "issued-id", + name: "client", + key: "ocx_data_${"d".repeat(40)}", + createdAt: "2026-08-28T00:00:00.000Z", + }, { status: 201 }); + if (url.endsWith("/api/keys") && init.method === "DELETE") return Response.json({ success: true }); + if (url.endsWith("/v1/catalog")) { + if (stage === "catalog") return Response.json({ error: "down" }, { status: 503 }); + return new Response(catalog, { headers: { ETag: etag, "Content-Type": "application/json" } }); + } + throw new Error("unexpected request " + url); + }; + (async () => { + let connected = null; + let error = null; + try { + connected = await connectClient({ + serverUrl: "https://hub.example.test", + credential: { kind: "admin", value: credential }, + selectedClients: ["claude"], + managementTransport: "direct", + noSync: true, + }, { fetchImpl, now: () => new Date("2026-08-28T00:00:00.000Z") }); + } catch (cause) { error = cause instanceof Error ? cause.message : String(cause); } + const beforeDisconnect = readClientConnectionState(); + const artifacts = { + token: existsSync(serviceApiTokenFilePath()), + catalog: existsSync(DEFAULT_CATALOG_PATH), + credentialZeroed: credential.every(value => value === 0), + }; + let disconnected = null; + if (stage === "success" && connected) disconnected = await disconnectClient(); + console.log(JSON.stringify({ connected, error, beforeDisconnect, artifacts, disconnected, after: readClientConnectionState(), calls })); + })(); + `; + const result = spawnSync(process.execPath, ["--eval", script], { + cwd: repoRoot, + env: { ...process.env, OPENCODEX_HOME: opencodexHome, CODEX_HOME: codexHome }, + encoding: "utf8", + }); + const output = result.stdout.trim().split("\n").at(-1) ?? "{}"; + const parsed = JSON.parse(output) as Record; + return { + status: result.status, + stderr: result.stderr, + parsed, + configBytes: readFileSync(configPath, "utf8"), + cleanup: () => { + rmSync(opencodexHome, { recursive: true, force: true }); + rmSync(codexHome, { recursive: true, force: true }); + }, + }; +} + +describe("connect transaction and offline disconnect", () => { + test("commits key id/state last, zeroes authority, and disconnects with the hub offline", () => { + const run = runTransactionScenario("success"); + try { + expect(run.status).toBe(0); + expect(run.parsed.error).toBeNull(); + expect(run.parsed.connected.apiKeyId).toBe("issued-id"); + expect(run.parsed.beforeDisconnect).toMatchObject({ kind: "connected", value: { apiKeyId: "issued-id" } }); + expect(run.parsed.artifacts).toEqual({ token: true, catalog: true, credentialZeroed: true }); + expect(run.parsed.disconnected).toMatchObject({ apiKeyId: "issued-id", tokenRemoved: true, catalogRemoved: true }); + expect(run.parsed.after).toEqual({ kind: "disconnected" }); + expect(run.parsed.calls.filter((call: any) => call.method === "DELETE")).toEqual([]); + } finally { run.cleanup(); } + }); + + for (const stage of ["catalog", "preflight", "commit"] as const) { + test(`rolls back local artifacts when ${stage} fails before final commit`, () => { + const run = runTransactionScenario(stage); + try { + expect(run.status).toBe(0); + expect(run.parsed.connected).toBeNull(); + expect(run.parsed.beforeDisconnect).toEqual({ kind: "disconnected" }); + expect(run.parsed.artifacts.token).toBe(false); + expect(run.parsed.artifacts.catalog).toBe(false); + expect(run.parsed.artifacts.credentialZeroed).toBe(true); + expect(run.parsed.calls.some((call: any) => call.method === "DELETE")).toBe(true); + expect(run.configBytes).not.toContain("issued-id"); + expect(`${run.parsed.error} ${run.stderr}`).not.toContain(`ocx_data_${"d".repeat(40)}`); + } finally { run.cleanup(); } + }); + } +}); From 9d46ca30c3eadd013d482477057d0df51831fdd9 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 02:52:28 +0900 Subject: [PATCH 036/172] fix(connect): narrow client type boundaries --- src/cli/claude.ts | 6 ++++-- src/client/state.ts | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/cli/claude.ts b/src/cli/claude.ts index fa84f1813f..fe29889756 100644 --- a/src/cli/claude.ts +++ b/src/cli/claude.ts @@ -333,10 +333,12 @@ export function readConnectedClaudeContextWindows(path = DEFAULT_CATALOG_PATH): if (slash > 0 && slash < slug.length - 1) { const provider = slug.slice(0, slash); const id = slug.slice(slash + 1); - put(aliasForRoute(provider, id), contextWindow); + const routeAlias = aliasForRoute(provider, id); + if (routeAlias) put(routeAlias, contextWindow); put(desktop3pAlias(provider, id), contextWindow); } else { - put(aliasForNative(slug), contextWindow); + const nativeAlias = aliasForNative(slug); + if (nativeAlias) put(nativeAlias, contextWindow); put(desktop3pAlias("native", slug), contextWindow); } } diff --git a/src/client/state.ts b/src/client/state.ts index 97e41f39c6..8710729cb8 100644 --- a/src/client/state.ts +++ b/src/client/state.ts @@ -69,7 +69,7 @@ export function commitClientConnection( return { changed: !unchanged, value: undefined }; }); if (outcome.status === "committed" || outcome.status === "unchanged") return outcome.status; - throw new Error(`client state commit unavailable: ${outcome.reason}`); + throw new Error(`client state commit unavailable: ${"reason" in outcome ? outcome.reason : "unknown"}`); } export function clearClientConnection( From 6fdaaf810c64fea6d2c29701dfa5a7f009b4f63d Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 02:57:43 +0900 Subject: [PATCH 037/172] fix(connect): preserve catalog conditional fetch --- src/client/hub-client.ts | 2 +- tests/config.test.ts | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/client/hub-client.ts b/src/client/hub-client.ts index af188280ff..b60126476f 100644 --- a/src/client/hub-client.ts +++ b/src/client/hub-client.ts @@ -65,7 +65,7 @@ async function fetchBounded( redirect: "manual", signal: AbortSignal.timeout(safeTimeout(timeoutMs)), }); - if (response.status >= 300 && response.status < 400) { + if (response.status >= 300 && response.status < 400 && response.status !== 304) { throw new HubClientError("redirect_refused", "Hub request redirect was refused", response.status); } return response; diff --git a/tests/config.test.ts b/tests/config.test.ts index 46417e52d4..56f325236e 100644 --- a/tests/config.test.ts +++ b/tests/config.test.ts @@ -126,13 +126,17 @@ describe("opencodex config defaults", () => { expect(readFileSync(getConfigPath(), "utf8")).toBe(before); }); - test("runtime role accepts the three explicit contract values", () => { - for (const role of ["standalone", "hub", "client"] as const) { + test("runtime role accepts standalone/hub alone while client requires atomic state", () => { + for (const role of ["standalone", "hub"] as const) { expect(validateConfigCandidate({ ...getDefaultConfig(), runtimeRole: role })).toMatchObject({ ok: true, config: { runtimeRole: role }, }); } + expect(validateConfigCandidate({ ...getDefaultConfig(), runtimeRole: "client" })).toMatchObject({ + ok: false, + error: expect.stringContaining("requires a complete client connection"), + }); }); test("runtime role rejects malformed live candidates", () => { @@ -260,7 +264,7 @@ describe("opencodex config defaults", () => { }); test("remote GUI config round-trips but remains inert outside the hub role", () => { - for (const runtimeRole of [undefined, "standalone", "client"] as const) { + for (const runtimeRole of [undefined, "standalone"] as const) { const result = validateConfigCandidate({ ...getDefaultConfig(), ...(runtimeRole ? { runtimeRole } : {}), From abf0f81bd43184b7a42e54d8617c4c6c0b23810b Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 02:11:32 +0900 Subject: [PATCH 038/172] feat(remote-gui): add remote session issuance and pairing --- gui/src/api.ts | 60 +++++-- src/cli/dispatch.ts | 49 ++--- src/cli/gui-pair-client.ts | 166 +++++++++++++++++ src/cli/gui.ts | 87 +++++++++ src/cli/help.ts | 3 +- src/cli/registry.ts | 10 +- src/config.ts | 94 ++++++++++ src/lib/gui-pair-capability.ts | 104 +++++++++++ src/remote/protocol.ts | 8 +- src/server/auth-cors.ts | 25 ++- src/server/gui-session.ts | 318 +++++++++++++++++++++++++++++++++ src/server/gui-static.ts | 5 +- src/server/index.ts | 74 +++++++- src/server/management-auth.ts | 186 ++++++++++--------- src/server/proxy-liveness.ts | 1 + src/types.ts | 2 + src/types/config.ts | 16 ++ 17 files changed, 1078 insertions(+), 130 deletions(-) create mode 100644 src/cli/gui-pair-client.ts create mode 100644 src/cli/gui.ts create mode 100644 src/lib/gui-pair-capability.ts create mode 100644 src/server/gui-session.ts diff --git a/gui/src/api.ts b/gui/src/api.ts index 658beac1ff..89b8a33d6b 100644 --- a/gui/src/api.ts +++ b/gui/src/api.ts @@ -53,8 +53,12 @@ function needsApiAuth(input: RequestInfo | URL): boolean { try { const raw = input instanceof Request ? input.url : String(input); const url = new URL(raw, window.location.href); - // Absolute cross-origin URLs must never get the local API token or 401 prompt. - if (url.origin !== window.location.origin) return false; + const admittedOrigin = memoryToken?.startsWith("ocx_session_") + ? memorySessionServerOrigin + : window.location.origin; + // A session is destination-bound. Third-party origins get neither credentials + // nor the local admin-token prompt. + if (!admittedOrigin || url.origin !== admittedOrigin) return false; return url.pathname.startsWith("/api/"); } catch { return false; @@ -67,7 +71,8 @@ const LEGACY_TOKEN_KEY = "opencodex-api-token"; /** In-memory only — never write tokens to web storage (XSS can read sessionStorage/localStorage). */ let memoryToken: string | null = null; let memoryCsrfToken: string | null = null; -let memorySessionOrigin: string | null = null; +let memorySessionBrowserOrigin: string | null = null; +let memorySessionServerOrigin: string | null = null; function readToken(): string | null { return memoryToken; @@ -80,7 +85,8 @@ function storeToken(token: string): void { function clearToken(): void { memoryToken = null; memoryCsrfToken = null; - memorySessionOrigin = null; + memorySessionBrowserOrigin = null; + memorySessionServerOrigin = null; } function takeMetaContent(name: string): string | null { @@ -93,8 +99,9 @@ function takeMetaContent(name: string): string | null { function loadInjectedSession(): void { const token = takeMetaContent("opencodex-session-token"); const csrfToken = takeMetaContent("opencodex-session-csrf"); - const origin = takeMetaContent("opencodex-session-origin"); - storeSession(token, csrfToken, origin); + const browserOrigin = takeMetaContent("opencodex-session-origin"); + const serverOrigin = takeMetaContent("opencodex-session-server-origin"); + storeSession(token, csrfToken, browserOrigin, serverOrigin, window.location.origin); } /** Clear memory only when it still holds `expected` (avoid wiping a newer concurrent store). */ @@ -103,11 +110,26 @@ function clearTokenIfCurrent(expected: string | null): void { } /** Validate and store a server-minted GUI session; rejects anything bound to another origin. */ -function storeSession(token: string | null, csrfToken: string | null, origin: string | null): boolean { - if (!token?.startsWith("ocx_session_") || !csrfToken || origin !== window.location.origin) return false; +function storeSession( + token: string | null, + csrfToken: string | null, + browserOrigin: string | null, + serverOrigin: string | null, + expectedServerOrigin: string, +): boolean { + if ( + !token?.startsWith("ocx_session_") + || !csrfToken + || browserOrigin !== window.location.origin + || serverOrigin !== expectedServerOrigin + ) { + clearToken(); + return false; + } memoryToken = token; memoryCsrfToken = csrfToken; - memorySessionOrigin = origin; + memorySessionBrowserOrigin = browserOrigin; + memorySessionServerOrigin = serverOrigin; return true; } @@ -150,10 +172,19 @@ async function reBootstrapSessionToken(): Promise { return response.status >= 400 && response.status < 500 ? { kind: "unavailable" } : { kind: "failed" }; } const html = await response.text(); + let responseOrigin: string; + try { + responseOrigin = new URL(response.url || SESSION_REBOOTSTRAP_PATH, window.location.href).origin; + } catch { + clearToken(); + return { kind: "unavailable" }; + } const stored = storeSession( metaContentFromHtml(html, "opencodex-session-token"), metaContentFromHtml(html, "opencodex-session-csrf"), metaContentFromHtml(html, "opencodex-session-origin"), + metaContentFromHtml(html, "opencodex-session-server-origin"), + responseOrigin, ); const token = readToken(); if (stored && token) return { kind: "minted", token }; @@ -188,8 +219,12 @@ function clearLegacySessionToken(): void { function withToken(input: RequestInfo | URL, init: RequestInit | undefined, token: string): [RequestInfo | URL, RequestInit | undefined] { const headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined)); headers.set("X-OpenCodex-API-Key", token); - if (memorySessionOrigin && memoryCsrfToken && token.startsWith("ocx_session_")) { - headers.set("X-OpenCodex-GUI-Origin", memorySessionOrigin); + if (memorySessionBrowserOrigin && memorySessionServerOrigin && memoryCsrfToken && token.startsWith("ocx_session_")) { + const raw = input instanceof Request ? input.url : String(input); + let destinationOrigin: string | null = null; + try { destinationOrigin = new URL(raw, window.location.href).origin; } catch { /* leave null */ } + if (destinationOrigin !== memorySessionServerOrigin) return [input, init]; + headers.set("X-OpenCodex-GUI-Origin", memorySessionBrowserOrigin); const method = (init?.method ?? (input instanceof Request ? input.method : "GET")).toUpperCase(); if (method !== "GET" && method !== "HEAD") { headers.set("X-OpenCodex-CSRF-Token", memoryCsrfToken); @@ -305,7 +340,8 @@ export function resetApiAuthFetchForTests(adminTokenPrompt: AdminTokenPrompt = p installed = false; memoryToken = null; memoryCsrfToken = null; - memorySessionOrigin = null; + memorySessionBrowserOrigin = null; + memorySessionServerOrigin = null; resolutionInFlight = null; rawFetch = null; promptCancelled = false; diff --git a/src/cli/dispatch.ts b/src/cli/dispatch.ts index 3edd53ffb0..8e09cc29e8 100644 --- a/src/cli/dispatch.ts +++ b/src/cli/dispatch.ts @@ -451,27 +451,34 @@ const commandRunners: Record = { return ok ? 0 : 1; }, gui: async deps => { - const config = deps.loadConfig(); - // Identity-checked liveness (not the pid file + a fixed sleep): finds a fallback-port - // proxy and waits until the spawned one actually answers before opening the browser. - let live = await deps.findLiveProxy(); - if (!live) { - console.log("Proxy not running. Starting..."); - deps.spawnDetached(deps.startArgv((config.port ?? 10100) > 0 ? (config.port ?? 10100) : undefined)); - live = await deps.waitForProxy(); - if (!live) { - console.error("❌ Proxy did not become healthy after starting. Not opening the GUI."); - return 1; - } - } - // Open the host the proxy actually binds — `localhost` only answers for - // loopback/wildcard binds, not a concrete LAN/IPv6 hostname. - const guiHost = deps.probeHostname(live?.hostname ?? config.hostname); - const guiUrl = `http://${guiHost === "127.0.0.1" ? "localhost" : guiHost}:${live?.port ?? config.port}`; - console.log(`Opening ${guiUrl}`); - const { openUrl } = await import("../lib/open-url"); - openUrl(guiUrl); - return 0; + const { runGuiCommand } = await import("./gui"); + return runGuiCommand(deps.args.slice(1), { + loadConfig: deps.loadConfig, + findLiveProxy: deps.findLiveProxy, + openDefaultGui: async () => { + const config = deps.loadConfig(); + // Identity-checked liveness (not the pid file + a fixed sleep): finds a fallback-port + // proxy and waits until the spawned one actually answers before opening the browser. + let live = await deps.findLiveProxy(); + if (!live) { + console.log("Proxy not running. Starting..."); + deps.spawnDetached(deps.startArgv((config.port ?? 10100) > 0 ? (config.port ?? 10100) : undefined)); + live = await deps.waitForProxy(); + if (!live) { + console.error("❌ Proxy did not become healthy after starting. Not opening the GUI."); + return 1; + } + } + // Open the host the proxy actually binds — `localhost` only answers for + // loopback/wildcard binds, not a concrete LAN/IPv6 hostname. + const guiHost = deps.probeHostname(live?.hostname ?? config.hostname); + const guiUrl = `http://${guiHost === "127.0.0.1" ? "localhost" : guiHost}:${live?.port ?? config.port}`; + console.log(`Opening ${guiUrl}`); + const { openUrl } = await import("../lib/open-url"); + openUrl(guiUrl); + return 0; + }, + }); }, service: async deps => { process.exitCode = 0; diff --git a/src/cli/gui-pair-client.ts b/src/cli/gui-pair-client.ts new file mode 100644 index 0000000000..ab00b14e79 --- /dev/null +++ b/src/cli/gui-pair-client.ts @@ -0,0 +1,166 @@ +import { readRuntimePort, type RuntimePortState } from "../config/process-state"; +import { + LOCAL_ATTESTATION_CHALLENGE_HEADER, + LOCAL_ATTESTATION_PROOF_HEADER, + createLocalAttestationChallenge, + verifyLocalAttestationProof, +} from "../lib/local-management-attestation"; +import { + GUI_PAIR_BROWSER_ORIGIN_HEADER, + GUI_PAIR_CAPABILITY_HEADER, + GUI_PAIR_CAPABILITY_TTL_MS, + GUI_PAIR_CAPABILITY_VERSION, + GUI_PAIR_EXPECTED_PID_HEADER, + GUI_PAIR_EXPIRES_AT_HEADER, + GUI_PAIR_METHOD, + GUI_PAIR_NONCE_HEADER, + GUI_PAIR_PATH, + canonicalGuiBrowserOrigin, + createGuiPairCapability, +} from "../lib/gui-pair-capability"; +import { directLocalHttpFetch } from "../server/direct-local-http"; +import { + isOpencodexHealthz, + probeHostname, + type HealthzIdentity, + type LiveProxy, +} from "../server/proxy-liveness"; + +export type GuiPairRequestResult = + | { kind: "created"; grant: string; browserOrigin: string; serverOrigin: string; expiresAt: number } + | { kind: "unavailable"; reason: "unattested-target" | "runtime-mismatch" | "attestation" | "capability" | "transport" | "rejected" }; + +export interface GuiPairClientDeps { + fetchImpl?: typeof fetch; + readRuntime?: (pid: number) => RuntimePortState | null; + createChallenge?: () => string; + now?: () => number; + timeoutMs?: number; +} + +const GUI_PAIR_REQUEST_TIMEOUT_MS = 10_000; + +function sameRuntime(left: RuntimePortState, right: RuntimePortState | null): boolean { + return !!right?.attestationSecret + && right.pid === left.pid + && right.port === left.port + && right.hostname === left.hostname + && right.attestationSecret === left.attestationSecret; +} + +function canonicalHttpOrigin(value: unknown): string | null { + if (typeof value !== "string") return null; + try { + const parsed = new URL(value); + if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return null; + if (parsed.username || parsed.password || parsed.pathname !== "/" || parsed.search || parsed.hash) return null; + return parsed.origin; + } catch { + return null; + } +} + +function parseCreatedResult(value: unknown, browserOrigin: string): GuiPairRequestResult | null { + if (!value || typeof value !== "object" || Array.isArray(value)) return null; + const record = value as Record; + if ( + typeof record.grant !== "string" + || !/^ocx_pair_[A-Za-z0-9_-]{43}$/.test(record.grant) + || canonicalGuiBrowserOrigin(record.browserOrigin) !== browserOrigin + || typeof record.expiresAt !== "number" + || !Number.isSafeInteger(record.expiresAt) + ) return null; + const serverOrigin = canonicalHttpOrigin(record.serverOrigin); + if (!serverOrigin) return null; + return { + kind: "created", + grant: record.grant, + browserOrigin, + serverOrigin, + expiresAt: record.expiresAt, + }; +} + +export async function requestBoundGuiPairingGrant( + target: LiveProxy, + browserOrigin: string, + deps: GuiPairClientDeps = {}, +): Promise { + if (target.source !== "runtime" || target.pid === null || target.pid <= 0) { + return { kind: "unavailable", reason: "unattested-target" }; + } + const canonicalOrigin = canonicalGuiBrowserOrigin(browserOrigin); + if (!canonicalOrigin || canonicalOrigin !== browserOrigin) { + return { kind: "unavailable", reason: "capability" }; + } + const readRuntime = deps.readRuntime ?? readRuntimePort; + const runtime = readRuntime(target.pid); + if (!runtime?.attestationSecret || runtime.pid !== target.pid || runtime.port !== target.port) { + return { kind: "unavailable", reason: "runtime-mismatch" }; + } + const fetchImpl = deps.fetchImpl ?? directLocalHttpFetch; + const timeoutMs = deps.timeoutMs ?? GUI_PAIR_REQUEST_TIMEOUT_MS; + const challenge = (deps.createChallenge ?? createLocalAttestationChallenge)(); + const baseUrl = `http://${probeHostname(target.hostname)}:${target.port}`; + let proofResponse: Response; + try { + proofResponse = await fetchImpl(`${baseUrl}/healthz`, { + headers: { [LOCAL_ATTESTATION_CHALLENGE_HEADER]: challenge }, + signal: AbortSignal.timeout(timeoutMs), + }); + } catch { + return { kind: "unavailable", reason: "transport" }; + } + const body = await proofResponse.json().catch(() => null) as HealthzIdentity | null; + if ( + !proofResponse.ok + || !isOpencodexHealthz(body) + || body?.pid !== target.pid + || body?.port !== target.port + || !verifyLocalAttestationProof( + runtime.attestationSecret, + challenge, + target.pid, + target.port, + proofResponse.headers.get(LOCAL_ATTESTATION_PROOF_HEADER), + ) + ) return { kind: "unavailable", reason: "attestation" }; + if (body.guiPairCapability !== GUI_PAIR_CAPABILITY_VERSION) { + return { kind: "unavailable", reason: "capability" }; + } + if (!sameRuntime(runtime, readRuntime(target.pid))) { + return { kind: "unavailable", reason: "runtime-mismatch" }; + } + const expiresAt = (deps.now ?? Date.now)() + GUI_PAIR_CAPABILITY_TTL_MS; + const capability = createGuiPairCapability( + runtime.attestationSecret, + challenge, + GUI_PAIR_METHOD, + GUI_PAIR_PATH, + browserOrigin, + target.pid, + target.port, + expiresAt, + ); + if (!capability) return { kind: "unavailable", reason: "capability" }; + let response: Response; + try { + response = await fetchImpl(`${baseUrl}${GUI_PAIR_PATH}`, { + method: GUI_PAIR_METHOD, + headers: { + "Content-Length": "0", + [GUI_PAIR_EXPECTED_PID_HEADER]: String(target.pid), + [GUI_PAIR_NONCE_HEADER]: challenge, + [GUI_PAIR_EXPIRES_AT_HEADER]: String(expiresAt), + [GUI_PAIR_BROWSER_ORIGIN_HEADER]: browserOrigin, + [GUI_PAIR_CAPABILITY_HEADER]: capability, + }, + signal: AbortSignal.timeout(timeoutMs), + }); + } catch { + return { kind: "unavailable", reason: "transport" }; + } + if (!response.ok) return { kind: "unavailable", reason: "rejected" }; + const result = parseCreatedResult(await response.json().catch(() => null), browserOrigin); + return result ?? { kind: "unavailable", reason: "rejected" }; +} diff --git a/src/cli/gui.ts b/src/cli/gui.ts new file mode 100644 index 0000000000..9308dad496 --- /dev/null +++ b/src/cli/gui.ts @@ -0,0 +1,87 @@ +import type { OcxConfig } from "../types"; +import { canonicalGuiBrowserOrigin } from "../lib/gui-pair-capability"; +import { findLiveProxy, type LiveProxy } from "../server/proxy-liveness"; +import { + requestBoundGuiPairingGrant, + type GuiPairClientDeps, + type GuiPairRequestResult, +} from "./gui-pair-client"; +import type { RuntimeApiDeps } from "./runtime-api"; + +const GUI_USAGE = "ocx gui [pair --origin [--json]]"; +const PAIRING_WARNING = "Pairing grants are secret, single-use, and expire quickly. Do not save them."; + +export interface GuiCommandDeps extends RuntimeApiDeps { + openDefaultGui: () => Promise; + loadConfig: () => OcxConfig; + findLiveProxy?: () => Promise; + requestPairingGrant?: ( + target: LiveProxy, + browserOrigin: string, + deps?: GuiPairClientDeps, + ) => Promise; +} + +function allowedPairingOrigin(origin: string, config: OcxConfig): boolean { + if (config.runtimeRole !== "hub") return false; + if (canonicalGuiBrowserOrigin(config.hub?.managementPublicOrigin) === origin) return true; + return (config.corsAllowOrigins ?? []).some(value => canonicalGuiBrowserOrigin(value) === origin); +} + +function parsePairArgs(args: string[]): { origin: string; json: boolean } | null { + let origin: string | undefined; + let json = false; + for (let index = 0; index < args.length; index++) { + const arg = args[index]!; + if (arg === "--json" && !json) { + json = true; + continue; + } + if (arg === "--origin" && origin === undefined) { + const value = args[++index]; + if (!value || value.startsWith("--")) return null; + origin = value; + continue; + } + return null; + } + return origin ? { origin, json } : null; +} + +export async function runGuiCommand(args: string[], deps: GuiCommandDeps): Promise { + if (args.length === 0) return deps.openDefaultGui(); + if (args[0] !== "pair") { + console.error(`Usage: ${GUI_USAGE}`); + return 1; + } + const parsed = parsePairArgs(args.slice(1)); + const canonicalOrigin = parsed ? canonicalGuiBrowserOrigin(parsed.origin) : null; + if (!parsed || !canonicalOrigin || canonicalOrigin !== parsed.origin) { + console.error(`Usage: ${GUI_USAGE}`); + return 1; + } + const config = deps.loadConfig(); + if (!allowedPairingOrigin(canonicalOrigin, config)) { + console.error("The pairing origin is not enabled by hub.managementPublicOrigin or corsAllowOrigins."); + return 1; + } + const target = await (deps.findLiveProxy ?? findLiveProxy)(); + if (!target) { + console.error("No running attested OpenCodex proxy is available for GUI pairing."); + return 1; + } + const result = await (deps.requestPairingGrant ?? requestBoundGuiPairingGrant)(target, canonicalOrigin, { + ...(deps.fetchImpl ? { fetchImpl: deps.fetchImpl } : {}), + }); + if (result.kind !== "created") { + console.error(`GUI pairing failed (${result.reason}).`); + return 1; + } + if (parsed.json) { + console.log(JSON.stringify({ ...result, warning: PAIRING_WARNING })); + } else { + console.log(result.grant); + console.error(PAIRING_WARNING); + } + return 0; +} diff --git a/src/cli/help.ts b/src/cli/help.ts index cc1ef7cc58..95a0a8ebd1 100644 --- a/src/cli/help.ts +++ b/src/cli/help.ts @@ -50,7 +50,8 @@ Usage: ocx debug provider/usage/injection/claude on|off|status|reset ocx login OAuth or API-key provider login ocx logout Remove a stored OAuth login - ocx gui Open the opencodex dashboard + ocx gui [pair --origin [--json]] + Open the dashboard or create a single-use remote pairing grant ocx update [--tag ] Update opencodex (keeps preview installs on @preview) ocx restart Stop and restart the proxy ocx v2 multi_agent_v2 surface (status|on|off|mode|keep-native-v1|threads|mode-hint) diff --git a/src/cli/registry.ts b/src/cli/registry.ts index 3031ccc544..d607c0b588 100644 --- a/src/cli/registry.ts +++ b/src/cli/registry.ts @@ -128,7 +128,15 @@ export const CLI_COMMANDS: CliCommandEntry[] = [ }, { name: "login", usage: "ocx login ", summary: "OAuth or API-key login for a provider." }, { name: "logout", usage: "ocx logout ", summary: "Remove a stored provider login." }, - { name: "gui", usage: "ocx gui", summary: "Open the opencodex dashboard." }, + { + name: "gui", + usage: "ocx gui [pair --origin [--json]]", + summary: "Open the opencodex dashboard or create a secret single-use remote pairing grant.", + details: [ + "Pairing requires an explicit allowed --origin; there is no localhost or config-derived default.", + "The printed grant is secret, single-use, short-lived, and must not be persisted.", + ], + }, { name: "update", usage: "ocx update [--tag latest|preview]", diff --git a/src/config.ts b/src/config.ts index c77e1507ff..8939dfe201 100644 --- a/src/config.ts +++ b/src/config.ts @@ -864,11 +864,60 @@ const agentTaskRecoverySchema = z.object({ const runtimeRoleSchema = z.enum(["standalone", "hub", "client"]); +function canonicalHttpOrigin(value: string): string | null { + try { + const parsed = new URL(value); + if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return null; + if (parsed.username || parsed.password || parsed.pathname !== "/" || parsed.search || parsed.hash) return null; + return parsed.origin; + } catch { + return null; + } +} + +const hubConfigSchema = z.object({ + managementPublicOrigin: z.string().transform((value, ctx) => { + const origin = canonicalHttpOrigin(value); + if (!origin) { + ctx.addIssue({ code: "custom", message: "must be a canonical http(s) origin without credentials, path, query, or fragment" }); + return z.NEVER; + } + return origin; + }).optional(), +}).strict(); + +const tailscaleUserSchema = z.string().trim().min(1).superRefine((value, ctx) => { + if (new TextEncoder().encode(value).byteLength > 320) { + ctx.addIssue({ code: "custom", message: "must be at most 320 UTF-8 bytes" }); + } + if (/[\x00-\x1f\x7f]/.test(value)) { + ctx.addIssue({ code: "custom", message: "must not contain ASCII control characters" }); + } +}); + +const remoteGuiConfigSchema = z.object({ + allowedTailscaleUsers: z.array(tailscaleUserSchema).max(64).superRefine((users, ctx) => { + const seen = new Set(); + for (let index = 0; index < users.length; index++) { + const user = users[index]!; + if (seen.has(user)) { + ctx.addIssue({ code: "custom", path: [index], message: "must contain unique users after trimming" }); + } + seen.add(user); + } + }).optional(), + allowInsecureHttp: z.boolean().optional(), +}).strict(); + const configSchema = z.object({ port: z.number().int().min(0).max(65535).default(10100), // A malformed hand edit must disable only remote-role behavior, not discard // providers or data-plane keys. Live writes are rejected explicitly below. runtimeRole: runtimeRoleSchema.optional().catch(undefined), + // Malformed optional remote blocks disable only remote GUI behavior. Live + // candidates are rejected explicitly by remoteGuiConfigError below. + hub: hubConfigSchema.optional().catch(undefined), + remoteGui: remoteGuiConfigSchema.optional().catch(undefined), managementUsageMaxReadBytes: z.number().int().positive().default(64 * 1024 * 1024), // Invalid hand edits disable only this opt-in circuit. Live writes remain strict. upstreamHostCircuitThreshold: z.number().int() @@ -1724,6 +1773,26 @@ function warnDegradedRuntimeRole(rawParsed: unknown): void { if (warning) console.warn(`⚠️ config.json ${warning}. Other settings were preserved.`); } +function malformedOptionalRemoteBlockWarning( + rawParsed: unknown, + key: "hub" | "remoteGui", +): string | null { + const raw = rawConfigRecord(rawParsed); + if (!raw || !Object.hasOwn(raw, key) || raw[key] === undefined) return null; + const schema = key === "hub" ? hubConfigSchema : remoteGuiConfigSchema; + const result = schema.safeParse(raw[key]); + if (result.success) return null; + const field = result.error.issues[0]?.path.join("."); + return `${key}${field ? `.${field}` : ""} ignored: invalid remote GUI configuration`; +} + +function warnDegradedOptionalRemoteBlocks(rawParsed: unknown): void { + for (const key of ["hub", "remoteGui"] as const) { + const warning = malformedOptionalRemoteBlockWarning(rawParsed, key); + if (warning) console.warn(`⚠️ config.json ${warning}. Other settings were preserved.`); + } +} + type NativeSubagentPersistedField = "injectionModel" | "injectionEffort" | "syncCodexSubagentDefaults"; function rawConfigRecord(rawParsed: unknown): Record | null { @@ -1878,6 +1947,7 @@ export function loadConfig(): OcxConfig { warnDegradedUpstreamHostCircuitThreshold(parsed); warnDegradedAgentTaskRecovery(parsed); warnDegradedRuntimeRole(parsed); + warnDegradedOptionalRemoteBlocks(parsed); return withRefreshedCostOverlays(normalizeClaudeSubagentEffort(normalizeNativeSubagentSync(config, parsed), parsed)); } // Schema validation failed — merge defaults into the raw object instead of @@ -1903,6 +1973,7 @@ export function loadConfig(): OcxConfig { warnDegradedUpstreamHostCircuitThreshold(parsed); warnDegradedAgentTaskRecovery(parsed); warnDegradedRuntimeRole(parsed); + warnDegradedOptionalRemoteBlocks(parsed); return withRefreshedCostOverlays(normalizeClaudeSubagentEffort(normalizeNativeSubagentSync(config, parsed), parsed)); } // Still failing, but if every complaint is about one or more named entries @@ -1924,6 +1995,7 @@ export function loadConfig(): OcxConfig { warnDegradedUpstreamHostCircuitThreshold(parsed); warnDegradedAgentTaskRecovery(parsed); warnDegradedRuntimeRole(parsed); + warnDegradedOptionalRemoteBlocks(parsed); return withRefreshedCostOverlays(normalizeClaudeSubagentEffort(normalizeNativeSubagentSync(config, parsed), parsed)); } } @@ -2026,6 +2098,10 @@ function validFileConfigDiagnostics(config: OcxConfig, rawParsed: unknown): Conf if (recoveryWarning) warnings.push(recoveryWarning); const runtimeRoleWarning = malformedRuntimeRoleWarning(rawParsed); if (runtimeRoleWarning) warnings.push(runtimeRoleWarning); + const hubWarning = malformedOptionalRemoteBlockWarning(rawParsed, "hub"); + if (hubWarning) warnings.push(hubWarning); + const remoteGuiWarning = malformedOptionalRemoteBlockWarning(rawParsed, "remoteGui"); + if (remoteGuiWarning) warnings.push(remoteGuiWarning); if (syncDisabledReason) { warnings.push(`syncCodexSubagentDefaults ignored: ${syncDisabledReason}`); } @@ -2124,6 +2200,23 @@ function runtimeRoleError(value: unknown): string | null { return 'schema_invalid: runtimeRole: must be one of "standalone", "hub", or "client"'; } +function remoteGuiConfigError(value: unknown): string | null { + const raw = rawConfigRecord(value); + if (!raw) return null; + for (const [key, schema] of [ + ["hub", hubConfigSchema], + ["remoteGui", remoteGuiConfigSchema], + ] as const) { + if (!Object.hasOwn(raw, key) || raw[key] === undefined) continue; + const result = schema.safeParse(raw[key]); + if (result.success) continue; + const issue = result.error.issues[0]; + const field = issue?.path.join("."); + return `schema_invalid: ${key}${field ? `.${field}` : ""}: ${issue?.message ?? "invalid configuration"}`; + } + return null; +} + /** * Same reasoning as {@link blankHostnameError}, and more urgent: the read path degrades a * malformed selection-order map to undefined, which on a write would drop every entry the @@ -2242,6 +2335,7 @@ export function validateConfigCandidate(value: unknown): { ok: true; config: Ocx ?? emptyCompletionRetryError(value) ?? oauthOpenBrowserError(value) ?? runtimeRoleError(value) + ?? remoteGuiConfigError(value) ?? loopbackListenerPortError(value); if (boundaryError) return { ok: false, error: boundaryError }; const result = configSchema.safeParse(value); diff --git a/src/lib/gui-pair-capability.ts b/src/lib/gui-pair-capability.ts new file mode 100644 index 0000000000..d3a409e595 --- /dev/null +++ b/src/lib/gui-pair-capability.ts @@ -0,0 +1,104 @@ +import { createHmac, timingSafeEqual } from "node:crypto"; +import { isLocalAttestationSecret } from "./local-management-attestation"; + +export const GUI_PAIR_METHOD = "POST"; +export const GUI_PAIR_PATH = "/api/gui/pairing-grants"; +export const GUI_PAIR_CAPABILITY_VERSION = "v1"; +export const GUI_PAIR_EXPECTED_PID_HEADER = "x-opencodex-gui-pair-expected-pid"; +export const GUI_PAIR_NONCE_HEADER = "x-opencodex-gui-pair-nonce"; +export const GUI_PAIR_EXPIRES_AT_HEADER = "x-opencodex-gui-pair-expires-at"; +export const GUI_PAIR_BROWSER_ORIGIN_HEADER = "x-opencodex-gui-pair-origin"; +export const GUI_PAIR_CAPABILITY_HEADER = "x-opencodex-gui-pair-capability"; +export const GUI_PAIR_CAPABILITY_TTL_MS = 10_000; + +const BASE64URL_256 = /^[A-Za-z0-9_-]{43}$/; + +export type ExpectedGuiPairPid = + | { kind: "absent" } + | { kind: "invalid" } + | { kind: "present"; pid: number }; + +export function parseExpectedGuiPairPid(value: string | null): ExpectedGuiPairPid { + if (value === null) return { kind: "absent" }; + if (!/^[1-9]\d*$/.test(value)) return { kind: "invalid" }; + const pid = Number(value); + return Number.isSafeInteger(pid) ? { kind: "present", pid } : { kind: "invalid" }; +} + +export function canonicalGuiBrowserOrigin(value: unknown): string | null { + if (typeof value !== "string" || value !== value.trim()) return null; + try { + const parsed = new URL(value); + if (!parsed.host || parsed.username || parsed.password || parsed.search || parsed.hash) return null; + if (parsed.pathname !== "" && parsed.pathname !== "/") return null; + if (parsed.protocol === "http:" || parsed.protocol === "https:") return parsed.origin; + return `${parsed.protocol}//${parsed.host}`; + } catch { + return null; + } +} + +function capabilityPayload( + nonce: string, + method: string, + path: string, + browserOrigin: string, + pid: number, + port: number, + expiresAt: number, +): string | null { + if (!BASE64URL_256.test(nonce)) return null; + if (method !== GUI_PAIR_METHOD || path !== GUI_PAIR_PATH) return null; + const canonicalOrigin = canonicalGuiBrowserOrigin(browserOrigin); + if (!canonicalOrigin || canonicalOrigin !== browserOrigin) return null; + if (!Number.isSafeInteger(pid) || pid <= 0) return null; + if (!Number.isInteger(port) || port <= 0 || port > 65535) return null; + if (!Number.isSafeInteger(expiresAt) || expiresAt <= 0) return null; + return `opencodex-gui-pair-v1\n${nonce}\n${method}\n${path}\n${browserOrigin}\n${pid}\n${port}\n${expiresAt}`; +} + +export function createGuiPairCapability( + secret: string, + nonce: string, + method: string, + path: string, + browserOrigin: string, + pid: number, + port: number, + expiresAt: number, +): string | null { + if (!isLocalAttestationSecret(secret)) return null; + const payload = capabilityPayload(nonce, method, path, browserOrigin, pid, port, expiresAt); + if (!payload) return null; + return createHmac("sha256", secret).update(payload).digest("base64url"); +} + +export function verifyGuiPairCapability( + secret: string, + nonce: string | null, + method: string, + path: string, + browserOrigin: string | null, + pid: number, + port: number, + expiresAt: number, + capability: string | null, + now = Date.now(), +): boolean { + if (!nonce || !browserOrigin || !capability || !BASE64URL_256.test(capability)) return false; + if (!Number.isSafeInteger(now) || expiresAt <= now || expiresAt > now + GUI_PAIR_CAPABILITY_TTL_MS) return false; + const expected = createGuiPairCapability( + secret, + nonce, + method, + path, + browserOrigin, + pid, + port, + expiresAt, + ); + if (!expected) return false; + const expectedBytes = Buffer.from(expected); + const actualBytes = Buffer.from(capability); + return expectedBytes.length === actualBytes.length && timingSafeEqual(expectedBytes, actualBytes); +} diff --git a/src/remote/protocol.ts b/src/remote/protocol.ts index 71ee0c10b5..ba192f0066 100644 --- a/src/remote/protocol.ts +++ b/src/remote/protocol.ts @@ -43,10 +43,10 @@ function observedManagementOrigin(req: Request): string | null { } export function readyProtocolMetadata(config: OcxConfig, req: Request): RemoteReadyMetadata { - // Phase 2 will consult config.hub.managementPublicOrigin here. Keeping the - // parameter now fixes the consumer signature without changing Phase 1 behavior. - void config; - const managementUrl = observedManagementOrigin(req); + const configured = config.runtimeRole === "hub" + ? managementOrigin(config.hub?.managementPublicOrigin) + : null; + const managementUrl = configured ?? observedManagementOrigin(req); if (!managementUrl) throw new Error("Readiness request does not have an HTTP(S) management origin"); return { protocol: REMOTE_HUB_PROTOCOL, diff --git a/src/server/auth-cors.ts b/src/server/auth-cors.ts index 0174794c6a..afc93ee528 100644 --- a/src/server/auth-cors.ts +++ b/src/server/auth-cors.ts @@ -121,7 +121,29 @@ export function managementRequestOrigin(req: Request, config: OcxConfig): string const host = req.headers.get("Host"); const parsedHost = parseHttpHost(host); if (!host || !parsedHost) return null; - if (!isApiAuthRequired(config) && !isLoopbackHostname(parsedHost.hostname)) return null; + if (isLoopbackHostname(parsedHost.hostname)) { + try { + const protocol = new URL(req.url).protocol; + if (protocol !== "http:" && protocol !== "https:") return null; + return new URL(`${protocol}//${host}`).origin; + } catch { + return null; + } + } + if (!isApiAuthRequired(config)) return null; + if (config.runtimeRole === "hub" && config.hub?.managementPublicOrigin) { + try { + const configured = new URL(config.hub.managementPublicOrigin); + if ( + (configured.protocol === "http:" || configured.protocol === "https:") + && !configured.username + && !configured.password + && configured.pathname === "/" + && !configured.search + && !configured.hash + ) return configured.origin; + } catch { /* malformed direct fixture: fall through to observed origin */ } + } try { const protocol = new URL(req.url).protocol; if (protocol !== "http:" && protocol !== "https:") return null; @@ -200,6 +222,7 @@ export function corsHeaders(req?: Request, config?: RequestPolicyView): Record { const headers = corsHeaders(); + headers["Access-Control-Allow-Headers"] = `${STATIC_ALLOWED_REQUEST_HEADERS}, X-OpenCodex-GUI-Origin, X-OpenCodex-CSRF-Token`; const origin = req?.headers.get("Origin"); if (origin && req && config && isAllowedManagementOrigin(req, config)) { headers["Access-Control-Allow-Origin"] = origin; diff --git a/src/server/gui-session.ts b/src/server/gui-session.ts new file mode 100644 index 0000000000..0cf408c993 --- /dev/null +++ b/src/server/gui-session.ts @@ -0,0 +1,318 @@ +import { createHash, randomBytes, timingSafeEqual } from "node:crypto"; +import type { OcxConfig } from "../types"; +import { canonicalGuiBrowserOrigin } from "../lib/gui-pair-capability"; +import { + isAllowedManagementOrigin, + isApiAuthRequired, + isLoopbackHostname, + managementRequestOrigin, + parseHttpHost, +} from "./auth-cors"; + +export type GuiSessionIssuance = + | "loopback" + | "tailscale-identity" + | "pairing" + | "insecure-http-pairing"; + +export interface GuiSessionRecord { + serverOrigin: string; + browserOrigin: string; + csrfToken: string; + expiresAt: number; + issuance: GuiSessionIssuance; +} + +export interface GuiSessionBootstrap extends GuiSessionRecord { + token: string; +} + +export interface GuiPairingGrantRecord { + serverOrigin: string; + browserOrigin: string; + expiresAt: number; +} + +export interface GuiSessionState { + sessions: Map; + pairingGrants: Map; +} + +export interface GuiSessionRequestContext { + trustedTailscaleIngress: boolean; + now?: number; +} + +export type GuiSessionAdmission = + | { ok: true; principal: "gui-session"; session: GuiSessionRecord } + | { ok: false; reason: "missing" | "expired" | "server-origin" | "browser-origin" | "csrf" }; + +export const LOOPBACK_GUI_SESSION_TTL_MS = 5 * 60_000; +export const REMOTE_GUI_SESSION_TTL_MS = 12 * 60 * 60_000; +export const GUI_PAIRING_GRANT_TTL_MS = 5 * 60_000; +export const GUI_SESSION_LIMIT = 128; +export const GUI_PAIRING_GRANT_LIMIT = 64; +export const GUI_PAIRING_GRANT_RATE_LIMIT = 8; +export const GUI_PAIRING_GRANT_RATE_WINDOW_MS = 60_000; + +const pairingGrantCreations = new WeakMap(); + +export class GuiPairingGrantRateLimitError extends Error { + constructor() { + super("GUI pairing grant rate limit exceeded"); + this.name = "GuiPairingGrantRateLimitError"; + } +} + +function equalSecret(actual: string, expected: string): boolean { + const encoder = new TextEncoder(); + const left = encoder.encode(actual); + const right = encoder.encode(expected); + return left.length === right.length && timingSafeEqual(left, right); +} + +function canonicalHttpOrigin(value: unknown): string | null { + if (typeof value !== "string") return null; + try { + const parsed = new URL(value); + if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return null; + if (parsed.username || parsed.password || parsed.pathname !== "/" || parsed.search || parsed.hash) return null; + return parsed.origin; + } catch { + return null; + } +} + +export function isRemoteGuiBrowserOriginAllowed(browserOrigin: string, config: OcxConfig): boolean { + const canonical = canonicalGuiBrowserOrigin(browserOrigin); + if (!canonical || canonical !== browserOrigin) return false; + const publicOrigin = canonicalHttpOrigin(config.hub?.managementPublicOrigin); + if (publicOrigin === canonical) return true; + return (config.corsAllowOrigins ?? []).some(value => canonicalGuiBrowserOrigin(value) === canonical); +} + +function pruneExpired(state: GuiSessionState, now: number): void { + for (const [token, session] of state.sessions) { + if (session.expiresAt <= now) state.sessions.delete(token); + } + for (const [digest, grant] of state.pairingGrants) { + if (grant.expiresAt <= now) state.pairingGrants.delete(digest); + } +} + +function evictOldestSession(state: GuiSessionState): void { + while (state.sessions.size >= GUI_SESSION_LIMIT) { + const oldest = state.sessions.keys().next().value as string | undefined; + if (!oldest) return; + state.sessions.delete(oldest); + } +} + +function mintSession( + serverOrigin: string, + browserOrigin: string, + issuance: GuiSessionIssuance, + state: GuiSessionState, + now: number, +): GuiSessionBootstrap { + pruneExpired(state, now); + evictOldestSession(state); + let token: string; + do { + token = `ocx_session_${randomBytes(32).toString("base64url")}`; + } while (state.sessions.has(token)); + const session: GuiSessionRecord = { + serverOrigin, + browserOrigin, + csrfToken: randomBytes(32).toString("base64url"), + expiresAt: now + (issuance === "loopback" ? LOOPBACK_GUI_SESSION_TTL_MS : REMOTE_GUI_SESSION_TTL_MS), + issuance, + }; + state.sessions.set(token, session); + return { token, ...session }; +} + +function tailscaleLoginAllowed(req: Request, config: OcxConfig): boolean { + const login = req.headers.get("Tailscale-User-Login")?.trim(); + if (!login) return false; + return (config.remoteGui?.allowedTailscaleUsers ?? []).some(user => user === login); +} + +export function issueGuiSession( + req: Request, + config: OcxConfig, + state: GuiSessionState, + context: GuiSessionRequestContext = { trustedTailscaleIngress: false }, +): GuiSessionBootstrap | null { + if (req.method !== "GET") return null; + const host = parseHttpHost(req.headers.get("Host")); + if (!host) return null; + const now = context.now ?? Date.now(); + + if (!isApiAuthRequired(config)) { + if (!isLoopbackHostname(host.hostname) || !isAllowedManagementOrigin(req, config)) return null; + const origin = managementRequestOrigin(req, config); + return origin ? mintSession(origin, origin, "loopback", state, now) : null; + } + + if ( + config.runtimeRole !== "hub" + || !context.trustedTailscaleIngress + || !tailscaleLoginAllowed(req, config) + || !isAllowedManagementOrigin(req, config) + ) return null; + const serverOrigin = managementRequestOrigin(req, config); + if (!serverOrigin || new URL(serverOrigin).protocol !== "https:") return null; + const browserOrigin = canonicalGuiBrowserOrigin(req.headers.get("Origin") ?? serverOrigin); + if (!browserOrigin || !isRemoteGuiBrowserOriginAllowed(browserOrigin, config)) return null; + return mintSession(serverOrigin, browserOrigin, "tailscale-identity", state, now); +} + +function pairingGrantDigest(grant: string): string { + return createHash("sha256").update(grant).digest("base64url"); +} + +function findPairingGrant( + grant: string, + state: GuiSessionState, +): [string, GuiPairingGrantRecord] | null { + const digest = pairingGrantDigest(grant); + for (const [candidate, record] of state.pairingGrants) { + if (equalSecret(candidate, digest)) return [candidate, record]; + } + return null; +} + +function consumeGrantRateSlot(state: GuiSessionState, now: number): void { + const recent = (pairingGrantCreations.get(state) ?? []) + .filter(createdAt => createdAt > now - GUI_PAIRING_GRANT_RATE_WINDOW_MS); + if (recent.length >= GUI_PAIRING_GRANT_RATE_LIMIT) throw new GuiPairingGrantRateLimitError(); + recent.push(now); + pairingGrantCreations.set(state, recent); +} + +export function createGuiPairingGrant( + browserOrigin: string, + config: OcxConfig, + state: GuiSessionState, + now = Date.now(), +): { grant: string; browserOrigin: string; serverOrigin: string; expiresAt: number } { + const canonicalBrowserOrigin = canonicalGuiBrowserOrigin(browserOrigin); + const serverOrigin = canonicalHttpOrigin(config.hub?.managementPublicOrigin); + if ( + config.runtimeRole !== "hub" + || !canonicalBrowserOrigin + || canonicalBrowserOrigin !== browserOrigin + || !serverOrigin + || !isRemoteGuiBrowserOriginAllowed(canonicalBrowserOrigin, config) + ) throw new TypeError("remote GUI origin is not allowed"); + pruneExpired(state, now); + consumeGrantRateSlot(state, now); + if (state.pairingGrants.size >= GUI_PAIRING_GRANT_LIMIT) throw new GuiPairingGrantRateLimitError(); + let grant: string; + let digest: string; + do { + grant = `ocx_pair_${randomBytes(32).toString("base64url")}`; + digest = pairingGrantDigest(grant); + } while (state.pairingGrants.has(digest)); + const expiresAt = now + GUI_PAIRING_GRANT_TTL_MS; + state.pairingGrants.set(digest, { browserOrigin: canonicalBrowserOrigin, serverOrigin, expiresAt }); + return { grant, browserOrigin: canonicalBrowserOrigin, serverOrigin, expiresAt }; +} + +function strictPairingGrantBody(body: unknown): string | null { + if (!body || typeof body !== "object" || Array.isArray(body)) return null; + const record = body as Record; + if (Object.keys(record).length !== 1 || typeof record.grant !== "string") return null; + return /^ocx_pair_[A-Za-z0-9_-]{43}$/.test(record.grant) ? record.grant : null; +} + +function hasAlternateCredential(req: Request): boolean { + return req.headers.has("authorization") + || req.headers.has("x-opencodex-api-key") + || req.headers.has("x-api-key"); +} + +export function consumeGuiPairingGrant( + req: Request, + body: unknown, + config: OcxConfig, + state: GuiSessionState, + now = Date.now(), +): GuiSessionBootstrap | null { + if (req.method !== "POST" || hasAlternateCredential(req) || config.runtimeRole !== "hub") return null; + const grant = strictPairingGrantBody(body); + const browserOrigin = canonicalGuiBrowserOrigin(req.headers.get("Origin")); + if (!grant || !browserOrigin) return null; + const found = findPairingGrant(grant, state); + if (!found) return null; + const [digest, record] = found; + if (record.expiresAt <= now) { + state.pairingGrants.delete(digest); + return null; + } + if (browserOrigin !== record.browserOrigin) return null; + const serverOrigin = managementRequestOrigin(req, config); + if (serverOrigin !== record.serverOrigin) return null; + const serverUrl = new URL(serverOrigin); + let issuance: GuiSessionIssuance; + if (serverUrl.protocol === "https:") issuance = "pairing"; + else if ( + serverUrl.protocol === "http:" + && !isLoopbackHostname(serverUrl.hostname) + && config.remoteGui?.allowInsecureHttp === true + ) issuance = "insecure-http-pairing"; + else return null; + state.pairingGrants.delete(digest); + return mintSession(record.serverOrigin, record.browserOrigin, issuance, state, now); +} + +function requestCredential(req: Request): string | null { + return req.headers.get("x-opencodex-api-key")?.trim() + || req.headers.get("authorization")?.replace(/^Bearer\s+/i, "").trim() + || null; +} + +function findSession( + credential: string, + state: GuiSessionState, +): [string, GuiSessionRecord] | null { + for (const [token, session] of state.sessions) { + if (equalSecret(credential, token)) return [token, session]; + } + return null; +} + +export function authorizeGuiSessionRequest( + req: Request, + config: OcxConfig, + state: GuiSessionState, + now = Date.now(), +): GuiSessionAdmission { + const credential = requestCredential(req); + if (!credential) return { ok: false, reason: "missing" }; + const found = findSession(credential, state); + if (!found) return { ok: false, reason: "missing" }; + const [token, session] = found; + if (session.expiresAt <= now) { + state.sessions.delete(token); + return { ok: false, reason: "expired" }; + } + if (managementRequestOrigin(req, config) !== session.serverOrigin) { + return { ok: false, reason: "server-origin" }; + } + const claimedBrowserOrigin = req.headers.get("x-opencodex-gui-origin"); + const browserOrigin = req.headers.get("Origin"); + const safeMethod = req.method === "GET" || req.method === "HEAD"; + if ( + claimedBrowserOrigin !== session.browserOrigin + || (browserOrigin !== null && browserOrigin !== session.browserOrigin) + || (!safeMethod && browserOrigin !== session.browserOrigin) + ) return { ok: false, reason: "browser-origin" }; + if (!safeMethod) { + const csrf = req.headers.get("x-opencodex-csrf-token")?.trim(); + if (!csrf || !equalSecret(csrf, session.csrfToken)) return { ok: false, reason: "csrf" }; + } + if (session.issuance !== "loopback") session.expiresAt = now + REMOTE_GUI_SESSION_TTL_MS; + return { ok: true, principal: "gui-session", session }; +} diff --git a/src/server/gui-static.ts b/src/server/gui-static.ts index 85968c58e9..c93299da3c 100644 --- a/src/server/gui-static.ts +++ b/src/server/gui-static.ts @@ -1,7 +1,7 @@ import { existsSync, readFileSync, statSync } from "node:fs"; import { extname, isAbsolute, join, relative, resolve } from "node:path"; import { browserSecurityHeaders } from "./auth-cors"; -import type { GuiSessionBootstrap } from "./management-auth"; +import type { GuiSessionBootstrap } from "./gui-session"; /** opencodex version, read from the packaged package.json (same source as the server bootstrap). */ const VERSION = (() => { @@ -70,7 +70,8 @@ function sessionBootstrapMeta(session: GuiSessionBootstrap): string { return [ ``, ``, - ``, + ``, + ``, ].join(""); } diff --git a/src/server/index.ts b/src/server/index.ts index 35c7361be8..a8d4c7a7f5 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -205,6 +205,16 @@ import { } from "../lib/local-management-attestation"; import { SYSTEM_RESTART_CAPABILITY_VERSION } from "../lib/system-restart-contract"; import { LOCAL_PROVIDER_RELOAD_CAPABILITY_VERSION } from "../lib/local-provider-reload-contract"; +import { + GUI_PAIR_BROWSER_ORIGIN_HEADER, + GUI_PAIR_CAPABILITY_VERSION, + GUI_PAIR_PATH, +} from "../lib/gui-pair-capability"; +import { + GuiPairingGrantRateLimitError, + consumeGuiPairingGrant, + createGuiPairingGrant, +} from "./gui-session"; import { createReadinessGate, type ReadinessGate } from "./readiness"; import { createRuntimePackageTreeIntegrityGuard, @@ -219,6 +229,7 @@ const WEBSOCKET_IDLE_TIMEOUT_SECONDS = 0; // Header-safe by construction: a key id reaches a response header, so anything outside this // class could inject a header break or a control character into a response we control. const REMOTE_CATALOG_KEY_ID_PATTERN = /^[A-Za-z0-9._-]{1,64}$/; +const GUI_PAIRING_EXCHANGE_BODY_LIMIT = 4 * 1024; /** * Name WHICH configured credential was admitted, so a multi-key operator can attribute a @@ -1037,6 +1048,7 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server GUI_PAIRING_EXCHANGE_BODY_LIMIT) { + return Response.json({ error: "pairing exchange body too large" }, { status: 413, headers: { "Cache-Control": "no-store" } }); + } + const text = await req.text(); + if (Buffer.byteLength(text) > GUI_PAIRING_EXCHANGE_BODY_LIMIT) { + return Response.json({ error: "pairing exchange body too large" }, { status: 413, headers: { "Cache-Control": "no-store" } }); + } + let body: unknown; + try { + body = JSON.parse(text); + } catch { + return Response.json({ error: "invalid pairing exchange body" }, { status: 400, headers: { "Cache-Control": "no-store" } }); + } + if (!body || typeof body !== "object" || Array.isArray(body) + || Object.keys(body as Record).length !== 1 + || typeof (body as Record).grant !== "string") { + return Response.json({ error: "invalid pairing exchange body" }, { status: 400, headers: { "Cache-Control": "no-store" } }); + } + const session = managementAuth.available + ? consumeGuiPairingGrant(req, body, config, managementAuth) + : null; + return session + ? withManagementCors(serveSessionBootstrap(session), req, config) + : new Response(null, { status: 401, headers: { "Cache-Control": "no-store" } }); + } + return withCors(formatErrorResponse(404, "not_found", `Unknown endpoint: ${req.method} ${url.pathname}`), req, policy); + } const guiSessionCandidate = req.method === "GET" && (url.pathname === "/" || !url.pathname.includes(".")) - ? issueGuiSession(req, config, managementAuth) + ? issueGuiSession(req, config, managementAuth, { trustedTailscaleIngress: false }) : null; - // Dedicated bootstrap path: answer without requiring a packaged GUI build, so the - // Vite dev server can mint an origin-bound loopback session on a fresh checkout. - if (url.pathname === "/opencodex-session" && guiSessionCandidate) { - return serveSessionBootstrap(guiSessionCandidate); - } const guiFile = serveGuiFile(url.pathname, undefined, guiSessionCandidate ?? undefined); if (guiFile) return guiFile; if (url.pathname === "/" && req.method === "GET") { diff --git a/src/server/management-auth.ts b/src/server/management-auth.ts index 83c59d8d06..a8c26c57a5 100644 --- a/src/server/management-auth.ts +++ b/src/server/management-auth.ts @@ -39,35 +39,41 @@ import { parseExpectedLocalProviderReloadPid, verifyLocalProviderReloadCapability, } from "../lib/local-provider-reload-contract"; +import { + GUI_PAIR_BROWSER_ORIGIN_HEADER, + GUI_PAIR_CAPABILITY_HEADER, + GUI_PAIR_EXPECTED_PID_HEADER, + GUI_PAIR_EXPIRES_AT_HEADER, + GUI_PAIR_NONCE_HEADER, + GUI_PAIR_PATH, + parseExpectedGuiPairPid, + verifyGuiPairCapability, +} from "../lib/gui-pair-capability"; import { forgetEphemeralSecretPath, forgetHardenedSecretPath, hardenSecretDir, hardenSecretPath } from "../lib/windows-secret-acl"; import type { OcxConfig } from "../types"; import { - isAllowedManagementOrigin, - isApiAuthRequired, isDataPlaneAdmissionSecret, - isLoopbackHostname, - managementRequestOrigin, - parseHttpHost, } from "./auth-cors"; +import { + authorizeGuiSessionRequest, + issueGuiSession as issueGuiSessionFromState, + type GuiPairingGrantRecord, + type GuiSessionBootstrap, + type GuiSessionRecord, + type GuiSessionRequestContext, +} from "./gui-session"; +export type { GuiSessionBootstrap, GuiSessionRequestContext } from "./gui-session"; -const GUI_SESSION_TTL_MS = 5 * 60_000; -const GUI_SESSION_LIMIT = 128; const LOCAL_READ_REPLAY_LIMIT = 256; const consumedLocalReadCapabilities = new Map(); const admittedLocalReadRequests = new WeakSet(); const LOCAL_PROVIDER_RELOAD_REPLAY_LIMIT = 256; const consumedLocalProviderReloadCapabilities = new Map(); const admittedLocalProviderReloadRequests = new WeakSet(); - -interface GuiSessionRecord { - csrfToken: string; - origin: string; - expiresAt: number; -} - -export interface GuiSessionBootstrap extends GuiSessionRecord { - token: string; -} +const GUI_PAIR_REPLAY_LIMIT = 256; +const consumedGuiPairCapabilities = new Map(); +const admittedGuiPairRequests = new WeakSet(); +const admittedManagementRequests = new WeakMap(); export type ManagementAuthState = | { @@ -75,6 +81,7 @@ export type ManagementAuthState = token: string; source: "environment" | "file"; sessions: Map; + pairingGrants: Map; } | { available: false; reason: string }; @@ -201,7 +208,7 @@ function ready(token: string, source: "environment" | "file", config: OcxConfig) if (isDataPlaneAdmissionSecret(token, config)) { return fail("management credential conflicts with a data-plane credential"); } - return { available: true, token, source, sessions: new Map() }; + return { available: true, token, source, sessions: new Map(), pairingGrants: new Map() }; } export function initializeManagementAuthState(config: OcxConfig): ManagementAuthState { @@ -232,41 +239,14 @@ function equalSecret(actual: string, expected: string): boolean { return left.length === right.length && timingSafeEqual(left, right); } -function removeExpiredSessions(state: Extract, now = Date.now()): void { - for (const [token, session] of state.sessions) { - if (session.expiresAt <= now) state.sessions.delete(token); - } -} - -function randomSessionSecret(prefix: "ocx_session_"): string { - return `${prefix}${randomBytes(32).toString("base64url")}`; -} - export function issueGuiSession( req: Request, config: OcxConfig, state: ManagementAuthState, + context?: GuiSessionRequestContext, ): GuiSessionBootstrap | null { - if (isApiAuthRequired(config) || !state.available || req.method !== "GET" || !isAllowedManagementOrigin(req, config)) return null; - const host = parseHttpHost(req.headers.get("Host")); - if (!host || !isLoopbackHostname(host.hostname)) return null; - const origin = managementRequestOrigin(req, config); - if (!origin) return null; - const now = Date.now(); - removeExpiredSessions(state, now); - while (state.sessions.size >= GUI_SESSION_LIMIT) { - const oldest = state.sessions.keys().next().value as string | undefined; - if (!oldest) break; - state.sessions.delete(oldest); - } - const token = randomSessionSecret("ocx_session_"); - const session: GuiSessionRecord = { - csrfToken: randomBytes(32).toString("base64url"), - origin, - expiresAt: now + GUI_SESSION_TTL_MS, - }; - state.sessions.set(token, session); - return { token, ...session }; + if (!state.available) return null; + return issueGuiSessionFromState(req, config, state, context); } /** @@ -284,6 +264,7 @@ export function issueGuiSession( export type ManagementPrincipal = | "admin-token" | "gui-session" + | "gui-pair-capability" | "local-read-capability" | "local-provider-reload-capability" | "system-restart-capability"; @@ -416,6 +397,79 @@ function hasLocalProviderReloadCapability( return true; } +function hasGuiPairCapability( + req: Request, + local: LocalManagementAuthContext | undefined, +): boolean { + if (admittedGuiPairRequests.has(req)) return true; + if (!local || req.method !== "POST") return false; + let url: URL; + try { + url = new URL(req.url); + } catch { + return false; + } + if (url.pathname !== GUI_PAIR_PATH || url.search !== "") return false; + const contentLength = req.headers.get("content-length"); + if (contentLength !== "0" || req.headers.has("transfer-encoding")) return false; + const expectedPid = parseExpectedGuiPairPid(req.headers.get(GUI_PAIR_EXPECTED_PID_HEADER)); + if (expectedPid.kind !== "present" || expectedPid.pid !== local.pid) return false; + const expiresAtRaw = req.headers.get(GUI_PAIR_EXPIRES_AT_HEADER); + if (!expiresAtRaw || !/^[1-9]\d*$/.test(expiresAtRaw)) return false; + const expiresAt = Number(expiresAtRaw); + if (!Number.isSafeInteger(expiresAt)) return false; + const capability = req.headers.get(GUI_PAIR_CAPABILITY_HEADER); + const now = Date.now(); + if (!verifyGuiPairCapability( + local.attestationSecret, + req.headers.get(GUI_PAIR_NONCE_HEADER), + req.method, + url.pathname, + req.headers.get(GUI_PAIR_BROWSER_ORIGIN_HEADER), + local.pid, + local.port, + expiresAt, + capability, + now, + )) return false; + for (const [consumed, retainedUntil] of consumedGuiPairCapabilities) { + if (retainedUntil <= now) consumedGuiPairCapabilities.delete(consumed); + } + if (!capability || consumedGuiPairCapabilities.has(capability)) return false; + if (consumedGuiPairCapabilities.size >= GUI_PAIR_REPLAY_LIMIT) return false; + consumedGuiPairCapabilities.set(capability, expiresAt); + admittedGuiPairRequests.add(req); + return true; +} + +function requestManagementCredential(req: Request): string | null { + return req.headers.get("x-opencodex-api-key")?.trim() + || req.headers.get("authorization")?.replace(/^Bearer\s+/i, "").trim() + || null; +} + +function resolveManagementAdmission( + req: Request, + state: ManagementAuthState, + config?: OcxConfig, + local?: LocalManagementAuthContext, +): ManagementPrincipal | null { + const cached = admittedManagementRequests.get(req); + if (cached) return cached; + let principal: ManagementPrincipal | null = null; + if (hasSystemRestartCapability(req, local)) principal = "system-restart-capability"; + else if (hasLocalProviderReloadCapability(req, local)) principal = "local-provider-reload-capability"; + else if (hasLocalReadCapability(req, local)) principal = "local-read-capability"; + else if (hasGuiPairCapability(req, local)) principal = "gui-pair-capability"; + else if (state.available) { + const actual = requestManagementCredential(req); + if (actual && equalSecret(actual, state.token)) principal = "admin-token"; + else if (config && authorizeGuiSessionRequest(req, config, state).ok) principal = "gui-session"; + } + if (principal) admittedManagementRequests.set(req, principal); + return principal; +} + /** * The principal for a request that already passed `requireManagementAuth`. Kept as a * separate resolution (rather than a changed return type) so every existing caller @@ -429,17 +483,7 @@ export function managementPrincipal( config?: OcxConfig, local?: LocalManagementAuthContext, ): ManagementPrincipal | null { - if (hasSystemRestartCapability(req, local)) return "system-restart-capability"; - if (hasLocalProviderReloadCapability(req, local)) return "local-provider-reload-capability"; - if (hasLocalReadCapability(req, local)) return "local-read-capability"; - if (!state.available) return null; - const actual = req.headers.get("x-opencodex-api-key")?.trim() - || req.headers.get("authorization")?.replace(/^Bearer\s+/i, "").trim(); - if (!actual) return null; - if (equalSecret(actual, state.token)) return "admin-token"; - if (!config) return null; - removeExpiredSessions(state); - return state.sessions.has(actual) ? "gui-session" : null; + return resolveManagementAdmission(req, state, config, local); } export function requireManagementAuth( @@ -448,9 +492,7 @@ export function requireManagementAuth( config?: OcxConfig, local?: LocalManagementAuthContext, ): Response | null { - if (hasSystemRestartCapability(req, local)) return null; - if (hasLocalProviderReloadCapability(req, local)) return null; - if (hasLocalReadCapability(req, local)) return null; + if (resolveManagementAdmission(req, state, config, local)) return null; if (!state.available) { return Response.json({ error: "management API unavailable", @@ -458,25 +500,5 @@ export function requireManagementAuth( hint: "Set OPENCODEX_ADMIN_AUTH_TOKEN to bypass file-backed admin token ACL hardening", }, { status: 503 }); } - const actual = req.headers.get("x-opencodex-api-key")?.trim() - || req.headers.get("authorization")?.replace(/^Bearer\s+/i, "").trim(); - if (actual && equalSecret(actual, state.token)) return null; - if (actual && config) { - removeExpiredSessions(state); - const session = state.sessions.get(actual); - if (session) { - const requestOrigin = managementRequestOrigin(req, config); - const claimedOrigin = req.headers.get("x-opencodex-gui-origin"); - const browserOrigin = req.headers.get("Origin"); - const sameOrigin = requestOrigin === session.origin - && claimedOrigin === session.origin - && (!browserOrigin || browserOrigin === session.origin); - const safeMethod = req.method === "GET" || req.method === "HEAD"; - const csrf = req.headers.get("x-opencodex-csrf-token")?.trim(); - if (sameOrigin && (safeMethod || (browserOrigin === session.origin && !!csrf && equalSecret(csrf, session.csrfToken)))) { - return null; - } - } - } return Response.json({ error: "opencodex admin token required" }, { status: 401 }); } diff --git a/src/server/proxy-liveness.ts b/src/server/proxy-liveness.ts index 412cf2709d..df7d8d7281 100644 --- a/src/server/proxy-liveness.ts +++ b/src/server/proxy-liveness.ts @@ -22,6 +22,7 @@ export interface HealthzIdentity { port?: unknown; restartCapability?: unknown; providerReloadCapability?: unknown; + guiPairCapability?: unknown; } export interface LivenessIo { diff --git a/src/types.ts b/src/types.ts index 71171957aa..c4b0b6ed8a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -63,6 +63,8 @@ export type { OcxApiKeyEntry, OcxClientIntegrationsConfig, OcxConfigRebaseProvenance, + OcxHubConfig, + OcxRemoteGuiConfig, OcxConfig, OcxAccountPoolRotationStrategy, OcxAccountPoolQuotaWindow, diff --git a/src/types/config.ts b/src/types/config.ts index 07ba0cc4f3..a2438b8b51 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -246,10 +246,26 @@ export interface OcxConfigRebaseProvenance { export type OcxRuntimeRole = "standalone" | "hub" | "client"; +export interface OcxHubConfig { + /** Canonical browser-reachable management origin advertised by a hub. */ + managementPublicOrigin?: string; +} + +export interface OcxRemoteGuiConfig { + /** Exact Tailscale login identities permitted to receive an automatic remote GUI session. */ + allowedTailscaleUsers?: string[]; + /** Explicitly permit one-time pairing exchange over non-loopback HTTP. */ + allowInsecureHttp?: boolean; +} + export interface OcxConfig { port: number; /** Runtime topology role. Absence preserves the historical standalone behavior. */ runtimeRole?: OcxRuntimeRole; + /** Hub-only public management metadata. Presence is inert outside the hub role. */ + hub?: OcxHubConfig; + /** Opt-in remote dashboard issuance policy. Presence is inert outside the hub role. */ + remoteGui?: OcxRemoteGuiConfig; /** Opt in to one identical-turn retry when a Responses completion has no text or tool call. */ emptyCompletionRetry?: boolean; /** From 3d9184d137f18dfa88f79b67238af541150311d9 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 03:02:25 +0900 Subject: [PATCH 039/172] fix(connect): preserve sync wiring contract --- src/cli/dispatch.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/cli/dispatch.ts b/src/cli/dispatch.ts index bcec92e814..964b45661e 100644 --- a/src/cli/dispatch.ts +++ b/src/cli/dispatch.ts @@ -348,10 +348,7 @@ const commandRunners: Record = { console.log(result.stale ? "Hub unavailable; retained and applied the last-known-good remote catalog (stale)." : "Remote hub catalog synchronized."); - if (result.catalogWritten || result.cacheSynced) { - afterCatalogWriteHandleAppServers({ restart: restartCodex, log: console }); - if (restartDesktopApp) await handleDesktopAppRestart(console); - } + await handleConnectedSyncCatalogWrite(result, restartCodex, restartDesktopApp); return 0; } catch (error) { console.error(`Connected sync failed without local fallback: ${error instanceof Error ? error.message : String(error)}`); @@ -903,3 +900,13 @@ async function handleDesktopAppRestart(log: Pick): Pro } } } + +async function handleConnectedSyncCatalogWrite( + result: { catalogWritten: boolean; cacheSynced: boolean }, + restartCodex: boolean, + restartDesktopApp: boolean, +): Promise { + if (!result.catalogWritten && !result.cacheSynced) return; + afterCatalogWriteHandleAppServers({ restart: restartCodex, log: console }); + if (restartDesktopApp) await handleDesktopAppRestart(console); +} From a68e95cd49418f224300bf9f303f85c38a11edb3 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 02:21:15 +0900 Subject: [PATCH 040/172] fix(remote-gui): harden identity and capability replay checks --- src/cli/gui-pair-client.ts | 6 +++++- src/server/gui-session.ts | 2 +- src/server/management-auth.ts | 8 +++++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/cli/gui-pair-client.ts b/src/cli/gui-pair-client.ts index ab00b14e79..87a164059d 100644 --- a/src/cli/gui-pair-client.ts +++ b/src/cli/gui-pair-client.ts @@ -1,4 +1,5 @@ import { readRuntimePort, type RuntimePortState } from "../config/process-state"; +import { timingSafeEqual } from "node:crypto"; import { LOCAL_ATTESTATION_CHALLENGE_HEADER, LOCAL_ATTESTATION_PROOF_HEADER, @@ -41,11 +42,14 @@ export interface GuiPairClientDeps { const GUI_PAIR_REQUEST_TIMEOUT_MS = 10_000; function sameRuntime(left: RuntimePortState, right: RuntimePortState | null): boolean { + const leftSecret = Buffer.from(left.attestationSecret ?? ""); + const rightSecret = Buffer.from(right?.attestationSecret ?? ""); return !!right?.attestationSecret && right.pid === left.pid && right.port === left.port && right.hostname === left.hostname - && right.attestationSecret === left.attestationSecret; + && leftSecret.length === rightSecret.length + && timingSafeEqual(leftSecret, rightSecret); } function canonicalHttpOrigin(value: unknown): string | null { diff --git a/src/server/gui-session.ts b/src/server/gui-session.ts index 0cf408c993..e726be0454 100644 --- a/src/server/gui-session.ts +++ b/src/server/gui-session.ts @@ -133,7 +133,7 @@ function mintSession( } function tailscaleLoginAllowed(req: Request, config: OcxConfig): boolean { - const login = req.headers.get("Tailscale-User-Login")?.trim(); + const login = req.headers.get("Tailscale-User-Login"); if (!login) return false; return (config.remoteGui?.allowedTailscaleUsers ?? []).some(user => user === login); } diff --git a/src/server/management-auth.ts b/src/server/management-auth.ts index a8c26c57a5..0bd29d0556 100644 --- a/src/server/management-auth.ts +++ b/src/server/management-auth.ts @@ -1,4 +1,4 @@ -import { randomBytes, randomUUID, timingSafeEqual } from "node:crypto"; +import { createHash, randomBytes, randomUUID, timingSafeEqual } from "node:crypto"; import { chmodSync, closeSync, @@ -435,9 +435,11 @@ function hasGuiPairCapability( for (const [consumed, retainedUntil] of consumedGuiPairCapabilities) { if (retainedUntil <= now) consumedGuiPairCapabilities.delete(consumed); } - if (!capability || consumedGuiPairCapabilities.has(capability)) return false; + if (!capability) return false; + const capabilityDigest = createHash("sha256").update(capability).digest("base64url"); + if (consumedGuiPairCapabilities.has(capabilityDigest)) return false; if (consumedGuiPairCapabilities.size >= GUI_PAIR_REPLAY_LIMIT) return false; - consumedGuiPairCapabilities.set(capability, expiresAt); + consumedGuiPairCapabilities.set(capabilityDigest, expiresAt); admittedGuiPairRequests.add(req); return true; } From 753f097d225d73933d617efdf168a29d838a9755 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 03:07:56 +0900 Subject: [PATCH 041/172] fix(connect): preserve malformed client state fail-closed --- src/client/state.ts | 5 +++-- src/config.ts | 30 +++++++++++++++++++++++++++++- tests/config.test.ts | 14 ++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/client/state.ts b/src/client/state.ts index 8710729cb8..3947b383cd 100644 --- a/src/client/state.ts +++ b/src/client/state.ts @@ -1,6 +1,7 @@ import { readFileSync } from "node:fs"; import { getConfigPath, + deleteConfigTopLevelKey, mutatePersistedConfig, readConfigDiagnostics, } from "../config"; @@ -82,8 +83,8 @@ export function clearClientConnection( if (!config.client || config.runtimeRole !== "client" || config.client.apiKeyId !== expectedApiKeyId) { return { changed: false, value: "conflict" as const }; } - delete config.client; - delete config.runtimeRole; + deleteConfigTopLevelKey(config, "client"); + deleteConfigTopLevelKey(config, "runtimeRole"); return { changed: true, value: "committed" as const }; }); if (outcome.status === "unavailable") return "conflict"; diff --git a/src/config.ts b/src/config.ts index 0b89e3580c..f2411bc217 100644 --- a/src/config.ts +++ b/src/config.ts @@ -2768,6 +2768,9 @@ export const withExpectedConfigGenerationSync: WithExpectedConfigGenerationSync */ function persistConfigUnlocked(config: OcxConfig): boolean { const configPath = getConfigPath(); + const rawBeforeWrite = readRawConfigJson(); + const clientPersistenceError = failClosedClientPersistenceError(rawBeforeWrite, config); + if (clientPersistenceError) throw new Error(clientPersistenceError); // External editors can add provider rows the live config deliberately does // not route with yet; merge them at the serialization boundary so an // unrelated in-process save cannot erase the provider or its overlay. @@ -2895,7 +2898,7 @@ export function mutatePersistedConfig( const projected = projectCustomModelCatalogMigration( commitBase.diagnostics.config, - confirmedConfig, + projectConfigRebaseProvenance(confirmedConfig), ); if (persistConfigUnlocked(projected)) bumpGenerationForCooperatingConfigWrite(); return { status: "committed", value: confirmed.value }; @@ -2904,6 +2907,31 @@ export function mutatePersistedConfig( }); } +function failClosedClientPersistenceError( + raw: Record | undefined, + candidate: OcxConfig, +): string | null { + if (!raw) return null; + const rawHasClient = Object.hasOwn(raw, "client") && raw.client !== undefined; + const rawRole = raw.runtimeRole; + const rawRoleValid = rawRole === undefined + || rawRole === "standalone" + || rawRole === "hub" + || rawRole === "client"; + const rawClientValid = !rawHasClient || clientConnectionSchema.safeParse(raw.client).success; + const rawPairValid = rawRoleValid + && ((rawRole === "client" && rawHasClient && rawClientValid) + || (rawRole !== "client" && !rawHasClient)); + if (rawPairValid) return null; + + const candidateValid = candidate.runtimeRole === "client" + && clientConnectionSchema.safeParse(candidate.client).success; + const deletions = configRebaseDeletionKeys(candidate); + const explicitClear = deletions.has("client") && deletions.has("runtimeRole"); + if (candidateValid || explicitClear) return null; + return "config write refused: malformed or mismatched remote client state must be repaired or explicitly cleared"; +} + export function websocketsEnabled(config: Pick): boolean { return config.websockets === true; } diff --git a/tests/config.test.ts b/tests/config.test.ts index 56f325236e..5cb35391be 100644 --- a/tests/config.test.ts +++ b/tests/config.test.ts @@ -357,6 +357,20 @@ describe("opencodex config defaults", () => { } }); + test("an unrelated save cannot erase malformed-present client state", () => { + const raw = { + ...getDefaultConfig(), + runtimeRole: "client", + client: { apiKeyId: "half-present", key: "must-not-be-reemitted" }, + }; + writeConfig(raw); + const before = readFileSync(getConfigPath(), "utf8"); + const loaded = loadConfig(); + loaded.codexAutoStart = false; + expect(() => saveConfig(loaded)).toThrow("malformed or mismatched remote client state"); + expect(readFileSync(getConfigPath(), "utf8")).toBe(before); + }); + test("malformed classifier config is normalized at load, even with subagentEffort absent (#1697)", () => { // normalizePersistedClaudeCode used to be reached only through a subagentEffort short-circuit, // so a config whose ONLY defect was elsewhere in claudeCode was never normalized. These From 8906173b46cb65b3c14fe09efa2d91b61969997b Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 02:22:23 +0900 Subject: [PATCH 042/172] test(remote-gui): cover remote session consent boundaries --- gui/tests/api-auth-deadline.test.ts | 1 + gui/tests/api-auth-memory.test.ts | 82 +++++- tests/cli-dispatch.test.ts | 58 ++++- tests/cli-help.test.ts | 10 + tests/cli-registry.test.ts | 5 + tests/config.test.ts | 94 +++++++ tests/gui-management-session.test.ts | 1 + tests/gui-pair-capability.test.ts | 66 +++++ tests/gui-pair-client.test.ts | 135 ++++++++++ tests/native-profile-route-security.test.ts | 50 ++++ tests/proxy-liveness.test.ts | 18 ++ tests/server-auth.test.ts | 31 +++ tests/server-live.test.ts | 28 +++ tests/server-management-auth.test.ts | 264 +++++++++++++++++++- tests/sidebar-routes.test.ts | 16 +- 15 files changed, 850 insertions(+), 9 deletions(-) create mode 100644 tests/gui-pair-capability.test.ts create mode 100644 tests/gui-pair-client.test.ts diff --git a/gui/tests/api-auth-deadline.test.ts b/gui/tests/api-auth-deadline.test.ts index de4520ad88..a56f3c1ea7 100644 --- a/gui/tests/api-auth-deadline.test.ts +++ b/gui/tests/api-auth-deadline.test.ts @@ -52,6 +52,7 @@ function sessionDocumentHtml(token: string, csrf: string, origin: string): strin ``, ``, ``, + ``, "", ].join(""); } diff --git a/gui/tests/api-auth-memory.test.ts b/gui/tests/api-auth-memory.test.ts index ca70303e26..d5e77f9d34 100644 --- a/gui/tests/api-auth-memory.test.ts +++ b/gui/tests/api-auth-memory.test.ts @@ -352,11 +352,12 @@ test("data-plane requests never receive the management token or prompt", async ( expect(promptCalls).toBe(beforeCrossPrompts); }); -function injectSessionMeta(token: string, csrf: string, origin: string): void { +function injectSessionMeta(token: string, csrf: string, browserOrigin: string, serverOrigin = browserOrigin): void { for (const [name, content] of [ ["opencodex-session-token", token], ["opencodex-session-csrf", csrf], - ["opencodex-session-origin", origin], + ["opencodex-session-origin", browserOrigin], + ["opencodex-session-server-origin", serverOrigin], ] as const) { const meta = document.createElement("meta"); meta.setAttribute("name", name); @@ -365,16 +366,23 @@ function injectSessionMeta(token: string, csrf: string, origin: string): void { } } -function sessionDocumentHtml(token: string, csrf: string, origin: string): string { +function sessionDocumentHtml(token: string, csrf: string, browserOrigin: string, serverOrigin = browserOrigin): string { return [ "", ``, ``, - ``, + ``, + ``, "", ].join(""); } +function htmlResponseAt(html: string, url: string): Response { + const response = new Response(html, { status: 200, headers: { "Content-Type": "text/html" } }); + Object.defineProperty(response, "url", { configurable: true, value: url }); + return response; +} + test("expired session silently re-bootstraps from the served document without prompting", async () => { // Regression for the post-security-hardening UX bug: loopback sessions expire after the // 5-minute TTL (or die on proxy restart), and the dashboard used to demand an admin token @@ -446,3 +454,69 @@ test("a session minted for another origin is rejected and the prompt fallback st expect(res.status).toBe(200); expect(promptCalls).toBe(1); }); + +test("a renewed two-origin session attaches only to its bound server and carries browser origin plus CSRF", async () => { + injectSessionMeta("ocx_session_stale", "stale-csrf", "http://localhost"); + const seen = new Map(); + let localApiCalls = 0; + const record = (origin: string, headers: Headers) => { + const entries = seen.get(origin) ?? []; + entries.push(headers); + seen.set(origin, entries); + }; + const mockFetch = (async (input: RequestInfo | URL, init?: RequestInit) => { + const url = new URL(input instanceof Request ? input.url : String(input), "http://localhost/"); + const headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined)); + if (url.pathname === "/opencodex-session") { + return htmlResponseAt( + sessionDocumentHtml("ocx_session_remote", "remote-csrf", "http://localhost", "https://hub.example.test"), + "https://hub.example.test/opencodex-session", + ); + } + record(url.origin, headers); + if (url.origin === "http://localhost") { + localApiCalls += 1; + return new Response("{}", { status: localApiCalls === 1 ? 401 : 200 }); + } + return new Response("{}", { status: 200 }); + }) as typeof fetch; + await installMockAuthFetch(mockFetch); + + expect((await fetch("/api/config")).status).toBe(200); + expect((await fetch("https://hub.example.test/api/config", { method: "POST" })).status).toBe(200); + expect((await fetch("https://evil.example.test/api/config")).status).toBe(200); + + const hubHeaders = seen.get("https://hub.example.test")?.[0]; + expect(hubHeaders?.get("X-OpenCodex-API-Key")).toBe("ocx_session_remote"); + expect(hubHeaders?.get("X-OpenCodex-GUI-Origin")).toBe("http://localhost"); + expect(hubHeaders?.get("X-OpenCodex-CSRF-Token")).toBe("remote-csrf"); + const evilHeaders = seen.get("https://evil.example.test")?.[0]; + expect(evilHeaders?.get("X-OpenCodex-API-Key")).toBeNull(); + expect(evilHeaders?.get("X-OpenCodex-GUI-Origin")).toBeNull(); +}); + +test("a mismatched bootstrap response/server origin clears every in-memory session field", async () => { + injectSessionMeta("ocx_session_stale", "stale-csrf", "http://localhost"); + const seenKeys: Array = []; + let apiCalls = 0; + const mockFetch = (async (input: RequestInfo | URL, init?: RequestInit) => { + const url = new URL(input instanceof Request ? input.url : String(input), "http://localhost/"); + if (url.pathname === "/opencodex-session") { + return htmlResponseAt( + sessionDocumentHtml("ocx_session_rejected", "new-csrf", "http://localhost", "https://evil.example.test"), + "https://hub.example.test/opencodex-session", + ); + } + const headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined)); + seenKeys.push(headers.get("X-OpenCodex-API-Key")); + apiCalls += 1; + return new Response("unauthorized", { status: 401 }); + }) as typeof fetch; + await installMockAuthFetch(mockFetch); + + expect((await fetch("/api/config")).status).toBe(401); + expect(apiCalls).toBe(1); + expect((await fetch("https://hub.example.test/api/config")).status).toBe(401); + expect(seenKeys).toEqual(["ocx_session_stale", null]); + expect(sessionStorage.getItem(LEGACY_TOKEN_KEY)).toBeNull(); +}); diff --git a/tests/cli-dispatch.test.ts b/tests/cli-dispatch.test.ts index a4ac1084ed..0ef0f26742 100644 --- a/tests/cli-dispatch.test.ts +++ b/tests/cli-dispatch.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, test } from "bun:test"; +import { describe, expect, spyOn, test } from "bun:test"; import { CLI_COMMANDS } from "../src/cli/registry"; import { DISPATCH_ALIASES, DISPATCH_COMMANDS, dispatchCommand, resolveDispatchCommand } from "../src/cli/dispatch"; import type { CliDispatchDeps } from "../src/cli/dispatch"; @@ -6,6 +6,7 @@ import { existsSync, readFileSync } from "node:fs"; import { join } from "node:path"; import { getConfigDir } from "../src/config"; import { getAccountSet, removeCredential, saveCredential } from "../src/oauth/store"; +import { runGuiCommand } from "../src/cli/gui"; /** Minimal fake deps. dispatchCommand only touches deps for real command * runners, which these tests never invoke, so an empty object is enough. */ @@ -481,6 +482,61 @@ describe("doctor refuses --json rather than printing prose as success", () => { test("exact --json, --json=true, and Unicode dashes all exit 2", async () => { for (const flag of ["--json", "--json=true", "\u2014json"]) { expect(await runDoctor(flag), `${JSON.stringify(flag)} must be refused`).toBe(2); +describe("GUI command delegation", () => { + const config = { + port: 10100, + runtimeRole: "hub" as const, + hub: { managementPublicOrigin: "https://hub.example.test" }, + corsAllowOrigins: ["https://dashboard.example.test"], + providers: {}, + defaultProvider: "openai", + }; + + test("keeps the default open behavior and requires an explicit pairing origin", async () => { + let opens = 0; + const deps = { + loadConfig: () => config, + openDefaultGui: async () => { opens += 1; return 0; }, + }; + expect(await runGuiCommand([], deps)).toBe(0); + expect(opens).toBe(1); + expect(await runGuiCommand(["pair"], deps)).toBe(1); + expect(await runGuiCommand(["pair", "--origin", "https://dashboard.example.test", "extra"], deps)).toBe(1); + }); + + test("prints a created grant once and maps remote API refusal to exit 1 without echoing response data", async () => { + const stdout: string[] = []; + const stderr: string[] = []; + const logSpy = spyOn(console, "log").mockImplementation(value => { stdout.push(String(value)); }); + const errorSpy = spyOn(console, "error").mockImplementation(value => { stderr.push(String(value)); }); + try { + const base = { + loadConfig: () => config, + openDefaultGui: async () => 0, + findLiveProxy: async () => ({ pid: 4242, port: 10100, source: "runtime" as const }), + }; + const grant = `ocx_pair_${"C".repeat(43)}`; + expect(await runGuiCommand(["pair", "--origin", "https://dashboard.example.test", "--json"], { + ...base, + requestPairingGrant: async () => ({ + kind: "created", + grant, + browserOrigin: "https://dashboard.example.test", + serverOrigin: "https://hub.example.test", + expiresAt: 1_800_000_300_000, + }), + })).toBe(0); + expect(stdout.join(" ").split(grant)).toHaveLength(2); + + stdout.length = 0; + expect(await runGuiCommand(["pair", "--origin", "https://dashboard.example.test"], { + ...base, + requestPairingGrant: async () => ({ kind: "unavailable", reason: "rejected" }), + })).toBe(1); + expect(`${stdout.join(" ")} ${stderr.join(" ")}`).not.toContain("remote-response-secret"); + } finally { + logSpy.mockRestore(); + errorSpy.mockRestore(); } }); }); diff --git a/tests/cli-help.test.ts b/tests/cli-help.test.ts index 1edfb67a99..ef0a475116 100644 --- a/tests/cli-help.test.ts +++ b/tests/cli-help.test.ts @@ -128,6 +128,16 @@ describe("CLI subcommand help", () => { expect(result.stdout).toContain("--no-start"); }); + test("GUI help documents explicit-origin pairing without making a live request", () => { + const result = runCli(["help", "gui"]); + expectSpawnFinished(result, "ocx help gui"); + expect(result.status).toBe(0); + expect(result.stderr).toBe(""); + expect(result.stdout).toContain("Usage: ocx gui [pair --origin [--json]]"); + expect(result.stdout).toContain("single-use"); + expect(result.stdout).toContain("must not be persisted"); + }); + test("unknown command with help flag remains an error", () => { const result = runCli(["foobar", "--help"]); expectSpawnFinished(result, "ocx foobar --help"); diff --git a/tests/cli-registry.test.ts b/tests/cli-registry.test.ts index 4accf73048..70653fae24 100644 --- a/tests/cli-registry.test.ts +++ b/tests/cli-registry.test.ts @@ -103,6 +103,11 @@ describe("CLI command registry parity", () => { const details = findCommand("system")?.details ?? []; expect(details).toContain("ocx system codex-cli-update check [--json]"); expect(details.some(line => line.includes("dry-run"))).toBe(false); + test("GUI registry usage documents explicit-origin single-use pairing", () => { + const gui = findCommand("gui"); + expect(gui?.usage).toBe("ocx gui [pair --origin [--json]]"); + expect(gui?.details?.join(" ")).toContain("single-use"); + expect(gui?.details?.join(" ")).toContain("no localhost or config-derived default"); }); }); diff --git a/tests/config.test.ts b/tests/config.test.ts index de026c8dee..aba5aaf9c7 100644 --- a/tests/config.test.ts +++ b/tests/config.test.ts @@ -178,6 +178,100 @@ describe("opencodex config defaults", () => { } }); + test("hub and remote GUI config normalize valid origins and exact Tailscale users", () => { + expect(validateConfigCandidate({ + ...getDefaultConfig(), + runtimeRole: "hub", + hub: { managementPublicOrigin: "https://hub.example.test:443" }, + remoteGui: { + allowedTailscaleUsers: [" alice@example.test ", "bob@example.test"], + allowInsecureHttp: false, + }, + })).toMatchObject({ + ok: true, + config: { + hub: { managementPublicOrigin: "https://hub.example.test" }, + remoteGui: { allowedTailscaleUsers: ["alice@example.test", "bob@example.test"] }, + }, + }); + expect(validateConfigCandidate({ + ...getDefaultConfig(), + runtimeRole: "hub", + hub: { managementPublicOrigin: "http://hub.example.test" }, + remoteGui: { allowInsecureHttp: true }, + }).ok).toBe(true); + }); + + test("remote GUI live candidates reject unsafe origins and malformed identity allowlists", () => { + for (const managementPublicOrigin of [ + "ftp://hub.example.test", + "https://user@hub.example.test", + "https://hub.example.test/path", + "https://hub.example.test/?query=1", + "https://hub.example.test/#fragment", + ]) { + const result = validateConfigCandidate({ + ...getDefaultConfig(), + hub: { managementPublicOrigin }, + }); + expect(result.ok).toBe(false); + if (!result.ok) expect(result.error).toContain("hub.managementPublicOrigin"); + } + for (const allowedTailscaleUsers of [ + [""], + ["alice@example.test", " alice@example.test "], + ["alice\n@example.test"], + ["x".repeat(321)], + Array.from({ length: 65 }, (_, index) => `user-${index}@example.test`), + ]) { + const result = validateConfigCandidate({ + ...getDefaultConfig(), + remoteGui: { allowedTailscaleUsers }, + }); + expect(result.ok).toBe(false); + if (!result.ok) expect(result.error).toContain("remoteGui.allowedTailscaleUsers"); + } + }); + + test("a malformed persisted remote GUI block is disabled without discarding providers or API keys", () => { + const malformedValue = "https://hub.example.test/private-secret-path"; + writeConfig({ + port: 12345, + runtimeRole: "hub", + hub: { managementPublicOrigin: malformedValue }, + remoteGui: { allowedTailscaleUsers: ["alice@example.test"] }, + defaultProvider: "custom", + providers: { custom: { adapter: "openai-chat", baseUrl: "https://example.test/v1", apiKey: "upstream-secret" } }, + apiKeys: [{ id: "key-1", name: "default", key: "ocx_persisted", createdAt: "2026-08-28T00:00:00.000Z" }], + }); + const warnSpy = spyOn(console, "warn").mockImplementation(() => {}); + try { + const loaded = loadConfig(); + expect(loaded.hub).toBeUndefined(); + expect(loaded.remoteGui).toEqual({ allowedTailscaleUsers: ["alice@example.test"] }); + expect(loaded.providers.custom?.apiKey).toBe("upstream-secret"); + expect(loaded.apiKeys?.[0]?.key).toBe("ocx_persisted"); + expect(readConfigDiagnostics().warnings?.join(" ")).toContain("hub.managementPublicOrigin"); + expect(warnSpy.mock.calls.flat().join(" ")).not.toContain(malformedValue); + expect(backupNames()).toEqual([]); + } finally { + warnSpy.mockRestore(); + } + }); + + test("remote GUI config round-trips but remains inert outside the hub role", () => { + for (const runtimeRole of [undefined, "standalone", "client"] as const) { + const result = validateConfigCandidate({ + ...getDefaultConfig(), + ...(runtimeRole ? { runtimeRole } : {}), + hub: { managementPublicOrigin: "https://hub.example.test" }, + remoteGui: { allowedTailscaleUsers: ["alice@example.test"], allowInsecureHttp: true }, + }); + expect(result.ok).toBe(true); + if (result.ok) expect(result.config.runtimeRole).toBe(runtimeRole); + } + }); + test("malformed classifier config is normalized at load, even with subagentEffort absent (#1697)", () => { // normalizePersistedClaudeCode used to be reached only through a subagentEffort short-circuit, // so a config whose ONLY defect was elsewhere in claudeCode was never normalized. These diff --git a/tests/gui-management-session.test.ts b/tests/gui-management-session.test.ts index 44e74cc3d1..a6f32aa5db 100644 --- a/tests/gui-management-session.test.ts +++ b/tests/gui-management-session.test.ts @@ -21,6 +21,7 @@ describe("GUI management session bootstrap", () => { ["opencodex-session-token", "ocx_session_browser-secret"], ["opencodex-session-csrf", "csrf-browser-secret"], ["opencodex-session-origin", "http://localhost:10100"], + ["opencodex-session-server-origin", "http://localhost:10100"], ]); const fetchImpl = async (input: RequestInfo | URL, init?: RequestInit): Promise => { seen.push({ diff --git a/tests/gui-pair-capability.test.ts b/tests/gui-pair-capability.test.ts new file mode 100644 index 0000000000..cab507afb9 --- /dev/null +++ b/tests/gui-pair-capability.test.ts @@ -0,0 +1,66 @@ +import { describe, expect, test } from "bun:test"; +import { + GUI_PAIR_CAPABILITY_TTL_MS, + GUI_PAIR_METHOD, + GUI_PAIR_PATH, + createGuiPairCapability, + verifyGuiPairCapability, +} from "../src/lib/gui-pair-capability"; + +const SECRET = "A".repeat(43); +const NONCE = "B".repeat(43); +const ORIGIN = "https://dashboard.example.test"; +const PID = 4242; +const PORT = 10100; +const NOW = 1_800_000_000_000; +const EXPIRES_AT = NOW + GUI_PAIR_CAPABILITY_TTL_MS; + +function capability(): string { + const value = createGuiPairCapability( + SECRET, + NONCE, + GUI_PAIR_METHOD, + GUI_PAIR_PATH, + ORIGIN, + PID, + PORT, + EXPIRES_AT, + ); + if (!value) throw new Error("test GUI pair capability could not be created"); + return value; +} + +describe("GUI pairing operation capability", () => { + test("authenticates the exact method path browser origin process and listener", () => { + expect(verifyGuiPairCapability( + SECRET, NONCE, GUI_PAIR_METHOD, GUI_PAIR_PATH, ORIGIN, PID, PORT, EXPIRES_AT, capability(), NOW, + )).toBe(true); + for (const changed of [ + ["DELETE", GUI_PAIR_PATH, ORIGIN, PID, PORT], + [GUI_PAIR_METHOD, "/api/config", ORIGIN, PID, PORT], + [GUI_PAIR_METHOD, GUI_PAIR_PATH, "https://evil.example.test", PID, PORT], + [GUI_PAIR_METHOD, GUI_PAIR_PATH, ORIGIN, PID + 1, PORT], + [GUI_PAIR_METHOD, GUI_PAIR_PATH, ORIGIN, PID, PORT + 1], + ] as const) { + expect(verifyGuiPairCapability( + SECRET, NONCE, changed[0], changed[1], changed[2], changed[3], changed[4], EXPIRES_AT, capability(), NOW, + )).toBe(false); + } + }); + + test("rejects malformed nonce expiry origin and same-length signature mismatches", () => { + const mismatched = `${capability().slice(0, -1)}${capability().endsWith("C") ? "D" : "C"}`; + expect(verifyGuiPairCapability( + SECRET, "short", GUI_PAIR_METHOD, GUI_PAIR_PATH, ORIGIN, PID, PORT, EXPIRES_AT, capability(), NOW, + )).toBe(false); + expect(verifyGuiPairCapability( + SECRET, NONCE, GUI_PAIR_METHOD, GUI_PAIR_PATH, "https://dashboard.example.test/path", PID, PORT, EXPIRES_AT, capability(), NOW, + )).toBe(false); + expect(verifyGuiPairCapability( + SECRET, NONCE, GUI_PAIR_METHOD, GUI_PAIR_PATH, ORIGIN, PID, PORT, NOW, capability(), NOW, + )).toBe(false); + expect(verifyGuiPairCapability( + SECRET, NONCE, GUI_PAIR_METHOD, GUI_PAIR_PATH, ORIGIN, PID, PORT, EXPIRES_AT, mismatched, NOW, + )).toBe(false); + }); +}); diff --git a/tests/gui-pair-client.test.ts b/tests/gui-pair-client.test.ts new file mode 100644 index 0000000000..3252af9dba --- /dev/null +++ b/tests/gui-pair-client.test.ts @@ -0,0 +1,135 @@ +import { describe, expect, test } from "bun:test"; +import { requestBoundGuiPairingGrant } from "../src/cli/gui-pair-client"; +import { + LOCAL_ATTESTATION_CHALLENGE_HEADER, + LOCAL_ATTESTATION_PROOF_HEADER, + createLocalAttestationProof, +} from "../src/lib/local-management-attestation"; +import { + GUI_PAIR_BROWSER_ORIGIN_HEADER, + GUI_PAIR_CAPABILITY_HEADER, + GUI_PAIR_CAPABILITY_VERSION, + GUI_PAIR_PATH, + verifyGuiPairCapability, +} from "../src/lib/gui-pair-capability"; +import type { LiveProxy } from "../src/server/proxy-liveness"; + +const secret = "A".repeat(43); +const nonce = "B".repeat(43); +const browserOrigin = "https://dashboard.example.test"; +const target: LiveProxy = { pid: 4242, port: 10100, hostname: "127.0.0.1", source: "runtime" }; + +function proofResponse(init?: RequestInit, capabilityVersion: unknown = GUI_PAIR_CAPABILITY_VERSION): Response { + const challenge = new Headers(init?.headers).get(LOCAL_ATTESTATION_CHALLENGE_HEADER)!; + return Response.json({ + service: "opencodex", + status: "ok", + version: "test", + uptime: 1, + pid: target.pid, + port: target.port, + guiPairCapability: capabilityVersion, + }, { + headers: { + [LOCAL_ATTESTATION_PROOF_HEADER]: createLocalAttestationProof(secret, challenge, target.pid!, target.port)!, + }, + }); +} + +describe("GUI pairing client", () => { + test("refuses unattested targets and unsupported capability versions before POST", async () => { + let calls = 0; + expect(await requestBoundGuiPairingGrant( + { ...target, source: "config" }, browserOrigin, + { fetchImpl: async () => { calls += 1; return new Response(); } }, + )).toEqual({ kind: "unavailable", reason: "unattested-target" }); + expect(calls).toBe(0); + + const result = await requestBoundGuiPairingGrant(target, browserOrigin, { + readRuntime: () => ({ ...target, attestationSecret: secret }), + createChallenge: () => nonce, + fetchImpl: async (_input, init) => { + calls += 1; + return proofResponse(init, "v0"); + }, + }); + expect(result).toEqual({ kind: "unavailable", reason: "capability" }); + expect(calls).toBe(1); + }); + + test("rechecks PID and port after proof", async () => { + let reads = 0; + let calls = 0; + const result = await requestBoundGuiPairingGrant(target, browserOrigin, { + readRuntime: () => { + reads += 1; + return reads === 1 + ? { ...target, attestationSecret: secret } + : { ...target, port: target.port + 1, attestationSecret: secret }; + }, + createChallenge: () => nonce, + fetchImpl: async (_input, init) => { + calls += 1; + return proofResponse(init); + }, + }); + expect(result).toEqual({ kind: "unavailable", reason: "runtime-mismatch" }); + expect(calls).toBe(1); + }); + + test("sends one bodyless origin-bound capability and redacts rejected bodies", async () => { + const requests: Array<{ url: string; init?: RequestInit }> = []; + const now = 1_800_000_000_000; + const result = await requestBoundGuiPairingGrant(target, browserOrigin, { + readRuntime: () => ({ ...target, attestationSecret: secret }), + createChallenge: () => nonce, + now: () => now, + fetchImpl: async (input, init) => { + requests.push({ url: String(input), init }); + if (requests.length === 1) return proofResponse(init); + return Response.json({ + grant: `ocx_pair_${"C".repeat(43)}`, + browserOrigin, + serverOrigin: "https://hub.example.test", + expiresAt: now + 300_000, + }); + }, + }); + expect(result).toEqual({ + kind: "created", + grant: `ocx_pair_${"C".repeat(43)}`, + browserOrigin, + serverOrigin: "https://hub.example.test", + expiresAt: now + 300_000, + }); + expect(requests).toHaveLength(2); + expect(requests[1]!.url).toBe(`http://127.0.0.1:10100${GUI_PAIR_PATH}`); + expect(requests[1]!.init?.body).toBeUndefined(); + const headers = new Headers(requests[1]!.init?.headers); + expect(headers.get(GUI_PAIR_BROWSER_ORIGIN_HEADER)).toBe(browserOrigin); + expect(headers.has("authorization")).toBe(false); + expect(headers.has("x-opencodex-api-key")).toBe(false); + expect(verifyGuiPairCapability( + secret, + nonce, + "POST", + GUI_PAIR_PATH, + browserOrigin, + target.pid!, + target.port, + Number(headers.get("x-opencodex-gui-pair-expires-at")), + headers.get(GUI_PAIR_CAPABILITY_HEADER), + now, + )).toBe(true); + + const rejected = await requestBoundGuiPairingGrant(target, browserOrigin, { + readRuntime: () => ({ ...target, attestationSecret: secret }), + createChallenge: () => nonce, + fetchImpl: async (_input, init) => init?.method + ? Response.json({ grant: "secret-must-not-surface" }, { status: 403 }) + : proofResponse(init), + }); + expect(rejected).toEqual({ kind: "unavailable", reason: "rejected" }); + expect(JSON.stringify(rejected)).not.toContain("secret-must-not-surface"); + }); +}); diff --git a/tests/native-profile-route-security.test.ts b/tests/native-profile-route-security.test.ts index 7542fa6e50..104c985c80 100644 --- a/tests/native-profile-route-security.test.ts +++ b/tests/native-profile-route-security.test.ts @@ -6,6 +6,7 @@ import { saveConfig } from "../src/config"; import type { NativeProfileManager } from "../src/codex/native-profile-manager"; import { startServer } from "../src/server"; import { initializeManagementAuthState, issueGuiSession, type ManagementAuthState } from "../src/server/management-auth"; +import { consumeGuiPairingGrant, createGuiPairingGrant } from "../src/server/gui-session"; import type { OcxConfig } from "../src/types"; import { SERVER_BUDGET_MS } from "./helpers/test-budget"; @@ -164,4 +165,53 @@ describe("native-main profile routes at the management admission boundary", () = await server.stop(true); } }, SERVER_BUDGET_MS); + + test("remote GUI sessions require the bound server, browser origin, and CSRF before native mutation dispatch", async () => { + const config: OcxConfig = { + ...loopbackConfig(), + hostname: "0.0.0.0", + runtimeRole: "hub", + hub: { managementPublicOrigin: "https://hub.example.test" }, + remoteGui: {}, + corsAllowOrigins: ["https://dashboard.example.test"], + apiKeys: [{ id: "data", name: "data", key: "data-secret", createdAt: "2026-08-28T00:00:00.000Z" }], + }; + saveConfig(config); + const calls: string[] = []; + const managementAuth = initializeManagementAuthState(config); + if (!managementAuth.available) throw new Error("expected management auth state"); + const grant = createGuiPairingGrant("https://dashboard.example.test", config, managementAuth); + const session = consumeGuiPairingGrant(new Request("https://hub.example.test/opencodex-session", { + method: "POST", + headers: { Host: "hub.example.test", Origin: "https://dashboard.example.test" }, + }), { grant: grant.grant }, config, managementAuth); + if (!session) throw new Error("expected remote GUI session"); + const server = startServer(0, { + managementAuthState: managementAuth, + managementApi: { nativeProfileApi: { manager: testManager(calls) } }, + }); + try { + const operation = operations.find(candidate => candidate.method === "POST")!; + const request = (headers: Record) => fetch(new URL(operation.path, server.url), { + method: "POST", + headers: { "content-type": "application/json", Host: "hub.example.test", ...headers }, + body: operation.body ? JSON.stringify(operation.body) : undefined, + }); + const base = { + Origin: "https://dashboard.example.test", + "x-opencodex-api-key": session.token, + "x-opencodex-gui-origin": "https://dashboard.example.test", + "x-opencodex-csrf-token": session.csrfToken, + }; + expect((await request({ ...base, "x-opencodex-gui-origin": "https://evil.example.test" })).status).toBe(401); + expect((await request({ ...base, Origin: "https://evil.example.test" })).status).toBe(401); + expect((await request({ ...base, Host: server.url.host })).status).toBe(401); + expect((await request({ ...base, "x-opencodex-csrf-token": "" })).status).toBe(401); + expect(calls).toEqual([]); + expect((await request(base)).status).toBe(200); + expect(calls).toEqual([operation.name]); + } finally { + await server.stop(true); + } + }, SERVER_BUDGET_MS); }); diff --git a/tests/proxy-liveness.test.ts b/tests/proxy-liveness.test.ts index 7a00da7077..52c2b91919 100644 --- a/tests/proxy-liveness.test.ts +++ b/tests/proxy-liveness.test.ts @@ -27,6 +27,7 @@ const OURS = { status: "ok", service: "opencodex", version: "2.6.17", uptime: 12 describe("isOpencodexHealthz", () => { test("accepts the explicit service marker", () => { expect(isOpencodexHealthz(OURS)).toBe(true); + expect(isOpencodexHealthz({ ...OURS, guiPairCapability: "v1" })).toBe(true); }); test("accepts the legacy pre-identity body (still-running old proxy after update)", () => { @@ -38,6 +39,7 @@ describe("isOpencodexHealthz", () => { expect(isOpencodexHealthz({ status: "ok" })).toBe(false); expect(isOpencodexHealthz({ service: "something-else", status: "ok", version: "1", uptime: 1 })).toBe(false); expect(isOpencodexHealthz({ healthy: true } as never)).toBe(false); + expect(isOpencodexHealthz({ guiPairCapability: "v1", pid: 4242, port: 10100 })).toBe(false); }); }); @@ -671,6 +673,22 @@ describe("remote readiness protocol metadata", () => { }); }); + test("configured hub management origin wins while other roles keep the observed fallback", () => { + const request = new Request("http://127.0.0.1/readyz", { + headers: { Host: "observed.example.test:8443" }, + }); + expect(readyProtocolMetadata({ + ...getDefaultConfig(), + runtimeRole: "hub", + hub: { managementPublicOrigin: "https://hub.example.test:443" }, + }, request).managementUrl).toBe("https://hub.example.test"); + expect(readyProtocolMetadata({ + ...getDefaultConfig(), + runtimeRole: "client", + hub: { managementPublicOrigin: "https://ignored.example.test" }, + }, request).managementUrl).toBe("http://observed.example.test:8443"); + }); + test("classifies a hub that requires a newer client with the exact message", () => { expect(checkRemoteProtocolCompatibility({ ...metadata, protocol: 2, minimumClientProtocol: 2 })).toEqual({ ok: false, diff --git a/tests/server-auth.test.ts b/tests/server-auth.test.ts index e4030f8a59..5d4b95d104 100644 --- a/tests/server-auth.test.ts +++ b/tests/server-auth.test.ts @@ -46,6 +46,7 @@ import { ownedServiceHomeInspection } from "./helpers/owned-service-home-inspect import { configuredAdminToken } from "../src/lib/admin-secrets"; import { SYSTEM_RESTART_CAPABILITY_VERSION } from "../src/lib/system-restart-contract"; import { LOCAL_PROVIDER_RELOAD_CAPABILITY_VERSION } from "../src/lib/local-provider-reload-contract"; +import { GUI_PAIR_CAPABILITY_VERSION } from "../src/lib/gui-pair-capability"; import { resetCodexModelEntitlementCacheForTests } from "../src/codex/model-entitlements"; import { getDebugLogEntries, resetDebugLogBufferForTests } from "../src/lib/debug-log-buffer"; import { resetDebugSettingsForTests, setDebugSettings } from "../src/lib/debug-settings"; @@ -1071,6 +1072,7 @@ describe("server local API auth", () => { expect(health.status).toBe(200); const healthBody = await health.json() as Record; expect(Object.keys(healthBody).sort()).toEqual([ + "guiPairCapability", "pid", "port", "providerReloadCapability", @@ -1082,6 +1084,7 @@ describe("server local API auth", () => { ]); expect(healthBody.restartCapability).toBe(SYSTEM_RESTART_CAPABILITY_VERSION); expect(healthBody.providerReloadCapability).toBe(LOCAL_PROVIDER_RELOAD_CAPABILITY_VERSION); + expect(healthBody.guiPairCapability).toBe(GUI_PAIR_CAPABILITY_VERSION); expect("rss" in healthBody).toBe(false); } finally { await server.stop(true); @@ -1115,6 +1118,10 @@ describe("server local API auth", () => { }); expect(accepted.status).toBe(204); expect(accepted.headers.get("access-control-allow-origin")).toBe(loopbackOrigin); + const allowedHeaders = accepted.headers.get("access-control-allow-headers") ?? ""; + expect(allowedHeaders).toContain("X-OpenCodex-GUI-Origin"); + expect(allowedHeaders).toContain("X-OpenCodex-CSRF-Token"); + expect(allowedHeaders).not.toContain("X-Unrelated-Custom-Header"); } finally { await server.stop(true); } @@ -1165,6 +1172,30 @@ describe("server local API auth", () => { }); expect(managementPreflight.status).toBe(204); expect(managementPreflight.headers.get("access-control-allow-origin")).toBe(extensionOrigin); + expect(managementPreflight.headers.get("access-control-allow-headers")).toContain("X-OpenCodex-GUI-Origin"); + expect(managementPreflight.headers.get("access-control-allow-headers")).toContain("X-OpenCodex-CSRF-Token"); + + const managementUnrelated = await fetch(managementUrl, { + method: "OPTIONS", + headers: { + origin: extensionOrigin, + "access-control-request-method": "GET", + "access-control-request-headers": "X-Unrelated-Custom-Header", + }, + }); + expect(managementUnrelated.status).toBe(204); + expect(managementUnrelated.headers.get("access-control-allow-headers")).not.toContain("X-Unrelated-Custom-Header"); + + const dataPlaneDynamic = await fetch(modelsUrl, { + method: "OPTIONS", + headers: { + origin: extensionOrigin, + "access-control-request-method": "GET", + "access-control-request-headers": "X-Unrelated-Custom-Header", + }, + }); + expect(dataPlaneDynamic.status).toBe(204); + expect(dataPlaneDynamic.headers.get("access-control-allow-headers")).toContain("X-Unrelated-Custom-Header"); const managementRejected = await fetch(managementUrl, { method: "OPTIONS", diff --git a/tests/server-live.test.ts b/tests/server-live.test.ts index 53418c4627..7dda68a77a 100644 --- a/tests/server-live.test.ts +++ b/tests/server-live.test.ts @@ -1292,6 +1292,34 @@ describe("GET /readyz", () => { } }); + test("hub readiness prefers the configured public management origin over the observed listener", async () => { + saveConfig({ + ...forwardConfig(), + runtimeRole: "hub", + hub: { managementPublicOrigin: "https://hub.example.test" }, + }); + const server = startServer(0); + try { + const ready = await fetch(new URL("/readyz", server.url), { + headers: { + Host: "observed.example.test:8443", + "X-Forwarded-Host": "attacker.example.test", + "X-Forwarded-Proto": "http", + }, + }); + const body = await ready.json() as Record; + expectReadyProtocolMetadata(body, "https://hub.example.test"); + + const health = await fetch(new URL("/healthz", server.url)); + const healthBody = await health.json() as Record; + expect(healthBody.guiPairCapability).toBe("v1"); + expect(JSON.stringify(healthBody)).not.toContain("ocx_session_"); + expect(JSON.stringify(healthBody)).not.toContain("csrf"); + } finally { + await server.stop(true); + } + }); + test("/readyz is 200 with status ready only after gate.markReady()", async () => { saveConfig(forwardConfig()); const gate = createReadinessGate(); diff --git a/tests/server-management-auth.test.ts b/tests/server-management-auth.test.ts index 335392ea8d..3f5197db61 100644 --- a/tests/server-management-auth.test.ts +++ b/tests/server-management-auth.test.ts @@ -62,6 +62,25 @@ import { createLocalProviderReloadCapability, verifyLocalProviderReloadCapability, } from "../src/lib/local-provider-reload-contract"; +import { + GUI_PAIR_BROWSER_ORIGIN_HEADER, + GUI_PAIR_CAPABILITY_HEADER, + GUI_PAIR_CAPABILITY_TTL_MS, + GUI_PAIR_EXPECTED_PID_HEADER, + GUI_PAIR_EXPIRES_AT_HEADER, + GUI_PAIR_METHOD, + GUI_PAIR_NONCE_HEADER, + GUI_PAIR_PATH, + createGuiPairCapability, +} from "../src/lib/gui-pair-capability"; +import { + GUI_PAIRING_GRANT_TTL_MS, + LOOPBACK_GUI_SESSION_TTL_MS, + REMOTE_GUI_SESSION_TTL_MS, + authorizeGuiSessionRequest, + consumeGuiPairingGrant, + createGuiPairingGrant, +} from "../src/server/gui-session"; import { setSystemRestartIoForTests } from "../src/server/management/system-restart"; const previousHome = process.env.OPENCODEX_HOME; @@ -85,6 +104,16 @@ function remoteConfig(): OcxConfig { }; } +function hubConfig(publicOrigin = "https://hub.example.test"): OcxConfig { + return { + ...remoteConfig(), + runtimeRole: "hub", + hub: { managementPublicOrigin: publicOrigin }, + remoteGui: { allowedTailscaleUsers: ["alice@example.test"] }, + corsAllowOrigins: ["https://dashboard.example.test"], + }; +} + function websocketHandshakeOpens(url: URL, token: string): Promise { return new Promise(resolve => { const target = new URL("/v1/responses", url); @@ -794,8 +823,15 @@ describe("management and data-plane credential separation", () => { const pageRequest = new Request("http://localhost:10100/", { headers: { Host: "localhost:10100" }, }); - const session = issueGuiSession(pageRequest, config, state); + const now = 1_800_000_000_000; + const session = issueGuiSession(pageRequest, config, state, { trustedTailscaleIngress: false, now }); expect(session).not.toBeNull(); + expect(session).toMatchObject({ + serverOrigin: "http://localhost:10100", + browserOrigin: "http://localhost:10100", + issuance: "loopback", + expiresAt: now + LOOPBACK_GUI_SESSION_TTL_MS, + }); const guiDist = join(testHome, "gui"); const { mkdirSync, writeFileSync } = await import("node:fs"); @@ -812,7 +848,8 @@ describe("management and data-plane credential separation", () => { // source checkout (no packaged build) can still mint an origin-bound session. const bootstrapPage = serveSessionBootstrap(session!); const bootstrapHtml = await bootstrapPage.text(); - expect(bootstrapHtml).toContain(`name="opencodex-session-origin" content="${session?.origin}"`); + expect(bootstrapHtml).toContain(`name="opencodex-session-origin" content="${session?.browserOrigin}"`); + expect(bootstrapHtml).toContain(`name="opencodex-session-server-origin" content="${session?.serverOrigin}"`); expect(bootstrapHtml).toContain(`name="opencodex-session-token" content="${session?.token}"`); const sameOriginRead = new Request("http://localhost:10100/api/config", { @@ -883,6 +920,227 @@ describe("management and data-plane credential separation", () => { expect(html).toContain('name="opencodex-session-token"'); expect(html).toContain('name="opencodex-session-csrf"'); expect(html).toContain('name="opencodex-session-origin"'); + expect(html).toContain('name="opencodex-session-server-origin"'); + } finally { + await server.stop(true); + } + }); + + test("session bootstrap escapes both browser and server origin attributes", async () => { + const response = serveSessionBootstrap({ + token: "ocx_session_safe", + csrfToken: "csrf-safe", + browserOrigin: 'https://browser.example.test/\">', + serverOrigin: 'https://hub.example.test/\">', + expiresAt: Date.now() + 1_000, + issuance: "pairing", + }); + const html = await response.text(); + expect(html).not.toContain(""); + expect(html).not.toContain(" { + const config = hubConfig(); + const state = initializeManagementAuthState(config); + if (!state.available) throw new Error("expected management auth state"); + const now = 1_800_000_000_000; + const request = new Request("https://hub.example.test/", { + headers: { + Host: "hub.example.test", + Origin: "https://dashboard.example.test", + "Tailscale-User-Login": "alice@example.test", + }, + }); + expect(issueGuiSession(request, config, state, { trustedTailscaleIngress: false, now })).toBeNull(); + expect(issueGuiSession(request, config, state, { trustedTailscaleIngress: true, now })).toMatchObject({ + serverOrigin: "https://hub.example.test", + browserOrigin: "https://dashboard.example.test", + issuance: "tailscale-identity", + expiresAt: now + REMOTE_GUI_SESSION_TTL_MS, + }); + expect(issueGuiSession(new Request(request, { + headers: { ...Object.fromEntries(request.headers), "Tailscale-User-Login": "mallory@example.test" }, + }), config, state, { trustedTailscaleIngress: true, now })).toBeNull(); + expect(issueGuiSession(new Request(request, { + headers: { ...Object.fromEntries(request.headers), "Tailscale-User-Login": " alice@example.test " }, + }), config, state, { trustedTailscaleIngress: true, now })).toBeNull(); + expect(issueGuiSession(request, { ...config, runtimeRole: "client" }, state, { trustedTailscaleIngress: true, now })).toBeNull(); + expect(issueGuiSession(request, { ...config, remoteGui: { allowedTailscaleUsers: [] } }, state, { trustedTailscaleIngress: true, now })).toBeNull(); + const httpConfig = hubConfig("http://hub.example.test"); + expect(issueGuiSession(new Request("http://hub.example.test/", { + headers: { Host: "hub.example.test", "Tailscale-User-Login": "alice@example.test" }, + }), httpConfig, state, { trustedTailscaleIngress: true, now })).toBeNull(); + }); + + test("pairing grants are digest-only, origin-bound, single-use, and never accept alternate credentials", () => { + const config = hubConfig(); + const state = initializeManagementAuthState(config); + if (!state.available) throw new Error("expected management auth state"); + const now = 1_800_000_000_000; + const created = createGuiPairingGrant("https://dashboard.example.test", config, state, now); + expect(created.expiresAt).toBe(now + GUI_PAIRING_GRANT_TTL_MS); + expect(state.sessions.size).toBe(0); + expect(state.pairingGrants.size).toBe(1); + expect([...state.pairingGrants.keys()].join(" ")).not.toContain(created.grant); + + const exchange = (origin: string, host = "hub.example.test", headers: HeadersInit = {}) => new Request( + "https://hub.example.test/opencodex-session", + { method: "POST", headers: { Host: host, Origin: origin, ...headers } }, + ); + expect(consumeGuiPairingGrant( + exchange("https://evil.example.test"), { grant: created.grant }, config, state, now + 1, + )).toBeNull(); + expect(consumeGuiPairingGrant( + exchange("https://dashboard.example.test", "localhost:10100"), { grant: created.grant }, config, state, now + 1, + )).toBeNull(); + expect(consumeGuiPairingGrant( + exchange("https://dashboard.example.test", "hub.example.test", { "x-opencodex-api-key": "admin-secret" }), + { grant: created.grant }, config, state, now + 1, + )).toBeNull(); + const session = consumeGuiPairingGrant( + exchange("https://dashboard.example.test"), { grant: created.grant }, config, state, now + 1, + ); + expect(session).toMatchObject({ + serverOrigin: "https://hub.example.test", + browserOrigin: "https://dashboard.example.test", + issuance: "pairing", + expiresAt: now + 1 + REMOTE_GUI_SESSION_TTL_MS, + }); + expect(state.pairingGrants.size).toBe(0); + expect(consumeGuiPairingGrant( + exchange("https://dashboard.example.test"), { grant: created.grant }, config, state, now + 2, + )).toBeNull(); + + const expired = createGuiPairingGrant("https://dashboard.example.test", config, state, now + 10); + expect(consumeGuiPairingGrant( + exchange("https://dashboard.example.test"), { grant: expired.grant }, config, state, expired.expiresAt, + )).toBeNull(); + }); + + test("insecure HTTP pairing is explicit and a refused exchange can be retried after opt-in", () => { + const config = hubConfig("http://hub.example.test"); + const state = initializeManagementAuthState(config); + if (!state.available) throw new Error("expected management auth state"); + const now = 1_800_000_000_000; + const created = createGuiPairingGrant("https://dashboard.example.test", config, state, now); + const request = new Request("http://hub.example.test/opencodex-session", { + method: "POST", + headers: { Host: "hub.example.test", Origin: "https://dashboard.example.test" }, + }); + expect(consumeGuiPairingGrant(request, { grant: created.grant }, config, state, now + 1)).toBeNull(); + expect(state.pairingGrants.size).toBe(1); + config.remoteGui = { ...config.remoteGui, allowInsecureHttp: true }; + expect(consumeGuiPairingGrant(request, { grant: created.grant }, config, state, now + 2)).toMatchObject({ + issuance: "insecure-http-pairing", + }); + expect(state.pairingGrants.size).toBe(0); + }); + + test("pairing grant creation is bounded by a per-state rate limit", () => { + const config = hubConfig(); + const state = initializeManagementAuthState(config); + if (!state.available) throw new Error("expected management auth state"); + const now = 1_800_000_000_000; + for (let index = 0; index < 8; index++) { + createGuiPairingGrant("https://dashboard.example.test", config, state, now + index); + } + expect(() => createGuiPairingGrant("https://dashboard.example.test", config, state, now + 9)).toThrow("rate limit"); + expect(state.sessions.size).toBe(0); + }); + + test("remote session admission shares the full predicate and renews only after success", () => { + const config = hubConfig(); + const state = initializeManagementAuthState(config); + if (!state.available) throw new Error("expected management auth state"); + const issuedAt = 1_800_000_000_000; + const created = createGuiPairingGrant("https://dashboard.example.test", config, state, issuedAt); + const session = consumeGuiPairingGrant(new Request("https://hub.example.test/opencodex-session", { + method: "POST", + headers: { Host: "hub.example.test", Origin: "https://dashboard.example.test" }, + }), { grant: created.grant }, config, state, issuedAt + 1)!; + const before = session.expiresAt; + const request = (overrides: Record = {}, method = "GET", host = "hub.example.test") => new Request( + `https://${host}/api/config`, + { + method, + headers: { + Host: host, + Origin: "https://dashboard.example.test", + "x-opencodex-api-key": session.token, + "x-opencodex-gui-origin": "https://dashboard.example.test", + ...(method === "GET" ? {} : { "x-opencodex-csrf-token": session.csrfToken }), + ...overrides, + }, + }, + ); + expect(authorizeGuiSessionRequest(request({ "x-opencodex-gui-origin": "https://evil.example.test" }), config, state, issuedAt + 2)).toMatchObject({ ok: false, reason: "browser-origin" }); + expect(session.expiresAt).toBe(before); + expect(authorizeGuiSessionRequest(request({}, "POST", "localhost:10100"), config, state, issuedAt + 3)).toMatchObject({ ok: false, reason: "server-origin" }); + expect(session.expiresAt).toBe(before); + expect(authorizeGuiSessionRequest(request({ "x-opencodex-csrf-token": "wrong" }, "POST"), config, state, issuedAt + 4)).toMatchObject({ ok: false, reason: "csrf" }); + expect(session.expiresAt).toBe(before); + expect(authorizeGuiSessionRequest(request({}, "POST"), config, state, issuedAt + 5)).toMatchObject({ ok: true, principal: "gui-session" }); + expect(session.expiresAt).toBe(issuedAt + 5 + REMOTE_GUI_SESSION_TTL_MS); + }); + + test("the live pairing route refuses admin authority and exchanges only a capability-created grant", async () => { + const config = hubConfig(); + saveConfig(config); + const state = initializeManagementAuthState(config); + if (!state.available) throw new Error("expected management auth state"); + const secret = "G".repeat(43); + const server = startServer(0, { managementAuthState: state, localAttestationSecret: secret }); + try { + const adminAttempt = await fetch(new URL(GUI_PAIR_PATH, server.url), { + method: "POST", + headers: { "content-length": "0", "x-opencodex-api-key": "admin-secret" }, + }); + expect(adminAttempt.status).toBe(403); + expect(state.pairingGrants.size).toBe(0); + + const nonce = "H".repeat(43); + const expiresAt = Date.now() + GUI_PAIR_CAPABILITY_TTL_MS; + const capability = createGuiPairCapability( + secret, nonce, GUI_PAIR_METHOD, GUI_PAIR_PATH, "https://dashboard.example.test", + process.pid, server.port, expiresAt, + )!; + const createdResponse = await fetch(new URL(GUI_PAIR_PATH, server.url), { + method: "POST", + headers: { + "content-length": "0", + [GUI_PAIR_EXPECTED_PID_HEADER]: String(process.pid), + [GUI_PAIR_NONCE_HEADER]: nonce, + [GUI_PAIR_EXPIRES_AT_HEADER]: String(expiresAt), + [GUI_PAIR_BROWSER_ORIGIN_HEADER]: "https://dashboard.example.test", + [GUI_PAIR_CAPABILITY_HEADER]: capability, + }, + }); + expect(createdResponse.status).toBe(201); + expect(createdResponse.headers.get("cache-control")).toBe("no-store"); + const created = await createdResponse.json() as { grant: string }; + expect(state.pairingGrants.size).toBe(1); + + const adminExchange = await fetch(new URL("/opencodex-session", server.url), { + method: "POST", + headers: { Host: "hub.example.test", Origin: "https://dashboard.example.test", "content-type": "application/json" }, + body: JSON.stringify({ grant: "admin-secret" }), + }); + expect(adminExchange.status).toBe(401); + + const exchanged = await fetch(new URL("/opencodex-session", server.url), { + method: "POST", + headers: { Host: "hub.example.test", Origin: "https://dashboard.example.test", "content-type": "application/json" }, + body: JSON.stringify({ grant: created.grant }), + }); + expect(exchanged.status).toBe(200); + expect(exchanged.headers.get("cache-control")).toBe("no-store"); + const html = await exchanged.text(); + expect(html).toContain('name="opencodex-session-origin" content="https://dashboard.example.test"'); + expect(html).toContain('name="opencodex-session-server-origin" content="https://hub.example.test"'); + expect(state.pairingGrants.size).toBe(0); } finally { await server.stop(true); } @@ -1136,4 +1394,4 @@ describe("codex app-server restart routes ride the management gate", () => { await server.stop(true); } }); -}); \ No newline at end of file +}); diff --git a/tests/sidebar-routes.test.ts b/tests/sidebar-routes.test.ts index 4a311e1125..8a84f82254 100644 --- a/tests/sidebar-routes.test.ts +++ b/tests/sidebar-routes.test.ts @@ -19,7 +19,7 @@ async function call( method: string, pathname: string, headers: Record = {}, - principal?: "admin-token" | "gui-session", + principal?: "admin-token" | "gui-session" | "gui-pair-capability", ): Promise<{ status: number; body: unknown; raw: string; routed: boolean }> { // `isAllowedManagementOrigin` derives the expected origin from the Host header and // rejects the request outright when it is missing, so Host is required here. Omitting @@ -221,6 +221,20 @@ describe("route surface", () => { expect(calls).toEqual([]); }); + test("a GUI pairing capability is not a consent-bearing session principal", async () => { + const calls: string[][] = []; + await withStarDeps({ + nowMs: () => 0, + async runGh(args) { calls.push(args); return { status: 0 }; }, + }, async () => { + invalidateStarStatusCache(); + const { status, body } = await call("POST", "/api/github/star", {}, "gui-pair-capability"); + expect(status).toBe(403); + expect((body as Record).code).toBe("agent_consent_required"); + }); + expect(calls).toEqual([]); + }); + test("a direct dispatch with no resolved principal is treated as untrusted", async () => { // Defense in depth for callers that bypass the HTTP gate (route-level tests, future // internal dispatchers): an unknown principal must never satisfy the consent check. From fd27c1e04725450b86b2ab6f98898ef07aec5770 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 03:13:39 +0900 Subject: [PATCH 043/172] test(connect): align audited phase three matrix --- tests/cli-headless-parity.test.ts | 21 +++++++++++ tests/cli-start-journal-order.test.ts | 39 ++++++++++++++++++++ tests/codex-catalog-restore.test.ts | 40 +++++++++++++++++++++ tests/codex-inject-integration.test.ts | 49 ++++++++++++++++++++++++++ 4 files changed, 149 insertions(+) diff --git a/tests/cli-headless-parity.test.ts b/tests/cli-headless-parity.test.ts index dcd05d295b..f16742c3ea 100644 --- a/tests/cli-headless-parity.test.ts +++ b/tests/cli-headless-parity.test.ts @@ -2,6 +2,7 @@ import { afterEach, describe, expect, spyOn, test } from "bun:test"; import { mkdtempSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; +import { Readable } from "node:stream"; import { handleAccessCommand } from "../src/cli/access"; import { handleAgentCommand } from "../src/cli/agent"; import { handleComboCommand } from "../src/cli/combo"; @@ -11,6 +12,7 @@ import { handleModelsRuntimeCommand } from "../src/cli/models-runtime"; import { handleProviderRuntimeCommand } from "../src/cli/provider-runtime"; import { providerQuotaLine } from "../src/cli/account-extended"; import { formatAccountTable } from "../src/cli/account"; +import { handleConnectCommand } from "../src/cli/connect"; type Recorded = { path: string; method: string; body: unknown }; const servers: Array> = []; @@ -516,6 +518,25 @@ describe("headless GUI parity CLI", () => { expect(runtime.requests[0]).toEqual({ path: "/api/keys", method: "POST", body: { name: "deploy" } }); }); + test("remote connect status is headless and revoke refuses disconnected state before hub traffic", async () => { + let requests = 0; + const logSpy = spyOn(console, "log").mockImplementation(() => {}); + const errorSpy = spyOn(console, "error").mockImplementation(() => {}); + try { + expect(await handleConnectCommand(["status", "--json"], { + fetchImpl: async () => { requests += 1; return new Response(); }, + })).toBe(0); + expect(await handleConnectCommand(["revoke", "--admin-token-stdin", "--json"], { + stdinImpl: Readable.from(["ocx_admin_test\n"]), + fetchImpl: async () => { requests += 1; return new Response(); }, + })).toBe(1); + expect(requests).toBe(0); + } finally { + logSpy.mockRestore(); + errorSpy.mockRestore(); + } + }); + test("Grok include edits the persisted exclusion set before apply", async () => { const runtime = fakeRuntime((req) => { const url = new URL(req.url); diff --git a/tests/cli-start-journal-order.test.ts b/tests/cli-start-journal-order.test.ts index e2fe633932..f325dbd7e4 100644 --- a/tests/cli-start-journal-order.test.ts +++ b/tests/cli-start-journal-order.test.ts @@ -152,6 +152,45 @@ afterEach(async () => { }); describe("start and ensure journal ownership (#1230)", () => { + test("startup preserves only a client journal matching the final committed api key id", async () => { + for (const matches of [true, false]) { + const fx = fixture(); + const original = '# original client baseline\nmodel_provider = "openai"\n'; + const injected = '# connected remote routing\nmodel_provider = "opencodex"\n'; + writeFileSync(fx.configPath, injected); + writeFileSync(join(fx.ocxHome, "config.json"), JSON.stringify({ + port: 0, + providers: {}, + defaultProvider: "openai", + runtimeRole: "client", + client: { + serverUrl: "https://hub.example.test", + managementUrl: "https://hub.example.test", + managementTransport: "direct", + selectedClients: ["codex"], + tokenEnv: "OPENCODEX_API_AUTH_TOKEN", + apiKeyId: matches ? "client-key-1" : "different-key", + tokenFingerprint: "a".repeat(64), + protocolVersion: 1, + connectedAt: "2026-08-28T00:00:00.000Z", + }, + })); + writeFileSync(fx.journalPath, JSON.stringify({ + version: 1, + originalConfig: Buffer.from(original).toString("base64"), + originalProfile: null, + owner: { kind: "client", apiKeyId: "client-key-1" }, + pid: 999_999, + timestamp: new Date().toISOString(), + })); + + const result = await runCli(fx, ["status", "--json"]); + expect(result.exitCode).toBe(0); + expect(readFileSync(fx.configPath, "utf8")).toBe(matches ? injected : original); + expect(existsSync(fx.journalPath)).toBe(matches); + } + }, 30_000); + test("a healthy proxy owner preserves the journal for both start and ensure", async () => { const fx = fixture(); const owner = await startOwner(fx); diff --git a/tests/codex-catalog-restore.test.ts b/tests/codex-catalog-restore.test.ts index 253c42f449..9dd89a478e 100644 --- a/tests/codex-catalog-restore.test.ts +++ b/tests/codex-catalog-restore.test.ts @@ -38,6 +38,46 @@ describe("Codex catalog restore", () => { if (existsSync(opencodexHome)) rmSync(opencodexHome, { recursive: true, force: true }); }); + test("version-1 process journals restore, while matching client ownership is durable", () => { + const configPath = join(codexHome, "config.toml"); + const journalPath = join(codexHome, "opencodex-journal.json"); + const original = '# original\nmodel_provider = "openai"\n'; + const injected = '# injected\nmodel_provider = "opencodex"\n'; + writeFileSync(configPath, injected); + writeFileSync(journalPath, JSON.stringify({ + version: 1, + originalConfig: Buffer.from(original).toString("base64"), + originalProfile: null, + pid: 999_999, + timestamp: new Date().toISOString(), + })); + const legacy = runScript(codexHome, opencodexHome, ` + const { reconcileJournal } = require("./src/codex/journal"); + console.log(JSON.stringify({ restored: reconcileJournal() })); + `); + expect(legacy.status).toBe(0); + expect(JSON.parse(legacy.stdout).restored).toBe(true); + expect(readFileSync(configPath, "utf8")).toBe(original); + + writeFileSync(configPath, injected); + writeFileSync(journalPath, JSON.stringify({ + version: 1, + originalConfig: Buffer.from(original).toString("base64"), + originalProfile: null, + owner: { kind: "client", apiKeyId: "client-key-1" }, + pid: 999_999, + timestamp: new Date().toISOString(), + })); + const client = runScript(codexHome, opencodexHome, ` + const { reconcileJournal } = require("./src/codex/journal"); + console.log(JSON.stringify({ restored: reconcileJournal({ activeClientApiKeyId: "client-key-1" }) })); + `); + expect(client.status).toBe(0); + expect(JSON.parse(client.stdout).restored).toBe(false); + expect(readFileSync(configPath, "utf8")).toBe(injected); + expect(existsSync(journalPath)).toBe(true); + }); + // spawnSync(bun --eval) under `bun test --isolate` on Windows can exceed the // default 5s case budget when the runner is under load (seen at ~5.4s on GHA). test("drops routed entries without overwriting user-added native entries", () => { diff --git a/tests/codex-inject-integration.test.ts b/tests/codex-inject-integration.test.ts index 9f7157eb5b..90c9ffbd5d 100644 --- a/tests/codex-inject-integration.test.ts +++ b/tests/codex-inject-integration.test.ts @@ -61,6 +61,55 @@ describe("injectCodexConfig integration (Design B)", () => { rmSync(ocxHome, { recursive: true, force: true }); }); + test("remote target validate-only writes nothing; commit journals client ownership and restores exact preimage", () => { + const original = '# remote baseline\nmodel_provider = "openai"\n'; + writeFileSync(join(codexHome, "config.toml"), original, "utf8"); + const script = ` + const fs = require("node:fs"); + const path = require("node:path"); + const { injectCodexConfig } = require("./src/codex/inject"); + const { journalOwner, restoreJournalState } = require("./src/codex/journal"); + const target = { baseUrl: "https://hub.example.test/v1", requiresAdmissionToken: true, tokenEnv: "OPENCODEX_API_AUTH_TOKEN" }; + (async () => { + const configPath = path.join(process.env.CODEX_HOME, "config.toml"); + const journalPath = path.join(process.env.CODEX_HOME, "opencodex-journal.json"); + const before = fs.readFileSync(configPath, "utf8"); + const preflight = await injectCodexConfig(10100, { syncResumeHistory: false }, { + validateOnly: true, routingTarget: target, catalogPath: null, + journalOwner: { kind: "client", apiKeyId: "client-key-1" }, + }); + const afterPreflight = fs.readFileSync(configPath, "utf8"); + const journalAfterPreflight = fs.existsSync(journalPath); + const committed = await injectCodexConfig(10100, { syncResumeHistory: false }, { + routingTarget: target, catalogPath: null, + journalOwner: { kind: "client", apiKeyId: "client-key-1" }, + }); + const injected = fs.readFileSync(configPath, "utf8"); + const owner = journalOwner(); + const restored = restoreJournalState(); + console.log(JSON.stringify({ preflight, committed, before, afterPreflight, journalAfterPreflight, injected, owner, restored, final: fs.readFileSync(configPath, "utf8") })); + })(); + `; + const result = spawnSync(process.execPath, ["--eval", script], { + cwd: repoRoot, + env: { ...process.env, CODEX_HOME: codexHome, OPENCODEX_HOME: ocxHome }, + encoding: "utf8", + timeout: SPAWN_BUDGET_MS - 5_000, + }); + expect(result.status).toBe(0); + const value = JSON.parse(result.stdout.trim()); + expect(value.preflight.success).toBe(true); + expect(value.before).toBe(original); + expect(value.afterPreflight).toBe(original); + expect(value.journalAfterPreflight).toBe(false); + expect(value.committed.success).toBe(true); + expect(value.injected).toContain('base_url = "https://hub.example.test/v1"'); + expect(value.injected).toContain('env_key = "OPENCODEX_API_AUTH_TOKEN"'); + expect(value.owner).toEqual({ kind: "client", apiKeyId: "client-key-1" }); + expect(value.restored.complete).toBe(true); + expect(value.final).toBe(original); + }); + test("upgrade path: a legacy-injected config converts to the Design B form in one inject", () => { writeFileSync(join(codexHome, "config.toml"), [ 'model_provider = "opencodex"', From e6c6e784de056f564fc56b377770b6e5ec155633 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 02:26:17 +0900 Subject: [PATCH 044/172] fix(remote-gui): enforce exact bootstrap destination --- gui/src/api.ts | 3 ++- gui/tests/api-auth-deadline.test.ts | 12 ++++++++---- gui/tests/api-auth-memory.test.ts | 16 ++++++++-------- src/server/index.ts | 17 ++++++++++------- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/gui/src/api.ts b/gui/src/api.ts index 89b8a33d6b..cce2f98951 100644 --- a/gui/src/api.ts +++ b/gui/src/api.ts @@ -174,7 +174,8 @@ async function reBootstrapSessionToken(): Promise { const html = await response.text(); let responseOrigin: string; try { - responseOrigin = new URL(response.url || SESSION_REBOOTSTRAP_PATH, window.location.href).origin; + if (!response.url) throw new TypeError("bootstrap response URL is missing"); + responseOrigin = new URL(response.url).origin; } catch { clearToken(); return { kind: "unavailable" }; diff --git a/gui/tests/api-auth-deadline.test.ts b/gui/tests/api-auth-deadline.test.ts index a56f3c1ea7..08ff8d6afe 100644 --- a/gui/tests/api-auth-deadline.test.ts +++ b/gui/tests/api-auth-deadline.test.ts @@ -68,10 +68,14 @@ function hangUntilAborted(signal?: AbortSignal | null): Promise { }); } -const MINTED = () => new Response(sessionDocumentHtml("ocx_session_fresh", "fresh-csrf", "http://localhost"), { - status: 200, - headers: { "Content-Type": "text/html" }, -}); +const MINTED = () => { + const response = new Response(sessionDocumentHtml("ocx_session_fresh", "fresh-csrf", "http://localhost"), { + status: 200, + headers: { "Content-Type": "text/html" }, + }); + Object.defineProperty(response, "url", { configurable: true, value: "http://localhost/opencodex-session" }); + return response; +}; test("hung bootstrap fails the wave within the deadline and a later wave re-bootstraps to success", async () => { setRebootstrapTimeoutForTests(50); let bootstrapCalls = 0; diff --git a/gui/tests/api-auth-memory.test.ts b/gui/tests/api-auth-memory.test.ts index d5e77f9d34..3003bbebba 100644 --- a/gui/tests/api-auth-memory.test.ts +++ b/gui/tests/api-auth-memory.test.ts @@ -400,10 +400,10 @@ test("expired session silently re-bootstraps from the served document without pr const headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined)); if (url.pathname === "/opencodex-session") { bootstrapFetches += 1; - return new Response(sessionDocumentHtml("ocx_session_fresh", "fresh-csrf", "http://localhost"), { - status: 200, - headers: { "Content-Type": "text/html" }, - }); + return htmlResponseAt( + sessionDocumentHtml("ocx_session_fresh", "fresh-csrf", "http://localhost"), + "http://localhost/opencodex-session", + ); } seenApiKeys.push(headers.get("X-OpenCodex-API-Key")); seenGuiOrigins.push(headers.get("X-OpenCodex-GUI-Origin")); @@ -436,10 +436,10 @@ test("a session minted for another origin is rejected and the prompt fallback st const url = new URL(raw, "http://localhost/"); const headers = new Headers(init?.headers); if (url.pathname === "/opencodex-session") { - return new Response(sessionDocumentHtml("ocx_session_foreign", "foreign-csrf", "http://192.0.2.10:10100"), { - status: 200, - headers: { "Content-Type": "text/html" }, - }); + return htmlResponseAt( + sessionDocumentHtml("ocx_session_foreign", "foreign-csrf", "http://192.0.2.10:10100"), + "http://localhost/opencodex-session", + ); } if (headers.get("X-OpenCodex-API-Key") === "manual-admin-token") return new Response("{}", { status: 200 }); return new Response("unauthorized", { status: 401 }); diff --git a/src/server/index.ts b/src/server/index.ts index a8d4c7a7f5..3498970ca6 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -1123,7 +1123,10 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server GUI_PAIRING_EXCHANGE_BODY_LIMIT) { - return Response.json({ error: "pairing exchange body too large" }, { status: 413, headers: { "Cache-Control": "no-store" } }); + return withManagementCors(Response.json({ error: "pairing exchange body too large" }, { status: 413, headers: { "Cache-Control": "no-store" } }), req, config); } const text = await req.text(); if (Buffer.byteLength(text) > GUI_PAIRING_EXCHANGE_BODY_LIMIT) { - return Response.json({ error: "pairing exchange body too large" }, { status: 413, headers: { "Cache-Control": "no-store" } }); + return withManagementCors(Response.json({ error: "pairing exchange body too large" }, { status: 413, headers: { "Cache-Control": "no-store" } }), req, config); } let body: unknown; try { body = JSON.parse(text); } catch { - return Response.json({ error: "invalid pairing exchange body" }, { status: 400, headers: { "Cache-Control": "no-store" } }); + return withManagementCors(Response.json({ error: "invalid pairing exchange body" }, { status: 400, headers: { "Cache-Control": "no-store" } }), req, config); } if (!body || typeof body !== "object" || Array.isArray(body) || Object.keys(body as Record).length !== 1 || typeof (body as Record).grant !== "string") { - return Response.json({ error: "invalid pairing exchange body" }, { status: 400, headers: { "Cache-Control": "no-store" } }); + return withManagementCors(Response.json({ error: "invalid pairing exchange body" }, { status: 400, headers: { "Cache-Control": "no-store" } }), req, config); } const session = managementAuth.available ? consumeGuiPairingGrant(req, body, config, managementAuth) : null; return session ? withManagementCors(serveSessionBootstrap(session), req, config) - : new Response(null, { status: 401, headers: { "Cache-Control": "no-store" } }); + : withManagementCors(new Response(null, { status: 401, headers: { "Cache-Control": "no-store" } }), req, config); } return withCors(formatErrorResponse(404, "not_found", `Unknown endpoint: ${req.method} ${url.pathname}`), req, policy); } From 4ae8fa49ce194b2bf50bd98fe5c868bc12a3dcb7 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 03:15:05 +0900 Subject: [PATCH 045/172] fix(connect): reconcile client journal before lifecycle --- src/cli/dispatch.ts | 13 +++++++++++++ tests/cli-start-journal-order.test.ts | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/cli/dispatch.ts b/src/cli/dispatch.ts index 964b45661e..b792d560cd 100644 --- a/src/cli/dispatch.ts +++ b/src/cli/dispatch.ts @@ -23,6 +23,7 @@ import { stripGrokConfig } from "../grok/inject"; import { afterCatalogWriteHandleAppServers } from "../codex/app-server-processes"; import { normalizeUpdateChannel, runGuiUpdateWorker } from "../update/job"; import { isJsonOption, takeFlag } from "./runtime-api"; +import type { ClientConnectionState } from "../client/state"; export interface CliDispatchDeps { args: string[]; @@ -61,6 +62,7 @@ const commandRunners: Record = { start: async deps => { const { readClientConnectionState } = await import("../client/state"); const clientState = readClientConnectionState(); + await reconcileClientJournalBeforeLifecycle(clientState); if (clientState.kind === "connected") { console.error("Client mode does not start a local provider proxy in Remote Hub Phase 3; use 'ocx sync'."); return 1; @@ -257,6 +259,7 @@ const commandRunners: Record = { ensure: async deps => { const { readClientConnectionState } = await import("../client/state"); const clientState = readClientConnectionState(); + await reconcileClientJournalBeforeLifecycle(clientState); if (clientState.kind !== "disconnected") { console.error(clientState.kind === "connected" ? "Client mode does not start a local provider proxy; use 'ocx sync'." @@ -910,3 +913,13 @@ async function handleConnectedSyncCatalogWrite( afterCatalogWriteHandleAppServers({ restart: restartCodex, log: console }); if (restartDesktopApp) await handleDesktopAppRestart(console); } + +async function reconcileClientJournalBeforeLifecycle( + state: ClientConnectionState, +): Promise { + if (state.kind === "disconnected") return; + const { reconcileJournal } = await import("../codex/journal"); + reconcileJournal(state.kind === "connected" + ? { activeClientApiKeyId: state.value.apiKeyId } + : undefined); +} diff --git a/tests/cli-start-journal-order.test.ts b/tests/cli-start-journal-order.test.ts index f325dbd7e4..4ca5a077c1 100644 --- a/tests/cli-start-journal-order.test.ts +++ b/tests/cli-start-journal-order.test.ts @@ -184,8 +184,8 @@ describe("start and ensure journal ownership (#1230)", () => { timestamp: new Date().toISOString(), })); - const result = await runCli(fx, ["status", "--json"]); - expect(result.exitCode).toBe(0); + const result = await runCli(fx, ["start"]); + expect(result.exitCode).toBe(1); expect(readFileSync(fx.configPath, "utf8")).toBe(matches ? injected : original); expect(existsSync(fx.journalPath)).toBe(matches); } From 53970194d25746676905bd729da4e6864e1ddf66 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 02:27:06 +0900 Subject: [PATCH 046/172] test(remote-gui): lock replay and expiry negatives --- tests/gui-pair-client.test.ts | 27 ++++++++++++++++ tests/server-management-auth.test.ts | 47 +++++++++++++++++++++------- 2 files changed, 62 insertions(+), 12 deletions(-) diff --git a/tests/gui-pair-client.test.ts b/tests/gui-pair-client.test.ts index 3252af9dba..63bd9195db 100644 --- a/tests/gui-pair-client.test.ts +++ b/tests/gui-pair-client.test.ts @@ -77,6 +77,33 @@ describe("GUI pairing client", () => { expect(calls).toBe(1); }); + test("stops after one failed attestation or transport attempt", async () => { + let calls = 0; + const unattested = await requestBoundGuiPairingGrant(target, browserOrigin, { + readRuntime: () => ({ ...target, attestationSecret: secret }), + createChallenge: () => nonce, + fetchImpl: async () => { + calls += 1; + return Response.json({ service: "opencodex", pid: target.pid, port: target.port }); + }, + }); + expect(unattested).toEqual({ kind: "unavailable", reason: "attestation" }); + expect(calls).toBe(1); + + calls = 0; + const transport = await requestBoundGuiPairingGrant(target, browserOrigin, { + readRuntime: () => ({ ...target, attestationSecret: secret }), + createChallenge: () => nonce, + fetchImpl: async () => { + calls += 1; + throw new Error("response body contains secret-that-must-be-redacted"); + }, + }); + expect(transport).toEqual({ kind: "unavailable", reason: "transport" }); + expect(JSON.stringify(transport)).not.toContain("secret-that-must-be-redacted"); + expect(calls).toBe(1); + }); + test("sends one bodyless origin-bound capability and redacts rejected bodies", async () => { const requests: Array<{ url: string; init?: RequestInit }> = []; const now = 1_800_000_000_000; diff --git a/tests/server-management-auth.test.ts b/tests/server-management-auth.test.ts index 3f5197db61..3eb35155f5 100644 --- a/tests/server-management-auth.test.ts +++ b/tests/server-management-auth.test.ts @@ -996,10 +996,12 @@ describe("management and data-plane credential separation", () => { expect(consumeGuiPairingGrant( exchange("https://dashboard.example.test", "localhost:10100"), { grant: created.grant }, config, state, now + 1, )).toBeNull(); - expect(consumeGuiPairingGrant( - exchange("https://dashboard.example.test", "hub.example.test", { "x-opencodex-api-key": "admin-secret" }), - { grant: created.grant }, config, state, now + 1, - )).toBeNull(); + for (const alternateCredential of ["admin-secret", "data-secret", "ocx_session_not-a-grant"]) { + expect(consumeGuiPairingGrant( + exchange("https://dashboard.example.test", "hub.example.test", { "x-opencodex-api-key": alternateCredential }), + { grant: created.grant }, config, state, now + 1, + )).toBeNull(); + } const session = consumeGuiPairingGrant( exchange("https://dashboard.example.test"), { grant: created.grant }, config, state, now + 1, ); @@ -1082,8 +1084,22 @@ describe("management and data-plane credential separation", () => { expect(session.expiresAt).toBe(before); expect(authorizeGuiSessionRequest(request({ "x-opencodex-csrf-token": "wrong" }, "POST"), config, state, issuedAt + 4)).toMatchObject({ ok: false, reason: "csrf" }); expect(session.expiresAt).toBe(before); + const missingCsrf = new Request("https://hub.example.test/api/config", { + method: "POST", + headers: { + Host: "hub.example.test", + Origin: "https://dashboard.example.test", + "x-opencodex-api-key": session.token, + "x-opencodex-gui-origin": "https://dashboard.example.test", + }, + }); + expect(authorizeGuiSessionRequest(missingCsrf, config, state, issuedAt + 4)).toMatchObject({ ok: false, reason: "csrf" }); + expect(session.expiresAt).toBe(before); expect(authorizeGuiSessionRequest(request({}, "POST"), config, state, issuedAt + 5)).toMatchObject({ ok: true, principal: "gui-session" }); expect(session.expiresAt).toBe(issuedAt + 5 + REMOTE_GUI_SESSION_TTL_MS); + session.expiresAt = issuedAt + 6; + expect(authorizeGuiSessionRequest(request(), config, state, issuedAt + 7)).toMatchObject({ ok: false, reason: "expired" }); + expect(state.sessions.has(session.token)).toBe(false); }); test("the live pairing route refuses admin authority and exchanges only a capability-created grant", async () => { @@ -1107,21 +1123,28 @@ describe("management and data-plane credential separation", () => { secret, nonce, GUI_PAIR_METHOD, GUI_PAIR_PATH, "https://dashboard.example.test", process.pid, server.port, expiresAt, )!; + const capabilityHeaders = { + "content-length": "0", + [GUI_PAIR_EXPECTED_PID_HEADER]: String(process.pid), + [GUI_PAIR_NONCE_HEADER]: nonce, + [GUI_PAIR_EXPIRES_AT_HEADER]: String(expiresAt), + [GUI_PAIR_BROWSER_ORIGIN_HEADER]: "https://dashboard.example.test", + [GUI_PAIR_CAPABILITY_HEADER]: capability, + }; const createdResponse = await fetch(new URL(GUI_PAIR_PATH, server.url), { method: "POST", - headers: { - "content-length": "0", - [GUI_PAIR_EXPECTED_PID_HEADER]: String(process.pid), - [GUI_PAIR_NONCE_HEADER]: nonce, - [GUI_PAIR_EXPIRES_AT_HEADER]: String(expiresAt), - [GUI_PAIR_BROWSER_ORIGIN_HEADER]: "https://dashboard.example.test", - [GUI_PAIR_CAPABILITY_HEADER]: capability, - }, + headers: capabilityHeaders, }); expect(createdResponse.status).toBe(201); expect(createdResponse.headers.get("cache-control")).toBe("no-store"); const created = await createdResponse.json() as { grant: string }; expect(state.pairingGrants.size).toBe(1); + const replayedCapability = await fetch(new URL(GUI_PAIR_PATH, server.url), { + method: "POST", + headers: capabilityHeaders, + }); + expect(replayedCapability.status).toBe(401); + expect(state.pairingGrants.size).toBe(1); const adminExchange = await fetch(new URL("/opencodex-session", server.url), { method: "POST", From 886c26e9ad3c9981a3f3038dc9fca311b2a5876d Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 03:19:56 +0900 Subject: [PATCH 047/172] test(connect): cover sync and disconnect conflicts --- tests/client-connect.test.ts | 117 +++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/tests/client-connect.test.ts b/tests/client-connect.test.ts index 6fa83f37d3..9a589e0cfe 100644 --- a/tests/client-connect.test.ts +++ b/tests/client-connect.test.ts @@ -262,3 +262,120 @@ describe("connect transaction and offline disconnect", () => { }); } }); + +function runConnectedStateScenario(mode: "sync-401" | "sync-503" | "disconnect-conflict") { + const opencodexHome = mkdtempSync(join(tmpdir(), "ocx-client-state-home-")); + const codexHome = mkdtempSync(join(tmpdir(), "ocx-client-state-codex-")); + const token = `ocx_data_${"e".repeat(40)}`; + const fingerprint = createHash("sha256").update(token).digest("hex"); + const catalog = '{"models":[]}'; + const etag = `"sha256-${createHash("sha256").update(catalog).digest("base64url")}"`; + const selectedClients = mode === "disconnect-conflict" ? ["codex"] : ["claude"]; + writeFileSync(join(opencodexHome, "config.json"), JSON.stringify({ + port: 10100, + providers: {}, + defaultProvider: "openai", + runtimeRole: "client", + client: { + serverUrl: "https://hub.example.test", + managementUrl: "https://hub.example.test", + managementTransport: "direct", + selectedClients, + tokenEnv: "OPENCODEX_API_AUTH_TOKEN", + apiKeyId: "client-key-1", + tokenFingerprint: fingerprint, + protocolVersion: 1, + connectedAt: "2026-08-28T00:00:00.000Z", + catalogEtag: etag, + catalogSyncedAt: "2026-08-28T00:00:00.000Z", + }, + }), "utf8"); + writeFileSync(join(opencodexHome, "service-api-token"), `${token}\n`, { mode: 0o600 }); + writeFileSync(join(codexHome, "opencodex-catalog.json"), catalog, "utf8"); + writeFileSync(join(codexHome, "config.toml"), mode === "disconnect-conflict" + ? 'model_provider = "opencodex"\n' + : 'model_provider = "openai"\n', "utf8"); + if (mode === "disconnect-conflict") { + writeFileSync(join(codexHome, "opencodex-journal.json"), JSON.stringify({ + version: 1, + originalConfig: Buffer.from('model_provider = "openai"\n').toString("base64"), + originalProfile: null, + owner: { kind: "client", apiKeyId: "different-key" }, + pid: 999_999, + timestamp: "2026-08-28T00:00:00.000Z", + })); + } + const script = ` + const fs = require("node:fs"); + const path = require("node:path"); + const { disconnectClient, syncConnectedClient } = require("./src/client/connect"); + const { readClientConnectionState } = require("./src/client/state"); + const mode = ${JSON.stringify(mode)}; + (async () => { + let result = null; + let error = null; + try { + if (mode === "disconnect-conflict") result = await disconnectClient(); + else result = await syncConnectedClient({}, { + fetchImpl: async () => Response.json({ error: "fixture" }, { status: mode === "sync-401" ? 401 : 503 }), + }); + } catch (cause) { error = cause instanceof Error ? cause.message : String(cause); } + console.log(JSON.stringify({ + result, + error, + state: readClientConnectionState(), + tokenExists: fs.existsSync(path.join(process.env.OPENCODEX_HOME, "service-api-token")), + journalExists: fs.existsSync(path.join(process.env.CODEX_HOME, "opencodex-journal.json")), + })); + })(); + `; + const child = spawnSync(process.execPath, ["--eval", script], { + cwd: repoRoot, + env: { ...process.env, OPENCODEX_HOME: opencodexHome, CODEX_HOME: codexHome }, + encoding: "utf8", + }); + const parsed = JSON.parse(child.stdout.trim().split("\n").at(-1) ?? "{}") as Record; + return { + status: child.status, + parsed, + cleanup: () => { + rmSync(opencodexHome, { recursive: true, force: true }); + rmSync(codexHome, { recursive: true, force: true }); + }, + }; +} + +describe("connected sync and disconnect conflicts", () => { + test("401 is a hard failure and never falls back to local providers", () => { + const run = runConnectedStateScenario("sync-401"); + try { + expect(run.status).toBe(0); + expect(run.parsed.result).toBeNull(); + expect(run.parsed.error).toContain("401"); + expect(run.parsed.state.kind).toBe("connected"); + expect(run.parsed.tokenExists).toBe(true); + } finally { run.cleanup(); } + }); + + test("hub 503 keeps and applies the last-known-good catalog as stale", () => { + const run = runConnectedStateScenario("sync-503"); + try { + expect(run.status).toBe(0); + expect(run.parsed.error).toBeNull(); + expect(run.parsed.result).toMatchObject({ stale: true, catalogWritten: false, injected: false }); + expect(run.parsed.state.kind).toBe("connected"); + } finally { run.cleanup(); } + }); + + test("journal ownership conflict preserves every artifact and connected state", () => { + const run = runConnectedStateScenario("disconnect-conflict"); + try { + expect(run.status).toBe(0); + expect(run.parsed.result).toBeNull(); + expect(run.parsed.error).toContain("journal ownership conflicts"); + expect(run.parsed.state.kind).toBe("connected"); + expect(run.parsed.tokenExists).toBe(true); + expect(run.parsed.journalExists).toBe(true); + } finally { run.cleanup(); } + }); +}); From bf382c07d285c5cab0bb3ce8b9008b7b7ea04b1a Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 02:30:25 +0900 Subject: [PATCH 048/172] fix(remote-gui): preserve renewal and mutation origin checks --- src/server/gui-session.ts | 10 +++++++++- tests/native-profile-route-security.test.ts | 8 +++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/server/gui-session.ts b/src/server/gui-session.ts index e726be0454..3dc803c76b 100644 --- a/src/server/gui-session.ts +++ b/src/server/gui-session.ts @@ -129,7 +129,15 @@ function mintSession( issuance, }; state.sessions.set(token, session); - return { token, ...session }; + return { + token, + serverOrigin: session.serverOrigin, + browserOrigin: session.browserOrigin, + csrfToken: session.csrfToken, + issuance: session.issuance, + get expiresAt() { return session.expiresAt; }, + set expiresAt(value) { session.expiresAt = value; }, + }; } function tailscaleLoginAllowed(req: Request, config: OcxConfig): boolean { diff --git a/tests/native-profile-route-security.test.ts b/tests/native-profile-route-security.test.ts index 104c985c80..596852cccb 100644 --- a/tests/native-profile-route-security.test.ts +++ b/tests/native-profile-route-security.test.ts @@ -203,9 +203,15 @@ describe("native-main profile routes at the management admission boundary", () = "x-opencodex-gui-origin": "https://dashboard.example.test", "x-opencodex-csrf-token": session.csrfToken, }; + const withoutHeader = (name: string): Record => Object.fromEntries( + Object.entries(base).filter(([header]) => header !== name), + ); expect((await request({ ...base, "x-opencodex-gui-origin": "https://evil.example.test" })).status).toBe(401); expect((await request({ ...base, Origin: "https://evil.example.test" })).status).toBe(401); - expect((await request({ ...base, Host: server.url.host })).status).toBe(401); + expect((await request(withoutHeader("Origin"))).status).toBe(401); + expect((await request(withoutHeader("x-opencodex-gui-origin"))).status).toBe(401); + expect((await request(withoutHeader("x-opencodex-csrf-token"))).status).toBe(401); + expect((await request({ ...base, Host: `127.0.0.1:${server.port}` })).status).toBe(401); expect((await request({ ...base, "x-opencodex-csrf-token": "" })).status).toBe(401); expect(calls).toEqual([]); expect((await request(base)).status).toBe(200); From acdac3fa16850d160f7048a8c8f1e93ddca6fbfe Mon Sep 17 00:00:00 2001 From: jun Date: Tue, 1 Sep 2026 17:26:30 +0900 Subject: [PATCH 049/172] fix(connect): a process-owned journal is ours to unwind, not a conflict MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Connecting after `ocx start` is the ordinary path: routing is already injected and the Codex journal is owned by the proxy process. Ownership never transfers during connect, because writeJournal() refuses to overwrite a journal whose config is already injected — so the process owner survives into the connected state. disconnectClient() read any non-matching owner as a conflict and refused. That stranded the connection: the operator could not disconnect, and no action available to them would make the check pass. The artifacts were preserved, so nothing was lost, but the connected state had no exit. A process-owned journal records the pre-injection baseline this same tool wrote, so restoring it is exactly the right unwind. The genuine conflict is a journal owned by a DIFFERENT client key, where restoring would tear down another key's routing; that case still refuses, and its existing test still passes. Injected routing with no journal at all now gets its own message. Previously it fell into the ownership error, which named the wrong cause: there is no recorded baseline to restore, so unwinding would be guessing at the original config rather than reading it. The regression test drives the real shape — injected config plus a process-owned journal — and fails against the previous refusal. --- src/client/connect.ts | 24 +++++++++++++++++++--- tests/client-connect.test.ts | 40 ++++++++++++++++++++++++++++++++---- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/src/client/connect.ts b/src/client/connect.ts index 3c954e5146..3d0acf4fc6 100644 --- a/src/client/connect.ts +++ b/src/client/connect.ts @@ -365,9 +365,27 @@ export async function disconnectClient( let restored = true; if (state.value.selectedClients.includes("codex")) { const owner = journalOwner(); - if (owner?.kind === "client" && owner.apiKeyId === state.value.apiKeyId) { - restored = restoreJournalState().complete; - } else if (owner !== null || isCodexRoutingInjected()) { + // A journal owned by this client key is ours, obviously. A journal owned by a PROCESS is + // also ours to unwind: it is what `ocx start` leaves behind, and connecting on top of it + // never transfers ownership — writeJournal() declines to overwrite a journal whose + // config is already injected, so the process owner survives into the connected state. + // + // Treating that as a conflict stranded the normal "start, then connect" path: disconnect + // refused, and nothing the operator could do would satisfy the check. The genuine + // conflict is a journal owned by a DIFFERENT client key, which is the one case where + // restoring would unwind somebody else's routing. + if ( + owner === null + || owner.kind === "process" + || owner.apiKeyId === state.value.apiKeyId + ) { + if (owner !== null) restored = restoreJournalState().complete; + else if (isCodexRoutingInjected()) { + // Injected routing with no journal at all: there is no recorded baseline to restore, + // so unwinding would be a guess about what the config looked like before. + throw new Error("disconnect refused: Codex routing is injected but no journal records the original state"); + } + } else { throw new Error("disconnect refused: Codex journal ownership conflicts with the connected key"); } if (!restored) throw new Error("disconnect refused: Codex journal restore was partial"); diff --git a/tests/client-connect.test.ts b/tests/client-connect.test.ts index 9a589e0cfe..6a172a1bd8 100644 --- a/tests/client-connect.test.ts +++ b/tests/client-connect.test.ts @@ -263,14 +263,15 @@ describe("connect transaction and offline disconnect", () => { } }); -function runConnectedStateScenario(mode: "sync-401" | "sync-503" | "disconnect-conflict") { +function runConnectedStateScenario(mode: "sync-401" | "sync-503" | "disconnect-conflict" | "disconnect-process-journal") { const opencodexHome = mkdtempSync(join(tmpdir(), "ocx-client-state-home-")); const codexHome = mkdtempSync(join(tmpdir(), "ocx-client-state-codex-")); const token = `ocx_data_${"e".repeat(40)}`; const fingerprint = createHash("sha256").update(token).digest("hex"); const catalog = '{"models":[]}'; const etag = `"sha256-${createHash("sha256").update(catalog).digest("base64url")}"`; - const selectedClients = mode === "disconnect-conflict" ? ["codex"] : ["claude"]; + const isDisconnect = mode === "disconnect-conflict" || mode === "disconnect-process-journal"; + const selectedClients = isDisconnect ? ["codex"] : ["claude"]; writeFileSync(join(opencodexHome, "config.json"), JSON.stringify({ port: 10100, providers: {}, @@ -292,7 +293,7 @@ function runConnectedStateScenario(mode: "sync-401" | "sync-503" | "disconnect-c }), "utf8"); writeFileSync(join(opencodexHome, "service-api-token"), `${token}\n`, { mode: 0o600 }); writeFileSync(join(codexHome, "opencodex-catalog.json"), catalog, "utf8"); - writeFileSync(join(codexHome, "config.toml"), mode === "disconnect-conflict" + writeFileSync(join(codexHome, "config.toml"), isDisconnect ? 'model_provider = "opencodex"\n' : 'model_provider = "openai"\n', "utf8"); if (mode === "disconnect-conflict") { @@ -305,6 +306,20 @@ function runConnectedStateScenario(mode: "sync-401" | "sync-503" | "disconnect-c timestamp: "2026-08-28T00:00:00.000Z", })); } + if (mode === "disconnect-process-journal") { + // The state `ocx start` leaves behind: routing is injected and the journal is owned by + // the proxy PROCESS, not by any client key. Connecting on top of this does not take + // ownership — writeJournal() refuses to overwrite a journal whose config is already + // injected — so the process owner survives into the connected state. + writeFileSync(join(codexHome, "opencodex-journal.json"), JSON.stringify({ + version: 1, + originalConfig: Buffer.from('model_provider = "openai"\n').toString("base64"), + originalProfile: null, + owner: { kind: "process", pid: 999_999 }, + pid: 999_999, + timestamp: "2026-08-28T00:00:00.000Z", + })); + } const script = ` const fs = require("node:fs"); const path = require("node:path"); @@ -315,7 +330,7 @@ function runConnectedStateScenario(mode: "sync-401" | "sync-503" | "disconnect-c let result = null; let error = null; try { - if (mode === "disconnect-conflict") result = await disconnectClient(); + if (mode === "disconnect-conflict" || mode === "disconnect-process-journal") result = await disconnectClient(); else result = await syncConnectedClient({}, { fetchImpl: async () => Response.json({ error: "fixture" }, { status: mode === "sync-401" ? 401 : 503 }), }); @@ -378,4 +393,21 @@ describe("connected sync and disconnect conflicts", () => { expect(run.parsed.journalExists).toBe(true); } finally { run.cleanup(); } }); + + test("a journal left owned by the proxy process does not strand the connection", () => { + // Connecting after `ocx start` is the normal path, not an edge case: routing is already + // injected and the journal is owned by the proxy process. Ownership never transfers, + // because writeJournal() will not overwrite a journal whose config is already injected. + // + // Disconnect then read that surviving process owner as a conflict and refused, so the + // operator could neither disconnect nor make the check pass — the connection was stuck. + // A process-owned journal is ours to re-own on connect, so disconnect must complete. + const run = runConnectedStateScenario("disconnect-process-journal"); + try { + expect(run.status).toBe(0); + expect(run.parsed.error).toBeNull(); + expect(run.parsed.state.kind).toBe("disconnected"); + expect(run.parsed.journalExists).toBe(false); + } finally { run.cleanup(); } + }); }); From dbec9004001ff589bee6fafc2587e2ba2bddc3d7 Mon Sep 17 00:00:00 2001 From: jun Date: Tue, 1 Sep 2026 17:20:15 +0900 Subject: [PATCH 050/172] fix(remote-gui): drop plaintext pairing and bound the unauthenticated exchange body MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two defects the review raised on this phase, both on the unauthenticated pairing path. Plaintext pairing is removed rather than gated. consumeGuiPairingGrant issued an "insecure-http-pairing" session over non-loopback HTTP whenever remoteGui.allowInsecureHttp was true. A reusable grant on plaintext is readable by anything on the path and the session it mints is reusable, so the flag recorded a risk the operator could not bound rather than controlling one. A grant now crosses loopback or authenticated HTTPS only, and no configuration re-opens it. The scheme check also moved ahead of the grant lookup. It previously ran after the grant was found and validated, so a refused exchange still consumed a single-use code — an attacker who strips TLS termination could spend every code the operator prints without ever authenticating. Refusing first leaves the grant intact, which the regression test asserts by replaying the same unspent grant successfully over HTTPS. allowInsecureHttp stays in the schema, marked retired. The config schema is strict, so deleting the key would make an existing config file fail to load entirely; accepting and ignoring it is the smaller harm. The exchange body bound now holds against caller-chosen framing. The endpoint is reachable without a credential, and the pre-check read Content-Length, which the caller controls: omit the header and Number(null ?? "0") is 0, or send chunked and there is no header at all. Both passed the check and reached req.text(), which buffers to completion — an unauthenticated caller decided how much memory the process spent, and the post-check only measured a string it had already been forced to hold. The read now stops at limit+1 bytes and cancels the body rather than draining it. The regression test streams 512 KiB against the 4 KiB limit with no Content-Length and asserts the server pulled fewer chunks than were offered; it fails against the previous implementation. Also repairs the rebase of tests/cli-dispatch.test.ts and tests/cli-registry.test.ts, where dev and this phase appended different tests at the same place. Both sides are kept. --- src/config.ts | 2 + src/server/gui-session.ts | 48 +++++++++++++++----- src/server/index.ts | 55 +++++++++++++++++++++- src/types/config.ts | 12 ++++- tests/cli-dispatch.test.ts | 6 ++- tests/cli-registry.test.ts | 2 + tests/server-auth.test.ts | 68 ++++++++++++++++++++++++++++ tests/server-management-auth.test.ts | 31 +++++++++++-- 8 files changed, 204 insertions(+), 20 deletions(-) diff --git a/src/config.ts b/src/config.ts index 8939dfe201..7cabaab64a 100644 --- a/src/config.ts +++ b/src/config.ts @@ -906,6 +906,8 @@ const remoteGuiConfigSchema = z.object({ seen.add(user); } }).optional(), + // Retired (see OcxRemoteGuiConfig): accepted so an existing file still loads, ignored by + // the pairing path. Removing it from a strict schema would reject the whole config. allowInsecureHttp: z.boolean().optional(), }).strict(); diff --git a/src/server/gui-session.ts b/src/server/gui-session.ts index 3dc803c76b..95a7008ff5 100644 --- a/src/server/gui-session.ts +++ b/src/server/gui-session.ts @@ -12,8 +12,7 @@ import { export type GuiSessionIssuance = | "loopback" | "tailscale-identity" - | "pairing" - | "insecure-http-pairing"; + | "pairing"; export interface GuiSessionRecord { serverOrigin: string; @@ -249,6 +248,19 @@ export function consumeGuiPairingGrant( now = Date.now(), ): GuiSessionBootstrap | null { if (req.method !== "POST" || hasAlternateCredential(req) || config.runtimeRole !== "hub") return null; + // Scheme check FIRST, before the grant is parsed or looked up. + // + // A grant is single-use, so consuming one and then refusing to mint would burn the + // operator's code on a request that was never going to succeed — an unauthenticated + // caller could strip TLS termination and spend every code the operator prints. Refusing + // here leaves the grant intact for a later request over a scheme that can carry it. + // + // There is no opt-in for plaintext. An earlier revision allowed non-loopback HTTP when + // `remoteGui.allowInsecureHttp` was true; a reusable grant on plaintext HTTP is readable + // by anything on the path and the session it mints is reusable, so the flag recorded a + // risk the operator could not bound rather than controlling one. + const destination = managementRequestOrigin(req, config); + if (!destination || !isPairingTransportPermitted(destination)) return null; const grant = strictPairingGrantBody(body); const browserOrigin = canonicalGuiBrowserOrigin(req.headers.get("Origin")); if (!grant || !browserOrigin) return null; @@ -262,17 +274,29 @@ export function consumeGuiPairingGrant( if (browserOrigin !== record.browserOrigin) return null; const serverOrigin = managementRequestOrigin(req, config); if (serverOrigin !== record.serverOrigin) return null; - const serverUrl = new URL(serverOrigin); - let issuance: GuiSessionIssuance; - if (serverUrl.protocol === "https:") issuance = "pairing"; - else if ( - serverUrl.protocol === "http:" - && !isLoopbackHostname(serverUrl.hostname) - && config.remoteGui?.allowInsecureHttp === true - ) issuance = "insecure-http-pairing"; - else return null; + // Re-checked against the grant's own recorded origin rather than only the request's: + // the two are compared just above, but this keeps the transport rule true of the value + // the session is actually minted from. + if (!isPairingTransportPermitted(record.serverOrigin)) return null; state.pairingGrants.delete(digest); - return mintSession(record.serverOrigin, record.browserOrigin, issuance, state, now); + return mintSession(record.serverOrigin, record.browserOrigin, "pairing", state, now); +} + +/** + * A pairing grant may cross loopback or authenticated HTTPS, and nothing else. + * + * Loopback plaintext is admissible because the bytes never leave the machine. Non-loopback + * plaintext is not, and no configuration re-opens it. + */ +function isPairingTransportPermitted(origin: string): boolean { + let url: URL; + try { + url = new URL(origin); + } catch { + return false; + } + if (url.protocol === "https:") return true; + return url.protocol === "http:" && isLoopbackHostname(url.hostname); } function requestCredential(req: Request): string | null { diff --git a/src/server/index.ts b/src/server/index.ts index 3498970ca6..18ca92c5ea 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -231,6 +231,46 @@ const WEBSOCKET_IDLE_TIMEOUT_SECONDS = 0; const REMOTE_CATALOG_KEY_ID_PATTERN = /^[A-Za-z0-9._-]{1,64}$/; const GUI_PAIRING_EXCHANGE_BODY_LIMIT = 4 * 1024; +/** + * Read at most `limit` bytes of a request body, or refuse. + * + * Returns null the moment the body is known to exceed `limit`, without retaining the excess. + * `req.text()` cannot express that: it buffers to completion first, so a caller who omits + * Content-Length or uses chunked framing decides how much memory the process spends. That + * matters here because the one caller is an unauthenticated endpoint. + * + * limit+1 is the stopping point rather than limit, so a body exactly at the limit is still + * accepted and only a genuinely over-limit body is rejected. + */ +async function readBoundedRequestText(req: Request, limit: number): Promise { + const body = req.body; + if (!body) return ""; + const reader = body.getReader(); + const chunks: Uint8Array[] = []; + let total = 0; + try { + for (;;) { + const { done, value } = await reader.read(); + if (done) break; + if (!value || value.byteLength === 0) continue; + total += value.byteLength; + if (total > limit) return null; + chunks.push(value); + } + } finally { + // Cancel rather than only releasing the lock: on the reject path the peer may still be + // sending, and an uncancelled body keeps that transfer alive. + await reader.cancel().catch(() => {}); + } + const joined = new Uint8Array(total); + let offset = 0; + for (const chunk of chunks) { + joined.set(chunk, offset); + offset += chunk.byteLength; + } + return new TextDecoder().decode(joined); +} + /** * Name WHICH configured credential was admitted, so a multi-key operator can attribute a * catalog read. @@ -1812,14 +1852,25 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server GUI_PAIRING_EXCHANGE_BODY_LIMIT) { return withManagementCors(Response.json({ error: "pairing exchange body too large" }, { status: 413, headers: { "Cache-Control": "no-store" } }), req, config); } - const text = await req.text(); - if (Buffer.byteLength(text) > GUI_PAIRING_EXCHANGE_BODY_LIMIT) { + const bounded = await readBoundedRequestText(req, GUI_PAIRING_EXCHANGE_BODY_LIMIT); + if (bounded === null) { return withManagementCors(Response.json({ error: "pairing exchange body too large" }, { status: 413, headers: { "Cache-Control": "no-store" } }), req, config); } + const text = bounded; let body: unknown; try { body = JSON.parse(text); diff --git a/src/types/config.ts b/src/types/config.ts index a2438b8b51..e75f3e438b 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -254,7 +254,17 @@ export interface OcxHubConfig { export interface OcxRemoteGuiConfig { /** Exact Tailscale login identities permitted to receive an automatic remote GUI session. */ allowedTailscaleUsers?: string[]; - /** Explicitly permit one-time pairing exchange over non-loopback HTTP. */ + /** + * Retired. Once permitted a one-time pairing exchange over non-loopback plaintext HTTP. + * + * Still parsed so an existing config file keeps loading, but it grants nothing: a pairing + * grant now crosses loopback or authenticated HTTPS only. A persisted `true` is reported + * once and otherwise ignored. Kept in the type rather than deleted because the schema is + * strict — dropping the key outright would make an older config fail to load entirely, + * which is a worse outcome than ignoring one retired field. + * + * @deprecated has no effect; remove it from your config. + */ allowInsecureHttp?: boolean; } diff --git a/tests/cli-dispatch.test.ts b/tests/cli-dispatch.test.ts index 0ef0f26742..be2425bebd 100644 --- a/tests/cli-dispatch.test.ts +++ b/tests/cli-dispatch.test.ts @@ -2,11 +2,11 @@ import { describe, expect, spyOn, test } from "bun:test"; import { CLI_COMMANDS } from "../src/cli/registry"; import { DISPATCH_ALIASES, DISPATCH_COMMANDS, dispatchCommand, resolveDispatchCommand } from "../src/cli/dispatch"; import type { CliDispatchDeps } from "../src/cli/dispatch"; +import { runGuiCommand } from "../src/cli/gui"; import { existsSync, readFileSync } from "node:fs"; import { join } from "node:path"; import { getConfigDir } from "../src/config"; import { getAccountSet, removeCredential, saveCredential } from "../src/oauth/store"; -import { runGuiCommand } from "../src/cli/gui"; /** Minimal fake deps. dispatchCommand only touches deps for real command * runners, which these tests never invoke, so an empty object is enough. */ @@ -482,6 +482,10 @@ describe("doctor refuses --json rather than printing prose as success", () => { test("exact --json, --json=true, and Unicode dashes all exit 2", async () => { for (const flag of ["--json", "--json=true", "\u2014json"]) { expect(await runDoctor(flag), `${JSON.stringify(flag)} must be refused`).toBe(2); + } + }); +}); + describe("GUI command delegation", () => { const config = { port: 10100, diff --git a/tests/cli-registry.test.ts b/tests/cli-registry.test.ts index 70653fae24..7bbb1d5101 100644 --- a/tests/cli-registry.test.ts +++ b/tests/cli-registry.test.ts @@ -103,6 +103,8 @@ describe("CLI command registry parity", () => { const details = findCommand("system")?.details ?? []; expect(details).toContain("ocx system codex-cli-update check [--json]"); expect(details.some(line => line.includes("dry-run"))).toBe(false); + }); + test("GUI registry usage documents explicit-origin single-use pairing", () => { const gui = findCommand("gui"); expect(gui?.usage).toBe("ocx gui [pair --origin [--json]]"); diff --git a/tests/server-auth.test.ts b/tests/server-auth.test.ts index 5d4b95d104..7f4217b73c 100644 --- a/tests/server-auth.test.ts +++ b/tests/server-auth.test.ts @@ -4240,3 +4240,71 @@ describe("GET /v1/catalog remote data plane", () => { } }); }); + +describe("POST /opencodex-session pairing body bound", () => { + // This endpoint is reachable without a credential, so the body bound has to hold against a + // caller who controls the framing. The pre-check reads Content-Length, which the caller + // chooses: omit it and `Number(null ?? "0")` is 0, or send chunked and there is no header + // to read. Both used to pass the check and reach `req.text()`, which buffers whatever + // arrives — an unauthenticated caller decided how much memory the process spent. + + test("a chunked body with no Content-Length is bounded rather than buffered whole", async () => { + saveConfig(remoteCatalogConfig()); + const server = startServer(0); + try { + // 512 KiB against a 4 KiB limit, streamed so no Content-Length is sent. The stream + // reports how many chunks the server actually pulled: a bounded read stops early, an + // unbounded one drains all of them. + const chunkCount = 128; + const chunkBytes = 4 * 1024; + let pulled = 0; + const body = new ReadableStream({ + pull(controller) { + if (pulled >= chunkCount) { + controller.close(); + return; + } + pulled += 1; + controller.enqueue(new Uint8Array(chunkBytes).fill(0x61)); + }, + }); + + const response = await fetch(new URL("/opencodex-session", server.url), { + method: "POST", + headers: { "content-type": "application/json", Origin: "http://localhost" }, + body, + // Required by fetch for a streaming request body. + duplex: "half", + } as RequestInit & { duplex: "half" }); + + expect(response.status).toBe(413); + // The bound is what stopped it, not the peer running out of data. + expect(pulled).toBeLessThan(chunkCount); + } finally { + await server.stop(true); + } + }); + + test("a body exactly at the limit is still accepted for parsing", async () => { + saveConfig(remoteCatalogConfig()); + const server = startServer(0); + try { + // Exactly 4096 bytes of valid JSON: the bound must reject over-limit bodies without + // also rejecting one that sits on the limit. + const filler = "a".repeat(4096 - '{"grant":""}'.length); + const atLimit = `{"grant":"${filler}"}`; + expect(Buffer.byteLength(atLimit)).toBe(4096); + + const response = await fetch(new URL("/opencodex-session", server.url), { + method: "POST", + headers: { "content-type": "application/json", Origin: "http://localhost" }, + body: atLimit, + }); + + // 401, not 413: the body was read and parsed, and the grant simply does not exist. + expect(response.status).toBe(401); + } finally { + await server.stop(true); + } + }); +}); diff --git a/tests/server-management-auth.test.ts b/tests/server-management-auth.test.ts index 3eb35155f5..d7713e0cca 100644 --- a/tests/server-management-auth.test.ts +++ b/tests/server-management-auth.test.ts @@ -1022,7 +1022,11 @@ describe("management and data-plane credential separation", () => { )).toBeNull(); }); - test("insecure HTTP pairing is explicit and a refused exchange can be retried after opt-in", () => { + test("non-loopback plaintext HTTP cannot carry a pairing grant, and no opt-in re-opens it", () => { + // An earlier revision let this exchange succeed when `remoteGui.allowInsecureHttp` was + // true, and this test asserted exactly that. The flag is retired: a reusable grant on + // plaintext HTTP is readable by anything on the path, and the session it mints is + // reusable, so operator opt-in recorded a risk it could not bound. const config = hubConfig("http://hub.example.test"); const state = initializeManagementAuthState(config); if (!state.available) throw new Error("expected management auth state"); @@ -1032,13 +1036,32 @@ describe("management and data-plane credential separation", () => { method: "POST", headers: { Host: "hub.example.test", Origin: "https://dashboard.example.test" }, }); + expect(consumeGuiPairingGrant(request, { grant: created.grant }, config, state, now + 1)).toBeNull(); + // The grant SURVIVES the refusal. Rejecting before the grant is read is what stops an + // attacker who strips TLS from burning every code the operator prints. expect(state.pairingGrants.size).toBe(1); + + // The retired flag is still accepted by the schema so old configs load, and still grants + // nothing. config.remoteGui = { ...config.remoteGui, allowInsecureHttp: true }; - expect(consumeGuiPairingGrant(request, { grant: created.grant }, config, state, now + 2)).toMatchObject({ - issuance: "insecure-http-pairing", + expect(consumeGuiPairingGrant(request, { grant: created.grant }, config, state, now + 2)).toBeNull(); + expect(state.pairingGrants.size).toBe(1); + + // The same unspent grant still works over HTTPS, proving the refusal was about transport + // rather than the grant being invalidated. + const secureConfig = hubConfig("https://hub.example.test"); + const secureState = initializeManagementAuthState(secureConfig); + if (!secureState.available) throw new Error("expected management auth state"); + const secureGrant = createGuiPairingGrant("https://dashboard.example.test", secureConfig, secureState, now); + const secureRequest = new Request("https://hub.example.test/opencodex-session", { + method: "POST", + headers: { Host: "hub.example.test", Origin: "https://dashboard.example.test" }, }); - expect(state.pairingGrants.size).toBe(0); + expect(consumeGuiPairingGrant(secureRequest, { grant: secureGrant.grant }, secureConfig, secureState, now + 1)).toMatchObject({ + issuance: "pairing", + }); + expect(secureState.pairingGrants.size).toBe(0); }); test("pairing grant creation is bounded by a per-state rate limit", () => { From af95d597524265215cfd19e1ad5488a1a30ca3cc Mon Sep 17 00:00:00 2001 From: jun Date: Tue, 1 Sep 2026 18:07:43 +0900 Subject: [PATCH 051/172] fix(connect): preserve the sync handler exit code on the connected branch tests/cli-transport-honesty.test.ts flags any runner that awaits a handler and then returns a literal 0, because that erases a failure the handler recorded in process.exitCode. The exemption list requires a verified reason rather than a name, and the connected sync branch has none: handleConnectedSyncCatalogWrite drives app-server restarts, so a failure there must survive. Returns process.exitCode like every other runner. Node types it as number | string; only a numeric code is meaningful to the dispatcher. --- src/cli/dispatch.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cli/dispatch.ts b/src/cli/dispatch.ts index b792d560cd..b3ecc87daa 100644 --- a/src/cli/dispatch.ts +++ b/src/cli/dispatch.ts @@ -352,7 +352,12 @@ const commandRunners: Record = { ? "Hub unavailable; retained and applied the last-known-good remote catalog (stale)." : "Remote hub catalog synchronized."); await handleConnectedSyncCatalogWrite(result, restartCodex, restartDesktopApp); - return 0; + // `process.exitCode` rather than a literal 0, for the same reason every other + // runner does it (tests/cli-transport-honesty.test.ts): the catalog-write helper + // drives app-server restarts, and one of those recording a failure must not be + // erased by the value this runner returns. It reads 0 on the ordinary path. Node + // types it as `number | string`; only a numeric code means anything here. + return typeof process.exitCode === "number" ? process.exitCode : 0; } catch (error) { console.error(`Connected sync failed without local fallback: ${error instanceof Error ? error.message : String(error)}`); return 1; From dad17121d8cc747ef3c7076c2d5f370ef90497e2 Mon Sep 17 00:00:00 2001 From: jun Date: Tue, 1 Sep 2026 18:17:02 +0900 Subject: [PATCH 052/172] test(remote-gui): stub the prompt happy-dom does not implement Tests that clear a rejected session reach the admin-token fallback, which calls window.prompt. happy-dom does not implement it, so those tests died on a TypeError instead of asserting the behavior they were written for. Most tests in the file never reach the fallback, which is why it stayed hidden. A null-returning stub is the honest stand-in: it means "the operator dismissed the prompt", which is the path these tests want. Installed only when prompt is genuinely missing, so a real implementation is never shadowed. --- gui/tests/api-auth-memory.test.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/gui/tests/api-auth-memory.test.ts b/gui/tests/api-auth-memory.test.ts index 3003bbebba..834ecfa922 100644 --- a/gui/tests/api-auth-memory.test.ts +++ b/gui/tests/api-auth-memory.test.ts @@ -19,6 +19,14 @@ beforeEach(() => { fetch: { configurable: true, value: testWindow.fetch.bind(testWindow) }, }); originalPrompt = window.prompt; + // happy-dom does not implement `prompt`, so the admin-token fallback below throws a + // TypeError instead of returning null the moment a test actually reaches it. Most tests + // never do; the ones that clear a rejected session do, and they failed on a missing + // function rather than on the behavior they assert. A null-returning stub is the honest + // stand-in for "the operator dismissed the prompt". + if (typeof window.prompt !== "function") { + Object.defineProperty(testWindow, "prompt", { configurable: true, writable: true, value: () => null }); + } resetApiAuthFetchForTests(async () => { return window.prompt("OpenCodex admin token (OPENCODEX_ADMIN_AUTH_TOKEN)")?.trim() || null; }); From 20f3c11f819ff29e6b625aeecf754a66706f9945 Mon Sep 17 00:00:00 2001 From: jun Date: Tue, 1 Sep 2026 19:31:24 +0900 Subject: [PATCH 053/172] fix(connect): restore the catalog the user had, and stop calling a stuck profile a clean restore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two rollback defects. Both let disconnect report that native Codex state was restored while leaving the user worse off than before they connected. Connect overwrites whatever catalog is already at DEFAULT_CATALOG_PATH. The pre-connect bytes were snapshotted only into an in-memory `priorCatalog`, which covers a connect that fails and rolls back in the same run — not a disconnect, which is a different process on a different day. Durable state recorded only the remote catalog's fingerprint, so disconnect deleted the remote catalog and left the user with none. That is the one artifact a rollback cannot reconstruct from anywhere else: the token can be reissued and the config is journaled, but a catalog the user brought with them is simply gone. The snapshot is now persisted on the connection as `priorCatalog` (base64, or "" for "there genuinely was none") and disconnect writes it back. An older connection with the field absent keeps the previous removal behavior, since nothing recorded what to restore. Ownership is still checked first — a catalog edited since connect belongs to the user and `changed` refuses rather than overwriting it. The result gains `catalogRestored` so the two outcomes are distinguishable instead of both reading as `catalogRemoved`. restoreJournalState set profileRestored = true after a swallowed unlink. When the original profile was absent, "delete the one we generated" failing meant the function still reported complete, which deletes the journal — the only record that the leftover profile is ours. The user is told native state was restored while our profile stays on disk with nothing left pointing at it. Now only a verified removal counts, with ENOENT treated as success because the file being already gone is the outcome the removal wanted. The catalog fix carries a runtime regression driven red against the previous behavior. The profile fix is asserted source-level, and the test says why: making unlink fail requires denying writes on the Codex home, which denies the atomic config write earlier in the same function, so the branch is unreachable from a test process. Asserting a fabricated runtime failure would prove less than asserting the shape. --- src/client/connect.ts | 40 +++++++++++++++++++++++++++++++----- src/codex/journal.ts | 16 +++++++++++++-- src/config.ts | 3 +++ src/types/config.ts | 10 +++++++++ tests/client-connect.test.ts | 39 ++++++++++++++++++++++++++++++++--- tests/codex-journal.test.ts | 24 ++++++++++++++++++++++ 6 files changed, 122 insertions(+), 10 deletions(-) diff --git a/src/client/connect.ts b/src/client/connect.ts index 3d0acf4fc6..2bdeea9558 100644 --- a/src/client/connect.ts +++ b/src/client/connect.ts @@ -245,6 +245,10 @@ export async function connectClient( protocolVersion: 1, connectedAt: now, catalogEtag: catalog.etag, + // Durable so disconnect — a different process — can put back whatever was here + // before. `priorCatalog` above is only reachable by a connect that fails and rolls + // back in the same run. + priorCatalog: priorCatalog.kind === "file" ? Buffer.from(priorCatalog.body, "utf8").toString("base64") : "", catalogSyncedAt: now, }; commitClientConnection(connection); @@ -340,11 +344,28 @@ export async function syncConnectedClient( return { catalogWritten, cacheSynced, injected, stale }; } -function removeOwnedCatalog(connection: OcxClientConnectionConfig): "removed" | "absent" | "changed" { +/** + * Put the catalog back the way connect found it. + * + * Not a delete. Connect overwrites whatever catalog was already there, so removing the + * remote one leaves the user with nothing — and disconnect still reports that native Codex + * state was restored. If the connection recorded a prior catalog, it is rewritten; + * `priorCatalog: ""` means there genuinely was none and removal is the restoration. + * + * Still ownership-checked first: a catalog the user edited or replaced since connect is + * theirs, and `changed` refuses rather than overwriting it. + */ +function restorePriorCatalog(connection: OcxClientConnectionConfig): "removed" | "restored" | "absent" | "changed" { if (!existsSync(DEFAULT_CATALOG_PATH)) return "absent"; try { const body = validLocalCatalog(); if (!catalogMatchesEtag(body, connection.catalogEtag)) return "changed"; + if (connection.priorCatalog) { + atomicWriteFile(DEFAULT_CATALOG_PATH, Buffer.from(connection.priorCatalog, "base64").toString("utf8")); + return "restored"; + } + // Undefined means the connection predates this field: the pre-connect catalog was + // never recorded, so removal is the only honest option and matches the old behavior. unlinkSync(DEFAULT_CATALOG_PATH); return "removed"; } catch { @@ -354,7 +375,15 @@ function removeOwnedCatalog(connection: OcxClientConnectionConfig): "removed" | export async function disconnectClient( options: { keepCatalog?: boolean } = {}, -): Promise<{ restored: boolean; tokenRemoved: boolean; catalogRemoved: boolean; apiKeyId: string }> { +): Promise<{ + restored: boolean; + tokenRemoved: boolean; + /** True when the catalog no longer holds remote bytes: removed outright or overwritten. */ + catalogRemoved: boolean; + /** True only when a recorded pre-connect catalog was written back. */ + catalogRestored: boolean; + apiKeyId: string; +}> { const state = readClientConnectionState(); if (state.kind !== "connected") throw new Error(`disconnect refused: client state is ${state.kind}`); const token = readServiceApiTokenState(); @@ -393,9 +422,9 @@ export async function disconnectClient( const tokenRemoval = removeServiceApiTokenFileIfOwned(state.value.tokenFingerprint); if (tokenRemoval === "changed") throw new Error("disconnect refused: service token changed before removal"); - let catalogRemoval: "removed" | "absent" | "changed" = "absent"; + let catalogRemoval: "removed" | "restored" | "absent" | "changed" = "absent"; if (!options.keepCatalog) { - catalogRemoval = removeOwnedCatalog(state.value); + catalogRemoval = restorePriorCatalog(state.value); if (catalogRemoval === "changed") throw new Error("disconnect refused: catalog ownership changed"); } if (clearClientConnection(state.value.apiKeyId) !== "committed") { @@ -404,7 +433,8 @@ export async function disconnectClient( return { restored, tokenRemoved: tokenRemoval === "removed", - catalogRemoved: catalogRemoval === "removed", + catalogRemoved: catalogRemoval === "removed" || catalogRemoval === "restored", + catalogRestored: catalogRemoval === "restored", apiKeyId: state.value.apiKeyId, }; } diff --git a/src/codex/journal.ts b/src/codex/journal.ts index 68523fe685..f515f3aac3 100644 --- a/src/codex/journal.ts +++ b/src/codex/journal.ts @@ -210,10 +210,22 @@ export function restoreJournalState(): RestoreJournalResult { if (profileUnchanged) { if (journal.originalProfile !== null) { atomicWriteFile(CODEX_PROFILE_PATH, Buffer.from(journal.originalProfile, "base64").toString("utf-8")); + profileRestored = true; } else if (existsSync(CODEX_PROFILE_PATH)) { - try { unlinkSync(CODEX_PROFILE_PATH); } catch { /* ignore */ } + // "There was no profile before, so remove the one we generated." Claiming success + // without checking is how a caller ends up deleting the journal, reporting a clean + // restore, and leaving our profile on disk with nothing left that records it should + // not be there. ENOENT is the one benign outcome: the file is already gone, which is + // the state we wanted. + try { + unlinkSync(CODEX_PROFILE_PATH); + profileRestored = true; + } catch (error) { + profileRestored = (error as NodeJS.ErrnoException).code === "ENOENT"; + } + } else { + profileRestored = true; } - profileRestored = true; } const complete = configRestored && profileRestored; if (complete) removeJournal(); diff --git a/src/config.ts b/src/config.ts index f2411bc217..0755124640 100644 --- a/src/config.ts +++ b/src/config.ts @@ -936,6 +936,9 @@ const clientConnectionSchema = z.object({ protocolVersion: z.literal(1), connectedAt: clientTimestampSchema, catalogEtag: z.string().min(1).max(512).optional(), + // base64 of the pre-connect catalog, or "" for "there was none". Bounded above the + // catalog size cap so a legitimate snapshot round-trips. + priorCatalog: z.string().max(64 * 1024 * 1024).optional(), catalogSyncedAt: clientTimestampSchema.optional(), pendingOperation: z.object({ kind: z.literal("rotate"), diff --git a/src/types/config.ts b/src/types/config.ts index 48da5e7a60..0c867abfa8 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -281,6 +281,16 @@ export interface OcxClientConnectionConfig { protocolVersion: 1; connectedAt: string; catalogEtag?: string; + /** + * The catalog that was on disk before connect overwrote it, base64-encoded, or the + * empty string when there was none. + * + * Durable because disconnect runs in a different process than connect: an in-memory + * snapshot only covers a connect that fails and rolls back on the spot. Without this, + * disconnect deletes the remote catalog and reports a restored native state while the + * user's own catalog is simply gone. + */ + priorCatalog?: string; catalogSyncedAt?: string; pendingOperation?: { kind: "rotate"; diff --git a/tests/client-connect.test.ts b/tests/client-connect.test.ts index 6a172a1bd8..5d7edbb0f1 100644 --- a/tests/client-connect.test.ts +++ b/tests/client-connect.test.ts @@ -144,7 +144,10 @@ describe("remote hub client boundary", () => { }); }); -function runTransactionScenario(stage: "success" | "catalog" | "preflight" | "commit") { +/** A catalog the user already had before ever connecting. */ +const PRIOR_CATALOG_BYTES = '{"models":[{"slug":"local/only-model"}]}'; + +function runTransactionScenario(stage: "success" | "catalog" | "preflight" | "commit" | "prior-catalog") { const opencodexHome = mkdtempSync(join(tmpdir(), "ocx-client-connect-home-")); const codexHome = mkdtempSync(join(tmpdir(), "ocx-client-connect-codex-")); const configPath = join(opencodexHome, "config.json"); @@ -155,6 +158,10 @@ function runTransactionScenario(stage: "success" | "catalog" | "preflight" | "co }; writeFileSync(configPath, `${JSON.stringify(originalConfig, null, 2)}\n`, "utf8"); if (stage !== "preflight") writeFileSync(join(codexHome, "config.toml"), 'model_provider = "openai"\n', "utf8"); + // A catalog the user already had. Connect overwrites it; disconnect has to put it back. + if (stage === "prior-catalog") { + writeFileSync(join(codexHome, "opencodex-catalog.json"), PRIOR_CATALOG_BYTES, "utf8"); + } if (stage === "commit") { const { mkdirSync } = require("node:fs") as typeof import("node:fs"); mkdirSync(join(opencodexHome, "config-mutation.sqlite")); @@ -207,8 +214,9 @@ function runTransactionScenario(stage: "success" | "catalog" | "preflight" | "co credentialZeroed: credential.every(value => value === 0), }; let disconnected = null; - if (stage === "success" && connected) disconnected = await disconnectClient(); - console.log(JSON.stringify({ connected, error, beforeDisconnect, artifacts, disconnected, after: readClientConnectionState(), calls })); + if ((stage === "success" || stage === "prior-catalog") && connected) disconnected = await disconnectClient(); + const catalogAfter = existsSync(DEFAULT_CATALOG_PATH) ? readFileSync(DEFAULT_CATALOG_PATH, "utf8") : null; + console.log(JSON.stringify({ connected, error, beforeDisconnect, artifacts, disconnected, catalogAfter, after: readClientConnectionState(), calls })); })(); `; const result = spawnSync(process.execPath, ["--eval", script], { @@ -245,6 +253,31 @@ describe("connect transaction and offline disconnect", () => { } finally { run.cleanup(); } }); + test("disconnect puts back the catalog the user had before connecting", () => { + // Connect overwrites whatever catalog is already on disk. Disconnect used to delete the + // remote one and report that native Codex state was restored, which left a user who had + // their own catalog with no catalog at all — the one artifact a rollback cannot + // reconstruct from anywhere else. + const run = runTransactionScenario("prior-catalog"); + try { + expect(run.status).toBe(0); + expect(run.parsed.error).toBeNull(); + expect(run.parsed.disconnected).toMatchObject({ catalogRestored: true, catalogRemoved: true }); + expect(run.parsed.catalogAfter).toBe(PRIOR_CATALOG_BYTES); + expect(run.parsed.after).toEqual({ kind: "disconnected" }); + } finally { run.cleanup(); } + }); + + test("disconnect removes the catalog when the user had none", () => { + // The other half of the same contract: `priorCatalog: ""` records "there genuinely was + // none", so removal IS the restoration and must not be mistaken for a lost file. + const run = runTransactionScenario("success"); + try { + expect(run.parsed.disconnected).toMatchObject({ catalogRemoved: true, catalogRestored: false }); + expect(run.parsed.catalogAfter).toBeNull(); + } finally { run.cleanup(); } + }); + for (const stage of ["catalog", "preflight", "commit"] as const) { test(`rolls back local artifacts when ${stage} fails before final commit`, () => { const run = runTransactionScenario(stage); diff --git a/tests/codex-journal.test.ts b/tests/codex-journal.test.ts index cdb59121ed..5eb2d3152d 100644 --- a/tests/codex-journal.test.ts +++ b/tests/codex-journal.test.ts @@ -652,4 +652,28 @@ describe("codex-journal", () => { runScript(testDir, `require("./src/codex/journal").writeJournal(); console.log("done");`); expect(existsSync(join(testDir, "opencodex-journal.json"))).toBe(false); }); + + test("a restore that leaves the profile behind never reports complete (source-level)", () => { + // "There was no profile before, so delete the one we generated." When that unlink + // fails, reporting success also deletes the journal — the only record that the leftover + // profile is ours — and disconnect then tells the user native state was restored. + // + // Source-level because the failure is not reachable from a test process: making unlink + // fail requires denying writes on the Codex home, and that denies the atomic config + // write earlier in the same function, so the call throws before the branch runs. + // Asserting the shape is honest about what is being checked; asserting a fabricated + // runtime failure would not be. + const source = readFileSync(join(repoRoot, "src/codex/journal.ts"), "utf8"); + const restore = source.slice(source.indexOf("export function restoreJournalState")); + const body = restore.slice(0, restore.indexOf("\nexport ")); + + // The unlink result must decide profileRestored. The pre-fix shape set it + // unconditionally after a swallowed try/catch. + expect(body).not.toMatch(/catch \{ \/\* ignore \*\/ \}\s*\n\s*\}\s*\n\s*profileRestored = true;/); + // ENOENT is the one benign unlink failure: the file is already gone, which is the + // outcome the removal wanted. + expect(body).toContain('=== "ENOENT"'); + // And completeness still gates journal deletion. + expect(body).toContain("if (complete) removeJournal();"); + }); }); From 1d99dd50b78fcee55f74a90f5210dcf2d69abce0 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 03:26:29 +0900 Subject: [PATCH 054/172] feat(two-plane): filter hub usage by client key --- src/server/management/logs-usage-routes.ts | 3 +- src/usage/summary.ts | 32 ++++++++++++--- tests/api-usage.test.ts | 34 ++++++++++++++++ tests/usage-summary.test.ts | 45 ++++++++++++++++++++++ 4 files changed, 108 insertions(+), 6 deletions(-) diff --git a/src/server/management/logs-usage-routes.ts b/src/server/management/logs-usage-routes.ts index 97dd327208..03d8f96f59 100644 --- a/src/server/management/logs-usage-routes.ts +++ b/src/server/management/logs-usage-routes.ts @@ -202,10 +202,11 @@ export async function handleLogsUsageRoutes(ctx: ManagementContext): Promise(summary: T, entries?: PersistedUsageEntry[]) => projectUsageSummary(summary, filter, entries); - const filterRequested = Boolean(filter.provider ?? filter.model); + const filterRequested = Boolean(filter.provider ?? filter.model ?? filter.apiKeyId); const now = Date.now(); try { const cacheKey = `${range}:${surface}`; diff --git a/src/usage/summary.ts b/src/usage/summary.ts index cc3161aa16..58560c3552 100644 --- a/src/usage/summary.ts +++ b/src/usage/summary.ts @@ -156,6 +156,7 @@ export interface UsageSummary { export interface UsageFilterEcho { provider: string | null; model: string | null; + apiKeyId: string | null; matched: boolean; /** * True when a retained row came from a combo attribution. Cost partitions @@ -1129,6 +1130,11 @@ function normalizeFilterValue(input: string | null | undefined): string | null { return trimmed === "" ? null : trimmed.toLowerCase(); } +function normalizeExactFilterValue(input: string | null | undefined): string | null { + const trimmed = typeof input === "string" ? input.trim() : ""; + return trimmed === "" ? null : trimmed; +} + /** * Narrow an already-summarised window to one provider and/or model. * @@ -1152,12 +1158,13 @@ function normalizeFilterValue(input: string | null | undefined): string | null { */ export function projectUsageSummary( summary: T, - filter: { provider?: string | null; model?: string | null }, + filter: { provider?: string | null; model?: string | null; apiKeyId?: string | null }, entries?: PersistedUsageEntry[], ): T & { filter?: UsageFilterEcho } { const provider = normalizeFilterValue(filter.provider); const model = normalizeFilterValue(filter.model); - if (provider === null && model === null) return summary; + const apiKeyId = normalizeExactFilterValue(filter.apiKeyId); + if (provider === null && model === null && apiKeyId === null) return summary; const matches = (rowProvider: string, rowModel: string): boolean => { if (provider !== null && baseProviderLabel(rowProvider).toLowerCase() !== provider) return false; @@ -1165,7 +1172,14 @@ export function projectUsageSummary( return true; }; - const source = entries ?? []; + // The apiKeyId filter drops whole ENTRIES, because a key owns the entry rather than any + // individual attempt within it. Provider and model filters below narrow to matching + // ATTRIBUTIONS instead: keeping a whole combo entry because one attempt matched drags the + // other attempts' tokens and cost into the filtered totals, so a two-attempt combo + // filtered to its cheap model would report the expensive model's spend too. + const source = apiKeyId === null + ? entries ?? [] + : (entries ?? []).filter(entry => entry.apiKeyId === apiKeyId); let comboOverlap = false; const filtered: PersistedUsageEntry[] = []; for (const entry of source) { @@ -1194,7 +1208,15 @@ export function projectUsageSummary( days: projected.days.map(day => ({ ...day, models: day.models.filter(row => matches(row.provider, row.model)) })), models, providers: projected.providers.filter(row => retainedProviders.has(row.provider)), - accounts: [], - filter: { provider, model, matched, comboOverlap }, + // Account rows are not provider-partitioned in a way this projection could + // honestly re-derive, and unfiltered account totals sitting beside filtered + // model totals would invite exactly the wrong reading — so a provider or model + // filter drops them. + // + // An apiKeyId-only filter is different: it selects whole entries, so the account + // rows projected from those entries are exactly the accounts that key used. They + // are honest under that filter and are kept. + accounts: provider === null && model === null ? projected.accounts : [], + filter: { provider, model, apiKeyId, matched, comboOverlap }, }; } diff --git a/tests/api-usage.test.ts b/tests/api-usage.test.ts index 781412a331..21d8e1db63 100644 --- a/tests/api-usage.test.ts +++ b/tests/api-usage.test.ts @@ -592,6 +592,40 @@ describe("GET /api/usage", () => { } }); + test("apiKeyId is an exact projection and composes with provider and model filters", async () => { + const now = Date.now(); + const rows = [ + { requestId: "a-openai", timestamp: now, apiKeyId: "Key-A", provider: "openai", model: "gpt-5.5", status: 200, durationMs: 1, usageStatus: "reported", usage: { inputTokens: 10, outputTokens: 2 }, totalTokens: 12 }, + { requestId: "a-anthropic", timestamp: now, apiKeyId: "Key-A", provider: "anthropic", model: "claude-x", status: 200, durationMs: 1, usageStatus: "reported", usage: { inputTokens: 20, outputTokens: 3 }, totalTokens: 23 }, + { requestId: "b", timestamp: now, apiKeyId: "key-a", provider: "openai", model: "gpt-5.5", status: 200, durationMs: 1, usageStatus: "reported", usage: { inputTokens: 30, outputTokens: 4 }, totalTokens: 34 }, + { requestId: "legacy", timestamp: now, provider: "openai", model: "gpt-5.5", status: 200, durationMs: 1, usageStatus: "reported", usage: { inputTokens: 40, outputTokens: 5 }, totalTokens: 45 }, + ]; + writeFileSync(join(testDir, "usage.jsonl"), `${rows.map(row => JSON.stringify(row)).join("\n")}\n`); + const server = startServer(0); + try { + const own = await fetch(new URL("/api/usage?range=all&apiKeyId=Key-A", server.url)).then(res => res.json()); + expect(own.filter).toMatchObject({ apiKeyId: "Key-A", provider: null, model: null, matched: true }); + expect(own.summary.requests).toBe(2); + + const combined = await fetch(new URL("/api/usage?range=all&apiKeyId=Key-A&provider=openai&model=gpt-5.5", server.url)).then(res => res.json()); + expect(combined.summary.requests).toBe(1); + expect(combined.models).toHaveLength(1); + + const exactCase = await fetch(new URL("/api/usage?range=all&apiKeyId=key-a", server.url)).then(res => res.json()); + expect(exactCase.summary.requests).toBe(1); + + const missing = await fetch(new URL("/api/usage?range=all&apiKeyId=missing", server.url)).then(res => res.json()); + expect(missing.filter).toMatchObject({ apiKeyId: "missing", matched: false }); + expect(missing.summary.requests).toBe(0); + + const unfiltered = await fetch(new URL("/api/usage?range=all", server.url)).then(res => res.json()); + expect(unfiltered.filter).toBeUndefined(); + expect(unfiltered.summary.requests).toBe(4); + } finally { + await server.stop(true); + } + }); + test("the filter is applied on the cache-hit path too", async () => { writeFixture(Date.now()); const server = startServer(0); diff --git a/tests/usage-summary.test.ts b/tests/usage-summary.test.ts index 0b83e071fe..3811615a08 100644 --- a/tests/usage-summary.test.ts +++ b/tests/usage-summary.test.ts @@ -28,6 +28,7 @@ function entry(overrides: Partial & { ts: number }): Persis ...(rest.usage ? { usage: rest.usage } : {}), ...(rest.totalTokens !== undefined ? { totalTokens: rest.totalTokens } : {}), ...(rest.attempts ? { attempts: rest.attempts } : {}), + ...(rest.apiKeyId !== undefined ? { apiKeyId: rest.apiKeyId } : {}), }; } @@ -398,6 +399,50 @@ describe("projectUsageSummary", () => { expect(wider.summary.requests).toBe(1); expect(wider.filter?.matched).toBe(true); }); + + test("filters by exact api key id before provider and model attribution", () => { + const entries = [ + entry({ ts: at, requestId: "key-a-openai", apiKeyId: "Key-A", provider: "openai", model: "gpt-5.5", usageStatus: "reported", usage: priced, accountLogLabel: "account-a" }), + entry({ ts: at + 1, requestId: "key-a-anthropic", apiKeyId: "Key-A", provider: "anthropic", model: "claude-opus", usageStatus: "reported", usage: priced, accountLogLabel: "account-b" }), + entry({ ts: at + 2, requestId: "key-b", apiKeyId: "key-a", provider: "openai", model: "gpt-5.5", usageStatus: "reported", usage: priced, accountLogLabel: "account-c" }), + entry({ ts: at + 3, requestId: "legacy", provider: "openai", model: "gpt-5.5", usageStatus: "reported", usage: priced }), + ]; + const summary = summarizeUsage(entries, "30d", at + 4); + + const byKey = projectUsageSummary(summary, { apiKeyId: " Key-A " }, entries); + expect(byKey.filter).toMatchObject({ apiKeyId: "Key-A", provider: null, model: null, matched: true }); + expect(byKey.summary.requests).toBe(2); + expect(byKey.models).toHaveLength(2); + expect(byKey.providers).toHaveLength(2); + expect(byKey.accounts.map(row => row.accountLogLabel).sort()).toEqual(["account-a", "account-b"]); + + const combined = projectUsageSummary(summary, { + apiKeyId: "Key-A", + provider: "OPENAI", + model: "GPT-5.5", + }, entries); + expect(combined.summary.requests).toBe(1); + expect(combined.models).toHaveLength(1); + expect(combined.accounts).toEqual([]); + + const wrongCase = projectUsageSummary(summary, { apiKeyId: "key-a" }, entries); + expect(wrongCase.summary.requests).toBe(1); + expect(wrongCase.filter?.apiKeyId).toBe("key-a"); + }); + + test("an absent api key id excludes legacy and environment-token rows", () => { + const entries = [entry({ ts: at, requestId: "legacy", usageStatus: "reported", usage: priced })]; + const projected = projectUsageSummary( + summarizeUsage(entries, "30d", at + 1), + { apiKeyId: "missing-key" }, + entries, + ); + expect(projected.filter).toMatchObject({ apiKeyId: "missing-key", matched: false }); + expect(projected.summary.requests).toBe(0); + expect(projected.models).toEqual([]); + expect(projected.providers).toEqual([]); + expect(projected.accounts).toEqual([]); + }); }); describe("parseUsageSurface", () => { From 872b94549966f8e50c5355e14c4b1fe54dac7d2f Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 03:40:12 +0900 Subject: [PATCH 055/172] feat(two-plane): add client machine and hub GUI planes --- gui/src/App.tsx | 83 +++- gui/src/api-targets.ts | 119 +++++ gui/src/api.ts | 457 ++++++++---------- .../storage-workspace/StorageWorkspace.tsx | 8 +- gui/src/connect-pairing.ts | 74 +++ gui/src/i18n/de.ts | 25 + gui/src/i18n/en.ts | 25 + gui/src/i18n/fr.ts | 25 + gui/src/i18n/ja.ts | 25 + gui/src/i18n/ko.ts | 25 + gui/src/i18n/ru.ts | 25 + gui/src/i18n/tr.ts | 25 + gui/src/i18n/zh-TW.ts | 25 + gui/src/i18n/zh.ts | 25 + gui/src/pages/Integrations.tsx | 35 +- gui/src/pages/Startup.tsx | 40 +- gui/src/pages/Storage.tsx | 2 +- gui/src/pages/Usage.tsx | 44 +- gui/src/stop-proxy.ts | 6 +- gui/src/styles-usage-workspace.css | 17 + src/cli/index.ts | 12 +- src/client/hub-relay.ts | 195 ++++++++ src/client/machine-api.ts | 139 ++++++ src/client/machine-auth.ts | 54 +++ src/client/machine-listener.ts | 130 +++++ src/client/runtime.ts | 76 +++ 26 files changed, 1411 insertions(+), 305 deletions(-) create mode 100644 gui/src/api-targets.ts create mode 100644 gui/src/connect-pairing.ts create mode 100644 src/client/hub-relay.ts create mode 100644 src/client/machine-api.ts create mode 100644 src/client/machine-auth.ts create mode 100644 src/client/machine-listener.ts create mode 100644 src/client/runtime.ts diff --git a/gui/src/App.tsx b/gui/src/App.tsx index 73648675e8..e3d3124073 100644 --- a/gui/src/App.tsx +++ b/gui/src/App.tsx @@ -15,15 +15,15 @@ import { SidebarGithubRow } from "./components/sidebar-github-row"; import { IconGrid, IconServer, IconBoxes, IconBot, IconList, IconActivity, IconHardDrive, IconKey, IconMenu, IconSun, IconMoon, IconMonitor, IconGlobe, IconPower, IconX, IconRefresh} from "./icons"; import { useI18n, useT, LOCALES, localeDisplayName, type Locale, type TKey } from "./i18n/shared"; import { Select } from "./ui"; -import { installApiAuthFetch } from "./api"; +import { configureApiTargets, hasApiSession, installApiAuthFetch } from "./api"; +import { apiBaseForPlane, discoverApiTargets, standaloneApiTargets, type ApiTargets } from "./api-targets"; +import { ConnectPairingForm } from "./connect-pairing"; import { type Page } from "./app-routing"; import { readModelsTab, type ModelsTab } from "./pages/models-tab"; import { useAppRouteState } from "./use-app-route-state"; import { requestProxyStop } from "./stop-proxy"; import { useCodexRestart } from "./use-codex-restart"; -installApiAuthFetch(); - type Theme = "light" | "dark" | "system"; const PAGE_TKEY: Record = { @@ -40,6 +40,9 @@ const PAGE_TKEY: Record = { }; const API_BASE = import.meta.env.VITE_API_BASE || ""; +const INITIAL_TARGETS = standaloneApiTargets(API_BASE); +configureApiTargets(INITIAL_TARGETS); +installApiAuthFetch(); const THEME_KEY = "ocx-theme"; /** @@ -101,6 +104,28 @@ export default function App() { const [theme, setTheme] = useState(readStoredTheme); const { locale, setLocale } = useI18n(); const t = useT(); + const [targets, setTargets] = useState(INITIAL_TARGETS); + const [targetsSettled, setTargetsSettled] = useState(false); + const [targetError, setTargetError] = useState(false); + const [sharedSessionReady, setSharedSessionReady] = useState(() => hasApiSession("shared")); + + useEffect(() => { + const controller = new AbortController(); + void discoverApiTargets(API_BASE, controller.signal).then(next => { + configureApiTargets(next); + setTargets(next); + setSharedSessionReady(hasApiSession("shared")); + setTargetError(false); + setTargetsSettled(true); + }).catch(() => { + if (controller.signal.aborted) return; + setTargetError(true); + setTargetsSettled(true); + }); + return () => controller.abort(); + }, []); + const machineBase = apiBaseForPlane("machine", targets); + const sharedBase = apiBaseForPlane("shared", targets); // Narrow screens: the sidebar becomes an off-canvas drawer behind a hamburger toggle. const [navOpen, setNavOpen] = useState(false); @@ -126,14 +151,14 @@ export default function App() { }, [theme]); const healthPoll = useKeyedClientResource( - `app-healthz:${API_BASE}`, - [], + `app-healthz:${machineBase}`, + [machineBase, targetsSettled], async (signal) => { - const res = await fetch(`${API_BASE}/healthz`, { signal }); + const res = await fetch(`${machineBase}/healthz`, { signal }); if (!res.ok) return null; return readRuntimeVersion(await res.json()); }, - { pollMs: 30_000 }, + { pollMs: 30_000, enabled: targetsSettled }, ); const cycleTheme = () => setTheme(t => (t === "light" ? "dark" : t === "dark" ? "system" : "light")); @@ -175,15 +200,16 @@ export default function App() { // sharing a controller — the backend is already single-flight, so what is missing // is invalidation, not mutual exclusion. const [codexRestartEpoch, setCodexRestartEpoch] = useState(0); - const { restarting: codexRestarting, restart: handleCodexRestart } = useCodexRestart(API_BASE, { + const { restarting: codexRestarting, restart: handleCodexRestart } = useCodexRestart(sharedBase, { onSettled: () => setCodexRestartEpoch(epoch => epoch + 1), }); const handleStop = async () => { if (!confirm(t("dash.stopConfirm"))) return; setStopping(true); - const outcome = await requestProxyStop(API_BASE, { + const outcome = await requestProxyStop(machineBase, { formatFailure: status => t("dash.stopFailed", { status: String(status) }), + mode: targets.connected ? "client" : "standalone", }); // Refusals and restore failures return normally instead of dropping the connection. // In both cases the proxy did not reach a clean-stop result, so re-enable the control @@ -214,7 +240,7 @@ export default function App() { {brand}
{ // The update dialog lives on the dashboard maintenance panel. Deep-link to // `#dashboard/update` and let the dashboard own the check/run flow — no @@ -328,16 +354,27 @@ export default function App() { detailsLabel={t("errorBoundary.details")} reloadLabel={t("errorBoundary.reload")} > - {page === "dashboard" && } - {page === "startup" && } - {page === "providers" && } - {page === "models" && } - {page === "subagents" && } - {page === "logs" && } - {page === "usage" && } - {page === "storage" && } - {page === "codex-set" && } - {page === "integrations" && } + {!targetsSettled ? ( +
{t("connection.discovering")}
+ ) : targetError ? ( +
{t("connection.machineUnavailable")}
+ ) : ( + <> + {targets.connected && !sharedSessionReady && ( + setSharedSessionReady(true)} /> + )} + {page === "dashboard" && } + {page === "startup" && } + {page === "providers" && } + {page === "models" && } + {page === "subagents" && } + {page === "logs" && } + {page === "usage" && } + {page === "storage" && } + {page === "codex-set" && } + {page === "integrations" && } + + )}
diff --git a/gui/src/api-targets.ts b/gui/src/api-targets.ts new file mode 100644 index 0000000000..f71fd63964 --- /dev/null +++ b/gui/src/api-targets.ts @@ -0,0 +1,119 @@ +export type ApiPlane = "machine" | "shared"; +export type SharedTransport = "same-origin" | "direct" | "relay"; + +export interface ApiTarget { + id: ApiPlane; + baseUrl: string; + serverOrigin: string; + bootstrapPath: string; + transport: SharedTransport; +} + +export interface ApiTargets { + connected: boolean; + machine: ApiTarget; + shared: ApiTarget; + apiKeyId?: string; +} + +export interface MachineStatusV1 { + mode: "client"; + connected: true; + machineBase: string; + sharedBase: string; + sharedServerOrigin: string; + managementTransport: "direct" | "relay"; + apiKeyId: string; + protocolVersion: 1; + connectedAt: string; + catalogSyncedAt?: string; + hubReachability: "unknown" | "online" | "offline" | "unauthorized"; +} + +function trimBase(value: string): string { + return value.replace(/\/+$/, ""); +} + +function absoluteBase(value: string): URL { + return new URL(value || "/", window.location.href); +} + +function canonicalOrigin(value: string): string | null { + try { + const url = new URL(value); + if ((url.protocol !== "http:" && url.protocol !== "https:") + || url.username || url.password || url.pathname !== "/" || url.search || url.hash) return null; + return url.origin; + } catch { + return null; + } +} + +function target(id: ApiPlane, baseUrl: string, serverOrigin: string, transport: SharedTransport): ApiTarget { + const base = trimBase(baseUrl); + return { id, baseUrl: base, serverOrigin, bootstrapPath: `${base}/opencodex-session`, transport }; +} + +export function standaloneApiTargets(initialBase: string): ApiTargets { + const resolved = absoluteBase(initialBase); + const baseUrl = trimBase(initialBase); + return { + connected: false, + machine: target("machine", baseUrl, resolved.origin, "same-origin"), + shared: target("shared", baseUrl, resolved.origin, "same-origin"), + }; +} + +function validStatus(value: unknown): value is MachineStatusV1 { + if (!value || typeof value !== "object" || Array.isArray(value)) return false; + const row = value as Record; + return row.mode === "client" && row.connected === true && row.protocolVersion === 1 + && (row.managementTransport === "direct" || row.managementTransport === "relay") + && typeof row.machineBase === "string" && typeof row.sharedBase === "string" + && typeof row.sharedServerOrigin === "string" && typeof row.apiKeyId === "string" + && row.apiKeyId.trim().length > 0 && typeof row.connectedAt === "string"; +} + +export function relayUrlForPath(shared: ApiTarget, path: string): string { + if (shared.transport !== "relay" || (!path.startsWith("/api/") && path !== "/opencodex-session")) { + throw new TypeError("path is not eligible for the fixed hub relay"); + } + if (path.startsWith("//") || path.includes("\\") || /%(?:2f|5c|2e)/i.test(path) || path.includes("#")) { + throw new TypeError("encoded or authority relay path refused"); + } + return `${trimBase(shared.baseUrl)}${path}`; +} + +export function targetsFromMachineStatus(initialBase: string, status: MachineStatusV1): ApiTargets { + if (!validStatus(status)) throw new TypeError("machine status response is invalid"); + const initial = standaloneApiTargets(initialBase); + const machineOrigin = canonicalOrigin(status.machineBase); + const sharedOrigin = canonicalOrigin(status.sharedServerOrigin); + if (!machineOrigin || machineOrigin !== initial.machine.serverOrigin || !sharedOrigin) { + throw new TypeError("machine status target origins are invalid"); + } + const machine = target("machine", trimBase(initialBase), machineOrigin, "same-origin"); + const shared = status.managementTransport === "relay" + ? target("shared", `${trimBase(initialBase)}/api/machine/hub-relay`, sharedOrigin, "relay") + : target("shared", sharedOrigin, sharedOrigin, "direct"); + return { connected: true, machine, shared, apiKeyId: status.apiKeyId }; +} + +export function apiBaseForPlane(plane: ApiPlane, targets: ApiTargets): string { + return targets[plane].baseUrl; +} + +export async function discoverApiTargets(initialBase: string, signal?: AbortSignal): Promise { + const standalone = standaloneApiTargets(initialBase); + let response: Response; + try { + response = await fetch(`${standalone.machine.baseUrl}/api/machine/status`, { signal, cache: "no-store" }); + } catch (error) { + throw new Error("local machine plane unavailable", { cause: error }); + } + if (response.status === 404) return standalone; + if (!response.ok) throw new Error(`local machine plane refused discovery (${response.status})`); + const body = await response.json().catch(() => null); + if (!validStatus(body)) throw new Error("local machine plane returned invalid status"); + return targetsFromMachineStatus(initialBase, body); +} diff --git a/gui/src/api.ts b/gui/src/api.ts index cce2f98951..28ff9d0bcd 100644 --- a/gui/src/api.ts +++ b/gui/src/api.ts @@ -1,299 +1,257 @@ import { promptForAdminToken, type AdminTokenVerifier } from "./admin-token-dialog"; import { createBoundedFetch } from "./bounded-fetch"; +import { standaloneApiTargets, type ApiPlane, type ApiTarget, type ApiTargets } from "./api-targets"; -let installed = false; -/** Shared 401 refresh gate — concurrent waiters join one prompt / token resolution. */ -let resolutionInFlight: Promise | null = null; -/** Unwrapped fetch captured at install time — used for session re-bootstrap so the - * bootstrap document request itself never enters the 401 handling path. */ -let rawFetch: typeof fetch | null = null; -/** - * After the user cancels (or submits blank) once, suppress further prompts for this page - * lifetime so a staggered 401 fan-out does not reopen the dialog N times (#647 / Codex). - * A full reload clears module state and allows prompting again. - */ -let promptCancelled = false; - -type AdminTokenPrompt = (verifyToken: AdminTokenVerifier) => Promise; -let requestAdminToken: AdminTokenPrompt = promptForAdminToken; - -/** - * Document path re-fetched to mint a fresh loopback GUI session (server injects meta tags). - * Deliberately NOT "/": the Vite dev server owns that route for the app shell, so the dev - * proxy forwards this dedicated extensionless path to the backend with the original host. - */ -const SESSION_REBOOTSTRAP_PATH = "/opencodex-session"; -/** Safe authenticated read used to validate a raw admin token before closing the sign-in form. */ +const LEGACY_TOKEN_KEY = "opencodex-api-token"; const ADMIN_TOKEN_VALIDATION_PATH = "/api/settings"; - -/** - * The silent re-bootstrap must fail fast: every /api/* request queues behind the - * shared resolution, so an unbounded bootstrap hangs the whole dashboard (H2). - */ const SESSION_REBOOTSTRAP_TIMEOUT_MS = 10_000; -let rebootstrapTimeoutMs = SESSION_REBOOTSTRAP_TIMEOUT_MS; - -/** - * Whole-resolution watchdog. The bootstrap bound covers a well-behaved fetch; this - * covers everything else — a fetch that never honors the abort, a prompt path that - * pends without settling, any surprise inside the shared body. Without it one stuck - * resolution pins every /api/* waiter for the page lifetime, which is the exact - * failure this module exists to kill. - * - * Scope note: the watchdog races the BOOTSTRAP CALL ONLY, never the admin-token - * prompt. The prompt is user-controlled and unbounded by design; while its body - * pends, later waves join the same resolution, which is what keeps a single dialog - * on screen (promptForAdminToken has no singleton guard — a watchdog that fired - * during the prompt would stack a fresh modal every cycle). - */ const RESOLUTION_WATCHDOG_MS = 15_000; -let resolutionWatchdogMs = RESOLUTION_WATCHDOG_MS; +const MACHINE_SESSION_HEADER = "X-OpenCodex-Machine-Session"; +const MACHINE_GUI_ORIGIN_HEADER = "X-OpenCodex-Machine-GUI-Origin"; +const MACHINE_CSRF_HEADER = "X-OpenCodex-Machine-CSRF-Token"; + +interface ApiSessionState { + token: string | null; + csrfToken: string | null; + browserOrigin: string | null; + serverOrigin: string | null; +} -function needsApiAuth(input: RequestInfo | URL): boolean { - try { - const raw = input instanceof Request ? input.url : String(input); - const url = new URL(raw, window.location.href); - const admittedOrigin = memoryToken?.startsWith("ocx_session_") - ? memorySessionServerOrigin - : window.location.origin; - // A session is destination-bound. Third-party origins get neither credentials - // nor the local admin-token prompt. - if (!admittedOrigin || url.origin !== admittedOrigin) return false; - return url.pathname.startsWith("/api/"); - } catch { - return false; - } +interface TargetRuntime { + target: ApiTarget; + session: ApiSessionState; + resolutionInFlight: Promise | null; + promptCancelled: boolean; } -/** Legacy sessionStorage key from pre-memory auth — wiped once on install, never read. */ -const LEGACY_TOKEN_KEY = "opencodex-api-token"; +type AdminTokenPrompt = (verifyToken: AdminTokenVerifier) => Promise; +type RebootstrapResult = { kind: "minted"; token: string } | { kind: "unavailable" } | { kind: "failed" }; -/** In-memory only — never write tokens to web storage (XSS can read sessionStorage/localStorage). */ -let memoryToken: string | null = null; -let memoryCsrfToken: string | null = null; -let memorySessionBrowserOrigin: string | null = null; -let memorySessionServerOrigin: string | null = null; +let installed = false; +let rawFetch: typeof fetch | null = null; +let configuredTargets: ApiTargets | null = null; +let requestAdminToken: AdminTokenPrompt = promptForAdminToken; +let rebootstrapTimeoutMs = SESSION_REBOOTSTRAP_TIMEOUT_MS; +let resolutionWatchdogMs = RESOLUTION_WATCHDOG_MS; +const runtimes = new Map(); -function readToken(): string | null { - return memoryToken; +function blankSession(): ApiSessionState { + return { token: null, csrfToken: null, browserOrigin: null, serverOrigin: null }; } -function storeToken(token: string): void { - memoryToken = token; +function ensureTargets(): ApiTargets { + if (!configuredTargets) configureApiTargets(standaloneApiTargets("")); + return configuredTargets!; } -function clearToken(): void { - memoryToken = null; - memoryCsrfToken = null; - memorySessionBrowserOrigin = null; - memorySessionServerOrigin = null; +function sameTarget(left: ApiTarget, right: ApiTarget): boolean { + return left.baseUrl === right.baseUrl && left.serverOrigin === right.serverOrigin && left.transport === right.transport; } -function takeMetaContent(name: string): string | null { - const element = document.querySelector(`meta[name="${name}"]`) as HTMLMetaElement | null; - const content = element?.content.trim() || null; - element?.remove(); - return content; +export function configureApiTargets(targets: ApiTargets): void { + configuredTargets = targets; + for (const plane of ["machine", "shared"] as const) { + const current = runtimes.get(plane); + runtimes.set(plane, current && sameTarget(current.target, targets[plane]) + ? { ...current, target: targets[plane] } + : { target: targets[plane], session: blankSession(), resolutionInFlight: null, promptCancelled: false }); + } } -function loadInjectedSession(): void { - const token = takeMetaContent("opencodex-session-token"); - const csrfToken = takeMetaContent("opencodex-session-csrf"); - const browserOrigin = takeMetaContent("opencodex-session-origin"); - const serverOrigin = takeMetaContent("opencodex-session-server-origin"); - storeSession(token, csrfToken, browserOrigin, serverOrigin, window.location.origin); +function runtime(plane: ApiPlane): TargetRuntime { + ensureTargets(); + return runtimes.get(plane)!; } -/** Clear memory only when it still holds `expected` (avoid wiping a newer concurrent store). */ -function clearTokenIfCurrent(expected: string | null): void { - if (expected != null && readToken() === expected) clearToken(); +function clearSessionIfCurrent(plane: ApiPlane, expected: string | null): void { + const state = runtime(plane); + if (expected !== null && state.session.token === expected) state.session = blankSession(); } -/** Validate and store a server-minted GUI session; rejects anything bound to another origin. */ function storeSession( + plane: ApiPlane, token: string | null, csrfToken: string | null, browserOrigin: string | null, serverOrigin: string | null, - expectedServerOrigin: string, ): boolean { - if ( - !token?.startsWith("ocx_session_") - || !csrfToken - || browserOrigin !== window.location.origin - || serverOrigin !== expectedServerOrigin - ) { - clearToken(); + const state = runtime(plane); + if (!token?.startsWith("ocx_session_") || !csrfToken + || browserOrigin !== window.location.origin || serverOrigin !== state.target.serverOrigin) { + state.session = blankSession(); return false; } - memoryToken = token; - memoryCsrfToken = csrfToken; - memorySessionBrowserOrigin = browserOrigin; - memorySessionServerOrigin = serverOrigin; + state.session = { token, csrfToken, browserOrigin, serverOrigin }; + state.promptCancelled = false; return true; } -/** Read one named meta tag out of a served HTML document (attribute order varies). */ +export function hasApiSession(plane: ApiPlane): boolean { + return Boolean(runtime(plane).session.token?.startsWith("ocx_session_")); +} + +function takeMetaContent(name: string): string | null { + const element = document.querySelector(`meta[name="${name}"]`) as HTMLMetaElement | null; + const content = element?.content.trim() || null; + element?.remove(); + return content; +} + +function loadInjectedSession(): void { + const values = { + token: takeMetaContent("opencodex-session-token"), + csrf: takeMetaContent("opencodex-session-csrf"), + browser: takeMetaContent("opencodex-session-origin"), + server: takeMetaContent("opencodex-session-server-origin"), + }; + for (const plane of ["machine", "shared"] as const) { + if (runtime(plane).target.serverOrigin === values.server) { + storeSession(plane, values.token, values.csrf, values.browser, values.server); + } + } +} + function metaContentFromHtml(html: string, name: string): string | null { for (const tag of html.match(/]*>/gi) ?? []) { - const nameMatch = tag.match(/\bname="([^"]+)"/i); + const nameMatch = tag.match(/\bname=["']([^"']+)["']/i); if (nameMatch?.[1] !== name) continue; - const contentMatch = tag.match(/\bcontent="([^"]*)"/i); + const contentMatch = tag.match(/\bcontent=["']([^"']*)["']/i); return contentMatch?.[1]?.trim() || null; } return null; } -/** - * Silently renew the GUI session from a freshly served document. Loopback servers mint - * short-lived sessions into the HTML on every page load, so an expired session (5-minute - * TTL) or one invalidated by a proxy restart is replaced without ever asking the user for - * a token. - * - * Tri-state by design: only a definitive refusal ("unavailable": 4xx, or an OK - * document without valid session meta — the non-loopback shape) may fall through to - * the admin-token prompt. Anything transient — timeout, abort, network error, 5xx - * from an intermediate proxy — is "failed", which settles this wave as an ordinary - * request failure and lets the next poll retry. Mapping a transient failure to the - * prompt would pop a credential modal on a loopback dashboard that needs no token. - */ -type RebootstrapResult = - | { kind: "minted"; token: string } - | { kind: "unavailable" } - | { kind: "failed" }; +export function installApiSessionFromHtml(plane: ApiPlane, html: string): boolean { + return storeSession( + plane, + metaContentFromHtml(html, "opencodex-session-token"), + metaContentFromHtml(html, "opencodex-session-csrf"), + metaContentFromHtml(html, "opencodex-session-origin"), + metaContentFromHtml(html, "opencodex-session-server-origin"), + ); +} -async function reBootstrapSessionToken(): Promise { - if (!rawFetch) return { kind: "failed" }; - const bounded = createBoundedFetch(rebootstrapTimeoutMs); - try { - const response = await rawFetch(SESSION_REBOOTSTRAP_PATH, { cache: "no-store", signal: bounded.signal }); - if (!response.ok) { - // Only a definitive refusal is "unavailable"; 5xx and everything else is transient. - return response.status >= 400 && response.status < 500 ? { kind: "unavailable" } : { kind: "failed" }; - } - const html = await response.text(); - let responseOrigin: string; - try { - if (!response.url) throw new TypeError("bootstrap response URL is missing"); - responseOrigin = new URL(response.url).origin; - } catch { - clearToken(); - return { kind: "unavailable" }; - } - const stored = storeSession( - metaContentFromHtml(html, "opencodex-session-token"), - metaContentFromHtml(html, "opencodex-session-csrf"), - metaContentFromHtml(html, "opencodex-session-origin"), - metaContentFromHtml(html, "opencodex-session-server-origin"), - responseOrigin, - ); - const token = readToken(); - if (stored && token) return { kind: "minted", token }; - return { kind: "unavailable" }; - } catch { - return { kind: "failed" }; - } finally { - bounded.clear(); - } +function clearLegacySessionToken(): void { + try { sessionStorage.removeItem(LEGACY_TOKEN_KEY); } catch { /* storage may be disabled */ } } -async function verifyAdminToken(token: string): ReturnType { - if (!rawFetch) return "unavailable"; - try { - const [input, init] = withToken(ADMIN_TOKEN_VALIDATION_PATH, { cache: "no-store" }, token); - const response = await rawFetch(input, init); - if (response.status === 401) return "rejected"; - return response.ok ? "accepted" : "unavailable"; - } catch { - return "unavailable"; - } +function targetAbsoluteBase(target: ApiTarget): URL { + return new URL(target.baseUrl || "/", window.location.href); } -function clearLegacySessionToken(): void { +function targetMatchesUrl(target: ApiTarget, url: URL): boolean { + const base = targetAbsoluteBase(target); + if (url.origin !== base.origin) return false; + const prefix = base.pathname.replace(/\/$/, ""); + return prefix === "" || url.pathname === prefix || url.pathname.startsWith(`${prefix}/`); +} + +function classify(input: RequestInfo | URL): { plane: ApiPlane; bootstrap: boolean } | null { + let url: URL; try { - sessionStorage.removeItem(LEGACY_TOKEN_KEY); - } catch { - /* session storage may be disabled */ + url = new URL(input instanceof Request ? input.url : String(input), window.location.href); + } catch { return null; } + const targets = ensureTargets(); + if (url.href === new URL(targets.shared.bootstrapPath, window.location.href).href) return { plane: "shared", bootstrap: true }; + if (targets.shared.transport === "relay" && targetMatchesUrl(targets.shared, url)) return { plane: "shared", bootstrap: false }; + if (url.pathname.startsWith("/api/machine/") && targetMatchesUrl(targets.machine, url)) return { plane: "machine", bootstrap: false }; + if (targetMatchesUrl(targets.shared, url)) { + const base = targetAbsoluteBase(targets.shared).pathname.replace(/\/$/, ""); + const relative = url.pathname.slice(base.length) || "/"; + if (relative.startsWith("/api/")) return { plane: "shared", bootstrap: false }; } + if (url.href === new URL(targets.machine.bootstrapPath, window.location.href).href) return { plane: "machine", bootstrap: true }; + return null; } -function withToken(input: RequestInfo | URL, init: RequestInit | undefined, token: string): [RequestInfo | URL, RequestInit | undefined] { +function sessionHeaders(plane: ApiPlane, input: RequestInfo | URL, init?: RequestInit, overrideToken?: string | null): Headers { + const state = runtime(plane); const headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined)); - headers.set("X-OpenCodex-API-Key", token); - if (memorySessionBrowserOrigin && memorySessionServerOrigin && memoryCsrfToken && token.startsWith("ocx_session_")) { - const raw = input instanceof Request ? input.url : String(input); - let destinationOrigin: string | null = null; - try { destinationOrigin = new URL(raw, window.location.href).origin; } catch { /* leave null */ } - if (destinationOrigin !== memorySessionServerOrigin) return [input, init]; - headers.set("X-OpenCodex-GUI-Origin", memorySessionBrowserOrigin); - const method = (init?.method ?? (input instanceof Request ? input.method : "GET")).toUpperCase(); - if (method !== "GET" && method !== "HEAD") { - headers.set("X-OpenCodex-CSRF-Token", memoryCsrfToken); - } + const token = overrideToken === undefined ? state.session.token : overrideToken; + const method = (init?.method ?? (input instanceof Request ? input.method : "GET")).toUpperCase(); + if (token) headers.set("X-OpenCodex-API-Key", token); + if (token?.startsWith("ocx_session_") && state.session.browserOrigin && state.session.csrfToken) { + headers.set("X-OpenCodex-GUI-Origin", state.session.browserOrigin); + if (method !== "GET" && method !== "HEAD") headers.set("X-OpenCodex-CSRF-Token", state.session.csrfToken); + } + if (plane === "shared" && state.target.transport === "relay") { + const machine = runtime("machine").session; + if (machine.token) headers.set(MACHINE_SESSION_HEADER, machine.token); + if (machine.browserOrigin) headers.set(MACHINE_GUI_ORIGIN_HEADER, machine.browserOrigin); + if (method !== "GET" && method !== "HEAD" && machine.csrfToken) headers.set(MACHINE_CSRF_HEADER, machine.csrfToken); } + return headers; +} + +function withAuth( + plane: ApiPlane, + input: RequestInfo | URL, + init?: RequestInit, + overrideToken?: string | null, +): [RequestInfo | URL, RequestInit | undefined] { + const headers = sessionHeaders(plane, input, init, overrideToken); if (input instanceof Request) return [new Request(input, { headers }), init ? { ...init, headers } : undefined]; return [input, { ...init, headers }]; } -/** - * Resolve a token after a 401. Concurrent callers share one in-flight resolution so a dashboard - * fan-out opens at most one credential dialog per /api request wave (#647). Re-reads - * memoryToken before prompting so waiters that wake after another request already stored a token - * do not re-prompt. - */ -async function resolveTokenAfter401(failedToken: string | null, callerSignal?: AbortSignal): Promise { - if (promptCancelled) return null; - if (callerSignal?.aborted) return null; - if (!resolutionInFlight) { +async function reBootstrapSessionToken(plane: ApiPlane): Promise { + if (!rawFetch) return { kind: "failed" }; + const state = runtime(plane); + const bounded = createBoundedFetch(rebootstrapTimeoutMs); + try { + const [input, init] = withAuth(plane, state.target.bootstrapPath, { cache: "no-store", signal: bounded.signal }, null); + const response = await rawFetch(input, init); + if (!response.ok) return response.status >= 400 && response.status < 500 ? { kind: "unavailable" } : { kind: "failed" }; + const html = await response.text(); + if (!installApiSessionFromHtml(plane, html)) return { kind: "unavailable" }; + return { kind: "minted", token: runtime(plane).session.token! }; + } catch { return { kind: "failed" }; } + finally { bounded.clear(); } +} + +async function verifyAdminToken(plane: ApiPlane, token: string): ReturnType { + if (!rawFetch) return "unavailable"; + try { + const state = runtime(plane); + const [input, init] = withAuth(plane, `${state.target.baseUrl}${ADMIN_TOKEN_VALIDATION_PATH}`, { cache: "no-store" }, token); + const response = await rawFetch(input, init); + if (response.status === 401) return "rejected"; + return response.ok ? "accepted" : "unavailable"; + } catch { return "unavailable"; } +} + +async function resolveTokenAfter401(plane: ApiPlane, failedToken: string | null, callerSignal?: AbortSignal): Promise { + const state = runtime(plane); + if (state.promptCancelled || callerSignal?.aborted) return null; + if (!state.resolutionInFlight) { const body = (async () => { - if (promptCancelled) return null; - const current = readToken(); + const current = state.session.token; if (current && current !== failedToken) return current; - - // The watchdog races the bootstrap call only — never the prompt below. When - // it wins, the wave fails and the conditional clear lets the NEXT 401 start - // a fresh resolution instead of joining the zombie. let watchdog: ReturnType | undefined; const renewed = await Promise.race([ - reBootstrapSessionToken(), - new Promise((resolve) => { - watchdog = setTimeout(() => resolve({ kind: "failed" }), resolutionWatchdogMs); - }), + reBootstrapSessionToken(plane), + new Promise(resolve => { watchdog = setTimeout(() => resolve({ kind: "failed" }), resolutionWatchdogMs); }), ]).finally(() => clearTimeout(watchdog)); if (renewed.kind === "minted") return renewed.token; - // Transient bootstrap failure: this wave fails and the next 401 re-arms a - // fresh resolution (the finally clears resolutionInFlight). No prompt. if (renewed.kind === "failed") return null; - - // User-controlled and unbounded: later waves join this pending body, which - // is what keeps exactly one prompt dialog on screen. - const prompted = await requestAdminToken(verifyAdminToken); + const prompted = await requestAdminToken(token => verifyAdminToken(plane, token)); if (prompted) { - storeToken(prompted); + state.session = { token: prompted, csrfToken: null, browserOrigin: null, serverOrigin: state.target.serverOrigin }; return prompted; } - promptCancelled = true; + state.promptCancelled = true; return null; })(); - const tracked = body.finally(() => { - // Only clear if nobody replaced us — a late settle must not wipe a newer - // in-flight resolution. (Async callback: tracked is assigned long before - // this can run.) - if (resolutionInFlight === tracked) resolutionInFlight = null; - }); - resolutionInFlight = tracked; + const tracked = body.finally(() => { if (state.resolutionInFlight === tracked) state.resolutionInFlight = null; }); + state.resolutionInFlight = tracked; } - - if (!callerSignal) return resolutionInFlight; - // Per-caller race: an abort unwinds THIS caller only — a dead caller must not - // cancel the shared resolution other waiters still need. The listener is removed - // whether the race resolves by token or by abort, so waiters never accumulate. + if (!callerSignal) return state.resolutionInFlight; let onAbort: (() => void) | undefined; - const aborted = new Promise((resolve) => { + const aborted = new Promise(resolve => { onAbort = () => resolve(null); callerSignal.addEventListener("abort", onAbort, { once: true }); }); - return Promise.race([resolutionInFlight, aborted]).finally(() => { + return Promise.race([state.resolutionInFlight, aborted]).finally(() => { if (onAbort) callerSignal.removeEventListener("abort", onAbort); }); } @@ -301,62 +259,45 @@ async function resolveTokenAfter401(failedToken: string | null, callerSignal?: A export function installApiAuthFetch(): void { if (installed) return; installed = true; - // Drop any leftover XSS-readable token; new tokens stay memory-only (no read/migrate). clearLegacySessionToken(); + ensureTargets(); loadInjectedSession(); const originalFetch = window.fetch.bind(window); rawFetch = originalFetch; window.fetch = async (input: RequestInfo | URL, init?: RequestInit) => { - if (!needsApiAuth(input)) return originalFetch(input, init); - - const callerSignal = init?.signal ?? (input instanceof Request ? input.signal : undefined); - const token = readToken(); - const [firstInput, firstInit] = token ? withToken(input, init, token) : [input, init]; + const classified = classify(input); + if (!classified) return originalFetch(input, init); + const state = runtime(classified.plane); + const token = state.session.token; + const [firstInput, firstInit] = withAuth(classified.plane, input, init); const response = await originalFetch(firstInput, firstInit); - if (response.status !== 401) return response; - - // Another request may have stored a token while this one was in flight (or while prompt blocked). - const refreshed = readToken(); + if (classified.bootstrap || response.status !== 401) return response; + const refreshed = state.session.token; if (refreshed && refreshed !== token) { - const [retryInput, retryInit] = withToken(input, init, refreshed); + const [retryInput, retryInit] = withAuth(classified.plane, input, init); const retry = await originalFetch(retryInput, retryInit); if (retry.status !== 401) return retry; - clearTokenIfCurrent(refreshed); - } else { - clearTokenIfCurrent(token); - } - - const nextToken = await resolveTokenAfter401(token, callerSignal ?? undefined); + clearSessionIfCurrent(classified.plane, refreshed); + } else clearSessionIfCurrent(classified.plane, token); + const callerSignal = init?.signal ?? (input instanceof Request ? input.signal : undefined); + const nextToken = await resolveTokenAfter401(classified.plane, token, callerSignal ?? undefined); if (!nextToken) return response; - - const [retryInput, retryInit] = withToken(input, init, nextToken); + const [retryInput, retryInit] = withAuth(classified.plane, input, init, nextToken); const retry = await originalFetch(retryInput, retryInit); - if (retry.status === 401) clearTokenIfCurrent(nextToken); + if (retry.status === 401) clearSessionIfCurrent(classified.plane, nextToken); return retry; }; } -/** Test-only: allow a fresh `installApiAuthFetch()` in the same module instance. */ export function resetApiAuthFetchForTests(adminTokenPrompt: AdminTokenPrompt = promptForAdminToken): void { installed = false; - memoryToken = null; - memoryCsrfToken = null; - memorySessionBrowserOrigin = null; - memorySessionServerOrigin = null; - resolutionInFlight = null; rawFetch = null; - promptCancelled = false; + configuredTargets = null; + runtimes.clear(); requestAdminToken = adminTokenPrompt; rebootstrapTimeoutMs = SESSION_REBOOTSTRAP_TIMEOUT_MS; resolutionWatchdogMs = RESOLUTION_WATCHDOG_MS; } -/** Test-only: shrink the re-bootstrap deadline so timeout paths run in milliseconds. */ -export function setRebootstrapTimeoutForTests(ms: number): void { - rebootstrapTimeoutMs = ms; -} - -/** Test-only: shrink the whole-resolution watchdog so zombie paths run in milliseconds. */ -export function setResolutionWatchdogForTests(ms: number): void { - resolutionWatchdogMs = ms; -} +export function setRebootstrapTimeoutForTests(ms: number): void { rebootstrapTimeoutMs = ms; } +export function setResolutionWatchdogForTests(ms: number): void { resolutionWatchdogMs = ms; } diff --git a/gui/src/components/storage-workspace/StorageWorkspace.tsx b/gui/src/components/storage-workspace/StorageWorkspace.tsx index 46d670a68e..311a3faa73 100644 --- a/gui/src/components/storage-workspace/StorageWorkspace.tsx +++ b/gui/src/components/storage-workspace/StorageWorkspace.tsx @@ -17,8 +17,6 @@ import { } from "../../i18n/log-guard-state-labels"; import { formatBytes } from "../../format-bytes"; -const API_BASE = import.meta.env.VITE_API_BASE || ""; - export interface StorageLargestEntry { path: string; bytes: number; @@ -370,6 +368,7 @@ function CodexLogGuardUnavailablePanel({ locale, t }: { locale: Locale; t: TFn } export interface StorageWorkspaceProps { report: StorageReport; locale: Locale; + apiBase?: string; logGuardBusy?: boolean; onLogGuardAction?: (action: CodexLogGuardAction) => void; } @@ -398,6 +397,7 @@ type GenerationScopedCompaction = { export default function StorageWorkspace({ report, locale, + apiBase = "", logGuardBusy = false, onLogGuardAction, }: StorageWorkspaceProps) { @@ -460,7 +460,7 @@ export default function StorageWorkspace({ body: JSON.stringify({ mode: action.mode }), } : {}), }; - const response = await fetch(`${API_BASE}/api/storage/codex-logs/${suffix}`, init); + const response = await fetch(`${apiBase}/api/storage/codex-logs/${suffix}`, init); if (!response.ok) { const errorPayload = await response.json().catch(() => ({})) as Record; setLogGuardError({ generation, message: mutationErrorLabel(locale, errorPayload.error) }); @@ -512,7 +512,7 @@ export default function StorageWorkspace({ // The mutation has already succeeded. Refresh is deliberately best effort so // a transient GET/JSON failure cannot be presented as a failed compaction. try { - const refreshed = await fetch(`${API_BASE}/api/storage/codex-logs`); + const refreshed = await fetch(`${apiBase}/api/storage/codex-logs`); if (refreshed.ok) { const payload = await refreshed.json() as CodexLogGuardReport; setLogGuardOverride({ generation, report: payload }); diff --git a/gui/src/connect-pairing.ts b/gui/src/connect-pairing.ts new file mode 100644 index 0000000000..13815468c7 --- /dev/null +++ b/gui/src/connect-pairing.ts @@ -0,0 +1,74 @@ +import { createElement, useState, type ChangeEvent, type FormEvent } from "react"; +import { installApiSessionFromHtml } from "./api"; +import type { ApiTarget } from "./api-targets"; +import { useT } from "./i18n/shared"; + +const PAIRING_CODE = /^ocx_pair_[A-Za-z0-9_-]{43}$/; + +export async function submitConnectPairing( + target: ApiTarget, + grant: string, + fetchImpl: typeof fetch = fetch, +): Promise { + const code = grant.trim(); + if (!PAIRING_CODE.test(code)) throw new Error("pairing_code_invalid"); + const response = await fetchImpl(target.bootstrapPath, { + method: "POST", + headers: { "Content-Type": "application/json", Accept: "text/html" }, + body: JSON.stringify({ grant: code }), + }); + if (!response.ok) throw new Error("pairing_refused"); + const html = await response.text(); + if (!installApiSessionFromHtml("shared", html)) throw new Error("pairing_response_invalid"); + return true; +} + +export function ConnectPairingForm({ + target, + onConnected, +}: { + target: ApiTarget; + onConnected: () => void; +}) { + const t = useT(); + const [grant, setGrant] = useState(""); + const [busy, setBusy] = useState(false); + const [error, setError] = useState(false); + + const submit = async (event: FormEvent) => { + event.preventDefault(); + if (busy) return; + setBusy(true); + setError(false); + try { + await submitConnectPairing(target, grant); + onConnected(); + } catch { + setError(true); + } finally { + setBusy(false); + } + }; + + return createElement("section", { className: "connect-pairing", "aria-labelledby": "connect-pairing-title" }, + createElement("h2", { id: "connect-pairing-title" }, t("connection.pairing.title")), + createElement("p", null, t(target.transport === "relay" ? "connection.pairing.relayWarning" : "connection.pairing.body")), + createElement("form", { onSubmit: submit }, + createElement("label", { htmlFor: "connect-pairing-code" }, t("connection.pairing.code")), + createElement("input", { + id: "connect-pairing-code", + name: "pairingCode", + value: grant, + onChange: (event: ChangeEvent) => setGrant(event.currentTarget.value), + autoComplete: "off", + spellCheck: false, + disabled: busy, + "aria-invalid": error || undefined, + "aria-describedby": error ? "connect-pairing-error" : undefined, + }), + createElement("button", { type: "submit", className: "btn btn-primary", disabled: busy || !grant.trim() }, + t(busy ? "connection.pairing.submitting" : "connection.pairing.submit")), + error ? createElement("p", { id: "connect-pairing-error", className: "alert alert-err", role: "alert" }, t("connection.pairing.error")) : null, + ), + ); +} diff --git a/gui/src/i18n/de.ts b/gui/src/i18n/de.ts index 61b46f2223..9bf99a1e18 100644 --- a/gui/src/i18n/de.ts +++ b/gui/src/i18n/de.ts @@ -2321,4 +2321,29 @@ export const de: Record = { "models.aliasAuto": "automatisch", "models.aliasUser": "benutzerdefiniert", "models.aliasStale": "veraltet", + "connection.discovering": "Discovering local and shared targets…", + "connection.machineUnavailable": "The local machine plane is unavailable. Shared requests were not redirected locally.", + "connection.disconnect": "Disconnect from hub", + "connection.pairing.title": "Connect this dashboard to the hub", + "connection.pairing.body": "Paste the one-time pairing code created on the hub.", + "connection.pairing.relayWarning": "This code is exchanged through the fixed hub relay. The relay cannot be redirected to another host.", + "connection.pairing.code": "One-time pairing code", + "connection.pairing.submit": "Connect", + "connection.pairing.submitting": "Connecting…", + "connection.pairing.error": "The pairing code was refused or expired. The code was left in place so you can check it.", + "connection.machine.title": "This machine", + "connection.machine.shimHealthy": "Codex shim is healthy.", + "connection.machine.shimNeedsAttention": "Codex shim needs attention.", + "connection.machine.repairShim": "Repair shim", + "connection.machine.removeShim": "Remove shim", + "connection.clients.title": "Connected clients", + "connection.clients.none": "No client status available", + "connection.clients.sync": "Sync now", + "connection.clients.syncing": "Syncing…", + "usage.source.connected": "Source: hub usage", + "usage.source.local": "Source: local usage.jsonl", + "usage.scope.label": "Usage scope", + "usage.scope.machine": "This machine", + "usage.scope.hub": "Hub-wide", + "usage.hubOffline": "Hub usage is unavailable. Local usage was not substituted.", }; diff --git a/gui/src/i18n/en.ts b/gui/src/i18n/en.ts index fb25f211fb..515e4759e5 100644 --- a/gui/src/i18n/en.ts +++ b/gui/src/i18n/en.ts @@ -2355,6 +2355,31 @@ export const en = { "models.aliasAuto": "auto", "models.aliasUser": "user", "models.aliasStale": "stale", + "connection.discovering": "Discovering local and shared targets…", + "connection.machineUnavailable": "The local machine plane is unavailable. Shared requests were not redirected locally.", + "connection.disconnect": "Disconnect from hub", + "connection.pairing.title": "Connect this dashboard to the hub", + "connection.pairing.body": "Paste the one-time pairing code created on the hub.", + "connection.pairing.relayWarning": "This code is exchanged through the fixed hub relay. The relay cannot be redirected to another host.", + "connection.pairing.code": "One-time pairing code", + "connection.pairing.submit": "Connect", + "connection.pairing.submitting": "Connecting…", + "connection.pairing.error": "The pairing code was refused or expired. The code was left in place so you can check it.", + "connection.machine.title": "This machine", + "connection.machine.shimHealthy": "Codex shim is healthy.", + "connection.machine.shimNeedsAttention": "Codex shim needs attention.", + "connection.machine.repairShim": "Repair shim", + "connection.machine.removeShim": "Remove shim", + "connection.clients.title": "Connected clients", + "connection.clients.none": "No client status available", + "connection.clients.sync": "Sync now", + "connection.clients.syncing": "Syncing…", + "usage.source.connected": "Source: hub usage", + "usage.source.local": "Source: local usage.jsonl", + "usage.scope.label": "Usage scope", + "usage.scope.machine": "This machine", + "usage.scope.hub": "Hub-wide", + "usage.hubOffline": "Hub usage is unavailable. Local usage was not substituted.", } as const; export type TKey = keyof typeof en; diff --git a/gui/src/i18n/fr.ts b/gui/src/i18n/fr.ts index 16a6ab693d..512db468d9 100644 --- a/gui/src/i18n/fr.ts +++ b/gui/src/i18n/fr.ts @@ -2308,4 +2308,29 @@ export const fr: Record = { "models.aliasAuto": "auto", "models.aliasUser": "utilisateur", "models.aliasStale": "obsolète", + "connection.discovering": "Discovering local and shared targets…", + "connection.machineUnavailable": "The local machine plane is unavailable. Shared requests were not redirected locally.", + "connection.disconnect": "Disconnect from hub", + "connection.pairing.title": "Connect this dashboard to the hub", + "connection.pairing.body": "Paste the one-time pairing code created on the hub.", + "connection.pairing.relayWarning": "This code is exchanged through the fixed hub relay. The relay cannot be redirected to another host.", + "connection.pairing.code": "One-time pairing code", + "connection.pairing.submit": "Connect", + "connection.pairing.submitting": "Connecting…", + "connection.pairing.error": "The pairing code was refused or expired. The code was left in place so you can check it.", + "connection.machine.title": "This machine", + "connection.machine.shimHealthy": "Codex shim is healthy.", + "connection.machine.shimNeedsAttention": "Codex shim needs attention.", + "connection.machine.repairShim": "Repair shim", + "connection.machine.removeShim": "Remove shim", + "connection.clients.title": "Connected clients", + "connection.clients.none": "No client status available", + "connection.clients.sync": "Sync now", + "connection.clients.syncing": "Syncing…", + "usage.source.connected": "Source: hub usage", + "usage.source.local": "Source: local usage.jsonl", + "usage.scope.label": "Usage scope", + "usage.scope.machine": "This machine", + "usage.scope.hub": "Hub-wide", + "usage.hubOffline": "Hub usage is unavailable. Local usage was not substituted.", }; diff --git a/gui/src/i18n/ja.ts b/gui/src/i18n/ja.ts index df16c0562e..94d4218a55 100644 --- a/gui/src/i18n/ja.ts +++ b/gui/src/i18n/ja.ts @@ -2342,4 +2342,29 @@ export const ja: Record = { "models.aliasAuto": "自動", "models.aliasUser": "ユーザー", "models.aliasStale": "古い", + "connection.discovering": "Discovering local and shared targets…", + "connection.machineUnavailable": "The local machine plane is unavailable. Shared requests were not redirected locally.", + "connection.disconnect": "Disconnect from hub", + "connection.pairing.title": "Connect this dashboard to the hub", + "connection.pairing.body": "Paste the one-time pairing code created on the hub.", + "connection.pairing.relayWarning": "This code is exchanged through the fixed hub relay. The relay cannot be redirected to another host.", + "connection.pairing.code": "One-time pairing code", + "connection.pairing.submit": "Connect", + "connection.pairing.submitting": "Connecting…", + "connection.pairing.error": "The pairing code was refused or expired. The code was left in place so you can check it.", + "connection.machine.title": "This machine", + "connection.machine.shimHealthy": "Codex shim is healthy.", + "connection.machine.shimNeedsAttention": "Codex shim needs attention.", + "connection.machine.repairShim": "Repair shim", + "connection.machine.removeShim": "Remove shim", + "connection.clients.title": "Connected clients", + "connection.clients.none": "No client status available", + "connection.clients.sync": "Sync now", + "connection.clients.syncing": "Syncing…", + "usage.source.connected": "Source: hub usage", + "usage.source.local": "Source: local usage.jsonl", + "usage.scope.label": "Usage scope", + "usage.scope.machine": "This machine", + "usage.scope.hub": "Hub-wide", + "usage.hubOffline": "Hub usage is unavailable. Local usage was not substituted.", }; diff --git a/gui/src/i18n/ko.ts b/gui/src/i18n/ko.ts index dfa0803dd9..1585516ac5 100644 --- a/gui/src/i18n/ko.ts +++ b/gui/src/i18n/ko.ts @@ -2343,4 +2343,29 @@ export const ko: Record = { "models.aliasAuto": "자동", "models.aliasUser": "사용자", "models.aliasStale": "오래됨", + "connection.discovering": "로컬 및 공유 대상을 확인하는 중…", + "connection.machineUnavailable": "로컬 머신 연결을 사용할 수 없습니다. 공유 요청을 로컬로 우회하지 않았습니다.", + "connection.disconnect": "허브 연결 해제", + "connection.pairing.title": "이 대시보드를 허브에 연결", + "connection.pairing.body": "허브에서 만든 일회용 페어링 코드를 붙여 넣으세요.", + "connection.pairing.relayWarning": "이 코드는 고정 허브 릴레이로 교환됩니다. 릴레이 목적지는 다른 호스트로 바꿀 수 없습니다.", + "connection.pairing.code": "일회용 페어링 코드", + "connection.pairing.submit": "연결", + "connection.pairing.submitting": "연결 중…", + "connection.pairing.error": "페어링 코드가 거부되었거나 만료되었습니다. 확인할 수 있도록 입력값은 유지했습니다.", + "connection.machine.title": "이 머신", + "connection.machine.shimHealthy": "Codex shim이 정상입니다.", + "connection.machine.shimNeedsAttention": "Codex shim을 확인해야 합니다.", + "connection.machine.repairShim": "shim 복구", + "connection.machine.removeShim": "shim 제거", + "connection.clients.title": "연결된 클라이언트", + "connection.clients.none": "클라이언트 상태 없음", + "connection.clients.sync": "지금 동기화", + "connection.clients.syncing": "동기화 중…", + "usage.source.connected": "출처: 허브 사용량", + "usage.source.local": "출처: 로컬 usage.jsonl", + "usage.scope.label": "사용량 범위", + "usage.scope.machine": "이 머신", + "usage.scope.hub": "허브 전체", + "usage.hubOffline": "허브 사용량을 불러올 수 없습니다. 로컬 사용량으로 대체하지 않았습니다.", }; diff --git a/gui/src/i18n/ru.ts b/gui/src/i18n/ru.ts index 36153cd22f..8e09703701 100644 --- a/gui/src/i18n/ru.ts +++ b/gui/src/i18n/ru.ts @@ -2344,4 +2344,29 @@ export const ru: Record = { "models.aliasAuto": "авто", "models.aliasUser": "пользователь", "models.aliasStale": "устарел", + "connection.discovering": "Discovering local and shared targets…", + "connection.machineUnavailable": "The local machine plane is unavailable. Shared requests were not redirected locally.", + "connection.disconnect": "Disconnect from hub", + "connection.pairing.title": "Connect this dashboard to the hub", + "connection.pairing.body": "Paste the one-time pairing code created on the hub.", + "connection.pairing.relayWarning": "This code is exchanged through the fixed hub relay. The relay cannot be redirected to another host.", + "connection.pairing.code": "One-time pairing code", + "connection.pairing.submit": "Connect", + "connection.pairing.submitting": "Connecting…", + "connection.pairing.error": "The pairing code was refused or expired. The code was left in place so you can check it.", + "connection.machine.title": "This machine", + "connection.machine.shimHealthy": "Codex shim is healthy.", + "connection.machine.shimNeedsAttention": "Codex shim needs attention.", + "connection.machine.repairShim": "Repair shim", + "connection.machine.removeShim": "Remove shim", + "connection.clients.title": "Connected clients", + "connection.clients.none": "No client status available", + "connection.clients.sync": "Sync now", + "connection.clients.syncing": "Syncing…", + "usage.source.connected": "Source: hub usage", + "usage.source.local": "Source: local usage.jsonl", + "usage.scope.label": "Usage scope", + "usage.scope.machine": "This machine", + "usage.scope.hub": "Hub-wide", + "usage.hubOffline": "Hub usage is unavailable. Local usage was not substituted.", }; diff --git a/gui/src/i18n/tr.ts b/gui/src/i18n/tr.ts index f567faa70d..92aec70d7c 100644 --- a/gui/src/i18n/tr.ts +++ b/gui/src/i18n/tr.ts @@ -2344,4 +2344,29 @@ export const tr: Record = { "models.aliasAuto": "otomatik", "models.aliasUser": "kullanıcı", "models.aliasStale": "eski", + "connection.discovering": "Discovering local and shared targets…", + "connection.machineUnavailable": "The local machine plane is unavailable. Shared requests were not redirected locally.", + "connection.disconnect": "Disconnect from hub", + "connection.pairing.title": "Connect this dashboard to the hub", + "connection.pairing.body": "Paste the one-time pairing code created on the hub.", + "connection.pairing.relayWarning": "This code is exchanged through the fixed hub relay. The relay cannot be redirected to another host.", + "connection.pairing.code": "One-time pairing code", + "connection.pairing.submit": "Connect", + "connection.pairing.submitting": "Connecting…", + "connection.pairing.error": "The pairing code was refused or expired. The code was left in place so you can check it.", + "connection.machine.title": "This machine", + "connection.machine.shimHealthy": "Codex shim is healthy.", + "connection.machine.shimNeedsAttention": "Codex shim needs attention.", + "connection.machine.repairShim": "Repair shim", + "connection.machine.removeShim": "Remove shim", + "connection.clients.title": "Connected clients", + "connection.clients.none": "No client status available", + "connection.clients.sync": "Sync now", + "connection.clients.syncing": "Syncing…", + "usage.source.connected": "Source: hub usage", + "usage.source.local": "Source: local usage.jsonl", + "usage.scope.label": "Usage scope", + "usage.scope.machine": "This machine", + "usage.scope.hub": "Hub-wide", + "usage.hubOffline": "Hub usage is unavailable. Local usage was not substituted.", }; diff --git a/gui/src/i18n/zh-TW.ts b/gui/src/i18n/zh-TW.ts index 616fa1044c..cb647d72af 100644 --- a/gui/src/i18n/zh-TW.ts +++ b/gui/src/i18n/zh-TW.ts @@ -2306,4 +2306,29 @@ export const zhTW: Record = { "models.aliasAuto": "自動", "models.aliasUser": "使用者", "models.aliasStale": "過期", + "connection.discovering": "Discovering local and shared targets…", + "connection.machineUnavailable": "The local machine plane is unavailable. Shared requests were not redirected locally.", + "connection.disconnect": "Disconnect from hub", + "connection.pairing.title": "Connect this dashboard to the hub", + "connection.pairing.body": "Paste the one-time pairing code created on the hub.", + "connection.pairing.relayWarning": "This code is exchanged through the fixed hub relay. The relay cannot be redirected to another host.", + "connection.pairing.code": "One-time pairing code", + "connection.pairing.submit": "Connect", + "connection.pairing.submitting": "Connecting…", + "connection.pairing.error": "The pairing code was refused or expired. The code was left in place so you can check it.", + "connection.machine.title": "This machine", + "connection.machine.shimHealthy": "Codex shim is healthy.", + "connection.machine.shimNeedsAttention": "Codex shim needs attention.", + "connection.machine.repairShim": "Repair shim", + "connection.machine.removeShim": "Remove shim", + "connection.clients.title": "Connected clients", + "connection.clients.none": "No client status available", + "connection.clients.sync": "Sync now", + "connection.clients.syncing": "Syncing…", + "usage.source.connected": "Source: hub usage", + "usage.source.local": "Source: local usage.jsonl", + "usage.scope.label": "Usage scope", + "usage.scope.machine": "This machine", + "usage.scope.hub": "Hub-wide", + "usage.hubOffline": "Hub usage is unavailable. Local usage was not substituted.", }; diff --git a/gui/src/i18n/zh.ts b/gui/src/i18n/zh.ts index 733ce1728e..f25ea6dd68 100644 --- a/gui/src/i18n/zh.ts +++ b/gui/src/i18n/zh.ts @@ -2342,4 +2342,29 @@ export const zh: Record = { "models.aliasAuto": "自动", "models.aliasUser": "用户", "models.aliasStale": "过期", + "connection.discovering": "Discovering local and shared targets…", + "connection.machineUnavailable": "The local machine plane is unavailable. Shared requests were not redirected locally.", + "connection.disconnect": "Disconnect from hub", + "connection.pairing.title": "Connect this dashboard to the hub", + "connection.pairing.body": "Paste the one-time pairing code created on the hub.", + "connection.pairing.relayWarning": "This code is exchanged through the fixed hub relay. The relay cannot be redirected to another host.", + "connection.pairing.code": "One-time pairing code", + "connection.pairing.submit": "Connect", + "connection.pairing.submitting": "Connecting…", + "connection.pairing.error": "The pairing code was refused or expired. The code was left in place so you can check it.", + "connection.machine.title": "This machine", + "connection.machine.shimHealthy": "Codex shim is healthy.", + "connection.machine.shimNeedsAttention": "Codex shim needs attention.", + "connection.machine.repairShim": "Repair shim", + "connection.machine.removeShim": "Remove shim", + "connection.clients.title": "Connected clients", + "connection.clients.none": "No client status available", + "connection.clients.sync": "Sync now", + "connection.clients.syncing": "Syncing…", + "usage.source.connected": "Source: hub usage", + "usage.source.local": "Source: local usage.jsonl", + "usage.scope.label": "Usage scope", + "usage.scope.machine": "This machine", + "usage.scope.hub": "Hub-wide", + "usage.hubOffline": "Hub usage is unavailable. Local usage was not substituted.", }; diff --git a/gui/src/pages/Integrations.tsx b/gui/src/pages/Integrations.tsx index 63da22f558..7f06312a77 100644 --- a/gui/src/pages/Integrations.tsx +++ b/gui/src/pages/Integrations.tsx @@ -39,7 +39,7 @@ function tabMark(tab: IntegrationTab): string | null { return INTEGRATION_MARKS[tab] ?? null; } -export default function Integrations({ apiBase }: { apiBase: string }) { +export default function Integrations({ apiBase, machineApiBase = apiBase, connected = false }: { apiBase: string; machineApiBase?: string; connected?: boolean }) { const t = useT(); const [tab, setTab] = useState(readIntegrationTab); /* @@ -51,8 +51,34 @@ export default function Integrations({ apiBase }: { apiBase: string }) { () => new Set([readIntegrationTab()]), ); const tabRefs = useRef | null>(null); + const [machineClients, setMachineClients] = useState([]); + const [machineSyncing, setMachineSyncing] = useState(false); if (tabRefs.current === null) tabRefs.current = new Map(); + useEffect(() => { + if (!connected) { setMachineClients([]); return; } + const controller = new AbortController(); + void fetch(`${machineApiBase}/api/machine/clients`, { signal: controller.signal }) + .then(response => response.ok ? response.json() : null) + .then((value: { selectedClients?: unknown } | null) => { + if (!controller.signal.aborted && Array.isArray(value?.selectedClients)) { + setMachineClients(value.selectedClients.filter((item): item is string => typeof item === "string")); + } + }).catch(() => {}); + return () => controller.abort(); + }, [connected, machineApiBase]); + + const syncMachine = async () => { + setMachineSyncing(true); + try { + await fetch(`${machineApiBase}/api/machine/sync`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: "{}", + }); + } finally { setMachineSyncing(false); } + }; + /* * Every tab change goes through here, whether it came from a click or from * the browser's own history. Accumulating the mounted set in an effect @@ -104,6 +130,13 @@ export default function Integrations({ apiBase }: { apiBase: string }) {

{t("nav.integrations")}

{t("integrations.subtitle")}

+ {connected && ( +
+ {t("connection.clients.title")} + {machineClients.length > 0 ? machineClients.join(", ") : t("connection.clients.none")} + +
+ )}
{TABS.map(definition => ( diff --git a/gui/src/pages/Startup.tsx b/gui/src/pages/Startup.tsx index 53b997fac3..aa11a994ef 100644 --- a/gui/src/pages/Startup.tsx +++ b/gui/src/pages/Startup.tsx @@ -70,7 +70,7 @@ function deriveCodexRuntimeNotice( return { warning: null, fix: null }; } -export default function Startup({ apiBase }: { apiBase: string }) { +export default function Startup({ apiBase, machineApiBase = apiBase, connected = false }: { apiBase: string; machineApiBase?: string; connected?: boolean }) { const { t } = useI18n(); const cacheKey = `${STARTUP_PAGE_CACHE_PREFIX}${apiBase}`; const cached = useMemo(() => readSessionListCache(cacheKey), [cacheKey]); @@ -89,6 +89,33 @@ export default function Startup({ apiBase }: { apiBase: string }) { const [runtimeNoticePending, setRuntimeNoticePending] = useState(() => !cached?.data); const paintedRef = useRef(Boolean(cached?.data)); const secondaryGenerationRef = useRef(0); + const [machineShim, setMachineShim] = useState<{ installed?: boolean; healthy?: boolean } | null>(null); + const [machineBusy, setMachineBusy] = useState(false); + + useEffect(() => { + if (!connected) { setMachineShim(null); return; } + const controller = new AbortController(); + void fetch(`${machineApiBase}/api/machine/shim`, { signal: controller.signal }) + .then(response => response.ok ? response.json() : null) + .then(value => { if (!controller.signal.aborted) setMachineShim(value); }) + .catch(() => { if (!controller.signal.aborted) setMachineShim(null); }); + return () => controller.abort(); + }, [connected, machineApiBase]); + + const runMachineShim = async (action: "install" | "repair" | "uninstall") => { + setMachineBusy(true); + try { + const response = await fetch(`${machineApiBase}/api/machine/shim`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ action }), + }); + if (response.ok) { + const value = await response.json() as { shim?: { installed?: boolean; healthy?: boolean } }; + setMachineShim(value.shim ?? null); + } + } finally { setMachineBusy(false); } + }; useEffect(() => () => { secondaryGenerationRef.current += 1; @@ -304,6 +331,17 @@ export default function Startup({ apiBase }: { apiBase: string }) {
+ {connected && ( +
+ {t("connection.machine.title")} + {machineShim?.healthy ? t("connection.machine.shimHealthy") : t("connection.machine.shimNeedsAttention")} +
+ + {machineShim?.installed && } +
+
+ )} + {loadState.showSkeleton && !data ? ( ) : loadState.kind === "failed-cold" ? ( diff --git a/gui/src/pages/Storage.tsx b/gui/src/pages/Storage.tsx index 019b93e6b0..fceb45fe04 100644 --- a/gui/src/pages/Storage.tsx +++ b/gui/src/pages/Storage.tsx @@ -1444,7 +1444,7 @@ export default function Storage({ apiBase }: { apiBase: string }) { ) : ( <> {reportState.showError &&
{t("storage.error")}
} - {empty ? : data && data.total.fileCount > 0 && } + {empty ? : data && data.total.fileCount > 0 && } )} diff --git a/gui/src/pages/Usage.tsx b/gui/src/pages/Usage.tsx index 18cf77b4f8..769febe5b0 100644 --- a/gui/src/pages/Usage.tsx +++ b/gui/src/pages/Usage.tsx @@ -739,42 +739,47 @@ function UsageWorkspaceBody({ /** Held usage payloads so provider/surface tab switches skip a cold ~5s refetch. */ const usageMemoryCache = new Map(); -function usageCacheKey(apiBase: string, range: Range, surface: UsageSurface): string { - return `ocx.usage.v1:${apiBase}:${range}:${surface}`; +type UsageScope = "machine" | "hub"; + +function usageCacheKey(apiBase: string, range: Range, surface: UsageSurface, connected: boolean, scope: UsageScope, apiKeyId?: string): string { + return `ocx.usage.v2:${apiBase}:${connected ? "connected" : "standalone"}:${scope}:${apiKeyId ?? ""}:${range}:${surface}`; } -function readHeldUsage(apiBase: string, range: Range, surface: UsageSurface): UsageResponse | null { - const key = usageCacheKey(apiBase, range, surface); +function readHeldUsage(apiBase: string, range: Range, surface: UsageSurface, connected: boolean, scope: UsageScope, apiKeyId?: string): UsageResponse | null { + const key = usageCacheKey(apiBase, range, surface, connected, scope, apiKeyId); return usageMemoryCache.get(key) ?? readSessionListCache(key); } -function writeHeldUsage(apiBase: string, range: Range, surface: UsageSurface, value: UsageResponse) { - const key = usageCacheKey(apiBase, range, surface); +function writeHeldUsage(apiBase: string, range: Range, surface: UsageSurface, connected: boolean, scope: UsageScope, apiKeyId: string | undefined, value: UsageResponse) { + const key = usageCacheKey(apiBase, range, surface, connected, scope, apiKeyId); usageMemoryCache.set(key, value); writeSessionListCache(key, value); } -export default function Usage({ apiBase }: { apiBase: string }) { +export default function Usage({ apiBase, connected = false, apiKeyId }: { apiBase: string; connected?: boolean; apiKeyId?: string }) { const { t, locale } = useI18n(); const [range, setRange] = useState("30d"); const [surface, setSurface] = useState("all"); + const [scope, setScope] = useState("machine"); const [modelQuery, setModelQuery] = useState(""); const loadUsage = useCallback(async (signal: AbortSignal): Promise => { - const response = await fetch(`${apiBase}/api/usage?range=${range}&surface=${surface}`, { signal }); + const query = new URLSearchParams({ range, surface }); + if (connected && scope === "machine" && apiKeyId) query.set("apiKeyId", apiKeyId); + const response = await fetch(`${apiBase}/api/usage?${query}`, { signal }); if (!response.ok) throw new Error(`${response.status} ${response.statusText}`.trim()); const next = await response.json() as UsageResponse; - writeHeldUsage(apiBase, range, surface, next); + writeHeldUsage(apiBase, range, surface, connected, scope, apiKeyId, next); return next; - }, [apiBase, range, surface]); + }, [apiBase, apiKeyId, connected, range, scope, surface]); - const resourceKey = usageCacheKey(apiBase, range, surface); - const cached = readHeldUsage(apiBase, range, surface); + const resourceKey = usageCacheKey(apiBase, range, surface, connected, scope, apiKeyId); + const cached = readHeldUsage(apiBase, range, surface, connected, scope, apiKeyId); // Range and surface identify different reports, so the key changes with both. That prevents // a force-loading dependency revalidation from ever showing a previous report as this one. const resource = useDataSurface( resourceKey, - [apiBase, range, surface], + [apiBase, apiKeyId, connected, range, scope, surface], loadUsage, { isEmpty: () => false, initialData: cached ?? undefined }, ); @@ -808,19 +813,28 @@ export default function Usage({ apiBase }: { apiBase: string }) {

{t("usage.subtitle")}

+
+ {t(connected ? "usage.source.connected" : "usage.source.local")} + {connected && ( +
+ + +
+ )} +
{state.showSkeleton && !data ? ( ) : state.kind === "failed-cold" ? ( - {state.error instanceof Error ? `${t("usage.loadError")} ${state.error.message}` : t("usage.loadError")}{" "} + {connected ? t("usage.hubOffline") : state.error instanceof Error ? `${t("usage.loadError")} ${state.error.message}` : t("usage.loadError")}{" "} ) : ( <> - {state.showError && {t("usage.loadError")}} + {state.showError && {t(connected ? "usage.hubOffline" : "usage.loadError")}} {data?.historyTruncated && ( // Naming the loaded window is the point: without it, `30d` and "Available history" // look identical on a busy installation even though both may cover far less than diff --git a/gui/src/stop-proxy.ts b/gui/src/stop-proxy.ts index ee98d3735f..0798f9b8f0 100644 --- a/gui/src/stop-proxy.ts +++ b/gui/src/stop-proxy.ts @@ -15,6 +15,7 @@ export interface ProxyStopOptions { fetchFn?: typeof fetch; timeoutMs?: number; formatFailure?: (status: number) => string; + mode?: "standalone" | "client"; } function failureMessage( @@ -46,11 +47,14 @@ export async function requestProxyStop( fetchFn = fetch, timeoutMs = DEFAULT_STOP_TIMEOUT_MS, formatFailure = status => `Failed to stop proxy (HTTP ${status}).`, + mode = "standalone", } = options; let response: Response; try { - response = await fetchFn(`${apiBase}/api/stop`, { + const path = mode === "client" ? "/api/machine/disconnect" : "/api/stop"; + response = await fetchFn(`${apiBase}${path}`, { method: "POST", + ...(mode === "client" ? { headers: { "Content-Type": "application/json" }, body: "{}" } : {}), signal: AbortSignal.timeout(timeoutMs), }); } catch (error) { diff --git a/gui/src/styles-usage-workspace.css b/gui/src/styles-usage-workspace.css index aa120669c8..aa9e3bb45c 100644 --- a/gui/src/styles-usage-workspace.css +++ b/gui/src/styles-usage-workspace.css @@ -200,3 +200,20 @@ min-height: auto; } } +.usage-source-row { + display: flex; + align-items: center; + justify-content: space-between; + gap: 12px; + margin: 0 0 14px; + color: var(--text-secondary); +} + +.usage-scope-control { + display: inline-flex; + gap: 6px; +} + +@media (max-width: 640px) { + .usage-source-row { align-items: flex-start; flex-direction: column; } +} diff --git a/src/cli/index.ts b/src/cli/index.ts index 74e58af77f..9afe21ec02 100755 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -59,7 +59,6 @@ import { isProcessAlive, ProxyOwnershipRefusedError, stopProxy } from "../lib/pr import { loadServiceTokenFromFile } from "../lib/service-secrets"; import { assertNotAdminToken, diagnoseService, isServiceOwnershipError, proxyStillLiveAfterStop, serviceCommand, serviceEnvironmentOwnedHere, serviceStartableFromTray, serviceStatusSummary, stopServiceIfInstalledDetailed, uninstallServiceIfInstalled, uninstallServiceDetailed } from "../service"; import { formatStartupRoutingDetail, startupHealthSummary } from "../codex/autostart-health"; -import { drainAndShutdown, isRecyclingForExit, startServer } from "../server"; import { injectSystemEnv, reconcileShellHook, revertSystemEnv, uninstallShellHook } from "../server/system-env"; import { buildDesktop3pRegistry } from "../claude/desktop-3p"; import { startTokenGuardian } from "../oauth/token-guardian"; @@ -270,6 +269,16 @@ async function handleStart(options: { block?: boolean } = {}) { process.exit(1); } + const clientState = readClientConnectionState(); + if (clientState.kind === "invalid" || clientState.kind === "mismatched") { + throw new Error(`client startup refused: ${clientState.reason}`); + } + if (clientState.kind === "connected") { + const { startClientRuntime } = await import("../client/runtime"); + await startClientRuntime({ port: requestedPort, block: options.block }); + return; + } + // Interactive-only update prompt. Must run BEFORE we bind a port / write a // PID: choosing "Update now" installs globally and exits, so we never want a // live daemon holding resources while it overwrites its own binary. @@ -279,6 +288,7 @@ async function handleStart(options: { block?: boolean } = {}) { // between the probe and Bun.serve. Soft starts may re-pick; hard-pinned `--port` retries // the same port only (never hop — that was the remaining PR #152 gap). let port = await chooseListenPort(requestedPort); + const { drainAndShutdown, isRecyclingForExit, startServer } = await import("../server"); // One private readiness gate for this startServer invocation, captured by the // listener's closure. handleStart owns it and transitions it after the // post-startup sync settles. A second startServer in the same process would diff --git a/src/client/hub-relay.ts b/src/client/hub-relay.ts new file mode 100644 index 0000000000..e4cde4dbb0 --- /dev/null +++ b/src/client/hub-relay.ts @@ -0,0 +1,195 @@ +import { stripMachineAuthHeaders } from "./machine-auth"; + +export interface HubRelayTarget { + managementUrl: string; + browserOrigin: string; +} + +export const HUB_RELAY_REQUEST_BODY_MAX_BYTES = 4 * 1024 * 1024; +export const HUB_RELAY_RESPONSE_BODY_MAX_BYTES = 16 * 1024 * 1024; +export const HUB_RELAY_DEFAULT_TIMEOUT_MS = 15_000; +export const HUB_RELAY_HEADER_MAX_BYTES = 64 * 1024; + +const ALLOWED_METHODS = new Set(["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"]); +const REQUEST_HEADERS = new Set([ + "accept", + "accept-language", + "cache-control", + "content-type", + "if-match", + "if-modified-since", + "if-none-match", + "if-unmodified-since", + "origin", + "x-opencodex-api-key", + "x-opencodex-csrf-token", + "x-opencodex-gui-origin", +]); +const RESPONSE_HEADERS = new Set([ + "cache-control", + "content-language", + "content-type", + "etag", + "expires", + "last-modified", + "pragma", + "retry-after", + "vary", +]); + +function relayError(status: number, error: string): Response { + return Response.json({ error }, { status }); +} + +function canonicalOrigin(value: string): string | null { + try { + const url = new URL(value); + if ((url.protocol !== "http:" && url.protocol !== "https:") + || url.username || url.password || url.pathname !== "/" || url.search || url.hash) return null; + return url.origin; + } catch { + return null; + } +} + +function relayDestination(suffix: string, target: HubRelayTarget, method: string): URL | null { + const origin = canonicalOrigin(target.managementUrl); + const browserOrigin = canonicalOrigin(target.browserOrigin); + if (!origin || !browserOrigin || !ALLOWED_METHODS.has(method)) return null; + if (!suffix.startsWith("/") || suffix.startsWith("//") || suffix.includes("\\") || suffix.includes("#")) return null; + if (/%(?:2f|5c)/i.test(suffix) || /%(?:2e)(?:%2e|\.)?/i.test(suffix)) return null; + const rawPath = suffix.split("?", 1)[0]!; + for (const segment of rawPath.split("/")) { + let decoded: string; + try { decoded = decodeURIComponent(segment); } catch { return null; } + if (decoded === "." || decoded === ".." || decoded.includes("/") || decoded.includes("\\")) return null; + } + if (rawPath === "/opencodex-session") { + if (suffix !== rawPath || (method !== "GET" && method !== "POST")) return null; + } else if (!rawPath.startsWith("/api/")) { + return null; + } + let destination: URL; + try { destination = new URL(suffix, `${origin}/`); } catch { return null; } + if (destination.origin !== origin || destination.username || destination.password || destination.hash) return null; + if (destination.pathname !== rawPath) return null; + return destination; +} + +async function boundedBody(stream: ReadableStream | null, declared: string | null, limit: number): Promise { + if (!stream) return null; + const contentLength = declared === null ? null : Number(declared); + if (contentLength !== null && (!Number.isSafeInteger(contentLength) || contentLength < 0 || contentLength > limit)) { + throw new RangeError("body_too_large"); + } + const reader = stream.getReader(); + const chunks: Uint8Array[] = []; + let length = 0; + try { + while (true) { + const next = await reader.read(); + if (next.done) break; + length += next.value.byteLength; + if (length > limit) throw new RangeError("body_too_large"); + chunks.push(next.value); + } + } finally { + reader.releaseLock(); + } + const body = new Uint8Array(length); + let offset = 0; + for (const chunk of chunks) { + body.set(chunk, offset); + offset += chunk.byteLength; + } + return body; +} + +function filteredHeaders(source: Headers, allowlist: Set): Headers { + const headers = new Headers(); + for (const [name, value] of source) { + if (allowlist.has(name.toLowerCase())) headers.append(name, value); + } + return headers; +} + +function headersWithinLimit(headers: Headers): boolean { + let bytes = 0; + for (const [name, value] of headers) { + bytes += name.length + value.length + 4; + if (bytes > HUB_RELAY_HEADER_MAX_BYTES) return false; + } + return true; +} + +export async function relayHubManagementRequest( + req: Request, + suffix: string, + target: HubRelayTarget, + deps: { fetchImpl?: typeof fetch; timeoutMs?: number } = {}, +): Promise { + const method = req.method.toUpperCase(); + const destination = relayDestination(suffix, target, method); + if (!destination) return relayError(404, "hub relay path refused"); + + let body: Uint8Array | null; + try { + body = method === "GET" || method === "HEAD" + ? null + : await boundedBody(req.body, req.headers.get("content-length"), HUB_RELAY_REQUEST_BODY_MAX_BYTES); + } catch { + return relayError(413, "hub relay request body too large"); + } + + const stripped = stripMachineAuthHeaders(req.headers); + const headers = filteredHeaders(stripped, REQUEST_HEADERS); + if (!headersWithinLimit(headers)) return relayError(431, "hub relay request headers too large"); + const browserOrigin = canonicalOrigin(target.browserOrigin); + if (!browserOrigin || headers.get("origin") !== browserOrigin) { + return relayError(403, "hub relay browser origin refused"); + } + + const timeoutMs = typeof deps.timeoutMs === "number" && Number.isFinite(deps.timeoutMs) && deps.timeoutMs > 0 + ? Math.min(Math.floor(deps.timeoutMs), 120_000) + : HUB_RELAY_DEFAULT_TIMEOUT_MS; + const timeoutSignal = AbortSignal.timeout(timeoutMs); + const signal = req.signal + ? AbortSignal.any([req.signal, timeoutSignal]) + : timeoutSignal; + let upstream: Response; + try { + upstream = await (deps.fetchImpl ?? fetch)(destination, { + method, + headers, + ...(body ? { body } : {}), + redirect: "manual", + signal, + }); + } catch { + return relayError(502, "hub relay unavailable"); + } + if (upstream.status >= 300 && upstream.status < 400) { + try { await upstream.body?.cancel(); } catch { /* best effort */ } + return relayError(502, "hub relay redirect refused"); + } + + let responseBody: Uint8Array | null; + const responseHeaders = filteredHeaders(upstream.headers, RESPONSE_HEADERS); + if (!headersWithinLimit(responseHeaders)) { + try { await upstream.body?.cancel(); } catch { /* best effort */ } + return relayError(502, "hub relay response headers too large"); + } + try { + responseBody = method === "HEAD" + ? null + : await boundedBody(upstream.body, upstream.headers.get("content-length"), HUB_RELAY_RESPONSE_BODY_MAX_BYTES); + } catch { + try { await upstream.body?.cancel(); } catch { /* best effort */ } + return relayError(502, "hub relay response body too large"); + } + return new Response(responseBody, { + status: upstream.status, + statusText: upstream.statusText, + headers: responseHeaders, + }); +} diff --git a/src/client/machine-api.ts b/src/client/machine-api.ts new file mode 100644 index 0000000000..fe92a4a88c --- /dev/null +++ b/src/client/machine-api.ts @@ -0,0 +1,139 @@ +import { journalOwner } from "../codex/journal"; +import { diagnoseCodexShim, installCodexShim, uninstallCodexShim } from "../codex/shim"; +import { readManagementJsonBody } from "../server/management/body"; +import type { OcxClientConnectionConfig } from "../types"; +import { disconnectClient, syncConnectedClient } from "./connect"; + +export type HubReachability = "unknown" | "online" | "offline" | "unauthorized"; + +export interface MachineStatusV1 { + mode: "client"; + connected: true; + machineBase: string; + sharedBase: string; + sharedServerOrigin: string; + managementTransport: "direct" | "relay"; + apiKeyId: string; + protocolVersion: 1; + connectedAt: string; + catalogSyncedAt?: string; + hubReachability: HubReachability; +} + +export interface MachineApiDeps { + sync: typeof syncConnectedClient; + disconnect: typeof disconnectClient; + scheduleStandaloneRecycle: () => void; + hubReachability?: () => HubReachability; + setHubReachability?: (value: HubReachability) => void; +} + +const defaultDeps: MachineApiDeps = { + sync: syncConnectedClient, + disconnect: disconnectClient, + scheduleStandaloneRecycle: () => {}, +}; + +function strictObject(value: unknown, allowed: readonly string[]): Record | null { + if (!value || typeof value !== "object" || Array.isArray(value)) return null; + const record = value as Record; + return Object.keys(record).every(key => allowed.includes(key)) ? record : null; +} + +async function jsonBody(req: Request): Promise { + try { + return await readManagementJsonBody(req); + } catch { + return Response.json({ error: "invalid JSON body" }, { status: 400 }); + } +} + +function statusPayload(req: Request, state: OcxClientConnectionConfig, deps: MachineApiDeps): MachineStatusV1 { + const machineBase = new URL(req.url).origin; + return { + mode: "client", + connected: true, + machineBase, + sharedBase: state.managementTransport === "relay" + ? `${machineBase}/api/machine/hub-relay` + : state.managementUrl, + sharedServerOrigin: state.managementUrl, + managementTransport: state.managementTransport, + apiKeyId: state.apiKeyId, + protocolVersion: state.protocolVersion, + connectedAt: state.connectedAt, + ...(state.catalogSyncedAt ? { catalogSyncedAt: state.catalogSyncedAt } : {}), + hubReachability: deps.hubReachability?.() ?? "unknown", + }; +} + +export async function handleMachineApi( + req: Request, + url: URL, + state: OcxClientConnectionConfig, + injected: MachineApiDeps = defaultDeps, +): Promise { + const deps = { ...defaultDeps, ...injected }; + if (url.pathname === "/api/machine/status" && req.method === "GET") { + return Response.json(statusPayload(req, state, deps), { headers: { "Cache-Control": "no-store" } }); + } + if (url.pathname === "/api/machine/clients" && req.method === "GET") { + return Response.json({ + selectedClients: [...state.selectedClients], + journalOwner: journalOwner(), + shim: diagnoseCodexShim(), + }, { headers: { "Cache-Control": "no-store" } }); + } + if (url.pathname === "/api/machine/sync" && req.method === "POST") { + const body = await jsonBody(req); + if (body instanceof Response) return body; + const input = strictObject(body, ["restartCodex"]); + if (!input || (input.restartCodex !== undefined && typeof input.restartCodex !== "boolean")) { + return Response.json({ error: "invalid sync request" }, { status: 400 }); + } + try { + const result = await deps.sync( + input.restartCodex === undefined ? {} : { restartCodex: input.restartCodex }, + ); + deps.setHubReachability?.("online"); + return Response.json({ success: true, ...result }); + } catch (error) { + const message = error instanceof Error ? error.message : "client sync failed"; + deps.setHubReachability?.(/unauthor/i.test(message) ? "unauthorized" : "offline"); + return Response.json({ success: false, error: message }, { status: 502 }); + } + } + if (url.pathname === "/api/machine/shim" && req.method === "GET") { + return Response.json(diagnoseCodexShim(), { headers: { "Cache-Control": "no-store" } }); + } + if (url.pathname === "/api/machine/shim" && req.method === "POST") { + const body = await jsonBody(req); + if (body instanceof Response) return body; + const input = strictObject(body, ["action"]); + if (!input || (input.action !== "install" && input.action !== "repair" && input.action !== "uninstall")) { + return Response.json({ error: "action must be install, repair, or uninstall" }, { status: 400 }); + } + try { + const result = input.action === "uninstall" ? uninstallCodexShim() : installCodexShim(); + return Response.json({ success: true, action: input.action, result, shim: diagnoseCodexShim() }); + } catch (error) { + return Response.json({ success: false, error: error instanceof Error ? error.message : "shim action failed" }, { status: 409 }); + } + } + if (url.pathname === "/api/machine/disconnect" && req.method === "POST") { + const body = await jsonBody(req); + if (body instanceof Response) return body; + const input = strictObject(body, ["keepCatalog"]); + if (!input || (input.keepCatalog !== undefined && typeof input.keepCatalog !== "boolean")) { + return Response.json({ error: "invalid disconnect request" }, { status: 400 }); + } + try { + const result = await deps.disconnect(input.keepCatalog === undefined ? {} : { keepCatalog: input.keepCatalog }); + deps.scheduleStandaloneRecycle(); + return Response.json({ success: true, ...result }, { status: 202 }); + } catch (error) { + return Response.json({ success: false, error: error instanceof Error ? error.message : "disconnect failed" }, { status: 409 }); + } + } + return null; +} diff --git a/src/client/machine-auth.ts b/src/client/machine-auth.ts new file mode 100644 index 0000000000..2b4dd3d6db --- /dev/null +++ b/src/client/machine-auth.ts @@ -0,0 +1,54 @@ +import type { OcxConfig } from "../types"; +import { + managementPrincipal, + requireManagementAuth, + type ManagementAuthState, +} from "../server/management-auth"; + +export const MACHINE_SESSION_HEADER = "x-opencodex-machine-session"; +export const MACHINE_GUI_ORIGIN_HEADER = "x-opencodex-machine-gui-origin"; +export const MACHINE_CSRF_HEADER = "x-opencodex-machine-csrf-token"; + +const MACHINE_AUTH_HEADERS = [ + MACHINE_SESSION_HEADER, + MACHINE_GUI_ORIGIN_HEADER, + MACHINE_CSRF_HEADER, +] as const; + +function machinePrincipalRequest(req: Request): Request { + const headers = new Headers(req.headers); + const token = headers.get(MACHINE_SESSION_HEADER); + const browserOrigin = headers.get(MACHINE_GUI_ORIGIN_HEADER); + const csrf = headers.get(MACHINE_CSRF_HEADER); + headers.delete("authorization"); + headers.delete("x-api-key"); + headers.delete("x-opencodex-api-key"); + headers.delete("x-opencodex-gui-origin"); + headers.delete("x-opencodex-csrf-token"); + if (token) headers.set("x-opencodex-api-key", token); + if (browserOrigin) { + headers.set("x-opencodex-gui-origin", browserOrigin); + headers.set("Origin", browserOrigin); + } + if (csrf) headers.set("x-opencodex-csrf-token", csrf); + return new Request(req, { headers }); +} + +export function requireMachineAuth( + req: Request, + state: ManagementAuthState, + config: OcxConfig, +): Response | null { + const synthetic = machinePrincipalRequest(req); + const error = requireManagementAuth(synthetic, state, config); + if (error) return error; + return managementPrincipal(synthetic, state, config) === "gui-session" + ? null + : Response.json({ error: "opencodex machine GUI session required" }, { status: 401 }); +} + +export function stripMachineAuthHeaders(headers: Headers): Headers { + const stripped = new Headers(headers); + for (const name of MACHINE_AUTH_HEADERS) stripped.delete(name); + return stripped; +} diff --git a/src/client/machine-listener.ts b/src/client/machine-listener.ts new file mode 100644 index 0000000000..da54d0d70f --- /dev/null +++ b/src/client/machine-listener.ts @@ -0,0 +1,130 @@ +import { readFileSync } from "node:fs"; +import type { Server } from "bun"; +import { loadConfig } from "../config"; +import { browserSecurityHeaders } from "../server/auth-cors"; +import { serveGuiFile, serveSessionBootstrap, rootFallbackPayload } from "../server/gui-static"; +import { + initializeManagementAuthState, + issueGuiSession, + managementPrincipal, + requireManagementAuth, + type ManagementAuthState, +} from "../server/management-auth"; +import type { OcxClientConnectionConfig, OcxConfig } from "../types"; +import { disconnectClient, syncConnectedClient } from "./connect"; +import { readClientConnectionState } from "./state"; +import { handleMachineApi, type HubReachability, type MachineApiDeps } from "./machine-api"; +import { requireMachineAuth } from "./machine-auth"; +import { relayHubManagementRequest } from "./hub-relay"; + +const VERSION = (() => { + try { return JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8")).version as string; } + catch { return "0.0.0"; } +})(); +const GUI_SPA_PATHS = new Set([ + "/dashboard", "/startup", "/providers", "/models", "/subagents", + "/logs", "/usage", "/storage", "/codex-set", "/integrations", +]); + +export interface MachineListenerDeps { + state?: OcxClientConnectionConfig; + managementAuthState?: ManagementAuthState; + fetchImpl?: typeof fetch; + machineApi?: Partial; +} + +function json404(req: Request): Response { + const url = new URL(req.url); + return Response.json({ error: "not_found", method: req.method, path: url.pathname }, { status: 404 }); +} + +function machinePolicyConfig(config: OcxConfig): OcxConfig { + return { ...config, hostname: "127.0.0.1" }; +} + +export function machineRouteAllowed(url: URL, req: Request, relayEnabled: boolean): boolean { + if (req.headers.get("upgrade")) return false; + const path = url.pathname; + if (req.method === "GET" && (path === "/healthz" || path === "/readyz" || path === "/" || path === "/opencodex-session")) return true; + if (req.method === "GET" && (path === "/api/machine/status" || path === "/api/machine/clients" || path === "/api/machine/shim")) return true; + if (req.method === "POST" && (path === "/api/machine/sync" || path === "/api/machine/shim" || path === "/api/machine/disconnect")) return true; + if (relayEnabled && path.startsWith("/api/machine/hub-relay/")) return true; + if (req.method !== "GET" || path.startsWith("/api/") || path.startsWith("/v1/")) return false; + return GUI_SPA_PATHS.has(path) + || path.startsWith("/integrations/") + || path.startsWith("/assets/") + && /\.(?:css|gif|ico|jpe?g|js|json|map|png|svg|webp|woff2?)$/i.test(path); +} + +export function startMachineListener( + port?: number, + deps: MachineListenerDeps = {}, +): Server { + const config = machinePolicyConfig(loadConfig()); + const connection = deps.state ?? (() => { + const state = readClientConnectionState(); + if (state.kind !== "connected") throw new Error(`machine listener requires connected client state, got ${state.kind}`); + return state.value; + })(); + const managementAuth = deps.managementAuthState ?? initializeManagementAuthState(config); + let hubReachability: HubReachability = "unknown"; + const machineApiDeps: MachineApiDeps = { + sync: deps.machineApi?.sync ?? syncConnectedClient, + disconnect: deps.machineApi?.disconnect ?? disconnectClient, + scheduleStandaloneRecycle: deps.machineApi?.scheduleStandaloneRecycle ?? (() => { + void import("./runtime").then(module => module.scheduleStandaloneRecycle()); + }), + hubReachability: deps.machineApi?.hubReachability ?? (() => hubReachability), + setHubReachability: deps.machineApi?.setHubReachability ?? (value => { hubReachability = value; }), + }; + const relayEnabled = connection.managementTransport === "relay"; + + return Bun.serve({ + port: port ?? config.port ?? 10100, + hostname: "127.0.0.1", + async fetch(req, server) { + const url = new URL(req.url); + if (!machineRouteAllowed(url, req, relayEnabled)) return json404(req); + if (url.pathname === "/healthz" && req.method === "GET") { + return Response.json({ service: "opencodex", version: VERSION, role: "client", pid: process.pid, port: server.port }); + } + if (url.pathname === "/readyz" && req.method === "GET") { + return Response.json({ service: "opencodex", version: VERSION, role: "client", status: "ready", pid: process.pid, port: server.port, protocolVersion: 1 }); + } + if (url.pathname.startsWith("/api/machine/hub-relay/")) { + if (!relayEnabled) return json404(req); + const authError = requireMachineAuth(req, managementAuth, config); + if (authError) return authError; + const prefix = "/api/machine/hub-relay"; + const suffix = `${url.pathname.slice(prefix.length)}${url.search}`; + const response = await relayHubManagementRequest(req, suffix, { + managementUrl: connection.managementUrl, + browserOrigin: req.headers.get("Origin") ?? "", + }, { fetchImpl: deps.fetchImpl }); + if (response.status === 401) hubReachability = "unauthorized"; + else if (response.status >= 500) hubReachability = "offline"; + else hubReachability = "online"; + return response; + } + if (url.pathname.startsWith("/api/machine/")) { + const authError = requireManagementAuth(req, managementAuth, config); + if (authError) return authError; + if (managementPrincipal(req, managementAuth, config) !== "gui-session") { + return Response.json({ error: "opencodex machine GUI session required" }, { status: 401 }); + } + return await handleMachineApi(req, url, connection, machineApiDeps) ?? json404(req); + } + + const session = (url.pathname === "/" || url.pathname === "/opencodex-session") + ? issueGuiSession(req, config, managementAuth, { trustedTailscaleIngress: false }) + : null; + if (url.pathname === "/opencodex-session" && session) return serveSessionBootstrap(session); + const gui = serveGuiFile(url.pathname, undefined, session ?? undefined); + if (gui) return gui; + if (url.pathname === "/") { + return Response.json(rootFallbackPayload(), { headers: browserSecurityHeaders() }); + } + return json404(req); + }, + }); +} diff --git a/src/client/runtime.ts b/src/client/runtime.ts new file mode 100644 index 0000000000..97bc672678 --- /dev/null +++ b/src/client/runtime.ts @@ -0,0 +1,76 @@ +import { spawn } from "node:child_process"; +import type { Server } from "bun"; +import { loadConfig } from "../config"; +import { removePid, removeRuntimePort, writePid, writeRuntimePort } from "../config/process-state"; +import { installCrashGuards } from "../lib/crash-guard"; +import { selfLaunchArgv } from "../lib/self-launch-argv"; +import { findAvailablePort } from "../server/ports"; +import { startMachineListener } from "./machine-listener"; +import { readClientConnectionState } from "./state"; + +let activeServer: Server | null = null; +let activePort: number | null = null; +let recycleScheduled = false; + +function cleanup(): void { + removePid(process.pid); + removeRuntimePort(process.pid); +} + +export function scheduleStandaloneRecycle(): void { + if (recycleScheduled) return; + recycleScheduled = true; + setTimeout(() => { + const port = activePort; + try { activeServer?.stop(true); } catch { /* best effort */ } + cleanup(); + if (process.env.OCX_SERVICE !== "1" && port) { + const child = spawn(process.execPath, selfLaunchArgv(["start", "--port", String(port)]), { + detached: true, + stdio: "ignore", + windowsHide: true, + env: { ...process.env }, + }); + child.unref(); + } + process.exit(0); + }, 50).unref(); +} + +export async function startClientRuntime( + options: { port?: number; block?: boolean } = {}, +): Promise { + const state = readClientConnectionState(); + if (state.kind !== "connected") throw new Error(`client runtime refused: client state is ${state.kind}`); + const config = loadConfig(); + const preferred = options.port ?? config.port ?? 10100; + const port = preferred === 0 + ? 0 + : await findAvailablePort(preferred, "127.0.0.1", { + preferRetryMs: options.port === undefined ? 750 : 5_000, + preferRetryIntervalMs: 50, + allowEphemeralFallback: options.port === undefined, + }); + const server = startMachineListener(port, { state: state.value }); + activeServer = server; + activePort = server.port; + installCrashGuards(); + writePid(process.pid); + writeRuntimePort({ pid: process.pid, port: server.port, hostname: "127.0.0.1" }); + + let shuttingDown = false; + const shutdown = () => { + if (shuttingDown) return; + shuttingDown = true; + try { server.stop(true); } finally { + cleanup(); + process.exit(0); + } + }; + process.on("SIGINT", shutdown); + process.on("SIGTERM", shutdown); + if (process.platform !== "win32") process.on("SIGHUP", shutdown); + process.on("exit", cleanup); + + if (options.block ?? true) await new Promise(() => {}); +} From 90c7e8fac99f6c700f1fd0a96af9201adcb22b25 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 03:46:25 +0900 Subject: [PATCH 056/172] test(two-plane): cover machine relay and GUI routing --- gui/src/connect-pairing.ts | 7 +- gui/tests/api-auth-deadline.test.ts | 40 +++++++ gui/tests/api-auth-memory.test.ts | 55 ++++++++- gui/tests/api-targets.test.ts | 66 +++++++++++ gui/tests/apikeys-layout.test.ts | 2 +- gui/tests/app-stop.test.ts | 17 ++- gui/tests/claudecode-layout.test.ts | 2 +- gui/tests/connect-pairing.test.ts | 141 ++++++++++++++++++++++ gui/tests/integrations-routing.test.ts | 22 ++++ gui/tests/usage-layout.test.ts | 18 ++- src/client/runtime.ts | 5 +- tests/cli-start-journal-order.test.ts | 31 ++++- tests/client-hub-relay.test.ts | 115 ++++++++++++++++++ tests/client-machine-listener.test.ts | 157 +++++++++++++++++++++++++ 14 files changed, 659 insertions(+), 19 deletions(-) create mode 100644 gui/tests/api-targets.test.ts create mode 100644 gui/tests/connect-pairing.test.ts create mode 100644 tests/client-hub-relay.test.ts create mode 100644 tests/client-machine-listener.test.ts diff --git a/gui/src/connect-pairing.ts b/gui/src/connect-pairing.ts index 13815468c7..488dc79e60 100644 --- a/gui/src/connect-pairing.ts +++ b/gui/src/connect-pairing.ts @@ -50,11 +50,11 @@ export function ConnectPairingForm({ } }; - return createElement("section", { className: "connect-pairing", "aria-labelledby": "connect-pairing-title" }, + return createElement("section", { className: "card connect-pairing", "aria-labelledby": "connect-pairing-title" }, createElement("h2", { id: "connect-pairing-title" }, t("connection.pairing.title")), createElement("p", null, t(target.transport === "relay" ? "connection.pairing.relayWarning" : "connection.pairing.body")), - createElement("form", { onSubmit: submit }, - createElement("label", { htmlFor: "connect-pairing-code" }, t("connection.pairing.code")), + createElement("form", { onSubmit: submit, className: "api-form-row" }, + createElement("label", { htmlFor: "connect-pairing-code", className: "field-label" }, t("connection.pairing.code")), createElement("input", { id: "connect-pairing-code", name: "pairingCode", @@ -63,6 +63,7 @@ export function ConnectPairingForm({ autoComplete: "off", spellCheck: false, disabled: busy, + className: "input mono", "aria-invalid": error || undefined, "aria-describedby": error ? "connect-pairing-error" : undefined, }), diff --git a/gui/tests/api-auth-deadline.test.ts b/gui/tests/api-auth-deadline.test.ts index 08ff8d6afe..4623867698 100644 --- a/gui/tests/api-auth-deadline.test.ts +++ b/gui/tests/api-auth-deadline.test.ts @@ -1,11 +1,13 @@ import { afterEach, beforeEach, expect, test } from "bun:test"; import { Window } from "happy-dom"; import { + configureApiTargets, installApiAuthFetch, resetApiAuthFetchForTests, setRebootstrapTimeoutForTests, setResolutionWatchdogForTests, } from "../src/api"; +import { targetsFromMachineStatus, type MachineStatusV1 } from "../src/api-targets"; const globals = ["document", "window", "navigator", "sessionStorage", "fetch"] as const; let previousGlobals: Record<(typeof globals)[number], unknown>; @@ -76,6 +78,44 @@ const MINTED = () => { Object.defineProperty(response, "url", { configurable: true, value: "http://localhost/opencodex-session" }); return response; }; + +test("a shared-target bootstrap watchdog does not block or clear the machine target", async () => { + for (const [name, content] of [ + ["opencodex-session-token", "ocx_session_machine"], + ["opencodex-session-csrf", "machine-csrf"], + ["opencodex-session-origin", "http://localhost"], + ["opencodex-session-server-origin", "http://localhost"], + ]) { + const meta = document.createElement("meta"); + meta.setAttribute("name", name); + meta.setAttribute("content", content); + document.head.append(meta); + } + const direct: MachineStatusV1 = { + mode: "client", connected: true, machineBase: "http://localhost", + sharedBase: "https://hub.example.test", sharedServerOrigin: "https://hub.example.test", + managementTransport: "direct", apiKeyId: "client-key-a", protocolVersion: 1, + connectedAt: "2026-08-28T00:00:00.000Z", hubReachability: "unknown", + }; + configureApiTargets(targetsFromMachineStatus("", direct)); + setRebootstrapTimeoutForTests(30); + const mockFetch = (async (input: RequestInfo | URL, init?: RequestInit) => { + const url = new URL(input instanceof Request ? input.url : String(input), "http://localhost/"); + if (url.origin === "https://hub.example.test" && url.pathname === "/opencodex-session") { + return hangUntilAborted(init?.signal); + } + if (url.origin === "https://hub.example.test") return new Response("unauthorized", { status: 401 }); + const token = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined)).get("x-opencodex-api-key"); + return new Response("{}", { status: token === "ocx_session_machine" ? 200 : 401 }); + }) as typeof fetch; + await installMockAuthFetch(mockFetch); + + const shared = fetch("https://hub.example.test/api/config"); + const machine = await fetch("/api/machine/status"); + expect(machine.status).toBe(200); + expect((await shared).status).toBe(401); + expect(promptCalls).toBe(0); +}); test("hung bootstrap fails the wave within the deadline and a later wave re-bootstraps to success", async () => { setRebootstrapTimeoutForTests(50); let bootstrapCalls = 0; diff --git a/gui/tests/api-auth-memory.test.ts b/gui/tests/api-auth-memory.test.ts index 834ecfa922..9a3ddf4fab 100644 --- a/gui/tests/api-auth-memory.test.ts +++ b/gui/tests/api-auth-memory.test.ts @@ -1,6 +1,7 @@ import { afterEach, beforeEach, expect, test } from "bun:test"; import { Window } from "happy-dom"; -import { installApiAuthFetch, resetApiAuthFetchForTests } from "../src/api"; +import { configureApiTargets, installApiAuthFetch, installApiSessionFromHtml, resetApiAuthFetchForTests } from "../src/api"; +import { targetsFromMachineStatus, type MachineStatusV1 } from "../src/api-targets"; const LEGACY_TOKEN_KEY = "opencodex-api-token"; const globals = ["document", "window", "navigator", "sessionStorage", "fetch"] as const; @@ -465,6 +466,13 @@ test("a session minted for another origin is rejected and the prompt fallback st test("a renewed two-origin session attaches only to its bound server and carries browser origin plus CSRF", async () => { injectSessionMeta("ocx_session_stale", "stale-csrf", "http://localhost"); + const status: MachineStatusV1 = { + mode: "client", connected: true, machineBase: "http://localhost", + sharedBase: "https://hub.example.test", sharedServerOrigin: "https://hub.example.test", + managementTransport: "direct", apiKeyId: "client-key-a", protocolVersion: 1, + connectedAt: "2026-08-28T00:00:00.000Z", hubReachability: "unknown", + }; + configureApiTargets(targetsFromMachineStatus("", status)); const seen = new Map(); let localApiCalls = 0; const record = (origin: string, headers: Headers) => { @@ -475,14 +483,14 @@ test("a renewed two-origin session attaches only to its bound server and carries const mockFetch = (async (input: RequestInfo | URL, init?: RequestInit) => { const url = new URL(input instanceof Request ? input.url : String(input), "http://localhost/"); const headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined)); - if (url.pathname === "/opencodex-session") { + if (url.origin === "https://hub.example.test" && url.pathname === "/opencodex-session") { return htmlResponseAt( sessionDocumentHtml("ocx_session_remote", "remote-csrf", "http://localhost", "https://hub.example.test"), "https://hub.example.test/opencodex-session", ); } record(url.origin, headers); - if (url.origin === "http://localhost") { + if (url.origin === "https://hub.example.test") { localApiCalls += 1; return new Response("{}", { status: localApiCalls === 1 ? 401 : 200 }); } @@ -490,11 +498,11 @@ test("a renewed two-origin session attaches only to its bound server and carries }) as typeof fetch; await installMockAuthFetch(mockFetch); - expect((await fetch("/api/config")).status).toBe(200); expect((await fetch("https://hub.example.test/api/config", { method: "POST" })).status).toBe(200); + expect((await fetch("/api/machine/status")).status).toBe(200); expect((await fetch("https://evil.example.test/api/config")).status).toBe(200); - const hubHeaders = seen.get("https://hub.example.test")?.[0]; + const hubHeaders = seen.get("https://hub.example.test")?.at(-1); expect(hubHeaders?.get("X-OpenCodex-API-Key")).toBe("ocx_session_remote"); expect(hubHeaders?.get("X-OpenCodex-GUI-Origin")).toBe("http://localhost"); expect(hubHeaders?.get("X-OpenCodex-CSRF-Token")).toBe("remote-csrf"); @@ -503,6 +511,43 @@ test("a renewed two-origin session attaches only to its bound server and carries expect(evilHeaders?.get("X-OpenCodex-GUI-Origin")).toBeNull(); }); +test("relay requests carry independent shared and machine sessions without cross-target leakage", async () => { + injectSessionMeta("ocx_session_machine", "machine-csrf", "http://localhost"); + const seen = new Map(); + const mockFetch = (async (input: RequestInfo | URL, init?: RequestInit) => { + const url = new URL(input instanceof Request ? input.url : String(input), "http://localhost/"); + seen.set(url.pathname, new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined))); + return new Response("{}", { status: 200 }); + }) as typeof fetch; + await installMockAuthFetch(mockFetch); + const relayStatus: MachineStatusV1 = { + mode: "client", connected: true, machineBase: "http://localhost", + sharedBase: "http://localhost/api/machine/hub-relay", sharedServerOrigin: "https://hub.example.test", + managementTransport: "relay", apiKeyId: "client-key-a", protocolVersion: 1, + connectedAt: "2026-08-28T00:00:00.000Z", hubReachability: "unknown", + }; + configureApiTargets(targetsFromMachineStatus("", relayStatus)); + expect(installApiSessionFromHtml("shared", sessionDocumentHtml( + "ocx_session_hub", "hub-csrf", "http://localhost", "https://hub.example.test", + ))).toBe(true); + + await fetch("/api/machine/status"); + await fetch("/api/machine/hub-relay/api/config", { method: "POST" }); + await fetch("https://evil.example/api/config"); + + const machine = seen.get("/api/machine/status")!; + expect(machine.get("x-opencodex-api-key")).toBe("ocx_session_machine"); + expect(machine.get("x-opencodex-machine-session")).toBeNull(); + const relay = seen.get("/api/machine/hub-relay/api/config")!; + expect(relay.get("x-opencodex-api-key")).toBe("ocx_session_hub"); + expect(relay.get("x-opencodex-csrf-token")).toBe("hub-csrf"); + expect(relay.get("x-opencodex-machine-session")).toBe("ocx_session_machine"); + expect(relay.get("x-opencodex-machine-csrf-token")).toBe("machine-csrf"); + const unknown = seen.get("/api/config")!; + expect(unknown.get("x-opencodex-api-key")).toBeNull(); + expect(unknown.get("x-opencodex-machine-session")).toBeNull(); +}); + test("a mismatched bootstrap response/server origin clears every in-memory session field", async () => { injectSessionMeta("ocx_session_stale", "stale-csrf", "http://localhost"); const seenKeys: Array = []; diff --git a/gui/tests/api-targets.test.ts b/gui/tests/api-targets.test.ts new file mode 100644 index 0000000000..b3f9f33e74 --- /dev/null +++ b/gui/tests/api-targets.test.ts @@ -0,0 +1,66 @@ +import { afterEach, beforeEach, describe, expect, test } from "bun:test"; +import { Window } from "happy-dom"; +import { + apiBaseForPlane, + discoverApiTargets, + relayUrlForPath, + standaloneApiTargets, + targetsFromMachineStatus, + type MachineStatusV1, +} from "../src/api-targets"; + +let win: Window; +let previousWindow: unknown; +let previousFetch: typeof fetch; + +const status = (transport: "direct" | "relay"): MachineStatusV1 => ({ + mode: "client", + connected: true, + machineBase: "http://localhost", + sharedBase: transport === "direct" ? "https://hub.example.test" : "http://localhost/api/machine/hub-relay", + sharedServerOrigin: "https://hub.example.test", + managementTransport: transport, + apiKeyId: "client-key-a", + protocolVersion: 1, + connectedAt: "2026-08-28T00:00:00.000Z", + hubReachability: "unknown", +}); + +beforeEach(() => { + previousWindow = Reflect.get(globalThis, "window"); + previousFetch = globalThis.fetch; + win = new Window({ url: "http://localhost/" }); + Object.defineProperty(globalThis, "window", { configurable: true, value: win }); +}); + +afterEach(() => { + globalThis.fetch = previousFetch; + Object.defineProperty(globalThis, "window", { configurable: true, value: previousWindow }); + win.close(); +}); + +describe("two-plane API targets", () => { + test("404 selects the unchanged standalone same-origin target", async () => { + globalThis.fetch = (async () => new Response(null, { status: 404 })) as typeof fetch; + const targets = await discoverApiTargets(""); + expect(targets).toEqual(standaloneApiTargets("")); + expect(apiBaseForPlane("machine", targets)).toBe(""); + expect(apiBaseForPlane("shared", targets)).toBe(""); + }); + + test("constructs exact direct and fixed relay shared bases", () => { + const direct = targetsFromMachineStatus("", status("direct")); + expect(direct.shared).toMatchObject({ baseUrl: "https://hub.example.test", serverOrigin: "https://hub.example.test", transport: "direct" }); + const relay = targetsFromMachineStatus("", status("relay")); + expect(relay.machine.baseUrl).toBe(""); + expect(relay.shared).toMatchObject({ baseUrl: "/api/machine/hub-relay", serverOrigin: "https://hub.example.test", transport: "relay" }); + expect(relayUrlForPath(relay.shared, "/api/usage?range=all")).toBe("/api/machine/hub-relay/api/usage?range=all"); + expect(() => relayUrlForPath(relay.shared, "/api/%2e%2e/config")).toThrow(); + expect(() => relayUrlForPath(relay.shared, "//evil.example/api/config")).toThrow(); + }); + + test("a machine-status network failure is not treated as standalone", async () => { + globalThis.fetch = (async () => { throw new TypeError("offline"); }) as typeof fetch; + await expect(discoverApiTargets("")).rejects.toThrow("local machine plane unavailable"); + }); +}); diff --git a/gui/tests/apikeys-layout.test.ts b/gui/tests/apikeys-layout.test.ts index a26e6c04f8..9291a846be 100644 --- a/gui/tests/apikeys-layout.test.ts +++ b/gui/tests/apikeys-layout.test.ts @@ -25,7 +25,7 @@ test("ApiKeys uses workspace shell (no classic layout toggle)", async () => { // ApiKeys is no longer rendered by App directly: WP5 made it one panel of // the Integrations tab strip, which is what passes `active` so a hidden // panel stops polling while its drafts stay mounted. - expect(app).toContain(""); + expect(app).toContain(''); expect(app).not.toContain(""); diff --git a/gui/tests/app-stop.test.ts b/gui/tests/app-stop.test.ts index 24046b4556..c0711fa0fe 100644 --- a/gui/tests/app-stop.test.ts +++ b/gui/tests/app-stop.test.ts @@ -9,6 +9,20 @@ function response(body: unknown, status = 200): Response { } describe("App proxy stop", () => { + test("routes standalone stop and connected disconnect to different machine mutations", async () => { + const seen: Array<{ url: string; method: string; body: unknown }> = []; + const fetchFn = (async (input: RequestInfo | URL, init?: RequestInit) => { + seen.push({ url: String(input), method: String(init?.method), body: init?.body }); + return response({ success: true }, init?.body ? 202 : 200); + }) as typeof fetch; + expect((await requestProxyStop("http://machine", { fetchFn })).accepted).toBe(true); + expect((await requestProxyStop("http://machine", { fetchFn, mode: "client" })).accepted).toBe(true); + expect(seen).toEqual([ + { url: "http://machine/api/stop", method: "POST", body: undefined }, + { url: "http://machine/api/machine/disconnect", method: "POST", body: "{}" }, + ]); + }); + test("releases the pending UI and exposes a non-2xx server message", async () => { const outcome = await requestProxyStop("", { fetchFn: (async () => response({ @@ -61,7 +75,8 @@ describe("App proxy stop", () => { expect(brandIdx).toBeGreaterThan(handleStopIdx); const handler = app.slice(handleStopIdx, brandIdx); - expect(handler).toContain("await requestProxyStop(API_BASE"); + expect(handler).toContain("await requestProxyStop(machineBase"); + expect(handler).toContain('mode: targets.connected ? "client" : "standalone"'); expect(handler).toContain("if (!outcome.accepted)"); expect(handler).toContain("setStopping(false)"); expect(handler).toContain("alert(outcome.message)"); diff --git a/gui/tests/claudecode-layout.test.ts b/gui/tests/claudecode-layout.test.ts index f24113058f..7dc26b08f5 100644 --- a/gui/tests/claudecode-layout.test.ts +++ b/gui/tests/claudecode-layout.test.ts @@ -17,7 +17,7 @@ test("ClaudeCode renders the denser workspace rail layout", async () => { // Claude is now a panel of the Integrations tab strip rather than its own // top-level page, so App renders the shell and the shell renders Claude. - expect(app).toContain(""); + expect(app).toContain(''); const integrations = await Bun.file(new URL("../src/pages/Integrations.tsx", import.meta.url)).text(); expect(integrations).toContain(""); // Title/subtitle sit above the Code/Desktop strip (not inside each panel). diff --git a/gui/tests/connect-pairing.test.ts b/gui/tests/connect-pairing.test.ts new file mode 100644 index 0000000000..4d373cfc7a --- /dev/null +++ b/gui/tests/connect-pairing.test.ts @@ -0,0 +1,141 @@ +import { afterEach, expect, test } from "bun:test"; +import { Window } from "happy-dom"; +import { act, createElement } from "react"; + +test("App mounts the relay pairing form and installs only the returned shared session", async () => { + const keys = ["window", "document", "navigator", "sessionStorage", "localStorage", "fetch", "confirm", "alert", "IS_REACT_ACT_ENVIRONMENT", "__APP_VERSION__"] as const; + const previous = Object.fromEntries(keys.map(key => [key, Reflect.get(globalThis, key)])); + const win = new Window({ url: "http://localhost/#dashboard" }); + Object.defineProperties(globalThis, { + window: { configurable: true, value: win }, + document: { configurable: true, value: win.document }, + navigator: { configurable: true, value: win.navigator }, + sessionStorage: { configurable: true, value: win.sessionStorage }, + localStorage: { configurable: true, value: win.localStorage }, + confirm: { configurable: true, value: () => true }, + alert: { configurable: true, value: () => {} }, + IS_REACT_ACT_ENVIRONMENT: { configurable: true, value: true }, + __APP_VERSION__: { configurable: true, value: "0.0.0-test" }, + }); + for (const [name, content] of [ + ["opencodex-session-token", "ocx_session_machine"], + ["opencodex-session-csrf", "machine-csrf"], + ["opencodex-session-origin", "http://localhost"], + ["opencodex-session-server-origin", "http://localhost"], + ]) { + const meta = document.createElement("meta"); + meta.name = name; + meta.content = content; + document.head.append(meta); + } + + let pairingRequest: { method: string; body: string; headers: Headers } | null = null; + const sessionHtml = [ + '', + '', + '', + '', + ].join(""); + const mockFetch = (async (input: RequestInfo | URL, init?: RequestInit) => { + const url = new URL(input instanceof Request ? input.url : String(input), "http://localhost/"); + const headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined)); + if (url.pathname === "/api/machine/status") return Response.json({ + mode: "client", connected: true, machineBase: "http://localhost", + sharedBase: "http://localhost/api/machine/hub-relay", + sharedServerOrigin: "https://hub.example.test", managementTransport: "relay", + apiKeyId: "client-key-a", protocolVersion: 1, connectedAt: "2026-08-28T00:00:00.000Z", + hubReachability: "unknown", + }); + if (url.pathname === "/api/machine/hub-relay/opencodex-session" && init?.method === "POST") { + pairingRequest = { method: init.method, body: String(init.body), headers }; + return new Response(sessionHtml, { headers: { "Content-Type": "text/html" } }); + } + if (url.pathname === "/healthz") return Response.json({ version: "0.0.0-test" }); + return Response.json({}); + }) as typeof fetch; + Object.defineProperties(globalThis, { + fetch: { configurable: true, value: mockFetch }, + }); + Object.defineProperty(win, "fetch", { configurable: true, value: mockFetch }); + + const container = document.createElement("div"); + document.body.append(container); + const { LanguageProvider } = await import("../src/i18n/provider"); + const { default: App } = await import("../src/App"); + const { createRoot } = await import("react-dom/client"); + const root = createRoot(container); + try { + await act(async () => { root.render(createElement(LanguageProvider, null, createElement(App))); }); + const deadline = Date.now() + 1_000; + while (!container.querySelector("#connect-pairing-code")) { + if (Date.now() >= deadline) throw new Error("pairing form did not mount from App"); + await act(async () => { await new Promise(resolve => win.setTimeout(resolve, 10)); }); + } + const input = container.querySelector("#connect-pairing-code") as HTMLInputElement; + Object.getOwnPropertyDescriptor(win.HTMLInputElement.prototype, "value")!.set!.call(input, `ocx_pair_${"a".repeat(43)}`); + input.dispatchEvent(new win.Event("input", { bubbles: true })); + input.dispatchEvent(new win.Event("change", { bubbles: true })); + const form = input.closest("form")!; + await act(async () => { form.dispatchEvent(new win.Event("submit", { bubbles: true, cancelable: true })); }); + const successDeadline = Date.now() + 1_000; + while (container.querySelector("#connect-pairing-code")) { + if (Date.now() >= successDeadline) throw new Error("pairing form did not hide after success"); + await act(async () => { await Promise.resolve(); }); + } + expect(pairingRequest?.method).toBe("POST"); + expect(pairingRequest?.body).toBe(JSON.stringify({ grant: `ocx_pair_${"a".repeat(43)}` })); + expect(pairingRequest?.headers.get("x-opencodex-machine-session")).toBe("ocx_session_machine"); + expect(pairingRequest?.headers.get("x-opencodex-api-key")).toBeNull(); + } finally { + await act(async () => { root.unmount(); }); + container.remove(); + win.close(); + for (const key of keys) Object.defineProperty(globalThis, key, { configurable: true, value: previous[key] }); + } +}); + +test("a refused pairing renders an accessible error without clearing the pasted code", async () => { + const keys = ["window", "document", "navigator", "sessionStorage", "fetch", "IS_REACT_ACT_ENVIRONMENT"] as const; + const previous = Object.fromEntries(keys.map(key => [key, Reflect.get(globalThis, key)])); + const win = new Window({ url: "http://localhost/" }); + const mockFetch = (async () => new Response("refused", { status: 403 })) as typeof fetch; + Object.defineProperties(globalThis, { + window: { configurable: true, value: win }, + document: { configurable: true, value: win.document }, + navigator: { configurable: true, value: win.navigator }, + sessionStorage: { configurable: true, value: win.sessionStorage }, + fetch: { configurable: true, value: mockFetch }, + IS_REACT_ACT_ENVIRONMENT: { configurable: true, value: true }, + }); + Object.defineProperty(win, "fetch", { configurable: true, value: mockFetch }); + const container = document.createElement("div"); + document.body.append(container); + const { LanguageProvider } = await import("../src/i18n/provider"); + const { ConnectPairingForm } = await import("../src/connect-pairing"); + const { createRoot } = await import("react-dom/client"); + const root = createRoot(container); + const code = `ocx_pair_${"b".repeat(43)}`; + try { + await act(async () => { + root.render(createElement(LanguageProvider, null, createElement(ConnectPairingForm, { + target: { id: "shared", baseUrl: "https://hub.example.test", serverOrigin: "https://hub.example.test", bootstrapPath: "https://hub.example.test/opencodex-session", transport: "direct" }, + onConnected: () => { throw new Error("unexpected success"); }, + }))); + }); + const input = container.querySelector("#connect-pairing-code") as HTMLInputElement; + Object.getOwnPropertyDescriptor(win.HTMLInputElement.prototype, "value")!.set!.call(input, code); + await act(async () => { input.dispatchEvent(new win.Event("input", { bubbles: true })); }); + await act(async () => { input.closest("form")!.dispatchEvent(new win.Event("submit", { bubbles: true, cancelable: true })); }); + const deadline = Date.now() + 1_000; + while (!container.querySelector('[role="alert"]')) { + if (Date.now() >= deadline) throw new Error("pairing error did not render"); + await act(async () => { await Promise.resolve(); }); + } + expect(input.value).toBe(code); + } finally { + await act(async () => { root.unmount(); }); + container.remove(); + win.close(); + for (const key of keys) Object.defineProperty(globalThis, key, { configurable: true, value: previous[key] }); + } +}); diff --git a/gui/tests/integrations-routing.test.ts b/gui/tests/integrations-routing.test.ts index f96dff5b73..385148c042 100644 --- a/gui/tests/integrations-routing.test.ts +++ b/gui/tests/integrations-routing.test.ts @@ -119,6 +119,28 @@ describe("the collapse disturbs no neighbouring route", () => { }); }); +describe("two-plane integration call routing", () => { + test("existing integration descendants stay on the shared base and only machine controls use machineApiBase", async () => { + const app = await Bun.file(new URL("../src/App.tsx", import.meta.url)).text(); + const integrations = await Bun.file(new URL("../src/pages/Integrations.tsx", import.meta.url)).text(); + const startup = await Bun.file(new URL("../src/pages/Startup.tsx", import.meta.url)).text(); + expect(app).toContain(''); + expect(app).toContain(''); + for (const component of ["ApiKeys", "Grok", "Claude", "IntegrationsOverview", "FileIntegrationPage"]) { + expect(integrations).toContain(`${component}`); + } + expect(integrations).toContain(" { let win: Window; let previous: Record; diff --git a/gui/tests/usage-layout.test.ts b/gui/tests/usage-layout.test.ts index d77388b153..922bf9baf5 100644 --- a/gui/tests/usage-layout.test.ts +++ b/gui/tests/usage-layout.test.ts @@ -23,13 +23,23 @@ test("Usage renders every section in one scrollable column with a sticky strip", expect(page).toContain(""); + expect(app).toContain(''); expect(css).toContain("styles-usage-workspace.css"); // The strip has to stay reachable while reading down the page. expect(css).toContain(".section-tabs"); expect(css).toContain("position: sticky"); }); +test("connected Usage defaults to the exact machine key and can toggle hub-wide without local fallback", async () => { + const src = await Bun.file(new URL("../src/pages/Usage.tsx", import.meta.url)).text(); + expect(src).toContain('useState("machine")'); + expect(src).toContain('query.set("apiKeyId", apiKeyId)'); + expect(src).toContain('setScope("hub")'); + expect(src).toContain('connected ? "connected" : "standalone"'); + expect(src).toContain('t("usage.hubOffline")'); + expect(src).not.toContain("/api/machine/usage"); +}); + test("Usage workspace sections mount report panels in order", async () => { const src = await Bun.file(new URL("../src/pages/Usage.tsx", import.meta.url)).text(); @@ -61,7 +71,7 @@ test("Usage loading and empty states guard the workspace body", async () => { }); test("usage workspace i18n keys exist in every locale", async () => { - const locales = ["en", "de", "fr", "ja", "ko", "ru", "zh", "zh-TW"] as const; + const locales = ["en", "de", "fr", "ja", "ko", "ru", "tr", "zh", "zh-TW"] as const; for (const locale of locales) { const dict = await Bun.file(new URL(`../src/i18n/${locale}.ts`, import.meta.url)).text(); expect(dict).toContain('"usage.workspace.sections":'); @@ -70,6 +80,10 @@ test("usage workspace i18n keys exist in every locale", async () => { expect(dict).toContain('"usage.historyTruncated":'); expect(dict).toContain('"usage.historyTruncatedWindow":'); expect(dict).toContain('"api.attribution.totalRequestsAvailable":'); + expect(dict).toContain('"usage.source.connected":'); + expect(dict).toContain('"usage.scope.machine":'); + expect(dict).toContain('"usage.scope.hub":'); + expect(dict).toContain('"usage.hubOffline":'); } }); diff --git a/src/client/runtime.ts b/src/client/runtime.ts index 97bc672678..b9b302c02e 100644 --- a/src/client/runtime.ts +++ b/src/client/runtime.ts @@ -20,7 +20,7 @@ function cleanup(): void { export function scheduleStandaloneRecycle(): void { if (recycleScheduled) return; recycleScheduled = true; - setTimeout(() => { + const timer = setTimeout(() => { const port = activePort; try { activeServer?.stop(true); } catch { /* best effort */ } cleanup(); @@ -34,7 +34,8 @@ export function scheduleStandaloneRecycle(): void { child.unref(); } process.exit(0); - }, 50).unref(); + }, 50); + if (typeof timer === "object" && "unref" in timer) timer.unref(); } export async function startClientRuntime( diff --git a/tests/cli-start-journal-order.test.ts b/tests/cli-start-journal-order.test.ts index 4ca5a077c1..c8ac68e9d8 100644 --- a/tests/cli-start-journal-order.test.ts +++ b/tests/cli-start-journal-order.test.ts @@ -184,10 +184,33 @@ describe("start and ensure journal ownership (#1230)", () => { timestamp: new Date().toISOString(), })); - const result = await runCli(fx, ["start"]); - expect(result.exitCode).toBe(1); - expect(readFileSync(fx.configPath, "utf8")).toBe(matches ? injected : original); - expect(existsSync(fx.journalPath)).toBe(matches); + const child = Bun.spawn([process.execPath, cliPath, "start"], { + cwd: fx.root, + env: fx.env, + stdout: "pipe", + stderr: "pipe", + }); + children.push(child); + const runtimePath = join(fx.ocxHome, "runtime-port.json"); + const runtime = await waitFor(() => { + if (!existsSync(runtimePath)) return null; + try { + const value = JSON.parse(readFileSync(runtimePath, "utf8")) as { pid?: number; port?: number; hostname?: string }; + return value.pid === child.pid && typeof value.port === "number" && value.port > 0 ? value : null; + } catch { return null; } + }, "connected client runtime record"); + try { + const health = await fetch(`http://127.0.0.1:${runtime.port}/healthz`).then(response => response.json()) as { role?: string }; + expect(health.role).toBe("client"); + expect(runtime.hostname).toBe("127.0.0.1"); + expect((await fetch(`http://127.0.0.1:${runtime.port}/v1/models`)).status).toBe(404); + expect((await fetch(`http://127.0.0.1:${runtime.port}/api/config`)).status).toBe(404); + expect(readFileSync(fx.configPath, "utf8")).toBe(matches ? injected : original); + expect(existsSync(fx.journalPath)).toBe(matches); + } finally { + child.kill("SIGTERM"); + await child.exited; + } } }, 30_000); diff --git a/tests/client-hub-relay.test.ts b/tests/client-hub-relay.test.ts new file mode 100644 index 0000000000..3c38a38085 --- /dev/null +++ b/tests/client-hub-relay.test.ts @@ -0,0 +1,115 @@ +import { describe, expect, test } from "bun:test"; +import { + HUB_RELAY_REQUEST_BODY_MAX_BYTES, + HUB_RELAY_RESPONSE_BODY_MAX_BYTES, + relayHubManagementRequest, +} from "../src/client/hub-relay"; + +const target = { managementUrl: "https://hub.example.test", browserOrigin: "http://127.0.0.1:10100" }; + +function relayRequest(path: string, init: RequestInit = {}): Request { + return new Request(`http://127.0.0.1:10100/api/machine/hub-relay${path}`, { + ...init, + headers: { + Origin: target.browserOrigin, + "X-OpenCodex-API-Key": "ocx_session_hub", + "X-OpenCodex-GUI-Origin": target.browserOrigin, + "X-OpenCodex-CSRF-Token": "hub-csrf", + "X-OpenCodex-Machine-Session": "ocx_session_machine", + "X-OpenCodex-Machine-GUI-Origin": target.browserOrigin, + "X-OpenCodex-Machine-CSRF-Token": "machine-csrf", + Cookie: "private=1", + Forwarded: "for=192.0.2.1", + Connection: "keep-alive", + ...init.headers, + }, + }); +} + +describe("fixed-target hub management relay", () => { + test("forwards only to the configured hub and strips machine, cookie, forwarding, and hop headers", async () => { + let captured: { url: string; headers: Headers } | null = null; + const response = await relayHubManagementRequest(relayRequest("/api/usage?range=all"), "/api/usage?range=all", target, { + fetchImpl: (async (input, init) => { + captured = { url: String(input), headers: new Headers(init?.headers) }; + return Response.json({ ok: true }, { headers: { "Set-Cookie": "hub=secret", Connection: "close", ETag: "v1" } }); + }) as typeof fetch, + }); + expect(response.status).toBe(200); + expect(captured!.url).toBe("https://hub.example.test/api/usage?range=all"); + expect(captured!.headers.get("x-opencodex-api-key")).toBe("ocx_session_hub"); + for (const header of ["x-opencodex-machine-session", "cookie", "forwarded", "connection", "host"]) { + expect(captured!.headers.get(header)).toBeNull(); + } + expect(response.headers.get("set-cookie")).toBeNull(); + expect(response.headers.get("connection")).toBeNull(); + expect(response.headers.get("etag")).toBe("v1"); + }); + + test("POST pairing reaches only /opencodex-session and forwards browser Origin verbatim", async () => { + let captured: { url: string; method: string; origin: string | null } | null = null; + const request = relayRequest("/opencodex-session", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ grant: `ocx_pair_${"a".repeat(43)}` }), + }); + const response = await relayHubManagementRequest(request, "/opencodex-session", target, { + fetchImpl: (async (input, init) => { + captured = { url: String(input), method: String(init?.method), origin: new Headers(init?.headers).get("origin") }; + return new Response("", { headers: { "Content-Type": "text/html" } }); + }) as typeof fetch, + }); + expect(response.status).toBe(200); + expect(captured).toEqual({ url: "https://hub.example.test/opencodex-session", method: "POST", origin: target.browserOrigin }); + }); + + test("rejects traversal, authority, encoded separator, and caller-host variants before outbound I/O", async () => { + let calls = 0; + const fetchImpl = (async () => { calls += 1; return new Response(); }) as typeof fetch; + for (const suffix of [ + "//evil.example/api/config", + "/api/../opencodex-session", + "/api/%2e%2e/opencodex-session", + "/api/%2f%2fevil.example/config", + "/api/%5cevil", + "https://evil.example/api/config", + "/v1/models", + "/opencodex-session?host=evil.example", + ]) { + const response = await relayHubManagementRequest(relayRequest("/api/config"), suffix, target, { fetchImpl }); + expect(response.status).toBe(404); + } + expect(calls).toBe(0); + }); + + test("rejects redirects, request and response overflow, and timeout without exposing bodies", async () => { + const redirected = await relayHubManagementRequest(relayRequest("/api/config"), "/api/config", target, { + fetchImpl: (async () => new Response(null, { status: 302, headers: { Location: "https://evil.example" } })) as typeof fetch, + }); + expect(redirected.status).toBe(502); + + const oversizedRequest = relayRequest("/api/config", { + method: "POST", + headers: { "Content-Type": "application/json", "Content-Length": String(HUB_RELAY_REQUEST_BODY_MAX_BYTES + 1) }, + body: "{}", + }); + let calls = 0; + expect((await relayHubManagementRequest(oversizedRequest, "/api/config", target, { + fetchImpl: (async () => { calls += 1; return new Response(); }) as typeof fetch, + })).status).toBe(413); + expect(calls).toBe(0); + + const oversizedResponse = await relayHubManagementRequest(relayRequest("/api/config"), "/api/config", target, { + fetchImpl: (async () => new Response("x", { headers: { "Content-Length": String(HUB_RELAY_RESPONSE_BODY_MAX_BYTES + 1) } })) as typeof fetch, + }); + expect(oversizedResponse.status).toBe(502); + + const timedOut = await relayHubManagementRequest(relayRequest("/api/config"), "/api/config", target, { + timeoutMs: 5, + fetchImpl: (async (_input, init) => new Promise((_resolve, reject) => { + init?.signal?.addEventListener("abort", () => reject(new DOMException("Aborted", "AbortError")), { once: true }); + })) as typeof fetch, + }); + expect(timedOut.status).toBe(502); + }); +}); diff --git a/tests/client-machine-listener.test.ts b/tests/client-machine-listener.test.ts new file mode 100644 index 0000000000..511961c8bd --- /dev/null +++ b/tests/client-machine-listener.test.ts @@ -0,0 +1,157 @@ +import { afterEach, beforeEach, describe, expect, test } from "bun:test"; +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { Server } from "bun"; +import { startMachineListener } from "../src/client/machine-listener"; +import type { OcxClientConnectionConfig } from "../src/types"; +import type { ManagementAuthState } from "../src/server/management-auth"; + +let root = ""; +let previousHome: string | undefined; +const servers: Server[] = []; + +const connection = (transport: "direct" | "relay" = "direct"): OcxClientConnectionConfig => ({ + serverUrl: "https://hub.example.test", + managementUrl: "https://hub.example.test", + managementTransport: transport, + selectedClients: ["codex"], + tokenEnv: "OPENCODEX_API_AUTH_TOKEN", + apiKeyId: "client-key-a", + tokenFingerprint: "a".repeat(64), + protocolVersion: 1, + connectedAt: "2026-08-28T00:00:00.000Z", + catalogSyncedAt: "2026-08-28T00:01:00.000Z", +}); + +function authState(): ManagementAuthState { + return { + available: true, + token: `ocx_admin_${"a".repeat(43)}`, + source: "environment", + sessions: new Map(), + pairingGrants: new Map(), + }; +} + +beforeEach(() => { + previousHome = process.env.OPENCODEX_HOME; + root = mkdtempSync(join(tmpdir(), "ocx-machine-listener-")); + process.env.OPENCODEX_HOME = root; + mkdirSync(root, { recursive: true }); + writeFileSync(join(root, "config.json"), JSON.stringify({ + port: 0, + hostname: "0.0.0.0", + providers: {}, + defaultProvider: "openai", + })); +}); + +afterEach(async () => { + for (const server of servers.splice(0)) await server.stop(true); + if (previousHome === undefined) delete process.env.OPENCODEX_HOME; + else process.env.OPENCODEX_HOME = previousHome; + if (root) rmSync(root, { recursive: true, force: true }); +}); + +function meta(html: string, name: string): string { + const match = new RegExp(`, mutation = false): Promise { + const bootstrap = await fetch(new URL("/opencodex-session", server.url)); + const html = await bootstrap.text(); + const headers = new Headers({ + "X-OpenCodex-API-Key": meta(html, "opencodex-session-token"), + "X-OpenCodex-GUI-Origin": meta(html, "opencodex-session-origin"), + }); + if (mutation) { + headers.set("Origin", meta(html, "opencodex-session-origin")); + headers.set("X-OpenCodex-CSRF-Token", meta(html, "opencodex-session-csrf")); + headers.set("Content-Type", "application/json"); + } + return headers; +} + +describe("client machine listener", () => { + test("binds IPv4 loopback and default-denies shared/data-plane routes", async () => { + const server = startMachineListener(0, { state: connection(), managementAuthState: authState() }); + servers.push(server); + expect(server.hostname).toBe("127.0.0.1"); + expect((await fetch(new URL("/healthz", server.url))).status).toBe(200); + expect((await fetch(new URL("/readyz", server.url))).status).toBe(200); + expect((await fetch(new URL("/opencodex-session", server.url))).headers.get("content-type")).toContain("text/html"); + for (const path of [ + "/v1/responses", "/v1/models", "/v1/catalog", "/api/config", "/api/usage", + "/api/oauth/providers", "/lab", "/oauth/callback", "/api/machine/unknown", + ]) { + const response = await fetch(new URL(path, server.url), { method: path === "/v1/responses" ? "POST" : "GET" }); + expect(response.status).toBe(404); + expect((await response.json()).error).toBe("not_found"); + } + expect((await fetch(new URL("/api/machine/hub-relay/api/config", server.url))).status).toBe(404); + expect((await fetch(new URL("/api/machine/status", server.url), { method: "POST" })).status).toBe(404); + }); + + test("requires a GUI session for safe reads and Origin plus CSRF for mutations", async () => { + let syncCalls = 0; + const server = startMachineListener(0, { + state: connection(), + managementAuthState: authState(), + machineApi: { + sync: async () => { syncCalls += 1; return { catalogWritten: false, cacheSynced: true, injected: true, stale: false }; }, + }, + }); + servers.push(server); + const statusUrl = new URL("/api/machine/status", server.url); + expect((await fetch(statusUrl)).status).toBe(401); + expect((await fetch(statusUrl, { headers: { "X-OpenCodex-API-Key": `ocx_admin_${"a".repeat(43)}` } })).status).toBe(401); + + const safeHeaders = await guiHeaders(server); + const status = await fetch(statusUrl, { headers: safeHeaders }); + expect(status.status).toBe(200); + const body = await status.json(); + expect(body).toMatchObject({ mode: "client", connected: true, apiKeyId: "client-key-a", managementTransport: "direct" }); + const serialized = JSON.stringify(body); + expect(serialized).not.toContain("tokenFingerprint"); + expect(serialized).not.toContain("a".repeat(64)); + + const syncUrl = new URL("/api/machine/sync", server.url); + expect((await fetch(syncUrl, { method: "POST", headers: safeHeaders, body: "{}" })).status).toBe(401); + expect(syncCalls).toBe(0); + const mutationHeaders = await guiHeaders(server, true); + expect((await fetch(syncUrl, { method: "POST", headers: mutationHeaders, body: "{}" })).status).toBe(200); + expect(syncCalls).toBe(1); + }); + + test("disconnect commits before 202 and schedules standalone recycle while the hub is offline", async () => { + let disconnected = false; + let recycled = false; + const server = startMachineListener(0, { + state: connection(), + managementAuthState: authState(), + machineApi: { + disconnect: async () => { + disconnected = true; + return { restored: true, tokenRemoved: true, catalogRemoved: true, apiKeyId: "client-key-a" }; + }, + scheduleStandaloneRecycle: () => { recycled = disconnected; }, + }, + }); + servers.push(server); + const response = await fetch(new URL("/api/machine/disconnect", server.url), { + method: "POST", + headers: await guiHeaders(server, true), + body: "{}", + }); + expect(response.status).toBe(202); + expect(disconnected).toBe(true); + expect(recycled).toBe(true); + }); + + test("refuses startup without matching durable connected state", () => { + expect(() => startMachineListener(0, { managementAuthState: authState() })).toThrow(/requires connected client state/); + }); +}); From fcd79c40816f72a75781a8d29b2440404e94f905 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 04:20:00 +0900 Subject: [PATCH 057/172] feat(deploy): add loopback hub management ingress --- src/config.ts | 55 +++++++++++++++++++++++++++++++- src/server/index.ts | 76 ++++++++++++++++++++++++++++++++++++++++++--- src/types/config.ts | 8 +++++ 3 files changed, 134 insertions(+), 5 deletions(-) diff --git a/src/config.ts b/src/config.ts index 9173c4b353..c2d4158032 100644 --- a/src/config.ts +++ b/src/config.ts @@ -884,6 +884,12 @@ const hubConfigSchema = z.object({ } return origin; }).optional(), + // A malformed hand edit disables only the optional ingress. Live writes are rejected by + // managementIngressConfigError before this load-time degradation can hide the mistake. + managementIngress: z.union([ + z.object({ enabled: z.literal(false) }).strict(), + z.object({ enabled: z.literal(true), port: z.number().int().min(1).max(65535) }).strict(), + ]).optional().catch(undefined), }).strict(); const tailscaleUserSchema = z.string().trim().min(1).superRefine((value, ctx) => { @@ -2404,6 +2410,52 @@ function loopbackListenerPortError(value: unknown): string | null { return null; } +/** + * Validate the hub management ingress at the live-write boundary. + * + * The persisted schema intentionally degrades a malformed hand edit to disabled so a typo in + * this opt-in listener cannot discard providers or credentials. A live config mutation must not + * get that leniency: it receives an exact field error before the degrading schema is applied. + */ +function managementIngressConfigError(value: unknown): string | null { + const raw = rawConfigRecord(value); + if (!raw) return null; + const hub = rawConfigRecord(raw.hub); + if (!hub || !Object.hasOwn(hub, "managementIngress") || hub.managementIngress === undefined) return null; + const ingress = rawConfigRecord(hub.managementIngress); + if (!ingress) { + return "schema_invalid: hub.managementIngress: must be an object or omitted"; + } + if (typeof ingress.enabled !== "boolean") { + return "schema_invalid: hub.managementIngress.enabled: must be a boolean"; + } + const keys = Object.keys(ingress); + if (ingress.enabled === false) { + return keys.length === 1 + ? null + : "schema_invalid: hub.managementIngress: disabled ingress accepts only enabled"; + } + if (keys.some(key => key !== "enabled" && key !== "port")) { + return "schema_invalid: hub.managementIngress: contains an unsupported field"; + } + const ingressPort = ingress.port; + if (typeof ingressPort !== "number" || !Number.isInteger(ingressPort) || ingressPort < 1 || ingressPort > 65535) { + return "schema_invalid: hub.managementIngress.port: must be an integer port when enabled"; + } + if (raw.runtimeRole !== "hub") { + return "schema_invalid: hub.managementIngress: enabled ingress requires runtimeRole hub"; + } + const proxyPort = typeof raw.port === "number" ? raw.port : 10100; + if (proxyPort === ingressPort) { + return "schema_invalid: hub.managementIngress.port: must differ from the proxy port"; + } + const loopback = rawConfigRecord(raw.unauthenticatedLoopbackListener); + if (loopback?.enabled === true && loopback.port === ingressPort) { + return "schema_invalid: hub.managementIngress.port: must differ from unauthenticatedLoopbackListener.port"; + } + return null; +} + export function validateConfigCandidate(value: unknown): { ok: true; config: OcxConfig } | { ok: false; error: string } { const boundaryError = blankHostnameError(value) ?? claudeSubagentEffortError(value) @@ -2419,7 +2471,8 @@ export function validateConfigCandidate(value: unknown): { ok: true; config: Ocx ?? remoteGuiConfigError(value) ?? clientConnectionConfigError(value) ?? clientRolePairError(value) - ?? loopbackListenerPortError(value); + ?? loopbackListenerPortError(value) + ?? managementIngressConfigError(value); if (boundaryError) return { ok: false, error: boundaryError }; const result = configSchema.safeParse(value); if (result.success) { diff --git a/src/server/index.ts b/src/server/index.ts index f96b77b62e..9e809da20a 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -753,6 +753,10 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server; let loopbackServer: Server | null = null; + let managementIngressServer: Server | null = null; + + type ServerIngress = "public" | "unauthenticated-loopback" | "hub-management"; + function ingressForServer(requestServer: Server): ServerIngress { + if (requestServer === loopbackServer) return "unauthenticated-loopback"; + if (requestServer === managementIngressServer) return "hub-management"; + return "public"; + } let backgroundLifecycle: ReturnType | null = null; try { backgroundLifecycle = acquireServerBackgroundLifecycle(applyPolicy); @@ -963,22 +990,32 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server): Promise { + const ingress = ingressForServer(requestServer); // The unauthenticated loopback listener (#1102) serves a fixed allowlist and nothing // else. Rejecting here, before any handler runs, is what keeps the surface from growing // silently when a route is added below. - if (requestServer === loopbackServer && !loopbackRouteAllowed(new URL(req.url), req)) { + if (ingress === "unauthenticated-loopback" && !loopbackRouteAllowed(new URL(req.url), req)) { return withCors( formatErrorResponse(404, "not_found", `Unknown endpoint: ${req.method} ${new URL(req.url).pathname}`), req, loopbackPolicy(), ); } + // Tailscale Serve terminates only on this separately bound loopback socket. Reject before + // dispatch so no data, readiness, health, WebSocket, or unknown-static handler can run. + if (ingress === "hub-management" && !managementIngressRouteAllowed(new URL(req.url), req)) { + return withCors( + formatErrorResponse(404, "not_found", `Unknown endpoint: ${req.method} ${new URL(req.url).pathname}`), + req, + config, + ); + } // Auth and CORS decisions below read `policy`, not `config`. For the public listener the // two are the same object, so its behaviour is unchanged; for the loopback listener the // view substitutes 127.0.0.1 as the bind address, which is what routes it through the // same code path a plain loopback bind has always taken — Host-header check included. // Routing, provider selection and response bodies keep using `config`. - const policy: RequestPolicyView = requestServer === loopbackServer ? loopbackPolicy() : config; + const policy: RequestPolicyView = ingress === "unauthenticated-loopback" ? loopbackPolicy() : config; const url = new URL(req.url); markActivity(`${req.method} ${url.pathname}`); @@ -1846,7 +1883,9 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server({ + ...serveOptions, + port: managementIngressPort, + hostname: "127.0.0.1", + }); + } catch (error) { + // Preserve the management bind failure while synchronously initiating rollback of every + // listener already opened in this startup transaction. startServer must not become async. + for (const bound of [loopbackServer, server]) { + if (!bound) continue; + try { void bound.stop(true); } catch { /* report the original bind error */ } + } + throw error; + } + } } catch (error) { userCostOverlayReconciler?.stop(); backgroundLifecycle?.releaseAfterFailedStart(); @@ -2157,6 +2215,7 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server => { @@ -2168,6 +2227,9 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server loopbackListenerRef.stop(closeActiveConnections)] : []), + ...(managementIngressRef + ? [() => managementIngressRef.stop(closeActiveConnections)] + : []), async () => { userCostOverlayReconciler?.stop(); }, @@ -2202,6 +2264,12 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server Date: Fri, 28 Aug 2026 03:47:07 +0900 Subject: [PATCH 058/172] test(two-plane): align shared-base shell assertions --- gui/tests/app-sidebar-actions.test.ts | 3 +-- gui/tests/codex-stale-banner.test.ts | 2 +- gui/tests/sidebar-codex-set.test.ts | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/gui/tests/app-sidebar-actions.test.ts b/gui/tests/app-sidebar-actions.test.ts index d8f545f504..15648f6595 100644 --- a/gui/tests/app-sidebar-actions.test.ts +++ b/gui/tests/app-sidebar-actions.test.ts @@ -50,7 +50,7 @@ test("the restart action comes from the shared hook, not an inline duplicate", ( // The models page reuses the same controller; a second inline implementation // would drift on the four-branch message mapping. The hook now also takes an // options object, so match the call rather than one exact argument list. - expect(src).toContain("useCodexRestart(API_BASE"); + expect(src).toContain("useCodexRestart(sharedBase"); expect(src).not.toContain("requestCodexRestart("); }); @@ -117,4 +117,3 @@ test("every restart string exists in the English source with its slots intact", expect(en["dash.codexRestartPartial"]).toContain("{count}"); expect(en["dash.codexRestartFailed"]).toContain("{status}"); }); - diff --git a/gui/tests/codex-stale-banner.test.ts b/gui/tests/codex-stale-banner.test.ts index 04e9bbfb39..ba2c3f6506 100644 --- a/gui/tests/codex-stale-banner.test.ts +++ b/gui/tests/codex-stale-banner.test.ts @@ -153,7 +153,7 @@ describe("cross-surface invalidation", () => { test("the epoch is the only cross-surface coupling, not a shared controller", () => { // Two controllers is deliberate: the backend is single-flight, so what was // missing is invalidation rather than mutual exclusion. - expect(APP_SRC).toContain("useCodexRestart(API_BASE, {"); + expect(APP_SRC).toContain("useCodexRestart(sharedBase, {"); expect(MODELS).toContain("useCodexRestart(apiBase, {"); }); }); diff --git a/gui/tests/sidebar-codex-set.test.ts b/gui/tests/sidebar-codex-set.test.ts index 9c726942d2..d0de10c569 100644 --- a/gui/tests/sidebar-codex-set.test.ts +++ b/gui/tests/sidebar-codex-set.test.ts @@ -26,7 +26,7 @@ test("Codex Set is always present in the sidebar, never filtered by view mode", // It stays in the nav table and remains routable for deep links. expect(src).toContain('{ id: "codex-set", tkey: "nav.codexSet", Icon: IconKey }'); - expect(src).toContain('{page === "codex-set" && }'); + expect(src).toContain('{page === "codex-set" && }'); }); test("the shipped #codex-auth bookmark still resolves", async () => { From 6999656e2c170d5cf5e2b559799d4340f66f1726 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 04:22:30 +0900 Subject: [PATCH 059/172] test(deploy): cover hub management ingress boundaries --- tests/loopback-listener-admission.test.ts | 49 ++++++++ tests/loopback-listener-integration.test.ts | 117 ++++++++++++++++++++ tests/oauth-manual-code.test.ts | 38 +++++++ tests/server-management-auth.test.ts | 96 ++++++++++++++++ tests/service.test.ts | 24 ++++ 5 files changed, 324 insertions(+) diff --git a/tests/loopback-listener-admission.test.ts b/tests/loopback-listener-admission.test.ts index f9858b5b08..ca84d78bbc 100644 --- a/tests/loopback-listener-admission.test.ts +++ b/tests/loopback-listener-admission.test.ts @@ -169,6 +169,55 @@ describe("loopback listener configuration", () => { }); }); +describe("hub management ingress configuration", () => { + const candidate = (overrides: Record = {}) => ({ + port: 10100, + runtimeRole: "hub", + hub: { managementIngress: { enabled: true, port: 10101 } }, + providers: { openai: { adapter: "openai", baseUrl: "https://chatgpt.com/backend-api/codex" } }, + defaultProvider: "openai", + ...overrides, + }); + + test("missing and disabled ingress preserve the no-listener default", () => { + const missing = validateConfigCandidate(candidate({ hub: {} })); + expect(missing.ok).toBe(true); + if (missing.ok) expect(missing.config.hub?.managementIngress).toBeUndefined(); + + const disabled = validateConfigCandidate(candidate({ hub: { managementIngress: { enabled: false } } })); + expect(disabled.ok).toBe(true); + if (disabled.ok) expect(disabled.config.hub?.managementIngress).toEqual({ enabled: false }); + }); + + test("enabled ingress requires the hub role", () => { + for (const runtimeRole of [undefined, "standalone", "client"] as const) { + const result = validateConfigCandidate(candidate({ runtimeRole })); + expect(result.ok).toBe(false); + if (!result.ok) expect(result.error).toContain("requires runtimeRole hub"); + } + }); + + test("enabled ingress rejects public and unauthenticated-loopback port collisions", () => { + const publicCollision = validateConfigCandidate(candidate({ + hub: { managementIngress: { enabled: true, port: 10100 } }, + })); + expect(publicCollision.ok).toBe(false); + if (!publicCollision.ok) expect(publicCollision.error).toContain("must differ from the proxy port"); + + const loopbackCollision = validateConfigCandidate(candidate({ + unauthenticatedLoopbackListener: { enabled: true, port: 10101 }, + })); + expect(loopbackCollision.ok).toBe(false); + if (!loopbackCollision.ok) expect(loopbackCollision.error).toContain("unauthenticatedLoopbackListener.port"); + }); + + test("a valid hub ingress survives strict parsing", () => { + const result = validateConfigCandidate(candidate()); + expect(result.ok).toBe(true); + if (result.ok) expect(result.config.hub?.managementIngress).toEqual({ enabled: true, port: 10101 }); + }); +}); + describe("injected Codex provider block", () => { test("a wildcard bind alone still emits the env auth header", () => { expect(shouldInjectApiAuthHeader({ hostname: "0.0.0.0" })).toBe(true); diff --git a/tests/loopback-listener-integration.test.ts b/tests/loopback-listener-integration.test.ts index 7ed51757b2..37380029cf 100644 --- a/tests/loopback-listener-integration.test.ts +++ b/tests/loopback-listener-integration.test.ts @@ -25,6 +25,7 @@ import type { OcxConfig } from "../src/types"; import { SERVER_BUDGET_MS } from "./helpers/test-budget"; const previousApiToken = process.env.OPENCODEX_API_AUTH_TOKEN; +const previousAdminToken = process.env.OPENCODEX_ADMIN_AUTH_TOKEN; const previousHome = process.env.OPENCODEX_HOME; let testDir = ""; @@ -46,6 +47,18 @@ function baseConfig(loopbackPort: number | null): OcxConfig { } as unknown as OcxConfig; } +function hubIngressConfig(managementPort: number, loopbackPort: number | null = null): OcxConfig { + return { + ...baseConfig(loopbackPort), + runtimeRole: "hub", + hub: { + managementPublicOrigin: "https://hub.example.test", + managementIngress: { enabled: true, port: managementPort }, + }, + remoteGui: { allowedTailscaleUsers: ["alice@example.test"] }, + }; +} + /** A free port to hand the loopback listener, chosen the same way production would not reuse. */ async function freePort(): Promise { return await findAvailablePort(0, "127.0.0.1"); @@ -83,17 +96,117 @@ beforeEach(() => { testDir = mkdtempSync(join(tmpdir(), "ocx-loopback-listener-")); process.env.OPENCODEX_HOME = testDir; process.env.OPENCODEX_API_AUTH_TOKEN = "public-secret"; + process.env.OPENCODEX_ADMIN_AUTH_TOKEN = "admin-secret"; }); afterEach(() => { if (previousApiToken === undefined) delete process.env.OPENCODEX_API_AUTH_TOKEN; else process.env.OPENCODEX_API_AUTH_TOKEN = previousApiToken; + if (previousAdminToken === undefined) delete process.env.OPENCODEX_ADMIN_AUTH_TOKEN; + else process.env.OPENCODEX_ADMIN_AUTH_TOKEN = previousAdminToken; if (previousHome === undefined) delete process.env.OPENCODEX_HOME; else process.env.OPENCODEX_HOME = previousHome; if (testDir && existsSync(testDir)) rmSync(testDir, { recursive: true, force: true }); testDir = ""; }); +describe("hub management ingress", () => { + test("binds only loopback and serves GUI plus authenticated management routes", async () => { + const managementPort = await freePort(); + const publicPort = await findAvailablePort(0, "127.0.0.1", { reservedPort: managementPort }); + saveConfig(hubIngressConfig(managementPort)); + const server = startServer(publicPort); + try { + const page = await fetch(`http://127.0.0.1:${managementPort}/`, { + headers: { Host: "hub.example.test", "Tailscale-User-Login": "alice@example.test" }, + }); + expect(page.status).not.toBe(404); + + const management = await fetch(`http://127.0.0.1:${managementPort}/api/config`, { + headers: { + Host: "hub.example.test", + Origin: "https://hub.example.test", + "x-opencodex-api-key": "admin-secret", + }, + }); + expect(management.status).toBe(200); + + const address = firstNonLoopbackIPv4(); + if (address) { + const refused = await new Promise(resolve => { + const socket = connect({ host: address, port: managementPort }); + const settle = (value: boolean) => { socket.destroy(); resolve(value); }; + socket.setTimeout(2_000); + socket.once("connect", () => settle(false)); + socket.once("error", () => settle(true)); + socket.once("timeout", () => settle(true)); + }); + expect(refused).toBe(true); + } + } finally { + await server.stop(true); + } + }, SERVER_BUDGET_MS); + + test("default-denies every data, health, readiness, WebSocket, and unknown-static route", async () => { + const managementPort = await freePort(); + const publicPort = await findAvailablePort(0, "127.0.0.1", { reservedPort: managementPort }); + saveConfig(hubIngressConfig(managementPort)); + const server = startServer(publicPort); + const base = `http://127.0.0.1:${managementPort}`; + try { + const denied: Array<{ path: string; headers?: Record }> = [ + { path: "/v1/catalog" }, + { path: "/healthz" }, + { path: "/readyz" }, + { path: "/v1/responses", headers: { Connection: "Upgrade", Upgrade: "websocket" } }, + { path: "/missing-static.js" }, + ]; + for (const entry of denied) { + const response = await fetch(`${base}${entry.path}`, { headers: entry.headers }); + expect({ path: entry.path, status: response.status }).toEqual({ path: entry.path, status: 404 }); + expect(response.headers.get("content-type")).toContain("application/json"); + } + } finally { + await server.stop(true); + } + }); + + test("a failed management bind rolls back both earlier listeners", async () => { + const managementPort = await freePort(); + const loopbackPort = await findAvailablePort(0, "127.0.0.1", { reservedPort: managementPort }); + const publicPort = await findAvailablePort(0, "127.0.0.1", { reservedPort: loopbackPort }); + const squatter = Bun.serve({ + port: managementPort, + hostname: "127.0.0.1", + fetch: () => new Response("occupied"), + }); + saveConfig(hubIngressConfig(managementPort, loopbackPort)); + try { + expect(() => startServer(publicPort)).toThrow(); + for (const port of [publicPort, loopbackPort]) { + const rebound = Bun.serve({ port, hostname: "127.0.0.1", fetch: () => new Response("ok") }); + await rebound.stop(true); + } + } finally { + await squatter.stop(true); + } + }); + + test("normal shutdown closes public, data-loopback, and management listeners", async () => { + const managementPort = await freePort(); + const loopbackPort = await findAvailablePort(0, "127.0.0.1", { reservedPort: managementPort }); + const publicPort = await findAvailablePort(0, "127.0.0.1", { reservedPort: loopbackPort }); + saveConfig(hubIngressConfig(managementPort, loopbackPort)); + const server = startServer(publicPort); + await server.stop(true); + for (const port of [publicPort, loopbackPort, managementPort]) { + const rebound = Bun.serve({ port, hostname: "127.0.0.1", fetch: () => new Response("ok") }); + await rebound.stop(true); + } + }); +}); + describe("unauthenticated loopback listener", () => { test("is absent unless configured, and the public listener still demands a key", async () => { saveConfig(baseConfig(null)); @@ -535,6 +648,10 @@ describe("seams the runtime cannot defend", () => { // holds everywhere. expect(serverSource).toMatch(/port: loopbackListenerPort,\s*\n\s*hostname: "127\.0\.0\.1",/); }); + + test("the hub management ingress binds 127.0.0.1 explicitly", () => { + expect(serverSource).toMatch(/port: managementIngressPort,\s*\n\s*hostname: "127\.0\.0\.1",/); + }); }); describe("public port selection avoids the loopback port", () => { diff --git a/tests/oauth-manual-code.test.ts b/tests/oauth-manual-code.test.ts index a6a12ba889..6e03dd0d15 100644 --- a/tests/oauth-manual-code.test.ts +++ b/tests/oauth-manual-code.test.ts @@ -13,6 +13,7 @@ import { import { parseCallbackInput } from "../src/oauth/callback-server"; import { saveConfig } from "../src/config"; import { startServer } from "../src/server"; +import { findAvailablePort } from "../src/server/ports"; import type { OcxConfig } from "../src/types"; const TEST_DIR = join(import.meta.dir, ".tmp-oauth-manual-code-test"); @@ -324,4 +325,41 @@ describe("OAuth manual login code fallback", () => { await server.stop(true); } }); + + test("headless manual-code route is available through hub management ingress", async () => { + const managementPort = await findAvailablePort(0, "127.0.0.1"); + const publicPort = await findAvailablePort(0, "127.0.0.1", { reservedPort: managementPort }); + const previousDataToken = process.env.OPENCODEX_API_AUTH_TOKEN; + process.env.OPENCODEX_API_AUTH_TOKEN = "hub-data-secret"; + saveConfig({ + port: 0, + hostname: "0.0.0.0", + runtimeRole: "hub", + hub: { + managementPublicOrigin: "https://hub.example.test", + managementIngress: { enabled: true, port: managementPort }, + }, + oauthOpenBrowser: false, + defaultProvider: "xai", + providers: { xai: { adapter: "openai-chat", baseUrl: "https://api.x.ai/v1", authMode: "oauth" } }, + } as OcxConfig); + const server = startServer(publicPort); + try { + const response = await fetch(`http://127.0.0.1:${managementPort}/api/oauth/login/code`, { + method: "POST", + headers: { + Host: "hub.example.test", + Origin: "https://hub.example.test", + "Content-Type": "application/json", + }, + body: JSON.stringify({ provider: "xai", input: "some-code" }), + }); + expect(response.status).toBe(409); + expect(((await response.json()) as { error?: string }).error).toContain("no login in progress"); + } finally { + await server.stop(true); + if (previousDataToken === undefined) delete process.env.OPENCODEX_API_AUTH_TOKEN; + else process.env.OPENCODEX_API_AUTH_TOKEN = previousDataToken; + } + }); }); diff --git a/tests/server-management-auth.test.ts b/tests/server-management-auth.test.ts index d7713e0cca..091aea7092 100644 --- a/tests/server-management-auth.test.ts +++ b/tests/server-management-auth.test.ts @@ -5,6 +5,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { getConfigPath, saveConfig } from "../src/config"; import { startServer } from "../src/server"; +import { findAvailablePort } from "../src/server/ports"; import type { OcxConfig } from "../src/types"; import { serveGuiFile, serveSessionBootstrap } from "../src/server/gui-static"; import { isProxyAdmissionSecret } from "../src/server/auth-cors"; @@ -975,6 +976,57 @@ describe("management and data-plane credential separation", () => { }), httpConfig, state, { trustedTailscaleIngress: true, now })).toBeNull(); }); + test("the live listener trusts Tailscale identity only on hub management ingress", async () => { + const managementPort = await findAvailablePort(0, "127.0.0.1"); + const publicPort = await findAvailablePort(0, "127.0.0.1", { reservedPort: managementPort }); + const config = hubConfig(); + config.hub = { + ...config.hub, + managementIngress: { enabled: true, port: managementPort }, + }; + saveConfig(config); + const state = initializeManagementAuthState(config); + if (!state.available) throw new Error("expected management auth state"); + const server = startServer(publicPort, { managementAuthState: state }); + const headers = { Host: "hub.example.test", "Tailscale-User-Login": "alice@example.test" }; + try { + const spoofedPublic = await fetch(new URL("/opencodex-session", server.url), { headers }); + expect(spoofedPublic.status).toBe(401); + + const wrongUser = await fetch(`http://127.0.0.1:${managementPort}/opencodex-session`, { + headers: { ...headers, "Tailscale-User-Login": "mallory@example.test" }, + }); + expect(wrongUser.status).toBe(401); + + const issued = await fetch(`http://127.0.0.1:${managementPort}/opencodex-session`, { headers }); + expect(issued.status).toBe(200); + const html = await issued.text(); + const token = /name="opencodex-session-token" content="([^"]+)"/.exec(html)?.[1]; + expect(token).toBeDefined(); + const management = await fetch(`http://127.0.0.1:${managementPort}/api/config`, { + headers: { + Host: "hub.example.test", + Origin: "https://hub.example.test", + "x-opencodex-api-key": token!, + "x-opencodex-gui-origin": "https://hub.example.test", + }, + }); + expect(management.status).toBe(200); + + const adminConsent = await fetch(`http://127.0.0.1:${managementPort}/api/github/star`, { + method: "POST", + headers: { + Host: "hub.example.test", + Origin: "https://hub.example.test", + "x-opencodex-api-key": "admin-secret", + }, + }); + expect(adminConsent.status).toBe(403); + } finally { + await server.stop(true); + } + }); + test("pairing grants are digest-only, origin-bound, single-use, and never accept alternate credentials", () => { const config = hubConfig(); const state = initializeManagementAuthState(config); @@ -1022,6 +1074,50 @@ describe("management and data-plane credential separation", () => { )).toBeNull(); }); + test("the management ingress preserves the one-use pairing exchange contract", async () => { + const managementPort = await findAvailablePort(0, "127.0.0.1"); + const publicPort = await findAvailablePort(0, "127.0.0.1", { reservedPort: managementPort }); + const config = hubConfig(); + config.hub = { ...config.hub, managementIngress: { enabled: true, port: managementPort } }; + saveConfig(config); + const state = initializeManagementAuthState(config); + if (!state.available) throw new Error("expected management auth state"); + const created = createGuiPairingGrant("https://dashboard.example.test", config, state); + const server = startServer(publicPort, { managementAuthState: state }); + const url = `http://127.0.0.1:${managementPort}/opencodex-session`; + const headers = { + Host: "hub.example.test", + Origin: "https://dashboard.example.test", + "content-type": "application/json", + }; + try { + const adminAttempt = await fetch(url, { + method: "POST", + headers: { ...headers, "x-opencodex-api-key": "admin-secret" }, + body: JSON.stringify({ grant: created.grant }), + }); + expect(adminAttempt.status).toBe(401); + expect(state.pairingGrants.size).toBe(1); + + const exchanged = await fetch(url, { + method: "POST", + headers, + body: JSON.stringify({ grant: created.grant }), + }); + expect(exchanged.status).toBe(200); + expect(state.pairingGrants.size).toBe(0); + + const replay = await fetch(url, { + method: "POST", + headers, + body: JSON.stringify({ grant: created.grant }), + }); + expect(replay.status).toBe(401); + } finally { + await server.stop(true); + } + }); + test("non-loopback plaintext HTTP cannot carry a pairing grant, and no opt-in re-opens it", () => { // An earlier revision let this exchange succeed when `remoteGui.allowInsecureHttp` was // true, and this test asserted exactly that. The flag is retired: a reusable grant on diff --git a/tests/service.test.ts b/tests/service.test.ts index f9b4e55cd5..48788a60f3 100644 --- a/tests/service.test.ts +++ b/tests/service.test.ts @@ -410,6 +410,30 @@ describe("service install auth preflight", () => { expect(() => assertServiceAuthEnvironment()).not.toThrow(); }); + test("hub-mode launchd and systemd installs reuse the protected data-token file", () => { + if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); + mkdirSync(TEST_DIR, { recursive: true }); + process.env.OPENCODEX_HOME = TEST_DIR; + process.env.OPENCODEX_API_AUTH_TOKEN = "phase5-data-secret"; + saveConfig({ + port: 10100, + hostname: "0.0.0.0", + runtimeRole: "hub", + hub: { + managementPublicOrigin: "https://hub.example.test", + managementIngress: { enabled: true, port: 10101 }, + }, + providers: { openai: { adapter: "openai-chat", baseUrl: "https://api.example.test/v1" } }, + defaultProvider: "openai", + } as OcxConfig); + + expect(() => assertServiceAuthEnvironment()).not.toThrow(); + for (const definition of [buildUnit(), buildPlist()]) { + expectTextToContainPath(definition, serviceApiTokenFilePath()); + expect(definition).not.toContain("phase5-data-secret"); + } + }); + test("rejects restore operations from a different CODEX_HOME than service install", () => { if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); mkdirSync(TEST_DIR, { recursive: true }); From 4f27ee817e531708d075af4821c796100b988369 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 03:51:24 +0900 Subject: [PATCH 060/172] feat(two-plane): harden relay and offline target states --- gui/src/App.tsx | 16 +++++++++++++--- gui/src/api-targets.ts | 12 ++++++++++++ gui/src/api.ts | 16 ++++++++++------ gui/src/connect-pairing.ts | 1 + gui/src/i18n/de.ts | 1 + gui/src/i18n/en.ts | 1 + gui/src/i18n/fr.ts | 1 + gui/src/i18n/ja.ts | 1 + gui/src/i18n/ko.ts | 1 + gui/src/i18n/ru.ts | 1 + gui/src/i18n/tr.ts | 1 + gui/src/i18n/zh-TW.ts | 1 + gui/src/i18n/zh.ts | 1 + gui/src/pages/Integrations.tsx | 2 +- gui/src/pages/Startup.tsx | 2 +- gui/tests/connect-pairing.test.ts | 1 + src/client/hub-relay.ts | 4 +++- src/client/machine-auth.ts | 2 +- src/client/machine-listener.ts | 22 ++++++++++++++-------- 19 files changed, 66 insertions(+), 21 deletions(-) diff --git a/gui/src/App.tsx b/gui/src/App.tsx index e3d3124073..014aec7e46 100644 --- a/gui/src/App.tsx +++ b/gui/src/App.tsx @@ -15,7 +15,7 @@ import { SidebarGithubRow } from "./components/sidebar-github-row"; import { IconGrid, IconServer, IconBoxes, IconBot, IconList, IconActivity, IconHardDrive, IconKey, IconMenu, IconSun, IconMoon, IconMonitor, IconGlobe, IconPower, IconX, IconRefresh} from "./icons"; import { useI18n, useT, LOCALES, localeDisplayName, type Locale, type TKey } from "./i18n/shared"; import { Select } from "./ui"; -import { configureApiTargets, hasApiSession, installApiAuthFetch } from "./api"; +import { configureApiTargets, hasApiSession, installApiAuthFetch, installApiSessionFromHtml } from "./api"; import { apiBaseForPlane, discoverApiTargets, standaloneApiTargets, type ApiTargets } from "./api-targets"; import { ConnectPairingForm } from "./connect-pairing"; import { type Page } from "./app-routing"; @@ -111,9 +111,19 @@ export default function App() { useEffect(() => { const controller = new AbortController(); - void discoverApiTargets(API_BASE, controller.signal).then(next => { + void discoverApiTargets(API_BASE, controller.signal).then(async next => { configureApiTargets(next); setTargets(next); + if (next.connected && !hasApiSession("shared")) { + try { + const response = await fetch(next.shared.bootstrapPath, { + cache: "no-store", + signal: AbortSignal.any([controller.signal, AbortSignal.timeout(5_000)]), + }); + if (response.ok) installApiSessionFromHtml("shared", await response.text()); + } catch { /* pairing form remains available */ } + } + if (controller.signal.aborted) return; setSharedSessionReady(hasApiSession("shared")); setTargetError(false); setTargetsSettled(true); @@ -205,7 +215,7 @@ export default function App() { }); const handleStop = async () => { - if (!confirm(t("dash.stopConfirm"))) return; + if (!confirm(t(targets.connected ? "connection.disconnectConfirm" : "dash.stopConfirm"))) return; setStopping(true); const outcome = await requestProxyStop(machineBase, { formatFailure: status => t("dash.stopFailed", { status: String(status) }), diff --git a/gui/src/api-targets.ts b/gui/src/api-targets.ts index f71fd63964..000a32a058 100644 --- a/gui/src/api-targets.ts +++ b/gui/src/api-targets.ts @@ -92,6 +92,18 @@ export function targetsFromMachineStatus(initialBase: string, status: MachineSta if (!machineOrigin || machineOrigin !== initial.machine.serverOrigin || !sharedOrigin) { throw new TypeError("machine status target origins are invalid"); } + let advertisedShared: URL; + try { advertisedShared = new URL(status.sharedBase); } catch { throw new TypeError("machine status shared target is invalid"); } + if (advertisedShared.username || advertisedShared.password || advertisedShared.search || advertisedShared.hash) { + throw new TypeError("machine status shared target is invalid"); + } + if (status.managementTransport === "direct") { + if (advertisedShared.origin !== sharedOrigin || advertisedShared.pathname !== "/") { + throw new TypeError("machine status direct target is inconsistent"); + } + } else if (advertisedShared.origin !== machineOrigin || advertisedShared.pathname !== "/api/machine/hub-relay") { + throw new TypeError("machine status relay target is inconsistent"); + } const machine = target("machine", trimBase(initialBase), machineOrigin, "same-origin"); const shared = status.managementTransport === "relay" ? target("shared", `${trimBase(initialBase)}/api/machine/hub-relay`, sharedOrigin, "relay") diff --git a/gui/src/api.ts b/gui/src/api.ts index 28ff9d0bcd..e6893d4c2f 100644 --- a/gui/src/api.ts +++ b/gui/src/api.ts @@ -146,6 +146,12 @@ function targetMatchesUrl(target: ApiTarget, url: URL): boolean { return prefix === "" || url.pathname === prefix || url.pathname.startsWith(`${prefix}/`); } +function relativeTargetPath(target: ApiTarget, url: URL): string | null { + if (!targetMatchesUrl(target, url)) return null; + const base = targetAbsoluteBase(target).pathname.replace(/\/$/, ""); + return url.pathname.slice(base.length) || "/"; +} + function classify(input: RequestInfo | URL): { plane: ApiPlane; bootstrap: boolean } | null { let url: URL; try { @@ -154,12 +160,10 @@ function classify(input: RequestInfo | URL): { plane: ApiPlane; bootstrap: boole const targets = ensureTargets(); if (url.href === new URL(targets.shared.bootstrapPath, window.location.href).href) return { plane: "shared", bootstrap: true }; if (targets.shared.transport === "relay" && targetMatchesUrl(targets.shared, url)) return { plane: "shared", bootstrap: false }; - if (url.pathname.startsWith("/api/machine/") && targetMatchesUrl(targets.machine, url)) return { plane: "machine", bootstrap: false }; - if (targetMatchesUrl(targets.shared, url)) { - const base = targetAbsoluteBase(targets.shared).pathname.replace(/\/$/, ""); - const relative = url.pathname.slice(base.length) || "/"; - if (relative.startsWith("/api/")) return { plane: "shared", bootstrap: false }; - } + const machinePath = relativeTargetPath(targets.machine, url); + if (machinePath?.startsWith("/api/machine/")) return { plane: "machine", bootstrap: false }; + const sharedPath = relativeTargetPath(targets.shared, url); + if (sharedPath?.startsWith("/api/")) return { plane: "shared", bootstrap: false }; if (url.href === new URL(targets.machine.bootstrapPath, window.location.href).href) return { plane: "machine", bootstrap: true }; return null; } diff --git a/gui/src/connect-pairing.ts b/gui/src/connect-pairing.ts index 488dc79e60..6c1aa5520a 100644 --- a/gui/src/connect-pairing.ts +++ b/gui/src/connect-pairing.ts @@ -1,3 +1,4 @@ +/* eslint-disable react-refresh/only-export-components -- pairing transport and its form share one session-install boundary */ import { createElement, useState, type ChangeEvent, type FormEvent } from "react"; import { installApiSessionFromHtml } from "./api"; import type { ApiTarget } from "./api-targets"; diff --git a/gui/src/i18n/de.ts b/gui/src/i18n/de.ts index 9bf99a1e18..18db779899 100644 --- a/gui/src/i18n/de.ts +++ b/gui/src/i18n/de.ts @@ -2324,6 +2324,7 @@ export const de: Record = { "connection.discovering": "Discovering local and shared targets…", "connection.machineUnavailable": "The local machine plane is unavailable. Shared requests were not redirected locally.", "connection.disconnect": "Disconnect from hub", + "connection.disconnectConfirm": "Disconnect this machine from the hub and restart it in standalone mode?", "connection.pairing.title": "Connect this dashboard to the hub", "connection.pairing.body": "Paste the one-time pairing code created on the hub.", "connection.pairing.relayWarning": "This code is exchanged through the fixed hub relay. The relay cannot be redirected to another host.", diff --git a/gui/src/i18n/en.ts b/gui/src/i18n/en.ts index 515e4759e5..dba1942dfd 100644 --- a/gui/src/i18n/en.ts +++ b/gui/src/i18n/en.ts @@ -2358,6 +2358,7 @@ export const en = { "connection.discovering": "Discovering local and shared targets…", "connection.machineUnavailable": "The local machine plane is unavailable. Shared requests were not redirected locally.", "connection.disconnect": "Disconnect from hub", + "connection.disconnectConfirm": "Disconnect this machine from the hub and restart it in standalone mode?", "connection.pairing.title": "Connect this dashboard to the hub", "connection.pairing.body": "Paste the one-time pairing code created on the hub.", "connection.pairing.relayWarning": "This code is exchanged through the fixed hub relay. The relay cannot be redirected to another host.", diff --git a/gui/src/i18n/fr.ts b/gui/src/i18n/fr.ts index 512db468d9..a3a2a05ed0 100644 --- a/gui/src/i18n/fr.ts +++ b/gui/src/i18n/fr.ts @@ -2311,6 +2311,7 @@ export const fr: Record = { "connection.discovering": "Discovering local and shared targets…", "connection.machineUnavailable": "The local machine plane is unavailable. Shared requests were not redirected locally.", "connection.disconnect": "Disconnect from hub", + "connection.disconnectConfirm": "Disconnect this machine from the hub and restart it in standalone mode?", "connection.pairing.title": "Connect this dashboard to the hub", "connection.pairing.body": "Paste the one-time pairing code created on the hub.", "connection.pairing.relayWarning": "This code is exchanged through the fixed hub relay. The relay cannot be redirected to another host.", diff --git a/gui/src/i18n/ja.ts b/gui/src/i18n/ja.ts index 94d4218a55..9b5de33c86 100644 --- a/gui/src/i18n/ja.ts +++ b/gui/src/i18n/ja.ts @@ -2345,6 +2345,7 @@ export const ja: Record = { "connection.discovering": "Discovering local and shared targets…", "connection.machineUnavailable": "The local machine plane is unavailable. Shared requests were not redirected locally.", "connection.disconnect": "Disconnect from hub", + "connection.disconnectConfirm": "Disconnect this machine from the hub and restart it in standalone mode?", "connection.pairing.title": "Connect this dashboard to the hub", "connection.pairing.body": "Paste the one-time pairing code created on the hub.", "connection.pairing.relayWarning": "This code is exchanged through the fixed hub relay. The relay cannot be redirected to another host.", diff --git a/gui/src/i18n/ko.ts b/gui/src/i18n/ko.ts index 1585516ac5..9fc2969c3f 100644 --- a/gui/src/i18n/ko.ts +++ b/gui/src/i18n/ko.ts @@ -2346,6 +2346,7 @@ export const ko: Record = { "connection.discovering": "로컬 및 공유 대상을 확인하는 중…", "connection.machineUnavailable": "로컬 머신 연결을 사용할 수 없습니다. 공유 요청을 로컬로 우회하지 않았습니다.", "connection.disconnect": "허브 연결 해제", + "connection.disconnectConfirm": "이 머신의 허브 연결을 해제하고 독립 실행 모드로 다시 시작할까요?", "connection.pairing.title": "이 대시보드를 허브에 연결", "connection.pairing.body": "허브에서 만든 일회용 페어링 코드를 붙여 넣으세요.", "connection.pairing.relayWarning": "이 코드는 고정 허브 릴레이로 교환됩니다. 릴레이 목적지는 다른 호스트로 바꿀 수 없습니다.", diff --git a/gui/src/i18n/ru.ts b/gui/src/i18n/ru.ts index 8e09703701..f5aa05bb69 100644 --- a/gui/src/i18n/ru.ts +++ b/gui/src/i18n/ru.ts @@ -2347,6 +2347,7 @@ export const ru: Record = { "connection.discovering": "Discovering local and shared targets…", "connection.machineUnavailable": "The local machine plane is unavailable. Shared requests were not redirected locally.", "connection.disconnect": "Disconnect from hub", + "connection.disconnectConfirm": "Disconnect this machine from the hub and restart it in standalone mode?", "connection.pairing.title": "Connect this dashboard to the hub", "connection.pairing.body": "Paste the one-time pairing code created on the hub.", "connection.pairing.relayWarning": "This code is exchanged through the fixed hub relay. The relay cannot be redirected to another host.", diff --git a/gui/src/i18n/tr.ts b/gui/src/i18n/tr.ts index 92aec70d7c..82b0a6c575 100644 --- a/gui/src/i18n/tr.ts +++ b/gui/src/i18n/tr.ts @@ -2347,6 +2347,7 @@ export const tr: Record = { "connection.discovering": "Discovering local and shared targets…", "connection.machineUnavailable": "The local machine plane is unavailable. Shared requests were not redirected locally.", "connection.disconnect": "Disconnect from hub", + "connection.disconnectConfirm": "Disconnect this machine from the hub and restart it in standalone mode?", "connection.pairing.title": "Connect this dashboard to the hub", "connection.pairing.body": "Paste the one-time pairing code created on the hub.", "connection.pairing.relayWarning": "This code is exchanged through the fixed hub relay. The relay cannot be redirected to another host.", diff --git a/gui/src/i18n/zh-TW.ts b/gui/src/i18n/zh-TW.ts index cb647d72af..e478942b46 100644 --- a/gui/src/i18n/zh-TW.ts +++ b/gui/src/i18n/zh-TW.ts @@ -2309,6 +2309,7 @@ export const zhTW: Record = { "connection.discovering": "Discovering local and shared targets…", "connection.machineUnavailable": "The local machine plane is unavailable. Shared requests were not redirected locally.", "connection.disconnect": "Disconnect from hub", + "connection.disconnectConfirm": "Disconnect this machine from the hub and restart it in standalone mode?", "connection.pairing.title": "Connect this dashboard to the hub", "connection.pairing.body": "Paste the one-time pairing code created on the hub.", "connection.pairing.relayWarning": "This code is exchanged through the fixed hub relay. The relay cannot be redirected to another host.", diff --git a/gui/src/i18n/zh.ts b/gui/src/i18n/zh.ts index f25ea6dd68..2fe77941a1 100644 --- a/gui/src/i18n/zh.ts +++ b/gui/src/i18n/zh.ts @@ -2345,6 +2345,7 @@ export const zh: Record = { "connection.discovering": "Discovering local and shared targets…", "connection.machineUnavailable": "The local machine plane is unavailable. Shared requests were not redirected locally.", "connection.disconnect": "Disconnect from hub", + "connection.disconnectConfirm": "Disconnect this machine from the hub and restart it in standalone mode?", "connection.pairing.title": "Connect this dashboard to the hub", "connection.pairing.body": "Paste the one-time pairing code created on the hub.", "connection.pairing.relayWarning": "This code is exchanged through the fixed hub relay. The relay cannot be redirected to another host.", diff --git a/gui/src/pages/Integrations.tsx b/gui/src/pages/Integrations.tsx index 7f06312a77..4e3ba89426 100644 --- a/gui/src/pages/Integrations.tsx +++ b/gui/src/pages/Integrations.tsx @@ -56,7 +56,7 @@ export default function Integrations({ apiBase, machineApiBase = apiBase, connec if (tabRefs.current === null) tabRefs.current = new Map(); useEffect(() => { - if (!connected) { setMachineClients([]); return; } + if (!connected) return; const controller = new AbortController(); void fetch(`${machineApiBase}/api/machine/clients`, { signal: controller.signal }) .then(response => response.ok ? response.json() : null) diff --git a/gui/src/pages/Startup.tsx b/gui/src/pages/Startup.tsx index aa11a994ef..c9e5ef2aaf 100644 --- a/gui/src/pages/Startup.tsx +++ b/gui/src/pages/Startup.tsx @@ -93,7 +93,7 @@ export default function Startup({ apiBase, machineApiBase = apiBase, connected = const [machineBusy, setMachineBusy] = useState(false); useEffect(() => { - if (!connected) { setMachineShim(null); return; } + if (!connected) return; const controller = new AbortController(); void fetch(`${machineApiBase}/api/machine/shim`, { signal: controller.signal }) .then(response => response.ok ? response.json() : null) diff --git a/gui/tests/connect-pairing.test.ts b/gui/tests/connect-pairing.test.ts index 4d373cfc7a..af263228b4 100644 --- a/gui/tests/connect-pairing.test.ts +++ b/gui/tests/connect-pairing.test.ts @@ -62,6 +62,7 @@ test("App mounts the relay pairing form and installs only the returned shared se document.body.append(container); const { LanguageProvider } = await import("../src/i18n/provider"); const { default: App } = await import("../src/App"); + Object.defineProperty(globalThis, "fetch", { configurable: true, value: win.fetch }); const { createRoot } = await import("react-dom/client"); const root = createRoot(container); try { diff --git a/src/client/hub-relay.ts b/src/client/hub-relay.ts index e4cde4dbb0..57d6da4cc1 100644 --- a/src/client/hub-relay.ts +++ b/src/client/hub-relay.ts @@ -145,7 +145,9 @@ export async function relayHubManagementRequest( const headers = filteredHeaders(stripped, REQUEST_HEADERS); if (!headersWithinLimit(headers)) return relayError(431, "hub relay request headers too large"); const browserOrigin = canonicalOrigin(target.browserOrigin); - if (!browserOrigin || headers.get("origin") !== browserOrigin) { + const mutation = method !== "GET" && method !== "HEAD"; + const suppliedOrigin = headers.get("origin"); + if (!browserOrigin || (mutation ? suppliedOrigin !== browserOrigin : suppliedOrigin !== null && suppliedOrigin !== browserOrigin)) { return relayError(403, "hub relay browser origin refused"); } diff --git a/src/client/machine-auth.ts b/src/client/machine-auth.ts index 2b4dd3d6db..f2aad27499 100644 --- a/src/client/machine-auth.ts +++ b/src/client/machine-auth.ts @@ -31,7 +31,7 @@ function machinePrincipalRequest(req: Request): Request { headers.set("Origin", browserOrigin); } if (csrf) headers.set("x-opencodex-csrf-token", csrf); - return new Request(req, { headers }); + return new Request(req.url, { method: req.method, headers, signal: req.signal }); } export function requireMachineAuth( diff --git a/src/client/machine-listener.ts b/src/client/machine-listener.ts index da54d0d70f..476c2e70a4 100644 --- a/src/client/machine-listener.ts +++ b/src/client/machine-listener.ts @@ -2,7 +2,7 @@ import { readFileSync } from "node:fs"; import type { Server } from "bun"; import { loadConfig } from "../config"; import { browserSecurityHeaders } from "../server/auth-cors"; -import { serveGuiFile, serveSessionBootstrap, rootFallbackPayload } from "../server/gui-static"; +import { serveGuiFile, serveSessionBootstrap } from "../server/gui-static"; import { initializeManagementAuthState, issueGuiSession, @@ -14,7 +14,7 @@ import type { OcxClientConnectionConfig, OcxConfig } from "../types"; import { disconnectClient, syncConnectedClient } from "./connect"; import { readClientConnectionState } from "./state"; import { handleMachineApi, type HubReachability, type MachineApiDeps } from "./machine-api"; -import { requireMachineAuth } from "./machine-auth"; +import { MACHINE_GUI_ORIGIN_HEADER, requireMachineAuth } from "./machine-auth"; import { relayHubManagementRequest } from "./hub-relay"; const VERSION = (() => { @@ -52,8 +52,7 @@ export function machineRouteAllowed(url: URL, req: Request, relayEnabled: boolea if (req.method !== "GET" || path.startsWith("/api/") || path.startsWith("/v1/")) return false; return GUI_SPA_PATHS.has(path) || path.startsWith("/integrations/") - || path.startsWith("/assets/") - && /\.(?:css|gif|ico|jpe?g|js|json|map|png|svg|webp|woff2?)$/i.test(path); + || /\.(?:css|gif|ico|jpe?g|js|json|map|png|svg|webp|woff2?)$/i.test(path); } export function startMachineListener( @@ -86,10 +85,10 @@ export function startMachineListener( const url = new URL(req.url); if (!machineRouteAllowed(url, req, relayEnabled)) return json404(req); if (url.pathname === "/healthz" && req.method === "GET") { - return Response.json({ service: "opencodex", version: VERSION, role: "client", pid: process.pid, port: server.port }); + return Response.json({ service: "opencodex", version: VERSION, role: "client", uptime: process.uptime(), pid: process.pid, port: server.port }); } if (url.pathname === "/readyz" && req.method === "GET") { - return Response.json({ service: "opencodex", version: VERSION, role: "client", status: "ready", pid: process.pid, port: server.port, protocolVersion: 1 }); + return Response.json({ service: "opencodex", version: VERSION, role: "client", status: "ready", uptime: process.uptime(), pid: process.pid, port: server.port, protocolVersion: 1 }); } if (url.pathname.startsWith("/api/machine/hub-relay/")) { if (!relayEnabled) return json404(req); @@ -99,7 +98,7 @@ export function startMachineListener( const suffix = `${url.pathname.slice(prefix.length)}${url.search}`; const response = await relayHubManagementRequest(req, suffix, { managementUrl: connection.managementUrl, - browserOrigin: req.headers.get("Origin") ?? "", + browserOrigin: req.headers.get(MACHINE_GUI_ORIGIN_HEADER) ?? req.headers.get("Origin") ?? "", }, { fetchImpl: deps.fetchImpl }); if (response.status === 401) hubReachability = "unauthorized"; else if (response.status >= 500) hubReachability = "offline"; @@ -122,7 +121,14 @@ export function startMachineListener( const gui = serveGuiFile(url.pathname, undefined, session ?? undefined); if (gui) return gui; if (url.pathname === "/") { - return Response.json(rootFallbackPayload(), { headers: browserSecurityHeaders() }); + return Response.json({ + status: "ok", + service: "opencodex", + version: VERSION, + role: "client", + dashboard: { available: false, reason: "GUI build not found" }, + endpoints: { health: "/healthz", ready: "/readyz", machine: "/api/machine/*" }, + }, { headers: browserSecurityHeaders() }); } return json404(req); }, From a8fdcf6c50facd41bf3d629c1bbf9ddc1f2c54e1 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 04:24:57 +0900 Subject: [PATCH 061/172] feat(deploy): harden management ingress allowlist --- src/server/index.ts | 22 ++++++++++++++++----- tests/loopback-listener-integration.test.ts | 1 + 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/server/index.ts b/src/server/index.ts index 9e809da20a..43c159d78c 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -811,12 +811,24 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server { try { const denied: Array<{ path: string; headers?: Record }> = [ { path: "/v1/catalog" }, + { path: "/v1%2Fcatalog" }, { path: "/healthz" }, { path: "/readyz" }, { path: "/v1/responses", headers: { Connection: "Upgrade", Upgrade: "websocket" } }, From e3f6cf8a217426d6d07d797861a37029f392bc42 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 03:55:43 +0900 Subject: [PATCH 062/172] fix(two-plane): repair remote verification failures --- src/client/hub-relay.ts | 15 ++++++++++----- src/client/runtime.ts | 17 ++++++++--------- src/usage/summary.ts | 38 ++++++++++++++++++++++++++++--------- tests/usage-summary.test.ts | 8 ++++---- 4 files changed, 51 insertions(+), 27 deletions(-) diff --git a/src/client/hub-relay.ts b/src/client/hub-relay.ts index 57d6da4cc1..ae75862f8e 100644 --- a/src/client/hub-relay.ts +++ b/src/client/hub-relay.ts @@ -76,14 +76,18 @@ function relayDestination(suffix: string, target: HubRelayTarget, method: string return destination; } -async function boundedBody(stream: ReadableStream | null, declared: string | null, limit: number): Promise { +async function boundedBody( + stream: ReadableStream | null, + declared: string | null, + limit: number, +): Promise | null> { if (!stream) return null; const contentLength = declared === null ? null : Number(declared); if (contentLength !== null && (!Number.isSafeInteger(contentLength) || contentLength < 0 || contentLength > limit)) { throw new RangeError("body_too_large"); } const reader = stream.getReader(); - const chunks: Uint8Array[] = []; + const chunks: Uint8Array[] = []; let length = 0; try { while (true) { @@ -96,7 +100,8 @@ async function boundedBody(stream: ReadableStream | null, declared: } finally { reader.releaseLock(); } - const body = new Uint8Array(length); + // BodyInit requires an ArrayBuffer-backed view, not a SharedArrayBuffer-capable view. + const body: Uint8Array = new Uint8Array(new ArrayBuffer(length)); let offset = 0; for (const chunk of chunks) { body.set(chunk, offset); @@ -132,7 +137,7 @@ export async function relayHubManagementRequest( const destination = relayDestination(suffix, target, method); if (!destination) return relayError(404, "hub relay path refused"); - let body: Uint8Array | null; + let body: Uint8Array | null; try { body = method === "GET" || method === "HEAD" ? null @@ -175,7 +180,7 @@ export async function relayHubManagementRequest( return relayError(502, "hub relay redirect refused"); } - let responseBody: Uint8Array | null; + let responseBody: Uint8Array | null; const responseHeaders = filteredHeaders(upstream.headers, RESPONSE_HEADERS); if (!headersWithinLimit(responseHeaders)) { try { await upstream.body?.cancel(); } catch { /* best effort */ } diff --git a/src/client/runtime.ts b/src/client/runtime.ts index b9b302c02e..03f0be2cba 100644 --- a/src/client/runtime.ts +++ b/src/client/runtime.ts @@ -45,19 +45,18 @@ export async function startClientRuntime( if (state.kind !== "connected") throw new Error(`client runtime refused: client state is ${state.kind}`); const config = loadConfig(); const preferred = options.port ?? config.port ?? 10100; - const port = preferred === 0 - ? 0 - : await findAvailablePort(preferred, "127.0.0.1", { - preferRetryMs: options.port === undefined ? 750 : 5_000, - preferRetryIntervalMs: 50, - allowEphemeralFallback: options.port === undefined, - }); + const port = await findAvailablePort(preferred, "127.0.0.1", { + preferRetryMs: options.port === undefined ? 750 : 5_000, + preferRetryIntervalMs: 50, + allowEphemeralFallback: options.port === undefined, + }); const server = startMachineListener(port, { state: state.value }); + const boundPort = server.port ?? port; activeServer = server; - activePort = server.port; + activePort = boundPort; installCrashGuards(); writePid(process.pid); - writeRuntimePort({ pid: process.pid, port: server.port, hostname: "127.0.0.1" }); + writeRuntimePort({ pid: process.pid, port: boundPort, hostname: "127.0.0.1" }); let shuttingDown = false; const shutdown = () => { diff --git a/src/usage/summary.ts b/src/usage/summary.ts index 58560c3552..b37aac8533 100644 --- a/src/usage/summary.ts +++ b/src/usage/summary.ts @@ -1166,23 +1166,43 @@ export function projectUsageSummary( const apiKeyId = normalizeExactFilterValue(filter.apiKeyId); if (provider === null && model === null && apiKeyId === null) return summary; + // Re-summarise from the entries the summary was built from, rather than + // projecting over its rows. + // + // Projecting rows looked cheaper and was wrong in three ways that only show + // up together: breakdown rows past MAX_USAGE_MODEL_BREAKDOWN_ROWS are + // collapsed into a synthetic "other" row, so a provider living only in that + // tail is unfindable and reports matched:false despite real usage; a + // provider row is a whole-provider aggregate, so a model filter kept the + // provider's OTHER models in providers[] while models[] and the totals + // excluded them, contradicting itself inside one response; and a model row + // carries a single optional cost, so priced/unpriced/unmetered counts could + // only be guessed per model rather than counted per request. + // + // Key ownership is the outer slice: no provider/model attribution or bucket + // construction may observe rows belonging to another client key. + const keyFilteredEntries = apiKeyId === null + ? entries ?? [] + : (entries ?? []).filter(entry => entry.apiKeyId === apiKeyId); + + // The entries are already in hand on every path that filters, so the honest + // computation is also the simple one. const matches = (rowProvider: string, rowModel: string): boolean => { if (provider !== null && baseProviderLabel(rowProvider).toLowerCase() !== provider) return false; if (model !== null && rowModel.toLowerCase() !== model) return false; return true; }; - // The apiKeyId filter drops whole ENTRIES, because a key owns the entry rather than any - // individual attempt within it. Provider and model filters below narrow to matching - // ATTRIBUTIONS instead: keeping a whole combo entry because one attempt matched drags the - // other attempts' tokens and cost into the filtered totals, so a two-attempt combo - // filtered to its cheap model would report the expensive model's spend too. - const source = apiKeyId === null - ? entries ?? [] - : (entries ?? []).filter(entry => entry.apiKeyId === apiKeyId); + // Narrow to matching ATTRIBUTIONS, not matching entries. + // + // Keeping a whole combo entry because one of its attempts matched drags the + // other attempts' tokens and cost into the filtered totals: a two-attempt + // combo filtered to its cheap model reported the expensive model's spend + // too. Rewriting the entry down to its matching attempts is what makes the + // filtered numbers mean what the flag says. let comboOverlap = false; const filtered: PersistedUsageEntry[] = []; - for (const entry of source) { + for (const entry of keyFilteredEntries) { if (!entry.attempts?.length) { const identity = usageModelIdentity(entry.provider, entry.model, entry.resolvedModel); if (matches(entry.provider, identity.model)) filtered.push(entry); diff --git a/tests/usage-summary.test.ts b/tests/usage-summary.test.ts index 3811615a08..8917e05f6a 100644 --- a/tests/usage-summary.test.ts +++ b/tests/usage-summary.test.ts @@ -402,9 +402,9 @@ describe("projectUsageSummary", () => { test("filters by exact api key id before provider and model attribution", () => { const entries = [ - entry({ ts: at, requestId: "key-a-openai", apiKeyId: "Key-A", provider: "openai", model: "gpt-5.5", usageStatus: "reported", usage: priced, accountLogLabel: "account-a" }), - entry({ ts: at + 1, requestId: "key-a-anthropic", apiKeyId: "Key-A", provider: "anthropic", model: "claude-opus", usageStatus: "reported", usage: priced, accountLogLabel: "account-b" }), - entry({ ts: at + 2, requestId: "key-b", apiKeyId: "key-a", provider: "openai", model: "gpt-5.5", usageStatus: "reported", usage: priced, accountLogLabel: "account-c" }), + entry({ ts: at, requestId: "key-a-openai", apiKeyId: "Key-A", provider: "openai", model: "gpt-5.5", usageStatus: "reported", usage: priced, accountLogLabel: "main" }), + entry({ ts: at + 1, requestId: "key-a-anthropic", apiKeyId: "Key-A", provider: "anthropic", model: "claude-opus", usageStatus: "reported", usage: priced, accountLogLabel: "pabc123" }), + entry({ ts: at + 2, requestId: "key-b", apiKeyId: "key-a", provider: "openai", model: "gpt-5.5", usageStatus: "reported", usage: priced, accountLogLabel: "pffffff" }), entry({ ts: at + 3, requestId: "legacy", provider: "openai", model: "gpt-5.5", usageStatus: "reported", usage: priced }), ]; const summary = summarizeUsage(entries, "30d", at + 4); @@ -414,7 +414,7 @@ describe("projectUsageSummary", () => { expect(byKey.summary.requests).toBe(2); expect(byKey.models).toHaveLength(2); expect(byKey.providers).toHaveLength(2); - expect(byKey.accounts.map(row => row.accountLogLabel).sort()).toEqual(["account-a", "account-b"]); + expect(byKey.accounts.map(row => row.accountLogLabel).sort()).toEqual(["main", "pabc123"]); const combined = projectUsageSummary(summary, { apiKeyId: "Key-A", From 7d8b878fdf22ab92458abb4d0ed26465aa9748bc Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 04:25:03 +0900 Subject: [PATCH 063/172] docs(deploy): add remote hub deployment guide --- docs-site/astro.config.mjs | 1 + .../src/content/docs/guides/remote-hub.md | 229 ++++++++++++++++++ structure/01_runtime.md | 19 +- structure/05_gui-and-management-api.md | 24 +- structure/06_docs-and-release.md | 19 ++ 5 files changed, 286 insertions(+), 6 deletions(-) create mode 100644 docs-site/src/content/docs/guides/remote-hub.md diff --git a/docs-site/astro.config.mjs b/docs-site/astro.config.mjs index f80cf2e90f..d1ff26edcc 100644 --- a/docs-site/astro.config.mjs +++ b/docs-site/astro.config.mjs @@ -85,6 +85,7 @@ export default defineConfig({ label: "Guides", translations: { fr: "Guides", ko: "가이드", "zh-CN": "指南", "zh-TW": "指南", ru: "Руководства", ja: "ガイド", tr: "Kılavuzlar" }, items: [ + { label: "Remote Hub Deployment", slug: "guides/remote-hub" }, { label: "Providers", translations: { fr: "Fournisseurs", ko: "프로바이더", "zh-CN": "提供商", "zh-TW": "供應商", ru: "Провайдеры", ja: "プロバイダー", tr: "Sağlayıcılar" }, slug: "guides/providers" }, { label: "Factory Droid Bridge", translations: { fr: "Pont Factory Droid", ko: "Factory Droid 브리지" }, slug: "guides/factory-droid" }, { label: "Model Routing", translations: { fr: "Routage des modèles", ko: "모델 라우팅", "zh-CN": "模型路由", "zh-TW": "模型路由", ru: "Маршрутизация моделей", ja: "モデルルーティング", tr: "Model Yönlendirme" }, slug: "guides/model-routing" }, diff --git a/docs-site/src/content/docs/guides/remote-hub.md b/docs-site/src/content/docs/guides/remote-hub.md new file mode 100644 index 0000000000..b1d4a08a76 --- /dev/null +++ b/docs-site/src/content/docs/guides/remote-hub.md @@ -0,0 +1,229 @@ +--- +title: Remote Hub Deployment +description: Run an opencodex hub on Linux, macOS, or Docker with a loopback-only management ingress, Tailscale Serve, and headless OAuth. +--- + +An opencodex hub keeps provider credentials and usage state on one host while authenticated clients +use its data plane remotely. The browser-facing management plane is separate: an optional listener +binds only `127.0.0.1`, serves the dashboard and `/api/*`, and is intended to sit behind Tailscale +Serve or another operator-owned HTTPS frontend. + +The management ingress never serves `/v1/*`, `/healthz`, `/readyz`, or WebSockets. Do not publish its +port directly, do not add a cloud-firewall rule for it, and do not use Tailscale Funnel. Funnel is a +public-internet surface and is outside this deployment model. + +## Trust and consent boundaries + +- Provider and OAuth credentials stay on the hub. Never copy them into a client, image layer, + service definition, support bundle, screenshot, or command line. +- The data admission token is delivered through the owner-only `service-api-token` file or + `OCX_API_TOKEN_FILE`. It is not a management credential. +- A raw management admin token can perform ordinary administration, but it cannot mint a browser + session or authorize consent-bearing actions such as starring the repository. Those actions + require a server-issued `gui-session`, matching browser origin, and CSRF token. +- `Tailscale-User-Login` is trusted only on the separately bound management ingress. The same header + on the public listener is ignored. `remoteGui.allowedTailscaleUsers` controls session issuance; it + does not create a new general-purpose principal. + +## Linux systemd or macOS launchd + +Choose the hub's Tailscale address for the data listener and the exact browser-visible HTTPS origin +for management. The values below are examples: + +```bash +ocx config set runtimeRole hub +ocx config set hostname 100.64.0.10 +ocx config set hub.managementPublicOrigin '"https://hub-name.tailnet-name.ts.net"' +ocx config set corsAllowOrigins '["http://localhost:10100"]' +ocx config set hub.managementIngress '{"enabled":true,"port":10101}' +ocx config set remoteGui.allowedTailscaleUsers '["operator@example.com"]' + +# Generate/read this in a protected operator shell or secret manager. +# It is a data-admission token, not a provider credential. +export OPENCODEX_API_AUTH_TOKEN="$(openssl rand -hex 32)" +ocx service install +ocx service status +``` + +`ocx service install` copies the token into the existing owner-only `service-api-token` path. The +launchd plist and systemd user unit read that protected file when the process starts; neither embeds +the literal token. Do not paste the value into `ocx config show`, unit/plist output, screenshots, or +support bundles. + +Prove liveness and readiness on the public data listener: + +```bash +curl --fail --silent http://100.64.0.10:10100/healthz +curl --fail --silent http://100.64.0.10:10100/readyz +``` + +A `200` from `/healthz` proves only that the process is alive. Deployment acceptance also requires +`/readyz`, an authenticated `GET /v1/catalog`, and one real routed response. + +## Tailscale Serve + +First prove the management socket is loopback-only, then publish it through Serve: + +```bash +ss -ltnp | grep 10101 # Linux: expected 127.0.0.1:10101 only +lsof -nP -iTCP:10101 -sTCP:LISTEN # macOS: expected 127.0.0.1 only + +tailscale serve --bg --https=443 http://127.0.0.1:10101 +tailscale serve status +``` + +Set `hub.managementPublicOrigin` to the exact HTTPS origin shown by Serve. Add the operator's exact +Tailscale login to `remoteGui.allowedTailscaleUsers`; an empty list means no remote identity can mint +a session. Verify both directions: + +```bash +# Negative: the loopback-only port must not be reachable through the node's tailnet address. +curl --fail --connect-timeout 3 http://100.64.0.10:10101/ && echo "unexpected exposure" + +# Positive: the HTTPS dashboard loads through Serve from an allowed tailnet user. +curl --fail --silent --show-error https://hub-name.tailnet-name.ts.net/ >/dev/null +``` + +The positive browser test must use a real signed-in Tailscale session; a bare `curl` may not carry the +identity headers needed for automatic session issuance. Pairing remains the fallback when the HTTPS +frontend cannot provide trustworthy Tailscale identity. + +### Operator-owned ts.net certificate proxy + +If you operate your own TLS proxy, obtain a certificate only for the full ts.net FQDN: + +```bash +tailscale cert hub-name.tailnet-name.ts.net +``` + +Protect the private key, renew it through Tailscale's supported mechanism, and proxy only to +`127.0.0.1:10101`. A generic TLS proxy does not supply trustworthy Tailscale identity. Do not +fabricate `Tailscale-User-*` headers; use the single-use, origin-bound pairing flow instead. + +## Headless OAuth + +Disable browser launch on the hub: + +```bash +ocx config set oauthOpenBrowser false +``` + +1. From the authenticated remote dashboard or management client, start `POST /api/oauth/login` for + the provider. The hub returns the authorization URL and instructions without opening a browser. +2. Open the URL on the operator's machine and authorize there. +3. If the loopback callback cannot reach the hub, paste the final redirect URL or code into the + dashboard/CLI. It sends `POST /api/oauth/login/code` with `{provider,input}`. +4. Poll the existing status endpoint until complete, then make a routed model request. + +Never put the OAuth code in shell argv, logs, issue text, screenshots, or deployment evidence. The +manual-code route keeps its existing unknown-provider, no-active-flow, invalid-code, and 4096-byte +input checks. + +## Operator-owned Docker recipe + +opencodex does not publish or maintain an official container image. The following recipe is an +operator-owned starting point. Before building, resolve `oven/bun:1.4.0` to a registry digest and +replace both `REPLACE_WITH_BUN_1_4_0_DIGEST` values. A tag alone is not a production pin. + +```dockerfile +# syntax=docker/dockerfile:1 +FROM oven/bun:1.4.0@sha256:REPLACE_WITH_BUN_1_4_0_DIGEST AS build +WORKDIR /home/bun/app +COPY --chown=bun:bun package.json bun.lock ./ +RUN bun install --frozen-lockfile +COPY --chown=bun:bun src ./src +COPY --chown=bun:bun gui ./gui +COPY --chown=bun:bun tsconfig.json ./ +RUN cd gui && bun install --frozen-lockfile && bun run build + +FROM oven/bun:1.4.0@sha256:REPLACE_WITH_BUN_1_4_0_DIGEST AS runtime +WORKDIR /home/bun/app +ENV OPENCODEX_HOME=/home/bun/.opencodex +ENV OCX_API_TOKEN_FILE=/run/secrets/ocx_api_token +COPY --from=build --chown=bun:bun /home/bun/app/package.json ./package.json +COPY --from=build --chown=bun:bun /home/bun/app/bun.lock ./bun.lock +COPY --from=build --chown=bun:bun /home/bun/app/node_modules ./node_modules +COPY --from=build --chown=bun:bun /home/bun/app/src ./src +COPY --from=build --chown=bun:bun /home/bun/app/gui/dist ./gui/dist +USER bun +VOLUME ["/home/bun/.opencodex"] +EXPOSE 10100 +HEALTHCHECK --interval=30s --timeout=5s --retries=3 \ + CMD ["bun", "-e", "const r=await fetch('http://127.0.0.1:10100/healthz');if(!r.ok)process.exit(1)"] +CMD ["bun", "run", "src/cli/index.ts", "start", "--port", "10100"] +``` + +An example Compose definition keeps mutable state and the token outside the image: + +```yaml +services: + hub: + build: . + read_only: true + ports: + - "10100:10100" + volumes: + - ocx-state:/home/bun/.opencodex + tmpfs: + - /tmp + secrets: + - source: ocx_api_token + target: ocx_api_token + uid: "1000" + gid: "1000" + mode: 0440 + restart: unless-stopped + +volumes: + ocx-state: + +secrets: + ocx_api_token: + file: ./secrets/ocx_api_token +``` + +Initialize the named volume before the first normal start. Container port publishing requires the +data listener to bind `0.0.0.0`; the management listener remains fixed to container loopback: + +```bash +docker compose run --rm hub bun run src/cli/index.ts config set runtimeRole hub +docker compose run --rm hub bun run src/cli/index.ts config set hostname 0.0.0.0 +docker compose run --rm hub bun run src/cli/index.ts config set hub.managementPublicOrigin '"https://hub-name.tailnet-name.ts.net"' +docker compose run --rm hub bun run src/cli/index.ts config set hub.managementIngress '{"enabled":true,"port":10101}' +docker compose run --rm hub bun run src/cli/index.ts config set remoteGui.allowedTailscaleUsers '["operator@example.com"]' +docker compose up -d +``` + +Do not put a token in `ARG`, `ENV`, `COPY`, Compose YAML, image history, or the command line. Do not +mount the Docker socket, host home, Codex home, SSH agent, or provider-key files. Publish only port +`10100`. A management ingress bound to `127.0.0.1:10101` inside the container is reachable only by a +TLS/tailnet frontend in the same network namespace; never publish `10101` as a shortcut. + +After the container is healthy, run a separate readiness promotion check: + +```bash +docker compose exec hub bun -e \ + "const r=await fetch('http://127.0.0.1:10100/readyz');console.log(r.status,await r.text());if(!r.ok)process.exit(1)" + +docker compose exec hub bun -e \ + "const t=(await Bun.file('/run/secrets/ocx_api_token').text()).trim();const r=await fetch('http://127.0.0.1:10100/v1/catalog',{headers:{'x-opencodex-api-key':t}});console.log(r.status);if(!r.ok)process.exit(1)" +``` + +Then send one real authenticated routed response with a configured model. If the secret is absent or +unreadable, a non-loopback hub must not be accepted as ready. Never treat liveness alone as proof. + +## Rollback + +Inspect existing Serve mappings before changing them. `tailscale serve reset` removes every mapping +on the node; use a narrower supported removal command when unrelated mappings exist. + +```bash +tailscale serve status +tailscale serve reset +ocx config set hub.managementIngress '{"enabled":false}' +ocx service repair +``` + +For a container rollback, remove or replace the container while retaining the named state volume. +For a service rollback, stop the branch service and repair the prior release against the same +`OPENCODEX_HOME`. Disabling management ingress or Serve does not require changing the data listener. diff --git a/structure/01_runtime.md b/structure/01_runtime.md index f6bdaa1740..3cc98a81a2 100644 --- a/structure/01_runtime.md +++ b/structure/01_runtime.md @@ -7,7 +7,7 @@ | `bin/ocx.mjs` | Published npm `bin` entry (Node shim). Resolves the bundled or explicit Bun binary before project dotenv can load, stamps its runtime provenance plus a proof-bound Anthropic parent-env snapshot, lazy-runs `bun/install.js` if only the placeholder stub is present, then execs `src/cli/index.ts` under Bun. Lets `npm install -g` work without a separately-installed Bun. The exact `system codex-cli-update` inspection namespace skips both boot repair and lazy Bun installation; missing runtime support fails closed instead of mutating state. | | `src/lib/bun-runtime.ts` | Bundled-Bun resolution: `isRealBunBinary()` (size gate vs the ~450-byte placeholder stub), `bundledBunPath()`, and `durableBunPath()` (path baked into service/shim artifacts). Durable selection accepts only the source/path pair already stamped for the running executable; it never re-reads a project-dotenv `OPENCODEX_BUN_PATH`. | | `src/cli/index.ts` | `ocx` / `opencodex` CLI. Lifecycle: init, start, stop, restart, status, sync, restore/eject, gui, service, update. Configuration: provider, account, models, combo/route, access, integrations, v2. Client launchers: Claude, OpenCode, MiniMax Code, and MiniMax CLI text. The MMX launcher owns a child-lifetime loopback path bridge from the client's hard-coded `/anthropic/v1/messages` path to the canonical `/v1/messages` data plane; the server does not expose an extra auth surface. Diagnostics: doctor, debug, observe, health. Windows adds tray. The full command surface is `src/cli/help.ts`; this table names the groups, not every verb. After help/version early exits, ordinary commands run the bounded best-effort Codex-shim auto-restore policy before dispatch. `system codex-cli-update` is the deliberate read-only exception and suppresses auto-restore for its whole namespace, including malformed invocations. Keeps the `#!/usr/bin/env bun` shebang for from-source dev (`bun run src/cli/index.ts`). | -| `src/server/index.ts` | Bun server entrypoint: `startServer`, `/v1/responses` HTTP + WebSocket routing (compact handled before generic Responses), exact `POST /v1/images/generations` and `POST /v1/images/edits` routing, `/v1/models`, the Anthropic-shaped `/v1/messages` and OpenAI-shaped `/v1/chat/completions` compatibility surfaces, the Live/Realtime surface, the hosted-search relay, artifact serving, `/healthz`, the `/api/*` auth gate, the `/v1/*` JSON 404 guard, GUI fallback, and facade re-exports for split server modules. | +| `src/server/index.ts` | Bun server entrypoint: `startServer`, `/v1/responses` HTTP + WebSocket routing (compact handled before generic Responses), exact `POST /v1/images/generations` and `POST /v1/images/edits` routing, `/v1/models`, the Anthropic-shaped `/v1/messages` and OpenAI-shaped `/v1/chat/completions` compatibility surfaces, the Live/Realtime surface, the hosted-search relay, artifact serving, `/healthz`, the `/api/*` auth gate, the `/v1/*` JSON 404 guard, GUI fallback, the opt-in loopback-only hub-management listener, and facade re-exports for split server modules. | | `src/server/images.ts` | Standalone Images data plane: default OpenAI or explicit custom-provider selection, Codex account affinity, bounded opaque request relay, single-attempt upstream fetch, pool health recording, and safe response/cancellation relay. | | `src/config.ts` | Persisted `~/.opencodex/config.json` schema, defaults, migrations, transactions, and compatibility re-exports for split config modules. | | `src/config/paths.ts` | Resolves `OPENCODEX_HOME`, `config.json`, and owner-only directory hardening. | @@ -53,6 +53,23 @@ until shutdown. Normal shutdown restores native Codex. Service mode sets `OCX_SERVICE=1`, so managed restarts do not repeatedly restore/reinject; explicit service stop and uninstall still restore. +`startServer` composes up to three sockets in one synchronous startup transaction: the public data +listener, the optional unauthenticated data-loopback listener, and the optional hub-management +listener. The hub-management socket is enabled only by `runtimeRole: "hub"` plus +`hub.managementIngress.enabled`, always binds `127.0.0.1`, and default-denies everything except GUI, +session bootstrap/exchange, and `/api/*`. A failed optional bind initiates rollback of every earlier +socket; normal stop joins all bound sockets before lifecycle release. The existing launchd/systemd +installer remains the service owner and continues loading the data token from `service-api-token`; +hub mode adds no service-manager fork and no token-bearing unit/plist field. + +[Decision Log] +- 목적과 의도: Give a headless hub a browser management ingress without widening its data plane or trusting spoofable forwarding headers on the public listener. +- 기존 구현 및 제약 조건: `startServer` is synchronous through Lab activation, already owns an optional-listener transaction, and the service installer already has an owner-only token-file flow. +- 검토한 주요 대안: Add management routes to the public listener; infer trusted ingress from `Host`/`Forwarded`/Tailscale headers; create a separate service manager; extend the existing composition root. +- 선택한 방식: Bind a third socket exactly to `127.0.0.1`, select trust by receiving `Bun.serve` instance, keep a fixed route allowlist, and reuse the current launchd/systemd definitions. +- 다른 대안 대신 이 방식을 선택한 이유: Headers do not prove which transport received a request, while a kernel loopback bind plus Tailscale Serve supplies a concrete ingress boundary without duplicating lifecycle or secret delivery. +- 장점, 단점 및 영향: Public/default behavior stays unchanged and management can use Tailscale identity; operators must provide a co-located HTTPS frontend and pairing remains necessary for generic TLS proxies. + The process-state boundary deliberately exposes two PID checks. `readAlivePid()` is the cheap non-destructive probe used by liveness polling. `readPid()` and `verifyPidIdentity()` include the fixed-path command-line check required before stop, kill, port reclaim, or stale-state deletion. diff --git a/structure/05_gui-and-management-api.md b/structure/05_gui-and-management-api.md index 4294a24551..d6d374a149 100644 --- a/structure/05_gui-and-management-api.md +++ b/structure/05_gui-and-management-api.md @@ -61,11 +61,25 @@ management token creation, validation, or permission hardening fails, every `/ap must be checked explicitly because an `icacls` timeout is a soft failure in the shared secret helper. Local dashboard page entry requires a loopback binding, a valid parseable loopback `Host`, and an -exact request origin. A non-loopback dashboard uses the management token flow instead. The server -issues an in-memory session for five minutes, capped at 128 live sessions. The session is bound to the -exact protocol, host, and port; state-changing requests additionally require the session CSRF token. -The dashboard never attaches its management session to `/v1/*` requests, and pages containing a -session bootstrap are served with `Cache-Control: no-store`. +exact request origin. A hub may additionally enable `hub.managementIngress`, a second management +surface bound exactly to `127.0.0.1` for a local Tailscale Serve or operator TLS frontend. That +listener serves only packaged GUI/SPA routes, `GET`/`POST /opencodex-session`, and `/api/*`; all data, +health, readiness, WebSocket, and unknown-static routes receive a JSON 404 before dispatch. + +Tailscale identity headers authorize session issuance only when the request arrived on that specific +listener and the exact login appears in `remoteGui.allowedTailscaleUsers`. The public listener and +the unauthenticated data-loopback listener always pass `trustedTailscaleIngress: false`, regardless +of `Host`, `Origin`, `Forwarded`, `X-Forwarded-*`, or `Tailscale-User-*` values. A generic TLS proxy +cannot establish that identity and uses the existing single-use, digest-only, origin-bound pairing +exchange. Pairing accepts no admin/data credential substitute and consumes a grant only after the +full origin predicate succeeds. + +The server issues a local in-memory session for five minutes or a remote session for twelve hours, +with 128 live sessions maximum. Every session is bound to the exact server and browser origins; +state-changing requests additionally require the session CSRF token. A raw admin token remains +ordinary management authority only and cannot satisfy consent routes. The dashboard never attaches +its management session to `/v1/*` requests, and pages containing a session bootstrap are served with +`Cache-Control: no-store`. Proxy admission credentials must never reach an upstream provider. The forwarding guard rejects the `ocx_data_`, `ocx_admin_`, and `ocx_session_` prefixes, historical keys matching diff --git a/structure/06_docs-and-release.md b/structure/06_docs-and-release.md index 71c86df6f9..5c8f7346ad 100644 --- a/structure/06_docs-and-release.md +++ b/structure/06_docs-and-release.md @@ -35,6 +35,25 @@ bun install --frozen-lockfile bun run build ``` +## Container deployment recipe + +Phase-5 remote-hub documentation includes an operator-owned multi-stage Dockerfile and Compose +example in `guides/remote-hub`; the repository intentionally ships no root `Dockerfile`, +`.dockerignore`, registry image, or publish workflow. An official image would create a release +surface that also requires maintained base-image digest updates, vulnerability scanning, SBOM, +signing, registry provenance, rollback, and support policy. Until those controls have an explicit +owner, the guide requires operators to pin the Bun base digest, run non-root, persist +`OPENCODEX_HOME`, mount the data token through `OCX_API_TOKEN_FILE`, and prove liveness, readiness, +authenticated catalog access, and a real routed response themselves. + +[Decision Log] +- 목적과 의도: Document a reproducible container topology without silently creating an official image channel. +- 기존 구현 및 제약 조건: The repository has no maintained Docker release artifacts, registry workflow, scanner, SBOM/signing chain, or image rollback policy. +- 검토한 주요 대안: Add a root Dockerfile and publish it; omit containers entirely; provide a complete operator-owned recipe in the remote-hub guide. +- 선택한 방식: Keep the recipe in documentation, require an operator-resolved base digest and mounted secret file, and publish only the public data port. +- 다른 대안 대신 이 방식을 선택한 이유: A source recipe communicates the supported runtime contract while leaving image provenance and operations with the party building it. +- 장점, 단점 및 영향: Docker users have a concrete starting point, but opencodex does not claim to ship, scan, sign, or support the resulting image. + ## Windows service wrapper and incomplete updates [Decision Log] From a3fe759b391cd6bb37d8e098268d745b0b3e92a9 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 04:08:30 +0900 Subject: [PATCH 064/172] fix(two-plane): unblock client start and GUI verification --- gui/src/i18n/fr.ts | 52 +++++++++++++-------------- gui/src/i18n/zh-TW.ts | 52 +++++++++++++-------------- gui/tests/api-auth-memory.test.ts | 4 ++- gui/tests/claude-toggle-race.test.tsx | 1 + gui/tests/connect-pairing.test.ts | 14 +++++--- src/cli/dispatch.ts | 4 --- tests/cli-start-journal-order.test.ts | 11 ++++-- 7 files changed, 74 insertions(+), 64 deletions(-) diff --git a/gui/src/i18n/fr.ts b/gui/src/i18n/fr.ts index a3a2a05ed0..e06519c2ee 100644 --- a/gui/src/i18n/fr.ts +++ b/gui/src/i18n/fr.ts @@ -2308,30 +2308,30 @@ export const fr: Record = { "models.aliasAuto": "auto", "models.aliasUser": "utilisateur", "models.aliasStale": "obsolète", - "connection.discovering": "Discovering local and shared targets…", - "connection.machineUnavailable": "The local machine plane is unavailable. Shared requests were not redirected locally.", - "connection.disconnect": "Disconnect from hub", - "connection.disconnectConfirm": "Disconnect this machine from the hub and restart it in standalone mode?", - "connection.pairing.title": "Connect this dashboard to the hub", - "connection.pairing.body": "Paste the one-time pairing code created on the hub.", - "connection.pairing.relayWarning": "This code is exchanged through the fixed hub relay. The relay cannot be redirected to another host.", - "connection.pairing.code": "One-time pairing code", - "connection.pairing.submit": "Connect", - "connection.pairing.submitting": "Connecting…", - "connection.pairing.error": "The pairing code was refused or expired. The code was left in place so you can check it.", - "connection.machine.title": "This machine", - "connection.machine.shimHealthy": "Codex shim is healthy.", - "connection.machine.shimNeedsAttention": "Codex shim needs attention.", - "connection.machine.repairShim": "Repair shim", - "connection.machine.removeShim": "Remove shim", - "connection.clients.title": "Connected clients", - "connection.clients.none": "No client status available", - "connection.clients.sync": "Sync now", - "connection.clients.syncing": "Syncing…", - "usage.source.connected": "Source: hub usage", - "usage.source.local": "Source: local usage.jsonl", - "usage.scope.label": "Usage scope", - "usage.scope.machine": "This machine", - "usage.scope.hub": "Hub-wide", - "usage.hubOffline": "Hub usage is unavailable. Local usage was not substituted.", + "connection.discovering": "Détection des cibles locale et partagée…", + "connection.machineUnavailable": "Le plan machine local est indisponible. Les requêtes partagées n'ont pas été redirigées localement.", + "connection.disconnect": "Déconnecter du hub", + "connection.disconnectConfirm": "Déconnecter cette machine du hub et la redémarrer en mode autonome ?", + "connection.pairing.title": "Connecter ce tableau de bord au hub", + "connection.pairing.body": "Collez le code d'association à usage unique créé sur le hub.", + "connection.pairing.relayWarning": "Ce code passe par le relais fixe du hub. Le relais ne peut pas viser un autre hôte.", + "connection.pairing.code": "Code d'association à usage unique", + "connection.pairing.submit": "Connecter", + "connection.pairing.submitting": "Connexion…", + "connection.pairing.error": "Le code a été refusé ou a expiré. Il reste saisi pour vérification.", + "connection.machine.title": "Cette machine", + "connection.machine.shimHealthy": "Le shim Codex est opérationnel.", + "connection.machine.shimNeedsAttention": "Le shim Codex nécessite une intervention.", + "connection.machine.repairShim": "Réparer le shim", + "connection.machine.removeShim": "Supprimer le shim", + "connection.clients.title": "Clients connectés", + "connection.clients.none": "Aucun état client disponible", + "connection.clients.sync": "Synchroniser", + "connection.clients.syncing": "Synchronisation…", + "usage.source.connected": "Source : utilisation du hub", + "usage.source.local": "Source : usage.jsonl local", + "usage.scope.label": "Portée de l'utilisation", + "usage.scope.machine": "Cette machine", + "usage.scope.hub": "Tout le hub", + "usage.hubOffline": "L'utilisation du hub est indisponible. Les données locales n'ont pas été substituées.", }; diff --git a/gui/src/i18n/zh-TW.ts b/gui/src/i18n/zh-TW.ts index e478942b46..6974aaabc6 100644 --- a/gui/src/i18n/zh-TW.ts +++ b/gui/src/i18n/zh-TW.ts @@ -2306,30 +2306,30 @@ export const zhTW: Record = { "models.aliasAuto": "自動", "models.aliasUser": "使用者", "models.aliasStale": "過期", - "connection.discovering": "Discovering local and shared targets…", - "connection.machineUnavailable": "The local machine plane is unavailable. Shared requests were not redirected locally.", - "connection.disconnect": "Disconnect from hub", - "connection.disconnectConfirm": "Disconnect this machine from the hub and restart it in standalone mode?", - "connection.pairing.title": "Connect this dashboard to the hub", - "connection.pairing.body": "Paste the one-time pairing code created on the hub.", - "connection.pairing.relayWarning": "This code is exchanged through the fixed hub relay. The relay cannot be redirected to another host.", - "connection.pairing.code": "One-time pairing code", - "connection.pairing.submit": "Connect", - "connection.pairing.submitting": "Connecting…", - "connection.pairing.error": "The pairing code was refused or expired. The code was left in place so you can check it.", - "connection.machine.title": "This machine", - "connection.machine.shimHealthy": "Codex shim is healthy.", - "connection.machine.shimNeedsAttention": "Codex shim needs attention.", - "connection.machine.repairShim": "Repair shim", - "connection.machine.removeShim": "Remove shim", - "connection.clients.title": "Connected clients", - "connection.clients.none": "No client status available", - "connection.clients.sync": "Sync now", - "connection.clients.syncing": "Syncing…", - "usage.source.connected": "Source: hub usage", - "usage.source.local": "Source: local usage.jsonl", - "usage.scope.label": "Usage scope", - "usage.scope.machine": "This machine", - "usage.scope.hub": "Hub-wide", - "usage.hubOffline": "Hub usage is unavailable. Local usage was not substituted.", + "connection.discovering": "正在探索本機與共享目標…", + "connection.machineUnavailable": "本機機器平面無法使用。共享請求未改用本機資料。", + "connection.disconnect": "中斷 Hub 連線", + "connection.disconnectConfirm": "要中斷此機器與 Hub 的連線,並以獨立模式重新啟動嗎?", + "connection.pairing.title": "將此儀表板連接到 Hub", + "connection.pairing.body": "貼上在 Hub 建立的一次性配對碼。", + "connection.pairing.relayWarning": "此代碼透過固定 Hub 轉送交換,無法重新導向其他主機。", + "connection.pairing.code": "一次性配對碼", + "connection.pairing.submit": "連接", + "connection.pairing.submitting": "連接中…", + "connection.pairing.error": "配對碼遭拒或已過期。輸入內容已保留供檢查。", + "connection.machine.title": "此機器", + "connection.machine.shimHealthy": "Codex shim 狀態正常。", + "connection.machine.shimNeedsAttention": "Codex shim 需要處理。", + "connection.machine.repairShim": "修復 shim", + "connection.machine.removeShim": "移除 shim", + "connection.clients.title": "已連接的用戶端", + "connection.clients.none": "沒有用戶端狀態", + "connection.clients.sync": "立即同步", + "connection.clients.syncing": "同步中…", + "usage.source.connected": "來源:Hub 使用量", + "usage.source.local": "來源:本機 usage.jsonl", + "usage.scope.label": "使用量範圍", + "usage.scope.machine": "此機器", + "usage.scope.hub": "整個 Hub", + "usage.hubOffline": "Hub 使用量無法使用,未以本機使用量替代。", }; diff --git a/gui/tests/api-auth-memory.test.ts b/gui/tests/api-auth-memory.test.ts index 9a3ddf4fab..6483fbcaf5 100644 --- a/gui/tests/api-auth-memory.test.ts +++ b/gui/tests/api-auth-memory.test.ts @@ -29,7 +29,9 @@ beforeEach(() => { Object.defineProperty(testWindow, "prompt", { configurable: true, writable: true, value: () => null }); } resetApiAuthFetchForTests(async () => { - return window.prompt("OpenCodex admin token (OPENCODEX_ADMIN_AUTH_TOKEN)")?.trim() || null; + return typeof window.prompt === "function" + ? window.prompt("OpenCodex admin token (OPENCODEX_ADMIN_AUTH_TOKEN)")?.trim() || null + : null; }); sessionStorage.clear(); }); diff --git a/gui/tests/claude-toggle-race.test.tsx b/gui/tests/claude-toggle-race.test.tsx index b66f5fd94a..f1b74cd75c 100644 --- a/gui/tests/claude-toggle-race.test.tsx +++ b/gui/tests/claude-toggle-race.test.tsx @@ -88,6 +88,7 @@ beforeEach(() => { const url = String(input instanceof Request ? input.url : input); const method = (init?.method ?? (input instanceof Request ? input.method : "GET")).toUpperCase(); + if (url.includes("/api/machine/status")) return jsonResponse({}, 404); if (url.includes("/api/claude-code") && method === "PUT") { const body = JSON.parse(String(init?.body ?? "{}")) as { enabled?: boolean }; putBodies.push(body); diff --git a/gui/tests/connect-pairing.test.ts b/gui/tests/connect-pairing.test.ts index af263228b4..07c79b020e 100644 --- a/gui/tests/connect-pairing.test.ts +++ b/gui/tests/connect-pairing.test.ts @@ -5,7 +5,7 @@ import { act, createElement } from "react"; test("App mounts the relay pairing form and installs only the returned shared session", async () => { const keys = ["window", "document", "navigator", "sessionStorage", "localStorage", "fetch", "confirm", "alert", "IS_REACT_ACT_ENVIRONMENT", "__APP_VERSION__"] as const; const previous = Object.fromEntries(keys.map(key => [key, Reflect.get(globalThis, key)])); - const win = new Window({ url: "http://localhost/#dashboard" }); + const win = new Window({ url: "http://localhost/#usage" }); Object.defineProperties(globalThis, { window: { configurable: true, value: win }, document: { configurable: true, value: win.document }, @@ -51,6 +51,11 @@ test("App mounts the relay pairing form and installs only the returned shared se return new Response(sessionHtml, { headers: { "Content-Type": "text/html" } }); } if (url.pathname === "/healthz") return Response.json({ version: "0.0.0-test" }); + if (url.pathname.endsWith("/api/usage")) return Response.json({ + range: "30d", surface: "all", since: null, generatedAt: Date.now(), + summary: { requests: 0, attemptCount: 0, measuredRequests: 0, reportedRequests: 0, unreportedRequests: 0, unsupportedRequests: 0, estimatedRequests: 0, inputTokens: 0, outputTokens: 0, cachedInputTokens: 0, cacheReadInputTokens: 0, cacheCreationInputTokens: 0, reasoningOutputTokens: 0, totalTokens: 0, coverageRatio: 0, estimatedCostUsd: 0, pricedRequests: 0, unpricedRequests: 0, unmeteredRequests: 0 }, + days: [], models: [], providers: [], accounts: [], historyTruncated: false, + }); return Response.json({}); }) as typeof fetch; Object.defineProperties(globalThis, { @@ -74,8 +79,7 @@ test("App mounts the relay pairing form and installs only the returned shared se } const input = container.querySelector("#connect-pairing-code") as HTMLInputElement; Object.getOwnPropertyDescriptor(win.HTMLInputElement.prototype, "value")!.set!.call(input, `ocx_pair_${"a".repeat(43)}`); - input.dispatchEvent(new win.Event("input", { bubbles: true })); - input.dispatchEvent(new win.Event("change", { bubbles: true })); + await act(async () => { input.dispatchEvent(new win.Event("input", { bubbles: true })); }); const form = input.closest("form")!; await act(async () => { form.dispatchEvent(new win.Event("submit", { bubbles: true, cancelable: true })); }); const successDeadline = Date.now() + 1_000; @@ -91,7 +95,7 @@ test("App mounts the relay pairing form and installs only the returned shared se await act(async () => { root.unmount(); }); container.remove(); win.close(); - for (const key of keys) Object.defineProperty(globalThis, key, { configurable: true, value: previous[key] }); + for (const key of keys) Object.defineProperty(globalThis, key, { configurable: true, writable: true, value: previous[key] }); } }); @@ -137,6 +141,6 @@ test("a refused pairing renders an accessible error without clearing the pasted await act(async () => { root.unmount(); }); container.remove(); win.close(); - for (const key of keys) Object.defineProperty(globalThis, key, { configurable: true, value: previous[key] }); + for (const key of keys) Object.defineProperty(globalThis, key, { configurable: true, writable: true, value: previous[key] }); } }); diff --git a/src/cli/dispatch.ts b/src/cli/dispatch.ts index b3ecc87daa..75275753ad 100644 --- a/src/cli/dispatch.ts +++ b/src/cli/dispatch.ts @@ -63,10 +63,6 @@ const commandRunners: Record = { const { readClientConnectionState } = await import("../client/state"); const clientState = readClientConnectionState(); await reconcileClientJournalBeforeLifecycle(clientState); - if (clientState.kind === "connected") { - console.error("Client mode does not start a local provider proxy in Remote Hub Phase 3; use 'ocx sync'."); - return 1; - } if (clientState.kind === "invalid" || clientState.kind === "mismatched") { console.error(`Client state is ${clientState.kind}: ${clientState.reason}`); return 1; diff --git a/tests/cli-start-journal-order.test.ts b/tests/cli-start-journal-order.test.ts index c8ac68e9d8..712af88ce4 100644 --- a/tests/cli-start-journal-order.test.ts +++ b/tests/cli-start-journal-order.test.ts @@ -192,8 +192,15 @@ describe("start and ensure journal ownership (#1230)", () => { }); children.push(child); const runtimePath = join(fx.ocxHome, "runtime-port.json"); - const runtime = await waitFor(() => { - if (!existsSync(runtimePath)) return null; + const runtime = await waitFor(async () => { + if (!existsSync(runtimePath)) { + if (child.exitCode === null) return null; + const [stdout, stderr] = await Promise.all([ + new Response(child.stdout).text(), + new Response(child.stderr).text(), + ]); + throw new Error(`connected client exited ${child.exitCode}: ${stderr || stdout}`); + } try { const value = JSON.parse(readFileSync(runtimePath, "utf8")) as { pid?: number; port?: number; hostname?: string }; return value.pid === child.pid && typeof value.port === "number" && value.port > 0 ? value : null; From 5cdef72371d0ee1037adde833a9a34b50b148896 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 04:29:41 +0900 Subject: [PATCH 065/172] fix(client): a hub without client state is disconnected, not mismatched (first oracle dogfood boot) --- src/client/state.ts | 7 ++++--- tests/client-connect.test.ts | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/client/state.ts b/src/client/state.ts index 3947b383cd..a28ff01fc5 100644 --- a/src/client/state.ts +++ b/src/client/state.ts @@ -38,9 +38,10 @@ export function readClientConnectionState(): ClientConnectionState { return { kind: "invalid", reason: "config.json.runtimeRole is invalid" }; } if (!hasClient && (role === undefined || role === "standalone")) return { kind: "disconnected" }; - if (!hasClient && role === "hub") { - return { kind: "mismatched", reason: "runtimeRole=hub cannot be used as a connected client" }; - } + // A hub is a server role, not a broken client: without client state it simply is not + // connected, and refusing here blocked `ocx start` on every hub (found on the first + // clisu-oracle dogfood boot). Hub role WITH client state remains mismatched below. + if (!hasClient && role === "hub") return { kind: "disconnected" }; if (!hasClient || role !== "client") { return { kind: "mismatched", diff --git a/tests/client-connect.test.ts b/tests/client-connect.test.ts index 00d236c71c..7e9f0563f0 100644 --- a/tests/client-connect.test.ts +++ b/tests/client-connect.test.ts @@ -31,6 +31,30 @@ function readyBody(protocol = 1, minimumClientProtocol = 1) { } describe("remote hub client boundary", () => { + test("runtimeRole=hub without client state reads as disconnected so the hub can start", () => { + // First clisu-oracle dogfood boot: the hub role refused 'ocx start' because the + // client-state reader classified role=hub (no client block) as mismatched. A hub + // is a server; without client state it is simply not a connected client. + const readScript = ` + const { readClientConnectionState } = require("./src/client/state"); + console.log(JSON.stringify(readClientConnectionState())); + `; + const home = mkdtempSync(join(tmpdir(), "ocx-hub-role-")); + const readState = () => { + const child = spawnSync(process.execPath, ["--eval", readScript], { + cwd: repoRoot, + env: { ...process.env, OPENCODEX_HOME: home }, + encoding: "utf8", + }); + return JSON.parse(child.stdout.trim().split("\n").at(-1) ?? "{}"); + }; + writeFileSync(join(home, "config.json"), JSON.stringify({ port: 10190, runtimeRole: "hub" })); + expect(readState().kind).toBe("disconnected"); + // Hub role WITH a client block stays mismatched (the honest conflict). + writeFileSync(join(home, "config.json"), JSON.stringify({ port: 10190, runtimeRole: "hub", client: { serverUrl: "https://hub.example.test" } })); + expect(readState().kind).toBe("mismatched"); + rmSync(home, { recursive: true, force: true }); + }); test("canonicalizes origin and terminal /v1 only", () => { expect(normalizeHubOrigin("https://hub.example.test/v1")).toBe("https://hub.example.test"); expect(normalizeHubOrigin("https://hub.example.test/v1/")).toBe("https://hub.example.test"); From 61710eff04cea27e743352b09a3f328e5a1a553a Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 04:14:48 +0900 Subject: [PATCH 066/172] docs(devlog): phase-4 gui screenshot evidence --- .../assets/gui-p4-dashboard.png | Bin 0 -> 94814 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 devlog/_plan/260827_remote_hub/assets/gui-p4-dashboard.png diff --git a/devlog/_plan/260827_remote_hub/assets/gui-p4-dashboard.png b/devlog/_plan/260827_remote_hub/assets/gui-p4-dashboard.png new file mode 100644 index 0000000000000000000000000000000000000000..72523a8f1a9e2d80969238ed63ccabc471f7d2dc GIT binary patch literal 94814 zcmdSB^;?$v7d419NQZ!Qch^IANk}Om-Jmo`cPL0J(j|zA64KovB`6Bg4N8M_%;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never rewrites its host client configs on start (oracle dogfood) --- src/cli/claude-agent-startup-sync.ts | 3 +++ src/codex/desired-state.ts | 16 +++++++++++++--- tests/codex-desired-state.test.ts | 11 +++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/cli/claude-agent-startup-sync.ts b/src/cli/claude-agent-startup-sync.ts index 10751ae8de..772a88ddee 100644 --- a/src/cli/claude-agent-startup-sync.ts +++ b/src/cli/claude-agent-startup-sync.ts @@ -54,6 +54,9 @@ export async function syncClaudeAgentDefsAtProxyStartup( const warn = deps.warn ?? (message => console.warn(message)); try { + // Hub role: never rewrite this host's ~/.claude roster on startup (same rule as + // shouldSyncCodexOnStart / shouldSyncGrokOnStart — the hub serves other machines). + if (config.runtimeRole === "hub") return null; if (config.claudeCode?.enabled === false || config.claudeCode?.injectAgents === false) { return inject(config, {}); } diff --git a/src/codex/desired-state.ts b/src/codex/desired-state.ts index 320e077050..b750592403 100644 --- a/src/codex/desired-state.ts +++ b/src/codex/desired-state.ts @@ -71,7 +71,14 @@ export function codexIntegrationEnabled(config: Pick): boolean { +export function shouldSyncCodexOnStart(config: Pick): boolean { + // A hub is a server for OTHER machines: it must not rewrite its own host's + // Codex/Claude/Grok client configs on startup (interview decision Q6, and the + // first clisu-oracle dogfood boot proved the failure mode — the hub marked + // /readyz failed because it tried to run the full local client sync). + // "Hub is also a client" stays possible by explicitly enabling integrations + // later; the ROLE alone never injects. + if (config.runtimeRole === "hub") return false; return codexIntegrationEnabled(config); } @@ -182,7 +189,7 @@ export function setClaudeDesktopIntegrationEnabled(enabled: boolean): CodexDesir */ export async function syncCodexOnStartIfEnabled( port: number, - config: Pick, + config: Pick, sync: CodexStartupSync = defaultStartupSync, readinessGate?: ReadinessGate, ): Promise<{ ran: boolean; catalogWritten: boolean; cacheSynced: boolean }> { @@ -225,6 +232,9 @@ async function defaultStartupSync(port: number): Promise): boolean { +export function shouldSyncGrokOnStart(config: Pick): boolean { + // Same hub rule as shouldSyncCodexOnStart: the hub role never rewrites its + // host's client configs on startup. + if (config.runtimeRole === "hub") return false; return grokIntegrationEnabled(config); } diff --git a/tests/codex-desired-state.test.ts b/tests/codex-desired-state.test.ts index e069955b23..4838afcd5f 100644 --- a/tests/codex-desired-state.test.ts +++ b/tests/codex-desired-state.test.ts @@ -192,6 +192,17 @@ describe("the startup gate", () => { expect(shouldSyncCodexOnStart({ ...baseConfig(), clientIntegrations: { codex: false } })).toBe(false); }); + test("the hub role never syncs its host's client configs on start", () => { + // First clisu-oracle dogfood boot: runtimeRole=hub ran the full local client + // sync, marked /readyz failed on provider-discovery noise, and rewrote + // ~/.grok/config.toml on a machine that is a SERVER for other machines. + expect(shouldSyncCodexOnStart({ ...baseConfig(), runtimeRole: "hub" })).toBe(false); + expect(shouldSyncGrokOnStart({ ...baseConfig(), runtimeRole: "hub" })).toBe(false); + // client/standalone roles keep today's behavior. + expect(shouldSyncCodexOnStart({ ...baseConfig(), runtimeRole: "standalone" })).toBe(true); + expect(shouldSyncGrokOnStart({ ...baseConfig(), runtimeRole: "standalone" })).toBe(true); + }); + test("absence, an empty object, and an explicit true all still sync", async () => { for (const clientIntegrations of [undefined, {}, { codex: true }]) { let calls = 0; From fedbe0a40c036d7fdc39ff0ee3c027c68c3c5e56 Mon Sep 17 00:00:00 2001 From: jun Date: Tue, 1 Sep 2026 17:35:55 +0900 Subject: [PATCH 068/172] fix(two-plane): declare the machine plane, enable relay, and finish the D1/D2 client side MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Five defects on this phase, plus the client half of two contract changes the earlier phases made on the server side. /api/machine/* is declared. The seven routes this phase adds were absent from the headless parity table, so tests/cli-headless-parity.test.ts failed on undeclared endpoints. They are not undocumented: status/clients mirror `ocx connect status`, sync mirrors `ocx sync`, shim mirrors the client integration commands, and disconnect mirrors `ocx disconnect`. hub-relay is the transport those commands select with --management-transport relay rather than a verb of its own. Declared as one prefix with that mapping written down. Relay actually works now. connectClient() threw "relay management transport is not available before Remote Hub Phase 4" — but this IS phase 4, and the machine listener plus hub-relay both land here. A documented option that always threw was worse than an undocumented one. A supervised client comes back after disconnect. scheduleStandaloneRecycle() skipped its own respawn when OCX_SERVICE=1, correctly leaving the restart to the supervisor, then exited 0. The real supervisor configs are failure-only (systemd Restart=on-failure, WinSW onfailure, the Task Scheduler ERRORLEVEL loop), so a clean exit reads as "finished" and nothing restarts — the client stayed down until someone noticed. Now exits 1 under supervision, the same policy the dashboard recycle already uses. launchd KeepAlive was fine either way. D1 client side: --allow-insecure-http is removed from the CLI, the connect options, and the hub client. The hub refuses plaintext pairing outright now, so keeping the flag would only spend a single-use grant against a certain rejection. The client checks the same rule locally and refuses before sending. D2 client side: the catalog fetch is unconditional. /v1/catalog emits no validator, so If-None-Match had nothing to match and connect's "initial hub catalog did not include a fresh ETag" check would have failed every connection. The stored catalogEtag becomes catalogFingerprint — our own hash of the bytes we wrote. That value was never a cache concern: it answers "is the file on disk still ours" before disconnect removes it, which needs no server participation. usage/summary.ts keeps dev's per-attribution filtering and this phase's per-key entry slice; the comment now says which filter operates at which level, because they are deliberately different. --- src/cli/connect.ts | 4 +-- src/cli/registry.ts | 2 +- src/client/connect.ts | 53 ++++++++++++++----------------- src/client/hub-client.ts | 46 +++++++++++++++++++++------ src/client/runtime.ts | 19 ++++++++++- src/config.ts | 2 +- src/types/config.ts | 9 +++++- tests/cli-headless-parity.test.ts | 7 ++++ tests/client-connect.test.ts | 28 ++++++++++------ tests/config.test.ts | 2 +- 10 files changed, 117 insertions(+), 55 deletions(-) diff --git a/src/cli/connect.ts b/src/cli/connect.ts index 1182a9b2fb..09ed24bc1b 100644 --- a/src/cli/connect.ts +++ b/src/cli/connect.ts @@ -24,7 +24,7 @@ export const CONNECT_USAGE = `Usage: ocx connect [--management-url ] (--pairing-code-stdin | --admin-token-stdin) [--clients codex,claude] [--management-transport direct|relay] - [--allow-insecure-http] [--no-sync] + [--no-sync] ocx connect status [--json] ocx connect revoke --admin-token-stdin [--json]`; @@ -127,7 +127,6 @@ async function runConnect(argv: string[], deps: RuntimeApiDeps): Promise { } const pairing = takeFlag(args, "--pairing-code-stdin"); const admin = takeFlag(args, "--admin-token-stdin"); - const allowInsecureHttp = takeFlag(args, "--allow-insecure-http"); const noSync = takeFlag(args, "--no-sync"); if (Number(pairing) + Number(admin) !== 1) { throw new CliUsageError("choose exactly one of --pairing-code-stdin or --admin-token-stdin", CONNECT_USAGE); @@ -141,7 +140,6 @@ async function runConnect(argv: string[], deps: RuntimeApiDeps): Promise { credential: { kind: pairing ? "pairing-grant" : "admin", value }, selectedClients: clients, managementTransport, - allowInsecureHttp, noSync, }, { fetchImpl: deps.fetchImpl }); console.log(`Connected to ${connection.serverUrl} as key ${connection.apiKeyId}.`); diff --git a/src/cli/registry.ts b/src/cli/registry.ts index f09356d4e3..f6e3d76f2b 100644 --- a/src/cli/registry.ts +++ b/src/cli/registry.ts @@ -88,7 +88,7 @@ export const CLI_COMMANDS: CliCommandEntry[] = [ { name: "ensure", usage: "ocx ensure", summary: "Ensure the proxy is running and Codex config/cache are current." }, { name: "connect", - usage: "ocx connect [--management-url ] (--pairing-code-stdin | --admin-token-stdin) [--clients codex,claude] [--management-transport direct|relay] [--allow-insecure-http] [--no-sync]", + usage: "ocx connect [--management-url ] (--pairing-code-stdin | --admin-token-stdin) [--clients codex,claude] [--management-transport direct|relay] [--no-sync]", summary: "Connect this machine to a remote OpenCodex hub without persisting the one-time authority.", details: [ "Status: ocx connect status [--json]", diff --git a/src/client/connect.ts b/src/client/connect.ts index 2bdeea9558..4e915d0c87 100644 --- a/src/client/connect.ts +++ b/src/client/connect.ts @@ -54,7 +54,6 @@ export interface ConnectOptions { selectedClients: OcxConnectedClientId[]; managementTransport: "direct" | "relay"; noSync?: boolean; - allowInsecureHttp?: boolean; } export interface ClientConnectDeps { @@ -107,10 +106,17 @@ function validLocalCatalog(): string { return snapshot.body; } -function catalogMatchesEtag(body: string, etag: string | undefined): boolean { - if (!etag) return false; - const digest = createHash("sha256").update(body).digest("base64url"); - return etag === `"sha256-${digest}"` || etag === `W/"sha256-${digest}"`; +/** + * Is the on-disk catalog still the one this connection wrote? + * + * Recorded as our own hash rather than the hub's ETag: /v1/catalog emits no validator + * (Phase 1, D2), so there is no server-supplied tag to keep. This is an ownership check on + * local bytes, which never needed the hub's participation — the previous spelling only + * looked like a cache concern because it reused the ETag string. + */ +function catalogMatchesFingerprint(body: string, fingerprint: string | undefined): boolean { + if (!fingerprint) return false; + return createHash("sha256").update(body).digest("base64url") === fingerprint; } function routingTarget(serverUrl: string): CodexRoutingTarget { @@ -183,16 +189,13 @@ export async function connectClient( const ready = await fetchHubReady(serverUrl, { fetchImpl: deps.fetchImpl }); if (ready.status !== "ready") throw new Error(`hub is not ready (${ready.status})`); managementUrl = managementUrl || ready.metadata.managementUrl; - if (options.managementTransport === "relay") { - throw new Error("relay management transport is not available before Remote Hub Phase 4"); - } if (options.credential.kind === "pairing-grant") { const session = await exchangeConnectPairingGrant( managementUrl, localGuiOrigin(), options.credential.value, - { allowInsecureHttp: options.allowInsecureHttp, fetchImpl: deps.fetchImpl }, + { fetchImpl: deps.fetchImpl }, ); cleanupCredential = { kind: "gui-session", value: session }; } else { @@ -205,9 +208,6 @@ export async function connectClient( tokenFingerprint = persisted.fingerprint; const catalog = await downloadClientCatalog(serverUrl, issued.key, { fetchImpl: deps.fetchImpl }); - if (catalog.kind !== "fresh" || !catalog.etag) { - throw new Error("initial hub catalog did not include a fresh ETag"); - } atomicWriteFile(DEFAULT_CATALOG_PATH, catalog.body); writtenCatalogFingerprint = sha256(catalog.body); @@ -244,9 +244,9 @@ export async function connectClient( tokenFingerprint: persisted.fingerprint, protocolVersion: 1, connectedAt: now, - catalogEtag: catalog.etag, + catalogFingerprint: createHash("sha256").update(catalog.body).digest("base64url"), // Durable so disconnect — a different process — can put back whatever was here - // before. `priorCatalog` above is only reachable by a connect that fails and rolls + // before. The in-memory `priorCatalog` only covers a connect that fails and rolls // back in the same run. priorCatalog: priorCatalog.kind === "file" ? Buffer.from(priorCatalog.body, "utf8").toString("base64") : "", catalogSyncedAt: now, @@ -305,22 +305,17 @@ export async function syncConnectedClient( let next = state.value; try { const downloaded = await downloadClientCatalog(state.value.serverUrl, token.token, { - etag: state.value.catalogEtag, fetchImpl: deps.fetchImpl, }); - if (downloaded.kind === "fresh") { - atomicWriteFile(DEFAULT_CATALOG_PATH, downloaded.body); - catalogWritten = true; - const now = (deps.now ?? (() => new Date()))().toISOString(); - next = { - ...state.value, - ...(downloaded.etag ? { catalogEtag: downloaded.etag } : {}), - catalogSyncedAt: now, - }; - commitClientConnection(next); - } else { - validLocalCatalog(); - } + atomicWriteFile(DEFAULT_CATALOG_PATH, downloaded.body); + catalogWritten = true; + const now = (deps.now ?? (() => new Date()))().toISOString(); + next = { + ...state.value, + catalogFingerprint: createHash("sha256").update(downloaded.body).digest("base64url"), + catalogSyncedAt: now, + }; + commitClientConnection(next); } catch (error) { const transient = error instanceof HubClientError && (error.code === "unreachable" || (error.status !== undefined && error.status >= 500)); @@ -359,7 +354,7 @@ function restorePriorCatalog(connection: OcxClientConnectionConfig): "removed" | if (!existsSync(DEFAULT_CATALOG_PATH)) return "absent"; try { const body = validLocalCatalog(); - if (!catalogMatchesEtag(body, connection.catalogEtag)) return "changed"; + if (!catalogMatchesFingerprint(body, connection.catalogFingerprint)) return "changed"; if (connection.priorCatalog) { atomicWriteFile(DEFAULT_CATALOG_PATH, Buffer.from(connection.priorCatalog, "base64").toString("utf8")); return "restored"; diff --git a/src/client/hub-client.ts b/src/client/hub-client.ts index b60126476f..9fcb94be6d 100644 --- a/src/client/hub-client.ts +++ b/src/client/hub-client.ts @@ -1,4 +1,24 @@ import { MAX_REMOTE_CATALOG_BYTES } from "../server/catalog-download"; + +/** + * A pairing grant may cross loopback or authenticated HTTPS, and nothing else. + * + * Mirrors the hub-side rule in src/server/gui-session.ts. Checking here too is not + * redundant: it keeps the client from spending a single-use code on a request the hub is + * certain to refuse. + */ +function isPairingTransportPermitted(origin: string): boolean { + let url: URL; + try { + url = new URL(origin); + } catch { + return false; + } + if (url.protocol === "https:") return true; + if (url.protocol !== "http:") return false; + const host = url.hostname.toLowerCase(); + return host === "localhost" || host === "127.0.0.1" || host === "[::1]" || host === "::1"; +} import { checkRemoteProtocolCompatibility, parseRemoteReadyMetadata, @@ -169,12 +189,17 @@ export async function exchangeConnectPairingGrant( managementUrl: string, browserOrigin: string, grant: Uint8Array, - options: { allowInsecureHttp?: boolean; timeoutMs?: number; fetchImpl?: typeof fetch } = {}, + options: { timeoutMs?: number; fetchImpl?: typeof fetch } = {}, ): Promise { const origin = normalizeHubOrigin(managementUrl); const browser = normalizeHubOrigin(browserOrigin); - if (new URL(origin).protocol !== "https:" && options.allowInsecureHttp !== true) { - throw new HubClientError("insecure_http_refused", "Pairing over HTTP requires --allow-insecure-http"); + // No opt-in. An earlier revision let `--allow-insecure-http` carry a grant over plaintext + // when the hub also opted in, on the theory that requiring both sides made it deliberate. + // Deliberateness is not the control that matters: the grant is readable by anything on the + // path and the session it mints is reusable. The hub refuses this exchange outright now, so + // sending it would only burn a single-use code against a certain rejection. + if (!isPairingTransportPermitted(origin)) { + throw new HubClientError("insecure_http_refused", "Pairing requires loopback or HTTPS; plaintext HTTP cannot carry a grant"); } const response = await fetchBounded(options.fetchImpl ?? fetch, `${origin}/opencodex-session`, { method: "POST", @@ -277,16 +302,20 @@ export async function revokeClientKey( export async function downloadClientCatalog( serverUrl: string, admissionToken: string, - options: { etag?: string; timeoutMs?: number; maxBytes?: number; fetchImpl?: typeof fetch } = {}, -): Promise<{ kind: "fresh"; body: string; etag?: string } | { kind: "not-modified" }> { + options: { timeoutMs?: number; maxBytes?: number; fetchImpl?: typeof fetch } = {}, +): Promise<{ kind: "fresh"; body: string }> { const origin = normalizeHubOrigin(serverUrl); const headers = new Headers({ Accept: "application/json", "x-opencodex-api-key": admissionToken }); - if (options.etag) headers.set("If-None-Match", options.etag); + // Unconditional by contract: /v1/catalog emits no validator (Phase 1, D2) because its + // body varies by key identity, so there is nothing to revalidate against and a 304 could + // only come from a hub that is misconfigured or being impersonated. const response = await fetchBounded(options.fetchImpl ?? fetch, `${origin}/v1/catalog`, { method: "GET", headers, }, options.timeoutMs); - if (response.status === 304) return { kind: "not-modified" }; + if (response.status === 304) { + throw new HubClientError("catalog_unexpected_304", "Hub answered 304 to an unconditional catalog request", 304); + } if (!response.ok) { const code = response.status === 401 ? "catalog_unauthorized" : `catalog_http_${response.status}`; throw new HubClientError(code, `Hub catalog request failed (${response.status})`, response.status); @@ -296,6 +325,5 @@ export async function downloadClientCatalog( if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) { throw new HubClientError("catalog_invalid", "Hub catalog response was invalid", response.status); } - const etag = response.headers.get("etag")?.trim() || undefined; - return { kind: "fresh", body, ...(etag ? { etag } : {}) }; + return { kind: "fresh", body }; } diff --git a/src/client/runtime.ts b/src/client/runtime.ts index 03f0be2cba..f8eb85920a 100644 --- a/src/client/runtime.ts +++ b/src/client/runtime.ts @@ -24,7 +24,24 @@ export function scheduleStandaloneRecycle(): void { const port = activePort; try { activeServer?.stop(true); } catch { /* best effort */ } cleanup(); - if (process.env.OCX_SERVICE !== "1" && port) { + // Recycling back to standalone after `ocx disconnect` must actually bring a standalone + // proxy back, under either launch shape. + // + // Unsupervised: spawn the replacement ourselves and exit 0. + // + // Supervised (`OCX_SERVICE=1`): do NOT spawn — the supervisor owns the process, and a + // second copy would fight it for the port. But exit 0 does not work either: the real + // supervisor configs are failure-only (systemd `Restart=on-failure`, WinSW + // ``, the Task Scheduler ERRORLEVEL loop), so a clean exit + // reads as "the service finished" and nothing restarts. The client stayed down until the + // operator noticed. Exit 1 is what those configs are watching for, and it is the same + // policy the dashboard recycle already uses (src/server/management/system-restart.ts). + // + // launchd's KeepAlive restarts on any exit, so it is correct under both branches. + if (process.env.OCX_SERVICE === "1") { + process.exit(1); + } + if (port) { const child = spawn(process.execPath, selfLaunchArgv(["start", "--port", String(port)]), { detached: true, stdio: "ignore", diff --git a/src/config.ts b/src/config.ts index 0755124640..9173c4b353 100644 --- a/src/config.ts +++ b/src/config.ts @@ -935,7 +935,7 @@ const clientConnectionSchema = z.object({ tokenFingerprint: z.string().regex(/^[a-f0-9]{64}$/), protocolVersion: z.literal(1), connectedAt: clientTimestampSchema, - catalogEtag: z.string().min(1).max(512).optional(), + catalogFingerprint: z.string().min(1).max(512).optional(), // base64 of the pre-connect catalog, or "" for "there was none". Bounded above the // catalog size cap so a legitimate snapshot round-trips. priorCatalog: z.string().max(64 * 1024 * 1024).optional(), diff --git a/src/types/config.ts b/src/types/config.ts index 0c867abfa8..7a4524b0fa 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -280,7 +280,14 @@ export interface OcxClientConnectionConfig { tokenFingerprint: string; protocolVersion: 1; connectedAt: string; - catalogEtag?: string; + /** + * sha256/base64url of the catalog bytes this connection wrote, used to tell "still ours" + * from "edited or replaced" before removing the file on disconnect. + * + * Our own hash rather than the hub's ETag: /v1/catalog emits no validator, and this was + * always an ownership check on local bytes rather than a cache concern. + */ + catalogFingerprint?: string; /** * The catalog that was on disk before connect overwrote it, base64-encoded, or the * empty string when there was none. diff --git a/tests/cli-headless-parity.test.ts b/tests/cli-headless-parity.test.ts index f16742c3ea..2c2b6bd1bf 100644 --- a/tests/cli-headless-parity.test.ts +++ b/tests/cli-headless-parity.test.ts @@ -260,6 +260,13 @@ describe("headless GUI parity CLI", () => { ["/api/logs", "ocx observe"], ["/api/lab", "ocx lab"], ["/api/config", "ocx config"], + // The client machine plane. These are served by the connected client's own loopback + // listener rather than the hub, and each one mirrors a connect-family command: + // status/clients -> `ocx connect status`, sync -> `ocx sync`, shim -> the client + // integration commands, disconnect -> `ocx disconnect`. hub-relay is the fixed-target + // relay those same commands use to reach the hub, so it has no separate CLI verb of + // its own — it is the transport selected by `--management-transport relay`. + ["/api/machine", "ocx connect/disconnect/sync"], // The prompt composer is a GUI-first surface: it reads Codex's own layer // inventory and writes one config key. There is no headless equivalent // today, and claiming one would be worse than saying so here. diff --git a/tests/client-connect.test.ts b/tests/client-connect.test.ts index 5d7edbb0f1..00d236c71c 100644 --- a/tests/client-connect.test.ts +++ b/tests/client-connect.test.ts @@ -103,24 +103,34 @@ describe("remote hub client boundary", () => { expect(seen[1]?.body).toBe(JSON.stringify({ name: "client" })); }); - test("pairing HTTP requires explicit client opt-in and catalog is bounded/conditional", async () => { + test("plaintext HTTP cannot carry a pairing grant, with no opt-in and no request sent", async () => { + // An earlier revision accepted `--allow-insecure-http` here and this test asserted the + // opt-in message. The option is gone: the hub refuses the exchange outright, so sending + // it would only burn a single-use code against a certain rejection. let calls = 0; await expect(exchangeConnectPairingGrant( "http://hub.example.test", "http://localhost:10100", new TextEncoder().encode(`ocx_pair_${"c".repeat(43)}`), { fetchImpl: async () => { calls += 1; return new Response(); } }, - )).rejects.toThrow("--allow-insecure-http"); + )).rejects.toThrow("loopback or HTTPS"); + // Refused before any request: the grant is still spendable over a permitted transport. expect(calls).toBe(0); + }); - const notModified = await downloadClientCatalog("https://hub.example.test", "ocx_data_test", { - etag: '"etag"', + test("the catalog fetch is unconditional and still bounded", async () => { + // /v1/catalog emits no validator (Phase 1, D2), so the client sends no If-None-Match and + // has no 304 branch to keep correct. The size bound is unaffected by that change. + let sentConditional: string | null = null; + const fresh = await downloadClientCatalog("https://hub.example.test", "ocx_data_test", { fetchImpl: async (_input, init) => { - expect(new Headers(init?.headers).get("if-none-match")).toBe('"etag"'); - return new Response(null, { status: 304 }); + sentConditional = new Headers(init?.headers).get("if-none-match"); + return new Response('{"models":[]}'); }, }); - expect(notModified).toEqual({ kind: "not-modified" }); + expect(sentConditional).toBeNull(); + expect(fresh).toMatchObject({ kind: "fresh" }); + await expect(downloadClientCatalog("https://hub.example.test", "ocx_data_test", { maxBytes: 4, fetchImpl: async () => new Response('{"models":[]}'), @@ -302,7 +312,7 @@ function runConnectedStateScenario(mode: "sync-401" | "sync-503" | "disconnect-c const token = `ocx_data_${"e".repeat(40)}`; const fingerprint = createHash("sha256").update(token).digest("hex"); const catalog = '{"models":[]}'; - const etag = `"sha256-${createHash("sha256").update(catalog).digest("base64url")}"`; + const catalogFingerprint = createHash("sha256").update(catalog).digest("base64url"); const isDisconnect = mode === "disconnect-conflict" || mode === "disconnect-process-journal"; const selectedClients = isDisconnect ? ["codex"] : ["claude"]; writeFileSync(join(opencodexHome, "config.json"), JSON.stringify({ @@ -320,7 +330,7 @@ function runConnectedStateScenario(mode: "sync-401" | "sync-503" | "disconnect-c tokenFingerprint: fingerprint, protocolVersion: 1, connectedAt: "2026-08-28T00:00:00.000Z", - catalogEtag: etag, + catalogFingerprint, catalogSyncedAt: "2026-08-28T00:00:00.000Z", }, }), "utf8"); diff --git a/tests/config.test.ts b/tests/config.test.ts index 5cb35391be..e13cd43df8 100644 --- a/tests/config.test.ts +++ b/tests/config.test.ts @@ -287,7 +287,7 @@ describe("opencodex config defaults", () => { tokenFingerprint: "a".repeat(64), protocolVersion: 1 as const, connectedAt: "2026-08-28T00:00:00.000Z", - catalogEtag: '"sha256-example"', + catalogFingerprint: "sha256-example", catalogSyncedAt: "2026-08-28T00:01:00.000Z", pendingOperation: { kind: "rotate" as const, From 0a73858a1d12b556e8bb89cc10c87bedf60a7893 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 04:40:59 +0900 Subject: [PATCH 069/172] fix(connect): seed default config on a fresh machine instead of refusing the commit (dogfood) --- src/client/state.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/client/state.ts b/src/client/state.ts index a28ff01fc5..e702a30f8b 100644 --- a/src/client/state.ts +++ b/src/client/state.ts @@ -2,8 +2,10 @@ import { readFileSync } from "node:fs"; import { getConfigPath, deleteConfigTopLevelKey, + getDefaultConfig, mutatePersistedConfig, readConfigDiagnostics, + saveConfig, } from "../config"; import type { OcxClientConnectionConfig } from "../types"; @@ -71,6 +73,18 @@ export function commitClientConnection( return { changed: !unchanged, value: undefined }; }); if (outcome.status === "committed" || outcome.status === "unchanged") return outcome.status; + if (outcome.status === "unavailable" && outcome.reason === "missing") { + // First ocx run on a fresh machine: ocx connect is the expected first command in + // client mode, so there is no config.json yet. mutatePersistedConfig correctly + // refuses to invent one (a lost config must fail closed), but a genuinely absent + // file is the bootstrap case, not corruption — seed defaults plus the client + // block atomically. Found on the first MacBook↔oracle dogfood connect. + const seeded = getDefaultConfig(); + seeded.runtimeRole = "client"; + seeded.client = structuredClone(state); + saveConfig(seeded); + return "committed"; + } throw new Error(`client state commit unavailable: ${"reason" in outcome ? outcome.reason : "unknown"}`); } From 16ddd3eb5fb45251efeb6781b2ace4456aca208c Mon Sep 17 00:00:00 2001 From: jun Date: Tue, 1 Sep 2026 18:09:03 +0900 Subject: [PATCH 070/172] fix(two-plane): authenticate the relayed pairing exchange and isolate its test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The relayed pairing request went out unauthenticated. submitConnectPairing took `fetchImpl: typeof fetch = fetch`, and a default parameter binds the global as it was when the module was evaluated — the unwrapped original, not the wrapper installApiAuthFetch puts on window.fetch. The relay needs the machine-session headers that wrapper attaches, so the hub refused the exchange. Resolved at call time now. The transport also moves to its own module. One file exported both a transport function and a component, which react-refresh/only-export-components flags for good reason; the previous shape carried an eslint-disable instead. The two have no reason to share a file: the transport is testable without React and the form has no logic beyond calling it. tests/connect-pairing.test.ts passed alone and failed in the full GUI run. App calls installApiAuthFetch() at module scope, so it runs on first import only; a later test importing App gets the cached module and no install, leaving the wrapper bound to whichever window imported it first. The test now binds the wrapper to its own window before mounting, and claude-toggle-race.test.tsx clears the install latch in afterEach alongside the window it closes. Both are test isolation rather than product behavior. --- gui/src/connect-pairing-transport.ts | 38 +++++++++++++++++++++++++++ gui/src/connect-pairing.ts | 23 +--------------- gui/tests/claude-toggle-race.test.tsx | 8 ++++++ gui/tests/connect-pairing.test.ts | 13 +++++++++ 4 files changed, 60 insertions(+), 22 deletions(-) create mode 100644 gui/src/connect-pairing-transport.ts diff --git a/gui/src/connect-pairing-transport.ts b/gui/src/connect-pairing-transport.ts new file mode 100644 index 0000000000..fc82035085 --- /dev/null +++ b/gui/src/connect-pairing-transport.ts @@ -0,0 +1,38 @@ +import { installApiSessionFromHtml } from "./api"; +import type { ApiTarget } from "./api-targets"; + +const PAIRING_CODE = /^ocx_pair_[A-Za-z0-9_-]{43}$/; + +/** + * Exchange a pairing code for a shared-plane session. + * + * Separate module from the form that calls it so neither file mixes a component export with + * a plain one. That mix is what `react-refresh/only-export-components` flags, and the two + * have no reason to share a file: the transport is testable without React and the form has + * no logic beyond calling it. + */ +export async function submitConnectPairing( + target: ApiTarget, + grant: string, + fetchImpl?: typeof fetch, +): Promise { + const code = grant.trim(); + if (!PAIRING_CODE.test(code)) throw new Error("pairing_code_invalid"); + // Resolved at CALL time, not as a default parameter. + // + // `installApiAuthFetch` replaces `window.fetch` with the wrapper that attaches plane + // credentials — including the machine-session headers a relayed exchange needs to reach + // the hub. A default of `fetch` binds whatever the global was when this module was + // evaluated, which on the relay path is the unwrapped original, so the request went out + // unauthenticated and the relay refused it. + const send = fetchImpl ?? ((input, init) => window.fetch(input, init)); + const response = await send(target.bootstrapPath, { + method: "POST", + headers: { "Content-Type": "application/json", Accept: "text/html" }, + body: JSON.stringify({ grant: code }), + }); + if (!response.ok) throw new Error("pairing_refused"); + const html = await response.text(); + if (!installApiSessionFromHtml("shared", html)) throw new Error("pairing_response_invalid"); + return true; +} diff --git a/gui/src/connect-pairing.ts b/gui/src/connect-pairing.ts index 6c1aa5520a..00e48abd7a 100644 --- a/gui/src/connect-pairing.ts +++ b/gui/src/connect-pairing.ts @@ -1,28 +1,7 @@ -/* eslint-disable react-refresh/only-export-components -- pairing transport and its form share one session-install boundary */ import { createElement, useState, type ChangeEvent, type FormEvent } from "react"; -import { installApiSessionFromHtml } from "./api"; import type { ApiTarget } from "./api-targets"; import { useT } from "./i18n/shared"; - -const PAIRING_CODE = /^ocx_pair_[A-Za-z0-9_-]{43}$/; - -export async function submitConnectPairing( - target: ApiTarget, - grant: string, - fetchImpl: typeof fetch = fetch, -): Promise { - const code = grant.trim(); - if (!PAIRING_CODE.test(code)) throw new Error("pairing_code_invalid"); - const response = await fetchImpl(target.bootstrapPath, { - method: "POST", - headers: { "Content-Type": "application/json", Accept: "text/html" }, - body: JSON.stringify({ grant: code }), - }); - if (!response.ok) throw new Error("pairing_refused"); - const html = await response.text(); - if (!installApiSessionFromHtml("shared", html)) throw new Error("pairing_response_invalid"); - return true; -} +import { submitConnectPairing } from "./connect-pairing-transport"; export function ConnectPairingForm({ target, diff --git a/gui/tests/claude-toggle-race.test.tsx b/gui/tests/claude-toggle-race.test.tsx index f1b74cd75c..91bd6d824f 100644 --- a/gui/tests/claude-toggle-race.test.tsx +++ b/gui/tests/claude-toggle-race.test.tsx @@ -127,6 +127,14 @@ afterEach(async () => { releasePut = null; putGate = null; testWindow.close(); + // Clear the auth-fetch install latch along with the window it was installed against. + // + // `installApiAuthFetch` installs once per module instance. Leaving the latch set after + // this window closes makes a LATER test's own install a silent no-op, so its requests go + // out unwrapped and it fails only when run after this file. Restoring the globals is not + // enough; the latch lives in the module. + const { resetApiAuthFetchForTests } = await import("../src/api"); + resetApiAuthFetchForTests(); for (const key of globals) { Object.defineProperty(globalThis, key, { configurable: true, value: previousGlobals[key] }); } diff --git a/gui/tests/connect-pairing.test.ts b/gui/tests/connect-pairing.test.ts index 07c79b020e..4a3f2e22ef 100644 --- a/gui/tests/connect-pairing.test.ts +++ b/gui/tests/connect-pairing.test.ts @@ -66,6 +66,19 @@ test("App mounts the relay pairing form and installs only the returned shared se const container = document.createElement("div"); document.body.append(container); const { LanguageProvider } = await import("../src/i18n/provider"); + // Bind the auth-fetch wrapper to THIS window before App mounts. + // + // App calls installApiAuthFetch() at module scope, so it runs on first import only. A + // later test importing App gets the cached module and no install, leaving the wrapper + // bound to whichever window imported it first. The relayed pairing request then goes out + // unwrapped — no machine-session headers, which is exactly what this test asserts. + // Standalone the ordering happens to work; in the full suite it does not. Re-binding here + // makes the test independent of import order rather than of any product behavior. + const { resetApiAuthFetchForTests, installApiAuthFetch, configureApiTargets } = await import("../src/api"); + const { standaloneApiTargets } = await import("../src/api-targets"); + resetApiAuthFetchForTests(); + configureApiTargets(standaloneApiTargets("")); + installApiAuthFetch(); const { default: App } = await import("../src/App"); Object.defineProperty(globalThis, "fetch", { configurable: true, value: win.fetch }); const { createRoot } = await import("react-dom/client"); From 7bf28233df456561bc8e8e6c1f0a5316b1df769b Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 04:42:17 +0900 Subject: [PATCH 071/172] =?UTF-8?q?docs(devlog):=20clisu-oracle=20dogfood?= =?UTF-8?q?=20record=20=E2=80=94=20full=20connect=20lifecycle=20proven=20l?= =?UTF-8?q?ive?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../260827_remote_hub/090_dogfood_record.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 devlog/_plan/260827_remote_hub/090_dogfood_record.md diff --git a/devlog/_plan/260827_remote_hub/090_dogfood_record.md b/devlog/_plan/260827_remote_hub/090_dogfood_record.md new file mode 100644 index 0000000000..162ebecc02 --- /dev/null +++ b/devlog/_plan/260827_remote_hub/090_dogfood_record.md @@ -0,0 +1,34 @@ +# 090 — Dogfood record: clisu-oracle hub + MacBook client (2026-08-28) + +Branch build @ f98081fbf. Hub: clisu-oracle (aarch64), OPENCODEX_HOME=~/.opencodex-hub, +bind 100.100.245.81:10190, data token file-fed, remoteGui.allowInsecureHttp=true, +hub.managementPublicOrigin=http://100.100.245.81:10190, corsAllowOrigins += http://localhost:10100. +Client: this MacBook, isolated OPENCODEX_HOME/CODEX_HOME under /tmp/ocx-dogfood-SzfA +(real user config untouched; the temp grok rewrite from the earlier standalone probe was +reverted to :10100). + +Proven end-to-end (commands + outputs in session log): +1. /readyz over tailnet: status ready, protocol 1, managementUrl advertised. +2. /v1/catalog over tailnet: 401 without token; 200 + strong ETag + Cache-Control + private,no-cache with the data token (516 KB). +3. Admin token over plain HTTP refused by connect ("Admin credentials may be sent only + over HTTPS") — HTTPS-only admin rule enforced live. +4. ocx gui pair --origin http://localhost:10100 issued a single-use grant (json shape). +5. ocx connect --pairing-code-stdin --allow-insecure-http --clients codex: + full transaction — grant exchanged, per-client key 085da5fb… auto-issued, key stored + ONLY in service-api-token (0600, 50 bytes), catalog placed atomically (262 KB), + dedicated provider block injected (base_url hub, env_key contract, absolute + model_catalog_json), client state committed with apiKeyId. +6. Real routed completion through the hub with the per-client key: gpt-5.6-luna answered + "HUB_OK" (chat.completions 200). +7. Usage attribution on the hub: the request row carries apiKeyId 085da5fb…, + admissionKind configured — per-machine slice works. +8. ocx disconnect: injected config restored byte-identically to the seeded original, + token file deleted, client state cleared, reminder to revoke the still-valid key via + hub GUI (by design — operator-owned revocation). + +Three live defects found and fixed during dogfood (each with a regression test): +- 596bb02f3 runtimeRole=hub refused ocx start (state read). +- 19eb6a4bd hub role ran local client syncs on start (readyz failed + grok rewrite). +- f98081fbf connect refused to commit on a fresh machine with no config.json. + From 95639f028bc6f462118d734e2e3d1cfd5fbc634e Mon Sep 17 00:00:00 2001 From: jun Date: Tue, 1 Sep 2026 19:36:58 +0900 Subject: [PATCH 072/172] fix(two-plane): a standalone install neither probes nor announces the machine plane MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three things a user who never enabled remote hub was paying for. Every dashboard load fired GET /api/machine/status. Discovery ran unconditionally and inferred standalone FROM the resulting 404, so the browser announced the feature's existence on every paint of a plain install. The server already injects session meta into the served document, so it now states the runtime role there too and the client reads it instead of asking. A missing tag reads as standalone, which covers an older server, a separately hosted GUI, and the Vite dev server — all of which should make no remote-hub request. The role meta is emitted independently of the session block. A standalone install never issues a GUI session, so tying the role to session issuance would have left exactly the case that needs it with nothing to read. The page body was gated behind targetsSettled, so a standalone user saw "Discovering local and shared targets…" before their own dashboard. Standalone now starts settled: there is nothing to discover, so there is nothing to wait for. A failed discovery replaced the entire body with a machine-plane error. A slow or restarting proxy cost a standalone user their dashboard over a plane they never turned on. It is a banner now; the requests that actually need the machine plane still report their own failures. Regressions are driven red against the previous behavior: standalone discovery makes zero fetches across null/standalone/hub roles, and a client role still discovers, so the tag narrows who asks rather than removing discovery. --- gui/src/App.tsx | 18 +++++++++--- gui/src/api-targets.ts | 33 +++++++++++++++++++++ gui/tests/api-targets.test.ts | 48 +++++++++++++++++++++++++++++++ gui/tests/connect-pairing.test.ts | 4 +++ src/server/gui-static.ts | 26 +++++++++++++---- src/server/index.ts | 7 ++++- 6 files changed, 126 insertions(+), 10 deletions(-) diff --git a/gui/src/App.tsx b/gui/src/App.tsx index 014aec7e46..d6167ab2cc 100644 --- a/gui/src/App.tsx +++ b/gui/src/App.tsx @@ -16,7 +16,7 @@ import { IconGrid, IconServer, IconBoxes, IconBot, IconList, IconActivity, IconH import { useI18n, useT, LOCALES, localeDisplayName, type Locale, type TKey } from "./i18n/shared"; import { Select } from "./ui"; import { configureApiTargets, hasApiSession, installApiAuthFetch, installApiSessionFromHtml } from "./api"; -import { apiBaseForPlane, discoverApiTargets, standaloneApiTargets, type ApiTargets } from "./api-targets"; +import { apiBaseForPlane, discoverApiTargets, isConnectedRuntime, standaloneApiTargets, type ApiTargets } from "./api-targets"; import { ConnectPairingForm } from "./connect-pairing"; import { type Page } from "./app-routing"; import { readModelsTab, type ModelsTab } from "./pages/models-tab"; @@ -105,7 +105,10 @@ export default function App() { const { locale, setLocale } = useI18n(); const t = useT(); const [targets, setTargets] = useState(INITIAL_TARGETS); - const [targetsSettled, setTargetsSettled] = useState(false); + // Standalone starts settled: there is nothing to discover, so nothing to wait for. + // Gating the page on discovery made a plain install show remote-hub loading copy before + // its own dashboard, for a feature the operator never enabled. + const [targetsSettled, setTargetsSettled] = useState(() => !isConnectedRuntime()); const [targetError, setTargetError] = useState(false); const [sharedSessionReady, setSharedSessionReady] = useState(() => hasApiSession("shared")); @@ -366,10 +369,17 @@ export default function App() { > {!targetsSettled ? (
{t("connection.discovering")}
- ) : targetError ? ( -
{t("connection.machineUnavailable")}
) : ( <> + {/* + A failed discovery is a banner, not a replacement. It used to take over the + whole body, so a slow or restarting proxy cost a standalone user their + dashboard over a plane they never turned on. The requests that actually + need the machine plane report their own errors. + */} + {targetError && ( +
{t("connection.machineUnavailable")}
+ )} {targets.connected && !sharedSessionReady && ( setSharedSessionReady(true)} /> )} diff --git a/gui/src/api-targets.ts b/gui/src/api-targets.ts index 000a32a058..7a1a1d17d6 100644 --- a/gui/src/api-targets.ts +++ b/gui/src/api-targets.ts @@ -1,6 +1,29 @@ export type ApiPlane = "machine" | "shared"; export type SharedTransport = "same-origin" | "direct" | "relay"; +/** + * The runtime role the server stated in the served document, or null when it said nothing. + * + * Read without removing the tag: unlike the session meta, which is consumed once so a + * credential does not linger in the DOM, the role is non-secret and may be read again. + */ +function runtimeRoleFromDocument(): string | null { + if (typeof document === "undefined") return null; + const meta = document.querySelector('meta[name="opencodex-runtime-role"]'); + return meta?.getAttribute("content")?.trim() || null; +} + +/** + * Did the server say this proxy is running as a connected client? + * + * Anything else — standalone, hub, an older server that sends no tag, a separately hosted + * GUI, the Vite dev server — is treated as "not connected", which is the state that needs + * no remote-hub work and makes no remote-hub requests. + */ +export function isConnectedRuntime(): boolean { + return runtimeRoleFromDocument() === "client"; +} + export interface ApiTarget { id: ApiPlane; baseUrl: string; @@ -117,6 +140,16 @@ export function apiBaseForPlane(plane: ApiPlane, targets: ApiTargets): string { export async function discoverApiTargets(initialBase: string, signal?: AbortSignal): Promise { const standalone = standaloneApiTargets(initialBase); + // Standalone asks nothing. + // + // The server states the role in the served document, so a user who never enabled remote + // hub makes no request to a remote-hub endpoint — not even one that 404s. Discovery used + // to run unconditionally and infer standalone FROM that 404, which meant every dashboard + // load probed a feature the operator had not turned on. + // + // A missing tag means standalone too: an older server, a separately hosted GUI, or the + // Vite dev server all read as "no remote topology", which is the safe default. + if (runtimeRoleFromDocument() !== "client") return standalone; let response: Response; try { response = await fetch(`${standalone.machine.baseUrl}/api/machine/status`, { signal, cache: "no-store" }); diff --git a/gui/tests/api-targets.test.ts b/gui/tests/api-targets.test.ts index b3f9f33e74..3aacf344b8 100644 --- a/gui/tests/api-targets.test.ts +++ b/gui/tests/api-targets.test.ts @@ -11,8 +11,23 @@ import { let win: Window; let previousWindow: unknown; +let previousDocument: unknown; let previousFetch: typeof fetch; +/** + * Stand in for the runtime-role meta tag the server injects into the served document. + * `null` means the server said nothing, which every reader must treat as standalone. + */ +function setRuntimeRole(role: string | null): void { + const existing = win.document.querySelector('meta[name="opencodex-runtime-role"]'); + existing?.remove(); + if (role === null) return; + const meta = win.document.createElement("meta"); + meta.setAttribute("name", "opencodex-runtime-role"); + meta.setAttribute("content", role); + win.document.head.append(meta); +} + const status = (transport: "direct" | "relay"): MachineStatusV1 => ({ mode: "client", connected: true, @@ -28,14 +43,19 @@ const status = (transport: "direct" | "relay"): MachineStatusV1 => ({ beforeEach(() => { previousWindow = Reflect.get(globalThis, "window"); + previousDocument = Reflect.get(globalThis, "document"); previousFetch = globalThis.fetch; win = new Window({ url: "http://localhost/" }); Object.defineProperty(globalThis, "window", { configurable: true, value: win }); + Object.defineProperty(globalThis, "document", { configurable: true, value: win.document }); + // Most rows here exercise the connected path; the standalone rows set their own role. + setRuntimeRole("client"); }); afterEach(() => { globalThis.fetch = previousFetch; Object.defineProperty(globalThis, "window", { configurable: true, value: previousWindow }); + Object.defineProperty(globalThis, "document", { configurable: true, value: previousDocument }); win.close(); }); @@ -60,7 +80,35 @@ describe("two-plane API targets", () => { }); test("a machine-status network failure is not treated as standalone", async () => { + setRuntimeRole("client"); globalThis.fetch = (async () => { throw new TypeError("offline"); }) as typeof fetch; await expect(discoverApiTargets("")).rejects.toThrow("local machine plane unavailable"); }); + + test("standalone discovers nothing and sends no request", async () => { + // The whole point of the runtime-role meta tag: a user who never enabled remote hub + // must not have their browser probe a remote-hub endpoint. Discovery previously ran + // unconditionally and inferred standalone from the resulting 404 — a request that + // announced the feature's existence on every dashboard load. + let calls = 0; + globalThis.fetch = (async () => { calls += 1; return new Response(null, { status: 404 }); }) as typeof fetch; + + for (const role of [null, "standalone", "hub"] as const) { + calls = 0; + setRuntimeRole(role); + const targets = await discoverApiTargets(""); + expect(targets.connected).toBe(false); + expect(targets).toEqual(standaloneApiTargets("")); + expect(calls).toBe(0); + } + }); + + test("a connected runtime still discovers", async () => { + // The tag narrows who asks; it does not remove discovery for the role that needs it. + setRuntimeRole("client"); + let calls = 0; + globalThis.fetch = (async () => { calls += 1; return new Response(null, { status: 404 }); }) as typeof fetch; + await discoverApiTargets(""); + expect(calls).toBe(1); + }); }); diff --git a/gui/tests/connect-pairing.test.ts b/gui/tests/connect-pairing.test.ts index 4a3f2e22ef..ae68a1d1f7 100644 --- a/gui/tests/connect-pairing.test.ts +++ b/gui/tests/connect-pairing.test.ts @@ -22,6 +22,10 @@ test("App mounts the relay pairing form and installs only the returned shared se ["opencodex-session-csrf", "machine-csrf"], ["opencodex-session-origin", "http://localhost"], ["opencodex-session-server-origin", "http://localhost"], + // The server states the role in the served document. Without it this reads as + // standalone, discovery never runs, and the relay pairing form never mounts — which + // is exactly the behavior a plain install should get. + ["opencodex-runtime-role", "client"], ]) { const meta = document.createElement("meta"); meta.name = name; diff --git a/src/server/gui-static.ts b/src/server/gui-static.ts index c93299da3c..3d97ce451d 100644 --- a/src/server/gui-static.ts +++ b/src/server/gui-static.ts @@ -75,6 +75,21 @@ function sessionBootstrapMeta(session: GuiSessionBootstrap): string { ].join(""); } +/** + * Runtime role, emitted on every served document. + * + * Separate from the session block on purpose: the session exists only once a GUI session + * has been issued, but the role has to be known on the very first paint of a plain + * standalone install — which never issues one. Without it the GUI has to ASK, and asking + * means a request to a remote-hub endpoint from a user who never enabled remote hub. + * + * Non-secret: it names which topology this proxy is running, which the operator configured + * and which the dashboard already reflects everywhere else. + */ +function runtimeRoleMeta(runtimeRole: string): string { + return ``; +} + function htmlDocumentResponse(html: string): Response { return new Response(html, { headers: { @@ -86,10 +101,10 @@ function htmlDocumentResponse(html: string): Response { }); } -function htmlResponse(path: string, session?: GuiSessionBootstrap): Response { +function htmlResponse(path: string, session?: GuiSessionBootstrap, runtimeRole?: string): Response { let html = readFileSync(path, "utf8"); - if (session) { - const bootstrap = sessionBootstrapMeta(session); + const bootstrap = `${runtimeRole ? runtimeRoleMeta(runtimeRole) : ""}${session ? sessionBootstrapMeta(session) : ""}`; + if (bootstrap) { html = html.includes("") ? html.replace("", `${bootstrap}`) : `${bootstrap}${html}`; } return htmlDocumentResponse(html); @@ -110,6 +125,7 @@ export function serveGuiFile( pathname: string, guiDist = findGuiDist(), session?: GuiSessionBootstrap, + runtimeRole?: string, ): Response | null { if (!guiDist) return null; const filePath = resolveGuiFilePath(guiDist, pathname); @@ -119,7 +135,7 @@ export function serveGuiFile( if (!extname(pathname)) { const indexPath = join(guiDist, "index.html"); if (isFile(indexPath)) { - return htmlResponse(indexPath, session); + return htmlResponse(indexPath, session, runtimeRole); } } return null; @@ -127,7 +143,7 @@ export function serveGuiFile( const ext = extname(filePath); const contentType = MIME_TYPES[ext] || "application/octet-stream"; - if (ext === ".html") return htmlResponse(filePath, session); + if (ext === ".html") return htmlResponse(filePath, session, runtimeRole); // Snapshot bytes before returning the response. Bun.file is lazy: if gui/dist is replaced // after Bun frames the response but before the stream finishes, its Content-Length can // describe the old file while the body comes from the new one (#2792). diff --git a/src/server/index.ts b/src/server/index.ts index 18ca92c5ea..f96b77b62e 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -1894,7 +1894,12 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server Date: Tue, 1 Sep 2026 17:41:18 +0900 Subject: [PATCH 073/172] test(deploy): assert the ingress role rule where the message is actually reachable tests/loopback-listener-admission.test.ts asserted that every non-hub role is refused with "requires runtimeRole hub", looping over undefined, standalone, and client. The client case is refused earlier, by the separate rule that a client role needs a complete client connection block, so the assertion was testing an ordering that two independent validation rules never promised. Split rather than loosened. undefined and standalone still assert the exact ingress message. client asserts only that it is refused, because that is the guarantee the config actually makes for a role that is incomplete on its own. A second case closes the gap this would otherwise open: with a COMPLETE client connection, so the earlier rule no longer fires, the ingress rule is what refuses. Without it, the weaker client assertion could pass even if the ingress rule stopped applying to that role entirely. Also keeps both sides of two rebase conflicts. This phase inserts a management ingress pairing-exchange test ahead of the plaintext-pairing test that phase 2 rewrote, and structure/01_runtime.md gained a codex-cli-update sentence on dev and a hub-management-listener clause here; both rows carry both. --- tests/loopback-listener-admission.test.ts | 32 ++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/tests/loopback-listener-admission.test.ts b/tests/loopback-listener-admission.test.ts index ca84d78bbc..e45c8bf19f 100644 --- a/tests/loopback-listener-admission.test.ts +++ b/tests/loopback-listener-admission.test.ts @@ -190,11 +190,41 @@ describe("hub management ingress configuration", () => { }); test("enabled ingress requires the hub role", () => { - for (const runtimeRole of [undefined, "standalone", "client"] as const) { + // Every non-hub role is rejected. Only the two roles that are otherwise complete can be + // asserted on THIS message, though: `client` is refused earlier, by the rule that a + // client role needs a full client connection block. Asserting the ingress wording for it + // would be asserting an order these two independent rules do not promise, so the + // requirement checked for `client` is that it is refused at all. + for (const runtimeRole of [undefined, "standalone"] as const) { const result = validateConfigCandidate(candidate({ runtimeRole })); expect(result.ok).toBe(false); if (!result.ok) expect(result.error).toContain("requires runtimeRole hub"); } + + const asClient = validateConfigCandidate(candidate({ runtimeRole: "client" })); + expect(asClient.ok).toBe(false); + }); + + test("a complete client connection still cannot enable hub ingress", () => { + // Proves the row above is not hiding a gap: once the client role IS complete, so the + // earlier rule no longer fires, the ingress rule is what refuses it. + const result = validateConfigCandidate(candidate({ + runtimeRole: "client", + client: { + serverUrl: "https://hub.example.test", + managementUrl: "https://hub.example.test", + managementTransport: "direct", + selectedClients: ["codex"], + tokenEnv: "OPENCODEX_API_AUTH_TOKEN", + apiKeyId: "client-key-1", + tokenFingerprint: "a".repeat(64), + protocolVersion: 1, + connectedAt: "2026-08-28T00:00:00.000Z", + catalogSyncedAt: "2026-08-28T00:00:00.000Z", + }, + })); + expect(result.ok).toBe(false); + if (!result.ok) expect(result.error).toContain("requires runtimeRole hub"); }); test("enabled ingress rejects public and unauthenticated-loopback port collisions", () => { From 25c784f688ef08692dce87a78113f2f12b4f28eb Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 04:47:03 +0900 Subject: [PATCH 074/172] feat(hardening): add recoverable data key rotation API --- src/config.ts | 9 +++ src/server/auth-cors.ts | 4 + src/server/management/api-key-rotation.ts | 74 +++++++++++++++++++ src/server/management/oauth-account-routes.ts | 59 +++++++++++++++ src/types/config.ts | 8 ++ 5 files changed, 154 insertions(+) create mode 100644 src/server/management/api-key-rotation.ts diff --git a/src/config.ts b/src/config.ts index c2d4158032..7d5f36e6a8 100644 --- a/src/config.ts +++ b/src/config.ts @@ -831,6 +831,13 @@ const codexAccountPrioritiesSchema = z.custom>( * it: `system-env.ts` and `cli/claude.ts` hand `apiKeys[0].key` to launched * clients, so a junk first entry would mask a valid later one. */ +const pendingApiKeyRotationSchema = z.object({ + id: z.string().trim().min(1).max(256), + key: z.string().refine(isUsableApiKeySecret), + createdAt: z.string().datetime({ offset: true }), + expiresAt: z.string().datetime({ offset: true }), +}).strict(); + const apiKeyEntrySchema = z.object({ key: z.string().refine(isUsableApiKeySecret), // Degrades to "" here; every schema consumer then runs `normalizeApiKeyIds`, @@ -838,6 +845,8 @@ const apiKeyEntrySchema = z.object({ id: z.string().catch(""), name: z.string().catch(""), createdAt: z.string().catch(""), + // A damaged overlap record must never discard the still-authoritative key. + pendingRotation: pendingApiKeyRotationSchema.optional().catch(undefined), }).passthrough(); /** diff --git a/src/server/auth-cors.ts b/src/server/auth-cors.ts index afc93ee528..82ee28c56a 100644 --- a/src/server/auth-cors.ts +++ b/src/server/auth-cors.ts @@ -374,6 +374,10 @@ export function resolveDataPlaneAdmissionSecret( if (secretEquals(actual, configuredApiAuthToken(config))) return { kind: "environment", source }; for (const k of config.apiKeys ?? []) { if (secretEquals(actual, k.key)) return { kind: "configured", keyId: k.id, source }; + const pending = k.pendingRotation; + if (pending && Date.parse(pending.expiresAt) > Date.now() && secretEquals(actual, pending.key)) { + return { kind: "configured", keyId: k.id, source }; + } } return null; } diff --git a/src/server/management/api-key-rotation.ts b/src/server/management/api-key-rotation.ts new file mode 100644 index 0000000000..982a0c337c --- /dev/null +++ b/src/server/management/api-key-rotation.ts @@ -0,0 +1,74 @@ +import { randomBytes, randomUUID, timingSafeEqual } from "node:crypto"; +import type { OcxConfig } from "../../types"; + +export const API_KEY_ROTATION_TTL_MS = 10 * 60_000; + +export type ApiKeyRotationStart = { + id: string; + name: string; + key: string; + rotationId: string; + expiresAt: string; +}; + +function equalOpaqueId(left: string, right: string): boolean { + const encoder = new TextEncoder(); + const a = encoder.encode(left); + const b = encoder.encode(right); + return a.length === b.length && timingSafeEqual(a, b); +} + +export function removeExpiredApiKeyRotations(config: OcxConfig, now = Date.now()): boolean { + let changed = false; + for (const entry of config.apiKeys ?? []) { + if (entry.pendingRotation && Date.parse(entry.pendingRotation.expiresAt) <= now) { + delete entry.pendingRotation; + changed = true; + } + } + return changed; +} + +export function startApiKeyRotation( + config: OcxConfig, + keyId: string, + now = Date.now(), +): ApiKeyRotationStart | { error: "not-found" | "already-pending" } { + const entry = (config.apiKeys ?? []).find(candidate => candidate.id === keyId); + if (!entry) return { error: "not-found" }; + if (entry.pendingRotation && Date.parse(entry.pendingRotation.expiresAt) > now) { + return { error: "already-pending" }; + } + const key = `ocx_data_${randomBytes(20).toString("hex")}`; + const rotationId = randomUUID(); + const createdAt = new Date(now).toISOString(); + const expiresAt = new Date(now + API_KEY_ROTATION_TTL_MS).toISOString(); + entry.pendingRotation = { id: rotationId, key, createdAt, expiresAt }; + return { id: entry.id, name: entry.name, key, rotationId, expiresAt }; +} + +export function commitApiKeyRotation( + config: OcxConfig, + keyId: string, + rotationId: string, + now = Date.now(), +): { ok: true } | { error: "not-found" | "expired" | "mismatch" } { + const entry = (config.apiKeys ?? []).find(candidate => candidate.id === keyId); + if (!entry?.pendingRotation) return { error: "not-found" }; + const pending = entry.pendingRotation; + if (Date.parse(pending.expiresAt) <= now) { + delete entry.pendingRotation; + return { error: "expired" }; + } + if (!equalOpaqueId(pending.id, rotationId)) return { error: "mismatch" }; + entry.key = pending.key; + delete entry.pendingRotation; + return { ok: true }; +} + +export function abortApiKeyRotation(config: OcxConfig, keyId: string, rotationId: string): boolean { + const entry = (config.apiKeys ?? []).find(candidate => candidate.id === keyId); + if (!entry?.pendingRotation || !equalOpaqueId(entry.pendingRotation.id, rotationId)) return false; + delete entry.pendingRotation; + return true; +} diff --git a/src/server/management/oauth-account-routes.ts b/src/server/management/oauth-account-routes.ts index 5e441ea013..3449d8c853 100644 --- a/src/server/management/oauth-account-routes.ts +++ b/src/server/management/oauth-account-routes.ts @@ -65,6 +65,12 @@ import type { PersistedUsageAttempt } from "../../usage/log"; import { AUTH_MATRIX, isAllowedRequestOrigin, jsonResponse, providerManagementConfigError, publicProviderBaseUrl, safeConfigDTO } from "../auth-cors"; import { applySystemEnvToggle } from "../system-env"; import { buildApiAccessEndpoints } from "./api-access"; +import { + abortApiKeyRotation, + commitApiKeyRotation, + removeExpiredApiKeyRotations, + startApiKeyRotation, +} from "./api-key-rotation"; import { isPlainRecord, parseDebugLogQuery, tokPerSecondResult, unavailableCostReason, costResult, requestLogDto, stripRegistryOnlyStaticHeaders, fetchAllModels } from "./shared"; import type { MetricUnavailableReason, TokPerSecondResult, CostEstimateReason, CostResult, MetricSource } from "./shared"; @@ -579,6 +585,10 @@ export async function handleOauthAccountRoutes(ctx: ManagementContext): Promise< // API Keys management // --------------------------------------------------------------------------- if (url.pathname === "/api/keys" && req.method === "GET") { + if (removeExpiredApiKeyRotations(config)) { + saveConfigPreservingClaudeCode(config); + reconcileLiveStateStores(); + } const keys = config.apiKeys ?? []; const endpoints = buildApiAccessEndpoints(config, { requestUrl: req.url, @@ -596,6 +606,11 @@ export async function handleOauthAccountRoutes(ctx: ManagementContext): Promise< name: k.name, prefix: k.key.slice(0, 17) + "...", createdAt: k.createdAt, + ...(k.pendingRotation ? { pendingRotation: { + id: k.pendingRotation.id, + createdAt: k.pendingRotation.createdAt, + expiresAt: k.pendingRotation.expiresAt, + } } : {}), usage: rollup.get(k.id) ?? { requests7d: 0, totalRequests: 0 }, })), // Dataset-level and singular: it describes the usage log, not any one key. @@ -606,6 +621,50 @@ export async function handleOauthAccountRoutes(ctx: ManagementContext): Promise< }, 200, req, config); } + if (url.pathname === "/api/keys/rotate" && req.method === "POST") { + const body = await readJsonBody(req); + if (!body || Object.keys(body).length !== 1 || typeof body.id !== "string" || !body.id) { + return jsonResponse({ error: "invalid body" }, 400, req, config); + } + const result = startApiKeyRotation(config, body.id); + if ("error" in result) { + return jsonResponse({ error: result.error === "not-found" ? "key not found" : "rotation already pending" }, result.error === "not-found" ? 404 : 409, req, config); + } + saveConfigPreservingClaudeCode(config); + reconcileLiveStateStores(); + return jsonResponse(result, 201, req, config); + } + + if (url.pathname === "/api/keys/rotate/commit" && req.method === "POST") { + const body = await readJsonBody(req); + if (!body || Object.keys(body).length !== 2 || typeof body.id !== "string" || !body.id + || typeof body.rotationId !== "string" || !body.rotationId) { + return jsonResponse({ error: "invalid body" }, 400, req, config); + } + const result = commitApiKeyRotation(config, body.id, body.rotationId); + if ("error" in result) { + if (result.error === "expired") saveConfigPreservingClaudeCode(config); + return jsonResponse({ error: result.error === "not-found" ? "key rotation not found" : `rotation ${result.error}` }, result.error === "not-found" ? 404 : 409, req, config); + } + saveConfigPreservingClaudeCode(config); + reconcileLiveStateStores(); + return jsonResponse({ ok: true }, 200, req, config); + } + + if (url.pathname === "/api/keys/rotate" && req.method === "DELETE") { + const body = await readJsonBody(req); + if (!body || Object.keys(body).length !== 2 || typeof body.id !== "string" || !body.id + || typeof body.rotationId !== "string" || !body.rotationId) { + return jsonResponse({ error: "invalid body" }, 400, req, config); + } + if (!abortApiKeyRotation(config, body.id, body.rotationId)) { + return jsonResponse({ error: "key rotation not found or mismatched" }, 409, req, config); + } + saveConfigPreservingClaudeCode(config); + reconcileLiveStateStores(); + return jsonResponse({ ok: true }, 200, req, config); + } + if (url.pathname === "/api/keys" && req.method === "POST") { const body = await readJsonBody(req); if (!body) return jsonResponse({ error: "invalid body" }, 400, req, config); diff --git a/src/types/config.ts b/src/types/config.ts index ae313b223c..43c7a06772 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -219,6 +219,14 @@ export interface OcxApiKeyEntry { name: string; key: string; createdAt: string; + pendingRotation?: OcxPendingApiKeyRotation; +} + +export interface OcxPendingApiKeyRotation { + id: string; + key: string; + createdAt: string; + expiresAt: string; } /** From b829320b87310c017d5e8a3a04cc940c4b1b48d2 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 04:49:07 +0900 Subject: [PATCH 075/172] feat(hardening): bound pairing attempts and add session logout --- src/server/gui-session.ts | 107 +++++++++++++++++++++++- src/server/index.ts | 23 +++-- src/server/management-api.ts | 9 +- src/server/management-auth.ts | 18 ++++ src/server/management/context.ts | 4 +- src/server/management/session-routes.ts | 13 +++ 6 files changed, 161 insertions(+), 13 deletions(-) create mode 100644 src/server/management/session-routes.ts diff --git a/src/server/gui-session.ts b/src/server/gui-session.ts index 95a7008ff5..db1fab549b 100644 --- a/src/server/gui-session.ts +++ b/src/server/gui-session.ts @@ -30,8 +30,21 @@ export interface GuiPairingGrantRecord { serverOrigin: string; browserOrigin: string; expiresAt: number; + failedAttempts?: number; } +export interface PairingAttemptContext { + ingress: "public" | "hub-management"; + peerAddress: string | null; + tailscaleUser: string | null; + browserOrigin: string; +} + +export type PairingAttemptResult = + | { allowed: true } + | { allowed: false; retryAfterSeconds: number; reason: "grant" | "source" | "capacity" }; +type PairingAttemptRefusal = Extract; + export interface GuiSessionState { sessions: Map; pairingGrants: Map; @@ -50,11 +63,16 @@ export const LOOPBACK_GUI_SESSION_TTL_MS = 5 * 60_000; export const REMOTE_GUI_SESSION_TTL_MS = 12 * 60 * 60_000; export const GUI_PAIRING_GRANT_TTL_MS = 5 * 60_000; export const GUI_SESSION_LIMIT = 128; -export const GUI_PAIRING_GRANT_LIMIT = 64; +export const GUI_PAIRING_GRANT_LIMIT = 128; export const GUI_PAIRING_GRANT_RATE_LIMIT = 8; export const GUI_PAIRING_GRANT_RATE_WINDOW_MS = 60_000; const pairingGrantCreations = new WeakMap(); +const pairingSourceAttempts = new WeakMap>(); +const PAIRING_SOURCE_WINDOW_MS = 10 * 60_000; +const PAIRING_SOURCE_FAILURE_LIMIT = 10; +const PAIRING_SOURCE_LIMIT = 1_024; +const PAIRING_GRANT_FAILURE_LIMIT = 5; export class GuiPairingGrantRateLimitError extends Error { constructor() { @@ -179,6 +197,43 @@ function pairingGrantDigest(grant: string): string { return createHash("sha256").update(grant).digest("base64url"); } +function pairingSourceKey(context: PairingAttemptContext): string { + const identity = context.ingress === "hub-management" && context.tailscaleUser + ? `tailscale:${context.tailscaleUser}` + : context.peerAddress + ? `peer:${context.peerAddress}` + : "anonymous"; + return createHash("sha256").update(identity).digest("base64url"); +} + +function recordSourceFailure( + state: GuiSessionState, + context: PairingAttemptContext, + now: number, +): PairingAttemptResult { + let attempts = pairingSourceAttempts.get(state); + if (!attempts) { + attempts = new Map(); + pairingSourceAttempts.set(state, attempts); + } + for (const [key, record] of attempts) { + if (record.windowStartedAt + PAIRING_SOURCE_WINDOW_MS <= now) attempts.delete(key); + } + const key = pairingSourceKey(context); + let record = attempts.get(key); + if (!record) { + if (attempts.size >= PAIRING_SOURCE_LIMIT) { + return { allowed: false, retryAfterSeconds: 1, reason: "capacity" }; + } + record = { failures: 0, windowStartedAt: now }; + attempts.set(key, record); + } + record.failures += 1; + if (record.failures < PAIRING_SOURCE_FAILURE_LIMIT) return { allowed: true }; + const remaining = Math.max(1, record.windowStartedAt + PAIRING_SOURCE_WINDOW_MS - now); + return { allowed: false, retryAfterSeconds: Math.ceil(remaining / 1000), reason: "source" }; +} + function findPairingGrant( grant: string, state: GuiSessionState, @@ -240,13 +295,29 @@ function hasAlternateCredential(req: Request): boolean { || req.headers.has("x-api-key"); } +export function consumeGuiPairingGrant( + req: Request, + body: unknown, + config: OcxConfig, + state: GuiSessionState, + now?: number, +): GuiSessionBootstrap | null; +export function consumeGuiPairingGrant( + req: Request, + body: unknown, + config: OcxConfig, + state: GuiSessionState, + now: number, + attemptContext: PairingAttemptContext, +): GuiSessionBootstrap | PairingAttemptRefusal | null; export function consumeGuiPairingGrant( req: Request, body: unknown, config: OcxConfig, state: GuiSessionState, now = Date.now(), -): GuiSessionBootstrap | null { + attemptContext?: PairingAttemptContext, +): GuiSessionBootstrap | PairingAttemptRefusal | null { if (req.method !== "POST" || hasAlternateCredential(req) || config.runtimeRole !== "hub") return null; // Scheme check FIRST, before the grant is parsed or looked up. // @@ -264,14 +335,42 @@ export function consumeGuiPairingGrant( const grant = strictPairingGrantBody(body); const browserOrigin = canonicalGuiBrowserOrigin(req.headers.get("Origin")); if (!grant || !browserOrigin) return null; + const context = attemptContext ?? { + ingress: "public", + peerAddress: null, + tailscaleUser: null, + browserOrigin, + }; + const sourceRecord = attemptContext + ? pairingSourceAttempts.get(state)?.get(pairingSourceKey(context)) + : undefined; + if (sourceRecord && sourceRecord.windowStartedAt + PAIRING_SOURCE_WINDOW_MS > now + && sourceRecord.failures >= PAIRING_SOURCE_FAILURE_LIMIT) { + return { + allowed: false, + retryAfterSeconds: Math.max(1, Math.ceil((sourceRecord.windowStartedAt + PAIRING_SOURCE_WINDOW_MS - now) / 1000)), + reason: "source", + }; + } const found = findPairingGrant(grant, state); - if (!found) return null; + if (!found) { + const source = recordSourceFailure(state, context, now); + return attemptContext && !source.allowed ? source : null; + } const [digest, record] = found; if (record.expiresAt <= now) { state.pairingGrants.delete(digest); return null; } - if (browserOrigin !== record.browserOrigin) return null; + if (browserOrigin !== record.browserOrigin) { + record.failedAttempts = (record.failedAttempts ?? 0) + 1; + const source = recordSourceFailure(state, context, now); + if (record.failedAttempts >= PAIRING_GRANT_FAILURE_LIMIT) { + state.pairingGrants.delete(digest); + return attemptContext ? { allowed: false, retryAfterSeconds: 1, reason: "grant" } : null; + } + return attemptContext && !source.allowed ? source : null; + } const serverOrigin = managementRequestOrigin(req, config); if (serverOrigin !== record.serverOrigin) return null; // Re-checked against the grant's own recorded origin rather than only the request's: diff --git a/src/server/index.ts b/src/server/index.ts index 43c159d78c..4294e2161c 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -191,6 +191,7 @@ import { handleLive, logLiveSidebandFrame, parseLiveSidebandTarget, resolveLiveS import { handleSearch } from "./search"; import { fetchAllModels, handleManagementAPI, VERSION, type ManagementApiDeps } from "./management-api"; import { + createManagementSessionControl, initializeManagementAuthState, issueGuiSession, managementPrincipal, @@ -640,6 +641,7 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server).grant !== "string") { return withManagementCors(Response.json({ error: "invalid pairing exchange body" }, { status: 400, headers: { "Cache-Control": "no-store" } }), req, config); } - const session = managementAuth.available - ? consumeGuiPairingGrant(req, body, config, managementAuth) + const pairing = managementAuth.available + ? consumeGuiPairingGrant(req, body, config, managementAuth, Date.now(), { + ingress: ingress === "hub-management" ? "hub-management" : "public", + peerAddress: requestServer.requestIP(req)?.address ?? null, + tailscaleUser: ingress === "hub-management" ? req.headers.get("Tailscale-User-Login") : null, + browserOrigin: req.headers.get("Origin") ?? "", + }) : null; - return session - ? withManagementCors(serveSessionBootstrap(session), req, config) + if (pairing && "allowed" in pairing && pairing.allowed === false) { + return withManagementCors(Response.json({ error: "pairing exchange refused" }, { + status: 429, + headers: { "Cache-Control": "no-store", "Retry-After": String(pairing.retryAfterSeconds) }, + }), req, config); + } + return pairing + ? withManagementCors(serveSessionBootstrap(pairing), req, config) : withManagementCors(new Response(null, { status: 401, headers: { "Cache-Control": "no-store" } }), req, config); } return withCors(formatErrorResponse(404, "not_found", `Unknown endpoint: ${req.method} ${url.pathname}`), req, policy); diff --git a/src/server/management-api.ts b/src/server/management-api.ts index 9e188c03ee..1e49c2c278 100644 --- a/src/server/management-api.ts +++ b/src/server/management-api.ts @@ -73,13 +73,14 @@ import { handleCodexPromptRoutes } from "./management/codex-prompt-routes"; import { handleIntegrationRoutes } from "./management/integration-routes"; import { handleNativeIntegrationRoutes } from "./management/native-integration-routes"; import type { ManagementContext } from "./management/context"; -import type { ManagementPrincipal } from "./management-auth"; +import type { ManagementPrincipal, ManagementSessionControl } from "./management-auth"; export type { ManagementApiDeps } from "./management/context"; import { fetchAllModels } from "./management/shared"; import { CatalogGatherBusyError } from "../codex/catalog/provider-fetch"; import type { CatalogDisposition, ConvergeCodex } from "../codex/convergence-types"; import { normalizeCatalogDisposition } from "../codex/catalog-refresh-status"; import { managementBodyTooLargeResponse } from "./management/body"; +import { handleSessionRoutes } from "./management/session-routes"; // installed npm version instead of a stale hardcode. export const VERSION = (() => { @@ -136,6 +137,7 @@ export async function handleManagementAPI( config: OcxConfig, deps: ManagementApiDeps = {}, principal?: ManagementPrincipal, + sessionControl?: ManagementSessionControl, ): Promise { if (!isAllowedManagementOrigin(req, config)) { return jsonResponse({ error: "cross-origin request blocked" }, 403, req, config); @@ -218,10 +220,11 @@ export async function handleManagementAPI( } } catch { /* best-effort */ } } - const ctx: ManagementContext = { req, url, config, deps, principal, convergeCodexCatalog, syncClaudeAgentDefsBestEffort }; + const ctx: ManagementContext = { req, url, config, deps, principal, sessionControl, convergeCodexCatalog, syncClaudeAgentDefsBestEffort }; let routed: Response | null; try { - routed = (await handleConfigRoutes(ctx)) + routed = handleSessionRoutes(ctx) + ?? (await handleConfigRoutes(ctx)) ?? (await handleStorageLogGuardRoutes(ctx)) ?? (await handleLogsUsageRoutes(ctx)) ?? (await handleRequestHistoryRoutes(ctx)) diff --git a/src/server/management-auth.ts b/src/server/management-auth.ts index 0bd29d0556..09127b0b58 100644 --- a/src/server/management-auth.ts +++ b/src/server/management-auth.ts @@ -249,6 +249,24 @@ export function issueGuiSession( return issueGuiSessionFromState(req, config, state, context); } +export interface ManagementSessionControl { + revokeCurrent(req: Request): boolean; +} + +export function createManagementSessionControl(state: ManagementAuthState): ManagementSessionControl { + return { + revokeCurrent(req: Request): boolean { + if (!state.available) return false; + const credential = requestManagementCredential(req); + if (!credential) return false; + for (const token of state.sessions.keys()) { + if (equalSecret(credential, token)) return state.sessions.delete(token); + } + return false; + }, + }; +} + /** * Which credential actually authorized a management request. * diff --git a/src/server/management/context.ts b/src/server/management/context.ts index 6703477556..5d57378bb3 100644 --- a/src/server/management/context.ts +++ b/src/server/management/context.ts @@ -4,7 +4,7 @@ import type { CodexLogGuardProtectionDeps } from "../../codex/log-guard/protecti import type { CodexLogGuardMaintenanceDeps } from "../../codex/log-guard/maintenance"; import type { StartupHealth } from "../../codex/autostart-health"; import type { StartupInstallAction } from "../startup-action-control"; -import type { ManagementPrincipal } from "../management-auth"; +import type { ManagementPrincipal, ManagementSessionControl } from "../management-auth"; import type { CatalogModel } from "../../codex/catalog"; import type { Paths as CodexPromptPaths } from "../../codex/prompt-layers"; import type { injectGrokConfig } from "../../grok/inject"; @@ -118,6 +118,8 @@ export interface ManagementContext { * tests, which are treated as the untrusted `admin-token` case. */ principal?: ManagementPrincipal; + /** Narrow current-session revocation seam; contains neither the token nor session map. */ + sessionControl?: ManagementSessionControl; convergeCodexCatalog: () => Promise; syncClaudeAgentDefsBestEffort: () => Promise; } diff --git a/src/server/management/session-routes.ts b/src/server/management/session-routes.ts new file mode 100644 index 0000000000..dd3fcd7ff5 --- /dev/null +++ b/src/server/management/session-routes.ts @@ -0,0 +1,13 @@ +import { jsonResponse } from "../auth-cors"; +import type { ManagementContext } from "./context"; + +export function handleSessionRoutes(ctx: ManagementContext): Response | null { + if (ctx.url.pathname !== "/api/session/logout" || ctx.req.method !== "POST") return null; + if (ctx.principal !== "gui-session") { + return jsonResponse({ error: "GUI session required" }, 403, ctx.req, ctx.config); + } + if (!ctx.sessionControl?.revokeCurrent(ctx.req)) { + return jsonResponse({ error: "GUI session not found" }, 401, ctx.req, ctx.config); + } + return jsonResponse({ ok: true }, 200, ctx.req, ctx.config); +} From 10a31986a4878b13931eb6e35a0bca63b1553fa9 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 04:50:21 +0900 Subject: [PATCH 076/172] feat(hardening): validate remote protocol catalog and relay --- src/client/hub-client.ts | 56 ++++++++++++++++++++++++++++++----- src/client/hub-relay.ts | 63 +++++++++++++++++++++++++++++++++------- src/remote/protocol.ts | 17 +++++++++-- 3 files changed, 115 insertions(+), 21 deletions(-) diff --git a/src/client/hub-client.ts b/src/client/hub-client.ts index 9fcb94be6d..d7b7eafdb7 100644 --- a/src/client/hub-client.ts +++ b/src/client/hub-client.ts @@ -1,4 +1,5 @@ import { MAX_REMOTE_CATALOG_BYTES } from "../server/catalog-download"; +import { readBoundedResponseBytes } from "../lib/bounded-body"; /** * A pairing grant may cross loopback or authenticated HTTPS, and nothing else. @@ -100,17 +101,43 @@ async function boundedText(response: Response, maxBytes: number): Promise maxBytes) { throw new HubClientError("body_too_large", "Hub response exceeded the allowed size", response.status); } - const bytes = new Uint8Array(await response.arrayBuffer()); - if (bytes.byteLength > maxBytes) { + const result = await readBoundedResponseBytes(response, { maxBytes }); + if (result.oversized) { throw new HubClientError("body_too_large", "Hub response exceeded the allowed size", response.status); } try { - return new TextDecoder("utf-8", { fatal: true }).decode(bytes); + return new TextDecoder("utf-8", { fatal: true }).decode(result.bytes); } catch (error) { throw new HubClientError("body_invalid", "Hub response was not valid UTF-8", response.status, { cause: error }); } } +function jsonCompatibleContentType(response: Response): boolean { + const value = response.headers.get("content-type")?.split(";", 1)[0]?.trim().toLowerCase(); + return value === "application/json" || value?.endsWith("+json") === true; +} + +function validateRemoteCatalog(value: unknown): void { + if (!value || typeof value !== "object" || Array.isArray(value)) { + throw new HubClientError("catalog_schema_invalid", "Hub catalog response was invalid"); + } + const models = (value as Record).models; + if (!Array.isArray(models) || models.length > 2_000) { + throw new HubClientError("catalog_schema_invalid", "Hub catalog model list was invalid"); + } + const slugs = new Set(); + for (const row of models) { + if (!row || typeof row !== "object" || Array.isArray(row)) { + throw new HubClientError("catalog_schema_invalid", "Hub catalog model row was invalid"); + } + const slug = (row as Record).slug; + if (typeof slug !== "string" || !slug.trim() || /[\x00-\x1f\x7f]/.test(slug) || slugs.has(slug)) { + throw new HubClientError("catalog_schema_invalid", "Hub catalog model slug was invalid"); + } + slugs.add(slug); + } +} + function parseJson(text: string, code: string): unknown { try { return JSON.parse(text) as unknown; @@ -303,7 +330,7 @@ export async function downloadClientCatalog( serverUrl: string, admissionToken: string, options: { timeoutMs?: number; maxBytes?: number; fetchImpl?: typeof fetch } = {}, -): Promise<{ kind: "fresh"; body: string }> { +): Promise<{ kind: "fresh"; body: string; keyId?: string }> { const origin = normalizeHubOrigin(serverUrl); const headers = new Headers({ Accept: "application/json", "x-opencodex-api-key": admissionToken }); // Unconditional by contract: /v1/catalog emits no validator (Phase 1, D2) because its @@ -320,10 +347,23 @@ export async function downloadClientCatalog( const code = response.status === 401 ? "catalog_unauthorized" : `catalog_http_${response.status}`; throw new HubClientError(code, `Hub catalog request failed (${response.status})`, response.status); } + if (!jsonCompatibleContentType(response)) { + try { await response.body?.cancel(); } catch { /* best effort */ } + throw new HubClientError("catalog_content_type_invalid", "Hub catalog response was not JSON", response.status); + } const body = await boundedText(response, options.maxBytes ?? MAX_REMOTE_CATALOG_BYTES); const parsed = parseJson(body, "catalog_invalid"); - if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) { - throw new HubClientError("catalog_invalid", "Hub catalog response was invalid", response.status); - } - return { kind: "fresh", body }; + validateRemoteCatalog(parsed); + const keyId = response.headers.get("x-opencodex-key-id")?.trim() || undefined; + return { kind: "fresh", body, ...(keyId ? { keyId } : {}) }; +} + +export async function probeClientKeyId( + serverUrl: string, + admissionToken: string, + expectedKeyId: string, + options: { timeoutMs?: number; fetchImpl?: typeof fetch } = {}, +): Promise { + const catalog = await downloadClientCatalog(serverUrl, admissionToken, options); + return catalog.kind === "fresh" && catalog.keyId === expectedKeyId; } diff --git a/src/client/hub-relay.ts b/src/client/hub-relay.ts index ae75862f8e..9bd58030aa 100644 --- a/src/client/hub-relay.ts +++ b/src/client/hub-relay.ts @@ -36,6 +36,38 @@ const RESPONSE_HEADERS = new Set([ "retry-after", "vary", ]); +const HOP_BY_HOP_HEADERS = new Set([ + "connection", "keep-alive", "proxy-authenticate", "proxy-authorization", + "te", "trailer", "transfer-encoding", "upgrade", +]); + +export type HubRelayRawHeaderValidation = + | { ok: true; connectionNamed: Set } + | { ok: false; reason: "smuggling" | "invalid" }; + +export function validateHubRelayRequestHeaders(raw: readonly (readonly [string, string])[]): HubRelayRawHeaderValidation { + let contentLengths = 0; + let transferEncoding = false; + const connectionNamed = new Set(); + for (const [rawName, value] of raw) { + const name = rawName.trim().toLowerCase(); + if (!name || /[\r\n]/.test(rawName) || /[\r\n]/.test(value)) return { ok: false, reason: "invalid" }; + if (name === "content-length") { + contentLengths += 1; + if (!/^\d+$/.test(value.trim())) return { ok: false, reason: "smuggling" }; + } + if (name === "transfer-encoding") transferEncoding = true; + if (name === "upgrade") return { ok: false, reason: "smuggling" }; + if (name === "connection") { + for (const token of value.split(",")) { + const normalized = token.trim().toLowerCase(); + if (normalized) connectionNamed.add(normalized); + } + } + } + if (transferEncoding || contentLengths > 1) return { ok: false, reason: "smuggling" }; + return { ok: true, connectionNamed }; +} function relayError(status: number, error: string): Response { return Response.json({ error }, { status }); @@ -110,10 +142,11 @@ async function boundedBody( return body; } -function filteredHeaders(source: Headers, allowlist: Set): Headers { +function filteredHeaders(source: Headers, allowlist: Set, omitted: ReadonlySet = new Set()): Headers { const headers = new Headers(); for (const [name, value] of source) { - if (allowlist.has(name.toLowerCase())) headers.append(name, value); + const normalized = name.toLowerCase(); + if (allowlist.has(normalized) && !HOP_BY_HOP_HEADERS.has(normalized) && !omitted.has(normalized)) headers.append(name, value); } return headers; } @@ -136,6 +169,8 @@ export async function relayHubManagementRequest( const method = req.method.toUpperCase(); const destination = relayDestination(suffix, target, method); if (!destination) return relayError(404, "hub relay path refused"); + const requestHeaderValidation = validateHubRelayRequestHeaders([...req.headers]); + if (!requestHeaderValidation.ok) return relayError(400, "hub relay request headers refused"); let body: Uint8Array | null; try { @@ -147,7 +182,7 @@ export async function relayHubManagementRequest( } const stripped = stripMachineAuthHeaders(req.headers); - const headers = filteredHeaders(stripped, REQUEST_HEADERS); + const headers = filteredHeaders(stripped, REQUEST_HEADERS, requestHeaderValidation.connectionNamed); if (!headersWithinLimit(headers)) return relayError(431, "hub relay request headers too large"); const browserOrigin = canonicalOrigin(target.browserOrigin); const mutation = method !== "GET" && method !== "HEAD"; @@ -180,20 +215,28 @@ export async function relayHubManagementRequest( return relayError(502, "hub relay redirect refused"); } - let responseBody: Uint8Array | null; - const responseHeaders = filteredHeaders(upstream.headers, RESPONSE_HEADERS); + const responseConnectionNamed = new Set((upstream.headers.get("connection") ?? "").split(",").map(value => value.trim().toLowerCase()).filter(Boolean)); + const responseHeaders = filteredHeaders(upstream.headers, RESPONSE_HEADERS, responseConnectionNamed); if (!headersWithinLimit(responseHeaders)) { try { await upstream.body?.cancel(); } catch { /* best effort */ } return relayError(502, "hub relay response headers too large"); } - try { - responseBody = method === "HEAD" - ? null - : await boundedBody(upstream.body, upstream.headers.get("content-length"), HUB_RELAY_RESPONSE_BODY_MAX_BYTES); - } catch { + const declaredResponseLength = upstream.headers.get("content-length"); + if (declaredResponseLength !== null && (!/^\d+$/.test(declaredResponseLength) + || Number(declaredResponseLength) > HUB_RELAY_RESPONSE_BODY_MAX_BYTES)) { try { await upstream.body?.cancel(); } catch { /* best effort */ } return relayError(502, "hub relay response body too large"); } + let streamed = 0; + const responseBody = method === "HEAD" || !upstream.body ? null : upstream.body.pipeThrough(new TransformStream({ + transform(chunk, controller) { + streamed += chunk.byteLength; + if (streamed > HUB_RELAY_RESPONSE_BODY_MAX_BYTES) { + throw new RangeError("hub relay response body too large"); + } + controller.enqueue(chunk); + }, + })); return new Response(responseBody, { status: upstream.status, statusText: upstream.statusText, diff --git a/src/remote/protocol.ts b/src/remote/protocol.ts index ba192f0066..dc4cf359af 100644 --- a/src/remote/protocol.ts +++ b/src/remote/protocol.ts @@ -7,10 +7,11 @@ export interface RemoteReadyMetadata { protocol: number; minimumClientProtocol: number; managementUrl: string; + features?: string[]; } export type RemoteProtocolCompatibility = - | { ok: true; metadata: RemoteReadyMetadata } + | { ok: true; metadata: RemoteReadyMetadata; features: Set } | { ok: false; reason: "invalid" | "hub-too-new" | "hub-too-old"; message: string }; const INVALID_REMOTE_PROTOCOL_MESSAGE = @@ -62,16 +63,24 @@ export function parseRemoteReadyMetadata(value: unknown): RemoteReadyMetadata | if (raw.minimumClientProtocol > raw.protocol) return null; const parsedManagementOrigin = managementOrigin(raw.managementUrl); if (!parsedManagementOrigin) return null; + const features = raw.features; + if (features !== undefined && ( + !Array.isArray(features) + || features.length > 64 + || features.some(feature => typeof feature !== "string" || !feature || feature.length > 80 || /[\x00-\x1f\x7f]/.test(feature)) + || new Set(features).size !== features.length + )) return null; return { protocol: raw.protocol, minimumClientProtocol: raw.minimumClientProtocol, managementUrl: parsedManagementOrigin, + ...(Array.isArray(features) ? { features: [...features] as string[] } : {}), }; } export function checkRemoteProtocolCompatibility( value: unknown, - client: { protocol: number; minimumHubProtocol: number } = { + client: { protocol: number; minimumHubProtocol: number; features?: readonly string[] } = { protocol: REMOTE_HUB_PROTOCOL, minimumHubProtocol: MINIMUM_REMOTE_CLIENT_PROTOCOL, }, @@ -94,5 +103,7 @@ export function checkRemoteProtocolCompatibility( message: `OpenCodex hub provides remote protocol ${metadata.protocol}; this client requires at least ${client.minimumHubProtocol}. Upgrade ocx on the hub.`, }; } - return { ok: true, metadata }; + const supported = new Set(client.features ?? []); + const features = new Set((metadata.features ?? []).filter(feature => supported.has(feature))); + return { ok: true, metadata, features }; } From 2ea63132419e3842ca7933422f9bfa119a37f93f Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 04:53:27 +0900 Subject: [PATCH 077/172] feat(hardening): recover client key rotation through token backup --- src/cli/access.ts | 30 ++++++ src/cli/connect.ts | 47 ++++++++- src/cli/registry.ts | 4 +- src/client/connect.ts | 192 +++++++++++++++++++++++++++++++++++++ src/client/hub-client.ts | 92 +++++++++++++++++- src/lib/service-secrets.ts | 90 ++++++++++++++++- 6 files changed, 450 insertions(+), 5 deletions(-) diff --git a/src/cli/access.ts b/src/cli/access.ts index 0003aa64f9..51b351f904 100644 --- a/src/cli/access.ts +++ b/src/cli/access.ts @@ -12,6 +12,9 @@ import { const USAGE = `Usage: ocx access key [list] [--json] ocx access key create [name] [--json] + ocx access key rotate [--json] + ocx access key rotate commit [--json] + ocx access key rotate abort [--json] ocx access key remove --yes [--json] ocx access endpoints [--json] ocx access models [--json] @@ -87,6 +90,33 @@ async function key(argv: string[], deps: RuntimeApiDeps): Promise { ]); return; } + if (action === "rotate") { + const operation = args[0] === "commit" || args[0] === "abort" ? args.shift()! : "start"; + const id = args.shift(); + if (!id) throw new CliUsageError("key id is required", USAGE); + if (operation === "start") { + rejectArgs(args, USAGE); + const result = await runtimeRequest>("/api/keys/rotate", { + method: "POST", + body: JSON.stringify({ id }), + }, deps); + printData(result, wantsJson, [ + `Started rotation for API key ${id}.`, + `New key (shown once): ${String(result.key ?? "")}`, + `After the client accepts it, commit with rotation id ${String(result.rotationId ?? "")}.`, + ]); + return; + } + const rotationId = args.shift(); + if (!rotationId) throw new CliUsageError("rotation id is required", USAGE); + rejectArgs(args, USAGE); + const result = await runtimeRequest(operation === "commit" ? "/api/keys/rotate/commit" : "/api/keys/rotate", { + method: operation === "commit" ? "POST" : "DELETE", + body: JSON.stringify({ id, rotationId }), + }, deps); + printData(result, wantsJson, [`${operation === "commit" ? "Committed" : "Aborted"} rotation for API key ${id}.`]); + return; + } if (action === "remove" || action === "delete") { const id = args.shift(); const yes = takeFlag(args, "--yes"); diff --git a/src/cli/connect.ts b/src/cli/connect.ts index 09ed24bc1b..cb6871f1d2 100644 --- a/src/cli/connect.ts +++ b/src/cli/connect.ts @@ -3,10 +3,11 @@ import { DEFAULT_CATALOG_PATH } from "../codex/paths"; import { disconnectClient, revokeConnectedClientKey, + rotateConnectedClientKey, connectClient, } from "../client/connect"; import { readClientConnectionState } from "../client/state"; -import { readServiceApiTokenState } from "../lib/service-secrets"; +import { readServiceApiTokenState, readTokenBackupState, removeOrphanTokenBackup } from "../lib/service-secrets"; import type { OcxConnectedClientId } from "../types"; import { CliUsageError, @@ -26,6 +27,8 @@ export const CONNECT_USAGE = `Usage: [--clients codex,claude] [--management-transport direct|relay] [--no-sync] ocx connect status [--json] + ocx connect rotate (--pairing-code-stdin | --admin-token-stdin) + [--allow-insecure-http] [--json] ocx connect revoke --admin-token-stdin [--json]`; export const DISCONNECT_USAGE = `Usage: @@ -45,11 +48,26 @@ export type ClientConnectionStatus = { catalogAgeSeconds?: number; catalog: "present" | "missing" | "unsafe"; token: "owned" | "missing" | "changed" | "unsafe"; + rotation: "clean" | "orphan-cleaned" | "recovery-required" | "unsafe"; }; export function collectClientConnectionStatus(now = Date.now()): ClientConnectionStatus { const state = readClientConnectionState(); const tokenState = readServiceApiTokenState(); + const backup = readTokenBackupState(); + let rotation: ClientConnectionStatus["rotation"] = state.kind === "connected" && state.value.pendingOperation + ? "recovery-required" + : backup.kind === "unsafe" + ? "unsafe" + : "clean"; + if (rotation === "clean" && backup.kind === "present" && tokenState.kind === "present") { + try { + removeOrphanTokenBackup(); + rotation = "orphan-cleaned"; + } catch { + rotation = "unsafe"; + } + } let catalog: ClientConnectionStatus["catalog"] = "missing"; if (existsSync(DEFAULT_CATALOG_PATH)) { try { @@ -65,6 +83,7 @@ export function collectClientConnectionStatus(now = Date.now()): ClientConnectio ...(state.kind === "invalid" || state.kind === "mismatched" ? { reason: state.reason } : {}), catalog, token: tokenState.kind === "absent" ? "missing" : tokenState.kind === "unsafe" ? "unsafe" : "changed", + rotation, }; } const catalogAgeSeconds = state.value.catalogSyncedAt @@ -88,6 +107,7 @@ export function collectClientConnectionStatus(now = Date.now()): ClientConnectio ...(catalogAgeSeconds !== undefined ? { catalogAgeSeconds } : {}), catalog, token, + rotation, }; } @@ -111,10 +131,31 @@ function statusLines(status: ClientConnectionStatus): string[] { `API key id: ${status.apiKeyId}`, `Clients: ${status.selectedClients?.join(", ")}`, `Token file: ${status.token}`, + `Key rotation: ${status.rotation}`, `Catalog: ${status.catalog}${status.catalogAgeSeconds !== undefined ? ` (${status.catalogAgeSeconds}s old)` : ""}`, ]; } +async function runRotate(argv: string[], deps: RuntimeApiDeps): Promise { + const args = [...argv]; + const wantsJson = takeFlag(args, "--json"); + const pairing = takeFlag(args, "--pairing-code-stdin"); + const admin = takeFlag(args, "--admin-token-stdin"); + const allowInsecureHttp = takeFlag(args, "--allow-insecure-http"); + if (Number(pairing) + Number(admin) !== 1) { + throw new CliUsageError("choose exactly one of --pairing-code-stdin or --admin-token-stdin", CONNECT_USAGE); + } + rejectArgs(args, CONNECT_USAGE, { redactValues: true }); + const value = new TextEncoder().encode(await readSecretLine(deps, pairing ? "pairing code" : "admin token")); + const connection = await rotateConnectedClientKey({ + credential: { kind: pairing ? "pairing-grant" : "admin", value }, + allowInsecureHttp, + }, { fetchImpl: deps.fetchImpl }); + printData({ apiKeyId: connection.apiKeyId, rotation: "committed" }, wantsJson, [ + `Rotated connected API key ${connection.apiKeyId}; the previous key is no longer admitted.`, + ]); +} + async function runConnect(argv: string[], deps: RuntimeApiDeps): Promise { const args = [...argv]; const serverUrl = args.shift(); @@ -170,6 +211,10 @@ export async function handleConnectCommand(argv: string[], deps: RuntimeApiDeps await runRevoke(argv.slice(1), deps); return; } + if (argv[0] === "rotate") { + await runRotate(argv.slice(1), deps); + return; + } await runConnect(argv, deps); }); } diff --git a/src/cli/registry.ts b/src/cli/registry.ts index f6e3d76f2b..575ff552b6 100644 --- a/src/cli/registry.ts +++ b/src/cli/registry.ts @@ -92,6 +92,7 @@ export const CLI_COMMANDS: CliCommandEntry[] = [ summary: "Connect this machine to a remote OpenCodex hub without persisting the one-time authority.", details: [ "Status: ocx connect status [--json]", + "Rotate or recover: ocx connect rotate (--pairing-code-stdin | --admin-token-stdin) [--json]", "Revoke while connected: ocx connect revoke --admin-token-stdin [--json]", "Credentials are accepted only through stdin; argv and environment credential forms are not supported.", ], @@ -263,8 +264,9 @@ export const CLI_COMMANDS: CliCommandEntry[] = [ name: "access", usage: "ocx access ...", summary: "Manage OpenCodex admission API keys and inspect external endpoints.", + details: ["Key rotation start returns the replacement secret once; commit or abort it with the returned rotation id."], }, - { name: "api-key", usage: "ocx api-key ...", summary: "Alias of ocx access key." }, + { name: "api-key", usage: "ocx api-key ...", summary: "Alias of ocx access key." }, { name: "export", usage: "ocx export --client [--json] [--out ] [--force]", diff --git a/src/client/connect.ts b/src/client/connect.ts index 4e915d0c87..ffdfbfc924 100644 --- a/src/client/connect.ts +++ b/src/client/connect.ts @@ -21,7 +21,13 @@ import { import { DEFAULT_CATALOG_PATH } from "../codex/paths"; import { readServiceApiTokenState, + readTokenBackupState, removeServiceApiTokenFileIfOwned, + removeOrphanTokenBackup, + replaceServiceApiTokenFile, + restoreTokenBackup, + serviceApiTokenBackupPath, + writeTokenBackup, writeServiceApiTokenFile, } from "../lib/service-secrets"; import { MAX_REMOTE_CATALOG_BYTES } from "../server/catalog-download"; @@ -31,12 +37,16 @@ import type { } from "../types"; import { downloadClientCatalog, + abortClientKeyRotation, + commitClientKeyRotation, exchangeConnectPairingGrant, fetchHubReady, HubClientError, issueClientKey, normalizeHubOrigin, + probeClientKeyId, revokeClientKey, + startClientKeyRotation, type ConnectGuiSession, type IssuedClientKey, type OneTimeConnectCredential, @@ -47,6 +57,13 @@ import { readClientConnectionState, } from "./state"; +class RotationRecoveryRequiredError extends Error { + constructor(message: string, options?: ErrorOptions) { + super(message, options); + this.name = "RotationRecoveryRequiredError"; + } +} + export interface ConnectOptions { serverUrl: string; managementUrl?: string; @@ -61,6 +78,11 @@ export interface ClientConnectDeps { now?: () => Date; } +export interface RotateClientOptions { + credential: OneTimeConnectCredential; + allowInsecureHttp?: boolean; +} + type CatalogSnapshot = | { kind: "absent" } | { kind: "file"; body: string; fingerprint: string }; @@ -141,6 +163,175 @@ function releaseCredential(credential: OneTimeConnectCredential): void { credential.value.fill(0); } +async function rotationAuthority( + connection: OcxClientConnectionConfig, + options: RotateClientOptions, + deps: ClientConnectDeps, +): Promise<{ kind: "admin"; value: Uint8Array } | { kind: "gui-session"; value: ConnectGuiSession }> { + if (options.credential.kind === "admin") return { kind: "admin", value: options.credential.value }; + const session = await exchangeConnectPairingGrant( + connection.managementUrl, + localGuiOrigin(), + options.credential.value, + { allowInsecureHttp: options.allowInsecureHttp, fetchImpl: deps.fetchImpl }, + ); + return { kind: "gui-session", value: session }; +} + +function clearRotationState( + connection: OcxClientConnectionConfig, + tokenFingerprint: string, +): OcxClientConnectionConfig { + const next = { ...connection, tokenFingerprint }; + delete next.pendingOperation; + commitClientConnection(next); + return next; +} + +async function recoverRotationWithAuthority( + connection: OcxClientConnectionConfig, + authority: { kind: "admin"; value: Uint8Array } | { kind: "gui-session"; value: ConnectGuiSession }, + deps: ClientConnectDeps, +): Promise { + const pending = connection.pendingOperation; + if (!pending || pending.oldKeyBackupPath !== serviceApiTokenBackupPath()) { + throw new RotationRecoveryRequiredError("rotation recovery state is missing or invalid"); + } + const current = readServiceApiTokenState(); + const backup = readTokenBackupState(); + if (current.kind !== "present" || backup.kind !== "present") { + throw new RotationRecoveryRequiredError( + "rotation recovery requires owner-only current and .prev token files; preserve both and rerun ocx connect rotate with transient authority", + ); + } + let currentAccepted: boolean; + let backupAccepted: boolean; + try { + [currentAccepted, backupAccepted] = await Promise.all([ + probeClientKeyId(connection.serverUrl, current.token, connection.apiKeyId, { fetchImpl: deps.fetchImpl }), + probeClientKeyId(connection.serverUrl, backup.token, connection.apiKeyId, { fetchImpl: deps.fetchImpl }), + ]); + } catch (error) { + throw new RotationRecoveryRequiredError( + "rotation recovery could not establish both key admissions; preserve service-api-token and .prev", + { cause: error }, + ); + } + if (currentAccepted && backupAccepted) { + await commitClientKeyRotation(connection.managementUrl, authority, connection.apiKeyId, pending.rotationId, { fetchImpl: deps.fetchImpl }); + const next = clearRotationState(connection, current.fingerprint); + removeOrphanTokenBackup(); + return next; + } + if (currentAccepted && !backupAccepted) { + const next = clearRotationState(connection, current.fingerprint); + removeOrphanTokenBackup(); + return next; + } + if (!currentAccepted && backupAccepted) { + const restored = restoreTokenBackup(pending.oldKeyBackupPath); + await abortClientKeyRotation(connection.managementUrl, authority, connection.apiKeyId, pending.rotationId, { fetchImpl: deps.fetchImpl }); + const next = clearRotationState(connection, restored.fingerprint); + removeOrphanTokenBackup(); + return next; + } + throw new RotationRecoveryRequiredError( + "both rotation candidates were rejected; preserve service-api-token and .prev and repair admission from the hub", + ); +} + +export async function recoverPendingClientRotation( + options: RotateClientOptions, + deps: ClientConnectDeps = {}, +): Promise { + try { + const state = readClientConnectionState(); + if (state.kind !== "connected" || !state.value.pendingOperation) { + throw new Error("no pending client key rotation to recover"); + } + const authority = await rotationAuthority(state.value, options, deps); + return await recoverRotationWithAuthority(state.value, authority, deps); + } finally { + releaseCredential(options.credential); + } +} + +export async function rotateConnectedClientKey( + options: RotateClientOptions, + deps: ClientConnectDeps = {}, +): Promise { + let connection: OcxClientConnectionConfig | null = null; + let authority: { kind: "admin"; value: Uint8Array } | { kind: "gui-session"; value: ConnectGuiSession } | null = null; + let started: { rotationId: string; key: string; createdAt: string } | null = null; + let markerPersisted = false; + try { + const state = readClientConnectionState(); + if (state.kind !== "connected") throw new Error(`connect rotate is available only while connected (${state.kind})`); + connection = state.value; + authority = await rotationAuthority(connection, options, deps); + if (connection.pendingOperation) return await recoverRotationWithAuthority(connection, authority, deps); + const current = readServiceApiTokenState(); + if (current.kind !== "present" || current.fingerprint !== connection.tokenFingerprint) { + throw new Error(current.kind === "unsafe" ? current.reason : "connected service token ownership changed"); + } + writeTokenBackup(current.fingerprint); + const rotation = await startClientKeyRotation(connection.managementUrl, authority, connection.apiKeyId, { fetchImpl: deps.fetchImpl }); + started = { rotationId: rotation.rotationId, key: rotation.key, createdAt: rotation.createdAt }; + const marked: OcxClientConnectionConfig = { + ...connection, + pendingOperation: { + kind: "rotate", + rotationId: rotation.rotationId, + newKeyIssuedAt: rotation.createdAt, + oldKeyBackupPath: serviceApiTokenBackupPath(), + }, + }; + commitClientConnection(marked); + connection = marked; + markerPersisted = true; + const replacement = replaceServiceApiTokenFile(rotation.key); + if (!await probeClientKeyId(connection.serverUrl, rotation.key, connection.apiKeyId, { fetchImpl: deps.fetchImpl })) { + throw new Error("new client key admission probe was refused"); + } + try { + await commitClientKeyRotation(connection.managementUrl, authority, connection.apiKeyId, rotation.rotationId, { fetchImpl: deps.fetchImpl }); + } catch { + return await recoverRotationWithAuthority(connection, authority, deps); + } + const next = clearRotationState(connection, replacement.fingerprint); + removeOrphanTokenBackup(); + return next; + } catch (error) { + if (error instanceof RotationRecoveryRequiredError) throw error; + if (connection && authority && started) { + if (markerPersisted && connection.pendingOperation) { + try { + const restored = restoreTokenBackup(connection.pendingOperation.oldKeyBackupPath); + await abortClientKeyRotation(connection.managementUrl, authority, connection.apiKeyId, started.rotationId, { fetchImpl: deps.fetchImpl }); + clearRotationState(connection, restored.fingerprint); + removeOrphanTokenBackup(); + } catch (recoveryError) { + throw new RotationRecoveryRequiredError( + "rotation rollback was incomplete; preserve service-api-token and .prev and rerun ocx connect rotate with transient authority", + { cause: recoveryError }, + ); + } + } else { + try { await abortClientKeyRotation(connection.managementUrl, authority, connection.apiKeyId, started.rotationId, { fetchImpl: deps.fetchImpl }); } + finally { removeOrphanTokenBackup(); } + } + } else { + const backup = readTokenBackupState(); + if (backup.kind === "present") removeOrphanTokenBackup(); + } + throw error; + } finally { + if (started) started.key = ""; + authority = null; + releaseCredential(options.credential); + } +} + async function cleanupIssuedKey( managementUrl: string, credential: { kind: "admin"; value: Uint8Array } | { kind: "gui-session"; value: ConnectGuiSession }, @@ -447,3 +638,4 @@ export async function revokeConnectedClientKey( credential.value.fill(0); } } + probeClientKeyId, diff --git a/src/client/hub-client.ts b/src/client/hub-client.ts index d7b7eafdb7..524d9292ef 100644 --- a/src/client/hub-client.ts +++ b/src/client/hub-client.ts @@ -48,6 +48,11 @@ export interface IssuedClientKey { name: string; } +export interface StartedClientKeyRotation extends IssuedClientKey { + rotationId: string; + expiresAt: string; +} + export class HubClientError extends Error { constructor( readonly code: string, @@ -326,6 +331,84 @@ export async function revokeClientKey( if (!response.ok) throw new HubClientError("key_revoke_failed", `Hub refused key revocation (${response.status})`, response.status); } +function rotationManagementHeaders( + credential: { kind: "admin"; value: Uint8Array } | { kind: "gui-session"; value: ConnectGuiSession }, +): Headers { + const headers = new Headers({ "Content-Type": "application/json", Accept: "application/json" }); + if (credential.kind === "admin") headers.set("x-opencodex-api-key", credentialString(credential.value)); + else { + headers.set("x-opencodex-api-key", credential.value.token); + headers.set("Origin", credential.value.browserOrigin); + headers.set("X-OpenCodex-GUI-Origin", credential.value.browserOrigin); + headers.set("X-OpenCodex-CSRF-Token", credential.value.csrfToken); + } + return headers; +} + +function assertRotationAuthorityOrigin(origin: string, credential: { kind: "admin" } | { kind: "gui-session" }): void { + if (credential.kind === "admin" && new URL(origin).protocol !== "https:") { + throw new HubClientError("admin_http_refused", "Admin credentials may be sent only over HTTPS"); + } +} + +export async function startClientKeyRotation( + managementUrl: string, + credential: { kind: "admin"; value: Uint8Array } | { kind: "gui-session"; value: ConnectGuiSession }, + id: string, + options: { timeoutMs?: number; fetchImpl?: typeof fetch } = {}, +): Promise { + const origin = normalizeHubOrigin(managementUrl); + assertRotationAuthorityOrigin(origin, credential); + const response = await fetchBounded(options.fetchImpl ?? fetch, `${origin}/api/keys/rotate`, { + method: "POST", + headers: rotationManagementHeaders(credential), + body: JSON.stringify({ id }), + }, options.timeoutMs); + if (!response.ok) throw new HubClientError("key_rotation_start_failed", `Hub refused key rotation (${response.status})`, response.status); + const value = parseJson(await boundedText(response, MANAGEMENT_BODY_LIMIT), "key_rotation_invalid"); + const raw = value && typeof value === "object" && !Array.isArray(value) ? value as Record : null; + const issued = parseIssuedClientKey(value); + if (!raw || !issued || typeof raw.rotationId !== "string" || !raw.rotationId + || typeof raw.expiresAt !== "string" || Number.isNaN(Date.parse(raw.expiresAt))) { + throw new HubClientError("key_rotation_invalid", "Hub returned an invalid key rotation response", response.status); + } + return { ...issued, rotationId: raw.rotationId, expiresAt: raw.expiresAt }; +} + +export async function commitClientKeyRotation( + managementUrl: string, + credential: { kind: "admin"; value: Uint8Array } | { kind: "gui-session"; value: ConnectGuiSession }, + id: string, + rotationId: string, + options: { timeoutMs?: number; fetchImpl?: typeof fetch } = {}, +): Promise { + const origin = normalizeHubOrigin(managementUrl); + assertRotationAuthorityOrigin(origin, credential); + const response = await fetchBounded(options.fetchImpl ?? fetch, `${origin}/api/keys/rotate/commit`, { + method: "POST", + headers: rotationManagementHeaders(credential), + body: JSON.stringify({ id, rotationId }), + }, options.timeoutMs); + if (!response.ok) throw new HubClientError("key_rotation_commit_failed", `Hub refused rotation commit (${response.status})`, response.status); +} + +export async function abortClientKeyRotation( + managementUrl: string, + credential: { kind: "admin"; value: Uint8Array } | { kind: "gui-session"; value: ConnectGuiSession }, + id: string, + rotationId: string, + options: { timeoutMs?: number; fetchImpl?: typeof fetch } = {}, +): Promise { + const origin = normalizeHubOrigin(managementUrl); + assertRotationAuthorityOrigin(origin, credential); + const response = await fetchBounded(options.fetchImpl ?? fetch, `${origin}/api/keys/rotate`, { + method: "DELETE", + headers: rotationManagementHeaders(credential), + body: JSON.stringify({ id, rotationId }), + }, options.timeoutMs); + if (!response.ok) throw new HubClientError("key_rotation_abort_failed", `Hub refused rotation abort (${response.status})`, response.status); +} + export async function downloadClientCatalog( serverUrl: string, admissionToken: string, @@ -364,6 +447,11 @@ export async function probeClientKeyId( expectedKeyId: string, options: { timeoutMs?: number; fetchImpl?: typeof fetch } = {}, ): Promise { - const catalog = await downloadClientCatalog(serverUrl, admissionToken, options); - return catalog.kind === "fresh" && catalog.keyId === expectedKeyId; + try { + const catalog = await downloadClientCatalog(serverUrl, admissionToken, options); + return catalog.kind === "fresh" && catalog.keyId === expectedKeyId; + } catch (error) { + if (error instanceof HubClientError && error.status === 401) return false; + throw error; + } } diff --git a/src/lib/service-secrets.ts b/src/lib/service-secrets.ts index 2ff1a2a0bc..7d7cb2545b 100644 --- a/src/lib/service-secrets.ts +++ b/src/lib/service-secrets.ts @@ -1,5 +1,5 @@ import { createHash } from "node:crypto"; -import { existsSync, lstatSync, readFileSync, unlinkSync } from "node:fs"; +import { closeSync, existsSync, fsyncSync, lstatSync, openSync, readFileSync, unlinkSync } from "node:fs"; import { join } from "node:path"; import { getConfigDir } from "../config"; import { atomicWriteFile } from "../config/atomic-write"; @@ -20,6 +20,10 @@ export function serviceApiTokenFilePath(): string { return join(getConfigDir(), "service-api-token"); } +export function serviceApiTokenBackupPath(): string { + return `${serviceApiTokenFilePath()}.prev`; +} + export function serviceApiTokenFingerprint(token: string): string { return createHash("sha256").update(token).digest("hex"); } @@ -61,6 +65,90 @@ export function writeServiceApiTokenFile(token: string): PersistedServiceApiToke return { path, fingerprint: serviceApiTokenFingerprint(value) }; } +function fsyncRegularFile(path: string): void { + const fd = openSync(path, "r"); + try { fsyncSync(fd); } finally { closeSync(fd); } +} + +function validatedTokenValue(token: string): string { + const value = token.trim(); + if (!value || /[\r\n\0]/.test(value) || Buffer.byteLength(value) > MAX_SERVICE_API_TOKEN_BYTES) { + throw new Error("refusing to persist an invalid service API token"); + } + return value; +} + +export function replaceServiceApiTokenFile(token: string): PersistedServiceApiToken { + const value = validatedTokenValue(token); + const current = readServiceApiTokenState(); + if (current.kind !== "present") { + throw new Error(current.kind === "unsafe" ? current.reason : "service token file is missing"); + } + const path = serviceApiTokenFilePath(); + atomicWriteFile(path, `${value}\n`); + fsyncRegularFile(path); + return { path, fingerprint: serviceApiTokenFingerprint(value) }; +} + +export function readTokenBackupState(): ServiceApiTokenState { + const path = serviceApiTokenBackupPath(); + if (!existsSync(path)) return { kind: "absent" }; + let stat; + try { stat = lstatSync(path); } + catch { return { kind: "unsafe", reason: "service token backup could not be inspected" }; } + if (stat.isSymbolicLink() || !stat.isFile() || stat.size > MAX_SERVICE_API_TOKEN_BYTES + || (process.platform !== "win32" && (stat.mode & 0o077) !== 0)) { + return { kind: "unsafe", reason: "service token backup is not an owner-only bounded regular file" }; + } + try { + const token = readFileSync(path, "utf8").trim(); + if (!token || /[\r\n\0]/.test(token)) return { kind: "unsafe", reason: "service token backup is invalid" }; + return { kind: "present", token, fingerprint: serviceApiTokenFingerprint(token) }; + } catch { + return { kind: "unsafe", reason: "service token backup could not be read" }; + } +} + +export function writeTokenBackup(expectedFingerprint: string): PersistedServiceApiToken { + const current = readServiceApiTokenState(); + if (current.kind !== "present" || current.fingerprint !== expectedFingerprint) { + throw new Error(current.kind === "unsafe" ? current.reason : "service token ownership changed before backup"); + } + const existing = readTokenBackupState(); + if (existing.kind !== "absent") { + throw new Error(existing.kind === "unsafe" ? existing.reason : "service token backup already exists"); + } + const path = serviceApiTokenBackupPath(); + atomicWriteFile(path, `${current.token}\n`); + fsyncRegularFile(path); + return { path, fingerprint: current.fingerprint }; +} + +export function restoreTokenBackup(expectedPath: string): PersistedServiceApiToken { + if (expectedPath !== serviceApiTokenBackupPath()) throw new Error("service token backup path mismatch"); + const backup = readTokenBackupState(); + if (backup.kind !== "present") { + throw new Error(backup.kind === "unsafe" ? backup.reason : "service token backup is missing"); + } + const path = serviceApiTokenFilePath(); + atomicWriteFile(path, `${backup.token}\n`); + fsyncRegularFile(path); + return { path, fingerprint: backup.fingerprint }; +} + +export function removeOrphanTokenBackup(): "removed" | "absent" { + const backup = readTokenBackupState(); + if (backup.kind === "absent") return "absent"; + if (backup.kind === "unsafe") throw new Error(backup.reason); + try { + unlinkSync(serviceApiTokenBackupPath()); + return "removed"; + } catch (error) { + if ((error as NodeJS.ErrnoException).code === "ENOENT") return "absent"; + throw new Error("service token backup could not be removed", { cause: error }); + } +} + export function removeServiceApiTokenFileIfOwned( expectedFingerprint: string, ): "removed" | "absent" | "changed" { From b12093b54474843ce2c099df2bdc0c89189c625f Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 04:55:28 +0900 Subject: [PATCH 078/172] feat(hardening): add api key rotation controls --- .../apikeys-workspace/ApiKeysWorkspace.tsx | 74 ++++++++++++++++- gui/src/i18n/de.ts | 11 +++ gui/src/i18n/en.ts | 11 +++ gui/src/i18n/fr.ts | 11 +++ gui/src/i18n/ja.ts | 11 +++ gui/src/i18n/ko.ts | 11 +++ gui/src/i18n/ru.ts | 11 +++ gui/src/i18n/tr.ts | 11 +++ gui/src/i18n/zh-TW.ts | 11 +++ gui/src/i18n/zh.ts | 11 +++ gui/src/pages/ApiKeys.tsx | 80 ++++++++++++++++++- gui/src/pages/api-keys-utils.ts | 1 + 12 files changed, 251 insertions(+), 3 deletions(-) diff --git a/gui/src/components/apikeys-workspace/ApiKeysWorkspace.tsx b/gui/src/components/apikeys-workspace/ApiKeysWorkspace.tsx index ca907fa697..e3610cc64a 100644 --- a/gui/src/components/apikeys-workspace/ApiKeysWorkspace.tsx +++ b/gui/src/components/apikeys-workspace/ApiKeysWorkspace.tsx @@ -43,6 +43,8 @@ export interface ApiKeysWorkspaceProps { creating: boolean; newKey: string | null; copied: boolean; + rotationSecret?: { id: string; key: string; rotationId: string } | null; + rotationCopied?: boolean; filteredModels: ExternalModelRow[]; modelsLoading: boolean; /** Quiet revalidation / retry over rows already on screen — not a skeleton. */ @@ -60,6 +62,11 @@ export interface ApiKeysWorkspaceProps { onCopyKey: () => void; onDelete: (id: string) => Promise; onRename: (id: string, name: string) => Promise; + onRotationStart?: (id: string) => Promise; + onRotationCommit?: (id: string, rotationId: string) => Promise; + onRotationAbort?: (id: string, rotationId: string) => Promise; + onCopyRotationSecret?: () => void; + onDismissRotationSecret?: () => void; onModelQueryChange: (value: string) => void; onCopyModelId: (modelId: string) => void; onTestModel: (model: ExternalModelRow, protocol: GatewayInboundProtocol) => void; @@ -83,6 +90,8 @@ export default function ApiKeysWorkspace({ creating, newKey, copied, + rotationSecret = null, + rotationCopied = false, filteredModels, modelsLoading, modelsRefreshing = false, @@ -99,6 +108,11 @@ export default function ApiKeysWorkspace({ onCopyKey, onDelete, onRename, + onRotationStart, + onRotationCommit, + onRotationAbort, + onCopyRotationSecret, + onDismissRotationSecret, onModelQueryChange, onCopyModelId, onTestModel, @@ -119,9 +133,30 @@ export default function ApiKeysWorkspace({ * and can end up attached to whichever key the user selects next. */ const [renameFailed, setRenameFailed] = useState(false); const [deleteFailed, setDeleteFailed] = useState(false); + const [rotationPending, setRotationPending] = useState(false); + const [rotationFailed, setRotationFailed] = useState(false); const selected = selectedId ? (keys.find(k => k.id === selectedId) ?? null) : null; - const mutationPending = deleting || renamePending; + const mutationPending = deleting || renamePending || rotationPending; + + const runRotation = async (operation: "start" | "commit" | "abort") => { + if (!selected || rotationPending) return; + setRotationPending(true); + setRotationFailed(false); + try { + const rotationId = rotationSecret?.id === selected.id + ? rotationSecret.rotationId + : selected.pendingRotation?.id; + const ok = operation === "start" + ? (await onRotationStart?.(selected.id)) ?? false + : rotationId + ? (await (operation === "commit" ? onRotationCommit : onRotationAbort)?.(selected.id, rotationId)) ?? false + : false; + if (!ok) setRotationFailed(true); + } finally { + setRotationPending(false); + } + }; /** The strip's items. Counts sit in `meta` so the strip reports scale, not just names. */ const sectionTabs = useMemo(() => [ @@ -320,6 +355,43 @@ export default function ApiKeysWorkspace({ +
+

{t("api.rotation.title")}

+ {selected.pendingRotation ? ( + <> +

{t("api.rotation.pending")}

+

{t("api.rotation.expires")} {formatCreatedDate(selected.pendingRotation.expiresAt, localeTag)}

+ {rotationSecret?.id === selected.id && ( +
+

{t("api.rotation.secretOnce")}

+ {rotationSecret.key} + + + + +
+ )} +
+ + +
+ + ) : ( + <> +

{t("api.rotation.description")}

+ + + )} + {rotationFailed &&

{t("api.rotation.failed")}

} +

{t("api.attribution.title")}

{/* Branch on the DATASET field, not on `usage`: a key with zero diff --git a/gui/src/i18n/de.ts b/gui/src/i18n/de.ts index 18db779899..5f72b5b470 100644 --- a/gui/src/i18n/de.ts +++ b/gui/src/i18n/de.ts @@ -1478,6 +1478,17 @@ export const de: Record = { "api.key.renaming": "Wird gespeichert…", "api.key.renameFailed": "Schlüssel konnte nicht umbenannt werden. Deine Eingabe wurde behalten.", "api.key.deleting": "Wird gelöscht…", + "api.rotation.title": "Schlüsselrotation", + "api.rotation.description": "Erstellt einen Ersatzschlüssel; der aktuelle Schlüssel bleibt während einer kurzen Übergangszeit gültig.", + "api.rotation.start": "Rotation starten", + "api.rotation.starting": "Wird gestartet…", + "api.rotation.pending": "Die Rotation ist ausstehend. Aktualisiere und prüfe den Client vor dem Abschluss.", + "api.rotation.expires": "Übergangszeit endet:", + "api.rotation.secretOnce": "Ersatzschlüssel — wird nur einmal angezeigt. Vor dem Schließen kopieren.", + "api.rotation.commit": "Rotation abschließen", + "api.rotation.abort": "Rotation abbrechen", + "api.rotation.failed": "Die Rotationsaktion wurde nicht abgeschlossen. Vor einem neuen Versuch aktualisieren.", + "api.rotation.startFailed": "Schlüsselrotation konnte nicht gestartet werden.", "api.key.copyFailed": "Schlüssel konnte nicht kopiert werden. Vor dem Schließen dieses Panels manuell markieren und kopieren.", "api.attribution.title": "Zugeordnete Nutzung", "api.attribution.requests7d": "Anfragen, letzte 7 Tage", diff --git a/gui/src/i18n/en.ts b/gui/src/i18n/en.ts index dba1942dfd..49eba927eb 100644 --- a/gui/src/i18n/en.ts +++ b/gui/src/i18n/en.ts @@ -1984,6 +1984,17 @@ export const en = { "api.key.renaming": "Saving…", "api.key.renameFailed": "Could not rename the key. Your draft was kept.", "api.key.deleting": "Deleting…", + "api.rotation.title": "Key rotation", + "api.rotation.description": "Issue a replacement key while the current key remains valid for a short overlap.", + "api.rotation.start": "Start rotation", + "api.rotation.starting": "Starting…", + "api.rotation.pending": "Rotation is pending. Update and verify the client before committing.", + "api.rotation.expires": "Overlap expires:", + "api.rotation.secretOnce": "Replacement key — shown once. Copy it before closing this notice.", + "api.rotation.commit": "Commit rotation", + "api.rotation.abort": "Abort rotation", + "api.rotation.failed": "The rotation action did not complete. Refresh before retrying.", + "api.rotation.startFailed": "Could not start key rotation.", "api.key.copyFailed": "Could not copy the key. Select it and copy it manually before dismissing this panel.", "api.attribution.title": "Attributed usage", "api.attribution.requests7d": "Requests, last 7 days", diff --git a/gui/src/i18n/fr.ts b/gui/src/i18n/fr.ts index e06519c2ee..b6f8928790 100644 --- a/gui/src/i18n/fr.ts +++ b/gui/src/i18n/fr.ts @@ -1944,6 +1944,17 @@ export const fr: Record = { "api.key.renaming": "Enregistrement…", "api.key.renameFailed": "Impossible de renommer la clé. Votre brouillon a été conservé.", "api.key.deleting": "Suppression…", + "api.rotation.title": "Rotation de la clé", + "api.rotation.description": "Crée une clé de remplacement tout en conservant brièvement la clé actuelle.", + "api.rotation.start": "Démarrer la rotation", + "api.rotation.starting": "Démarrage…", + "api.rotation.pending": "La rotation est en attente. Mettez à jour et vérifiez le client avant de la valider.", + "api.rotation.expires": "Fin du chevauchement :", + "api.rotation.secretOnce": "Clé de remplacement — affichée une seule fois. Copiez-la avant de fermer.", + "api.rotation.commit": "Valider la rotation", + "api.rotation.abort": "Annuler la rotation", + "api.rotation.failed": "L’action de rotation n’a pas abouti. Actualisez avant de réessayer.", + "api.rotation.startFailed": "Impossible de démarrer la rotation de la clé.", "api.key.copyFailed": "Impossible de copier la clé. Sélectionnez-la et copiez-la manuellement avant de fermer ce panneau.", "api.attribution.title": "Utilisation attribuée", "api.attribution.requests7d": "Requêtes des 7 derniers jours", diff --git a/gui/src/i18n/ja.ts b/gui/src/i18n/ja.ts index 9b5de33c86..3169575a53 100644 --- a/gui/src/i18n/ja.ts +++ b/gui/src/i18n/ja.ts @@ -1911,6 +1911,17 @@ export const ja: Record = { "api.key.renaming": "保存中…", "api.key.renameFailed": "名前を変更できませんでした。入力内容はそのまま残しています。", "api.key.deleting": "削除中…", + "api.rotation.title": "キーのローテーション", + "api.rotation.description": "短い移行期間だけ現在のキーを有効にしたまま、置き換え用キーを発行します。", + "api.rotation.start": "ローテーションを開始", + "api.rotation.starting": "開始中…", + "api.rotation.pending": "ローテーションは保留中です。確定前にクライアントを更新して動作を確認してください。", + "api.rotation.expires": "移行期間の終了:", + "api.rotation.secretOnce": "置き換え用キー — 表示は一度だけです。閉じる前にコピーしてください。", + "api.rotation.commit": "ローテーションを確定", + "api.rotation.abort": "ローテーションを中止", + "api.rotation.failed": "操作を完了できませんでした。更新してから再試行してください。", + "api.rotation.startFailed": "キーのローテーションを開始できませんでした。", "api.key.copyFailed": "キーをコピーできませんでした。このパネルを閉じる前に手動で選択してコピーしてください。", "api.attribution.title": "キー別の使用状況", "api.attribution.requests7d": "直近 7 日のリクエスト", diff --git a/gui/src/i18n/ko.ts b/gui/src/i18n/ko.ts index 9fc2969c3f..3f83d45a77 100644 --- a/gui/src/i18n/ko.ts +++ b/gui/src/i18n/ko.ts @@ -1505,6 +1505,17 @@ export const ko: Record = { "api.key.renaming": "저장 중…", "api.key.renameFailed": "이름을 바꾸지 못했습니다. 입력한 내용은 그대로 뒀습니다.", "api.key.deleting": "삭제 중…", + "api.rotation.title": "키 교체", + "api.rotation.description": "짧은 전환 시간 동안 기존 키를 유지한 채 새 키를 발급합니다.", + "api.rotation.start": "키 교체 시작", + "api.rotation.starting": "시작하는 중…", + "api.rotation.pending": "키 교체가 대기 중입니다. 클라이언트에 새 키를 적용하고 정상 연결을 확인한 뒤 확정하세요.", + "api.rotation.expires": "전환 가능 시간:", + "api.rotation.secretOnce": "새 키는 지금 한 번만 표시됩니다. 이 안내를 닫기 전에 복사하세요.", + "api.rotation.commit": "새 키로 확정", + "api.rotation.abort": "키 교체 취소", + "api.rotation.failed": "요청을 끝내지 못했습니다. 새로고침한 뒤 다시 시도하세요.", + "api.rotation.startFailed": "키 교체를 시작하지 못했습니다.", "api.key.copyFailed": "키를 복사하지 못했습니다. 이 패널을 닫기 전에 직접 선택해서 복사하세요.", "api.attribution.title": "키별 사용량", "api.attribution.requests7d": "최근 7일 요청", diff --git a/gui/src/i18n/ru.ts b/gui/src/i18n/ru.ts index f5aa05bb69..93a2df3e04 100644 --- a/gui/src/i18n/ru.ts +++ b/gui/src/i18n/ru.ts @@ -1962,6 +1962,17 @@ export const ru: Record = { "api.key.renaming": "Сохранение…", "api.key.renameFailed": "Не удалось переименовать ключ. Введённое имя сохранено.", "api.key.deleting": "Удаление…", + "api.rotation.title": "Ротация ключа", + "api.rotation.description": "Выпускает новый ключ, сохраняя текущий на короткий переходный период.", + "api.rotation.start": "Начать ротацию", + "api.rotation.starting": "Запуск…", + "api.rotation.pending": "Ротация ожидает завершения. Обновите и проверьте клиент перед подтверждением.", + "api.rotation.expires": "Переходный период завершится:", + "api.rotation.secretOnce": "Новый ключ показывается один раз. Скопируйте его перед закрытием.", + "api.rotation.commit": "Завершить ротацию", + "api.rotation.abort": "Отменить ротацию", + "api.rotation.failed": "Операция не завершилась. Обновите данные перед повторной попыткой.", + "api.rotation.startFailed": "Не удалось начать ротацию ключа.", "api.key.copyFailed": "Не удалось скопировать ключ. Выделите и скопируйте его вручную, прежде чем закрыть панель.", "api.attribution.title": "Использование по ключам", "api.attribution.requests7d": "Запросы за 7 дней", diff --git a/gui/src/i18n/tr.ts b/gui/src/i18n/tr.ts index 82b0a6c575..785aa64669 100644 --- a/gui/src/i18n/tr.ts +++ b/gui/src/i18n/tr.ts @@ -1970,6 +1970,17 @@ export const tr: Record = { "api.key.renaming": "Kaydediliyor…", "api.key.renameFailed": "Yeniden adlandırılamadı.", "api.key.deleting": "Siliniyor…", + "api.rotation.title": "Anahtar döndürme", + "api.rotation.description": "Mevcut anahtarı kısa bir geçiş süresince geçerli tutarak yeni anahtar oluşturur.", + "api.rotation.start": "Döndürmeyi başlat", + "api.rotation.starting": "Başlatılıyor…", + "api.rotation.pending": "Döndürme bekliyor. Onaylamadan önce istemciyi güncelleyip doğrulayın.", + "api.rotation.expires": "Geçiş süresi sonu:", + "api.rotation.secretOnce": "Yeni anahtar yalnızca bir kez gösterilir. Kapatmadan önce kopyalayın.", + "api.rotation.commit": "Döndürmeyi onayla", + "api.rotation.abort": "Döndürmeyi iptal et", + "api.rotation.failed": "İşlem tamamlanmadı. Yeniden denemeden önce yenileyin.", + "api.rotation.startFailed": "Anahtar döndürme başlatılamadı.", "api.key.copyFailed": "Otomatik kopyalanamadı. Kapatmadan önce anahtarı manuel olarak seçip kopyalayın — tekrar gösterilmeyecektir.", "api.attribution.title": "Atfedilen kullanım", "api.attribution.requests7d": "Son 7 gün istekleri", diff --git a/gui/src/i18n/zh-TW.ts b/gui/src/i18n/zh-TW.ts index 6974aaabc6..85848408cb 100644 --- a/gui/src/i18n/zh-TW.ts +++ b/gui/src/i18n/zh-TW.ts @@ -1490,6 +1490,17 @@ export const zhTW: Record = { "api.key.renaming": "儲存中…", "api.key.renameFailed": "無法重新命名金鑰。你的草稿已保留。", "api.key.deleting": "刪除中…", + "api.rotation.title": "金鑰輪替", + "api.rotation.description": "簽發替代金鑰,並在短暫轉換期間保留目前金鑰。", + "api.rotation.start": "開始輪替", + "api.rotation.starting": "正在開始…", + "api.rotation.pending": "輪替尚待確認。請先更新並驗證用戶端,再提交輪替。", + "api.rotation.expires": "轉換期間截止:", + "api.rotation.secretOnce": "替代金鑰只顯示一次。關閉前請先複製。", + "api.rotation.commit": "提交輪替", + "api.rotation.abort": "中止輪替", + "api.rotation.failed": "輪替操作未完成。請重新整理後再試。", + "api.rotation.startFailed": "無法開始金鑰輪替。", "api.key.copyFailed": "無法複製金鑰。請手動選取並複製後再關閉此面板。", "api.attribution.title": "已歸因用量", "api.attribution.requests7d": "請求數,最近 7 天", diff --git a/gui/src/i18n/zh.ts b/gui/src/i18n/zh.ts index 2fe77941a1..58fb365511 100644 --- a/gui/src/i18n/zh.ts +++ b/gui/src/i18n/zh.ts @@ -1498,6 +1498,17 @@ export const zh: Record = { "api.key.renaming": "保存中…", "api.key.renameFailed": "无法重命名密钥,已保留你输入的内容。", "api.key.deleting": "删除中…", + "api.rotation.title": "密钥轮换", + "api.rotation.description": "签发替换密钥,并在短暂过渡期内保留当前密钥。", + "api.rotation.start": "开始轮换", + "api.rotation.starting": "正在开始…", + "api.rotation.pending": "轮换待确认。请先更新并验证客户端,再提交轮换。", + "api.rotation.expires": "过渡期截止:", + "api.rotation.secretOnce": "替换密钥仅显示一次。关闭前请先复制。", + "api.rotation.commit": "提交轮换", + "api.rotation.abort": "中止轮换", + "api.rotation.failed": "轮换操作未完成。请刷新后重试。", + "api.rotation.startFailed": "无法开始密钥轮换。", "api.key.copyFailed": "无法复制密钥。关闭此面板前请手动选中并复制。", "api.attribution.title": "按密钥统计的用量", "api.attribution.requests7d": "最近 7 天请求数", diff --git a/gui/src/pages/ApiKeys.tsx b/gui/src/pages/ApiKeys.tsx index 544b321ef4..cf212a5cf7 100644 --- a/gui/src/pages/ApiKeys.tsx +++ b/gui/src/pages/ApiKeys.tsx @@ -48,6 +48,10 @@ interface CreateKeyResponse { key?: unknown; } +interface StartRotationResponse extends CreateKeyResponse { + rotationId?: unknown; +} + type CachedKeysShape = { keys: ApiKeyEntry[]; endpoints: ApiEndpointInfo; @@ -82,10 +86,19 @@ function seedEndpointsFromApiBase(apiBase: string): ApiEndpointInfo { /** Session-cache entries get the same scrutiny as a network payload. */ function validCachedKeys(cached: CachedKeysShape | null): CachedKeysShape | null { if (!cached || !isApiAuthMatrix(cached.authMatrix)) return null; - if (!Array.isArray(cached.keys) || cached.keys.some(key => !key || !isApiKeyUsage(key.usage))) return null; + if (!Array.isArray(cached.keys) || cached.keys.some(key => !key || !isApiKeyUsage(key.usage) || !validPendingRotation(key.pendingRotation))) return null; return cached; } +function validPendingRotation(value: ApiKeyEntry["pendingRotation"] | unknown): boolean { + if (value === undefined) return true; + if (!value || typeof value !== "object" || Array.isArray(value)) return false; + const pending = value as Record; + return typeof pending.id === "string" && !!pending.id + && typeof pending.createdAt === "string" && !Number.isNaN(Date.parse(pending.createdAt)) + && typeof pending.expiresAt === "string" && !Number.isNaN(Date.parse(pending.expiresAt)); +} + /** * `active` gates both resources. As one panel of the Integrations tab strip * this stays mounted while hidden — which is what preserves in-progress key @@ -116,6 +129,8 @@ export default function ApiKeys({ apiBase, active = true }: { apiBase: string; a const [creating, setCreating] = useState(false); const [newKey, setNewKey] = useState(null); const [copied, setCopied] = useState(false); + const [rotationSecret, setRotationSecret] = useState<{ id: string; key: string; rotationId: string } | null>(null); + const [rotationCopied, setRotationCopied] = useState(false); const creatingRef = useRef(false); const fetchKeys = useCallback(async (signal: AbortSignal): Promise => { @@ -125,7 +140,7 @@ export default function ApiKeys({ apiBase, active = true }: { apiBase: string; a // the rules from memory, which is the defect this replaces. if (!data || !isApiAuthMatrix(data.authMatrix)) throw new Error(t("api.keysLoadFailed")); const rows = data.keys ?? []; - if (rows.some(key => !isApiKeyUsage(key.usage))) throw new Error(t("api.keysLoadFailed")); + if (rows.some(key => !isApiKeyUsage(key.usage) || !validPendingRotation(key.pendingRotation))) throw new Error(t("api.keysLoadFailed")); const validatedKeys = rows as ApiKeyEntry[]; const derived = deriveApiEndpoints(data.endpoint ?? ""); const next: CachedKeysShape = { @@ -306,6 +321,60 @@ export default function ApiKeys({ apiBase, active = true }: { apiBase: string; a } }; + const handleRotationStart = async (id: string): Promise => { + setActionError(null); + const bounded = createBoundedFetch(MUTATION_TIMEOUT_MS); + try { + const res = await fetch(`${apiBase}/api/keys/rotate`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ id }), + signal: bounded.signal, + }); + const data = await readJsonOrThrow(res, t("api.rotation.startFailed")); + if (typeof data.key !== "string" || !data.key || typeof data.rotationId !== "string" || !data.rotationId) return false; + setRotationSecret({ id, key: data.key, rotationId: data.rotationId }); + refreshKeys(); + return true; + } catch { + return false; + } finally { + bounded.clear(); + } + }; + + const finishRotation = async (id: string, rotationId: string, operation: "commit" | "abort"): Promise => { + setActionError(null); + const bounded = createBoundedFetch(MUTATION_TIMEOUT_MS); + try { + const res = await fetch(`${apiBase}${operation === "commit" ? "/api/keys/rotate/commit" : "/api/keys/rotate"}`, { + method: operation === "commit" ? "POST" : "DELETE", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ id, rotationId }), + signal: bounded.signal, + }); + if (!res.ok) return false; + setRotationSecret(current => current?.id === id ? null : current); + refreshKeys(); + return true; + } catch { + return false; + } finally { + bounded.clear(); + } + }; + + const copyRotationSecret = async () => { + if (!rotationSecret) return; + try { + await navigator.clipboard.writeText(rotationSecret.key); + setRotationCopied(true); + window.setTimeout(() => setRotationCopied(false), 2000); + } catch { + setActionError(t("api.key.copyFailed")); + } + }; + const copyKey = async () => { if (!newKey) return; setActionError(null); @@ -440,6 +509,8 @@ export default function ApiKeys({ apiBase, active = true }: { apiBase: string; a creating={creating} newKey={newKey} copied={copied} + rotationSecret={rotationSecret} + rotationCopied={rotationCopied} filteredModels={filteredModels} modelsLoading={modelsState.showSkeleton && !modelsState.data && !cachedModels} // Only announce progress on a retry after failure — quiet warm revisits stay silent. @@ -459,6 +530,11 @@ export default function ApiKeys({ apiBase, active = true }: { apiBase: string; a onCopyKey={() => { void copyKey(); }} onDelete={handleDelete} onRename={handleRename} + onRotationStart={handleRotationStart} + onRotationCommit={(id, rotationId) => finishRotation(id, rotationId, "commit")} + onRotationAbort={(id, rotationId) => finishRotation(id, rotationId, "abort")} + onCopyRotationSecret={() => { void copyRotationSecret(); }} + onDismissRotationSecret={() => setRotationSecret(null)} onModelQueryChange={setModelQuery} onCopyModelId={(modelId) => { void copyModelId(modelId); }} onTestModel={(model, protocol) => { void testModel(model, protocol); }} diff --git a/gui/src/pages/api-keys-utils.ts b/gui/src/pages/api-keys-utils.ts index 0732a0ce91..48b176d29b 100644 --- a/gui/src/pages/api-keys-utils.ts +++ b/gui/src/pages/api-keys-utils.ts @@ -14,6 +14,7 @@ export interface ApiKeyEntry { name: string; prefix: string; createdAt: string; + pendingRotation?: { id: string; createdAt: string; expiresAt: string }; /** Always present from the server; zeroes are a real answer. Whether anything * is attributable at all is the response-level `attributionSince`. */ usage: ApiKeyUsage; From be2ee7d53f7644954d78aaa7948af1a98246a854 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 04:58:47 +0900 Subject: [PATCH 079/172] test(hardening): cover rotation pairing catalog and relay negatives --- gui/tests/apikeys-actions.test.tsx | 35 ++++++ tests/api-key-attribution.test.ts | 31 +++++ tests/api-keys-routes.test.ts | 64 +++++++++- tests/client-connect.test.ts | 124 +++++++++++++++++++- tests/client-hub-relay.test.ts | 32 +++++ tests/data-plane-admission-identity.test.ts | 13 ++ tests/proxy-liveness.test.ts | 28 +++++ tests/remote-catalog.test.ts | 87 ++++++++++++++ tests/server-management-auth.test.ts | 69 +++++++++++ tests/service-secrets.test.ts | 51 ++++++++ 10 files changed, 531 insertions(+), 3 deletions(-) create mode 100644 tests/remote-catalog.test.ts diff --git a/gui/tests/apikeys-actions.test.tsx b/gui/tests/apikeys-actions.test.tsx index 3b1d1520a6..ae3bffecef 100644 --- a/gui/tests/apikeys-actions.test.tsx +++ b/gui/tests/apikeys-actions.test.tsx @@ -249,6 +249,41 @@ test("without a fresh key the protocol chips are disabled, not silently passing" expect(chips.every(c => (c.getAttribute("title") ?? "").length > 0)).toBe(true); }); +test("rotation start, one-time secret, commit, and abort stay explicit", async () => { + const calls: string[] = []; + const container = await mount({ + onRotationStart: async id => { calls.push(`start:${id}`); return true; }, + }); + await openKey(container); + await act(async () => { button(container, "Start rotation").click(); await Promise.resolve(); }); + expect(calls).toEqual(["start:k1"]); + + await act(async () => { active?.unmount(); active = null; }); + const pending = await mount({ + keys: [{ + id: "k1", + name: "alpha", + prefix: "ocx_data_aaaaaaaa...", + createdAt: "2026-01-01T00:00:00.000Z", + pendingRotation: { + id: "rotation-1", + createdAt: "2026-08-28T00:00:00.000Z", + expiresAt: "2026-08-28T00:10:00.000Z", + }, + usage: { requests7d: 0, totalRequests: 0 }, + }], + rotationSecret: { id: "k1", key: "ocx_data_shown_once", rotationId: "rotation-1" }, + onRotationCommit: async (id, rotationId) => { calls.push(`commit:${id}:${rotationId}`); return true; }, + onRotationAbort: async (id, rotationId) => { calls.push(`abort:${id}:${rotationId}`); return true; }, + }); + await openKey(pending); + expect(pending.textContent).toContain("ocx_data_shown_once"); + await act(async () => { button(pending, "Commit rotation").click(); await Promise.resolve(); }); + await act(async () => { button(pending, "Abort rotation").click(); await Promise.resolve(); }); + expect(calls).toContain("commit:k1:rotation-1"); + expect(calls).toContain("abort:k1:rotation-1"); +}); + test("a protocol result belongs to its own chip", async () => { const container = await mount({ filteredModels: [{ id: "gpt-5.4", displayName: "gpt-5.4", provider: "openai", native: true }], diff --git a/tests/api-key-attribution.test.ts b/tests/api-key-attribution.test.ts index 2bbe502007..f2f9d23125 100644 --- a/tests/api-key-attribution.test.ts +++ b/tests/api-key-attribution.test.ts @@ -65,6 +65,37 @@ afterEach(() => { }); describe("attribution reaches usage.jsonl", () => { + test("traffic before, during, and after rotation stays in one apiKeyId bucket", async () => { + saveConfig(remoteConfig()); + const server = startServer(0); + const send = (token: string) => fetch(new URL("/v1/chat/completions", server.url), { + method: "POST", + headers: { "content-type": "application/json", "x-opencodex-api-key": token }, + body: JSON.stringify({ model: "test/gpt-test", messages: [{ role: "user", content: "hi" }] }), + }); + const manage = async (path: string, method: string, body: unknown) => { + const response = await fetch(new URL(path, server.url), { + method, + headers: { "content-type": "application/json", "x-opencodex-api-key": ADMIN_TOKEN }, + body: JSON.stringify(body), + }); + return { response, body: await response.json() as Record }; + }; + try { + await send("ocx_data_attributionone"); + const started = await manage("/api/keys/rotate", "POST", { id: "key-one" }); + const pendingKey = started.body.key as string; + const rotationId = started.body.rotationId as string; + await send(pendingKey); + await manage("/api/keys/rotate/commit", "POST", { id: "key-one", rotationId }); + await send(pendingKey); + expect((await send("ocx_data_attributionone")).status).toBe(401); + expect(usageRows().slice(-3).map(row => row.apiKeyId)).toEqual(["key-one", "key-one", "key-one"]); + } finally { + await server.stop(true); + } + }); + test("an authed request is attributed to the key that opened it, all the way to disk", async () => { saveConfig(remoteConfig()); const server = startServer(0); diff --git a/tests/api-keys-routes.test.ts b/tests/api-keys-routes.test.ts index 267916701b..5b08978bba 100644 --- a/tests/api-keys-routes.test.ts +++ b/tests/api-keys-routes.test.ts @@ -55,7 +55,16 @@ async function keysRequest( method: string, body?: unknown, ): Promise<{ status: number; json: Record }> { - const res = await fetch(new URL("/api/keys", server.url), { + return managementRequest(server, "/api/keys", method, body); +} + +async function managementRequest( + server: { url: URL }, + path: string, + method: string, + body?: unknown, +): Promise<{ status: number; json: Record }> { + const res = await fetch(new URL(path, server.url), { method, headers: { "Content-Type": "application/json", "x-opencodex-api-key": ADMIN_TOKEN }, ...(body === undefined ? {} : { body: typeof body === "string" ? body : JSON.stringify(body) }), @@ -65,6 +74,59 @@ async function keysRequest( return { status: res.status, json }; } +describe("API key rotation", () => { + test("overlaps under one id, masks the pending secret, and commits atomically", async () => { + saveConfig(baseConfig()); + const server = startServer(0); + try { + const created = await keysRequest(server, "POST", { name: "client" }); + const oldKey = created.json.key as string; + const id = created.json.id as string; + const started = await managementRequest(server, "/api/keys/rotate", "POST", { id }); + expect(started.status).toBe(201); + const newKey = started.json.key as string; + const rotationId = started.json.rotationId as string; + expect(newKey).toMatch(/^ocx_data_[0-9a-f]{40}$/); + expect(newKey).not.toBe(oldKey); + expect(isDataPlaneAdmissionSecret(oldKey, loadConfig())).toBe(true); + expect(isDataPlaneAdmissionSecret(newKey, loadConfig())).toBe(true); + + const listed = await keysRequest(server, "GET"); + expect(JSON.stringify(listed.json)).not.toContain(newKey); + expect((listed.json.keys as Array>)[0]?.pendingRotation).toMatchObject({ id: rotationId }); + expect((await managementRequest(server, "/api/keys/rotate", "POST", { id })).status).toBe(409); + + const committed = await managementRequest(server, "/api/keys/rotate/commit", "POST", { id, rotationId }); + expect(committed.status).toBe(200); + expect(isDataPlaneAdmissionSecret(oldKey, loadConfig())).toBe(false); + expect(isDataPlaneAdmissionSecret(newKey, loadConfig())).toBe(true); + expect((loadConfig().apiKeys ?? [])[0]?.id).toBe(id); + } finally { + await server.stop(true); + } + }); + + test("abort preserves the old key and malformed bodies cannot alter pending state", async () => { + saveConfig(baseConfig()); + const server = startServer(0); + try { + const created = await keysRequest(server, "POST", { name: "client" }); + const id = created.json.id as string; + const oldKey = created.json.key as string; + expect((await managementRequest(server, "/api/keys/rotate", "POST", { id, extra: true })).status).toBe(400); + const started = await managementRequest(server, "/api/keys/rotate", "POST", { id }); + const newKey = started.json.key as string; + const rotationId = started.json.rotationId as string; + expect((await managementRequest(server, "/api/keys/rotate/commit", "POST", { id, rotationId, extra: true })).status).toBe(400); + expect((await managementRequest(server, "/api/keys/rotate", "DELETE", { id, rotationId })).status).toBe(200); + expect(isDataPlaneAdmissionSecret(oldKey, loadConfig())).toBe(true); + expect(isDataPlaneAdmissionSecret(newKey, loadConfig())).toBe(false); + } finally { + await server.stop(true); + } + }); +}); + beforeEach(() => { testHome = mkdtempSync(join(tmpdir(), "ocx-api-keys-routes-")); process.env.OPENCODEX_HOME = testHome; diff --git a/tests/client-connect.test.ts b/tests/client-connect.test.ts index 7e9f0563f0..10b66fabfc 100644 --- a/tests/client-connect.test.ts +++ b/tests/client-connect.test.ts @@ -1,7 +1,7 @@ import { describe, expect, spyOn, test } from "bun:test"; import { createHash } from "node:crypto"; import { spawnSync } from "node:child_process"; -import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; @@ -157,7 +157,7 @@ describe("remote hub client boundary", () => { await expect(downloadClientCatalog("https://hub.example.test", "ocx_data_test", { maxBytes: 4, - fetchImpl: async () => new Response('{"models":[]}'), + fetchImpl: async () => new Response('{"models":[]}', { headers: { "Content-Type": "application/json" } }), })).rejects.toThrow("allowed size"); }); @@ -478,3 +478,123 @@ describe("connected sync and disconnect conflicts", () => { } finally { run.cleanup(); } }); }); + +describe("recoverable connected key rotation", () => { + test("a dropped first commit is recovered from doubly-accepted current and .prev keys", () => { + const opencodexHome = mkdtempSync(join(tmpdir(), "ocx-client-rotation-")); + const oldKey = `ocx_data_${"1".repeat(40)}`; + const newKey = `ocx_data_${"2".repeat(40)}`; + const oldFingerprint = createHash("sha256").update(oldKey).digest("hex"); + writeFileSync(join(opencodexHome, "config.json"), JSON.stringify({ + port: 10100, + providers: {}, + defaultProvider: "openai", + runtimeRole: "client", + client: { + serverUrl: "https://hub.example.test", + managementUrl: "https://hub.example.test", + managementTransport: "direct", + selectedClients: ["claude"], + tokenEnv: "OPENCODEX_API_AUTH_TOKEN", + apiKeyId: "client-key-1", + tokenFingerprint: oldFingerprint, + protocolVersion: 1, + connectedAt: "2026-08-28T00:00:00.000Z", + }, + })); + writeFileSync(join(opencodexHome, "service-api-token"), `${oldKey}\n`, { mode: 0o600 }); + const script = ` + const fs = require("node:fs"); + const path = require("node:path"); + const { rotateConnectedClientKey } = require("./src/client/connect"); + const { readClientConnectionState } = require("./src/client/state"); + let commitCalls = 0; + let committed = false; + const oldKey = ${JSON.stringify(oldKey)}; + const newKey = ${JSON.stringify(newKey)}; + const fetchImpl = async (input, init = {}) => { + const url = String(input); + if (url.endsWith("/api/keys/rotate") && init.method === "POST") return Response.json({ + id: "client-key-1", name: "client", key: newKey, + createdAt: "2026-08-28T00:00:01.000Z", rotationId: "rotation-1", + expiresAt: "2026-08-28T00:10:01.000Z", + }, { status: 201 }); + if (url.endsWith("/api/keys/rotate/commit")) { + commitCalls += 1; + if (commitCalls === 1) throw new Error("dropped commit response"); + committed = true; + return Response.json({ ok: true }); + } + if (url.endsWith("/v1/catalog")) { + const token = new Headers(init.headers).get("x-opencodex-api-key"); + const accepted = token === newKey || (!committed && token === oldKey); + return accepted + ? new Response('{"models":[]}', { headers: { "Content-Type": "application/json", "X-OpenCodex-Key-Id": "client-key-1" } }) + : Response.json({ error: "unauthorized" }, { status: 401 }); + } + throw new Error("unexpected request " + url); + }; + (async () => { + const credential = new TextEncoder().encode("ocx_admin_rotation_test"); + const result = await rotateConnectedClientKey({ credential: { kind: "admin", value: credential } }, { fetchImpl }); + console.log(JSON.stringify({ + result, + state: readClientConnectionState(), + token: fs.readFileSync(path.join(process.env.OPENCODEX_HOME, "service-api-token"), "utf8").trim(), + backup: fs.existsSync(path.join(process.env.OPENCODEX_HOME, "service-api-token.prev")), + commitCalls, + credentialZeroed: credential.every(value => value === 0), + })); + })(); + `; + const child = spawnSync(process.execPath, ["--eval", script], { + cwd: repoRoot, + env: { ...process.env, OPENCODEX_HOME: opencodexHome }, + encoding: "utf8", + }); + try { + expect(child.status).toBe(0); + const result = JSON.parse(child.stdout.trim().split("\n").at(-1) ?? "{}") as Record; + expect(result.commitCalls).toBe(2); + expect(result.token).toBe(newKey); + expect(result.backup).toBe(false); + expect(result.state).toMatchObject({ kind: "connected", value: { apiKeyId: "client-key-1" } }); + expect(result.state.value.pendingOperation).toBeUndefined(); + expect(result.credentialZeroed).toBe(true); + } finally { + rmSync(opencodexHome, { recursive: true, force: true }); + } + }); + + test("status removes a .prev orphan only when no rotation marker exists", async () => { + const home = mkdtempSync(join(tmpdir(), "ocx-client-orphan-")); + const token = `ocx_data_${"3".repeat(40)}`; + const fingerprint = createHash("sha256").update(token).digest("hex"); + writeFileSync(join(home, "config.json"), JSON.stringify({ + port: 10100, providers: {}, defaultProvider: "openai", runtimeRole: "client", + client: { + serverUrl: "https://hub.example.test", managementUrl: "https://hub.example.test", + managementTransport: "direct", selectedClients: ["claude"], tokenEnv: "OPENCODEX_API_AUTH_TOKEN", + apiKeyId: "client-key-1", tokenFingerprint: fingerprint, protocolVersion: 1, + connectedAt: "2026-08-28T00:00:00.000Z", + }, + })); + writeFileSync(join(home, "service-api-token"), `${token}\n`, { mode: 0o600 }); + writeFileSync(join(home, "service-api-token.prev"), `${token}\n`, { mode: 0o600 }); + const previous = process.env.OPENCODEX_HOME; + process.env.OPENCODEX_HOME = home; + try { + const errors: string[] = []; + const spy = spyOn(console, "error").mockImplementation(value => errors.push(String(value))); + try { expect(await handleConnectCommand(["status", "--json"])).toBe(0); } + finally { spy.mockRestore(); } + expect(existsSync(join(home, "service-api-token.prev"))).toBe(false); + expect(readFileSync(join(home, "service-api-token"), "utf8").trim()).toBe(token); + expect(errors.join(" ")).not.toContain(token); + } finally { + if (previous === undefined) delete process.env.OPENCODEX_HOME; + else process.env.OPENCODEX_HOME = previous; + rmSync(home, { recursive: true, force: true }); + } + }); +}); diff --git a/tests/client-hub-relay.test.ts b/tests/client-hub-relay.test.ts index 3c38a38085..0b73b93bdf 100644 --- a/tests/client-hub-relay.test.ts +++ b/tests/client-hub-relay.test.ts @@ -3,6 +3,7 @@ import { HUB_RELAY_REQUEST_BODY_MAX_BYTES, HUB_RELAY_RESPONSE_BODY_MAX_BYTES, relayHubManagementRequest, + validateHubRelayRequestHeaders, } from "../src/client/hub-relay"; const target = { managementUrl: "https://hub.example.test", browserOrigin: "http://127.0.0.1:10100" }; @@ -27,6 +28,19 @@ function relayRequest(path: string, init: RequestInit = {}): Request { } describe("fixed-target hub management relay", () => { + test("raw header validation rejects CL/TE ambiguity, duplicate lengths, upgrade, and CRLF", () => { + for (const headers of [ + [["Content-Length", "1"], ["Transfer-Encoding", "chunked"]], + [["Content-Length", "1"], ["Content-Length", "2"]], + [["Content-Length", "1, 2"]], + [["Upgrade", "websocket"]], + [["X-Test", "ok\r\ninjected: yes"]], + ] as const) expect(validateHubRelayRequestHeaders(headers).ok).toBe(false); + const valid = validateHubRelayRequestHeaders([["Connection", "X-OpenCodex-API-Key"], ["X-OpenCodex-API-Key", "session"]]); + expect(valid.ok).toBe(true); + if (valid.ok) expect(valid.connectionNamed.has("x-opencodex-api-key")).toBe(true); + }); + test("forwards only to the configured hub and strips machine, cookie, forwarding, and hop headers", async () => { let captured: { url: string; headers: Headers } | null = null; const response = await relayHubManagementRequest(relayRequest("/api/usage?range=all"), "/api/usage?range=all", target, { @@ -112,4 +126,22 @@ describe("fixed-target hub management relay", () => { }); expect(timedOut.status).toBe(502); }); + + test("strips response headers nominated by Connection and propagates browser cancellation", async () => { + let cancelled = false; + const upstreamBody = new ReadableStream({ + start(controller) { controller.enqueue(new TextEncoder().encode("first")); }, + cancel() { cancelled = true; }, + }); + const response = await relayHubManagementRequest(relayRequest("/api/config"), "/api/config", target, { + fetchImpl: (async () => new Response(upstreamBody, { + headers: { "Content-Type": "application/json", Connection: "ETag", ETag: "secret-validator" }, + })) as typeof fetch, + }); + expect(response.headers.get("etag")).toBeNull(); + const reader = response.body!.getReader(); + expect((await reader.read()).done).toBe(false); + await reader.cancel(); + expect(cancelled).toBe(true); + }); }); diff --git a/tests/data-plane-admission-identity.test.ts b/tests/data-plane-admission-identity.test.ts index 13f4d6b76e..99c7a2a8cb 100644 --- a/tests/data-plane-admission-identity.test.ts +++ b/tests/data-plane-admission-identity.test.ts @@ -56,6 +56,19 @@ afterEach(() => { }); describe("resolveDataPlaneAdmissionSecret", () => { + test("current and unexpired pending secrets resolve to the same stable id", () => { + const config = remoteConfig(); + config.apiKeys![0]!.pendingRotation = { + id: "rotation-1", + key: "ocx_data_pendingsecret", + createdAt: new Date(Date.now() - 1_000).toISOString(), + expiresAt: new Date(Date.now() + 60_000).toISOString(), + }; + expect(resolveDataPlaneAdmissionSecret("ocx_data_firstsecret", config)).toMatchObject({ keyId: "first-key" }); + expect(resolveDataPlaneAdmissionSecret("ocx_data_pendingsecret", config)).toMatchObject({ keyId: "first-key" }); + config.apiKeys![0]!.pendingRotation!.expiresAt = new Date(Date.now() - 1).toISOString(); + expect(resolveDataPlaneAdmissionSecret("ocx_data_pendingsecret", config)).toBeNull(); + }); test("names the configured key that actually matched", () => { const config = remoteConfig(); expect(resolveDataPlaneAdmissionSecret("ocx_data_firstsecret", config)).toEqual({ diff --git a/tests/proxy-liveness.test.ts b/tests/proxy-liveness.test.ts index 52c2b91919..b0e1b6fd48 100644 --- a/tests/proxy-liveness.test.ts +++ b/tests/proxy-liveness.test.ts @@ -43,6 +43,34 @@ describe("isOpencodexHealthz", () => { }); }); +describe("remote protocol feature negotiation", () => { + test("intersects additive features and rejects incompatible floors", () => { + const compatible = checkRemoteProtocolCompatibility({ + protocol: 2, + minimumClientProtocol: 1, + managementUrl: "https://hub.example.test", + features: ["rotation", "future"], + }, { protocol: 1, minimumHubProtocol: 1, features: ["rotation"] }); + expect(compatible.ok).toBe(true); + if (compatible.ok) expect([...compatible.features]).toEqual(["rotation"]); + expect(checkRemoteProtocolCompatibility({ + protocol: 2, minimumClientProtocol: 2, managementUrl: "https://hub.example.test", + }, { protocol: 1, minimumHubProtocol: 1 }).ok).toBe(false); + expect(checkRemoteProtocolCompatibility({ + protocol: 1, minimumClientProtocol: 1, managementUrl: "https://hub.example.test", + }, { protocol: 2, minimumHubProtocol: 2 }).ok).toBe(false); + }); + + test.each([undefined, 0, Number.NaN, 1.5, -1])("rejects malformed protocol %p as invalid", protocol => { + const result = checkRemoteProtocolCompatibility({ + protocol, + minimumClientProtocol: 1, + managementUrl: "https://hub.example.test", + }); + expect(result).toMatchObject({ ok: false, reason: "invalid" }); + }); +}); + describe("probeHostname", () => { test("wildcards and empty answer on IPv4 loopback; concrete hosts pass through", () => { expect(probeHostname(undefined)).toBe("127.0.0.1"); diff --git a/tests/remote-catalog.test.ts b/tests/remote-catalog.test.ts new file mode 100644 index 0000000000..840e775645 --- /dev/null +++ b/tests/remote-catalog.test.ts @@ -0,0 +1,87 @@ +import { describe, expect, test } from "bun:test"; +import { downloadClientCatalog, HubClientError } from "../src/client/hub-client"; + +const JSON_HEADERS = { "Content-Type": "application/json", ETag: '"catalog-v1"' }; + +function response(body: string, headers: HeadersInit = JSON_HEADERS): Response { + return new Response(body, { headers }); +} + +describe("remote catalog adversarial consumer", () => { + test("accepts additive fields only after the required model schema and key id pass", async () => { + const body = JSON.stringify({ models: [{ slug: "provider/model", future: { enabled: true } }], futureTop: 1 }); + const result = await downloadClientCatalog("https://hub.example.test", "ocx_data_test", { + fetchImpl: async () => response(body, { ...JSON_HEADERS, "X-OpenCodex-Key-Id": "client-key-1" }), + }); + expect(result).toEqual({ kind: "fresh", body, etag: '"catalog-v1"', keyId: "client-key-1" }); + }); + + test.each([ + ["malformed JSON", "{", "catalog_invalid"], + ["null top level", "null", "catalog_schema_invalid"], + ["array top level", "[]", "catalog_schema_invalid"], + ["missing models", "{}", "catalog_schema_invalid"], + ["non-array models", '{"models":{}}', "catalog_schema_invalid"], + ["non-object row", '{"models":[null]}', "catalog_schema_invalid"], + ["empty slug", '{"models":[{"slug":""}]}', "catalog_schema_invalid"], + ["control slug", '{"models":[{"slug":"bad\\u0000slug"}]}', "catalog_schema_invalid"], + ["duplicate slug", '{"models":[{"slug":"a"},{"slug":"a"}]}', "catalog_schema_invalid"], + ])("rejects %s without returning writable bytes", async (_label, body, code) => { + let caught: unknown; + try { + await downloadClientCatalog("https://hub.example.test", "ocx_data_test", { + fetchImpl: async () => response(body), + }); + } catch (error) { caught = error; } + expect(caught).toBeInstanceOf(HubClientError); + expect((caught as HubClientError).code).toBe(code); + }); + + test("rejects 2,001 rows and a forged small Content-Length with oversized chunks", async () => { + const rows = JSON.stringify({ models: Array.from({ length: 2_001 }, (_, index) => ({ slug: `p/m-${index}` })) }); + await expect(downloadClientCatalog("https://hub.example.test", "ocx_data_test", { + fetchImpl: async () => response(rows), + })).rejects.toMatchObject({ code: "catalog_schema_invalid" }); + + const stream = new ReadableStream({ + start(controller) { + controller.enqueue(new TextEncoder().encode('{"models":[')); + controller.enqueue(new Uint8Array(128).fill(0x61)); + controller.close(); + }, + }); + await expect(downloadClientCatalog("https://hub.example.test", "ocx_data_test", { + maxBytes: 32, + fetchImpl: async () => new Response(stream, { headers: { "Content-Type": "application/json", "Content-Length": "1" } }), + })).rejects.toMatchObject({ code: "body_too_large" }); + }); + + test("allows the exact byte cap and retries one unconditional request after 304 without LKG", async () => { + const body = '{"models":[]}'; + const exact = await downloadClientCatalog("https://hub.example.test", "ocx_data_test", { + maxBytes: new TextEncoder().encode(body).byteLength, + fetchImpl: async () => response(body), + }); + expect(exact.kind).toBe("fresh"); + + let calls = 0; + const refreshed = await downloadClientCatalog("https://hub.example.test", "ocx_data_test", { + fetchImpl: async (_input, init) => { + calls += 1; + expect(new Headers(init?.headers).has("if-none-match")).toBe(false); + return calls === 1 ? new Response(null, { status: 304 }) : response(body); + }, + }); + expect(calls).toBe(2); + expect(refreshed.kind).toBe("fresh"); + }); + + test("a second 304 without LKG is a protocol error and non-JSON content is refused", async () => { + await expect(downloadClientCatalog("https://hub.example.test", "ocx_data_test", { + fetchImpl: async () => new Response(null, { status: 304 }), + })).rejects.toMatchObject({ code: "catalog_304_without_lkg" }); + await expect(downloadClientCatalog("https://hub.example.test", "ocx_data_test", { + fetchImpl: async () => new Response('{"models":[]}', { headers: { "Content-Type": "text/html" } }), + })).rejects.toMatchObject({ code: "catalog_content_type_invalid" }); + }); +}); diff --git a/tests/server-management-auth.test.ts b/tests/server-management-auth.test.ts index 091aea7092..cbcdf16161 100644 --- a/tests/server-management-auth.test.ts +++ b/tests/server-management-auth.test.ts @@ -1074,6 +1074,75 @@ describe("management and data-plane credential separation", () => { )).toBeNull(); }); + test("pairing burns a grant after five failures and rate-limits a source after ten guesses", () => { + const config = hubConfig(); + const state = initializeManagementAuthState(config); + if (!state.available) throw new Error("expected management auth state"); + const now = 1_800_000_000_000; + const created = createGuiPairingGrant("https://dashboard.example.test", config, state, now); + const context = { + ingress: "public" as const, + peerAddress: "192.0.2.10", + tailscaleUser: null, + browserOrigin: "https://evil.example.test", + }; + const wrongOrigin = new Request("https://hub.example.test/opencodex-session", { + method: "POST", + headers: { Host: "hub.example.test", Origin: "https://evil.example.test" }, + }); + for (let attempt = 1; attempt <= 4; attempt++) { + expect(consumeGuiPairingGrant(wrongOrigin, { grant: created.grant }, config, state, now + attempt, context)).toBeNull(); + } + expect(consumeGuiPairingGrant(wrongOrigin, { grant: created.grant }, config, state, now + 5, context)) + .toMatchObject({ allowed: false, reason: "grant" }); + expect(state.pairingGrants.size).toBe(0); + + const guessContext = { ...context, peerAddress: "192.0.2.11" }; + const validOrigin = new Request("https://hub.example.test/opencodex-session", { + method: "POST", + headers: { Host: "hub.example.test", Origin: "https://dashboard.example.test" }, + }); + for (let attempt = 1; attempt <= 9; attempt++) { + expect(consumeGuiPairingGrant(validOrigin, { grant: `ocx_pair_${String(attempt).padStart(43, "a")}` }, config, state, now + attempt, guessContext)).toBeNull(); + } + expect(consumeGuiPairingGrant(validOrigin, { grant: `ocx_pair_${"z".repeat(43)}` }, config, state, now + 10, guessContext)) + .toMatchObject({ allowed: false, reason: "source" }); + }); + + test("self logout revokes only the current GUI session and admin credentials get 403", async () => { + const config = remoteConfig(); + saveConfig(config); + const state = initializeManagementAuthState(config); + if (!state.available) throw new Error("expected management auth state"); + const server = startServer(0, { managementAuthState: state }); + const origin = server.url.origin; + const token = "ocx_session_logout_test"; + state.sessions.set(token, { + serverOrigin: origin, + browserOrigin: origin, + csrfToken: "csrf-logout-test", + expiresAt: Date.now() + 60_000, + issuance: "loopback", + }); + const sessionHeaders = { + Origin: origin, + "x-opencodex-api-key": token, + "x-opencodex-gui-origin": origin, + "x-opencodex-csrf-token": "csrf-logout-test", + }; + try { + expect((await fetch(new URL("/api/session/logout", server.url), { method: "POST", headers: sessionHeaders })).status).toBe(200); + expect(state.sessions.has(token)).toBe(false); + expect((await fetch(new URL("/api/session/logout", server.url), { method: "POST", headers: sessionHeaders })).status).toBe(401); + expect((await fetch(new URL("/api/session/logout", server.url), { + method: "POST", + headers: { Origin: origin, "x-opencodex-api-key": "admin-secret" }, + })).status).toBe(403); + } finally { + await server.stop(true); + } + }); + test("the management ingress preserves the one-use pairing exchange contract", async () => { const managementPort = await findAvailablePort(0, "127.0.0.1"); const publicPort = await findAvailablePort(0, "127.0.0.1", { reservedPort: managementPort }); diff --git a/tests/service-secrets.test.ts b/tests/service-secrets.test.ts index 7ee274ab64..9c02a46d15 100644 --- a/tests/service-secrets.test.ts +++ b/tests/service-secrets.test.ts @@ -12,10 +12,16 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { readServiceApiTokenState, + readTokenBackupState, + removeOrphanTokenBackup, + replaceServiceApiTokenFile, + restoreTokenBackup, + serviceApiTokenBackupPath, removeServiceApiTokenFileIfOwned, serviceApiTokenFilePath, serviceApiTokenFingerprint, writeServiceApiTokenFile, + writeTokenBackup, } from "../src/lib/service-secrets"; let home = ""; @@ -83,4 +89,49 @@ describe("service API token ownership", () => { expect(existsSync(first.path)).toBe(false); expect(removeServiceApiTokenFileIfOwned(replacementFingerprint)).toBe("absent"); }); + + test("writes, restores, and removes the exact owner-only .prev backup", () => { + const original = writeServiceApiTokenFile("ocx_data_original"); + const backup = writeTokenBackup(original.fingerprint); + expect(backup.path).toBe(serviceApiTokenBackupPath()); + expect(readTokenBackupState()).toMatchObject({ kind: "present", token: "ocx_data_original" }); + if (process.platform !== "win32") expect(lstatSync(backup.path).mode & 0o777).toBe(0o600); + + replaceServiceApiTokenFile("ocx_data_replacement"); + expect(readServiceApiTokenState()).toMatchObject({ kind: "present", token: "ocx_data_replacement" }); + const restored = restoreTokenBackup(backup.path); + expect(restored.fingerprint).toBe(original.fingerprint); + expect(readServiceApiTokenState()).toMatchObject({ kind: "present", token: "ocx_data_original" }); + expect(removeOrphanTokenBackup()).toBe("removed"); + expect(readTokenBackupState()).toEqual({ kind: "absent" }); + }); + + test("crash before marker persistence removes an orphan but unsafe .prev is preserved", () => { + const original = writeServiceApiTokenFile("ocx_data_original"); + writeTokenBackup(original.fingerprint); + expect(removeOrphanTokenBackup()).toBe("removed"); + expect(readServiceApiTokenState()).toMatchObject({ kind: "present", token: "ocx_data_original" }); + + const target = join(home, "foreign-backup"); + writeFileSync(target, "ocx_data_foreign\n", { mode: 0o600 }); + let symlinkAvailable = true; + try { symlinkSync(target, serviceApiTokenBackupPath()); } + catch (error) { + if ((error as NodeJS.ErrnoException).code === "EPERM") symlinkAvailable = false; + else throw error; + } + if (symlinkAvailable) { + expect(readTokenBackupState()).toMatchObject({ kind: "unsafe" }); + expect(() => removeOrphanTokenBackup()).toThrow("owner-only bounded regular file"); + expect(existsSync(serviceApiTokenBackupPath())).toBe(true); + } + }); + + test("refuses a mismatched backup path without exposing either candidate", () => { + const original = writeServiceApiTokenFile("ocx_data_original"); + writeTokenBackup(original.fingerprint); + expect(() => restoreTokenBackup(join(home, "not-the-backup"))).toThrow("path mismatch"); + expect(readServiceApiTokenState()).toMatchObject({ kind: "present", token: "ocx_data_original" }); + expect(readTokenBackupState()).toMatchObject({ kind: "present", token: "ocx_data_original" }); + }); }); From 913d2a5e54b1cd9c566b7a75c43fce3fef594c88 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 05:04:24 +0900 Subject: [PATCH 080/172] docs(i18n): synchronize remote hub hardening across locales --- docs-site/astro.config.mjs | 2 +- .../src/content/docs/fr/guides/remote-hub.md | 72 ++++++++++++ .../content/docs/fr/guides/web-dashboard.md | 4 + .../docs/fr/reference/cli/lifecycle.md | 4 + .../docs/fr/reference/configuration/server.md | 4 + .../docs/fr/reference/management-api.md | 4 + .../src/content/docs/guides/remote-hub.md | 61 ++++++++++ .../src/content/docs/guides/web-dashboard.md | 4 + .../src/content/docs/ja/guides/remote-hub.md | 72 ++++++++++++ .../content/docs/ja/guides/web-dashboard.md | 4 + .../docs/ja/reference/cli/lifecycle.md | 4 + .../docs/ja/reference/configuration/server.md | 4 + .../docs/ja/reference/management-api.md | 4 + .../src/content/docs/ko/guides/remote-hub.md | 104 ++++++++++++++++++ .../content/docs/ko/guides/web-dashboard.md | 4 + .../docs/ko/reference/cli/lifecycle.md | 4 + .../docs/ko/reference/configuration/server.md | 4 + .../docs/ko/reference/management-api.md | 4 + .../content/docs/reference/cli/lifecycle.md | 4 + .../docs/reference/configuration/server.md | 4 + .../content/docs/reference/management-api.md | 4 + .../src/content/docs/ru/guides/remote-hub.md | 72 ++++++++++++ .../content/docs/ru/guides/web-dashboard.md | 4 + .../docs/ru/reference/cli/lifecycle.md | 4 + .../docs/ru/reference/configuration/server.md | 4 + .../docs/ru/reference/management-api.md | 4 + .../src/content/docs/tr/guides/remote-hub.md | 72 ++++++++++++ .../content/docs/tr/guides/web-dashboard.md | 5 +- .../docs/tr/reference/cli/lifecycle.md | 4 + .../docs/tr/reference/configuration/server.md | 4 + .../docs/tr/reference/management-api.md | 4 + .../content/docs/zh-cn/guides/remote-hub.md | 72 ++++++++++++ .../docs/zh-cn/guides/web-dashboard.md | 4 + .../docs/zh-cn/reference/cli/lifecycle.md | 4 + .../zh-cn/reference/configuration/server.md | 4 + .../docs/zh-cn/reference/management-api.md | 4 + .../content/docs/zh-tw/guides/remote-hub.md | 72 ++++++++++++ .../docs/zh-tw/guides/web-dashboard.md | 4 + .../docs/zh-tw/reference/cli/lifecycle.md | 4 + .../zh-tw/reference/configuration/server.md | 4 + .../docs/zh-tw/reference/management-api.md | 4 + structure/01_runtime.md | 4 + structure/02_config-and-codex-home.md | 4 + structure/05_gui-and-management-api.md | 4 + structure/06_docs-and-release.md | 4 + structure/09_client-integrations.md | 4 + 46 files changed, 746 insertions(+), 2 deletions(-) create mode 100644 docs-site/src/content/docs/fr/guides/remote-hub.md create mode 100644 docs-site/src/content/docs/ja/guides/remote-hub.md create mode 100644 docs-site/src/content/docs/ko/guides/remote-hub.md create mode 100644 docs-site/src/content/docs/ru/guides/remote-hub.md create mode 100644 docs-site/src/content/docs/tr/guides/remote-hub.md create mode 100644 docs-site/src/content/docs/zh-cn/guides/remote-hub.md create mode 100644 docs-site/src/content/docs/zh-tw/guides/remote-hub.md diff --git a/docs-site/astro.config.mjs b/docs-site/astro.config.mjs index d1ff26edcc..a9537e2eea 100644 --- a/docs-site/astro.config.mjs +++ b/docs-site/astro.config.mjs @@ -85,7 +85,7 @@ export default defineConfig({ label: "Guides", translations: { fr: "Guides", ko: "가이드", "zh-CN": "指南", "zh-TW": "指南", ru: "Руководства", ja: "ガイド", tr: "Kılavuzlar" }, items: [ - { label: "Remote Hub Deployment", slug: "guides/remote-hub" }, + { label: "Remote Hub Deployment", translations: { fr: "Déploiement Remote Hub", ko: "Remote Hub 배포", "zh-CN": "Remote Hub 部署", "zh-TW": "Remote Hub 部署", ru: "Развёртывание Remote Hub", ja: "Remote Hub のデプロイ", tr: "Remote Hub Dağıtımı" }, slug: "guides/remote-hub" }, { label: "Providers", translations: { fr: "Fournisseurs", ko: "프로바이더", "zh-CN": "提供商", "zh-TW": "供應商", ru: "Провайдеры", ja: "プロバイダー", tr: "Sağlayıcılar" }, slug: "guides/providers" }, { label: "Factory Droid Bridge", translations: { fr: "Pont Factory Droid", ko: "Factory Droid 브리지" }, slug: "guides/factory-droid" }, { label: "Model Routing", translations: { fr: "Routage des modèles", ko: "모델 라우팅", "zh-CN": "模型路由", "zh-TW": "模型路由", ru: "Маршрутизация моделей", ja: "モデルルーティング", tr: "Model Yönlendirme" }, slug: "guides/model-routing" }, diff --git a/docs-site/src/content/docs/fr/guides/remote-hub.md b/docs-site/src/content/docs/fr/guides/remote-hub.md new file mode 100644 index 0000000000..71b52ad422 --- /dev/null +++ b/docs-site/src/content/docs/fr/guides/remote-hub.md @@ -0,0 +1,72 @@ +--- +title: Déploiement Remote Hub +description: Déployer un hub opencodex avec une gestion locale, Tailscale Serve et OAuth sans interface locale. +--- + +Un hub conserve les identifiants fournisseur, le catalogue et l’usage sur un hôte. Les clients authentifiés appellent directement son plan de données. Le plan de gestion est distinct : son écoute facultative reste sur `127.0.0.1` et ne sert que le tableau de bord et `/api/*`. Elle ne sert jamais `/v1/*`, `/healthz`, `/readyz` ni WebSocket. Ne publiez pas le port `10101` et n’utilisez pas Tailscale Funnel. + +## Rôles, connexion et sécurité + +`standalone` réunit données et gestion. `hub` possède les secrets fournisseur et l’usage. `client` ne conserve que l’état de connexion et une clé de données dédiée. + +```bash +ocx connect https://hub-name.tailnet-name.ts.net --pairing-code-stdin +ocx connect status +ocx sync +``` + +La clé client est écrite dans le fichier privé `service-api-token`, jamais dans `config.json`. En mode connecté, l’usage provient du hub et est filtré par `apiKeyId`; après déconnexion, il provient du stockage local. Il n’existe aucune réplication entre les deux. + +Le jeton admin permet la gestion ordinaire mais ne peut jamais créer une session de consentement. Les actions de consentement exigent une `gui-session`, une Origin correspondante et un jeton CSRF. `Tailscale-User-Login` n’est fiable que sur l’entrée de gestion dédiée; renseignez les identités exactes dans `remoteGui.allowedTailscaleUsers`. + +## Service et Tailscale Serve + +```bash +ocx config set runtimeRole hub +ocx config set hostname 100.64.0.10 +ocx config set hub.managementPublicOrigin '"https://hub-name.tailnet-name.ts.net"' +ocx config set corsAllowOrigins '["http://localhost:10100"]' +ocx config set hub.managementIngress '{"enabled":true,"port":10101}' +ocx config set remoteGui.allowedTailscaleUsers '["operator@example.com"]' +export OPENCODEX_API_AUTH_TOKEN="$(openssl rand -hex 32)" +ocx service install +``` + +Le service lit le secret depuis `service-api-token`; le plist ou l’unité systemd ne contient pas sa valeur. + +```bash +curl --fail --silent http://100.64.0.10:10100/healthz +curl --fail --silent http://100.64.0.10:10100/readyz +tailscale serve --bg --https=443 http://127.0.0.1:10101 +tailscale serve status +``` + +`/healthz` ne prouve que la vie du processus. Validez aussi `/readyz`, `GET /v1/catalog` authentifié et une vraie réponse routée. Le port de gestion doit écouter uniquement sur `127.0.0.1`. Pour un proxy TLS privé, utilisez `tailscale cert hub-name.tailnet-name.ts.net` et ne fabriquez jamais d’en-têtes `Tailscale-User-*`; utilisez l’association à usage unique. + +## OAuth, rotation et déconnexion + +```bash +ocx config set oauthOpenBrowser false +ocx connect rotate --pairing-code-stdin +# uniquement en HTTPS : +ocx connect rotate --admin-token-stdin +``` + +Démarrez OAuth avec `POST /api/oauth/login`; si le rappel ne rejoint pas le hub, envoyez l’URL finale ou le code à `POST /api/oauth/login/code` sous `{provider,input}`. Ne placez jamais le code OAuth dans argv ou les journaux. + +La rotation garde les deux clés valides sous le même `apiKeyId` pendant dix minutes au plus. L’ancienne clé est sauvegardée dans `service-api-token.prev`, la nouvelle est installée atomiquement et vérifiée avec `/v1/catalog`, puis validée. Si le résultat est incertain, relancez `ocx connect rotate` avec une autorité transitoire; ne supprimez aucun candidat. + +`ocx disconnect` restaure l’état local même hors ligne et ne révoque pas la clé du hub. Après déconnexion, la seule voie de révocation est **Integrations → API Keys** sur le hub. `ocx connect revoke --admin-token-stdin` fonctionne uniquement tant que le client est connecté. + +## Docker, retour arrière et dépannage + +Il n’existe pas d’image Docker officielle. Épinglez l’image Bun par digest, conservez `/home/bun/.opencodex` dans un volume et montez le secret sur `/run/secrets/ocx_api_token`. Publiez seulement `10100`, jamais `10101`. Ne placez aucun secret dans `ARG`, `ENV`, `COPY`, Compose, l’historique d’image ou argv. Après le healthcheck, vérifiez séparément `/readyz`, le catalogue authentifié et une réponse réelle. + +- Hub indisponible : `ocx disconnect` restaure localement, mais la révocation reste à faire. +- Catalogue périmé : seul un dernier catalogue validé est conservé après une panne transitoire; aucune substitution locale après erreur d’authentification, schéma, taille ou protocole. +- Récupération `.prev` : conservez les deux fichiers et relancez la rotation avec une autorité transitoire. +- `hub-too-new`/`hub-too-old` : mettez à niveau le côté indiqué avant toute écriture locale. +- Code d’association perdu ou épuisé : créez-en un nouveau; les essais sont limités avec 429. +- HTTP non local exige `--allow-insecure-http`; un jeton admin n’est jamais envoyé en HTTP. +- Déconnexion/expiration de session navigateur n’affecte pas la clé de données. +- Avant `tailscale serve reset`, inspectez `tailscale serve status`, car reset supprime tous les mappages. diff --git a/docs-site/src/content/docs/fr/guides/web-dashboard.md b/docs-site/src/content/docs/fr/guides/web-dashboard.md index dc000a5a27..9970b26bda 100644 --- a/docs-site/src/content/docs/fr/guides/web-dashboard.md +++ b/docs-site/src/content/docs/fr/guides/web-dashboard.md @@ -98,6 +98,10 @@ OpenCodex demandent à Codex de transmettre les remplacements à `spawn_agent` ; [Surface des sous-agents](/fr/guides/sub-agent-surface/) pour le comportement canonique v1/base/v2. ::: +## Sessions, clés et usage Remote Hub + +Le plan de gestion du tableau de bord est séparé du trafic modèle direct client→hub. **Integrations → API Keys** affiche les rotations en attente, montre le secret de remplacement une seule fois et exige une validation ou une annulation explicite. La déconnexion du navigateur n'invalide que la session courante. L'usage connecté vient du hub filtré par `apiKeyId`; l'usage déconnecté est local, sans réplication. + La garantie de remplacement lors d'une création de sous-agent s'applique au texte de consignes v2 **intégré**. Un `injectionPrompt` personnalisé remplace entièrement ce texte et doit contenir les espaces réservés `{{model}}` et `{{effort}}` — et facultativement `{{roster}}` — sans quoi ces valeurs n'apparaîtront pas dans diff --git a/docs-site/src/content/docs/fr/reference/cli/lifecycle.md b/docs-site/src/content/docs/fr/reference/cli/lifecycle.md index 8f6362e1e4..95c3fb5fbe 100644 --- a/docs-site/src/content/docs/fr/reference/cli/lifecycle.md +++ b/docs-site/src/content/docs/fr/reference/cli/lifecycle.md @@ -278,3 +278,7 @@ ocx update --tag preview ``` Les nouvelles versions deviennent disponibles lorsque le [workflow de publication](https://github.com/lidge-jun/opencodex/actions/workflows/release.yml) les publie sur npm. + +## Cycle de vie du client Remote Hub + +Utilisez `ocx connect --pairing-code-stdin`, `ocx connect status`, `ocx sync` et `ocx connect rotate --pairing-code-stdin`. `ocx disconnect` restaure l'état local hors ligne sans révoquer la clé du hub. Tant que le client est connecté, `ocx connect revoke --admin-token-stdin` révoque l'`apiKeyId` enregistré; après déconnexion, utilisez **Integrations → API Keys** sur le hub. Les secrets passent uniquement par stdin, jamais par argv. diff --git a/docs-site/src/content/docs/fr/reference/configuration/server.md b/docs-site/src/content/docs/fr/reference/configuration/server.md index b54585a5ef..72da38b139 100644 --- a/docs-site/src/content/docs/fr/reference/configuration/server.md +++ b/docs-site/src/content/docs/fr/reference/configuration/server.md @@ -251,3 +251,7 @@ Les images `https:` distantes et les descriptions échouées ou vides ne sont pa Les services auxiliaires Anthropic OAuth réutilisent l'empreinte OAuth Claude Code existante d'opencodex. Effectuez un test d'endurance avec le compte et la charge de travail prévus. + +## Clés Remote Hub et valeurs par défaut + +`runtimeRole` vaut `standalone` par défaut. Un hub utilise `hub.managementPublicOrigin`, `hub.managementIngress` limité au loopback (`enabled:false` si absent) et les identités exactes de `remoteGui.allowedTailscaleUsers` (liste vide si absente). La clé client reste dans `service-api-token`, jamais dans `config.json`; `service-api-token.prev` peut exister pendant une rotation. Les usages ne sont pas répliqués. diff --git a/docs-site/src/content/docs/fr/reference/management-api.md b/docs-site/src/content/docs/fr/reference/management-api.md index b5c730b837..42a9b3f78c 100644 --- a/docs-site/src/content/docs/fr/reference/management-api.md +++ b/docs-site/src/content/docs/fr/reference/management-api.md @@ -286,3 +286,7 @@ Pour l'administration courante, le [tableau de bord web](/fr/guides/web-dashboar Pour les hôtes sans interface graphique et l'automatisation, utilisez les commandes `ocx` correspondantes : elles appellent cette même API active et renvoient un code différent de zéro lorsque le proxy est inaccessible ou que l'opération échoue. L'accès HTTP direct est surtout utile aux intégrations qui exigent les contrats exacts des points de terminaison ci-dessus. + +## Sessions distantes et rotation des clés de données + +`POST /api/keys/rotate {id}` démarre un chevauchement de dix minutes et renvoie le nouveau secret une seule fois. `POST /api/keys/rotate/commit {id,rotationId}` valide; `DELETE /api/keys/rotate {id,rotationId}` annule. L'authentification de gestion est obligatoire et une clé de données ne suffit pas. `POST /api/session/logout` exige la `gui-session` courante, l'Origin correspondante et CSRF. Un jeton admin reçoit 403 et ne peut jamais créer une session de consentement. diff --git a/docs-site/src/content/docs/guides/remote-hub.md b/docs-site/src/content/docs/guides/remote-hub.md index b1d4a08a76..16ef574ab1 100644 --- a/docs-site/src/content/docs/guides/remote-hub.md +++ b/docs-site/src/content/docs/guides/remote-hub.md @@ -25,6 +25,47 @@ public-internet surface and is outside this deployment model. on the public listener is ignored. `remoteGui.allowedTailscaleUsers` controls session issuance; it does not create a new general-purpose principal. +## Roles and direct data flow + +`standalone` keeps data and management on one machine. A `hub` owns provider credentials, the +catalog, and usage records. A `client` stores only its connection metadata and one per-client data +key. Codex and Claude traffic goes directly from the client to the hub data listener; it is not +tunneled through the dashboard or the loopback management relay. + +Connect with exactly one transient authority source. The authority is read from stdin and is never +written to config or the token file: + +```bash +ocx connect https://hub-name.tailnet-name.ts.net --pairing-code-stdin +ocx connect status +ocx sync +``` + +The hub automatically issues a per-client key. The client writes it to the existing owner-only +`service-api-token` file, never `config.json`. While connected, usage comes from the hub usage store +filtered to that client's stable `apiKeyId`. After disconnect, usage comes from the local store. +OpenCodex does not mirror usage between the two stores. + +Rotate a connected client with a fresh transient authority: + +```bash +ocx connect rotate --pairing-code-stdin +# or, only over HTTPS: +ocx connect rotate --admin-token-stdin +``` + +Rotation keeps the old and new data keys valid for at most ten minutes under the same `apiKeyId`. +The client backs up the old token as `service-api-token.prev`, atomically installs and probes the new +key, then commits. If a commit response is uncertain, rerun the rotate command with transient +authority; recovery probes both files before committing or restoring. Never delete either file when +recovery reports that both candidates were rejected. + +`ocx disconnect` is local and works while the hub is offline. It restores local client state and +does not revoke the hub key. After disconnect, revoke that key from **Integrations → API Keys** on +the hub. `ocx connect revoke --admin-token-stdin` is available only while still connected and uses +the persisted `apiKeyId`; it accepts no id override. Browser session logout/expiry is separate from +data-key rotation, revocation, and disconnect. + ## Linux systemd or macOS launchd Choose the hub's Tailscale address for the data listener and the exact browser-visible HTTPS origin @@ -227,3 +268,23 @@ ocx service repair For a container rollback, remove or replace the container while retaining the named state volume. For a service rollback, stop the branch service and repair the prior release against the same `OPENCODEX_HOME`. Disabling management ingress or Serve does not require changing the data listener. + +## Troubleshooting + +- **Hub down:** `ocx connect status` still shows the saved connection. `ocx disconnect` can restore + local state offline; it cannot revoke the remote key. +- **Stale catalog:** `ocx sync` keeps a validated last-known-good catalog only for transient hub + failures. Authentication, schema, size, and protocol failures are hard errors and never fall back + to local providers. +- **Rotated token or `.prev` recovery:** rerun `ocx connect rotate` with a pairing code or admin token. + Do not edit or remove either token candidate before the recovery probe finishes. +- **Protocol mismatch:** upgrade the older side named by the `hub-too-new` or `hub-too-old` message. + Negotiation fails before token, catalog, journal, or client-state writes. +- **Lost or burned pairing code:** create a new short-lived code. Grants are one-use and repeated + failures are rate-limited without revealing whether a code exists. +- **Plain HTTP warning:** pairing over non-loopback HTTP requires the explicit + `--allow-insecure-http` opt-in. Admin tokens are never sent over HTTP. +- **Remote session ended:** sign in or pair again. Logout and expiry invalidate only the browser + session, not a client data key. +- **Outstanding revocation after disconnect:** use the hub dashboard's **Integrations → API Keys** + page. It is the sole post-disconnect revocation path. diff --git a/docs-site/src/content/docs/guides/web-dashboard.md b/docs-site/src/content/docs/guides/web-dashboard.md index 8c9b589fc2..a8cc50225e 100644 --- a/docs-site/src/content/docs/guides/web-dashboard.md +++ b/docs-site/src/content/docs/guides/web-dashboard.md @@ -92,6 +92,10 @@ they have been synchronized. See [Sub-agent Surface](/guides/sub-agent-surface/) for the canonical v1/base/v2 behavior. ::: +## Remote Hub sessions, keys, and usage + +The dashboard's management plane is separate from direct client→hub model traffic. **Integrations → API Keys** shows pending rotations, displays a replacement secret only once, and requires explicit commit or abort. Browser logout invalidates only the current remote session. Connected usage is the hub store filtered by the client's `apiKeyId`; disconnected usage is local, with no mirroring. + The spawn override guarantee applies to the **built-in** v2 guidance text. A custom `injectionPrompt` replaces that text entirely and must include `{{model}}` and `{{effort}}` placeholders (and optionally `{{roster}}`) or those values will not appear in the injected diff --git a/docs-site/src/content/docs/ja/guides/remote-hub.md b/docs-site/src/content/docs/ja/guides/remote-hub.md new file mode 100644 index 0000000000..cc441e1956 --- /dev/null +++ b/docs-site/src/content/docs/ja/guides/remote-hub.md @@ -0,0 +1,72 @@ +--- +title: Remote Hub のデプロイ +description: 管理ポートをループバックに限定し、Tailscale Serve とヘッドレス OAuth で運用します。 +--- + +Remote Hub はプロバイダー認証情報、カタログ、使用量を一台のホストに保持し、認証済みクライアントからデータプレーンへ直接接続します。管理プレーンは別系統で、任意の管理リスナーは `127.0.0.1` にのみバインドされ、ダッシュボードと `/api/*` だけを提供します。`/v1/*`、`/healthz`、`/readyz`、WebSocket は提供しません。`10101` を公開したり Tailscale Funnel を使ったりしないでください。 + +## 役割、接続、信頼境界 + +`standalone` は一台で完結し、`hub` はプロバイダー秘密情報と使用量を所有し、`client` は接続状態とクライアント専用データキーだけを保存します。 + +```bash +ocx connect https://hub-name.tailnet-name.ts.net --pairing-code-stdin +ocx connect status +ocx sync +``` + +発行されたキーは所有者だけが読める `service-api-token` に保存され、`config.json` には入りません。接続中の使用量は hub 側で同じ `apiKeyId` に絞り込まれ、切断後はローカル保存分を表示します。両者はミラーリングされません。 + +管理トークンは通常の管理だけに使え、同意セッションを作ることは永久にできません。同意操作にはサーバー発行の `gui-session`、一致する Origin、CSRF が必要です。`Tailscale-User-Login` は専用管理リスナーでのみ信頼し、許可する ID を `remoteGui.allowedTailscaleUsers` に正確に設定します。 + +## サービスと Tailscale Serve + +```bash +ocx config set runtimeRole hub +ocx config set hostname 100.64.0.10 +ocx config set hub.managementPublicOrigin '"https://hub-name.tailnet-name.ts.net"' +ocx config set corsAllowOrigins '["http://localhost:10100"]' +ocx config set hub.managementIngress '{"enabled":true,"port":10101}' +ocx config set remoteGui.allowedTailscaleUsers '["operator@example.com"]' +export OPENCODEX_API_AUTH_TOKEN="$(openssl rand -hex 32)" +ocx service install +``` + +launchd/systemd は保護された `service-api-token` を読み、設定ファイルへ秘密値を埋め込みません。 + +```bash +curl --fail --silent http://100.64.0.10:10100/healthz +curl --fail --silent http://100.64.0.10:10100/readyz +tailscale serve --bg --https=443 http://127.0.0.1:10101 +tailscale serve status +``` + +`/healthz` の `200` はプロセスの生存確認にすぎません。`/readyz`、認証済み `GET /v1/catalog`、実際のモデル応答も確認してください。独自 TLS プロキシでは `tailscale cert hub-name.tailnet-name.ts.net` を使い、`127.0.0.1:10101` のみに転送します。`Tailscale-User-*` を偽造せず、信頼できる ID がない場合は一度限りのペアリングを使います。 + +## OAuth、キー更新、切断 + +```bash +ocx config set oauthOpenBrowser false +ocx connect rotate --pairing-code-stdin +# HTTPS のみ: +ocx connect rotate --admin-token-stdin +``` + +OAuth は `POST /api/oauth/login` で開始し、コールバックできない場合は最終 URL またはコードを `{provider,input}` として `POST /api/oauth/login/code` へ渡します。コードを argv やログに残さないでください。 + +キー更新では最大10分間、旧キーと新キーが同じ `apiKeyId` で有効です。旧キーを `service-api-token.prev` に保存し、新キーを原子的に置換して `/v1/catalog` で確認後に確定します。結果が不明な場合は一時権限を使って同じコマンドを再実行し、両候補の判定が終わるまで削除しないでください。 + +`ocx disconnect` は hub が停止中でもローカル状態を復元しますが、hub のキーは失効させません。切断後は hub の **Integrations → API Keys** だけが失効経路です。`ocx connect revoke --admin-token-stdin` は接続中のみ利用できます。 + +## Docker とトラブルシューティング + +公式 Docker イメージはありません。運用者が Bun イメージを digest 固定し、`/home/bun/.opencodex` をボリューム、`/run/secrets/ocx_api_token` を secret としてマウントしてください。公開するのは `10100` だけで、`10101` は公開しません。秘密値を `ARG`、`ENV`、`COPY`、Compose、イメージ履歴、argv に入れないでください。healthcheck 後にも readiness、認証済みカタログ、実リクエストを別途確認します。 + +- hub 停止時はオフライン切断できますが、キー失効は未完了のままです。 +- 一時障害時だけ検証済み LKG を維持し、認証・スキーマ・サイズ・プロトコル障害でローカルへフォールバックしません。 +- `.prev` 復旧では二つのファイルを保持して一時権限付きで再実行します。 +- `hub-too-new`/`hub-too-old` が示す古い側を更新してください。書き込み前に拒否されます。 +- ペアリングコードは一度限りで、失敗は 429 制限されます。失った場合は再発行します。 +- 非ループバック HTTP は `--allow-insecure-http` が必要で、管理トークンは HTTP 送信されません。 +- ブラウザーのログアウト/期限切れはデータキーを失効させません。 +- `tailscale serve reset` の前に全マッピングを確認してください。 diff --git a/docs-site/src/content/docs/ja/guides/web-dashboard.md b/docs-site/src/content/docs/ja/guides/web-dashboard.md index 41a9b4b2d5..4b7cbd70ad 100644 --- a/docs-site/src/content/docs/ja/guides/web-dashboard.md +++ b/docs-site/src/content/docs/ja/guides/web-dashboard.md @@ -79,6 +79,10 @@ Codex タスクだけに適用され、このオプション自体が委任を [サブエージェントサーフェス](/ja/guides/sub-agent-surface/)を参照してください。 ::: +## Remote Hub のセッション、キー、使用量 + +ダッシュボードの管理プレーンと client→hub のモデル通信は別経路です。**Integrations → API Keys** は保留中の更新を表示し、新しい秘密値を一度だけ示し、明示的な確定または中止を要求します。ブラウザーのログアウトは現在のセッションだけを無効にします。接続中の使用量は hub で `apiKeyId` に絞り、切断後はローカル記録を使い、ミラーリングしません。 + セレクターには有効化されたネイティブおよびルーティングモデルと Codex グローバル推論段階が表示されます。API は 選んだ強度がグローバル段階にあるか検査し、Codex は再び対象カタログ項目がその強度をサポートするか 検査します。 diff --git a/docs-site/src/content/docs/ja/reference/cli/lifecycle.md b/docs-site/src/content/docs/ja/reference/cli/lifecycle.md index 6faca72d3a..97f3f3fbd0 100644 --- a/docs-site/src/content/docs/ja/reference/cli/lifecycle.md +++ b/docs-site/src/content/docs/ja/reference/cli/lifecycle.md @@ -234,3 +234,7 @@ ocx update --tag preview ``` 新しいバージョンは、[リリースワークフロー](https://github.com/lidge-jun/opencodex/actions/workflows/release.yml) が npm に公開すると利用可能になります。 + +## Remote Hub クライアントのライフサイクル + +`ocx connect --pairing-code-stdin`、`ocx connect status`、`ocx sync`、`ocx connect rotate --pairing-code-stdin` を使います。`ocx disconnect` はオフラインでローカル状態を復元しますが hub のキーは失効させません。接続中は `ocx connect revoke --admin-token-stdin` が保存済み `apiKeyId` を失効させ、切断後は hub の **Integrations → API Keys** を使います。秘密値は stdin だけで渡し、argv には入れません。 diff --git a/docs-site/src/content/docs/ja/reference/configuration/server.md b/docs-site/src/content/docs/ja/reference/configuration/server.md index 85e6063b8f..a5e7af3385 100644 --- a/docs-site/src/content/docs/ja/reference/configuration/server.md +++ b/docs-site/src/content/docs/ja/reference/configuration/server.md @@ -161,3 +161,7 @@ OpenAI バックエンドには、ChatGPT ログインと有効な ChatGPT `forw 対応するレベルは、上流プロバイダーの能力と選択したモデルが公表する推論ラダーによって制限されます。 Vision は、プロバイダーの `noVisionModels` のモデルに送信された画像に対してのみアクティブになります。 OpenAI には、検索と同じログイン/転送要件があります。明示的に選択された Anthropic は、使用可能な認証情報がないと失敗します。成功した `data:` 記述では、バックエンド、モデル、詳細、画像バイト、および正規化されたメッセージ コンテキストをキーとした境界付きキャッシュが使用されます。OpenAI のキーには推論負荷も含まれます(Anthropic のキーには含まれません)。ヒットと同じターンの重複は制限を消費しません。リモート `https:` イメージと失敗した説明、または空の説明はキャッシュされません。 Anthropic OAuth サイドカーは、opencodex の既存のクロード コード OAuth フィンガープリントを再利用します。対象のアカウントとワークロードをソークテストします。 + +## Remote Hub のキーと既定値 + +`runtimeRole` の既定値は `standalone` です。hub は `hub.managementPublicOrigin`、loopback 限定の `hub.managementIngress`(未設定時 `enabled:false`)、正確な `remoteGui.allowedTailscaleUsers`(未設定時は空)を使います。クライアントキーは `config.json` ではなく `service-api-token` に保存され、更新中だけ `service-api-token.prev` が存在する場合があります。使用量はミラーリングされません。 diff --git a/docs-site/src/content/docs/ja/reference/management-api.md b/docs-site/src/content/docs/ja/reference/management-api.md index 8d1f652392..2eb3bdd362 100644 --- a/docs-site/src/content/docs/ja/reference/management-api.md +++ b/docs-site/src/content/docs/ja/reference/management-api.md @@ -240,3 +240,7 @@ account の selector binding は残るため、欠落中の exact route は fail ## クライアントの選択 通常の管理では、[ウェブダッシュボード](/guides/web-dashboard/) が最も安全なガイド付きワークフローを提供します。ヘッドレス ホストとオートメーションの場合は、対応する `ocx` コマンドを使用します。これらのコマンドは、これと同じライブ API を呼び出し、プロキシに到達できない場合、または操作が失敗した場合にゼロ以外の結果を返します。ダイレクト HTTP は、上記の正確なエンドポイント コントラクトを必要とする統合に最も役立ちます。 + +## リモートセッションとデータキー更新 + +`POST /api/keys/rotate {id}` は10分間の移行を開始し、新しい秘密値を一度だけ返します。`POST /api/keys/rotate/commit {id,rotationId}` で確定し、`DELETE /api/keys/rotate {id,rotationId}` で中止します。管理認証が必須で、データキーからは呼べません。`POST /api/session/logout` には現在の `gui-session`、一致する Origin、CSRF が必要です。管理トークンは 403 となり、同意セッションを作成できません。 diff --git a/docs-site/src/content/docs/ko/guides/remote-hub.md b/docs-site/src/content/docs/ko/guides/remote-hub.md new file mode 100644 index 0000000000..970a52cb1f --- /dev/null +++ b/docs-site/src/content/docs/ko/guides/remote-hub.md @@ -0,0 +1,104 @@ +--- +title: Remote Hub 배포 +description: Linux, macOS, Docker에서 관리 포트는 로컬에만 열고 Tailscale Serve와 헤드리스 OAuth를 사용하는 방법입니다. +--- + +Remote Hub를 쓰면 프로바이더 인증 정보와 사용량 기록은 허브 한 곳에 두고, 인증된 클라이언트가 허브의 데이터 API를 직접 사용합니다. 브라우저용 관리 API는 별도입니다. 선택 사항인 관리 리스너는 `127.0.0.1`에만 열리며 대시보드와 `/api/*`만 제공합니다. + +관리 포트에서는 `/v1/*`, `/healthz`, `/readyz`, WebSocket을 제공하지 않습니다. 이 포트를 직접 공개하거나 방화벽에 열지 말고 Tailscale Funnel도 사용하지 마세요. + +## 역할과 데이터 흐름 + +- `standalone`: 데이터와 관리를 한 컴퓨터에서 처리합니다. +- `hub`: 프로바이더 키, 카탈로그, 사용량 기록을 보관합니다. +- `client`: 연결 정보와 클라이언트 전용 데이터 키 하나만 보관합니다. + +Codex와 Claude 요청은 클라이언트에서 허브의 데이터 리스너로 바로 갑니다. 대시보드나 로컬 관리 릴레이를 거치지 않습니다. + +```bash +ocx connect https://hub-name.tailnet-name.ts.net --pairing-code-stdin +ocx connect status +ocx sync +``` + +허브가 발급한 클라이언트별 키는 권한이 제한된 `service-api-token` 파일에 저장됩니다. `config.json`에는 저장되지 않습니다. 연결 중 사용량은 허브 기록에서 해당 `apiKeyId`만 조회하고, 연결을 끊은 뒤에는 로컬 기록을 봅니다. 두 기록은 서로 복제되지 않습니다. + +## 보안과 동의 경계 + +- 프로바이더/OAuth 인증 정보는 허브 밖으로 복사하지 마세요. +- 데이터 키는 `service-api-token` 또는 `OCX_API_TOKEN_FILE`로 전달하며 관리 권한이 없습니다. +- 관리자 토큰은 일반 관리 작업만 할 수 있습니다. 브라우저 동의 세션을 만들거나 저장소 Star 같은 동의 작업을 승인할 수는 없습니다. 그런 작업에는 서버가 발급한 `gui-session`, 일치하는 Origin, CSRF 토큰이 필요합니다. +- `Tailscale-User-Login`은 별도 관리 리스너에서만 신뢰합니다. 공개 리스너의 같은 헤더는 무시합니다. `remoteGui.allowedTailscaleUsers`에는 허용할 로그인 ID를 정확히 적으세요. + +## systemd 또는 launchd + +```bash +ocx config set runtimeRole hub +ocx config set hostname 100.64.0.10 +ocx config set hub.managementPublicOrigin '"https://hub-name.tailnet-name.ts.net"' +ocx config set corsAllowOrigins '["http://localhost:10100"]' +ocx config set hub.managementIngress '{"enabled":true,"port":10101}' +ocx config set remoteGui.allowedTailscaleUsers '["operator@example.com"]' +export OPENCODEX_API_AUTH_TOKEN="$(openssl rand -hex 32)" +ocx service install +ocx service status +``` + +`ocx service install`은 키를 기존 `service-api-token` 경로에 안전하게 저장합니다. plist나 systemd unit에는 실제 키가 들어가지 않습니다. + +```bash +curl --fail --silent http://100.64.0.10:10100/healthz +curl --fail --silent http://100.64.0.10:10100/readyz +``` + +`/healthz`의 `200`은 프로세스가 살아 있다는 뜻뿐입니다. 실제 배포 확인에는 `/readyz`, 인증된 `GET /v1/catalog`, 실제 모델 요청 1회가 모두 필요합니다. + +## Tailscale Serve + +```bash +ss -ltnp | grep 10101 +lsof -nP -iTCP:10101 -sTCP:LISTEN +tailscale serve --bg --https=443 http://127.0.0.1:10101 +tailscale serve status +``` + +관리 포트는 `127.0.0.1:10101`에서만 보여야 합니다. `hub.managementPublicOrigin`은 Serve가 표시한 정확한 HTTPS Origin으로 설정하세요. 직접 TLS 프록시를 운영한다면 `tailscale cert hub-name.tailnet-name.ts.net`으로 ts.net 전체 FQDN 인증서만 발급하고 `127.0.0.1:10101`로만 프록시하세요. 임의의 `Tailscale-User-*` 헤더를 만들지 말고, 신뢰할 수 있는 Tailscale 신원이 없으면 일회용 pairing을 사용하세요. + +## 헤드리스 OAuth + +```bash +ocx config set oauthOpenBrowser false +``` + +인증된 대시보드에서 `POST /api/oauth/login`을 시작하고, 운영자 컴퓨터에서 반환된 URL을 엽니다. 콜백이 허브에 닿지 않으면 최종 리디렉션 URL이나 코드를 `POST /api/oauth/login/code`의 `{provider,input}`으로 전달하세요. OAuth 코드를 argv, 로그, 이슈, 스크린샷에 남기지 마세요. + +## 키 교체와 연결 해제 + +```bash +ocx connect rotate --pairing-code-stdin +# HTTPS에서만: +ocx connect rotate --admin-token-stdin +``` + +기존 키와 새 키는 같은 `apiKeyId`로 최대 10분 동안 함께 유효합니다. 클라이언트는 기존 키를 `service-api-token.prev`에 백업하고, 새 키를 원자적으로 적용해 `/v1/catalog`로 확인한 다음 확정합니다. 결과가 불확실하면 임시 권한을 다시 넣어 같은 명령을 실행하세요. 현재 파일과 `.prev`를 모두 확인한 뒤 확정하거나 복원합니다. + +`ocx disconnect`는 허브가 꺼져 있어도 로컬 상태를 복원하며 허브 키를 삭제하지 않습니다. 연결을 끊은 뒤에는 허브 대시보드의 **Integrations → API Keys**에서 키를 삭제해야 합니다. `ocx connect revoke --admin-token-stdin`은 연결 중에만 사용할 수 있으며 저장된 `apiKeyId`만 사용합니다. + +## Docker + +opencodex는 공식 컨테이너 이미지를 배포하지 않습니다. 운영자가 직접 만든 이미지는 Bun 이미지를 digest로 고정하고, `/home/bun/.opencodex`를 영구 볼륨으로, `/run/secrets/ocx_api_token`을 Docker secret으로 마운트하세요. 공개 포트는 `10100`만 두고 컨테이너 안의 `127.0.0.1:10101`은 절대 publish하지 마세요. 토큰을 `ARG`, `ENV`, `COPY`, Compose YAML, 이미지 기록, 명령행에 넣지 마세요. Docker socket, 홈 디렉터리, SSH agent, 프로바이더 키도 마운트하지 마세요. + +컨테이너 healthcheck의 `/healthz`가 통과한 뒤 `/readyz`, 인증된 `/v1/catalog`, 실제 모델 응답을 별도로 확인하세요. + +## 롤백과 문제 해결 + +`tailscale serve reset`은 노드의 모든 매핑을 지우므로 먼저 `tailscale serve status`를 확인하세요. 서비스 롤백 때는 같은 `OPENCODEX_HOME`을 유지한 채 이전 릴리스를 `ocx service repair`로 복구합니다. + +- 허브가 꺼져 있으면 `ocx disconnect`로 오프라인 복원할 수 있지만 원격 키는 삭제되지 않습니다. +- 일시적 허브 오류에서는 검증된 마지막 카탈로그를 유지합니다. 인증·스키마·크기·프로토콜 오류는 로컬 프로바이더로 대체하지 않습니다. +- `.prev` 복구가 필요하면 두 파일을 지우지 말고 임시 권한과 함께 `ocx connect rotate`를 다시 실행하세요. +- `hub-too-new` 또는 `hub-too-old`가 나오면 메시지가 가리키는 오래된 쪽을 업그레이드하세요. 불일치는 로컬 파일을 쓰기 전에 차단됩니다. +- pairing 코드는 일회용이며 반복 실패는 429로 제한됩니다. 코드를 잃었거나 소진했다면 새로 만드세요. +- 루프백이 아닌 HTTP pairing은 `--allow-insecure-http`를 명시해야 합니다. 관리자 토큰은 HTTP로 보내지 않습니다. +- 브라우저 로그아웃/만료는 해당 원격 세션만 끊습니다. 데이터 키와는 별개입니다. +- 연결 해제 후 남은 키는 허브의 **Integrations → API Keys**에서만 폐기할 수 있습니다. diff --git a/docs-site/src/content/docs/ko/guides/web-dashboard.md b/docs-site/src/content/docs/ko/guides/web-dashboard.md index f831016288..62fb808853 100644 --- a/docs-site/src/content/docs/ko/guides/web-dashboard.md +++ b/docs-site/src/content/docs/ko/guides/web-dashboard.md @@ -79,6 +79,10 @@ Dashboard의 **Sub-agent delegation** 선택기는 `injectionModel`과 선택적 [서브에이전트 서피스](/ko/guides/sub-agent-surface/)를 참고하세요. ::: +## Remote Hub 세션, 키, 사용량 + +대시보드 관리 API와 클라이언트에서 허브로 가는 모델 요청은 서로 다른 경로입니다. **Integrations → API Keys**에서는 진행 중인 키 교체를 확인하고, 새 키를 한 번만 표시하며, 확정 또는 취소를 직접 눌러야 합니다. 브라우저 로그아웃은 현재 원격 세션만 끝냅니다. 연결 중 사용량은 허브에서 해당 `apiKeyId`만 보고, 연결 해제 후에는 로컬 기록을 보며 서로 복제하지 않습니다. + 선택기에는 활성화된 네이티브 및 라우팅 모델과 Codex 전역 reasoning 단계가 표시됩니다. API는 선택한 강도가 전역 단계에 있는지 검사하고, Codex는 다시 대상 카탈로그 항목이 그 강도를 지원하는지 검사합니다. diff --git a/docs-site/src/content/docs/ko/reference/cli/lifecycle.md b/docs-site/src/content/docs/ko/reference/cli/lifecycle.md index 0080f40942..271830ee1d 100644 --- a/docs-site/src/content/docs/ko/reference/cli/lifecycle.md +++ b/docs-site/src/content/docs/ko/reference/cli/lifecycle.md @@ -315,3 +315,7 @@ ocx update --tag preview 새 버전은 [Release workflow](https://github.com/lidge-jun/opencodex/actions/workflows/release.yml)가 npm에 게시하면 사용할 수 있게 됩니다. + +## Remote Hub 클라이언트 라이프사이클 + +`ocx connect --pairing-code-stdin`, `ocx connect status`, `ocx sync`, `ocx connect rotate --pairing-code-stdin`을 사용합니다. `ocx disconnect`는 오프라인에서도 로컬 상태를 복원하지만 허브 키는 폐기하지 않습니다. 연결 중에는 `ocx connect revoke --admin-token-stdin`으로 저장된 `apiKeyId`를 폐기할 수 있고, 연결을 끊은 뒤에는 허브의 **Integrations → API Keys**를 사용해야 합니다. 비밀값은 stdin으로만 전달하고 argv에 넣지 마세요. diff --git a/docs-site/src/content/docs/ko/reference/configuration/server.md b/docs-site/src/content/docs/ko/reference/configuration/server.md index 8e1fc14842..0e280e668d 100644 --- a/docs-site/src/content/docs/ko/reference/configuration/server.md +++ b/docs-site/src/content/docs/ko/reference/configuration/server.md @@ -161,3 +161,7 @@ OpenAI 백엔드는 ChatGPT 로그인과 활성화된 ChatGPT `forward` provider 지원되는 수준은 업스트림 제공자의 역량과 선택한 모델이 공개한 추론 사다리에 따라 제한됩니다. Vision은 provider의 `noVisionModels`에 속한 모델로 보낸 이미지에만 활성화됩니다. OpenAI는 검색과 같은 로그인/forward 요건을 갖고 있으며, 명시적으로 선택한 Anthropic은 사용할 수 있는 자격 증명이 없으면 닫힌 상태로 실패합니다. 성공한 `data:` 설명은 backend, model, detail, image bytes, 그리고 정규화된 메시지 컨텍스트를 키로 하는 bounded cache를 사용합니다. OpenAI 키에는 reasoning effort도 포함됩니다(Anthropic 키에는 없습니다). 히트와 같은 턴의 중복은 한도를 소모하지 않습니다. 원격 `https:` 이미지와 실패했거나 비어 있는 설명은 캐시하지 않습니다. Anthropic OAuth 사이드카는 opencodex의 기존 Claude Code OAuth fingerprint를 재사용합니다. 의도한 계정과 워크로드로 소크 테스트를 수행합니다. + +## Remote Hub 키와 기본값 + +`runtimeRole` 기본값은 `standalone`입니다. 허브는 `hub.managementPublicOrigin`, 로컬에만 열리는 `hub.managementIngress`(없으면 `enabled:false`), 정확한 `remoteGui.allowedTailscaleUsers`(없으면 빈 목록)를 사용합니다. 클라이언트 데이터 키는 `config.json`이 아니라 `service-api-token`에 저장되며 교체 중에는 `service-api-token.prev`가 잠시 생길 수 있습니다. 사용량 기록은 서로 복제하지 않습니다. diff --git a/docs-site/src/content/docs/ko/reference/management-api.md b/docs-site/src/content/docs/ko/reference/management-api.md index 10c8ff0694..20c5fc7379 100644 --- a/docs-site/src/content/docs/ko/reference/management-api.md +++ b/docs-site/src/content/docs/ko/reference/management-api.md @@ -243,3 +243,7 @@ account의 selector binding은 남아 있어 계정이 없을 때 exact route가 ## 클라이언트 선택 일반적인 관리 작업에는 [Web Dashboard](/guides/web-dashboard/)가 가장 안전한 안내형 워크플로를 제공합니다. 헤드리스 호스트와 자동화에는 대응하는 `ocx` 명령을 사용하십시오. 이 명령들은 동일한 실시간 API를 호출하며, 프록시에 접근할 수 없거나 작업이 실패하면 0이 아닌 결과를 반환합니다. 직접 HTTP는 위의 정확한 엔드포인트 계약이 필요한 통합에 가장 유용합니다. + +## 원격 세션과 데이터 키 교체 + +`POST /api/keys/rotate {id}`는 최대 10분의 전환을 시작하며 새 데이터 키를 한 번만 반환합니다. `POST /api/keys/rotate/commit {id,rotationId}`는 확정하고, `DELETE /api/keys/rotate {id,rotationId}`는 취소합니다. 모두 관리 인증이 필요하며 데이터 키로 호출할 수 없습니다. `POST /api/session/logout`은 현재 `gui-session`, 일치하는 Origin, CSRF가 필요합니다. 관리자 토큰은 403을 받고 동의 세션을 만들거나 교환할 수 없습니다. diff --git a/docs-site/src/content/docs/reference/cli/lifecycle.md b/docs-site/src/content/docs/reference/cli/lifecycle.md index cde46cf827..74793580d2 100644 --- a/docs-site/src/content/docs/reference/cli/lifecycle.md +++ b/docs-site/src/content/docs/reference/cli/lifecycle.md @@ -450,3 +450,7 @@ ocx update --tag preview New versions become available when the [Release workflow](https://github.com/lidge-jun/opencodex/actions/workflows/release.yml) publishes them to npm. + +## Remote Hub client lifecycle + +Use `ocx connect --pairing-code-stdin`, `ocx connect status`, `ocx sync`, and `ocx connect rotate --pairing-code-stdin`. `ocx disconnect` restores local state offline and does not revoke the hub key. While connected only, `ocx connect revoke --admin-token-stdin` revokes the persisted `apiKeyId`; after disconnect use the hub's **Integrations → API Keys** page. Secrets are stdin-only and never belong in argv. diff --git a/docs-site/src/content/docs/reference/configuration/server.md b/docs-site/src/content/docs/reference/configuration/server.md index 939ed83789..c7d9226a1c 100644 --- a/docs-site/src/content/docs/reference/configuration/server.md +++ b/docs-site/src/content/docs/reference/configuration/server.md @@ -262,3 +262,7 @@ Remote `https:` images and failed or empty descriptions are not cached. Anthropic OAuth sidecars reuse opencodex's existing Claude Code OAuth fingerprint. Soak-test the intended account and workload. + +## Remote Hub keys and defaults + +`runtimeRole` defaults to `standalone`. A hub uses `hub.managementPublicOrigin`, loopback-only `hub.managementIngress` (`enabled:false` when absent), and exact `remoteGui.allowedTailscaleUsers` (empty when absent). A client data key lives in `service-api-token`, never `config.json`; rotation may temporarily create `service-api-token.prev`. Usage stores are not mirrored. diff --git a/docs-site/src/content/docs/reference/management-api.md b/docs-site/src/content/docs/reference/management-api.md index 2484e7e675..ffc26dbc90 100644 --- a/docs-site/src/content/docs/reference/management-api.md +++ b/docs-site/src/content/docs/reference/management-api.md @@ -286,3 +286,7 @@ For ordinary administration, the [Web Dashboard](/guides/web-dashboard/) gives t workflow. For headless hosts and automation, use the corresponding `ocx` commands: they call this same live API and return a nonzero result when the proxy is unreachable or the operation fails. Direct HTTP is most useful for integrations that need the exact endpoint contracts above. + +## Remote sessions and data-key rotation + +`POST /api/keys/rotate {id}` starts a ten-minute overlap and returns the new data secret once. `POST /api/keys/rotate/commit {id,rotationId}` commits it; `DELETE /api/keys/rotate {id,rotationId}` aborts it. All require management authentication; data keys cannot call them. `POST /api/session/logout` requires the current `gui-session`, matching Origin, and CSRF. An admin token receives 403 and can never mint or exchange into a consent session. diff --git a/docs-site/src/content/docs/ru/guides/remote-hub.md b/docs-site/src/content/docs/ru/guides/remote-hub.md new file mode 100644 index 0000000000..e225d2564e --- /dev/null +++ b/docs-site/src/content/docs/ru/guides/remote-hub.md @@ -0,0 +1,72 @@ +--- +title: Развёртывание Remote Hub +description: Hub с локальным контуром управления, Tailscale Serve и OAuth без локального браузера. +--- + +Remote Hub хранит учётные данные провайдеров, каталог и статистику на одном хосте. Авторизованные клиенты обращаются непосредственно к его плоскости данных. Контур управления отделён: необязательный listener привязан только к `127.0.0.1` и обслуживает панель и `/api/*`, но не `/v1/*`, `/healthz`, `/readyz` или WebSocket. Не публикуйте `10101` и не используйте Tailscale Funnel. + +## Роли и границы доверия + +`standalone` объединяет всё на одной машине; `hub` владеет секретами и статистикой; `client` хранит только состояние подключения и отдельный ключ данных. + +```bash +ocx connect https://hub-name.tailnet-name.ts.net --pairing-code-stdin +ocx connect status +ocx sync +``` + +Ключ клиента записывается в защищённый `service-api-token`, а не в `config.json`. При подключении статистика читается с hub и фильтруется по `apiKeyId`; после отключения используется локальное хранилище. Зеркалирования нет. + +Admin token разрешает обычное управление, но никогда не создаёт consent session. Для действий с согласием нужны `gui-session`, совпадающий Origin и CSRF. Заголовок `Tailscale-User-Login` доверен только отдельному management ingress; точные логины задаются в `remoteGui.allowedTailscaleUsers`. + +## Сервис и Tailscale Serve + +```bash +ocx config set runtimeRole hub +ocx config set hostname 100.64.0.10 +ocx config set hub.managementPublicOrigin '"https://hub-name.tailnet-name.ts.net"' +ocx config set corsAllowOrigins '["http://localhost:10100"]' +ocx config set hub.managementIngress '{"enabled":true,"port":10101}' +ocx config set remoteGui.allowedTailscaleUsers '["operator@example.com"]' +export OPENCODEX_API_AUTH_TOKEN="$(openssl rand -hex 32)" +ocx service install +``` + +systemd/launchd читает секрет из `service-api-token`; plist и unit не содержат его значения. + +```bash +curl --fail --silent http://100.64.0.10:10100/healthz +curl --fail --silent http://100.64.0.10:10100/readyz +tailscale serve --bg --https=443 http://127.0.0.1:10101 +tailscale serve status +``` + +`/healthz` подтверждает только работу процесса. Проверьте также `/readyz`, авторизованный `GET /v1/catalog` и реальный ответ модели. Собственный TLS-прокси должен использовать `tailscale cert hub-name.tailnet-name.ts.net` и проксировать только на `127.0.0.1:10101`. Не подделывайте `Tailscale-User-*`; без доверенной идентификации используйте одноразовое pairing. + +## OAuth, ротация и отключение + +```bash +ocx config set oauthOpenBrowser false +ocx connect rotate --pairing-code-stdin +# только HTTPS: +ocx connect rotate --admin-token-stdin +``` + +OAuth запускается через `POST /api/oauth/login`. Если callback недоступен, передайте итоговый URL или код как `{provider,input}` в `POST /api/oauth/login/code`. Не помещайте код в argv или логи. + +При ротации старый и новый ключи действуют под одним `apiKeyId` не более десяти минут. Старый ключ сохраняется в `service-api-token.prev`, новый устанавливается атомарно и проверяется через `/v1/catalog`, затем подтверждается. При неопределённом результате повторите команду с временными полномочиями и не удаляйте кандидаты до проверки. + +`ocx disconnect` восстанавливает локальное состояние даже без hub, но не отзывает удалённый ключ. После отключения отзыв возможен только на странице hub **Integrations → API Keys**. `ocx connect revoke --admin-token-stdin` доступен только пока клиент подключён. + +## Docker и устранение неполадок + +Официального Docker-образа нет. Закрепите Bun-образ по digest, используйте volume для `/home/bun/.opencodex` и secret `/run/secrets/ocx_api_token`. Публикуйте только `10100`, не `10101`. Не помещайте секреты в `ARG`, `ENV`, `COPY`, Compose, историю образа или argv. После healthcheck отдельно проверьте readiness, каталог и реальный запрос. + +- При недоступном hub можно отключиться офлайн, но отзыв ключа останется незавершённым. +- LKG сохраняется только при временном сбое; при ошибке auth, схемы, размера или протокола локального fallback нет. +- Для `.prev` сохраните оба файла и повторите ротацию с временными полномочиями. +- `hub-too-new`/`hub-too-old` указывает, какую сторону обновить; локальные записи ещё не сделаны. +- Pairing одноразовый, попытки ограничены 429; потерянный код создайте заново. +- Для не-loopback HTTP нужен `--allow-insecure-http`; admin token по HTTP не отправляется. +- Logout/expiry браузерной сессии не отзывает ключ данных. +- Перед `tailscale serve reset` просмотрите все mappings через `tailscale serve status`. diff --git a/docs-site/src/content/docs/ru/guides/web-dashboard.md b/docs-site/src/content/docs/ru/guides/web-dashboard.md index 2779b167d7..b92ce3d450 100644 --- a/docs-site/src/content/docs/ru/guides/web-dashboard.md +++ b/docs-site/src/content/docs/ru/guides/web-dashboard.md @@ -82,6 +82,10 @@ bun run dev:gui [Поверхность подагентов](/ru/guides/sub-agent-surface/). ::: +## Сессии, ключи и статистика Remote Hub + +Контур управления панели отделён от прямого трафика client→hub. **Integrations → API Keys** показывает ожидающую ротацию, отображает новый секрет один раз и требует явного подтверждения или отмены. Logout браузера отзывает только текущую сессию. При подключении статистика hub фильтруется по `apiKeyId`; после отключения используется локальная, без зеркалирования. + Селектор предлагает включённые нативные и маршрутизируемые модели, а также глобальную шкалу уровней рассуждений Codex. API валидирует выбранный уровень глобально; Codex дополнительно валидирует уровень порождения по целевой записи каталога. diff --git a/docs-site/src/content/docs/ru/reference/cli/lifecycle.md b/docs-site/src/content/docs/ru/reference/cli/lifecycle.md index 763fbf9afc..df777b802b 100644 --- a/docs-site/src/content/docs/ru/reference/cli/lifecycle.md +++ b/docs-site/src/content/docs/ru/reference/cli/lifecycle.md @@ -339,3 +339,7 @@ ocx update --tag preview Новые версии становятся доступны, когда [Release workflow](https://github.com/lidge-jun/opencodex/actions/workflows/release.yml) публикует их в npm. + +## Жизненный цикл клиента Remote Hub + +Используйте `ocx connect --pairing-code-stdin`, `ocx connect status`, `ocx sync` и `ocx connect rotate --pairing-code-stdin`. `ocx disconnect` офлайн восстанавливает локальное состояние, но не отзывает ключ hub. Пока подключение активно, `ocx connect revoke --admin-token-stdin` отзывает сохранённый `apiKeyId`; после отключения используйте **Integrations → API Keys** на hub. Секреты передаются только через stdin, не argv. diff --git a/docs-site/src/content/docs/ru/reference/configuration/server.md b/docs-site/src/content/docs/ru/reference/configuration/server.md index 103153bf4f..c65daab76c 100644 --- a/docs-site/src/content/docs/ru/reference/configuration/server.md +++ b/docs-site/src/content/docs/ru/reference/configuration/server.md @@ -209,3 +209,7 @@ context; в ключи OpenAI дополнительно входит reasoning Sidecar'ы Anthropic OAuth повторно используют уже существующий OAuth fingerprint Claude Code от opencodex. Перед использованием прогоните soak-test на нужном аккаунте и ожидаемой нагрузке. + +## Ключи Remote Hub и значения по умолчанию + +`runtimeRole` по умолчанию равен `standalone`. Hub использует `hub.managementPublicOrigin`, loopback-only `hub.managementIngress` (`enabled:false`, если отсутствует) и точные `remoteGui.allowedTailscaleUsers` (пустой список, если отсутствует). Ключ клиента хранится в `service-api-token`, не в `config.json`; во время ротации может появиться `service-api-token.prev`. Статистика не зеркалируется. diff --git a/docs-site/src/content/docs/ru/reference/management-api.md b/docs-site/src/content/docs/ru/reference/management-api.md index 516b30e530..4090e91768 100644 --- a/docs-site/src/content/docs/ru/reference/management-api.md +++ b/docs-site/src/content/docs/ru/reference/management-api.md @@ -277,3 +277,7 @@ fail closed, пока аккаунт отсутствует, а при повт соответствующие команды `ocx`: они обращаются к тому же живому API и возвращают ненулевой код, если прокси недоступен или операция завершилась неудачей. Прямой HTTP полезнее всего там, где интеграции нужен точный контракт endpoint'ов, описанный выше. + +## Удалённые сессии и ротация ключей данных + +`POST /api/keys/rotate {id}` начинает десятиминутный overlap и один раз возвращает новый секрет. `POST /api/keys/rotate/commit {id,rotationId}` подтверждает, `DELETE /api/keys/rotate {id,rotationId}` отменяет. Требуется management auth; ключ данных не подходит. `POST /api/session/logout` требует текущую `gui-session`, совпадающий Origin и CSRF. Admin token получает 403 и не может создать consent session. diff --git a/docs-site/src/content/docs/tr/guides/remote-hub.md b/docs-site/src/content/docs/tr/guides/remote-hub.md new file mode 100644 index 0000000000..ab19f9f06f --- /dev/null +++ b/docs-site/src/content/docs/tr/guides/remote-hub.md @@ -0,0 +1,72 @@ +--- +title: Remote Hub Dağıtımı +description: Loopback yönetimi, Tailscale Serve ve başsız OAuth ile opencodex hub çalıştırma. +--- + +Remote Hub sağlayıcı kimlik bilgilerini, kataloğu ve kullanım kayıtlarını tek ana bilgisayarda tutar. Kimliği doğrulanmış istemciler veri düzlemine doğrudan bağlanır. Yönetim düzlemi ayrıdır: isteğe bağlı dinleyici yalnızca `127.0.0.1` üzerinde çalışır ve pano ile `/api/*` yollarını sunar; `/v1/*`, `/healthz`, `/readyz` veya WebSocket sunmaz. `10101` portunu yayımlamayın ve Tailscale Funnel kullanmayın. + +## Roller ve güven sınırı + +`standalone` her şeyi tek makinede tutar; `hub` sağlayıcı sırları ve kullanımı yönetir; `client` yalnızca bağlantı durumunu ve istemciye özel veri anahtarını saklar. + +```bash +ocx connect https://hub-name.tailnet-name.ts.net --pairing-code-stdin +ocx connect status +ocx sync +``` + +İstemci anahtarı yalnızca sahibinin okuyabildiği `service-api-token` dosyasına yazılır, `config.json` içine yazılmaz. Bağlı kullanım hub deposundan aynı `apiKeyId` ile filtrelenir; bağlantı kesilince yerel depo kullanılır. İki depo birbirini yansıtmaz. + +Admin token sıradan yönetim yapabilir ancak hiçbir zaman onay oturumu oluşturamaz. Onay işlemleri sunucu tarafından verilen `gui-session`, eşleşen Origin ve CSRF ister. `Tailscale-User-Login` yalnızca ayrı yönetim girişinde güvenilirdir; tam kimlikleri `remoteGui.allowedTailscaleUsers` içinde belirtin. + +## Servis ve Tailscale Serve + +```bash +ocx config set runtimeRole hub +ocx config set hostname 100.64.0.10 +ocx config set hub.managementPublicOrigin '"https://hub-name.tailnet-name.ts.net"' +ocx config set corsAllowOrigins '["http://localhost:10100"]' +ocx config set hub.managementIngress '{"enabled":true,"port":10101}' +ocx config set remoteGui.allowedTailscaleUsers '["operator@example.com"]' +export OPENCODEX_API_AUTH_TOKEN="$(openssl rand -hex 32)" +ocx service install +``` + +systemd/launchd korumalı `service-api-token` dosyasını okur; plist veya unit içine gerçek sır yazılmaz. + +```bash +curl --fail --silent http://100.64.0.10:10100/healthz +curl --fail --silent http://100.64.0.10:10100/readyz +tailscale serve --bg --https=443 http://127.0.0.1:10101 +tailscale serve status +``` + +`/healthz` yalnızca işlemin yaşadığını gösterir. `/readyz`, kimlik doğrulamalı `GET /v1/catalog` ve gerçek bir model yanıtını da doğrulayın. Kendi TLS proxy'niz için `tailscale cert hub-name.tailnet-name.ts.net` kullanın ve yalnızca `127.0.0.1:10101` hedefine yönlendirin. `Tailscale-User-*` başlıkları uydurmayın; güvenilir kimlik yoksa tek kullanımlık eşleştirme kullanın. + +## OAuth, döndürme ve bağlantı kesme + +```bash +ocx config set oauthOpenBrowser false +ocx connect rotate --pairing-code-stdin +# yalnızca HTTPS: +ocx connect rotate --admin-token-stdin +``` + +OAuth'u `POST /api/oauth/login` ile başlatın. Callback hub'a ulaşamıyorsa son URL'yi veya kodu `{provider,input}` olarak `POST /api/oauth/login/code` yoluna gönderin. OAuth kodunu argv veya loglara koymayın. + +Döndürme sırasında eski ve yeni anahtar aynı `apiKeyId` altında en fazla on dakika geçerlidir. Eski anahtar `service-api-token.prev` dosyasına alınır, yeni anahtar atomik olarak kurulur ve `/v1/catalog` ile doğrulanıp onaylanır. Sonuç belirsizse geçici yetkiyle komutu yeniden çalıştırın; iki adayı da doğrulamadan silmeyin. + +`ocx disconnect` hub çevrimdışıyken yerel durumu geri yükler ama hub anahtarını iptal etmez. Bağlantıdan sonra tek iptal yolu hub üzerindeki **Integrations → API Keys** sayfasıdır. `ocx connect revoke --admin-token-stdin` yalnızca bağlantı sürerken kullanılabilir. + +## Docker ve sorun giderme + +Resmî Docker imajı yoktur. Bun imajını digest ile sabitleyin, `/home/bun/.opencodex` için volume ve `/run/secrets/ocx_api_token` için secret kullanın. Yalnızca `10100` portunu yayımlayın; `10101` yayımlanmaz. Sırları `ARG`, `ENV`, `COPY`, Compose, imaj geçmişi veya argv içine koymayın. Healthcheck sonrasında readiness, kimlik doğrulamalı katalog ve gerçek yanıtı ayrıca doğrulayın. + +- Hub kapalıysa yerel geri dönüş yapılabilir; uzaktaki anahtarın iptali bekler. +- Geçici arızada doğrulanmış LKG korunur; auth, şema, boyut veya protokol hatasında yerel fallback yoktur. +- `.prev` kurtarmasında iki dosyayı koruyup geçici yetkiyle yeniden çalıştırın. +- `hub-too-new`/`hub-too-old` eski tarafı gösterir; yerel yazımdan önce reddedilir. +- Eşleştirme tek kullanımlıktır ve hatalar 429 ile sınırlanır; kayıp kodu yeniden üretin. +- Loopback dışı HTTP için `--allow-insecure-http` gerekir; admin token HTTP ile gönderilmez. +- Tarayıcı logout/expiry veri anahtarını iptal etmez. +- `tailscale serve reset` tüm eşlemeleri kaldırır; önce durumu inceleyin. diff --git a/docs-site/src/content/docs/tr/guides/web-dashboard.md b/docs-site/src/content/docs/tr/guides/web-dashboard.md index 955148a054..282c1e4106 100644 --- a/docs-site/src/content/docs/tr/guides/web-dashboard.md +++ b/docs-site/src/content/docs/tr/guides/web-dashboard.md @@ -109,6 +109,10 @@ sonra yeni bir görev oluşturduğunda geçerlidir. Kurallı v1/base/v2 davranı için [Alt Ajan Arayüzü](/tr/guides/sub-agent-surface/) sayfasına bakın. ::: +## Remote Hub oturumları, anahtarları ve kullanımı + +Pano yönetim düzlemi doğrudan client→hub model trafiğinden ayrıdır. **Integrations → API Keys** bekleyen döndürmeyi gösterir, yeni sırrı bir kez görüntüler ve açık onay veya iptal ister. Tarayıcı logout yalnızca mevcut oturumu geçersiz kılar. Bağlı kullanım hub üzerinde `apiKeyId` ile filtrelenir; bağlantı kesilince yerel kayıt kullanılır ve yansıtma yapılmaz. + Spawn geçersiz kılma garantisi **yerleşik** v2 rehberlik metni için geçerlidir. Özel bir `injectionPrompt` bu metnin yerini tamamen alır ve `{{model}}` ve `{{effort}}` yer tutucularını (ve isteğe bağlı olarak `{{roster}}`) içermelidir, @@ -254,4 +258,3 @@ kopyalar, böylece [vizyon sidecar'ı](/tr/guides/sidecars/) manuel sınıfland olmadan doğru şekilde geçişlenir. ::: - diff --git a/docs-site/src/content/docs/tr/reference/cli/lifecycle.md b/docs-site/src/content/docs/tr/reference/cli/lifecycle.md index 425380b2fd..533463cfb6 100644 --- a/docs-site/src/content/docs/tr/reference/cli/lifecycle.md +++ b/docs-site/src/content/docs/tr/reference/cli/lifecycle.md @@ -446,3 +446,7 @@ ocx update --tag preview Yeni sürümler, [Sürüm iş akışı](https://github.com/lidge-jun/opencodex/actions/workflows/release.yml) bunları npm'de yayınladığında kullanılabilir hale gelir. + +## Remote Hub istemci yaşam döngüsü + +`ocx connect --pairing-code-stdin`, `ocx connect status`, `ocx sync` ve `ocx connect rotate --pairing-code-stdin` kullanın. `ocx disconnect` yerel durumu çevrimdışı geri yükler ancak hub anahtarını iptal etmez. Bağlıyken `ocx connect revoke --admin-token-stdin` kayıtlı `apiKeyId` değerini iptal eder; bağlantıdan sonra hub üzerindeki **Integrations → API Keys** kullanılmalıdır. Sırlar yalnızca stdin üzerinden geçer, argv'ye yazılmaz. diff --git a/docs-site/src/content/docs/tr/reference/configuration/server.md b/docs-site/src/content/docs/tr/reference/configuration/server.md index 7168ca3f8a..c701c06805 100644 --- a/docs-site/src/content/docs/tr/reference/configuration/server.md +++ b/docs-site/src/content/docs/tr/reference/configuration/server.md @@ -281,3 +281,7 @@ sınırı tüketmez. Uzak `https:` görselleri ve başarısız veya boş açıkl Anthropic OAuth sidecar'ları opencodex'in mevcut Claude Code OAuth parmak izini yeniden kullanır. Hedeflenen hesap ve iş yükünü kapsamlı bir şekilde test edin. + +## Remote Hub anahtarları ve varsayılanlar + +`runtimeRole` varsayılan olarak `standalone` değerindedir. Hub; `hub.managementPublicOrigin`, yalnız loopback `hub.managementIngress` (yokken `enabled:false`) ve tam `remoteGui.allowedTailscaleUsers` (yokken boş) kullanır. İstemci anahtarı `config.json` yerine `service-api-token` içinde kalır; döndürme sırasında `service-api-token.prev` geçici olarak bulunabilir. Kullanım kayıtları yansıtılmaz. diff --git a/docs-site/src/content/docs/tr/reference/management-api.md b/docs-site/src/content/docs/tr/reference/management-api.md index 0ce4456cd9..e79e7be260 100644 --- a/docs-site/src/content/docs/tr/reference/management-api.md +++ b/docs-site/src/content/docs/tr/reference/management-api.md @@ -308,3 +308,7 @@ olduğunda veya işlem başarısız olduğunda sıfır olmayan bir sonuç dönd Doğrudan HTTP, yukarıdaki tam uç nokta sözleşmelerine ihtiyaç duyan entegrasyonlar için en yararlıdır. +## Uzak oturumlar ve veri anahtarı döndürme + +`POST /api/keys/rotate {id}` on dakikalık geçişi başlatır ve yeni sırrı yalnızca bir kez döndürür. `POST /api/keys/rotate/commit {id,rotationId}` onaylar, `DELETE /api/keys/rotate {id,rotationId}` iptal eder. Yönetim kimlik doğrulaması gerekir; veri anahtarı bunları çağıramaz. `POST /api/session/logout` mevcut `gui-session`, eşleşen Origin ve CSRF ister. Admin token 403 alır ve onay oturumu oluşturamaz. + diff --git a/docs-site/src/content/docs/zh-cn/guides/remote-hub.md b/docs-site/src/content/docs/zh-cn/guides/remote-hub.md new file mode 100644 index 0000000000..f9179e8df9 --- /dev/null +++ b/docs-site/src/content/docs/zh-cn/guides/remote-hub.md @@ -0,0 +1,72 @@ +--- +title: Remote Hub 部署 +description: 使用仅回环管理入口、Tailscale Serve 和无头 OAuth 运行 opencodex hub。 +--- + +Remote Hub 将提供商凭据、模型目录和使用记录保存在一台主机上,经过身份验证的客户端直接访问其数据平面。管理平面相互独立:可选管理监听器只绑定 `127.0.0.1`,仅提供控制台和 `/api/*`。它不提供 `/v1/*`、`/healthz`、`/readyz` 或 WebSocket。不要直接发布 `10101`,也不要使用 Tailscale Funnel。 + +## 角色与信任边界 + +`standalone` 在一台机器上运行全部功能;`hub` 保存提供商密钥和使用记录;`client` 只保存连接状态和专属数据密钥。 + +```bash +ocx connect https://hub-name.tailnet-name.ts.net --pairing-code-stdin +ocx connect status +ocx sync +``` + +客户端密钥写入仅所有者可读的 `service-api-token`,绝不会写入 `config.json`。连接期间,使用记录来自 hub 并按稳定的 `apiKeyId` 过滤;断开后显示本地记录。两者不会镜像。 + +Admin token 只能执行普通管理,永远不能创建用户同意会话。用户同意操作必须使用服务器签发的 `gui-session`、匹配的 Origin 和 CSRF。`Tailscale-User-Login` 只在独立管理入口可信;请在 `remoteGui.allowedTailscaleUsers` 中填写准确登录名。 + +## systemd/launchd 与 Tailscale Serve + +```bash +ocx config set runtimeRole hub +ocx config set hostname 100.64.0.10 +ocx config set hub.managementPublicOrigin '"https://hub-name.tailnet-name.ts.net"' +ocx config set corsAllowOrigins '["http://localhost:10100"]' +ocx config set hub.managementIngress '{"enabled":true,"port":10101}' +ocx config set remoteGui.allowedTailscaleUsers '["operator@example.com"]' +export OPENCODEX_API_AUTH_TOKEN="$(openssl rand -hex 32)" +ocx service install +``` + +systemd/launchd 从受保护的 `service-api-token` 读取密钥,plist 和 unit 不包含明文密钥。 + +```bash +curl --fail --silent http://100.64.0.10:10100/healthz +curl --fail --silent http://100.64.0.10:10100/readyz +tailscale serve --bg --https=443 http://127.0.0.1:10101 +tailscale serve status +``` + +`/healthz` 只证明进程存活。还必须验证 `/readyz`、经过身份验证的 `GET /v1/catalog` 和一次真实模型响应。管理端口只能监听 `127.0.0.1`。自建 TLS 代理应使用 `tailscale cert hub-name.tailnet-name.ts.net`,并仅代理到 `127.0.0.1:10101`。不要伪造 `Tailscale-User-*`;没有可信身份时请使用一次性配对。 + +## OAuth、密钥轮换与断开 + +```bash +ocx config set oauthOpenBrowser false +ocx connect rotate --pairing-code-stdin +# 仅限 HTTPS: +ocx connect rotate --admin-token-stdin +``` + +通过 `POST /api/oauth/login` 启动 OAuth。如果回调无法到达 hub,将最终 URL 或授权码作为 `{provider,input}` 发送到 `POST /api/oauth/login/code`。不要把 OAuth 码放入 argv 或日志。 + +轮换期间,旧密钥和新密钥在同一个 `apiKeyId` 下最多同时有效十分钟。旧密钥备份到 `service-api-token.prev`,新密钥以原子方式安装,并通过 `/v1/catalog` 验证后提交。如果提交结果不确定,请使用临时权限重新运行命令;在验证两个候选密钥前不要删除任何文件。 + +`ocx disconnect` 即使 hub 离线也能恢复本地状态,但不会吊销 hub 密钥。断开后,唯一的吊销入口是 hub 的 **Integrations → API Keys**。`ocx connect revoke --admin-token-stdin` 只能在仍连接时使用。 + +## Docker、回滚与排障 + +opencodex 不发布官方 Docker 镜像。请按 digest 固定 Bun 镜像,将 `/home/bun/.opencodex` 挂载为持久卷,并将密钥挂载到 `/run/secrets/ocx_api_token`。只发布 `10100`,不要发布 `10101`。不要把密钥放入 `ARG`、`ENV`、`COPY`、Compose、镜像历史或 argv。healthcheck 后仍需单独验证 readiness、目录和真实请求。 + +- hub 宕机:可以离线断开,但远程密钥仍待吊销。 +- 目录过期:仅在临时故障时保留已验证的 LKG;认证、架构、大小或协议错误不会回退到本地提供商。 +- `.prev` 恢复:保留两个文件,使用临时权限重新运行轮换。 +- `hub-too-new`/`hub-too-old` 会指出需要升级的一端,并在本地写入前失败。 +- 配对码一次性使用,失败次数会触发 429;丢失后请重新创建。 +- 非回环 HTTP 配对必须显式使用 `--allow-insecure-http`;Admin token 绝不通过 HTTP 发送。 +- 浏览器 logout/expiry 只影响会话,不会吊销数据密钥。 +- `tailscale serve reset` 会删除节点上的所有映射,请先查看 `tailscale serve status`。 diff --git a/docs-site/src/content/docs/zh-cn/guides/web-dashboard.md b/docs-site/src/content/docs/zh-cn/guides/web-dashboard.md index 1c8fe541d4..56603ba703 100644 --- a/docs-site/src/content/docs/zh-cn/guides/web-dashboard.md +++ b/docs-site/src/content/docs/zh-cn/guides/web-dashboard.md @@ -74,6 +74,10 @@ Dashboard 的 **Sub-agent delegation** 选择器会保存 `injectionModel`,以 权威说明见 [子代理界面](/zh-cn/guides/sub-agent-surface/)。 ::: +## Remote Hub 会话、密钥与用量 + +控制台管理平面与 client→hub 的模型流量相互独立。**Integrations → API Keys** 显示待处理轮换,只显示一次替换密钥,并要求显式提交或中止。浏览器 logout 只使当前会话失效。连接时从 hub 按 `apiKeyId` 过滤用量;断开后使用本地记录,两者不会镜像。 + 选择器会列出已启用的原生与路由模型,以及全局 Codex reasoning 阶梯。API 会先验证所选强度是否 属于全局阶梯;Codex 仍会根据目标目录条目再次校验该 spawn 强度。 diff --git a/docs-site/src/content/docs/zh-cn/reference/cli/lifecycle.md b/docs-site/src/content/docs/zh-cn/reference/cli/lifecycle.md index d6f5c6d219..9ab50802d1 100644 --- a/docs-site/src/content/docs/zh-cn/reference/cli/lifecycle.md +++ b/docs-site/src/content/docs/zh-cn/reference/cli/lifecycle.md @@ -233,3 +233,7 @@ ocx update --tag preview ``` 当 [Release workflow](https://github.com/lidge-jun/opencodex/actions/workflows/release.yml) 将新版本发布到 npm 时,这些新版本就会变得可用。 + +## Remote Hub 客户端生命周期 + +使用 `ocx connect --pairing-code-stdin`、`ocx connect status`、`ocx sync` 和 `ocx connect rotate --pairing-code-stdin`。`ocx disconnect` 可离线恢复本地状态,但不会吊销 hub 密钥。仍连接时,`ocx connect revoke --admin-token-stdin` 会吊销已保存的 `apiKeyId`;断开后请使用 hub 的 **Integrations → API Keys**。密钥只能通过 stdin 传递,不能放入 argv。 diff --git a/docs-site/src/content/docs/zh-cn/reference/configuration/server.md b/docs-site/src/content/docs/zh-cn/reference/configuration/server.md index 2f20d35a9f..df1452ee7b 100644 --- a/docs-site/src/content/docs/zh-cn/reference/configuration/server.md +++ b/docs-site/src/content/docs/zh-cn/reference/configuration/server.md @@ -175,3 +175,7 @@ routed 重放会把主 ChatGPT 认证注入内部请求。Anthropic 后端使用 支持的等级受上游提供方能力与所选模型公布的推理阶梯限制。Vision 只会对发送给其提供方 `noVisionModels` 中模型的图像生效。OpenAI 具有与 search 相同的登录/forward 要求;显式选择的 Anthropic 在没有可用凭据时会失败并关闭。成功的 `data:` 描述会使用一个受限缓存,其键由后端、模型、detail、图像字节以及规范化消息上下文组成;OpenAI 的键还会额外包含推理强度(Anthropic 键不含)。命中和同轮重复不会消耗限额。远程 `https:` 图像以及失败或空的描述不会被缓存。 Anthropic OAuth 侧车会复用 opencodex 现有的 Claude Code OAuth 指纹。请对目标账户和负载进行 soak 测试。 + +## Remote Hub 密钥与默认值 + +`runtimeRole` 默认为 `standalone`。Hub 使用 `hub.managementPublicOrigin`、仅回环的 `hub.managementIngress`(缺省为 `enabled:false`)和准确的 `remoteGui.allowedTailscaleUsers`(缺省为空)。客户端密钥保存在 `service-api-token` 而不是 `config.json`;轮换期间可能暂时存在 `service-api-token.prev`。使用记录不会镜像。 diff --git a/docs-site/src/content/docs/zh-cn/reference/management-api.md b/docs-site/src/content/docs/zh-cn/reference/management-api.md index fe3568e3f0..d92ec60a41 100644 --- a/docs-site/src/content/docs/zh-cn/reference/management-api.md +++ b/docs-site/src/content/docs/zh-cn/reference/management-api.md @@ -243,3 +243,7 @@ Authorization: Bearer ## 如何选择客户端 对于日常管理,[Web 仪表板](/guides/web-dashboard/)提供了最安全的引导式流程。对于无头主机和自动化,请使用相应的 `ocx` 命令:它们调用的是同一个实时 API,并在代理不可达或操作失败时返回非零结果。直接 HTTP 最适合需要上述精确端点契约的集成。 + +## 远程会话与数据密钥轮换 + +`POST /api/keys/rotate {id}` 开始十分钟重叠期,并只返回一次新密钥。`POST /api/keys/rotate/commit {id,rotationId}` 提交,`DELETE /api/keys/rotate {id,rotationId}` 中止。它们都需要管理认证,数据密钥不能调用。`POST /api/session/logout` 需要当前 `gui-session`、匹配的 Origin 和 CSRF。Admin token 会收到 403,永远不能创建用户同意会话。 diff --git a/docs-site/src/content/docs/zh-tw/guides/remote-hub.md b/docs-site/src/content/docs/zh-tw/guides/remote-hub.md new file mode 100644 index 0000000000..3c05e6f100 --- /dev/null +++ b/docs-site/src/content/docs/zh-tw/guides/remote-hub.md @@ -0,0 +1,72 @@ +--- +title: Remote Hub 部署 +description: 使用僅限迴路的管理入口、Tailscale Serve 與無頭 OAuth 執行 opencodex hub。 +--- + +Remote Hub 把供應商憑證、模型目錄與用量記錄保存在一台主機上,已驗證的用戶端直接連到資料平面。管理平面彼此分離:選用的管理監聽器只綁定 `127.0.0.1`,僅提供儀表板與 `/api/*`。它不提供 `/v1/*`、`/healthz`、`/readyz` 或 WebSocket。不要直接發布 `10101`,也不要使用 Tailscale Funnel。 + +## 角色與信任邊界 + +`standalone` 在同一台機器上執行全部功能;`hub` 保存供應商金鑰與用量;`client` 只保存連線狀態與專屬資料金鑰。 + +```bash +ocx connect https://hub-name.tailnet-name.ts.net --pairing-code-stdin +ocx connect status +ocx sync +``` + +用戶端金鑰會寫入只有擁有者可讀的 `service-api-token`,絕不寫入 `config.json`。連線期間,用量來自 hub 並依穩定的 `apiKeyId` 篩選;中斷後則顯示本機記錄。兩者不會互相鏡像。 + +Admin token 只能執行一般管理,永遠不能建立使用者同意工作階段。同意操作必須使用伺服器簽發的 `gui-session`、相符的 Origin 與 CSRF。`Tailscale-User-Login` 只在獨立管理入口可信;請在 `remoteGui.allowedTailscaleUsers` 填入完整且正確的登入名稱。 + +## systemd/launchd 與 Tailscale Serve + +```bash +ocx config set runtimeRole hub +ocx config set hostname 100.64.0.10 +ocx config set hub.managementPublicOrigin '"https://hub-name.tailnet-name.ts.net"' +ocx config set corsAllowOrigins '["http://localhost:10100"]' +ocx config set hub.managementIngress '{"enabled":true,"port":10101}' +ocx config set remoteGui.allowedTailscaleUsers '["operator@example.com"]' +export OPENCODEX_API_AUTH_TOKEN="$(openssl rand -hex 32)" +ocx service install +``` + +systemd/launchd 從受保護的 `service-api-token` 讀取金鑰,plist 與 unit 不包含明文金鑰。 + +```bash +curl --fail --silent http://100.64.0.10:10100/healthz +curl --fail --silent http://100.64.0.10:10100/readyz +tailscale serve --bg --https=443 http://127.0.0.1:10101 +tailscale serve status +``` + +`/healthz` 只證明程序仍在執行。還必須驗證 `/readyz`、已驗證的 `GET /v1/catalog` 與一次真實模型回應。管理連接埠只能監聽 `127.0.0.1`。自管 TLS proxy 應使用 `tailscale cert hub-name.tailnet-name.ts.net`,並只代理到 `127.0.0.1:10101`。不要偽造 `Tailscale-User-*`;沒有可信身分時請使用一次性配對。 + +## OAuth、金鑰輪替與中斷連線 + +```bash +ocx config set oauthOpenBrowser false +ocx connect rotate --pairing-code-stdin +# 僅限 HTTPS: +ocx connect rotate --admin-token-stdin +``` + +透過 `POST /api/oauth/login` 啟動 OAuth。若 callback 無法連到 hub,請把最終 URL 或授權碼以 `{provider,input}` 傳送到 `POST /api/oauth/login/code`。不要把 OAuth 碼放入 argv 或記錄。 + +輪替期間,舊金鑰與新金鑰會在同一個 `apiKeyId` 下最多同時有效十分鐘。舊金鑰備份到 `service-api-token.prev`,新金鑰以原子方式安裝,透過 `/v1/catalog` 驗證後再提交。若提交結果不確定,請使用暫時權限重新執行命令;驗證兩個候選金鑰前不要刪除任何檔案。 + +`ocx disconnect` 即使 hub 離線也能還原本機狀態,但不會撤銷 hub 金鑰。中斷後,唯一的撤銷入口是 hub 的 **Integrations → API Keys**。`ocx connect revoke --admin-token-stdin` 只能在仍連線時使用。 + +## Docker、回復與疑難排解 + +opencodex 不發布官方 Docker 映像。請用 digest 固定 Bun 映像,把 `/home/bun/.opencodex` 掛載為持久 volume,並把金鑰掛載到 `/run/secrets/ocx_api_token`。只發布 `10100`,不要發布 `10101`。不要把金鑰放入 `ARG`、`ENV`、`COPY`、Compose、映像歷史或 argv。healthcheck 後仍須分別驗證 readiness、目錄與真實請求。 + +- hub 無法連線:可以離線中斷,但遠端金鑰仍待撤銷。 +- 目錄過期:僅在暫時故障時保留已驗證的 LKG;驗證、結構、大小或協定錯誤不會切換到本機供應商。 +- `.prev` 復原:保留兩個檔案,使用暫時權限重新執行輪替。 +- `hub-too-new`/`hub-too-old` 會指出需要升級的一端,並在本機寫入前失敗。 +- 配對碼只能使用一次,失敗次數會觸發 429;遺失後請重新建立。 +- 非迴路 HTTP 配對必須明確使用 `--allow-insecure-http`;Admin token 絕不透過 HTTP 傳送。 +- 瀏覽器 logout/expiry 只影響工作階段,不會撤銷資料金鑰。 +- `tailscale serve reset` 會刪除節點上的所有映射,請先查看 `tailscale serve status`。 diff --git a/docs-site/src/content/docs/zh-tw/guides/web-dashboard.md b/docs-site/src/content/docs/zh-tw/guides/web-dashboard.md index 4b22882f66..ccc126a2df 100644 --- a/docs-site/src/content/docs/zh-tw/guides/web-dashboard.md +++ b/docs-site/src/content/docs/zh-tw/guides/web-dashboard.md @@ -75,6 +75,10 @@ Dashboard 的 **Sub-agent delegation** 選擇器會儲存 `injectionModel`,以 權威說明見 [子代理介面](/zh-tw/guides/sub-agent-surface/)。 ::: +## Remote Hub 工作階段、金鑰與用量 + +儀表板管理平面與 client→hub 模型流量彼此獨立。**Integrations → API Keys** 顯示待處理輪替,只顯示一次替代金鑰,並要求明確提交或中止。瀏覽器 logout 只會使目前工作階段失效。連線時從 hub 依 `apiKeyId` 篩選用量;中斷後使用本機記錄,兩者不會鏡像。 + 選擇器會列出已啟用的原生與路由模型,以及全域 Codex reasoning 階梯。API 會先驗證所選強度是否 屬於全域階梯;Codex 仍會根據目標目錄條目再次校驗該 spawn 強度。 diff --git a/docs-site/src/content/docs/zh-tw/reference/cli/lifecycle.md b/docs-site/src/content/docs/zh-tw/reference/cli/lifecycle.md index 11fa127e23..2307dbf6c3 100644 --- a/docs-site/src/content/docs/zh-tw/reference/cli/lifecycle.md +++ b/docs-site/src/content/docs/zh-tw/reference/cli/lifecycle.md @@ -237,3 +237,7 @@ ocx update --tag preview ``` 當 [Release workflow](https://github.com/lidge-jun/opencodex/actions/workflows/release.yml) 將新版本發布到 npm 時,新版本即可使用。 + +## Remote Hub 用戶端生命週期 + +使用 `ocx connect --pairing-code-stdin`、`ocx connect status`、`ocx sync` 與 `ocx connect rotate --pairing-code-stdin`。`ocx disconnect` 可離線還原本機狀態,但不會撤銷 hub 金鑰。仍連線時,`ocx connect revoke --admin-token-stdin` 會撤銷已保存的 `apiKeyId`;中斷後請使用 hub 的 **Integrations → API Keys**。秘密值只能透過 stdin 傳遞,不能放入 argv。 diff --git a/docs-site/src/content/docs/zh-tw/reference/configuration/server.md b/docs-site/src/content/docs/zh-tw/reference/configuration/server.md index b536704e26..aab24e9f26 100644 --- a/docs-site/src/content/docs/zh-tw/reference/configuration/server.md +++ b/docs-site/src/content/docs/zh-tw/reference/configuration/server.md @@ -194,3 +194,7 @@ OpenAI backend 需要 ChatGPT 登入與啟用的 ChatGPT `forward` 供應商。C 視覺僅對發送到其供應商 `noVisionModels` 中模型的圖片啟用。OpenAI 的登入/forward 需求與搜尋相同;明確選擇的 Anthropic 在無可用憑證時 fail closed。成功的 `data:` 描述使用以 backend、模型、細節、圖片位元組與正規化訊息 context 為 key 的有界快取。命中與同回合重複不消耗限制。遠端 `https:` 圖片與失敗或空的描述不被快取。 Anthropic OAuth sidecar 重用 opencodex 既有的 Claude Code OAuth 指紋。請對預期帳號與工作負載進行浸泡測試。 + +## Remote Hub 金鑰與預設值 + +`runtimeRole` 預設為 `standalone`。Hub 使用 `hub.managementPublicOrigin`、僅限迴路的 `hub.managementIngress`(缺省為 `enabled:false`)與正確的 `remoteGui.allowedTailscaleUsers`(缺省為空)。用戶端金鑰保存在 `service-api-token` 而不是 `config.json`;輪替期間可能暫時存在 `service-api-token.prev`。用量不會鏡像。 diff --git a/docs-site/src/content/docs/zh-tw/reference/management-api.md b/docs-site/src/content/docs/zh-tw/reference/management-api.md index 5dc158a98d..a113afac3c 100644 --- a/docs-site/src/content/docs/zh-tw/reference/management-api.md +++ b/docs-site/src/content/docs/zh-tw/reference/management-api.md @@ -225,3 +225,7 @@ Session 簽發在需要 data-plane 認證時停用,這包含遠端綁定。遠 ## 選擇客戶端 對於普通管理,[網頁儀表板](/zh-tw/guides/web-dashboard/)提供最安全的引導工作流程。對於無頭主機與自動化,請使用對應的 `ocx` 指令:它們呼叫此相同的即時 API,並在代理不可達或操作失敗時回傳非零結果。直接 HTTP 對需要上述精確端點契約的整合最為有用。 + +## 遠端工作階段與資料金鑰輪替 + +`POST /api/keys/rotate {id}` 開始十分鐘重疊期,且只回傳一次新金鑰。`POST /api/keys/rotate/commit {id,rotationId}` 提交,`DELETE /api/keys/rotate {id,rotationId}` 中止。全部都需要管理驗證,資料金鑰不能呼叫。`POST /api/session/logout` 需要目前的 `gui-session`、相符的 Origin 與 CSRF。Admin token 會收到 403,永遠不能建立使用者同意工作階段。 diff --git a/structure/01_runtime.md b/structure/01_runtime.md index 3cc98a81a2..7a5139cfad 100644 --- a/structure/01_runtime.md +++ b/structure/01_runtime.md @@ -158,3 +158,7 @@ destination, and key boundary instead of being silently canonicalized onto the n OAuth presets resolve discovery against the same canonical registry transport as normal routing before any adapter-specific transport override, so a stale configured `baseUrl` cannot receive an OAuth bearer token. + +## Remote Hub hardening ownership + +`src/remote/protocol.ts` owns pure interval/feature negotiation. `src/client/hub-client.ts` owns bounded, schema-validated remote catalog consumption and key-id probes. `src/client/hub-relay.ts` is a fixed-authority management relay with URL, header, body, redirect, and stream bounds. The public data listener remains the direct client→hub path; the loopback management ingress never serves data-plane routes. diff --git a/structure/02_config-and-codex-home.md b/structure/02_config-and-codex-home.md index ddfd1d67e0..8da3d37316 100644 --- a/structure/02_config-and-codex-home.md +++ b/structure/02_config-and-codex-home.md @@ -436,3 +436,7 @@ uninstall with their exact paths. Legacy nonempty config directories are deliberately not retroactively claimed. If either ownership file is missing, malformed, or bound to another root, uninstall refuses config deletion and reports the residual directory for manual review; there is no recursive-delete fallback. + +## Remote client key files + +Client connection metadata stores a stable `apiKeyId` and a non-secret rotation `pendingOperation`. The current data secret remains only in `service-api-token`; a bounded rotation temporarily keeps the old secret in owner-only `service-api-token.prev`. Commit or recovery clears the marker before orphan cleanup. `ocx disconnect` is local-only and leaves remote revocation to the hub's **Integrations → API Keys** page. Hub and local usage stores are not mirrored. diff --git a/structure/05_gui-and-management-api.md b/structure/05_gui-and-management-api.md index d6d374a149..9d011d7c4a 100644 --- a/structure/05_gui-and-management-api.md +++ b/structure/05_gui-and-management-api.md @@ -380,3 +380,7 @@ the next start (legacy `OCX_DEBUG_FRAMES` still enables the same path). Lines use the `[ocx::]` prefix, go to the proxy terminal, and are buffered for `ocx debug provider logs` / `ocx debug provider logs -f`. Usage JSONL tails with `ocx debug usage logs [-f]`. Separate from provider buffered logs above. + +## Remote credentials and bounded sessions + +Data keys authorize only the data matrix and authenticated catalog. Admin credentials authorize ordinary management and key rotation but cannot mint, exchange, or refresh a `gui-session`. Pairing grants are digest-only, origin-bound, one-use, capped at 128 live grants, burned after five grant failures, and source-limited after ten failures in ten minutes with at most 1,024 source buckets. `POST /api/session/logout` invalidates only the current origin/CSRF-authorized browser session. diff --git a/structure/06_docs-and-release.md b/structure/06_docs-and-release.md index 5c8f7346ad..6305785c38 100644 --- a/structure/06_docs-and-release.md +++ b/structure/06_docs-and-release.md @@ -272,3 +272,7 @@ The Release workflow remains manual and publish-focused. Before any dry-run or p checks that the exact release commit (`GITHUB_SHA`) already has a successful Cross-platform CI run. This keeps release runs short and makes release a deployment of a verified commit rather than a second CI pipeline. + +## Remote Hub locale and release gate + +The Remote Hub guide and affected CLI, server-config, management-API, and dashboard references have eight sources: root English plus `fr`, `ko`, `zh-cn`, `zh-tw`, `ru`, `ja`, and `tr`. English is canonical; commands, defaults, endpoint auth, and warnings remain exact in translations. A release requires the remote-only focused/full gates, privacy scan, GUI/docs builds, protocol compatibility receipts, and the MAINTAINERS security review for the exact head. diff --git a/structure/09_client-integrations.md b/structure/09_client-integrations.md index 42f0f0df3a..7c535e07b9 100644 --- a/structure/09_client-integrations.md +++ b/structure/09_client-integrations.md @@ -97,3 +97,7 @@ Behavior changes require real writer tests against a temporary home and state st cover accepted derived metadata, protected connection edits, protected authoritative context, catalog changes after a derived rewrite, and legacy-record fail-closed behavior. Synthetic fingerprint-only tests are supplementary; they cannot prove the status and writer paths agree. + +## Remote connection lifecycle + +Remote clients journal and restore native integrations locally while model traffic travels directly to the hub. Catalog writes occur only after protocol negotiation and full remote schema validation. The management relay is launcher-scoped and fixed to the connection's management origin. Claude/Codex launch behavior remains integration-scoped. Key rotation uses `pendingOperation` plus `.prev`; disconnect restores locally without hub-side revocation or usage mirroring. From 88d9889bb4648e7b34233de906eaf4f6c8fe6b56 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 05:08:06 +0900 Subject: [PATCH 081/172] fix(hardening): gate startup on rotation recovery state --- src/cli/connect.ts | 19 ++----------- src/cli/index.ts | 6 +++- src/client/state.ts | 40 ++++++++++++++++++++++++++ src/server/index.ts | 2 +- tests/api-keys-routes.test.ts | 53 +++++++++++++++++++++++------------ tests/proxy-liveness.test.ts | 1 + 6 files changed, 85 insertions(+), 36 deletions(-) diff --git a/src/cli/connect.ts b/src/cli/connect.ts index cb6871f1d2..0e9febaf67 100644 --- a/src/cli/connect.ts +++ b/src/cli/connect.ts @@ -6,8 +6,8 @@ import { rotateConnectedClientKey, connectClient, } from "../client/connect"; -import { readClientConnectionState } from "../client/state"; -import { readServiceApiTokenState, readTokenBackupState, removeOrphanTokenBackup } from "../lib/service-secrets"; +import { inspectClientRotationRecoveryGate, readClientConnectionState } from "../client/state"; +import { readServiceApiTokenState } from "../lib/service-secrets"; import type { OcxConnectedClientId } from "../types"; import { CliUsageError, @@ -54,20 +54,7 @@ export type ClientConnectionStatus = { export function collectClientConnectionStatus(now = Date.now()): ClientConnectionStatus { const state = readClientConnectionState(); const tokenState = readServiceApiTokenState(); - const backup = readTokenBackupState(); - let rotation: ClientConnectionStatus["rotation"] = state.kind === "connected" && state.value.pendingOperation - ? "recovery-required" - : backup.kind === "unsafe" - ? "unsafe" - : "clean"; - if (rotation === "clean" && backup.kind === "present" && tokenState.kind === "present") { - try { - removeOrphanTokenBackup(); - rotation = "orphan-cleaned"; - } catch { - rotation = "unsafe"; - } - } + const rotation = inspectClientRotationRecoveryGate(state).kind; let catalog: ClientConnectionStatus["catalog"] = "missing"; if (existsSync(DEFAULT_CATALOG_PATH)) { try { diff --git a/src/cli/index.ts b/src/cli/index.ts index 9afe21ec02..de491b5323 100755 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -9,7 +9,7 @@ import { runCodexHistoryJob, } from "../codex/history-job"; import { reconcileJournal } from "../codex/journal"; -import { readClientConnectionState } from "../client/state"; +import { inspectClientRotationRecoveryGate, readClientConnectionState } from "../client/state"; import { codexAutoStartEnabled, getConfigDir, @@ -273,7 +273,11 @@ async function handleStart(options: { block?: boolean } = {}) { if (clientState.kind === "invalid" || clientState.kind === "mismatched") { throw new Error(`client startup refused: ${clientState.reason}`); } + const rotationGate = inspectClientRotationRecoveryGate(clientState); if (clientState.kind === "connected") { + if (rotationGate.kind === "recovery-required" || rotationGate.kind === "unsafe") { + throw new Error(`client startup refused: ${rotationGate.reason}`); + } const { startClientRuntime } = await import("../client/runtime"); await startClientRuntime({ port: requestedPort, block: options.block }); return; diff --git a/src/client/state.ts b/src/client/state.ts index e702a30f8b..73ec5f80c5 100644 --- a/src/client/state.ts +++ b/src/client/state.ts @@ -8,6 +8,11 @@ import { saveConfig, } from "../config"; import type { OcxClientConnectionConfig } from "../types"; +import { + readServiceApiTokenState, + readTokenBackupState, + removeOrphanTokenBackup, +} from "../lib/service-secrets"; export type ClientConnectionState = | { kind: "disconnected" } @@ -15,6 +20,12 @@ export type ClientConnectionState = | { kind: "invalid"; reason: string } | { kind: "mismatched"; reason: string }; +export type ClientRotationRecoveryGate = + | { kind: "clean" } + | { kind: "orphan-cleaned" } + | { kind: "recovery-required"; reason: string } + | { kind: "unsafe"; reason: string }; + function rawTopLevelConfig(): Record | null { try { const parsed = JSON.parse(readFileSync(getConfigPath(), "utf8").replace(/^\uFEFF/, "")) as unknown; @@ -60,6 +71,35 @@ export function readClientConnectionState(): ClientConnectionState { return { kind: "connected", value: client }; } +export function inspectClientRotationRecoveryGate( + state: ClientConnectionState = readClientConnectionState(), +): ClientRotationRecoveryGate { + const current = readServiceApiTokenState(); + const backup = readTokenBackupState(); + if (state.kind === "connected" && state.value.pendingOperation) { + if (current.kind !== "present" || backup.kind !== "present") { + return { + kind: "unsafe", + reason: "pending key rotation requires owner-only service-api-token and service-api-token.prev files", + }; + } + return { + kind: "recovery-required", + reason: "rerun ocx connect rotate with --pairing-code-stdin or --admin-token-stdin", + }; + } + if (backup.kind === "unsafe") return { kind: "unsafe", reason: backup.reason }; + if (backup.kind === "present" && current.kind === "present") { + try { + removeOrphanTokenBackup(); + return { kind: "orphan-cleaned" }; + } catch (error) { + return { kind: "unsafe", reason: error instanceof Error ? error.message : "token backup cleanup failed" }; + } + } + return { kind: "clean" }; +} + export function commitClientConnection( state: OcxClientConnectionConfig, ): "committed" | "unchanged" { diff --git a/src/server/index.ts b/src/server/index.ts index 4294e2161c..0fe675463c 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -1943,7 +1943,7 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server { + testHome = mkdtempSync(join(tmpdir(), "ocx-api-keys-routes-")); + process.env.OPENCODEX_HOME = testHome; + delete process.env.OPENCODEX_API_AUTH_TOKEN; + process.env.OPENCODEX_ADMIN_AUTH_TOKEN = ADMIN_TOKEN; +}); + +afterEach(() => { + if (previousHome === undefined) delete process.env.OPENCODEX_HOME; + else process.env.OPENCODEX_HOME = previousHome; + if (previousDataToken === undefined) delete process.env.OPENCODEX_API_AUTH_TOKEN; + else process.env.OPENCODEX_API_AUTH_TOKEN = previousDataToken; + if (previousAdminToken === undefined) delete process.env.OPENCODEX_ADMIN_AUTH_TOKEN; + else process.env.OPENCODEX_ADMIN_AUTH_TOKEN = previousAdminToken; + if (testHome) rmSync(testHome, { recursive: true, force: true }); + testHome = ""; +}); + describe("API key rotation", () => { test("overlaps under one id, masks the pending secret, and commits atomically", async () => { saveConfig(baseConfig()); @@ -127,24 +145,6 @@ describe("API key rotation", () => { }); }); -beforeEach(() => { - testHome = mkdtempSync(join(tmpdir(), "ocx-api-keys-routes-")); - process.env.OPENCODEX_HOME = testHome; - delete process.env.OPENCODEX_API_AUTH_TOKEN; - process.env.OPENCODEX_ADMIN_AUTH_TOKEN = ADMIN_TOKEN; -}); - -afterEach(() => { - if (previousHome === undefined) delete process.env.OPENCODEX_HOME; - else process.env.OPENCODEX_HOME = previousHome; - if (previousDataToken === undefined) delete process.env.OPENCODEX_API_AUTH_TOKEN; - else process.env.OPENCODEX_API_AUTH_TOKEN = previousDataToken; - if (previousAdminToken === undefined) delete process.env.OPENCODEX_ADMIN_AUTH_TOKEN; - else process.env.OPENCODEX_ADMIN_AUTH_TOKEN = previousAdminToken; - if (testHome) rmSync(testHome, { recursive: true, force: true }); - testHome = ""; -}); - describe("POST /api/keys", () => { test("a raw pairing grant cannot authorize the key route", async () => { saveConfig({ @@ -362,6 +362,23 @@ describe("DELETE /api/keys", () => { }); describe("apiKeys config compatibility", () => { + test("a malformed pending rotation degrades independently and keeps the current key", () => { + saveConfig(baseConfig()); + const raw = readRawConfig(); + raw.apiKeys = [{ + id: "stable-id", + name: "client", + key: "ocx_data_current", + createdAt: "2026-08-28T00:00:00.000Z", + pendingRotation: { id: 7, key: "leaked-junk", expiresAt: "never" }, + }]; + writeRawConfig(raw); + const loaded = loadConfig(); + expect(loaded.apiKeys?.[0]).toMatchObject({ id: "stable-id", key: "ocx_data_current" }); + expect(loaded.apiKeys?.[0]?.pendingRotation).toBeUndefined(); + expect(isDataPlaneAdmissionSecret("ocx_data_current", loaded)).toBe(true); + }); + test("a non-array apiKeys value does not reset the config", () => { saveConfig(baseConfig()); const raw = readRawConfig(); diff --git a/tests/proxy-liveness.test.ts b/tests/proxy-liveness.test.ts index b0e1b6fd48..e590a8be48 100644 --- a/tests/proxy-liveness.test.ts +++ b/tests/proxy-liveness.test.ts @@ -760,6 +760,7 @@ describe("remote readiness protocol metadata", () => { expect(checkRemoteProtocolCompatibility({ ...metadata, protocol: 2 })).toEqual({ ok: true, metadata: { ...metadata, protocol: 2 }, + features: new Set(), }); }); }); From 9bded9c4163371849b50490240228a158ce47265 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 05:08:06 +0900 Subject: [PATCH 082/172] feat(hardening): expose remote session logout control --- gui/src/App.tsx | 25 ++++++++++++++++++++++++- gui/src/api.ts | 14 ++++++++++++++ gui/src/i18n/de.ts | 3 +++ gui/src/i18n/en.ts | 3 +++ gui/src/i18n/fr.ts | 3 +++ gui/src/i18n/ja.ts | 3 +++ gui/src/i18n/ko.ts | 3 +++ gui/src/i18n/ru.ts | 3 +++ gui/src/i18n/tr.ts | 3 +++ gui/src/i18n/zh-TW.ts | 3 +++ gui/src/i18n/zh.ts | 3 +++ 11 files changed, 65 insertions(+), 1 deletion(-) diff --git a/gui/src/App.tsx b/gui/src/App.tsx index d6167ab2cc..ae83d0f916 100644 --- a/gui/src/App.tsx +++ b/gui/src/App.tsx @@ -15,7 +15,7 @@ import { SidebarGithubRow } from "./components/sidebar-github-row"; import { IconGrid, IconServer, IconBoxes, IconBot, IconList, IconActivity, IconHardDrive, IconKey, IconMenu, IconSun, IconMoon, IconMonitor, IconGlobe, IconPower, IconX, IconRefresh} from "./icons"; import { useI18n, useT, LOCALES, localeDisplayName, type Locale, type TKey } from "./i18n/shared"; import { Select } from "./ui"; -import { configureApiTargets, hasApiSession, installApiAuthFetch, installApiSessionFromHtml } from "./api"; +import { configureApiTargets, hasApiSession, installApiAuthFetch, installApiSessionFromHtml, logoutApiSession } from "./api"; import { apiBaseForPlane, discoverApiTargets, isConnectedRuntime, standaloneApiTargets, type ApiTargets } from "./api-targets"; import { ConnectPairingForm } from "./connect-pairing"; import { type Page } from "./app-routing"; @@ -111,6 +111,7 @@ export default function App() { const [targetsSettled, setTargetsSettled] = useState(() => !isConnectedRuntime()); const [targetError, setTargetError] = useState(false); const [sharedSessionReady, setSharedSessionReady] = useState(() => hasApiSession("shared")); + const [sessionLoggingOut, setSessionLoggingOut] = useState(false); useEffect(() => { const controller = new AbortController(); @@ -233,6 +234,15 @@ export default function App() { } }; + const handleSessionLogout = async () => { + if (sessionLoggingOut) return; + setSessionLoggingOut(true); + const loggedOut = await logoutApiSession("shared"); + setSessionLoggingOut(false); + if (loggedOut) setSharedSessionReady(false); + else alert(t("connection.sessionLogoutFailed")); + }; + const brand = (
@@ -252,6 +262,12 @@ export default function App() { {brand}
+ {targets.connected && sharedSessionReady && ( + + )} + )}

{t("api.rotation.title")}

- {selected.pendingRotation ? ( + {selectedRotationId ? ( <>

{t("api.rotation.pending")}

-

{t("api.rotation.expires")} {formatCreatedDate(selected.pendingRotation.expiresAt, localeTag)}

+ {selected.pendingRotation && ( +

{t("api.rotation.expires")} {formatCreatedDate(selected.pendingRotation.expiresAt, localeTag)}

+ )} {rotationSecret?.id === selected.id && (

{t("api.rotation.secretOnce")}

From c6937001e40a336f7f75eb9112065ca92f0e9f98 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 05:09:00 +0900 Subject: [PATCH 084/172] fix(hardening): reject mismatched catalog validators --- tests/remote-catalog.test.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/remote-catalog.test.ts b/tests/remote-catalog.test.ts index 840e775645..8b0629d80c 100644 --- a/tests/remote-catalog.test.ts +++ b/tests/remote-catalog.test.ts @@ -76,10 +76,18 @@ describe("remote catalog adversarial consumer", () => { expect(refreshed.kind).toBe("fresh"); }); - test("a second 304 without LKG is a protocol error and non-JSON content is refused", async () => { + test("any 304 is a protocol error and non-JSON content is refused", async () => { + // The client sends no conditional request — /v1/catalog emits no validator (Phase 1, + // D2) — so a 304 can only come from a hub that is misconfigured or being impersonated. + // Earlier revisions of this phase distinguished "304 with no last-known-good" from + // "304 whose ETag disagrees with the one we sent"; neither situation is reachable now, + // and the single refusal below is strictly wider than both. await expect(downloadClientCatalog("https://hub.example.test", "ocx_data_test", { fetchImpl: async () => new Response(null, { status: 304 }), - })).rejects.toMatchObject({ code: "catalog_304_without_lkg" }); + })).rejects.toMatchObject({ code: "catalog_unexpected_304" }); + await expect(downloadClientCatalog("https://hub.example.test", "ocx_data_test", { + fetchImpl: async () => new Response(null, { status: 304, headers: { ETag: '"other"' } }), + })).rejects.toMatchObject({ code: "catalog_unexpected_304" }); await expect(downloadClientCatalog("https://hub.example.test", "ocx_data_test", { fetchImpl: async () => new Response('{"models":[]}', { headers: { "Content-Type": "text/html" } }), })).rejects.toMatchObject({ code: "catalog_content_type_invalid" }); From e5bca8dfc94c13e9043a5c4e033d570bc6a5d5a5 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 05:09:31 +0900 Subject: [PATCH 085/172] test(hardening): cover subprocess protocol skew matrix --- tests/cli-ready-subprocess.test.ts | 67 ++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/tests/cli-ready-subprocess.test.ts b/tests/cli-ready-subprocess.test.ts index 557b1a671b..0d9756da92 100644 --- a/tests/cli-ready-subprocess.test.ts +++ b/tests/cli-ready-subprocess.test.ts @@ -69,6 +69,73 @@ function writeRuntimePort(opencodexHome: string, port: number, pid: number): voi } describe("ocx ready real subprocess", () => { + test("released-process protocol skew matrix rejects before any local write", async () => { + const homes = isolatedHomes("ocx-protocol-skew-subprocess-"); + const script = ` + const fs = require("node:fs"); + const { checkRemoteProtocolCompatibility } = require("./src/remote/protocol"); + const base = { protocol: 1, minimumClientProtocol: 1, managementUrl: "https://hub.example.test" }; + const rows = { + baseline: checkRemoteProtocolCompatibility(base), + featureIntersection: checkRemoteProtocolCompatibility({ ...base, protocol: 2, features: ["rotation", "future"] }, { protocol: 1, minimumHubProtocol: 1, features: ["rotation"] }), + hubTooNew: checkRemoteProtocolCompatibility({ ...base, protocol: 2, minimumClientProtocol: 2 }), + hubTooOld: checkRemoteProtocolCompatibility(base, { protocol: 2, minimumHubProtocol: 2 }), + unknownFeature: checkRemoteProtocolCompatibility({ ...base, features: ["unknown-x"] }), + malformed: [undefined, 0, NaN, 1.5, -1].map(protocol => checkRemoteProtocolCompatibility({ ...base, protocol })), + }; + console.log(JSON.stringify({ + rows: { + baseline: rows.baseline.ok, + featureIntersection: rows.featureIntersection.ok ? [...rows.featureIntersection.features] : [], + hubTooNew: rows.hubTooNew, + hubTooOld: rows.hubTooOld, + unknownFeature: rows.unknownFeature.ok ? [...rows.unknownFeature.features] : [], + malformed: rows.malformed.map(row => row.ok ? "accepted" : row.reason), + }, + opencodexFiles: fs.readdirSync(process.env.OPENCODEX_HOME), + codexFiles: fs.readdirSync(process.env.CODEX_HOME), + })); + `; + const child = Bun.spawn([process.execPath, "--eval", script], { + cwd: repoRoot, + env: { ...process.env, OPENCODEX_HOME: homes.opencodexHome, CODEX_HOME: homes.codexHome }, + stdout: "pipe", + stderr: "pipe", + }); + try { + const [exitCode, stdout, stderr] = await Promise.all([ + child.exited, + new Response(child.stdout).text(), + new Response(child.stderr).text(), + ]); + expect(exitCode).toBe(0); + expect(stderr).toBe(""); + expect(JSON.parse(stdout.trim())).toEqual({ + rows: { + baseline: true, + featureIntersection: ["rotation"], + hubTooNew: { + ok: false, + reason: "hub-too-new", + message: "OpenCodex hub requires remote protocol 2; this client supports protocol 1. Upgrade ocx on this client.", + }, + hubTooOld: { + ok: false, + reason: "hub-too-old", + message: "OpenCodex hub provides remote protocol 1; this client requires at least 2. Upgrade ocx on the hub.", + }, + unknownFeature: [], + malformed: ["invalid", "invalid", "invalid", "invalid", "invalid"], + }, + opencodexFiles: [], + codexFiles: [], + }); + } finally { + child.kill(); + rmSync(homes.root, { recursive: true, force: true }); + } + }); + test("ready --wait exits immediately on terminal failed readiness", async () => { const homes = isolatedHomes("ocx-ready-subprocess-failed-"); const fixturePid = process.pid; From bd0064d60f70551dd9e98ac884a46da72d968a14 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 05:09:48 +0900 Subject: [PATCH 086/172] test(hardening): reject rotation secrets and revoke ids in argv --- tests/client-connect.test.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/client-connect.test.ts b/tests/client-connect.test.ts index 10b66fabfc..08404d85a0 100644 --- a/tests/client-connect.test.ts +++ b/tests/client-connect.test.ts @@ -172,6 +172,17 @@ describe("remote hub client boundary", () => { ])).toBe(2); expect(errors.join(" ")).not.toContain("super-secret-value"); expect(errors.join(" ")).toContain(""); + errors.length = 0; + expect(await handleConnectCommand([ + "rotate", + "--admin-token-stdin", + "--admin-token=rotation-secret-value", + ])).toBe(2); + expect(errors.join(" ")).not.toContain("rotation-secret-value"); + expect(errors.join(" ")).toContain(""); + errors.length = 0; + expect(await handleConnectCommand(["revoke", "client-key-override", "--admin-token-stdin"])).toBe(2); + expect(errors.join(" ")).not.toContain("client-key-override"); } finally { spy.mockRestore(); } From 9088d606c31de621370390569aa8b9778c0a07f0 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 05:10:02 +0900 Subject: [PATCH 087/172] fix(hardening): bound remote session logout request --- gui/src/api.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/gui/src/api.ts b/gui/src/api.ts index 2359cf484b..1c0827f25e 100644 --- a/gui/src/api.ts +++ b/gui/src/api.ts @@ -93,14 +93,20 @@ export function hasApiSession(plane: ApiPlane): boolean { export async function logoutApiSession(plane: ApiPlane): Promise { const state = runtime(plane); if (!state.session.token?.startsWith("ocx_session_")) return false; + const bounded = createBoundedFetch(SESSION_REBOOTSTRAP_TIMEOUT_MS); try { - const response = await window.fetch(`${state.target.baseUrl}/api/session/logout`, { method: "POST" }); + const response = await window.fetch(`${state.target.baseUrl}/api/session/logout`, { + method: "POST", + signal: bounded.signal, + }); if (!response.ok) return false; state.session = blankSession(); state.promptCancelled = false; return true; } catch { return false; + } finally { + bounded.clear(); } } From 54d799cafba55bdacd450c27563565987c7ebcd8 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 05:10:30 +0900 Subject: [PATCH 088/172] test(hardening): keep rotation evidence secret-free --- tests/client-connect.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/client-connect.test.ts b/tests/client-connect.test.ts index 08404d85a0..d4263fd014 100644 --- a/tests/client-connect.test.ts +++ b/tests/client-connect.test.ts @@ -551,7 +551,7 @@ describe("recoverable connected key rotation", () => { console.log(JSON.stringify({ result, state: readClientConnectionState(), - token: fs.readFileSync(path.join(process.env.OPENCODEX_HOME, "service-api-token"), "utf8").trim(), + tokenIsNew: fs.readFileSync(path.join(process.env.OPENCODEX_HOME, "service-api-token"), "utf8").trim() === newKey, backup: fs.existsSync(path.join(process.env.OPENCODEX_HOME, "service-api-token.prev")), commitCalls, credentialZeroed: credential.every(value => value === 0), @@ -567,7 +567,7 @@ describe("recoverable connected key rotation", () => { expect(child.status).toBe(0); const result = JSON.parse(child.stdout.trim().split("\n").at(-1) ?? "{}") as Record; expect(result.commitCalls).toBe(2); - expect(result.token).toBe(newKey); + expect(result.tokenIsNew).toBe(true); expect(result.backup).toBe(false); expect(result.state).toMatchObject({ kind: "connected", value: { apiKeyId: "client-key-1" } }); expect(result.state.value.pendingOperation).toBeUndefined(); From c22792e0565c677f63df537c0e9a03128318a238 Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 05:15:52 +0900 Subject: [PATCH 089/172] fix(hardening): drop a stray import fragment and guard an undefined rotation response --- gui/src/pages/ApiKeys.tsx | 2 +- src/client/connect.ts | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/gui/src/pages/ApiKeys.tsx b/gui/src/pages/ApiKeys.tsx index cf212a5cf7..b4773c1bae 100644 --- a/gui/src/pages/ApiKeys.tsx +++ b/gui/src/pages/ApiKeys.tsx @@ -332,7 +332,7 @@ export default function ApiKeys({ apiBase, active = true }: { apiBase: string; a signal: bounded.signal, }); const data = await readJsonOrThrow(res, t("api.rotation.startFailed")); - if (typeof data.key !== "string" || !data.key || typeof data.rotationId !== "string" || !data.rotationId) return false; + if (!data || typeof data.key !== "string" || !data.key || typeof data.rotationId !== "string" || !data.rotationId) return false; setRotationSecret({ id, key: data.key, rotationId: data.rotationId }); refreshKeys(); return true; diff --git a/src/client/connect.ts b/src/client/connect.ts index ffdfbfc924..cc6792692c 100644 --- a/src/client/connect.ts +++ b/src/client/connect.ts @@ -638,4 +638,3 @@ export async function revokeConnectedClientKey( credential.value.fill(0); } } - probeClientKeyId, From 5da9f17b846322c523422134ef86caf38e91645a Mon Sep 17 00:00:00 2001 From: jun Date: Fri, 28 Aug 2026 05:23:31 +0900 Subject: [PATCH 090/172] fix(hardening): repair phase six full-suite regressions --- src/cli/registry.ts | 7 ++- src/client/hub-relay.ts | 63 +++++++++++++++++++---- src/codex/native-residue.ts | 7 +++ tests/cli-headless-parity.test.ts | 4 ++ tests/loopback-listener-admission.test.ts | 53 ++++++++----------- 5 files changed, 91 insertions(+), 43 deletions(-) diff --git a/src/cli/registry.ts b/src/cli/registry.ts index 575ff552b6..36bbf9a771 100644 --- a/src/cli/registry.ts +++ b/src/cli/registry.ts @@ -94,6 +94,8 @@ export const CLI_COMMANDS: CliCommandEntry[] = [ "Status: ocx connect status [--json]", "Rotate or recover: ocx connect rotate (--pairing-code-stdin | --admin-token-stdin) [--json]", "Revoke while connected: ocx connect revoke --admin-token-stdin [--json]", + "Machine resources: /api/machine/status, /api/machine/shim, /api/machine/clients, /api/machine/sync, /api/machine/disconnect, and the fixed /api/machine/hub-relay namespace.", + "Remote browser self-logout uses /api/session/logout from the GUI; it is distinct from client disconnect and key revocation.", "Credentials are accepted only through stdin; argv and environment credential forms are not supported.", ], }, @@ -264,7 +266,10 @@ export const CLI_COMMANDS: CliCommandEntry[] = [ name: "access", usage: "ocx access ...", summary: "Manage OpenCodex admission API keys and inspect external endpoints.", - details: ["Key rotation start returns the replacement secret once; commit or abort it with the returned rotation id."], + details: [ + "Key rotation start uses POST /api/keys/rotate and returns the replacement secret once.", + "Commit uses POST /api/keys/rotate/commit; abort uses DELETE /api/keys/rotate with the returned rotation id.", + ], }, { name: "api-key", usage: "ocx api-key ...", summary: "Alias of ocx access key." }, { diff --git a/src/client/hub-relay.ts b/src/client/hub-relay.ts index 9bd58030aa..820ffd3845 100644 --- a/src/client/hub-relay.ts +++ b/src/client/hub-relay.ts @@ -160,6 +160,56 @@ function headersWithinLimit(headers: Headers): boolean { return true; } +function boundedRelayResponseStream( + body: ReadableStream, + limit: number, + signal: AbortSignal, +): ReadableStream { + const reader = body.getReader(); + let bytes = 0; + let finished = false; + const finish = () => { + if (finished) return; + finished = true; + signal.removeEventListener("abort", onAbort); + try { reader.releaseLock(); } catch { /* a pending read may still own it */ } + }; + const onAbort = () => { + if (finished) return; + try { void reader.cancel(signal.reason).catch(() => undefined).finally(finish); } + catch { finish(); } + }; + signal.addEventListener("abort", onAbort, { once: true }); + if (signal.aborted) onAbort(); + return new ReadableStream({ + async pull(controller) { + try { + const next = await reader.read(); + if (next.done) { + finish(); + controller.close(); + return; + } + bytes += next.value.byteLength; + if (bytes > limit) { + try { await reader.cancel(new RangeError("hub relay response body too large")); } catch { /* best effort */ } + finish(); + controller.error(new RangeError("hub relay response body too large")); + return; + } + controller.enqueue(next.value); + } catch (error) { + finish(); + controller.error(error); + } + }, + async cancel(reason) { + try { await reader.cancel(reason); } catch { /* best effort */ } + finish(); + }, + }); +} + export async function relayHubManagementRequest( req: Request, suffix: string, @@ -227,16 +277,9 @@ export async function relayHubManagementRequest( try { await upstream.body?.cancel(); } catch { /* best effort */ } return relayError(502, "hub relay response body too large"); } - let streamed = 0; - const responseBody = method === "HEAD" || !upstream.body ? null : upstream.body.pipeThrough(new TransformStream({ - transform(chunk, controller) { - streamed += chunk.byteLength; - if (streamed > HUB_RELAY_RESPONSE_BODY_MAX_BYTES) { - throw new RangeError("hub relay response body too large"); - } - controller.enqueue(chunk); - }, - })); + const responseBody = method === "HEAD" || !upstream.body + ? null + : boundedRelayResponseStream(upstream.body, HUB_RELAY_RESPONSE_BODY_MAX_BYTES, signal); return new Response(responseBody, { status: upstream.status, statusText: upstream.statusText, diff --git a/src/codex/native-residue.ts b/src/codex/native-residue.ts index 138659f816..67be842546 100644 --- a/src/codex/native-residue.ts +++ b/src/codex/native-residue.ts @@ -133,6 +133,13 @@ function resolveRegularFile(path: string): PathResult { function readRegularFile(path: string): ReadResult { const resolved = resolveRegularFile(path); if (resolved.kind !== "path") return resolved; + // Root can read a chmod(000) file on Linux, which made the residue verdict + // depend on who ran the suite. No read bit means the configured surface is + // operationally unreadable to an ordinary Codex process and must remain + // indeterminate even when the inspector itself has elevated privileges. + if (process.platform !== "win32" && (resolved.stat.mode & 0o444) === 0) { + return { kind: "indeterminate", reason: "EACCES: surface has no read permission bits" }; + } try { const content = readFileSync(resolved.path, "utf8"); const after = statSync(resolved.path); diff --git a/tests/cli-headless-parity.test.ts b/tests/cli-headless-parity.test.ts index 2c2b6bd1bf..f9b9762eb6 100644 --- a/tests/cli-headless-parity.test.ts +++ b/tests/cli-headless-parity.test.ts @@ -257,6 +257,10 @@ describe("headless GUI parity CLI", () => { ["/api/grok", "ocx grok"], ["/api/injection", "ocx agent"], ["/api/keys", "ocx access"], + ["/api/keys/rotate", "ocx access key rotate"], + ["/api/keys/rotate/commit", "ocx access key rotate commit"], + ["/api/machine", "ocx connect/status/sync/disconnect"], + ["/api/session/logout", "(none — GUI current-session logout)"], ["/api/logs", "ocx observe"], ["/api/lab", "ocx lab"], ["/api/config", "ocx config"], diff --git a/tests/loopback-listener-admission.test.ts b/tests/loopback-listener-admission.test.ts index e45c8bf19f..ca7907b5da 100644 --- a/tests/loopback-listener-admission.test.ts +++ b/tests/loopback-listener-admission.test.ts @@ -190,41 +190,30 @@ describe("hub management ingress configuration", () => { }); test("enabled ingress requires the hub role", () => { - // Every non-hub role is rejected. Only the two roles that are otherwise complete can be - // asserted on THIS message, though: `client` is refused earlier, by the rule that a - // client role needs a full client connection block. Asserting the ingress wording for it - // would be asserting an order these two independent rules do not promise, so the - // requirement checked for `client` is that it is refused at all. - for (const runtimeRole of [undefined, "standalone"] as const) { - const result = validateConfigCandidate(candidate({ runtimeRole })); + // `client` carries a complete connection block so the ingress rule is what refuses it. + // Without one it is refused earlier, by the rule that a client role needs that block, + // and asserting the ingress wording would be asserting an ordering these two + // independent rules never promise. + for (const runtimeRole of [undefined, "standalone", "client"] as const) { + const result = validateConfigCandidate(candidate({ + runtimeRole, + ...(runtimeRole === "client" ? { + client: { + serverUrl: "https://hub.example.test", + managementUrl: "https://hub.example.test", + managementTransport: "direct", + selectedClients: ["codex"], + tokenEnv: "OPENCODEX_API_AUTH_TOKEN", + apiKeyId: "client-key-1", + tokenFingerprint: "a".repeat(64), + protocolVersion: 1, + connectedAt: "2026-08-28T00:00:00.000Z", + }, + } : {}), + })); expect(result.ok).toBe(false); if (!result.ok) expect(result.error).toContain("requires runtimeRole hub"); } - - const asClient = validateConfigCandidate(candidate({ runtimeRole: "client" })); - expect(asClient.ok).toBe(false); - }); - - test("a complete client connection still cannot enable hub ingress", () => { - // Proves the row above is not hiding a gap: once the client role IS complete, so the - // earlier rule no longer fires, the ingress rule is what refuses it. - const result = validateConfigCandidate(candidate({ - runtimeRole: "client", - client: { - serverUrl: "https://hub.example.test", - managementUrl: "https://hub.example.test", - managementTransport: "direct", - selectedClients: ["codex"], - tokenEnv: "OPENCODEX_API_AUTH_TOKEN", - apiKeyId: "client-key-1", - tokenFingerprint: "a".repeat(64), - protocolVersion: 1, - connectedAt: "2026-08-28T00:00:00.000Z", - catalogSyncedAt: "2026-08-28T00:00:00.000Z", - }, - })); - expect(result.ok).toBe(false); - if (!result.ok) expect(result.error).toContain("requires runtimeRole hub"); }); test("enabled ingress rejects public and unauthenticated-loopback port collisions", () => { From 6a71a415fc98ec7e44f1e77c4c59200808823140 Mon Sep 17 00:00:00 2001 From: jun Date: Tue, 1 Sep 2026 17:49:12 +0900 Subject: [PATCH 091/172] fix(hardening): confirm the abort before rewinding, and never delete an in-flight backup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two rotation defects, plus the client-side reconciliation this phase needs after the earlier contract changes. Abort before restore. The rollback path restored the local token file and then asked the hub to abort the pending rotation. When that abort failed transiently, the process held the old key locally while the hub still had a pending rotation for the new one — two sides disagreeing about which generation is current, surfaced only as "rollback was incomplete". The hub is the authority on which generation is live, so it is asked first and the local file is rewound only after it agrees. On failure both candidates and the pending marker stay on disk, because recovery genuinely cannot tell which generation wins without asking. A concurrent status no longer deletes a live backup. The orphan-cleanup branch fires on "backup present, token present, no pending marker", which is exactly what `ocx connect status` sees if it lands in the window where rotateConnectedClientKey has written .prev but has not yet persisted pendingOperation. It deleted the rollback target the in-flight rotation was relying on. The gate now re-reads persisted state — the caller's snapshot may predate the marker — and declines to clean while a rotation is recorded. Client catalog reconciliation. This phase adds schema validation and the x-opencodex-key-id echo on top of a conditional-fetch path that Phase 1 (D2) removed, so the validator handling is dropped and the additions are kept: no If-None-Match, no ETag in the result, and any 304 is catalog_unexpected_304. That refusal is strictly wider than the two cases it replaces (catalog_304_without_lkg, catalog_etag_mismatch), both of which required a conditional request the client no longer makes. The rotate path also still carried --allow-insecure-http; removed for the same reason as the connect path. tests/loopback-listener-admission.test.ts takes this phase's fix for the ingress role assertion — supplying a complete client connection so the ingress rule is what refuses — over the narrower split made while rebasing p5. --- src/cli/connect.ts | 4 +--- src/client/connect.ts | 16 +++++++++++++--- src/client/state.ts | 28 ++++++++++++++++++++++++++++ tests/client-connect.test.ts | 2 +- tests/remote-catalog.test.ts | 25 ++++++++++++++++--------- 5 files changed, 59 insertions(+), 16 deletions(-) diff --git a/src/cli/connect.ts b/src/cli/connect.ts index 0e9febaf67..8cc3b6dc4b 100644 --- a/src/cli/connect.ts +++ b/src/cli/connect.ts @@ -28,7 +28,7 @@ export const CONNECT_USAGE = `Usage: [--no-sync] ocx connect status [--json] ocx connect rotate (--pairing-code-stdin | --admin-token-stdin) - [--allow-insecure-http] [--json] + [--json] ocx connect revoke --admin-token-stdin [--json]`; export const DISCONNECT_USAGE = `Usage: @@ -128,7 +128,6 @@ async function runRotate(argv: string[], deps: RuntimeApiDeps): Promise { const wantsJson = takeFlag(args, "--json"); const pairing = takeFlag(args, "--pairing-code-stdin"); const admin = takeFlag(args, "--admin-token-stdin"); - const allowInsecureHttp = takeFlag(args, "--allow-insecure-http"); if (Number(pairing) + Number(admin) !== 1) { throw new CliUsageError("choose exactly one of --pairing-code-stdin or --admin-token-stdin", CONNECT_USAGE); } @@ -136,7 +135,6 @@ async function runRotate(argv: string[], deps: RuntimeApiDeps): Promise { const value = new TextEncoder().encode(await readSecretLine(deps, pairing ? "pairing code" : "admin token")); const connection = await rotateConnectedClientKey({ credential: { kind: pairing ? "pairing-grant" : "admin", value }, - allowInsecureHttp, }, { fetchImpl: deps.fetchImpl }); printData({ apiKeyId: connection.apiKeyId, rotation: "committed" }, wantsJson, [ `Rotated connected API key ${connection.apiKeyId}; the previous key is no longer admitted.`, diff --git a/src/client/connect.ts b/src/client/connect.ts index cc6792692c..cc990ae15a 100644 --- a/src/client/connect.ts +++ b/src/client/connect.ts @@ -80,7 +80,6 @@ export interface ClientConnectDeps { export interface RotateClientOptions { credential: OneTimeConnectCredential; - allowInsecureHttp?: boolean; } type CatalogSnapshot = @@ -173,7 +172,7 @@ async function rotationAuthority( connection.managementUrl, localGuiOrigin(), options.credential.value, - { allowInsecureHttp: options.allowInsecureHttp, fetchImpl: deps.fetchImpl }, + { fetchImpl: deps.fetchImpl }, ); return { kind: "gui-session", value: session }; } @@ -306,11 +305,22 @@ export async function rotateConnectedClientKey( if (connection && authority && started) { if (markerPersisted && connection.pendingOperation) { try { - const restored = restoreTokenBackup(connection.pendingOperation.oldKeyBackupPath); + // Abort FIRST, restore second. + // + // The old order restored the local token and then asked the hub to abort. If that + // abort failed transiently the process was left holding the old key locally while + // the hub still had a pending rotation for the new one — two sides disagreeing + // about which generation is current, with the failure surfaced only as "rollback + // was incomplete". Confirming the hub's state first means the local file is only + // rewound once the authority that decides it has agreed. await abortClientKeyRotation(connection.managementUrl, authority, connection.apiKeyId, started.rotationId, { fetchImpl: deps.fetchImpl }); + const restored = restoreTokenBackup(connection.pendingOperation.oldKeyBackupPath); clearRotationState(connection, restored.fingerprint); removeOrphanTokenBackup(); } catch (recoveryError) { + // Both candidates and the pending marker stay on disk. Recovery cannot tell which + // generation is authoritative without the hub, so it preserves the evidence and + // names the command that carries the authority to ask. throw new RotationRecoveryRequiredError( "rotation rollback was incomplete; preserve service-api-token and .prev and rerun ocx connect rotate with transient authority", { cause: recoveryError }, diff --git a/src/client/state.ts b/src/client/state.ts index 73ec5f80c5..4711586d09 100644 --- a/src/client/state.ts +++ b/src/client/state.ts @@ -71,6 +71,17 @@ export function readClientConnectionState(): ClientConnectionState { return { kind: "connected", value: client }; } +/** + * Does the persisted config record a rotation that has not finished? + * + * Read fresh rather than taken from a caller-supplied snapshot: the whole point is to see a + * `pendingOperation` that landed after that snapshot was taken. + */ +function rotationInFlight(): boolean { + const current = readClientConnectionState(); + return current.kind === "connected" && current.value.pendingOperation !== undefined; +} + export function inspectClientRotationRecoveryGate( state: ClientConnectionState = readClientConnectionState(), ): ClientRotationRecoveryGate { @@ -90,6 +101,22 @@ export function inspectClientRotationRecoveryGate( } if (backup.kind === "unsafe") return { kind: "unsafe", reason: backup.reason }; if (backup.kind === "present" && current.kind === "present") { + // Only an ORPHAN backup is cleanable, and this branch cannot always tell an orphan from + // a backup belonging to a rotation that is mid-flight. + // + // `rotateConnectedClientKey` writes the .prev backup BEFORE it persists + // `pendingOperation`. A concurrent `ocx connect status` landing in that window sees + // "backup present, token present, no pending marker" — indistinguishable from a stale + // leftover — and deleted the live rollback target. If the rotation then failed, its + // restore had nothing to restore from. + // + // Re-reading the persisted state closes most of the window: the caller's `state` may + // have been captured before the marker landed, while a fresh read sees it. The + // remaining window is narrow enough that the rotation's own lock is the right owner, + // and deleting nothing is the safe side of it. + if (rotationInFlight()) { + return { kind: "recovery-required", reason: "a key rotation is in flight; leave service-api-token.prev in place" }; + } try { removeOrphanTokenBackup(); return { kind: "orphan-cleaned" }; @@ -101,6 +128,7 @@ export function inspectClientRotationRecoveryGate( } export function commitClientConnection( + state: OcxClientConnectionConfig, ): "committed" | "unchanged" { const outcome = mutatePersistedConfig(config => { diff --git a/tests/client-connect.test.ts b/tests/client-connect.test.ts index d4263fd014..6ddb3cec09 100644 --- a/tests/client-connect.test.ts +++ b/tests/client-connect.test.ts @@ -149,7 +149,7 @@ describe("remote hub client boundary", () => { const fresh = await downloadClientCatalog("https://hub.example.test", "ocx_data_test", { fetchImpl: async (_input, init) => { sentConditional = new Headers(init?.headers).get("if-none-match"); - return new Response('{"models":[]}'); + return new Response('{"models":[]}', { headers: { "Content-Type": "application/json" } }); }, }); expect(sentConditional).toBeNull(); diff --git a/tests/remote-catalog.test.ts b/tests/remote-catalog.test.ts index 8b0629d80c..62ee966d63 100644 --- a/tests/remote-catalog.test.ts +++ b/tests/remote-catalog.test.ts @@ -13,7 +13,10 @@ describe("remote catalog adversarial consumer", () => { const result = await downloadClientCatalog("https://hub.example.test", "ocx_data_test", { fetchImpl: async () => response(body, { ...JSON_HEADERS, "X-OpenCodex-Key-Id": "client-key-1" }), }); - expect(result).toEqual({ kind: "fresh", body, etag: '"catalog-v1"', keyId: "client-key-1" }); + // No etag in the result: /v1/catalog emits no validator (Phase 1, D2), and the fixture's + // ETag header is deliberately left in place to prove the client ignores one even when a + // hub sends it. + expect(result).toEqual({ kind: "fresh", body, keyId: "client-key-1" }); }); test.each([ @@ -56,24 +59,28 @@ describe("remote catalog adversarial consumer", () => { })).rejects.toMatchObject({ code: "body_too_large" }); }); - test("allows the exact byte cap and retries one unconditional request after 304 without LKG", async () => { + test("allows the exact byte cap", async () => { const body = '{"models":[]}'; const exact = await downloadClientCatalog("https://hub.example.test", "ocx_data_test", { maxBytes: new TextEncoder().encode(body).byteLength, fetchImpl: async () => response(body), }); expect(exact.kind).toBe("fresh"); + }); - let calls = 0; - const refreshed = await downloadClientCatalog("https://hub.example.test", "ocx_data_test", { + test("no request carries a conditional header", async () => { + // The retry-after-304 branch this replaces existed to recover from a conditional request + // the client no longer makes. With no validator to send, a 304 is a protocol error + // (asserted below) rather than something to retry past. + let sentConditional: boolean | null = null; + const result = await downloadClientCatalog("https://hub.example.test", "ocx_data_test", { fetchImpl: async (_input, init) => { - calls += 1; - expect(new Headers(init?.headers).has("if-none-match")).toBe(false); - return calls === 1 ? new Response(null, { status: 304 }) : response(body); + sentConditional = new Headers(init?.headers).has("if-none-match"); + return response('{"models":[]}'); }, }); - expect(calls).toBe(2); - expect(refreshed.kind).toBe("fresh"); + expect(sentConditional).toBe(false); + expect(result.kind).toBe("fresh"); }); test("any 304 is a protocol error and non-JSON content is refused", async () => { From 0e443d8d8ae3830665a52af088cc8c434f180b07 Mon Sep 17 00:00:00 2001 From: jun Date: Tue, 1 Sep 2026 18:32:09 +0900 Subject: [PATCH 092/172] fix(hardening): declare the four routes this phase adds tests/management-route-registry.test.ts reconciles the declared registry against source and found four routes this phase serves but never declared: POST, POST /commit and DELETE on /api/keys/rotate, plus POST /api/session/logout. The rotate trio are ordinary management mutations and are declared as such. /api/session/logout carries a session-only exemption with its reason. It ends the CURRENT gui-session and requires that session's own Origin and CSRF, so there is nothing for a CLI verb to act on: the CLI holds an admin token, and this route refuses the admin token precisely so it cannot end a consent session it never established. Giving it a CLI surface would mean inventing one. --- src/server/management/route-registry.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/server/management/route-registry.ts b/src/server/management/route-registry.ts index 3ebce889e2..a8a7a983da 100644 --- a/src/server/management/route-registry.ts +++ b/src/server/management/route-registry.ts @@ -232,6 +232,7 @@ export const MANAGEMENT_ROUTES: readonly ManagementRoute[] = [ { method: "PUT", path: "/api/native-integrations/grok", module: "server/management/native-integration-routes", mutates: true }, // server/management/oauth-account-routes { method: "DELETE", path: "/api/keys", module: "server/management/oauth-account-routes", mutates: true }, + { method: "DELETE", path: "/api/keys/rotate", module: "server/management/oauth-account-routes", mutates: true }, { method: "DELETE", path: "/api/oauth/accounts", module: "server/management/oauth-account-routes", mutates: true }, { method: "DELETE", path: "/api/providers/keys", module: "server/management/oauth-account-routes", mutates: true }, { method: "GET", path: "/api/key-providers", module: "server/management/oauth-account-routes", mutates: false }, @@ -244,6 +245,8 @@ export const MANAGEMENT_ROUTES: readonly ManagementRoute[] = [ { method: "PATCH", path: "/api/keys", module: "server/management/oauth-account-routes", mutates: true }, { method: "PATCH", path: "/api/oauth/accounts/pool", module: "server/management/oauth-account-routes", mutates: true }, { method: "POST", path: "/api/keys", module: "server/management/oauth-account-routes", mutates: true }, + { method: "POST", path: "/api/keys/rotate", module: "server/management/oauth-account-routes", mutates: true }, + { method: "POST", path: "/api/keys/rotate/commit", module: "server/management/oauth-account-routes", mutates: true }, { method: "POST", path: "/api/oauth/accounts/clear-cooldown", module: "server/management/oauth-account-routes", mutates: true }, { method: "POST", path: "/api/oauth/accounts/import", module: "server/management/oauth-account-routes", mutates: true }, { method: "POST", path: "/api/oauth/login", module: "server/management/oauth-account-routes", mutates: true }, @@ -275,6 +278,8 @@ export const MANAGEMENT_ROUTES: readonly ManagementRoute[] = [ { method: "GET", path: "/api/routing-profiles", module: "server/management/routing-profile-routes", mutates: false }, { method: "POST", path: "/api/routing-profiles/dry-run", module: "server/management/routing-profile-routes", mutates: true }, { method: "PUT", path: "/api/routing-profiles", module: "server/management/routing-profile-routes", mutates: true }, + // server/management/session-routes + { method: "POST", path: "/api/session/logout", module: "server/management/session-routes", mutates: true, exempt: { reason: "session-only", why: "Logs out the CURRENT gui-session and requires its own Origin and CSRF. There is nothing for a CLI verb to log out of: the CLI holds an admin token, and the admin token is refused here precisely so it cannot end a consent session it never established." } }, // server/management/sidebar-routes { method: "GET", path: "/api/github/star", module: "server/management/sidebar-routes", mutates: false }, { method: "GET", path: "/api/update/badge", module: "server/management/sidebar-routes", mutates: false }, From dd8c3e6ca576531d7107acd177b41cbcc2f22d04 Mon Sep 17 00:00:00 2001 From: jun Date: Tue, 1 Sep 2026 18:38:48 +0900 Subject: [PATCH 093/172] feat(cli): declare ocx connect rotate as a capability Declaring the three rotate routes in the registry surfaced the other half of that contract: tests/cli-capabilities.test.ts requires every management route to be capability-covered, exempt, or in the dated ratchet. These were none of the three, and unlike POST /api/session/logout they have a real CLI driver, so an exemption would have been a false claim. One capability covers all three routes because they are one operation: start returns the new secret once, commit promotes it, abort unwinds a rotation that could not be confirmed. Splitting them into separate verbs would imply an operator can stop halfway, which is the state the recovery gate exists to prevent. skills/ocx/references/01_management_surface.md is regenerated, since it is derived from this table. --- .../ocx/references/01_management_surface.md | 25 +++++++++++++++++-- src/cli/capabilities.ts | 23 +++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/skills/ocx/references/01_management_surface.md b/skills/ocx/references/01_management_surface.md index eea5f96421..4d94005106 100644 --- a/skills/ocx/references/01_management_surface.md +++ b/skills/ocx/references/01_management_surface.md @@ -337,6 +337,27 @@ JSON mode: `payload`. Each of these writes. Check the flags column before running one unattended. +### `ocx connect rotate` + +Rotate the connected client's data key against the hub, with commit and abort. + +| Method | Route | +|---|---| +| POST | `/api/keys/rotate` | +| POST | `/api/keys/rotate/commit` | +| DELETE | `/api/keys/rotate` | + +| Flag | Value | Meaning | +|---|---|---| +| `--pairing-code-stdin` | boolean | Read a one-time pairing code from stdin as the rotation authority. | +| `--admin-token-stdin` | boolean | Read the hub admin token from stdin as the rotation authority. | +| `--json` | boolean | Emit the rotation result as JSON. | + +JSON mode: `payload`. + +- Requires transient authority on stdin; the credential is never persisted or echoed. +- A rotation left pending by a crash is resumed here — startup and status stop rather than guess which key generation is live. + ### `ocx account pause` Stop routing new requests to one account in the Codex pool. @@ -547,6 +568,6 @@ JSON mode: `payload`. ## Counts -- declared capabilities: 30 -- of those, state-changing: 11 +- declared capabilities: 31 +- of those, state-changing: 12 - head-resolved invocations: 2 diff --git a/src/cli/capabilities.ts b/src/cli/capabilities.ts index e1569bf467..789e626293 100644 --- a/src/cli/capabilities.ts +++ b/src/cli/capabilities.ts @@ -107,6 +107,29 @@ export const CAPABILITIES: readonly Capability[] = [ json: "envelope", details: ["Reads /healthz plus local config; drives no management API route."], }, + { + command: ["connect", "rotate"], + summary: "Rotate the connected client's data key against the hub, with commit and abort.", + // One command drives all three: start returns the new secret once, commit promotes it, + // and abort unwinds a rotation that could not be confirmed. They are not separate verbs + // because a half-rotation is not a state an operator should be able to leave behind. + routes: [ + { method: "POST", path: "/api/keys/rotate" }, + { method: "POST", path: "/api/keys/rotate/commit" }, + { method: "DELETE", path: "/api/keys/rotate" }, + ], + flags: [ + { name: "--pairing-code-stdin", value: "boolean", summary: "Read a one-time pairing code from stdin as the rotation authority." }, + { name: "--admin-token-stdin", value: "boolean", summary: "Read the hub admin token from stdin as the rotation authority." }, + { name: "--json", value: "boolean", summary: "Emit the rotation result as JSON." }, + ], + mutates: true, + json: "payload", + details: [ + "Requires transient authority on stdin; the credential is never persisted or echoed.", + "A rotation left pending by a crash is resumed here — startup and status stop rather than guess which key generation is live.", + ], + }, { command: ["capabilities"], summary: "List the declared CLI capabilities and the management routes they drive.", From efefe3671202080ba6154a44a89242d06e635a16 Mon Sep 17 00:00:00 2001 From: jun Date: Tue, 1 Sep 2026 19:41:14 +0900 Subject: [PATCH 094/172] fix(hardening): keep the remaining two-plane UI off a standalone install Two surfaces this stack added still rendered for a user who never enabled remote hub. Key rotation was offered on every API key. It is a connected-client operation: it swaps the data key this machine uses against its hub, through a commit/abort handshake the hub arbitrates. A standalone install has no hub to rotate against, so the control both advertised remote hub to someone who never turned it on and would have driven a handshake with nothing on the other end. The handlers are passed only for a connected runtime; without them the section does not render. The usage source row read "Source: local usage.jsonl" on standalone. Naming the source answers "which store served these numbers", and that question only exists once there are two. On a standalone install there is exactly one, so the line said nothing the page did not already imply while still making the user read past a sentence about topology. It renders only when connected, alongside the machine/hub scope toggle it belongs with. --- gui/src/pages/ApiKeys.tsx | 18 +++++++++++++----- gui/src/pages/Usage.tsx | 17 ++++++++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/gui/src/pages/ApiKeys.tsx b/gui/src/pages/ApiKeys.tsx index b4773c1bae..d10ff3c33d 100644 --- a/gui/src/pages/ApiKeys.tsx +++ b/gui/src/pages/ApiKeys.tsx @@ -3,6 +3,7 @@ import { Notice } from "../ui"; import { useI18n, LOCALES } from "../i18n/shared"; import { formatProviderDisplayName } from "../provider-icons"; import { readJsonIfOk, readJsonOrThrow } from "../fetch-json"; +import { isConnectedRuntime } from "../api-targets"; import { classifyExternalModel, externalModelId, @@ -530,11 +531,18 @@ export default function ApiKeys({ apiBase, active = true }: { apiBase: string; a onCopyKey={() => { void copyKey(); }} onDelete={handleDelete} onRename={handleRename} - onRotationStart={handleRotationStart} - onRotationCommit={(id, rotationId) => finishRotation(id, rotationId, "commit")} - onRotationAbort={(id, rotationId) => finishRotation(id, rotationId, "abort")} - onCopyRotationSecret={() => { void copyRotationSecret(); }} - onDismissRotationSecret={() => setRotationSecret(null)} + {...(isConnectedRuntime() ? { + // Key rotation is a connected-client operation: it swaps the data key this + // machine uses against its hub, with a commit/abort handshake the hub arbitrates. + // A standalone install has no hub to rotate against, so offering the control + // there advertises remote hub to someone who never enabled it — and the buttons + // would drive a handshake with nothing on the other end. + onRotationStart: handleRotationStart, + onRotationCommit: (id: string, rotationId: string) => finishRotation(id, rotationId, "commit"), + onRotationAbort: (id: string, rotationId: string) => finishRotation(id, rotationId, "abort"), + onCopyRotationSecret: () => { void copyRotationSecret(); }, + onDismissRotationSecret: () => setRotationSecret(null), + } : {})} onModelQueryChange={setModelQuery} onCopyModelId={(modelId) => { void copyModelId(modelId); }} onTestModel={(model, protocol) => { void testModel(model, protocol); }} diff --git a/gui/src/pages/Usage.tsx b/gui/src/pages/Usage.tsx index 769febe5b0..bd7537073b 100644 --- a/gui/src/pages/Usage.tsx +++ b/gui/src/pages/Usage.tsx @@ -813,15 +813,22 @@ export default function Usage({ apiBase, connected = false, apiKeyId }: { apiBas

{t("usage.subtitle")}

-
- {t(connected ? "usage.source.connected" : "usage.source.local")} - {connected && ( + {/* + Only shown when connected. Naming the source is a two-plane concept: it answers + "which store served these numbers", and that question only exists once there are + two. A standalone install has exactly one, so the row says nothing the page does + not already imply — while still being a line about topology that a user who never + enabled remote hub has to read past. + */} + {connected && ( +
+ {t("usage.source.connected")}
- )} -
+
+ )} {state.showSkeleton && !data ? ( From 3275b5a2744e7897e3ad6b9021d2e90d838ce856 Mon Sep 17 00:00:00 2001 From: JUN Date: Tue, 1 Sep 2026 23:41:05 +0900 Subject: [PATCH 095/172] docs(devlog): remote hub restack planning unit (#3149) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * docs(devlog): remote hub restack roadmap — measured conflict surface and blocker reclassification * docs(devlog): fold audit r1 — 10 blockers closed, 33 review threads ledgered, phase ownership corrected * docs(devlog): fix markdown lint in the restack unit (MD018/MD060/MD040/MD004/MD036) * docs(devlog): record wp1 outcome — design rebased, six contract defects closed across two review rounds * docs(devlog): record wp2 outcome — p1 rebased, catalog contract reconciled with the landed #2979 * docs(devlog): record the wp2 commit map and why the fixups are a separate commit * docs(devlog): record wp3 outcome — p2 rebased, plaintext pairing removed, unauthenticated body bounded * docs(devlog): record the wp3 commit map * docs(devlog): record wp4 outcome — p3 rebased, stranded-connection journal defect fixed * docs(devlog): record the wp4 commit map * docs(devlog): record wp5 outcome — p4 rebased, machine plane declared, relay enabled, D1/D2 client side finished * docs(devlog): record the wp5 commit map * docs(devlog): record wp6 outcome — p5 rebased, three of four reported blockers were inherited staleness * docs(devlog): record the wp6 commit map * docs(devlog): record wp7 outcome — p6 rebased, rotation abort ordering and in-flight backup fixed * docs(devlog): record the rebased stack state across all seven phases * docs(devlog): record the exact-head CI repairs and the dev-side macos flake finding * docs(devlog): record the route-registry gap and the dev flake root cause split to #3147 * docs(devlog): close the remote hub restack unit * docs(devlog): note the split-out dev flake PR in the outcome * docs(devlog): audit remote hub exposure, requests, and rollback for a standalone user * docs(devlog): record the exposure, request, and rollback polish * docs(devlog): record the polish commit map and final chain * docs(devlog): describe the lint suppression without writing the directive * docs(devlog): record which axis closed where * docs(devlog): record the per-axis verification commands * docs(devlog): record why the server axis needed no change * docs(devlog): record why rollback was the heaviest axis * docs(devlog): record the post-polish stack state * docs(devlog): plan the stack merge train and the #3147 prerequisite * docs(devlog): correct the merge train plan after the audit refuted the P1 * docs(devlog): record the #3147 seed restore and what it does not fix * docs(devlog): reverse the merge order so the T20 fix lands before its writeup * docs(devlog): correct the fetch-binding claim, replace snapshot greens with merge results, drop out-of-scope files --------- Co-authored-by: jun --- .../061_wp7_outcome.md | 57 -------- .../260901_merge_train_round3/070_outcome.md | 81 ------------ .../260901_remote_hub_restack/000_research.md | 118 +++++++++++++++++ .../002_audit_r1_synthesis.md | 96 ++++++++++++++ .../003_review_thread_ledger.md | 96 ++++++++++++++ .../010_wp1_design_contract.md | 113 ++++++++++++++++ .../011_wp1_outcome.md | 77 +++++++++++ .../020_wp2_p1_protocol_catalog.md | 74 +++++++++++ .../021_wp2_outcome.md | 79 +++++++++++ .../030_wp3_p2_remote_session.md | 67 ++++++++++ .../031_wp3_outcome.md | 79 +++++++++++ .../040_wp4_p3_connect.md | 52 ++++++++ .../041_wp4_outcome.md | 67 ++++++++++ .../050_wp5_p4_two_plane.md | 80 +++++++++++ .../051_wp5_outcome.md | 92 +++++++++++++ .../060_wp6_p5_deploy.md | 61 +++++++++ .../061_wp6_outcome.md | 61 +++++++++ .../070_wp7_p6_hardening.md | 75 +++++++++++ .../071_wp7_outcome.md | 67 ++++++++++ .../072_stack_state.md | 40 ++++++ .../080_wp8_stack_integrity.md | 79 +++++++++++ .../081_wp8_ci_repairs.md | 124 ++++++++++++++++++ .../260901_remote_hub_restack/090_outcome.md | 98 ++++++++++++++ .../100_polish_audit.md | 65 +++++++++ .../101_polish_outcome.md | 106 +++++++++++++++ .../102_axis_ledger.md | 92 +++++++++++++ .../110_merge_train_plan.md | 85 ++++++++++++ .../111_wp1_3147_outcome.md | 58 ++++++++ .../112_wp2_order_reversal.md | 51 +++++++ 29 files changed, 2152 insertions(+), 138 deletions(-) delete mode 100644 devlog/_plan/260901_merge_train_round3/061_wp7_outcome.md delete mode 100644 devlog/_plan/260901_merge_train_round3/070_outcome.md create mode 100644 devlog/_plan/260901_remote_hub_restack/000_research.md create mode 100644 devlog/_plan/260901_remote_hub_restack/002_audit_r1_synthesis.md create mode 100644 devlog/_plan/260901_remote_hub_restack/003_review_thread_ledger.md create mode 100644 devlog/_plan/260901_remote_hub_restack/010_wp1_design_contract.md create mode 100644 devlog/_plan/260901_remote_hub_restack/011_wp1_outcome.md create mode 100644 devlog/_plan/260901_remote_hub_restack/020_wp2_p1_protocol_catalog.md create mode 100644 devlog/_plan/260901_remote_hub_restack/021_wp2_outcome.md create mode 100644 devlog/_plan/260901_remote_hub_restack/030_wp3_p2_remote_session.md create mode 100644 devlog/_plan/260901_remote_hub_restack/031_wp3_outcome.md create mode 100644 devlog/_plan/260901_remote_hub_restack/040_wp4_p3_connect.md create mode 100644 devlog/_plan/260901_remote_hub_restack/041_wp4_outcome.md create mode 100644 devlog/_plan/260901_remote_hub_restack/050_wp5_p4_two_plane.md create mode 100644 devlog/_plan/260901_remote_hub_restack/051_wp5_outcome.md create mode 100644 devlog/_plan/260901_remote_hub_restack/060_wp6_p5_deploy.md create mode 100644 devlog/_plan/260901_remote_hub_restack/061_wp6_outcome.md create mode 100644 devlog/_plan/260901_remote_hub_restack/070_wp7_p6_hardening.md create mode 100644 devlog/_plan/260901_remote_hub_restack/071_wp7_outcome.md create mode 100644 devlog/_plan/260901_remote_hub_restack/072_stack_state.md create mode 100644 devlog/_plan/260901_remote_hub_restack/080_wp8_stack_integrity.md create mode 100644 devlog/_plan/260901_remote_hub_restack/081_wp8_ci_repairs.md create mode 100644 devlog/_plan/260901_remote_hub_restack/090_outcome.md create mode 100644 devlog/_plan/260901_remote_hub_restack/100_polish_audit.md create mode 100644 devlog/_plan/260901_remote_hub_restack/101_polish_outcome.md create mode 100644 devlog/_plan/260901_remote_hub_restack/102_axis_ledger.md create mode 100644 devlog/_plan/260901_remote_hub_restack/110_merge_train_plan.md create mode 100644 devlog/_plan/260901_remote_hub_restack/111_wp1_3147_outcome.md create mode 100644 devlog/_plan/260901_remote_hub_restack/112_wp2_order_reversal.md diff --git a/devlog/_plan/260901_merge_train_round3/061_wp7_outcome.md b/devlog/_plan/260901_merge_train_round3/061_wp7_outcome.md deleted file mode 100644 index 888dd4013b..0000000000 --- a/devlog/_plan/260901_merge_train_round3/061_wp7_outcome.md +++ /dev/null @@ -1,57 +0,0 @@ -# 061 — wp7 outcome: the flake is fixed, and the work-phase numbering is not - -`c8c8dc338` — `test(auth): close the startup-prime window that rotates the credential -mid-fixture (#3139)`. Merged with the roadmap unit in the same PR. - -## Result - -``` -gh pr checks 3139 - macos pass 11m40s - ci pass 4s -``` - -That is the verifier that matters. The same assertion failed on `macos` once for #3133 and -twice for #3137, on heads without the fix. It passed on the **first** run of the fixed head. - -Local: `bun test tests/server-auth.test.ts` -> 91 pass / 0 fail / 618 expect(). - -## Where wp7's work actually happened - -In wp6, not wp7. The FSM's active work-phase was wp6 when the fix was written, and wp6 was -the landing phase blocked by exactly this flake — so its plan absorbed the fix rather than -the two units pretending to be independent. - -Recording that plainly instead of back-dating an attest: wp7 was registered as a -work-phase, its plan doc (`060`) is real and was written under it, and its implementation -rode wp6's cycle. The ledger shows one cycle, which is what happened. - -## What this phase is really a record of - -Three explanations, two wrong, one measured — the table is in `060`. Both wrong ones were -plausible, cited real mechanisms, and would have justified the same one-line fix. That is -what made them dangerous rather than harmless: the fix would have worked, the reasoning -would have been wrong, and the next person to touch this fixture would have inherited the -wrong model. - -What broke the tie was the runtime's own counter: - -``` -$ OPENCODEX_DEBUG_QUOTA=1 bun test ... -t "websocket passthrough refreshes pool auth" -[codex-quota] prime done (reason=startup, pool=1, refreshed=1) -``` - -`refreshed=1` on five runs of the unfixed tree **and** five of the fixed one. Staleness never -varied, so the "cache age crosses the TTL" story was dead — and the surviving explanation is -that the prime always fetches, and what varied was whether it hit the stubbed `fetch` and -pinned clock or the real ones. - -`LOOP-MECHANISM-PROOF-01` asks for activation evidence before adopting a mechanism. Here it -did more than confirm: it killed the hypothesis I had already written into a devlog document -and two PR comments. - -## Residual - -The comments on #3109 and #3112 quote the first wrong explanation. They were left in place — -their operational advice (rerun rather than read a single red as a regression) was correct, -and is now moot because the flake is fixed. `051` carries the pointer to the correction. diff --git a/devlog/_plan/260901_merge_train_round3/070_outcome.md b/devlog/_plan/260901_merge_train_round3/070_outcome.md deleted file mode 100644 index 603b2ac2d7..0000000000 --- a/devlog/_plan/260901_merge_train_round3/070_outcome.md +++ /dev/null @@ -1,81 +0,0 @@ -# 070 — outcome: merge train round 3 - -Terminal outcome: **DONE**. Every item in the round-3 scope reached a terminal state. - -## What landed on `dev` - -| commit | what | origin | -| --- | --- | --- | -| `abcda8e13` | 2026-08-31 non-priority-70 bug triage record | #3114 | -| `0dc01cdaa` | canonical fake-IP addresses on provider PATCH | #3122 via #3133 | -| `b14b741dc` | Windows cold-start budget + code-page scheduler paths | #3104 via #3134 | -| `c8c8dc338` | startup-prime window fix + this roadmap unit | #3139 | -| `58be3c5bb` | probe for a free pid instead of assuming 4242 is dead | #3042 via #3137 | - -## Closed - -Issues #3009, #3064. Pull requests #3104, #3122, #3042, #3077, and a credit comment on the -already-closed #3067. Every closure names the merged commit and what changed from the -original; none is a bare "superseded". - -#3039 was closed by its own author at `2026-09-01T04:17:43Z`, not by this train. The comment -recording which contribution #3104 did **not** carry — the elapsed-time diagnostic, replaced -by the configured budget — landed anyway, so the residual is findable. - -## Rebased, not merged - -#3109 to `b3b502045` (five commits; `926a8d8c` dropped because it had already landed as -#3128) and #3112 to `f3c4e9f75` (four commits). Both `range-diff`-identical. Neither PR's -review blockers were touched — #3112's three credential-path findings still stand and it -still needs a fresh security review. - -## Untouched, deliberately - -#3117 reverses a direction `b46164e78` pinned one day earlier and is a policy decision about -#1690, not a mechanical. #3061 has a substantive rebuttal on record. Both were named OUT at -wp0 and stayed out. - -## What the round is actually evidence of - -**Three wrong explanations, caught by measurement rather than review.** The websocket flake -was explained three times: a 60 s skew margin (wrong — the margin is months), a cache age -crossing a TTL (wrong — `refreshed=1` on every run of both trees), and finally the measured -one. Both wrong versions were plausible, cited real code, and **would have justified the same -fix**. That is what made them worth catching: the fix would have worked and the reasoning -would have been wrong, which is how a fixture acquires folklore. - -`LOOP-MECHANISM-PROOF-01` is why it was caught. Asking for activation evidence before -adopting a mechanism killed a hypothesis already written into a devlog document and two PR -comments. - -**A citation can be worse than silence.** "#3128 fixed that flake" was repeated across three -PRs and a release-train record. It was false — #3128 is an ancestor of every head that failed -afterwards — and its effect was to teach reviewers to dismiss a red. The correction is now on -#3109, #3112, #3104, and in `051` and `060`. - -**A plan audit that returns FAIL is cheap.** Round 1 returned five blockers; three were -folded and changed the train's shape — fork PRs became cherry-pick carries once -`enforce-pr-target.yml:740-746` was read properly, and #3039's closure was withdrawn. Two -were rebutted with evidence. The audit cost one subagent and prevented stranding two -contributor PRs in draft. - -**The test suite commits into the developer's checkout.** -`tests/test-runner.test.ts` calls `commitFixture(cwd, ...)`, which makes a real commit in -whatever worktree runs the suite. It rode along on the first push of two carry branches -(author `OpenCodex Test `, adding `base.txt`) and both had to be -reset and force-pushed. Not fixed here — it is a real trap and belongs to its own unit. - -## Verification summary - -| check | result | -| --- | --- | -| `bun test tests/service.test.ts` (#3134 carry) | 191 pass / 0 fail | -| `bun test tests/management-provider-validation.test.ts tests/destination-policy-resolved.test.ts` (#3133 carry) | 129 pass / 0 fail | -| `bun test tests/responses-state.test.ts tests/doctor.test.ts tests/cli-status-json.test.ts` (#3137 carry) | 214 pass / 0 fail | -| `bun test tests/server-auth.test.ts` (flake fix) | 91 pass / 0 fail | -| exact-head CI on #3133, #3134, #3137, #3139 | fully green before each merge | - -Every merge used `--admin`, because GitHub refuses self-approval and `dev` requires a -reviewed PR. That is a real gap and worth stating rather than burying: what stood in for -review was an independent security lane on #3122, direct maintainer audits on the rest, and -a green exact-head matrix on all four. diff --git a/devlog/_plan/260901_remote_hub_restack/000_research.md b/devlog/_plan/260901_remote_hub_restack/000_research.md new file mode 100644 index 0000000000..faf6298725 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/000_research.md @@ -0,0 +1,118 @@ +# Remote hub 스택 재스택 — 리서치 + +측정 시각 2026-09-01, base `origin/dev@15b0f701e`. + +## 대상 + +7단계 스택. 베이스만 `dev`를 향하고 나머지는 직전 단계의 head 브랜치를 향한다. + +| PR | 브랜치 | base | 커밋 | 파일 | draft | +| --- | --- | --- | --- | --- | --- | +| #2771 | codex/remote-hub-design | dev | 9 | 12 | no | +| #2772 | codex/remote-hub-p1 | design | 5 | 14 | no | +| #2776 | codex/remote-hub-p2 | p1 | 6 | 32 | yes | +| #2777 | codex/remote-hub-p3 | p2 | 11 | 34 | no | +| #2781 | codex/remote-hub-p4 | p3 | 8 | 48 | yes | +| #2786 | codex/remote-hub-p5 | p4 | 8 | 19 | no | +| #2789 | codex/remote-hub-p6 | p5 | 17 | 95 | yes | + +전부 `Ingwannu`의 CHANGES_REQUESTED가 걸려 있다. 포크 지점은 +`8b1b65b8d`이고 그 이후 `dev`는 336커밋 전진하면서 1075개 파일을 건드렸다. + +## 충돌 표면 — 실측 + +시험 워크트리에서 `rebase --onto`를 단계별로 순차 실행해 측정했다. +design 단계는 문서 전용이라 충돌 없이 통과한다(`f17605021`). p1부터 걸린다. + +| 단계 | 단계 파일 | dev와 겹치는 파일 | +| --- | --- | --- | +| design | 12 | 0 | +| p1 | 14 | 12 | +| p2 | 32 | 15 | +| p3 | 34 | 18 | +| p4 | 48 | 17 | +| p5 | 19 | 5 | +| p6 | 95 | 58 | + +p1의 실제 충돌 3파일: `src/server/catalog-download.ts`, +`src/server/index.ts`, `src/server/management/model-routes.ts`. 세 파일 모두 +`f6367639c feat(server): add least-privilege GET /v1/catalog for remote Codex +clients (#2979)`가 마지막으로 건드렸다. 이건 우연이 아니다 — #2979는 이 스택이 +제안한 `/v1/catalog`를 별도 PR로 먼저 랜딩시킨 것이다. 즉 p1의 카탈로그 델타는 +상당 부분 이미 dev에 있다. 재스택할 때 재구현이 아니라 **중복 제거**가 필요하다. + +## 반복 후보 충돌원 + +`dev`가 포크 이후 스택 파일에 남긴 관련 랜딩: + +- `f6367639c` (#2979) — `/v1/catalog` 최소권한 라우트. p1 카탈로그 델타와 직접 중복. +- `f83368dfd` (#3057) — entitlement 삼상태. `src/server/index.ts` 공유. +- `c3da277bc` (#2891) — entitlement roster 클라이언트 버전. `model-routes.ts` 공유. +- i18n 9개 로케일 파일 — p4/p6가 전부 건드리고 dev도 계속 건드린다. 텍스트 추가 충돌이라 + 기계적이지만 건수가 많다. + +## 블로커 재분류 + +리뷰 7건을 원인별로 다시 묶으면 세 종류뿐이다. + +### (1) stale 아티팩트 — 재스택이 곧 해소 + +`tests/release-version-line.test.ts:108` 실패가 #2772/#2777/#2786에 공통으로 +걸려 있다. 정확히 말하면 `:108`은 "뒤처짐" 분기가 아니라 **동일(equality)** +분기다(`:99-108`): 트리 버전이 최고 릴리스 태그와 같은데 이 커밋이 그 태그가 +가리키는 커밋이 아니면 거절한다. 스택의 `package.json`은 `2.34.0`이고 당시 +최고 태그가 `v2.34.0`이었다. + +현재 `origin/dev`는 `2.40.0`, 최고 태그는 `v2.39.0`이므로 지금 리베이스하면 +해소된다. p1은 `package.json`을 수정하지 않으므로 dev 값이 그대로 온다. +**다만 자동 소멸을 가정하지 않는다** — `v2.40.0`이 dev 전진보다 먼저 태깅되면 +재발한다. 리베이스된 head마다 `bun test tests/release-version-line.test.ts`를 +포커스드로 돌려 확인한다. **게이트는 건드리지 않는다.** + +### (2) 구조적 보류 — 자동화는 통과, 사람 리뷰는 별개 + +`#2776`/#2781/#2789는 "중간 스택 head라 최종 승인 불가"라는 보류다. +`AGENTS.md:278-281`과 `.github/workflows/enforce-pr-target.yml:533-557`은 +열린 부모 head를 타깃하는 stacked child에 대해 wrong-base 게이트를 실제로 +면제한다. 저자가 `lidge-jun`(push 권한)이라 기여자 readiness 체크리스트 +(`enforce-pr-target.yml:740-746`)도 적용되지 않는다. + +**그러나 이건 자동화 게이트만 통과시킨다.** 리뷰어의 CHANGES_REQUESTED는 +draft 해제로도 CI 그린으로도 해제되지 않는다. `MAINTAINERS.md:57-61`은 +비저자 메인테이너 승인과 보안 리뷰를 요구하고, Ingwannu가 유일한 비저자 +메인테이너다. 우리가 도달할 수 있는 종료선은 **재리뷰 요청 가능 상태**이며, +승인 자체는 외부 의존이다. + +### (3) 실질 결함 — 코드/문서 수정 필요 + +- #2771 문서 계약 4건 (아래 010). +- `gui/tests/api-auth-memory.test.ts:23` — #2777에 보고됐지만 **소유 단계는 p2**다. +- `tests/cli-headless-parity.test.ts:287` 미선언 `/api/machine/*` 7개 — + #2786에 보고됐지만 **소유 단계는 p4**다. +- `tests/update-stop-first.test.ts:225`, `tests/loopback-listener-admission.test.ts:196`, + privacy 게이트 — p5. +- 미해결 인라인 리뷰 스레드 **33건**(P1 6건 포함). 전수는 `003` 원장 참조. + +### 소유 단계 실측 + +"어느 PR에서 실패가 보고됐는가"와 "어느 단계가 그 결함을 도입했는가"는 다르다. +diff로 측정했다: + +- `/api/machine/` 추가 라인: p1~p3 = 0, **p4 = 49**, p5 = 0, p6 = 2. + 라우트를 도입한 건 p4다. +- `gui/tests/api-auth-memory.test.ts`를 건드리는 단계: **p2**와 p4. p3은 0. + +상류에서 고쳐야 한다. 하류에서 고치면 그 사이 단계들은 자기 head에서 빨간 채로 +남고, 그 위에 다음 단계를 쌓게 된다. + +**단계 초록 불변식:** 각 단계는 자기 head에서 초록이어야 다음 단계를 그 위에 쌓는다. + +(3)만 실제 작업이다. (1)은 재스택의 부산물이고 (2)는 절차 + 외부 의존이다. + +## 제약 + +- 푸시는 `--no-verify` (사용자 지시). `prepush`가 전체 스위트를 부르므로 로컬에서 + 돌 수 없다. +- 로컬 전체 스위트 금지. 판정은 exact-head CI. +- 머지 금지. "머지 가능한 상태까지"가 종료선이다. +- `dev`/`main`/`preview` 직접 푸시 금지. diff --git a/devlog/_plan/260901_remote_hub_restack/002_audit_r1_synthesis.md b/devlog/_plan/260901_remote_hub_restack/002_audit_r1_synthesis.md new file mode 100644 index 0000000000..6aa0c41b5e --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/002_audit_r1_synthesis.md @@ -0,0 +1,96 @@ +# 감사 라운드 1 — 종합 + +감사자: 읽기전용 레인(gpt-5.6-sol high). verdict **FAIL**, 블로커 10건. +아래는 각 건에 대한 판정과 로드맵 수정 내역이다. 수용/반박을 명시한다. + +## A1 (High) — DONE 정의가 머지 가능 상태가 아니다 · 수용 + +080의 종료선은 CI 그린 + 체인 정합 + draft 해제까지였다. 그런데 7개 PR 전부 +`CHANGES_REQUESTED`이고 `MAINTAINERS.md`는 비저자 메인테이너 승인과 보안 +리뷰를 요구한다. CI가 초록이어도 리뷰 상태가 걸려 있으면 머지 버튼은 막힌다. + +수정: 080의 종료선에 "각 PR `reviewDecision`이 `CHANGES_REQUESTED`가 아닐 것"과 +"스레드 33개 해소"를 추가한다. 다만 승인 자체는 우리가 만들 수 없으므로, +우리 종료선은 **재리뷰 요청 가능 상태**까지다. 승인 획득은 외부 의존이며 +그 지점에서 막히면 BLOCKED으로 보고한다. + +## A2 (High) — 미해결 리뷰 스레드 33개 누락 · 수용, 가장 큰 누락 + +로드맵이 리뷰 본문만 읽고 인라인 스레드를 안 봤다. 실측 결과: +`#2771`: 18, #2772: 1, #2776: 2, #2777: 3, #2781: 4, #2786: 2, #2789: 3. +P1 등급이 6건 섞여 있다. 별도 원장 `003_review_thread_ledger.md`로 분리했다. + +## A3 (High) — stacked 면제의 효력 과대 해석 · 수용 + +`AGENTS.md:278-281` + `enforce-pr-target.yml:533-557`의 면제는 실재한다. +저자가 `lidge-jun`(push 권한)이라 기여자 체크리스트도 적용되지 않는다. +그러나 이건 **자동화만** 통과시킨다. 사람 리뷰의 CHANGES_REQUESTED는 그대로다. +030/050/070의 "draft 해제하면 해소" 서술을 "자동화 게이트는 통과, 리뷰는 별도"로 +정정한다. + +## A4 (High) — "dev wins"가 /v1/catalog 계약을 훼손할 수 있다 · 수용 + +감사자가 실제 차이를 열거했다: dev(#2979)는 GET+HEAD, `x-api-key` 허용, +256 MiB 캡, 507. p1은 GET only, `x-api-key` 거부, 32 MiB, 503, 그리고 +`x-opencodex-key-id`와 프로토콜 메타데이터를 **단독으로** 갖는다. + +"dev wins"를 통째로 적용하면 p1 고유 기여가 조용히 사라진다. 020을 병합 매트릭스로 +교체한다. + +## A5 (High) — 블로커 2건이 한 단계씩 늦게 배정됐다 · 수용, 실측 확인 + +직접 측정했다: + +- `/api/machine/` 추가 라인 수: p1~p3 = 0, **p4 = 49**, p5 = 0, p6 = 2. + 즉 라우트를 도입한 건 p4다. 060(wp6/p5)이 아니라 050(wp5/p4)이 고쳐야 한다. +- `gui/tests/api-auth-memory.test.ts`를 건드리는 단계: **p2**와 p4. p3은 0. + 040(wp4/p3)이 아니라 030(wp3/p2)이 고쳐야 한다. + +원칙도 함께 채택한다: **각 단계는 자기 head에서 초록이어야 다음 단계를 그 위에 쌓는다.** +상류에서 고치면 하류 리베이스는 이미 깨진 것을 옮기는 셈이 된다. + +## A6 (High) — 다섯 번째 설계 결함(D5) 누락 · 수용 + +`080_phase6_hardening.md` 8.3 응답 규칙이 "safe content type, cache control, +ETag ... 만 보존"이라고 적어, 릴레이된 세션/부트스트랩/관리 응답에 validator가 +살아남는 것을 허용한다. D2와 같은 결함이 릴레이 경로에 한 번 더 있는 것이다. +D5로 추가하고 구현은 릴레이를 처음 갖는 wp5/p4에 배정한다. + +## A7 (High) — D4 해법이 미명세 · 수용 + +"애매하면 재개"로는 부족하다는 지적이 맞다. `pendingOperation`은 어느 파일이 +새 시크릿을 담았는지 식별하지 못한다. 계약을 구체화한다: probe 이전에 후보 +identity를 비교한다 → 두 후보가 동일하면 교체 이전 상태이므로 절대 commit하지 +않는다 → abort/restore는 확인된 권위가 있을 때만 → abort 불확실 시 증거를 보존한다. + +## A8 (High) — D1의 HTTPS 업그레이드에 신뢰 앵커가 없다 · 수용 + +평문 부트스트랩이 HTTPS 엔드포인트를 "알려주는" 구조는 on-path 공격자가 다른 +유효한 HTTPS origin을 끼워넣을 수 있다. 업그레이드 대상이 의도한 허브인지 +증명할 수단이 없으면 업그레이드는 보안이 아니라 의식이다. + +채택: 비-loopback HTTP를 전면 거부하는 쪽을 기본으로 한다. 사전에 알려진 HTTPS +origin이 있는 경우에 한해 동일 호스트 scheme 업그레이드만 허용하고, 정상 +인증서 검증을 요구하며 리다이렉트에서 권위를 파생하지 않는다. + +## A9 (Medium) — 체인 검증이 부모 계보를 증명하지 못한다 · 수용 + +`origin/dev`가 조상인지만 보면, 부모를 건너뛰고 dev 위로 직접 리베이스된 +자식도 통과한다. 각 엣지를 `git merge-base --is-ancestor origin/ +origin/`로 확인하고 양쪽 OID를 기록한다. + +## A10 (Medium) — release-version 결론이 조건부로만 옳다 · 수용 + +감사자가 정확히 짚었다: `:108`은 "뒤처짐"이 아니라 **동일(equality)** 분기다. +과거 실패는 트리 버전이 `v2.34.0` 태그와 같은데 그 커밋이 아니었기 때문이다. +지금 리베이스하면 해소되지만, `v2.40.0`이 dev 전진보다 먼저 태깅되면 재발한다. +"자동 소멸"을 "리베이스된 head마다 포커스드 체크 필수"로 바꾼다. + +감사자가 `bun test tests/release-version-line.test.ts` 3 pass와 +`bun run privacy:scan` 통과를 현재 트리에서 확인했다. privacy 실패도 상속된 +staleness였다는 뜻이며, 충돌 해소 후 재발하는지만 보면 된다. + +## 반박 없음 + +10건 전부 수용한다. D2(no-store)와 D3(Origin verbatim)는 감사자도 타당하다고 +했고, 바텀업 리베이스 골격도 유지된다. 바뀐 것은 단계 소유권과 종료 게이트다. diff --git a/devlog/_plan/260901_remote_hub_restack/003_review_thread_ledger.md b/devlog/_plan/260901_remote_hub_restack/003_review_thread_ledger.md new file mode 100644 index 0000000000..1185f2ae0f --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/003_review_thread_ledger.md @@ -0,0 +1,96 @@ +# 미해결 리뷰 스레드 원장 — 33건 + +`gh api graphql`로 `isResolved=false` 스레드를 전수 조회했다(2026-09-01). +각 건에 소유 단계를 배정한다. 소유 단계 = 그 결함을 처음 도입한 단계. + +> **공개 시점에 관한 기록.** 이 문서는 스택이 `dev`에 머지된 뒤에 공개됐다. +> 여기 적힌 P1들은 전부 소유 단계에서 수정된 뒤 그 수정과 함께 랜딩했으므로, +> 이 원장은 미수정 결함의 사전 공개가 아니라 이미 공개 diff가 드러낸 것의 +> 사후 기록이다. `AGENTS.md`의 판정 기준("이미 이 약점을 드러내는 공개 diff가 +> 있는가")을 그대로 적용한 결과다. 특히 T20(미인증 바디 버퍼링)의 수정은 +> `b7282858b`로 #2776(`39e5aefb6`)에 실려 들어갔고, `dev`의 +> `src/server/index.ts`에서 `declaredLength` 하드 캡으로 확인된다. 수정 전에 +> 이 문서를 머지했다면 규정 위반이었다 — 실제로 그 순서로 계획했다가 리뷰 +> 지적을 받고 뒤집었다(`112_wp2_order_reversal.md`). + +## #2771 design — 18건 + +대부분 CodeRabbit의 마크다운 린트(MD018/MD022, 테이블 파이프 이스케이프)와 +문서 계약 지적이다. 실질 건만 추린다. + +| # | 위치 | 등급 | 요지 | 배정 | +| --- | --- | --- | --- | --- | +| T1 | 000_research.md:22 | **P1** | 미공개 보안 분석이 추적되는 공개 devlog에 있다 | wp1 | +| T2 | 060_phase4_two_plane.md:348 | P2 | 연결된 GUI에 인증된 models 경로 필요(`/v1/models`가 데이터플레인으로 감) | wp5 | +| T3 | 070_phase5_deploy.md:164 | P2 | 관리 ingress에서 GUI health 엔드포인트 보존 | wp6 | +| T4 | 040_phase2_remote_session.md:19 | Major | D1과 동일 사안 | wp1+wp3 | +| T5 | 030_phase1_protocol_catalog.md:40 | Minor | D2와 동일 사안 | wp1+wp2 | +| T6 | 060_phase4_two_plane.md:305 | Major | D3과 동일 사안 | wp1+wp5 | +| T7 | 060_phase4_two_plane.md:431 | Major | 요약 경로 보안 | wp5 | +| T8 | 080_phase6_hardening.md:323 | Major | D4와 동일 사안(교체 이전 크래시) | wp1+wp7 | +| T9 | 080_phase6_hardening.md:501 | Major | D5 — 릴레이 응답 validator 보존 | wp1+wp5 | +| T10 | 050_phase3_connect.md:308 | Major | 데이터 정합 | wp4 | +| T11 | 070_phase5_deploy.md:300 | Major | 안정성 | wp6 | +| T12-T18 | 010/020/060/070 각처 | Minor | 마크다운 린트 6건 + 미래 날짜 1건 | wp1 | + +**T1이 가장 무겁다.** `AGENTS.md`의 보안 작업 규정과 정면으로 부딪힌다: +미수정 결함의 분석은 추적 디렉터리가 아니라 스크래치에 있어야 한다. +이 스택의 devlog가 미공개 인증/세션 결함 분석을 담고 있다면, 그 부분은 +공개 전에 제거되어야 한다. wp1에서 해당 문단을 판정하고 처리한다. + +## #2772 p1 — 1건 + +| # | 위치 | 등급 | 요지 | 배정 | +| --- | --- | --- | --- | --- | +| T19 | src/server/index.ts:1013 | P2 | 확장된 readiness 응답을 `docs-site/.../cli/lifecycle.md`에 문서화 | wp2 | + +## #2776 p2 — 2건 + +| # | 위치 | 등급 | 요지 | 배정 | +| --- | --- | --- | --- | --- | +| T20 | src/server/index.ts:1684 | **P1** | pairing 바디를 버퍼링 전에 제한. `Content-Length` 없거나 chunked면 `declaredLength`가 0이 되어 미인증 호출자가 무제한 버퍼링 유발 | wp3 | +| T21 | src/types/config.ts:251 | P2 | `hub.managementPublicOrigin`, `remoteGui.allowedTailscaleUsers`, `remoteGui.allowInsecure*` 문서화 | wp3 | + +T20은 미인증 DoS다. D1과 같은 층에 있으므로 wp3에서 함께 닫는다. + +## #2777 p3 — 3건 + +| # | 위치 | 등급 | 요지 | 배정 | +| --- | --- | --- | --- | --- | +| T22 | src/client/connect.ts:229 | **P1** | 연결 전 기존 Codex journal 재소유 필요. `ocx start` 후 정상 상태에서 `injectCodexConfig`가 소유권을 잃는다 | wp4 | +| T23 | src/client/hub-client.ts:85 | P2 | 신뢰할 수 없는 `Content-Length`에 대해 응답 읽기 제한 | wp4 | +| T24 | src/cli/help.ts:35 | P2 | connect/disconnect 워크플로 문서화 | wp4 | + +## #2781 p4 — 4건 + +| # | 위치 | 등급 | 요지 | 배정 | +| --- | --- | --- | --- | --- | +| T25 | src/client/machine-listener.ts:79 | **P1** | `--management-transport relay` 선택 시 `connectClient`가 여전히 throw — 문서화된 옵션이 동작하지 않음 | wp5 | +| T26 | src/client/runtime.ts:27 | **P1** | systemd/WinSW로 뜬 런타임이 disconnect 후 재시작되지 않음(`OCX_SERVICE=1`이 분기를 건너뜀) | wp5 | +| T27 | gui/src/App.tsx:222 | P2 | disconnect 202 성공 시 targets 갱신 누락 | wp5 | +| T28 | gui/src/App.tsx:376 | P2 | pairing 완료 전 공유 페이지 게이팅 | wp5 | + +## #2786 p5 — 2건 + +| # | 위치 | 등급 | 요지 | 배정 | +| --- | --- | --- | --- | --- | +| T29 | src/client/state.ts:46 | P2 | hub role을 disconnected client state에서 배제 | wp6 | +| T30 | src/client/state.ts:85 | P2 | missing-config 부트스트랩 조건화(락 획득 전 반환으로 경쟁) | wp6 | + +## #2789 p6 — 3건 + +| # | 위치 | 등급 | 요지 | 배정 | +| --- | --- | --- | --- | --- | +| T31 | src/client/connect.ts:304 | **P1** | abort 실패 시 토큰 identity 보존. 새 토큰 설치 후 abort가 일시 실패하면 복원이 잘못된 세대를 남긴다 | wp7 | +| T32 | src/client/state.ts:95 | P2 | `ocx connect status`가 진행 중인 로테이션 백업을 삭제 | wp7 | +| T33 | src/client/hub-relay.ts:282 | P2 | 릴레이 오류를 과대 응답 노출 전에 반환 | wp7 | + +T31/T32는 D4와 같은 사안의 서로 다른 얼굴이다. wp7에서 하나의 계약으로 닫는다. + +## 처리 원칙 + +1. P1 6건(T1, T20, T22, T25, T26, T31)은 반드시 코드/문서 수정으로 닫는다. +2. P2/Minor는 수정하거나, 근거를 갖춘 반박을 스레드에 남기고 resolve한다. + 침묵은 허용하지 않는다. +3. 각 스레드는 소유 단계에서 닫고, 그 단계 head가 초록이 된 뒤 다음 단계를 쌓는다. +4. resolve 후 exact head로 재리뷰를 요청한다. diff --git a/devlog/_plan/260901_remote_hub_restack/010_wp1_design_contract.md b/devlog/_plan/260901_remote_hub_restack/010_wp1_design_contract.md new file mode 100644 index 0000000000..9d84c218db --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/010_wp1_design_contract.md @@ -0,0 +1,113 @@ +# wp1 — design(#2771) 재스택 + 문서 트러스트 경계 4건 + +브랜치 `codex/remote-hub-design`, 현재 head `bad162407`. +시험 재스택 결과 충돌 없음(`f17605021`). 문서 12파일 전용. + +## 수정 대상 4건 + +리뷰어가 `bad1624075c75592115ab92f9e49ebcf0c525ce6` exact head에 대해 제기했다. +전부 `devlog/_plan/260827_remote_hub/` 안의 설계 계약 문서다. + +### D1 — 평문 HTTP로 재사용 가능한 credential이 건너간다 + +위치: `040_phase2_remote_session.md:11-25`, `050_phase3_connect.md:324-340`. + +현재 계약은 config 플래그 두 개를 켜면 비-loopback 평문 HTTP 위로 재사용 가능한 +pairing grant가 오가고 재사용 가능한 GUI 세션이 반환되는 것을 허용한다. +운영자 opt-in은 수동적 자격증명 탈취나 on-path 교환을 막지 못한다. + +수정(감사 A8 반영): "HTTPS로 업그레이드"만으로는 부족하다. 평문 부트스트랩이 +HTTPS 엔드포인트를 알려주는 구조는 on-path 공격자가 자기 소유의 유효한 HTTPS +origin을 끼워넣을 수 있다 — 업그레이드 대상이 의도한 허브라는 신뢰 앵커가 없으면 +업그레이드는 보안이 아니라 의식이다. + +계약: + +1. 기본은 **비-loopback 평문 HTTP 전면 거부**다. opt-in 플래그로 뚫을 수 없다. +2. 사전에 알려진 HTTPS origin이 있는 경우에 한해 동일 호스트 scheme 업그레이드만 + 허용한다. 정상 인증서 검증을 요구하고, 리다이렉트에서 권위를 파생하지 않는다. +3. 브라우저 origin은 검증된 출처에서 와야 하며 config에서 파생하지 않는다 + (#2771 미해결 스레드 요구사항). + +문서에 "평문 HTTP에서 전송 가능한 것"의 화이트리스트를 명시하고, 그 목록에 +credential류가 없음을 계약으로 못박는다. + +### D2 — identity-varying 응답에 공유 strong ETag + +위치: `030_phase1_protocol_catalog.md:29-40`. + +인증된 카탈로그 응답이 키마다 내용이 다른데도 공유 strong ETag를 갖고 +`private, no-cache`로 나간다. `x-opencodex-key-id`로 vary한다고 적혀 있지만, +identity로 파티션된 validator/캐시 키가 실제로 테스트되지 않은 상태에서 +저장된 200/304 표현이 키 타입과 키 id를 넘나들 수 있다. + +수정: identity를 실은 응답에 `Cache-Control: no-store`를 쓰고 ETag/304를 +제거한다. 파티션을 유지하려면 파티션이 증명되어야 하는데, 증명 비용보다 +no-store가 싸다. 이 결정을 문서에 근거와 함께 기록한다. + +### D3 — Origin이 한 엔드포인트에만 전달된다 + +위치: `060_phase4_two_plane.md:298-307`. + +브라우저 `Origin`을 정확히 `POST /opencodex-session`에만 전달한다. +그런데 발급된 GUI 세션은 origin에 바인딩되고 관리 API 변경은 Origin/CSRF 검사를 +한다. 릴레이된 `/api/*`의 POST/PUT/PATCH/DELETE는 허브가 필요로 하는 증거를 +잃고 실패한다. 즉 이건 보안 결함이자 기능 결함이다. + +수정: 허용된 모든 세션 인증 mutation에 대해 브라우저 Origin을 verbatim +전달한다. 합성 fallback을 두지 않는다(합성 Origin은 CSRF 검사를 무의미하게 +만든다). 허용 메서드마다 테스트를 건다. + +### D4 — 키 로테이션 크래시 복구가 잘못된 증거를 신뢰한다 + +위치: `080_phase6_hardening.md:318-323`. + +"current와 backup 둘 다 probe 성공"을 current 파일이 새 키를 담고 있다는 +증거로 취급한다. `pendingOperation` 저장 직후 크래시가 나면 두 파일이 모두 +옛 키를 담은 채로 둘 다 probe에 성공할 수 있다. 그러면 복구 로직은 이미 +끝났다고 판단하고 로테이션을 유실한다. + +수정(감사 A7 반영): "애매하면 재개"로는 부족하다. `pendingOperation`은 어느 +파일이 새 시크릿을 담았는지 식별하지 못하고, 그 시크릿은 마커 저장 이후에도 +유실될 수 있다. 계약을 다음 순서로 못박는다: + +1. probe **이전에** 두 후보의 identity를 비교한다. +2. 두 후보가 동일하면 교체 이전 상태다 — 절대 commit하지 않는다. +3. abort/restore는 확인된 권위가 있을 때만 수행한다. +4. abort가 불확실하게 실패하면 증거를 보존한다(조용한 복원 금지). + +회귀 테스트 3종: 동일-구세대 후보, abort 실패, 진행 중 백업을 지우는 동시 +status 실행. 뒤 두 개는 #2789의 열린 스레드(T31/T32)와 같은 사안이다. + +### D5 — 릴레이 응답이 validator를 보존한다 + +위치: `080_phase6_hardening.md:496-503` (8.3 응답 규칙). + +응답 규칙이 "safe content type, cache control, ETag ... 만 보존"이라고 적어, +릴레이된 세션/부트스트랩/관리 응답에 validator가 살아남는 것을 허용한다. +D2와 같은 결함이 릴레이 경로에 한 번 더 있는 셈이다. + +수정: 릴레이된 세션/부트스트랩/관리 응답은 기본이 `Cache-Control: no-store`이고 +validator(ETag/Last-Modified)를 제거한다. 구현은 릴레이를 처음 갖는 wp5/p4에 +배정하고, p6에서 적대적 커버리지를 추가한다. + +## 작업 순서 + +1. `origin/dev` 위로 `rebase --onto` (충돌 없음 확인됨). +2. D1~D4를 설계 문서에 반영. 각 수정은 "무엇이 틀렸는지 → 새 계약" 형태로 + 기존 문단을 대체한다. 리뷰 코멘트를 인용만 하고 계약을 안 바꾸면 무의미하다. +3. `--no-verify` 푸시. +4. PR #2771 설명 갱신 — 4건 각각 어디서 어떻게 해소됐는지 파일:줄로 지목. + +## 검증 + +- `git range-diff origin/dev..bad162407 origin/dev..` 로 9커밋 보존 확인 + (D1~D4 수정 커밋은 추가분). +- 문서 전용이므로 로컬 테스트 대상 없음. exact-head CI 그린으로 판정. +- D1~D5의 구현 정합은 각각 wp3(D1), wp2(D2), wp5(D3), wp7(D4), wp5(D5)에서 + 처리한다. 이 단계는 계약만 고친다. +- `003` 원장의 T1(미공개 보안 분석이 공개 devlog에 있음, P1)을 함께 처리한다. + `AGENTS.md` 보안 규정상 미수정 결함의 분석은 추적 디렉터리에 있으면 안 된다. + 해당 문단을 판정해 제거하거나, 이미 공개 diff로 드러난 사안임을 확인한다. +- #2771의 마크다운 린트 6건(MD018/MD022/테이블 파이프)과 미래 날짜 1건도 + 이 단계에서 닫는다. diff --git a/devlog/_plan/260901_remote_hub_restack/011_wp1_outcome.md b/devlog/_plan/260901_remote_hub_restack/011_wp1_outcome.md new file mode 100644 index 0000000000..db0b600b78 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/011_wp1_outcome.md @@ -0,0 +1,77 @@ +# wp1 결과 — design(#2771) 재스택 + 계약 결함 6건 + +브랜치 `codex/remote-hub-design`: `bad162407` → `36992baa9`. + +## 재스택 + +`origin/dev@15b0f701e` 위로 `rebase --onto`. 충돌 0건. +`range-diff`로 원본 9커밋이 전부 `=`로 보존됨을 확인했다. 탈락한 5커밋은 +이미 dev에 랜딩된 무관 커밋이라 자연 소멸한 것이다. authorship 보존, 오염 커밋 +(`opencodex.invalid`) 0건, `devlog/` 외 파일 미변경. + +## 커밋 3개 + +| 커밋 | 내용 | +| --- | --- | +| `dfae1da61` | D1~D5 + T1 1차 수정 | +| `45951cebd` | D1 잔여 제거(010/050/070) | +| `36992baa9` | 리뷰 지적 2~5번 수정 | + +## 리뷰 라운드 + +읽기전용 적대적 리뷰어(gpt-5.6-sol high)가 `dfae1da61`을 심사해 **FAIL**, +지적 5건 + CLOSED 2건을 냈다. 판정과 처리: + +**1번 D1 미완 (HIGH) — 리뷰 시점 이전에 이미 수정됨.** +리뷰어가 `dfae1da61` 블롭을 봤는데, 그 시점 이후 `45951cebd`로 닫혀 있었다. +지적 자체는 정확했다: 040에서만 제거하고 010/050/070에 계약이 살아 있었다. +특히 050의 클라이언트 `--allow-insecure-http`는 서버가 거부하는 경로를 +클라이언트가 제공하는 자기모순이었다. + +**2번 D2가 하위 단계에 미반영 (HIGH) — 수용.** +030만 고치고 050/080의 클라이언트를 안 고쳤다. 서버는 validator를 안 주는데 +클라이언트는 ETag를 저장하고 `If-None-Match`를 보내고 304를 처리하도록 +명세돼 있었다. Phase 3을 따라 구현하면 Phase 1이 지운 것을 그대로 되살린다. +클라이언트를 무조건 페치로 바꾸고, 요청하지도 않은 304는 캐시 히트가 아니라 +프로토콜 오류로 규정했다. + +**3번 D3 과잉 수정 (HIGH) — 수용.** +"never omit it"이 040 §5.2의 안전 읽기 허용(Origin 없는 GET/HEAD)과 +충돌했다. 규칙을 둘로 분리했다: *전달*은 브라우저가 보낸 값이 있으면 항상 +원문 그대로, *요구*는 허브 predicate가 결정. 릴레이는 값을 지어내야 하는 +경우에만 거절한다. 소유 테스트 행도 메서드별 + Origin 부재 양 갈래로 확장했다. + +**4번 D4 실행 불가 (HIGH) — 수용, 가장 중요한 지적.** +두 문제가 있었다. 첫째, 새 규칙을 쓰면서 세 문단 위의 옛 "both accepted → +commit" 규칙과 활성화 매트릭스 행을 안 지워서 문서가 자기모순이었다. +둘째, "처음부터 재개"가 불가능하다 — 시크릿은 한 번만 반환되고, 재시작은 +`already-pending`으로 막히며, startup/status에는 관리 권한이 없다. +옛 텍스트를 교체하고, 복구는 증거를 보존한 채 **정지**하며 재개는 전이 권한을 +가진 다음 `ocx connect rotate`가 `rotationId` abort를 확인한 뒤 수행하도록 +상태 기계를 다시 썼다. + +**5번 부기 오류 (MEDIUM) — 수용.** +중복 `P2-A11`과, 검증 섹션 뒤 표 바깥에 붙은 `P6-A20..A22`. 각각 `P2-A21` +재번호와 활성화 매트릭스 편입으로 처리하고 낡은 행을 교체했다. + +**6번 D5 — CLOSED.** 리뷰어가 계약이 실제로 닫혔다고 확인. + +**7번 T1 — CLOSED.** 리프레이밍이 타당하다고 확인했다. 근거를 실물로 검증: +`src/server/management-auth.ts:245-252`가 원격 세션 발급을 거부하고, +`sidebar-routes.ts:41-49`/`codex-prompt-routes.ts:299-305`가 `gui-session`을 +요구하며, 공개 문서 `web-dashboard.md:24-35`가 이미 이 경계를 설명한다. +즉 이미 공개된 fail-closed 제약이지 미공개 취약점이 아니다. 리뷰어는 유닛 +나머지에서도 미수정 취약점 사전공개 텍스트를 찾지 못했다. + +## 검증 + +- 중복 acceptance ID 0건(P4-A4b/A4c는 접미사가 붙은 별개 ID). +- D1 활성 참조 0건 — 남은 언급은 전부 "제거했다" 서술. +- markdownlint 회귀 0건(8개 문서 before/after 동일). +- 푸시 후 `origin/dev`가 `origin/codex/remote-hub-design`의 조상임을 확인. + +## 남은 것 + +#2771의 미해결 스레드 18건 중 마크다운 린트 6건과 T2/T3/T7/T10/T11은 +아직 열려 있다. D1~D5에 해당하는 T4/T5/T6/T8/T9와 T1은 이 커밋들로 닫혔다. +PR 설명 갱신과 스레드 resolve는 wp8에서 일괄 처리한다. diff --git a/devlog/_plan/260901_remote_hub_restack/020_wp2_p1_protocol_catalog.md b/devlog/_plan/260901_remote_hub_restack/020_wp2_p1_protocol_catalog.md new file mode 100644 index 0000000000..4ac97869f1 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/020_wp2_p1_protocol_catalog.md @@ -0,0 +1,74 @@ +# wp2 — p1(#2772) 재스택 + 카탈로그 중복 제거 + +브랜치 `codex/remote-hub-p1`, head `c10ef21a9`, 5커밋 / 14파일. + +## 실측 충돌 + +`rebase --onto trial-remote-hub-design origin/codex/remote-hub-design` 에서 +`4fa130bf6 feat(remote): serve authenticated catalog snapshots` 가 3파일에서 멈춘다. + +- `src/server/catalog-download.ts` +- `src/server/index.ts` +- `src/server/management/model-routes.ts` + +## 원인 — 재구현이 아니라 선행 랜딩 + +세 파일의 dev 쪽 마지막 변경은 전부 `f6367639c feat(server): add +least-privilege GET /v1/catalog for remote Codex clients (#2979)` 이다. +`#2979`는 이 스택이 설계한 `/v1/catalog`를 별도 PR로 먼저 랜딩시킨 것이다. + +"dev wins"를 통째로 적용하면 안 된다(감사 A4). 두 구현은 의미가 갈린다: + +| 항목 | dev (#2979) | p1 | 채택 | +| --- | --- | --- | --- | +| 메서드 | GET + HEAD | GET only | **dev** — HEAD 제거는 랜딩된 기능 회귀 | +| `x-api-key` | 허용 | 거부 | **판단 필요** — 아래 | +| 크기 캡 | 라우트 한정 256 MiB | 32 MiB | **dev** — 랜딩된 지원 크기를 줄이지 않는다 | +| 초과 시 | 507 | 503 | **dev** | +| `x-opencodex-key-id` | 없음 | 있음 | **p1** — 고유 기여 | +| 프로토콜 메타데이터 | 없음 | 있음 | **p1** — 고유 기여 | +| 캐시 헤더 | — | ETag + private,no-cache | **둘 다 아님** — D2에 따라 `no-store`, validator 제거 | + +근거: dev 쪽 구현은 `src/server/index.ts:1073-1120`과 +`src/server/catalog-download.ts:18-29`에 있다. + +`x-api-key` 허용/거부는 의도적으로 판정한다. p1이 거부하는 것은 최소권한 +의도로 보이지만, dev가 이미 허용한 상태로 랜딩됐으므로 좁히는 것은 동작 회귀다. +좁히려면 별도 근거와 함께 PR 설명에 명시하고 테스트를 함께 바꾼다. 기본은 dev 유지. + +해소 후 반드시 확인할 것: `/v1/catalog`의 최소권한 admission이 p1 델타에 의해 +느슨해지지 않았는가. `tests/api-catalog-route.test.ts`가 이 계약을 들고 있다. + +## D2 구현 정합 + +010의 D2(identity-varying 응답의 ETag/304 제거)가 이 단계 코드에 걸린다. +`67e818da1 test(remote): cover phase one protocol and catalog contract` 와 +`c10ef21a9 fix(remote): type catalog bytes over ArrayBuffer and scope the +key-id warn assertion` 이 해당 경로를 다룬다. 재스택 후 카탈로그 응답 헤더를 +`no-store` + ETag 없음으로 맞추고 테스트를 그에 맞게 고친다. + +## release-version-line + +`:108`은 equality 분기다(000 참조). 리베이스로 해소되지만 자동 소멸을 가정하지 +않는다 — 이 단계 head에서 `bun test tests/release-version-line.test.ts`를 +명시적으로 돌려 확인한다. 테스트를 손대지 않는다. + +## 미해결 스레드 + +T19 (#2772, P2): 확장된 readiness 응답을 `docs-site/src/content/docs/reference/cli/lifecycle.md`에 +문서화. `src/server/index.ts:1013`이 대상. + +## privacy:scan + +p1에서 privacy 게이트가 실패한다고 기록돼 있다. 재스택 후 실제로 재현하는지 +먼저 확인한다(`bun run privacy:scan`은 전체 스위트가 아니므로 허용 범위). +재현되면 로그/직렬화 경로에서 자격증명이나 계정 식별자가 새는 지점을 찾아 +**게이트가 아니라 코드**를 고친다. + +## 검증 + +- `git range-diff` 5커밋 보존. +- `bun test tests/api-catalog-route.test.ts tests/server-auth.test.ts tests/config.test.ts` + (변경 파일 직결 포커스드). +- `bun run privacy:scan`. +- 최종 판정은 exact-head CI. diff --git a/devlog/_plan/260901_remote_hub_restack/021_wp2_outcome.md b/devlog/_plan/260901_remote_hub_restack/021_wp2_outcome.md new file mode 100644 index 0000000000..db9fc20e47 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/021_wp2_outcome.md @@ -0,0 +1,79 @@ +# wp2 결과 — p1(#2772) 재스택 + 카탈로그 계약 재조정 + +브랜치 `codex/remote-hub-p1`: `c10ef21a9` → `07d7f1006`. +베이스는 재스택된 `codex/remote-hub-design@36992baa9`. + +## 충돌과 해소 + +예측대로 `4fa130bf6`에서 3파일이 충돌했고, 이후 두 커밋에서도 테스트 파일이 +걸렸다. 원인은 020이 적은 그대로다: #2979(`f6367639c`)가 이 단계가 설계한 +`/v1/catalog`를 먼저 랜딩시켰다. + +**다만 020의 "p1 고유 기여" 판정은 절반이 틀렸다.** 초기 조사에서 dev의 +`src/server/index.ts`에 `withRemoteCatalogKeyId`와 프로토콜 메타데이터가 +보이길래 "dev가 이미 갖고 있다"고 적었는데, 그건 이전 리베이스 시도가 남긴 +작업 트리 잔재였다. `git show origin/dev:src/server/index.ts`로 확인하니 +dev에는 그 헬퍼가 **아예 없었다**. p1의 key-id 에코는 실재하는 고유 기여였고, +그걸 버렸다면 다중 키 운영자의 카탈로그 읽기 귀속이 사라졌을 것이다. + +교훈: 작업 트리의 grep은 브랜치의 내용이 아니다. 리베이스 중에는 +`git show :`로 확인해야 한다. + +## 최종 병합 결정 + +| 항목 | dev(#2979) | p1 | 채택 | 근거 | +| --- | --- | --- | --- | --- | +| 메서드 | GET+HEAD | GET only | dev | 랜딩된 기능 회귀 금지 | +| 크기 캡 | 256 MiB / 507 | 32 MiB / 503 | dev | 2000모델≈92MB, 32MiB는 유효 입력 거부 | +| malformed | 404 | 500 | dev | "파일 손상"과 "카탈로그 없음"을 구별시키지 않음 | +| `x-api-key` | 허용 | 거부 | dev | 상류로 자격증명 전달 없음 → 추가 권한 없음. 거부하면 유효한 Anthropic-SDK 클라이언트가 401 | +| `x-opencodex-key-id` | 없음(죽은 코드) | 있음 | **p1** | 실재하는 고유 기여, 라우트에 배선 | +| 캐시 헤더 | private,no-cache + ETag | ETag + no-cache | **둘 다 아님** | D2: `no-store`, validator 없음 | + +`AUTH_MATRIX`에 `/v1/catalog` 행이 둘 생겼고 `xApiKey`가 정반대였다. +행렬이 자기모순이라 라이브 서버 검증이 어느 행을 먼저 읽느냐로 갈렸다. +p1 행을 제거했다. + +## 테스트 조정 + +p1이 자기 구현에 맞춰 쓴 단언들을 dev+D2 계약으로 다시 썼다. 지운 게 아니라 +뒤집었고, 각각 왜 반대가 됐는지 주석으로 남겼다. + +- `api-catalog-route`: malformed→404, `no-store`/ETag 없음, HEAD 동일, + 조건부 요청이 200을 받는다(관리 라우트 ETag를 흉내내도). +- `server-auth`: 304 테스트를 "어떤 조건부 요청도 304를 끌어낼 수 없다"로 반전. + 사라진 `catalogDataPlaneResponse` API를 쓰던 캡 테스트는 제거(dev의 + `api-catalog-route`가 같은 경계를 이미 커버한다). ETag 스펠링을 dev의 hex로. +- `api-key-attribution`: dev 쪽 주석 있는 버전 채택. + +## 검증 + +- `bun run typecheck` 통과. +- 포커스드 7파일 **412 pass / 0 fail** + (server-auth, api-catalog-route, api-key-attribution, config, + proxy-liveness, release-version-line, server-live). +- `bun run privacy:scan` 통과 — 리뷰가 보고한 privacy 실패는 상속된 + staleness였고 재스택으로 소멸했다. +- `release-version-line` 통과 — 020의 예측대로 `package.json`이 dev의 + 2.40.0으로 해소됐다. +- 부수 확인: `server-auth`의 websocket refresh flake도 함께 사라졌다. + +## 남은 것 + +T19(확장된 readiness 응답을 `docs-site/.../cli/lifecycle.md`에 문서화)는 +아직 열려 있다. wp8에서 처리한다. + +## 커밋 + +| 커밋 | 내용 | +| --- | --- | +| `733d0e62b` | feat(remote): add protocol metadata and runtime role (원본 보존) | +| `f65484844` | feat(remote): serve authenticated catalog snapshots (원본 보존) | +| `b24a15a22` | fix(remote): derive management origin from request host (원본 보존) | +| `0d81baffa` | test(remote): cover phase one protocol and catalog contract (원본 보존) | +| `58ab13df0` | fix(remote): type catalog bytes over ArrayBuffer (원본 보존) | +| `07d7f1006` | fix(remote): reconcile the phase-one catalog contract with the landed /v1/catalog (신규) | + +원본 5커밋은 authorship과 메시지가 보존됐다. 충돌 해소로 내용이 바뀐 부분은 +커밋을 다시 쓰지 않고 마지막에 조정 커밋 하나로 모았다 — 원저자의 커밋을 +내가 편집한 것처럼 보이게 만들지 않기 위해서다. diff --git a/devlog/_plan/260901_remote_hub_restack/030_wp3_p2_remote_session.md b/devlog/_plan/260901_remote_hub_restack/030_wp3_p2_remote_session.md new file mode 100644 index 0000000000..cbdfe763fd --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/030_wp3_p2_remote_session.md @@ -0,0 +1,67 @@ +# wp3 — p2(#2776) 재스택 + D1 HTTPS 업그레이드 정합 + +브랜치 `codex/remote-hub-p2`, head `7099760a5`, 6커밋 / 32파일, draft. +dev와 겹치는 파일 15개. + +## 겹침 + +`src/cli/dispatch.ts`, `src/cli/help.ts`, `src/cli/registry.ts`, +`src/config.ts`, `src/server/auth-cors.ts`, `src/server/gui-static.ts`, +`src/server/index.ts`, `src/server/proxy-liveness.ts`, `src/types.ts`, +`src/types/config.ts` + 테스트 5. + +CLI 레지스트리와 config 타입은 dev가 계속 확장한 곳이라 추가-추가 충돌이 +예상된다. 원칙: dev의 항목을 지우지 않고 스택 항목을 병렬로 추가한다. + +## D1 — 평문 HTTP credential 금지 구현 + +010의 D1이 이 단계에서 코드가 된다. 관련 커밋: + +- `1e3f7d2b7 feat(remote-gui): add remote session issuance and pairing` +- `6c8dd333e fix(remote-gui): enforce exact bootstrap destination` +- `7099760a5 fix(remote-gui): preserve renewal and mutation origin checks` + +요구: 비-loopback 평문 HTTP에서는 pairing grant도 GUI 세션도 발급되지 않는다. +opt-in 플래그로 이 금지를 뚫을 수 없어야 한다. HTTP는 "여기 HTTPS 엔드포인트가 +있다"만 알려주는 credential-free 부트스트랩으로 남긴다. + +테스트: 평문 HTTP 비-loopback 요청에 대해 grant 발급이 거절되는 네거티브, +그리고 loopback은 기존대로 허용되는 포지티브. `gui/tests/connect-pairing.test.ts`와 +서버 쪽 remote-session 테스트에 건다. + +## 이 단계가 소유하는 블로커 — 감사로 재배정됨 + +### gui/tests/api-auth-memory.test.ts:23 + +`#2777`(p3)에 보고됐지만 실측 결과 이 파일을 처음 건드리는 단계는 **p2**다 +(p3은 0건). 여기서 고친다. 재스택 후 실패를 재현해 어느 쪽 계약이 맞는지 +판정한다 — dev가 맞으면 스택 코드를 맞추고, 스택이 의도적으로 바꾼 것이면 +근거를 PR 설명에 적고 테스트를 함께 갱신한다. 테스트만 지우는 해소는 금지. + +### T20 (P1) — pairing 바디 무제한 버퍼링 + +`src/server/index.ts:1684`. 미인증 호출자가 `Content-Length`를 생략하거나 +chunked를 쓰면 `declaredLength`가 0이 되어 바디가 제한 없이 버퍼링된다. +미인증 DoS다. 선언 길이가 없을 때도 하드 캡을 적용하고 초과 시 거절한다. + +### T21 (P2) — 설정 문서화 + +`hub.managementPublicOrigin`, `remoteGui.allowedTailscaleUsers`, +`remoteGui.allowInsecure*`가 사용자 노출 설정인데 문서가 없다. +D1이 `allowInsecure*`의 의미를 바꾸므로 문서도 새 계약으로 쓴다. + +## draft 해제 + +`#2776`은 draft이고 base가 `codex/remote-hub-p1`이다. 이 base는 정당하다 — +`AGENTS.md:278-281`과 `enforce-pr-target.yml:533-557`이 열린 부모 head를 +타깃하는 자식의 wrong-base 게이트를 면제한다. 재스택 + CI 그린 후 draft를 +해제한다. 다만 draft 해제는 자동화 게이트만 여는 것이고 리뷰어의 +CHANGES_REQUESTED는 그대로다(감사 A3). + +## 검증 + +- `git range-diff` 6커밋 보존. +- `bun test tests/server-auth.test.ts tests/config.test.ts tests/cli-registry.test.ts tests/release-version-line.test.ts` +- `cd gui && bun test tests/connect-pairing.test.ts tests/api-auth-memory.test.ts`. +- **이 단계 head가 초록이어야 p3을 그 위에 쌓는다.** +- exact-head CI. diff --git a/devlog/_plan/260901_remote_hub_restack/031_wp3_outcome.md b/devlog/_plan/260901_remote_hub_restack/031_wp3_outcome.md new file mode 100644 index 0000000000..47dfc97b71 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/031_wp3_outcome.md @@ -0,0 +1,79 @@ +# wp3 결과 — p2(#2776) 재스택 + D1 구현 + T20 + +브랜치 `codex/remote-hub-p2`: `7099760a5` → `b7282858b`. +베이스는 재스택된 `codex/remote-hub-p1@07d7f1006`. + +## 충돌 + +`tests/cli-dispatch.test.ts`와 `tests/cli-registry.test.ts`에서 순수 +추가-추가 충돌. dev와 이 단계가 같은 위치에 서로 다른 테스트를 넣었다. + +처음에 정규식으로 충돌 마커만 지우는 방식을 썼는데, 그게 닫는 중괄호를 +삼켜서 두 파일이 파싱 불가가 됐다(dispatch 3개, registry 1개 손실). +테스트가 "Unexpected end of file"로 죽고 나서야 드러났다. + +고친 방법: dev 원본 파일에서 시작해 이 단계가 **추가한 블록만** 얹었다. +마커 텍스트를 편집하는 대신 양쪽의 의도를 재구성하는 쪽이 안전하다. +두 테스트 파일 42건 전부 통과한다. + +## D1 구현 — 평문 pairing 제거 + +설계(wp1)에서 계약을 고쳤지만 코드는 그대로였다. `src/server/gui-session.ts`의 +`consumeGuiPairingGrant`가 `remoteGui.allowInsecureHttp === true`이면 +비-loopback HTTP로 `insecure-http-pairing` 세션을 발급하고 있었다. + +제거했다. 그리고 **순서를 바꿨다.** 기존 코드는 grant를 찾아 검증한 뒤에 +scheme을 판정해서, 거절된 교환이 이미 단회용 코드를 소비했다. TLS 종단을 +걷어낸 공격자가 운영자가 출력하는 코드를 전부 태울 수 있다는 뜻이다. +이제 grant를 읽기 전에 거절하며, 회귀 테스트가 "같은 미사용 grant가 HTTPS로는 +여전히 통한다"로 이를 증명한다. + +`allowInsecureHttp` 키는 스키마에 남기고 retired로 표시했다. 설정 스키마가 +`.strict()`라 키를 지우면 기존 설정 파일 전체가 로드 실패한다. 받아들이되 +무시하는 쪽이 피해가 작다. + +## T20 (P1) — 미인증 바디 무제한 버퍼링 + +`POST /opencodex-session`은 자격증명 없이 도달 가능한데, 바디 제한이 +`Content-Length`에 의존했다. 헤더를 생략하면 `Number(null ?? "0")`이 0이고, +chunked를 쓰면 헤더 자체가 없다. 둘 다 사전 검사를 통과해 `req.text()`에 +도달했고, 그건 끝까지 버퍼링한다. 사후 검사는 이미 프로세스가 붙들도록 +강요당한 문자열을 잰 것이다. + +읽는 중에 limit+1에서 멈추고 바디를 cancel하도록 바꿨다. 회귀 테스트는 +4 KiB 제한에 512 KiB를 `Content-Length` 없이 스트리밍하고, 서버가 제공된 +청크보다 적게 당겼음을 단언한다. + +**레드-퍼스트 확인:** 수정 전 코드로 되돌려 이 테스트가 실제로 실패하는 것을 +확인한 뒤 다시 적용했다. 경계값(정확히 4096바이트)이 여전히 통과하는 것도 +함께 고정했다. + +## 검증 + +- `bun run typecheck` 통과. +- 포커스드 7파일 **348 pass / 0 fail** + (server-auth, server-management-auth, config, cli-dispatch, cli-registry, + gui-pair-capability, gui-pair-client). +- `server-management-auth` 35건 전부 통과 — D1 계약 반전 테스트 포함. + +## 남은 것 + +T21(`hub.managementPublicOrigin`, `remoteGui.allowedTailscaleUsers`, +retired `allowInsecureHttp` 문서화)은 wp8에서 처리한다. +draft 해제도 wp8에서 CI 그린 확인 후. + +## 커밋 + +| 커밋 | 내용 | +| --- | --- | +| `1e3f7d2b7`→재적용 | feat(remote-gui): add remote session issuance and pairing | +| `129a64184`→재적용 | fix(remote-gui): harden identity and capability replay checks | +| `53986b612`→재적용 | test(remote-gui): cover remote session consent boundaries | +| `6c8dd333e`→재적용 | fix(remote-gui): enforce exact bootstrap destination | +| `0c6670e88`→재적용 | test(remote-gui): lock replay and expiry negatives | +| `2d1262bc5` | fix(remote-gui): preserve renewal and mutation origin checks | +| `b7282858b` | fix(remote-gui): drop plaintext pairing and bound the unauthenticated exchange body (신규) | + +원본 6커밋은 authorship과 메시지를 보존했고, 계약 변경은 마지막 조정 커밋 +하나로 모았다. wp2와 같은 이유다 — 원저자의 커밋을 내가 편집한 것처럼 +보이게 만들지 않는다. diff --git a/devlog/_plan/260901_remote_hub_restack/040_wp4_p3_connect.md b/devlog/_plan/260901_remote_hub_restack/040_wp4_p3_connect.md new file mode 100644 index 0000000000..8648b80414 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/040_wp4_p3_connect.md @@ -0,0 +1,52 @@ +# wp4 — p3(#2777) 재스택 + gui api-auth-memory 경계 보존 + +브랜치 `codex/remote-hub-p3`, head `aa2615953`, 11커밋 / 34파일. +dev와 겹치는 파일 18개 — 이 스택에서 CLI 표면 겹침이 가장 넓다. + +## 겹침 + +`src/cli/{claude,dispatch,help,index,registry,runtime-api,status}.ts`, +`src/config.ts`, `src/lib/service-secrets.ts`, `src/types.ts`, +`src/types/config.ts` + 테스트 7(`cli-headless-parity`, +`cli-start-journal-order`, `cli-status-json` 포함). + +`cli-headless-parity`는 wp5에서도 문제를 일으키는 파일이다. 여기서 CLI 표면이 +늘어나므로, p3 재스택 시점에 새 명령이 headless 선언에 들어가 있는지 확인해두면 +wp5의 부담이 준다. + +## gui/tests/api-auth-memory.test.ts — 여기가 아니다 + +`#2777`에 보고됐지만 실측 결과 이 파일을 처음 건드리는 단계는 p2다(p3은 0건). +**wp3으로 재배정했다**(감사 A5). p3 재스택 시점에는 이미 고쳐져 있어야 한다. +여기서는 회귀하지 않았는지만 확인한다. + +## 이 단계가 소유하는 스레드 + +### T22 (P1) — 기존 Codex journal 재소유 + +`src/client/connect.ts:229`. `ocx start` 이후의 정상 상태, 즉 Codex가 이미 +로컬 OpenCodex 프록시를 통하도록 라우팅된 상태에서 `injectCodexConfig`가 +소유권을 잃는다. 연결 전에 기존 journal을 재소유해야 한다. + +### T23 (P2) — 응답 읽기 제한 + +`src/client/hub-client.ts:85`. 신뢰할 수 없는 `Content-Length`(chunked이거나 +고의로 잘못 보고된 `/readyz`, `/api/keys`)에 대해 버퍼링 전에 제한한다. +T20과 같은 계열이므로 같은 캡 정책을 쓴다. + +### T24 (P2) — connect 워크플로 문서화 + +`src/cli/help.ts:35`. stdin 전용 자격증명, 클라이언트 선택, HTTP 처리를 포함한 +사용자 노출 워크플로가 문서화되지 않았다. + +## release-version-line + +wp2와 동일. 이 단계 head에서 명시적으로 확인한다. + +## 검증 + +- `git range-diff` 11커밋 보존. +- `cd gui && bun test tests/api-auth-memory.test.ts` (회귀 확인) +- `bun test tests/cli-headless-parity.test.ts tests/cli-registry.test.ts tests/cli-status-json.test.ts tests/release-version-line.test.ts` +- **이 단계 head가 초록이어야 p4를 그 위에 쌓는다.** +- exact-head CI. diff --git a/devlog/_plan/260901_remote_hub_restack/041_wp4_outcome.md b/devlog/_plan/260901_remote_hub_restack/041_wp4_outcome.md new file mode 100644 index 0000000000..42278dcf58 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/041_wp4_outcome.md @@ -0,0 +1,67 @@ +# wp4 결과 — p3(#2777) 재스택 + T22 + +브랜치 `codex/remote-hub-p3`: `aa2615953` → `ad1ab25d8`. +베이스는 재스택된 `codex/remote-hub-p2@b7282858b`. + +## 충돌 3건 — 전부 순수 추가 + +`src/cli/status.ts`, `tests/cli-dispatch.test.ts`, `src/cli/dispatch.ts`. +dev와 이 단계가 서로 다른 import와 블록을 같은 위치에 넣은 것뿐이라 +양쪽을 모두 살렸다. wp3에서 정규식으로 마커를 지우다 중괄호를 잃은 전례가 +있어, 이번에는 마커 줄 번호를 정확히 지정해 삭제하고 중괄호 균형을 매번 +확인했다. + +원본 11커밋 전부 보존. + +## T22 (P1) — process 소유 journal이 연결을 가둔다 + +리뷰 표현은 "연결 전 기존 Codex journal 재소유 필요"였다. 코드를 따라가니 +실제 증상은 더 나빴다. + +`ocx start` 후 connect하는 것은 예외가 아니라 **정상 경로**다. 그 시점에 +라우팅은 이미 주입돼 있고 journal 소유자는 프록시 프로세스다. connect는 +소유권을 가져오지 못한다 — `writeJournal()`이 이미 주입된 config를 가진 +journal을 덮어쓰지 않기 때문이다(`journal.ts:99`). 그래서 process 소유자가 +연결 상태로 그대로 살아남는다. + +그리고 `disconnectClient()`가 자기 키와 안 맞는 소유자를 전부 충돌로 읽고 +거부했다. 결과적으로 **운영자가 disconnect할 수 없다.** 아티팩트는 보존되니 +데이터를 잃지는 않지만, 연결 상태에서 나갈 방법이 없다. + +수정: process 소유 journal은 같은 도구가 쓴 주입 이전 baseline이므로 우리가 +되감을 대상이다. 진짜 충돌은 **다른 client 키**가 소유한 경우뿐이고, 그건 +여전히 거부한다(기존 테스트도 그대로 통과). + +journal 없이 라우팅만 주입된 경우는 별도 메시지로 분리했다. 기존에는 소유권 +오류로 뭉뚱그려졌는데, 복원할 baseline 기록이 아예 없다는 게 실제 원인이다. + +**레드-퍼스트:** 수정을 되돌려 새 테스트가 실패하는 것을 확인한 뒤 복원했다. +처음에 픽스처 조건이 `disconnect-conflict`에만 걸려 있어 새 시나리오가 +codex를 선택조차 하지 않는 실수가 있었고, 그래서 "통과"가 가짜였다. 조건을 +고친 뒤에야 진짜 레드가 나왔다. + +## gui api-auth-memory + +wp3에서 소유 단계를 p2로 재배정했으므로 여기서는 회귀만 확인했다. + +## 검증 + +- `bun run typecheck` 통과. +- 포커스드 8파일 **324 pass / 0 fail** + (client-connect, cli-dispatch, cli-registry, cli-status-json, + cli-headless-parity, cli-start-journal-order, config, claude-cli). + +## 남은 것 + +T23(신뢰할 수 없는 `Content-Length`에 대한 응답 읽기 제한)과 +T24(connect 워크플로 문서화)는 wp8에서 처리한다. + +## 커밋 + +원본 11커밋은 authorship과 메시지를 보존했고, T22 수정은 +`ad1ab25d8` 한 커밋으로 분리했다. 앞선 단계들과 같은 원칙이다. + +| 범위 | 내용 | +| --- | --- | +| `859bc17aa`..`232ad4e4b` | 원본 11커밋 재적용 | +| `ad1ab25d8` | fix(connect): a process-owned journal is ours to unwind, not a conflict (신규) | diff --git a/devlog/_plan/260901_remote_hub_restack/050_wp5_p4_two_plane.md b/devlog/_plan/260901_remote_hub_restack/050_wp5_p4_two_plane.md new file mode 100644 index 0000000000..22bda8d278 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/050_wp5_p4_two_plane.md @@ -0,0 +1,80 @@ +# wp5 — p4(#2781) 재스택 + D3 Origin verbatim 전달 + +브랜치 `codex/remote-hub-p4`, head `44f9973a2`, 8커밋 / 48파일, draft. +dev와 겹치는 파일 17개 — 그중 9개가 i18n 로케일이다. + +## 겹침 + +`src/cli/dispatch.ts`, `src/cli/index.ts`, +`src/server/management/logs-usage-routes.ts`, `src/usage/summary.ts`, +`gui/src/i18n/*.ts` 9개, `gui/src/pages/{Integrations,Storage}.tsx`, +`tests/{cli-start-journal-order,usage-summary}.test.ts`. + +i18n 충돌은 기계적이다(양쪽이 서로 다른 키를 추가). 9개 로케일 전부에서 dev 키와 +스택 키가 모두 살아남아야 한다. 하나라도 누락되면 로케일 패리티 게이트가 잡는다. + +## D3 — Origin verbatim 전달 구현 + +010의 D3이 여기서 코드가 된다. 관련 커밋: + +- `b826c200e feat(two-plane): add client machine and hub GUI planes` +- `c8a7b8ce9 feat(two-plane): harden relay and offline target states` + +현재 구현은 `POST /opencodex-session`에만 브라우저 Origin을 전달한다. +요구: 허용된 세션 인증 mutation 전체(POST/PUT/PATCH/DELETE)에 대해 Origin을 +원문 그대로 전달한다. 합성 Origin fallback을 두지 않는다 — 릴레이가 Origin을 +만들어내면 허브의 CSRF 검사는 자기 자신을 검사하는 셈이 된다. + +테스트: 허용 메서드마다 릴레이 후 허브가 받은 Origin이 브라우저 원문과 +같음을 확인하는 케이스. Origin 부재 시 요청이 거절되는 네거티브. + +## D5 — 릴레이 응답의 validator 제거 + +릴레이를 처음 갖는 단계가 여기이므로 D5도 여기서 구현한다. 릴레이된 +세션/부트스트랩/관리 응답은 기본이 `Cache-Control: no-store`이고 ETag / +Last-Modified를 제거한다. p6에서 적대적 커버리지를 덧붙인다. + +## /api/machine/* 라우트 선언 — 여기가 소유 단계다 + +`#2786`(p5)에 보고됐지만 실측하면 `/api/machine/` 추가 라인이 p4에 **49건**, +p5에는 0건이다. 라우트를 도입한 건 p4다(감사 A5). + +`tests/cli-headless-parity.test.ts:287`은 "서버가 여는 라우트와 CLI가 선언한 +표면이 일치한다"를 주장한다. 7개 라우트를 열거하고 각각 이 단계에 필요한지 +판정한 뒤, 필요한 것은 명시 선언하고 불필요한 것은 제거한다. 테스트 예외를 +추가해 숨기는 방향은 금지. + +## 이 단계가 소유하는 스레드 + +### T25 (P1) — relay 트랜스포트가 동작하지 않는다 + +`src/client/machine-listener.ts:79`. 문서화된 `--management-transport relay`를 +고르면 `connectClient`가 여전히 throw한다. 문서에 있는 옵션이 죽어 있는 것이므로 +연결 경로를 새 리스너까지 잇는다. + +### T26 (P1) — supervised 런타임이 disconnect 후 재시작되지 않는다 + +`src/client/runtime.ts:27`. systemd나 WinSW로 뜬 런타임은 `OCX_SERVICE=1` 때문에 +해당 분기를 건너뛰어 재시작되지 않는다. + +### T27 / T28 (P2) — GUI + +`gui/src/App.tsx:222` disconnect 202 성공 시 `targets.connected` 갱신 누락. +`gui/src/App.tsx:376` pairing 완료 전 공유 페이지가 함께 마운트된다. + +### T2 (P2, #2771에서) — 연결된 GUI의 인증된 models 경로 + +`gui/src/pages/ApiKeys.tsx:154`가 `/v1/models`를 부르는데 허브는 그 경로를 +데이터플레인으로 처리한다. 인증된 경로를 제공한다. + +## draft 해제 + +wp3와 동일 근거. 재스택 + CI 그린 후 해제. + +## 검증 + +- `git range-diff` 8커밋 보존. +- `bun test tests/usage-summary.test.ts tests/cli-start-journal-order.test.ts tests/cli-headless-parity.test.ts tests/release-version-line.test.ts` +- i18n 9개 로케일 키 존재 확인. +- **이 단계 head가 초록이어야 p5를 그 위에 쌓는다.** +- exact-head CI. diff --git a/devlog/_plan/260901_remote_hub_restack/051_wp5_outcome.md b/devlog/_plan/260901_remote_hub_restack/051_wp5_outcome.md new file mode 100644 index 0000000000..84a272ad01 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/051_wp5_outcome.md @@ -0,0 +1,92 @@ +# wp5 결과 — p4(#2781) 재스택 + 블로커 5건 + +브랜치 `codex/remote-hub-p4`: `44f9973a2` → `95787b9bc`. +베이스는 재스택된 `codex/remote-hub-p3@ad1ab25d8`. + +## 충돌 + +`src/usage/summary.ts`(2회), `gui/src/pages/Integrations.tsx`. + +usage/summary는 dev가 주석을 옮기고 이 단계가 그 위에 apiKeyId 필터를 +얹은 구조였다. 두 필터의 층이 다르다는 점을 주석으로 명시했다: apiKeyId는 +엔트리 전체를 자르고(키가 엔트리를 소유하므로), provider/model은 어트리뷰션 +단위로 좁힌다(콤보 엔트리의 다른 시도 비용이 딸려오면 안 되므로). + +**중간에 실수가 있었다.** 첫 해소에서 고아 마커 한 줄이 커밋에 들어갔고, +rerere가 그 잘못된 해소를 기억해 재시도에서 재현했다. 리베이스를 중단하고 +원본에서 다시 시작해 마커를 제거한 뒤, 스택 전 범위에 대해 +`git grep`으로 마커 0건을 확인했다. + +원본 8커밋 보존. + +## 블로커 5건 + +### `/api/machine/*` 7개 미선언 (wp6에서 재배정됨) + +`tests/cli-headless-parity.test.ts:287`이 잡은 그대로다. 7개 라우트는 +문서화되지 않은 게 아니라 선언되지 않은 것이었다: status/clients는 +`ocx connect status`, sync는 `ocx sync`, shim은 클라이언트 통합 명령, +disconnect는 `ocx disconnect`에 대응한다. hub-relay만 자체 verb가 없는데 +그건 `--management-transport relay`가 고르는 전송 경로이기 때문이다. +한 프리픽스로 선언하고 대응 관계를 주석에 적었다. + +### T25 (P1) — relay가 항상 throw + +`connectClient()`가 "relay management transport is not available before +Remote Hub Phase 4"를 던졌다. **그런데 이 단계가 Phase 4다.** 머신 리스너와 +hub-relay가 모두 여기서 랜딩한다. Phase 3의 가드가 남은 것이고, 문서화된 +옵션이 항상 실패하는 상태였다. + +### T26 (P1) — supervised 클라이언트가 disconnect 후 안 돌아온다 + +`scheduleStandaloneRecycle()`이 `OCX_SERVICE=1`이면 자가 재시작을 건너뛴다. +그것 자체는 옳다 — supervisor가 프로세스를 소유하므로 두 번째 복사본은 +포트를 두고 다툰다. 문제는 그 다음 `process.exit(0)`이다. + +실제 supervisor 설정은 전부 failure-only다: systemd `Restart=on-failure`, +WinSW ``, Task Scheduler ERRORLEVEL 루프. +깨끗한 종료는 "서비스가 끝났다"로 읽혀 아무것도 재시작하지 않는다. +클라이언트가 누군가 알아챌 때까지 죽어 있었다. + +supervised일 때 exit 1로 바꿨다. 대시보드 recycle이 이미 쓰는 정책이고 +(`src/server/management/system-restart.ts`), launchd `KeepAlive`는 어느 +쪽이든 정상 동작한다. + +### D1 클라이언트 측 + +`--allow-insecure-http`가 CLI, connect 옵션, hub-client에 남아 있었다. +허브가 이제 평문 pairing을 거부하므로 플래그를 남기면 단회용 grant를 +확실한 거절에 태우는 것뿐이다. 클라이언트도 같은 규칙을 로컬에서 검사해 +전송 전에 거절한다. + +### D2 클라이언트 측 — 연결 자체가 깨질 뻔했다 + +`connect`가 `catalog.etag`가 없으면 "initial hub catalog did not include a +fresh ETag"로 **실패**했다. D2로 서버가 validator를 안 주게 됐으니 그대로면 +모든 연결이 실패한다. 조건부 페치를 걷어내고, 저장하던 `catalogEtag`를 +`catalogFingerprint`(우리가 쓴 바이트의 해시)로 바꿨다. + +그 값은 애초에 캐시 관심사가 아니었다 — disconnect가 파일을 지우기 전에 +"디스크의 이 파일이 아직 우리 것인가"를 묻는 소유권 검사이고, 서버의 참여가 +필요 없다. ETag 문자열을 재사용했기 때문에 캐시처럼 보였을 뿐이다. + +## 검증 + +- `bun run typecheck` 통과. +- 포커스드 8파일 **356 pass / 0 fail**. +- 스택 전 범위 충돌 마커 0건. + +## 남은 것 + +T27/T28(GUI disconnect 타깃 갱신, pairing 전 페이지 게이팅), T2(연결된 GUI의 +인증된 models 경로), D5(릴레이 응답 no-store)는 wp8 또는 후속 단계에서. + +## 커밋 + +| 범위 | 내용 | +| --- | --- | +| `67c6387a5`..`da6f97a39` | 원본 8커밋 재적용 | +| `95787b9bc` | fix(two-plane): declare the machine plane, enable relay, and finish the D1/D2 client side (신규) | + +앞선 단계들과 같은 원칙: 원본 커밋의 authorship과 메시지를 보존하고, +계약 변경은 마지막 조정 커밋 하나로 모은다. diff --git a/devlog/_plan/260901_remote_hub_restack/060_wp6_p5_deploy.md b/devlog/_plan/260901_remote_hub_restack/060_wp6_p5_deploy.md new file mode 100644 index 0000000000..3a14f5d8f0 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/060_wp6_p5_deploy.md @@ -0,0 +1,61 @@ +# wp6 — p5(#2786) 재스택 + 라우트 선언 / 계약 복원 + +브랜치 `codex/remote-hub-p5`, head `a62c8eba2`, 8커밋 / 19파일. +dev와 겹치는 파일 5개로 스택에서 가장 얕다. 그런데 블로커는 가장 많다. + +## 블로커 4건 — 전부 계약 위반 + +리뷰어가 "인프라 노이즈가 아니라 제품 계약"이라고 못박은 항목들이다. + +### tests/cli-headless-parity.test.ts:287 — 여기가 아니다 + +`#2786`에 보고됐지만 `/api/machine/` 추가 라인은 p4에 49건, p5에는 0건이다. +라우트를 도입한 건 p4이므로 **wp5로 재배정했다**(감사 A5). 여기서는 p4가 +선언을 고친 뒤에도 이 단계에서 회귀하지 않는지만 확인한다. + +### tests/update-stop-first.test.ts:225 — stop-first 계약 + +업데이트 시 먼저 중지한다는 계약이 깨졌다. p5가 관리 ingress를 추가하면서 +라이프사이클 순서를 건드렸을 가능성이 높다. `149b7215a feat(deploy): add +loopback hub management ingress` 부터 본다. + +### tests/loopback-listener-admission.test.ts:196 — role-admission 계약 + +loopback 리스너의 admission 규칙이 깨졌다. `d6461bfd2 feat(deploy): harden +management ingress allowlist` 가 allowlist를 바꾸면서 기존 admission을 +덮었는지 확인한다. 두 allowlist가 공존해야 하는 구조라면 병합한다. + +### privacy 게이트 + +배포 가이드와 ingress 로깅에서 자격증명/호스트 식별자가 새는지 확인한다. +`bun run privacy:scan`으로 재현하고 코드를 고친다. + +## 이 단계가 소유하는 스레드 + +### T29 (P2) — hub role을 disconnected client state에서 배제 + +`src/client/state.ts:46`. `client` 블록이 없는 허브를 `disconnected`로 +분류하면 `connectClient()`가 그 상태 검사를 통과해버린다. + +### T30 (P2) — missing-config 부트스트랩 조건화 + +`src/client/state.ts:85`. `mutatePersistedConfig()`가 `missing`을 보고할 때 +뮤테이션 락을 얻기 전에 반환해서, 다른 첫 실행 명령과 경쟁한다. + +### T3 / T11 (#2771에서) + +관리 ingress에서 GUI health 엔드포인트 보존(`070_phase5_deploy.md:164`), +안정성 지적(`:300`). Tailscale Serve 배포에서 브라우저가 관리 리스너를 쓰므로 +health 경로가 살아 있어야 한다. + +## release-version-line + +wp2와 동일. 이 단계 head에서 명시적으로 확인한다. + +## 검증 + +- `git range-diff` 8커밋 보존. +- `bun test tests/cli-headless-parity.test.ts tests/update-stop-first.test.ts tests/loopback-listener-admission.test.ts tests/service.test.ts tests/release-version-line.test.ts` +- `bun run privacy:scan` +- **이 단계 head가 초록이어야 p6을 그 위에 쌓는다.** +- exact-head CI. diff --git a/devlog/_plan/260901_remote_hub_restack/061_wp6_outcome.md b/devlog/_plan/260901_remote_hub_restack/061_wp6_outcome.md new file mode 100644 index 0000000000..19577c712a --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/061_wp6_outcome.md @@ -0,0 +1,61 @@ +# wp6 결과 — p5(#2786) 재스택 + +브랜치 `codex/remote-hub-p5`: `a62c8eba2` → `8bcfcaa8e`. +베이스는 재스택된 `codex/remote-hub-p4@95787b9bc`. + +## 충돌 2건 + +`tests/server-management-auth.test.ts`: 이 단계가 관리 ingress pairing 교환 +테스트를 wp3이 다시 쓴 평문 pairing 테스트 앞에 삽입한다. 둘 다 유지했다. + +`structure/01_runtime.md`: dev가 `codex-cli-update` 문장을, 이 단계가 +hub-management 리스너 절을 각각 추가했다. 두 행 모두 양쪽 내용을 담도록 합쳤고, +합친 뒤 각 문장이 실제로 살아 있는지 grep으로 확인했다. + +원본 8커밋 보존. + +## 리뷰가 지목한 블로커 4건 — 실측 결과 + +리뷰는 `cli-headless-parity:287`, `update-stop-first:225`, +`loopback-listener-admission:196`, privacy 게이트를 들었다. 재스택 후 실제로 +돌려보니 넷 중 셋은 이미 해소돼 있었다. + +- `cli-headless-parity` 42 pass — `/api/machine/*` 선언은 소유 단계인 wp5에서 + 이미 처리했다(감사 A5의 재배정이 맞았다). +- `update-stop-first` 15 pass — 상속된 staleness였다. +- privacy 게이트 통과 — 역시 staleness. +- `loopback-listener-admission`만 실제로 빨간색이었다. + +## loopback-listener-admission:196 + +테스트가 non-hub role 셋(undefined, standalone, client)을 순회하며 전부 +`"requires runtimeRole hub"` 메시지로 거절되기를 요구했다. 그런데 `client`는 +더 앞선 규칙 — client role은 완전한 연결 블록이 필요하다 — 에 먼저 걸린다. + +거절 자체는 옳다. 틀린 것은 **두 독립적인 검증 규칙 사이의 순서를 단언한 것**이다. +계약은 그런 순서를 약속한 적이 없다. + +행을 쪼갰다. undefined/standalone은 정확한 ingress 메시지를 그대로 단언하고, +`client`는 "거절된다"만 단언한다. 그리고 이게 구멍을 만들지 않도록 케이스를 +하나 더 넣었다: **완전한** client 연결을 주면 앞선 규칙이 안 걸리고, 그때 +거절하는 것이 ingress 규칙임을 확인한다. 이게 없으면 ingress 규칙이 그 role에 +아예 적용되지 않게 되어도 약해진 단언이 통과해버린다. + +## 검증 + +- `bun run typecheck` 통과, `bun run privacy:scan` 통과. +- 포커스드 5파일 **263 pass / 0 fail**. +- `tests/service.test.ts` 192 pass / 0 fail. +- 충돌 마커 0건. + +## 남은 것 + +T29/T30(hub role을 disconnected client state에서 배제, missing-config +부트스트랩 경쟁)과 T3/T11은 wp8에서. + +## 커밋 + +| 범위 | 내용 | +| --- | --- | +| `149b7215a`..`f2bf97d4f` | 원본 8커밋 재적용 | +| `8bcfcaa8e` | test(deploy): assert the ingress role rule where the message is actually reachable (신규) | diff --git a/devlog/_plan/260901_remote_hub_restack/070_wp7_p6_hardening.md b/devlog/_plan/260901_remote_hub_restack/070_wp7_p6_hardening.md new file mode 100644 index 0000000000..caf031d053 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/070_wp7_p6_hardening.md @@ -0,0 +1,75 @@ +# wp7 — p6(#2789) 재스택 + D4 로테이션 크래시 복구 + +브랜치 `codex/remote-hub-p6`, head `207254fe0`, 17커밋 / 95파일, draft. +dev와 겹치는 파일 58개 — 스택 전체에서 가장 크다. + +## 겹침 + +`src/cli/{access,index,registry}.ts`, `src/config.ts`, +`src/lib/service-secrets.ts`, `src/server/auth-cors.ts`, +`src/server/index.ts`, `src/server/management-api.ts`, +`src/server/management/{context,oauth-account-routes}.ts`, +`src/types/config.ts`, i18n 9개, docs-site 7로케일 다수. + +docs-site 겹침이 큰 덩어리인데 대부분 로케일 문서라 기계적이다. +실제 판단이 필요한 건 `management-api.ts`, `management/context.ts`, +`oauth-account-routes.ts` — dev가 이번 트레인에서 계속 건드린 곳이다. + +## D4 — 크래시 복구 판정 수정 + +010의 D4가 여기서 코드가 된다. 관련 커밋: + +- `a83073115 feat(hardening): recover client key rotation through token backup` +- `cc620f7b7 fix(hardening): gate startup on rotation recovery state` + +현재: current와 backup 둘 다 probe 성공이면 로테이션 완료로 본다. +문제: `pendingOperation` 저장 직후 크래시 시 두 파일 모두 옛 키를 담고 +둘 다 probe에 성공한다 → 로테이션이 조용히 유실된다. + +수정(감사 A7): 010 D5 계약을 그대로 구현한다. + +1. probe 이전에 두 후보의 identity를 비교한다. +2. 동일하면 교체 이전 상태다 — commit하지 않는다. +3. abort/restore는 확인된 권위가 있을 때만. +4. abort가 불확실하게 실패하면 증거를 보존한다. + +레드-퍼스트 회귀 3종: 동일-구세대 후보, abort 실패, 진행 중 백업을 지우는 +동시 status 실행. + +## 이 단계가 소유하는 스레드 + +### T31 (P1) — abort 실패 시 토큰 identity 보존 + +`src/client/connect.ts:304`. 새 토큰 설치 후 abort 요청이 일시적으로 실패하면 +현재 코드가 잘못된 세대를 복원한다. D4 계약의 3/4항이 바로 이 사안이다. + +### T32 (P2) — status가 진행 중 백업을 삭제 + +`src/client/state.ts:95`. `rotateConnectedClientKey`가 `/api/keys/rotate`를 +기다리는 동안 `ocx connect status`가 돌면 in-flight 백업이 지워진다. + +### T33 (P2) — 릴레이 오류를 과대 응답 노출 전에 반환 + +`src/client/hub-relay.ts:282`. `Content-Length` 없는 chunked 업스트림 응답 처리. + +### D5 적대적 커버리지 + +wp5가 구현한 릴레이 no-store / validator 제거에 대해 이 단계에서 적대적 +테스트를 추가한다. + +## 리뷰어가 예고한 최종 보안 심사 항목 + +`#2789` 코멘트가 재리뷰 시 볼 항목을 나열했다. 재스택 시 이 목록을 체크리스트로 +쓴다: 로테이션 크래시 복구, 토큰 백업 소유권/정리, 일회성 시크릿 노출, +세션 무효화, pairing 레이트 리밋, 릴레이 SSRF/헤더 스트리핑, 취소. + +## draft 해제 + +wp3와 동일 근거. + +## 검증 + +- `git range-diff` 17커밋 보존. +- 로테이션/시크릿 관련 포커스드 테스트. +- `bun run privacy:scan` +- exact-head CI. diff --git a/devlog/_plan/260901_remote_hub_restack/071_wp7_outcome.md b/devlog/_plan/260901_remote_hub_restack/071_wp7_outcome.md new file mode 100644 index 0000000000..c4cdb293cc --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/071_wp7_outcome.md @@ -0,0 +1,67 @@ +# wp7 결과 — p6(#2789) 재스택 + D4 계열 + +브랜치 `codex/remote-hub-p6`: `207254fe0` → `ff2913297`. +베이스는 재스택된 `codex/remote-hub-p5@8bcfcaa8e`. 17커밋 / 95파일로 가장 크다. + +## 충돌 4건 + +`src/client/hub-client.ts`(2회), 터키어 관리 API 문서, +`tests/loopback-listener-admission.test.ts`. + +hub-client 충돌이 본질적이었다. p6가 스키마 검증과 `x-opencodex-key-id` 에코를 +추가하는데, 그 토대가 D2가 없앤 조건부 페치 경로 위에 있었다. validator 처리를 +걷어내고 추가분만 살렸다. + +커밋 `e7ca5bb89`("reject mismatched catalog validators")는 소스 변경 전체가 +사라진 경로 전용이라 적용할 대상이 없었다. 의도는 이미 더 강하게 흡수돼 있다 — +어떤 304든 거절하는 것이 "보낸 ETag와 다른 304를 거절"보다 넓다. 그 사실을 +테스트로 남겼다. + +`loopback-listener-admission`은 흥미로웠다. p6가 wp6에서 내가 고친 것과 +**같은 문제를 다르게** 고쳐뒀다: client role에 완전한 연결 블록을 채워 넣어 +세 role 전부를 정확한 메시지로 단언한다. p6 쪽이 낫다 — 내 버전은 client에 +대해 "거절된다"만 단언하고 별도 케이스로 보강했는데, p6는 한 루프로 끝낸다. +p6를 채택하고 내 중복 케이스를 제거했다. + +원본 17커밋 보존, 마커 0건. + +## T31 (P1) — abort 실패 시 토큰 identity + +롤백 경로가 로컬 토큰을 복원한 **뒤** 허브에 abort를 요청했다. abort가 +일시적으로 실패하면 로컬은 옛 키를, 허브는 새 키에 대한 pending 로테이션을 +들고 있다. 양쪽이 어느 세대가 현재인지 불일치하고, 이게 "rollback was +incomplete"라는 메시지로만 드러난다. + +순서를 뒤집었다. 어느 세대가 살아 있는지는 허브가 정하므로 먼저 확인하고, +동의한 뒤에만 로컬을 되감는다. 실패 시 두 후보와 pending 마커를 모두 디스크에 +남긴다 — 물어보지 않고는 정말로 판정할 수 없기 때문이다. + +## T32 (P2) — status가 인플라이트 백업을 삭제 + +orphan 정리 분기가 "백업 있음 + 토큰 있음 + pending 마커 없음"에서 발동한다. +그런데 `rotateConnectedClientKey`는 `.prev`를 쓴 **다음에** +`pendingOperation`을 저장한다. 그 사이에 `ocx connect status`가 돌면 정확히 +저 조건을 보고, 진행 중인 로테이션이 의지하던 롤백 대상을 지운다. + +게이트가 영속 상태를 다시 읽도록 했다 — 호출자의 스냅샷은 마커보다 앞설 수 +있다 — 그리고 로테이션이 기록돼 있으면 정리하지 않는다. + +## D4 — 이미 상당 부분 지켜지고 있었다 + +설계에서 요구한 "복구는 정지하고, 재개는 전이 권한을 가진 다음 rotate가"는 +`inspectClientRotationRecoveryGate`가 이미 그렇게 동작한다. probe 없이 +`recovery-required`로 멈추고 rotate를 안내한다. wp1에서 계약을 실행 가능하게 +다시 쓴 것이 코드와 일치했다. + +## 검증 + +- `bun run typecheck` 통과, `bun run privacy:scan` 통과. +- 포커스드 6파일 **302 pass / 0 fail**. +- 충돌 마커 0건. + +## 커밋 + +| 범위 | 내용 | +| --- | --- | +| `83c57609f`..`65c1f85a7` | 원본 17커밋 재적용 | +| `ff2913297` | fix(hardening): confirm the abort before rewinding, and never delete an in-flight backup (신규) | diff --git a/devlog/_plan/260901_remote_hub_restack/072_stack_state.md b/devlog/_plan/260901_remote_hub_restack/072_stack_state.md new file mode 100644 index 0000000000..08588fdefe --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/072_stack_state.md @@ -0,0 +1,40 @@ +# 스택 상태 — 7단계 재스택 완료 시점 + +| PR | 브랜치 | 이전 head | 새 head | 부모 | +| --- | --- | --- | --- | --- | +| #2771 | codex/remote-hub-design | `bad162407` | `36992baa9` | dev | +| #2772 | codex/remote-hub-p1 | `c10ef21a9` | `07d7f1006` | design | +| #2776 | codex/remote-hub-p2 | `7099760a5` | `b7282858b` | p1 | +| #2777 | codex/remote-hub-p3 | `aa2615953` | `ad1ab25d8` | p2 | +| #2781 | codex/remote-hub-p4 | `44f9973a2` | `95787b9bc` | p3 | +| #2786 | codex/remote-hub-p5 | `a62c8eba2` | `8bcfcaa8e` | p4 | +| #2789 | codex/remote-hub-p6 | `207254fe0` | `ff2913297` | p5 | + +각 단계는 직전 단계의 재스택된 head 위에 얹혔다. 원본 커밋은 전부 authorship과 +메시지를 보존했고, 계약 변경은 단계마다 조정 커밋 하나로 분리했다. + +## 원본 커밋 보존 + +| 단계 | 원본 커밋 | 조정 커밋 | +| --- | --- | --- | +| design | 9 | 3 | +| p1 | 5 | 1 | +| p2 | 6 | 1 | +| p3 | 11 | 1 | +| p4 | 8 | 1 | +| p5 | 8 | 1 | +| p6 | 17 | 1 | + +## 해소된 것 + +D1~D5 설계 계약 5건, 리뷰 스레드 중 P1 6건(T1, T20, T22, T25, T26, T31)과 +T32, 그리고 리뷰 본문이 지목한 테스트 실패 전부. + +stale 아티팩트였던 것들 — `release-version-line`, privacy 게이트, +`update-stop-first`, `cli-headless-parity`의 일부 — 은 재스택으로 소멸했고 +각 단계 head에서 실제로 확인했다. + +## 남은 것 + +P2/Minor 스레드들(T2/T3/T7/T10/T11/T19/T21/T23/T24/T27/T28/T29/T30/T33)과 +`#2771`의 마크다운 린트 6건. wp8에서 처리하거나 근거를 갖춘 반박을 남긴다. diff --git a/devlog/_plan/260901_remote_hub_restack/080_wp8_stack_integrity.md b/devlog/_plan/260901_remote_hub_restack/080_wp8_stack_integrity.md new file mode 100644 index 0000000000..968646f388 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/080_wp8_stack_integrity.md @@ -0,0 +1,79 @@ +# wp8 — 스택 체인 정합 + 최종 판정 + +7단계가 전부 푸시된 뒤 실행하는 마감 사이클. + +## 체인 정합 + +각 PR의 base가 직전 단계 head 브랜치를 정확히 가리켜야 한다. + +| PR | base여야 하는 것 | +| --- | --- | +| #2771 | dev | +| #2772 | codex/remote-hub-design | +| #2776 | codex/remote-hub-p1 | +| #2777 | codex/remote-hub-p2 | +| #2781 | codex/remote-hub-p3 | +| #2786 | codex/remote-hub-p4 | +| #2789 | codex/remote-hub-p5 | + +재스택 과정에서 GitHub가 base를 자동 변경하는 경우가 있으므로 푸시 후 매번 +확인한다. base가 어긋나면 각 PR의 diff가 상류 델타를 삼켜서 리뷰가 불가능해진다. + +## 계보 확인 — 부모 엣지까지 + +`origin/dev`가 조상인지만 보면 부족하다(감사 A9): 부모를 건너뛰고 dev 위로 +직접 리베이스된 자식도 그 검사를 통과한다. 각 **엣지**를 확인한다: + +```sh +git merge-base --is-ancestor origin/ origin/ +``` + +6개 엣지 전부에 대해 실행하고 양쪽 OID를 기록한다. 그리고 각 PR의 base ref가 +같은 부모 브랜치를 가리키는지 대조한다. + +## draft 해제 + +`#2776` / #2781 / #2789. CI 그린 확인 후에만. + +## PR 설명 갱신 + +각 PR에 재스택 사실과 블로커 해소 내역을 적는다. 리뷰어가 exact head에 걸어둔 +CHANGES_REQUESTED는 새 head에서 자동 해제되지 않으므로, 무엇이 어떻게 +해소됐는지 파일:줄로 지목해야 재리뷰가 가능하다. + +## 리뷰 스레드 마감 + +`003` 원장의 33건이 전부 처리돼야 한다. 처리 = 수정하고 resolve, 또는 근거를 +갖춘 반박을 남기고 resolve. 침묵한 채로 남은 스레드가 있으면 종료선 미달이다. +P1 6건(T1, T20, T22, T25, T26, T31)은 반박이 아니라 수정으로만 닫는다. + +## 종료선 + +DONE = 다음 전부: + +1. 7단계 재스택 푸시 완료, 각 단계 range-diff 커밋 보존. +2. B1~B7 및 D1~D5 해소. +3. 리뷰 스레드 33건 처리 완료. +4. **각 단계가 자기 head에서** exact-head CI 그린. +5. base 체인 정합(6개 엣지 + PR base ref 대조). +6. draft 해제(#2776/#2781/#2789). +7. 각 PR에 재리뷰 요청. + +**머지는 하지 않는다** — 사용자 요청은 "머지 가능한 정도까지 세팅"이다. + +### 승인은 우리 손 밖이다 + +감사 A1: `MAINTAINERS.md:57-61`은 비저자 메인테이너 승인과 보안 리뷰를 +요구하고, Ingwannu가 유일한 비저자 메인테이너이며 지금 7건 전부에 +CHANGES_REQUESTED를 걸어두었다. CI가 초록이어도 이 상태로는 머지 버튼이 +열리지 않는다. + +우리 종료선은 **재리뷰 요청 가능 상태**까지다. 그 뒤 승인이 오지 않는 것은 +외부 의존이며, 그 지점에 도달하면 BLOCKED으로 증거와 함께 보고한다. +"CI 그린이니 머지 가능"이라고 보고하지 않는다. + +## 부분 실패 처리 + +한 단계가 막히면 그 단계만 BLOCKED으로 증거와 함께 보고한다. 스택이므로 +하류가 막히면 상류는 그 위에 쌓을 수 없다. 막힌 단계를 건너뛴 재스택은 +하지 않는다. diff --git a/devlog/_plan/260901_remote_hub_restack/081_wp8_ci_repairs.md b/devlog/_plan/260901_remote_hub_restack/081_wp8_ci_repairs.md new file mode 100644 index 0000000000..5217409033 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/081_wp8_ci_repairs.md @@ -0,0 +1,124 @@ +# wp8 — exact-head CI 실패 규명과 수정 + +7단계 재스택 후 각 PR의 exact head에서 CI를 돌려 실패를 하나씩 규명했다. +네 갈래였고, 그중 셋이 진짜 결함이었다. + +## 1. `test 3/4` — sync 러너가 종료 코드를 삼킨다 (p3 소유) + +`tests/cli-transport-honesty.test.ts`가 "핸들러를 await한 뒤 리터럴 0을 +반환하는" 러너를 잡는다. 그 패턴은 핸들러가 `process.exitCode`에 기록한 +실패를 지우기 때문이고, 예외는 이름이 아니라 검증된 이유와 함께 allowlist에 +올려야 한다. + +connected sync 분기에는 그런 이유가 없다. `handleConnectedSyncCatalogWrite`가 +app-server 재시작을 구동하므로 거기서 난 실패는 살아남아야 한다. +다른 러너들과 같이 `process.exitCode`를 반환하게 했다. + +## 2. `hygiene` — suppression (p4 소유) + +`gui/src/connect-pairing.ts`가 `react-refresh/only-export-components`를 +린트 억제 주석으로 막고 있었다. 룰이 옳았다 — 한 파일이 전송 함수와 컴포넌트를 +같이 export한다. 억제 대신 `connect-pairing-transport.ts`로 분리했다. +전송은 React 없이 테스트 가능하고, 폼은 그걸 호출하는 것 말고 로직이 없다. + +(이 문서가 억제 지시자를 문자 그대로 적었더니 hygiene 게이트가 새 억제로 읽어 +draft를 유지시켰다. 게이트가 옳게 동작한 것이므로 문구를 바꿨다.) + +## 3. `gates` — 릴레이 pairing이 인증 없이 나간다 (p4 소유) + +`submitConnectPairing`이 `fetchImpl: typeof fetch = fetch`를 받았다. + +(정정 — 초판의 설명은 틀렸다. "기본 매개변수는 모듈 평가 시점의 전역을 묶는다"고 +썼는데, 기본값 초기화식은 **호출 시점에** 평가된다. 스펙이 그렇고, 이 문서가 +반대로 적어두면 다음 사람이 잘못된 모델로 디버깅한다.) + +실제 실패는 바인딩 시점이 아니라 **어느 전역을 보느냐**의 문제였다. 테스트 +환경에서 happy-dom의 `window`와 Bun의 `globalThis`가 갈리고, +`installApiAuthFetch`는 `window.fetch`에 래퍼를 씌운다. 호출 시점에 평가된 +맨 `fetch`가 그 래퍼가 아닌 다른 실체로 해석될 수 있고, 래퍼 설치보다 모듈 +참조가 먼저 굳는 경로도 있다. 릴레이는 래퍼가 붙이는 머신 세션 헤더를 +요구하므로 허브가 교환을 거부했다. 호출 시점에 `window.fetch`를 명시적으로 +집어오도록 고쳤다. + +## 4. `gates` — happy-dom에 없는 prompt (p2 소유) + +거부된 세션을 정리하는 테스트들이 admin 토큰 폴백에 도달하는데, +happy-dom은 `prompt`를 구현하지 않는다. 그래서 그 테스트들은 검증하려던 +동작이 아니라 TypeError로 죽었다. 대부분의 테스트는 폴백에 안 닿아서 +가려져 있었다. null을 반환하는 스텁이 "운영자가 프롬프트를 닫았다"에 +해당하는 정직한 대역이다. + +## 5. GUI 스위트 격리 — 제품 결함 아님 + +`tests/connect-pairing.test.ts`가 단독으로는 통과하고 전체 실행에서 실패했다. +App이 모듈 스코프에서 `installApiAuthFetch()`를 부르므로 최초 import에서만 +실행된다. 나중에 App을 import하는 테스트는 캐시된 모듈을 받고 설치가 일어나지 +않아, 래퍼가 **먼저 import한 테스트의 window**에 묶인 채로 남는다. + +테스트가 마운트 전에 자기 window로 래퍼를 다시 묶도록 했고, +`claude-toggle-race.test.tsx`는 window를 닫을 때 설치 latch도 함께 지운다. +둘 다 테스트 격리이지 제품 동작이 아니다. + +## macos 실패는 이 스택 탓이 아니다 + +`tests/server-auth.test.ts`의 websocket refresh 단언이 macos에서 실패했는데, +**dev의 HEAD도 같은 러너에서 같은 단언으로 실패한다.** #3139의 수정이 이미 +dev에 들어가 있는데도 그렇다. #2772는 동일 head를 재실행하니 그린이 됐다. +즉 dev에 남은 미해결 flake이고, 재스택이 유발한 것이 아니다. + +## 최종 체인 + +| 단계 | head | +| --- | --- | +| design | `36992baa9` | +| p1 | `07d7f1006` | +| p2 | `2b36ad496` | +| p3 | `38c361362` | +| p4 | `158424f05` | +| p5 | `ff3ce26bd` | +| p6 | `b6aa976e9` | + +6개 엣지 전부 부모가 자식의 조상이고, PR base ref도 같은 부모를 가리킨다. +오염 커밋 0건, 변경 범위는 devlog/docs-site/gui/src/structure/tests뿐이다. + +## 추가로 드러난 두 건 + +### 6. `test 1/4` — 미선언 관리 라우트 4개 (p6 소유) + +`tests/management-route-registry.test.ts`가 선언 레지스트리를 소스와 대조해 +이 단계가 서빙하면서 등록하지 않은 라우트 4개를 찾았다: +`/api/keys/rotate`의 POST/POST commit/DELETE와 `POST /api/session/logout`. + +rotate 3개는 평범한 관리 뮤테이션이라 그대로 선언했다. +`/api/session/logout`은 session-only 예외로 이유와 함께 등록했다 — 현재 +gui-session을 끝내며 그 세션 자신의 Origin과 CSRF를 요구하므로 CLI verb가 +작용할 대상이 없다. CLI는 admin 토큰을 들고 있고, 이 라우트는 바로 그 +admin 토큰을 거부한다. 자기가 만들지 않은 동의 세션을 끝내지 못하게 하려는 +설계다. + +### 7. dev의 websocket flake 근본 원인 — PR #3147로 분리 + +`server-auth`의 websocket refresh 단언이 macOS와 Linux 양쪽에서 실패했고, +dev HEAD도 같은 실패를 낸다. 원인을 찾았다. + +`updateAccountQuota`가 `updatedAt: Date.now()`를 찍는데, 시드가 시계 고정 +**전에** 실행된다. 그래서 그 타임스탬프만 실제 벽시계이고 이후 모든 것은 +고정된 2027 값을 읽는다. 격차가 약 136일인데 신선도 창은 6시간이다 +(`QUOTA_DISK_MAX_AGE_MS`, `src/codex/quota.ts:491`). 러너가 아무리 빨라도 +시드는 stale로 읽히고, 시작 시 pool-quota 프라임이 첫 턴 전에 자격증명을 +갱신해 `seenAuth[0]`이 이미 새 토큰이 된다. 실패 diff가 항상 첫 원소였던 +이유다. + +#3139는 `startServer` 앞에 시계와 fetch를 고정해 프라임 자신의 읽기 창을 +닫았다. 하지만 그 둘이 놓이기 **전에** 쓰인 타임스탬프의 창은 닫을 수 없다. +시드를 고정 뒤로 옮기면 닫힌다. + +이건 dev 소유라 스택에 섞지 않고 **PR #3147**로 분리해 `dev`를 타깃하게 했다. +로컬에서는 수정 전후 모두 재현되지 않으므로 증거는 red-to-green이 아니라 +메커니즘이다 — 6시간 창에 136일 격차는 경쟁이 아니라 산술이다. + +## 남은 것 — 사람이 해야 하는 항목 + +`enforce-target`이 #2776/#2781/#2789에 대해 UI 스크린샷을 요구한다. +세 PR 모두 실제 GUI 변경(각각 3/34/15 파일)을 담고 있으므로 요구가 정당하다. +스크린샷은 사람이 캡처해 PR 설명에 붙여야 한다. diff --git a/devlog/_plan/260901_remote_hub_restack/090_outcome.md b/devlog/_plan/260901_remote_hub_restack/090_outcome.md new file mode 100644 index 0000000000..124e88e2b6 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/090_outcome.md @@ -0,0 +1,98 @@ +# 결과 — remote hub 스택 재스택 + +## 상태 — 최종 (2026-09-01) + +7단계 전부 `dev`에 머지됐다. + +| PR | 브랜치 | 머지 커밋 | +| --- | --- | --- | +| #2771 | design | `278fd613a` | +| #2772 | p1 | `87459f8c3` | +| #2776 | p2 | `39e5aefb6` | +| #2777 | p3 | `fd8b6b895` | +| #2781 | p4 | `163feb6ee` | +| #2786 | p5 | `6d732d3dc` | +| #2789 | p6 | `9232df0e6` | + +### 이 문서가 한 번 틀렸던 것 + +초판은 위 표를 "그린"으로 채웠다. 그 시점의 스냅샷으로는 맞았을지 몰라도, +리뷰 시점의 exact head에서는 #2781과 #2789가 빨갰다. 포커스 검사 통과를 +required CI 통과와 같은 칸에 적은 것이 문제였다 — 둘은 다른 주장이다. + +머지 직전 실제로 겪은 실패는 셋이고 전부 코드 회귀가 아니었다: + +- `tests/server-auth.test.ts`의 websocket refresh 플레이크 — `dev`가 소유한 + 결함. #3147(`408652698`)로 루트에서 고치고 그 위로 재스택했다. +- `Responses previous_response_id state > shutdown drain cap expiry enters the + synchronous spill fallback` — 스택이 건드리지 않는 파일의 부하성 플레이크. + 재실행으로 통과. +- `keyring-smoke=abandoned` — 러너 중단. 집계 잡 `ci`가 이것 때문에 빨갛게 + 보였다. 재실행으로 통과. + +#2776의 스크린샷 게이트는 `gui-screenshot-waived` 라벨로 면제했다(GUI 변경이 +`gui/src/api.ts`와 테스트 2개뿐이라 렌더 변화가 없다). #2789는 면제하지 않고 +실제 스크린샷을 붙였다 — 키 교체 UI는 진짜 화면 변경이다. + +체인 6개 엣지 전부 부모가 자식의 조상이고, PR base ref도 같은 부모를 가리킨다. +오염 커밋 0건. 원본 64커밋 전부 authorship과 메시지를 보존했고, 계약 변경은 +단계마다 조정 커밋으로 분리했다. + +## 닫은 것 + +**설계 계약 5건** — D1 평문 pairing 제거(설정 키까지, 4개 문서), D2 +identity-varying 응답의 validator 제거(서버+클라이언트), D3 Origin verbatim +전달(안전 읽기 허용 보존), D4 로테이션 크래시 복구를 실행 가능한 상태 기계로, +D5 릴레이 응답 no-store. + +**P1 리뷰 스레드 6건** — T1(공개 devlog 프레이밍), T20(미인증 바디 무제한 +버퍼링), T22(process 소유 journal이 연결을 가둠), T25(relay가 항상 throw), +T26(supervised 클라이언트가 disconnect 후 안 돌아옴), T31(abort 실패 시 토큰 +identity). T32도 함께 닫았다. + +**CI 실패 6건** — sync 러너 종료 코드, eslint suppression, 릴레이 pairing +미인증, happy-dom prompt, GUI 테스트 격리, 미선언 관리 라우트 4개 + +capability 미선언. + +## 실제로 스택 문제가 아니었던 것 + +리뷰가 지목한 실패 중 상당수가 상속된 staleness였다. +`release-version-line`, privacy 게이트, `update-stop-first`, +`cli-headless-parity`의 일부는 재스택만으로 사라졌고 각 단계 head에서 +확인했다. + +`server-auth`의 websocket refresh flake는 **dev 자체의 결함**이었다. +dev HEAD도 같은 단언으로 실패한다. 근본 원인(시계 고정 전에 찍히는 두 개의 +타임스탬프)을 찾아 **PR #3147**로 분리했다. 스택에 섞지 않은 이유는 소유가 +dev이기 때문이다. + +## 감사가 바꾼 것 + +로드맵 1차 감사가 FAIL 10건을 냈고 전건 수용했다. 그중 둘이 실제 작업 순서를 +바꿨다: 미해결 인라인 스레드 33건이 로드맵에 아예 빠져 있었고, +블로커 2건이 한 단계씩 늦게 배정돼 있었다(`/api/machine/*`는 p4가 도입, +`api-auth-memory`는 p2가 터치). diff로 실측해 재배정했다. + +설계 수정 1차에 대한 적대적 리뷰도 FAIL을 냈다. D1을 040에서만 지우고 +010/050/070에 계약이 살아 있었고, D4는 새 규칙을 쓰면서 옛 규칙을 안 지워 +문서가 자기모순이었다. 둘 다 리뷰 지적대로 닫았다. + +## 남은 것 — 사람이 해야 함 + +1. **UI 스크린샷** — `enforce-target`이 #2776/#2789에 요구한다. 세 PR 모두 + 실제 GUI 변경을 담고 있어 요구가 정당하다. +2. **리뷰 승인** — 7건 전부 `CHANGES_REQUESTED` 상태다. + `MAINTAINERS.md`가 비저자 메인테이너 승인과 보안 리뷰를 요구하고, + Ingwannu가 유일한 비저자 메인테이너다. CI가 초록이어도 이 상태로는 + 머지 버튼이 열리지 않는다. +3. **P2/Minor 스레드** — T2/T3/T7/T10/T11/T19/T21/T23/T24/T27/T28/T29/T30/T33과 + #2771의 마크다운 린트 6건. 수정하거나 근거를 갖춘 반박을 남기고 resolve한다. + +사용자 요청은 "머지 가능한 정도까지 세팅"이었다. 자동화 게이트 기준으로는 +도달했다. 승인은 우리 손 밖이다. + +## 분리한 PR + +**#3147** `test(auth): seed the pool quota and credential after the clock is pinned` +— `dev` 타깃, 테스트 파일 한 개. 이 스택의 브랜치가 아니라 dev가 소유하는 +flake라서 섞지 않았다. 스택 7건과 독립적으로 리뷰·머지된다. diff --git a/devlog/_plan/260901_remote_hub_restack/100_polish_audit.md b/devlog/_plan/260901_remote_hub_restack/100_polish_audit.md new file mode 100644 index 0000000000..1e392bd9db --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/100_polish_audit.md @@ -0,0 +1,65 @@ +# 최종 폴리싱 감사 — 노출 / 요청 / 롤백 + +요구사항: 기능을 켜지 않은 일반 사용자에게 **UI가 노출되지 않고**, **API 요청이 +발생하지 않으며**, **로컬로 되돌리기 쉬울 것.** + +세 축을 코드로 추적했다. 서버는 깨끗했고, 클라이언트에 실질 위반 세 건이 있다. +전부 이 스택이 도입한 것이고 `dev`에는 없다. + +## 서버 — 위반 없음 + +머신 플레인 라우트(`/api/machine/*`)는 `src/client/machine-listener.ts`가 +서빙하고, 그 리스너는 `src/client/runtime.ts`가 **연결된 클라이언트 롤에서만** +띄운다. standalone 프록시의 `src/server/index.ts`에는 해당 라우트가 아예 없다. + +즉 standalone 사용자의 프로세스는 이 라우트를 열지 않는다. `AGENTS.md`의 +optional-subsystem 원칙과 같은 모양이다 — 켜지 않으면 코드가 돌지 않는다. + +## 위반 1 (요청) — 모든 부팅에서 나가는 discovery 요청 + +`gui/src/App.tsx:113-137`의 `useEffect`가 조건 없이 실행되고, +`gui/src/api-targets.ts:118-131`의 `discoverApiTargets()`가 +`GET /api/machine/status`를 친다. + +standalone에서는 그 라우트가 없으므로 404가 돌아오고 `:126`이 standalone +타깃으로 폴백한다. 동작은 옳다. 그런데 **요청 자체는 나간다.** remote hub를 +켠 적 없는 사용자의 브라우저가 매 로드마다 이 스택이 정의한 엔드포인트를 +한 번씩 두드린다. + +404 폴백은 "기능이 조용하다"가 아니라 "기능이 없다는 것을 매번 물어서 +확인한다"이다. + +## 위반 2 (노출) — 전체 페이지가 discovery 결과 뒤로 밀린다 + +`gui/src/App.tsx:390-393`이 페이지 본문 전체를 `targetsSettled` 뒤에 둔다. +정착 전에는 `connection.discovering`("로컬 및 공유 대상을 확인하는 중…") +배너만 보이고, 대시보드도 프로바이더도 로그도 렌더되지 않는다. + +standalone 사용자에게 이건 자기가 쓰지 않는 기능의 로딩 문구다. 그리고 +`dev`의 App에는 이 게이트가 존재하지 않는다 — 스택이 만든 것이다. + +## 위반 3 (노출) — discovery 실패가 대시보드 전체를 대체한다 + +같은 곳 `:392-393`. `targetError`면 본문 전체가 +`connection.machineUnavailable`("로컬 머신 연결을 사용할 수 없습니다. 공유 +요청을 로컬로 우회하지 않았습니다.")로 대체된다. + +`discoverApiTargets`는 fetch가 **throw할 때** 에러를 던진다(`:123-125`). +프록시가 재시작 중이거나 잠깐 느리면 standalone 사용자가 대시보드 대신 +원격 플레인 이야기를 하는 에러 화면을 본다. 자기가 켠 적 없는 기능 때문에 +쓰던 화면을 잃는 것이다. + +## 롤백 — 재검증 대상 + +`disconnect`의 원상복구 계약은 wp4에서 이미 한 번 고쳤다(process 소유 +journal을 충돌로 오독해 연결이 갇히던 문제). 이번 사이클에서 부분 복구가 +조용히 성공으로 보이지 않는지 재확인한다. + +## 방향 + +서버가 이미 GUI HTML에 세션 메타 태그를 주입한다(`src/server/gui-static.ts:69-75`). +같은 자리에 롤을 실어 보내면 클라이언트는 **묻지 않고도** 자기가 standalone인지 +안다. 요청이 사라지고, 게이트가 사라지고, 에러 화면이 사라진다. + +standalone은 아무것도 하지 않는 것이 기본값이어야 한다. 지금은 아니라고 +확인하는 절차가 기본값이다. diff --git a/devlog/_plan/260901_remote_hub_restack/101_polish_outcome.md b/devlog/_plan/260901_remote_hub_restack/101_polish_outcome.md new file mode 100644 index 0000000000..6cc8729823 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/101_polish_outcome.md @@ -0,0 +1,106 @@ +# 폴리싱 결과 — 노출 / 요청 / 롤백 + +감사(`100`)가 낸 위반 3건과, 적대적 리뷰가 추가로 잡은 4건을 닫았다. +리뷰 verdict는 **FAIL**이었고 내 감사가 불완전했다는 지적이 맞았다. + +## 내가 놓친 것 — 리뷰가 잡음 + +**연결 전 로컬 카탈로그가 복구되지 않았다.** 이게 가장 무거웠고, 사용자가 +말한 "다시 로컬로 롤백"의 정확히 그 지점이다. connect는 +`DEFAULT_CATALOG_PATH`에 있던 것을 덮어쓰는데, 원본 스냅샷을 메모리 +(`priorCatalog`)에만 뒀다. 그건 같은 실행 안에서 실패해 롤백하는 경우만 +커버한다 — disconnect는 다른 날 다른 프로세스다. 영속 상태에는 원격 카탈로그의 +지문만 있어서, disconnect는 원격 카탈로그를 **지우고** "native Codex state was +restored"라고 보고했다. 사용자가 원래 갖고 있던 카탈로그는 그냥 사라진다. + +토큰은 재발급되고 config는 저널에 있다. 카탈로그는 다른 어디에서도 복원할 수 +없는 유일한 아티팩트다. + +**부분 프로필 복구가 성공으로 위장됐다.** `restoreJournalState`가 삼켜진 +unlink 뒤에 `profileRestored = true`를 무조건 세팅했다. 원본 프로필이 없던 +경우 "우리가 만든 걸 지운다"가 실패해도 complete로 보고되고, 그러면 저널이 +지워진다 — 남은 프로필이 우리 것이라는 유일한 기록이. 사용자는 복구됐다는 +말을 듣고, 우리 프로필은 아무도 가리키지 않는 채로 디스크에 남는다. + +**standalone에 두 UI가 더 남아 있었다.** 키 로테이션 컨트롤(모든 API 키에)과 +"Source: local usage.jsonl" 줄. 둘 다 dev에는 없다. + +## 수정 + +### 요청 — 서버가 롤을 말한다 + +서버가 이미 세션 메타를 주입하니, 같은 자리에 `opencodex-runtime-role`을 +싣는다. 세션 블록과 **독립적으로** 내보낸다 — standalone은 GUI 세션을 발급하지 +않으므로, 세션에 묶으면 정작 필요한 경우가 빈다. + +클라이언트는 묻는 대신 읽는다. 태그가 없으면 standalone으로 읽는데, 구버전 +서버·별도 호스팅 GUI·Vite 개발 서버가 전부 여기 해당하고 셋 다 요청을 보내면 +안 되는 쪽이다. + +### 노출 — 기본이 "아무것도 안 함" + +`targetsSettled`가 standalone에서 `true`로 시작한다. 발견할 게 없으니 +기다릴 것도 없다. 그리고 발견 실패는 배너지 대체가 아니다 — 느리거나 재시작 +중인 프록시가 standalone 사용자의 대시보드를 앗아가지 않는다. + +rotation 핸들러는 연결된 런타임에만 전달한다(없으면 섹션이 렌더되지 않는다). +usage source 행도 연결됐을 때만 — "어느 저장소가 이 숫자를 줬나"는 저장소가 +둘일 때만 존재하는 질문이다. + +### 롤백 — 되돌리기지 지우기가 아니다 + +`priorCatalog`를 연결 상태에 영속화하고 disconnect가 되돌려 쓴다. +`""`는 "정말 없었다"라서 제거가 곧 복원이다. 필드가 없는 옛 연결은 기존 +동작을 유지한다 — 복원할 대상이 기록된 적이 없으니 그게 정직하다. +소유권 검사는 그대로다: connect 이후 편집된 카탈로그는 사용자 것이고 +`changed`로 거절한다. 결과에 `catalogRestored`를 더해 두 결과를 구별한다. + +프로필은 **확인된** 제거만 성공으로 친다. ENOENT는 성공인데, 파일이 이미 +없는 것이 제거가 원한 결과이기 때문이다. + +## 검증 + +- GUI 스위트 **1207 pass / 0 fail**. +- 포커스드: client-connect, codex-journal, config, cli-capabilities, + management-route-registry, gui-static, server-management-auth 전부 그린. +- `bun run typecheck`, `bun run lint:gui` 클린. +- 레드-퍼스트 확인: standalone 무요청(0 fetch), 카탈로그 복구, 프로필 계약 + 셋 다 수정 전 실패를 확인한 뒤 적용했다. + +## 정직하게 남기는 것 + +프로필 unlink 실패는 **런타임으로 재현할 수 없다.** unlink를 실패시키려면 Codex +홈에 쓰기를 막아야 하는데, 그러면 같은 함수의 앞선 atomic config 쓰기가 먼저 +던진다. 그래서 그 계약은 source-level로 고정하고 테스트에 이유를 적었다. +조작된 런타임 실패를 만들어내는 것보다 모양을 단언하는 쪽이 증명하는 바가 많다. + +## 리뷰가 지적했으나 이번에 다루지 않은 것 + +- `/healthz`의 `guiPairCapability`, `/readyz`의 프로토콜 메타데이터, 관리 + CORS의 GUI-세션 헤더 광고. UI가 아니라 프로토콜 표면이고, 롤 게이팅이 + 프로토콜 협상 자체를 깨뜨릴 수 있어 별도 판단이 필요하다. +- disconnect의 비트랜잭션성: 카탈로그 충돌 시 config는 복구됐는데 + `runtimeRole=client`가 남는 경로. 에러로 보고되므로 조용한 실패는 아니지만, + 복구 가능한 상태 기계로 만드는 것은 이번 스코프를 넘는다. + +## 커밋 + +| 단계 | 커밋 | 내용 | +| --- | --- | --- | +| p3 | `c5420db86` | 카탈로그 복구 + 프로필 계약 | +| p4 | `4aad8abbf` | 롤 메타 태그, standalone 무요청, 페이지 게이트 제거 | +| p6 | `2349d39e8` | standalone rotation UI + usage source 행 제거 | + +## 최종 체인 + +| 단계 | head | +| --- | --- | +| design | `36992baa9` | +| p1 | `07d7f1006` | +| p2 | `2b36ad496` | +| p3 | `c5420db86` | +| p4 | `4aad8abbf` | +| p5 | `072cc29c3` | +| p6 | `2349d39e8` | + +6개 엣지 전부 부모가 자식의 조상이고, 오염 커밋은 없다. diff --git a/devlog/_plan/260901_remote_hub_restack/102_axis_ledger.md b/devlog/_plan/260901_remote_hub_restack/102_axis_ledger.md new file mode 100644 index 0000000000..52625ce401 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/102_axis_ledger.md @@ -0,0 +1,92 @@ +# 축별 종결 원장 + +폴리싱은 하나의 감사(`100`)와 하나의 적대적 리뷰에서 출발해 세 브랜치에 +수정으로 떨어졌다. goalplan은 축을 work-phase로 쪼개 두었으므로, 각 축이 +어디에서 닫혔는지를 여기 기록한다. + +| 축 | 닫힌 곳 | 증거 | +| --- | --- | --- | +| 부팅 요청 제거 | p4 `4aad8abbf` | `gui/tests/api-targets.test.ts` — standalone 0 fetch (null/standalone/hub), client는 여전히 discovery | +| standalone UI 미렌더 | p4 `4aad8abbf`, p6 `2349d39e8` | GUI 스위트 1207 pass / 0 fail | +| 서버 라우트 폐쇄 | 확인만 (수정 불필요) | `/api/machine/*`는 연결된 클라이언트 리스너 전용, standalone 프록시에 라우트 없음 | +| disconnect 롤백 | p3 `c5420db86` | client-connect 15 pass, codex-journal 25 pass, 둘 다 레드퍼스트 | +| 스택 전파 | p3→p4→p5→p6 | 체인 6엣지 정합, 오염 0 | + +## 서버 축이 수정 없이 닫힌 이유 + +감사 시작 시 가장 걱정한 것이 "standalone 프로세스가 원격 라우트를 연다"였는데, +실측 결과 그렇지 않았다. `/api/machine/*` 핸들러는 +`src/client/machine-listener.ts`에만 있고, 그 리스너는 +`src/client/runtime.ts`가 연결된 클라이언트 롤에서만 띄운다. standalone +프록시의 `src/server/index.ts`에는 그 라우트가 없다. + +`AGENTS.md`의 optional-subsystem 원칙과 같은 모양이다 — 켜지 않으면 코드가 +돌지 않는다. 문제는 서버가 아니라 **클라이언트가 묻는 것**이었다. + +## 남긴 것 + +리뷰가 지적한 두 건은 이번 스코프를 넘어 그대로 둔다: +`/healthz`·`/readyz`·관리 CORS의 프로토콜 메타데이터(롤 게이팅이 프로토콜 +협상을 깨뜨릴 수 있음), disconnect의 비트랜잭션성(복구 상태 기계 신설이 필요). +둘 다 `101`에 이유와 함께 적혀 있다. + +## 축별 검증 커맨드 + +각 축을 닫을 때 실제로 돌린 것. 기록해 두면 다음 사람이 같은 주장을 다시 +확인할 때 무엇을 실행해야 하는지 찾을 필요가 없다. + +| 축 | 커맨드 | +| --- | --- | +| 부팅 요청 | `cd gui && bun test tests/api-targets.test.ts` | +| standalone UI | `cd gui && bun test tests/usage-layout.test.ts tests/apikeys-actions.test.tsx tests/connect-pairing.test.ts` | +| 서버 라우트 | `bun test tests/cli-headless-parity.test.ts tests/management-route-registry.test.ts` | +| 롤백 | `bun test tests/client-connect.test.ts tests/codex-journal.test.ts` | +| 체인 | `git merge-base --is-ancestor`를 6개 엣지에 대해 | + +## 서버 축 판정 근거 (수정 없음) + +`src/client/machine-listener.ts:49-51`이 `/api/machine/*` 라우트를 정의하고, +`src/client/runtime.ts:70`의 `startMachineListener`가 연결된 클라이언트 +상태에서만 그것을 띄운다. standalone 프록시(`src/server/index.ts`)를 grep하면 +해당 경로가 나오지 않는다 — 라우트가 없으므로 인증된 요청도 일반 관리 디스패처를 +거쳐 404가 된다. + +즉 standalone 사용자의 프로세스는 이 표면을 열지 않는다. 고칠 것이 없어서 +이 축은 확인만으로 닫혔다. + +## 롤백 축이 가장 무거웠던 이유 + +사용자가 요구한 세 가지 중 "다시 로컬로 롤백"이 유일하게 **데이터를 잃을 수 +있는** 축이었다. 노출과 요청은 거슬리는 것이고, 롤백 실패는 복구 불가능하다. + +토큰은 재발급할 수 있고 Codex config는 저널에 원본이 있다. 카탈로그만은 +다른 어디에도 사본이 없다 — connect가 덮어쓰고, disconnect가 지우면 끝이다. +그런데 그 상태에서 CLI는 "native Codex state was restored"를 출력했다. + +수정 후에는 connect가 원본을 연결 상태에 실어두고 disconnect가 되돌려 쓴다. +두 결과(`restored` / `removed`)를 구분해 반환하므로, "복구했다"와 +"원래 없었으니 지웠다"가 같은 신호로 뭉뚱그려지지 않는다. + +## 최종 상태 (2026-09-01, 머지 완료) + +위 표는 초판에서 폴리싱 시점 head를 "그린"으로 적었다. 그건 그 스냅샷의 +주장이었고, 리뷰 시점 exact head에서는 #2781과 #2789가 빨갰다. 지금은 +스냅샷이 아니라 머지 결과를 적는다. + +| PR | 머지 커밋 | +| --- | --- | +| #2771 | `278fd613a` | +| #2772 | `87459f8c3` | +| #2776 | `39e5aefb6` | +| #2777 | `fd8b6b895` | +| #2781 | `163feb6ee` | +| #2786 | `6d732d3dc` | +| #2789 | `9232df0e6` | + +분리 PR: **#3147** — `dev`의 websocket flake 근본 수정, `408652698`로 머지. +**#3149** — 이 로드맵 유닛. + +`enforce-target` 2건은 스크린샷 요구였고 서로 다르게 닫혔다. #2776은 +`gui-screenshot-waived` 라벨로 면제했다(`gui/src/api.ts` + 테스트 2개, 렌더 +변화 없음). #2789는 면제 대상이 아니어서 키 교체 UI를 실제로 띄워 캡처하고 +PR 설명에 붙였다. diff --git a/devlog/_plan/260901_remote_hub_restack/110_merge_train_plan.md b/devlog/_plan/260901_remote_hub_restack/110_merge_train_plan.md new file mode 100644 index 0000000000..bf0ba4147d --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/110_merge_train_plan.md @@ -0,0 +1,85 @@ +# 110 — 스택 머지 트레인 계획 (감사 후 정정본) + +초판은 A 게이트 감사에서 fail을 받았다. 두 개의 사실 주장이 틀렸고, 아래는 실제 +로그와 코드로 확인한 정정본이다. 틀린 서술을 지우지 않고 무엇이 왜 틀렸는지 +남긴다 — 다음 사람이 같은 추론을 반복하지 않게. + +## 정정 1: 리뷰어 P1의 인과가 뒤집혀 있다 + +#3147에 걸린 CHANGES_REQUESTED는 선행 테스트 +`expired thread affinity returns 409 for an idle-expired thread`에서 +`updateAccountQuota("pool-a", 10, 5)`가 삭제되어 startup pool-quota prime이 +WHAM 요청을 한 건 더 보내고 `expect(upstreamRequests).toBe(3)`이 깨진다는 +주장이다. 초판은 이것을 그대로 받아 적었다. 틀렸다. + +`src/codex/auth-api.ts:1332-1335`의 stale 판정은 +`!q || Date.now() - q.updatedAt >= POOL_CACHE_TTL` 이다. `dev` 쪽 코드에서 그 +시드는 시계 핀 **이전**에 실행되므로 `updatedAt`에 실제 시각이 찍힌다. 테스트는 +곧이어 `Date.now`를 `1_800_000_000_000`으로 핀한다. 그 차이는 약 136일이고 +`POOL_CACHE_TTL`은 5분이다. 즉 **시드가 있어도 이미 stale이었다.** 삭제는 +`!q`를 false-but-stale에서 true-and-stale로 바꿀 뿐, 같은 가지로 떨어진다. +prime의 fetch 여부는 삭제 전후가 동일하다. + +두 번째로, 그 fetch는 애초에 카운터에 닿지 못한다. `redirectCanonicalCodexTo` +(`tests/server-auth.test.ts:106-117`)는 `hostname === "chatgpt.com"` 이면서 +`pathname`이 `/backend-api/codex`로 시작하는 것만 로컬 `Bun.serve`로 돌린다. +WHAM은 `/backend-api/wham/usage`다(`src/codex/auth-api.ts:1157`). 리다이렉트를 +타지 않으므로 `upstreamRequests`를 증가시킬 수 없다. prime이 아무리 이겨도 +단언은 4를 볼 수 없다. + +리뷰어가 맞은 부분은 한 고리뿐이다: 자격증명이 시드되어 있으므로 prime은 실제로 +`fetchPoolAccountQuota`까지 간다(`auth-api.ts:1360-1362`, `:1201-1202`은 null +`existing`에 early-return 하지 않는다). 그 고리가 단언까지 이어지지 않을 뿐이다. + +## 정정 2: 주석은 실제로 거짓말한다 — 이게 유일한 유효 지적 + +head `ecf51c67`의 `tests/server-auth.test.ts:2132` 주석은 +"`updateAccountQuota` above stamped `updatedAt` with the REAL clock"이라고 +말하는데 above에 그 호출이 없다. 이건 P1이 아니라 문서 위생 문제다. 고쳐야 하지만 +"레이스를 닫는다"는 명분으로 고치면 안 된다. + +따라서 수정은 하되 근거를 바꾼다: 시계 핀 **이후**에 시드를 복원하면 prime이 +처음으로 진짜 fresh를 보고 조용해지고, 주석도 참이 된다. 개선은 맞다. 레이스 +수정은 아니다. + +## 정정 3: #2789는 #3147로 초록이 되지 않는다 + +초판은 세 브랜치가 같은 한 건으로 실패한다고 썼다. 실제 macOS 로그: + +| PR | 결과 | 실패 테스트 | +|----|------|-------------| +| #2777 | 17037 pass / 1 fail | websocket passthrough refreshes pool auth | +| #2781 | 17049 pass / 1 fail | 위와 동일 | +| #2789 | 17098 pass / **2 fail** | 위 + `ocx launcher graceful shutdown > SIGINT to the launcher tears down the Bun proxy` (20069ms 워치독 타임아웃, `tests/shutdown-launcher.test.ts`) | + +#2789는 별개의 타임아웃 플레이크를 하나 더 가지고 있고, `enforce-target`도 +따로 실패한다. 실패 사유는 wrong_base가 아니다 — 로그에 +"Base codex/remote-hub-p5 matches an open PR head; treating as stacked +(skip wrong_base)"가 찍혀 있고, 실제 사유는 "PR quality gate failed: missing UI +screenshot"다. #2789 본문에 GUI 스크린샷이 없다. #2776도 같은 사유다. + +## 정정 4: 순서는 맞지만 "유일"하지 않다 + +더 싼 대안이 있다: 실패한 macOS 잡 세 개를 재실행하는 것. 플레이크니까 통과할 +확률이 높다. 하지만 그건 내구성이 없다 — 다음 푸시에서 다시 진다. #3147을 +`dev`에 넣는 쪽을 택하는 이유는 "유일해서"가 아니라 **루트에서 고치는 게 +여섯 브랜치를 매번 재실행하는 것보다 내구적이어서**다. + +## 확정 실행 순서 + +1. **wp1** #3147: affinity 테스트에 시드 복원(핀 이후) + 주석 정정. 근거는 + 위생, 레이스 아님. `--no-verify` 푸시 → exact-head CI → 리뷰어에게 인과 + 정정을 회신하고 P1 해소 → admin 머지. +2. **wp2** #3143(리뷰어 중복본) 클로즈, #3149 머지. +3. **wp3** 허브 6개 브랜치를 새 `dev` 위로 리베이스. 현재 전부 `dev` 팁 위에 + 있으므로(behind 0) 실제로는 fast-forward 재적층이다. +4. **wp4** #2771부터 순차 머지. 각 자식은 부모가 랜딩하면 `dev`로 재타겟. + #2776 / #2789의 `enforce-target`은 UI 스크린샷 누락이므로 본문에 스크린샷을 + 넣거나 admin 오버라이드로 넘긴다. #2789의 launcher 타임아웃은 별도 플레이크로 + 재실행 대상. +5. **wp5** `dev` 최종 검증. + +## 검증 경계 + +로컬 전체 스위트 금지. 검증은 exact-head 원격 CI. 푸시는 `--no-verify`. +`dev`/`main`/`preview` 직접 푸시 금지 — 모든 랜딩은 PR 머지 경로. diff --git a/devlog/_plan/260901_remote_hub_restack/111_wp1_3147_outcome.md b/devlog/_plan/260901_remote_hub_restack/111_wp1_3147_outcome.md new file mode 100644 index 0000000000..9c7643f866 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/111_wp1_3147_outcome.md @@ -0,0 +1,58 @@ +# 111 — wp1 결과: #3147 시드 복원 + +## 무엇을 했나 + +`codex/ws-refresh-quota-seed-flake` 위에 커밋 `0cf5ef7b5`를 올렸다. 선행 테스트 +`expired thread affinity returns 409 for an idle-expired thread`에 +`updateAccountQuota("pool-a", 10, 5)`를 복원하되, 시계 핀 **이후** +(`Date.now = () => now` 다음, `startServer(0)` 이전)에 놓았다. 그리고 존재하지 +않는 호출을 가리키던 주석을 참인 문장으로 바꿨다. + +## 무엇을 하지 않았나 — 이게 더 중요하다 + +이것을 레이스 수정이라고 기록하지 않았다. 감사가 리뷰어의 인과를 반박했고, +반박이 옳았다: + +- `primeCodexPoolQuotas`의 stale 판정은 + `!q || Date.now() - q.updatedAt >= POOL_CACHE_TTL` + (`src/codex/auth-api.ts:1332-1335`)이다. `dev`에서는 시드가 핀 이전에 돌아 + `updatedAt`에 실제 시각이 찍혔고, 테스트는 `Date.now`를 + `1_800_000_000_000`으로 핀한다. 약 136일 대 5분 TTL — **시드가 있어도 이미 + stale이었다.** 삭제는 같은 `||` 가지 안에서 위치만 바꿨다. +- 설령 prime이 fetch를 해도 카운터에 닿지 못한다. + `redirectCanonicalCodexTo`(`tests/server-auth.test.ts:106-117`)는 + `/backend-api/codex` 접두사만 로컬 `Bun.serve`로 돌리는데, prime의 WHAM 호출은 + `/backend-api/wham/usage`(`auth-api.ts:1157`)다. `upstreamRequests`는 3에서 + 움직일 수 없다. + +리뷰어가 맞은 고리는 하나다: 자격증명이 시드되어 있으므로 prime은 실제로 +`fetchPoolAccountQuota`까지 간다(`:1201-1202`은 null `existing`에 early-return +하지 않는다). 그 고리가 단언까지 이어지지 않을 뿐이다. + +그래서 복원의 근거는 두 가지로 남긴다. 주석이 거짓말을 멈춘다는 것, 그리고 핀 +이후 시드가 prime을 **처음으로** 실제 억제한다는 것. "가끔 지는 레이스를 닫았다"가 +아니다. + +## 검증 + +`bun test tests/server-auth.test.ts` — 91 pass / 0 fail / 618 expect calls, +31.43s. 전체 스위트는 돌리지 않았다(금지). + +exact head `0cf5ef7b5`의 원격 매트릭스는 전부 초록이다: macos, test 1~4/4, +gates, storage policy, api usage, keyring ubuntu/windows/macos, hygiene, +react-doctor, enforce-target, ci. FAILURE 0건. 남은 블로커는 리뷰어의 +CHANGES_REQUESTED 하나뿐이고, 인과 정정은 PR 코멘트로 회신했다. + +## 다음 단계로 넘기는 사실 + +#2789는 이 수정으로 초록이 되지 않는다. macOS 잡이 17098 pass / **2 fail**이고 +두 번째는 `ocx launcher graceful shutdown > SIGINT to the launcher tears down the +Bun proxy`의 20069ms 워치독 타임아웃이다(`tests/shutdown-launcher.test.ts`). + +그리고 #2776과 #2789의 `enforce-target` 실패는 base 문제가 아니라 "missing UI +screenshot"이다. 워크플로에 정식 면제 경로가 있다 — +`.github/workflows/enforce-pr-target.yml:259`의 `gui-screenshot-waived` 라벨을 +`MAINTAINERS.md`에 등재된 사람이 붙이면 그 실패만 걷힌다. 다만 #2789는 +`gui/src/pages/ApiKeys.tsx`, `Usage.tsx` 등 실제 화면을 16개 파일 건드리므로 +면제가 아니라 스크린샷이 맞다. #2776이 건드리는 GUI 파일은 `gui/src/api.ts`와 +테스트 2개뿐이라 면제가 타당하다. diff --git a/devlog/_plan/260901_remote_hub_restack/112_wp2_order_reversal.md b/devlog/_plan/260901_remote_hub_restack/112_wp2_order_reversal.md new file mode 100644 index 0000000000..0996777df0 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/112_wp2_order_reversal.md @@ -0,0 +1,51 @@ +# 112 — wp2에서 순서를 뒤집은 이유 + +계획은 "#3149 문서 PR을 먼저 머지하고 스택을 나중에"였다. 뒤집었다. 이유는 +#3149에 걸린 리뷰 지적 4번이고, 확인해보니 맞았다. + +## 무엇이 문제인가 + +`AGENTS.md`의 보안 작업 규정은 명시적이다: **미수정 결함의 분석은 추적 +디렉터리가 아니라 스크래치에 있어야 한다.** 판정 기준도 적혀 있다 — "이미 이 +약점을 드러내는 공개 diff가 있는가?" 수정이 배포됐으면 공개해도 새로 드러나는 +게 없으니 `_fin/`에 들어간다. 아직이면 그건 사전 공개 자료다. + +#3149가 담고 있는 것: + +- `003_review_thread_ledger.md:41` — T20: `src/server/index.ts:1684`에서 + `Content-Length` 생략 또는 chunked 시 `declaredLength`가 0이 되어 미인증 + 호출자가 무제한 버퍼링을 유발한다. 재현 조건까지 적혀 있는 미인증 DoS다. +- `030_wp3_p2_remote_session.md:41-45` — 같은 내용을 더 자세히. +- 그 외 P1 6건(T1, T20, T22, T25, T26, T31)의 위치와 성격. + +그리고 `dev`의 `src/server/index.ts`에는 `declaredLength`가 **없다.** 수정은 +`codex/remote-hub-p2` 브랜치의 `b7282858b` +"fix(remote-gui): drop plaintext pairing and bound the unauthenticated exchange +body"에 들어 있고, 그 브랜치는 아직 머지되지 않았다. + +즉 계획대로 #3149를 먼저 머지하면, 수정이 없는 상태의 취약점 재현 조건을 +공개 저장소 기본 브랜치에 올리게 된다. 정확히 규정이 막는 행위다. 게다가 +히스토리는 사후에 걷어내기가 실질적으로 불가능하다. + +## 어떻게 바꿨나 + +스택을 먼저 머지한다. `#2771 → #2772 → #2776 → ... → #2789`가 `dev`에 들어가면 +`b7282858b`도 함께 들어가고, 그 시점에 T20은 "공개 diff가 이미 드러낸 약점"이 +된다. 그 다음에야 #3149의 서술이 사전 공개가 아니라 사후 기록이 된다. + +goalplan에 `wp2b`를 추가해 `wp4`(스택 머지)에 의존시켰다. 원래의 `wp2`는 +#3143 정리만 남긴다(완료). + +## 남은 #3149 지적 3건 + +순서와 무관하게 고쳐야 한다. 스택 머지 후 `wp2b`에서 처리한다. + +1. `081_wp8_ci_repairs.md:29-32` — "기본 매개변수는 모듈 평가 시점의 전역을 + 묶는다"는 **틀렸다.** 기본값 초기화식은 호출 시점에 평가된다. 관찰된 실패의 + 실제 원인은 `window`/`globalThis` 렐름 분리이거나 래퍼 설치 시점 문제다. + 틀린 인과를 히스토리로 보존할 수는 없다. +2. `090_outcome.md`와 `102_axis_ledger.md`가 스택을 "그린"이라 부른다. 문서를 + 쓴 시점에는 참이었을지 몰라도 지금 exact head 기준으로 #2781/#2789는 + 빨갛다. 포커스 검사 통과를 required CI 통과와 같게 적으면 안 된다. +3. `260901_merge_train_round3/061_wp7_outcome.md`와 `070_outcome.md`는 이 + 유닛 범위가 아니다. 분리하거나 뺀다. From b27bab041f6c2c89f6d25c54c93df21b1c7e0dc3 Mon Sep 17 00:00:00 2001 From: JUN Date: Tue, 1 Sep 2026 23:55:43 +0900 Subject: [PATCH 096/172] docs(devlog): close out the remote hub merge train (#3159) Co-authored-by: jun --- .../120_merge_train_outcome.md | 62 +++++++++++++++++++ .../121_open_threads_after_merge.md | 42 +++++++++++++ .../122_post_merge_verification.md | 29 +++++++++ .../130_final_state.md | 31 ++++++++++ 4 files changed, 164 insertions(+) create mode 100644 devlog/_plan/260901_remote_hub_restack/120_merge_train_outcome.md create mode 100644 devlog/_plan/260901_remote_hub_restack/121_open_threads_after_merge.md create mode 100644 devlog/_plan/260901_remote_hub_restack/122_post_merge_verification.md create mode 100644 devlog/_plan/260901_remote_hub_restack/130_final_state.md diff --git a/devlog/_plan/260901_remote_hub_restack/120_merge_train_outcome.md b/devlog/_plan/260901_remote_hub_restack/120_merge_train_outcome.md new file mode 100644 index 0000000000..18bcdae0bb --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/120_merge_train_outcome.md @@ -0,0 +1,62 @@ +# 120 — 머지 트레인 최종 결과 + +## 랜딩한 것 + +| PR | 내용 | 커밋 | +| --- | --- | --- | +| #3147 | `dev` websocket refresh 플레이크 근본 수정 | `408652698` | +| #2771 | design — 설계 계약 | `278fd613a` | +| #2772 | p1 — 런타임 롤, `/readyz` 협상 | `87459f8c3` | +| #2776 | p2 — 원격 GUI 세션, pairing, T20 캡 | `39e5aefb6` | +| #2777 | p3 — `ocx connect` 클라이언트 코어 | `fd8b6b895` | +| #2781 | p4 — 머신 리스너, 투플레인 | `163feb6ee` | +| #2786 | p5 — 허브 관리 ingress, 배포 | `6d732d3dc` | +| #2789 | p6 — 키 로테이션, 적대적 게이트 | `9232df0e6` | +| #3149 | 이 로드맵 유닛 | `3275b5a27` | + +#3143은 #3147과 같은 결함의 중복본이라 크레딧을 남기고 닫았다. + +## 순서가 두 번 바뀌었다 + +**첫 번째.** 원래 계획은 스택을 먼저 리베이스하는 것이었는데, 세 브랜치의 +macOS 실패가 `dev`가 소유한 플레이크였다. 스택을 재스택해도 같은 플레이크를 +다시 상속하므로 #3147을 루트에 먼저 넣었다. + +**두 번째, 더 중요한 것.** 문서 PR(#3149)을 먼저 머지하려던 계획을 뒤집었다. +그 문서가 T20 — 미인증 바디 무제한 버퍼링 — 의 재현 조건을 담고 있는데, +수정은 `codex/remote-hub-p2`의 `b7282858b`에 있었고 `dev`에는 없었다. +먼저 머지했다면 수정 없는 상태의 취약점을 공개 기본 브랜치에 올리는 것이었다. +스택을 먼저 넣어 수정이 랜딩한 뒤에 문서를 올렸다. `112_wp2_order_reversal.md`. + +## 감사가 나를 두 번 세웠다 + +A 게이트 감사가 첫 계획에 fail을 냈고 옳았다. 리뷰어가 #3147에 건 P1의 +인과가 뒤집혀 있었다 — 삭제된 quota 시드는 `dev`에서도 이미 stale이었고 +(`auth-api.ts:1332-1335`), prime의 WHAM 호출은 `redirectCanonicalCodexTo`가 +리다이렉트하지 않는 경로라 `upstreamRequests` 카운터에 닿지도 못한다. 시드는 +복원했지만 근거를 "레이스 수정"에서 "주석 정합성 + prime 억제 위생"으로 바꿔 +기록했다. 같은 감사가 #2789에 내가 못 본 두 번째 실패(launcher SIGINT +타임아웃)가 있다는 것도 잡아냈다. + +#3149 리뷰는 문서의 JS 시맨틱 오류를 잡았다. "기본 매개변수는 모듈 평가 +시점의 전역을 묶는다"는 틀렸고, 호출 시점에 평가된다. 실제 원인은 +happy-dom `window` 대 Bun `globalThis` 렐름 분리였다. + +## 머지 직전 빨갛던 것들 + +전부 코드 회귀가 아니었다. + +- `shutdown drain cap expiry enters the synchronous spill fallback` — 스택이 + 건드리지 않는 파일의 부하성 플레이크. 재실행 통과. +- `keyring-smoke=abandoned` — 러너 중단. 집계 잡 `ci`를 빨갛게 만들었다. + 재실행 통과. +- `enforce-target` 2건 — 스크린샷 요구. #2776은 + `gui-screenshot-waived` 라벨로 면제(`gui/src/api.ts` + 테스트뿐). + #2789는 면제하지 않고 실제로 프록시를 띄워 키 교체 UI를 캡처해 붙였다. + +## 검증 경계 + +로컬 전체 스위트는 돌리지 않았다. 실행한 테스트는 +`bun test tests/server-auth.test.ts` 한 파일(91 pass / 0 fail)뿐이고, 나머지 +검증은 전부 exact-head 원격 CI다. 모든 푸시는 `--no-verify`, `dev` 직접 푸시는 +0건 — 아홉 건 전부 PR 머지 경로다. diff --git a/devlog/_plan/260901_remote_hub_restack/121_open_threads_after_merge.md b/devlog/_plan/260901_remote_hub_restack/121_open_threads_after_merge.md new file mode 100644 index 0000000000..566f550742 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/121_open_threads_after_merge.md @@ -0,0 +1,42 @@ +# 121 — 머지 후 남은 리뷰 스레드 + +머지 시점에 `isResolved=false`로 남은 스레드가 있다. 숫자는 이렇다: +#2771 15건, #2776 2건, #2781 4건, #2789 3건. + +이걸 "정리 안 함"으로 적는 게 정직하다. 그리고 왜 지금 정리하지 않는지도. + +## 왜 지금 닫지 않나 + +머지된 PR의 스레드를 사후에 resolve 표시하는 건 기록을 바꿀 뿐 코드를 바꾸지 +않는다. 미해결 표시를 지우면 "닫혔다"는 신호만 남고 실제로 무엇이 처리됐는지는 +오히려 흐려진다. 남겨두면 최소한 다음 사람이 스레드를 읽을 수 있다. + +실질 내용은 이미 처리됐다. P1 6건(T1, T20, T22, T25, T26, T31)은 소유 단계의 +코드 수정으로 닫혔고 그 수정과 함께 랜딩했다 — `003_review_thread_ledger.md`의 +배정표와 각 `0X1_wpN_outcome.md`가 어느 커밋이 어느 스레드를 닫았는지 적고 +있다. 남은 다수는 #2771의 마크다운 린트(MD018/MD022, 테이블 파이프 +이스케이프)와 문서 계약 지적이다. + +## 무엇이 진짜 남았나 + +P2 중 코드가 필요한 건들: + +- T2 — 연결된 GUI에 인증된 models 경로. `/v1/models`가 데이터플레인으로 간다. +- T3 — 관리 ingress에서 GUI health 엔드포인트 보존. +- T19 — 확장된 readiness 응답을 `docs-site/.../cli/lifecycle.md`에 문서화. +- T21 — `hub.managementPublicOrigin`, `remoteGui.allowedTailscaleUsers`, + `remoteGui.allowInsecure*` 문서화. + +이건 새 유닛의 일이지 이 유닛의 잔업이 아니다. 스택은 머지됐고, 위 넷은 +`dev` 위에서 각자의 PR로 처리하는 게 맞다. + +**추적: #3158.** 머지된 PR의 스레드는 닫히면 사실상 사라지므로, 위 넷과 아래 +플레이크를 이슈로 옮겨 적었다. 스레드를 resolve 표시하는 것보다 이쪽이 다음 +사람에게 실제로 도달한다. + +## 별도로 남은 플레이크 + +`ocx launcher graceful shutdown > SIGINT to the launcher tears down the Bun +proxy` (`tests/shutdown-launcher.test.ts`)가 #2789 macOS에서 20069ms 워치독 +타임아웃으로 한 번 졌다. 재실행으로 통과했으므로 머지를 막지 않았지만, 근본 +원인은 보지 않았다. `dev`에 남아 있는 플레이크로 취급해야 한다. diff --git a/devlog/_plan/260901_remote_hub_restack/122_post_merge_verification.md b/devlog/_plan/260901_remote_hub_restack/122_post_merge_verification.md new file mode 100644 index 0000000000..721cbbeda2 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/122_post_merge_verification.md @@ -0,0 +1,29 @@ +# 122 — 머지 후 `dev` 검증 + +9건이 랜딩한 `dev`(`3275b5a27`)가 실제로 정합한지 확인했다. 머지가 성공했다는 +것과 트리가 멀쩡하다는 것은 다른 주장이라서다. + +## 스택 코드가 실제로 있다 + +`src/client/machine-listener.ts`, `src/client/connect.ts`, +`src/client/hub-relay.ts`, `src/routing/compatibility/provider-slot.ts` 전부 +`origin/dev`에 존재한다. T20 캡은 `src/server/index.ts`의 `declaredLength` +2회 참조로 확인된다. + +## 구조 불변식이 살아 있다 + +`AGENTS.md`가 가장 크게 지키라고 적은 두 가지를 좁게 돌렸다: + +``` +bun test tests/core-lab-boundary.test.ts tests/repo-hygiene.test.ts +29 pass / 0 fail / 71 expect() calls +``` + +이건 스타일 검사가 아니다. core-lab boundary는 Lab이 코어 요청 경로로 새어드는 +것을 런타임 import 그래프로 막고, 그 안의 activation-window 스캔은 +`startServer`가 동기로 남아 있는지를 본다. 리모트 허브는 `startServer` 주변에 +라우트와 런타임 롤을 추가하는 스택이므로, 이 둘이 초록인 것이 "코어 경로를 +건드리지 않았다"의 실질 증거다. repo-hygiene은 gitlink와 벤더 클론이 인덱스에 +다시 나타나지 않았음을 본다. + +전체 스위트는 돌리지 않았다(금지). 나머지 검증은 각 PR의 exact-head CI다. diff --git a/devlog/_plan/260901_remote_hub_restack/130_final_state.md b/devlog/_plan/260901_remote_hub_restack/130_final_state.md new file mode 100644 index 0000000000..0860f26012 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/130_final_state.md @@ -0,0 +1,31 @@ +# 130 — 유닛 종료 상태 + +## 이 유닛이 끝낸 것 + +리모트 허브 7단계 스택 + 분리 PR 2건, 총 9건이 `dev`에 랜딩했다. +`dev` HEAD는 `3275b5a27`. 중복본 #3143은 크레딧을 남기고 닫았다. + +검증은 두 층이다. 각 PR의 exact-head 원격 CI, 그리고 머지된 `dev`에서 +`core-lab-boundary` + `repo-hygiene` 29건 통과. 로컬 전체 스위트는 돌리지 +않았고 `dev` 직접 푸시는 0건이다. + +## 남긴 것 + +**#3158** — 머지 시점에 열려 있던 P2 스레드 4건(T2 인증된 models 경로, +T3 관리 ingress의 GUI health, T19 readiness 응답 문서화, T21 신규 config 키 +문서화)과 `shutdown-launcher` 워치독 플레이크. 머지된 PR의 스레드는 사실상 +접근이 끊기므로 이슈로 옮겼다. + +## 이 유닛에서 배운 것 두 가지 + +**리뷰 지적은 결론이 아니라 입력이다.** #3147의 P1은 "삭제된 시드 때문에 +추가 요청이 나가 카운터가 깨진다"였다. 시드를 복원한 건 맞지만 인과는 +틀렸다 — `dev`에서도 이미 stale이었고, 그 요청은 리다이렉트 경로 밖이라 +카운터에 닿지도 못한다. 지적을 그대로 받아 적었다면 존재하지 않는 레이스를 +수정 이력에 남길 뻔했다. 반대로 #3149의 네 지적은 전부 사실이었고, 그중 +하나는 순서를 바꿔야 할 만큼 무거웠다. 매번 확인하는 것 말고 지름길은 없다. + +**공개 순서는 코드 순서와 다른 제약이다.** 문서 PR을 먼저 머지하는 건 기술적으로 +아무 문제가 없다. 문제는 그 문서가 아직 수정되지 않은 취약점의 재현 조건을 +담고 있을 때다. `AGENTS.md`의 기준 — "이미 이 약점을 드러내는 공개 diff가 +있는가" — 은 파일 내용이 아니라 **머지 순서**에 걸리는 제약이다. From c69283129289224c6c116cafafdc65f9370809cf Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 00:02:34 +0900 Subject: [PATCH 097/172] docs(devlog): record the objective closeout and worktree realignment (#3160) * docs(devlog): record how the worktree was realigned after the squash merge * docs(devlog): confirm the objective closed with evidence * docs(devlog): note how the closeout notes reach dev --------- Co-authored-by: jun --- .../131_worktree_state.md | 18 ++++++++++ .../140_objective_closeout.md | 34 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 devlog/_plan/260901_remote_hub_restack/131_worktree_state.md create mode 100644 devlog/_plan/260901_remote_hub_restack/140_objective_closeout.md diff --git a/devlog/_plan/260901_remote_hub_restack/131_worktree_state.md b/devlog/_plan/260901_remote_hub_restack/131_worktree_state.md new file mode 100644 index 0000000000..6036178184 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/131_worktree_state.md @@ -0,0 +1,18 @@ +# 131 — 워크트리 정리 상태 + +유닛이 끝난 뒤 이 워크트리(`/Users/jun/.codex/worktrees/89ca/opencodex`)를 +`dev` 최신(`b27bab041`)에 맞췄다. + +`codex/remote-hub-restack-roadmap`은 #3149로 스쿼시 머지됐다. 스쿼시라 로컬 +브랜치의 39개 커밋이 `dev`의 커밋 하나와 조상 관계를 갖지 않아 fast-forward가 +되지 않는다. 내용은 동일하다 — `git diff origin/dev HEAD -- devlog/`가 빈 +출력이다. + +그래서 리셋 대신 이렇게 했다: + +- `codex/remote-hub-restack-roadmap-archive` — 원래 39커밋 히스토리를 보존. + 스쿼시가 지운 커밋 단위 기록이 필요할 때 여기 있다. +- `codex/remote-hub-closeout` — `origin/dev`에서 새로 시작한 현재 브랜치. + +`git reset --hard`는 쓰지 않았다. 스쿼시 머지 후의 갈라짐은 파괴적 명령으로 +풀 문제가 아니라 브랜치를 하나 더 만들면 되는 문제다. diff --git a/devlog/_plan/260901_remote_hub_restack/140_objective_closeout.md b/devlog/_plan/260901_remote_hub_restack/140_objective_closeout.md new file mode 100644 index 0000000000..1ce1db4c8e --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/140_objective_closeout.md @@ -0,0 +1,34 @@ +# 140 — 목표 종료 확인 + +## 최종 판정: DONE + +열 건이 `dev`에 랜딩했고 한 건은 중복으로 닫았다. `dev` HEAD `b27bab041`. + +| PR | 상태 | +| --- | --- | +| #2771 #2772 #2776 #2777 #2781 #2786 #2789 | MERGED (스택 7단계) | +| #3147 | MERGED (`dev` websocket flake 근본 수정) | +| #3149 | MERGED (로드맵 유닛) | +| #3159 | MERGED (머지 트레인 클로즈아웃) | +| #3143 | CLOSED (#3147과 중복, 크레딧 기록) | + +## 제약 준수 + +- **로컬 전체 스위트 미실행.** 실행한 테스트는 두 번뿐이다: + `bun test tests/server-auth.test.ts`(91 pass, #3147 검증)와 + `bun test tests/core-lab-boundary.test.ts tests/repo-hygiene.test.ts` + (29 pass, 머지 후 `dev` 구조 불변식). 둘 다 파일 지정 포커스 실행이다. +- **모든 푸시 `--no-verify`.** +- **`dev`/`main`/`preview` 직접 푸시 0건.** 열한 건 전부 PR 경로다. + +## 미해결로 남긴 것 (#3158) + +P2 4건 — T2 인증된 models 경로, T3 관리 ingress의 GUI health, T19 readiness +응답 문서화, T21 신규 config 키 문서화. 그리고 `shutdown-launcher`의 +SIGINT 워치독 플레이크. 전부 이 유닛의 잔업이 아니라 다음 유닛의 입력이다. + +## 이 문서의 랜딩 경로 + +`131_worktree_state.md`와 이 문서는 `codex/remote-hub-closeout`에서 작성해 +PR로 `dev`에 올린다. 스쿼시 머지 이후 워크트리를 `origin/dev`에서 다시 시작한 +브랜치라, 여기 커밋은 `dev`와 선형 관계를 갖는다. From 75d9ada92774164fda3a42dad52e5cd5e9d8f5c3 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 00:08:07 +0900 Subject: [PATCH 098/172] docs(devlog): verify the four review fixes reached dev (#3161) Co-authored-by: jun --- .../141_review_fix_verification.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 devlog/_plan/260901_remote_hub_restack/141_review_fix_verification.md diff --git a/devlog/_plan/260901_remote_hub_restack/141_review_fix_verification.md b/devlog/_plan/260901_remote_hub_restack/141_review_fix_verification.md new file mode 100644 index 0000000000..0e9b91bbd5 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/141_review_fix_verification.md @@ -0,0 +1,17 @@ +# 141 — #3149 리뷰 수정 반영 확인 + +리뷰 지적 네 건이 실제로 `dev`에 도달했는지 문서 내용으로 확인했다. 커밋이 +머지됐다는 것과 그 안의 문장이 고쳐졌다는 것은 다른 주장이라서다. + +`origin/dev` (`c69283129`) 기준: + +| 지적 | 확인 방법 | 결과 | +| --- | --- | --- | +| 1. `fetchImpl` 기본값 바인딩 시점 | `081_wp8_ci_repairs.md`에 "호출 시점에" | 3회 | +| 2. 스냅샷 "그린" 주장 | `090_outcome.md`에 "머지 커밋" 표 | 존재 | +| 3. 범위 외 파일 | `260901_merge_train_round3/070_outcome.md` | 없음 | +| 4. 사전 공개 보안 상세 | `003_review_thread_ledger.md`에 공개 시점 헤더 | 존재 | + +4번이 가장 중요하다. 헤더만 추가한 게 아니라 **머지 순서를 바꿔서** 해결했다. +T20 캡(`b7282858b`)이 #2776으로 `dev`에 들어간 뒤에야 그 재현 조건을 적은 +문서가 올라갔다. 헤더는 그 순서를 기록할 뿐이고, 실제 안전장치는 순서다. From 7ac4c9f7e6205690ce12bf6d801dd054f9840ff5 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 00:13:12 +0900 Subject: [PATCH 099/172] docs(devlog): close the remote hub restack unit (#3162) Co-authored-by: jun --- .../150_unit_closed.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 devlog/_plan/260901_remote_hub_restack/150_unit_closed.md diff --git a/devlog/_plan/260901_remote_hub_restack/150_unit_closed.md b/devlog/_plan/260901_remote_hub_restack/150_unit_closed.md new file mode 100644 index 0000000000..eeb38a1997 --- /dev/null +++ b/devlog/_plan/260901_remote_hub_restack/150_unit_closed.md @@ -0,0 +1,29 @@ +# 150 — 유닛 종료 + +리모트 허브 재스택 유닛을 닫는다. `dev` HEAD `75d9ada92`. + +## 랜딩 총계 + +열두 건이 머지됐고 한 건을 중복으로 닫았다. + +- 스택 7단계: #2771 #2772 #2776 #2777 #2781 #2786 #2789 +- `dev` 플레이크 근본 수정: #3147 +- 문서: #3149 #3159 #3160 #3161 +- 중복 정리: #3143 (CLOSED) + +## 남은 것 + +**#3158** — P2 4건(T2 인증된 models 경로, T3 관리 ingress GUI health, +T19 readiness 문서화, T21 config 키 문서화)과 `shutdown-launcher` SIGINT +워치독 플레이크. + +## 이 유닛이 남긴 판단 두 개 + +**리뷰 지적을 검증 없이 반영하지 않는다.** #3147의 P1은 인과가 뒤집혀 있었고 +(`auth-api.ts:1332-1335`에서 `dev`도 이미 stale, WHAM은 리다이렉트 경로 밖), +그대로 받아 적었다면 없는 레이스를 수정 이력에 남길 뻔했다. 반대로 #3149의 +네 지적은 전부 사실이었다. 구분하는 방법은 매번 코드를 보는 것뿐이다. + +**공개 순서는 코드 순서와 별개 제약이다.** 문서가 미수정 취약점의 재현 조건을 +담고 있으면, 그 문서의 머지는 수정의 머지 이후여야 한다. `AGENTS.md`의 +"이미 공개 diff가 드러냈는가" 기준은 파일 내용이 아니라 순서에 걸린다. From 2f2629f2744ad37fe0040e9ed920646dc17173cc Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 00:38:03 +0900 Subject: [PATCH 100/172] docs(skill): teach ocx the remote hub roles, pairing boundary, and disconnect halves (#3165) An agent reading skills/ocx had no way to answer two questions it will be asked. The first is whether configuring a hub requires pairing. It does not. GET /opencodex-session mints a session on its own for a loopback request, and for a hub reached through the trusted Tailscale ingress when the login is in remoteGui.allowedTailscaleUsers (src/server/gui-session.ts:177-193). A pairing grant is the fallback for a remote browser that neither position nor identity vouches for, and the management API is a separate admission ladder again (src/server/management-auth.ts:471-491) which an agent drives with the admin token and never pairs at all. The second is that ocx disconnect is only half of leaving a hub. It restores local state and clears the connection, then reports that the hub key is still valid -- stopping there leaves a working credential behind. Revocation is the other half: connect revoke while still connected, or delete the key in the hub dashboard once the device is gone. Also records why rotation is a two-step commit (the old key has to outlive the moment the new one is issued, or a client that has not been updated is stranded), why the four disconnect refusals are the safety property rather than an obstacle, and that credentials are stdin-only with no argv form. references/05_remote_hub.md carries the detail; SKILL.md carries the two corrections inline so an agent that never opens the reference still answers correctly. The test's reference list grows by one entry. Co-authored-by: jun --- skills/ocx/SKILL.md | 26 ++++ skills/ocx/references/05_remote_hub.md | 163 +++++++++++++++++++++++++ tests/skill-ocx.test.ts | 10 +- 3 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 skills/ocx/references/05_remote_hub.md diff --git a/skills/ocx/SKILL.md b/skills/ocx/SKILL.md index cba29e7dd9..a9975e7745 100644 --- a/skills/ocx/SKILL.md +++ b/skills/ocx/SKILL.md @@ -106,6 +106,31 @@ Report the count and bytes from that output and get explicit approval before add `--mode quarantine` (the default) can be undone with `storage trash restore`; `--mode permanent` cannot. +## Remote hub: two things agents get wrong + +**Pairing is not hub setup.** Configuring a hub — providers, accounts, routing, keys — never +needs a pairing code. `GET /opencodex-session` mints a session by itself for a loopback +request, and for a `hub` reached over the trusted Tailscale ingress when the login is in +`remoteGui.allowedTailscaleUsers`. A pairing grant is the fallback for a remote browser that +neither position nor identity vouches for. The management API is a separate ladder again: an +agent driving a hub uses the admin token and never pairs. When a human asks "do I have to pair +to set this up?", the answer is no. + +**`ocx disconnect` is only half of leaving a hub.** It restores local state and clears the +connection, then tells you the hub key is still valid. Revoke it too: `ocx connect revoke +--admin-token-stdin` while still connected, or delete the key in the hub dashboard under +Integrations → API Keys once the device is gone. Stopping after `disconnect` leaves a working +credential behind. + +Credentials for these commands are stdin-only — `--pairing-code-stdin` and +`--admin-token-stdin`. There is no argv or environment form, and that is deliberate. + +When `disconnect` refuses, do not route around it. Each refusal means the unwind cannot be +proven safe: another process owns the token, no journal records the pre-connect state, a +different client key owns the journal, or the restore was only partial. + +Details, including key rotation's two-step commit: `references/05_remote_hub.md`. + ## References | File | Use it for | @@ -114,6 +139,7 @@ cannot. | `references/02_json_shapes.md` | response envelopes and error shapes | | `references/03_recipes.md` | copy-paste sequences for real tasks | | `references/04_failure_semantics.md` | exit codes, 503 classes, what to retry | +| `references/05_remote_hub.md` | hub/client roles, when pairing is and is not needed, key rotation, disconnection | `01_management_surface.md` is generated by `scripts/generate-ocx-skill-surface.ts` and a test fails if the committed copy drifts from the capability table. When it and the running binary disagree, diff --git a/skills/ocx/references/05_remote_hub.md b/skills/ocx/references/05_remote_hub.md new file mode 100644 index 0000000000..9951247a6f --- /dev/null +++ b/skills/ocx/references/05_remote_hub.md @@ -0,0 +1,163 @@ +# Remote hub: roles, sessions, and disconnection + +The remote hub lets one machine hold the models and credentials while other machines +and browsers use them. Three questions come up constantly, and two of them have +answers that are easy to guess wrong. + +## Which parts need pairing (the common misconception) + +**Pairing is not how you configure a hub.** It is how a *remote browser* gets a session +when it cannot be trusted by position or identity. Configuring the hub itself — providers, +accounts, routing, keys — never requires a pairing code. + +`GET /opencodex-session` mints a session on its own in two cases (`src/server/gui-session.ts`): + +| Situation | What happens | +|---|---| +| API auth not required, request is loopback, origin allowed | Session minted, source `loopback`. This is the ordinary local dashboard. | +| Role is `hub`, request arrived through the trusted Tailscale ingress over HTTPS, the login is in `remoteGui.allowedTailscaleUsers`, and the browser origin is allowed | Session minted, source `tailscale-identity`. No pairing code involved. | +| Anything else | `null` — the browser gets 401 and must exchange a pairing grant at `POST /opencodex-session`. | + +So a pairing code is the fallback for the third row only. If the operator is sitting at +the hub, or their Tailscale identity is on the allow-list, there is nothing to pair. + +The management API has its own admission ladder, independent of the browser session +(`src/server/management-auth.ts` `resolveManagementAdmission`). In order: process-scoped +local capabilities, then the GUI-pair capability, then the admin token, then a GUI session. +An agent driving the hub over the management API uses the admin token and never touches +pairing at all. + +**Answer the question directly when a human asks it:** no, the hub dashboard does not need +pairing to be set up. Pairing exists so a browser on *another* machine can get in when +neither loopback position nor Tailscale identity vouches for it. + +## Roles + +`runtimeRole` is one config key with three values, and it decides whether remote code runs at all. + +| Role | Meaning | +|---|---| +| `standalone` (default) | No hub UI renders and no machine-plane request is issued. The feature is absent, not merely disabled — `gui/tests/api-targets.test.ts` pins zero requests at boot. | +| `hub` | Holds models and credentials. Other machines connect to it. | +| `client` | Connected to a hub. `ocx connect` puts a machine in this role. | + +Minimum hub config: + +```json +{ + "runtimeRole": "hub", + "hub": { "managementPublicOrigin": "https://host.ts.net" } +} +``` + +`managementPublicOrigin` is the origin a browser actually reaches, which is the outside +address when a TLS terminator or reverse proxy sits in front. `/readyz` advertises it as +`managementUrl`. + +Optional management-only listener: + +```json +"hub": { + "managementPublicOrigin": "https://host.ts.net", + "managementIngress": { "enabled": true, "port": 10120 } +} +``` + +The socket is always bound to `127.0.0.1` — the hostname is deliberately not configurable. +Only GUI, session bootstrap, and management API routes are admitted; the data plane is not. + +## Commands + +Credentials are accepted **only** through stdin. The CLI says so itself: "argv and +environment credential forms are not supported." Do not construct a command that puts a +secret in argv; there is no flag for it and adding one would defeat the design. + +| Command | Purpose | +|---|---| +| `ocx connect --pairing-code-stdin` | Join a hub with a one-time pairing code | +| `ocx connect --admin-token-stdin` | Join a hub with the hub admin token (automation) | +| `ocx connect status [--json]` | Inspect the connection | +| `ocx connect rotate --pairing-code-stdin` | Rotate this client's data key | +| `ocx connect revoke --admin-token-stdin` | Kill this client's key at the hub — works only while connected | +| `ocx disconnect [--keep-catalog]` | Restore local state and clear the connection | +| `ocx gui` | Open the dashboard | +| `ocx gui pair --origin ` | Issue a pairing grant for a remote browser | + +Connect flags: `--clients codex,claude` (which client configs to point at the hub), +`--management-url ` (when management lives at a different address), +`--management-transport direct|relay` (`relay` tunnels management over the data +connection when no management port can be opened), `--no-sync` (connect without pulling +the catalog). + +`ocx gui pair` refuses an origin that is not in `hub.managementPublicOrigin` or +`corsAllowOrigins`. Grants are single-use, expire in five minutes, are origin-bound, +stored as digests, and rate-capped at 8/min. They are secrets: do not persist one. + +## Reading `ocx connect status` + +Disconnected is a single line. Connected prints hub, management URL and transport, +protocol version, API key id, selected clients, and three health fields worth checking: + +| Field | What a non-nominal value means | +|---|---| +| `Token file` | `owned` is nominal. `changed` means another process overwrote the token, and `disconnect` will refuse until that is resolved. | +| `Key rotation` | `recovery-required` means a rotation was interrupted. Re-run `connect rotate` to commit or abort it. | +| `Catalog` | `unsafe` means the catalog bytes are not the ones this connection wrote. | + +## Key rotation is a two-step commit + +Starting a rotation issues the new key while **the old key stays valid**. The dashboard +says so and offers exactly two exits: commit, or abort. + +The ordering is not ceremony. If the old key died at issuance, a client that had not yet +received the new key would be disconnected — and a disconnected client cannot be given a +new key. So the contract is: apply the new key, verify the connection, then commit. + +The token backup (`.prev`) is not deleted while a rotation is in flight, and +commits only once both sides are confirmed to have accepted. + +## Disconnection happens in two places + +This is the part that is most often done halfway. + +`ocx disconnect` is **local only**. It restores the pre-connect Codex config from the +journal, removes the service token, and clears the hub catalog (`--keep-catalog` keeps +it). It then tells you plainly that the hub key is still valid and must be revoked from +Integrations → API Keys. + +Revocation is the other half: + +- **Device still connected:** `ocx connect revoke --admin-token-stdin`, then `ocx disconnect`. + `revoke` only works while connected, so it comes first. +- **Device lost, already disconnected, or unreachable:** delete the key in the hub + dashboard under Integrations → API Keys. + +To return the hub itself to a normal install, set `runtimeRole` to `standalone` and +restart. Leftover `hub` and `remoteGui` blocks are inert outside the hub role. + +A remote browser logging itself out (`/api/session/logout`) is a third, separate action. +It ends a browser session; it does not disconnect a client or revoke a key. + +### When `disconnect` refuses, that is the safety property + +Do not work around these. Each one means unwinding would damage state that +`disconnect` cannot prove is safe to touch. + +| Refusal | Cause | +|---|---| +| `service token ownership changed` | Another process owns the token file. Disconnecting now would unwind someone else's state. | +| `Codex routing is injected but no journal records the original state` | There is no recorded baseline, so restoring would be a guess. | +| `Codex journal ownership conflicts with the connected key` | A different client key owns the journal; that client must disconnect. | +| `Codex journal restore was partial` | A half-restore is not reported as success. | + +## What to tell a human who asks + +- *"Do I need to pair to set up the hub?"* No. Pairing is only for a remote browser that + is neither on loopback nor covered by `remoteGui.allowedTailscaleUsers`. +- *"I ran `ocx disconnect`, am I done?"* Not yet — the hub key is still valid. Revoke it + at the hub, or delete it from Integrations → API Keys. +- *"Why does rotation need two steps?"* Because the old key must outlive the moment the + new one is issued, or a client that has not yet been updated is stranded. +- *"Why is there no remote UI on my machine?"* Expected — `runtimeRole` is not `hub`. +- *"Can I pass the pairing code as an argument?"* No. Credentials are stdin-only by design. + diff --git a/tests/skill-ocx.test.ts b/tests/skill-ocx.test.ts index 37edcfb168..41ddfb6af3 100644 --- a/tests/skill-ocx.test.ts +++ b/tests/skill-ocx.test.ts @@ -17,14 +17,20 @@ import { CLI_COMMANDS } from "../src/cli/registry"; */ const SKILL_DIR = join(import.meta.dir, "..", "skills", "ocx"); const SKILL = join(SKILL_DIR, "SKILL.md"); -const REFERENCES = ["01_management_surface.md", "02_json_shapes.md", "03_recipes.md", "04_failure_semantics.md"]; +const REFERENCES = [ + "01_management_surface.md", + "02_json_shapes.md", + "03_recipes.md", + "04_failure_semantics.md", + "05_remote_hub.md", +]; function read(file: string): string { return readFileSync(join(SKILL_DIR, file), "utf8"); } describe("skills/ocx structure", () => { - test("SKILL.md and all four references exist", () => { + test("SKILL.md and every reference exist", () => { expect(existsSync(SKILL)).toBe(true); for (const ref of REFERENCES) { expect(existsSync(join(SKILL_DIR, "references", ref)), ref).toBe(true); From e236c36239c93f006a706aba3e7c84da167b5dd9 Mon Sep 17 00:00:00 2001 From: ingwannu Date: Wed, 2 Sep 2026 01:04:38 +0900 Subject: [PATCH 101/172] fix(catalog): read Copilot context window limits (#3163) Co-authored-by: Ingwannu --- src/codex/catalog/provider-fetch.ts | 6 ++++ tests/codex-catalog.test.ts | 43 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/codex/catalog/provider-fetch.ts b/src/codex/catalog/provider-fetch.ts index 5ffc357cb1..bc86c8c6e4 100644 --- a/src/codex/catalog/provider-fetch.ts +++ b/src/codex/catalog/provider-fetch.ts @@ -1195,9 +1195,15 @@ export function catalogHintsFromModelsApiItem(providerName: string, item: Provid const metadata = plainRecord(item.metadata); const capabilityRecord = plainRecord(metadata?.capabilities) ?? plainRecord(item.capabilities); const limits = plainRecord(metadata?.limits); + const capabilityLimits = plainRecord(plainRecord(item.capabilities)?.limits); const contextWindow = positiveSafeInteger( limits?.max_context_length, + // GitHub Copilot reports the live context window here instead of in the metadata or + // top-level fields used by other OpenAI-compatible catalogs (#3156). Keep the existing + // metadata field authoritative when both are present: adding this provider-specific + // fallback must not change previously recognized providers. + capabilityLimits?.max_context_window_tokens, metadata?.context_length, item.context_length, item.context_size, diff --git a/tests/codex-catalog.test.ts b/tests/codex-catalog.test.ts index 05223ca1e5..143dc8a986 100644 --- a/tests/codex-catalog.test.ts +++ b/tests/codex-catalog.test.ts @@ -5366,6 +5366,49 @@ describe("Codex catalog routed normalization", () => { expect(routed?.context_window).toBe(64_000); }); + test("GitHub Copilot capabilities preserve the live context window (#3156)", async () => { + globalThis.fetch = (async () => new Response(JSON.stringify({ + data: [{ + id: "copilot-wide-model", + capabilities: { + limits: { max_context_window_tokens: 1_000_000 }, + }, + }, { + id: "copilot-existing-metadata", + metadata: { limits: { max_context_length: 256_000 } }, + capabilities: { + limits: { max_context_window_tokens: 1_000_000 }, + }, + }, { + id: "copilot-invalid-window", + capabilities: { + limits: { max_context_window_tokens: -1 }, + }, + }], + }))) as typeof fetch; + + const models = await gatherRoutedModels({ + port: 10100, + defaultProvider: "github-copilot", + providers: { + "github-copilot": { + adapter: "openai-chat", + baseUrl: "https://api.githubcopilot.com", + apiKey: "sk-test", + }, + }, + }); + const routed = buildCatalogEntries(nativeTemplate(), [], models) + .find(entry => entry.slug === "github-copilot/copilot-wide-model"); + + expect(models.find(model => model.id === "copilot-wide-model")?.contextWindow).toBe(1_000_000); + expect(routed?.context_window).toBe(1_000_000); + expect(routed?.max_context_window).toBe(1_000_000); + expect(routed?.auto_compact_token_limit).toBe(900_000); + expect(models.find(model => model.id === "copilot-existing-metadata")?.contextWindow).toBe(256_000); + expect(models.find(model => model.id === "copilot-invalid-window")?.contextWindow).toBeUndefined(); + }); + test("liveModels false preserves configured catalog metadata without live fetch", async () => { let fetchCalls = 0; globalThis.fetch = (() => { From 75090d4e0e26637a3db0157edf3090830ba00d52 Mon Sep 17 00:00:00 2001 From: ingwannu Date: Wed, 2 Sep 2026 01:04:49 +0900 Subject: [PATCH 102/172] fix(codex): preserve request-owned main pins (#3166) Co-authored-by: Ingwannu --- src/codex/auth-context.ts | 43 +++++++++++++- structure/08_openai-provider-tiers.md | 23 +++++++ tests/codex-auth-context.test.ts | 86 +++++++++++++++++++++++++++ 3 files changed, 150 insertions(+), 2 deletions(-) diff --git a/src/codex/auth-context.ts b/src/codex/auth-context.ts index 812cadd22f..ac12ca7081 100644 --- a/src/codex/auth-context.ts +++ b/src/codex/auth-context.ts @@ -25,7 +25,9 @@ import { isNativeMainTrafficBlocked, nativeMainStartupGateSnapshot } from "./nat import type { NativeMainStartupBlockReason } from "./native-profile-startup"; import { codexQuotaScopeForModel, + computeCodexUsageScore, getCodexQuotaHealthSnapshot, + isEffectiveCodexAccountPinned, releaseCodexQuotaProbeLease, releaseCodexQuotaScopeProbeLease, tryAcquireCodexQuotaProbeLease, @@ -42,7 +44,7 @@ import { ACCOUNT_GATED_NATIVE_OPENAI_MODELS } from "./catalog/native-models"; import type { CodexCooldownSource, CodexQuotaScope } from "./routing"; import { maskAccountId } from "../lib/privacy"; import { formatErrorResponse } from "../bridge"; -import { getAccountQuota } from "./quota"; +import { CODEX_UNKNOWN_USAGE_SCORE, getAccountQuota } from "./quota"; import type { CodexAccountMode, OcxConfig, OcxProviderConfig } from "../types"; import { FORWARD_HEADERS } from "../adapters/openai-responses"; import { captureConfigGeneration } from "../lib/state-store-sweeper"; @@ -52,6 +54,21 @@ import { extractAccountId } from "../oauth/chatgpt"; const CODEX_AFFINITY_COMPONENT_MAX_BYTES = 512; const CODEX_APP_AFFINITY_KEY = randomBytes(32); +/** + * A request-owned bearer cannot inspect the physical main credential for its plan, but cached + * WHAM usage is still valid routing evidence for the same logical main account. Score it with + * the conservative unknown-plan rule: an unobserved governing window preserves the pin, while + * any known weekly/monthly/short value at the threshold releases it through the ordinary Pool + * path. This keeps the keyring boundary intact instead of reading auth.json just to classify a + * request that already brought its own credential (#3157). + */ +function requestOwnedMainPinHasQuotaHeadroom(config: OcxConfig): boolean { + const threshold = config.autoSwitchThreshold ?? 80; + if (threshold <= 0) return true; + const usage = computeCodexUsageScore(getAccountQuota(MAIN_CODEX_ACCOUNT_ID)); + return usage >= CODEX_UNKNOWN_USAGE_SCORE || usage < threshold; +} + function boundedCodexAffinityComponent(value: string | null): string | undefined { const normalized = value?.trim(); if (!normalized) return undefined; @@ -372,6 +389,12 @@ export async function resolveCodexAuthContext( const requestScopedMainCredential = options.requestScopedMainCredential === true && hasCallerCodexBearer(headers); const fixedAccountId = options.accountId; + const preserveRequestOwnedMainPin = requestScopedMainCredential + && fixedAccountId === undefined + && config.activeCodexAccountPinned === MAIN_CODEX_ACCOUNT_ID + && isEffectiveCodexAccountPinned(config) + && !isCodexAccountPaused(config, MAIN_CODEX_ACCOUNT_ID) + && requestOwnedMainPinHasQuotaHeadroom(config); if (fixedAccountId !== undefined && options.excludeAccountId !== undefined) { throw new Error("Codex auth context cannot select and exclude an account simultaneously"); } @@ -425,6 +448,19 @@ export async function resolveCodexAuthContext( directSelectionAdmission.release(); } }; + // Pool discovery excludes request-owned main credentials by design: they must never be folded + // into stored-account entitlement, affinity, or persistence state. An effective manual main pin + // is the one exception where that exclusion is selection evidence in the opposite direction. + // Validate the caller's own gated-model roster before using it, and fall through to a Pool model + // detour when it lacks the grant. This branch performs no physical-main credential read. + if (preserveRequestOwnedMainPin) { + const callerEntitled = !options.modelId + || !ACCOUNT_GATED_NATIVE_OPENAI_MODELS.has(options.modelId) + || await ( + options.isDirectCallerEntitledToCodexModel ?? isDirectCallerEntitledToCodexModel + )(headers, options.modelId); + if (callerEntitled) return { kind: "main", accountId: null }; + } // An explicit namespace binding is stronger than the provider's default mode. It must use the // selected stored credential even while the canonical OpenAI provider is globally Direct. // A request-owned bearer is deliberately not represented as `main-pool`: Pool account ids own @@ -473,7 +509,10 @@ export async function resolveCodexAuthContext( // it. Retained recovery makes main wholly ineligible so pool routing continues. nativeMainSelectionOnly, isMainAccountTokenLive: requestScopedMainCredential - ? () => false + // Main stays excluded from this request's model roster below. This synthetic liveness is + // consulted only by shared-state preservation, so a caller-owned pin survives a model + // detour without reading or selecting the physical main credential. + ? () => preserveRequestOwnedMainPin : options.isMainAccountTokenLive, modelEligibleAccountIds, }; diff --git a/structure/08_openai-provider-tiers.md b/structure/08_openai-provider-tiers.md index ab15088fb4..d563f4989f 100644 --- a/structure/08_openai-provider-tiers.md +++ b/structure/08_openai-provider-tiers.md @@ -108,6 +108,29 @@ pinned account and the effective active account are different questions, and the both (`pinned` and `pinnedAccountId`). A surface that marks only the active account loses the pin from view exactly when it is doing the most work — suppressing every higher tier. +A keyring-backed Codex request can carry its own forwardable ChatGPT bearer while the provider remains +in Pool mode. When the effective manual pin is `__main__`, main is not paused, and its cached quota still +has headroom, auth resolution validates the caller bearer's own gated-model roster and uses that +request-owned credential before stored-Pool selection. The credential never enters Pool persistence, +affinity, entitlement cache, or health state, and this decision never reads the physical main credential. +If the caller lacks the requested model, a stored-account model detour may serve the request without +clearing the healthy shared main pin. A paused or quota-drained main skips this exception and follows the +ordinary Pool promotion path. + +[Decision Log] +- 목적과 의도: Keep an explicit healthy main selection from being replaced by an exhausted stored + account merely because the client supplied main through a request-owned keyring bearer. +- 기존 구현 및 제약 조건: Request-owned credentials are deliberately excluded from stored-account + entitlement discovery, but shared-state preservation interpreted that exclusion as a dead main login. +- 검토한 주요 대안: Persist the caller credential, read the physical main token for identity, ignore + the manual pin, or validate the caller independently before stored-Pool selection. +- 선택한 방식: Use only the effective pin, pause state, cached quota, and the caller credential's own + gated-model check; synthesize shared-state liveness only while main stays request-ineligible. +- 다른 대안 대신 이 방식을 선택한 이유: It preserves credential isolation and explicit operator + intent without admitting an unentitled model or binding an ephemeral bearer into durable Pool state. +- 장점, 단점 및 영향: Healthy main pins survive keyring requests and model-only detours; cached quota + remains the only proactive drain evidence available without crossing the physical credential boundary. + ```text gpt-5.6-sol # openai; Pool or Direct follows the provider option main/gpt-daybreak-blue-latest # openai; observed account-native Daybreak, Sol capability metadata diff --git a/tests/codex-auth-context.test.ts b/tests/codex-auth-context.test.ts index 93653a3002..9a424f1b32 100644 --- a/tests/codex-auth-context.test.ts +++ b/tests/codex-auth-context.test.ts @@ -47,6 +47,7 @@ import { handleCodexAuthAPI, isAccountNeedsReauth, markAccountNeedsReauth, + setAccountQuotaFromParsed, } from "../src/codex/auth-api"; import { __resetGuardianState, guardianSweep } from "../src/oauth/token-guardian"; import { @@ -1088,6 +1089,91 @@ describe("Codex auth context", () => { clearAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID); } }); + + async function resolveRequestOwnedMainPinCase(options: { + mainWeeklyPercent: number; + poolWeeklyPercent: number; + callerEntitled: boolean; + }): Promise<{ + cfg: OcxConfig; + context: Awaited>; + directEntitlementChecks: number; + }> { + const cfg = config(); + cfg.accountPoolStrategy = "quota"; + cfg.autoSwitchThreshold = 90; + cfg.activeCodexAccountId = MAIN_CODEX_ACCOUNT_ID; + cfg.activeCodexAccountPinned = MAIN_CODEX_ACCOUNT_ID; + cfg.codexAccountPriorities = { + [MAIN_CODEX_ACCOUNT_ID]: 0, + "pool-a": 0, + }; + resetCodexRoutingForManualSelection(MAIN_CODEX_ACCOUNT_ID); + saveCodexAccountCredential("pool-a", { + accessToken: "pool-token", + refreshToken: "pool-refresh", + expiresAt: Date.now() + 5 * 60_000, + chatgptAccountId: "pool-account", + }); + setAccountQuotaFromParsed(MAIN_CODEX_ACCOUNT_ID, { weeklyPercent: options.mainWeeklyPercent }); + setAccountQuotaFromParsed("pool-a", { weeklyPercent: options.poolWeeklyPercent }); + let directEntitlementChecks = 0; + const context = await resolveCodexAuthContext(new Headers({ + authorization: "Bearer caller-keyring-token", + "chatgpt-account-id": "caller-keyring-account", + }), cfg, "pool", { + requestScopedMainCredential: true, + modelId: "gpt-5.6-sol", + isDirectCallerEntitledToCodexModel: async () => { + directEntitlementChecks += 1; + return options.callerEntitled; + }, + resolveCodexModelEntitlements: async () => ({ + modelsByAccount: new Map([["pool-a", new Set(["gpt-5.6-sol"])]]), + clientVersionByAccount: new Map([["pool-a", "0.150.1"]]), + confirmedAccountIds: new Set(["pool-a"]), + credentialIdentities: new Map([["pool-a", "pool:1:pool-account"]]), + }), + }); + return { cfg, context, directEntitlementChecks }; + } + + test("a healthy manual main pin keeps the validated caller bearer ahead of an exhausted pool account (#3157)", async () => { + const { cfg, context, directEntitlementChecks } = await resolveRequestOwnedMainPinCase({ + mainWeeklyPercent: 16, + poolWeeklyPercent: 100, + callerEntitled: true, + }); + expect(context).toMatchObject({ kind: "main", accountId: null }); + expect(directEntitlementChecks).toBe(1); + expect(cfg.activeCodexAccountId).toBe(MAIN_CODEX_ACCOUNT_ID); + expect(cfg.activeCodexAccountPinned).toBe(MAIN_CODEX_ACCOUNT_ID); + }); + + test("an exhausted request-owned main pin still yields to the healthy Pool account (#3157)", async () => { + const { cfg, context, directEntitlementChecks } = await resolveRequestOwnedMainPinCase({ + mainWeeklyPercent: 100, + poolWeeklyPercent: 16, + callerEntitled: true, + }); + expect(context).toMatchObject({ kind: "pool", accountId: "pool-a" }); + expect(directEntitlementChecks).toBe(0); + expect(cfg.activeCodexAccountId).toBe("pool-a"); + expect(cfg.activeCodexAccountPinned).toBeUndefined(); + }); + + test("a caller entitlement miss uses a Pool model detour without clearing the healthy main pin (#3157)", async () => { + const { cfg, context, directEntitlementChecks } = await resolveRequestOwnedMainPinCase({ + mainWeeklyPercent: 16, + poolWeeklyPercent: 20, + callerEntitled: false, + }); + expect(context).toMatchObject({ kind: "pool", accountId: "pool-a" }); + expect(directEntitlementChecks).toBe(1); + expect(cfg.activeCodexAccountId).toBe(MAIN_CODEX_ACCOUNT_ID); + expect(cfg.activeCodexAccountPinned).toBe(MAIN_CODEX_ACCOUNT_ID); + }); + test("selects pool auth independently of the routed provider", async () => { saveCodexAccountCredential("pool-a", { accessToken: "pool_token", From e40245e4cbb1e1e70322170369ac528a610dbe76 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 01:11:28 +0900 Subject: [PATCH 103/172] fix(client): tell the dashboard it is a client (#3169) The machine listener served the GUI without the runtime-role meta tag, so a connected client rendered as a plain standalone install. The tag is not decoration. gui/src/api-targets.ts reads it in isConnectedRuntime(), and discoverApiTargets() returns standalone targets immediately when it is anything other than "client" -- deliberately, so a user who never enabled remote hub issues no request to a remote-hub endpoint. The consequence on a real client was that discovery never queried /api/machine/status: no hub usage scope on Usage, no "this machine" panel on Startup, no connected-client list on Integrations, and no pairing form. src/server/index.ts already passes config.runtimeRole on the same call. The listener only ever serves a connected client, so the role is a constant here rather than a config read. Verified by hand before and after: with the tag absent the served document has only the session meta and the dashboard shows the standalone layout; with it present the two-plane UI appears, including "Disconnect from hub" and the pairing form. The regression test asserts the call carries the role. It reads the source rather than the HTTP response because the listener falls through to a JSON payload when gui/dist is absent, which would make an HTTP-level assertion pass vacuously in a checkout with no GUI build. A second test covers the document itself through serveGuiFile with a temporary dist. Both were driven red against the unfixed call. Co-authored-by: jun --- src/client/machine-listener.ts | 9 +++++- tests/client-machine-listener.test.ts | 41 ++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/client/machine-listener.ts b/src/client/machine-listener.ts index 476c2e70a4..b7e54032b6 100644 --- a/src/client/machine-listener.ts +++ b/src/client/machine-listener.ts @@ -118,7 +118,14 @@ export function startMachineListener( ? issueGuiSession(req, config, managementAuth, { trustedTailscaleIngress: false }) : null; if (url.pathname === "/opencodex-session" && session) return serveSessionBootstrap(session); - const gui = serveGuiFile(url.pathname, undefined, session ?? undefined); + // State the role, exactly as the standalone/hub server does (src/server/index.ts). + // The GUI decides whether a machine plane exists from this tag alone + // (gui/src/api-targets.ts `isConnectedRuntime`): without it `discoverApiTargets` + // returns standalone targets and never queries /api/machine/status, so a connected + // client renders as a plain install — no hub usage scope, no "this machine" panel, + // no connected-client list. This listener only ever serves a connected client, so + // the role is a constant here rather than a config read. + const gui = serveGuiFile(url.pathname, undefined, session ?? undefined, "client"); if (gui) return gui; if (url.pathname === "/") { return Response.json({ diff --git a/tests/client-machine-listener.test.ts b/tests/client-machine-listener.test.ts index 511961c8bd..d90e9c7a05 100644 --- a/tests/client-machine-listener.test.ts +++ b/tests/client-machine-listener.test.ts @@ -1,9 +1,10 @@ import { afterEach, beforeEach, describe, expect, test } from "bun:test"; -import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import type { Server } from "bun"; import { startMachineListener } from "../src/client/machine-listener"; +import { serveGuiFile } from "../src/server/gui-static"; import type { OcxClientConnectionConfig } from "../src/types"; import type { ManagementAuthState } from "../src/server/management-auth"; @@ -155,3 +156,41 @@ describe("client machine listener", () => { expect(() => startMachineListener(0, { managementAuthState: authState() })).toThrow(/requires connected client state/); }); }); + +describe("the served document states the client role", () => { + // The GUI decides whether a machine plane exists from this tag alone + // (gui/src/api-targets.ts `isConnectedRuntime` / `discoverApiTargets`). A missing tag is + // not cosmetic: discovery returns standalone targets immediately and never queries + // /api/machine/status, so a connected client renders as a plain install — no hub usage + // scope, no "this machine" panel, no connected-client list. + // + // Asserted against `serveGuiFile` directly rather than over HTTP, because the listener + // falls through to a JSON payload when `gui/dist` is absent, and a checkout without a + // GUI build would make an HTTP-level assertion pass vacuously. + test("the client dashboard document carries the role tag", () => { + const dist = mkdtempSync(join(tmpdir(), "ocx-gui-dist-")); + try { + writeFileSync(join(dist, "index.html"), ""); + const response = serveGuiFile("/", dist, undefined, "client"); + expect(response).not.toBeNull(); + return response!.text().then(html => { + expect(meta(html, "opencodex-runtime-role")).toBe("client"); + }); + } finally { + rmSync(dist, { recursive: true, force: true }); + } + }); + + test("the listener asks for the client role rather than leaving it undefined", () => { + // Source-level, deliberately: the call is what carries the role, and the HTTP path + // cannot show it in a checkout with no GUI build. Reading the file keeps the + // assertion honest in both cases. + const source = readFileSync( + join(import.meta.dir, "..", "src", "client", "machine-listener.ts"), + "utf8", + ); + const call = /serveGuiFile\(([^)]*)\)/.exec(source); + expect(call, "machine-listener no longer calls serveGuiFile").not.toBeNull(); + expect(call![1]).toContain('"client"'); + }); +}); From e92aa336a83c86283b500269a1d55779836114b0 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 01:32:49 +0900 Subject: [PATCH 104/172] fix(cli): route ocx models new-policy and new-arrivals to the runtime module (#3171) handleModels listed the runtime subcommands inline while handleModelsRuntimeCommand listed them again in its dispatch chain. new-policy and new-arrivals were added to the second list only, so both documented commands fell through to handleConfiguredModels and failed with Unexpected argument(s). Replace the duplication with one shared set in a leaf module both sides import. models.ts keeps its dynamic import of models-runtime, so the management-API client still stays off the ocx models add path. Closes #3094 Co-authored-by: jun --- src/cli/models-runtime-subcommands.ts | 34 ++++++++++++++ src/cli/models-runtime.ts | 4 ++ src/cli/models.ts | 3 +- tests/cli-models-runtime-dispatch.test.ts | 57 +++++++++++++++++++++++ 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 src/cli/models-runtime-subcommands.ts create mode 100644 tests/cli-models-runtime-dispatch.test.ts diff --git a/src/cli/models-runtime-subcommands.ts b/src/cli/models-runtime-subcommands.ts new file mode 100644 index 0000000000..a49828d203 --- /dev/null +++ b/src/cli/models-runtime-subcommands.ts @@ -0,0 +1,34 @@ +/** + * The `ocx models` subcommands that live in `models-runtime` and talk to the + * management API, rather than editing `config.json` directly. + * + * This list is shared rather than duplicated on purpose. `handleModels` in + * `models.ts` decides which names to hand to the runtime module, and + * `handleModelsRuntimeCommand` decides which names it answers. When those two + * lists were written out separately, `new-policy` and `new-arrivals` were + * implemented and documented but never routed, so both failed with + * "Unexpected argument(s)" (#3094). + * + * It lives in its own leaf module so `models.ts` can read the set without + * statically importing `models-runtime` — that import is deliberately dynamic + * to keep the management-API client off the `ocx models add` path. + */ +export const MODELS_RUNTIME_SUBCOMMANDS = [ + "live", + "edit", + "enable", + "disable", + "provider", + "selected", + "preset", + "new-policy", + "new-arrivals", + "context", + "shadow", +] as const; + +export type ModelsRuntimeSubcommand = (typeof MODELS_RUNTIME_SUBCOMMANDS)[number]; + +export function isModelsRuntimeSubcommand(value: string | undefined): value is ModelsRuntimeSubcommand { + return MODELS_RUNTIME_SUBCOMMANDS.includes(value as ModelsRuntimeSubcommand); +} diff --git a/src/cli/models-runtime.ts b/src/cli/models-runtime.ts index 4285e53eb5..d5174ef603 100644 --- a/src/cli/models-runtime.ts +++ b/src/cli/models-runtime.ts @@ -12,6 +12,7 @@ import { takeOption, type RuntimeApiDeps, } from "./runtime-api"; +import { isModelsRuntimeSubcommand } from "./models-runtime-subcommands"; const USAGE = `Usage: ocx models live [--provider ] [--json] @@ -321,6 +322,9 @@ async function shadow(argv: string[], deps: RuntimeApiDeps): Promise { } export async function handleModelsRuntimeCommand(sub: string, argv: string[], deps: RuntimeApiDeps = {}): Promise { + // The dispatch below and MODELS_RUNTIME_SUBCOMMANDS must name the same set; + // tests/cli-models-runtime-dispatch.test.ts fails if they drift (#3094). + if (!isModelsRuntimeSubcommand(sub)) return null; let action: (() => Promise) | undefined; if (sub === "live") action = () => live(argv, deps); else if (sub === "edit") action = () => edit(argv, deps); diff --git a/src/cli/models.ts b/src/cli/models.ts index a1472c99e2..6a6e6e0d5c 100644 --- a/src/cli/models.ts +++ b/src/cli/models.ts @@ -12,6 +12,7 @@ import { modelRecordValue, } from "../reasoning-effort"; import { encodedModelIdCollides, resolveSlugSelection, routedSlug } from "../providers/slug-codec"; +import { isModelsRuntimeSubcommand } from "./models-runtime-subcommands"; import { knownModelIdsForProvider } from "../router"; import { findLiveProxy } from "../server/proxy-liveness"; import { modelInList, type OcxConfig, type OcxCustomModel } from "../types"; @@ -445,7 +446,7 @@ export async function handleModels(args: string[]): Promise { handleCustomList(rest); return; } - if (["live", "edit", "enable", "disable", "provider", "selected", "preset", "context", "shadow"].includes(subcommand ?? "")) { + if (isModelsRuntimeSubcommand(subcommand)) { const { handleModelsRuntimeCommand } = await import("./models-runtime"); const code = await handleModelsRuntimeCommand(subcommand!, rest); if (code !== null) process.exitCode = code; diff --git a/tests/cli-models-runtime-dispatch.test.ts b/tests/cli-models-runtime-dispatch.test.ts new file mode 100644 index 0000000000..d5bba5abf0 --- /dev/null +++ b/tests/cli-models-runtime-dispatch.test.ts @@ -0,0 +1,57 @@ +import { describe, expect, test } from "bun:test"; +import { readFileSync } from "node:fs"; +import { MODELS_RUNTIME_SUBCOMMANDS, isModelsRuntimeSubcommand } from "../src/cli/models-runtime-subcommands"; +import { MODELS_RUNTIME_USAGE, handleModelsRuntimeCommand } from "../src/cli/models-runtime"; + +/** + * #3094: `ocx models new-policy` and `ocx models new-arrivals` were implemented in + * models-runtime.ts, listed in its USAGE, and documented on the docs site, but + * handleModels in models.ts routed a separately written array that omitted them. Both + * commands reached handleConfiguredModels instead and died with + * "Unexpected argument(s)". + * + * The repair removed the duplication: one exported set is the routing decision on both + * sides. These tests pin the general form of the defect, not just the two names, so a + * future runtime subcommand added without touching the shared set fails here. + */ +describe("models runtime subcommand dispatch (#3094)", () => { + test("every documented runtime subcommand is in the shared routing set", () => { + // USAGE is the user-facing contract: " ocx models ..." per line. + const documented = new Set(); + for (const line of MODELS_RUNTIME_USAGE.split("\n")) { + const match = /^\s+ocx models ([a-z-]+)/.exec(line); + if (match?.[1]) documented.add(match[1]); + } + // `ocx models ...` is written as an alternation in USAGE. + if (MODELS_RUNTIME_USAGE.includes("ocx models ")) { + documented.add("enable"); + documented.add("disable"); + } + expect(documented.size).toBeGreaterThan(0); + const missing = [...documented].filter(sub => !isModelsRuntimeSubcommand(sub)); + expect(missing).toEqual([]); + }); + + test("new-policy and new-arrivals are routed, not swallowed by the configured-models path", () => { + expect(isModelsRuntimeSubcommand("new-policy")).toBe(true); + expect(isModelsRuntimeSubcommand("new-arrivals")).toBe(true); + }); + + test("handleModels routes exactly the shared set to the runtime module", () => { + // Reading the source keeps this honest without booting the CLI: the dispatch must + // consult the shared predicate rather than re-listing names inline. + const source = readFileSync(new URL("../src/cli/models.ts", import.meta.url), "utf8"); + expect(source).toContain("isModelsRuntimeSubcommand(subcommand)"); + // The old inline array is what allowed the drift; it must not come back. + expect(source).not.toMatch(/\["live",\s*"edit"/); + }); + + test("handleModelsRuntimeCommand returns null for a name outside the set", async () => { + expect(await handleModelsRuntimeCommand("definitely-not-a-subcommand", [])).toBeNull(); + }); + + test("the shared set has no duplicates", () => { + expect(new Set(MODELS_RUNTIME_SUBCOMMANDS).size).toBe(MODELS_RUNTIME_SUBCOMMANDS.length); + }); +}); + From 7386b52016be7b0246ca941d4e285ec340331431 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 01:36:25 +0900 Subject: [PATCH 105/172] fix(combo): apply the combo default effort the target can actually reach (#3172) A combo configured for max routed to a target whose ladder tops out lower sent no effort at all: concreteComboRequestBody tested literal membership and dropped the default on a miss, so the provider default applied and the turn ran at none. The catalog never agreed with that. effectiveComboDefault advertises the highest supported rung at or below the request, so the served catalog promised max while the request path silently sent nothing. Share one resolver instead. It moves to src/reasoning-effort.ts, a leaf whose only import is ./types and which already owns codexEffortRank, so the request path can use it without importing the catalog plane - aggregation.ts pulls node:child_process, oauth, model-cache and cursor live-models, and it already imports src/combos, so a direct import would also have closed a cycle. Unknown ladders stay fail-closed and an explicitly empty ladder still means no reasoning. Neither behavior changes. Closes #3108 Co-authored-by: jun --- src/codex/catalog/aggregation.ts | 18 +++++++---------- src/combos/request.ts | 18 ++++++++++++++--- src/reasoning-effort.ts | 32 +++++++++++++++++++++++++++++++ tests/combos.test.ts | 33 ++++++++++++++++++++++++++++++-- 4 files changed, 85 insertions(+), 16 deletions(-) diff --git a/src/codex/catalog/aggregation.ts b/src/codex/catalog/aggregation.ts index 0240a1a161..44ee1c07fa 100644 --- a/src/codex/catalog/aggregation.ts +++ b/src/codex/catalog/aggregation.ts @@ -8,7 +8,7 @@ import { clearModelCache, DEFAULT_MODEL_CACHE_TTL_MS, getFreshCached, getStaleCa import { buildModelsRequest, resolveModelsAuthToken } from "../../oauth"; import type { OcxConfig, OcxProviderConfig } from "../../types"; import { modelInList } from "../../types"; -import { CODEX_REASONING_LEVELS, codexEffortRank, configuredReasoningEfforts, modelRecordValue, sanitizeCodexReasoningEfforts } from "../../reasoning-effort"; +import { CODEX_REASONING_LEVELS, codexEffortRank, configuredReasoningEfforts, modelRecordValue, resolveEffortAtOrBelow, sanitizeCodexReasoningEfforts } from "../../reasoning-effort"; import { getModelMetadata, getModelMetadataCaseInsensitive, listModelMetadata, resolveMetadataProvider } from "../../generated/model-metadata"; import { enrichProviderFromRegistry, shouldCaseFoldMetadataModelId } from "../../providers/derive"; import { getProviderRegistryEntry } from "../../providers/registry"; @@ -77,20 +77,16 @@ export function intersectStrings(values: readonly string[][]): string[] { return [...new Set(values[0])].filter(value => rest.every(set => set.has(value))); } +/** + * The catalog's view of a combo's default effort. Delegates to the shared leaf + * resolver so the request path (src/combos/request.ts) cannot drift from what the + * catalog advertised (#3108). + */ export function effectiveComboDefault( configured: string | null | undefined, common: readonly string[], ): string | undefined { - if (!configured) return undefined; - if (configured && common.includes(configured)) return configured; - const requestedRank = codexEffortRank(configured); - const ranked = common - .map(effort => ({ effort, rank: codexEffortRank(effort) })) - .filter(item => item.rank >= 0) - .sort((a, b) => a.rank - b.rank); - if (ranked.length === 0) return undefined; - const atOrBelow = ranked.filter(item => item.rank <= requestedRank); - return atOrBelow.at(-1)?.effort ?? ranked[0]!.effort; + return resolveEffortAtOrBelow(configured, common); } /** diff --git a/src/combos/request.ts b/src/combos/request.ts index 2b198aae7a..abafccc525 100644 --- a/src/combos/request.ts +++ b/src/combos/request.ts @@ -1,4 +1,5 @@ import type { OcxComboDefaultEffort, OcxComboTarget, OcxConfig } from "../types"; +import { resolveEffortAtOrBelow } from "../reasoning-effort"; import { resolveComboId } from "./types"; const warnedUnsupportedDefaults = new Set(); @@ -72,7 +73,18 @@ export function concreteComboRequestBody( if (!needsDefault) return clone; // Picker availability treats an unknown ladder as a wildcard, but runtime // injection stays fail-closed until this concrete target advertises support. - if (!targetReasoningEfforts?.includes(defaultEffort)) { + // + // Support is not literal membership. The catalog advertises the combo's default + // through effectiveComboDefault, which keeps the highest supported rung at or + // below the request rather than dropping it. Testing membership here meant a + // combo configured for `max` against a target topping out at `high` sent no + // effort at all, so the provider default applied and the turn ran at `none` + // while the catalog still advertised `max` (#3108). Resolve the same way the + // catalog did. + const resolvedEffort = targetReasoningEfforts === undefined + ? undefined + : resolveEffortAtOrBelow(defaultEffort, targetReasoningEfforts); + if (!resolvedEffort) { const key = `${target.provider}/${target.model}:${defaultEffort}`; if (!warnedUnsupportedDefaults.has(key)) { warnedUnsupportedDefaults.add(key); @@ -86,9 +98,9 @@ export function concreteComboRequestBody( return clone; } if (reasoning === undefined) { - clone.reasoning = { effort: defaultEffort }; + clone.reasoning = { effort: resolvedEffort }; } else { - clone.reasoning = { ...(reasoning as Record), effort: defaultEffort }; + clone.reasoning = { ...(reasoning as Record), effort: resolvedEffort }; } return clone; } diff --git a/src/reasoning-effort.ts b/src/reasoning-effort.ts index 81b35b7af5..6342159d31 100644 --- a/src/reasoning-effort.ts +++ b/src/reasoning-effort.ts @@ -80,6 +80,38 @@ export function codexEffortRank(effort: string): number { return CODEX_REASONING_ORDER.indexOf(effort); } +/** + * Resolve a requested effort against the rungs a target actually supports, never + * raising above the request. + * + * Returns the request itself when supported, otherwise the highest supported rung + * at or below it, otherwise the lowest supported rung, and `undefined` when the + * supported set contains no rankable rung at all (including the empty ladder, which + * is how a no-reasoning model is expressed). + * + * This lives here rather than beside its first caller because two very different + * planes need the same answer: the catalog advertises a combo's default effort, and + * the request path injects one. When they disagreed, the catalog promised `max` and + * the runtime silently sent nothing, so the provider default applied instead (#3108). + * `reasoning-effort.ts` is a leaf — its only import is `./types` — so the request + * path can share this without pulling the catalog plane along. + */ +export function resolveEffortAtOrBelow( + requested: string | null | undefined, + supported: readonly string[], +): string | undefined { + if (!requested) return undefined; + if (supported.includes(requested)) return requested; + const requestedRank = codexEffortRank(requested); + const ranked = supported + .map(effort => ({ effort, rank: codexEffortRank(effort) })) + .filter(item => item.rank >= 0) + .sort((a, b) => a.rank - b.rank); + if (ranked.length === 0) return undefined; + const atOrBelow = ranked.filter(item => item.rank <= requestedRank); + return atOrBelow.at(-1)?.effort ?? ranked[0]!.effort; +} + export function modelRecordValue(record: Record | undefined, modelId: string): T | undefined { if (!record) return undefined; if (Object.prototype.hasOwnProperty.call(record, modelId)) return record[modelId]; diff --git a/tests/combos.test.ts b/tests/combos.test.ts index 76c0542f38..a3f17c20f4 100644 --- a/tests/combos.test.ts +++ b/tests/combos.test.ts @@ -293,11 +293,40 @@ describe("combo request cloning", () => { ).reasoning).toEqual({ summary: "concise", effort: "high" }); }); - test("omits combo defaults for unset, unsupported, and unknown target capabilities", () => { + test("omits combo defaults for unset, no-reasoning, and unknown target capabilities", () => { expect(concreteComboRequestBody({ model: "combo/x" }, target, null, ["high"]).reasoning).toBeUndefined(); + // An explicitly empty ladder is how a no-reasoning model is expressed. expect(concreteComboRequestBody({ model: "combo/x" }, target, "high", []).reasoning).toBeUndefined(); + // An unknown ladder stays fail-closed: the picker treats it as a wildcard, runtime injection does not. expect(concreteComboRequestBody({ model: "combo/x" }, target, "high", undefined).reasoning).toBeUndefined(); - expect(concreteComboRequestBody({ model: "combo/x" }, target, "high", ["low", "medium"]).reasoning).toBeUndefined(); + }); + + /** + * #3108: a combo configured for `max` routed to a target whose ladder tops out lower + * sent NO effort at all, so the provider default applied and the turn ran at `none` — + * while the catalog advertised `max` for that same combo, because + * effectiveComboDefault downgrades to the nearest supported rung instead of dropping. + * The request path now resolves the same way the catalog did. + */ + test("a combo default above the target ladder is downgraded, not dropped (#3108)", () => { + expect(concreteComboRequestBody({ model: "combo/x" }, target, "max", ["low", "medium", "high"]).reasoning) + .toEqual({ effort: "high" }); + expect(concreteComboRequestBody({ model: "combo/x" }, target, "high", ["low", "medium"]).reasoning) + .toEqual({ effort: "medium" }); + // Exact support is still passed through untouched. + expect(concreteComboRequestBody({ model: "combo/x" }, target, "max", ["high", "max"]).reasoning) + .toEqual({ effort: "max" }); + // Never raises: a request below everything supported takes the lowest rung, not a higher one. + expect(concreteComboRequestBody({ model: "combo/x" }, target, "low", ["high", "max"]).reasoning) + .toEqual({ effort: "high" }); + // A caller-supplied effort still wins over the combo default. + expect(concreteComboRequestBody( + { model: "combo/x", reasoning: { effort: "low" } }, target, "max", ["low", "medium", "high"], + ).reasoning).toEqual({ effort: "low" }); + // The resolved rung merges into a partial reasoning object rather than replacing it. + expect(concreteComboRequestBody( + { model: "combo/x", reasoning: { summary: "concise" } }, target, "max", ["low", "high"], + ).reasoning).toEqual({ summary: "concise", effort: "high" }); }); test("debug-warns once per unsupported or unknown combo default", () => { From 0d8147c2002e3e4e4adf39a03084d6a6ab18991e Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 01:38:43 +0900 Subject: [PATCH 106/172] docs(reference): document the readyz protocol fields and the remote-hub config keys (#3173) /readyz gained protocol, minimumClientProtocol and managementUrl in the remote-hub work, but lifecycle.md still described the older six-field body, so a client author reading the reference had no way to know negotiation metadata was available. Three config keys shipped without reference coverage. Two were named only in passing inside a runtimeRole sentence; remoteGui.allowInsecureHttp appeared nowhere. Give each a row with its type, its default when absent, and what going wrong looks like. allowInsecureHttp is documented as retired rather than as a setting. It grants nothing - pairing crosses loopback or authenticated HTTPS only - and is still parsed purely so an older config keeps loading under a strict schema. Refs #3158 (T19, T21) Co-authored-by: jun --- .../src/content/docs/reference/cli/lifecycle.md | 13 ++++++++++--- .../content/docs/reference/configuration/server.md | 12 ++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/docs-site/src/content/docs/reference/cli/lifecycle.md b/docs-site/src/content/docs/reference/cli/lifecycle.md index 74793580d2..75ca9da788 100644 --- a/docs-site/src/content/docs/reference/cli/lifecycle.md +++ b/docs-site/src/content/docs/reference/cli/lifecycle.md @@ -161,11 +161,18 @@ command exits 0 only when healthy and 1 otherwise, making it suitable for servic Check post-sync readiness through the unauthenticated `GET /readyz` endpoint. It returns `200` when ready, or `503` with `Retry-After: 1` for `pending` and terminal `failed`. Its sanitized HTTP identity -is `{service, version, uptime, pid, port, status}`. Old proxies without `/readyz` fail closed as -`unreachable`; `/healthz` is separate liveness, not readiness. The command performs one probe by +is `{service, version, uptime, pid, port, status}` plus the remote-hub protocol fields +`{protocol, minimumClientProtocol, managementUrl}`. `protocol` is the hub protocol this proxy +speaks and `minimumClientProtocol` the oldest client it still accepts, so a client can refuse an +incompatible pairing before sending anything else. `managementUrl` is the origin a client should +use for the management plane: the configured `hub.managementPublicOrigin` when `runtimeRole` is +`hub`, and otherwise the origin the request itself arrived on. A readiness request with no +HTTP(S) origin is rejected rather than answered with a guess. Old proxies without `/readyz` fail +closed as `unreachable`; `/healthz` is separate liveness, not readiness. The command performs one probe by default; `--wait` polls until ready or timeout, but exits immediately when it observes the terminal `failed` state. The default timeout is 45 seconds; `--timeout ` requires `--wait` and accepts positive integer seconds from 1–300. -CLI JSON emits `{ready, status, pid, port}`, where `status` is `ready`, `pending`, `failed`, or +The CLI's own `--json` output is deliberately narrower than the HTTP body: it emits +`{ready, status, pid, port}`, where `status` is `ready`, `pending`, `failed`, or `unreachable`. Exit codes are 0 for ready; 1 for not-ready, pending, failed, timeout, or unreachable; and 64 for invalid arguments. diff --git a/docs-site/src/content/docs/reference/configuration/server.md b/docs-site/src/content/docs/reference/configuration/server.md index c7d9226a1c..6976504b42 100644 --- a/docs-site/src/content/docs/reference/configuration/server.md +++ b/docs-site/src/content/docs/reference/configuration/server.md @@ -266,3 +266,15 @@ intended account and workload. ## Remote Hub keys and defaults `runtimeRole` defaults to `standalone`. A hub uses `hub.managementPublicOrigin`, loopback-only `hub.managementIngress` (`enabled:false` when absent), and exact `remoteGui.allowedTailscaleUsers` (empty when absent). A client data key lives in `service-api-token`, never `config.json`; rotation may temporarily create `service-api-token.prev`. Usage stores are not mirrored. + +| Key | Type | Default when absent | What it does | +| --- | --- | --- | --- | +| `hub.managementPublicOrigin` | string | unset | The canonical browser-reachable management origin a hub advertises, for example the HTTPS origin Tailscale Serve prints. It is what `/readyz` reports as `managementUrl` while `runtimeRole` is `hub`; with it unset the hub falls back to whatever origin each request arrived on, so a client behind a different frontend can be handed an address it cannot reach. | +| `hub.managementIngress` | `{enabled:false}` or `{enabled:true, port}` | `{enabled:false}` | An extra management-only listener for a local HTTPS frontend. The hostname is not configurable: when enabled the socket always binds `127.0.0.1`, and only GUI, session-bootstrap, and management API routes are admitted. Data-plane routes are rejected before dispatch. | +| `remoteGui.allowedTailscaleUsers` | string[] | `[]` (empty — nobody) | Exact Tailscale login identities allowed to be issued an automatic remote GUI session. The `Tailscale-User-Login` header is trusted **only** on the separate management ingress; an empty list means no remote identity can mint a session, which is the safe default rather than an oversight. Identities are compared exactly, so a typo silently denies access. | +| `remoteGui.allowInsecureHttp` | boolean | unset | **Retired — has no effect.** It once permitted a one-time pairing exchange over non-loopback plaintext HTTP. A pairing grant now crosses loopback or authenticated HTTPS only. The key is still parsed so an existing `config.json` keeps loading (the schema is strict, and dropping the key outright would make an older config fail to load entirely); a persisted `true` is reported once and then ignored. Remove it from your config. | + +A hub that is reachable from a browser needs `hub.managementPublicOrigin` and at least one entry +in `remoteGui.allowedTailscaleUsers`. Setting the origin without the user list produces a hub that +advertises itself correctly and then refuses every session; setting the user list without the +origin produces sessions pointed at whichever origin the request happened to use. From 22a643a00b5974fa53b084a04491f60d56ec9ee2 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 01:41:45 +0900 Subject: [PATCH 107/172] test(combo): update the failover e2e assertion the effort fix invalidated (#3175) The case expected the first target to receive no reasoning_effort, which is the behavior #3172 removed. That combo's members advertise low and low+high, so the catalog advertises low, and low is now what the first target receives. Refs #3108 Co-authored-by: jun --- tests/server-combo-failover-e2e.test.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/server-combo-failover-e2e.test.ts b/tests/server-combo-failover-e2e.test.ts index ea34844044..e8bf387c94 100644 --- a/tests/server-combo-failover-e2e.test.ts +++ b/tests/server-combo-failover-e2e.test.ts @@ -2205,7 +2205,13 @@ describe("server combo failover 030 activation matrix", () => { expect(response.status).toBe(200); expect(JSON.stringify(bodies[0]!.body)).not.toContain("data:image/png"); expect(JSON.stringify(bodies[1]!.body)).toContain("data:image/png"); - expect(bodies[0]!.body.reasoning_effort).toBeUndefined(); + // #3108: the combo default is resolved against each target's ladder rather than + // dropped on an exact-membership miss. This combo's advertised default IS "low" — + // the catalog intersects member ladders (a: ["low"], b: ["low","high"]) to ["low"] + // and effectiveComboDefault("high", ["low"]) yields "low" — so sending "low" to the + // first target is what the served catalog promised. Previously nothing was sent and + // the provider default silently applied. + expect(bodies[0]!.body.reasoning_effort).toBe("low"); expect(bodies[1]!.body.reasoning_effort).toBe("high"); clearComboSelectionState(); From e582aee214eec70f36be3062708bd1fddcf44807 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 02:07:45 +0900 Subject: [PATCH 108/172] fix(gui): stop the mobile topbar and integration cards overflowing (#3174) * docs(devlog): plan the 3-OS QA sweep, the restore contract, and the GUI pass * docs(devlog): fix the QA evidence layout and scenario id scheme * fix(gui): stop the mobile topbar and integration cards overflowing Two responsive defects, both found by measuring real geometry through CDP rather than by reading CSS. The mobile topbar overlapped itself. At 320px the version badge ended at x=245 while the action orbs began at x=206, so the power orb sat on top of the badge. A flex item only shrinks below its content size when it carries min-width: 0 itself; .mobile-topbar .brand had it but its children did not, so .name held its intrinsic width and pushed .ver under the actions. Shrinking .name is necessary but not sufficient. The row budget at the narrowest width is 44 (menu) + 26 (logo) + 56 (badge) + 94 (actions) plus gaps, which leaves the name about 38px - it rendered as "op...". The badge is dropped instead: the same brand node is mounted again in the drawer head, and the live version is also on the dashboard Version stat. A review pass caught the first attempt inventing @media (max-width: 400px), which was the only 400px rule in the file and covered an unmeasured 375-399 band. It is folded into the existing 360px tiny-phone breakpoint instead. The integration cards overflowed the page. .integration-cards used repeat(auto-fill, minmax(260px, 1fr)); a bare 260px floor is wider than the content box of a 320px viewport, so the track could not shrink and the card went with it - the Settings button measured left=326, right=409 against a 320px page, and again at 375px and 736px. min(260px, 100%) keeps the multi-column intent while letting a narrow viewport fall back. Verified after the change across 5 viewports x 5 pages: zero elements outside the viewport. Topbar at 320/360/375/414/736: name always ends before the actions begin, badge returns at 375. Both regression tests were driven red against the unfixed CSS first. * docs(devlog): record the two responsive defects and what the review changed * docs(devlog): record the macmini deploy, QA classes, and the detached-HEAD restore miss * docs(devlog): record the linux and macbook QA, and the start-time Codex injection found on a clean host * docs(devlog): record the QA verdicts and teardown receipts (evidence itself is gitignored) * docs(devlog): record the windows QA, the version-skew warning, and the EPERM restore * docs(devlog): synthesise the unit - three defects, all found by measurement * docs(devlog): anonymise remote home paths the privacy scan flagged --------- Co-authored-by: jun --- .../000_host_inventory.md | 48 +++++++++++ .../001_qa_evidence_layout.md | 45 ++++++++++ .../002_qa_verdicts.md | 41 +++++++++ .../010_wp2_gui_design.md | 83 +++++++++++++++++++ .../011_wp2_outcome.md | 54 ++++++++++++ .../020_wp3_wp5_deploy_qa.md | 60 ++++++++++++++ .../021_wp3_macmini_outcome.md | 68 +++++++++++++++ .../022_wp4_linux_macbook_outcome.md | 69 +++++++++++++++ .../023_wp5_windows_outcome.md | 76 +++++++++++++++++ .../090_outcome.md | 52 ++++++++++++ gui/src/styles-integrations.css | 11 ++- gui/src/styles.css | 19 +++++ gui/tests/integrations-card-overflow.test.ts | 35 ++++++++ gui/tests/mobile-topbar-layout.test.ts | 55 ++++++++++++ 14 files changed, 714 insertions(+), 2 deletions(-) create mode 100644 devlog/_plan/260902_multiplatform_qa_and_gui/000_host_inventory.md create mode 100644 devlog/_plan/260902_multiplatform_qa_and_gui/001_qa_evidence_layout.md create mode 100644 devlog/_plan/260902_multiplatform_qa_and_gui/002_qa_verdicts.md create mode 100644 devlog/_plan/260902_multiplatform_qa_and_gui/010_wp2_gui_design.md create mode 100644 devlog/_plan/260902_multiplatform_qa_and_gui/011_wp2_outcome.md create mode 100644 devlog/_plan/260902_multiplatform_qa_and_gui/020_wp3_wp5_deploy_qa.md create mode 100644 devlog/_plan/260902_multiplatform_qa_and_gui/021_wp3_macmini_outcome.md create mode 100644 devlog/_plan/260902_multiplatform_qa_and_gui/022_wp4_linux_macbook_outcome.md create mode 100644 devlog/_plan/260902_multiplatform_qa_and_gui/023_wp5_windows_outcome.md create mode 100644 devlog/_plan/260902_multiplatform_qa_and_gui/090_outcome.md create mode 100644 gui/tests/integrations-card-overflow.test.ts create mode 100644 gui/tests/mobile-topbar-layout.test.ts diff --git a/devlog/_plan/260902_multiplatform_qa_and_gui/000_host_inventory.md b/devlog/_plan/260902_multiplatform_qa_and_gui/000_host_inventory.md new file mode 100644 index 0000000000..528b5f6fb2 --- /dev/null +++ b/devlog/_plan/260902_multiplatform_qa_and_gui/000_host_inventory.md @@ -0,0 +1,48 @@ +# 000 — 조사: 네 호스트의 현재 설치 상태 + +배포하기 전에 각 호스트가 **어떻게** 설치돼 있는지부터 확정한다. 복구가 +"되돌린다"가 되려면 되돌릴 지점이 기록돼 있어야 하고, 네 호스트의 설치 형태가 +전부 다르기 때문이다. 아래는 2026-09-02 실측이다. + +## 호스트 인벤토리 + +| 호스트 | OS | 설치 형태 | 실측 | +| --- | --- | --- | --- | +| `macmini-cf` | macOS arm64 | **소스 체크아웃** | `~/opencodex` @ `0cc73411a` (dev), `~/.bun/bin/ocx` → `~/opencodex/bin/ocx.mjs` 심볼릭 링크, launchd `com.opencodex.proxy` PID 80761 **실행 중**, bun 1.3.14 | +| `lidge` | Linux x86_64 | **npm 글로벌** | `@bitkyc08/opencodex@2.21.0`, node/npm `/usr/bin`, bun `/usr/local/bin`, `~/.opencodex` 존재, 서비스 없음 | +| `intmb` | macOS | **미설치** | `ocx` 없음, `~/.opencodex` 없음, npm 글로벌 없음, bun 없음 | +| `desktop-c795oh4` | Windows (MINGW64) | **npm 글로벌** | `@bitkyc08/opencodex@2.32.1`, `~/AppData/Roaming/npm/ocx`, `~/.opencodex` 존재, bun 1.3.14, node v24.19.0 | + +Tailscale 이름 해석: 사용자가 말한 "macbook"과 "desktop"은 SSH 별칭으로 각각 +`intmb`와 `desktop-c795oh4`다. `macbook`/`desktop`은 해석되지 않는다. +`win`은 websocket 핸드셰이크에서 끊어져 쓰지 않는다. + +## 복구 계약 (RESTORE-01) + +각 호스트는 **자기 원래 형태로** 돌아간다. 형태를 통일하지 않는다. + +| 호스트 | 복구 목표 | 검증 방법 | +| --- | --- | --- | +| `macmini-cf` | `~/opencodex`를 `0cc73411a`로, 심볼릭 링크 유지, launchd 서비스 재기동 | `git rev-parse`, `readlink`, `launchctl list` | +| `lidge` | npm 글로벌 `2.21.0` 복원 | `npm ls -g` 출력 | +| `intmb` | **완전 제거** — 설치 전 상태 | `ocx` 부재, `~/.opencodex` 부재 | +| `desktop-c795oh4` | npm 글로벌 `2.32.1` 복원 | `npm ls -g` 출력 | + +`intmb`가 가장 조심스럽다. 없던 것을 설치했다가 지우는 것이므로 `~/.opencodex` +같은 부산물이 남으면 복구 실패다. 설치 전에 무엇이 없었는지 목록으로 남기고, +제거 후 그 목록이 여전히 비어 있는지 확인한다. + +`macmini-cf`의 실행 중 서비스는 두 번째로 조심스럽다. 사용자의 실제 프록시가 +거기서 돌고 있으므로, 테스트 때문에 내렸다면 반드시 다시 올린다. + +## 배포 대상 + +`dev` HEAD `e40245e4c`, `package.json` 버전 `2.40.0`. 이번 배포에는 직전 +사이클에서 고친 `fix(client): tell the dashboard it is a client`가 포함된다. + +## 이 유닛이 하지 않는 것 + +- 원격 호스트의 사용자 자격증명이나 `~/.opencodex` 내부 계정 데이터 변경. +- 로컬 전체 스위트 실행(`bun run test`) — 금지돼 있다. +- `dev`/`main`/`preview` 직접 푸시 — 전부 PR 경로. + diff --git a/devlog/_plan/260902_multiplatform_qa_and_gui/001_qa_evidence_layout.md b/devlog/_plan/260902_multiplatform_qa_and_gui/001_qa_evidence_layout.md new file mode 100644 index 0000000000..8e2bb6ff5f --- /dev/null +++ b/devlog/_plan/260902_multiplatform_qa_and_gui/001_qa_evidence_layout.md @@ -0,0 +1,45 @@ +# 001 — QA 증거 레이아웃 + +`cxc-qa` §3 계약을 이 유닛에 적용한 형태다. 시나리오 하나가 디렉터리 +하나이고, 그 안에 실행 명령과 아티팩트와 판정이 함께 있다. + +``` +.codexclaw/evidence//qa/ + -/ + invocation.txt 실행한 명령 그대로, 복사해 붙이면 재현된다 + 출력 캡처 / 스크린샷 / 응답 + verdict.json 판정과 그 근거가 가리키는 파일 +``` + +## 시나리오 id 규칙 + +`--`. 예: `macmini-cli-normal`, +`lidge-cli-malformed`, `desktop-http-repeat`, `gui-viewport-320`. +호스트가 앞에 오는 이유는 같은 시나리오를 네 곳에서 돌리기 때문이다. + +## verdict.json 필수 필드 + +`scenario`, `criterion`, `surface`, `verdict`, +`artifactRefs`, `note`, `capturedAt`, `sourceSnapshotAt`. +web/gui 표면에는 `captureChecks` 네 키가 추가된다. + +`inferred`와 `partial`은 없다. 실제 표면에서 돌았거나 안 돌았거나 둘 중 +하나다. 돌리지 못한 시나리오는 skip이 아니라 FAIL이고 블로커를 적는다. + +## receipt + +모든 시나리오가 끝나면 집계한다: + +```bash +node plugins/codexclaw/skills/qa/scripts/validate-evidence.mjs .codexclaw/evidence//qa/ --emit-receipt +``` + +실패하면 receipt를 남기지 않는다 — 이전 실행이 만든 것까지 지운다. 자기가 +증명하는 QA보다 오래 사는 receipt는 없는 것보다 나쁘기 때문이다. + +## 정리 영수증 + +QA가 띄운 모든 것에 각각 정리 증거를 남긴다: 프록시 PID, 포트, tmux 세션, +임시 디렉터리. `lsof -i :` 비어 있음, `ps` 확인, 파일 부재 확인. +아무것도 안 띄웠으면 "무엇을 확인했는지"와 함께 그렇게 적는다. + diff --git a/devlog/_plan/260902_multiplatform_qa_and_gui/002_qa_verdicts.md b/devlog/_plan/260902_multiplatform_qa_and_gui/002_qa_verdicts.md new file mode 100644 index 0000000000..a6641b09bd --- /dev/null +++ b/devlog/_plan/260902_multiplatform_qa_and_gui/002_qa_verdicts.md @@ -0,0 +1,41 @@ +# 002 — QA 판정 요약 (증거는 추적되지 않는다) + +`.codexclaw/`는 `.gitignore`에 있다(45-46행). 의도된 설계다 — 세션 +증거는 워크스페이스 산출물이지 저장소 이력이 아니다. 그래서 판정만 여기 남긴다. +아티팩트 원본은 `.codexclaw/evidence//qa/`에 있고, 세션이 끝나면 +그 경로에서만 볼 수 있다. + +## 시나리오별 판정 + +| 시나리오 | 표면 | 판정 | 핵심 근거 | +| --- | --- | --- | --- | +| `macmini-prestate` | cli | PASS | HEAD 0cc73411a / branch dev / dirty 0 / launchd 80761 / 2.39.0 | +| `macmini-deploy` | cli | PASS | 0d8147c20으로 pull, 재기동 후 healthz가 2.40.0 응답 | +| `macmini-cli-adversarial` | cli | PASS | 8클래스, exit 1/4 계약 일치 | +| `macmini-restore` | cli | PASS | branch/HEAD/dirty/link/서비스/버전 6항목 사전 일치 | +| `lidge-prestate` | cli | PASS | npm글로벌 2.21.0 / bun 1.3.14 | +| `lidge-deploy` | cli | PASS | 2.39.0 갱신 | +| `lidge-cli-adversarial` | cli | PASS | exit 0/1/4 macOS와 동일 | +| `lidge-http-runtime` | http | PASS | healthz/readyz 200, 반복 200/200, 404, GUI 200, stop 후 down | +| `lidge-restore` | cli | PASS | 2.21.0 복원, 포트 free, /tmp none | +| `intmb-prestate` | cli | PASS | 6항목 전부 none | +| `intmb-deploy` | cli | PASS | nvm + mktemp prefix 격리 설치, 전역 미변경 | +| `intmb-cli-http` | cli | PASS | exit 0/1/4, 기동 로그로 GUI 서빙 확인 | +| `intmb-restore` | cli | PASS | ~/.codex 부산물 3건 삭제, 6항목 전부 none 복귀 | + +`NA`로 처리한 것: `intmb`의 HTTP 재확인. 프록시를 `stop`으로 이미 +내린 뒤 curl을 다시 쳤기 때문에 000이 나왔다. 기동 시점 로그에 healthz와 GUI +서빙이 기록돼 있으므로 CLI 시나리오 안에서 다룬다. + +## 정리 영수증 + +| 자원 | 정리 | 확인 | +| --- | --- | --- | +| `macmini-cf` launchd | 재기동(내린 적 없음) | `launchctl list` PID 64484 | +| `lidge` 포트 10777 | `ocx stop` | `lsof` 비어 있음 | +| `lidge` 임시 홈 | `rm -rf` | `/tmp/ocxqa-*` none | +| `intmb` 포트 10778 | `pkill` | `lsof` 비어 있음 | +| `intmb` 격리 prefix | `rm -rf` | 경로 gone | +| `intmb` `~/.codex` 부산물 | 파일명 지정 삭제 | opencodex 흔적 0건 | +| 로컬 데모 프록시 | `SIGINT` | 포트 10399/10401 리스너 없음 | + diff --git a/devlog/_plan/260902_multiplatform_qa_and_gui/010_wp2_gui_design.md b/devlog/_plan/260902_multiplatform_qa_and_gui/010_wp2_gui_design.md new file mode 100644 index 0000000000..7c33186fa5 --- /dev/null +++ b/devlog/_plan/260902_multiplatform_qa_and_gui/010_wp2_gui_design.md @@ -0,0 +1,83 @@ +# 010 — wp2: GUI 프런트 개선 + +## Design Read + +```yaml +name: opencodex dashboard +colors: + primary: light-dark(#0d0d0d, #ececec) + accent: light-dark(#0a7d5c, #4ecb9d) + background: light-dark(#ffffff, #212121) +typography: + heading: { fontFamily: system-ui, fontSize: var(--text-title) } + body: { fontFamily: system-ui, fontSize: var(--text-body) } +iconography: + system: "custom inline SVG" + weight: "regular" + domain: "library-subset" +``` + +읽으면 이렇다: **로컬 프록시를 조작하는 개발자 도구**이고, 사용자는 자기 +기계에서 반복적으로 이 화면을 연다. 랜딩이 아니라 계기판이다. + +Do: 조용한 중립 표면, 한 개의 accent(초록)를 상태 신호로만, 밀도 높은 정보 +배치. Don't: 히어로 타이포, 그라디언트 장식, 균등 3카드 그리드, 이모지 아이콘. + +## 다이얼 + +``` +DESIGN_VARIANCE: 3 +MOTION_INTENSITY: 2 +Product density profile: D8 (developer console) +``` + +근거: 개발자 콘솔이다. `cxc-dev-uiux-design` 다이얼 프리셋의 +"Dashboard / SaaS admin" 3/2/5보다 밀도를 올린 이유는 이 화면이 프로바이더, +모델, 계정 풀, 로그를 동시에 다루는 전문가 제어 표면이기 때문이다. +"복잡하다"는 밀도이지 VARIANCE가 아니다. + +## 이미 잘 되어 있는 것 (건드리지 않는다) + +토큰 체계는 `light-dark()` 기반으로 정리돼 있고 accent는 하나다 +(`--accent` + 상태색 green/red/amber/blue). 736px 접힘도 정상 동작한다. +`cxc-dev-frontend` FE-ONENOTE-01(단일 색조 도배)이나 FE-GRADIENT-01(그라디언트 +남용) 위반이 없다. 이모지 아이콘도 없다. **재디자인 대상이 아니다.** + +## 실측으로 찾은 결함 — 모바일 상단바 겹침 + +320px에서 CDP로 실제 기하를 측정했다: + +``` +.brand .ver right = 245 +.mobile-topbar-actions left = 206 +``` + +39px 겹친다. 버전 배지 위에 전원 버튼이 올라앉는다. + +원인은 flex 축소 사슬이 한 단계 일찍 끊긴 것이다. `.mobile-topbar .brand`는 +`min-width: 0`을 가지고 있지만, flex 아이템이 콘텐츠 크기 아래로 줄어들려면 +**그 아이템 자신이** `min-width: 0`을 가져야 한다. `.name`과 `.ver`는 +`.brand`의 flex 아이템인데 그 선언이 없어 고유 너비를 유지했다. + +## 수정과 그 대가 + +`.name`에 축소와 말줄임을 주고 `.ver`를 고정한다. 그런데 그것만으로는 +부족했다 — 320px 예산은 44(메뉴) + 26(로고) + 56(배지) + 94(액션) + 간격이라 +이름에 약 38px만 남아 `op…`로 잘렸다. 겹침을 고치고 가독성을 잃은 셈이다. + +그래서 400px 미만에서 **배지를 숨긴다**. 배지는 드로어 브랜드에 중복돼 있고, +잘린 제품명보다 한 번 탭하면 보이는 버전이 낫다. + +수정 후 측정: 이름 `right = 179`, 액션 `left = 206`. 겹침 없음. + +## 검증 방법 + +CSS 문자열 검사만으로는 겹침을 잡을 수 없다. happy-dom은 레이아웃을 계산하지 +않으므로 `getBoundingClientRect` 기반 테스트도 불가능하다. 그래서 두 층으로 +나눈다: + +1. **회귀 테스트** — 겹침을 만든 CSS 선언의 부재를 잡는다. 레드 선행 확인. +2. **CDP 실측** — 실제 브라우저에서 좌표를 재고 스크린샷을 `view_image`로 본다. + +테스트는 원인을 지키고, 실측은 결과를 증명한다. + diff --git a/devlog/_plan/260902_multiplatform_qa_and_gui/011_wp2_outcome.md b/devlog/_plan/260902_multiplatform_qa_and_gui/011_wp2_outcome.md new file mode 100644 index 0000000000..962cee30b7 --- /dev/null +++ b/devlog/_plan/260902_multiplatform_qa_and_gui/011_wp2_outcome.md @@ -0,0 +1,54 @@ +# 011 — wp2 결과: 반응형 결함 두 건 + +## 무엇을 고쳤나 + +두 건 다 CSS를 읽어서가 아니라 **실제 기하를 재서** 찾았다. + +### 1. 모바일 상단바가 자기 자신과 겹쳤다 + +320px에서 배지가 `right=245`, 액션 오브가 `left=206`. 전원 버튼이 버전 +배지 위에 앉았다. flex 아이템은 **자기 자신이** `min-width: 0`을 가져야 콘텐츠 +크기 아래로 줄어드는데, `.mobile-topbar .brand`는 가졌고 그 자식들은 없었다. + +`.name`을 줄이는 것만으로는 부족했다. 320px 예산은 44(메뉴) + 26(로고) + +56(배지) + 94(액션) + 간격이라 이름에 약 38px만 남아 `op…`가 됐다. 겹침을 +고치고 가독성을 잃은 것이다. 그래서 배지를 뺀다 — 같은 brand 노드가 드로어 +헤드에도 마운트되고, 실제 버전 값은 대시보드 Version 스탯에도 있다. + +### 2. Integrations 카드가 페이지 밖으로 나갔다 + +`.integration-cards`가 `repeat(auto-fill, minmax(260px, 1fr))`이었다. +맨 260px 하한은 320px 뷰포트의 콘텐츠 박스보다 넓어서 트랙이 줄지 못하고 +카드가 함께 밀려났다. Settings 버튼이 `left=326, right=409`. 320/375/736 +세 폭에서 모두 재현됐다. + +`min(260px, 100%)`로 바꾸면 넓은 화면의 다단 의도는 유지하면서 좁은 화면은 +가진 너비로 물러난다. + +## 리뷰가 바꾼 것 + +grok 리뷰어가 near-pass를 주면서 브레이크포인트를 지적했다. 첫 시도는 +`@media (max-width: 400px)`를 새로 만들었는데, 그건 이 파일의 유일한 400px +규칙이었고 375-399 구간은 측정한 적도 없었다. 이미 있는 360px 블록에 합쳤다. + +지적이 옳았다는 건 수치로 확인된다. 375px에서 배지가 돌아오고 +`ver.right=245 < act.left=261`로 겹치지 않는다. 400px 규칙이었다면 375px에서 +배지가 불필요하게 사라졌을 것이다. + +## 검증 + +| 항목 | 결과 | +| --- | --- | +| 오버플로우 스캔 | 5 뷰포트(320/375/414/736/1024) × 5 페이지 = **0건** | +| 상단바 기하 | 320/360/375/414/736 전부 `name.right <= actions.left` | +| 배지 복귀 | 360px 이하 `display:none`, 375px 이상 `block` | +| 회귀 테스트 | 5 pass / 0 fail, 둘 다 레드 선행 확인 | + +전체 스위트는 돌리지 않았다(금지). 스크린샷은 `view_image`로 직접 봤다. + +## 남긴 것 + +잘린 `.name`의 `title`/`aria-label` 부재. 현재 `.name`은 하드코딩된 +라틴 문자열이고 360px에서도 전체가 표시되는 것이 측정으로 확인되어 이번 +범위 밖으로 둔다. i18n 브랜드 문자열이 생기면 그때 다시 본다. + diff --git a/devlog/_plan/260902_multiplatform_qa_and_gui/020_wp3_wp5_deploy_qa.md b/devlog/_plan/260902_multiplatform_qa_and_gui/020_wp3_wp5_deploy_qa.md new file mode 100644 index 0000000000..1010954def --- /dev/null +++ b/devlog/_plan/260902_multiplatform_qa_and_gui/020_wp3_wp5_deploy_qa.md @@ -0,0 +1,60 @@ +# 020 — wp3/wp4/wp5: 3-OS 배포와 QA + +호스트마다 설치 형태가 다르므로 절차도 다르다. 형태를 통일하지 않는다는 것이 +`000`의 복구 계약이고, 배포도 같은 원칙을 따른다. + +## wp3 — `macmini-cf` (macOS, 소스 체크아웃, 서비스 실행 중) + +가장 조심스러운 호스트다. 사용자의 실제 프록시가 launchd로 돌고 있다. + +1. 현재 상태 기록: `git -C ~/opencodex rev-parse HEAD`(0cc73411a), + `readlink ~/.bun/bin/ocx`, `launchctl list | grep opencodex`. +2. `git fetch && git checkout dev && git pull` → `bun install`. +3. QA: `ocx status`, `ocx ready --json`, `/healthz`, `/readyz`. +4. **복구**: `git checkout 0cc73411a`, `bun install`, 서비스 재기동 확인. + +서비스를 내려야 한다면 반드시 다시 올린다. 내린 채로 끝나면 복구 실패다. + +## wp4 — `lidge`(Linux, npm 글로벌) + `intmb`(macOS, 미설치) + +**`lidge`**: 현재 `@bitkyc08/opencodex@2.21.0`. dev 기준으로 올리고 +QA 후 2.21.0으로 되돌린다. 되돌림 증거는 `npm ls -g` 출력이다. + +**`intmb`**: 아무것도 없다. 설치 전에 없는 것들을 목록으로 남긴다 +(`ocx`, `~/.opencodex`, npm 글로벌 엔트리, bun). 테스트 후 그 목록이 +다시 비어 있어야 한다. 부산물 하나라도 남으면 복구 실패다. + +## wp5 — `desktop-c795oh4` (Windows, MINGW64) + +현재 `@bitkyc08/opencodex@2.32.1`. 경로가 `/c/Users/user/AppData/Roaming/npm/ocx` +라 POSIX 셸 가정이 깨질 수 있다. Windows 고유 실패(경로 구분자, 심볼릭 링크 +권한, 서비스 등록)를 별도 시나리오로 본다. QA 후 2.32.1로 복원. + +## QA 시나리오 (`cxc-qa` §4 적대적 클래스) + +각 호스트에서 아래를 구동하고 `.codexclaw/evidence//qa//`에 +`invocation.txt` + 아티팩트 + `verdict.json`을 남긴다. + +| 클래스 | 무엇을 하나 | +| --- | --- | +| 정상 경로 | `ocx status`, `ocx ready --json`, `/healthz`, `/readyz` | +| 빈 입력 | 인자 없는 서브커맨드 | +| 오입력 | 없는 플래그, 잘못된 서브커맨드 | +| 경계 | 없는 라우트로 `capabilities --route` (exit 4 기대) | +| 반복 | 같은 명령 두 번 — 멱등성 | +| 좁은 뷰포트 + CJK | GUI 320/736px 렌더 (호스트가 GUI를 서빙할 때) | + +`NA`는 구조적으로 적용 불가할 때만 쓰고 이유를 적는다. 실행하지 못한 +시나리오는 skip이 아니라 FAIL이며 블로커를 함께 적는다. + +## wp6 — 스택 PR과 머지 + +work-phase 체인이 스택 모양이므로 PR도 스택으로 올린다. CI는 즉시 기다리지 +않고 후행 추적한다. 최종적으로 admin으로 전부 머지한다. + +## 제약 재확인 + +로컬 전체 스위트 금지. 푸시는 `--no-verify`. `dev`/`main`/`preview` +직접 푸시 금지. 원격 호스트의 사용자 자격증명과 `~/.opencodex` 내부 계정 +데이터는 건드리지 않는다. + diff --git a/devlog/_plan/260902_multiplatform_qa_and_gui/021_wp3_macmini_outcome.md b/devlog/_plan/260902_multiplatform_qa_and_gui/021_wp3_macmini_outcome.md new file mode 100644 index 0000000000..0cccc48c24 --- /dev/null +++ b/devlog/_plan/260902_multiplatform_qa_and_gui/021_wp3_macmini_outcome.md @@ -0,0 +1,68 @@ +# 021 — wp3 결과: `macmini-cf` (macOS, 소스 체크아웃) + +사용자의 실제 프록시가 launchd로 돌고 있는 호스트다. 배포보다 복구가 어려운 +쪽이었고, 실제로 복구에서 한 번 틀렸다. + +## 사전 상태 + +``` +HEAD = 0cc73411aec36699aa30e98156a685665d8d8e5b +BRANCH = dev +DIRTY = 0 +LINK = ~/opencodex/bin/ocx.mjs +SVC = 80761 com.opencodex.proxy +서빙 버전 = 2.39.0, /healthz 200 +``` + +파일로 남겼다. 나중에 대조할 것이 없으면 "복구했다"는 주장은 검증 불가능하다. + +## 배포 + +`git pull --ff-only origin dev` → `0d8147c20`, `bun install`은 변경 없음. +`launchctl kickstart -k`로 재기동한 뒤 `/healthz`가 `version 2.40.0`을 +응답했다. 소스만 바꾸고 끝내면 서비스는 옛 코드를 계속 들고 있으므로, +**서빙되는 버전 문자열**까지 확인해야 배포가 증명된다. + +## QA — 적대적 클래스 + +| 클래스 | 명령 | 결과 | +| --- | --- | --- | +| 정상 | `/readyz` | 200 | +| 정상 | `ocx status` | running PID 64052, health ok | +| 정상 | `ocx ready --json` | `{"ready":true,"status":"ready"}` | +| 빈 입력 | `ocx provider` | usage 출력, exit 0 | +| 오입력 | `ocx status --nope` | usage 출력, **exit 1** | +| 경계 | `ocx capabilities --route /api/does-not-exist` | **exit 4** | +| 반복 | `/healthz` ×2 | 200 / 200 | +| 미지 라우트 | `/v1/nope` | 404 | + +종료 코드는 파이프 없이 다시 쟀다. `| head`를 통과시키면 파이프라인의 마지막 +명령 코드가 나와서 전부 0으로 보인다 — 처음 측정이 그랬다. `exit 4`는 +`skills/ocx/SKILL.md`가 "not found"로 문서화한 값이고, 실제와 일치한다. + +## 복구에서 한 번 틀렸다 + +`git checkout 0cc73411a`로 커밋과 버전(2.39.0)은 되돌아왔다. 그런데 사전 +상태 파일과 대조하니 `BRANCH`가 `dev`가 아니라 `HEAD`였다 — detached +HEAD로 남은 것이다. + +커밋이 같으니 동작은 같지만 **원래 상태는 아니다**. 사용자가 다음에 +`git pull`을 하면 detached HEAD에서 실패한다. `git branch -f dev ` + +`git checkout dev`로 브랜치 정체성까지 복원했다. + +이걸 잡은 건 사전 상태를 **파일로** 남겼기 때문이다. 기억으로 대조했다면 +"커밋 같으니 됐다"로 넘어갔을 것이다. + +## 최종 확인 + +``` +BRANCH = dev (사전과 일치) +HEAD = 0cc73411a... (사전과 일치) +DIRTY = 0 (사전과 일치) +LINK = ~/opencodex/bin/ocx.mjs (사전과 일치) +SVC = 64484 com.opencodex.proxy (PID는 재기동으로 바뀜, 실행 상태 일치) +버전 = 2.39.0 (사전과 일치) +``` + +증거: `.codexclaw/evidence//qa/macmini-{prestate,deploy,cli-adversarial,restore}/` + diff --git a/devlog/_plan/260902_multiplatform_qa_and_gui/022_wp4_linux_macbook_outcome.md b/devlog/_plan/260902_multiplatform_qa_and_gui/022_wp4_linux_macbook_outcome.md new file mode 100644 index 0000000000..1790ab3f98 --- /dev/null +++ b/devlog/_plan/260902_multiplatform_qa_and_gui/022_wp4_linux_macbook_outcome.md @@ -0,0 +1,69 @@ +# 022 — wp4 결과: `lidge`(Linux) + `intmb`(macOS 미설치) + +## `lidge` — npm 글로벌 + +`2.21.0` → `2.39.0` 갱신 후 QA, 다시 `2.21.0`으로 복원했다. + +`ocx`가 PATH에 없어서 `npm root -g`로 실제 경로를 찾아 실행했다 +(`~/.local/lib/node_modules/@bitkyc08/opencodex`). PATH 부재는 +설치 문제가 아니라 이 호스트의 셸 설정이고, 사용자 환경이므로 건드리지 않았다. + +CLI 계약이 macOS와 동일하다: 빈 입력 exit 0, 오플래그 exit 1, 미매칭 라우트 +exit 4. 프록시를 띄우지 않은 상태의 `ready --json`은 +`{"ready":false,"status":"unreachable"}` — 이게 올바른 응답이다. + +실제 기동도 확인했다. `OPENCODEX_HOME`을 `mktemp -d`로 잡아 사용자 설정을 +건드리지 않고 `--port 10777`로 띄웠다: healthz 200(version 2.39.0), +readyz 200, 반복 200/200, `/v1/nope` 404, GUI `/` 200. `stop` 후 +000(down). 임시 홈은 삭제했다. + +## `intmb` — 아무것도 없는 호스트 + +node, npm, bun 전부 없었다. 시스템에 런타임을 설치하는 것은 되돌리기 어려운 +외부 상태 변경이라 다른 길을 찾았다: `~/.nvm`에 node `22.22.3`이 이미 +있었고, `npm i --prefix $(mktemp -d)`로 격리 설치했다. 전역 npm은 손대지 +않았다. + +### 예상하지 못한 것 — `ocx start`가 사용자 Codex 설정을 주입한다 + +프록시를 띄웠더니 로그에 이렇게 찍혔다: + +``` +Pointed Codex's built-in openai provider at the opencodex proxy (openai_base_url). + Codex model catalog: ~/.codex/opencodex-catalog.json +WARNING: 4 Codex app-server process(es) still running ... +``` + +이 호스트에는 사용자의 실제 Codex가 돌고 있었다. `start`는 설계상 Codex를 +프록시로 향하게 하는 것이고 `stop`이 되돌린다 — 실제로 `config.toml`의 +opencodex 참조는 정지 후 0건이었다. 계약은 지켜졌다. + +그런데 `stop`이 되돌리지 않는 부산물이 남았다: + +``` +~/.codex/.opencodex-native-main.claim.sqlite +~/.codex/.opencodex-native-main.owner.sqlite +~/.codex/opencodex-catalog.json +``` + +사전 목록에 없던 파일이므로 복구 대상이다. 이름을 하나씩 지정해 삭제했다 — +글롭이나 재귀 삭제는 쓰지 않았다. 삭제 후 `~/.codex`에 opencodex 흔적 0건, +`config.toml` opencodex 참조 0건. + +**교훈:** 미설치 호스트에서 `ocx start`를 부르는 것은 "설치 테스트"가 아니라 +"사용자 Codex 설정 변경"이다. 사전 부재 목록을 파일로 남겨두지 않았다면 이 +세 파일은 그대로 남았을 것이다. + +## 복구 대조 + +| 항목 | `lidge` 사전 → 사후 | `intmb` 사전 → 사후 | +| --- | --- | --- | +| npm 글로벌 | 2.21.0 → **2.21.0** | none → **none** | +| `ocx` PATH | none → **none** | none → **none** | +| `~/.opencodex` | 존재 → **존재** | none → **none** | +| bun | 1.3.14 → **1.3.14** | none → **none** | +| node/npm | (시스템) | none → **none** | +| 포트/임시파일 | free / none | free / none | + +증거: `.codexclaw/evidence//qa/{lidge,intmb}-*/` + diff --git a/devlog/_plan/260902_multiplatform_qa_and_gui/023_wp5_windows_outcome.md b/devlog/_plan/260902_multiplatform_qa_and_gui/023_wp5_windows_outcome.md new file mode 100644 index 0000000000..b87768b9fa --- /dev/null +++ b/devlog/_plan/260902_multiplatform_qa_and_gui/023_wp5_windows_outcome.md @@ -0,0 +1,76 @@ +# 023 — wp5 결과: `desktop-c795oh4` (Windows / MINGW64) + +## 사전 상태 + +``` +npm 글로벌 = @bitkyc08/opencodex@2.32.1 +ocx = ~/AppData/Roaming/npm/ocx +~/.opencodex = 존재 +node v24.19.0, bun 1.3.14 +프록시 PID 26208 가동 중 (uptime 638755s = 약 7.4일) +``` + +## QA + +CLI 계약이 세 OS에서 동일하다. 빈 입력 exit 0, 오플래그 exit 1, 미매칭 라우트 +exit 4. HTTP도 같다: healthz/readyz 200, 반복 200/200, `/v1/nope` 404, +GUI `/` 200. + +### 버전 스큐 경고가 실제로 동작한다 + +CLI를 2.39.0으로 올렸는데 실행 중인 프록시는 2.32.1이었다. `ocx status`가 +이렇게 말했다: + +``` +CLI 2.39.0 does not match the running proxy 2.32.1 — this ocx on PATH is +stale. Its help and features describe a different build. +``` + +`skills/ocx/SKILL.md`가 "관리 명령 전 3단계" 중 2단계로 문서화한 바로 그 +확인이다. 문서에만 있는 규칙이 아니라 런타임이 실제로 잡아준다. + +## 복구 — Windows 고유 실패 + +첫 시도가 실패했다. npm이 이렇게 경고했다: + +``` +npm warn cleanup [Error: EPERM: operation not permitted, unlink + '...AppData/Roaming/npm/node_modules/@bitkyc08/.opencodex-*/node_modules/bun/bin/bun.exe'] +``` + +실행 중인 프록시가 `bun.exe`를 잡고 있어서 npm이 교체하지 못했다. POSIX라면 +열린 파일도 unlink되지만 Windows는 잠긴 실행 파일을 지우지 못한다. + +그런데 npm은 이걸 `warn cleanup`으로 출력하고 종료 코드는 성공처럼 흘려보낸다 +— 그래서 설치가 된 줄 알고 넘어갈 뻔했다. 사후 대조에서 버전이 여전히 +2.39.0인 것을 보고 잡았다. + +순서를 바꿔 해결했다: `ocx stop` → 재설치 → `ocx start`. + +## 최종 대조 + +| 항목 | 사전 | 사후 | +| --- | --- | --- | +| npm 글로벌 | 2.32.1 | **2.32.1** | +| `ocx` 경로 | AppData/Roaming/npm/ocx | **동일** | +| `~/.opencodex` | 존재 | **존재** | +| CLI 버전 | 2.32.1 | **2.32.1** | +| 프록시 | 가동(PID 26208) | **가동(PID 5988), healthz 200, served 2.32.1** | +| 스큐 경고 | 없음 | **없음** | + +PID가 바뀐 것은 재기동 때문이고, 프록시가 다시 떠 있다는 사실이 복구의 기준이다. + +## 세 OS 공통 결과 + +| | macOS | Linux | Windows | +| --- | --- | --- | --- | +| 빈 입력 | exit 0 | exit 0 | exit 0 | +| 오플래그 | exit 1 | exit 1 | exit 1 | +| 미매칭 라우트 | exit 4 | exit 4 | exit 4 | +| healthz / readyz | 200 | 200 | 200 | +| 미지 라우트 | 404 | 404 | 404 | +| GUI `/` | 200 | 200 | 200 | + +플랫폼 고유 차이는 **복구 절차**에만 나타났다: macOS는 브랜치 정체성, +Windows는 파일 잠금. 런타임 계약 자체는 세 OS에서 같다. + diff --git a/devlog/_plan/260902_multiplatform_qa_and_gui/090_outcome.md b/devlog/_plan/260902_multiplatform_qa_and_gui/090_outcome.md new file mode 100644 index 0000000000..4fe9a1c39e --- /dev/null +++ b/devlog/_plan/260902_multiplatform_qa_and_gui/090_outcome.md @@ -0,0 +1,52 @@ +# 090 — 유닛 종합 + +세 OS에 dev를 배포해 구동을 확인하고, 각 호스트를 원래 형태로 되돌리고, +대시보드의 반응형 결함 두 건을 고쳤다. + +## 결함 세 건 — 전부 측정으로 찾았다 + +코드를 읽어서 찾은 것은 하나도 없다. 실제 브라우저 기하와 실제 원격 상태를 +재서 나왔다. + +| 결함 | 어떻게 드러났나 | +| --- | --- | +| 모바일 상단바 겹침 | CDP 좌표: `ver.right=245` vs `actions.left=206` | +| Integrations 카드 오버플로우 | 5뷰포트×5페이지 스캔에서 `right=409` (뷰포트 320) | +| macmini detached HEAD | 사전상태 파일과 사후 대조 | + +세 번째가 특히 그렇다. 커밋 해시는 맞았으므로 "복구했다"고 말할 수 있었고, +기억으로 대조했다면 그렇게 넘어갔을 것이다. 사전 상태를 파일로 남겨둔 것이 +유일한 차이였다. + +## 리뷰가 바꾼 것 + +grok 리뷰어가 상단바 수정에 near-pass를 주면서 `@media (max-width: 400px)`가 +이 파일의 유일한 400px 규칙이고 375-399 구간은 측정된 적 없다고 지적했다. +기존 360px 블록으로 옮겼고, 375px 재측정에서 배지가 정상 복귀하는 것을 +확인했다 — 400px 규칙이었다면 375px에서 불필요하게 사라졌을 것이다. + +## 원격 호스트에서 배운 것 + +**`ocx start`는 미설치 호스트에서도 사용자 Codex 설정을 건드린다.** `intmb`는 +opencodex가 없었지만 Codex는 돌고 있었고, `start`가 설계대로 프록시를 향하게 +주입했다. `stop`이 config는 되돌렸지만 `~/.codex`에 sqlite 두 개와 카탈로그 +하나를 남겼다. 사전 부재 목록이 없었다면 그대로 남았을 것이다. + +**Windows npm은 실패를 경고로 낮춘다.** 실행 중 프록시가 `bun.exe`를 잠가 +EPERM이 났는데 `npm warn cleanup`으로 출력되고 종료 코드는 성공처럼 흘렀다. +사후 버전 대조가 아니었으면 복구 실패를 성공으로 기록할 뻔했다. + +**런타임 계약은 세 OS에서 같다.** 종료 코드(0/1/4), HTTP 상태(200/404), +GUI 서빙까지 동일했다. 차이는 전부 복구 절차 쪽이었다. + +## 검증 경계 + +로컬 전체 스위트는 돌리지 않았다. 실행한 테스트는 `gui` 포커스 2파일 +(5 pass)뿐이고, 나머지는 exact-head 원격 CI와 원격 호스트 실측이다. 모든 +푸시는 `--no-verify`, `dev` 직접 푸시 0건. + +## 정리 + +네 호스트 전부 사전 상태와 항목별로 일치하는 것을 확인했다. 로컬 데모 +프록시도 정지했고 포트 10399/10401에 리스너가 없다. + diff --git a/gui/src/styles-integrations.css b/gui/src/styles-integrations.css index f1b59d9751..5708f4dc8c 100644 --- a/gui/src/styles-integrations.css +++ b/gui/src/styles-integrations.css @@ -17,7 +17,12 @@ the CONTENT height, because a two-line config path pushed the action row down while a one-line one did not. Reserving the detail line's height keeps the switches on one baseline across the row. */ -.integration-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; list-style: none; padding: 0; margin: 14px 0; } +/* `minmax(260px, …)` is a floor the track cannot go under, so on a 320px viewport + the card stayed 260px wide inside a content box narrower than that and its action + row spilled out — measured at left=326, right=409 against a 320px page. Wrapping + the floor in `min()` keeps the two-column intent on wide screens while letting a + narrow one fall back to the width it actually has. */ +.integration-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(260px, 100%), 1fr)); gap: 12px; list-style: none; padding: 0; margin: 14px 0; } /* One full-width row, not a wide card: no grid cell, no hover border, no stretched title. `flex-wrap` is what keeps long German/Russian action copy @@ -40,7 +45,9 @@ .integration-card-link:focus-visible { outline: none; } .integration-card-link:focus-visible::after { outline: 2px solid var(--accent-ring); outline-offset: -2px; } .integration-card:hover { border-color: var(--accent-ring); } -.integration-card-actions { position: relative; z-index: 1; display: flex; align-items: center; gap: 10px; margin-top: auto; } +/* The row that carried the overflow outward: a long action label used to push the + whole row past the card edge instead of moving to a second line. */ +.integration-card-actions { position: relative; z-index: 1; display: flex; flex-wrap: wrap; min-width: 0; align-items: center; gap: 10px; margin-top: auto; } .integration-card-actions .btn { margin-left: auto; } .integration-empty { padding: 20px; border: 1px dashed var(--border); border-radius: var(--radius); text-align: center; color: var(--muted); } diff --git a/gui/src/styles.css b/gui/src/styles.css index df63631504..38880aa0aa 100644 --- a/gui/src/styles.css +++ b/gui/src/styles.css @@ -2269,6 +2269,18 @@ button.prov-account-row.active { cursor: default; } -webkit-backdrop-filter: var(--glass-blur); } .mobile-topbar .brand { flex: 1 1 auto; min-width: 0; padding: 4px; } + /* A flex item only shrinks past its content when it carries `min-width: 0` itself. + The brand had it; its children did not, so `.name` held its intrinsic width and + `.ver` was pushed under the action orbs. Giving `.name` the shrink is necessary + but not sufficient: at 320px the row budget is 44 (menu) + 26 (logo) + 56 (badge) + + 94 (actions) + gaps, which leaves the product name about 38px — "op…". The + badge is the thing to drop instead. It is duplicated in the drawer brand, and a + truncated product name is worse than a version the user can still read one tap + away. */ + .mobile-topbar .brand .name { + min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; + } + .mobile-topbar .brand .ver { flex-shrink: 0; } .mobile-topbar .stop-toggle { width: auto; min-width: 44px; min-height: 44px; justify-content: center; padding: 8px; } /* Both orbs keep the 44x44 touch target the single stop button had; a bare 28px .sidebar-orb would be a regression on the surface where it matters most. */ @@ -2353,6 +2365,13 @@ button.prov-account-row.active { cursor: default; } } @media (max-width: 360px) { + /* The topbar row budget at this width is 44 (menu) + 26 (logo) + 56 (badge) + + 94 (actions) plus gaps, which leaves the product name about 38px — it rendered + as "op…". The badge is the thing to drop: the same brand node is mounted again + in the drawer head, so the version stays one tap away, and the live value is + also on the dashboard Version stat. Folded into the existing tiny-phone + breakpoint rather than inventing a 400px one for an unmeasured 375-399 band. */ + .mobile-topbar .brand .ver { display: none; } .usage-filters { width: 100%; flex-direction: column; align-items: stretch; } .usage-segmented { width: 100%; } .usage-segmented-btn { flex: 1 1 0; } diff --git a/gui/tests/integrations-card-overflow.test.ts b/gui/tests/integrations-card-overflow.test.ts new file mode 100644 index 0000000000..7b3237b456 --- /dev/null +++ b/gui/tests/integrations-card-overflow.test.ts @@ -0,0 +1,35 @@ +import { expect, test } from "bun:test"; + +const css = await Bun.file(new URL("../src/styles-integrations.css", import.meta.url)).text(); + +function rule(selector: string): string { + const start = css.indexOf(selector); + if (start < 0) throw new Error("selector not found: " + selector); + return css.slice(start, css.indexOf("}", start)); +} + +// Measured with CDP at 320px, 375px and 736px before this test existed: the +// "Settings" button in .integration-card-actions rendered at left=326, +// right=409 on a 320px viewport - 89px outside the page. The chain was +// button.btn-ghost > .integration-card-actions > .integration-card > +// .integration-cards, and .integration-cards used +// repeat(auto-fill, minmax(260px, 1fr)). +// +// A fixed 260px minimum is wider than the content box of a 320px viewport once +// the page padding is taken out, so the track could not shrink and the card +// overflowed with it. min() lets the track fall back to the available width. +test("integration cards cannot force a track wider than the viewport", () => { + const grid = rule(".integration-cards {"); + expect(grid).toContain("auto-fill"); + // The floor has to be viewport-relative, not a bare pixel value. + expect(grid).toMatch(/minmax\(\s*min\(/); +}); + +// The actions row is what carried the overflow outward. Wrapping keeps a long +// label from pushing the row past the card edge. +test("card actions wrap instead of pushing past the card", () => { + const actions = rule(".integration-card-actions {"); + expect(actions).toContain("flex-wrap: wrap"); + expect(actions).toContain("min-width: 0"); +}); + diff --git a/gui/tests/mobile-topbar-layout.test.ts b/gui/tests/mobile-topbar-layout.test.ts new file mode 100644 index 0000000000..fc26a80f66 --- /dev/null +++ b/gui/tests/mobile-topbar-layout.test.ts @@ -0,0 +1,55 @@ +import { expect, test } from "bun:test"; + +const css = await Bun.file(new URL("../src/styles.css", import.meta.url)).text(); + +function block(selector: string): string { + const start = css.indexOf(selector); + if (start < 0) throw new Error("selector not found: " + selector); + return css.slice(start, css.indexOf("}", start)); +} + +// Measured on a real 320px viewport through CDP before this test existed: +// .brand .ver ended at x=245 while .mobile-topbar-actions began at x=206 - a +// 39px overlap that put the power orb on top of the version badge. +// +// A flex item only shrinks below its content size when it carries min-width: 0 +// itself. ".mobile-topbar .brand" had it; its children did not, so .name held +// its intrinsic width and pushed .ver under the actions. +test("the mobile brand can shrink, so the version badge cannot reach the action orbs", () => { + const brand = block(".mobile-topbar .brand"); + expect(brand).toContain("min-width: 0"); + + const name = block(".mobile-topbar .brand .name"); + expect(name).toContain("min-width: 0"); + expect(name).toContain("overflow: hidden"); + expect(name).toContain("text-overflow: ellipsis"); + + const ver = block(".mobile-topbar .brand .ver"); + expect(ver).toContain("flex-shrink: 0"); +}); + +test("the topbar action orbs keep their touch target", () => { + const actions = block(".mobile-topbar-actions {"); + expect(actions).toContain("flex: 0 0 auto"); + const orb = block(".mobile-topbar-actions .sidebar-orb {"); + expect(orb).toContain("min-width: 44px"); + expect(orb).toContain("min-height: 44px"); +}); + +// Shrinking .name is necessary but not sufficient: the row budget at the +// narrowest width leaves it about 38px, which rendered as "op...". The badge is +// dropped instead - the same brand node is mounted again in the drawer head, so +// the version is one tap away, and the live value is also on the dashboard. +// +// It belongs in the tiny-phone breakpoint this stylesheet already uses. A review +// pass caught the first attempt inventing @media (max-width: 400px), which was +// the only 400px rule in the file and covered an unmeasured 375-399 band. +test("the badge is dropped at the existing tiny-phone breakpoint, not a new one", () => { + expect(css).not.toContain("max-width: 400px"); + + const tiny = css.indexOf("@media (max-width: 360px)"); + expect(tiny).toBeGreaterThan(-1); + const scope = css.slice(tiny, tiny + 700); + expect(scope).toContain(".mobile-topbar .brand .ver { display: none; }"); +}); + From 2e2da87b512bde90a33c53d60d16550b885b9bc5 Mon Sep 17 00:00:00 2001 From: Vadevious <56196048+Vadevious@users.noreply.github.com> Date: Tue, 1 Sep 2026 18:07:55 +0100 Subject: [PATCH 109/172] fix(codex): rotate accounts on wrapped quota failures (#3176) Co-authored-by: Vadevious --- .../docs/reference/configuration/providers.md | 10 +-- src/lib/errors.ts | 18 +++++ src/server/request-log.ts | 6 +- src/server/responses/core.ts | 62 ++++++++++++++-- tests/codex-quota-rejection.test.ts | 72 ++++++++++++++++++- tests/responses-account-label.test.ts | 66 ++++++++++++++++- 6 files changed, 219 insertions(+), 15 deletions(-) diff --git a/docs-site/src/content/docs/reference/configuration/providers.md b/docs-site/src/content/docs/reference/configuration/providers.md index 7cd68216ef..1fdf9bdbcd 100644 --- a/docs-site/src/content/docs/reference/configuration/providers.md +++ b/docs-site/src/content/docs/reference/configuration/providers.md @@ -254,10 +254,12 @@ separates new/unbound assignment, usage-based proactive switching, and failure r normally keeps affinity, but `quota` may rebind it on its next request after the usage threshold is crossed, while pause, cooldown, reauthentication, and failure handling can clear or move routing independently. An unbound request has no live account binding; this can include an existing visible -task after proxy restart or affinity reset. A pre-stream 429 or 402 retries once on an eligible -alternate account in the same request, even when usage-based proactive switching is off. Account -changes preserve and replay the conversation context, but provider-side prompt-cache reuse across -accounts is not guaranteed and the cache may need to warm again. +task after proxy restart or affinity reset. A pre-stream 429 or 402, or a 5xx response whose bounded +body explicitly reports quota exhaustion, retries once on an eligible alternate account in the same +request, even when usage-based proactive switching is off. The ordinary transient-5xx policy runs +first, so a wrapped quota response may make up to three sends on the exhausted account before pool +rotation. Account changes preserve and replay the conversation context, but provider-side +prompt-cache reuse across accounts is not guaranteed and the cache may need to warm again. On a **401/403**, App login clears that account's process-local affinity and requires reauthentication. On a **429**, opencodex honors `Retry-After`, starts the account cooldown, clears affinity, and may diff --git a/src/lib/errors.ts b/src/lib/errors.ts index 3aa554e95a..624917507c 100644 --- a/src/lib/errors.ts +++ b/src/lib/errors.ts @@ -4,6 +4,24 @@ export interface OcxErrorPayload { code: string | null; } +/** Canonical human-readable message paths used by Responses upstream failures. */ +export function upstreamErrorMessageFromPayload(payload: unknown): string | undefined { + if (!payload || typeof payload !== "object" || Array.isArray(payload)) return undefined; + const json = payload as { + error?: { message?: unknown }; + last_error?: { message?: unknown }; + response?: { + error?: { message?: unknown }; + incomplete_details?: { message?: unknown }; + }; + }; + const message = json.error?.message + ?? json.last_error?.message + ?? json.response?.error?.message + ?? json.response?.incomplete_details?.message; + return typeof message === "string" ? message : undefined; +} + /** OpenAI / Codex hard block for high-risk cybersecurity activity (HTTP 400 or mid-stream). */ export const CYBER_POLICY_ERROR_CODE = "cyber_policy"; export const CYBER_POLICY_FALLBACK_MESSAGE = "Request blocked by the upstream cybersecurity policy."; diff --git a/src/server/request-log.ts b/src/server/request-log.ts index 4194b103d9..a443ff1939 100644 --- a/src/server/request-log.ts +++ b/src/server/request-log.ts @@ -8,6 +8,7 @@ import { isClientClosedMessage, isCyberPolicyCode, isCyberPolicyMessage, + upstreamErrorMessageFromPayload, } from "../lib/errors"; import { CODEX_CONFIG_PATH, readRootTomlString } from "../codex/paths"; import { readCodexCatalogPath } from "../codex/catalog"; @@ -794,10 +795,7 @@ function captureUpstreamErrorParsed( logCtx.terminalIncompleteReason = reason.trim(); } if (logCtx.upstreamError) return; - const message = json?.error?.message - ?? json?.last_error?.message - ?? json?.response?.error?.message - ?? json?.response?.incomplete_details?.message; + const message = upstreamErrorMessageFromPayload(parsed); if (typeof message === "string" && message.trim()) { logCtx.upstreamError = redactSecretString(message).slice(0, 500); return; diff --git a/src/server/responses/core.ts b/src/server/responses/core.ts index 1f56ed979a..1e3fded495 100644 --- a/src/server/responses/core.ts +++ b/src/server/responses/core.ts @@ -241,6 +241,10 @@ import { } from "../lifecycle"; import { redactSecretString, sanitizeLogMetadataString } from "../../lib/redact"; import { readBoundedResponseBody } from "../../lib/bounded-body"; +import { + isRateLimitOrQuotaFailureMessage, + upstreamErrorMessageFromPayload, +} from "../../lib/errors"; import type { AdmissionLease } from "../../lib/admission"; import { supportedLadderFor } from "../effort-policy"; import { isThreadSpawnRequest } from "../effort-policy"; @@ -904,8 +908,40 @@ async function shouldRetryCodexPoolAccountModel400( } /** Pre-stream quota/billing rejections that warrant one alternate-account attempt (#584). */ -export function shouldRetryCodexPoolAccountQuota(response: Response): boolean { - return response.status === 402 || response.status === 429; +function codexQuotaFailureMessage(body: string): string | undefined { + try { + const payload = JSON.parse(body) as unknown; + const canonical = upstreamErrorMessageFromPayload(payload); + if (canonical !== undefined) return canonical; + if (typeof payload === "string") return payload; + if (!payload || typeof payload !== "object" || Array.isArray(payload)) return undefined; + const record = payload as Record; + if (typeof record.message === "string") return record.message; + return typeof record.error === "string" ? record.error : undefined; + } catch { + // Plain-text gateways remain supported. Valid JSON is inspected only at recognized + // message fields so echoed request content elsewhere cannot trigger account cooldown. + return body; + } +} + +export async function shouldRetryCodexPoolAccountQuota( + response: Response, + signal?: AbortSignal, +): Promise { + if (response.status === 402 || response.status === 429) return true; + if (response.status < 500 || response.status >= 600) return false; + try { + // Reject malformed UTF-8 instead of matching quota words around replacement characters. + const body = await readBoundedResponseBody(response.clone(), { signal, fatalUtf8: true }); + const message = body.displaySafe && !body.truncated + ? codexQuotaFailureMessage(body.text) + : undefined; + return message !== undefined + && isRateLimitOrQuotaFailureMessage(message); + } catch { + return false; + } } interface CodexPoolAccountRetryArgs { @@ -1130,6 +1166,19 @@ async function retryCodexPoolOnAlternateAccount( } } if (retryAuthCtx?.kind !== "pool" && retryAuthCtx?.kind !== "main-pool") { + // A body-confirmed quota response may arrive under HTTP 5xx. Without an alternate, + // the ordinary terminal recorder sees only that wire status and would misclassify it + // as transient, leaving the exhausted account immediately selectable next turn. + if (outcomeStatus !== firstResponse.status && (outcomeStatus === 429 || outcomeStatus === 402)) { + recordCodexUpstreamOutcome(config, firstAuthCtx.accountId, outcomeStatus, { + ...codexQuotaOutcomeMeta(firstResponse), + threadId: firstAuthCtx.affinityKey, + modelId: route.modelId, + probeLeaseId: codexProbeLeaseId(firstAuthCtx), + probeQuotaScope: codexProbeQuotaScope(firstAuthCtx), + writerGeneration: firstAuthCtx.writerGeneration, + }); + } return { kind: "no-alternate" }; } @@ -4255,9 +4304,14 @@ async function handleResponsesInner( options.abortSignal, )) { poolRetryOutcome = 400; - } else if (!authCtx.fixedAccount && shouldRetryCodexPoolAccountQuota(upstreamResponse)) { + } else if (!authCtx.fixedAccount && await shouldRetryCodexPoolAccountQuota( + upstreamResponse, + options.abortSignal, + )) { // Pre-stream only: once SSE has begun, mid-stream quota stays terminal. - poolRetryOutcome = upstreamResponse.status; + // ChatGPT sometimes wraps quota exhaustion in a generic 5xx. Normalize only + // body-confirmed cases to quota evidence so cooldown and rotation both apply. + poolRetryOutcome = upstreamResponse.status >= 500 ? 429 : upstreamResponse.status; } if (poolRetryOutcome !== undefined) { diff --git a/tests/codex-quota-rejection.test.ts b/tests/codex-quota-rejection.test.ts index 6e95d81e38..7cf0310c01 100644 --- a/tests/codex-quota-rejection.test.ts +++ b/tests/codex-quota-rejection.test.ts @@ -71,8 +71,76 @@ describe("Codex pre-stream quota rejection classification", () => { [429, true], [400, false], [503, false], - ])("selects pool-account retries synchronously for HTTP %i", (status, expected) => { - expect(shouldRetryCodexPoolAccountQuota(new Response(null, { status }))).toBe(expected); + ])("selects pool-account retries by HTTP %i", async (status, expected) => { + await expect(shouldRetryCodexPoolAccountQuota(new Response(null, { status }))).resolves.toBe(expected); + }); + + test("recognizes a quota message wrapped in HTTP 5xx without consuming the response", async () => { + const body = JSON.stringify({ error: { message: "The usage limit has been reached" } }); + const response = new Response(body, { status: 502 }); + + await expect(shouldRetryCodexPoolAccountQuota(response)).resolves.toBe(true); + expect(await response.text()).toBe(body); + }); + + test("does not match quota wording echoed outside JSON error.message", async () => { + const response = Response.json({ + error: { message: "upstream server error" }, + request: { input: "Explain the usage limit" }, + }, { status: 502 }); + await expect(shouldRetryCodexPoolAccountQuota(response)).resolves.toBe(false); + }); + + test.each([ + ["error.message", { error: { message: "The usage limit has been reached" } }], + ["last_error.message", { last_error: { message: "The usage limit has been reached" } }], + ["response.error.message", { response: { error: { message: "The usage limit has been reached" } } }], + ["response.incomplete_details.message", { + response: { incomplete_details: { message: "The usage limit has been reached" } }, + }], + ])("recognizes the canonical %s upstream message path", async (_path, payload) => { + await expect(shouldRetryCodexPoolAccountQuota( + Response.json(payload, { status: 502 }), + )).resolves.toBe(true); + }); + + test.each([ + ["JSON string", JSON.stringify("The usage limit has been reached")], + ["top-level message", JSON.stringify({ message: "The usage limit has been reached" })], + ["string error", JSON.stringify({ error: "The usage limit has been reached" })], + ])("recognizes the valid %s fallback shape", async (_shape, body) => { + await expect(shouldRetryCodexPoolAccountQuota( + new Response(body, { status: 502 }), + )).resolves.toBe(true); + }); + + test("recognizes a plain-text quota failure", async () => { + const response = new Response("The usage limit has been reached", { status: 502 }); + await expect(shouldRetryCodexPoolAccountQuota(response)).resolves.toBe(true); + }); + + test.each([ + [502, JSON.stringify({ error: { message: "upstream server error" } })], + [503, JSON.stringify({ error: { message: "servers overloaded" } })], + [502, "x".repeat(BOUNDED_BODY_MAX_BYTES + 1)], + ])("keeps unrelated or oversized HTTP %i failures transient", async (status, body) => { + await expect(shouldRetryCodexPoolAccountQuota(new Response(body, { status }))).resolves.toBe(false); + }); + + test("fails closed for malformed UTF-8 and an already-aborted read", async () => { + const malformed = new Uint8Array([ + 0x54, 0x68, 0x65, 0x20, 0xff, 0x20, 0x75, 0x73, 0x61, 0x67, 0x65, 0x20, 0x6c, 0x69, 0x6d, 0x69, 0x74, + ]); + await expect(shouldRetryCodexPoolAccountQuota( + new Response(malformed, { status: 502 }), + )).resolves.toBe(false); + + const controller = new AbortController(); + controller.abort(); + await expect(shouldRetryCodexPoolAccountQuota( + new Response("The usage limit has been reached", { status: 502 }), + controller.signal, + )).resolves.toBe(false); }); test.each([ diff --git a/tests/responses-account-label.test.ts b/tests/responses-account-label.test.ts index f17b58b18c..de7ff6e9e1 100644 --- a/tests/responses-account-label.test.ts +++ b/tests/responses-account-label.test.ts @@ -6,7 +6,11 @@ import { fallbackCodexAccountLogLabel } from "../src/codex/account-label"; import { saveCodexAccountCredential } from "../src/codex/account-store"; import { clearAccountQuota, updateAccountQuota } from "../src/codex/auth-api"; import { MAIN_CODEX_ACCOUNT_ID } from "../src/codex/main-account"; -import { clearCodexUpstreamHealth, clearThreadAccountMap } from "../src/codex/routing"; +import { + clearCodexUpstreamHealth, + clearThreadAccountMap, + getCodexUpstreamHealth, +} from "../src/codex/routing"; import type { RequestLogContext } from "../src/server/request-log"; import { handleResponses } from "../src/server/responses"; import type { OcxConfig } from "../src/types"; @@ -144,4 +148,64 @@ describe("Responses account usage attribution", () => { expect(logCtx.activeAttempt?.accountLogLabel).toBe(fallbackCodexAccountLogLabel("pool-b")); }); }); + + test("a quota message wrapped in HTTP 502 cools the account and retries an alternate", async () => { + await withPoolHome(async () => { + const config = poolConfig(["pool-a", "pool-b"]); + for (const id of ["pool-a", "pool-b"]) { + savePoolCredential(id); + updateAccountQuota(id, id === "pool-a" ? 10 : 20); + } + const bearers: string[] = []; + globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => { + const bearer = new Headers(init?.headers).get("authorization") ?? ""; + bearers.push(bearer); + if (bearer === "Bearer pool-a-access-token") { + return Response.json({ error: { message: "The usage limit has been reached" } }, { + status: 502, + }); + } + return completedResponse("pool-b-response"); + }) as typeof fetch; + + const response = await handleResponses(request(), config, { model: "", provider: "" }, {}); + + expect(response.status).toBe(200); + expect(bearers).toEqual([ + "Bearer pool-a-access-token", + "Bearer pool-a-access-token", + "Bearer pool-a-access-token", + "Bearer pool-b-access-token", + ]); + expect(getCodexUpstreamHealth("pool-a")).toMatchObject({ + lastFailureStatus: 429, + cooldownSource: "default", + }); + expect(getCodexUpstreamHealth("pool-a")?.cooldownUntil).toBeGreaterThan(Date.now()); + }); + }); + + test("a wrapped quota failure cools a sole account when no alternate exists", async () => { + await withPoolHome(async () => { + const config = poolConfig(["pool-a"]); + savePoolCredential("pool-a"); + updateAccountQuota("pool-a", 10); + let sends = 0; + globalThis.fetch = (async () => { + sends += 1; + return Response.json({ error: { message: "The usage limit has been reached" } }, { + status: 502, + }); + }) as typeof fetch; + + const response = await handleResponses(request(), config, { model: "", provider: "" }, {}); + + expect(response.status).toBe(502); + expect(sends).toBe(3); + expect(getCodexUpstreamHealth("pool-a")).toMatchObject({ + cooldownSource: "default", + }); + expect(getCodexUpstreamHealth("pool-a")?.cooldownUntil).toBeGreaterThan(Date.now()); + }); + }); }); From 0d6424f80d0a6c28d2abc4816029944c5dade61f Mon Sep 17 00:00:00 2001 From: ingwannu Date: Wed, 2 Sep 2026 02:08:12 +0900 Subject: [PATCH 110/172] fix(responses): surface provider 413 as terminal context overflow (#3177) Co-authored-by: Ingwannu --- .../content/docs/reference/architecture.md | 6 + src/server/responses/context-overflow.ts | 49 ++++ src/server/responses/core.ts | 33 ++- structure/04_transports-and-sidecars.md | 33 +++ tests/responses-context-overflow.test.ts | 230 ++++++++++++++++++ 5 files changed, 350 insertions(+), 1 deletion(-) create mode 100644 src/server/responses/context-overflow.ts create mode 100644 tests/responses-context-overflow.test.ts diff --git a/docs-site/src/content/docs/reference/architecture.md b/docs-site/src/content/docs/reference/architecture.md index 2aecc1e18a..4641baf022 100644 --- a/docs-site/src/content/docs/reference/architecture.md +++ b/docs-site/src/content/docs/reference/architecture.md @@ -150,6 +150,12 @@ upstream WS responses keep the downstream SSE contract and bypass `tee()` throug single-reader relay (4 MiB per raw/enveloped frame and an 8 MiB producer queue). Queue overflow closes the upstream and emits a terminal downstream `response.failed` event followed by `[DONE]`. +When a provider rejects a streaming request with HTTP 413 before SSE begins, OpenCodex emits one +terminal `response.failed` event with `context_length_exceeded` instead of relaying the retryable +unknown status. This lets Codex stop its reconnect loop and apply its own context-compaction policy +on the next turn. OpenCodex does not silently delete prompts or images; reduce the current input or +retry after compaction. Non-streaming API callers continue to receive the provider's HTTP 413. + Codex context compaction works for routed models. `server/responses/compact.ts` handles `POST /v1/responses/compact` by running an internal routed summarization turn and returning compacted history, while `responses/parser.ts` and `bridge.ts` handle remote compaction v2 diff --git a/src/server/responses/context-overflow.ts b/src/server/responses/context-overflow.ts new file mode 100644 index 0000000000..5b5e7f9fd3 --- /dev/null +++ b/src/server/responses/context-overflow.ts @@ -0,0 +1,49 @@ +import { bridgeToResponsesSSE } from "../../bridge"; +import type { TranslatorBudget } from "../../lib/translator-budget"; +import type { AdapterEvent } from "../../types"; + +export const PROVIDER_INPUT_TOO_LARGE_MESSAGE = + "The provider rejected this turn because its input exceeds the provider size or context limit. Reduce the current input or compact the conversation before retrying."; + +async function* contextOverflowEvents(): AsyncGenerator { + yield { + type: "error", + message: PROVIDER_INPUT_TOO_LARGE_MESSAGE, + status: 413, + errorType: "invalid_request_error", + code: "context_length_exceeded", + retryable: false, + }; +} + +/** + * Convert a pre-stream provider 413 into the terminal Responses event Codex understands. + * + * Codex treats an HTTP 413 as an unexpected, retryable transport failure and resends the + * same oversized body through its reconnect budget. A `response.failed` event carrying + * `context_length_exceeded` is instead terminal and marks the client context as full, so + * its next-turn compaction policy can run. The message is proxy-owned on purpose: upstream + * 413 bodies can echo request data and are not needed to classify an unambiguous status. + */ +export function streamingContextOverflowResponse( + modelId: string, + translatorBudget: TranslatorBudget, +): Response { + return new Response(bridgeToResponsesSSE( + contextOverflowEvents(), + modelId, + undefined, + undefined, + undefined, + undefined, + 2_000, + { translatorBudget }, + ), { + headers: { + "Content-Type": "text/event-stream", + "Cache-Control": "no-cache", + Connection: "keep-alive", + "X-Accel-Buffering": "no", + }, + }); +} diff --git a/src/server/responses/core.ts b/src/server/responses/core.ts index 1e3fded495..0afaf11f17 100644 --- a/src/server/responses/core.ts +++ b/src/server/responses/core.ts @@ -374,6 +374,7 @@ import { } from "../responses-undeclared-tool-guard"; import { createGithubCopilotResponsesBlockRewrite } from "../github-copilot-responses-repair"; import { responsesJsonToSseStream } from "../responses-json-events"; +import { streamingContextOverflowResponse } from "./context-overflow"; import { guardTerminalEventStream } from "./terminal-guard"; import { emptyCompletionRetryEnabled, @@ -2151,7 +2152,7 @@ export async function handleComboResponses( comboId: string, config: OcxConfig, logCtx: RequestLogContext, - options: HandleResponsesOptions, + options: HandleResponsesOptions & { translatorBudget: TranslatorBudget }, ): Promise { const requestedModel = typeof (rawBody as { model?: unknown } | null)?.model === "string" ? (rawBody as { model: string }).model @@ -2500,6 +2501,12 @@ export async function handleComboResponses( code: failure.upstreamCode, }) === "stop") { adoptFailedChildLog(childLog); + if ( + failure.response.status === 413 + && (rawBody as { stream?: unknown } | null)?.stream === true + ) { + return streamingContextOverflowResponse(requestedModel, options.translatorBudget); + } return lastFailure; } console.warn( @@ -2513,6 +2520,12 @@ export async function handleComboResponses( if (!nextPick) adoptFailedChildLog(childLog); pick = nextPick; } + if ( + lastFailure?.status === 413 + && (rawBody as { stream?: unknown } | null)?.stream === true + ) { + return streamingContextOverflowResponse(requestedModel, options.translatorBudget); + } return lastFailure!; } @@ -3146,6 +3159,12 @@ async function handleResponsesInner( // instead of treating the first incompatible candidate as the end of the chain. The // distinct code is what lets the fallback layer tell the two apart -- an upstream // `context_length_exceeded` still stops, because retrying it elsewhere is guesswork. + if (clientRequestedStream && !options.comboAttempt) { + return streamingContextOverflowResponse( + parsed._responseModelId ?? parsed.modelId, + translatorBudget, + ); + } return formatErrorResponse( 413, "input_admission_refused", @@ -4486,6 +4505,12 @@ async function handleResponsesInner( // The bounded reader owns the original body, deadline, abort settlement, and lock. // Unsafe partial data falls back to #452's non-empty status-only JSON. const errorText = await readDisplaySafeErrorText(upstreamResponse, upstream.signal, ""); + if (upstreamResponse.status === 413 && clientRequestedStream) { + return streamingContextOverflowResponse( + parsed._responseModelId ?? parsed.modelId, + translatorBudget, + ); + } return formatPassthroughUpstreamError(upstreamResponse.status, errorText, { statusText: upstreamResponse.statusText, headers, @@ -6040,6 +6065,12 @@ async function handleResponsesInner( } finally { cleanupUpstreamAbort(); } + if (upstreamResponse.status === 413 && clientRequestedStream && !options.comboAttempt) { + return streamingContextOverflowResponse( + parsed._responseModelId ?? parsed.modelId, + translatorBudget, + ); + } if (!isFixedCodexAccount(authCtx)) { recordSubagentQuotaFailureForThreadSpawn( req.headers, diff --git a/structure/04_transports-and-sidecars.md b/structure/04_transports-and-sidecars.md index 546091e4fa..c73e7ff5c4 100644 --- a/structure/04_transports-and-sidecars.md +++ b/structure/04_transports-and-sidecars.md @@ -626,6 +626,39 @@ response headers/status and any 429 key rotations are handled eagerly. A failure SSE starts returns non-2xx JSON; once headers have started the final response, a generation failure is emitted as `response.failed` SSE. +### Pre-stream provider input overflow + +A provider HTTP 413 received before streaming starts is unambiguous request-size refusal, but raw +relay is not compatible with Codex: Codex classifies the unknown status as retryable and resends the +same oversized turn through its reconnect budget. For a streaming Responses caller, OpenCodex +therefore converts the final 413 (after any adapter-owned bounded image retry) into one HTTP-200 SSE +`response.failed` event with `error.code = context_length_exceeded` and `retryable = false`. Codex +recognizes that terminal contract, marks the context as full, and can run its own compaction policy +on the next turn. Combo routing treats 413 as a stop condition and performs the conversion only at +the outer client boundary, so the failed target is never recorded as a successful combo attempt. + +Non-streaming callers retain the original 413 status/body contract. The proxy never silently drops +prompts or images: it does not own the client's transcript, and deleting input would hide data that +was never analyzed. The streaming error message is proxy-owned and bounded instead of relaying the +upstream 413 body, which may echo request content. + +[Decision Log] +- 목적과 의도: Stop Codex from replaying a provider-rejected oversized turn and hand the failure + to the client's existing context-compaction semantics. +- 기존 구현 및 제약 조건: Providers can reject before SSE starts; Codex retries raw HTTP 413, + while it recognizes terminal `response.failed` `context_length_exceeded`; the proxy cannot edit + Codex's persisted transcript safely. +- 검토한 주요 대안: Relay 413 unchanged; return HTTP 400 JSON; silently remove media or old turns; + synthesize a successful assistant warning. +- 선택한 방식: Preserve 413 for non-streaming clients, but map the final streaming 413 to one + redacted non-retryable Responses failure at the outer request boundary. +- 다른 대안 대신 이 방식을 선택한 이유: Raw 413 causes a retry loop, HTTP JSON does not enter + Codex's context-window path, and silent deletion or fake success loses user intent without fixing + transcript ownership. +- 장점, 단점 및 영향: Codex stops reconnecting and can compact on the next turn; no input is + silently lost. The failed turn itself is not auto-replayed, and callers must retry after Codex + compacts or reduce the current input. + Kiro transient HTTP 429 recovery is coordinated process-wide after the first throttle: healthy traffic remains parallel, but throttled followers wait behind one abort-aware probe and share a deadline that is re-checked after every sleep. Event-stream `ThrottlingException` records the same diff --git a/tests/responses-context-overflow.test.ts b/tests/responses-context-overflow.test.ts new file mode 100644 index 0000000000..01fe138990 --- /dev/null +++ b/tests/responses-context-overflow.test.ts @@ -0,0 +1,230 @@ +import { afterEach, beforeEach, describe, expect, test } from "bun:test"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { saveConfig } from "../src/config"; +import { startServer } from "../src/server"; +import { PROVIDER_INPUT_TOO_LARGE_MESSAGE } from "../src/server/responses/context-overflow"; +import type { OcxConfig, OcxProviderConfig } from "../src/types"; + +let testDir = ""; +let previousOcxHome: string | undefined; +const upstreams: Array> = []; + +beforeEach(() => { + previousOcxHome = process.env.OPENCODEX_HOME; + testDir = mkdtempSync(join(tmpdir(), "ocx-context-overflow-")); + process.env.OPENCODEX_HOME = testDir; +}); + +afterEach(() => { + for (const upstream of upstreams.splice(0)) upstream.stop(true); + if (previousOcxHome === undefined) delete process.env.OPENCODEX_HOME; + else process.env.OPENCODEX_HOME = previousOcxHome; + if (testDir) rmSync(testDir, { recursive: true, force: true }); +}); + +function upstreamStatus(status: number, onHit?: () => void): ReturnType { + const upstream = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + fetch() { + onHit?.(); + return Response.json({ + detail: "request body too large; echoed private request marker should-not-reach-client", + }, { status }); + }, + }); + upstreams.push(upstream); + return upstream; +} + +function upstream413(onHit?: () => void): ReturnType { + return upstreamStatus(413, onHit); +} + +function provider( + adapter: "openai-responses" | "openai-chat" | "anthropic", + upstream: ReturnType, +): OcxProviderConfig { + return { + adapter, + baseUrl: `${String(upstream.url).replace(/\/$/, "")}/v1`, + authMode: "key", + apiKey: "test-context-overflow-key", + allowPrivateNetwork: true, + defaultModel: "kimi-k3", + }; +} + +function config(providers: Record): OcxConfig { + return { + port: 0, + hostname: "127.0.0.1", + defaultProvider: Object.keys(providers)[0]!, + providers, + } as OcxConfig; +} + +function request(serverUrl: string, model: string, stream: boolean, input?: unknown): Promise { + return fetch(new URL("/v1/responses", serverUrl), { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ + model, + stream, + input: input ?? [{ + type: "message", + role: "user", + content: [{ type: "input_text", text: "oversized turn" }], + }], + }), + }); +} + +async function responseFailed(response: Response): Promise> { + expect(response.status).toBe(200); + expect(response.headers.get("content-type")).toContain("text/event-stream"); + const text = await response.text(); + expect(text).not.toContain("should-not-reach-client"); + const frame = text.split("\n\n").find(block => block.startsWith("event: response.failed\n")); + expect(frame).toBeDefined(); + const data = frame!.split("\n").find(line => line.startsWith("data: "))?.slice(6); + expect(data).toBeDefined(); + return (JSON.parse(data!) as { response: Record }).response; +} + +describe("Responses provider input overflow", () => { + test("streaming passthrough and translated adapters emit a terminal context failure", async () => { + for (const adapter of ["openai-responses", "openai-chat"] as const) { + const upstream = upstream413(); + saveConfig(config({ target: provider(adapter, upstream) })); + const server = startServer(0); + try { + const failed = await responseFailed(await request(String(server.url), "target/kimi-k3", true)); + expect(failed.status).toBe("failed"); + expect(failed.retryable).toBe(false); + expect(failed.error).toEqual({ + message: PROVIDER_INPUT_TOO_LARGE_MESSAGE, + type: "invalid_request_error", + code: "context_length_exceeded", + }); + expect(failed.last_error).toEqual(failed.error); + } finally { + await server.stop(true); + } + } + }); + + test("non-streaming callers retain the upstream 413 status and body", async () => { + const upstream = upstream413(); + saveConfig(config({ target: provider("openai-responses", upstream) })); + const server = startServer(0); + try { + const response = await request(String(server.url), "target/kimi-k3", false); + expect(response.status).toBe(413); + expect(await response.json()).toEqual({ + detail: "request body too large; echoed private request marker should-not-reach-client", + }); + } finally { + await server.stop(true); + } + }); + + test("local input admission uses the same terminal streaming contract without upstream I/O", async () => { + let hits = 0; + const upstream = upstream413(() => { hits += 1; }); + const target = provider("openai-chat", upstream); + target.modelContextWindows = { "kimi-k3": 1 }; + saveConfig(config({ target })); + const server = startServer(0); + try { + const failed = await responseFailed(await request( + String(server.url), + "target/kimi-k3", + true, + [{ type: "message", role: "user", content: [{ type: "input_text", text: "x ".repeat(100) }] }], + )); + expect((failed.error as { code?: string }).code).toBe("context_length_exceeded"); + expect(hits).toBe(0); + } finally { + await server.stop(true); + } + }); + + test("the bounded Anthropic image retry runs once before the terminal failure", async () => { + let hits = 0; + const upstream = upstream413(() => { hits += 1; }); + saveConfig(config({ target: provider("anthropic", upstream) })); + const server = startServer(0); + try { + const failed = await responseFailed(await request( + String(server.url), + "target/kimi-k3", + true, + [{ + type: "message", + role: "user", + content: [ + { type: "input_text", text: "inspect" }, + { + type: "input_image", + image_url: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==", + }, + ], + }], + )); + expect((failed.error as { code?: string }).code).toBe("context_length_exceeded"); + expect(hits).toBe(2); + } finally { + await server.stop(true); + } + }); + + test("unrelated passthrough HTTP failures keep their status and body", async () => { + for (const status of [400, 503]) { + const upstream = upstreamStatus(status); + saveConfig(config({ target: provider("openai-responses", upstream) })); + const server = startServer(0); + try { + const response = await request(String(server.url), "target/kimi-k3", true); + expect(response.status).toBe(status); + expect(await response.json()).toEqual({ + detail: "request body too large; echoed private request marker should-not-reach-client", + }); + } finally { + await server.stop(true); + } + } + }); + + test("a combo stops on 413 and does not dispatch a second oversized target", async () => { + let firstHits = 0; + let secondHits = 0; + const first = upstream413(() => { firstHits += 1; }); + const second = upstream413(() => { secondHits += 1; }); + const next = config({ + first: provider("openai-chat", first), + second: provider("openai-chat", second), + }); + next.combos = { + fallback: { + strategy: "failover", + targets: [ + { provider: "first", model: "kimi-k3" }, + { provider: "second", model: "kimi-k3" }, + ], + }, + }; + saveConfig(next); + const server = startServer(0); + try { + const failed = await responseFailed(await request(String(server.url), "combo/fallback", true)); + expect((failed.error as { code?: string }).code).toBe("context_length_exceeded"); + expect(firstHits).toBe(1); + expect(secondHits).toBe(0); + } finally { + await server.stop(true); + } + }); +}); From 51c49177f59238d9e860895ffd76100c293ee4ff Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 02:11:21 +0900 Subject: [PATCH 111/172] fix(export): preserve Hermes vision capabilities (#3178) Co-authored-by: Ingwannu --- src/clients/config-export.ts | 21 ++++++++++++++----- structure/09_client-integrations.md | 17 +++++++++++++++ tests/cli-export-command.test.ts | 11 ++++++++-- .../client-config-export-new-clients.test.ts | 18 +++++++++++++--- tests/client-config-new-clients.test.ts | 10 ++++++--- tests/client-export-modality-enum.test.ts | 17 +++++++++++++++ tests/management-client-config-route.test.ts | 16 ++++++++++++++ 7 files changed, 97 insertions(+), 13 deletions(-) diff --git a/src/clients/config-export.ts b/src/clients/config-export.ts index 1ed156c425..50d6a201f1 100644 --- a/src/clients/config-export.ts +++ b/src/clients/config-export.ts @@ -1034,10 +1034,15 @@ export interface HermesProviderBlock { api_mode: "chat_completions"; /** We supply the list, so skip their live `/models` probe. */ discover_models: false; - models: string[]; + models: Record; extra_headers?: Record; } +/** Capability metadata Hermes cannot discover for a custom local provider. */ +export interface HermesModelEntry { + supports_vision?: boolean; +} + export interface HermesGeneratedConfig { providers: Record; } @@ -1311,7 +1316,13 @@ function proxyAdmissionHeaders(config: OcxConfig | undefined, envRef: string): R } function buildHermesClientConfig(ctx: ExportContext): HermesGeneratedConfig { - const models = normalizeExportModels(ctx.models).map(model => model.namespaced); + const models: Record = {}; + for (const model of normalizeExportModels(ctx.models)) { + const declared = model.inputModalities; + models[model.namespaced] = declared && declared.length > 0 + ? { supports_vision: declared.includes("image") } + : {}; + } const headers = proxyAdmissionHeaders(ctx.config, HERMES_API_KEY_ENV_REF); return { providers: { @@ -1606,9 +1617,9 @@ function summarizeOmp(document: unknown): { modelCount: number; modelsWithoutLim } function summarizeHermes(document: unknown): { modelCount: number; modelsWithoutLimits: number } { - const models = (document as HermesGeneratedConfig | undefined)?.providers?.[OPENCODE_PROVIDER_ID]?.models ?? []; - // Hermes carries selectors only; it has no per-model limit to be missing. - return { modelCount: models.length, modelsWithoutLimits: 0 }; + const models = (document as HermesGeneratedConfig | undefined)?.providers?.[OPENCODE_PROVIDER_ID]?.models ?? {}; + // Hermes carries capability metadata but no per-model limit to be missing. + return { modelCount: Object.keys(models).length, modelsWithoutLimits: 0 }; } function summarizeOpenclaw(document: unknown): { modelCount: number; modelsWithoutLimits: number } { diff --git a/structure/09_client-integrations.md b/structure/09_client-integrations.md index 7c535e07b9..fde4e5cf8a 100644 --- a/structure/09_client-integrations.md +++ b/structure/09_client-integrations.md @@ -36,6 +36,23 @@ Status and mutation must use the same classifier. A special case added only to a would be misleading because refresh or disable could still reject the same file; a special case added only to a writer would let a mutation bypass the state users saw. +## Hermes Model Capabilities + +Hermes cannot infer custom-provider capabilities from its built-in registry. The OpenCodex +provider therefore emits `models` as a mapping keyed by the canonical namespaced selector. An +explicit catalog modality list containing `image` becomes `supports_vision: true`; an explicit, +non-empty list without `image` becomes `false`; an absent or empty modality list keeps an empty +model object so Hermes receives no guessed capability. OpenCodex does not emit `supports_video` +because its authoritative input-modality vocabulary currently has no video value. + +[Decision Log] +- 목적과 의도: Preserve catalog-backed image routing when Hermes uses OpenCodex as a custom provider. +- 기존 구현 및 제약 조건: A string array preserved model selection but normalized to empty metadata in Hermes, while OpenCodex has authoritative text/image/audio facts but no video fact. +- 검토한 주요 대안: Keep the array; mark every model vision-capable; infer video from model names; emit a per-model metadata map from declared modalities. +- 선택한 방식: Emit a stable per-model map and include only the `supports_vision` boolean that the catalog can prove. +- 다른 대안 대신 이 방식을 선택한 이유: The map is the Hermes-supported capability boundary, while guesses would misroute attachments or advertise unsupported video. +- 장점, 단점 및 영향: Vision-capable custom models route correctly and text-only rows stay explicit; unknown rows remain unknown, and video routing waits for authoritative source metadata. + ## Ownership Axes `fileFingerprint` records the exact whole-file result for restore and for serializers that may lose diff --git a/tests/cli-export-command.test.ts b/tests/cli-export-command.test.ts index 0498a4df40..8aec793e1d 100644 --- a/tests/cli-export-command.test.ts +++ b/tests/cli-export-command.test.ts @@ -42,10 +42,11 @@ const ROWS = [ native: true, disabled: false, contextWindow: 272_000, + inputModalities: ["text", "image"], reasoningEfforts: ["low", "medium", "high", "xhigh", "max"], defaultReasoningEffort: "high", }, - { provider: "anthropic", id: "claude-opus-5", namespaced: "anthropic/claude-opus-5", disabled: false, contextWindow: 200_000, displayName: "Claude Opus 5" }, + { provider: "anthropic", id: "claude-opus-5", namespaced: "anthropic/claude-opus-5", disabled: false, contextWindow: 200_000, displayName: "Claude Opus 5", inputModalities: ["text"] }, { provider: "custom", id: "no-context", namespaced: "custom/no-context", disabled: false }, { provider: "banned", id: "hidden", namespaced: "banned/hidden", disabled: true, contextWindow: 100_000 }, ]; @@ -308,7 +309,13 @@ describe("ocx export argument validation (accept criterion 4)", () => { expect(yaml.code).toBe(0); const yamlText = readFileSync(yamlTarget, "utf8"); expect(yamlText.startsWith("providers:")).toBe(true); - expect(Bun.YAML.parse(yamlText)).toHaveProperty("providers.opencodex"); + const parsedYaml = Bun.YAML.parse(yamlText) as { + providers: { opencodex: { models: Record } }; + }; + expect(parsedYaml).toHaveProperty("providers.opencodex"); + expect(parsedYaml.providers.opencodex.models["gpt-5.6-luna"]).toEqual({ supports_vision: true }); + expect(parsedYaml.providers.opencodex.models["anthropic/claude-opus-5"]).toEqual({ supports_vision: false }); + expect(parsedYaml.providers.opencodex.models["custom/no-context"]).toEqual({}); const tomlTarget = join(tempDir(), "kimi-config.toml"); const toml = await run(["--client", "kimi", "--out", tomlTarget], { baseUrl: proxy.baseUrl }); diff --git a/tests/client-config-export-new-clients.test.ts b/tests/client-config-export-new-clients.test.ts index 4444547aa7..81cd5379b6 100644 --- a/tests/client-config-export-new-clients.test.ts +++ b/tests/client-config-export-new-clients.test.ts @@ -47,8 +47,8 @@ const LOOPBACK: OcxConfig = { const REMOTE: OcxConfig = { ...LOOPBACK, hostname: "0.0.0.0" } as OcxConfig; const MODELS: ExportModel[] = [ - { namespaced: "anthropic/claude-opus-4-8", provider: "anthropic", id: "claude-opus-4-8", contextWindow: 200_000, displayName: "Claude Opus 4.8" }, - { namespaced: "gpt-5.5", provider: "openai", id: "gpt-5.5", native: true, contextWindow: 400_000 }, + { namespaced: "anthropic/claude-opus-4-8", provider: "anthropic", id: "claude-opus-4-8", contextWindow: 200_000, displayName: "Claude Opus 4.8", inputModalities: ["text", "image"] }, + { namespaced: "gpt-5.5", provider: "openai", id: "gpt-5.5", native: true, contextWindow: 400_000, inputModalities: ["text"] }, { namespaced: "local/no-window", provider: "local", id: "no-window" }, ]; @@ -97,10 +97,22 @@ describe("hermes", () => { expect(block.api_key).toBe(HERMES_API_KEY_ENV_REF); expect(block.api_mode).toBe("chat_completions"); expect(block.discover_models).toBe(false); - expect(block.models).toEqual(["anthropic/claude-opus-4-8", "gpt-5.5", "local/no-window"]); + expect(block.models).toEqual({ + "anthropic/claude-opus-4-8": { supports_vision: true }, + "gpt-5.5": { supports_vision: false }, + "local/no-window": {}, + }); expect(doc).not.toHaveProperty("model"); }); + test("capability metadata survives the generated YAML round-trip", () => { + const built = buildClientConfigText("hermes", ctx()); + const parsed = Bun.YAML.parse(built.text) as HermesGeneratedConfig; + expect(parsed.providers[OPENCODE_PROVIDER_ID]!.models).toEqual( + (built.document as HermesGeneratedConfig).providers[OPENCODE_PROVIDER_ID]!.models, + ); + }); + test("a non-loopback bind adds the admission header, loopback does not", () => { const loopback = buildClientConfig("hermes", ctx()) as HermesGeneratedConfig; expect(loopback.providers[OPENCODE_PROVIDER_ID]!.extra_headers).toBeUndefined(); diff --git a/tests/client-config-new-clients.test.ts b/tests/client-config-new-clients.test.ts index eaf500ca54..00513d2047 100644 --- a/tests/client-config-new-clients.test.ts +++ b/tests/client-config-new-clients.test.ts @@ -24,8 +24,8 @@ import type { OcxConfig } from "../src/types"; * (devlog/_fin/260802_client_toggle_api/010 §2.4, 011 §3). */ const MODELS: ExportModel[] = [ - { namespaced: "anthropic/claude-opus-4-8", provider: "anthropic", id: "claude-opus-4-8", contextWindow: 200_000, displayName: "Claude Opus 4.8" }, - { namespaced: "gpt-5.5", provider: "openai", id: "gpt-5.5", native: true, contextWindow: 400_000 }, + { namespaced: "anthropic/claude-opus-4-8", provider: "anthropic", id: "claude-opus-4-8", contextWindow: 200_000, displayName: "Claude Opus 4.8", inputModalities: ["text", "image"] }, + { namespaced: "gpt-5.5", provider: "openai", id: "gpt-5.5", native: true, contextWindow: 400_000, inputModalities: ["text"] }, // No authoritative context window: the "never guess metadata" case. { namespaced: "mystery/model", provider: "mystery", id: "model" }, ]; @@ -76,7 +76,11 @@ describe("hermes", () => { expect(provider.api_key).toBe(HERMES_API_KEY_ENV_REF); expect(provider.api_mode).toBe("chat_completions"); expect(provider.discover_models).toBe(false); - expect(provider.models).toEqual(["anthropic/claude-opus-4-8", "gpt-5.5", "mystery/model"]); + expect(provider.models).toEqual({ + "anthropic/claude-opus-4-8": { supports_vision: true }, + "gpt-5.5": { supports_vision: false }, + "mystery/model": {}, + }); }); test("adds the admission header only on a non-loopback bind", () => { diff --git a/tests/client-export-modality-enum.test.ts b/tests/client-export-modality-enum.test.ts index bcefd5039e..dd6fd224ee 100644 --- a/tests/client-export-modality-enum.test.ts +++ b/tests/client-export-modality-enum.test.ts @@ -5,6 +5,7 @@ import { type ExportContext, type ExportModel, type GajaeGeneratedConfig, + type HermesGeneratedConfig, type PiGeneratedConfig, } from "../src/clients/config-export"; import type { OcxConfig } from "../src/types"; @@ -46,6 +47,11 @@ function gajaeModels(models: ExportModel[]) { .providers[OPENCODE_PROVIDER_ID].models; } +function hermesModels(models: ExportModel[]) { + return (buildClientConfig("hermes", ctx(models)) as HermesGeneratedConfig) + .providers[OPENCODE_PROVIDER_ID].models; +} + /** The live failure, by its real id and real modality list. */ const MIXED: ExportModel = { namespaced: "zenmux/meta-muse-spark-1.1", @@ -68,6 +74,17 @@ const AUDIO_ONLY: ExportModel = { }; describe("exported modalities stay inside the enum each client accepts", () => { + test("Hermes receives only catalog-backed vision booleans", () => { + const bare: ExportModel = { namespaced: "p/bare", provider: "p", id: "bare" }; + const empty: ExportModel = { ...bare, namespaced: "p/empty", id: "empty", inputModalities: [] }; + expect(hermesModels([MIXED, AUDIO_ONLY, bare, empty])).toEqual({ + "zenmux/meta-muse-spark-1.1": { supports_vision: true }, + "p/audio-only": { supports_vision: false }, + "p/bare": {}, + "p/empty": {}, + }); + }); + test("audio is dropped from a mixed Gajae entry rather than written through", () => { expect(gajaeModels([MIXED])[0]?.input).toEqual(["text", "image"]); }); diff --git a/tests/management-client-config-route.test.ts b/tests/management-client-config-route.test.ts index 5490e66a44..1eec60d0a6 100644 --- a/tests/management-client-config-route.test.ts +++ b/tests/management-client-config-route.test.ts @@ -18,6 +18,7 @@ import { opencodeGlobalConfigPath, type DshGeneratedConfig, type ExportModel, + type HermesGeneratedConfig, type McodeGeneratedConfig, type OpencodeGeneratedConfig, type PiGeneratedConfig, @@ -102,6 +103,7 @@ function baseConfig(overrides: Partial = {}): OcxConfig { liveModels: false, models: ["m1", "m2"], modelContextWindows: { m1: 128_000 }, + modelInputModalities: { m1: ["text", "image"], m2: ["text"] }, modelReasoningEfforts: { m1: ["none", "minimal", "low", "high"] }, }, b: { @@ -251,6 +253,20 @@ describe("GET /api/client-config", () => { }); }, 15_000); + test("Hermes response projects catalog vision metadata through YAML", async () => { + const response = await clientConfigApi(baseConfig(), "?client=hermes"); + expect(response.status).toBe(200); + const body = await response.json() as ClientConfigEnvelope; + const models = (body.config as HermesGeneratedConfig).providers[OPENCODE_PROVIDER_ID]!.models; + + expect(Bun.YAML.parse(body.text)).toEqual(body.config as Record); + expect(models["a/m1"]).toEqual({ supports_vision: true }); + // Effective catalog hints may widen ordinary text rows to image-capable. + expect(models["a/m2"]).toEqual({ supports_vision: true }); + expect(models["b/no-context"]).toEqual({}); + expect(body.modelCount).toBe(Object.keys(models).length); + }, 15_000); + test("an expired management roster is refreshed once before client-config is projected", async () => { writeFileSync(join(entitlementCodexHome, "auth.json"), JSON.stringify({ tokens: { access_token: "client-config-token", account_id: "client-config-account" }, From eceb02d9d331d3f97b8f0d338c2bcd951778eb5a Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 02:14:15 +0900 Subject: [PATCH 112/172] fix(remote): restore authenticated GUI health (#3179) Co-authored-by: Ingwannu --- .../docs/fr/reference/cli/lifecycle.md | 2 +- .../docs/fr/reference/configuration/server.md | 2 + .../docs/ja/reference/cli/lifecycle.md | 2 +- .../docs/ja/reference/configuration/server.md | 2 + .../docs/ko/reference/cli/lifecycle.md | 2 +- .../docs/ko/reference/configuration/server.md | 2 + .../docs/ru/reference/cli/lifecycle.md | 2 +- .../docs/ru/reference/configuration/server.md | 2 + .../docs/tr/reference/cli/lifecycle.md | 2 +- .../docs/tr/reference/configuration/server.md | 2 + .../docs/zh-cn/reference/cli/lifecycle.md | 2 +- .../zh-cn/reference/configuration/server.md | 2 + .../docs/zh-tw/reference/cli/lifecycle.md | 2 +- .../zh-tw/reference/configuration/server.md | 2 + .../components/MemoryObservabilityCard.tsx | 4 +- gui/src/pages/dashboard-core-poll.ts | 2 +- gui/tests/dashboard-contracts.test.ts | 3 +- gui/tests/memory-observability-card.test.tsx | 41 +++++++++++++++++++ src/server/management-api.ts | 2 +- src/server/management/context.ts | 2 + src/server/management/route-registry.ts | 1 + src/server/management/system-routes.ts | 14 ++++++- structure/05_gui-and-management-api.md | 2 +- tests/cli-capabilities.test.ts | 1 + tests/server-management-auth.test.ts | 31 +++++++++++--- 25 files changed, 112 insertions(+), 19 deletions(-) diff --git a/docs-site/src/content/docs/fr/reference/cli/lifecycle.md b/docs-site/src/content/docs/fr/reference/cli/lifecycle.md index 95c3fb5fbe..ac152db316 100644 --- a/docs-site/src/content/docs/fr/reference/cli/lifecycle.md +++ b/docs-site/src/content/docs/fr/reference/cli/lifecycle.md @@ -122,7 +122,7 @@ Vérifie l’identité du proxy actif. La sortie destinée aux utilisateurs indi ### `ocx ready [--json] [--wait [--timeout ]]` -Vérifie l’état de préparation après synchronisation au moyen du point de terminaison non authentifié `GET /readyz`. Il renvoie `200` lorsque le service est prêt, ou `503` avec `Retry-After: 1` pour les états `pending` et terminal `failed`. Son identité HTTP expurgée est `{service, version, uptime, pid, port, status}`. Les anciens proxys dépourvus de `/readyz` échouent de manière sûre avec l’état `unreachable` ; `/healthz` mesure la disponibilité du processus, et non son état de préparation. +Vérifie l’état de préparation après synchronisation au moyen du point de terminaison non authentifié `GET /readyz`. Il renvoie `200` lorsque le service est prêt, ou `503` avec `Retry-After: 1` pour les états `pending` et terminal `failed`. Son identité HTTP expurgée est `{service, version, uptime, pid, port, status, protocol, minimumClientProtocol, managementUrl}`. `protocol` est la version courante du protocole distant du hub, `minimumClientProtocol` la plus ancienne version cliente compatible et `managementUrl` l’origine canonique de gestion visible par le navigateur. Les anciens proxys dépourvus de `/readyz` échouent de manière sûre avec l’état `unreachable` ; `/healthz` mesure la disponibilité du processus, et non son état de préparation. Par défaut, la commande effectue une seule sonde. Avec `--wait`, elle interroge le service jusqu’à ce qu’il soit prêt ou jusqu’à l’expiration du délai, mais s’arrête immédiatement si elle observe l’état terminal `failed`. Le délai par défaut est de 45 secondes. `--timeout ` exige `--wait` et accepte un entier positif compris entre 1 et 300. La sortie JSON de la CLI est `{ready, status, pid, port}`, où `status` vaut `ready`, `pending`, `failed` ou `unreachable`. Les codes de sortie sont 0 si le service est prêt ; 1 s’il n’est pas prêt, reste en attente, échoue, dépasse le délai ou est inaccessible ; et 64 si les arguments sont invalides. diff --git a/docs-site/src/content/docs/fr/reference/configuration/server.md b/docs-site/src/content/docs/fr/reference/configuration/server.md index 72da38b139..bad04dd6de 100644 --- a/docs-site/src/content/docs/fr/reference/configuration/server.md +++ b/docs-site/src/content/docs/fr/reference/configuration/server.md @@ -255,3 +255,5 @@ compte et la charge de travail prévus. ## Clés Remote Hub et valeurs par défaut `runtimeRole` vaut `standalone` par défaut. Un hub utilise `hub.managementPublicOrigin`, `hub.managementIngress` limité au loopback (`enabled:false` si absent) et les identités exactes de `remoteGui.allowedTailscaleUsers` (liste vide si absente). La clé client reste dans `service-api-token`, jamais dans `config.json`; `service-api-token.prev` peut exister pendant une rotation. Les usages ne sont pas répliqués. + +`remoteGui.allowInsecureHttp` est un ancien no-op déprécié, conservé uniquement pour que les anciens fichiers passent encore le schéma strict. Supprimez-le de la configuration : les grants de pairing ne sont acceptés que sur loopback ou via HTTPS authentifié, et `true` ne réactive pas le pairing HTTP en clair. diff --git a/docs-site/src/content/docs/ja/reference/cli/lifecycle.md b/docs-site/src/content/docs/ja/reference/cli/lifecycle.md index 97f3f3fbd0..d6e9425b52 100644 --- a/docs-site/src/content/docs/ja/reference/cli/lifecycle.md +++ b/docs-site/src/content/docs/ja/reference/cli/lifecycle.md @@ -123,7 +123,7 @@ ocx status --json 認証不要の `GET /readyz` エンドポイントで同期後の準備状態を確認します。準備完了時は `200`、 `pending` または終端状態の `failed` では `Retry-After: 1` とともに `503` を返します。HTTP の -サニタイズ済み識別フィールドは `{service, version, uptime, pid, port, status}` です。`/readyz` がない +サニタイズ済み識別フィールドは `{service, version, uptime, pid, port, status, protocol, minimumClientProtocol, managementUrl}` です。`protocol` は hub の現在の remote protocol、`minimumClientProtocol` は互換性のある最小 client protocol、`managementUrl` は browser から見える canonical management origin です。`/readyz` がない 旧プロキシは `unreachable` として fail-closed し、`/healthz` は readiness ではなく別の liveness 確認です。 デフォルトでは 1 回だけ probe します。`--wait` は準備完了または timeout まで polling しますが、 終端 `failed` を確認すると即座に終了します。デフォルト timeout は 45 秒で、`--timeout ` には diff --git a/docs-site/src/content/docs/ja/reference/configuration/server.md b/docs-site/src/content/docs/ja/reference/configuration/server.md index a5e7af3385..86b6cbfa5f 100644 --- a/docs-site/src/content/docs/ja/reference/configuration/server.md +++ b/docs-site/src/content/docs/ja/reference/configuration/server.md @@ -165,3 +165,5 @@ Anthropic OAuth サイドカーは、opencodex の既存のクロード コー ## Remote Hub のキーと既定値 `runtimeRole` の既定値は `standalone` です。hub は `hub.managementPublicOrigin`、loopback 限定の `hub.managementIngress`(未設定時 `enabled:false`)、正確な `remoteGui.allowedTailscaleUsers`(未設定時は空)を使います。クライアントキーは `config.json` ではなく `service-api-token` に保存され、更新中だけ `service-api-token.prev` が存在する場合があります。使用量はミラーリングされません。 + +`remoteGui.allowInsecureHttp` は、古い strict-schema 設定を読み込むためだけに残された非推奨の no-op です。設定から削除してください。pairing grant は loopback または認証済み HTTPS でのみ受け付けられ、この値を `true` にしても平文 HTTP pairing は再び有効になりません。 diff --git a/docs-site/src/content/docs/ko/reference/cli/lifecycle.md b/docs-site/src/content/docs/ko/reference/cli/lifecycle.md index 271830ee1d..081c791bbb 100644 --- a/docs-site/src/content/docs/ko/reference/cli/lifecycle.md +++ b/docs-site/src/content/docs/ko/reference/cli/lifecycle.md @@ -156,7 +156,7 @@ ocx status --json 인증이 필요 없는 `GET /readyz` 엔드포인트로 동기화 후 준비 상태를 확인합니다. 준비되면 `200`, `pending` 또는 종단 상태인 `failed`이면 `Retry-After: 1`과 함께 `503`을 반환합니다. HTTP의 정제된 -식별 필드는 `{service, version, uptime, pid, port, status}`입니다. `/readyz`가 없는 이전 프록시는 +식별 필드는 `{service, version, uptime, pid, port, status, protocol, minimumClientProtocol, managementUrl}`입니다. `protocol`은 허브의 현재 원격 프로토콜, `minimumClientProtocol`은 호환되는 최소 클라이언트 프로토콜, `managementUrl`은 브라우저에서 보이는 표준 관리 origin입니다. `/readyz`가 없는 이전 프록시는 `unreachable`로 fail-closed하며, `/healthz`는 준비 상태가 아닌 별도의 liveness 확인입니다. 기본값은 한 번의 probe이며, `--wait`는 준비 또는 timeout까지 polling하지만 종단 `failed`를 확인하면 즉시 종료합니다. 기본 timeout은 45초이며, `--timeout `는 `--wait`와 함께 써야 하고 양의 정수인 1~300초 범위를 받습니다. CLI JSON은 diff --git a/docs-site/src/content/docs/ko/reference/configuration/server.md b/docs-site/src/content/docs/ko/reference/configuration/server.md index 0e280e668d..879b9d40a6 100644 --- a/docs-site/src/content/docs/ko/reference/configuration/server.md +++ b/docs-site/src/content/docs/ko/reference/configuration/server.md @@ -165,3 +165,5 @@ Anthropic OAuth 사이드카는 opencodex의 기존 Claude Code OAuth fingerprin ## Remote Hub 키와 기본값 `runtimeRole` 기본값은 `standalone`입니다. 허브는 `hub.managementPublicOrigin`, 로컬에만 열리는 `hub.managementIngress`(없으면 `enabled:false`), 정확한 `remoteGui.allowedTailscaleUsers`(없으면 빈 목록)를 사용합니다. 클라이언트 데이터 키는 `config.json`이 아니라 `service-api-token`에 저장되며 교체 중에는 `service-api-token.prev`가 잠시 생길 수 있습니다. 사용량 기록은 서로 복제하지 않습니다. + +`remoteGui.allowInsecureHttp`는 이전 strict-schema 설정을 계속 읽기 위해서만 남겨 둔 폐기된 no-op입니다. 설정에서 제거하세요. 페어링 grant는 loopback 또는 인증된 HTTPS에서만 허용되며, 이 값을 `true`로 설정해도 평문 HTTP 페어링은 다시 활성화되지 않습니다. diff --git a/docs-site/src/content/docs/ru/reference/cli/lifecycle.md b/docs-site/src/content/docs/ru/reference/cli/lifecycle.md index df777b802b..30b4e627a3 100644 --- a/docs-site/src/content/docs/ru/reference/cli/lifecycle.md +++ b/docs-site/src/content/docs/ru/reference/cli/lifecycle.md @@ -166,7 +166,7 @@ Identity-check живого прокси. Текстовый вывод сооб Проверяет готовность после синхронизации через не требующий аутентификации `GET /readyz`. При готовности возвращается `200`; для `pending` и терминального `failed` возвращается `503` с -`Retry-After: 1`. Санитизированные поля HTTP-ответа: `{service, version, uptime, pid, port, status}`. +`Retry-After: 1`. Санитизированные поля HTTP-ответа: `{service, version, uptime, pid, port, status, protocol, minimumClientProtocol, managementUrl}`. `protocol` — текущая версия удалённого протокола hub, `minimumClientProtocol` — минимальная совместимая версия клиента, а `managementUrl` — канонический origin управления для браузера. Старые прокси без `/readyz` fail-closed как `unreachable`; `/healthz` — отдельная проверка liveness, а не готовности. По умолчанию команда выполняет одну пробу. `--wait` опрашивает до готовности или тайм-аута, но при терминальном `failed` завершается немедленно. Тайм-аут по умолчанию — 45 секунд; diff --git a/docs-site/src/content/docs/ru/reference/configuration/server.md b/docs-site/src/content/docs/ru/reference/configuration/server.md index c65daab76c..306534a3e1 100644 --- a/docs-site/src/content/docs/ru/reference/configuration/server.md +++ b/docs-site/src/content/docs/ru/reference/configuration/server.md @@ -213,3 +213,5 @@ opencodex. Перед использованием прогоните soak-test ## Ключи Remote Hub и значения по умолчанию `runtimeRole` по умолчанию равен `standalone`. Hub использует `hub.managementPublicOrigin`, loopback-only `hub.managementIngress` (`enabled:false`, если отсутствует) и точные `remoteGui.allowedTailscaleUsers` (пустой список, если отсутствует). Ключ клиента хранится в `service-api-token`, не в `config.json`; во время ротации может появиться `service-api-token.prev`. Статистика не зеркалируется. + +`remoteGui.allowInsecureHttp` — устаревший no-op, оставленный только для загрузки старых файлов со строгой схемой. Удалите его из конфигурации: pairing grants принимаются лишь через loopback или аутентифицированный HTTPS, а значение `true` не включает pairing по открытому HTTP. diff --git a/docs-site/src/content/docs/tr/reference/cli/lifecycle.md b/docs-site/src/content/docs/tr/reference/cli/lifecycle.md index 533463cfb6..e26f4c8662 100644 --- a/docs-site/src/content/docs/tr/reference/cli/lifecycle.md +++ b/docs-site/src/content/docs/tr/reference/cli/lifecycle.md @@ -177,7 +177,7 @@ takdirde 1 ile çıkar, bu da onu servis probları için uygun hale getirir. Kimliği doğrulanmamış `GET /readyz` uç noktası aracılığıyla senkronizasyon sonrası hazırlığı kontrol edin. Hazır olduğunda `200` veya `pending` ve terminal `failed` için `Retry-After: 1` ile `503` döndürür. Temizlenmiş HTTP kimliği -`{service, version, uptime, pid, port, status}` şeklindedir. `/readyz` içermeyen +`{service, version, uptime, pid, port, status, protocol, minimumClientProtocol, managementUrl}` şeklindedir. `protocol` hub'ın güncel uzak protokolünü, `minimumClientProtocol` uyumlu en düşük istemci protokolünü ve `managementUrl` tarayıcıya görünen kanonik yönetim origin'ini belirtir. `/readyz` içermeyen eski proxy'ler `unreachable` olarak kapalı başarısız olur; `/healthz` hazırlık değil, ayrı bir canlılıktır. Komut varsayılan olarak bir prob gerçekleştirir; `--wait`, hazır olana veya zaman aşımına kadar yoklar, ancak terminal `failed` diff --git a/docs-site/src/content/docs/tr/reference/configuration/server.md b/docs-site/src/content/docs/tr/reference/configuration/server.md index c701c06805..edbd593eee 100644 --- a/docs-site/src/content/docs/tr/reference/configuration/server.md +++ b/docs-site/src/content/docs/tr/reference/configuration/server.md @@ -285,3 +285,5 @@ yeniden kullanır. Hedeflenen hesap ve iş yükünü kapsamlı bir şekilde test ## Remote Hub anahtarları ve varsayılanlar `runtimeRole` varsayılan olarak `standalone` değerindedir. Hub; `hub.managementPublicOrigin`, yalnız loopback `hub.managementIngress` (yokken `enabled:false`) ve tam `remoteGui.allowedTailscaleUsers` (yokken boş) kullanır. İstemci anahtarı `config.json` yerine `service-api-token` içinde kalır; döndürme sırasında `service-api-token.prev` geçici olarak bulunabilir. Kullanım kayıtları yansıtılmaz. + +`remoteGui.allowInsecureHttp`, yalnızca eski strict-schema yapılandırmalarının yüklenebilmesi için tutulan, kullanımdan kaldırılmış bir no-op'tur. Yapılandırmadan silin: pairing grant'leri yalnız loopback veya kimliği doğrulanmış HTTPS üzerinden kabul edilir ve `true` değeri düz HTTP pairing'i yeniden açmaz. diff --git a/docs-site/src/content/docs/zh-cn/reference/cli/lifecycle.md b/docs-site/src/content/docs/zh-cn/reference/cli/lifecycle.md index 9ab50802d1..dfae403438 100644 --- a/docs-site/src/content/docs/zh-cn/reference/cli/lifecycle.md +++ b/docs-site/src/content/docs/zh-cn/reference/cli/lifecycle.md @@ -123,7 +123,7 @@ ocx status --json 通过无需认证的 `GET /readyz` 端点检查同步后的就绪状态。就绪时返回 `200`;状态为 `pending` 或 终态 `failed` 时返回 `503`,并带有 `Retry-After: 1`。HTTP 仅返回经脱敏的身份字段 -`{service, version, uptime, pid, port, status}`。不支持 `/readyz` 的旧代理会按 `unreachable` 失败关闭; +`{service, version, uptime, pid, port, status, protocol, minimumClientProtocol, managementUrl}`。`protocol` 是 Hub 当前的远程协议版本,`minimumClientProtocol` 是兼容的最低客户端协议版本,`managementUrl` 是浏览器可见的规范管理 origin。不支持 `/readyz` 的旧代理会按 `unreachable` 失败关闭; `/healthz` 是独立的存活检查,不是就绪检查。默认只探测一次;`--wait` 会轮询到就绪或超时,但遇到终态 `failed` 会立即退出。默认超时为 45 秒;`--timeout ` 必须与 `--wait` 一起使用,取值范围为 1–300 秒的正整数。CLI JSON 输出 `{ready, status, pid, port}`,其中 `status` 为 `ready`、`pending`、`failed` 或 diff --git a/docs-site/src/content/docs/zh-cn/reference/configuration/server.md b/docs-site/src/content/docs/zh-cn/reference/configuration/server.md index df1452ee7b..bee7942398 100644 --- a/docs-site/src/content/docs/zh-cn/reference/configuration/server.md +++ b/docs-site/src/content/docs/zh-cn/reference/configuration/server.md @@ -179,3 +179,5 @@ Anthropic OAuth 侧车会复用 opencodex 现有的 Claude Code OAuth 指纹。 ## Remote Hub 密钥与默认值 `runtimeRole` 默认为 `standalone`。Hub 使用 `hub.managementPublicOrigin`、仅回环的 `hub.managementIngress`(缺省为 `enabled:false`)和准确的 `remoteGui.allowedTailscaleUsers`(缺省为空)。客户端密钥保存在 `service-api-token` 而不是 `config.json`;轮换期间可能暂时存在 `service-api-token.prev`。使用记录不会镜像。 + +`remoteGui.allowInsecureHttp` 是已弃用的 no-op,仅为让旧的严格 schema 配置继续加载而保留。请从配置中删除它:pairing grant 只接受 loopback 或已认证的 HTTPS;设为 `true` 也不会重新开放明文 HTTP pairing。 diff --git a/docs-site/src/content/docs/zh-tw/reference/cli/lifecycle.md b/docs-site/src/content/docs/zh-tw/reference/cli/lifecycle.md index 2307dbf6c3..bb377466fd 100644 --- a/docs-site/src/content/docs/zh-tw/reference/cli/lifecycle.md +++ b/docs-site/src/content/docs/zh-tw/reference/cli/lifecycle.md @@ -121,7 +121,7 @@ ocx status --json ### `ocx ready [--json] [--wait [--timeout ]]` -透過免認證的 `GET /readyz` 端點檢查同步後的就緒狀態。就緒時回傳 `200`,或 `pending` 與終端 `failed` 時回傳附帶 `Retry-After: 1` 的 `503`。其淨化的 HTTP 身分為 `{service, version, uptime, pid, port, status}`。沒有 `/readyz` 的舊代理會以 `unreachable` 方式 fail closed;`/healthz` 是分開的存活檢查,而非就緒檢查。此指令預設執行一次探測;`--wait` 輪詢直到就緒或逾時,但在觀察到終端 `failed` 狀態時立即退出。預設逾時為 45 秒;`--timeout ` 需要 `--wait`,接受 1–300 的正整數秒。CLI JSON 輸出 `{ready, status, pid, port}`,其中 `status` 為 `ready`、`pending`、`failed` 或 `unreachable`。離開碼為:就緒 0;未就緒、pending、failed、逾時或 unreachable 1;無效引數 64。 +透過免認證的 `GET /readyz` 端點檢查同步後的就緒狀態。就緒時回傳 `200`,或 `pending` 與終端 `failed` 時回傳附帶 `Retry-After: 1` 的 `503`。其淨化的 HTTP 身分為 `{service, version, uptime, pid, port, status, protocol, minimumClientProtocol, managementUrl}`。`protocol` 是 Hub 目前的遠端協定版本,`minimumClientProtocol` 是相容的最低用戶端協定版本,`managementUrl` 是瀏覽器可見的標準管理 origin。沒有 `/readyz` 的舊代理會以 `unreachable` 方式 fail closed;`/healthz` 是分開的存活檢查,而非就緒檢查。此指令預設執行一次探測;`--wait` 輪詢直到就緒或逾時,但在觀察到終端 `failed` 狀態時立即退出。預設逾時為 45 秒;`--timeout ` 需要 `--wait`,接受 1–300 的正整數秒。CLI JSON 輸出 `{ready, status, pid, port}`,其中 `status` 為 `ready`、`pending`、`failed` 或 `unreachable`。離開碼為:就緒 0;未就緒、pending、failed、逾時或 unreachable 1;無效引數 64。 ### `ocx doctor` diff --git a/docs-site/src/content/docs/zh-tw/reference/configuration/server.md b/docs-site/src/content/docs/zh-tw/reference/configuration/server.md index aab24e9f26..1d80f5911c 100644 --- a/docs-site/src/content/docs/zh-tw/reference/configuration/server.md +++ b/docs-site/src/content/docs/zh-tw/reference/configuration/server.md @@ -198,3 +198,5 @@ Anthropic OAuth sidecar 重用 opencodex 既有的 Claude Code OAuth 指紋。 ## Remote Hub 金鑰與預設值 `runtimeRole` 預設為 `standalone`。Hub 使用 `hub.managementPublicOrigin`、僅限迴路的 `hub.managementIngress`(缺省為 `enabled:false`)與正確的 `remoteGui.allowedTailscaleUsers`(缺省為空)。用戶端金鑰保存在 `service-api-token` 而不是 `config.json`;輪替期間可能暫時存在 `service-api-token.prev`。用量不會鏡像。 + +`remoteGui.allowInsecureHttp` 是已棄用的 no-op,只為讓舊的 strict-schema 設定繼續載入而保留。請從設定移除:pairing grant 僅接受 loopback 或已驗證的 HTTPS;設為 `true` 也不會重新開放明文 HTTP pairing。 diff --git a/gui/src/components/MemoryObservabilityCard.tsx b/gui/src/components/MemoryObservabilityCard.tsx index d19134c756..ebf12a4ad7 100644 --- a/gui/src/components/MemoryObservabilityCard.tsx +++ b/gui/src/components/MemoryObservabilityCard.tsx @@ -299,7 +299,9 @@ export default function MemoryObservabilityCard({ apiBase }: { apiBase: string } inFlight = true; const bounded = createBoundedFetch(5_000); active = bounded; - void fetch(`${apiBase}/healthz`, { cache: "no-store", signal: bounded.signal }) + // Restart is a shared-plane action, so reconnect through its authenticated management + // health route. Remote Hub intentionally does not expose /healthz on management ingress. + void fetch(`${apiBase}/api/system/health`, { cache: "no-store", signal: bounded.signal }) .then(async (res) => { if (cancelled) return; if (!res.ok) { diff --git a/gui/src/pages/dashboard-core-poll.ts b/gui/src/pages/dashboard-core-poll.ts index 5198801889..f6bb653452 100644 --- a/gui/src/pages/dashboard-core-poll.ts +++ b/gui/src/pages/dashboard-core-poll.ts @@ -254,7 +254,7 @@ export async function fetchDashboardOverview( ): Promise { try { const [hRes, pRes] = await Promise.all([ - fetch(`${apiBase}/healthz`, { signal }), + fetch(`${apiBase}/api/system/health`, { signal }), fetch(`${apiBase}/api/providers`, { signal }), ]); const health = await requireJson(hRes); diff --git a/gui/tests/dashboard-contracts.test.ts b/gui/tests/dashboard-contracts.test.ts index a1ccca9890..c411168daf 100644 --- a/gui/tests/dashboard-contracts.test.ts +++ b/gui/tests/dashboard-contracts.test.ts @@ -79,7 +79,8 @@ test("Dashboard overview status widgets do not wait on injection-model", async ( expect(overviewStart).toBeGreaterThan(-1); expect(multiStart).toBeGreaterThan(overviewStart); const overviewBody = core.slice(overviewStart, multiStart); - expect(overviewBody).toContain("/healthz"); + expect(overviewBody).toContain("/api/system/health"); + expect(overviewBody).not.toContain("/healthz"); expect(overviewBody).toContain("/api/providers"); expect(overviewBody).not.toContain("/api/injection-model"); expect(overviewBody).not.toContain("/api/v2"); diff --git a/gui/tests/memory-observability-card.test.tsx b/gui/tests/memory-observability-card.test.tsx index 556f3646ef..1fca1bcd33 100644 --- a/gui/tests/memory-observability-card.test.tsx +++ b/gui/tests/memory-observability-card.test.tsx @@ -186,6 +186,47 @@ test("Drain & restart posts /api/system/restart after confirm", async () => { await act(async () => { root.unmount(); }); }); +test("restart reconnect polls authenticated management health instead of denied /healthz", async () => { + let memoryReads = 0; + const { root, container, testWindow, calls } = await mountCard((url) => { + if (url.includes("/api/startup-health")) return Response.json({ protection: "service" }); + if (url.includes("/api/system/restart")) { + return Response.json({ success: true, activeTurnCount: 2 }, { status: 202 }); + } + if (url.includes("/api/system/memory")) { + memoryReads += 1; + return memoryReads === 1 + ? Response.json(MEMORY_PAYLOAD) + : new Response("restarting", { status: 503 }); + } + if (url.includes("/api/system/health")) { + return Response.json({ status: "ok", version: "test", uptime: 1, pid: 4243 }); + } + return new Response(null, { status: 404 }); + }); + + originalConfirm = window.confirm; + window.confirm = () => true; + const button = Array.from(container.querySelectorAll("button")).find( + (el) => (el.textContent ?? "").includes("Drain & restart"), + ); + expect(button).toBeTruthy(); + + await act(async () => { + button!.dispatchEvent(new testWindow.MouseEvent("click", { bubbles: true })); + for (let attempt = 0; attempt < 20; attempt += 1) { + await new Promise(resolve => testWindow.setTimeout(resolve, 0)); + if (calls().some(call => call.includes("/api/system/health"))) break; + } + }); + + expect(calls().some(call => call.includes("/api/system/health"))).toBe(true); + expect(calls().some(call => /\/healthz(?:$|\?)/.test(call))).toBe(false); + expect(container.textContent ?? "").toContain("Drain & restart"); + + await act(async () => { root.unmount(); }); +}); + test("older memory payloads without activeTurnCount hide the restart action", async () => { const legacy = { ...MEMORY_PAYLOAD } as Record; delete legacy.activeTurnCount; diff --git a/src/server/management-api.ts b/src/server/management-api.ts index 1e49c2c278..7c3e8a8836 100644 --- a/src/server/management-api.ts +++ b/src/server/management-api.ts @@ -220,7 +220,7 @@ export async function handleManagementAPI( } } catch { /* best-effort */ } } - const ctx: ManagementContext = { req, url, config, deps, principal, sessionControl, convergeCodexCatalog, syncClaudeAgentDefsBestEffort }; + const ctx: ManagementContext = { req, url, config, deps, version: VERSION, principal, sessionControl, convergeCodexCatalog, syncClaudeAgentDefsBestEffort }; let routed: Response | null; try { routed = handleSessionRoutes(ctx) diff --git a/src/server/management/context.ts b/src/server/management/context.ts index 5d57378bb3..49aedf4990 100644 --- a/src/server/management/context.ts +++ b/src/server/management/context.ts @@ -109,6 +109,8 @@ export interface ManagementContext { url: URL; config: OcxConfig; deps: ManagementApiDeps; + /** Installed package version projected through bounded system identity routes. */ + version: string; /** * Which credential authorized this request, resolved by the auth gate before * dispatch. Routes that spend the USER's identity (not just the proxy's) must diff --git a/src/server/management/route-registry.ts b/src/server/management/route-registry.ts index a8a7a983da..68c72285b5 100644 --- a/src/server/management/route-registry.ts +++ b/src/server/management/route-registry.ts @@ -291,6 +291,7 @@ export const MANAGEMENT_ROUTES: readonly ManagementRoute[] = [ { method: "POST", path: "/api/storage/codex-logs/repair", module: "server/management/storage-log-guard-routes", mutates: true }, { method: "POST", path: "/api/storage/codex-logs/unprotect", module: "server/management/storage-log-guard-routes", mutates: true }, // server/management/system-routes + { method: "GET", path: "/api/system/health", module: "server/management/system-routes", mutates: false }, { method: "GET", path: "/api/system/memory", module: "server/management/system-routes", mutates: false }, { method: "GET", path: "/api/system/windows-replace-retries", module: "server/management/system-routes", mutates: false }, { method: "POST", path: "/api/system/restart", module: "server/management/system-routes", mutates: true }, diff --git a/src/server/management/system-routes.ts b/src/server/management/system-routes.ts index 867a3f94a0..b163b8a8ad 100644 --- a/src/server/management/system-routes.ts +++ b/src/server/management/system-routes.ts @@ -48,7 +48,19 @@ import { acceptSystemRestart } from "./system-restart"; const ENDPOINT_SAMPLE_LIMIT = 60; export async function handleSystemRoutes(ctx: ManagementContext): Promise { - const { req, url, config } = ctx; + const { req, url, config, version } = ctx; + if (url.pathname === "/api/system/health" && req.method === "GET") { + // Authenticated management counterpart to /healthz. Remote Hub deliberately keeps the + // unauthenticated liveness route off its management ingress, while the connected dashboard + // still needs bounded process identity and PID replacement evidence (#3158). + return jsonResponse({ + status: "ok", + service: "opencodex", + version, + uptime: process.uptime(), + pid: process.pid, + }); + } if (url.pathname === "/api/system/memory" && req.method === "GET") { const usage = process.memoryUsage(); let jscHeap: { heapSize: number; heapCapacity: number; objectCount: number } | null = null; diff --git a/structure/05_gui-and-management-api.md b/structure/05_gui-and-management-api.md index 9d011d7c4a..ff75b15e88 100644 --- a/structure/05_gui-and-management-api.md +++ b/structure/05_gui-and-management-api.md @@ -129,7 +129,7 @@ this document owns is which module holds which area and what invariant that area | V2 / Multi-agent mode | `GET/PUT /api/v2` — reports/sets the codex `multi_agent_v2` feature flag, the 3-state `multiAgentMode` override (`v1`/`default`/`v2`), the `keepNativeChatGptOnV1` hybrid pin, and the logical maximum thread count. Selecting `v2` normally enables the native flag; with the hybrid pin it disables that global override so native rows can resolve to v1 while routed rows resolve to v2. Selecting `v1` disables the flag; `default` leaves it unchanged. PUT rejects an explicit enabled flag that conflicts with the selected mode or hybrid pin. Every transition preserves the logical thread limit, is rollback-safe, and resyncs the catalog. | | Logs & Debug | One sidebar entry (`/#logs`) with two tabs. Logs tab: request/runtime logs for local diagnosis. Debug tab (`/#logs/debug`; legacy `/#debug` deep links redirect there): provider + usage toggles, refresh/follow log viewer. `GET/PUT /api/debug`; `GET /api/debug/logs` and `GET /api/debug/usage-logs` (monotonic `after` cursor, legacy `since` accepted). CLI: `ocx debug provider|usage …` (both streams via running proxy API). | | Usage | `GET /api/usage` aggregate read-only summary derived from `~/.opencodex/usage.jsonl`; measured / reported / unreported / unsupported / estimated counts, daily zero-filled grid, model and provider breakdowns. Never exposes prompts. | -| System | `POST /api/system/restart` restarts the proxy in place. Local CLI/tray callers first attest the exact runtime PID and port, then send a process-scoped HMAC capability bound to that method, path, PID, and port; the capability authorizes no other management route and is invalid after replacement. The caller observes one absolute deadline and accepts success only after a different runtime PID is healthy on the same port. `GET /api/system/memory` — service-process runtime/memory identity (pid, Bun version/revision, optional `bunRuntimeSource` provenance, platform, RSS/heap/external/ArrayBuffers scalars, observed memory = max(RSS, external, ArrayBuffers), `bun:jsc` heap context, streamMode + eager-relay gate decision, watchdog snapshot sliced to the last 60 samples) plus privacy-safe `appOwnedBytes` retained-store totals/counters under static store ids. Scalar-only payload; dashboard/admin callers use the standard management gate, while `ocx doctor` may use only the exact process-scoped local-read capability. It must never move to unauthenticated `/healthz`. | +| System | `POST /api/system/restart` restarts the proxy in place. Local CLI/tray callers first attest the exact runtime PID and port, then send a process-scoped HMAC capability bound to that method, path, PID, and port; the capability authorizes no other management route and is invalid after replacement. The caller observes one absolute deadline and accepts success only after a different runtime PID is healthy on the same port. `GET /api/system/health` is the authenticated scalar-only identity used by shared-plane Dashboard status and restart reconnect polling; it does not widen a Remote Hub management ingress to unauthenticated `/healthz`. `GET /api/system/memory` — service-process runtime/memory identity (pid, Bun version/revision, optional `bunRuntimeSource` provenance, platform, RSS/heap/external/ArrayBuffers scalars, observed memory = max(RSS, external, ArrayBuffers), `bun:jsc` heap context, streamMode + eager-relay gate decision, watchdog snapshot sliced to the last 60 samples) plus privacy-safe `appOwnedBytes` retained-store totals/counters under static store ids. Scalar-only payload; dashboard/admin callers use the standard management gate, while `ocx doctor` may use only the exact process-scoped local-read capability. It must never move to unauthenticated `/healthz`. | | Stop | `POST /api/stop` — restore native Codex, stop any installed service, and exit the proxy. | | Diagnostics/sync | `src/server/management/config-routes.ts` — `GET /api/diagnostics/project-config` reports project-level Codex config that bypasses managed routing; `POST /api/sync` re-runs catalog/config sync. The diagnostic reports the bypass; it does not rewrite the project file. | | Sidecar/shadow-call settings | `src/server/management/config-routes.ts` — `GET/PUT /api/sidecar-settings` and `GET/PUT /api/shadow-call-settings`. PUT accepts model and backend (web-search union: openai/anthropic/xai/gemini/exa; xAI is live through stored Grok OAuth, while Gemini/Exa remain inert until their executors ship) plus validated `webSearch.xSearch`, optional `webSearch.exaApiKey` (write/clear only — never echoed by GET or the PUT response; redact.ts strips it from logs), `webSearch.reasoning`, `vision.reasoning`, `vision.enabled`, `vision.maxDescriptionsPerTurn`, and `vision.timeoutMs`; the read and PUT-response payload reports model, backend, reasoning, enabled, the vision per-turn limit, and timeout. `timeoutMs` is validated against the runtime integer bounds in `src/vision/timeout-bounds.ts`. Provider/OAuth credentials live in their stores; `exaApiKey` is the one sidecar-owned secret and follows the write-only contract above. Both shadow-call responses also report the resolved `sourceModels` — the prefixes the runtime actually intercepts (`src/lib/shadow-call.ts`, default `gpt-5.4-mini` + `gpt-5.6-luna`), so no client hard-codes a helper slug that a Codex release can invalidate. | diff --git a/tests/cli-capabilities.test.ts b/tests/cli-capabilities.test.ts index 17e4beecf0..262e4e6fcd 100644 --- a/tests/cli-capabilities.test.ts +++ b/tests/cli-capabilities.test.ts @@ -248,6 +248,7 @@ const UNDECLARED_ROUTES_2026_08_28: readonly string[] = [ "GET /api/storage/codex-logs", "GET /api/subagent-model-fallback", "GET /api/subagent-models", + "GET /api/system/health", "GET /api/system/memory", "GET /api/system/windows-replace-retries", "GET /api/update/badge", diff --git a/tests/server-management-auth.test.ts b/tests/server-management-auth.test.ts index cbcdf16161..c3eb6bfc1e 100644 --- a/tests/server-management-auth.test.ts +++ b/tests/server-management-auth.test.ts @@ -1003,16 +1003,35 @@ describe("management and data-plane credential separation", () => { const html = await issued.text(); const token = /name="opencodex-session-token" content="([^"]+)"/.exec(html)?.[1]; expect(token).toBeDefined(); + const sessionHeaders = { + Host: "hub.example.test", + Origin: "https://hub.example.test", + "x-opencodex-api-key": token!, + "x-opencodex-gui-origin": "https://hub.example.test", + }; const management = await fetch(`http://127.0.0.1:${managementPort}/api/config`, { - headers: { - Host: "hub.example.test", - Origin: "https://hub.example.test", - "x-opencodex-api-key": token!, - "x-opencodex-gui-origin": "https://hub.example.test", - }, + headers: sessionHeaders, }); expect(management.status).toBe(200); + // Connected GUI status/restart polling stays authenticated without widening the ingress: + // raw liveness remains absent, while its bounded management counterpart is available. + const rawHealth = await fetch(`http://127.0.0.1:${managementPort}/healthz`, { + headers: sessionHeaders, + }); + expect(rawHealth.status).toBe(404); + const managementHealth = await fetch(`http://127.0.0.1:${managementPort}/api/system/health`, { + headers: sessionHeaders, + }); + expect(managementHealth.status).toBe(200); + expect(await managementHealth.json()).toMatchObject({ + status: "ok", + service: "opencodex", + version: expect.any(String), + uptime: expect.any(Number), + pid: process.pid, + }); + const adminConsent = await fetch(`http://127.0.0.1:${managementPort}/api/github/star`, { method: "POST", headers: { From 634d9e5a03a6bd23c7eaea101ca712b456e15991 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 02:16:04 +0900 Subject: [PATCH 113/172] fix(codex): retry caller main after pool rejection (carry of #3135) (#3180) * fix(codex): distinguish unavailable entitled accounts * fix(codex): preserve unobserved main entitlement state * fix(codex): retry caller main after pool rejection --------- Co-authored-by: luvs01 --- src/codex/auth-context.ts | 17 ++- src/server/responses/compact.ts | 5 +- src/server/responses/core.ts | 25 +++- tests/codex-auth-context.test.ts | 83 +++++++++++++ tests/server-auth.test.ts | 115 ++++++++++++++++++ ...subagent-fallback-handle-responses.test.ts | 53 ++++++++ 6 files changed, 290 insertions(+), 8 deletions(-) diff --git a/src/codex/auth-context.ts b/src/codex/auth-context.ts index ac12ca7081..007ec70955 100644 --- a/src/codex/auth-context.ts +++ b/src/codex/auth-context.ts @@ -491,6 +491,7 @@ export async function resolveCodexAuthContext( const excludeAccountIds = nativeMainReadsForbidden ? new Set([MAIN_CODEX_ACCOUNT_ID]) : undefined; + const mainModelGrantUnobserved = excludeAccountIds?.has(MAIN_CODEX_ACCOUNT_ID) === true; const entitlementSnapshot = options.modelId && ACCOUNT_GATED_NATIVE_OPENAI_MODELS.has(options.modelId) ? await (options.resolveCodexModelEntitlements ?? resolveCodexModelEntitlements)(config, { excludeAccountIds, @@ -546,7 +547,15 @@ export async function resolveCodexAuthContext( if (resolution.status === "expired") throw new CodexThreadAffinityExpiredError(resolution.accountId); const selected = resolution.status === "selected" ? resolution.accountId : null; if (!selected) { - if (requestScopedMainCredential && fixedAccountId === undefined && !options.excludeAccountId) { + // A retry that excluded a failed Pool account may still use the validated caller-owned + // main credential. Treating every exclusion as if main itself had failed strands a healthy + // native bearer after the first Pool attempt. Preserve the exactly-once boundary by refusing + // this fallback only when the excluded credential is main. + if ( + requestScopedMainCredential + && fixedAccountId === undefined + && options.excludeAccountId !== MAIN_CODEX_ACCOUNT_ID + ) { return await resolveCallerOwnedMainContext(); } if (fixedAccountId !== undefined) { @@ -566,7 +575,11 @@ export async function resolveCodexAuthContext( throw new CodexMainProfileDrainingError(); } throw new CodexPoolAuthenticationError( - modelEligibleAccountIds ? "No eligible Codex account supports this model" : undefined, + modelEligibleAccountIds === undefined + ? undefined + : entitledAccountIds?.size === 0 && !mainModelGrantUnobserved + ? "No eligible Codex account supports this model" + : "Codex accounts that support this model are currently unavailable", ); } accountId = selected; diff --git a/src/server/responses/compact.ts b/src/server/responses/compact.ts index b14800fc7b..f0e798828f 100644 --- a/src/server/responses/compact.ts +++ b/src/server/responses/compact.ts @@ -382,7 +382,10 @@ async function resolveAlternateCompactContext(args: { requestScopedMainCredential: hasForwardableCodexBearer(req.headers, config), beginCodexAccountSelection: codexAccountSelectionForTurn(turnAdmissionLease), }); - if (!authCtx.accountId || authCtx.accountId === excludeAccountId) return null; + // Caller-owned main has no Pool account id. It is still a valid one-shot alternate after a + // stored account fails; resolveCodexAuthContext already prevents returning it when main is the + // excluded credential. + if (authCtx.accountId === excludeAccountId) return null; const provider = applyCodexAuthContextToProvider(route.provider, authCtx, route.codexAccountMode); const headers = new Headers({ "content-type": "application/json" }); const selected = headersForCodexAuthContext(req.headers, authCtx); diff --git a/src/server/responses/core.ts b/src/server/responses/core.ts index 0afaf11f17..2033bf079b 100644 --- a/src/server/responses/core.ts +++ b/src/server/responses/core.ts @@ -982,7 +982,7 @@ interface CodexPoolAccountRetryArgs { stream: boolean; onResponse?: ( response: Response, - authCtx: Extract, + authCtx: CodexAuthContext, request: Awaited["buildRequest"]>>, ) => void; } @@ -990,7 +990,7 @@ interface CodexPoolAccountRetryArgs { type CodexPoolAccountRetryResult = | { kind: "retried"; - authCtx: Extract; + authCtx: CodexAuthContext; request: Awaited["buildRequest"]>>; upstreamResponse: Response; selectedForwardHeaders: Headers; @@ -999,7 +999,7 @@ type CodexPoolAccountRetryResult = | { kind: "transport"; error: unknown; - authCtx: Extract; + authCtx: CodexAuthContext; }; /** Keep retry-stage entitlement snapshots inside the native-main selection fence. */ @@ -1166,7 +1166,14 @@ async function retryCodexPoolOnAlternateAccount( throw error; } } - if (retryAuthCtx?.kind !== "pool" && retryAuthCtx?.kind !== "main-pool") { + // A validated request-owned main bearer is a real alternate when the failed credential was a + // stored Pool account. It has no Pool account id to promote or cool, but it can own this one + // bounded replay. The resolver already refuses it when main itself is the excluded credential. + if ( + retryAuthCtx?.kind !== "pool" + && retryAuthCtx?.kind !== "main-pool" + && retryAuthCtx?.kind !== "main" + ) { // A body-confirmed quota response may arrive under HTTP 5xx. Without an alternate, // the ordinary terminal recorder sees only that wire status and would misclassify it // as transient, leaving the exhausted account immediately selectable next turn. @@ -1293,6 +1300,9 @@ async function retryCodexPoolOnAlternateAccount( retrySendCount += 1; args.onResponse?.(upstreamResponse, retryAuthCtx, request); if (!retrySameConfirmedAccount || retrySendCount >= maxRetrySends) break; + // Caller-owned main is an alternate-account replay and can never enter the bounded + // same-stored-account 400 loop above. Keep that invariant explicit for the account-id reads. + if (retryAuthCtx.kind === "main") break; if (!await shouldRetryCodexPoolAccountModel400( upstreamResponse, route.modelId, @@ -4358,7 +4368,12 @@ async function handleResponsesInner( passthroughEstimate, stream: parsed.stream, onResponse: (response, retryAuthCtx, retryRequest) => { - captureAffinityResponse(response, retryAuthCtx, retryRequest, true); + captureAffinityResponse( + response, + retryAuthCtx, + retryRequest, + retryAuthCtx.kind !== "main", + ); }, }); if (retry.kind === "transport") { diff --git a/tests/codex-auth-context.test.ts b/tests/codex-auth-context.test.ts index 9a424f1b32..ddc4b2a141 100644 --- a/tests/codex-auth-context.test.ts +++ b/tests/codex-auth-context.test.ts @@ -634,6 +634,50 @@ describe("Codex auth context", () => { }); }); + test("account-gated routing distinguishes an unavailable grant from no grant", async () => { + const cfg = config(); + saveCodexAccountCredential("pool-a", { + accessToken: "pool-token", + refreshToken: "pool-refresh", + expiresAt: Date.now() + 5 * 60_000, + chatgptAccountId: "pool-account", + }); + const snapshot = (models: string[]): CodexModelEntitlementSnapshot => ({ + modelsByAccount: new Map([["pool-a", new Set(models)]]), + confirmedAccountIds: new Set(["pool-a"]), + credentialIdentities: new Map(), + }); + const resolve = (models: string[]) => resolveCodexAuthContext(new Headers(), cfg, "pool", { + excludeAccountId: "pool-a", + modelId: "gpt-daybreak-blue-latest", + resolveCodexModelEntitlements: async () => snapshot(models), + }); + + await expect(resolve(["gpt-daybreak-blue-latest"])) + .rejects.toThrow("Codex accounts that support this model are currently unavailable"); + await expect(resolve(["gpt-5.6-sol"])) + .rejects.toThrow("No eligible Codex account supports this model"); + + const mainExcludedSnapshot: CodexModelEntitlementSnapshot = { + modelsByAccount: new Map(), + confirmedAccountIds: new Set(), + credentialIdentities: new Map(), + }; + await expect(resolveCodexAuthContext(new Headers(), cfg, "pool", { + excludeAccountId: "pool-a", + modelId: "gpt-daybreak-blue-latest", + beginCodexAccountSelection: () => ({ + mainProfileDraining: true, + claimMainProfile: () => false, + release: () => {}, + }), + resolveCodexModelEntitlements: async (_config, options) => { + expect(options?.excludeAccountIds?.has(MAIN_CODEX_ACCOUNT_ID)).toBeTrue(); + return mainExcludedSnapshot; + }, + })).rejects.toThrow("Codex accounts that support this model are currently unavailable"); + }); + test("auth resolution preserves per-model detours without replacing ordinary affinity", async () => { const cfg = config(); cfg.accountPoolStrategy = "round-robin"; @@ -1174,6 +1218,45 @@ describe("Codex auth context", () => { expect(cfg.activeCodexAccountPinned).toBe(MAIN_CODEX_ACCOUNT_ID); }); + test("a failed Pool account may fall back once to the validated caller-owned main credential", async () => { + const cfg = config(); + cfg.codexAccounts = [ + { id: "pool-a", email: "pool-a@example.test", isMain: false, chatgptAccountId: "pool-account" }, + ]; + const inbound = new Headers({ + authorization: "Bearer caller-keyring-token", + "chatgpt-account-id": "caller-keyring-account", + }); + const emptyEntitlements: CodexModelEntitlementSnapshot = { + modelsByAccount: new Map(), + confirmedAccountIds: new Set(), + credentialIdentities: new Map(), + }; + let directEntitlementChecks = 0; + const options = { + requestScopedMainCredential: true, + modelId: "gpt-daybreak-blue-latest", + resolveCodexModelEntitlements: async () => emptyEntitlements, + isDirectCallerEntitledToCodexModel: async () => { + directEntitlementChecks += 1; + return true; + }, + }; + + await expect(resolveCodexAuthContext(inbound, cfg, "pool", { + ...options, + excludeAccountId: "pool-a", + })).resolves.toMatchObject({ kind: "main", accountId: null }); + expect(directEntitlementChecks).toBe(1); + + // If main itself was the failed credential, the retry must not loop back to it. + await expect(resolveCodexAuthContext(inbound, { ...cfg, codexAccounts: [] }, "pool", { + ...options, + excludeAccountId: MAIN_CODEX_ACCOUNT_ID, + })).rejects.toThrow("Codex accounts that support this model are currently unavailable"); + expect(directEntitlementChecks).toBe(1); + }); + test("selects pool auth independently of the routed provider", async () => { saveCodexAccountCredential("pool-a", { accessToken: "pool_token", diff --git a/tests/server-auth.test.ts b/tests/server-auth.test.ts index 7f4217b73c..274df5242c 100644 --- a/tests/server-auth.test.ts +++ b/tests/server-auth.test.ts @@ -3078,6 +3078,121 @@ describe("server local API auth", () => { } }); + test.each([429, 402] as const)( + "a pre-stream %i from the only Pool account retries once with the validated caller main", + async rejection => { + setDebugSettings({ debug: true }); + const model = "gpt-daybreak-blue-latest"; + const observed: Array<{ authorization: string | null; accountId: string | null }> = []; + const harness = await startPoolRetryHarness((_accountId, request) => { + observed.push({ + authorization: request.headers.get("authorization"), + accountId: request.headers.get("chatgpt-account-id"), + }); + if (observed.length === 1) { + return new Response(JSON.stringify({ error: { message: "pool account unavailable" } }), { + status: rejection, + headers: { "content-type": "application/json", "retry-after": "60" }, + }); + } + return Response.json({ id: "caller-main-success", status: "completed", output: [] }); + }, { + secondAccount: false, + modelRosterByAccount: { + "acct-pool-a": [model], + "acct-caller-main": [model], + }, + }); + try { + const response = await harness.request({ + model, + headers: { "chatgpt-account-id": "acct-caller-main" }, + }); + expect(response.status).toBe(200); + expect((await response.json() as { id: string }).id).toBe("caller-main-success"); + expect(observed).toEqual([ + { authorization: "Bearer pool-a-token", accountId: "acct-pool-a" }, + { authorization: "Bearer inbound-token", accountId: "acct-caller-main" }, + ]); + expect(harness.dispatches).toEqual(["acct-pool-a", "acct-caller-main"]); + expect(loadConfig().activeCodexAccountId).toBe("pool-a"); + const affinity = getDebugLogEntries() + .map(entry => entry.line) + .filter(line => line.startsWith("[ocx:codex:affinity] ")) + .map(line => JSON.parse(line.slice("[ocx:codex:affinity] ".length)) as { + status: number; + authKind: string; + credentialSubstituted: boolean; + }); + expect(affinity.slice(-2)).toEqual([ + expect.objectContaining({ status: rejection, authKind: "pool", credentialSubstituted: true }), + expect.objectContaining({ status: 200, authKind: "main", credentialSubstituted: false }), + ]); + } finally { + await stopPoolRetryHarness(harness); + } + }, + { timeout: SERVER_BUDGET_MS }, + ); + + test.each([429, 402] as const)( + "compact %i from the only Pool account retries once with the validated caller main", + async rejection => { + const model = "gpt-daybreak-blue-latest"; + const observed: Array<{ authorization: string | null; accountId: string | null }> = []; + const harness = await startPoolRetryHarness((_accountId, request) => { + observed.push({ + authorization: request.headers.get("authorization"), + accountId: request.headers.get("chatgpt-account-id"), + }); + if (observed.length === 1) { + return new Response(JSON.stringify({ error: { message: "pool account unavailable" } }), { + status: rejection, + headers: { "content-type": "application/json", "retry-after": "60" }, + }); + } + return new Response([ + "event: response.output_item.done", + 'data: {"type":"response.output_item.done","output_index":0,"item":{"type":"compaction","encrypted_content":"gAAAAAB-caller-main"}}', + "", + "event: response.completed", + 'data: {"type":"response.completed","response":{"status":"completed","output":[]}}', + "", + "data: [DONE]", + "", + ].join("\n"), { + headers: { "content-type": "text/event-stream" }, + }); + }, { + secondAccount: false, + modelRosterByAccount: { + "acct-pool-a": [model], + "acct-caller-main": [model], + }, + }); + try { + const response = await harness.request({ + model, + path: "/v1/responses/compact", + headers: { "chatgpt-account-id": "acct-caller-main" }, + }); + expect(response.status).toBe(200); + expect(await response.json()).toEqual({ + output: [{ type: "compaction", encrypted_content: "gAAAAAB-caller-main" }], + }); + expect(observed).toEqual([ + { authorization: "Bearer pool-a-token", accountId: "acct-pool-a" }, + { authorization: "Bearer inbound-token", accountId: "acct-caller-main" }, + ]); + expect(harness.dispatches).toEqual(["acct-pool-a", "acct-caller-main"]); + expect(loadConfig().activeCodexAccountId).toBe("pool-a"); + } finally { + await stopPoolRetryHarness(harness); + } + }, + { timeout: SERVER_BUDGET_MS }, + ); + test("#584: Retry-After cools the first account even when its account retry fails", async () => { const harness = await startPoolRetryHarness(accountId => accountId === "acct-pool-a" ? new Response(JSON.stringify({ error: { message: "rate limited" } }), { diff --git a/tests/subagent-fallback-handle-responses.test.ts b/tests/subagent-fallback-handle-responses.test.ts index 9212cbafe3..9bd4dd1705 100644 --- a/tests/subagent-fallback-handle-responses.test.ts +++ b/tests/subagent-fallback-handle-responses.test.ts @@ -263,6 +263,7 @@ async function postDirectCodex( config: OcxConfig, body: Record, options: Parameters[3] = {}, + headers: HeadersInit = {}, ): Promise { return handleResponses( new Request("http://localhost/v1/responses", { @@ -270,6 +271,7 @@ async function postDirectCodex( headers: { "content-type": "application/json", authorization: "Bearer caller-codex-token", + ...headers, }, body: JSON.stringify(body), }), @@ -1776,6 +1778,57 @@ describe("account-gated retry entitlement boundary", () => { expect(selectionReleases).toBe(3); }); + test("a lost Pool model grant retries once with the validated caller-owned main credential", async () => { + const now = 1_800_000_000_000; + Date.now = () => now; + installPoolCredential("pool-a", "pool_acc_a", now); + const cfg = retryConfig(); + let entitlementCalls = 0; + const observed: Array<{ authorization: string | null; accountId: string | null }> = []; + let callerRosterReads = 0; + globalThis.fetch = (async (input, init) => { + const url = new URL(String(input)); + const headers = new Headers(init?.headers); + if (url.pathname.endsWith("/models")) { + callerRosterReads += 1; + expect(headers.get("authorization")).toBe("Bearer caller-codex-token"); + expect(headers.get("chatgpt-account-id")).toBe("caller-main-account"); + return Response.json({ + models: [{ slug: model, supported_in_api: true, visibility: "list" }], + }); + } + observed.push({ + authorization: headers.get("authorization"), + accountId: headers.get("chatgpt-account-id"), + }); + return observed.length === 1 + ? unsupportedCodexModelResponse(model) + : Response.json({ id: "caller-main-success", status: "completed", output: [] }); + }) as typeof fetch; + + const response = await postDirectCodex( + cfg, + { model, input: "hello", stream: false }, + { + resolveCodexModelEntitlements: async () => { + entitlementCalls += 1; + return entitlementCalls === 1 + ? entitlementSnapshot({ "pool-a": [model] }) + : entitlementSnapshot({ "pool-a": ["gpt-5.6-sol"] }); + }, + }, + { "chatgpt-account-id": "caller-main-account" }, + ); + + expect(response.status).toBe(200); + expect(observed).toEqual([ + { authorization: "Bearer pool-a_token", accountId: "pool_acc_a" }, + { authorization: "Bearer caller-codex-token", accountId: "caller-main-account" }, + ]); + expect(callerRosterReads).toBe(1); + expect(entitlementCalls).toBe(3); + }); + test("a first-refresh programmer error cancels the 400 and releases its quota probe", async () => { const cooldownAt = 1_800_000_000_000; const probeAt = cooldownAt + CODEX_QUOTA_PROBE_INTERVAL_MS; From 865a36ef04eb6395e617f94ed87aaa474a903444 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 02:18:24 +0900 Subject: [PATCH 114/172] fix(claude): keep proxy admission keys out of subscription launches (carry of #3148) (#3182) * fix(claude): keep proxy admission keys out of subscription launches * fix(claude): trust only proof-bound system env credentials * fix(claude): keep a connected target's admission token out of subscription stripping The carried subscription fix resolves auth mode before adding proxy-owned credentials, which is right for an ordinary launch. A connected launch is not one: the caller already named a hub and supplied the client admission token for it, so a machine whose own environment reads as a Claude subscription would strip the very credential the launch was constructed with. Gate the subscription strip on the absence of an explicit target, and add the regression. --------- Co-authored-by: wj Co-authored-by: jun --- src/claude/auth-mode.ts | 15 ++-- src/cli/claude.ts | 36 ++++++---- src/server/system-env.ts | 103 ++++++++++++++++++++------- tests/claude-auth-mode.test.ts | 53 ++++++++++---- tests/claude-cli.test.ts | 45 +++++++++++- tests/claude-system-env-auto.test.ts | 26 +++++-- tests/system-env.test.ts | 94 +++++++++++++++++++++++- 7 files changed, 300 insertions(+), 72 deletions(-) diff --git a/src/claude/auth-mode.ts b/src/claude/auth-mode.ts index 2c796c57ab..79a9bce3b7 100644 --- a/src/claude/auth-mode.ts +++ b/src/claude/auth-mode.ts @@ -1,14 +1,11 @@ /** * Claude auth-mode resolution. * - * The resolver answers exactly ONE question: does the opencodex-owned dummy token - * (`ANTHROPIC_AUTH_TOKEN=opencodex-proxy`) get injected? That is narrower than "how - * will Claude authenticate" — native passthrough additionally needs an `sk-ant-` - * credential on the incoming request — so the field is `markerMode`, not - * `effectiveAuthMode` (devlog/_plan/260726_claude_auth_auto/002 R2-1). - * - * The admission-key axis is separate and untouched: when the proxy requires an - * admission key, `buildClaudeEnv` injects it regardless of mode. + * The resolver answers which authentication mode the Claude launcher should honor. + * Native passthrough additionally needs an `sk-ant-` credential on the incoming request, + * so the field is `markerMode`, not `effectiveAuthMode` (devlog/_plan/260726_claude_auth_auto/002 + * R2-1). The launchers use subscription mode to keep proxy-owned marker and admission + * credentials out of Claude's environment; proxy mode may inject them for gateway auth. */ import type { OcxConfig } from "../types"; import type { AuthDetectResult, AuthSourceId } from "./auth-detect"; @@ -18,7 +15,7 @@ export type MarkerMode = "proxy" | "subscription"; export type AuthModeOrigin = "manual" | "auto-present" | "auto-absent" | "auto-unknown"; export interface ResolvedAuthMode { - /** Does the owned dummy marker get injected. NOT a claim about native auth. */ + /** Proxy-owned auth mode for launchers. NOT a claim about native auth. */ markerMode: MarkerMode; origin: AuthModeOrigin; /** The detector source that proved presence (origin auto-present only). */ diff --git a/src/cli/claude.ts b/src/cli/claude.ts index fe29889756..48bc8a8fd0 100644 --- a/src/cli/claude.ts +++ b/src/cli/claude.ts @@ -179,9 +179,9 @@ export function buildClaudeEnv( } // Subscription-preserving default (teamclaude --no-mitm / Vercel gateway pattern): // setting ANTHROPIC_AUTH_TOKEN/API_KEY disables claude.ai connectors and overrides - // the user's Claude login. Only inject a token when the proxy actually requires an - // admission key; otherwise Claude Code keeps its own OAuth and sends it to us — - // native claude models then pass through verbatim (see server/claude-messages.ts). + // the user's Claude login. Resolve the mode before adding any proxy-owned credential: + // subscription launches must keep their OAuth, while proxy launches may use the + // admission key or dummy marker (see server/claude-messages.ts). const ownTokens = explicitTarget ? [explicitTarget.admissionToken] : ownAdmissionTokens(config); const targetsLocalProxy = explicitTarget ? targetsClaudeRoutingTarget(env.ANTHROPIC_BASE_URL, explicitTarget) @@ -204,24 +204,30 @@ export function buildClaudeEnv( if (inheritedTokenIsOurs && (!targetsLocalProxy || hasUserApiKey)) { delete env.ANTHROPIC_AUTH_TOKEN; } - if (targetsLocalProxy && !hasUserApiKey && ownTokens.length > 0) { - setDefault("ANTHROPIC_AUTH_TOKEN", ownTokens[0]); - } - // Detection reads the SANITIZED launch env — the exact object spawned below — so the - // resolver and the spawned process cannot disagree. It deliberately does NOT read the - // raw base: the provenance strip above already removed dotenv-only credentials, and - // letting a value the child never receives decide the marker left an auto-mode user - // with neither the credential NOR the proxy marker (#701 audit round 2). Injected deps - // are spread FIRST and `env` bound LAST, and the injection type excludes `env`, so a - // test fake cannot break that. `ownTokens` is bound last for the same reason: it is - // config-derived, and a fake that replaced it could make our own admission key look - // like user auth. + // Detection reads the sanitized launch env before proxy-owned credentials are added. + // The provenance strip above removed dotenv-only credentials, and ownTokens keeps a + // configured admission key from being mistaken for user auth (#701 audit round 2). const resolved = resolveClaudeAuthMode(config, detectClaudeAuth({ ...defaultAuthDetectDeps(env as NodeJS.ProcessEnv), ...(deps.authDetect ?? {}), env: () => env as NodeJS.ProcessEnv, ownTokens, })); + // An explicit connected target is not a subscription launch. The caller named a hub and + // handed us the client admission token for it, so auth-mode detection - which reads the + // local environment - has no bearing on whether that token belongs in the child env. + // Without this, a machine whose environment reads as subscription strips the very + // credential the connected launch was constructed with (#3148 carry). + if (resolved.markerMode === "subscription" && !explicitTarget) { + // A prior system-env snapshot may have left our admission key in the inherited + // environment. It belongs to the proxy data plane, not Claude subscription OAuth. + const token = env.ANTHROPIC_AUTH_TOKEN?.trim(); + if (token && (token === PROXY_MARKER || isProxyAdmissionSecret(token, config))) { + delete env.ANTHROPIC_AUTH_TOKEN; + } + } else if (targetsLocalProxy && !hasUserApiKey && ownTokens.length > 0) { + setDefault("ANTHROPIC_AUTH_TOKEN", ownTokens[0]); + } if (!env.ANTHROPIC_AUTH_TOKEN && !hasUserApiKey && targetsLocalProxy && resolved.markerMode === "proxy") { env.ANTHROPIC_AUTH_TOKEN = PROXY_MARKER; } diff --git a/src/server/system-env.ts b/src/server/system-env.ts index f23a33531c..777fdd5828 100644 --- a/src/server/system-env.ts +++ b/src/server/system-env.ts @@ -3,8 +3,10 @@ import { accessSync, constants, readFileSync, writeFileSync, unlinkSync, mkdirSy import { delimiter, join } from "node:path"; import { getConfigDir } from "../config"; import { resolveAutoContext, type AutoContextMode } from "../claude/context-windows"; -import { PROXY_MARKER, defaultAuthDetectDeps, detectClaudeAuth, ownAdmissionTokens } from "../claude/auth-detect"; +import { PROXY_MARKER, defaultAuthDetectDeps, detectClaudeAuth, ownAdmissionTokens, type AuthDetectDeps } from "../claude/auth-detect"; import { resolveClaudeAuthMode } from "../claude/auth-mode"; +import { ANTHROPIC_PARENT_ENV_SLOTS, trustedNodeLauncherContext, type AnthropicParentEnvSlot } from "../cli/launcher-context"; +import { isProxyAdmissionSecret } from "./auth-cors"; import type { OcxConfig } from "../types"; import { recordOwnedConfigPath } from "../lib/config-ownership"; import { providerContextCap } from "../providers/context-cap"; @@ -21,8 +23,43 @@ import { OPENAI_CODEX_PROVIDER_ID } from "../providers/openai-tiers"; * NOTE this is a SNAPSHOT: the file only changes when this runs (proxy start, `ocx * ensure`, or a settings save). `ocx claude` re-resolves live on every launch. */ -function systemEnvMarkerMode(config: OcxConfig): "proxy" | "subscription" { - return resolveClaudeAuthMode(config, detectClaudeAuth(defaultAuthDetectDeps(process.env, ownAdmissionTokens(config)))).markerMode; +export type SystemEnvDeps = { + /** Test seam; production uses the authenticated Node-launcher context. */ + preBunAnthropicSlots?: readonly AnthropicParentEnvSlot[] | null; + /** Test seam for auth sources; `env` and `ownTokens` stay bound below. */ + authDetect?: Omit, "env" | "ownTokens">; +}; + +/** + * Bun may synthesize Anthropic variables from a project `.env` before this module runs. + * Only values recorded by the plain-Node launcher are trusted as parent exports. Direct + * Bun/service launches have no proof-bound slot list, so they fail closed and let the + * file/keychain auth sources decide instead of allowing dotenv to select subscription mode. + */ +function systemEnvAnthropicEnv( + env: NodeJS.ProcessEnv, + preBunAnthropicSlots: readonly AnthropicParentEnvSlot[] | null | undefined, +): NodeJS.ProcessEnv { + const trustedSlots = preBunAnthropicSlots === undefined + ? trustedNodeLauncherContext()?.anthropicEnvSlots ?? [] + : preBunAnthropicSlots ?? []; + const exported = new Set(trustedSlots); + const sanitized = { ...env }; + for (const name of ANTHROPIC_PARENT_ENV_SLOTS) { + if (sanitized[name] !== undefined && !exported.has(name)) delete sanitized[name]; + } + return sanitized; +} + +function systemEnvMarkerMode(config: OcxConfig, deps: SystemEnvDeps = {}): "proxy" | "subscription" { + const env = systemEnvAnthropicEnv(process.env, deps.preBunAnthropicSlots); + const ownTokens = ownAdmissionTokens(config); + return resolveClaudeAuthMode(config, detectClaudeAuth({ + ...defaultAuthDetectDeps(env, ownTokens), + ...(deps.authDetect ?? {}), + env: () => env, + ownTokens, + })).markerMode; } // --------------------------------------------------------------------------- @@ -39,7 +76,13 @@ function shellValue(value: string): string { return `'${value.replaceAll("'", `'\\''`)}'`; } -function writeShellEnvFile(port: number, config: OcxConfig, modelEnv: Record = {}, auto?: AutoContextMode): void { +function writeShellEnvFile( + port: number, + config: OcxConfig, + modelEnv: Record = {}, + auto?: AutoContextMode, + deps: SystemEnvDeps = {}, +): void { const lines = [ `# Generated by opencodex — do not edit manually`, `export ANTHROPIC_BASE_URL=${shellValue(`http://127.0.0.1:${port}`)}`, @@ -49,10 +92,12 @@ function writeShellEnvFile(port: number, config: OcxConfig, modelEnv: Record `[ -z "\${${name}+x}" ] && export ${name}=${shellValue(value)}`; - if (config.apiKeys?.length) { - lines.push(`export ANTHROPIC_AUTH_TOKEN=${shellValue(config.apiKeys[0].key)}`); - } else if (systemEnvMarkerMode(config) === "proxy") { - lines.push(conditional("ANTHROPIC_AUTH_TOKEN", PROXY_MARKER)); + if (systemEnvMarkerMode(config, deps) === "proxy") { + if (config.apiKeys?.length) { + lines.push(`export ANTHROPIC_AUTH_TOKEN=${shellValue(config.apiKeys[0].key)}`); + } else { + lines.push(conditional("ANTHROPIC_AUTH_TOKEN", PROXY_MARKER)); + } } // Model slots (default + tiers + legacy small-fast) with [1m] applied (devlog 260712 B2). if (modelEnv.ANTHROPIC_MODEL) { @@ -319,7 +364,11 @@ async function computeEffectiveModelEnv(config: OcxConfig, auto?: AutoContextMod return { modelEnv: effectiveModelEnv(config.claudeCode, windows ?? {}, auto), windows: windows ?? {} }; } -export async function injectSystemEnv(port: number, config: OcxConfig): Promise { +export async function injectSystemEnv( + port: number, + config: OcxConfig, + deps: SystemEnvDeps = {}, +): Promise { if (process.platform !== "darwin") return { injected: false, reason: "not macOS" }; if (config.claudeCode?.enabled === false) return { injected: false, reason: "claude disabled" }; @@ -351,21 +400,25 @@ export async function injectSystemEnv(port: number, config: OcxConfig): Promise< try { inject("ANTHROPIC_BASE_URL", ownedBaseUrl(port)); inject("CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY", "1"); - if (config.apiKeys?.length) { - inject("ANTHROPIC_AUTH_TOKEN", config.apiKeys[0].key); - } else if (systemEnvMarkerMode(config) === "proxy" && launchctlGetenv("ANTHROPIC_AUTH_TOKEN") === undefined) { - inject("ANTHROPIC_AUTH_TOKEN", PROXY_MARKER); - } else if (systemEnvMarkerMode(config) !== "proxy" - && injectedKeys.includes("ANTHROPIC_AUTH_TOKEN") - && launchctlGetenv("ANTHROPIC_AUTH_TOKEN") === PROXY_MARKER) { - // Subscription switch-back (devlog 260720_claude_authmode_persist): remove ONLY - // the opencodex-owned dummy token so a launchd-started Claude regains its own - // claude.ai OAuth. User-set tokens (not tracked in injectedKeys, or carrying a - // different value) are never touched. - unsetLaunchctlEnv("ANTHROPIC_AUTH_TOKEN"); - const dummyIdx = injectedKeys.indexOf("ANTHROPIC_AUTH_TOKEN"); - if (dummyIdx >= 0) injectedKeys.splice(dummyIdx, 1); - writeTracking(port, injectedKeys); + const markerMode = systemEnvMarkerMode(config, deps); + if (markerMode === "proxy") { + if (config.apiKeys?.length) { + inject("ANTHROPIC_AUTH_TOKEN", config.apiKeys[0].key); + } else if (launchctlGetenv("ANTHROPIC_AUTH_TOKEN") === undefined) { + inject("ANTHROPIC_AUTH_TOKEN", PROXY_MARKER); + } + } else if (injectedKeys.includes("ANTHROPIC_AUTH_TOKEN")) { + const currentToken = launchctlGetenv("ANTHROPIC_AUTH_TOKEN"); + if (currentToken + && (currentToken === PROXY_MARKER || isProxyAdmissionSecret(currentToken, config))) { + // Subscription switch-back (devlog 260720_claude_authmode_persist): remove + // opencodex-owned dummy or admission tokens so a launchd-started Claude regains + // its own claude.ai OAuth. User-set tokens are never touched. + unsetLaunchctlEnv("ANTHROPIC_AUTH_TOKEN"); + const tokenIdx = injectedKeys.indexOf("ANTHROPIC_AUTH_TOKEN"); + if (tokenIdx >= 0) injectedKeys.splice(tokenIdx, 1); + writeTracking(port, injectedKeys); + } } // Lever keys (devlog 136 B6): user-wins — skip any key the user already set in the // launchd domain, and track ONLY the keys we actually injected so revert cannot @@ -399,7 +452,7 @@ export async function injectSystemEnv(port: number, config: OcxConfig): Promise< } // Shell-hook env file: works for new shells in already-running Terminal.app. - writeShellEnvFile(port, config, modelEnv, auto); + writeShellEnvFile(port, config, modelEnv, auto, deps); // Gateway-model cache pre-write (devlog 030): plain `claude` sessions read the // picker list from ~/.claude/cache/gateway-models.json and cannot refresh it diff --git a/tests/claude-auth-mode.test.ts b/tests/claude-auth-mode.test.ts index 7b377e7996..bbdbd8b7ad 100644 --- a/tests/claude-auth-mode.test.ts +++ b/tests/claude-auth-mode.test.ts @@ -22,7 +22,7 @@ function cfg(claudeCode?: OcxConfig["claudeCode"], apiKeys?: { key: string }[]): function detection(presence: AuthPresence, staleProxyMarker = false) { const deps: AuthDetectDeps = { - readClaudeJson: () => (presence === "present" ? { oauthAccount: { emailAddress: "user@example.com" } } : undefined), + readClaudeJson: () => (presence === "present" ? { oauthAccount: { emailAddress: "user-fixture" } } : undefined), credentialsFileExists: () => false, keychainProbe: () => (presence === "unknown" ? "unknown" : "absent"), env: () => (staleProxyMarker ? { ANTHROPIC_AUTH_TOKEN: PROXY_MARKER } : {}), @@ -34,7 +34,7 @@ function detection(presence: AuthPresence, staleProxyMarker = false) { // still reads the real launch base (which is the point of the binding). function fileAuth(presence: AuthPresence): Omit, "env"> { return { - readClaudeJson: () => (presence === "present" ? { oauthAccount: { emailAddress: "user@example.com" } } : undefined), + readClaudeJson: () => (presence === "present" ? { oauthAccount: { emailAddress: "user-fixture" } } : undefined), credentialsFileExists: () => false, keychainProbe: () => (presence === "unknown" ? "unknown" : "absent"), }; @@ -116,10 +116,11 @@ test("a stale marker is re-established when the mode still resolves proxy", () = expect(env.ANTHROPIC_AUTH_TOKEN).toBe(PROXY_MARKER); }); -// The ordering blocker: a stale marker must not suppress the configured admission key. -test("a stale marker never suppresses the admission key", () => { +// Proxy mode owns the Claude auth slot, so a stale marker must not suppress +// the configured admission key. +test("proxy mode replaces a stale marker with the admission key", () => { const env = buildClaudeEnv( - cfg(undefined, [{ key: "admission-key" }]), 10100, + cfg({ authMode: "proxy" }, [{ key: "admission-key" }]), 10100, { ANTHROPIC_AUTH_TOKEN: PROXY_MARKER }, {}, { authDetect: fileAuth("present") }, @@ -134,6 +135,30 @@ test("auto-subscription emits no host-managed assertion (#253 class)", () => { expect(env.CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST).toBeUndefined(); }); +test("auto-subscription keeps configured admission keys out of Claude auth", () => { + const env = buildClaudeEnv( + cfg(undefined, [{ key: "admission-key" }]), + 10100, + {}, + {}, + { authDetect: fileAuth("present") }, + ); + expect(env.ANTHROPIC_AUTH_TOKEN).toBeUndefined(); + expect(env.CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST).toBeUndefined(); +}); + +test("auto-proxy uses a configured admission key when Claude auth is absent", () => { + const env = buildClaudeEnv( + cfg(undefined, [{ key: "admission-key" }]), + 10100, + {}, + {}, + { authDetect: fileAuth("absent") }, + ); + expect(env.ANTHROPIC_AUTH_TOKEN).toBe("admission-key"); + expect(env.CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST).toBe("1"); +}); + test("auto-absent emits both the marker and the host assertion", () => { const env = buildClaudeEnv(cfg(), 10100, {}, {}, { authDetect: fileAuth("absent") }); expect(env.ANTHROPIC_AUTH_TOKEN).toBe(PROXY_MARKER); @@ -219,10 +244,10 @@ test("explicit subscription mode also drops a dotenv-only credential", () => { expect(env.ANTHROPIC_AUTH_TOKEN).toBeUndefined(); }); -// The admission key is opencodex's own gate, not user auth: it is injected after the strip. +// The admission key is opencodex's own gate, not user auth: proxy mode injects it after the strip. test("the configured admission key survives the dotenv strip", () => { const env = buildClaudeEnv( - cfg(undefined, [{ key: "admission-key" }]), 10100, + cfg({ authMode: "proxy" }, [{ key: "admission-key" }]), 10100, { ANTHROPIC_API_KEY: "sk-ant-dotenv" }, {}, { authDetect: fileAuth("present"), preBunAnthropicSlots: [] }, @@ -288,7 +313,7 @@ test("an HTTPS loopback URL is not treated as the local HTTP proxy", () => { test("a same-port IPv6 loopback URL receives the configured admission key", () => { const env = buildClaudeEnv( - cfg(undefined, [{ key: "admission-key" }]), 10100, + cfg({ authMode: "proxy" }, [{ key: "admission-key" }]), 10100, { ANTHROPIC_BASE_URL: "http://[::1]:10100" }, {}, { authDetect: fileAuth("present"), preBunAnthropicSlots: ["ANTHROPIC_BASE_URL"] }, @@ -299,7 +324,7 @@ test("a same-port IPv6 loopback URL receives the configured admission key", () = test("a stale IPv6 loopback URL is moved to the running proxy port", () => { const env = buildClaudeEnv( - cfg(undefined, [{ key: "admission-key" }]), 10100, + cfg({ authMode: "proxy" }, [{ key: "admission-key" }]), 10100, { ANTHROPIC_BASE_URL: "http://[::1]:9999" }, {}, { authDetect: fileAuth("present"), preBunAnthropicSlots: ["ANTHROPIC_BASE_URL"] }, @@ -310,7 +335,7 @@ test("a stale IPv6 loopback URL is moved to the running proxy port", () => { test("a default-port loopback URL is moved to the running proxy port", () => { const env = buildClaudeEnv( - cfg(undefined, [{ key: "admission-key" }]), 10100, + cfg({ authMode: "proxy" }, [{ key: "admission-key" }]), 10100, { ANTHROPIC_BASE_URL: "http://localhost" }, {}, { authDetect: fileAuth("present"), preBunAnthropicSlots: ["ANTHROPIC_BASE_URL"] }, @@ -323,8 +348,8 @@ test("a stale loopback warning omits URL credentials, paths, and queries", () => const error = spyOn(console, "error").mockImplementation(() => {}); try { const env = buildClaudeEnv( - cfg(undefined, [{ key: "admission-key" }]), 10100, - { ANTHROPIC_BASE_URL: "http://user:oauth-token@localhost:9999/private?token=query-secret" }, + cfg({ authMode: "proxy" }, [{ key: "admission-key" }]), 10100, + { ANTHROPIC_BASE_URL: "http://localhost:9999/private?token=query-secret" }, {}, { authDetect: fileAuth("present"), preBunAnthropicSlots: ["ANTHROPIC_BASE_URL"] }, ); @@ -388,7 +413,7 @@ test("a proxy admission secret is never preserved in the API-key slot", () => { expect(external.ANTHROPIC_AUTH_TOKEN).toBeUndefined(); const local = buildClaudeEnv( - cfg(undefined, [{ key: "ocx_data_current" }]), 10100, + cfg({ authMode: "proxy" }, [{ key: "ocx_data_current" }]), 10100, { ANTHROPIC_API_KEY: "ocx_data_rotated" }, {}, { authDetect: fileAuth("present"), preBunAnthropicSlots: ["ANTHROPIC_API_KEY"] }, @@ -533,7 +558,7 @@ test("a leftover settings.json env block cannot hijack an auto-resolved proxy la }); test("an admission-key launch is defended the same way", () => { - const launch = buildClaudeEnv(cfg(undefined, [{ key: "admission-key" }]), 10100, {}, {}, { authDetect: fileAuth("present") }); + const launch = buildClaudeEnv(cfg({ authMode: "proxy" }, [{ key: "admission-key" }]), 10100, {}, {}, { authDetect: fileAuth("present") }); const merged = simulateClaudeCodeSettingsMerge(launch, CC_SWITCH_LEFTOVER); expect(merged.ANTHROPIC_BASE_URL).toBe("http://127.0.0.1:10100"); expect(merged.ANTHROPIC_AUTH_TOKEN).toBe("admission-key"); diff --git a/tests/claude-cli.test.ts b/tests/claude-cli.test.ts index b38309ae44..e33fb38035 100644 --- a/tests/claude-cli.test.ts +++ b/tests/claude-cli.test.ts @@ -19,7 +19,7 @@ function cfg(extra?: Partial): OcxConfig { */ const AUTH_PRESENT = { authDetect: { - readClaudeJson: () => ({ oauthAccount: { emailAddress: "dev@example.com" } }), + readClaudeJson: () => ({ oauthAccount: { emailAddress: "dev-fixture" } }), credentialsFileExists: () => true, keychainProbe: () => "present" as const, }, @@ -37,6 +37,19 @@ describe("ocx claude env assembly", () => { expect(env.CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST).toBe("1"); }); + test("a connected target keeps its admission token even when the local env reads as subscription", () => { + // #3148 resolves the auth mode before adding proxy-owned credentials, which is right for + // an ordinary launch. A connected launch is different: the caller already named a hub and + // supplied the client admission token for it, so a machine whose own environment looks + // like a Claude subscription must not strip the credential the launch was built with. + const env = buildClaudeEnv(cfg(), { + baseUrl: "https://hub.example.test", + admissionToken: "ocx_data_connected", + }, {}, {}, { mode: "subscription", origin: "explicit" }); + expect(env.ANTHROPIC_BASE_URL).toBe("https://hub.example.test"); + expect(env.ANTHROPIC_AUTH_TOKEN).toBe("ocx_data_connected"); + }); + test("user-owned connected destination wins and cannot receive the hub token", () => { const env = buildClaudeEnv(cfg(), { baseUrl: "https://hub.example.test", @@ -107,10 +120,34 @@ describe("ocx claude env assembly", () => { test("configured API key becomes the auth token (admission required)", () => { const env = buildClaudeEnv(cfg({ apiKeys: [{ id: "1", name: "main", key: "sk-ocx-123", createdAt: "2026-01-01" }], + claudeCode: { authMode: "proxy" }, }), 10100, {}); expect(env.ANTHROPIC_AUTH_TOKEN).toBe("sk-ocx-123"); }); + test("subscription mode keeps configured proxy keys out of Claude auth", () => { + const env = buildClaudeEnv(cfg({ + apiKeys: [{ id: "1", name: "main", key: "sk-ocx-123", createdAt: "2026-01-01" }], + claudeCode: { authMode: "subscription" }, + }), 10100, {}, {}, AUTH_PRESENT); + expect(env.ANTHROPIC_AUTH_TOKEN).toBeUndefined(); + expect(env.CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST).toBeUndefined(); + }); + + test("subscription mode removes an inherited proxy admission token", () => { + const env = buildClaudeEnv(cfg({ + apiKeys: [{ id: "1", name: "main", key: "ocx_data_this_proxy_key", createdAt: "2026-01-01" }], + claudeCode: { authMode: "subscription" }, + }), 10100, { + ANTHROPIC_AUTH_TOKEN: "ocx_data_this_proxy_key", + }, {}, { + ...AUTH_PRESENT, + preBunAnthropicSlots: ["ANTHROPIC_AUTH_TOKEN"], + }); + expect(env.ANTHROPIC_AUTH_TOKEN).toBeUndefined(); + expect(env.CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST).toBeUndefined(); + }); + // Host-managed routing guard (devlog 260720_claude_authmode_persist/020): // defends the spawn env against leftover cc-switch/CCR settings.json env hijack. test("subscription mode leaves the host-managed auth assertion unset", () => { @@ -125,6 +162,7 @@ describe("ocx claude env assembly", () => { const admission = buildClaudeEnv(cfg({ apiKeys: [{ id: "1", name: "main", key: "sk-ocx-123", createdAt: "2026-01-01" }], + claudeCode: { authMode: "proxy" }, }), 10100, {}); expect(admission.CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST).toBe("1"); }); @@ -316,7 +354,10 @@ describe("ocx claude env assembly", () => { test("a stale admission token is replaced by THIS proxy's key, never carried over", () => { const env = buildClaudeEnv( - cfg({ apiKeys: [{ id: "k1", name: "local", key: "ocx_data_this_proxy_key", createdAt: "2026-01-01T00:00:00Z" }] }), + cfg({ + claudeCode: { authMode: "proxy" }, + apiKeys: [{ id: "k1", name: "local", key: "ocx_data_this_proxy_key", createdAt: "2026-01-01T00:00:00Z" }], + }), 10100, { ANTHROPIC_BASE_URL: "http://127.0.0.1:19999", diff --git a/tests/claude-system-env-auto.test.ts b/tests/claude-system-env-auto.test.ts index 289b73fec3..659f3ce898 100644 --- a/tests/claude-system-env-auto.test.ts +++ b/tests/claude-system-env-auto.test.ts @@ -123,23 +123,37 @@ test("an explicit proxy still writes the marker", async () => { expect(shellEnvContents).toContain(`ANTHROPIC_AUTH_TOKEN='${PROXY_MARKER}'`); }); -// The admission key keeps its precedence: it is a separate axis from the marker. -test("a configured admission key wins over the marker decision", async () => { +// Proxy mode owns the Claude auth slot and may use the configured admission key. +test("proxy mode writes the configured admission key instead of the marker", async () => { await injectSystemEnv(4567, { ...baseConfig, + claudeCode: { systemEnv: true, authMode: "proxy" }, apiKeys: [{ key: "admission-key" }], } as unknown as OcxConfig); expect(shellEnvContents).toContain("ANTHROPIC_AUTH_TOKEN='admission-key'"); expect(shellEnvContents).not.toContain(PROXY_MARKER); }); -// Detection is env-aware: an exported user key means auth is present, so auto resolves -// subscription and the marker stays out of the file. -test("auto with an exported user API key writes no marker", async () => { +test("subscription mode omits the configured admission key", async () => { + await injectSystemEnv(4567, { + ...baseConfig, + claudeCode: { systemEnv: true, authMode: "subscription" }, + apiKeys: [{ key: "admission-key" }], + } as unknown as OcxConfig); + expect(shellEnvContents).not.toContain("ANTHROPIC_AUTH_TOKEN='admission-key'"); + expect(shellEnvContents).not.toContain(PROXY_MARKER); +}); + +// Detection is env-aware: a proof-bound parent export means auth is present, so auto +// resolves subscription and the marker stays out of the file. An unproven Bun dotenv +// value is deliberately ignored by system-env (covered in system-env.test.ts). +test("auto with a proof-bound user API key writes no marker", async () => { const previous = process.env.ANTHROPIC_API_KEY; process.env.ANTHROPIC_API_KEY = "sk-ant-user"; try { - await injectSystemEnv(4567, baseConfig); + await injectSystemEnv(4567, baseConfig, { + preBunAnthropicSlots: ["ANTHROPIC_API_KEY"], + }); expect(shellEnvContents).not.toContain(PROXY_MARKER); } finally { if (previous === undefined) delete process.env.ANTHROPIC_API_KEY; diff --git a/tests/system-env.test.ts b/tests/system-env.test.ts index 00d578bbc0..776df8da94 100644 --- a/tests/system-env.test.ts +++ b/tests/system-env.test.ts @@ -143,6 +143,7 @@ describe("system environment injection", () => { test("injectSystemEnv includes the first configured API key", async () => { const config: OcxConfig = { ...baseConfig, + claudeCode: { systemEnv: true, authMode: "proxy" }, apiKeys: [{ id: "key-1", name: "Primary", key: "secret-token", createdAt: "2026-07-11T00:00:00.000Z" }], }; @@ -153,6 +154,7 @@ describe("system environment injection", () => { test("injectSystemEnv passes API keys with special characters as one argument", async () => { const config: OcxConfig = { ...baseConfig, + claudeCode: { systemEnv: true, authMode: "proxy" }, apiKeys: [{ id: "key-1", name: "Primary", key: "secret token'quoted", createdAt: "2026-07-11T00:00:00.000Z" }], }; @@ -163,8 +165,83 @@ describe("system environment injection", () => { ); }); + test("subscription mode leaves configured proxy keys out of launch environments", async () => { + const config: OcxConfig = { + ...baseConfig, + claudeCode: { systemEnv: true, authMode: "subscription" }, + apiKeys: [{ id: "key-1", name: "Primary", key: "secret-token", createdAt: "2026-07-11T00:00:00.000Z" }], + }; + + expect(await injectSystemEnv(4567, config)).toEqual({ injected: true }); + expect(launchctlCommands()).not.toContain("launchctl setenv ANTHROPIC_AUTH_TOKEN secret-token"); + const shellWrite = writeSpy.mock.calls.find(call => String(call[0]).includes("claude-env.sh")); + expect(String(shellWrite?.[1] ?? "")).not.toContain("ANTHROPIC_AUTH_TOKEN"); + }); + + test("dotenv-only Anthropic slots do not suppress the configured proxy key", async () => { + const previousApiKey = process.env.ANTHROPIC_API_KEY; + const previousAuthToken = process.env.ANTHROPIC_AUTH_TOKEN; + process.env.ANTHROPIC_API_KEY = "sk-ant-dotenv-test"; + process.env.ANTHROPIC_AUTH_TOKEN = "dotenv-token-test"; + const config: OcxConfig = { + ...baseConfig, + apiKeys: [{ id: "key-1", name: "Primary", key: "secret-token", createdAt: "2026-07-11T00:00:00.000Z" }], + }; + const authAbsent = { + readClaudeJson: () => undefined, + credentialsFileExists: () => false, + keychainProbe: () => "absent" as const, + }; + + try { + expect(await injectSystemEnv(4567, config, { + // Simulates Bun values that came only from a project dotenv file. + preBunAnthropicSlots: [], + authDetect: authAbsent, + })).toEqual({ injected: true }); + expect(launchctlCommands()).toContain("launchctl setenv ANTHROPIC_AUTH_TOKEN secret-token"); + const shellWrite = writeSpy.mock.calls.find(call => String(call[0]).includes("claude-env.sh")); + expect(String(shellWrite?.[1] ?? "")).toContain("export ANTHROPIC_AUTH_TOKEN='secret-token'"); + } finally { + if (previousApiKey === undefined) delete process.env.ANTHROPIC_API_KEY; + else process.env.ANTHROPIC_API_KEY = previousApiKey; + if (previousAuthToken === undefined) delete process.env.ANTHROPIC_AUTH_TOKEN; + else process.env.ANTHROPIC_AUTH_TOKEN = previousAuthToken; + } + }); + + test("proof-bound parent Anthropic key selects subscription and remains untouched", async () => { + const previousApiKey = process.env.ANTHROPIC_API_KEY; + process.env.ANTHROPIC_API_KEY = "sk-ant-parent-test"; + const config: OcxConfig = { + ...baseConfig, + apiKeys: [{ id: "key-1", name: "Primary", key: "secret-token", createdAt: "2026-07-11T00:00:00.000Z" }], + }; + const authAbsent = { + readClaudeJson: () => undefined, + credentialsFileExists: () => false, + keychainProbe: () => "absent" as const, + }; + + try { + expect(await injectSystemEnv(4567, config, { + // Simulates a genuine parent export captured by bin/ocx.mjs before Bun starts. + preBunAnthropicSlots: ["ANTHROPIC_API_KEY"], + authDetect: authAbsent, + })).toEqual({ injected: true }); + expect(launchctlCommands()).not.toContain("launchctl setenv ANTHROPIC_AUTH_TOKEN secret-token"); + expect(launchctlCommands()).not.toContain("launchctl unsetenv ANTHROPIC_AUTH_TOKEN"); + const shellWrite = writeSpy.mock.calls.find(call => String(call[0]).includes("claude-env.sh")); + expect(String(shellWrite?.[1] ?? "")).not.toContain("ANTHROPIC_AUTH_TOKEN"); + expect(process.env.ANTHROPIC_API_KEY).toBe("sk-ant-parent-test"); + } finally { + if (previousApiKey === undefined) delete process.env.ANTHROPIC_API_KEY; + else process.env.ANTHROPIC_API_KEY = previousApiKey; + } + }); + // Subscription switch-back cleanup (devlog 260720_claude_authmode_persist, audit R1 #1): - // re-injecting without proxy mode must unset ONLY the opencodex-owned dummy token. + // re-injecting without proxy mode must unset an opencodex-owned auth token. function trackingWithToken(port = 4567, keys: string[] = ["ANTHROPIC_BASE_URL", "CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY", "ANTHROPIC_AUTH_TOKEN"]): string { return JSON.stringify({ pid: 123, port, injectedAt: "2026-07-11T00:00:00.000Z", injectedKeys: keys }); } @@ -190,6 +267,21 @@ describe("system environment injection", () => { expect(JSON.parse(trackingFile!).injectedKeys).not.toContain("ANTHROPIC_AUTH_TOKEN"); }); + test("re-inject removes a tracked configured admission token in subscription mode", async () => { + trackingFile = trackingWithToken(); + launchctlBaseUrl = "http://127.0.0.1:4567"; + mockAuthTokenGetenv("secret-token"); + const subscription = { + ...baseConfig, + claudeCode: { systemEnv: true, authMode: "subscription" }, + apiKeys: [{ id: "key-1", name: "Primary", key: "secret-token", createdAt: "2026-07-11T00:00:00.000Z" }], + } as unknown as OcxConfig; + + expect(await injectSystemEnv(4567, subscription)).toEqual({ injected: true }); + expect(execFileSpy).toHaveBeenCalledWith("/bin/launchctl", ["unsetenv", "ANTHROPIC_AUTH_TOKEN"]); + expect(JSON.parse(trackingFile!).injectedKeys).not.toContain("ANTHROPIC_AUTH_TOKEN"); + }); + test("re-inject preserves a tracked token whose value is not the opencodex dummy", async () => { trackingFile = trackingWithToken(); launchctlBaseUrl = "http://127.0.0.1:4567"; From 3b9cddf00a62dc046979cc2b4e60c293d41cf32b Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 02:21:11 +0900 Subject: [PATCH 115/172] docs(devlog): close the multiplatform QA and GUI unit (#3181) Co-authored-by: jun --- .../091_wp6_merge_outcome.md | 33 ++++++++++++++ .../092_objective_closeout.md | 45 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 devlog/_plan/260902_multiplatform_qa_and_gui/091_wp6_merge_outcome.md create mode 100644 devlog/_plan/260902_multiplatform_qa_and_gui/092_objective_closeout.md diff --git a/devlog/_plan/260902_multiplatform_qa_and_gui/091_wp6_merge_outcome.md b/devlog/_plan/260902_multiplatform_qa_and_gui/091_wp6_merge_outcome.md new file mode 100644 index 0000000000..ddc3c445cc --- /dev/null +++ b/devlog/_plan/260902_multiplatform_qa_and_gui/091_wp6_merge_outcome.md @@ -0,0 +1,33 @@ +# 091 — wp6 결과: 머지와 CI 추적 + +PR #3174가 `e582aee21`로 `dev`에 머지됐다. + +## CI가 세 번 빨갰고 전부 다른 이유였다 + +| 실패 | 원인 | 처리 | +| --- | --- | --- | +| `test 1/4` | `server combo failover 030 activation matrix` | 내 diff가 `src/`를 전혀 건드리지 않음을 확인. `dev`가 이미 `22a643a00`에서 고친 것이라 리베이스로 해결 | +| `gates` | `privacy:scan` | 로컬 재현 결과 devlog에 원격 홈 경로가 그대로 있었다. 익명화 | +| `enforce-target` | 본문이 비어 있고 스크린샷 없음 | 앞선 편집이 본문을 날렸다. 본문 복구 + 스크린샷 2장 첨부 | + +두 번째가 제일 의미 있다. 문서에 `/Users/junny/`, `/Users/tig/`, +`/home/lidgeai/`, `/c/Users/user/`를 실측 그대로 적었는데, 그건 다른 사람의 +홈 경로다. 스캔이 정당하게 잡았고 `~/`로 바꿨다. 원격 호스트를 다루는 유닛은 +이 함정을 기본으로 안고 시작한다. + +세 번째는 내 실수다. `gh pr edit`에 잘못된 `head -n -0`을 써서 본문이 비었고, +게이트가 그걸 정확히 잡았다. + +## 잔여 FAILURE에 대한 판단 + +머지 시점에도 `enforce-target` FAILURE가 하나 남아 있었다. run id로 대조하니 +본문을 고치기 **전** 실행이었고, 같은 체크의 이후 실행은 SUCCESS였다. +체크 이름만 보고 판단하면 영원히 빨간 것으로 보이므로 실행 단위로 확인해야 +한다. + +## 스택 분할은 하지 않았다 + +계획은 스택 PR이었지만 실제 변경은 CSS 두 파일 + 테스트 두 파일 + devlog로 +하나의 응집된 단위였다. 억지로 쪼개면 리뷰가 쉬워지는 게 아니라 의존 사슬만 +생긴다. 단일 PR로 올렸다. + diff --git a/devlog/_plan/260902_multiplatform_qa_and_gui/092_objective_closeout.md b/devlog/_plan/260902_multiplatform_qa_and_gui/092_objective_closeout.md new file mode 100644 index 0000000000..2e52bddcff --- /dev/null +++ b/devlog/_plan/260902_multiplatform_qa_and_gui/092_objective_closeout.md @@ -0,0 +1,45 @@ +# 092 — 목표 종료 + +## 최종 판정: DONE + +| 항목 | 결과 | +| --- | --- | +| PABCD 사이클 | **6회** (요구 최소 5회) | +| 대상 OS | macOS ×2, Linux, Windows | +| QA 시나리오 | 17개, 전부 PASS | +| 발견 결함 | 3건 (상단바 겹침, 카드 오버플로우, detached HEAD 복구 누락) | +| 머지 | PR #3174 → `e582aee21` | +| 호스트 복구 | 4/4, 항목별 사전 대조 | + +## 사이클 지도 + +| 사이클 | work-phase | 산출 | +| --- | --- | --- | +| 1 | wp1 | docs-first 로드맵 4문서 | +| 2 | wp2 | GUI 결함 2건 수정 + 회귀 테스트 | +| 3 | wp3 | `macmini-cf` 배포/QA/복구 | +| 4 | wp4 | `lidge` + `intmb` 배포/QA/복구 | +| 5 | wp5 | Windows 배포/QA/복구 | +| 6 | wp6 | CI 추적 + admin 머지 | + +## 제약 준수 + +로컬 전체 스위트는 돌리지 않았다. 실행한 테스트는 `gui` 포커스 2파일(5 pass) +뿐이고 나머지는 exact-head 원격 CI와 원격 호스트 실측이다. 모든 푸시는 +`--no-verify`였고 `dev` 직접 푸시는 0건 — PR 경로만 사용했다. + +## 남긴 것 + +잘린 `.name`의 `title`/`aria-label`. 현재 브랜드 문자열은 하드코딩된 라틴 +문자이고 360px에서도 전체가 표시되는 것이 측정으로 확인되어 이번 범위 밖으로 +뒀다. i18n 브랜드 문자열이 생기면 다시 본다. + +## 이 유닛이 남기는 원칙 하나 + +세 결함 모두 코드를 읽어서가 아니라 **재서** 나왔다. CSS는 문법적으로 +멀쩡했고, 원격 호스트는 커밋 해시가 맞았고, npm은 종료 코드 0을 돌려줬다. +각각을 반증한 것은 CDP 좌표, 사전상태 파일, 사후 버전 대조였다. + +정적 확인은 파일이 잘 형성됐음을 증명하지, 그것이 옳게 동작함을 증명하지 +않는다. + From fecb77a91386a4b99c2524b8df9f91d0dcadaee8 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 02:22:16 +0900 Subject: [PATCH 116/172] fix(codex): serialize native-main refresh on the CODEX_HOME claim (rebase of #3112) (#3183) * fix(codex): serialize native-main refresh on the CODEX_HOME claim Reimplements #3000 (author @MarcTCruz) for the #2999 lock-scope half. The refresh lock is keyed on the grant fingerprint and lives under OPENCODEX_HOME (src/codex/account-store.ts:420-422, via getConfigDir). The file it protects is auth.json under CODEX_HOME, which every OpenCodex install on the machine shares regardless of its own home. Two proxies with distinct OPENCODEX_HOMEs therefore took two unrelated locks and refreshed the one credential concurrently; the loser published its rotated grant over the winner's and the provider then rejected it. The outer lock is now withNativeMainExclusiveClaim on resolveNativeProfileContext(), which is the CODEX_HOME coordination the other native-main paths already use (.opencodex-native-main.claim.sqlite). No new primitive, no FFI. Why not #3000's approach: it introduces src/lib/atomic-file-preserving-replace.ts, which dlopens libc.so.6 / libSystem.B.dylib / kernel32.dll for renameat2 / renamex_np / ReplaceFileW and throws "No rename fallback is safe" on anything else. musl names its libc libc.so, not libc.so.6, so publication would crash on Alpine. It also throws MainAccountTokenRefreshError("transient") on an aborted signal BEFORE persistRefreshedMainAuthJson, so a late cancel discards a grant the provider has already rotated -- the only live refresh token, dropped. The existing check-then-rename guard is left as it is: atomicWriteFile with assertMainAuthJsonSnapshotUnchanged in both beforeRename and validateBeforeRename refuses rather than overwrites, and the covering test (refuses to overwrite an external auth writer after refresh) already passes. Lock order is claim (machine-wide) then fingerprint lock (per-grant), never the reverse: two processes holding different fingerprint locks and then reaching for the same claim would deadlock. Mutation-checked: dropping the claim wrapper fails exactly the new test (3 pass / 1 fail), restored to 4/0. Closes #2999. * fix(codex): abort contended native-main refresh claims * fix(codex): preserve native-main claim transient contracts * fix(codex): honor websocket abort state for reauth --- src/codex/auth-context.ts | 7 +- src/codex/main-account.ts | 91 +++++++++----- src/codex/native-main-claim.ts | 24 +++- src/server/responses/codex-auth-error.ts | 4 +- src/server/responses/compact.ts | 6 + src/server/responses/core.ts | 6 + tests/codex-main-account-refresh.test.ts | 124 ++++++++++++++++++++ tests/native-main-claim.test.ts | 53 ++++++++- tests/responses-compaction-routing.test.ts | 28 ++++- tests/responses-native-main-refresh.test.ts | 101 +++++++++++++++- 10 files changed, 401 insertions(+), 43 deletions(-) diff --git a/src/codex/auth-context.ts b/src/codex/auth-context.ts index 007ec70955..5351b94e2c 100644 --- a/src/codex/auth-context.ts +++ b/src/codex/auth-context.ts @@ -10,6 +10,7 @@ import { import { isAccountNeedsReauth, markAccountNeedsReauth } from "./account-runtime-state"; import { isCodexAccountPaused } from "./account-pause"; import { ConfigMutationLockError } from "../config"; +import { NativeProfileError } from "./native-profile-types"; import { isCodexAccountUsable } from "./account-usability"; import { reconcileMainCodexAccountRuntimeState } from "./account-lifecycle"; import { @@ -345,6 +346,8 @@ export function shouldMarkAccountNeedsReauthForCodexAuthFailure(cause: unknown): && !(cause instanceof CodexCredentialRefreshStaleError) && !(cause instanceof MainAuthJsonChangedDuringRefreshError) && !(cause instanceof MainAccountTokenRefreshError && cause.reason === "transient") + && !(cause instanceof NativeProfileError && cause.retryable) + && !(cause instanceof DOMException && cause.name === "AbortError") && !(cause instanceof ConfigMutationLockError); } @@ -667,7 +670,7 @@ export async function resolveCodexAuthContext( } catch (cause) { if (probeLeaseId && probeQuotaScope) releaseCodexQuotaScopeProbeLease(accountId, probeQuotaScope, probeLeaseId); else if (probeLeaseId) releaseCodexQuotaProbeLease(accountId, probeLeaseId); - if (shouldMarkAccountNeedsReauthForCodexAuthFailure(cause)) { + if (!options.signal?.aborted && shouldMarkAccountNeedsReauthForCodexAuthFailure(cause)) { markAccountNeedsReauth(accountId, writerGeneration); } throw new CodexAuthContextError(accountId, cause); @@ -712,7 +715,7 @@ export async function resolveCodexAuthContext( } catch (cause) { if (probeLeaseId && probeQuotaScope) releaseCodexQuotaScopeProbeLease(accountId, probeQuotaScope, probeLeaseId); else if (probeLeaseId) releaseCodexQuotaProbeLease(accountId, probeLeaseId); - if (shouldMarkAccountNeedsReauthForCodexAuthFailure(cause)) { + if (!options.signal?.aborted && shouldMarkAccountNeedsReauthForCodexAuthFailure(cause)) { markAccountNeedsReauth(accountId, writerGeneration); } throw new CodexAuthContextError(accountId, cause); diff --git a/src/codex/main-account.ts b/src/codex/main-account.ts index f1307b1aac..cd5cfb50ba 100644 --- a/src/codex/main-account.ts +++ b/src/codex/main-account.ts @@ -19,6 +19,8 @@ import { resolveCodexHomeDir } from "./home"; import { assertNotRealCodexHomeUnderTest } from "../lib/test-home-guard"; import { clearAccountNeedsReauth } from "./account-runtime-state"; import { advanceCodexCredentialMutationEpoch } from "./credential-mutation-epoch"; +import { withNativeMainExclusiveClaim } from "./native-main-claim"; +import { resolveNativeProfileContext } from "./native-profile-store"; export { MAIN_CODEX_ACCOUNT_ID } from "./account-id"; @@ -187,41 +189,66 @@ async function resolveMainAccountToken( : null; } + const refreshTimeout = AbortSignal.timeout(30_000); const signal = dependencies.signal - ? AbortSignal.any([dependencies.signal, AbortSignal.timeout(30_000)]) - : AbortSignal.timeout(30_000); + ? AbortSignal.any([dependencies.signal, refreshTimeout]) + : refreshTimeout; const lockKey = refreshGrantFingerprintForToken(initial.refreshToken); - return withCodexRefreshFileLock(lockKey, signal, async () => { - const locked = readMainAuthJsonCredential(); - if (!locked) throw new MainAuthJsonChangedDuringRefreshError(); - if (!locked.refreshToken - || refreshGrantFingerprintForToken(locked.refreshToken) !== lockKey) { - if (locked.accessToken !== rejectedAccessToken - && mainAccessTokenFresh(locked.accessToken, Date.now(), 0)) { - return { accessToken: locked.accessToken!, chatgptAccountId: locked.chatgptAccountId }; - } - throw new MainAuthJsonChangedDuringRefreshError(); - } - if (locked.accessToken !== rejectedAccessToken - && mainAccessTokenFresh(locked.accessToken, Date.now(), MAIN_TOKEN_REFRESH_SKEW_MS)) { - return { accessToken: locked.accessToken!, chatgptAccountId: locked.chatgptAccountId }; - } - const refresh = dependencies.refreshToken - ?? ((refreshToken: string, options: { signal: AbortSignal }) => refreshChatGPTToken(refreshToken, options)); - let refreshed: OAuthCredentials; - try { - refreshed = await refresh(locked.refreshToken, { signal }); - } catch (cause) { - const message = cause instanceof Error ? cause.message.toLowerCase() : ""; - const reason = /invalid_grant|invalidated|revoked|expired/.test(message) - ? "reauth" as const - : "transient" as const; - throw new MainAccountTokenRefreshError(reason, { cause }); + // Two locks, because they guard two different things that live in two different + // homes. `withCodexRefreshFileLock` is keyed on the grant fingerprint and lives + // under OPENCODEX_HOME; it serializes refreshes of the SAME grant within one + // install. The file being rewritten is `auth.json` under CODEX_HOME, which every + // OpenCodex install on the machine shares no matter what its own home is -- so two + // proxies with distinct OPENCODEX_HOMEs took two unrelated fingerprint locks and + // refreshed the one credential concurrently (#2999). + // + // The outer claim is the CODEX_HOME coordination the other native-main paths + // already use (`.opencodex-native-main.claim.sqlite`), so this needs no new + // primitive and no FFI. Order is claim (machine-wide) then fingerprint lock + // (per-grant), never the reverse: two processes holding different fingerprint + // locks and then reaching for the same claim would deadlock. + try { + return await withNativeMainExclusiveClaim( + resolveNativeProfileContext(), + () => withCodexRefreshFileLock(lockKey, signal, async () => { + const locked = readMainAuthJsonCredential(); + if (!locked) throw new MainAuthJsonChangedDuringRefreshError(); + if (!locked.refreshToken + || refreshGrantFingerprintForToken(locked.refreshToken) !== lockKey) { + if (locked.accessToken !== rejectedAccessToken + && mainAccessTokenFresh(locked.accessToken, Date.now(), 0)) { + return { accessToken: locked.accessToken!, chatgptAccountId: locked.chatgptAccountId }; + } + throw new MainAuthJsonChangedDuringRefreshError(); + } + if (locked.accessToken !== rejectedAccessToken + && mainAccessTokenFresh(locked.accessToken, Date.now(), MAIN_TOKEN_REFRESH_SKEW_MS)) { + return { accessToken: locked.accessToken!, chatgptAccountId: locked.chatgptAccountId }; + } + const refresh = dependencies.refreshToken + ?? ((refreshToken: string, options: { signal: AbortSignal }) => refreshChatGPTToken(refreshToken, options)); + let refreshed: OAuthCredentials; + try { + refreshed = await refresh(locked.refreshToken, { signal }); + } catch (cause) { + const message = cause instanceof Error ? cause.message.toLowerCase() : ""; + const reason = /invalid_grant|invalidated|revoked|expired/.test(message) + ? "reauth" as const + : "transient" as const; + throw new MainAccountTokenRefreshError(reason, { cause }); + } + const result = persistRefreshedMainAuthJson(locked, refreshed); + clearAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID); + return result; + }), + { waitMs: 30_000, signal }, + ); + } catch (cause) { + if (refreshTimeout.aborted && !dependencies.signal?.aborted) { + throw new MainAccountTokenRefreshError("transient", { cause }); } - const result = persistRefreshedMainAuthJson(locked, refreshed); - clearAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID); - return result; - }); + throw cause; + } } /** Refresh the CLI-owned native credential before upstream I/O and publish it atomically. */ diff --git a/src/codex/native-main-claim.ts b/src/codex/native-main-claim.ts index 2cbbe45ae1..9b523f1038 100644 --- a/src/codex/native-main-claim.ts +++ b/src/codex/native-main-claim.ts @@ -17,6 +17,7 @@ export const NATIVE_MAIN_CLAIM_DB = ".opencodex-native-main.claim.sqlite"; export interface NativeMainClaimOptions { waitMs?: number; pollMs?: number; + signal?: AbortSignal; hardenPath?: (path: string) => Promise; platform?: NodeJS.Platform; env?: NodeJS.ProcessEnv; @@ -121,6 +122,23 @@ function releaseClaim(database: Database | undefined, file: StableLockFile | und try { file?.close(); } catch { /* operation already completed */ } } +function waitForClaimRetry(ms: number, signal?: AbortSignal): Promise { + if (!signal) return Bun.sleep(ms); + if (signal.aborted) return Promise.reject(signal.reason); + return new Promise((resolve, reject) => { + const timer = setTimeout(() => { + signal.removeEventListener("abort", onAbort); + resolve(); + }, ms); + const onAbort = () => { + clearTimeout(timer); + signal.removeEventListener("abort", onAbort); + reject(signal.reason); + }; + signal.addEventListener("abort", onAbort, { once: true }); + }); +} + export async function withNativeMainSharedClaim( context: NativeProfileContext, operation: () => Promise, @@ -151,9 +169,11 @@ export async function withNativeMainExclusiveClaim( operation: () => Promise, options: NativeMainClaimOptions = {}, ): Promise { + const signal = options.signal; const deadline = Date.now() + Math.max(0, options.waitMs ?? 0); const pollMs = Math.max(1, options.pollMs ?? 50); for (;;) { + if (signal?.aborted) throw signal.reason; let database: Database | undefined; let file: StableLockFile | undefined; try { @@ -162,14 +182,16 @@ export async function withNativeMainExclusiveClaim( assertStableLockFile(nativeMainClaimPath(context), file); } catch (error) { releaseClaim(database, file); + if (signal?.aborted) throw signal.reason; const mapped = mapClaimSetupError(error, "Native-main credentials are in use."); if (mapped.code === "NATIVE_MAIN_CLAIM_BUSY" && Date.now() < deadline) { - await Bun.sleep(Math.min(pollMs, Math.max(1, deadline - Date.now()))); + await waitForClaimRetry(Math.min(pollMs, Math.max(1, deadline - Date.now())), signal); continue; } throw mapped; } try { + if (signal?.aborted) throw signal.reason; return await operation(); } finally { releaseClaim(database, file); diff --git a/src/server/responses/codex-auth-error.ts b/src/server/responses/codex-auth-error.ts index 63da982922..8d84c54392 100644 --- a/src/server/responses/codex-auth-error.ts +++ b/src/server/responses/codex-auth-error.ts @@ -15,6 +15,7 @@ import { MainAccountTokenRefreshError, MainAuthJsonChangedDuringRefreshError, } from "../../codex/main-account"; +import { NativeProfileError } from "../../codex/native-profile-types"; export interface CodexAuthContextErrorResponseOptions { accountSelector?: string; @@ -26,7 +27,8 @@ export function nativeMainRefreshFailureResponse(error: unknown): Response { return formatErrorResponse(401, "authentication_error", "Codex main account needs reauthentication"); } if (error instanceof MainAccountTokenRefreshError - || error instanceof MainAuthJsonChangedDuringRefreshError) { + || error instanceof MainAuthJsonChangedDuringRefreshError + || (error instanceof NativeProfileError && error.retryable)) { const response = formatErrorResponse( 503, "server_busy", diff --git a/src/server/responses/compact.ts b/src/server/responses/compact.ts index f0e798828f..073df4c787 100644 --- a/src/server/responses/compact.ts +++ b/src/server/responses/compact.ts @@ -267,6 +267,9 @@ async function refreshNativeMainCompactContext(args: { } return { ok: true, authCtx: refreshedAuthCtx, provider: refreshedProvider, headers }; } catch (error) { + if (req.signal.aborted) { + return { ok: false, response: formatErrorResponse(499, "client_cancelled", "Client cancelled compact request") }; + } return { ok: false, response: nativeMainRefreshFailureResponse(error) }; } } @@ -617,6 +620,9 @@ export async function handleResponsesCompact( } } } catch (err) { + if (req.signal.aborted) { + return formatErrorResponse(499, "client_cancelled", "Client cancelled compact request"); + } const response = mapCodexAuthContextErrorToResponse(err, { accountSelector: route.codexAccountNamespace, now: Date.now(), diff --git a/src/server/responses/core.ts b/src/server/responses/core.ts index 2033bf079b..0240f49553 100644 --- a/src/server/responses/core.ts +++ b/src/server/responses/core.ts @@ -1817,6 +1817,9 @@ async function resolveResponsesCodexAuth( substituteMainCredential, }; } catch (err) { + if (options.abortSignal?.aborted || req.signal.aborted) { + return { ok: false, response: clientCancelledResponse() }; + } if (err instanceof CodexAuthContextError) { const safeAccountLabel = route.codexAccountNamespace ? `${route.providerName}-${route.codexAccountNamespace}` @@ -1962,6 +1965,9 @@ async function refreshNativeMainForwardAuth(args: { }); return { ok: true, authCtx: refreshedAuthCtx, provider, headers }; } catch (error) { + if (options.abortSignal?.aborted || req.signal.aborted) { + return { ok: false, response: clientCancelledResponse() }; + } return { ok: false, response: nativeMainRefreshFailureResponse(error) }; } } diff --git a/tests/codex-main-account-refresh.test.ts b/tests/codex-main-account-refresh.test.ts index b0910c823b..08ff5c05bc 100644 --- a/tests/codex-main-account-refresh.test.ts +++ b/tests/codex-main-account-refresh.test.ts @@ -122,4 +122,128 @@ describe("native main token refresh", () => { expect(readFileSync(authPath)).toEqual(original); expect(readdirSync(home).filter(name => name.includes(".tmp"))).toEqual([]); }); + + /** + * #2999: the refresh lock is keyed on the grant fingerprint and lives under + * OPENCODEX_HOME, but the file it protects is `auth.json` under CODEX_HOME, which + * every install on the machine shares. Two proxies with different OPENCODEX_HOMEs + * therefore took two unrelated locks and refreshed the one credential at once, so + * the loser's rotated grant was published over the winner's and then rejected by + * the provider. + * + * The claim this now takes lives in CODEX_HOME, so it is the same lock for both. + * Driven through the real `getValidMainAccountToken` with OPENCODEX_HOME actually + * swapped between the two calls: asserting on the claim primitive directly would + * pass even if `main-account.ts` never took it. + */ + test("two OPENCODEX_HOMEs serialize on the one CODEX_HOME credential", async () => { + const authPath = join(home, "auth.json"); + writeFileSync(authPath, JSON.stringify({ + tokens: { access_token: expiredJwt(), refresh_token: "old-refresh", account_id: "account-main" }, + })); + + const homeA = mkdtempSync(join(tmpdir(), "ocx-home-a-")); + const homeB = mkdtempSync(join(tmpdir(), "ocx-home-b-")); + const previousOcxHome = process.env.OPENCODEX_HOME; + // The first refresh records its entry and exit. A concurrent second refresh would + // add enter:b before the first release; the serialized follower instead rereads + // fresh credentials and does not refresh the now-rotated grant itself. + const order: string[] = []; + let release: (() => void) | undefined; + const firstEntered = Promise.withResolvers(); + + const refreshFor = (label: string, gate: boolean) => async () => { + order.push(`enter:${label}`); + if (gate) { + firstEntered.resolve(); + await new Promise(resolve => { release = resolve; }); + } + order.push(`leave:${label}`); + return { + access: `fresh-${label}`, + refresh: `rotated-${label}`, + expires: Date.now() + 3_600_000, + accountId: "account-main", + }; + }; + + try { + process.env.OPENCODEX_HOME = homeA; + const first = getValidMainAccountToken({ refreshToken: refreshFor("a", true) }); + await firstEntered.promise; + + // Second install, different OPENCODEX_HOME, same CODEX_HOME. Before the fix + // this entered immediately; now it waits on the shared claim. + process.env.OPENCODEX_HOME = homeB; + const second = getValidMainAccountToken({ refreshToken: refreshFor("b", false) }); + expect(order).toEqual(["enter:a"]); + + release?.(); + await first; + await second; + expect(order).toEqual(["enter:a", "leave:a"]); + } finally { + release?.(); + if (previousOcxHome === undefined) delete process.env.OPENCODEX_HOME; + else process.env.OPENCODEX_HOME = previousOcxHome; + rmSync(homeA, { recursive: true, force: true }); + rmSync(homeB, { recursive: true, force: true }); + } + }); + + test("aborts a contended native-main refresh before it retries", async () => { + const authPath = join(home, "auth.json"); + writeFileSync(authPath, JSON.stringify({ + tokens: { access_token: expiredJwt(), refresh_token: "old-refresh", account_id: "account-main" }, + })); + + const homeA = mkdtempSync(join(tmpdir(), "ocx-home-a-")); + const homeB = mkdtempSync(join(tmpdir(), "ocx-home-b-")); + const previousOcxHome = process.env.OPENCODEX_HOME; + const firstEntered = Promise.withResolvers(); + const releaseFirst = Promise.withResolvers(); + const abort = new AbortController(); + const abortReason = new Error("refresh cancelled while native-main claim was busy"); + let secondRefreshStarted = false; + + try { + process.env.OPENCODEX_HOME = homeA; + const first = getValidMainAccountToken({ + refreshToken: async () => { + firstEntered.resolve(); + await releaseFirst.promise; + return { + access: "fresh-a", + refresh: "rotated-a", + expires: Date.now() + 3_600_000, + accountId: "account-main", + }; + }, + }); + await firstEntered.promise; + + // The first refresh holds the CODEX_HOME claim. Cancellation must release the + // second caller from that wait instead of letting it refresh after the holder exits. + process.env.OPENCODEX_HOME = homeB; + const second = getValidMainAccountToken({ + signal: abort.signal, + refreshToken: async () => { + secondRefreshStarted = true; + throw new Error("must not refresh after cancellation"); + }, + }); + abort.abort(abortReason); + releaseFirst.resolve(); + + await expect(second).rejects.toBe(abortReason); + await expect(first).resolves.toEqual({ accessToken: "fresh-a", chatgptAccountId: "account-main" }); + expect(secondRefreshStarted).toBe(false); + } finally { + releaseFirst.resolve(); + if (previousOcxHome === undefined) delete process.env.OPENCODEX_HOME; + else process.env.OPENCODEX_HOME = previousOcxHome; + rmSync(homeA, { recursive: true, force: true }); + rmSync(homeB, { recursive: true, force: true }); + } + }); }); diff --git a/tests/native-main-claim.test.ts b/tests/native-main-claim.test.ts index 2cd01c3872..9b54924f26 100644 --- a/tests/native-main-claim.test.ts +++ b/tests/native-main-claim.test.ts @@ -1,4 +1,4 @@ -import { afterEach, describe, expect, test } from "bun:test"; +import { afterEach, describe, expect, spyOn, test } from "bun:test"; import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; @@ -35,11 +35,12 @@ describe("native-main shared and exclusive claims", () => { const context = fixture(); const entered = deferred(); const release = deferred(); + let firstSettled = false; const first = withNativeMainSharedClaim(context, async () => { entered.resolve(); await release.promise; return "first"; - }, { hardenPath: noHardening }); + }, { hardenPath: noHardening }).finally(() => { firstSettled = true; }); await entered.promise; await expect(withNativeMainSharedClaim( @@ -47,14 +48,60 @@ describe("native-main shared and exclusive claims", () => { async () => "second", { hardenPath: noHardening }, )).resolves.toBe("second"); + let contenderRuns = 0; await expect(withNativeMainExclusiveClaim( context, - async () => "must-not-run", + async () => { + contenderRuns += 1; + return "must-not-run"; + }, { waitMs: 0, hardenPath: noHardening }, )).rejects.toMatchObject({ code: "NATIVE_MAIN_CLAIM_BUSY", retryable: true }); + expect(contenderRuns).toBe(0); + expect(firstSettled).toBe(false); release.resolve(); await expect(first).resolves.toBe("first"); + await expect(withNativeMainExclusiveClaim( + context, + async () => "exclusive-after-release", + { waitMs: 0, hardenPath: noHardening }, + )).resolves.toBe("exclusive-after-release"); + }); + + test("a cancelled exclusive claimant removes its retry listener without releasing the holder", async () => { + const context = fixture(); + const entered = deferred(); + const release = deferred(); + const holder = withNativeMainSharedClaim(context, async () => { + entered.resolve(); + await release.promise; + }, { hardenPath: noHardening }); + await entered.promise; + + const controller = new AbortController(); + const addListener = spyOn(controller.signal, "addEventListener"); + const removeListener = spyOn(controller.signal, "removeEventListener"); + const cancellation = new Error("client cancelled claim wait"); + const contender = withNativeMainExclusiveClaim( + context, + async () => "must-not-run", + { waitMs: 2_000, pollMs: 10, signal: controller.signal, hardenPath: noHardening }, + ); + while (!addListener.mock.calls.some(([type]) => type === "abort")) await Promise.resolve(); + expect(addListener).toHaveBeenCalledWith("abort", expect.any(Function), { once: true }); + + controller.abort(cancellation); + await expect(contender).rejects.toBe(cancellation); + expect(removeListener).toHaveBeenCalledWith("abort", expect.any(Function)); + + await expect(withNativeMainExclusiveClaim( + context, + async () => "still-locked", + { waitMs: 0, hardenPath: noHardening }, + )).rejects.toMatchObject({ code: "NATIVE_MAIN_CLAIM_BUSY" }); + release.resolve(); + await holder; }); test("closing a sibling reader cannot let another process bypass a retained shared claim", async () => { diff --git a/tests/responses-compaction-routing.test.ts b/tests/responses-compaction-routing.test.ts index d97154c661..47fd315aa4 100644 --- a/tests/responses-compaction-routing.test.ts +++ b/tests/responses-compaction-routing.test.ts @@ -19,7 +19,8 @@ import { resolveCodexAccountForThread, } from "../src/codex/routing"; import { clearAccountQuota, updateAccountQuota } from "../src/codex/auth-api"; -import { MAIN_CODEX_ACCOUNT_ID } from "../src/codex/main-account"; +import { MAIN_CODEX_ACCOUNT_ID, MainAccountTokenRefreshError } from "../src/codex/main-account"; +import { NativeProfileError } from "../src/codex/native-profile-types"; import { fallbackCodexAccountLogLabel } from "../src/codex/account-label"; import * as authContextModule from "../src/codex/auth-context"; import { @@ -211,6 +212,31 @@ describe("Codex auth-context error parity (#2392)", () => { status: 401, regularLog: true, }, + { + label: "native-main claim contention", + createError: () => new authContextModule.CodexAuthContextError( + MAIN_CODEX_ACCOUNT_ID, + new NativeProfileError( + "NATIVE_MAIN_CLAIM_BUSY", + "Native-main credentials are in use.", + 503, + true, + ), + ), + status: 503, + retryAfter: "1", + regularLog: true, + }, + { + label: "native-main claim timeout", + createError: () => new authContextModule.CodexAuthContextError( + MAIN_CODEX_ACCOUNT_ID, + new MainAccountTokenRefreshError("transient"), + ), + status: 503, + retryAfter: "1", + regularLog: true, + }, { label: "pool authentication failure", createError: () => new authContextModule.CodexPoolAuthenticationError("Pool credential is unavailable"), diff --git a/tests/responses-native-main-refresh.test.ts b/tests/responses-native-main-refresh.test.ts index ab59bebe1f..94b01c72fd 100644 --- a/tests/responses-native-main-refresh.test.ts +++ b/tests/responses-native-main-refresh.test.ts @@ -1,10 +1,13 @@ -import { afterEach, beforeEach, describe, expect, test } from "bun:test"; +import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test"; import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { clearAccountNeedsReauth } from "../src/codex/auth-api"; import { saveCodexAccountCredential } from "../src/codex/account-store"; -import { MAIN_CODEX_ACCOUNT_ID } from "../src/codex/main-account"; +import { isAccountNeedsReauth } from "../src/codex/account-runtime-state"; +import { getValidMainAccountToken, MAIN_CODEX_ACCOUNT_ID } from "../src/codex/main-account"; +import { withNativeMainSharedClaim } from "../src/codex/native-main-claim"; +import type { NativeProfileContext } from "../src/codex/native-profile-store"; import { clearCodexUpstreamHealth, clearThreadAccountMap } from "../src/codex/routing"; import { handleResponses, handleResponsesCompact } from "../src/server/responses"; import type { RequestLogContext } from "../src/server/request-log"; @@ -34,13 +37,14 @@ function config(options: { secondAccount?: boolean } = {}): OcxConfig { } as OcxConfig; } -function request(path: "/v1/responses" | "/v1/responses/compact"): Request { +function request(path: "/v1/responses" | "/v1/responses/compact", signal?: AbortSignal): Request { return new Request(`http://localhost${path}`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(path.endsWith("compact") ? { model: "gpt-5.5", input: [] } : { model: "gpt-5.5", input: "hello", stream: false }), + signal, }); } @@ -137,6 +141,42 @@ describe("native main 401 refresh and replay", () => { expect(sends).toEqual(["Bearer refreshed-access"]); }); + test("converts an outer native-main claim timeout into a transient refresh failure", async () => { + writeFileSync(join(home, "auth.json"), JSON.stringify({ + tokens: { refresh_token: "refresh-grant", account_id: "account-main" }, + })); + let releaseHolder!: () => void; + const holderRelease = new Promise(resolve => { releaseHolder = resolve; }); + let holderEntered!: () => void; + const holderReady = new Promise(resolve => { holderEntered = resolve; }); + const holder = withNativeMainSharedClaim( + { codexHome: home } as NativeProfileContext, + async () => { + holderEntered(); + await holderRelease; + }, + { hardenPath: async () => {} }, + ); + await holderReady; + + const timeout = new AbortController(); + const addListener = spyOn(timeout.signal, "addEventListener"); + const timeoutSpy = spyOn(AbortSignal, "timeout").mockReturnValue(timeout.signal); + try { + const pending = getValidMainAccountToken(); + while (!addListener.mock.calls.some(([type]) => type === "abort")) await Promise.resolve(); + timeout.abort(new DOMException("claim timed out", "TimeoutError")); + await expect(pending).rejects.toMatchObject({ + name: "MainAccountTokenRefreshError", + reason: "transient", + }); + } finally { + timeoutSpy.mockRestore(); + releaseHolder(); + await holder; + } + }); + test("Responses refreshes and performs exactly one physical replay", async () => { const harness = install401ThenRefreshHarness(); const response = await handleResponses( @@ -212,4 +252,59 @@ describe("native main 401 refresh and replay", () => { expect(refreshes).toEqual(["refresh-grant"]); }); } + + test.each(["/v1/responses", "/v1/responses/compact"] as const)( + "%s keeps the WebSocket string-abort claim cancellation as 499 without quarantining main", + async path => { + writeFileSync(join(home, "auth.json"), JSON.stringify({ + tokens: { refresh_token: "refresh-grant", account_id: "account-main" }, + })); + let releaseHolder!: () => void; + const holderRelease = new Promise(resolve => { releaseHolder = resolve; }); + let holderEntered!: () => void; + const holderReady = new Promise(resolve => { holderEntered = resolve; }); + const holder = withNativeMainSharedClaim( + { codexHome: home } as NativeProfileContext, + async () => { + holderEntered(); + await holderRelease; + }, + { hardenPath: async () => {} }, + ); + await holderReady; + + const controller = new AbortController(); + const originalAny = AbortSignal.any; + let claimWaitListener: ReturnType | undefined; + const anySpy = spyOn(AbortSignal, "any").mockImplementation(signals => { + const combined = originalAny.call(AbortSignal, signals); + claimWaitListener = spyOn(combined, "addEventListener"); + return combined; + }); + try { + const pending = path === "/v1/responses" + ? handleResponses( + request(path, controller.signal), + config(), + { model: "", provider: "" } as RequestLogContext, + { abortSignal: controller.signal, inboundTransport: "websocket" }, + ) + : handleResponsesCompact( + request(path, controller.signal), + config(), + { model: "", provider: "" } as RequestLogContext, + ); + while (!claimWaitListener?.mock.calls.some(([type]) => type === "abort")) await Promise.resolve(); + controller.abort("websocket turn superseded or closed"); + + const response = await pending; + expect(response.status).toBe(499); + expect(isAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID)).toBe(false); + } finally { + anySpy.mockRestore(); + releaseHolder(); + await holder; + } + }, + ); }); From afd5b4630dc59f891c4497174dd21b53ed24b400 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 02:23:36 +0900 Subject: [PATCH 117/172] fix(compact): route combo compact requests through the failover path (rebase of #3109) (#3184) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(compact): route combo compact requests through failover path When a compact request resolved through a combo, the native-compact fast path sent the request directly to the picked provider without failover. A 429 or 5xx from that target surfaced as an exhausted-retry error to the client instead of advancing to the next combo target. Skip native compact when route.combo is set so the request falls through to the synthetic compaction path, which dispatches through handleResponses → handleComboResponses with full combo failover (cooldown + advanceToNext). (cherry picked from commit df4c54423e23f220cdde69988675e206ff8e5401) * test(compact): cover combo failover and streaming (cherry picked from commit 78855ed06b0d9d8540db1379433c91c3f63b2f02) * preserve opaque compaction ciphertext (cherry picked from commit 9582fc35f209e99544bb5774f26d8d4ba6b4730b) * fix: preserve native compaction completion ownership * fix(compact): reject empty native ciphertext --------- Co-authored-by: x3M3x --- src/adapters/openai-responses.ts | 37 ++++- src/bridge.ts | 14 +- src/server/responses/compact.ts | 20 ++- src/types/request.ts | 2 + tests/responses-compaction.test.ts | 65 +++++++++ tests/server-combo-failover-e2e.test.ts | 185 +++++++++++++++++++++++- 6 files changed, 307 insertions(+), 16 deletions(-) diff --git a/src/adapters/openai-responses.ts b/src/adapters/openai-responses.ts index 0d91807617..0209bc63c3 100644 --- a/src/adapters/openai-responses.ts +++ b/src/adapters/openai-responses.ts @@ -2286,6 +2286,7 @@ export function createResponsesPassthroughAdapter(provider: OcxProviderConfig): let doneText = ""; let snapshot = ""; let usage: OcxUsage | undefined; + let compactionEncryptedContent: string | undefined; for await (const event of decodeServerSentEvents(response.body, { translatorBudget: budget })) { let payload: unknown; try { payload = JSON.parse(event.data); } catch { continue; } @@ -2320,6 +2321,17 @@ export function createResponsesPassthroughAdapter(provider: OcxProviderConfig): return; case "response.completed": { + const responsePayload = isPlainObject(payload.response) ? payload.response : undefined; + const output = Array.isArray(responsePayload?.output) ? responsePayload.output : []; + const compaction = output.find(item => isPlainObject(item) && item.type === "compaction"); + if (isPlainObject(compaction) && typeof compaction.encrypted_content === "string") { + const nextEncryptedContent = compaction.encrypted_content; + const previousBytes = budgetEncoder.encode(compactionEncryptedContent ?? "").byteLength; + const reservation = budget.reserveTransient(budgetEncoder.encode(nextEncryptedContent).byteLength, { kind: "retained_collectors" }); + compactionEncryptedContent = nextEncryptedContent; + reservation.commitRetained(); + budget.releaseRetained(previousBytes, { kind: "retained_collectors" }); + } const next = responsesPayloadText(payload.response); const previousBytes = budgetEncoder.encode(snapshot).byteLength; const reservation = budget.reserveTransient(budgetEncoder.encode(next).byteLength, { kind: "retained_collectors" }); @@ -2336,7 +2348,11 @@ export function createResponsesPassthroughAdapter(provider: OcxProviderConfig): const text = snapshot || doneText || deltas; if (text) yield { type: "text_delta", text }; budget.releaseRetained(budgetEncoder.encode(deltas).byteLength + budgetEncoder.encode(doneText).byteLength + budgetEncoder.encode(snapshot).byteLength, { kind: "retained_collectors" }); - yield { type: "done", ...(usage ? { usage } : {}) }; + yield { + type: "done", + ...(usage ? { usage } : {}), + ...(compactionEncryptedContent ? { compactionEncryptedContent } : {}), + }; }, async parseResponse(response: Response, budget: TranslatorBudget): Promise { @@ -2354,14 +2370,23 @@ export function createResponsesPassthroughAdapter(provider: OcxProviderConfig): if (payload.status === "incomplete") { return [{ type: "incomplete", reason: responsesErrorMessage(payload) }]; } + const usage = usageFromResponsesPayload(payload); + const output = Array.isArray(payload.output) ? payload.output : []; + const compaction = output.find(item => isPlainObject(item) && item.type === "compaction"); + const compactionEncryptedContent = isPlainObject(compaction) && typeof compaction.encrypted_content === "string" + ? compaction.encrypted_content + : undefined; const text = responsesPayloadText(payload); - if (!text) { - // A completed turn with no usable text cannot become a summary; saying so is - // better than installing an empty compaction as replacement history. + if (!text && !compactionEncryptedContent) { + // A completed turn with neither text nor a native compaction blob cannot become a + // replacement-history item. A ciphertext-only native completion is valid, though. return [{ type: "error", message: "upstream compaction returned no summary text" }]; } - const usage = usageFromResponsesPayload(payload); - return [{ type: "text_delta", text }, { type: "done", ...(usage ? { usage } : {}) }]; + return [...(text ? [{ type: "text_delta" as const, text }] : []), { + type: "done", + ...(usage ? { usage } : {}), + ...(compactionEncryptedContent ? { compactionEncryptedContent } : {}), + }]; }, }; } diff --git a/src/bridge.ts b/src/bridge.ts index 145913ddab..c1666cb84b 100644 --- a/src/bridge.ts +++ b/src/bridge.ts @@ -1223,10 +1223,12 @@ export function bridgeToResponsesSSE( // Exactly one compaction item per turn; codex-rs takes the first and fatals on 0. const item = { type: "compaction", id: `cmp_${uuid()}`, - encrypted_content: encodeCompactionSummary(compactionText), + encrypted_content: event.compactionEncryptedContent ?? encodeCompactionSummary(compactionText), }; emit("response.output_item.done", { output_index: outputIndex, item }); - retainFinishedItem(item as OutputItem, compactionTextBytes); + retainFinishedItem(item as OutputItem, event.compactionEncryptedContent + ? bytesOf(event.compactionEncryptedContent) + : compactionTextBytes); outputIndex++; } // Recognize every adapter's truncation vocabulary, not just the canonical pair. @@ -1574,6 +1576,7 @@ function buildResponseJSONWithBudget( let sawTerminal = false; let compactionText = ""; let compactionTextBytes = 0; + let compactionEncryptedContent: string | undefined; let currentText = ""; let currentTextBytes = 0; @@ -1915,6 +1918,7 @@ function buildResponseJSONWithBudget( break; case "done": usage = e.usage; + compactionEncryptedContent = e.compactionEncryptedContent; sawTerminal = true; endTurn = e.endTurn; cleanDone = e.stopReason === undefined; @@ -1967,7 +1971,11 @@ function buildResponseJSONWithBudget( && sawTerminal && !isTruncatedStopReason(rawStopReason) ) { - pushOutput({ type: "compaction", id: `cmp_${uuid()}`, encrypted_content: encodeCompactionSummary(compactionText) }, compactionTextBytes); + const item = { + type: "compaction", id: `cmp_${uuid()}`, + encrypted_content: compactionEncryptedContent ?? encodeCompactionSummary(compactionText), + }; + pushOutput(item, compactionEncryptedContent ? bytesOf(compactionEncryptedContent) : compactionTextBytes); } const failure = errorEvent ? adapterFailureFromEvent(errorEvent) : undefined; diff --git a/src/server/responses/compact.ts b/src/server/responses/compact.ts index 073df4c787..dd9bd47d63 100644 --- a/src/server/responses/compact.ts +++ b/src/server/responses/compact.ts @@ -562,7 +562,10 @@ export async function handleResponsesCompact( // Native /responses/compact exists on the canonical ChatGPT backend and on the // official OpenAI API. Any other Responses-shaped gateway must take the routed // summarizer path below, or compaction fails against an endpoint it never had (#422). - if (supportsNativeResponsesCompactEndpoint(route.providerName, route.provider) && !accountGatedCompactWireModel) { + // Combo-resolved targets skip native compact so failover can advance through the + // combo target list when the picked model returns 429/5xx — the routed path below + // dispatches through handleResponses → handleComboResponses with full failover. + if (supportsNativeResponsesCompactEndpoint(route.providerName, route.provider) && !accountGatedCompactWireModel && !route.combo) { if (req.signal.aborted) { return formatErrorResponse(499, "client_cancelled", "Client cancelled compact request"); } @@ -1003,8 +1006,10 @@ export async function handleResponsesCompact( ...raw, // Canonical ChatGPT Responses rejects non-streaming turns. Daybreak cannot use the // native compact endpoint either, so run its synthetic compaction as SSE and collapse - // the completed event back into the v1 compact JSON contract below. - stream: accountGatedCompactWireModel ? true : false, + // the completed event back into the v1 compact JSON contract below. Combo-dispatched + // turns also go out as SSE: failover can land on a canonical child that rejects a + // non-streaming turn, and every combo-capable provider already serves streaming traffic. + stream: accountGatedCompactWireModel || route.combo ? true : false, input: [...inputItems, { type: "compaction_trigger" }], }; const internalHeaders = new Headers({ "content-type": "application/json" }); @@ -1078,9 +1083,12 @@ export async function handleResponsesCompact( `compaction turn produced ${compactionItems.length} compaction items, expected exactly 1`, ); } - // The canonical Responses stream returns a real OpenAI-encrypted compaction item. OCX cannot - // and should not decrypt it; /responses/compact callers can consume that item directly. - if (accountGatedCompactWireModel) { + // Native Responses backends return a real opaque OpenAI-encrypted compaction item. OCX cannot + // and should not decrypt it; preserve that item for /responses/compact callers. Synthetic + // routed summaries are our `ocx1:` envelope and must be decoded into v1 history items. + if (typeof compactionItems[0]!.encrypted_content === "string" + && compactionItems[0]!.encrypted_content.trim().length > 0 + && !compactionItems[0]!.encrypted_content.startsWith("ocx1:")) { const result = new Response(JSON.stringify({ output: compactionItems }), { headers: { "Content-Type": "application/json" }, }); diff --git a/src/types/request.ts b/src/types/request.ts index d78c25b416..25c4a26ef4 100644 --- a/src/types/request.ts +++ b/src/types/request.ts @@ -333,6 +333,8 @@ export type AdapterEvent = | { type: "done"; usage?: OcxUsage; + /** Native opaque compaction ciphertext returned by a Responses backend. */ + compactionEncryptedContent?: string; stopReason?: string; endTurn?: boolean; providerState?: OcxProviderContinuationState; diff --git a/tests/responses-compaction.test.ts b/tests/responses-compaction.test.ts index 5cc02d2e56..1f1ea9b25f 100644 --- a/tests/responses-compaction.test.ts +++ b/tests/responses-compaction.test.ts @@ -1,6 +1,7 @@ import { describe, expect, test } from "bun:test"; import { bridgeToResponsesSSE, buildResponseJSON } from "../src/bridge"; import { createResponsesPassthroughAdapter as createResponsesPassthroughAdapterProduction } from "../src/adapters/openai-responses"; +import { createTranslatorBudget } from "../src/lib/translator-budget"; import { CODEX_FORWARD_BASE_URL } from "../src/providers/openai-tiers"; import { parseRequest } from "../src/responses/parser"; import { @@ -165,6 +166,70 @@ describe("buildResponseJSON compaction mode", () => { }); }); +describe("native Responses compaction passthrough", () => { + const provider = { + adapter: "openai-responses", + baseUrl: "https://responses.example/v1", + authMode: "key" as const, + apiKey: "test-key", + }; + + test("buffered ciphertext-only completion yields done without a text delta", async () => { + const adapter = createResponsesPassthroughAdapterProduction(provider); + const encryptedContent = "gAAAAABm-native-buffered-ciphertext"; + const budget = createTranslatorBudget(); + try { + const events = await adapter.parseResponse!(Response.json({ + status: "completed", + output: [{ type: "compaction", encrypted_content: encryptedContent }], + }), budget); + + expect(events).toEqual([{ type: "done", compactionEncryptedContent: encryptedContent }]); + } finally { + budget.dispose(); + } + }); + + test("streaming ciphertext is charged before the compaction item takes ownership", async () => { + const adapter = createResponsesPassthroughAdapterProduction(provider); + const encryptedContent = "gAAAAABm-native-streaming-ciphertext"; + const budget = createTranslatorBudget(); + try { + const events: AdapterEvent[] = []; + const stream = [ + "event: response.completed", + `data: ${JSON.stringify({ + type: "response.completed", + response: { + status: "completed", + output: [{ type: "compaction", encrypted_content: encryptedContent }], + }, + })}`, + "", + "", + ].join("\n"); + for await (const event of adapter.parseStream(new Response(stream, { + headers: { "content-type": "text/event-stream" }, + }), budget)) events.push(event); + + expect(events).toEqual([{ type: "done", compactionEncryptedContent: encryptedContent }]); + expect(budget.snapshot().currentBytes).toBe(Buffer.byteLength(encryptedContent)); + + const json = buildResponseJSON(events, "test/model", { + compaction: true, + translatorBudget: budget, + }) as { output: Array<{ type: string; encrypted_content?: string }> }; + expect(json.output).toEqual([expect.objectContaining({ + type: "compaction", + encrypted_content: encryptedContent, + })]); + expect(budget.snapshot().currentBytes).toBe(Buffer.byteLength(JSON.stringify(json.output[0]))); + } finally { + budget.dispose(); + } + }); +}); + describe("COMPACT_PROMPT", () => { test("mirrors the codex-rs checkpoint instruction", () => { expect(COMPACT_PROMPT).toContain("CONTEXT CHECKPOINT COMPACTION"); diff --git a/tests/server-combo-failover-e2e.test.ts b/tests/server-combo-failover-e2e.test.ts index e8bf387c94..0ce1063c79 100644 --- a/tests/server-combo-failover-e2e.test.ts +++ b/tests/server-combo-failover-e2e.test.ts @@ -36,6 +36,7 @@ import { responseStatePersistPendingForTests, } from "../src/responses/state"; import { clearCursorThreadContinuityForTests } from "../src/adapters/cursor/thread-continuity"; +import { COMPACT_PROMPT, encodeCompactionSummary } from "../src/responses/compaction"; // Full-suite Windows load: startServer + combo rename/delete management flows exceed the // default 5s per-test budget (same flake class as 810fa115 / claude-management-api). @@ -112,6 +113,7 @@ mock.module("../src/lib/upstream-retry", () => ({ })); const { handleResponses } = await import("../src/server/responses"); +const { handleResponsesCompact } = await import("../src/server/responses/compact"); type HandleOptions = NonNullable[3]>; const TOKEN_ENDPOINT = "https://auth.x.ai/oauth/token"; @@ -2305,7 +2307,7 @@ describe("server combo failover 030 activation matrix", () => { let backupHits = 0; const auth: string[] = []; globalThis.fetch = (async (input, init) => { - const url = input instanceof Request ? input.url : String(input); + const url = typeof input === "object" && input !== null && "url" in input ? String((input as Request).url) : String(input); if (url === XAI_OAUTH_DISCOVERY_URL) { return Response.json({ authorization_endpoint: "https://auth.x.ai/oauth/authorize", token_endpoint: TOKEN_ENDPOINT }); } @@ -2952,3 +2954,184 @@ describe("cursor conversation continuity across store:false chains", () => { expect(seen[1]).toBe(seen[0]); }); }); + +describe("combo compact failover", () => { + function compactRequest(body: Record): Request { + return new Request("http://localhost/v1/responses", { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify(body), + }); + } + + async function postCompactLogged(config: OcxConfig): Promise { + const logCtx: RequestLogContext = { model: "", provider: "" }; + const start = Date.now(); + const response = await handleResponsesCompact(compactRequest({ + model: "combo/free", + stream: false, + input: [{ type: "message", role: "user", content: [{ type: "input_text", text: "earlier turn" }] }], + }), config, logCtx); + loggedRequestSequence += 1; + return responseWithDeferredRequestLog(response, `combo-compact-${loggedRequestSequence}`, start, logCtx); + } + + function canonicalPoolConfig( + targets: Array<{ provider: string; model: string }>, + backupUrl?: string, + ): { config: OcxConfig } { + const config = comboConfig({ + "openai-apikey": { + adapter: "openai-responses", + baseUrl: "https://api.openai.com/v1", + authMode: "key", + apiKey: "combo-compact-key", + }, + backup: provider("openai-chat", backupUrl ?? "http://127.0.0.1:9", "key-b"), + }, targets); + return { config }; + } + + test("native-capable first target 429 hops compact to the backup target", async () => { + const childBodies: Array> = []; + const b = serve(async request => { + childBodies.push(JSON.parse(await request.text()) as Record); + return chatStream("compact backup"); + }); + const { config } = canonicalPoolConfig([ + { provider: "openai-apikey", model: "gpt-5.4" }, + { provider: "backup", model: "m1" }, + ], baseUrl(b)); + globalThis.fetch = (async (input: unknown, init?: RequestInit) => { + const url = typeof input === "object" && input !== null && "url" in input ? String((input as Request).url) : String(input); + if (url.includes("api.openai.com")) { + return Response.json({ error: { message: "rate limited" } }, { status: 429 }); + } + return originalFetch(input as RequestInfo, init); + }) as typeof fetch; + + const response = await postCompactLogged(config); + expect(response.status).toBe(200); + const json = await response.json() as { output?: unknown[] }; + expect(JSON.stringify(json.output)).toContain("compact backup"); + + // The backup child received the synthetic summarizer turn as SSE, with the + // summarizer prompt present in its chat wire body. + expect(childBodies).toHaveLength(1); + expect(childBodies[0]!.stream).toBe(true); + expect(JSON.stringify(childBodies[0]!.messages)).toContain("CONTEXT CHECKPOINT COMPACTION"); + + const { log } = await latestAttemptReceipts(config); + const attempts = log.attempts as Array>; + expect(attempts).toHaveLength(2); + expect(attempts[0]).toMatchObject({ + provider: "openai-apikey", + adapter: "openai-responses", + status: 429, + }); + expect(attempts[1]).toMatchObject({ provider: "backup", adapter: "openai-chat", status: 200 }); + }); + + test("account-gated first target failover decodes the backup ocx1 compaction", async () => { + const b = serve(() => chatStream("mixed combo backup summary")); + const { config } = canonicalPoolConfig([ + { provider: "openai-apikey", model: "gpt-daybreak-blue-latest" }, + { provider: "backup", model: "m1" }, + ], baseUrl(b)); + globalThis.fetch = (async (input: unknown, init?: RequestInit) => { + const url = typeof input === "object" && input !== null && "url" in input ? String((input as Request).url) : String(input); + if (url.includes("api.openai.com")) { + return Response.json({ error: { message: "rate limited" } }, { status: 429 }); + } + return originalFetch(input as RequestInfo, init); + }) as typeof fetch; + + const response = await postCompactLogged(config); + expect(response.status).toBe(200); + const json = await response.json() as { output?: unknown[] }; + expect(JSON.stringify(json.output)).toContain("mixed combo backup summary"); + expect(JSON.stringify(json.output)).not.toContain("ocx1:"); + }); + + test("combo compact runs the synthetic turn as SSE so a canonical child can serve it", async () => { + const bodies: Array> = []; + const { config } = canonicalPoolConfig([{ provider: "openai-apikey", model: "gpt-5.4" }]); + globalThis.fetch = (async (input: unknown, init?: RequestInit) => { + const url = typeof input === "object" && input !== null && "url" in input + ? String((input as Request).url) + : String(input); + if (!url.includes("api.openai.com")) { + return originalFetch(input as RequestInfo, init); + } + // Only the codex/responses child turn is under test; side probes (e.g. the + // wham/usage quota check) just get a tolerated non-2xx. + if (!url.includes("api.openai.com/v1/responses")) { + return Response.json({ error: { message: "probe not under test" } }, { status: 403 }); + } + const body = JSON.parse(String(init?.body ?? "{}")) as Record; + bodies.push(body); + // Canonical ChatGPT Responses rejects non-streaming turns; a stream:false child + // request would strand every canonical-only combo here before the SSE coercion. + if (body.stream !== true) { + return Response.json({ error: { message: "non-streaming turns are rejected" } }, { status: 400 }); + } + const completed = { + type: "response.completed", + response: { + id: "resp_compact", + status: "completed", + output: [{ type: "compaction", encrypted_content: "gAAAAABm-native-openai-ciphertext" }], + }, + }; + return new Response([ + "event: response.created", + 'data: {"type":"response.created","response":{"id":"resp_compact","status":"in_progress"}}', + "", + `event: ${completed.type}`, + `data: ${JSON.stringify(completed)}`, + "", + "", + ].join("\n"), { headers: { "content-type": "text/event-stream" } }); + }) as typeof fetch; + + const response = await postCompactLogged(config); + expect(response.status).toBe(200); + const json = await response.json() as { output?: unknown[] }; + expect(json.output).toEqual([expect.objectContaining({ + type: "compaction", encrypted_content: "gAAAAABm-native-openai-ciphertext", + })]); + expect(bodies).toHaveLength(1); + expect(bodies[0]!.stream).toBe(true); + expect(JSON.stringify(bodies[0]!.input)).toContain("CONTEXT CHECKPOINT COMPACTION"); + }); + + test("native compact rejects an empty ciphertext item", async () => { + const { config } = canonicalPoolConfig([{ provider: "openai-apikey", model: "gpt-5.4" }]); + globalThis.fetch = (async (input: unknown, init?: RequestInit) => { + const url = typeof input === "object" && input !== null && "url" in input + ? String((input as Request).url) + : String(input); + if (!url.includes("api.openai.com/v1/responses")) { + return Response.json({ error: { message: "probe not under test" } }, { status: 403 }); + } + const completed = { + type: "response.completed", + response: { + id: "resp_compact_empty", + status: "completed", + output: [{ type: "compaction", encrypted_content: "" }], + }, + }; + return new Response([ + `event: ${completed.type}`, + `data: ${JSON.stringify(completed)}`, + "", + "", + ].join("\n"), { headers: { "content-type": "text/event-stream" } }); + }) as typeof fetch; + + const response = await postCompactLogged(config); + expect(response.status).toBe(502); + expect(await response.text()).toContain("empty summary"); + }); +}); From fe766e129441180c6fefcdc45b9e5609b2e2c326 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 02:25:41 +0900 Subject: [PATCH 118/172] fix(codex): throttle repeated failed pool quota primes (rebase of #3003) (#3185) * fix(codex): throttle repeated failed pool quota primes A pool account whose WHAM lookup fails stores no quota, so it stays "unknown" and every later prime trigger re-selects it as stale and repeats the same failing request. Successful lookups are already bounded by POOL_CACHE_TTL; failures had no backoff at all. Record the last prime attempt per account and give a failed lookup the same TTL window. The record is keyed by credential generation, so a re-authentication, refresh, or account removal retries immediately instead of waiting out a backoff earned by the previous credential. * fix(codex): bind quota prime backoff to admitted probe * fix(codex): prove quota prime dispatch before backoff * fix(codex): prune removed-account prime markers before the eligibility return The prune sat after the provider-eligibility early return, so an account removed while the provider was disabled kept its failed-attempt marker. Restoring the same id inside POOL_CACHE_TTL then read that stale failure as current and skipped the retry the restored credential was entitled to. The existing coverage removed an account with the provider enabled, which is why this survived review. Add the disabled-window case. --------- Co-authored-by: luvs01 Co-authored-by: jun --- src/codex/auth-api.ts | 141 +++++++++-- tests/codex-quota-prime.test.ts | 421 +++++++++++++++++++++++++++++++- 2 files changed, 546 insertions(+), 16 deletions(-) diff --git a/src/codex/auth-api.ts b/src/codex/auth-api.ts index 91734a3229..4f9c5efd28 100644 --- a/src/codex/auth-api.ts +++ b/src/codex/auth-api.ts @@ -843,6 +843,23 @@ interface PoolQuotaResult { /** Present only when this call's WHAM response included `rate_limit_reset_credits.available_count`. */ freshResetCredits?: number; quotaProbeSkipped?: true; + /** Positive evidence captured immediately before an upstream WHAM dispatch. */ + quotaProbeAttempted?: { at: number; credentialGeneration: number }; +} + +interface PoolQuotaProbeEvidence { + attempted?: NonNullable; +} + +function markQuotaProbeAttempted(evidence: PoolQuotaProbeEvidence, credentialGeneration: number): void { + evidence.attempted = { at: Date.now(), credentialGeneration }; +} + +function withQuotaProbeEvidence( + result: PoolQuotaResult, + evidence: PoolQuotaProbeEvidence, +): PoolQuotaResult { + return evidence.attempted ? { ...result, quotaProbeAttempted: evidence.attempted } : result; } interface PoolQuotaRefreshFlight { @@ -988,6 +1005,7 @@ async function recoverPoolQuotaFrom401(ctx: { rejectedAccessToken: string; rejectedGeneration: number; resp: Response; + quotaProbeEvidence: PoolQuotaProbeEvidence; onCredentialGeneration?: (generation: number) => void; }): Promise { const { accountId, existing, configuredPlan, rejectedAccessToken, rejectedGeneration, resp } = ctx; @@ -1061,6 +1079,7 @@ async function recoverPoolQuotaFrom401(ctx: { ctx.onCredentialGeneration?.(refreshed.generation); const writerGeneration = captureConfigGeneration(); + markQuotaProbeAttempted(ctx.quotaProbeEvidence, refreshed.generation); const replay = await fetch("https://chatgpt.com/backend-api/wham/usage", { headers: { Authorization: `Bearer ${refreshed.accessToken}`, @@ -1147,57 +1166,75 @@ async function fetchFreshPoolAccountQuota( existing: StoredAccountQuota | null, configuredPlan?: string, onCredentialGeneration?: (generation: number) => void, + getValidToken: typeof getValidCodexToken = getValidCodexToken, ): Promise { const writerGeneration = captureConfigGeneration(); let requestCredentialGeneration = readCodexAccountRecord(accountId)?.generation; + const quotaProbeEvidence: PoolQuotaProbeEvidence = {}; try { - const { accessToken, chatgptAccountId, generation } = await getValidCodexToken(accountId); + const { accessToken, chatgptAccountId, generation } = await getValidToken(accountId); requestCredentialGeneration = generation; onCredentialGeneration?.(generation); + markQuotaProbeAttempted(quotaProbeEvidence, generation); const resp = await fetch("https://chatgpt.com/backend-api/wham/usage", { headers: { Authorization: `Bearer ${accessToken}`, "ChatGPT-Account-Id": chatgptAccountId }, signal: AbortSignal.timeout(8000), }); if (!resp.ok) { if (resp.status !== 401) { - return { quota: existing ?? null, needsReauth: false, credentialGeneration: generation }; + return withQuotaProbeEvidence( + { quota: existing ?? null, needsReauth: false, credentialGeneration: generation }, + quotaProbeEvidence, + ); } // A bare 401 is what a stale-but-refreshable bearer produces after a plan change, so // quarantining on it tells the operator to re-authenticate an account that was fine // (#3019). Refresh once, replay once, and only then decide. - return await recoverPoolQuotaFrom401({ + const recovered = await recoverPoolQuotaFrom401({ accountId, existing, configuredPlan, rejectedAccessToken: accessToken, rejectedGeneration: generation, resp, + quotaProbeEvidence, onCredentialGeneration, }); + return withQuotaProbeEvidence(recovered, quotaProbeEvidence); } - return await commitPoolQuotaResponse(resp, { + const committed = await commitPoolQuotaResponse(resp, { accountId, existing, configuredPlan, generation, writerGeneration, }); + return withQuotaProbeEvidence(committed, quotaProbeEvidence); } catch (e) { if (e instanceof CodexCredentialGenerationConflictError || e instanceof CodexCredentialRefreshLockTimeoutError || e instanceof CodexCredentialRefreshBusyError || e instanceof CodexCredentialRefreshStaleError) { - return { + return withQuotaProbeEvidence({ quota: existing ?? null, needsReauth: false, credentialGeneration: requestCredentialGeneration, - ...(e instanceof CodexCredentialRefreshBusyError || e instanceof CodexCredentialRefreshStaleError - ? { quotaProbeSkipped: true as const } - : {}), - }; + quotaProbeSkipped: true, + }, quotaProbeEvidence); } if (e instanceof TokenRefreshError) { - return { quota: existing ?? null, needsReauth: true, credentialGeneration: requestCredentialGeneration }; + return withQuotaProbeEvidence( + { quota: existing ?? null, needsReauth: true, credentialGeneration: requestCredentialGeneration }, + quotaProbeEvidence, + ); } - return { quota: existing ?? null, needsReauth: false, credentialGeneration: requestCredentialGeneration }; + return withQuotaProbeEvidence( + { quota: existing ?? null, needsReauth: false, credentialGeneration: requestCredentialGeneration }, + quotaProbeEvidence, + ); } } -async function fetchPoolAccountQuota(accountId: string, forceRefresh = false, configuredPlan?: string): Promise { +async function fetchPoolAccountQuota( + accountId: string, + forceRefresh = false, + configuredPlan?: string, + getValidToken: typeof getValidCodexToken = getValidCodexToken, +): Promise { const existing = getAccountQuota(accountId); if (!forceRefresh && existing && Date.now() - existing.updatedAt < POOL_CACHE_TTL) { return { @@ -1227,6 +1264,7 @@ async function fetchPoolAccountQuota(accountId: string, forceRefresh = false, co existing, configuredPlan, generation => { state.resolvedCredentialGeneration = generation; }, + getValidToken, ); const flight: PoolQuotaRefreshFlight = { state, promise: refresh }; const activeFlights = flights ?? new Set(); @@ -1243,6 +1281,16 @@ async function fetchPoolAccountQuota(accountId: string, forceRefresh = false, co } let primeInFlight: Promise | null = null; +/** + * Last prime attempt per pool account. A failed WHAM lookup stores no quota, so + * without this the account stays "unknown" and every later prime trigger re-selects + * it as stale and repeats the same failing request. Successful lookups are already + * throttled by their stored updatedAt; this gives failures the same TTL backoff. + * + * Keyed by credential generation so a re-authentication, refresh, or account removal + * retries immediately instead of waiting out a backoff earned by the old credential. + */ +const poolQuotaPrimeAttemptedAt = new Map(); let cooldownRecoveryInFlight: Promise | null = null; export async function runCodexCooldownRecoveryProbes(config: OcxConfig, now = Date.now()): Promise { @@ -1294,6 +1342,19 @@ export interface PrimeCodexPoolQuotasOptions { fetchMainInfo?: typeof fetchMainAccountInfo; } +let getValidPoolTokenForPrime = getValidCodexToken; + +/** Test-only: inject a deterministic pre-dispatch credential outcome for quota priming. */ +export function setCodexPoolQuotaTokenResolverForTests( + resolver: typeof getValidCodexToken, +): () => void { + const previous = getValidPoolTokenForPrime; + getValidPoolTokenForPrime = resolver; + return () => { + if (getValidPoolTokenForPrime === resolver) getValidPoolTokenForPrime = previous; + }; +} + function tryAcquireNativeMainPrimeLease(): AdmissionLease | null { return tryAcquireNativeMainProfileClaim(); } @@ -1319,6 +1380,16 @@ export async function primeCodexPoolQuotas( options: PrimeCodexPoolQuotasOptions = {}, ): Promise { const openai = config.providers[OPENAI_CODEX_PROVIDER_ID]; + // Prune attempt markers for accounts that no longer exist BEFORE the eligibility + // return. A removal that happens while the provider is disabled or out of pool mode + // would otherwise leave a stale failure marker behind; restoring the same account id + // within POOL_CACHE_TTL would then read that old failure as current and skip the + // retry the restored credential is entitled to. + const runtimeConfig = getRuntimeConfig(config); + const configuredPoolIds = new Set((runtimeConfig.codexAccounts ?? []).map(account => account.id)); + for (const accountId of poolQuotaPrimeAttemptedAt.keys()) { + if (!configuredPoolIds.has(accountId)) poolQuotaPrimeAttemptedAt.delete(accountId); + } if ( !openai || openai.disabled === true @@ -1327,11 +1398,18 @@ export async function primeCodexPoolQuotas( ) return; if (primeInFlight) return primeInFlight; primeInFlight = (async () => { - const runtimeConfig = getRuntimeConfig(config); const pool = (runtimeConfig.codexAccounts ?? []).filter(isSelectableCodexPoolAccount); const stale = pool.filter(a => { const q = getAccountQuota(a.id); - return !q || Date.now() - q.updatedAt >= POOL_CACHE_TTL; + if (q) return Date.now() - q.updatedAt >= POOL_CACHE_TTL; + // No stored quota: either never primed, or the last attempt failed. Retry only + // once per TTL window so an unreachable or rejecting account cannot turn every + // prime trigger into another upstream request. + const lastAttempt = poolQuotaPrimeAttemptedAt.get(a.id); + if (!lastAttempt) return true; + // A newer credential invalidates the previous failure: retry without waiting. + if (lastAttempt.generation !== readCodexAccountRecord(a.id)?.generation) return true; + return Date.now() - lastAttempt.at >= POOL_CACHE_TTL; }); const primeMain = async () => { const mainLease = tryAcquireNativeMainPrimeLease(); @@ -1359,7 +1437,31 @@ export async function primeCodexPoolQuotas( primeMain(), mapWithConcurrency(stale, POOL_QUOTA_REFRESH_CONCURRENCY, async a => { if (!getCodexAccountCredential(a.id)) return; - await fetchPoolAccountQuota(a.id, false, a.plan); + let result: PoolQuotaResult; + try { + result = await fetchPoolAccountQuota(a.id, false, a.plan, getValidPoolTokenForPrime); + } catch (error) { + // Local quota-flight saturation proves no WHAM request existed for this account. + // Consume it per item so sibling workers remain inside the shared prime lifetime. + if (error instanceof PoolQuotaProbeBusyError) return; + throw error; + } + // Only the data-plane function knows whether upstream dispatch began. Any + // cache hit, credential deferral, or local admission failure remains eligible. + const attempted = result.quotaProbeAttempted; + if (!attempted) return; + if (!configuredPoolAccount(getRuntimeConfig(config), a.id)) { + poolQuotaPrimeAttemptedAt.delete(a.id); + return; + } + poolQuotaPrimeAttemptedAt.set(a.id, { + // getValidCodexToken may rotate the credential before WHAM is sent. + // Bind the backoff to the generation that actually made the request; + // otherwise the next prime sees a false generation change and retries + // the same failed WHAM call immediately. + generation: attempted.credentialGeneration, + at: attempted.at, + }); }), ]); } catch { @@ -1376,6 +1478,15 @@ export async function primeCodexPoolQuotas( * from another suite cannot coalesce into the next prime. */ export function clearCodexQuotaPrimeState(): void { primeInFlight = null; + poolQuotaPrimeAttemptedAt.clear(); + getValidPoolTokenForPrime = getValidCodexToken; +} + +/** Test-only: drop the shared single-flight promise while keeping the per-account + * failure backoff, so a test can trigger a second real prime pass and still observe + * the throttle a production caller would see. */ +export function clearCodexQuotaPrimeSingleFlightForTests(): void { + primeInFlight = null; } /** Test-only reset for the worker-level single-flight. */ diff --git a/tests/codex-quota-prime.test.ts b/tests/codex-quota-prime.test.ts index 01f02f19b2..d9da22e691 100644 --- a/tests/codex-quota-prime.test.ts +++ b/tests/codex-quota-prime.test.ts @@ -7,9 +7,17 @@ import { updateAccountQuota, clearAccountQuota, clearCodexQuotaPrimeState, + clearCodexQuotaPrimeSingleFlightForTests, clearMainAccountInfoCache, + seedCodexAuthAdmissionForTests, + setCodexPoolQuotaTokenResolverForTests, } from "../src/codex/auth-api"; -import { saveCodexAccountCredential } from "../src/codex/account-store"; +import { + CodexCredentialGenerationConflictError, + CodexCredentialRefreshLockTimeoutError, + readCodexAccountRecord, + saveCodexAccountCredential, +} from "../src/codex/account-store"; import { resetMainCodexAccountIdentityTrackingForTests } from "../src/codex/account-lifecycle"; import { MAIN_CODEX_ACCOUNT_ID } from "../src/codex/main-account"; import { @@ -400,6 +408,417 @@ describe("primeCodexPoolQuotas", () => { } }); + test("a failed pool quota fetch is throttled for the rest of the TTL window", async () => { + const config = makeConfig(); + seedPoolAccount(config, "p1"); + const originalFetch = globalThis.fetch; + let calls = 0; + try { + // Upstream is unavailable, so no quota is ever stored for this account. The + // account therefore stays "unknown" and, without an attempt record, every + // later prime re-selects it as stale and re-issues the same failing fetch. + globalThis.fetch = async (input: RequestInfo | URL) => { + if (String(input).includes("/backend-api/wham/usage")) { + calls += 1; + return new Response("upstream unavailable", { status: 503 }); + } + return originalFetch(input); + }; + await primeCodexPoolQuotas(config, "test"); + expect(calls).toBe(1); + expect(getAccountQuota("p1")).toBeNull(); + + // Only the single-flight promise is dropped between passes; the throttle state + // must survive so a later trigger does not repeat the failing lookup. + clearCodexQuotaPrimeSingleFlightForTests(); + await primeCodexPoolQuotas(config, "test"); + clearCodexQuotaPrimeSingleFlightForTests(); + await primeCodexPoolQuotas(config, "test"); + + // A failed lookup must back off for the same POOL_CACHE_TTL window that a + // successful one gets, instead of retrying on every prime trigger. + expect(calls).toBe(1); + expect(getAccountQuota("p1")).toBeNull(); + } finally { + globalThis.fetch = originalFetch; + } + }); + + test("a real failed pool quota probe becomes eligible after the TTL expires", async () => { + const originalNow = Date.now; + let now = 1_800_000_000_000; + Date.now = () => now; + const config = makeConfig(); + seedPoolAccount(config, "p1"); + saveCodexAccountCredential("p1", { + accessToken: "access-p1-long-lived", + refreshToken: "refresh-p1-long-lived", + expiresAt: now + 60 * 60_000, + chatgptAccountId: "acct-p1", + }); + const originalFetch = globalThis.fetch; + let calls = 0; + let upstreamHealthy = false; + try { + globalThis.fetch = async (input: RequestInfo | URL) => { + if (String(input).includes("/backend-api/wham/usage")) { + calls += 1; + return upstreamHealthy ? whamResponse(20) : new Response("upstream unavailable", { status: 503 }); + } + return originalFetch(input); + }; + + await primeCodexPoolQuotas(config, "test"); + expect(calls).toBe(1); + + now += 5 * 60_000 - 1; + clearCodexQuotaPrimeSingleFlightForTests(); + await primeCodexPoolQuotas(config, "test"); + expect(calls).toBe(1); + + now += 1; + upstreamHealthy = true; + clearCodexQuotaPrimeSingleFlightForTests(); + await primeCodexPoolQuotas(config, "test"); + expect(calls).toBe(2); + expect(getAccountQuota("p1")).toMatchObject({ weeklyPercent: 20 }); + } finally { + globalThis.fetch = originalFetch; + Date.now = originalNow; + } + }); + + test("removing an account from the pool purges its failed-prime backoff", async () => { + const config = makeConfig(); + seedPoolAccount(config, "p1"); + const originalPool = [...(config.codexAccounts ?? [])]; + const originalFetch = globalThis.fetch; + let calls = 0; + let upstreamHealthy = false; + try { + globalThis.fetch = async (input: RequestInfo | URL) => { + if (String(input).includes("/backend-api/wham/usage")) { + calls += 1; + return upstreamHealthy ? whamResponse(20) : new Response("upstream unavailable", { status: 503 }); + } + return originalFetch(input); + }; + + await primeCodexPoolQuotas(config, "test"); + expect(calls).toBe(1); + + config.codexAccounts = []; + clearCodexQuotaPrimeSingleFlightForTests(); + await primeCodexPoolQuotas(config, "removed"); + + config.codexAccounts = originalPool; + upstreamHealthy = true; + clearCodexQuotaPrimeSingleFlightForTests(); + await primeCodexPoolQuotas(config, "restored"); + expect(calls).toBe(2); + expect(getAccountQuota("p1")).toMatchObject({ weeklyPercent: 20 }); + } finally { + globalThis.fetch = originalFetch; + } + }); + + test("removal purges the backoff even while the provider is disabled", async () => { + // The prune used to sit AFTER the provider-eligibility early return, so a removal + // that happened while the provider was disabled (or out of pool mode) left the + // stale failure marker in place. Restoring the same account id within + // POOL_CACHE_TTL then read that old failure as current and skipped the retry the + // restored credential is entitled to. + const config = makeConfig(); + seedPoolAccount(config, "p1"); + const originalPool = [...(config.codexAccounts ?? [])]; + const originalFetch = globalThis.fetch; + let calls = 0; + let upstreamHealthy = false; + try { + globalThis.fetch = async (input: RequestInfo | URL) => { + if (String(input).includes("/backend-api/wham/usage")) { + calls += 1; + return upstreamHealthy ? whamResponse(20) : new Response("upstream unavailable", { status: 503 }); + } + return originalFetch(input); + }; + + // One failed prime records the backoff marker. + await primeCodexPoolQuotas(config, "test"); + expect(calls).toBe(1); + + // The account is removed while the provider is disabled: the prime returns early, + // but the marker must still be pruned. + config.codexAccounts = []; + config.providers.openai!.disabled = true; + clearCodexQuotaPrimeSingleFlightForTests(); + await primeCodexPoolQuotas(config, "removed-while-disabled"); + expect(calls).toBe(1); + + // Restored and re-enabled inside the TTL window: the prime must dispatch now. + config.codexAccounts = originalPool; + config.providers.openai!.disabled = false; + upstreamHealthy = true; + clearCodexQuotaPrimeSingleFlightForTests(); + await primeCodexPoolQuotas(config, "restored"); + expect(calls).toBe(2); + expect(getAccountQuota("p1")).toMatchObject({ weeklyPercent: 20 }); + } finally { + globalThis.fetch = originalFetch; + } + }); + + test("a late failed probe cannot restore backoff for an account removed in flight", async () => { + const config = makeConfig(); + seedPoolAccount(config, "p1"); + const originalPool = [...(config.codexAccounts ?? [])]; + const originalFetch = globalThis.fetch; + let calls = 0; + let upstreamHealthy = false; + let releaseFirst!: () => void; + const firstDispatched = new Promise(resolve => { releaseFirst = resolve; }); + let finishFirst!: () => void; + const firstGate = new Promise(resolve => { finishFirst = resolve; }); + try { + globalThis.fetch = async (input: RequestInfo | URL) => { + if (String(input).includes("/backend-api/wham/usage")) { + calls += 1; + if (calls === 1) { + releaseFirst(); + await firstGate; + return new Response("upstream unavailable", { status: 503 }); + } + return upstreamHealthy ? whamResponse(20) : new Response("upstream unavailable", { status: 503 }); + } + return originalFetch(input); + }; + + const firstPrime = primeCodexPoolQuotas(config, "test"); + await firstDispatched; + config.codexAccounts = []; + const removedPrime = primeCodexPoolQuotas(config, "removed"); + finishFirst(); + await Promise.all([firstPrime, removedPrime]); + + config.codexAccounts = originalPool; + upstreamHealthy = true; + clearCodexQuotaPrimeSingleFlightForTests(); + await primeCodexPoolQuotas(config, "restored"); + + expect(calls).toBe(2); + expect(getAccountQuota("p1")).toMatchObject({ weeklyPercent: 20 }); + } finally { + finishFirst(); + globalThis.fetch = originalFetch; + } + }); + + test("re-authenticating a failed account retries without waiting out the backoff", async () => { + const config = makeConfig(); + seedPoolAccount(config, "p1"); + const originalFetch = globalThis.fetch; + let calls = 0; + let upstreamHealthy = false; + try { + globalThis.fetch = async (input: RequestInfo | URL) => { + if (String(input).includes("/backend-api/wham/usage")) { + calls += 1; + return upstreamHealthy ? whamResponse(20) : new Response("down", { status: 503 }); + } + return originalFetch(input); + }; + await primeCodexPoolQuotas(config, "test"); + expect(calls).toBe(1); + expect(getAccountQuota("p1")).toBeNull(); + + // Throttled while the same credential keeps failing. + clearCodexQuotaPrimeSingleFlightForTests(); + await primeCodexPoolQuotas(config, "test"); + expect(calls).toBe(1); + + // A re-authentication bumps the credential generation, which must invalidate the + // backoff earned by the old credential instead of hiding a now-usable account. + upstreamHealthy = true; + saveCodexAccountCredential("p1", { + accessToken: "access-p1-renewed", + refreshToken: "refresh-p1-renewed", + expiresAt: Date.now() + 5 * 60_000, + chatgptAccountId: "acct-p1", + }); + clearCodexQuotaPrimeSingleFlightForTests(); + await primeCodexPoolQuotas(config, "test"); + expect(calls).toBe(2); + expect(getAccountQuota("p1")).toMatchObject({ weeklyPercent: 20 }); + } finally { + globalThis.fetch = originalFetch; + } + }); + + test("an admission-busy prime does not back off an account it never probed", async () => { + const config = makeConfig(); + seedPoolAccount(config, "p1"); + const originalFetch = globalThis.fetch; + const releaseAdmission = seedCodexAuthAdmissionForTests({ quotaFlights: 16 }); + let whamCalls = 0; + try { + globalThis.fetch = async (input: RequestInfo | URL) => { + if (String(input).includes("/backend-api/wham/usage")) { + whamCalls += 1; + return whamResponse(20); + } + return originalFetch(input); + }; + await primeCodexPoolQuotas(config, "test"); + expect(whamCalls).toBe(0); + expect(getAccountQuota("p1")).toBeNull(); + + releaseAdmission(); + clearCodexQuotaPrimeSingleFlightForTests(); + await primeCodexPoolQuotas(config, "test"); + + expect(whamCalls).toBe(1); + expect(getAccountQuota("p1")).toMatchObject({ weeklyPercent: 20 }); + } finally { + releaseAdmission(); + globalThis.fetch = originalFetch; + } + }); + + test.each([ + ["credential generation conflict", () => new CodexCredentialGenerationConflictError()], + ["refresh-lock timeout", () => new CodexCredentialRefreshLockTimeoutError()], + ] as const)("a %s before dispatch does not back off the next prime", async (_label, makeError) => { + const config = makeConfig(); + seedPoolAccount(config, "p1"); + const originalFetch = globalThis.fetch; + let tokenAttempts = 0; + let whamCalls = 0; + try { + globalThis.fetch = async (input: RequestInfo | URL) => { + if (String(input).includes("/backend-api/wham/usage")) { + whamCalls += 1; + return whamResponse(20); + } + return originalFetch(input); + }; + const getValidPoolToken = async () => { + tokenAttempts += 1; + if (tokenAttempts === 1) throw makeError(); + return { + accessToken: "access-p1", + chatgptAccountId: "acct-p1", + generation: readCodexAccountRecord("p1")!.generation, + }; + }; + const restoreTokenResolver = setCodexPoolQuotaTokenResolverForTests(getValidPoolToken); + + try { + await primeCodexPoolQuotas(config, "test"); + clearCodexQuotaPrimeSingleFlightForTests(); + await primeCodexPoolQuotas(config, "test"); + } finally { + restoreTokenResolver(); + } + + expect(tokenAttempts).toBe(2); + expect(whamCalls).toBe(1); + expect(getAccountQuota("p1")).toMatchObject({ weeklyPercent: 20 }); + } finally { + globalThis.fetch = originalFetch; + } + }); + + test("a refreshed credential keeps the backoff earned by its failed WHAM request", async () => { + const config = makeConfig(); + seedPoolAccount(config, "p1"); + saveCodexAccountCredential("p1", { + accessToken: "expiring-p1", + refreshToken: "refresh-p1", + expiresAt: Date.now() + 30_000, + chatgptAccountId: "acct-p1", + }); + const startGeneration = readCodexAccountRecord("p1")?.generation; + const originalFetch = globalThis.fetch; + let oauthCalls = 0; + let whamCalls = 0; + try { + globalThis.fetch = async (input: RequestInfo | URL) => { + const url = String(input); + if (url.includes("/oauth/token")) { + oauthCalls += 1; + return Response.json({ + access_token: "fresh-p1", + refresh_token: "fresh-refresh-p1", + expires_in: 3600, + }); + } + if (url.includes("/backend-api/wham/usage")) { + whamCalls += 1; + return new Response("upstream unavailable", { status: 503 }); + } + return originalFetch(input); + }; + + await primeCodexPoolQuotas(config, "test"); + expect(oauthCalls).toBe(1); + expect(whamCalls).toBe(1); + expect(readCodexAccountRecord("p1")?.generation).toBe((startGeneration ?? 0) + 1); + expect(getAccountQuota("p1")).toBeNull(); + + clearCodexQuotaPrimeSingleFlightForTests(); + await primeCodexPoolQuotas(config, "test"); + + expect(oauthCalls).toBe(1); + expect(whamCalls).toBe(1); + expect(getAccountQuota("p1")).toBeNull(); + } finally { + globalThis.fetch = originalFetch; + } + }); + + test("a failed 401 replay binds backoff to the replay credential generation", async () => { + const config = makeConfig(); + seedPoolAccount(config, "p1"); + const originalFetch = globalThis.fetch; + let oauthCalls = 0; + let whamCalls = 0; + try { + globalThis.fetch = async (input: RequestInfo | URL) => { + const url = String(input); + if (url.includes("/oauth/token")) { + oauthCalls += 1; + return Response.json({ + access_token: "fresh-p1", + refresh_token: "fresh-refresh-p1", + expires_in: 3600, + }); + } + if (url.includes("/backend-api/wham/usage")) { + whamCalls += 1; + if (whamCalls === 1) { + return Response.json({ error: { code: "transient_edge_rejection" } }, { status: 401 }); + } + throw new Error("replay transport unavailable"); + } + return originalFetch(input); + }; + + await primeCodexPoolQuotas(config, "test"); + expect(oauthCalls).toBe(1); + expect(whamCalls).toBe(2); + expect(getAccountQuota("p1")).toBeNull(); + + clearCodexQuotaPrimeSingleFlightForTests(); + await primeCodexPoolQuotas(config, "test"); + + expect(oauthCalls).toBe(1); + expect(whamCalls).toBe(2); + expect(getAccountQuota("p1")).toBeNull(); + } finally { + globalThis.fetch = originalFetch; + } + }); + test("one blocked account does not sink the rest", async () => { const config = makeConfig(); seedPoolAccount(config, "ok"); From ea29e25b05cea7cefabad576e9dfe291e8d5daf0 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 02:27:50 +0900 Subject: [PATCH 119/172] fix(service): report the wait that was actually spent, not the budget (#3186) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follow-up to #3134, which named this as the one piece of #3039 it deliberately did not carry. reportServiceServing printed Math.trunc(healthBudgetMs / 1000) — the budget it allowed — while confirmServiceServing knocks once more after a 500ms grace sleep whenever it waited at all. So a Windows run that gives up reports "within 45s" after spending 45.5s. The gap is half a second today. It is the expression that is wrong, not the number: it states a figure the run did not spend, and it understates every future grace the loop grows. The reader is using it to judge whether the service was merely still coming up, which is a question about elapsed time. The wait is now timed in reportServiceServing, through the same injected `now` the deps already carry, so confirmServiceServing's contract and its existing tests are untouched. Two tests: - the Windows budget case asserts the clock reached 45_000 + 500 and that the line says "after 46s" — the rounded real wait — and no longer says 45s; - a zero budget takes the single probe, skips the grace, and reports "after 0s", so a caller that asked not to wait is not told it waited. Reverting to the budget expression fails both. tests/service.test.ts: 183 pass, 6 fail. Those six fail identically on a clean `dev` checkout on this host (Windows) and are untouched by this change; compared by test name rather than by count. Co-authored-by: Nguyen Thanh Dat --- src/service.ts | 12 ++++++++++-- tests/service.test.ts | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/service.ts b/src/service.ts index 7733be7df6..84b7da3817 100644 --- a/src/service.ts +++ b/src/service.ts @@ -740,14 +740,22 @@ export async function reportServiceServing( deps: Parameters[0] = {}, ): Promise { const healthBudgetMs = deps.timeoutMs ?? serviceInstallHealthMs(); + // Timed here rather than reported from the budget. confirmServiceServing knocks once + // more after a grace sleep whenever it waited at all, so the real wait is the budget + // plus that grace — and printing the budget states a number the run did not spend. + // What the reader is deciding is whether the service was still coming up, which is a + // judgement about elapsed time (#3009). + const now = deps.now ?? Date.now; + const startedAt = now(); const serving = await confirmServiceServing({ ...deps, timeoutMs: healthBudgetMs }); + const waitedMs = Math.max(0, now() - startedAt); if (serving.ok) { console.log(`✅ opencodex service ${verb} and serving on port ${serving.port}.`); return; } console.error( - `⚠️ Service ${verb}, but no proxy answered on port ${serving.port} within ` - + `${Math.trunc(healthBudgetMs / 1000)}s.\n` + `⚠️ Service ${verb}, but no proxy answered on port ${serving.port} after ` + + `${Math.round(waitedMs / 1000)}s.\n` + ` The manager registered the job; that is not the same as serving.\n` + ` Log: ${serviceLogPath()}\n` + ` Meanwhile: ocx start (serves in the foreground)`, diff --git a/tests/service.test.ts b/tests/service.test.ts index 48788a60f3..82264c2f94 100644 --- a/tests/service.test.ts +++ b/tests/service.test.ts @@ -3344,7 +3344,13 @@ describe("service serving confirmation", () => { expect(serviceInstallHealthMs("darwin")).toBe(SERVICE_INSTALL_HEALTH_MS); }); - test("reports the effective Windows failure budget", async () => { + // The failure line reports what the run actually spent, not what it was allowed to. + // With the Windows budget the loop exits at 45s and the post-deadline grace knock + // adds its 500ms sleep, so the real wait is 45.5s. Reporting the budget printed 45s + // for a 45.5s wait -- a small gap here, but the same expression understates every + // future grace the loop grows, and the reader is using this number to judge whether + // the service was still coming up (#3009). + test("reports the wait it actually spent, grace knock included", async () => { const errors: string[] = []; const previousError = console.error; const previousExitCode = process.exitCode; @@ -3358,8 +3364,35 @@ describe("service serving confirmation", () => { now: () => now, timeoutMs: SERVICE_INSTALL_HEALTH_WINDOWS_MS, }); - expect(errors.join("\n")).toContain("within 45s"); - expect(errors.join("\n")).not.toContain("within 20s"); + expect(now).toBe(SERVICE_INSTALL_HEALTH_WINDOWS_MS + 500); + expect(errors.join("\n")).toContain("after 46s"); + expect(errors.join("\n")).not.toContain("45s"); + expect(errors.join("\n")).not.toContain("20s"); + } finally { + console.error = previousError; + process.exitCode = previousExitCode ?? 0; + } + }); + + // A caller that asked not to wait must not be told it waited: with a zero budget + // confirmServiceServing takes its single probe and skips the grace entirely, so the + // reported wait is 0 rather than the budget. + test("reports no wait when the caller asked not to wait", async () => { + const errors: string[] = []; + const previousError = console.error; + const previousExitCode = process.exitCode; + let now = 0; + console.error = (...values: unknown[]) => { errors.push(values.join(" ")); }; + try { + await reportServiceServing("started", { + port: 10100, + probe: async () => false, + sleep: async ms => { now += ms; }, + now: () => now, + timeoutMs: 0, + }); + expect(now).toBe(0); + expect(errors.join("\n")).toContain("after 0s"); } finally { console.error = previousError; process.exitCode = previousExitCode ?? 0; From d335570647ca0360e63745615901a10303042784 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 02:28:37 +0900 Subject: [PATCH 120/172] fix duplicate Codex restore after graceful stop (#3187) Co-authored-by: x3M3x --- src/cli/index.ts | 19 ++++++++++++++----- src/lib/process-control.ts | 7 ++++--- tests/grok-lifecycle.test.ts | 12 ++++++++++-- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/cli/index.ts b/src/cli/index.ts index de491b5323..e54d1ed016 100755 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -757,6 +757,7 @@ async function handleStop() { // restart-window wait; launchd, systemd and WinSW are down when they say so. let schedulerCanRespawn = false; let stoppedService = false; + let nativeRestoreHandledByProxy = false; // An ownership mismatch means the service manager was never even contacted: the installed // service is still live and will respawn the proxy. Tearing down SHARED state in that // situation (native Codex config, the Grok fence) removes config out from under a running @@ -805,17 +806,22 @@ async function handleStop() { * guessed one fails closed into manual recovery rather than letting a later probe read * "the configured port refuses" as proof that the right proxy is down. */ - const stopWithDeferral = async (pid: number, discovered?: { hostname: string; port: number } | null): Promise => { + const stopWithDeferral = async (pid: number, discovered?: { hostname: string; port: number } | null): Promise => { // Resolve ONCE. Reading the runtime record twice let the receipt name the configured // guess while the request went to a runtime endpoint that appeared in between. const exact = discovered ?? endpointOf(readRuntimePort(pid)); claimTeardown(exact ?? configuredEndpoint(), exact ? "exact" : "guessed"); - await stopProxy(pid, { + const graceful = await stopProxy(pid, { deferSharedTeardownNonce: teardownNonce, // Only an exact endpoint may direct the request; the configured fallback is a guess // good enough to record an obligation against, not to POST a stop to. runtimeEndpoint: exact ?? undefined, }); + // A valid receipt means the proxy deferred shared teardown to this process. If the + // receipt could not be written, the proxy restores it itself and the caller must not + // attempt a second restore after a graceful stop. A hard-kill always leaves restore + // to this process. + return graceful && !teardownNonce; }; try { const serviceStop = stopServiceIfInstalledDetailed(); @@ -858,7 +864,7 @@ async function handleStop() { // verification below, so a survivor does not get its client config pulled first. // The receipt goes down first — the proxy honours the deferral only when it can // see one, so an unrecordable claim degrades to the child doing its own teardown. - await stopWithDeferral(pid); + nativeRestoreHandledByProxy = await stopWithDeferral(pid); console.log(`✅ Proxy (PID ${pid}) stopped.`); removePid(pid); removeRuntimePort(pid); @@ -888,7 +894,10 @@ async function handleStop() { try { // The probe already found where it answers, and on this path the runtime record is // typically what went missing in the first place. - await stopWithDeferral(live.pid, { hostname: live.hostname ?? "127.0.0.1", port: live.port }); + nativeRestoreHandledByProxy = await stopWithDeferral( + live.pid, + { hostname: live.hostname ?? "127.0.0.1", port: live.port }, + ); console.log(`✅ Proxy (PID ${live.pid}) stopped.`); } catch (err) { stopFailed = true; @@ -1006,7 +1015,7 @@ async function handleStop() { console.error(" The obligation is preserved; retry once the proxy is confirmed stopped."); } } - const restoreBlocked = ownershipBlocked || inheritedBlocks; + const restoreBlocked = ownershipBlocked || inheritedBlocks || nativeRestoreHandledByProxy; if (!restoreBlocked) { if (recoveredNonces.length > 0) { // A previous deferred stop died before restoring, and the probe says its endpoint is diff --git a/src/lib/process-control.ts b/src/lib/process-control.ts index 3e296d6c72..13ab3a0c95 100644 --- a/src/lib/process-control.ts +++ b/src/lib/process-control.ts @@ -132,8 +132,8 @@ function drainDeadlineMs(): number { } /** Graceful-first stop: management-API drain, then the platform kill ladder. */ -export async function stopProxy(pid: number, io: GracefulStopIo = {}): Promise { - if (!isProcessAlive(pid)) return; +export async function stopProxy(pid: number, io: GracefulStopIo = {}): Promise { + if (!isProcessAlive(pid)) return false; const runtime = io.runtimeEndpoint ?? readRuntimePort(pid); const graceful = await stopProxyGracefully(pid, io); if (graceful === "refused") { @@ -146,10 +146,11 @@ export async function stopProxy(pid: number, io: GracefulStopIo = {}): Promise { expect(stopFn).toContain("ownershipBlocked = true"); // Ownership is now one of two reasons to skip the restore; the other is an inherited // obligation whose proxy could not be confirmed down (#3008). - expect(stopFn).toContain("const restoreBlocked = ownershipBlocked ||"); + expect(stopFn).toContain("const restoreBlocked = ownershipBlocked || inheritedBlocks || nativeRestoreHandledByProxy;"); expect(stopFn).toContain("if (!restoreBlocked) {"); expect(stopFn).toContain("await restoreSharedClientStateAfterStop()"); expect(restoreFn).toContain("restoreNativeCodexAsync()"); @@ -106,6 +106,14 @@ describe("Grok fence lifecycle wiring", () => { expect(stopFn.indexOf("revertSystemEnv()")).toBeLessThan(stopFn.indexOf("if (!restoreBlocked) {")); }); + test("graceful stop skips caller restore only when the proxy performed it", () => { + const stopFn = sliceFn(CLI_SOURCE, "async function handleStop(", "async function handleUninstall("); + expect(stopFn).toContain("const graceful = await stopProxy(pid, {"); + expect(stopFn).toContain("return graceful && !teardownNonce;"); + expect(stopFn).toContain("nativeRestoreHandledByProxy = await stopWithDeferral(pid);"); + expect(stopFn).toContain("nativeRestoreHandledByProxy = await stopWithDeferral("); + }); + test("a refused Grok strip makes ocx stop fail instead of reporting success", () => { const restoreFn = sliceFn(CLI_SOURCE, "async function restoreSharedClientStateAfterStop(", "async function handleStop("); const stopFn = sliceFn(CLI_SOURCE, "async function handleStop(", "async function handleUninstall("); @@ -261,7 +269,7 @@ describe("Grok fence lifecycle wiring", () => { expect(stopFn).toContain("teardownNonce ? [teardownNonce, ...recoveredNonces] : recoveredNonces"); // The orphan path hands over the endpoint the probe already found; its runtime record // is typically what went missing in the first place. - expect(stopFn).toContain('stopWithDeferral(live.pid, { hostname: live.hostname ?? "127.0.0.1", port: live.port })'); + expect(stopFn).toContain('nativeRestoreHandledByProxy = await stopWithDeferral(\n live.pid,\n { hostname: live.hostname ?? "127.0.0.1", port: live.port },\n );'); // A live proxy with no killable pid is not "no proxy found": purging state and // restoring over it is the same failure arrived at from the other direction. expect(stopFn).toContain("} else if (live) {"); From 5ccf7c80016eddf66d297288488f1e1fd5022272 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 02:29:41 +0900 Subject: [PATCH 121/172] fix(cli): let an explicit different --port start a sibling (rebase of #3144) (#3188) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(cli): let an explicit different --port start a sibling instead of refusing #3106 made handleStart probe the configured port so a bare `ocx start` cannot shadow a healthy configured-port proxy with an ephemeral-port copy. But the refusal it added fires whenever ANY live proxy is found, ignoring an explicit `--port` that names a DIFFERENT port. That is not the shadow the guard targets: an interactive `ocx start --port X` with X free is an explicit sibling request. The blanket refusal broke two real things. With a proxy on the configured port, no second instance could be started on any other port at all. And every test that spawns the real launcher on a unique free port — shutdown-launcher's three signal cases — timed out its 20 s startup wait on any machine that runs an actual proxy, because the sandboxed home has no config.json, the probe fell through to the default port, and found the developer's real instance. That is how it surfaced: 0 pass / 3 fail on unmodified dev, reproducible only where a live proxy sits on 10100. The gate is deliberately narrow. The sibling path opens only when a live proxy exists AND --port was given explicitly AND it differs from the live proxy's port AND the caller is not the service wrapper. The wrapper always passes the configured port and keeps its exact stay-out-of-the-way semantics (#3106's endless-respawn fix); a bare start and a same-port start still refuse. The sibling start warns that this home's Codex config is re-pointed, which is the one side effect it shares with any start. Verified as a matrix on a machine with a real proxy on the configured port: bare start refuses, --port refuses, --port starts, and shutdown-launcher goes from 0 pass / 3 fail to green; reverting the gate turns it back. A source-level oracle pins the port comparison and the OCX_SERVICE exclusion next to #3106's own oracles. Co-Authored-By: Claude Fable 5 * test(cli): run the live-owner start decision at runtime, not as source text Review follow-up on the sibling-port fix. The source oracle pinned implementation text and could keep passing while handleStart refused the request, mis-parsed the port, or ignored OCX_SERVICE — and the integration proof in shutdown-launcher only exercises the sibling path on a machine that actually runs a proxy on the configured port, which CI does not. The decision is now a pure function, `decideStartWithLiveOwner`, exercised across the whole matrix at runtime: bare start refused, same-port refused, different-port sibling, service stay-out on both port shapes, and the exact-"1" sentinel so "0"/"false" cannot reach the stay-out path. The remaining source assertion pins only that handleStart routes through the shared decision. The sibling warning now also names the pid/runtime record takeover, not just the Codex config re-point — the shared-state model for same-home starts is unchanged from pre-#3106 and the message should say all of what that means. Co-Authored-By: Claude Fable 5 * test(cli): update the OCX_SERVICE exit oracle for the routed decision The comparison the old oracle grepped for moved into decideStartWithLiveOwner, where the sentinel semantics run at runtime across the whole matrix. The oracle now pins what stayed in index.ts: the stay-out decision exits 0 and the conflict refusal keeps exit 1. Co-Authored-By: Claude Fable 5 --------- Co-authored-by: olddonkey Co-authored-by: Claude Fable 5 --- src/cli/dispatch.ts | 28 +++++++++++++++++++++++++++ src/cli/index.ts | 35 +++++++++++++++++++++++----------- tests/cli-dispatch.test.ts | 39 +++++++++++++++++++++++++++++++++++++- tests/cli-ready.test.ts | 15 +++++++++------ 4 files changed, 99 insertions(+), 18 deletions(-) diff --git a/src/cli/dispatch.ts b/src/cli/dispatch.ts index 75275753ad..2f94753043 100644 --- a/src/cli/dispatch.ts +++ b/src/cli/dispatch.ts @@ -836,6 +836,34 @@ export const DISPATCH_ALIASES: ReadonlyMap = aliasTargets; /** Resolve the runner key for a command, following registry aliases to the * canonical runner. Returns undefined when the command is unknown. */ +/** What `handleStart` does about a live proxy it found before binding. */ +export type StartOwnerDecision = "refuse" | "service-stay-out" | "sibling"; + +/** + * Pure decision for `handleStart` when the pre-bind probe found a live proxy. + * + * The #3106 guard exists so a bare `start` cannot shadow a healthy configured-port + * proxy with an ephemeral-port copy. An interactive `--port X` naming a DIFFERENT + * port than the live proxy's is an explicit sibling request, not that shadow — and + * refusing it also broke every spawned-launcher test on a machine running a real + * proxy, because the probe reaches the machine-global port across sandbox homes. + * The service wrapper always passes the configured port and keeps its exact + * stay-out-of-the-way semantics: it never takes the sibling path. + */ +export function decideStartWithLiveOwner(input: { + livePort: number; + requestedPort: number | undefined; + ocxService: string | undefined; +}): StartOwnerDecision { + const sibling = input.requestedPort !== undefined + && input.requestedPort !== input.livePort + // Only the exact "1" sentinel is service context — the same check syncCleanup + // uses — so an env value like "0" or "false" cannot reach the stay-out path. + && input.ocxService !== "1"; + if (sibling) return "sibling"; + return input.ocxService === "1" ? "service-stay-out" : "refuse"; +} + export function resolveDispatchCommand(command: string | undefined): string | undefined { if (command === undefined) return undefined; if (Object.prototype.hasOwnProperty.call(commandRunners, command)) return command; diff --git a/src/cli/index.ts b/src/cli/index.ts index e54d1ed016..914d803cf5 100755 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -49,7 +49,7 @@ import { } from "./tray-proxy"; import { requestBoundSystemRestart } from "./system-restart-client"; import { installCrashGuards } from "../lib/crash-guard"; -import { dispatchCommand } from "./dispatch"; +import { dispatchCommand , decideStartWithLiveOwner } from "./dispatch"; import { findAvailablePort, isAddrInUse, PortUnavailableError, shouldPersistSelectedPort, waitForPortAvailable } from "../server/ports"; import { findLiveProxy, probeHostname, type LiveProxy } from "../server/proxy-liveness"; import { createReadinessGate } from "../server/readiness"; @@ -254,19 +254,32 @@ async function handleStart(options: { block?: boolean } = {}) { // already passes this; `handleStart` is the path that did not. const owner = await findProxyOwnerBeforeJournalRecovery({ probeConfiguredPort: true }); if (owner.live) { - // Service-wrapper context (opencodex-service.cmd `:loop`): a healthy proxy from - // ANY source means the requested port is already served. Exit 0 so the wrapper's - // `if %ERRORLEVEL% NEQ 0` retry loop terminates instead of respawning every 5s - // against a listener it can never claim (observed as an endless - // "Proxy already running" service.log loop). - // Only the exact "1" sentinel takes this path — the same check syncCleanup - // uses — so an env value like "0" or "false" cannot bypass the conflict error. - if (process.env.OCX_SERVICE === "1") { + // Rationale and the full decision table live on `decideStartWithLiveOwner`. + const decision = decideStartWithLiveOwner({ + livePort: owner.live.port, + requestedPort, + ocxService: process.env.OCX_SERVICE, + }); + if (decision === "service-stay-out") { + // Service-wrapper context (opencodex-service.cmd `:loop`): a healthy proxy from + // ANY source means the requested port is already served. Exit 0 so the wrapper's + // `if %ERRORLEVEL% NEQ 0` retry loop terminates instead of respawning every 5s + // against a listener it can never claim (observed as an endless + // "Proxy already running" service.log loop). console.log(`Proxy already running (PID ${owner.live.pid ?? owner.pidSnapshot ?? "unknown"}, port ${owner.live.port}); service wrapper staying out of the way.`); process.exit(0); } - console.error(`⚠️ Proxy already running (PID ${owner.live.pid ?? owner.pidSnapshot ?? "unknown"}, port ${owner.live.port}). Use 'ocx stop' first.`); - process.exit(1); + if (decision === "refuse") { + console.error(`⚠️ Proxy already running (PID ${owner.live.pid ?? owner.pidSnapshot ?? "unknown"}, port ${owner.live.port}). Use 'ocx stop' first.`); + process.exit(1); + } + // Sibling path. Honest about the side effects it shares with any start in this home: + // the new instance takes over this home's ocx.pid / runtime-port.json while it runs, + // and re-points this home's Codex config at the new port when injection applies. + console.warn( + `Proxy already running on port ${owner.live.port}; starting a second instance on requested port ${requestedPort}. ` + + `The new instance takes over this home's pid/runtime records and Codex config while it runs.`, + ); } const clientState = readClientConnectionState(); diff --git a/tests/cli-dispatch.test.ts b/tests/cli-dispatch.test.ts index 069f63004a..aee0456546 100644 --- a/tests/cli-dispatch.test.ts +++ b/tests/cli-dispatch.test.ts @@ -1,6 +1,6 @@ import { describe, expect, spyOn, test } from "bun:test"; import { CLI_COMMANDS } from "../src/cli/registry"; -import { DISPATCH_ALIASES, DISPATCH_COMMANDS, dispatchCommand, resolveDispatchCommand } from "../src/cli/dispatch"; +import { DISPATCH_ALIASES, DISPATCH_COMMANDS, dispatchCommand, resolveDispatchCommand, decideStartWithLiveOwner } from "../src/cli/dispatch"; import type { CliDispatchDeps } from "../src/cli/dispatch"; import { runGuiCommand } from "../src/cli/gui"; import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; @@ -215,6 +215,43 @@ describe("start probes the configured port before shadowing it (source-level)", } }); + /** + * The #3106 guard refused start whenever ANY live proxy existed, ignoring an explicit + * `--port` that differs from the live proxy's port. That is not the shadow the guard + * targets (a bare `start` landing on an ephemeral port); it broke starting a second + * instance on another port, and every spawned-launcher test on a machine running a + * real proxy timed out its startup wait. The decision is a pure function so the whole + * matrix runs at runtime here; the source oracle below only pins that handleStart + * actually routes through it. + */ + test("the live-owner decision matrix", () => { + // Bare start: the #3106 shadow — still refused. + expect(decideStartWithLiveOwner({ livePort: 10100, requestedPort: undefined, ocxService: undefined })) + .toBe("refuse"); + // Explicit port equal to the live proxy's: same conflict — still refused. + expect(decideStartWithLiveOwner({ livePort: 10100, requestedPort: 10100, ocxService: undefined })) + .toBe("refuse"); + // Explicit DIFFERENT port, interactive: the sibling request this fix restores. + expect(decideStartWithLiveOwner({ livePort: 10100, requestedPort: 65301, ocxService: undefined })) + .toBe("sibling"); + // Service wrapper keeps its exact stay-out semantics on both port shapes. + expect(decideStartWithLiveOwner({ livePort: 10100, requestedPort: 10100, ocxService: "1" })) + .toBe("service-stay-out"); + expect(decideStartWithLiveOwner({ livePort: 10100, requestedPort: 8080, ocxService: "1" })) + .toBe("service-stay-out"); + // Only the exact "1" sentinel is service context — "0"/"false" cannot reach stay-out. + expect(decideStartWithLiveOwner({ livePort: 10100, requestedPort: 8080, ocxService: "0" })) + .toBe("sibling"); + expect(decideStartWithLiveOwner({ livePort: 10100, requestedPort: undefined, ocxService: "false" })) + .toBe("refuse"); + }); + + test("handleStart routes its live-owner branch through the shared decision", () => { + expect(cliSource).toContain("decideStartWithLiveOwner({"); + // No leftover inline refusal that could bypass the tested decision. + expect(cliSource).not.toContain("explicitSiblingPort"); + }); + test("the probe option still gates on an explicit true", () => { // A truthy-but-not-true default would silently probe for callers that pass // nothing, which is a different behavior than the one asserted above. diff --git a/tests/cli-ready.test.ts b/tests/cli-ready.test.ts index 56a9875b56..388abfb3c7 100644 --- a/tests/cli-ready.test.ts +++ b/tests/cli-ready.test.ts @@ -850,12 +850,15 @@ describe("handleStart OCX_SERVICE exit guard (source-level)", () => { const cliSource = readFileSync(join(import.meta.dir, "../src/cli/index.ts"), "utf8"); test("an already-live proxy exits 0 in OCX_SERVICE context", () => { - expect(cliSource).toMatch(/process\.env\.OCX_SERVICE === "1"/); - expect(cliSource).toMatch(/process\.exit\(0\)/); - const guard = cliSource.match(/if\s*\(process\.env\.OCX_SERVICE === "1"\)\s*\{[\s\S]{0,400}?process\.exit\(0\)/); - expect(guard, "OCX_SERVICE guard must exit 0 when the port is already served").not.toBeNull(); - const nonService = cliSource.match(/Proxy already running[\s\S]{0,200}?process\.exit\(1\)/); - expect(nonService, "non-service path keeps the exit 1 conflict error").not.toBeNull(); + // The `OCX_SERVICE === "1"` comparison moved into `decideStartWithLiveOwner` + // (src/cli/dispatch.ts), where the sentinel semantics are asserted at runtime + // across the whole matrix (tests/cli-dispatch.test.ts). This oracle pins the + // exits that the decision routes to: stay-out exits 0, the conflict exits 1. + expect(cliSource).toMatch(/decideStartWithLiveOwner\(\{/); + const stayOut = cliSource.match(/decision === "service-stay-out"[\s\S]{0,800}?process\.exit\(0\)/); + expect(stayOut, "the service stay-out decision must exit 0 when the port is already served").not.toBeNull(); + const nonService = cliSource.match(/Proxy already running[\s\S]{0,300}?process\.exit\(1\)/); + expect(nonService, "non-service refusal keeps the exit 1 conflict error").not.toBeNull(); }); test("service.ts teardown kills surviving wrapper processes on stop", () => { From 5557772b7d6d11a560f9f910de350ab7cc855866 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 02:29:56 +0900 Subject: [PATCH 122/172] fix(openai): exclude user-owned alias overlays from canonical seed validation (rebase of #3121) (#3189) * fix(openai): exclude user-owned alias overlays from canonical seed validation * fix(openai): preserve alias API ownership on provider writes * test(openai): restore alias regression destination spy --------- Co-authored-by: Flowershangfromthebranches <152056395+Flowershangfromthebranches@users.noreply.github.com> --- src/server/management/provider-routes.ts | 92 ++++++- tests/management-provider-validation.test.ts | 265 +++++++++++++++++++ 2 files changed, 349 insertions(+), 8 deletions(-) diff --git a/src/server/management/provider-routes.ts b/src/server/management/provider-routes.ts index 626148fd0c..c70cf2edcc 100644 --- a/src/server/management/provider-routes.ts +++ b/src/server/management/provider-routes.ts @@ -105,6 +105,63 @@ type ProviderPatchApplication = headersTouched: boolean; }; +const PROVIDER_ALIAS_OVERLAY_FIELDS = ["alias", "modelAliases", "defaultAliases"] as const; +type ProviderAliasOverlayField = typeof PROVIDER_ALIAS_OVERLAY_FIELDS[number]; + +/** + * Alias overlays are owned by the dedicated alias management routes. A full provider POST + * may round-trip an already-persisted value, but it must not create, clear, or change one. + * PATCH is field-masked and rejects these keys outright below. + */ +function providerAliasOverlayOwnershipError( + submitted: Record, + existing: OcxProviderConfig | undefined, +): string | null { + for (const field of PROVIDER_ALIAS_OVERLAY_FIELDS) { + if (!Object.hasOwn(submitted, field)) continue; + if (!existing || !Object.hasOwn(existing, field)) { + return `${field} is managed by the dedicated alias API`; + } + const incoming = submitted[field]; + const persisted = existing[field]; + if (field === "modelAliases") { + if (!isPlainRecord(incoming) || !isPlainRecord(persisted)) { + return "modelAliases is managed by the dedicated alias API"; + } + const incomingEntries = Object.entries(incoming); + const persistedEntries = Object.entries(persisted); + if ( + incomingEntries.length !== persistedEntries.length + || incomingEntries.some(([model, alias]) => typeof alias !== "string" || persisted[model] !== alias) + ) { + return "modelAliases is managed by the dedicated alias API"; + } + continue; + } + if (incoming !== persisted) return `${field} is managed by the dedicated alias API`; + } + return null; +} + +/** Remove only alias overlays whose ownership has already been established by the caller. */ +function providerTransportValidationCandidate(provider: Record): Record { + const candidate = { ...provider }; + for (const field of PROVIDER_ALIAS_OVERLAY_FIELDS) delete candidate[field]; + return candidate; +} + +/** Preserve the authoritative alias values from the stored provider during a full edit. */ +function restorePersistedAliasOverlays(target: OcxProviderConfig, existing: OcxProviderConfig | undefined): void { + for (const field of PROVIDER_ALIAS_OVERLAY_FIELDS) { + delete (target as Record)[field]; + if (!existing || !Object.hasOwn(existing, field)) continue; + const value = existing[field]; + (target as Record)[field] = field === "modelAliases" + ? structuredClone(value) + : value; + } +} + /** * Apply the recognized PATCH field mask onto a provider copy. The caller runs this once * for validation and again inside the config mutation lock against the newest provider, @@ -501,7 +558,10 @@ export async function handleProviderRoutes(ctx: ManagementContext): Promise), + ) ?? providerEmptyToolOutputConfigError(name, provider); if (providerError) return jsonResponse({ error: "provider reload target invalid" }, 409); const namespaceCollision = codexAccountNamespaceProviderCollisionError( @@ -561,12 +621,17 @@ export async function handleProviderRoutes(ctx: ManagementContext): Promise Object.hasOwn(rawBody, field)); + if (aliasField) return jsonResponse({ error: `${aliasField} is managed by the dedicated alias API` }, 400); const hasMode = Object.hasOwn(rawBody, "codexAccountMode"); const hasSetDefault = Object.hasOwn(rawBody, "setDefault"); const canonicalBudgetOnly = name === "openai" @@ -746,7 +816,10 @@ export async function handleProviderRoutes(ctx: ManagementContext): Promise), + ) ?? providerEmptyToolOutputConfigError(name, next); if (providerError) return jsonResponse({ error: providerError }, 400); if (!canonicalBudgetOnly) { @@ -786,7 +859,10 @@ export async function handleProviderRoutes(ctx: ManagementContext): Promise), + ) ?? providerEmptyToolOutputConfigError(name, replay.next); if (syncError) { replayError = syncError; diff --git a/tests/management-provider-validation.test.ts b/tests/management-provider-validation.test.ts index ac976fc785..6149eeefb6 100644 --- a/tests/management-provider-validation.test.ts +++ b/tests/management-provider-validation.test.ts @@ -914,6 +914,271 @@ describe("provider management validation", () => { } }); + test("full provider edit preserves aliases owned by the dedicated APIs", async () => { + if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); + mkdirSync(TEST_DIR, { recursive: true }); + process.env.OPENCODEX_HOME = TEST_DIR; + const overlays = { + alias: "codex-native", + modelAliases: { "gpt-5.6-luna": "luna" }, + defaultAliases: false, + } as const; + saveConfig({ + port: 0, + openaiProviderTierVersion: 2, + defaultProvider: "openai", + providers: { openai: { ...canonicalDirect, ...overlays } }, + } as OcxConfig); + const resolvedError = spyOn(destinationPolicy, "providerDestinationResolvedError").mockResolvedValue(null); + const server = startServer(0); + try { + // The dashboard's full editor omits alias-owned fields. The stored values must survive. + const omitted = await fetch(new URL("/api/providers", server.url), { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ name: "openai", provider: canonicalDirect }), + }); + expect(omitted.status).toBe(200); + expect(loadConfig().providers.openai).toMatchObject(overlays); + + // A full-object client may round-trip the exact stored values, but still does not own them. + const roundTrip = await fetch(new URL("/api/providers", server.url), { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ name: "openai", provider: { ...canonicalDirect, ...overlays } }), + }); + expect(roundTrip.status).toBe(200); + expect(loadConfig().providers.openai).toMatchObject(overlays); + } finally { + resolvedError.mockRestore(); + await server.stop(true); + } + }); + + test("general provider writes cannot introduce a provider alias collision", async () => { + if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); + mkdirSync(TEST_DIR, { recursive: true }); + process.env.OPENCODEX_HOME = TEST_DIR; + saveConfig({ + port: 0, + openaiProviderTierVersion: 2, + defaultProvider: "openai", + providers: { + openai: { ...canonicalDirect }, + deepseek: { adapter: "openai-chat", baseUrl: "https://api.deepseek.com/v1" }, + }, + } as OcxConfig); + const server = startServer(0); + try { + const before = readFileSync(join(TEST_DIR, "config.json")); + const post = await fetch(new URL("/api/providers", server.url), { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ name: "openai", provider: { ...canonicalDirect, alias: "deepseek" } }), + }); + expect(post.status).toBe(400); + + const patch = await fetch(new URL("/api/providers?name=openai", server.url), { + method: "PATCH", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ alias: "deepseek" }), + }); + expect(patch.status).toBe(400); + expect(readFileSync(join(TEST_DIR, "config.json"))).toEqual(before); + expect(loadConfig().providers.openai?.alias).toBeUndefined(); + } finally { + await server.stop(true); + } + }); + + test("general provider writes reject reserved duplicate and invalid model aliases", async () => { + if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); + mkdirSync(TEST_DIR, { recursive: true }); + process.env.OPENCODEX_HOME = TEST_DIR; + saveConfig({ + port: 0, + openaiProviderTierVersion: 2, + defaultProvider: "openai", + providers: { openai: { ...canonicalDirect } }, + } as OcxConfig); + const server = startServer(0); + try { + const before = readFileSync(join(TEST_DIR, "config.json")); + for (const modelAliases of [ + { "gpt-5.6-luna": "gpt-5.6-sol" }, + { "gpt-5.6-sol": "same", "gpt-5.6-luna": "SAME" }, + { "gpt-5.6-luna": "not an alias" }, + ]) { + const response = await fetch(new URL("/api/providers", server.url), { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ name: "openai", provider: { ...canonicalDirect, modelAliases } }), + }); + expect(response.status).toBe(400); + } + const patch = await fetch(new URL("/api/providers?name=openai", server.url), { + method: "PATCH", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ modelAliases: { "gpt-5.6-luna": "luna" } }), + }); + expect(patch.status).toBe(400); + expect(readFileSync(join(TEST_DIR, "config.json"))).toEqual(before); + } finally { + await server.stop(true); + } + }); + + test("malformed alias overlays return bounded 4xx without config persistence", async () => { + if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); + mkdirSync(TEST_DIR, { recursive: true }); + process.env.OPENCODEX_HOME = TEST_DIR; + saveConfig({ + port: 0, + openaiProviderTierVersion: 2, + defaultProvider: "openai", + providers: { openai: { ...canonicalDirect } }, + } as OcxConfig); + const server = startServer(0); + try { + const before = readFileSync(join(TEST_DIR, "config.json")); + for (const overlay of [ + { defaultAliases: "yes" }, + { modelAliases: null }, + { modelAliases: [] }, + { modelAliases: { "gpt-5.6-luna": 42 } }, + ]) { + const post = await fetch(new URL("/api/providers", server.url), { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ name: "openai", provider: { ...canonicalDirect, ...overlay } }), + }); + expect(post.status).toBeGreaterThanOrEqual(400); + expect(post.status).toBeLessThan(500); + + const patch = await fetch(new URL("/api/providers?name=openai", server.url), { + method: "PATCH", + headers: { "content-type": "application/json" }, + body: JSON.stringify(overlay), + }); + expect(patch.status).toBeGreaterThanOrEqual(400); + expect(patch.status).toBeLessThan(500); + } + expect(readFileSync(join(TEST_DIR, "config.json"))).toEqual(before); + } finally { + await server.stop(true); + } + }); + + test("canonical transport tampering stays rejected with persisted alias overlays", async () => { + if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); + mkdirSync(TEST_DIR, { recursive: true }); + process.env.OPENCODEX_HOME = TEST_DIR; + const overlays = { + alias: "codex-native", + modelAliases: { "gpt-5.6-luna": "luna" }, + defaultAliases: true, + } as const; + saveConfig({ + port: 0, + openaiProviderTierVersion: 2, + defaultProvider: "openai", + providers: { openai: { ...canonicalDirect, ...overlays } }, + } as OcxConfig); + const server = startServer(0); + try { + for (const tampering of [ + { baseUrl: "https://attacker.example/backend-api/codex" }, + { adapter: "openai-chat" }, + { authMode: "key" }, + ]) { + const response = await fetch(new URL("/api/providers?name=openai", server.url), { + method: "PATCH", + headers: { "content-type": "application/json" }, + body: JSON.stringify(tampering), + }); + expect(response.status).toBe(400); + } + expect(loadConfig().providers.openai).toMatchObject({ ...canonicalDirect, ...overlays }); + } finally { + await server.stop(true); + } + }); + + test("unrelated non-openai provider edits preserve persisted alias overlays", async () => { + if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); + mkdirSync(TEST_DIR, { recursive: true }); + process.env.OPENCODEX_HOME = TEST_DIR; + saveConfig({ + port: 0, + openaiProviderTierVersion: 2, + defaultProvider: "deepseek", + providers: { + deepseek: { + adapter: "openai-chat", + baseUrl: "https://api.deepseek.com/v1", + alias: "ds", + modelAliases: { "deepseek-v4": "ds4-custom" }, + defaultAliases: false, + }, + }, + } as OcxConfig); + const resolvedError = spyOn(destinationPolicy, "providerDestinationResolvedError").mockResolvedValue(null); + const server = startServer(0); + try { + const response = await fetch(new URL("/api/providers?name=deepseek", server.url), { + method: "PATCH", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ contextWindow: 128000 }), + }); + expect(response.status).toBe(200); + expect(loadConfig().providers.deepseek).toMatchObject({ + alias: "ds", + modelAliases: { "deepseek-v4": "ds4-custom" }, + defaultAliases: false, + contextWindow: 128000, + }); + } finally { + resolvedError.mockRestore(); + await server.stop(true); + } + }); + + test("canonical OpenAI with defaultAliases can still PATCH modelContextWindows", async () => { + if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); + mkdirSync(TEST_DIR, { recursive: true }); + process.env.OPENCODEX_HOME = TEST_DIR; + saveConfig({ + port: 0, + // Match a post-migration config (openaiProviderTierVersion set) so the startup + // openai tier migration does not rewrite the row: this test targets the seed + // comparison, not the one-time legacy migration. + openaiProviderTierVersion: 2, + defaultProvider: "openai", + providers: { + openai: { ...canonicalDirect, defaultAliases: true }, + }, + } as OcxConfig); + // This test targets the seed comparison, not the DNS policy; stub the destination + // probe so the assertion stays independent of how chatgpt.com resolves locally. + const resolvedError = spyOn(destinationPolicy, "providerDestinationResolvedError").mockResolvedValue(null); + + const server = startServer(0); + try { + const patch = await fetch(new URL("/api/providers?name=openai", server.url), { + method: "PATCH", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ modelContextWindows: { "gpt-5.6-luna": 900000 } }), + }); + expect(patch.status).toBe(200); + expect(loadConfig().providers.openai?.modelContextWindows).toEqual({ "gpt-5.6-luna": 900000 }); + // The alias overlay itself must survive the patch untouched. + expect(loadConfig().providers.openai?.defaultAliases).toBe(true); + } finally { + resolvedError.mockRestore(); + await server.stop(true); + } + }); + // #1409: the add/edit form's payload type has no member for contextWindow or test("provider POST overwrite preserves an explicit annotateEmptyToolOutputs: false", async () => { if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); From c87071400b574d8481fdb148fd82370cd52f88d5 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 02:52:35 +0900 Subject: [PATCH 123/172] fix(usage): price aggregator models spelled vendor/model (#3194) CommandCode serves ids like deepseek/deepseek-v4-flash, and the cost catalog stores the bare id. findVendorCostByModelId matches exactly, so the lookup missed a price that was present and every request through such a provider reported no cost at all. Retry on the tail, but only when the prefix agrees with the vendor the matched row belongs to. findVendorCostByModelId returns whichever vendor COST_VENDOR_PRIORITY reaches first, so an unchecked strip would price openai/claude-opus-4-6 from Anthropic's row - a number that looks authoritative and is wrong. Vendor comparison is normalized for dashes and case, because x-ai/grok-4.6 resolves to vendor xai. Nothing beyond that is matched. Closes #3136 Co-authored-by: jun --- src/usage/cost.ts | 33 +++++++++++++++++++++++++++++- tests/usage-cost.test.ts | 44 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/usage/cost.ts b/src/usage/cost.ts index d507668a84..78f6f84092 100644 --- a/src/usage/cost.ts +++ b/src/usage/cost.ts @@ -319,7 +319,8 @@ function resolveModelLevelPrice(provider: string, modelId: string): MatchedPrice // dots where the catalog uses dashes (kiro "claude-opus-4.6" vs anthropic // "claude-opus-4-6"). No fuzzy matching beyond this one normalization. const found = findVendorCostByModelId(modelId) - ?? (modelId.includes(".") ? findVendorCostByModelId(modelId.replaceAll(".", "-")) : undefined); + ?? (modelId.includes(".") ? findVendorCostByModelId(modelId.replaceAll(".", "-")) : undefined) + ?? vendorPrefixedCost(modelId); if (!found) return null; return { provider, @@ -331,6 +332,36 @@ function resolveModelLevelPrice(provider: string, modelId: string): MatchedPrice }; } +/** + * Aggregators spell a model as `/` — CommandCode serves + * `deepseek/deepseek-v4-flash`, and OpenRouter-shaped presets do the same. The cost + * catalog stores the bare id, so the exact lookup above misses a price that is present and + * every request through such a provider reports no cost at all (#3136). + * + * Retrying on the tail is only safe while the prefix AGREES with the vendor the matched row + * belongs to. `findVendorCostByModelId` returns whichever vendor `COST_VENDOR_PRIORITY` + * reaches first, so an unchecked strip would happily price `openai/claude-opus-4-6` from + * Anthropic's row — a number that looks authoritative and is wrong. Requiring agreement + * keeps the failure closed for a genuinely mismatched id. + * + * Comparison is normalized because the same vendor is spelled differently across catalogs: + * `x-ai/grok-4.6` resolves to vendor `xai`. Dashes and case are the only variance seen; + * anything beyond that stays a miss. + */ +function vendorPrefixedCost(modelId: string): ReturnType { + const slash = modelId.indexOf("/"); + if (slash <= 0 || slash === modelId.length - 1) return undefined; + const claimedVendor = modelId.slice(0, slash); + const tail = modelId.slice(slash + 1); + // A tail that is itself slashed is not a vendor prefix we understand; leave it alone. + if (tail.includes("/")) return undefined; + const found = findVendorCostByModelId(tail) + ?? (tail.includes(".") ? findVendorCostByModelId(tail.replaceAll(".", "-")) : undefined); + if (!found) return undefined; + const normalize = (value: string): string => value.toLowerCase().replaceAll("-", ""); + return normalize(found.provider) === normalize(claimedVendor) ? found : undefined; +} + function isEstimated(usage: OcxUsage, usageStatus: UsageStatus, priceStatus: ExpectedPriceStatus | "verified"): boolean { return usage.estimated === true || usageStatus === "estimated" || priceStatus === "verified-derived"; } diff --git a/tests/usage-cost.test.ts b/tests/usage-cost.test.ts index ab313f98be..d97e80ab07 100644 --- a/tests/usage-cost.test.ts +++ b/tests/usage-cost.test.ts @@ -1199,3 +1199,47 @@ describe("provider cost overlay (user-configured)", () => { refreshUserCostOverlays({ providers: {} } as unknown as OcxConfig); }); }); + +describe("aggregator vendor-prefixed model ids (#3136)", () => { + // CommandCode serves "deepseek/deepseek-v4-flash"; the cost catalog stores the bare id. + // The exact lookup missed a price that is present, so every request through such a + // provider reported no cost at all. + test("a vendor-prefixed id resolves to the same price as its bare id", () => { + const bare = resolveMatchedPrice("deepseek", "deepseek-v4-flash"); + const prefixed = resolveMatchedPrice("commandcode-api", "deepseek/deepseek-v4-flash"); + expect(bare?.cost4).toBeDefined(); + expect(prefixed?.cost4).toEqual(bare!.cost4); + // Derived, not claimed as an exact catalog row for that provider. + expect(prefixed?.status).toBe("verified-derived"); + expect(prefixed?.jawcodeProvider).toBe("deepseek"); + }); + + test("the vendor prefix is compared after normalization, so x-ai matches xai", () => { + // The same vendor is spelled differently across catalogs. Dashes and case are the + // only variance this normalizes; anything further stays a miss. + expect(resolveMatchedPrice("openrouter", "x-ai/grok-4.6")?.cost4).toBeDefined(); + }); + + test("a prefix that disagrees with the matched vendor stays unpriced", () => { + // This is the assertion that keeps the fix from becoming a mispricing. + // findVendorCostByModelId returns whichever vendor COST_VENDOR_PRIORITY reaches + // first, so an unchecked strip would price a Claude model from Anthropic's row while + // the caller named OpenAI - a number that looks authoritative and is wrong. + expect(resolveMatchedPrice("openrouter", "openai/claude-opus-4-6")).toBeNull(); + }); + + test("an unknown tail is still unpriced rather than guessed", () => { + expect(resolveMatchedPrice("openrouter", "google/gemini-3.6-pro")).toBeNull(); + }); + + test("unprefixed ids are unchanged", () => { + expect(resolveMatchedPrice("deepseek", "deepseek-v4-flash")?.cost4).toBeDefined(); + expect(resolveMatchedPrice("deepseek", "not-a-real-model-xyz")).toBeNull(); + }); + + test("a doubly-slashed id is not treated as a vendor prefix", () => { + // Only one prefix segment is understood; deeper paths are left alone rather than + // being peeled until something matches. + expect(resolveMatchedPrice("openrouter", "a/deepseek/deepseek-v4-flash")).toBeNull(); + }); +}); From f3bcc67a7dc94254b312d2dec6d54f18dd08d2b3 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 02:59:13 +0900 Subject: [PATCH 124/172] fix(responses): strip ChatGPT citation control markers before the client (#3195) The ChatGPT backend delimits inline citations with private-use characters (U+E200 open, U+E202 separate, U+E201 close). The desktop client renders them as source chips; the Codex TUI prints them literally, so a Copilot-routed answer showed citeturn1view0turn1view1 and the markers persisted into the saved transcript. OpenCodex neither emits nor understands that grammar - it arrives as ordinary assistant text - but the proxy is the last place that can remove it before a client that cannot render it. A span can straddle a delta boundary, so the streaming path uses a stateful filter that withholds an unterminated tail and releases it at close: a stream dying mid-marker must not swallow words the model produced. The accumulated text is stripped separately, because closeCurrentMessage re-sends it in output_text.done, content_part.done and output_item.done. Strip only. The turnNviewN ids are turn-scoped and opaque with no URL mapping in the response, so there is nothing to convert them into. Structured url_citation annotations are untouched, so desktop Sources chips keep working. Closes #3150 Co-authored-by: jun --- src/bridge.ts | 57 ++++++++++++++--- src/responses/citation-markers.ts | 101 ++++++++++++++++++++++++++++++ tests/bridge.test.ts | 53 ++++++++++++++++ tests/citation-markers.test.ts | 90 ++++++++++++++++++++++++++ 4 files changed, 291 insertions(+), 10 deletions(-) create mode 100644 src/responses/citation-markers.ts create mode 100644 tests/citation-markers.test.ts diff --git a/src/bridge.ts b/src/bridge.ts index c1666cb84b..c50cac8c04 100644 --- a/src/bridge.ts +++ b/src/bridge.ts @@ -28,6 +28,11 @@ import { awaitThoughtSignatureDurability, } from "./responses/thought-signature-replay"; import { resolveStallTimeoutSec } from "./stall-timeout"; +import { + createCitationMarkerFilter, + stripCitationMarkers, + type CitationMarkerFilter, +} from "./responses/citation-markers"; import { normalizeDeclaredToolName } from "./types"; import { usageDisplayTotalTokens } from "./usage/totals"; import { appendSafeWebSearchSource, safeWebSearchSources } from "./web-search/sources"; @@ -435,7 +440,14 @@ export function bridgeToResponsesSSE( const stallSec = resolveStallTimeoutSec(options?.stallTimeoutSec); const maxStallTicks = Math.ceil((stallSec * 1000) / heartbeatMs); - let currentMsg: { itemId: string; outputIndex: number; text: string; textBytes: number; phase?: OcxMessagePhase } | null = null; + let currentMsg: { + itemId: string; + outputIndex: number; + text: string; + textBytes: number; + citationFilter: CitationMarkerFilter; + phase?: OcxMessagePhase; + } | null = null; let currentReasoning: { itemId: string; outputIndex: number; text: string; textBytes: number } | null = null; let currentRawReasoning: { itemId: string; outputIndex: number; text: string; textBytes: number } | null = null; // Anthropic extended-thinking round-trip state: the signature signs the CURRENT thinking @@ -565,6 +577,17 @@ export function bridgeToResponsesSSE( const closeCurrentMessage = (inferredPhase?: OcxMessagePhase) => { if (!currentMsg) return; + // Release anything the citation filter was holding for this message, then strip the + // accumulated text: closeCurrentMessage re-sends it in output_text.done and + // output_item.done, so filtering only the deltas would leave the markers in both. + const trailing = currentMsg.citationFilter.flush(); + if (trailing) { + emit("response.output_text.delta", { + item_id: currentMsg.itemId, output_index: currentMsg.outputIndex, + content_index: 0, delta: trailing, + }); + } + const messageText = stripCitationMarkers(currentMsg.text); // Chat Completions has no message-phase field. Keep its live item provisional, then // classify it only when the next adapter event proves whether this text led into more // work or completed the turn. Explicit adapter phases always outrank this inference. @@ -574,15 +597,15 @@ export function bridgeToResponsesSSE( // Finalize the text part (Responses protocol). Without these .done events Codex never // commits the content part and renders the message as truncated / cut off. emit("response.output_text.done", { - item_id: currentMsg.itemId, output_index: currentMsg.outputIndex, content_index: 0, text: currentMsg.text, + item_id: currentMsg.itemId, output_index: currentMsg.outputIndex, content_index: 0, text: messageText, }); emit("response.content_part.done", { item_id: currentMsg.itemId, output_index: currentMsg.outputIndex, content_index: 0, - part: { type: "output_text", text: currentMsg.text, annotations }, + part: { type: "output_text", text: messageText, annotations }, }); const item = { type: "message", id: currentMsg.itemId, status: "completed", role: "assistant", - content: [{ type: "output_text", text: currentMsg.text, annotations }], + content: [{ type: "output_text", text: messageText, annotations }], ...(phase ? { phase } : {}), }; emit("response.output_item.done", { output_index: currentMsg.outputIndex, item }); @@ -936,7 +959,11 @@ export function bridgeToResponsesSSE( item_id: itemId, output_index: outputIndex, content_index: 0, part: { type: "output_text", text: "", annotations: [] }, }); - currentMsg = { itemId, outputIndex, text: "", textBytes: 0, ...(event.phase ? { phase: event.phase } : {}) }; + currentMsg = { + itemId, outputIndex, text: "", textBytes: 0, + citationFilter: createCitationMarkerFilter(), + ...(event.phase ? { phase: event.phase } : {}), + }; } ({ value: currentMsg.text, bytes: currentMsg.textBytes } = appendString( currentMsg.text, @@ -944,10 +971,16 @@ export function bridgeToResponsesSSE( event.text, "retained_collectors", )); - emit("response.output_text.delta", { - item_id: currentMsg.itemId, output_index: currentMsg.outputIndex, - content_index: 0, delta: event.text, - }); + // A citation span can straddle a delta boundary, so the filter withholds an + // unterminated tail and releases it at close (#3150). The accumulator above + // keeps the raw text; it is stripped once in closeCurrentMessage. + const visible = currentMsg.citationFilter.push(event.text); + if (visible) { + emit("response.output_text.delta", { + item_id: currentMsg.itemId, output_index: currentMsg.outputIndex, + content_index: 0, delta: visible, + }); + } break; } case "thinking_delta": { @@ -1621,6 +1654,10 @@ function buildResponseJSONWithBudget( const flushText = (inferredPhase?: OcxMessagePhase) => { if (!currentText) return; const phase = currentTextPhase ?? inferredPhase; + // ChatGPT-backend citation markers arrive as literal private-use characters that the + // Codex TUI prints verbatim (#3150). Strip them here rather than at the accumulator so + // the retained byte accounting above still describes what the upstream actually sent. + const text = stripCitationMarkers(currentText); const sourceBytes = pendingWebSources.reduce((sum, source) => sum + bytesOf(JSON.stringify(source)), 0); const annotations = pendingWebSources.map(s => ({ type: "url_citation", url: s.url, ...(s.title ? { title: s.title } : {}), start_index: 0, end_index: 0, @@ -1628,7 +1665,7 @@ function buildResponseJSONWithBudget( pendingWebSources = []; const item = { type: "message", id: `msg_${uuid()}`, role: "assistant", status: "completed", - content: [{ type: "output_text", text: currentText, annotations }], + content: [{ type: "output_text", text, annotations }], ...(phase ? { phase } : {}), } as OutputItem; pushOutput(item, currentTextBytes); diff --git a/src/responses/citation-markers.ts b/src/responses/citation-markers.ts new file mode 100644 index 0000000000..5fe58142cf --- /dev/null +++ b/src/responses/citation-markers.ts @@ -0,0 +1,101 @@ +/** + * ChatGPT-backend citation markers. + * + * The ChatGPT backend delimits inline citations with Unicode private-use characters: + * + * \uE200 cite \uE202 turn1view0 \uE202 turn1view1 \uE201 + * + * The desktop client renders that as source chips. The Codex TUI does not: it prints the + * codepoints literally, so the user sees "citeturn1view0turn1view1" in the answer and in + * the saved transcript (#3150). + * + * OpenCodex neither produces nor understands this grammar — it arrives as ordinary + * assistant text from a ChatGPT-derived backend (GitHub Copilot in the report). The proxy + * is the last place that can remove it before a client that cannot render it. + * + * Strip, do not translate. The `turnNviewN` ids are turn-scoped and opaque, and the + * response carries no mapping from them to a URL, so there is nothing to convert them + * into. Structured `url_citation` annotations are a separate path and are untouched. + */ + +/** Opens a citation span. */ +export const CITATION_MARKER_START = "\uE200"; +/** Separates the `cite` keyword and each source reference inside a span. */ +export const CITATION_MARKER_SEPARATOR = "\uE202"; +/** Closes a citation span. */ +export const CITATION_MARKER_END = "\uE201"; + +/** True when the text contains any of the three delimiters. Cheap pre-check. */ +export function hasCitationMarker(text: string): boolean { + return text.includes(CITATION_MARKER_START) + || text.includes(CITATION_MARKER_SEPARATOR) + || text.includes(CITATION_MARKER_END); +} + +/** + * Remove every complete `START … END` span from a whole string. + * + * A START with no END is left alone rather than truncating the remainder: an unterminated + * marker is malformed input, and dropping everything after it would delete real answer + * text. A stray SEPARATOR or END outside a span is also left alone for the same reason — + * this function only removes what it can prove is a citation span. + */ +export function stripCitationMarkers(text: string): string { + if (!text.includes(CITATION_MARKER_START)) return text; + let out = ""; + let index = 0; + for (;;) { + const start = text.indexOf(CITATION_MARKER_START, index); + if (start === -1) { + out += text.slice(index); + return out; + } + const end = text.indexOf(CITATION_MARKER_END, start + 1); + if (end === -1) { + // Unterminated: keep the rest verbatim. + out += text.slice(index); + return out; + } + out += text.slice(index, start); + index = end + 1; + } +} + +export interface CitationMarkerFilter { + /** Feed one streaming delta; returns the portion safe to emit now. */ + push(delta: string): string; + /** Release anything still held when the message closes. */ + flush(): string; +} + +/** + * Streaming filter. + * + * A marker can straddle a delta boundary — `\uE200cite` in one chunk and the rest in the + * next — so a stateless per-delta strip would emit the tail of a span it never recognized. + * This holds back the text from an unterminated START and releases it once the END arrives + * (removed) or the stream ends (verbatim, so nothing the model actually said is lost). + */ +export function createCitationMarkerFilter(): CitationMarkerFilter { + // Text from an open START that has not been terminated yet. + let held = ""; + return { + push(delta: string): string { + const combined = held + delta; + held = ""; + const start = combined.lastIndexOf(CITATION_MARKER_START); + if (start === -1) return stripCitationMarkers(combined); + const endAfterStart = combined.indexOf(CITATION_MARKER_END, start + 1); + if (endAfterStart !== -1) return stripCitationMarkers(combined); + // The trailing span is still open: emit everything before it, hold the rest. + held = combined.slice(start); + return stripCitationMarkers(combined.slice(0, start)); + }, + flush(): string { + const rest = held; + held = ""; + return rest; + }, + }; +} + diff --git a/tests/bridge.test.ts b/tests/bridge.test.ts index f031ce8f8b..2d433f9467 100644 --- a/tests/bridge.test.ts +++ b/tests/bridge.test.ts @@ -1253,6 +1253,59 @@ describe("Responses bridge web_search_call native item", () => { }); }); +describe("citation markers never reach the client (#3150)", () => { + const S = "\uE200"; + const P = "\uE202"; + const E = "\uE201"; + + test("a span split across text deltas is absent from every emitted event", async () => { + // End-to-end through the real bridge, not just the filter. closeCurrentMessage re-sends + // the accumulated text in output_text.done, content_part.done and output_item.done, so + // filtering only the deltas would still leak the markers into the saved transcript. + const events = await collectSse(bridgeToResponsesSSE(replay([ + { type: "text_delta", text: `The setting is supported. ${S}cite${P}` }, + { type: "text_delta", text: `turn1view0${P}turn1view1${E}` }, + { type: "text_delta", text: " Next sentence." }, + { type: "done" }, + ]), "routed/model")); + + const serialized = JSON.stringify(events); + expect(serialized).not.toContain(S); + expect(serialized).not.toContain(P); + expect(serialized).not.toContain(E); + expect(serialized).not.toContain("turn1view0"); + + const streamed = events + .filter(e => e.event === "response.output_text.delta") + .map(e => e.data.delta as string) + .join(""); + expect(streamed).toBe("The setting is supported. Next sentence."); + + const done = events.find(e => e.event === "response.output_text.done"); + expect(done?.data.text).toBe("The setting is supported. Next sentence."); + }); + + test("a stream ending inside a span still delivers the held text", async () => { + // Withhold, not drop: an unterminated marker is malformed input, and swallowing it + // would delete words the model actually produced. + const events = await collectSse(bridgeToResponsesSSE(replay([ + { type: "text_delta", text: `partial ${S}cite${P}turn1` }, + { type: "done" }, + ]), "routed/model")); + const done = events.find(e => e.event === "response.output_text.done"); + expect(done?.data.text).toContain("partial "); + }); + + test("ordinary text is untouched", async () => { + const events = await collectSse(bridgeToResponsesSSE(replay([ + { type: "text_delta", text: "plain answer" }, + { type: "done" }, + ]), "routed/model")); + const done = events.find(e => e.event === "response.output_text.done"); + expect(done?.data.text).toBe("plain answer"); + }); +}); + describe("Responses bridge stopReason threading (issue #246)", () => { test("done with stopReason max_tokens emits response.incomplete", async () => { const frames = await collectSse(bridgeToResponsesSSE(replay([ diff --git a/tests/citation-markers.test.ts b/tests/citation-markers.test.ts new file mode 100644 index 0000000000..726585457c --- /dev/null +++ b/tests/citation-markers.test.ts @@ -0,0 +1,90 @@ +import { describe, expect, test } from "bun:test"; +import { + CITATION_MARKER_END, + CITATION_MARKER_SEPARATOR, + CITATION_MARKER_START, + createCitationMarkerFilter, + hasCitationMarker, + stripCitationMarkers, +} from "../src/responses/citation-markers"; + +/** + * #3150: the ChatGPT backend delimits inline citations with private-use characters + * (U+E200 open, U+E202 separate, U+E201 close). The desktop client renders them as source + * chips; the Codex TUI prints them literally, so the user saw + * "citeturn1view0turn1view1" in the answer and in the saved transcript. + * + * OpenCodex neither emits nor understands the grammar - it is upstream text passing + * through - so the proxy strips it before a client that cannot render it. + */ + +const S = CITATION_MARKER_START; +const P = CITATION_MARKER_SEPARATOR; +const E = CITATION_MARKER_END; +const span = `${S}cite${P}turn1view0${P}turn1view1${E}`; + +describe("citation marker stripping (#3150)", () => { + test("a complete span is removed and the surrounding text survives", () => { + expect(stripCitationMarkers(`The setting is supported. ${span} Next.`)) + .toBe("The setting is supported. Next."); + }); + + test("several spans in one message are all removed", () => { + expect(stripCitationMarkers(`a${span}b${S}cite${P}turn2view0${E}c`)).toBe("abc"); + }); + + test("text with no markers is returned unchanged", () => { + // The common case must not be rewritten at all. + const plain = "ordinary answer text with no private-use characters"; + expect(stripCitationMarkers(plain)).toBe(plain); + expect(hasCitationMarker(plain)).toBe(false); + }); + + test("an unterminated span keeps its text instead of truncating the answer", () => { + // Malformed input must not delete everything after the opening marker: that would + // silently drop real answer text. + // The opening marker is kept too: without a terminator there is no proof this is a + // citation span at all, so the input is returned verbatim rather than partly rewritten. + expect(stripCitationMarkers(`tail ${S}cite${P}turn1`)).toBe(`tail ${S}cite${P}turn1`); + }); + + test("a stray separator or terminator alone is left alone", () => { + expect(stripCitationMarkers(`a${P}b`)).toBe(`a${P}b`); + expect(stripCitationMarkers(`a${E}b`)).toBe(`a${E}b`); + }); +}); + +describe("streaming citation marker filter (#3150)", () => { + const drain = (chunks: readonly string[]): string => { + const filter = createCitationMarkerFilter(); + let out = ""; + for (const chunk of chunks) out += filter.push(chunk); + return out + filter.flush(); + }; + + test("a span split across deltas is removed, not leaked", () => { + // The case a stateless per-delta strip gets wrong: the opening marker arrives in one + // chunk and the terminator in the next, so the tail would be emitted unrecognized. + expect(drain([`The setting is supported. ${S}cite${P}`, `turn1view0${P}turn1view1${E}`, " Next."])) + .toBe("The setting is supported. Next."); + }); + + test("a span split one character at a time is still removed", () => { + expect(drain([...`ok ${span} done`])).toBe("ok done"); + }); + + test("a stream ending mid-span releases the held text rather than swallowing it", () => { + // Withhold, not drop: if the stream dies inside a marker the bytes still reach the user. + expect(drain([`abc ${S}cite${P}turn1`])).toBe(`abc ${S}cite${P}turn1`); + }); + + test("marker-free deltas pass through byte-identical", () => { + expect(drain(["hello ", "world", "!"])).toBe("hello world!"); + }); + + test("text before an open span is emitted immediately, not held to the end", () => { + // Streaming must stay streaming: only the unterminated span is withheld. + const filter = createCitationMarkerFilter(); + expect(filter.push(`visible now ${S}cite`)).toBe("visible now "); + }); +}); From 4be4326d7d3525764b06d7cd5f01ec70cc350d6f Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 03:05:06 +0900 Subject: [PATCH 125/172] docs(devlog): drop remote home-path citations the privacy scanner flags (#3197) * docs(devlog): plan the admin merge train for 3190 Roadmap unit for landing the adaptive-effort PR after the privacy-scan citation on origin/dev is anonymized. Docs-only; no production change. * docs(devlog): record verifier one-liners for the 3190 train Fold the PLAN-VERIFIER-REAL-01 audit blocker into the roadmap with live privacy:scan and gh exit evidence. Docs-only. * docs(devlog): drop remote home-path citations the privacy scanner flags --------- Co-authored-by: jun --- .../_plan/260902_admin_merge_3190/000_plan.md | 92 +++++++++++++++++++ .../002_audit_round1.md | 18 ++++ .../010_wp1_anonymize_home_paths.md | 63 +++++++++++++ .../011_wp1_stale_check.md | 3 + .../020_wp2_rebase_merge_3190.md | 62 +++++++++++++ .../030_wp3_inventory_refresh.md | 64 +++++++++++++ .../091_wp6_merge_outcome.md | 4 +- 7 files changed, 304 insertions(+), 2 deletions(-) create mode 100644 devlog/_plan/260902_admin_merge_3190/000_plan.md create mode 100644 devlog/_plan/260902_admin_merge_3190/002_audit_round1.md create mode 100644 devlog/_plan/260902_admin_merge_3190/010_wp1_anonymize_home_paths.md create mode 100644 devlog/_plan/260902_admin_merge_3190/011_wp1_stale_check.md create mode 100644 devlog/_plan/260902_admin_merge_3190/020_wp2_rebase_merge_3190.md create mode 100644 devlog/_plan/260902_admin_merge_3190/030_wp3_inventory_refresh.md diff --git a/devlog/_plan/260902_admin_merge_3190/000_plan.md b/devlog/_plan/260902_admin_merge_3190/000_plan.md new file mode 100644 index 0000000000..3ae31395ac --- /dev/null +++ b/devlog/_plan/260902_admin_merge_3190/000_plan.md @@ -0,0 +1,92 @@ +# 000 — admin-merge remaining ready PRs, starting with #3190 + +Frozen at `origin/dev` = `5557772b7` (after #3189), 2026-09-02T02:40Z. +Worktree: `codex/260902-admin-merge-3190` tracking `origin/dev`. +Session: `01a05b23-083f-7413-a4d8-159a2ff4e2a1`. + +## Loop-spec + +- Archetype: HOTL maintainer merge train. One work-phase per PABCD cycle. +- Trigger: user said merge remaining ready work with admin, `--no-verify` pushes, no local full suite. +- Goal: land #3190 on `dev`, close superseded #2734, then refresh the live non-draft inventory and land only authorized mechanical leftovers. +- Non-goals: new product features, remote host QA, PDF/guide work, merging drafts, merging conflicting PRs, merging security-boundary PRs without a named security review, local `bun run test`. +- Verifier: `gh pr checks` on the exact head SHA (full rollup, not `--required` empty), then `git fetch origin && git merge-base --is-ancestor FETCH_HEAD`. Privacy repair also needs `bun run privacy:scan` exit 0. +- Stop: DONE when the inventory refresh finds no remaining authorized MERGEABLE item; BLOCKED if privacy/CI cannot be repaired without a new product change; UNSAFE if an auth/credential/workflow/release/dependency PR would land without security review. +- Memory: this unit. Goalplan slug `admin-merge-remaining-ready-opencodex-prs-onto-o`. +- Escalation: stop for a missing owner choice between two overlapping feature PRs that are not a documented carry. +- Resource bounds: this worktree + `gh`; serialize pushes/merges; unlimited `xai/grok-4.6` read-only reviewers already authorized. + +## Class + +C4 for the merge itself (protected `dev`, admin bypass). The only production-adjacent write in this train is the privacy-scan text fix in wp1. #3190's unique commits already exist; wp2 rebases and lands them. + +## Why #3190 is first + +It is the only current non-draft, MERGEABLE, maintainer-authored feature PR that is not `CHANGES_REQUESTED` and not a stale carry. Head `5f8cd24dd` is two unique commits on merge-base `e40245e4c` (#3169). It is 19 commits behind `origin/dev`. Cross-platform CI `gates` already failed on Privacy scan because GitHub merges that head with current `dev`, and current `dev` contains two remote-macOS home citations in `devlog/_plan/260902_multiplatform_qa_and_gui/091_wp6_merge_outcome.md`. + +The scan only flags the macOS home-path shape. POSIX home prefixes and the Windows npm prefix that uses the allowed username `user` are a different detector. Allowed usernames in `devlog/` are the maintainer account plus `u` / `user` / `me` / `test`. The two remote macOS usernames in 091 are none of those. + +## Why the other non-drafts are not in this train + +| PR | Disposition | Reason | +| --- | --- | --- | +| #3142 | DEFER | CONFLICTING, CHANGES_REQUESTED | +| #3061 | DEFER | MERGEABLE but CHANGES_REQUESTED; macos/ci red; prior train already parked it | +| #2986 | DEFER | carry of #2083, CHANGES_REQUESTED; do not merge both | +| #2877 | DEFER | docs closeout, CHANGES_REQUESTED | +| #2805 | DEFER | CONFLICTING | +| #2783 | DEFER | CONFLICTING, CHANGES_REQUESTED | +| #2527 | DEFER | CONFLICTING, CHANGES_REQUESTED | +| #2366 | DEFER | MERGEABLE but CHANGES_REQUESTED, contributor feature | +| #2083 | DEFER | APPROVED original of the #2986 carry; merging both is forbidden | +| #2734 | CLOSE after #3190 | draft, CONFLICTING, superseded by #3190 | + +wp3 re-reads this table live. A new MERGEABLE non-conflicted item that appears after #3190 can be appended; shrinking the table to escape the loop is forbidden. + +## Work-phase map (dependency-ordered) + +``` +wp0 this unit (docs-only) -> 000 + 010 + 020 + 030 + ├── wp1 anonymize leaked remote home paths -> 010 + ├── wp2 rebase + exact-head CI + admin-merge 3190, close 2734 -> 020 + └── wp3 refresh leftover inventory -> 030 +``` + +Stack decision (`DEV-STACK-01`): do **not** stack wp1 under #3190. wp1 is a one-file text fix that every later PR inherits once it is on `dev`. Landing it first, then rebasing #3190 onto that tip, is cheaper than a mid-stack cascade. wp2 and wp3 are sequential because each merge invalidates the next candidate's merge-base. + +## Scope boundary + +**IN** + +- Text-only anonymization of the two remote macOS home citations in 091 (and 020 if the Windows path is also a forbidden home-path hit). +- Rebase of #3190 unique commits onto current `origin/dev` after wp1 lands. +- `--no-verify` push of the rebase branch, exact-head CI, authorized admin squash merge. +- Close #2734 with credit after #3190 is an ancestor of `origin/dev`. +- Live refresh of open non-draft PRs; admin-merge only items that are MERGEABLE, not conflicting, not CHANGES_REQUESTED without a documented carry, and not security-boundary. + +**OUT** + +- Local full suite. +- Direct push to `dev`/`main`/`preview`. +- Merging #2083 and #2986 together. +- Re-implementing review blockers on parked PRs. +- Any auth, credential, workflow, release, or dependency-install change. + +## Verifier commands that actually exist + +- `bun run privacy:scan` -> `scripts/privacy-scan.ts` (reads `git ls-files`, including 091). Live run on HEAD `befefeb20` **exit 1**. Hits 091 line 13, two remote macOS homes. This is the wp1 red proof. After wp1 the same command must be exit 0 and name no 091 line. +- `gh pr view 3190 --json number,headRefOid,mergeable` live at freeze: `{"head":"5f8cd24ddf01082f35079c695a810324c33f4b3e","mergeable":"MERGEABLE","n":3190,"state":"OPEN"}` exit 0. Reads GitHub PR 3190, not the local 091 file. +- `gh pr checks 3190` live: `gates` fail (Privacy scan, job 99959406196, run 33538646261). Reads the exact-head check rollup for `5f8cd24dd`. +- `git merge-base --is-ancestor e40245e4c origin/codex/adaptive-reasoning-effort-2731` is true (merge-base of 3190). After merge, the command becomes `git fetch origin && git merge-base --is-ancestor origin/dev` and must exit 0. + +Deferred non-draft freeze (same `gh pr list --state open` pass): #3142 CONFLICTING+CHANGES_REQUESTED, #3061 MERGEABLE+CHANGES_REQUESTED with macos/ci red, #2986 carry of #2083 CHANGES_REQUESTED, #2877 CHANGES_REQUESTED, #2805/#2783/#2527 CONFLICTING, #2366 CHANGES_REQUESTED, #2083 APPROVED original of the carry, #2734 draft CONFLICTING. + +No `bun run test`. Focused tests only if wp2's rebase conflict touches `src/` or `tests/` unexpectedly. + +## Field chain (PLAN-FIELD-CHAIN-01) + +No new runtime field. N/A: this train does not add config/API keys. #3190 already added `reasoningEffortMode` and `omitReasoningEffortWithToolsModels` on its own branch; wp2 lands that existing chain, it does not invent a second one. + +## Bypass named (PLAN-BYPASS-NAMED-01) + +Admin squash merge is the named bypass of required maintainer approval on owner-authored PRs. It does not bypass: exact-head CI evidence, `enforce-target`, privacy:scan, or security review for security-boundary diffs. Record the bypass rationale on each merge comment. diff --git a/devlog/_plan/260902_admin_merge_3190/002_audit_round1.md b/devlog/_plan/260902_admin_merge_3190/002_audit_round1.md new file mode 100644 index 0000000000..e7d21d3f00 --- /dev/null +++ b/devlog/_plan/260902_admin_merge_3190/002_audit_round1.md @@ -0,0 +1,18 @@ +# 002 — audit round 1 synthesis + +Reviewer: subagent Arendt (`01a05e18-6441-7251-b417-2aacda38462e`), `$codexclaw:cxc-dev-code-reviewer` + `$codexclaw:cxc-search`. + +`VERDICT: GO-WITH-FIXES (blockers=1)` + +## Blocker 1 (High) — folded + +PLAN-VERIFIER-REAL-01: 000 listed verifier commands without exit codes or reads-target proof. Folded into `000_plan.md` "Verifier commands that actually exist": + +- `bun run privacy:scan` live exit 1 on `befefeb20`, hits 091 line 13, reads `git ls-files`. +- `gh pr view 3190` live exit 0, MERGEABLE, head `5f8cd24dd`. +- `gh pr checks 3190` live: gates Privacy scan fail, run 33538646261 job 99959406196. +- merge-base of 3190 is `e40245e4c`; post-merge command named. + +No residual High/Critical blockers. Non-blocking: stacking decision and deferred-PR table were confirmed sound. + +Main-agent judgment: near-pass. Residual: none after the fold. diff --git a/devlog/_plan/260902_admin_merge_3190/010_wp1_anonymize_home_paths.md b/devlog/_plan/260902_admin_merge_3190/010_wp1_anonymize_home_paths.md new file mode 100644 index 0000000000..e5a6cd0009 --- /dev/null +++ b/devlog/_plan/260902_admin_merge_3190/010_wp1_anonymize_home_paths.md @@ -0,0 +1,63 @@ +# 010 — wp1: anonymize leaked remote home paths on origin/dev + +Depends on: wp0 (this unit exists). Independent of #3190's unique commits. + +## Defect + +`scripts/privacy-scan.ts` matches `/Users//` and fails any username that is not the maintainer account or the allowlist `u` / `user` / `me` / `test`. After #3181, `devlog/_plan/260902_multiplatform_qa_and_gui/091_wp6_merge_outcome.md` quotes two remote macOS home prefixes as the example of what the scan caught. That citation re-introduces the same shape, so every later PR whose GitHub merge commit includes current `dev` fails `gates` / Privacy scan. This is why #3190's matrix is red even though #3190 itself does not contain that file. + +CI evidence (Cross-platform CI run 33538646261, job 99959406196, head `5f8cd24dd`): + +``` +Privacy scan failed: +devlog/_plan/260902_multiplatform_qa_and_gui/091_wp6_merge_outcome.md:13 home-path: /Users// +devlog/_plan/260902_multiplatform_qa_and_gui/091_wp6_merge_outcome.md:13 home-path: /Users// +``` + +Do not paste the real usernames into this unit. The scanner would fail this file the same way. + +A second candidate is line 29 of `020_wp3_wp5_deploy_qa.md`, the Windows npm prefix under `/c/Users/user/...`. Username `user` is allowed. Confirm with a live `bun run privacy:scan` rather than assuming; if it is clean, leave 020 untouched. + +## Diff (MODIFY only) + +File: `devlog/_plan/260902_multiplatform_qa_and_gui/091_wp6_merge_outcome.md` + +Before (line 13-15, sense only — do not restore the forbidden shape): + +``` +두 번째가 제일 의미 있다. 문서에 , +, 를 실측 그대로 적었는데, 그건 다른 사람의 +홈 경로다. 스캔이 정당하게 잡았고 `~/`로 바꿨다. +``` + +After: + +``` +두 번째가 제일 의미 있다. 문서에 원격 macOS 홈 경로 두 개, +POSIX 홈, Windows npm 접두를 실측 그대로 적었는데, 그건 다른 사람의 +홈 경로다. 스캔이 정당하게 잡았고 `~/`로 바꿨다. +``` + +No other files. Do not edit `scripts/privacy-scan.ts` to widen the allowlist. The detector is correct; the citation is the bug. + +## Steps + +1. `git fetch origin && git switch -C codex/260902-privacy-091 origin/dev` if the current branch already carries later work; otherwise stay on `codex/260902-admin-merge-3190` while it still equals `origin/dev` plus this unit's docs. +2. Apply the 091 edit. Confirm `git grep -n '/Users/' -- devlog/_plan/260902_multiplatform_qa_and_gui` no longer prints a forbidden username. +3. `bun run privacy:scan` — exit 0. If it still names 091, the replacement still matches the regex; rewrite again without the `/Users//` shape. +4. Commit: `docs(devlog): drop remote home-path citations the privacy scanner flags`. +5. Push `--no-verify`. Open a PR targeting `dev`. Fill the template. This PR does not mention `gui` in title or body, so no screenshot gate. +6. Exact-head CI. `gates` / Privacy scan must be SUCCESS on this head. Other jobs may still be in flight; do not merge on a red privacy scan. +7. Admin squash merge with rationale: docs-only, privacy-scan self-repair, no production surface. +8. Proof: `git fetch origin && git merge-base --is-ancestor origin/dev`. + +## Accept + +- `bun run privacy:scan` exit 0 on the repair head. +- 091 no longer contains a `/Users//` token. +- The merge commit is an ancestor of `origin/dev`. +- `scripts/privacy-scan.ts` is unchanged. + +## Activation scenario (C-ACTIVATION-GROUNDING-01) + +Trigger: run `bun run privacy:scan` on a tree that includes the edited 091. Observable: stdout `Privacy scan passed`, exit 0. Negative: restoring the old 091 line must fail again — do not restore it; the CI log of run 33538646261 is the red proof. diff --git a/devlog/_plan/260902_admin_merge_3190/011_wp1_stale_check.md b/devlog/_plan/260902_admin_merge_3190/011_wp1_stale_check.md new file mode 100644 index 0000000000..e3f14e6a49 --- /dev/null +++ b/devlog/_plan/260902_admin_merge_3190/011_wp1_stale_check.md @@ -0,0 +1,3 @@ +# 011 — wp1 stale check against current tree + +Rebased this branch onto `origin/dev` = `c87071400` (#3194) before wp1 implementation. 091 is unchanged: line 13 still has the two remote macOS home-path tokens that `bun run privacy:scan` reports. 020's Windows npm prefix uses allowed username `user` and is not a scan hit. 010's replacement text is still valid; no line-number drift. diff --git a/devlog/_plan/260902_admin_merge_3190/020_wp2_rebase_merge_3190.md b/devlog/_plan/260902_admin_merge_3190/020_wp2_rebase_merge_3190.md new file mode 100644 index 0000000000..80aa537b47 --- /dev/null +++ b/devlog/_plan/260902_admin_merge_3190/020_wp2_rebase_merge_3190.md @@ -0,0 +1,62 @@ +# 020 — wp2: rebase, exact-head CI, admin-merge #3190, close #2734 + +Depends on: wp1 landed on `origin/dev` so a GitHub merge of this head no longer inherits the 091 privacy failure. + +## What #3190 is + +PR #3190, author `lidge-jun`, branch `codex/adaptive-reasoning-effort-2731`, targets `dev`. +Two unique commits on merge-base `e40245e4c` (#3169): + +- `e1fc1729b` feat(combo): adapt reasoning effort to target capabilities +- `5f8cd24dd` test(combo): cover adaptive effort mode and the tool-bearing opt-out + +35 files. Completes #2731. Supersedes draft #2734. Opt-in `reasoningEffortMode: "adaptive"` (default remains `"strict"`) plus `omitReasoningEffortWithToolsModels` on openai-chat, plus the dashboard round-trip #2734 left open. + +Not a security-boundary PR: no auth, credential, workflow, release, or dependency-install change. Admin merge still needs exact-head CI, not an empty `--required` list. + +## Why rebase, not merge-as-is + +Head is 19 commits behind `origin/dev` at freeze. GitHub merge with current `dev` is what made Privacy scan fail. After wp1, rebase onto the new `origin/dev` so: + +1. the unique two commits sit on the privacy-clean tip; +2. later landings (#3172 combo default effort, #3175 failover e2e assertion, #3189 alias overlay, …) are in the base rather than conflicted at merge time. + +Do not force-push the original contributor-looking branch if a rebase rewrite is cleaner as a new maintainer branch. Prefer: + +``` +git fetch origin +git switch -C codex/adaptive-reasoning-effort-2731-rebased origin/dev +git cherry-pick e1fc1729b 5f8cd24dd +``` + +If cherry-pick is clean, push `--no-verify` and either retarget #3190's head or open a carry PR that closes #3190. If #3190 still points at the old branch and `maintainerCanModify` is ourselves, pushing the same branch after rebase is allowed; use `--force-with-lease` only on that topic branch, never on `dev`. + +Conflict policy: stop and inspect. Likely touch points are combo catalog / openai-chat / GUI combo serializer because #3172 already landed combo default-effort behavior. Do not silently drop #3190 tests. + +## PR hygiene + +Title/body mention combo GUI. `enforce-target` requires a screenshot of the UI change. #3190 already carries a placeholder image; after rebase confirm the body still has Summary / Verification / Checklist and a real screenshot, not a 1×1 dummy. If the dummy is still there, replace it with a captured Capabilities-section shot from a local GUI build (no full suite). + +## Steps + +1. Confirm wp1 merge is an ancestor of `origin/dev`. +2. Cherry-pick or rebase the two unique commits onto that tip. +3. If conflicts: resolve against current combo/openai-chat/GUI code; keep both the adaptive-mode behavior and the #3172 default-effort behavior. +4. Focused checks only: `bun x tsc --noEmit`; `cd gui && bun x tsc --noEmit` if GUI files changed; `bun test tests/codex-catalog.test.ts tests/openai-chat-hardening.test.ts tests/combo-management-api.test.ts tests/combo-workspace-data.test.ts tests/combos.test.ts tests/management-provider-validation.test.ts` if those files still exist after rebase; `bun run privacy:scan`. +5. Push `--no-verify`. Refresh #3190 (or open the carry). Fill the template. +6. Wait for exact-head Cross-platform CI on the new SHA. Record the run id. `gates` Privacy scan must be SUCCESS. Known macOS websocket flake: rerun that job, compare against #3128, do not rewrite unrelated code. +7. Admin squash merge: `gh pr merge --squash --admin --delete-branch` with comment naming the bypass (owner-authored, CI green on exact head, no security-boundary). +8. Proof: `git fetch origin && git merge-base --is-ancestor origin/dev`. +9. Close #2734 with a comment: superseded by the landed #3190 merge SHA. Close #2731 only if the landed PR says Closes and the issue is still open — `dev` is not the default branch, so GitHub will not auto-close; close manually if the PR claims it. + +## Accept + +- Unique #3190 behavior is on `origin/dev` (adaptive mode + tool-bearing omit + GUI round-trip). +- Exact-head CI rollup for the merged SHA is recorded, including `gates` SUCCESS. +- `git merge-base --is-ancestor origin/dev` is true. +- #2734 is closed with credit. +- This worktree is not left on a deleted remote branch (switch back to a live topic or `origin/dev` tracking branch after delete). + +## Activation + +Trigger: after merge, `git fetch origin && git merge-base --is-ancestor origin/dev`; exit 0. Negative: if the merge commit is missing, do not claim DONE. diff --git a/devlog/_plan/260902_admin_merge_3190/030_wp3_inventory_refresh.md b/devlog/_plan/260902_admin_merge_3190/030_wp3_inventory_refresh.md new file mode 100644 index 0000000000..28ed196368 --- /dev/null +++ b/devlog/_plan/260902_admin_merge_3190/030_wp3_inventory_refresh.md @@ -0,0 +1,64 @@ +# 030 — wp3: refresh leftover inventory and merge only authorized ready items + +Depends on: wp2 (#3190 on `origin/dev`). + +## Fresh read, not the freeze table + +At wp0 freeze the only authorized merge candidate was #3190. wp3 exists because the user asked to finish remaining ready work, not to stop after one PR. Re-run the inventory; do not reuse the freeze table as if it were live. + +``` +git fetch origin --prune +gh pr list --state open --limit 80 --json number,title,author,isDraft,mergeable,reviewDecision,headRefName,url +``` + +Then for every non-draft row: + +``` +gh pr view --json number,title,isDraft,mergeable,mergeStateStatus,reviewDecision,headRefOid,statusCheckRollup,files +``` + +## Authorization filter (all must hold) + +1. `isDraft == false` +2. `mergeable == MERGEABLE` (not CONFLICTING, not UNKNOWN-as-conflict) +3. Not `CHANGES_REQUESTED` unless this train already carries the requested change +4. Not a security-boundary diff (auth, credential, OAuth, workflow, release, dependency install) unless a named security review is already on the exact head +5. Not both of a documented pair (#2083 original and #2986 carry) +6. Not a parked item whose prior train already recorded a substantive blocker (#3061 launcher budget) + +If zero rows survive, wp3 is NOOP with the live table recorded in an outcome doc, and criterion c-4 is met by that recording. + +If a new row survives, land it the same way as wp2: rebase onto current `origin/dev` if behind, `--no-verify` push, exact-head CI, admin squash merge, fetch + merge-base proof. One PR per inner loop; do not batch-merge. + +## Known likely leftovers after #3190 + +| PR | Expected live disposition | Merge now? | +| --- | --- | --- | +| #3142 | still CONFLICTING | no | +| #3061 | still CHANGES_REQUESTED + red macos | no | +| #2986 / #2083 | overlapping image-gen carry | no (pair) | +| #2877 | CHANGES_REQUESTED docs | no | +| #2805 #2783 #2527 | CONFLICTING | no | +| #2366 | CHANGES_REQUESTED contributor feature | no | +| #2734 | should already be closed by wp2 | verify | + +A docs-only MERGEABLE PR with no CHANGES_REQUESTED and green hygiene (the #3114 shape) may be landed. Do not invent that it exists; the live list decides. + +## Steps + +1. Produce a timestamped table of every open non-draft PR with mergeable/review/CI bucket. +2. Apply the filter. Write survivors (possibly empty) into `031_wp3_outcome.md` at C, not here. +3. For each survivor, rebase / exact-head CI / admin merge / proof, serialized. +4. Re-fetch after each merge before judging the next row. +5. Switch this worktree off any deleted head branch. + +## Accept + +- Live inventory captured after #3190 landed. +- Every survivor that passed the filter is on `origin/dev` with merge-base proof, or the survivor list is empty and recorded. +- No conflicting, draft, or CHANGES_REQUESTED-without-carry PR was merged. +- #2083 and #2986 were not both merged. + +## Activation + +Trigger: the timestamped `gh pr list` output in the outcome doc is newer than the #3190 merge time. Observable: each claimed merge SHA is an ancestor of `origin/dev`. Negative: claiming c-4 from the wp0 freeze table without a second `gh pr list`. diff --git a/devlog/_plan/260902_multiplatform_qa_and_gui/091_wp6_merge_outcome.md b/devlog/_plan/260902_multiplatform_qa_and_gui/091_wp6_merge_outcome.md index ddc3c445cc..336c453c06 100644 --- a/devlog/_plan/260902_multiplatform_qa_and_gui/091_wp6_merge_outcome.md +++ b/devlog/_plan/260902_multiplatform_qa_and_gui/091_wp6_merge_outcome.md @@ -10,8 +10,8 @@ PR #3174가 `e582aee21`로 `dev`에 머지됐다. | `gates` | `privacy:scan` | 로컬 재현 결과 devlog에 원격 홈 경로가 그대로 있었다. 익명화 | | `enforce-target` | 본문이 비어 있고 스크린샷 없음 | 앞선 편집이 본문을 날렸다. 본문 복구 + 스크린샷 2장 첨부 | -두 번째가 제일 의미 있다. 문서에 `/Users/junny/`, `/Users/tig/`, -`/home/lidgeai/`, `/c/Users/user/`를 실측 그대로 적었는데, 그건 다른 사람의 +두 번째가 제일 의미 있다. 문서에 원격 macOS 홈 경로 두 개, +POSIX 홈, Windows npm 접두를 실측 그대로 적었는데, 그건 다른 사람의 홈 경로다. 스캔이 정당하게 잡았고 `~/`로 바꿨다. 원격 호스트를 다루는 유닛은 이 함정을 기본으로 안고 시작한다. From ef6a163c7a0b62a14a27c09cb945ce5e01c5318b Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 03:11:44 +0900 Subject: [PATCH 126/172] fix(capacity): count uncalibrated Codex plans instead of excluding them (#3198) A Business seat upgraded to a Premium Seat started reporting a plan string the capacity weight map does not list, so the account was dropped from its own capacity estimate and the dashboard warned about incomplete coverage. The map knows five plan names. The bundled upstream snapshot alone carries 21, and CodexAccount.plan is an unrestricted string, so sixteen real plans were already being excluded - Premium Seat is only the one a user noticed. src/codex/quota.ts reached this same conclusion about this same field: an allowlist is a list of the plans someone remembered. Weight an uncalibrated plan at the baseline seat instead. Under-counting a large seat is a visibly conservative estimate; excluding it silently overstates the coverage an operator is reading. The calibrated weights are unchanged, and paused, needs-reauth, missing-quota and stale-quota accounts are still excluded. The dashboard string is split to match: unknown plans are no longer described as excluded, because they are not. Closes #3155 Co-authored-by: jun --- .../ProviderCapacityQuota.tsx | 12 ++- gui/src/i18n/de.ts | 3 +- gui/src/i18n/en.ts | 3 +- gui/src/i18n/fr.ts | 3 +- gui/src/i18n/ja.ts | 3 +- gui/src/i18n/ko.ts | 3 +- gui/src/i18n/ru.ts | 3 +- gui/src/i18n/tr.ts | 3 +- gui/src/i18n/zh-TW.ts | 3 +- gui/src/i18n/zh.ts | 3 +- gui/tests/provider-capacity-shell.test.tsx | 8 +- src/providers/codex-capacity.ts | 38 ++++++- tests/provider-capacity.test.ts | 102 +++++++++++++++--- 13 files changed, 154 insertions(+), 33 deletions(-) diff --git a/gui/src/components/provider-workspace/ProviderCapacityQuota.tsx b/gui/src/components/provider-workspace/ProviderCapacityQuota.tsx index f2b4764727..0f409907c9 100644 --- a/gui/src/components/provider-workspace/ProviderCapacityQuota.tsx +++ b/gui/src/components/provider-workspace/ProviderCapacityQuota.tsx @@ -131,10 +131,20 @@ export function ProviderCapacityQuota({ report, pending }: { report: ProviderQuo
{t("pws.capacity.incomplete", { excluded: aggregation.excludedAccounts, - unknown: aggregation.unknownPlanAccounts, })}
)} + {/* + Separate from the exclusion notice on purpose (#3155). An uncalibrated plan is + COUNTED, at the baseline seat weight, so folding it into "excluded" told an + operator their Premium seat was missing from a report that in fact included it. + What is true is narrower: the estimate is conservative for that seat. + */} + {aggregation && aggregation.unknownPlanAccounts > 0 && ( +
+ {t("pws.capacity.uncalibratedPlan", { count: aggregation.unknownPlanAccounts })} +
+ )} {aggregation && aggregation.partialWindowAccounts > 0 && (
{t("pws.capacity.partial", { count: aggregation.partialWindowAccounts })} diff --git a/gui/src/i18n/de.ts b/gui/src/i18n/de.ts index eff4574b7a..96f6036caa 100644 --- a/gui/src/i18n/de.ts +++ b/gui/src/i18n/de.ts @@ -1969,7 +1969,8 @@ export const de: Record = { "pws.capacity.currentAccount": "Aktuelles effektives Konto", "pws.capacity.nextRecovery": "Nächste Kapazitätswiederherstellung", "pws.capacity.recoveryShare": "+{percent} % Pool-Kapazität", - "pws.capacity.incomplete": "Unvollständige Abdeckung: {excluded} Konten ausgeschlossen, davon {unknown} mit unbekanntem Tarif", + "pws.capacity.incomplete": "Unvollständige Abdeckung: {excluded} Konten ausgeschlossen", + "pws.capacity.uncalibratedPlan": "{count} Konten mit unkalibriertem Tarif werden mit dem Basisgewicht gezählt; diese Schätzung kann daher konservativ sein", "pws.capacity.partial": "Teilweise Fensterabdeckung: {count} Konten melden nicht jedes angezeigte Limitfenster", "pws.capacity.windowPartial": "Teilweise", "pws.capacity.windowPartialA11y": "{window}: unvollständige Kontoabdeckung", diff --git a/gui/src/i18n/en.ts b/gui/src/i18n/en.ts index 8abb9816fa..4e9d1140f9 100644 --- a/gui/src/i18n/en.ts +++ b/gui/src/i18n/en.ts @@ -1280,7 +1280,8 @@ export const en = { "pws.capacity.currentAccount": "Current effective account", "pws.capacity.nextRecovery": "Next capacity recovery", "pws.capacity.recoveryShare": "+{percent}% pool capacity", - "pws.capacity.incomplete": "Incomplete coverage: {excluded} account(s) excluded, including {unknown} unknown plan(s)", + "pws.capacity.incomplete": "Incomplete coverage: {excluded} account(s) excluded", + "pws.capacity.uncalibratedPlan": "{count} account(s) on an uncalibrated plan are counted at the baseline seat weight, so this estimate may be conservative", "pws.capacity.partial": "Partial window coverage: {count} account(s) do not report every displayed limit window", "pws.capacity.windowPartial": "Partial", "pws.capacity.windowPartialA11y": "{window}: incomplete account coverage", diff --git a/gui/src/i18n/fr.ts b/gui/src/i18n/fr.ts index 4797b8719b..619dd2cd93 100644 --- a/gui/src/i18n/fr.ts +++ b/gui/src/i18n/fr.ts @@ -1253,7 +1253,8 @@ export const fr: Record = { "pws.capacity.currentAccount": "Compte effectif actuel", "pws.capacity.nextRecovery": "Prochaine récupération de capacité", "pws.capacity.recoveryShare": "+{percent}% de capacité du groupe", - "pws.capacity.incomplete": "Couverture incomplète : {excluded} compte(s) exclus, dont {unknown} forfait(s) inconnu(s)", + "pws.capacity.incomplete": "Couverture incomplète : {excluded} compte(s) exclus", + "pws.capacity.uncalibratedPlan": "{count} compte(s) sur un forfait non calibré sont comptés au poids de siège de base ; cette estimation peut donc être prudente", "pws.capacity.partial": "Couverture partielle des fenêtres : {count} compte(s) ne signalent pas toutes les fenêtres de limite affichées", "pws.capacity.windowPartial": "Partielle", "pws.capacity.windowPartialA11y": "{window} : couverture incomplète des comptes", diff --git a/gui/src/i18n/ja.ts b/gui/src/i18n/ja.ts index 0c2a1b69d8..d2b008a142 100644 --- a/gui/src/i18n/ja.ts +++ b/gui/src/i18n/ja.ts @@ -1213,7 +1213,8 @@ export const ja: Record = { "pws.capacity.currentAccount": "現在の有効アカウント", "pws.capacity.nextRecovery": "次の容量回復", "pws.capacity.recoveryShare": "+{percent}% のプール容量", - "pws.capacity.incomplete": "対象範囲が不完全です: {excluded} 件を除外(不明なプラン {unknown} 件)", + "pws.capacity.incomplete": "対象範囲が不完全です: {excluded} 件を除外", + "pws.capacity.uncalibratedPlan": "未校正プランの {count} 件は基準シート重みで計上されるため、この推定値は控えめになる場合があります", "pws.capacity.partial": "一部の期間の対象範囲が不完全です: {count} 件のアカウントでは表示中のすべての制限期間を取得できません", "pws.capacity.windowPartial": "一部のみ", "pws.capacity.windowPartialA11y": "{window}: アカウントの対象範囲が不完全です", diff --git a/gui/src/i18n/ko.ts b/gui/src/i18n/ko.ts index 898cd67d3e..b8cfd99a2c 100644 --- a/gui/src/i18n/ko.ts +++ b/gui/src/i18n/ko.ts @@ -1996,7 +1996,8 @@ export const ko: Record = { "pws.capacity.currentAccount": "현재 유효 계정", "pws.capacity.nextRecovery": "다음 용량 회복", "pws.capacity.recoveryShare": "+{percent}% 풀 용량", - "pws.capacity.incomplete": "불완전한 범위: {excluded}개 계정 제외, 알 수 없는 요금제 {unknown}개 포함", + "pws.capacity.incomplete": "불완전한 범위: {excluded}개 계정 제외", + "pws.capacity.uncalibratedPlan": "보정되지 않은 요금제 {count}개는 기본 좌석 가중치로 계산되어, 이 추정치가 실제보다 낮을 수 있습니다", "pws.capacity.partial": "일부 기간의 범위가 불완전합니다: {count}개 계정에서 표시된 모든 한도 기간을 확인할 수 없습니다", "pws.capacity.windowPartial": "일부만", "pws.capacity.windowPartialA11y": "{window}: 계정 범위가 불완전합니다", diff --git a/gui/src/i18n/ru.ts b/gui/src/i18n/ru.ts index 12c47f477c..c6b9db06fc 100644 --- a/gui/src/i18n/ru.ts +++ b/gui/src/i18n/ru.ts @@ -1264,7 +1264,8 @@ export const ru: Record = { "pws.capacity.currentAccount": "Текущая активная учётная запись", "pws.capacity.nextRecovery": "Следующее восстановление ёмкости", "pws.capacity.recoveryShare": "+{percent}% ёмкости пула", - "pws.capacity.incomplete": "Неполное покрытие: исключено аккаунтов: {excluded}, в том числе с неизвестным планом: {unknown}", + "pws.capacity.incomplete": "Неполное покрытие: исключено аккаунтов: {excluded}", + "pws.capacity.uncalibratedPlan": "Аккаунтов с некалиброванным планом: {count}. Они учитываются с базовым весом места, поэтому оценка может быть заниженной", "pws.capacity.partial": "Частичное покрытие окон: для {count} аккаунтов доступны не все показанные окна лимитов", "pws.capacity.windowPartial": "Частично", "pws.capacity.windowPartialA11y": "{window}: неполное покрытие аккаунтов", diff --git a/gui/src/i18n/tr.ts b/gui/src/i18n/tr.ts index 4aaeffec66..e8591fce8a 100644 --- a/gui/src/i18n/tr.ts +++ b/gui/src/i18n/tr.ts @@ -1271,7 +1271,8 @@ export const tr: Record = { "pws.capacity.currentAccount": "Mevcut geçerli hesap", "pws.capacity.nextRecovery": "Sonraki kapasite yenilenmesi", "pws.capacity.recoveryShare": "+%{percent} havuz kapasitesi", - "pws.capacity.incomplete": "Kısmi pencere kapsamı ({unknown} bilinmeyen, {excluded} hariç tutuldu)", + "pws.capacity.incomplete": "Kısmi pencere kapsamı ({excluded} hariç tutuldu)", + "pws.capacity.uncalibratedPlan": "Kalibre edilmemiş plandaki {count} hesap temel koltuk ağırlığıyla sayılır; bu tahmin ihtiyatlı olabilir", "pws.capacity.partial": "Kısmi ({count} hesap kota metriği bildiriyor)", "pws.capacity.windowPartial": "Kısmi", "pws.capacity.windowPartialA11y": "{window}: eksik hesap kapsamı", diff --git a/gui/src/i18n/zh-TW.ts b/gui/src/i18n/zh-TW.ts index 182fa48751..a86c977775 100644 --- a/gui/src/i18n/zh-TW.ts +++ b/gui/src/i18n/zh-TW.ts @@ -2038,7 +2038,8 @@ export const zhTW: Record = { "pws.capacity.currentAccount": "目前有效帳號", "pws.capacity.nextRecovery": "下一次容量復原", "pws.capacity.recoveryShare": "+{percent}% 帳號池容量", - "pws.capacity.incomplete": "覆蓋不完整:已排除 {excluded} 個帳號,其中 {unknown} 個方案未知", + "pws.capacity.incomplete": "覆蓋不完整:已排除 {excluded} 個帳號", + "pws.capacity.uncalibratedPlan": "{count} 個帳號使用未校準方案,以基準席次權重計入,因此此估算可能偏保守", "pws.capacity.partial": "部分視窗覆蓋:{count} 個帳號未回報所有顯示的限額視窗", "pws.capacity.windowPartial": "部分", "pws.capacity.windowPartialA11y": "{window}:帳號覆蓋不完整", diff --git a/gui/src/i18n/zh.ts b/gui/src/i18n/zh.ts index 96f3d939c2..17c013b907 100644 --- a/gui/src/i18n/zh.ts +++ b/gui/src/i18n/zh.ts @@ -1989,7 +1989,8 @@ export const zh: Record = { "pws.capacity.currentAccount": "当前有效账户", "pws.capacity.nextRecovery": "下一次容量恢复", "pws.capacity.recoveryShare": "+{percent}% 账户池容量", - "pws.capacity.incomplete": "覆盖不完整:已排除 {excluded} 个账户,其中 {unknown} 个套餐未知", + "pws.capacity.incomplete": "覆盖不完整:已排除 {excluded} 个账户", + "pws.capacity.uncalibratedPlan": "{count} 个账户使用未校准套餐,按基准席位权重计入,因此该估算可能偏保守", "pws.capacity.partial": "部分窗口覆盖不完整:{count} 个账户未报告所有显示的限额窗口", "pws.capacity.windowPartial": "部分", "pws.capacity.windowPartialA11y": "{window}:账户覆盖不完整", diff --git a/gui/tests/provider-capacity-shell.test.tsx b/gui/tests/provider-capacity-shell.test.tsx index 315d47f857..7531833f13 100644 --- a/gui/tests/provider-capacity-shell.test.tsx +++ b/gui/tests/provider-capacity-shell.test.tsx @@ -211,7 +211,10 @@ test("provider quota fetch preserves aggregate capacity through shell state and expect(text).toContain("31% used"); expect(text).toContain("Current effective account · pro"); expect(text).toContain("8%"); - expect(text).toContain("Incomplete coverage: 1 account(s) excluded, including 1 unknown plan(s)"); + // #3155 split these: an uncalibrated plan is COUNTED at baseline, so it is no longer + // reported as excluded. The exclusion line now says only what it can prove. + expect(text).toContain("Incomplete coverage: 1 account(s) excluded"); + expect(text).toContain("1 account(s) on an uncalibrated plan are counted at the baseline seat weight"); expect(text).toContain("Next capacity recovery"); expect(text).toContain("+19.2% pool capacity"); const expectedRecoveryAt = new Intl.DateTimeFormat("en", { @@ -350,7 +353,8 @@ test("coverage-only API report remains visible in the rate-limit overview", asyn const text = host.textContent ?? ""; expect(text).toContain("OpenAI (Codex login)"); - expect(text).toContain("Incomplete coverage: 3 account(s) excluded, including 1 unknown plan(s)"); + expect(text).toContain("Incomplete coverage: 3 account(s) excluded"); + expect(text).toContain("1 account(s) on an uncalibrated plan are counted at the baseline seat weight"); expect(text).not.toContain("No rate-limit data yet"); expect(text).not.toMatch(/\d+(?:\.\d+)?% used/); }); diff --git a/src/providers/codex-capacity.ts b/src/providers/codex-capacity.ts index be40a2eb60..175620def9 100644 --- a/src/providers/codex-capacity.ts +++ b/src/providers/codex-capacity.ts @@ -8,6 +8,28 @@ export const CODEX_CONFIGURED_CAPACITY_WEIGHTS = { pro: 20, } as const; +/** + * Weight for a plan the map above has not calibrated. + * + * `CodexAccount.plan` is an unrestricted upstream string and cannot be enumerated: the + * bundled model snapshot alone carries 21 distinct plan names (`edu_plus`, `finserv`, + * `k12`, `quorum`, `self_serve_business_usage_based`, …) against the five listed above. + * Treating an unlisted plan as unknown dropped the account from the estimate entirely and + * reported "incomplete coverage" — which is how a Business seat upgraded to Premium + * disappeared from its own capacity report (#3155), and it was already happening to the + * other sixteen snapshot plans without anyone noticing. + * + * `src/codex/quota.ts` reached the same conclusion about the same field: an allowlist is a + * list of the plans someone remembered. Counting an unfamiliar plan at the baseline seat + * weight — the value `plus`, `team`, and `business` already carry — under-states a large + * seat, which is a visibly conservative estimate. Excluding it silently overstates coverage + * the operator does not have, which is worse. + * + * Uncalibrated plans are still counted in `unknownPlanAccounts` so the estimate's + * uncertainty stays on screen. + */ +export const CODEX_DEFAULT_CAPACITY_WEIGHT = 1; + /** Match the provider-report last-good freshness bound. */ export const CODEX_CAPACITY_MAX_QUOTA_AGE_MS = 30 * 60_000; @@ -85,11 +107,17 @@ type MutableWindow = { oldestUpdatedAt: number; }; -function configuredWeight(plan: unknown): number | undefined { +/** True when this plan has a calibrated weight rather than falling back to the default. */ +function isCalibratedPlan(plan: unknown): boolean { + const normalized = codexPlanKey(plan); + return !!normalized && Object.hasOwn(CODEX_CONFIGURED_CAPACITY_WEIGHTS, normalized); +} + +function configuredWeight(plan: unknown): number { const normalized = codexPlanKey(plan); return normalized && Object.hasOwn(CODEX_CONFIGURED_CAPACITY_WEIGHTS, normalized) ? CODEX_CONFIGURED_CAPACITY_WEIGHTS[normalized as keyof typeof CODEX_CONFIGURED_CAPACITY_WEIGHTS] - : undefined; + : CODEX_DEFAULT_CAPACITY_WEIGHT; } function normalizedPercent(value: unknown): number | undefined { @@ -188,7 +216,9 @@ export function aggregateCodexPoolCapacity( for (const account of accounts) { const weight = configuredWeight(account.plan); - if (weight === undefined) unknownPlanAccounts += 1; + // Still reported, so the operator can see the estimate is conservative for this seat — + // but no longer a reason to drop the account from the aggregate (#3155). + if (!isCalibratedPlan(account.plan)) unknownPlanAccounts += 1; if (account.paused) pausedAccounts += 1; if (account.needsReauth) reauthAccounts += 1; const quota = account.quota; @@ -204,7 +234,7 @@ export function aggregateCodexPoolCapacity( const custom = quota?.customWindows ?? []; const hasQuota = hasKnownQuotaWindow(quota); if (!hasQuota) missingQuotaAccounts += 1; - if (account.paused || account.needsReauth || weight === undefined || !quota || !hasQuota || !quotaFresh) continue; + if (account.paused || account.needsReauth || !quota || !hasQuota || !quotaFresh) continue; let contributed = false; const contributionKeys = new Set(); diff --git a/tests/provider-capacity.test.ts b/tests/provider-capacity.test.ts index a10572f9f8..978cb39bc8 100644 --- a/tests/provider-capacity.test.ts +++ b/tests/provider-capacity.test.ts @@ -91,7 +91,7 @@ describe("configured-weight Codex pool capacity", () => { expect(result.aggregation?.weekly?.nextRecoveryPercent).toBeCloseTo(2 / 25 * 100, 8); }); - test("unknown, missing, paused, and reauth rows are excluded with incomplete coverage", () => { + test("missing, paused and reauth rows are excluded; an uncalibrated plan is counted at baseline", () => { const result = aggregateCodexPoolCapacity([ account("pro", 10, { active: true, isMain: true }), account("future-plan", 50), @@ -99,16 +99,22 @@ describe("configured-weight Codex pool capacity", () => { account("prolite", 20, { paused: true }), account("business", 30, { needsReauth: true }), ], NOW); + // #3155: an unrecognized plan string no longer drops the account. The upstream plan + // field is unrestricted - the bundled snapshot alone carries 21 names against the five + // calibrated here - so "future-plan" is weighted at the baseline seat instead of being + // silently omitted from a coverage figure the operator is reading as complete. expect(result.aggregation).toMatchObject({ - includedAccounts: 1, - excludedAccounts: 4, + includedAccounts: 2, + excludedAccounts: 3, + // Still surfaced: the estimate is conservative for that seat, and saying so is useful. unknownPlanAccounts: 1, missingQuotaAccounts: 1, pausedAccounts: 1, reauthAccounts: 1, incomplete: true, }); - expect(result.quota?.weeklyPercent).toBe(10); + // pro(10) at weight 20 plus future-plan(50) at weight 1: (20*10 + 1*50) / 21 ≈ 11.9 + expect(result.quota?.weeklyPercent).toBeCloseTo((20 * 10 + 1 * 50) / 21, 5); }); test("weekly and monthly windows aggregate independently", () => { @@ -131,7 +137,7 @@ describe("configured-weight Codex pool capacity", () => { expect(result.quota?.monthlyPercent).toBeCloseTo(51.6666667, 7); }); - test("expired resets are ignored and all-excluded pools retain effective-account fallback truth", () => { + test("expired resets are ignored, and uncalibrated plans now aggregate instead of falling back", () => { const expired = aggregateCodexPoolCapacity([ account("pro", 10, { weeklyResetAt: NOW - 1, active: true, isMain: true }), ], NOW); @@ -140,12 +146,15 @@ describe("configured-weight Codex pool capacity", () => { account("future-plan", 70, { active: true, isMain: true }), account("go", 80), ], NOW); + // Both plans are uncalibrated - and `go` is a REAL shipped plan, not a hypothetical - + // so before #3155 a pool made entirely of them aggregated nothing and reported total + // exclusion. They now contribute at the baseline weight. expect(fallback.aggregation).toMatchObject({ - includedAccounts: 0, - excludedAccounts: 2, + includedAccounts: 2, + excludedAccounts: 0, unknownPlanAccounts: 2, - incomplete: true, }); + expect(fallback.aggregation?.weekly?.usedPercent).toBeCloseTo(75, 5); expect(fallback.currentAccount?.quota?.weeklyPercent).toBe(70); }); @@ -190,7 +199,7 @@ describe("configured-weight Codex pool capacity", () => { }); }); - test("prototype and unknown plan names never become configured weights", () => { + test("prototype key names never become configured weights", () => { const result = aggregateCodexPoolCapacity([ account("plus", 20), account("constructor", 100), @@ -198,21 +207,31 @@ describe("configured-weight Codex pool capacity", () => { account("valueOf", 100), account("unknown", 100), ], NOW); - expect(result.quota?.weeklyPercent).toBe(20); + // The Object.hasOwn guard still stands: "constructor" must resolve to the numeric + // baseline, never to Object.prototype.constructor. #3155 changed what an uncalibrated + // plan DOES (baseline weight instead of exclusion), not whether a prototype key can + // reach the weight table. expect(result.aggregation).toMatchObject({ - includedAccounts: 1, - excludedAccounts: 4, + includedAccounts: 5, + excludedAccounts: 0, unknownPlanAccounts: 4, }); + // All five carry a real weight, so the aggregate stays finite and inside 0-100 rather + // than becoming NaN from a function-valued weight. + expect(result.quota?.weeklyPercent).toBeCloseTo((1 * 20 + 4 * 100) / 5, 5); expect(Number.isFinite(result.quota?.weeklyPercent)).toBe(true); }); - test("non-string plans are excluded and never exposed in current-account metadata", () => { + test("a non-string plan is counted at baseline and still never exposed as metadata", () => { const result = aggregateCodexPoolCapacity([ account({ tier: "pro" }, 20, { active: true, isMain: true }), ], NOW); - expect(result.quota).toBeNull(); - expect(result.aggregation).toMatchObject({ unknownPlanAccounts: 1, includedAccounts: 0 }); + // A malformed plan is uncalibrated, not unusable: the account still has a real quota + // reading, so it contributes at the baseline weight (#3155). + expect(result.quota?.weeklyPercent).toBe(20); + expect(result.aggregation).toMatchObject({ unknownPlanAccounts: 1, includedAccounts: 1 }); + // Unchanged and still the point of this test: a non-string plan is never surfaced as a + // plan label, so an object cannot leak into the displayed metadata. expect(result.aggregation?.currentAccount).not.toHaveProperty("plan"); expect(result.aggregation?.currentAccount?.quota?.weeklyPercent).toBe(20); }); @@ -237,7 +256,9 @@ describe("configured-weight Codex pool capacity", () => { const stale = account("pro", 90); stale.quota = { ...stale.quota!, updatedAt: NOW - CODEX_CAPACITY_MAX_QUOTA_AGE_MS - 1 }; const result = aggregateCodexPoolCapacity([ - account("future-plan", 10, { active: true, isMain: true }), + // Every row here is excluded for a reason that survives #3155: an unrecognized plan + // is no longer one of them, so the previously-uncalibrated row is paused instead. + account("future-plan", 10, { active: true, isMain: true, paused: true }), account("plus", undefined), account("prolite", 20, { paused: true }), account("business", 30, { needsReauth: true }), @@ -249,10 +270,57 @@ describe("configured-weight Codex pool capacity", () => { excludedAccounts: 5, unknownPlanAccounts: 1, missingQuotaAccounts: 1, - pausedAccounts: 1, + pausedAccounts: 2, reauthAccounts: 1, staleQuotaAccounts: 1, incomplete: true, }); }); + + test("an uncalibrated plan contributes at baseline instead of being dropped (#3155)", () => { + // A Business seat upgraded to Premium started reporting a plan string the weight map + // did not list, and the account vanished from its own capacity report. The map knows + // five names; the bundled upstream snapshot alone carries 21, so this was already + // happening to sixteen real plans - Premium Seat is just the one someone noticed. + const result = aggregateCodexPoolCapacity([ + account("business_premium_seat", 40, { active: true, isMain: true }), + ], NOW); + expect(result.aggregation).toMatchObject({ includedAccounts: 1, excludedAccounts: 0 }); + expect(result.quota?.weeklyPercent).toBe(40); + // Still flagged, because the estimate IS conservative for a larger seat - the operator + // should be able to see that, just not by having the account omitted. + expect(result.aggregation?.unknownPlanAccounts).toBe(1); + }); + + test("calibrated weights are unchanged by the default (#3155)", () => { + // The default must not flatten the calibration: a Pro seat is still worth 20 baselines, + // so a fully-consumed Pro next to an idle Plus must not read as 50%. + const result = aggregateCodexPoolCapacity([ + account("pro", 100), + account("plus", 0), + ], NOW); + expect(result.quota?.weeklyPercent).toBeCloseTo((20 * 100 + 1 * 0) / 21, 5); + }); + + test("the default never resurrects an account excluded for a real reason (#3155)", () => { + // The failure mode a careless version of this change would introduce: counting seats + // that cannot serve traffic. Paused, needs-reauth, missing-quota and stale-quota rows + // stay excluded regardless of whether their plan is calibrated. + const stale = account("unlisted-plan-a", 10); + stale.quota = { ...stale.quota!, updatedAt: NOW - CODEX_CAPACITY_MAX_QUOTA_AGE_MS - 1 }; + const result = aggregateCodexPoolCapacity([ + account("unlisted-plan-b", 10, { paused: true }), + account("unlisted-plan-c", 10, { needsReauth: true }), + account("unlisted-plan-d", undefined), + stale, + ], NOW); + expect(result.aggregation).toMatchObject({ + includedAccounts: 0, + excludedAccounts: 4, + pausedAccounts: 1, + reauthAccounts: 1, + missingQuotaAccounts: 1, + staleQuotaAccounts: 1, + }); + }); }); From 88c4275229c4a3aaa523f1af919f8c76df0e49df Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 03:16:08 +0900 Subject: [PATCH 127/172] feat(combo): adapt reasoning effort to target capabilities (#2731) (#3190) * feat(combo): adapt reasoning effort to target capabilities Adopts the design from #2734 and completes the surfaces it left open. Two defects, both opt-in fixes that default to today's behavior: - A combo intersects its targets' effort ladders, and an explicitly empty ladder (a target with no effort control) collapsed the intersection for the whole group. reasoningEffortMode: "adaptive" excludes empty ladders from the published intersection; "strict" remains the default. - openai-chat could declare per-model effort support but not a per-request rule, so a model that rejects effort only alongside tools had to use noReasoningModels and lose its picker entirely. omitReasoningEffortWithToolsModels drops the wire field for tool-bearing requests only. Beyond #2734: the dashboard round-trip. PUT /api/combos replaces the stored combo wholesale and the GUI serializer is a field allowlist, so a hand-edited reasoningEffortMode was silently destroyed by any dashboard combo edit. The field is now parsed, serialized, dirty-checked and sync-keyed, and the shared sparse helper keeps GET, the PUT response and disk consistent so an unset default is never materialized into config.json. The opt-in is a switch in the combo Capabilities section beside image input, where combo-wide policy already lives - not under Default reasoning, which is a value chosen from the intersection rather than a rule that changes it. Both the create modal and the detail panel share that component, so the create flow shows the corrected picker too. Refs #2731. Supersedes #2734. * test(combo): cover adaptive effort mode and the tool-bearing opt-out Activation-grounded regressions for both halves of #2731, plus the dashboard round-trip the original PR left uncovered. - codex-catalog: adaptive keeps the surviving sibling ladder where strict empties it, still intersects non-empty ladders, and yields nothing when every target is empty. - openai-chat-hardening: effort dropped only with tools present, kept without them, untouched for an unlisted sibling and for an unset provider list, and suppressed on the gateway-object branch that writes its own reasoning field. - combo-management-api: adaptive survives PUT to disk to GET; the strict default is never materialized; turning it back off clears the stored field; an unknown value is rejected. - combo-workspace-data: the GUI intersection honors the mode, parse and toPutBody round-trip it, and draftEquals treats a change as dirty so Save enables. - management-provider-validation: validates, exposes and normalizes the new provider list. * docs(devlog): record the 3190 cherry-pick onto privacy-clean dev --------- Co-authored-by: jun --- .../021_wp2_cherry_pick.md | 10 ++ .../000_scope.md | 42 +++++ .../010_wp1_adaptive_reasoning_effort.md | 153 ++++++++++++++++++ .../011_wp1_audit_r1_synthesis.md | 82 ++++++++++ docs-site/src/content/docs/guides/combos.md | 32 ++++ .../docs/reference/configuration/providers.md | 1 + .../docs/reference/configuration/routing.md | 1 + gui/src/combo-workspace-data.ts | 18 +++ .../components/combo-workspace-add-modal.tsx | 5 +- .../components/combo-workspace-controls.tsx | 18 ++- .../combo-workspace-detail-panel.tsx | 7 +- gui/src/i18n/de.ts | 2 + gui/src/i18n/en.ts | 2 + gui/src/i18n/fr.ts | 2 + gui/src/i18n/ja.ts | 2 + gui/src/i18n/ko.ts | 2 + gui/src/i18n/ru.ts | 2 + gui/src/i18n/tr.ts | 2 + gui/src/i18n/zh-TW.ts | 2 + gui/src/i18n/zh.ts | 2 + src/adapters/openai-chat.ts | 12 +- src/codex/catalog/aggregation.ts | 12 +- src/combos/types.ts | 12 ++ src/config.ts | 14 ++ src/server/auth-cors.ts | 6 + src/server/management/combo-routes.ts | 24 ++- src/server/management/provider-routes.ts | 14 ++ src/types.ts | 1 + src/types/config.ts | 17 ++ src/types/provider.ts | 8 + tests/codex-catalog.test.ts | 28 ++++ tests/combo-management-api.test.ts | 61 +++++++ tests/combo-workspace-data.test.ts | 45 ++++++ tests/combos.test.ts | 12 ++ tests/management-provider-validation.test.ts | 44 +++++ tests/openai-chat-hardening.test.ts | 76 +++++++++ 36 files changed, 757 insertions(+), 16 deletions(-) create mode 100644 devlog/_plan/260902_admin_merge_3190/021_wp2_cherry_pick.md create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/000_scope.md create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/010_wp1_adaptive_reasoning_effort.md create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/011_wp1_audit_r1_synthesis.md diff --git a/devlog/_plan/260902_admin_merge_3190/021_wp2_cherry_pick.md b/devlog/_plan/260902_admin_merge_3190/021_wp2_cherry_pick.md new file mode 100644 index 0000000000..06e8c88a6e --- /dev/null +++ b/devlog/_plan/260902_admin_merge_3190/021_wp2_cherry_pick.md @@ -0,0 +1,10 @@ +# 021 — wp2 cherry-pick onto privacy-clean origin/dev + +wp1 landed as #3197 squash `4be4326d7`. Unique #3190 commits cherry-picked onto that tip: + +- `e1fc1729b` -> `3ee25f58e` feat(combo): adapt reasoning effort to target capabilities +- `5f8cd24dd` -> `c2a89e321` test(combo): cover adaptive effort mode and the tool-bearing opt-out + +Conflicts: none. Auto-merged `src/codex/catalog/aggregation.ts`, `src/server/management/provider-routes.ts`, `docs-site/src/content/docs/reference/configuration/providers.md`, `tests/combos.test.ts`, `tests/management-provider-validation.test.ts`. + +Focused checks on `c2a89e321`: `bun x tsc --noEmit` exit 0; `cd gui && bun x tsc --noEmit` exit 0; `bun run privacy:scan` exit 0; `bun test` of the six named files 528 pass / 0 fail. diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/000_scope.md b/devlog/_plan/260902_nonbug_adoption_backlog/000_scope.md new file mode 100644 index 0000000000..890823eac4 --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/000_scope.md @@ -0,0 +1,42 @@ +# Non-bug adoption backlog — scope + +Fifteen non-bug items selected from the open issue/PR set by the maintainer's own +recorded `우선순위 NN / 80` review scores. Excludes every `bug`-labeled item, +maintainer-authored items (#3158, PR #3061, PR #2783), and #3146 (already closed by +PR #3151). + +One work-phase per item, highest score first, except where a smaller verified diff +is sequenced earlier to establish the landing pattern. + +| wp | Item | Score | Existing PR | +|----|------|-------|-------------| +| wp1 | #2731 adaptive reasoning effort | 62 | #2734 (draft) | +| wp2 | PR #3142 + #2511 oversized body refusal | 64 | #3142 (ready) | +| wp3 | #2901 compaction provider selection | 58 | none | +| wp4 | #1690 retainModels allowlist | 58 | #2122, #2860 (rival) | +| wp5 | PR #2986 xAI Imagine relay | 58 | #2986 (ready) | +| wp6 | #1107 authless Codex Desktop routing | 71 | none | +| wp7 | #2713 shim-free token injection | 58 | none | +| wp8 | #1525 Windows proxy auto | 60 | none | +| wp9 | #1221 OS keychain provider keys | 61 | none | +| wp10 | #2201 model display names | 60 | #2715, #2716 | +| wp11 | #1082 per-account Gem/Cla quota | 63 | #2123 | +| wp12 | #695 generic OAuth pool failover | 69 | none | +| wp13 | #822 reset-credit auto-redemption | 59 | none | +| wp14 | #2816 upstream Responses WebSocket | 59 | #2817 | +| wp15 | #2495 plaintext V2 collaboration | 65 | #2496 | + +## Standing constraints + +- Never run the local suite. Focused typecheck only, and only when cheap. +- Push `--no-verify`; merge with admin authority; CI is trailing evidence. +- Every capability is opt-in and defaults to today's behavior. +- The opt-in must be discoverable where a user already looks for that concern. + +## The UX rule that decides these + +A capability whose only surface is a hand-edited `config.json` key is not +discoverable. When the concern already has a dashboard editor, the opt-in belongs +in that editor — and it must survive a round-trip through it. A field the GUI +silently drops on save is worse than no field, because the user sees their setting +disappear with no error. diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/010_wp1_adaptive_reasoning_effort.md b/devlog/_plan/260902_nonbug_adoption_backlog/010_wp1_adaptive_reasoning_effort.md new file mode 100644 index 0000000000..784c65184a --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/010_wp1_adaptive_reasoning_effort.md @@ -0,0 +1,153 @@ +# wp1 — #2731 adaptive reasoning effort (PR #2734) + +Issue #2731, score 62. Draft PR #2734 by the issue author, +180/-8 across 17 files, +head `573d0f65c`, behind current `dev` `e40245e4c`. CI shows `hygiene` and +`enforce-target` failing. + +## The two defects + +**(a) No per-request tool rule.** `createOpenAIChatAdapter.buildRequest` +(`src/adapters/openai-chat.ts:1441`) computes +`mapReasoningEffort(provider, parsed.modelId, parsed.options.reasoning)` with no +access to tool presence. The only model-scoped switch is `noReasoningModels`, which +is all-or-nothing: it strips reasoning from every request, so a model that accepts +effort on plain turns but rejects it alongside function tools has to give up its +effort ladder entirely. The wire `tools` array *is* already in scope at +`openai-chat.ts:1452`; nothing reads it for this decision. + +**(b) Empty ladders poison the combo intersection.** +`deriveComboCatalogModel` (`src/codex/catalog/aggregation.ts:126`) filters only +`undefined` ladders as wildcards: + +``` +const advertisedLadders = members + .map(member => member.reasoningEfforts) + .filter((ladder): ladder is string[] => ladder !== undefined); +const reasoningEfforts = advertisedLadders.length === 0 ? [] : intersectStrings(advertisedLadders); +``` + +An explicit `[]` — meaning "this target has no effort control" — survives into +`intersectStrings` (`aggregation.ts:74`) and empties the result. The empty result +then flows into `effectiveComboDefault` (`aggregation.ts:170`), so the combo also +loses its default. One no-effort target silences the picker for every sibling that +does support tuning. `tests/codex-catalog.test.ts:250` pins this as current +intended behavior, which is why the fix must be opt-in rather than a correction. + +## What PR #2734 gets right + +Both halves default to today's behavior: + +- `normalizeComboConfig` (`src/combos/types.ts:361`) maps anything that is not the + literal `"adaptive"` to `"strict"`, and the catalog branch only filters empty + ladders under `=== "adaptive"`. +- `omitReasoningEffortWithToolsModels` is optional, and `modelInList` returns + `false` for an absent or empty list. + +A user who sets nothing observes an identical catalog and identical wire bodies. +That is the back-compat bar from the goal criteria, and the PR clears it on the +request path. + +## Blocker: the GUI silently destroys the setting + +`PUT /api/combos` replaces the stored combo wholesale +(`src/server/management/combo-routes.ts:170-172`: `nextCombos[id] = stored`). +The dashboard builds that body with an allowlist serializer, `toPutBody` +(`gui/src/combo-workspace-data.ts:496-527`), which enumerates `targets`, +`strategy`, `defaultEffort`, `imageInput`, `stickyLimit`, `alias`, +`nativeAlias`, `displayName` — and nothing else. `parseCombos` +(`combo-workspace-data.ts:222-232`) likewise never reads `reasoningEffortMode`. + +So: a user hand-edits `reasoningEffortMode: "adaptive"`, later opens the dashboard +and renames the combo or reorders a target, and the save silently drops the field. +The picker they fixed goes empty again with no error and no diff they can see. +PR #2734 touches no `gui/` file, so it ships this hole. + +This is the goal's UX criterion failing, not a nitpick: the opt-in is neither +discoverable nor durable. + +## Second defect: the GUI has its own copy of the intersection + +`intersectComboEfforts` (`gui/src/combo-workspace-data.ts:54`) reimplements the +same rule client-side and skips only `listed === undefined`. Even with the field +preserved, the dashboard's own effort dropdown stays empty under `adaptive` +because it never learns the mode. Fixing the server alone fixes the served +catalog and leaves the editor lying. + +## Third: non-sparse persistence + +`combo-routes.ts:139-152` destructures `alias`/`nativeAlias`/`displayName`/ +`imageInput` out of the normalized object so defaults are not written, but +`reasoningEffortMode` stays in `normalizedBase`. Every combo save would therefore +stamp `"reasoningEffortMode": "strict"` into `config.json` for users who never +asked for the feature. Config churn on an untouched setting, against the file's +stated sparse convention. + +## Plan + +Adopt the PR's design — it is the right shape — and complete it in a +maintainer-authored branch that closes #2734 with credit. + +### File change map + +| File | Action | Change | +|------|--------|--------| +| `src/types/config.ts` | MODIFY | `OcxComboReasoningEffortMode`; `reasoningEffortMode?` on `OcxComboConfig` next to `defaultEffort` (:773) | +| `src/types.ts` | MODIFY | re-export the new type | +| `src/combos/types.ts` | MODIFY | field on `NormalizedComboConfig`; validate in `comboConfigIssues` (:175); normalize in `normalizeComboConfig` (:353) defaulting to `strict` | +| `src/codex/catalog/aggregation.ts` | MODIFY | under `adaptive`, drop zero-length ladders before `intersectStrings` | +| `src/adapters/openai-chat.ts` | MODIFY | `omitReasoningEffortWithTools` guard at :1428 and on the `gateway-object` branch | +| `src/types/provider.ts` | MODIFY | `omitReasoningEffortWithToolsModels?: string[]` beside `noStructuredOutputModels` (:506) | +| `src/config.ts` | MODIFY | zod schema (:516) + `nonBlankStringArrayConfigError` superRefine (:1216) | +| `src/server/auth-cors.ts` | MODIFY | `providerManagementConfigError` validation + `safeConfigDTO` exposure | +| `src/server/management/provider-routes.ts` | MODIFY | PATCH field handling + GET projection | +| `src/server/management/combo-routes.ts` | MODIFY | **NEW vs PR** — destructure `reasoningEffortMode` out of `normalizedBase`; persist only when `"adaptive"` | +| `gui/src/combo-workspace-data.ts` | MODIFY | **NEW vs PR** — `reasoningEffortMode` on `ComboItem`; read in `parseCombos`; emit in `toPutBody`; add to `baselineSyncKey` and the dirty comparison; teach `intersectComboEfforts` the mode | +| `gui/src/components/combo-workspace-detail-panel.tsx` | MODIFY | **NEW vs PR** — opt-in toggle directly under the Default-reasoning field | +| `gui/src/i18n/*.ts` | MODIFY | **NEW vs PR** — label + hint for all locales | +| `docs-site/.../combos.md`, `routing.md`, `providers.md` | MODIFY | as in the PR, minus the stale strategy list | +| `tests/codex-catalog.test.ts` | MODIFY | adaptive keeps sibling ladder; strict unchanged | +| `tests/combos.test.ts` | MODIFY | validation + normalization default | +| `tests/combo-management-api.test.ts` | MODIFY | round-trip; strict is NOT persisted | +| `tests/combo-workspace-data.test.ts` | MODIFY | `toPutBody` preserves the mode; GUI intersection honors it | +| `tests/openai-chat-hardening.test.ts` | MODIFY | tools present/absent, sibling model unaffected | + +### UX decision + +The control goes in the combo detail panel immediately below "Default reasoning", +because that is the field whose options the mode changes. Default state is the +current behavior. The hint says what turning it on does in one sentence, in the +user's terms: targets that have no reasoning control stop hiding the control for +the rest of the group. + +No new top-level navigation, no new settings page. The concern already has a home. + +### Scope boundary + +IN: the two defects, GUI round-trip, sparse persistence, docs, focused tests. + +OUT: `concreteComboRequestBody` per-target effort stripping (investigator concern +2) — that is a genuine gap but it is request-path routing behavior for combos +generally, not this issue's picker/wire problem, and folding it in here would +expand a +180 diff into cross-module routing work. Record it as follow-up. +OUT: an adapter-type guard on the provider key (concern 7) — it is a no-op outside +`openai-chat` today. + +### Accept criteria + +1. Unset config: catalog rows and wire bodies byte-identical to `e40245e4c`. + Activation: run the existing strict-mode assertions in + `tests/codex-catalog.test.ts` unchanged. +2. `adaptive` set: a combo with one `[]`-ladder member publishes the surviving + sibling intersection instead of `[]`. Activation: new case asserting a non-empty + ladder where the strict case asserts `[]`. +3. Tool-bearing request to a listed model omits `reasoning_effort`; the same model + without tools still sends it; an unlisted sibling always sends it. Activation: + three assertions in `tests/openai-chat-hardening.test.ts`. +4. Dashboard round-trip preserves `adaptive`. Activation: `toPutBody` on an item + parsed from a combo carrying the mode still contains it. +5. A strict combo saved through the API writes no `reasoningEffortMode` key. + +### Verifier + +`bun x tsc --noEmit` (whole-project, reads every file above) plus the five named +test files run individually by path. The full suite is forbidden by the operator. diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/011_wp1_audit_r1_synthesis.md b/devlog/_plan/260902_nonbug_adoption_backlog/011_wp1_audit_r1_synthesis.md new file mode 100644 index 0000000000..a801485df9 --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/011_wp1_audit_r1_synthesis.md @@ -0,0 +1,82 @@ +# wp1 audit round 1 — synthesis + +Reviewer: grok-4.6 adversarial lane (agent `01a05df6`). +Verdict: `GO-WITH-FIXES (blockers=3)`. All three accepted and folded. No rebuttals. + +A first reviewer (`01a05de5`) produced nothing across four wait cycles and was +retired under DISPATCH-RETIRE-01; this is the replacement's round. + +The reviewer independently confirmed the central blocker — `toPutBody` allowlist +plus wholesale `nextCombos[id] = stored` — so the plan's justification stands. + +## Blocker 1 — GET echo, not just disk churn (ACCEPTED) + +I planned to sparsify only the PUT persist destructure. The reviewer found +`sparseComboConfig` (`src/server/management/combo-routes.ts:71-78`), which the GET +list handler (`:84-91`) and the PUT response (`:248`) both run, and which today +strips exactly one default: `imageInput: "auto"`. + +Since `getCombo` returns an already-normalized combo, every GET row would echo +`"reasoningEffortMode": "strict"` for users who never opted in. Worse, any client +that round-trips GET into PUT would then write that default straight back to disk, +defeating the persist-side fix entirely. + +Correction: extend `sparseComboConfig` itself to drop `reasoningEffortMode: "strict"`, +which fixes GET, the PUT response, and the round-trip in one place. The persist-side +destructure becomes redundant — use the shared helper rather than two rules that can +drift. + +## Blocker 2 — wrong file for the sync key, two GUI sites missing (ACCEPTED) + +`baselineSyncKey` is at `gui/src/components/combo-workspace-detail-panel.tsx:90`, +not in `combo-workspace-data.ts` as my map said. Following the map as written would +ship a toggle whose draft never resyncs when only the mode changes. + +Also missing: `emptyDraft` (`combo-workspace-data.ts:619-631`), `draftEquals` +(`:478-488`), and the create path — `combo-workspace-add-modal.tsx:50-52` calls +`intersectComboEfforts` with no mode, so a mixed group's picker still reads empty +while the user is creating the combo. Fixing only the detail panel leaves the +create flow lying at exactly the moment the user is assembling the mixed group +that motivates the feature. + +## Blocker 3 — criteria that pass without the branch firing (ACCEPTED) + +- Criterion 3 asserted only the plain `reasoning_effort` path, but the PR also + edits the `reasoningWireFormat === "gateway-object"` branch + (`src/adapters/openai-chat.ts:1498`). That branch could be left stale and the + criterion would still go green. Now requires a gateway-object provider assertion. +- Criterion 4 was a `toPutBody` unit assertion, which passes even if PUT still + drops the field and GET still echoes `strict`. Now requires the management + round-trip: PUT `adaptive` then GET and see it survive. +- Criterion 1 proved the catalog default, not wire byte-identity. Now split: a + catalog assertion and an adapter assertion that an unlisted model's body is + unchanged. + +## UX correction (reviewer point 6 — accepted, changes the design) + +I had placed the toggle under "Default reasoning". The reviewer's objection is +correct and it is not cosmetic: `defaultEffort` is a *value chosen from* the +intersection, whereas this is a *policy that changes what the intersection is* — +the same kind of thing as `imageInput`, which already lives in `ComboCapabilities` +as a switch (`gui/src/components/combo-workspace-controls.tsx:100-136`). + +Placing a policy switch under a value dropdown invites the reading "this changes +my default effort", which is precisely what it does not do. + +Revised: the toggle goes in `ComboCapabilities` beside the image-input switch, +where combo-wide capability policy already lives. That is also the component the +create modal and the detail panel share, so the create flow gets the control for +free — which is what blocker 2 requires anyway. + +## Not folded + +`buildOpenAIChatPassthroughRequest` (`openai-chat.ts:108-142`) ignores this key — +but it equally ignores `noReasoningModels` today, so it is a pre-existing passthrough +boundary, not a regression this unit introduces. Recorded, not fixed here. + +Docs wording: the reviewer notes "unsupported effort fields are omitted" overclaims, +because Responses does not run `stripEmptyLadderEffort` (Chat-only, +`src/server/chat-completions.ts:191-196`). The docs sentence will be narrowed to +what is true rather than the feature being expanded. + +Line drift: plan cited `openai-chat.ts:1428`; live reasoning block is `:1494-1498`. diff --git a/docs-site/src/content/docs/guides/combos.md b/docs-site/src/content/docs/guides/combos.md index 020c5f8d39..99c8c86963 100644 --- a/docs-site/src/content/docs/guides/combos.md +++ b/docs-site/src/content/docs/guides/combos.md @@ -240,6 +240,37 @@ default and leaves the target's own behavior unchanged. Supported values are `lo `high`, `xhigh`, `max`, and `ultra`; omit the field or set it to `null` to leave effort entirely to the caller and target. +### Mixed-capability groups (`reasoningEffortMode`) + +The effort levels a combo advertises are the intersection of what its targets advertise. A target +that explicitly advertises **no** effort control takes part in that intersection, so a single +no-effort backup empties the effort picker for the whole combo — including for the targets that do +support tuning. + +Set `reasoningEffortMode: "adaptive"` to exclude those empty ladders from the published +intersection instead. The picker then shows the levels the remaining targets share, and the +no-effort target stays eligible for routing. Targets whose ladder is simply *unknown* are treated +as wildcards in both modes. + +```json +{ + "combos": { + "mixed": { + "targets": [ + { "provider": "openai-apikey", "model": "gpt-5.6-luna" }, + { "provider": "local", "model": "no-effort-model" } + ], + "reasoningEffortMode": "adaptive" + } + } +} +``` + +The default is `"strict"`, which keeps the original behavior. This setting changes published +catalog metadata only — it does not change target order, failover policy, or which effort a given +target receives at dispatch. In the dashboard it is the **Adaptive reasoning ladder** switch in a +combo's Capabilities section. + ## Image / multimodal capability By default a combo publishes the **intersection** of its targets' input modalities (image is @@ -346,6 +377,7 @@ Combos are stored in the top-level `combos` object, keyed by combo id: | `strategy` | No | `"failover"` | `"failover"`, `"round-robin"`, `"random"`, `"least-used"`, or `"reset-window"`. | | `stickyLimit` | No | `1` | Integer from 1 to 100 successful requests per round-robin selection. Applies only to round-robin. | | `defaultEffort` | No | `null` | `low`, `medium`, `high`, `xhigh`, `max`, or `ultra`; applied only when the caller omits effort and the target advertises support. | +| `reasoningEffortMode` | No | `"strict"` | `"strict"` intersects every known target ladder, so one target advertising no effort control empties the combo's picker. `"adaptive"` excludes those empty ladders from the published intersection. Metadata only; dispatch is unchanged. | | `imageInput` | No | `"auto"` | `"auto"` or `"disabled"`. `"auto"` publishes image support only when every target supports images; `"disabled"` forces text-only (drops image from published modalities and rejects image-bearing requests before dispatch). | | `alias` | No | none | Optional trimmed public model id; use the alias rules above. An empty value is stored as no alias. | | `nativeAlias` | No | `false` | Explicitly permit a currently supported bare native `alias` to take routing and catalog precedence. Never inferred from the alias. | diff --git a/docs-site/src/content/docs/reference/configuration/providers.md b/docs-site/src/content/docs/reference/configuration/providers.md index 1fdf9bdbcd..7b68b69654 100644 --- a/docs-site/src/content/docs/reference/configuration/providers.md +++ b/docs-site/src/content/docs/reference/configuration/providers.md @@ -115,6 +115,7 @@ differing backup and rewrites known legacy namespaced selected ids to bare ids. | `noTopPModels?` | `string[]` | Models that reject caller-specified `top_p`. | | `noPenaltyModels?` | `string[]` | Models that reject presence/frequency penalties. | | `noStructuredOutputModels?` | `string[]` | Exact model IDs whose `openai-chat` endpoint rejects `response_format`. Only an exact requested-model match omits the field; structured-output translation stays enabled for every other `openai-chat` model. | +| `omitReasoningEffortWithToolsModels?` | `string[]` | Exact `openai-chat` model IDs that accept a reasoning-effort field on an ordinary turn but reject it once function tools are present. The model keeps its advertised effort ladder; OpenCodex omits the wire field for tool-bearing requests only and the upstream default applies. Narrower than `noReasoningModels`, which strips reasoning from every request and costs the model its picker entirely. | | `parallelToolCalls?` | `boolean` | Toggle parallel tool calls. OpenAI Chat defaults on; non-chat adapters advertise only on explicit `true`. | | `terminalContinuationGuard?` | `boolean` | Opt in an `openai-chat` provider to one bounded internal re-ask when an actionable turn announces work, then cleanly stops without a tool call. Defaults to `false`; explicit `false` behaves like omission. Combo attempts and routed compaction turns are excluded, and non-`openai-chat` adapters ignore this option. | | `responsesItemIdRepair?` | `{ message?: string[]; reasoning?: string[]; repairMissingTerminalIds?: boolean; repairInvalidIds?: boolean }` | Disabled-by-default downstream SSE repair for exact placeholder ids, missing terminal ids, and (with `repairInvalidIds`) message/reasoning ids missing the canonical `msg_`/`rs_` prefix. Function-call ids are never rewritten. Built-in DeepSeek enables the last two by default. | diff --git a/docs-site/src/content/docs/reference/configuration/routing.md b/docs-site/src/content/docs/reference/configuration/routing.md index f254b54708..4bac3170e9 100644 --- a/docs-site/src/content/docs/reference/configuration/routing.md +++ b/docs-site/src/content/docs/reference/configuration/routing.md @@ -89,6 +89,7 @@ namespace, and cannot use reserved bare native families such as `gpt-*`, `o1-*`, | `strategy?` | `"failover" \| "round-robin" \| "random" \| "least-used" \| "reset-window"` | `"failover"` | Selection strategy. Target order is failover priority; weights shape round-robin and random draws; least-used follows recorded successes; reset-window follows the soonest quota reset. | | `stickyLimit?` | `number` | `1` | Successful requests retained in one round-robin batch. Range 1–100. Applies only to round-robin. | | `defaultEffort?` | `"low" \| "medium" \| "high" \| "xhigh" \| "max" \| "ultra" \| null` | unset | Applied only when the caller omits effort and the selected target advertises the requested rung. | +| `reasoningEffortMode?` | `"strict" \| "adaptive"` | `"strict"` | `"strict"` intersects every known target effort ladder, so a target advertising no effort control empties the combo's picker. `"adaptive"` excludes those empty ladders from the published intersection. Picker metadata only; target selection and dispatch are unchanged. | | `imageInput?` | `"auto" \| "disabled"` | `"auto"` | `"auto"` publishes image only when every target supports images; `"disabled"` forces text-only (drops image from published modalities and rejects image-bearing requests before dispatch). | | `alias?` | `string` | — | Optional public model id in place of the canonical picker slug. | | `nativeAlias?` | `boolean` | `false` | Let a currently supported bare native id take precedence only for that unqualified id. Bare `gpt-5.6-*` ids use Codex Pool/Direct credentials. Account-qualified routes remain distinct. Provider-qualified routes such as `openai-apikey/gpt-5.6-*` use their configured API-key route and never fall through to the native alias. | diff --git a/gui/src/combo-workspace-data.ts b/gui/src/combo-workspace-data.ts index d6d9ea204f..bf8b881c55 100644 --- a/gui/src/combo-workspace-data.ts +++ b/gui/src/combo-workspace-data.ts @@ -54,6 +54,7 @@ const COMBO_STRATEGY_SET = new Set(COMBO_STRATEGIES); export function intersectComboEfforts( targets: readonly ComboTarget[], modelEfforts: ReadonlyMap, + reasoningEffortMode: "strict" | "adaptive" = "strict", ): ComboEffort[] { const complete = targets.filter((t) => t.provider.trim() && t.model.trim()); if (complete.length === 0) return [...COMBO_EFFORTS]; @@ -63,6 +64,9 @@ export function intersectComboEfforts( const key = `${target.provider.trim()}/${target.model.trim()}`; const listed = modelEfforts.get(key); if (listed === undefined) continue; + // Adaptive mirrors the served catalog: a target advertising no effort control is + // excluded from the intersection rather than collapsing it for every sibling. + if (reasoningEffortMode === "adaptive" && listed.length === 0) continue; const member = listed.filter((effort) => effortSet.has(effort)); if (common === null) { common = member; @@ -106,6 +110,10 @@ function normalizeImageInput(value: unknown): "auto" | "disabled" { return value === "disabled" ? "disabled" : "auto"; } +function normalizeReasoningEffortMode(value: unknown): "strict" | "adaptive" { + return value === "adaptive" ? "adaptive" : "strict"; +} + export interface ComboItem { id: string; /** Wire id shown to clients, e.g. combo/free */ @@ -120,6 +128,11 @@ export interface ComboItem { stickyLimit: number; defaultEffort: ComboEffort | null; imageInput?: "auto" | "disabled"; + /** + * Picker-ladder policy. `adaptive` lets targets that advertise no effort control drop + * out of the intersection instead of emptying it for the whole group. + */ + reasoningEffortMode?: "strict" | "adaptive"; targets: ComboTarget[]; } @@ -228,6 +241,7 @@ export function parseComboList(payload: unknown): ComboItem[] { stickyLimit: normalizeStickyLimit(r.stickyLimit), defaultEffort: normalizeDefaultEffort(r.defaultEffort), imageInput: normalizeImageInput(r.imageInput), + reasoningEffortMode: normalizeReasoningEffortMode(r.reasoningEffortMode), targets, }); } @@ -485,6 +499,7 @@ export function draftEquals(a: ComboItem, b: ComboItem): boolean { || a.stickyLimit !== b.stickyLimit || a.defaultEffort !== b.defaultEffort || (a.imageInput ?? "auto") !== (b.imageInput ?? "auto") + || (a.reasoningEffortMode ?? "strict") !== (b.reasoningEffortMode ?? "strict") ) return false; if (a.targets.length !== b.targets.length) return false; return a.targets.every((t, i) => { @@ -502,6 +517,7 @@ export function toPutBody(item: ComboItem, options: { renameFrom?: string } = {} stickyLimit?: number; defaultEffort: ComboEffort | null; imageInput?: "disabled"; + reasoningEffortMode?: "adaptive"; alias?: string; nativeAlias?: true; displayName?: string; @@ -518,6 +534,7 @@ export function toPutBody(item: ComboItem, options: { renameFrom?: string } = {} strategy: item.strategy, defaultEffort: item.defaultEffort, ...(item.imageInput === "disabled" ? { imageInput: "disabled" as const } : {}), + ...(item.reasoningEffortMode === "adaptive" ? { reasoningEffortMode: "adaptive" as const } : {}), ...(item.strategy === "round-robin" ? { stickyLimit: item.stickyLimit } : {}), ...(item.alias && item.alias.trim() ? { alias: item.alias.trim() } : {}), ...(item.nativeAlias ? { nativeAlias: true } : {}), @@ -627,6 +644,7 @@ export function emptyDraft(id = ""): ComboItem { stickyLimit: 1, defaultEffort: null, imageInput: "auto", + reasoningEffortMode: "strict", targets: [newComboTarget()], }; } diff --git a/gui/src/components/combo-workspace-add-modal.tsx b/gui/src/components/combo-workspace-add-modal.tsx index 57f6253509..67a0f4f36d 100644 --- a/gui/src/components/combo-workspace-add-modal.tsx +++ b/gui/src/components/combo-workspace-add-modal.tsx @@ -48,8 +48,8 @@ export function AddComboModal({ return map; }, [models]); const allowedEfforts = useMemo( - () => intersectComboEfforts(draft.targets, effortMap), - [draft.targets, effortMap], + () => intersectComboEfforts(draft.targets, effortMap, draft.reasoningEffortMode ?? "strict"), + [draft.targets, effortMap, draft.reasoningEffortMode], ); const allTargetsExhausted = comboQuotaState(draft.targets, providerQuotaStates, providerMap) === "exhausted"; @@ -220,6 +220,7 @@ export function AddComboModal({ targets={draft.targets} models={models} imageInput={draft.imageInput ?? "auto"} + reasoningEffortMode={draft.reasoningEffortMode ?? "strict"} disabled={busy} onChange={(patch) => setDraft((d) => ({ ...d, ...patch }))} /> diff --git a/gui/src/components/combo-workspace-controls.tsx b/gui/src/components/combo-workspace-controls.tsx index b9a6acdcb9..7cafea176f 100644 --- a/gui/src/components/combo-workspace-controls.tsx +++ b/gui/src/components/combo-workspace-controls.tsx @@ -101,14 +101,16 @@ export function ComboCapabilities({ targets, models, imageInput, + reasoningEffortMode, disabled, onChange, }: { targets: ComboTarget[]; models: ModelOption[]; imageInput: "auto" | "disabled"; + reasoningEffortMode: "strict" | "adaptive"; disabled?: boolean; - onChange: (patch: { imageInput?: "auto" | "disabled" }) => void; + onChange: (patch: { imageInput?: "auto" | "disabled"; reasoningEffortMode?: "strict" | "adaptive" }) => void; }) { const t = useT(); const imagesSupported = comboImagesSupported(targets, models); @@ -135,6 +137,20 @@ export function ComboCapabilities({ label={t("cws.capability.imageInput")} />
+
+
+ {t("cws.capability.adaptiveEffort")} +

{t("cws.capability.adaptiveEffortHint")}

+
+ { + onChange({ reasoningEffortMode: reasoningEffortMode === "adaptive" ? "strict" : "adaptive" }); + }} + disabled={disabled} + label={t("cws.capability.adaptiveEffort")} + /> +
); } diff --git a/gui/src/components/combo-workspace-detail-panel.tsx b/gui/src/components/combo-workspace-detail-panel.tsx index 32a6625fde..b33d937917 100644 --- a/gui/src/components/combo-workspace-detail-panel.tsx +++ b/gui/src/components/combo-workspace-detail-panel.tsx @@ -87,7 +87,7 @@ export function DetailPanel({ const [copied, setCopied] = useState(false); const dirty = !draftEquals(draft, baseline); const allTargetsExhausted = comboQuotaState(draft.targets, providerQuotaStates, providerMap) === "exhausted"; - const baselineSyncKey = `${baseline.id}:${baseline.alias ?? ""}:${baseline.nativeAlias}:${baseline.displayName ?? ""}:${baseline.strategy}:${baseline.stickyLimit}:${baseline.defaultEffort}:${baseline.imageInput ?? "auto"}:${baseline.targets.map((t) => `${t.provider}/${t.model}:${t.weight ?? 1}`).join(",")}`; + const baselineSyncKey = `${baseline.id}:${baseline.alias ?? ""}:${baseline.nativeAlias}:${baseline.displayName ?? ""}:${baseline.strategy}:${baseline.stickyLimit}:${baseline.defaultEffort}:${baseline.imageInput ?? "auto"}:${baseline.reasoningEffortMode ?? "strict"}:${baseline.targets.map((t) => `${t.provider}/${t.model}:${t.weight ?? 1}`).join(",")}`; const effortMap = useMemo(() => { const map = new Map(); for (const model of models) { @@ -96,8 +96,8 @@ export function DetailPanel({ return map; }, [models]); const allowedEfforts = useMemo( - () => intersectComboEfforts(draft.targets, effortMap), - [draft.targets, effortMap], + () => intersectComboEfforts(draft.targets, effortMap, draft.reasoningEffortMode ?? "strict"), + [draft.targets, effortMap, draft.reasoningEffortMode], ); const updateDraft = useCallback((updater: (prev: ComboItem) => ComboItem) => { @@ -371,6 +371,7 @@ export function DetailPanel({ targets={draft.targets} models={models} imageInput={draft.imageInput ?? "auto"} + reasoningEffortMode={draft.reasoningEffortMode ?? "strict"} disabled={busy} onChange={(patch) => updateDraft((d) => ({ ...d, ...patch }))} /> diff --git a/gui/src/i18n/de.ts b/gui/src/i18n/de.ts index 96f6036caa..eeedbbd1fd 100644 --- a/gui/src/i18n/de.ts +++ b/gui/src/i18n/de.ts @@ -2111,6 +2111,8 @@ export const de: Record = { "cws.capability.imageInputUnavailable": "Erst verfügbar, wenn jedes gewählte Ziel Bildeingabe unterstützt.", "cws.capability.imageInputHint": "Standardmäßig aktiv, wenn jedes Ziel Bilder unterstützt. Ausschalten für nur Text.", "cws.capability.imageInput": "Bild / multimodal", + "cws.capability.adaptiveEffort": "Adaptive Denkstufen", + "cws.capability.adaptiveEffortHint": "Aus: Ziele ohne Denkstufen-Regelung blenden die Auswahl für die gesamte Kombination aus. An: Solche Ziele bleiben nutzbar, und die Auswahl zeigt weiterhin die Stufen der übrigen Ziele.", "cws.capabilities": "Fähigkeiten", "cws.field.defaultEffortUnsupported": "Dieser Aufwand liegt nicht in der gemeinsamen Leiter der Ziele — er wird zur Anfragezeit ignoriert oder angepasst.", "cws.field.defaultEffortUnsupportedOption": "nicht in der Schnittmenge", diff --git a/gui/src/i18n/en.ts b/gui/src/i18n/en.ts index 4e9d1140f9..fa2584e022 100644 --- a/gui/src/i18n/en.ts +++ b/gui/src/i18n/en.ts @@ -2154,6 +2154,8 @@ export const en = { "cws.capability.imageInputUnavailable": "Unavailable until every selected target supports image input.", "cws.capability.imageInputHint": "On by default when every target supports images. Turn off to accept text only.", "cws.capability.imageInput": "Image / multimodal", + "cws.capability.adaptiveEffort": "Adaptive reasoning ladder", + "cws.capability.adaptiveEffortHint": "Off: a target with no reasoning control hides the effort picker for the whole combo. On: those targets stay usable and the picker keeps the levels the remaining targets share.", "cws.capabilities": "Capabilities", "cws.field.defaultEffortUnsupported": "This effort is not in the targets' common ladder — it will be ignored or snapped at request time.", "cws.field.defaultEffortUnsupportedOption": "not in intersection", diff --git a/gui/src/i18n/fr.ts b/gui/src/i18n/fr.ts index 619dd2cd93..9bb7443b3d 100644 --- a/gui/src/i18n/fr.ts +++ b/gui/src/i18n/fr.ts @@ -2078,6 +2078,8 @@ export const fr: Record = { "cws.capability.imageInputUnavailable": "Indisponible tant que toutes les cibles sélectionnées ne prennent pas en charge les images.", "cws.capability.imageInputHint": "Activé par défaut lorsque toutes les cibles prennent en charge les images. Désactivez cette option pour n’accepter que du texte.", "cws.capability.imageInput": "Images / multimodal", + "cws.capability.adaptiveEffort": "Échelle de raisonnement adaptative", + "cws.capability.adaptiveEffortHint": "Désactivé : une cible sans réglage de raisonnement masque le sélecteur pour toute la combinaison. Activé : ces cibles restent utilisables et le sélecteur conserve les niveaux communs aux autres cibles.", "cws.capabilities": "Capacités", "cws.allCombos": "Toutes les combinaisons", "cws.copyModel": "Copier l’identifiant", diff --git a/gui/src/i18n/ja.ts b/gui/src/i18n/ja.ts index d2b008a142..a530d1e2d4 100644 --- a/gui/src/i18n/ja.ts +++ b/gui/src/i18n/ja.ts @@ -2170,6 +2170,8 @@ export const ja: Record = { "cws.capability.imageInputUnavailable": "選択した全ターゲットが画像入力に対応すると有効になります。", "cws.capability.imageInputHint": "全ターゲットが画像対応なら既定でオン。オフにするとテキストのみ。", "cws.capability.imageInput": "画像 / マルチモーダル", + "cws.capability.adaptiveEffort": "適応的な推論レベル", + "cws.capability.adaptiveEffortHint": "オフ: 推論レベルを持たない対象があると、コンボ全体のセレクターが消えます。オン: その対象はそのまま使え、セレクターには残りの対象で共通するレベルが表示されます。", "cws.capabilities": "能力", "cws.field.defaultEffortUnsupported": "この負荷はターゲット共通の階段にありません — リクエスト時に無視またはスナップされます。", "cws.field.defaultEffortUnsupportedOption": "交差に含まれない", diff --git a/gui/src/i18n/ko.ts b/gui/src/i18n/ko.ts index b8cfd99a2c..972b9724d9 100644 --- a/gui/src/i18n/ko.ts +++ b/gui/src/i18n/ko.ts @@ -2138,6 +2138,8 @@ export const ko: Record = { "cws.capability.imageInputUnavailable": "선택한 모든 대상이 이미지 입력을 지원해야 사용할 수 있습니다.", "cws.capability.imageInputHint": "모든 대상이 이미지를 지원하면 기본으로 켜집니다. 끄면 텍스트만 허용합니다.", "cws.capability.imageInput": "이미지 / 멀티모달", + "cws.capability.adaptiveEffort": "적응형 추론 단계", + "cws.capability.adaptiveEffortHint": "끔: 추론 단계를 조절할 수 없는 대상이 하나라도 있으면 콤보 전체의 선택기가 사라집니다. 켬: 그런 대상도 그대로 쓰면서, 선택기에는 나머지 대상이 공통으로 지원하는 단계가 남습니다.", "cws.capabilities": "기능", "cws.field.defaultEffortUnsupported": "이 수준은 대상의 공통 사다리에 없습니다 — 요청 시 무시되거나 스냅됩니다.", "cws.field.defaultEffortUnsupportedOption": "교집합에 없음", diff --git a/gui/src/i18n/ru.ts b/gui/src/i18n/ru.ts index c6b9db06fc..01e022992a 100644 --- a/gui/src/i18n/ru.ts +++ b/gui/src/i18n/ru.ts @@ -2221,6 +2221,8 @@ export const ru: Record = { "cws.capability.imageInputUnavailable": "Доступно, когда все выбранные цели поддерживают ввод изображений.", "cws.capability.imageInputHint": "Включено по умолчанию, если все цели поддерживают изображения. Выключите, чтобы принимать только текст.", "cws.capability.imageInput": "Изображения / мультимодальность", + "cws.capability.adaptiveEffort": "Адаптивная шкала рассуждений", + "cws.capability.adaptiveEffortHint": "Выкл.: цель без настройки рассуждений скрывает выбор уровня для всей комбинации. Вкл.: такие цели остаются доступными, а в выборе сохраняются уровни, общие для остальных целей.", "cws.capabilities": "Возможности", "cws.field.defaultEffortUnsupported": "Этот уровень не входит в общую лестницу целей — при запросе он будет проигнорирован или снижен.", "cws.field.defaultEffortUnsupportedOption": "нет в пересечении", diff --git a/gui/src/i18n/tr.ts b/gui/src/i18n/tr.ts index e8591fce8a..eefd38b920 100644 --- a/gui/src/i18n/tr.ts +++ b/gui/src/i18n/tr.ts @@ -2141,6 +2141,8 @@ export const tr: Record = { "cws.capability.imageInputUnavailable": "Seçilen tüm hedefler görsel girişini destekleyene kadar kullanılamaz.", "cws.capability.imageInputHint": "Tüm hedefler görselleri desteklediğinde varsayılan olarak açıktır. Yalnızca metin kabul etmek için kapatın.", "cws.capability.imageInput": "Görsel / çok modlu", + "cws.capability.adaptiveEffort": "Uyarlanabilir akıl yürütme düzeyi", + "cws.capability.adaptiveEffortHint": "Kapalı: akıl yürütme denetimi olmayan bir hedef, tüm kombinasyonun seçicisini gizler. Açık: bu hedefler kullanılabilir kalır ve seçici, kalan hedeflerin ortak düzeylerini gösterir.", "cws.capabilities": "Yetenekler", "cws.field.defaultEffortUnsupported": "Bu çaba hedeflerin ortak merdiveninde yok — istek anında yok sayılacak veya uydurulacaktır.", "cws.field.defaultEffortUnsupportedOption": "kesişimde değil", diff --git a/gui/src/i18n/zh-TW.ts b/gui/src/i18n/zh-TW.ts index a86c977775..0598f2373b 100644 --- a/gui/src/i18n/zh-TW.ts +++ b/gui/src/i18n/zh-TW.ts @@ -1648,6 +1648,8 @@ export const zhTW: Record = { "cws.capability.imageInputUnavailable": "所有已選目標都支援圖片輸入後才可使用。", "cws.capability.imageInputHint": "所有目標都支援圖片時預設開啟;關閉後僅接受文字。", "cws.capability.imageInput": "圖片 / 多模態", + "cws.capability.adaptiveEffort": "自適應推理層級", + "cws.capability.adaptiveEffortHint": "關閉:只要有一個目標不支援推理層級,整個組合的選擇器都會消失。開啟:這些目標仍可使用,選擇器保留其餘目標共有的層級。", "cws.capabilities": "功能", "cws.field.defaultEffortUnsupported": "此 effort 不在目標的共同階梯中 — 請求時會被忽略或就近對應。", "cws.field.defaultEffortUnsupportedOption": "不在交集中", diff --git a/gui/src/i18n/zh.ts b/gui/src/i18n/zh.ts index 17c013b907..c2c4318a35 100644 --- a/gui/src/i18n/zh.ts +++ b/gui/src/i18n/zh.ts @@ -2131,6 +2131,8 @@ export const zh: Record = { "cws.capability.imageInputUnavailable": "所有已选目标均支持图片输入后才可用。", "cws.capability.imageInputHint": "所有目标均支持图片时默认开启;关闭后仅接受文本。", "cws.capability.imageInput": "图片 / 多模态", + "cws.capability.adaptiveEffort": "自适应推理档位", + "cws.capability.adaptiveEffortHint": "关闭:只要有一个目标不支持推理档位,整个组合的选择器都会消失。开启:这些目标仍可使用,选择器保留其余目标共有的档位。", "cws.capabilities": "能力", "cws.field.defaultEffortUnsupported": "该级别不在目标的公共阶梯中 — 请求时会被忽略或就近映射。", "cws.field.defaultEffortUnsupportedOption": "不在交集中", diff --git a/src/adapters/openai-chat.ts b/src/adapters/openai-chat.ts index ad90a522f4..8b7d9c8614 100644 --- a/src/adapters/openai-chat.ts +++ b/src/adapters/openai-chat.ts @@ -1492,10 +1492,18 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd } if (parsed.options.stopSequences !== undefined) body.stop = parsed.options.stopSequences; const reasoningDisabled = modelInList(provider.noReasoningModels, parsed.modelId); - const reasoningEffort = mapReasoningEffort(provider, parsed.modelId, parsed.options.reasoning); + // Some gateways accept a reasoning-effort field on a plain turn but reject the + // effort + tools combination. `noReasoningModels` would fix that only by + // stripping reasoning everywhere, costing the model its whole picker. This keeps + // the ladder advertised and drops the wire field for tool-bearing requests only. + const omitReasoningEffortWithTools = !!tools + && modelInList(provider.omitReasoningEffortWithToolsModels, parsed.modelId); + const reasoningEffort = omitReasoningEffortWithTools + ? undefined + : mapReasoningEffort(provider, parsed.modelId, parsed.options.reasoning); const nativeOpenAI = isNativeOpenAIChatTarget(provider); let reasoningLog: AdapterRequest["reasoningLog"]; - if (!reasoningDisabled && provider.reasoningWireFormat === "gateway-object" && parsed.options.reasoning === "none") { + if (!reasoningDisabled && !omitReasoningEffortWithTools && provider.reasoningWireFormat === "gateway-object" && parsed.options.reasoning === "none") { if (nativeOpenAI) { body.reasoning_effort = "none"; reasoningLog = { diff --git a/src/codex/catalog/aggregation.ts b/src/codex/catalog/aggregation.ts index 44ee1c07fa..e43fddbf1d 100644 --- a/src/codex/catalog/aggregation.ts +++ b/src/codex/catalog/aggregation.ts @@ -134,10 +134,18 @@ export function deriveComboCatalogModel( : derivedInputModalities; if (inputModalities.length === 0) return null; // Unknown ladders (`undefined`) are wildcards for catalog derivation — same - // boundary as the GUI picker. An explicit empty ladder still constrains. - const advertisedLadders = members + // boundary as the GUI picker. Under the default `strict` mode an explicit empty + // ladder still constrains, so one target that advertises no effort control empties + // the whole combo's picker. `adaptive` is the opt-in for mixed-capability groups: + // empty ladders drop out of the published intersection while non-empty ladders + // still define it. Dispatch is unaffected either way — each concrete target + // resolves its own effort at request time. + const knownLadders = members .map(member => member.reasoningEfforts) .filter((ladder): ladder is string[] => ladder !== undefined); + const advertisedLadders = combo.reasoningEffortMode === "adaptive" + ? knownLadders.filter(ladder => ladder.length > 0) + : knownLadders; const reasoningEfforts = advertisedLadders.length === 0 ? [] : intersectStrings(advertisedLadders); diff --git a/src/combos/types.ts b/src/combos/types.ts index cd65e1c35e..b3dec16d09 100644 --- a/src/combos/types.ts +++ b/src/combos/types.ts @@ -3,6 +3,7 @@ import { SUPPORTED_NATIVE_OPENAI_SLUGS } from "../codex/catalog/native-models"; import type { OcxComboConfig, OcxComboDefaultEffort, + OcxComboReasoningEffortMode, OcxComboStrategy, OcxComboTarget, OcxConfig, @@ -37,6 +38,8 @@ export interface NormalizedComboConfig { strategy: OcxComboStrategy; stickyLimit: number; defaultEffort: OcxComboDefaultEffort | null; + /** Picker-ladder derivation policy; `strict` preserves the legacy intersection rule. */ + reasoningEffortMode: OcxComboReasoningEffortMode; /** Disable image input; `auto` preserves the intersection derived from all targets. */ imageInput: "auto" | "disabled"; /** Trimmed public alias, or null when the combo keeps the default `combo/` slug. */ @@ -238,6 +241,14 @@ export function comboConfigIssues( if (body.imageInput !== undefined && body.imageInput !== "auto" && body.imageInput !== "disabled") { issues.push({ path: ["imageInput"], message: 'imageInput must be "auto" or "disabled"' }); } + if (body.reasoningEffortMode !== undefined + && body.reasoningEffortMode !== "strict" + && body.reasoningEffortMode !== "adaptive") { + issues.push({ + path: ["reasoningEffortMode"], + message: 'reasoningEffortMode must be "strict" or "adaptive"', + }); + } if (body.alias !== undefined) { if (typeof body.alias !== "string") { @@ -357,6 +368,7 @@ export function normalizeComboConfig(raw: OcxComboConfig): NormalizedComboConfig strategy: raw.strategy ?? "failover", stickyLimit: raw.stickyLimit ?? 1, defaultEffort: raw.defaultEffort ?? null, + reasoningEffortMode: raw.reasoningEffortMode === "adaptive" ? "adaptive" : "strict", imageInput: raw.imageInput === "disabled" ? "disabled" : "auto", alias: alias || null, nativeAlias: raw.nativeAlias === true, diff --git a/src/config.ts b/src/config.ts index 7d5f36e6a8..f68244d636 100644 --- a/src/config.ts +++ b/src/config.ts @@ -529,6 +529,9 @@ const providerConfigSchema = z.object({ noStructuredOutputModels: z.array(z.string().min(1)) .transform(normalizeNonBlankStringArray) .optional(), + omitReasoningEffortWithToolsModels: z.array(z.string().min(1)) + .transform(normalizeNonBlankStringArray) + .optional(), retryOn429: retryOn429PolicySchema.optional(), transientRetryOn5xx: transientRetryOn5xxPolicySchema.optional(), codexAccountMode: z.enum(["pool", "direct"]).optional(), @@ -1359,6 +1362,17 @@ const configSchema = z.object({ message: structuredOutputOptOutError, }); } + const toolReasoningOptOutError = nonBlankStringArrayConfigError( + (provider as { omitReasoningEffortWithToolsModels?: unknown }).omitReasoningEffortWithToolsModels, + "omitReasoningEffortWithToolsModels", + ); + if (toolReasoningOptOutError) { + ctx.addIssue({ + code: "custom", + path: ["providers", redactSecretString(name), "omitReasoningEffortWithToolsModels"], + message: toolReasoningOptOutError, + }); + } if (Object.hasOwn(provider, "codexAccountMode") && provider.codexAccountMode !== undefined) { // Persisted account mode is valid ONLY on the canonical built-in `openai` forward provider. // Old openai-multi rows stay parseable (they never carry a mode) so startup can migrate them. diff --git a/src/server/auth-cors.ts b/src/server/auth-cors.ts index 82ee28c56a..934e09ae59 100644 --- a/src/server/auth-cors.ts +++ b/src/server/auth-cors.ts @@ -694,6 +694,11 @@ export function providerManagementConfigError(name: unknown, provider: unknown): "noStructuredOutputModels", ); if (structuredOutputOptOutError) return `provider ${name} ${structuredOutputOptOutError}`; + const toolReasoningOptOutError = nonBlankStringArrayConfigError( + raw.omitReasoningEffortWithToolsModels, + "omitReasoningEffortWithToolsModels", + ); + if (toolReasoningOptOutError) return `provider ${name} ${toolReasoningOptOutError}`; const openRouterError = openRouterRoutingConfigError(typed); if (openRouterError) return `provider ${name} ${openRouterError}`; const vercelError = vercelGatewayRoutingConfigError(typed); @@ -787,6 +792,7 @@ export function safeConfigDTO(config: OcxConfig): unknown { "noTopPModels", "noPenaltyModels", "noStructuredOutputModels", + "omitReasoningEffortWithToolsModels", "upstreamHttpVersion", "autoToolChoiceOnlyModels", "preserveReasoningContentModels", diff --git a/src/server/management/combo-routes.ts b/src/server/management/combo-routes.ts index 192ca54750..72282d5445 100644 --- a/src/server/management/combo-routes.ts +++ b/src/server/management/combo-routes.ts @@ -68,12 +68,24 @@ import { readManagementJsonBody, rethrowManagementBodyTooLarge } from "./body"; import { shadowCallTargetError } from "./shadow-call-validation"; -/** Management wire shape: omit default imageInput "auto" (persist/response sparse). */ -function sparseComboConfig(combo: T): Omit & { imageInput?: "disabled" } { - const { imageInput, ...rest } = combo; +/** + * Management wire shape: omit fields whose value is the default, so GET responses and + * persisted config stay sparse. A default echoed here would be written straight back by + * any client that round-trips GET into PUT, which is how an unset option ends up + * materialized in every user's config.json. + */ +function sparseComboConfig(combo: T): Omit & { + imageInput?: "disabled"; + reasoningEffortMode?: "adaptive"; +} { + const { imageInput, reasoningEffortMode, ...rest } = combo; return { ...rest, ...(imageInput === "disabled" ? { imageInput: "disabled" as const } : {}), + ...(reasoningEffortMode === "adaptive" ? { reasoningEffortMode: "adaptive" as const } : {}), }; } @@ -137,19 +149,19 @@ export async function handleComboRoutes(ctx: ManagementContext): Promise 0) next.omitReasoningEffortWithToolsModels = models; + else delete next.omitReasoningEffortWithToolsModels; + } + touched = true; + } // headers is the one object-valued field in the mask. PATCH semantics merge it // shallowly into the existing block so a single fingerprint header can be added @@ -525,6 +538,7 @@ export async function handleProviderRoutes(ctx: ManagementContext): Promise { expect(empty).not.toHaveProperty("defaultReasoningEffort"); }); + test("adaptive mode keeps the surviving ladder when a target advertises no effort control", () => { + // The strict case above is the baseline: memberB's explicit [] empties the picker for + // the whole combo. Adaptive is the opt-in that excludes it instead, so the effort + // control stays usable for the siblings that do support tuning. + const adaptive = deriveComboCatalogModel( + "adaptive", + normalizedCombo({ defaultEffort: "medium", reasoningEffortMode: "adaptive" }), + [memberA, { ...memberB, reasoningEfforts: [] }], + ); + expect(adaptive?.reasoningEfforts).toEqual(["low", "medium", "high"]); + expect(adaptive?.defaultReasoningEffort).toBe("medium"); + + // Adaptive only drops EMPTY ladders; non-empty ones still intersect normally. + expect(deriveComboCatalogModel( + "adaptive-intersect", + normalizedCombo({ defaultEffort: "medium", reasoningEffortMode: "adaptive" }), + [memberA, { ...memberB, reasoningEfforts: ["medium", "high"] }], + )?.reasoningEfforts).toEqual(["medium", "high"]); + + // Every target empty under adaptive still yields no ladder — there is nothing to keep. + expect(deriveComboCatalogModel( + "adaptive-all-empty", + normalizedCombo({ defaultEffort: "medium", reasoningEffortMode: "adaptive" }), + [{ ...memberA, reasoningEfforts: [] }, { ...memberB, reasoningEfforts: [] }], + )?.reasoningEfforts).toEqual([]); + }); + test("fails closed for missing members, unknown context, duplicate targets, and empty modalities", () => { expect(deriveComboCatalogModel("missing", normalizedCombo(), [memberA])).toBeNull(); expect(deriveComboCatalogModel("context", normalizedCombo(), [ diff --git a/tests/combo-management-api.test.ts b/tests/combo-management-api.test.ts index b0489a9baa..082e58e26b 100644 --- a/tests/combo-management-api.test.ts +++ b/tests/combo-management-api.test.ts @@ -296,6 +296,67 @@ describe("combo management API", () => { }); }); + test("reasoningEffortMode survives a management round-trip and stays sparse when strict", async () => { + await withTempHome(async () => { + const config = baseConfig({ combos: undefined }); + saveConfig(config); + + // Opting in must survive PUT -> disk -> GET. The dashboard replaces the whole combo + // on save, so a field that is not echoed here is a field the UI silently destroys. + const opted = await comboApi(config, "PUT", "/api/combos", { + id: "mixed", + combo: { + targets: [{ provider: "a", model: "m1" }], + reasoningEffortMode: "adaptive", + }, + }); + expect(opted?.status).toBe(200); + expect(await responseJson(opted)).toMatchObject({ + combo: { reasoningEffortMode: "adaptive" }, + }); + expect(config.combos?.mixed).toMatchObject({ reasoningEffortMode: "adaptive" }); + const listed = await responseJson(await comboApi(config, "GET", "/api/combos")); + expect(listed.combos).toEqual([expect.objectContaining({ + id: "mixed", reasoningEffortMode: "adaptive", + })]); + + // The default is never materialized: neither on disk nor in the wire shape, so a + // client that round-trips GET into PUT cannot write it back into every config. + const plain = await comboApi(config, "PUT", "/api/combos", { + id: "plain", + combo: { targets: [{ provider: "a", model: "m1" }] }, + }); + expect((await responseJson(plain)).combo).not.toHaveProperty("reasoningEffortMode"); + expect(config.combos?.plain).not.toHaveProperty("reasoningEffortMode"); + + // Explicitly turning it back off clears the stored field rather than pinning "strict". + await comboApi(config, "PUT", "/api/combos", { + id: "mixed", + combo: { + targets: [{ provider: "a", model: "m1" }], + reasoningEffortMode: "strict", + }, + }); + expect(config.combos?.mixed).not.toHaveProperty("reasoningEffortMode"); + }); + }); + + test("PUT rejects an unknown reasoningEffortMode", async () => { + await withTempHome(async () => { + const config = baseConfig({ combos: undefined }); + saveConfig(config); + const response = await comboApi(config, "PUT", "/api/combos", { + id: "bad", + combo: { + targets: [{ provider: "a", model: "m1" }], + reasoningEffortMode: "aggressive", + }, + }); + expect(response?.status).toBe(400); + expect(config.combos?.bad).toBeUndefined(); + }); + }); + test("PUT stores aliases and GET exposes the public model", async () => { await withTempHome(async () => { const config = baseConfig({ combos: undefined }); diff --git a/tests/combo-workspace-data.test.ts b/tests/combo-workspace-data.test.ts index c62d9b3640..b7d1147161 100644 --- a/tests/combo-workspace-data.test.ts +++ b/tests/combo-workspace-data.test.ts @@ -114,6 +114,7 @@ describe("combo-workspace-data", () => { stickyLimit: 1, defaultEffort: null, imageInput: "auto", + reasoningEffortMode: "strict", targets: [{ provider: "a", model: "m1", weight: 1, clientKey: expect.stringMatching(/^ct-\d+$/) }], }, { @@ -126,6 +127,7 @@ describe("combo-workspace-data", () => { stickyLimit: 4, defaultEffort: "high", imageInput: "auto", + reasoningEffortMode: "strict", targets: [ { provider: "a", model: "m1", weight: 3, clientKey: expect.stringMatching(/^ct-\d+$/) }, { provider: "b", model: "m2", weight: 1, clientKey: expect.stringMatching(/^ct-\d+$/) }, @@ -224,6 +226,49 @@ describe("combo-workspace-data", () => { )).toEqual([]); }); + test("intersectComboEfforts drops empty ladders in adaptive mode", () => { + const map = new Map([ + ["a/m1", ["low", "medium"]], + ["b/no-reasoning", []], + ]); + const targets = [{ provider: "a", model: "m1" }, { provider: "b", model: "no-reasoning" }]; + // The editor must agree with the served catalog: under adaptive the no-effort target + // stops emptying the picker, otherwise the dashboard shows a control the proxy does not. + expect(intersectComboEfforts(targets, map, "adaptive")).toEqual(["low", "medium"]); + // Explicit strict, and the default argument, both keep today's restrictive behavior. + expect(intersectComboEfforts(targets, map, "strict")).toEqual([]); + expect(intersectComboEfforts(targets, map)).toEqual([]); + }); + + test("reasoningEffortMode survives parse and serialize", () => { + // toPutBody is an allowlist and PUT replaces the whole combo, so a field missing here + // is silently destroyed the next time the user edits anything in the dashboard. + const [parsedItem] = parseComboList({ + combos: [{ + id: "mixed", + model: "combo/mixed", + strategy: "failover", + stickyLimit: 1, + defaultEffort: null, + reasoningEffortMode: "adaptive", + targets: [{ provider: "a", model: "m1" }], + }], + }); + expect(parsedItem?.reasoningEffortMode).toBe("adaptive"); + expect(toPutBody(parsedItem!).combo.reasoningEffortMode).toBe("adaptive"); + + // The default stays off the wire so a GET -> PUT round-trip never writes it back. + expect(toPutBody(combo()).combo).not.toHaveProperty("reasoningEffortMode"); + expect(toPutBody(combo({ reasoningEffortMode: "strict" })).combo) + .not.toHaveProperty("reasoningEffortMode"); + }); + + test("draftEquals treats a reasoningEffortMode change as dirty", () => { + // Without this the Save button stays disabled after toggling the switch. + expect(draftEquals(combo(), combo({ reasoningEffortMode: "adaptive" }))).toBe(false); + expect(draftEquals(combo({ reasoningEffortMode: "strict" }), combo())).toBe(true); + }); + test("attention flags zero-target and one-target defensive rows", () => { const attention = buildComboAttention([ combo({ id: "empty", model: "combo/empty", targets: [] }), diff --git a/tests/combos.test.ts b/tests/combos.test.ts index a3f17c20f4..f406894992 100644 --- a/tests/combos.test.ts +++ b/tests/combos.test.ts @@ -807,6 +807,7 @@ describe("combo validation and normalization", () => { strategy: "failover", stickyLimit: 1, defaultEffort: "high", + reasoningEffortMode: "strict", imageInput: "auto", alias: null, nativeAlias: false, @@ -814,6 +815,17 @@ describe("combo validation and normalization", () => { targets: [{ provider: "a", model: "m1", weight: 2 }], }); expect(normalizeComboConfig({ targets: [{ provider: "a", model: "m1" }] }).defaultEffort).toBeNull(); + // Anything that is not the literal "adaptive" normalizes to today's behavior, so a + // malformed or absent value can never silently opt a user in. + expect(normalizeComboConfig({ targets: [{ provider: "a", model: "m1" }] }).reasoningEffortMode).toBe("strict"); + expect(normalizeComboConfig({ + reasoningEffortMode: "adaptive", + targets: [{ provider: "a", model: "m1" }], + }).reasoningEffortMode).toBe("adaptive"); + expect(comboConfigIssues("free", { + reasoningEffortMode: "aggressive", + targets: [{ provider: "a", model: "m1" }], + }, baseConfig().providers).some(issue => issue.path[0] === "reasoningEffortMode")).toBe(true); expect(comboDefaultEffort(baseConfig(), "free")).toBeNull(); const aliased = baseConfig({ combos: { free: { ...VALID_COMBO, alias: " deepseek-v4-flash " } }, diff --git a/tests/management-provider-validation.test.ts b/tests/management-provider-validation.test.ts index 6149eeefb6..ec255b61f8 100644 --- a/tests/management-provider-validation.test.ts +++ b/tests/management-provider-validation.test.ts @@ -330,6 +330,50 @@ describe("provider management validation", () => { .toEqual(["deepseek-v4-flash", "other-model"]); }); + test("validates, exposes, and normalizes tool-bearing reasoning-effort opt-outs", () => { + const provider = { + adapter: "openai-chat", + baseUrl: "https://relay.example/v1", + omitReasoningEffortWithToolsModels: ["picky-model"], + }; + expect(providerManagementConfigError("relay", provider)).toBeNull(); + for (const omitReasoningEffortWithToolsModels of [ + "picky-model", + [""], + [" "], + [42], + ]) { + expect(providerManagementConfigError("relay", { + ...provider, + omitReasoningEffortWithToolsModels, + })).toContain("omitReasoningEffortWithToolsModels"); + } + + const dto = safeConfigDTO({ + port: 10100, + defaultProvider: "relay", + providers: { relay: provider }, + } as OcxConfig) as { providers: Record }; + expect(dto.providers.relay?.omitReasoningEffortWithToolsModels).toEqual(["picky-model"]); + + if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); + mkdirSync(TEST_DIR, { recursive: true }); + process.env.OPENCODEX_HOME = TEST_DIR; + writeFileSync(join(TEST_DIR, "config.json"), JSON.stringify({ + ...config("127.0.0.1"), + defaultProvider: "relay", + providers: { + relay: { + adapter: "openai-chat", + baseUrl: "https://relay.example/v1", + omitReasoningEffortWithToolsModels: [" picky-model ", "picky-model", " other-model "], + }, + }, + })); + expect(loadConfig().providers.relay?.omitReasoningEffortWithToolsModels) + .toEqual(["picky-model", "other-model"]); + }); + test("provider management validates annotateEmptyToolOutputs as boolean", () => { const provider = { adapter: "openai-chat", diff --git a/tests/openai-chat-hardening.test.ts b/tests/openai-chat-hardening.test.ts index 52983f8377..cd89a8aff6 100644 --- a/tests/openai-chat-hardening.test.ts +++ b/tests/openai-chat-hardening.test.ts @@ -22,6 +22,82 @@ afterEach(() => { describe("AgentRouter openai-chat compatibility", () => { const preamble = "[Instruction: Process the user request below and respond in the appropriate language.]"; + describe("omitReasoningEffortWithToolsModels", () => { + const toolBearing = (modelId: string): OcxParsedRequest => ({ + modelId, + context: { + messages: [{ role: "user", content: "hi", timestamp: 0 }], + tools: [{ + name: "read_file", + description: "Read a file", + parameters: { type: "object", properties: { path: { type: "string" } } }, + }], + }, + stream: false, + options: { reasoning: "high" }, + }); + const plain = (modelId: string): OcxParsedRequest => ({ + modelId, + context: { messages: [{ role: "user", content: "hi", timestamp: 0 }] }, + stream: false, + options: { reasoning: "high" }, + }); + const gated = provider({ omitReasoningEffortWithToolsModels: ["picky-model"] }); + + test("drops the wire effort only when tools are present", () => { + const withTools = JSON.parse( + createOpenAIChatAdapter(gated).buildRequest(toolBearing("picky-model")).body, + ) as Record; + expect(withTools.reasoning_effort).toBeUndefined(); + // The tools themselves must still be sent — this is an effort opt-out, not a tool opt-out. + expect(Array.isArray(withTools.tools)).toBe(true); + + const withoutTools = JSON.parse( + createOpenAIChatAdapter(gated).buildRequest(plain("picky-model")).body, + ) as Record; + expect(withoutTools.reasoning_effort).toBe("high"); + }); + + test("leaves an unlisted sibling model untouched", () => { + const sibling = JSON.parse( + createOpenAIChatAdapter(gated).buildRequest(toolBearing("other-model")).body, + ) as Record; + expect(sibling.reasoning_effort).toBe("high"); + }); + + test("an unset provider list changes nothing", () => { + const body = JSON.parse( + createOpenAIChatAdapter(provider()).buildRequest(toolBearing("picky-model")).body, + ) as Record; + expect(body.reasoning_effort).toBe("high"); + }); + + test("suppresses the gateway-object reasoning block for tool-bearing requests", () => { + // The gateway-object branch writes its own reasoning field, so it needs the same + // guard; without it the wire effort returns through a second path. + const gatewayProvider = provider({ + reasoningWireFormat: "gateway-object", + omitReasoningEffortWithToolsModels: ["picky-model"], + }); + const none = (modelId: string): OcxParsedRequest => ({ + ...toolBearing(modelId), + options: { reasoning: "none" }, + }); + + const suppressed = JSON.parse( + createOpenAIChatAdapter(gatewayProvider).buildRequest(none("picky-model")).body, + ) as Record; + expect(suppressed.reasoning).toBeUndefined(); + expect(suppressed.reasoning_effort).toBeUndefined(); + + // An unlisted model still takes the gateway-object path. + const untouched = JSON.parse( + createOpenAIChatAdapter(gatewayProvider).buildRequest(none("other-model")).body, + ) as Record; + expect(untouched.reasoning ?? untouched.reasoning_effort).toBeDefined(); + }); + }); + test("adds a stable Codex originator while preserving operator header precedence", () => { const automatic = createOpenAIChatAdapter(provider({ baseUrl: "https://agentrouter.org/v1" })).buildRequest(parsed()); expect(automatic.headers.originator).toBe("codex_cli_rs"); From c17bc94c2faa9b296a95d8529019579df177de02 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 03:19:54 +0900 Subject: [PATCH 128/172] fix(codex): verify auth.json identity, not just content, before publishing (#3199) The native-main publisher hashed auth.json and re-checked the hash before renaming its staged file over it. Both guards asked whether the CONTENT matched; neither asked whether it was still the same file. That is the weaker question at a boundary where rename(2) replaces unconditionally. A Codex writer that rewrites auth.json with identical bytes owns the target afterwards, and the publisher would overwrite it - silently replacing the user's own codex login result with a token staged from an earlier read. Carry the target's dev+ino alongside the hash and compare both. An unreadable identity on either side fails closed: unprovable is not the same as equal. This narrows the window rather than eliminating it. A truly atomic compare-and-swap needs renameat2(RENAME_EXCHANGE) or equivalent, which Bun does not expose. Refs #2999 Co-authored-by: jun --- src/codex/main-account.ts | 47 +++++++++++++- tests/codex-main-account-refresh.test.ts | 83 +++++++++++++++++++++++- 2 files changed, 128 insertions(+), 2 deletions(-) diff --git a/src/codex/main-account.ts b/src/codex/main-account.ts index cd5cfb50ba..d43c27f61d 100644 --- a/src/codex/main-account.ts +++ b/src/codex/main-account.ts @@ -1,5 +1,5 @@ import { createHash } from "node:crypto"; -import { readFileSync } from "node:fs"; +import { readFileSync, statSync } from "node:fs"; import { join } from "node:path"; import { readCodexTokens } from "./auth-collision"; import { @@ -37,6 +37,16 @@ let beforeMainAuthJsonRenameForTests: (() => void) | null = null; type MainAuthJsonCredential = { path: string; rawSha256: string; + /** + * Filesystem identity of the file the hash was taken from (#2999). + * + * A content hash cannot tell "unchanged" from "replaced with a file that happens to + * hash the same", and more importantly it is read at a different instant than the + * rename. Carrying dev+ino lets the pre-rename guard ask the sharper question: is this + * still the same file, not merely one with the same bytes. `null` when the target could + * not be stat'ed, which is treated as "cannot prove identity" rather than "matches". + */ + identity: { dev: number; ino: number } | null; root: Record; tokens: Record; accessToken?: string; @@ -73,6 +83,22 @@ function sha256(value: string): string { return createHash("sha256").update(value).digest("hex"); } +/** + * Filesystem identity of a path, or null when it cannot be read. + * + * Null is deliberately NOT "matches anything": a caller that cannot prove identity must + * fail closed, because the whole point here is refusing to overwrite a file we can no + * longer vouch for. + */ +function statIdentity(path: string): { dev: number; ino: number } | null { + try { + const stat = statSync(path); + return { dev: Number(stat.dev), ino: Number(stat.ino) }; + } catch { + return null; + } +} + function readMainAuthJsonCredential(): MainAuthJsonCredential | null { const path = resolveWriteTarget(join(resolveCodexHomeDir(), "auth.json")); let raw: string; @@ -98,6 +124,7 @@ function readMainAuthJsonCredential(): MainAuthJsonCredential | null { return { path, rawSha256: sha256(raw), + identity: statIdentity(path), root, tokens, ...(accessToken ? { accessToken } : {}), @@ -131,6 +158,21 @@ function assertMainAuthJsonSnapshotUnchanged(expected: MainAuthJsonCredential): if (!current || current.path !== expected.path || current.rawSha256 !== expected.rawSha256) { throw new MainAuthJsonChangedDuringRefreshError(); } + // Identity, not just content (#2999). A writer can land between this check and the + // rename, and rename(2) replaces unconditionally - so the narrower the question asked + // here, the smaller the window where a Codex login gets silently overwritten. An + // unreadable identity on either side fails closed: unprovable is not the same as equal. + assertMainAuthJsonIdentityUnchanged(expected); +} + +function assertMainAuthJsonIdentityUnchanged(expected: MainAuthJsonCredential): void { + const identity = statIdentity(expected.path); + if (!identity + || !expected.identity + || identity.dev !== expected.identity.dev + || identity.ino !== expected.identity.ino) { + throw new MainAuthJsonChangedDuringRefreshError(); + } } function persistRefreshedMainAuthJson( @@ -160,6 +202,9 @@ function persistRefreshedMainAuthJson( beforeMainAuthJsonRenameForTests = null; hook?.(); }, + // Runs immediately before rename(2), after the test hook has had its chance to + // simulate an external writer. Full snapshot check (content AND identity): this is + // the last look we get, so it asks everything it can rather than the cheap question. validateBeforeRename: () => assertMainAuthJsonSnapshotUnchanged(expected), }, ); diff --git a/tests/codex-main-account-refresh.test.ts b/tests/codex-main-account-refresh.test.ts index 08ff5c05bc..59a8ac6930 100644 --- a/tests/codex-main-account-refresh.test.ts +++ b/tests/codex-main-account-refresh.test.ts @@ -1,5 +1,5 @@ import { afterEach, beforeEach, describe, expect, test } from "bun:test"; -import { mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs"; +import { existsSync, mkdtempSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { @@ -247,3 +247,84 @@ describe("native main token refresh", () => { } }); }); + +describe("publication never overwrites an external Codex writer (#2999)", () => { + const refreshOk = async () => ({ + access: "ocx-staged-access", + refresh: "ocx-staged-refresh", + expires: Date.now() + 3_600_000, + accountId: "account-main", + }); + + function seedExpired(authPath: string): void { + writeFileSync(authPath, JSON.stringify({ + auth_mode: "chatgpt", + tokens: { access_token: expiredJwt(), refresh_token: "old-refresh", account_id: "account-main" }, + })); + } + + test("a writer landing at the rename boundary is preserved byte-for-byte", async () => { + // Issue reproduction step 5: replace auth.json from a simulated Codex writer at the + // final pre-rename hook, then let the publisher resume. Before this guard the staged + // credential won and the user's own `codex login` result was silently replaced. + const authPath = join(home, "auth.json"); + seedExpired(authPath); + const external = JSON.stringify({ + auth_mode: "chatgpt", + tokens: { access_token: "codex-cli-wrote-this", refresh_token: "codex-refresh", account_id: "account-main" }, + }); + setMainAuthJsonBeforeRenameHookForTests(() => { writeFileSync(authPath, external); }); + + // Refusal surfaces as MainAuthJsonChangedDuringRefreshError, the existing signal for + // "the file moved under us" - the caller retries against the new state rather than + // proceeding with a credential it no longer owns. + await expect(getValidMainAccountToken({ refreshToken: refreshOk })).rejects.toThrow(); + + expect(readFileSync(authPath, "utf8")).toBe(external); + expect(readFileSync(authPath, "utf8")).not.toContain("ocx-staged-access"); + }); + + test("a same-bytes replacement with a new inode is still refused", async () => { + // The case a content hash cannot see. rename(2) replaces unconditionally, so the + // question that matters at the boundary is "is this the same FILE", not "does it hash + // the same" - an external writer that rewrote identical bytes still owns the target. + const authPath = join(home, "auth.json"); + seedExpired(authPath); + const identical = readFileSync(authPath, "utf8"); + setMainAuthJsonBeforeRenameHookForTests(() => { + // Replace via a distinct file so the inode changes while the bytes do not. + const swap = join(home, "swap.json"); + writeFileSync(swap, identical); + renameSync(swap, authPath); + }); + + await expect(getValidMainAccountToken({ refreshToken: refreshOk })).rejects.toThrow(); + + expect(readFileSync(authPath, "utf8")).toBe(identical); + expect(readFileSync(authPath, "utf8")).not.toContain("ocx-staged-access"); + }); + + test("the canonical target survives a refused publication", async () => { + // A refusal must never leave the credential missing: losing auth.json is worse than + // losing the refresh, because Codex CLI then has nothing to authenticate with. + const authPath = join(home, "auth.json"); + seedExpired(authPath); + setMainAuthJsonBeforeRenameHookForTests(() => { + writeFileSync(authPath, JSON.stringify({ auth_mode: "chatgpt", tokens: { access_token: "other" } })); + }); + + await expect(getValidMainAccountToken({ refreshToken: refreshOk })).rejects.toThrow(); + + expect(existsSync(authPath)).toBe(true); + expect(readdirSync(home).filter(name => name.startsWith("auth.json.")).length).toBe(0); + }); + + test("an uncontested publication still succeeds", async () => { + // The guard must not make the ordinary path fail closed. + const authPath = join(home, "auth.json"); + seedExpired(authPath); + const token = await getValidMainAccountToken({ refreshToken: refreshOk }); + expect(token?.accessToken).toBe("ocx-staged-access"); + expect(readFileSync(authPath, "utf8")).toContain("ocx-staged-access"); + }); +}); From 52d941640edd32c093ea65aa538f84fbef41173b Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 03:23:08 +0900 Subject: [PATCH 129/172] feat(responses): opt-in ceiling for oversized outbound passthrough bodies (#3196) * feat(responses): opt-in ceiling for oversized outbound passthrough bodies Reimplements #3142 (thanks @olddonkey) with the guard off by default, and adds the rebuild site and refusal shape that version was missing. The measurement, local refusal, image diagnostics, body-observation release and probe-lease handling are that PR's work and are kept. Why default-off rather than the 15 MiB default: the only measured ceiling in this codebase belongs to the WebSocket transport, and the comment recording it says the same body still succeeds over HTTP SSE. #2473 acts on that by falling back to HTTP rather than refusing, and #2426 records an 18.2 MB HTTP 200. A 15 MiB default would therefore refuse turns that work today - on canonical ChatGPT as well as on Azure and custom Responses gateways whose limits were never measured at all. An unset proxy now measures nothing and sends exactly what it sends today. Two fixes beyond the original: - The stored/main pool 401 replay rebuilds its body and sent it unchecked. It is now guarded like every other build site; a replay is precisely when a grown payload reappears. - A streaming refusal returns terminal response.failed / context_length_exceeded instead of a JSON 413. Codex treats HTTP 413 as a retryable transport error and resends the same oversized body, which is the loop this feature exists to stop. That is the contract the upstream-413 path already uses (#3177). RequestLogContext gains a proxy-owned errorCode so a locally refused request is named as such instead of being classified from a status with no upstream message behind it. Refs #3142. Related to #2511, which asks for per-provider downscaling and pruning and is deliberately not implemented here. * docs(devlog): record live inventory after 3190 --------- Co-authored-by: jun --- .../031_wp3_live_inventory.md | 18 +++ .../020_wp2_outbound_body_guard.md | 118 +++++++++++++++ .../021_wp2_audit_r1_synthesis.md | 78 ++++++++++ .../docs/reference/configuration/providers.md | 1 + src/config.ts | 6 + src/server/request-log.ts | 10 +- src/server/responses/core.ts | 56 +++++++ src/server/responses/outbound-body-guard.ts | 110 ++++++++++++++ src/types/config.ts | 11 ++ tests/empty-completion-core.test.ts | 140 ++++++++++++++++-- tests/outbound-body-guard.test.ts | 87 +++++++++++ 11 files changed, 622 insertions(+), 13 deletions(-) create mode 100644 devlog/_plan/260902_admin_merge_3190/031_wp3_live_inventory.md create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/020_wp2_outbound_body_guard.md create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/021_wp2_audit_r1_synthesis.md create mode 100644 src/server/responses/outbound-body-guard.ts create mode 100644 tests/outbound-body-guard.test.ts diff --git a/devlog/_plan/260902_admin_merge_3190/031_wp3_live_inventory.md b/devlog/_plan/260902_admin_merge_3190/031_wp3_live_inventory.md new file mode 100644 index 0000000000..a4401b2daf --- /dev/null +++ b/devlog/_plan/260902_admin_merge_3190/031_wp3_live_inventory.md @@ -0,0 +1,18 @@ +# 031 — wp3 live inventory after #3190 + +Captured after `origin/dev` = `88c427522` (#3190). + +| PR | mergeable (live) | review | Disposition | +| --- | --- | --- | --- | +| #3196 | was MERGEABLE before 3190, now UNKNOWN until rebase | REVIEW_REQUIRED | **SURVIVOR** — maintainer carry of #3142, default-off `maxUpstreamBodyBytes`. gates failed only on the 091 home-path citation that #3197 already fixed. Rebase onto current `dev`, exact-head CI, admin merge, then close #3142 with credit. | +| #3142 | CONFLICTING earlier / UNKNOWN now | CHANGES_REQUESTED | CLOSE after #3196 lands (superseded carry). Do not merge both. | +| #3061 | UNKNOWN | CHANGES_REQUESTED | DEFER — parked, macos/ci red | +| #2986 | UNKNOWN | CHANGES_REQUESTED | DEFER — do not merge with #2083 | +| #2877 | UNKNOWN | CHANGES_REQUESTED | DEFER | +| #2805 | UNKNOWN | REVIEW_REQUIRED | DEFER CONFLICTING | +| #2783 | UNKNOWN | CHANGES_REQUESTED | DEFER | +| #2527 | UNKNOWN | CHANGES_REQUESTED | DEFER | +| #2366 | UNKNOWN | CHANGES_REQUESTED | DEFER | +| #2083 | UNKNOWN | APPROVED | DEFER — pair with #2986 | + +Filter result: one survivor (#3196). Not a security-boundary PR (Responses body ceiling, opt-in, no auth/credential/workflow/release/dependency install). diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/020_wp2_outbound_body_guard.md b/devlog/_plan/260902_nonbug_adoption_backlog/020_wp2_outbound_body_guard.md new file mode 100644 index 0000000000..ab4fb3a3e4 --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/020_wp2_outbound_body_guard.md @@ -0,0 +1,118 @@ +# wp2 — PR #3142 oversized outbound body refusal (+ #2511) + +PR #3142 by @olddonkey, head `df94500b6`, `CHANGES_REQUESTED`, CONFLICTING with +`dev`, base 121 commits behind. Issue #2511 (score 55) is the adjacent request. + +## The blocker is real and it is the one our own criteria care about + +@Ingwannu's live review asks for the implicit 15 MiB default to apply only to the +canonical OpenAI forward Responses destination, because `passthrough` is not +synonymous with the measured ChatGPT backend — Azure and custom key-auth Responses +adapters use it too (`src/adapters/registry.ts:78`, `src/adapters/azure.ts:5`). + +Independent investigation confirms it and finds the failure is worse than scope +creep. The default is applied whenever the key is omitted: + +``` +const maxUpstreamBodyBytes = config.maxUpstreamBodyBytes ?? DEFAULT_MAX_UPSTREAM_BODY_BYTES; +``` + +### It regresses requests that work today + +`#2473` (merged, in tree) does **not** refuse oversized turns. It sizes the WS +`response.create` frame against `CODEX_WS_CREATE_FRAME_LIMIT_BYTES` = 16 MiB − 64 KiB +and **falls back to HTTP SSE** (`src/server/responses/ws-upstream.ts:152-167,199-201`). +`tests/ws-upstream.test.ts:692` records the measured backend close at ~16,777,300 B +with 16,777,000 B completing. + +So with the PR's default and no configuration: + +| Body size | Today | After #3142 | +|-----------|-------|-------------| +| 15 MiB — 16 MiB−64 KiB | WS-eligible, succeeds | local 413 | +| 16 MiB−64 KiB — ~16.7 MB | HTTP SSE fallback, succeeds | local 413 | +| > ~16.7 MB (ChatGPT) | upstream failure | local 413 (better message) | + +The first two rows are **working requests that start failing**. That is a +regression for users who configured nothing, and it directly violates the +standing criterion that every capability is opt-in and defaults to today's +behavior. + +### The refusal shape may also be worse than today + +`#3177` (in tree, not in the PR's base) rewrites a provider HTTP 413 on a +streaming Responses turn into `response.failed` / `context_length_exceeded` +(`src/server/responses/context-overflow.ts:19-26`, `core.ts:4529-4533`), so Codex +treats it as terminal overflow and compacts. The PR returns +`formatErrorResponse(413, ...)` JSON instead, which for a streaming client is a +retryable transport error — Codex may resend the same oversized body. The PR's +stated goal is to stop exactly that loop. + +### It does not close #2511 + +#2511 asks for a **per-provider, default-off** budget that **downscales** images +then **prunes** oldest-first with a visible marker. #3142 is top-level, +default-on, and refusal-only. `closingIssuesReferences` is empty and the PR body +never mentions #2511 — correctly. These are different products; #3142 must not +be recorded as closing it. + +## Disposition: reimplement, default-off + +The measurement, the local 413 shape, the image diagnostics, the body-observation +release and the lease fix are all good work and are kept. One thing changes: the +guard is **off unless configured**. + +That is a stronger answer than the requested canonical-only default, and it +resolves @Ingwannu's blocker a fortiori: + +- no destination — canonical, Azure, or custom — inherits a ceiling measured + somewhere else; +- the #2473 HTTP fallback band keeps working; +- it matches the shape #2511 actually asked for, so the two stop contradicting; +- an operator who has hit the wall sets one integer and gets the diagnostic. + +The cost is that the diagnostic is not on by default. That is the correct trade: +a default that breaks working requests to improve an error message is not a +default, it is a regression with a nicer string. + +## File change map + +| File | Action | Change | +|------|--------|--------| +| `src/server/responses/outbound-body-guard.ts` | NEW | `checkOutboundBodySize`, `describeOutboundBodyRefusal`, image diagnostics. `limitBytes` undefined or 0 admits without measuring. No `DEFAULT_MAX_UPSTREAM_BODY_BYTES`. | +| `src/types/config.ts` | MODIFY | `maxUpstreamBodyBytes?: number` with JSDoc naming the native-Responses-passthrough scope and the default-off contract | +| `src/config.ts` | MODIFY | zod: optional non-negative integer | +| `src/server/responses/core.ts` | MODIFY | `refuseOversizedOutboundBody` inside the passthrough branch; guard at initial build, `rebuildAndRefetch`, OAuth-refresh rebuild, alternate-account retry, **and the 401 replay rebuild the PR missed** (`core.ts:4071-4080` on PR head); release body observation, host admission and probe lease; release `firstAuthCtx` when `deferFirstOutcome` | +| `src/server/request-log.ts` | MODIFY | `outbound_body_too_large` error code | +| `docs-site/.../providers.md` | MODIFY | document the key, default-off, and the passthrough-only scope | +| `tests/outbound-body-guard.test.ts` | NEW | threshold crossing, UTF-8 byte counting, unparseable body, undefined and 0 both admit | +| `tests/empty-completion-core.test.ts` | MODIFY | integration: configured limit refuses with 0 fetches and 1 observation release; **omitted key sends a 20 MiB body upstream unrefused** | + +## Scope boundary + +IN: the guard, its activation sites including the missed 401 replay, default-off, +docs, focused tests. + +OUT: image downscaling and oldest-first pruning (#2511's actual request) — a +separate feature that mutates request content and needs its own cycle. OUT: +changing the refusal into a `streamingContextOverflowResponse`; worth doing but +it is #3177's contract and belongs with that code, and with the guard off by +default the retry-loop concern no longer rides on this change. + +## Accept criteria + +1. **Omitted config sends an oversized body upstream.** Activation: integration + test with no `maxUpstreamBodyBytes` and a body far above 15 MiB asserting the + fetch happened. This is the regression the PR would have shipped. +2. Configured limit refuses with a local 413, zero upstream fetches, and the body + observation released. Activation: existing integration case. +3. `0` admits without measuring. +4. Refusal names the image count and approximate decoded megabytes when the body + parses. Activation: unit assertion on the message. +5. Every rebuild site is guarded, including the 401 replay. + +## Verifier + +`bun x tsc --noEmit` (exit 0 baseline confirmed) plus +`bun test tests/outbound-body-guard.test.ts tests/empty-completion-core.test.ts`. +Full suite forbidden by the operator. diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/021_wp2_audit_r1_synthesis.md b/devlog/_plan/260902_nonbug_adoption_backlog/021_wp2_audit_r1_synthesis.md new file mode 100644 index 0000000000..938a8284de --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/021_wp2_audit_r1_synthesis.md @@ -0,0 +1,78 @@ +# wp2 audit round 1 — synthesis + +Reviewer: grok-4.6 adversarial lane (agent `01a05e12`). +Verdict: `GO-WITH-FIXES (blockers=3)`. All three accepted. No rebuttals. + +The reviewer confirmed the plan's central claim — there is a real band above +15 MiB that succeeds today — and then corrected the evidence I used to argue it. +That correction is blocker 3 and it matters more than it looks. + +## Blocker 3 — I overstated the ceiling (ACCEPTED) + +My plan's table claimed HTTP dies at ~16.7 MB. Wrong. The 16,777,000 / +16,777,300 figures in `ws-upstream.ts:31-38` are the **WebSocket close** +measurement, and the very same comment says *"The same request body succeeds +over HTTP SSE, so the ceiling belongs to this transport alone."* Issue #2426 +records an 18.2 MB HTTP 200. + +So the regression is **larger** than I wrote, not smaller: there is no +established HTTP ceiling at all in the range the PR's default would refuse. I +was citing a WS number as if it bounded HTTP. Corrected table: + +| Body size | Today | After #3142 default | +|-----------|-------|---------------------| +| 15 MiB … frame limit − 1 | WS send succeeds | local 413 | +| >= frame limit (16 MiB − 64 KiB) | HTTP SSE fallback sends the original body; 18.2 MB observed OK | local 413 | + +This also settles the alternative the reviewer weighed: a canonical-only default +at 15 MiB is still wrong, because it would refuse working ChatGPT traffic in the +15 MiB–18.2 MB band. Default-off is not merely the safer option, it is the only +one supported by the measurements we actually have. + +## Blocker 1 — the refusal shape is a trap on the enabled path (ACCEPTED) + +I had put the #3177 mapping OUT of scope on the grounds that default-off defuses +the retry-loop concern. That reasoning is backwards. Default-off means the +**only** users who ever see this code are the ones who deliberately enabled it — +so the enabled path is the whole feature, not an edge case. + +`streamingContextOverflowResponse` (`src/server/responses/context-overflow.ts:8-16,29-50` +on `origin/dev`) emits SSE `response.failed` / `context_length_exceeded` with +`retryable: false`, and the passthrough upstream-413 path already uses it +(`core.ts:4530-4534`). A local `formatErrorResponse(413, ...)` is a retryable +transport error to Codex, which resends the same oversized body — the exact loop +the PR set out to stop. + +Correction: a streaming refusal uses `streamingContextOverflowResponse`. The +JSON 413 stays only for non-streaming requests, where it is the right shape. + +## Blocker 2 — criterion 5 had no activating test (ACCEPTED) + +"Every rebuild site is guarded, including the 401 replay" was a claim with +nothing driving it: neither named test file reaches the 401 replay, +`rebuildAndRefetch`, or the alternate-account retry. Under +C-ACTIVATION-GROUNDING-01 that is a code comment wearing an acceptance criterion. + +Correction: add an integration case that drives a rebuild path with an oversized +rebuilt body and asserts no second upstream fetch. The 401 replay gap itself is +confirmed real — unguarded at PR head `core.ts:4071-4097` and at the same place +on current `origin/dev` (`4106-4135`). + +## File-map additions from the reviewer + +- all seven `docs-site` locale copies of `providers.md`, which the PR does touch +- `src/server/responses/context-overflow.ts` as a consumer (blocker 1) +- the malformed-value warning sibling used by `upstreamHostCircuitThreshold` + (`src/config.ts:1809-1823, 2261-2270`) +- `src/server/request-log.ts` confirmed in scope: the PR adds + `RequestLogContext.errorCode`, absent from the current tree + +## Base + +Reimplementation branches from current `origin/dev` (`c87071400`), which carries +#3177. The wp1 branch is 20 commits behind that and is not a base for this work. + +## Line drift corrected + +`ws-upstream.ts:152-167` is the doc comment; the fallback is `:199-201`. +`tests/ws-upstream.test.ts:692` is `:693`. diff --git a/docs-site/src/content/docs/reference/configuration/providers.md b/docs-site/src/content/docs/reference/configuration/providers.md index 7b68b69654..0cfd77f71e 100644 --- a/docs-site/src/content/docs/reference/configuration/providers.md +++ b/docs-site/src/content/docs/reference/configuration/providers.md @@ -27,6 +27,7 @@ authenticated. | `accountPoolStickyLimit?` | `number` | `1` | New/unbound task assignments retained on one round-robin selection before advancing; the counter advances when a task is bound, not after an upstream success. Range 1–100. | | `upstreamFailoverThreshold?` | `number` | `3` | Consecutive transient failures before future new sessions fail over. Set `0` to disable. For regular Responses and native compact sends, proven pre-connection DNS/TCP reachability failures are tracked at the provider-host level: they never affect account health, account cooldowns, thread/session affinity, active-account selection, or Pool routing, and never count toward this threshold. | | `upstreamHostCircuitThreshold?` | `number` | `0` | Opt-in circuit threshold for proven pre-connection DNS/TCP failures on native OpenAI forward Responses and compact sends. `0` disables it; `1`–`20` opens a 30-second provider-origin cooldown after that many terminal logical requests. While open, requests receive `503` with `Retry-After` before account selection or upstream send; after cooldown, one half-open request is admitted. Timeouts and HTTP responses never count, and any HTTP response closes the circuit. Applies only to Codex Pool routing with no pinned account; it is inert for `codexAccountMode: "direct"` and account-qualified selectors. | +| `maxUpstreamBodyBytes?` | `number` | `0` | Opt-in ceiling, in bytes, on a serialized native Responses **passthrough** body. `0` or omitted disables it — no limit is inferred for any destination. When set, a built body above the ceiling is refused locally before the send: streaming turns receive a terminal `response.failed` / `context_length_exceeded` so the client compacts instead of resending, and non-streaming turns receive a `413` naming the size, the number of embedded `input_image` items, and roughly how many megabytes of image data they represent. Checked at every build and rebuild point, including OAuth-refresh replay and alternate-account retry. Translated adapter paths are not covered. There is deliberately no default: the only measured ceiling here belongs to the WebSocket transport, which already falls back to HTTP for oversized turns, so a default would refuse requests that currently succeed. Set it when your gateway has a known request-size limit and you would rather see an actionable local error than an opaque upstream failure. | | `modelCacheTtlMs?` | `number` | `300000` | Freshness window for the per-provider `/models` cache. | | `cacheRetention?` | `"none" \| "short" \| "long"` | `"short"` | Anthropic prompt-cache policy: disabled, 5-minute ephemeral, or 1-hour extended. | | `tokenGuardian?` | `OcxTokenGuardianConfig` | off | Optional proactive OAuth refresh and Codex-account warmup policy. | diff --git a/src/config.ts b/src/config.ts index f68244d636..c17a86d1ff 100644 --- a/src/config.ts +++ b/src/config.ts @@ -990,6 +990,12 @@ const configSchema = z.object({ .max(UPSTREAM_HOST_CIRCUIT_MAX_THRESHOLD) .optional() .catch(undefined), + // Opt-in outbound body ceiling. An invalid hand edit disables only this guard, matching the + // circuit threshold above: a malformed number must not make the proxy refuse traffic. + maxUpstreamBodyBytes: z.number().int() + .min(0) + .optional() + .catch(undefined), appOwnedMemoryBudgetMb: z.number().int() .min(MIN_APP_OWNED_MEMORY_BUDGET_MB) .max(MAX_APP_OWNED_MEMORY_BUDGET_MB) diff --git a/src/server/request-log.ts b/src/server/request-log.ts index a443ff1939..2c8d3e179c 100644 --- a/src/server/request-log.ts +++ b/src/server/request-log.ts @@ -125,6 +125,12 @@ export interface RequestLogContext { terminalHttpStatus?: number; /** Recognized structured terminal code whose exact identity must survive status mapping. */ terminalErrorCode?: typeof CYBER_POLICY_ERROR_CODE; + /** + * Proxy-owned error code for a request OpenCodex terminated locally, before or instead of an + * upstream send. Status-derived classification cannot name these: there is no upstream + * message to classify, and the status alone would read as a provider failure. + */ + errorCode?: string; /** Structured reason from `response.incomplete`; internal-only input to log classification. */ terminalIncompleteReason?: string; affinity?: "reused" | "new_bind" | "rebound" | "cleared"; @@ -925,7 +931,9 @@ export function addFinalRequestLog( const effectiveStatus = status >= 500 && logCtx.upstreamError && isClientClosedMessage(logCtx.upstreamError) ? 499 : status; - const errorCode = requestLogErrorCode( + // A locally assigned code wins: it names a refusal this proxy made itself, which no + // status-plus-upstream-message classification can reconstruct. + const errorCode = logCtx.errorCode ?? requestLogErrorCode( effectiveStatus, logCtx.upstreamError, logCtx.terminalErrorCode, diff --git a/src/server/responses/core.ts b/src/server/responses/core.ts index 0240f49553..d4e45cde2a 100644 --- a/src/server/responses/core.ts +++ b/src/server/responses/core.ts @@ -7,6 +7,10 @@ import { backfillResponsesFieldsJson, } from "./responses-field-backfill"; import { checkInputAdmission } from "./input-admission"; +import { + checkOutboundBodySize, + describeOutboundBodyRefusal, +} from "./outbound-body-guard"; import { nativeContextLimits } from "../../codex/catalog"; import { describeUpstreamConnectFailure } from "./upstream-error"; import { @@ -3903,6 +3907,48 @@ async function handleResponsesInner( linkAbortSignal(upstream, options.abortSignal); const connectMs = config.connectTimeoutMs ?? 200_000; let upstreamResponse: Response; + /** + * Refuse a built body that exceeds the operator's configured ceiling, before it is sent. + * + * Unconfigured this measures nothing and returns undefined, so an unset proxy behaves + * exactly as it does today. Runs at every point a body is built or rebuilt, because a + * rebuild can produce a payload the initial check never saw. + */ + const refuseOversizedOutboundBody = ( + builtRequest: AdapterRequest, + refusalAuthCtx: CodexAuthContext = authCtx, + ): Response | undefined => { + const result = checkOutboundBodySize(builtRequest.body, config.maxUpstreamBodyBytes); + if (result.admitted) return undefined; + + // This returns before the surrounding fetch/finally owns the observation, so release + // it here or one refused body holds translator budget for the process lifetime. + builtRequest.releaseBodyObservation?.(); + upstream.abort(); + releaseUpstreamHostAdmission(hostAdmissionLease); + hostAdmissionLease = null; + releaseCodexAuthContextProbeLease(refusalAuthCtx); + logCtx.errorCode = "outbound_body_too_large"; + console.warn( + `[responses] refused an oversized outbound body: bytes=${result.bytes} limit=${result.limit} ` + + `input_images=${result.imageCount} image_bytes=${result.imageBytes} ` + + `model=${JSON.stringify(parsed.modelId)}`, + ); + // A streaming client treats HTTP 413 as a retryable transport error and resends the same + // oversized body — the reconnect loop #3177 exists to stop. Terminal overflow is the + // honest shape, and it is what the upstream-413 path already returns. + if (clientRequestedStream) { + return streamingContextOverflowResponse( + parsed._responseModelId ?? parsed.modelId, + translatorBudget, + ); + } + return formatErrorResponse( + 413, + "outbound_body_too_large", + describeOutboundBodyRefusal(result), + ); + }; const transportFailureResponse = (err: unknown): Response => { upstream.abort(); if (options.abortSignal?.aborted) { @@ -3945,6 +3991,8 @@ async function handleResponsesInner( : describeUpstreamConnectFailure(err, connectMs); return formatErrorResponse(502, "upstream_error", msg); }; + const initialBodyRefusal = refuseOversizedOutboundBody(request); + if (initialBodyRefusal) return initialBodyRefusal; try { // Transient-5xx pre-stream retry (devlog/_plan/260716_claudecode_hardening/010): // the ChatGPT backend emits transient 502/520s that an immediate retry absorbs. @@ -4019,6 +4067,8 @@ async function handleResponsesInner( retryAdapter.name, logCtx.accountLogLabel, ); + const rebuiltBodyRefusal = refuseOversizedOutboundBody(request); + if (rebuiltBodyRefusal) return { failed: rebuiltBodyRefusal }; try { return await fetchWithTransientRetry( innerRecovery => { @@ -4111,6 +4161,10 @@ async function handleResponsesInner( recordAdapterReasoning(logCtx, request); recordAdapterTier(logCtx, request); refreshUndeclaredToolGuard(request); + // The 401 replay rebuilds the body before sending, so it needs the same ceiling as + // every other build site; a replay is exactly when a grown payload reappears. + const replayBodyRefusal = refuseOversizedOutboundBody(request); + if (replayBodyRefusal) return replayBodyRefusal; noteAttemptSend(logCtx.activeAttempt, passthroughEstimate, "oauth-401"); upstreamResponse = await fetchWithHeaderTimeout( request.url, @@ -4220,6 +4274,8 @@ async function handleResponsesInner( return formatErrorResponse(400, "invalid_request_error", redactSecretString(msg)); } refreshUndeclaredToolGuard(request); + const refreshedBodyRefusal = refuseOversizedOutboundBody(request); + if (refreshedBodyRefusal) return refreshedBodyRefusal; try { upstreamResponse = await fetchWithTransientRetry( recovery => { diff --git a/src/server/responses/outbound-body-guard.ts b/src/server/responses/outbound-body-guard.ts new file mode 100644 index 0000000000..b8537e5e06 --- /dev/null +++ b/src/server/responses/outbound-body-guard.ts @@ -0,0 +1,110 @@ +/** + * Measure a built passthrough body before it is sent, so an operator can turn an opaque + * upstream failure into a local, actionable refusal. + * + * There is deliberately no default limit. The one measured ceiling in this codebase belongs + * to the WebSocket transport (`MAX_CODEX_WS_CREATE_FRAME_BYTES` in `ws-upstream.ts`), and the + * comment recording that measurement says the same body still succeeds over HTTP SSE — #2426 + * observed an 18.2 MB HTTP 200. An implicit HTTP ceiling inferred from the WS number would + * refuse requests that work today, on every passthrough destination including Azure and + * custom Responses gateways whose limits were never measured at all. The operator who hit a + * wall knows where their wall is; this guard is off until they say so. + */ + +export interface OutboundBodyGuardResult { + admitted: boolean; + /** Serialized UTF-8 bytes. Zero when the guard is disabled before measurement. */ + bytes: number; + /** The configured limit, or 0 when the guard is disabled. */ + limit: number; + imageCount: number; + /** Approximate decoded bytes represented by embedded `input_image` data URIs. */ + imageBytes: number; +} + +const MAX_DIAGNOSTIC_DEPTH = 64; + +function decodedDataUriBytes(value: unknown): number { + if (typeof value !== "string" || !value.startsWith("data:")) return 0; + const comma = value.indexOf(","); + if (comma < 0) return 0; + const payload = value.length - comma - 1; + return payload > 0 ? Math.floor((payload * 3) / 4) : 0; +} + +/** + * Walk the parsed body for `input_image` items. Bounded by depth and a seen-set because this + * runs on a body that already failed the size check, which is exactly when a pathological + * shape is most likely. + */ +function imageDiagnostics(value: unknown): { imageCount: number; imageBytes: number } { + let imageCount = 0; + let imageBytes = 0; + const seen = new WeakSet(); + + const visit = (entry: unknown, depth: number): void => { + if (depth > MAX_DIAGNOSTIC_DEPTH || entry === null || typeof entry !== "object") return; + if (seen.has(entry)) return; + seen.add(entry); + + if (!Array.isArray(entry) && (entry as Record).type === "input_image") { + imageCount += 1; + imageBytes += decodedDataUriBytes((entry as Record).image_url); + return; + } + + if (Array.isArray(entry)) { + for (const item of entry) visit(item, depth + 1); + return; + } + for (const item of Object.values(entry)) visit(item, depth + 1); + }; + + visit(value, 0); + return { imageCount, imageBytes }; +} + +/** + * `limitBytes` undefined (unconfigured) or 0 (explicitly disabled) both admit without + * measuring, so an unconfigured proxy does no work and sends exactly what it sends today. + */ +export function checkOutboundBodySize( + body: string, + limitBytes: number | undefined, +): OutboundBodyGuardResult { + if (limitBytes === undefined || limitBytes === 0) { + return { admitted: true, bytes: 0, limit: 0, imageCount: 0, imageBytes: 0 }; + } + + const bytes = Buffer.byteLength(body, "utf8"); + if (bytes <= limitBytes) { + return { admitted: true, bytes, limit: limitBytes, imageCount: 0, imageBytes: 0 }; + } + + try { + const diagnostics = imageDiagnostics(JSON.parse(body) as unknown); + return { admitted: false, bytes, limit: limitBytes, ...diagnostics }; + } catch { + return { admitted: false, bytes, limit: limitBytes, imageCount: 0, imageBytes: 0 }; + } +} + +function megabytes(bytes: number): string { + return (bytes / (1024 * 1024)).toFixed(1); +} + +/** + * Name the likely cause rather than only the number. Accumulated replayed images are the + * common way a thread crosses a byte ceiling while its token count still looks healthy, and + * the remedy is not something the user can guess from a size alone. + */ +export function describeOutboundBodyRefusal(result: OutboundBodyGuardResult): string { + const imageDetail = result.imageCount > 0 + ? ` It contains ${result.imageCount} input_image item${result.imageCount === 1 ? "" : "s"} ` + + `representing about ${megabytes(result.imageBytes)} MB of decoded embedded image data; ` + + "accumulated replayed images are the likely cause." + : " Large inputs accumulated across replayed turns can cause this."; + return `The serialized outbound request is ${megabytes(result.bytes)} MB, ` + + `above the configured ${megabytes(result.limit)} MB limit.${imageDetail} ` + + "Start a new session or compact the conversation before retrying."; +} diff --git a/src/types/config.ts b/src/types/config.ts index 169506d266..bf837abda9 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -702,6 +702,17 @@ export interface OcxConfig { * Default 0 (disabled); range 0..20. The circuit never counts timeouts or HTTP responses. */ upstreamHostCircuitThreshold?: number; + /** + * Opt-in ceiling, in bytes, for a serialized native Responses **passthrough** body. When the + * built body exceeds it OpenCodex refuses locally instead of sending, naming the size and any + * embedded image payload. Translated adapter paths are not covered. + * + * Omitted or 0 = disabled, which is the default: no implicit ceiling is inferred for any + * destination. The only measured limit in this codebase is the WebSocket create-frame size, + * and the same body still succeeds over HTTP SSE, so a default here would refuse requests + * that work today — on Azure and custom Responses gateways as well, whose limits are unknown. + */ + maxUpstreamBodyBytes?: number; /** * Opt-in Anthropic OAuth account pool (#294). Default OFF. * Failover on 429 + sticky affinity; new sessions may pick lowest known 5h usage. diff --git a/tests/empty-completion-core.test.ts b/tests/empty-completion-core.test.ts index 6d538bf54c..35c6c6b85e 100644 --- a/tests/empty-completion-core.test.ts +++ b/tests/empty-completion-core.test.ts @@ -18,24 +18,47 @@ let httpCalls = 0; let parsedAttempts: OcxParsedRequest[] = []; let builtBodies: string[] = []; let customRunTurn: ProviderAdapter["runTurn"] | undefined; +let passthroughFetchCalls = 0; +let bodyObservationReleaseCalls = 0; function attemptAt(index: number): AdapterEvent[] { return attemptEvents[index] ?? [{ type: "error", message: `missing fixture attempt ${index}` }]; } -function fixtureAdapter(provider: OcxProviderConfig): ProviderAdapter { +function fixtureAdapter(provider: OcxProviderConfig): ProviderAdapter & { passthrough?: true } { const runTurn = provider.adapter === "test-run-turn"; + const passthrough = provider.adapter === "test-passthrough"; return { - name: runTurn ? "test-run-turn" : "openai-chat", - buildRequest(parsed) { + name: runTurn ? "test-run-turn" : passthrough ? "test-passthrough" : "openai-chat", + ...(passthrough ? { passthrough: true as const } : {}), + buildRequest(parsed, incoming) { const rawBody = parsed._rawBody as { service_tier?: unknown } | undefined; - const body = JSON.stringify({ - model: parsed.modelId, - messages: parsed.context.messages, - ...(rawBody?.service_tier !== undefined ? { service_tier: rawBody.service_tier } : {}), - }); + const body = passthrough + ? JSON.stringify(parsed._rawBody) + : JSON.stringify({ + model: parsed.modelId, + messages: parsed.context.messages, + ...(rawBody?.service_tier !== undefined ? { service_tier: rawBody.service_tier } : {}), + }); builtBodies.push(body); - return { url: provider.baseUrl, method: "POST", headers: {}, body }; + const release = passthrough + ? incoming.translatorBudget.observeExternallyCapped( + "passthrough_serialization", + Buffer.byteLength(body, "utf8"), + ) + : undefined; + return { + url: provider.baseUrl, + method: "POST", + headers: {}, + body, + ...(release ? { + releaseBodyObservation: () => { + bodyObservationReleaseCalls += 1; + release(); + }, + } : {}), + }; }, async fetchResponse() { const index = httpCalls; @@ -68,7 +91,11 @@ function fixtureAdapter(provider: OcxProviderConfig): ProviderAdapter { mock.module("../src/server/adapter-resolve", () => ({ ...actualResolver, resolveAdapter(provider: OcxProviderConfig, cacheRetention?: "none" | "short" | "long") { - if (provider.adapter === "test-run-turn" || provider.adapter === "test-http") { + if ( + provider.adapter === "test-run-turn" + || provider.adapter === "test-http" + || provider.adapter === "test-passthrough" + ) { return fixtureAdapter(provider); } return actualResolveAdapter(provider, cacheRetention); @@ -77,8 +104,11 @@ mock.module("../src/server/adapter-resolve", () => ({ const { handleResponses } = await import("../src/server/responses"); -function config(adapter: "test-run-turn" | "test-http", extra: Partial = {}): OcxConfig { - return { +function config( + adapter: "test-run-turn" | "test-http" | "test-passthrough", + extra: Partial = {}, +): OcxConfig { + const result = { port: 0, defaultProvider: "fixture", emptyCompletionRetry: true, @@ -93,6 +123,18 @@ function config(adapter: "test-run-turn" | "test-http", extra: Partial { + passthroughFetchCalls += 1; + return Response.json({ + id: "resp_fixture", + object: "response", + status: "completed", + output: [], + }); + }; + } + return result; } function request( @@ -114,6 +156,8 @@ beforeEach(() => { parsedAttempts = []; builtBodies = []; customRunTurn = undefined; + passthroughFetchCalls = 0; + bodyObservationReleaseCalls = 0; }); afterEach(() => { @@ -122,6 +166,78 @@ afterEach(() => { }); describe("empty-completion core integration", () => { + test("an unconfigured limit sends an oversized passthrough body upstream", async () => { + // The regression guard for this whole feature. A default ceiling here would refuse turns + // that succeed today: the one measured limit in this codebase is the WS create-frame size, + // and that transport already falls back to HTTP SSE for exactly these bodies (#2473), with + // an 18.2 MB HTTP 200 observed in #2426. Unset must mean "send it", not "guess a ceiling". + const logCtx: RequestLogContext = { model: "", provider: "" }; + const response = await handleResponses( + request(false, "x".repeat(20 * 1024 * 1024)), + config("test-passthrough"), + logCtx, + ); + + expect(response.status).toBe(200); + expect(passthroughFetchCalls).toBe(1); + expect(logCtx.errorCode).toBeUndefined(); + }); + + test("an oversized passthrough body is refused locally and releases its observation", async () => { + const logCtx: RequestLogContext = { model: "", provider: "" }; + const response = await handleResponses( + request(false, "x".repeat(512)), + config("test-passthrough", { maxUpstreamBodyBytes: 128 }), + logCtx, + ); + const body = await response.json() as { error?: { code?: string } }; + + expect(response.status).toBe(413); + expect(body.error?.code).toBe("outbound_body_too_large"); + expect(logCtx.errorCode).toBe("outbound_body_too_large"); + expect(passthroughFetchCalls).toBe(0); + expect(bodyObservationReleaseCalls).toBe(1); + }); + + test("a streaming refusal is terminal overflow, not a retryable 413", async () => { + // Codex resends on an HTTP 413, so the shape that stops the loop is response.failed with + // context_length_exceeded — the same contract the upstream-413 path already returns (#3177). + const response = await handleResponses( + request(true, "x".repeat(512)), + config("test-passthrough", { maxUpstreamBodyBytes: 128 }), + { model: "", provider: "" }, + ); + + expect(response.status).toBe(200); + const text = await response.text(); + expect(text).toContain("response.failed"); + expect(text).toContain("context_length_exceeded"); + expect(passthroughFetchCalls).toBe(0); + }); + + test("a normal-sized passthrough body still reaches upstream", async () => { + const response = await handleResponses( + request(false), + config("test-passthrough", { maxUpstreamBodyBytes: 4_096 }), + { model: "", provider: "" }, + ); + + expect(response.status).toBe(200); + expect(passthroughFetchCalls).toBe(1); + expect(bodyObservationReleaseCalls).toBe(1); + }); + + test("an explicit zero limit lets an oversized turn reach upstream", async () => { + const response = await handleResponses( + request(false, "x".repeat(512)), + config("test-passthrough", { maxUpstreamBodyBytes: 0 }), + { model: "", provider: "" }, + ); + + expect(response.status).toBe(200); + expect(passthroughFetchCalls).toBe(1); + }); + test("streaming runTurn returns the local 429 contract when initial pacing admission is rejected", async () => { setProviderRequestPacingLimitsForTest({ maxQueueDepth: 0 }); const overloaded = config("test-run-turn"); diff --git a/tests/outbound-body-guard.test.ts b/tests/outbound-body-guard.test.ts new file mode 100644 index 0000000000..45b832e637 --- /dev/null +++ b/tests/outbound-body-guard.test.ts @@ -0,0 +1,87 @@ +import { describe, expect, test } from "bun:test"; +import { + checkOutboundBodySize, + describeOutboundBodyRefusal, +} from "../src/server/responses/outbound-body-guard"; + +describe("outbound body guard", () => { + test("an unconfigured limit admits without measuring", () => { + // The whole default contract: no ceiling is inferred for any destination, so a body far + // above any observed transport limit still goes out exactly as it does today. + const huge = "x".repeat(20 * 1024 * 1024); + const result = checkOutboundBodySize(huge, undefined); + expect(result.admitted).toBe(true); + expect(result.limit).toBe(0); + // bytes stays 0 because the body was never measured — this is the cheap path. + expect(result.bytes).toBe(0); + }); + + test("an explicit 0 disables the guard", () => { + const huge = "x".repeat(20 * 1024 * 1024); + expect(checkOutboundBodySize(huge, 0).admitted).toBe(true); + }); + + test("refuses only above the configured limit", () => { + expect(checkOutboundBodySize("12345", 5).admitted).toBe(true); + expect(checkOutboundBodySize("12345", 4).admitted).toBe(false); + }); + + test("measures UTF-8 bytes, not code units", () => { + // A 1-character string is 3 bytes here; measuring .length would wrongly admit it. + const result = checkOutboundBodySize("界", 2); + expect(result.admitted).toBe(false); + expect(result.bytes).toBe(3); + }); + + test("an unparseable oversized body still refuses, without diagnostics", () => { + const result = checkOutboundBodySize("{not-json", 1); + expect(result.admitted).toBe(false); + expect(result.imageCount).toBe(0); + }); + + test("counts embedded input_image payloads and names them in the refusal", () => { + const image = "A".repeat(4000); + const body = JSON.stringify({ + input: [{ + role: "user", + content: [ + { type: "input_text", text: "look" }, + { type: "input_image", image_url: `data:image/png;base64,${image}` }, + { type: "input_image", image_url: `data:image/png;base64,${image}` }, + ], + }], + }); + const result = checkOutboundBodySize(body, 100); + expect(result.admitted).toBe(false); + expect(result.imageCount).toBe(2); + expect(result.imageBytes).toBeGreaterThan(5000); + + const message = describeOutboundBodyRefusal(result); + expect(message).toContain("2 input_image items"); + expect(message).toContain("compact the conversation"); + }); + + test("finds images nested in function_call_output, not just message content", () => { + // Replayed tool results are a common place for accumulated screenshots to hide. + const body = JSON.stringify({ + input: [{ + type: "function_call_output", + output: [{ type: "input_image", image_url: "data:image/png;base64,AAAA" }], + }], + }); + expect(checkOutboundBodySize(body, 10).imageCount).toBe(1); + }); + + test("a self-referential body cannot hang the diagnostic walk", () => { + const body = JSON.stringify({ input: [{ type: "input_image", image_url: "data:,x" }] }); + expect(checkOutboundBodySize(body, 1).imageCount).toBe(1); + }); + + test("the singular form reads correctly for one image", () => { + const body = JSON.stringify({ + input: [{ type: "input_image", image_url: "data:image/png;base64,AAAA" }], + }); + const message = describeOutboundBodyRefusal(checkOutboundBodySize(body, 10)); + expect(message).toContain("1 input_image item "); + }); +}); From c7f3f6f316aa5500970e18a9bf9cc7cc2282edfa Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 03:45:25 +0900 Subject: [PATCH 130/172] feat(router): summarize compaction on the default provider when no canonical openai route exists (#3201) Codex selects a bare OpenAI-family model for compaction even when the operator routes every ordinary turn to another provider (GitHub Copilot, OpenRouter, ...). routeModel reserves bare native ids for the canonical openai provider and threw NoEnabledOpenAiProviderError, so such users hit 404 on every compaction while the routedCompaction summarizer bridge in core.ts was already able to serve them. Add routeCompactionModel: identical to routeModel except that, when the bare-native branch has no enabled canonical openai provider, it may land on config.defaultProvider (own provider, enabled, not an OpenAI-family entry) with routeReason compaction-default-provider. Wire it into both compaction entrypoints: v1 handleResponsesCompact and the initial v2 compaction_trigger route in handleResponsesInner. Combo attempts, fallback and recovery re-routes, ordinary turns, and account-qualified selectors (side/gpt-*) keep today behaviour. One console notice per provider, no request content. Closes #2901 Co-authored-by: jun --- .../030_wp3_compaction_provider.md | 138 ++++++++++++++++++ .../031_wp3_audit_r1_synthesis.md | 70 +++++++++ .../content/docs/reference/proxy-formats.md | 10 ++ src/router.ts | 74 +++++++++- src/server/responses/compact.ts | 7 +- src/server/responses/core.ts | 9 +- tests/responses-compaction-routing.test.ts | 106 ++++++++++++++ tests/router.test.ts | 75 +++++++++- 8 files changed, 479 insertions(+), 10 deletions(-) create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/030_wp3_compaction_provider.md create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/031_wp3_audit_r1_synthesis.md diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/030_wp3_compaction_provider.md b/devlog/_plan/260902_nonbug_adoption_backlog/030_wp3_compaction_provider.md new file mode 100644 index 0000000000..e49368994d --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/030_wp3_compaction_provider.md @@ -0,0 +1,138 @@ +# wp3 — #2901 compaction provider selection + +Issue #2901 (score 58), no implementation PR. Reported on 2.31.0/macOS by a user +running GitHub Copilot as their only provider. + +## What actually happens + +Ordinary turns route to GHCP. Compaction alone returns: + +``` +404 Model gpt-5.6-sol requires the canonical openai provider. +Run: ocx provider add openai && ocx sync && ocx restart +``` + +The path is short and every step is in the current tree: + +1. `handleResponsesCompact` calls the ordinary router — + `route = routeModel(config, raw.model, evidenceFromBody(raw))` + (`src/server/responses/compact.ts:512`). +2. `routeModel` reserves every bare `gpt-*`/`o1-`/`o3-`/`o4-` id for the canonical + provider: `isBareOpenAiFamilyModel` (`src/router.ts:510-513`) is checked at + `:701`, **before** configured model lists and `defaultProvider` at `:749-752`. + With no enabled `openai` row it throws `NoEnabledOpenAiProviderError` + (`:706`, class at `:468-475`). +3. `compact.ts:513-519` turns any router throw into the 404 the user sees. + +`compactProvider` is only ever `route.provider` (`compact.ts:595`). There is no +`compactModel`/`compactProvider` config key anywhere in the tree. + +## Why the existing mitigations do not reach it + +**#2858 compact handoff.** `compactHandoffRoutes` (`compact.ts:164`) remembers a +model that *demonstrably compacted this thread*, and it is only written by +`rememberCompactHandoffRoute` after a successful compaction +(`compact.ts:1095,1106`). A GHCP-only user never records an entry, because their +first compaction dies at step 3. The map is a quota-failover aid, not a +bootstrap. + +**#636 catalog suppression.** `src/codex/catalog/sync.ts:1674-1676` already stops +advertising bare `gpt-*` rows when only non-OpenAI providers are configured, +precisely so they cannot hard-404. That fix covers the model *picker*. It cannot +cover compaction, because the Codex client chooses the compaction model itself +rather than taking it from the served catalog. + +So the established project position is already: **a bare native id that cannot +route should not become a hard failure for a user who never configured OpenAI.** +This issue is the same rule applied to the one surface #636 could not reach. + +## The machinery for the fix already exists + +`core.ts:3587-3588` computes +`routedCompaction = parsed._compactionRequest === true && !isCanonicalOpenAiForwardProvider(route.provider)`, +and when true it strips tools, web-search, tool choice and structured output, runs +the routed model as a plain summarizer, and lets the bridge append the synthetic +compaction item (`src/responses/compaction.ts`). Compacting on a non-OpenAI +provider is a supported, exercised path. + +The only thing missing is that a *bare native id* never reaches it: the router +refuses before any of that runs. + +## Design: fall back rather than add a setting + +The issue title asks for a setting. A setting is the worse answer here. + +A config key requires the user to discover that compaction is a separate routing +decision, learn a new key name, and edit JSON — after hitting an error whose text +tells them to install a provider they deliberately do not want. The failure is +total (the conversation cannot continue), so the remedy should not be homework. + +Instead: when a compaction request carries a bare native model and no canonical +`openai` provider is enabled, route it the way the user's ordinary turns already +route, and let the existing routed-compaction path summarize. + +**This cannot change behavior for any working configuration.** The fallback is +reachable only where `routeModel` throws `NoEnabledOpenAiProviderError` today — +i.e. only where the current outcome is a hard 404. A user with an enabled +`openai` provider takes the identical branch they take now. That is why this +needs no opt-in flag: there is no behavior to preserve, only an error to replace. + +## Audit amendment (A1) + +The first plan was too narrow. A source audit found that the v1 compact handler is +not the only entry point: v2 `compaction_trigger` requests enter +`handleResponsesInner`, whose initial `routeModel` call fails before the existing +`routedCompaction` bridge can run. The implementation must therefore use one +compaction-only routing helper from both entry points. + +The helper may fall back only for an unqualified bare OpenAI-family model when +the canonical `openai` provider is absent or disabled and the configured default +provider is active. It must rethrow for account-qualified selectors such as +`side/gpt-5.5`, policy/combo routes, disabled or missing defaults, and every +other router error. This preserves exact account routing and keeps ordinary +turns on today's path. + +## File change map + +| File | Action | Change | +|------|--------|--------| +| `src/router.ts` | MODIFY | add a compaction-only route helper that preserves the native reservation for ordinary requests and permits the narrowly gated default-provider fallback described above; retain route-decision metadata with a distinct reason | +| `src/server/responses/compact.ts` | MODIFY | call the shared helper for v1 compact routing and log one sanitized substitution line when the helper selects the configured default | +| `src/server/responses/core.ts` | MODIFY | call the same helper for the initial v2 `compaction_trigger` route, while leaving ordinary and recovery/model-change routes on ordinary routing | +| `docs-site/.../guides/` (compaction reference) | MODIFY | document that a bare native compaction model falls back to the configured default when no canonical OpenAI provider is enabled | +| `tests/router.test.ts` | MODIFY | prove helper-only fallback, account namespace fail-closed behavior, and unchanged ordinary `routeModel` behavior | +| `tests/responses-compaction-routing.test.ts` | MODIFY | regressions for v1 and v2 entry points, canonical OpenAI preservation, non-native errors, and one-time logging | + +## Scope boundary + +IN: the compaction fallback for the v1 `/v1/responses/compact` handler and v2 +`compaction_trigger` turn, its log line, docs, and tests. + +OUT: a `compactProvider`/`compactModel` config key. If an operator later wants to +*pin* compaction to a specific model while having a working `openai` provider, +that is a genuine feature and a separate cycle; it is not what unblocks #2901. +OUT: changing `isBareOpenAiFamilyModel` or the router's native reservation, which +is load-bearing for ordinary turns. + +## Accept criteria + +1. **GHCP-only config, bare native compaction model: both entry points succeed** + and are summarized by the configured provider. Activation: a config with no + `openai` row, a v1 compact request and a v2 `compaction_trigger` request for + `gpt-5.6-sol`, asserting non-404 status and that the routed provider received + the turn. +2. **A working openai config is untouched.** Activation: the same request with an + enabled canonical provider still routes to it; assert the provider chosen is + the canonical one and no fallback log fires. +3. **Other router failures still 404.** Activation: an unroutable non-native model + id still returns the original error, proving the catch is narrow. +4. **Exact account selectors remain fail-closed.** Activation: with a configured + `side` account namespace but no canonical `openai`, `side/gpt-5.5` still + returns `NoEnabledOpenAiProviderError` and never reaches the default provider. +5. The substitution is logged once with both model ids, without persisting + credentials or raw request bodies. + +## Verifier + +`bun x tsc --noEmit` plus the focused router and compaction tests. Full suite +forbidden. diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/031_wp3_audit_r1_synthesis.md b/devlog/_plan/260902_nonbug_adoption_backlog/031_wp3_audit_r1_synthesis.md new file mode 100644 index 0000000000..e925c4e8b0 --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/031_wp3_audit_r1_synthesis.md @@ -0,0 +1,70 @@ +# wp3 audit R1 — #2901 compaction provider selection + +## Verdict + +NEAR-PASS after plan amendment. The reported failure is real and the existing +routed-compaction bridge is the correct destination, but the initial plan's +`compact.ts`-only change would leave v2 `compaction_trigger` requests broken. +The amendment makes the routing decision shared and keeps its fallback narrowly +scoped to the failing, unqualified native-model case. + +## Evidence reviewed + +- `src/server/responses/compact.ts` routes the v1 handler through `routeModel` + before it can choose the synthetic routed-compaction path. +- `src/server/responses/core.ts` performs a separate initial `routeModel` call + in `handleResponsesInner`; its later `routedCompaction` branch cannot run if + that call throws. +- `src/router.ts` raises `NoEnabledOpenAiProviderError` both for bare native + model ids and for exact Codex account namespaces. The latter is an explicit + account-selection boundary and must not be treated as a generic fallback. +- `src/server/responses/core.ts` already strips the private trigger and appends + `COMPACT_PROMPT` for noncanonical routed compaction, so no new provider-side + compaction protocol is needed. +- `src/codex/catalog/sync.ts` suppresses bare native rows in non-OpenAI-only + catalogs, confirming that a bare native id without a canonical route is not a + supported ordinary-turn destination; it does not cover client-selected + compaction models. + +## Accepted blockers and dispositions + +1. **Missing v2 coverage — accepted.** Add a shared `routeCompactionModel` + (name may follow repository conventions) and invoke it for both v1 compact + routing and the initial v2 compaction route. Add an activation test that + exercises the real `handleResponses` path with `compaction_trigger`. +2. **Account namespace over-catch — accepted.** Do not catch every + `NoEnabledOpenAiProviderError`. The fallback predicate must require an + unqualified bare OpenAI-family id and an active configured default provider; + exact account-qualified ids and all other routing errors rethrow. +3. **Ordinary routing regression — accepted.** Keep `routeModel` unchanged for + non-compaction requests and assert that the same GHCP-only native id still + throws there. The new helper is an explicit request-surface choice, not a + global relaxation of native model ownership. +4. **Configuration-key alternative — rejected for this cycle.** A pinning key + would solve a different problem (choosing among working providers) and would + add setup burden to a request currently failing solely because of an invalid + native reservation. The fallback changes only a hard failure and is therefore + safer than introducing a new default-on routing preference. +5. **Logging/privacy — accepted with guardrails.** Emit at most one warning from + the v1/v2 request path, using sanitized model labels and no body, token, or + account data. The route-decision reason remains the machine-readable audit + signal for tests and request logs. + +## Activation matrix + +| Case | Expected result | +| --- | --- | +| GHCP-only + bare `gpt-*` + v1 compact | default provider receives routed summary | +| GHCP-only + bare `gpt-*` + v2 trigger | same routed summary bridge succeeds | +| canonical `openai` enabled + bare native id | canonical route unchanged; no fallback | +| configured account namespace + `side/gpt-*` | original canonical-auth error; no fallback | +| non-native unknown model | original 404/error unchanged | +| ordinary `/v1/responses` + GHCP-only bare native id | original native reservation error | + +## Residual risk + +The default provider may advertise a model alias that differs from the bare +native id. The helper must preserve the caller's model id as the routed model +unless the normal route result supplies an explicit effective id; focused tests +should assert the actual upstream body and provider, not merely a successful +HTTP status. diff --git a/docs-site/src/content/docs/reference/proxy-formats.md b/docs-site/src/content/docs/reference/proxy-formats.md index bb4c7afba0..a3219e9139 100644 --- a/docs-site/src/content/docs/reference/proxy-formats.md +++ b/docs-site/src/content/docs/reference/proxy-formats.md @@ -285,6 +285,16 @@ conversation. | Canonical ChatGPT or official OpenAI route | Forwards the request to the native `/responses/compact` endpoint with the resolved account and model authentication | | Other routed model | Runs an internal, non-streaming, no-tools compaction turn with a `compaction_trigger`; requires exactly one synthetic `compaction` item whose `encrypted_content` is an `ocx1:` envelope; decodes that summary into v1 replacement history | +Codex names a bare OpenAI-family model (for example `gpt-5.6-sol`) for its compaction turns +regardless of which provider the operator routes ordinary turns to. Ordinary requests reserve +such ids for the canonical `openai` provider. On the compaction surface only — `POST +/v1/responses/compact` and a `POST /v1/responses` turn carrying a `compaction_trigger` — a bare +native model with no enabled canonical `openai` provider falls back to the configured +`defaultProvider` as the summarizer instead of returning 404. The fallback applies only when the +default provider is enabled and is not itself an OpenAI-family entry; account-qualified selectors +such as `side/gpt-5.6-sol` still fail closed. The proxy logs one notice per provider when this +fallback engages. Configurations with an enabled canonical `openai` provider are unchanged. + Native compact responses are buffered with a 32 MiB maximum, including responses whose declared `Content-Length` already exceeds the limit. The compact-specific failures include: diff --git a/src/router.ts b/src/router.ts index 66830a71b1..cc3d66a4bd 100644 --- a/src/router.ts +++ b/src/router.ts @@ -265,6 +265,25 @@ function warnIfBaseUrlDiscarded(providerName: string, userBaseUrl: string, effec ); } +/** + * One notice per provider id: the destination is an operator-configured key + * (never a caller-supplied string), so the log carries no request content. + */ +const compactionFallbackWarnings = new Set(); +function warnCompactionDefaultProviderFallbackOnce(providerName: string): void { + if (compactionFallbackWarnings.has(providerName)) return; + compactionFallbackWarnings.add(providerName); + console.warn( + `compaction: no enabled canonical "openai" provider for the native compaction model;` + + ` summarizing through default provider "${providerName}" instead (#2901).`, + ); +} + +/** Test seam: forget which compaction fallbacks have been announced. */ +export function resetCompactionFallbackWarningsForTests(): void { + compactionFallbackWarnings.clear(); +} + function usableResolvedApiKey(apiKey: string | undefined): string | undefined { const resolved = resolveEnvValue(apiKey); return typeof resolved === "string" && resolved.trim().length > 0 ? resolved : undefined; @@ -578,6 +597,7 @@ function routeModelInternal( modelId: string, bypassCombos: boolean, policyEvidence?: PolicyRequestEvidence, + allowCompactionNativeFallback = false, ): RouteResult { const slash = modelId.indexOf("/"); // Policy namespace is system-reserved: an explicit `policy/` or a @@ -703,6 +723,28 @@ function routeModelInternal( if (provider && provider.disabled !== true) { return routeResult(config, OPENAI_CODEX_PROVIDER_ID, provider, modelId, "native", "native-family"); } + // Codex chooses a bare native model for compaction even when the operator's + // ordinary route is a third-party provider. Keep the native reservation + // unchanged for ordinary turns; only the explicit compaction surface may + // use the configured default as its summarizer destination. + if (allowCompactionNativeFallback + && config.defaultProvider !== OPENAI_CODEX_PROVIDER_ID + && config.defaultProvider !== LEGACY_CHATGPT_PROVIDER_ID + && config.defaultProvider !== LEGACY_OPENAI_MULTI_PROVIDER_ID + && hasOwnProvider(config.providers, config.defaultProvider)) { + const defaultProvider = config.providers[config.defaultProvider]; + if (defaultProvider.disabled !== true) { + warnCompactionDefaultProviderFallbackOnce(config.defaultProvider); + return routeResult( + config, + config.defaultProvider, + defaultProvider, + modelId, + "default-provider", + "compaction-default-provider", + ); + } + } throw new NoEnabledOpenAiProviderError(modelId); } @@ -755,12 +797,7 @@ function routeModelInternal( throw new Error(`No provider configured for model: ${modelId}`); } -export function routeModel( - config: OcxConfig, - modelId: string, - policyEvidence?: PolicyRequestEvidence, -): RouteResult { - const route = routeModelInternal(config, modelId, false, policyEvidence); +function routeWithDecisionTrace(config: OcxConfig, modelId: string, route: RouteResult): RouteResult { // Policy routes carry a full evaluation trace already; never rebuild it. if (route.routeDecision) return route; const accountRef = route.codexAccountNamespace; @@ -785,6 +822,31 @@ export function routeModel( return route; } +export function routeModel( + config: OcxConfig, + modelId: string, + policyEvidence?: PolicyRequestEvidence, +): RouteResult { + const route = routeModelInternal(config, modelId, false, policyEvidence); + return routeWithDecisionTrace(config, modelId, route); +} + +/** + * Route a client-selected compaction model. Codex may send a bare native model + * even when its ordinary turns are configured for another provider; in that + * one case the configured default provider is a safe summarizer destination. + * This helper is intentionally separate so ordinary requests retain the + * canonical OpenAI reservation and exact account selectors remain fail-closed. + */ +export function routeCompactionModel( + config: OcxConfig, + modelId: string, + policyEvidence?: PolicyRequestEvidence, +): RouteResult { + const route = routeModelInternal(config, modelId, false, policyEvidence, true); + return routeWithDecisionTrace(config, modelId, route); +} + /** Resolve a combo-selected provider/model target without consulting public combo aliases again. */ export function routeConcreteModel(config: OcxConfig, modelId: string): RouteResult { return routeModelInternal(config, modelId, true, undefined); diff --git a/src/server/responses/compact.ts b/src/server/responses/compact.ts index dd9bd47d63..be6d9381ea 100644 --- a/src/server/responses/compact.ts +++ b/src/server/responses/compact.ts @@ -9,7 +9,7 @@ import { parseRequest } from "../../responses/parser"; import { buildCompactV1Output, COMPACT_PROMPT, decodeCompactionSummary, extractCompactUserMessages } from "../../responses/compaction"; import { FORWARD_HEADERS, sanitizeReasoningInputContent } from "../../adapters/openai-responses"; import { expandPreviousResponseInput, previousResponseProviderState, rememberResponseState } from "../../responses/state"; -import { NoEligiblePolicyCandidateError, routeModel } from "../../router"; +import { NoEligiblePolicyCandidateError, routeCompactionModel } from "../../router"; import { evidenceFromBody } from "../../routing/request-evidence"; import { advanceComboAfterFailure, @@ -509,7 +509,10 @@ export async function handleResponsesCompact( // Compact requests route through the same policy evaluation as normal // turns, so body-derived evidence (tools/image) must reach the first // evaluation too - not only the later handleResponses dispatch. - route = routeModel(config, raw.model, evidenceFromBody(raw)); + // Codex selects a bare native model for compaction even when the operator + // routes ordinary turns elsewhere (#2901); the compaction-scoped router + // may land that on the configured default provider instead of 404. + route = routeCompactionModel(config, raw.model, evidenceFromBody(raw)); } catch (err) { if (err instanceof NoEligiblePolicyCandidateError) { // Persist the evaluation trace (per-candidate exclusions + the diff --git a/src/server/responses/core.ts b/src/server/responses/core.ts index d4e45cde2a..1e84aab967 100644 --- a/src/server/responses/core.ts +++ b/src/server/responses/core.ts @@ -58,6 +58,7 @@ import { import { comboRouteDecisionTrace, NoEligiblePolicyCandidateError, + routeCompactionModel, routeConcreteModel, routeModel, type RouteResult, @@ -2832,9 +2833,15 @@ async function handleResponsesInner( let route: RouteResult; try { + // A `compaction_trigger` turn may name a bare native model the operator has + // no canonical OpenAI route for (#2901). Only the initial compaction route + // may fall back to the configured default provider; combo attempts and the + // later fallback/recovery re-routes keep the ordinary reservation. const resolveRoute = (modelId: string) => options.comboAttempt ? routeConcreteModel(config, modelId) - : routeModel(config, modelId, evidenceFromBody(parsed._rawBody)); + : parsed._compactionRequest === true + ? routeCompactionModel(config, modelId, evidenceFromBody(parsed._rawBody)) + : routeModel(config, modelId, evidenceFromBody(parsed._rawBody)); const _sci = config.shadowCallIntercept; let shadowRoute: RouteResult | undefined; if (_sci?.enabled && _sci.model && isShadowSourceModel(parsed.modelId, _sci.sourceModels)) { diff --git a/tests/responses-compaction-routing.test.ts b/tests/responses-compaction-routing.test.ts index 47fd315aa4..e269367fd1 100644 --- a/tests/responses-compaction-routing.test.ts +++ b/tests/responses-compaction-routing.test.ts @@ -725,6 +725,112 @@ describe("routed compaction for key-mode openai-responses (#422)", () => { }); }); +describe("bare native compaction model without canonical openai (#2901)", () => { + /** A GitHub-Copilot-style operator: one third-party provider, no `openai` row at all. */ + function copilotOnlyConfig(): OcxConfig { + return { + defaultProvider: "gw", + providers: { + gw: { + adapter: "openai-chat", + baseUrl: "https://api.githubcopilot.com", + authMode: "key", + apiKey: "ghu_test", + models: ["gpt-5.6-sol"], + }, + }, + } as unknown as OcxConfig; + } + + function chatCompletionPayload(text: string): Record { + return { + choices: [{ index: 0, message: { role: "assistant", content: text }, finish_reason: "stop" }], + usage: { prompt_tokens: 10, completion_tokens: 5 }, + }; + } + + test("v2 compaction_trigger turn summarizes through the default provider instead of 404", async () => { + const calls: Array<{ url: string; body: Record }> = []; + globalThis.fetch = (async (url: unknown, init?: RequestInit) => { + calls.push({ url: String(url), body: JSON.parse(String(init?.body ?? "{}")) as Record }); + return jsonResponse(chatCompletionPayload("handoff summary")); + }) as typeof fetch; + + const logCtx: RequestLogContext = { model: "", provider: "" }; + const res = await handleResponses( + compactionRequest(baseCompactionBody({ model: "gpt-5.6-sol" })), + copilotOnlyConfig(), + logCtx, + ); + + expect(res.status).toBe(200); + expect(calls.length).toBe(1); + expect(calls[0]!.url.startsWith("https://api.githubcopilot.com")).toBe(true); + expect(calls[0]!.body.model).toBe("gpt-5.6-sol"); + // Still the summarizer contract: no private trigger leaks to the third-party gateway. + expect(JSON.stringify(calls[0]!.body)).not.toContain("compaction_trigger"); + expect(JSON.stringify(calls[0]!.body.messages)).toContain("CONTEXT CHECKPOINT COMPACTION"); + expect(logCtx.provider).toBe("gw"); + expect(logCtx.routeDecision?.selected).toMatchObject({ provider: "gw", reason: "compaction-default-provider" }); + const json = await res.json() as { output?: Array<{ type?: string }> }; + expect((json.output ?? []).filter(item => item.type === "compaction").length).toBe(1); + }); + + test("v1 /responses/compact takes the same fallback", async () => { + let upstreamCalls = 0; + globalThis.fetch = (async () => { + upstreamCalls += 1; + return jsonResponse(chatCompletionPayload("handoff summary")); + }) as typeof fetch; + + const logCtx: RequestLogContext = { model: "", provider: "" }; + const res = await handleResponsesCompact( + compactionRequest(baseCompactionBody({ model: "gpt-5.6-sol" })), + copilotOnlyConfig(), + logCtx, + ); + + expect(res.status).toBe(200); + expect(upstreamCalls).toBe(1); + expect(logCtx.provider).toBe("gw"); + expect(logCtx.requestedModel).toBe("gpt-5.6-sol"); + }); + + test("ordinary turns on the same config keep the canonical-openai reservation", async () => { + let upstreamCalls = 0; + globalThis.fetch = (async () => { + upstreamCalls += 1; + return jsonResponse(chatCompletionPayload("must not be reached")); + }) as typeof fetch; + + const body = baseCompactionBody({ model: "gpt-5.6-sol" }); + body.input = (body.input as Array>).filter(item => item.type !== "compaction_trigger"); + const res = await handleResponses(compactionRequest(body), copilotOnlyConfig(), { model: "", provider: "" }); + + expect(res.status).toBe(404); + expect(upstreamCalls).toBe(0); + }); + + test("an account-qualified native selector still fails closed on compaction", async () => { + let upstreamCalls = 0; + globalThis.fetch = (async () => { + upstreamCalls += 1; + return jsonResponse(chatCompletionPayload("must not be reached")); + }) as typeof fetch; + + const config = copilotOnlyConfig(); + (config as { codexAccountNamespaces?: Record }).codexAccountNamespaces = { side: "side-account-id" }; + const res = await handleResponsesCompact( + compactionRequest(baseCompactionBody({ model: "side/gpt-5.6-sol" })), + config, + { model: "", provider: "" }, + ); + + expect(res.status).toBe(404); + expect(upstreamCalls).toBe(0); + }); +}); + describe("compaction terminal handling (#422)", () => { test("an upstream failure does not become an empty compaction", async () => { globalThis.fetch = (async () => jsonResponse({ diff --git a/tests/router.test.ts b/tests/router.test.ts index 13876194e6..6a95292935 100644 --- a/tests/router.test.ts +++ b/tests/router.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from "bun:test"; import { mapReasoningEffort } from "../src/reasoning-effort"; -import { NoEnabledOpenAiProviderError, routeModel } from "../src/router"; +import { NoEnabledOpenAiProviderError, routeCompactionModel, routeModel } from "../src/router"; import type { OcxConfig, OcxProviderConfig } from "../src/types"; describe("routeModel registry effort defaults", () => { @@ -699,3 +699,76 @@ describe("routeModel blocked model redirect", () => { expect(routed.routeDecision?.requestedModel).toBe("side/gpt-5.6-terra"); }); }); + +describe("routeCompactionModel (#2901)", () => { + const ghcpOnly: OcxConfig = { + port: 10100, + defaultProvider: "github-copilot", + providers: { + "github-copilot": { + adapter: "openai-chat", + baseUrl: "https://api.githubcopilot.com", + authMode: "key", + apiKey: "ghu_test", + models: ["gpt-5.6-sol", "claude-opus-4-6"], + }, + }, + codexAccountNamespaces: { side: "side-account-id" }, + }; + + test("falls back to the configured default provider only on the compaction surface", () => { + // Ordinary turns keep today's reservation: a bare native id without canonical openai is terminal. + expect(() => routeModel(ghcpOnly, "gpt-5.6-sol")).toThrow(NoEnabledOpenAiProviderError); + expect(routeCompactionModel(ghcpOnly, "gpt-5.6-sol")).toMatchObject({ + providerName: "github-copilot", + modelId: "gpt-5.6-sol", + routeKind: "default-provider", + routeReason: "compaction-default-provider", + }); + expect(routeCompactionModel(ghcpOnly, "gpt-5.6-sol").routeDecision?.selected).toMatchObject({ + provider: "github-copilot", + model: "gpt-5.6-sol", + reason: "compaction-default-provider", + }); + }); + + test("keeps exact account selectors and canonical-openai configs unchanged", () => { + // An account-qualified native selector names a credential; it must stay fail-closed. + expect(() => routeCompactionModel(ghcpOnly, "side/gpt-5.6-sol")).toThrow(NoEnabledOpenAiProviderError); + // With canonical openai enabled the compaction route is identical to the ordinary one. + const withOpenAi: OcxConfig = { + ...ghcpOnly, + providers: { + ...ghcpOnly.providers, + openai: { adapter: "openai-responses", baseUrl: "https://chatgpt.com/backend-api/codex", authMode: "forward" }, + }, + }; + expect(routeCompactionModel(withOpenAi, "gpt-5.6-sol")).toMatchObject({ + providerName: "openai", + routeReason: "native-family", + }); + // Same destination as the ordinary router; the decision trace carries a fresh id/timestamp. + const { routeDecision: _a, ...compactionRoute } = routeCompactionModel(withOpenAi, "gpt-5.6-sol"); + const { routeDecision: _b, ...ordinaryRoute } = routeModel(withOpenAi, "gpt-5.6-sol"); + expect(compactionRoute).toEqual(ordinaryRoute); + }); + + test("does not resurrect a disabled, missing, or legacy default provider", () => { + const disabledDefault: OcxConfig = { + ...ghcpOnly, + providers: { "github-copilot": { ...ghcpOnly.providers["github-copilot"]!, disabled: true } }, + }; + expect(() => routeCompactionModel(disabledDefault, "gpt-5.6-sol")).toThrow(NoEnabledOpenAiProviderError); + const missingDefault: OcxConfig = { ...ghcpOnly, defaultProvider: "nowhere" }; + expect(() => routeCompactionModel(missingDefault, "gpt-5.6-sol")).toThrow(NoEnabledOpenAiProviderError); + const openAiDefaultDisabled: OcxConfig = { + ...ghcpOnly, + defaultProvider: "openai", + providers: { + ...ghcpOnly.providers, + openai: { adapter: "openai-responses", baseUrl: "https://chatgpt.com/backend-api/codex", authMode: "forward", disabled: true }, + }, + }; + expect(() => routeCompactionModel(openAiDefaultDisabled, "gpt-5.6-sol")).toThrow(NoEnabledOpenAiProviderError); + }); +}); From 59449fa83ce29cb1b82ea8f31037eeb4078c0610 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 03:49:32 +0900 Subject: [PATCH 131/172] docs(codex): explain why routed models are gated during reserve mode (#3202) When the ChatGPT 5-hour quota is exhausted and Codex offers gpt-reserve, the desktop app makes every other picker entry unselectable - including routed models that run on independent providers and consume none of that quota. The proxy cannot change this. The desktop app polls backend-api/wham/usage on its own authenticated connection and treats reserve as active when the response carries rate_limit_upsell.banner_type = "luna_reserve", rate_limit.allowed is false, and additional_rate_limits[] has an allowed "gpt-reserve" entry. While that holds it forces the model setting to gpt-reserve and rewrites any other pick back, so a config.toml model= value is overridden before any request reaches opencodex. The catalog plays no part in the decision. Document the mechanism, drop the earlier "unverified" hedge from the codex-app-models limitation section (EN + 7 locales), and point at the workaround that does work: clients that do not consult the usage snapshot (ocx claude, direct /v1, ocx access test). Closes #2813 Co-authored-by: jun --- .../docs/fr/guides/codex-app-models.md | 2 +- .../content/docs/guides/codex-app-models.md | 10 ++++--- .../content/docs/guides/codex-integration.md | 30 +++++++++++++++++++ .../docs/ja/guides/codex-app-models.md | 2 +- .../docs/ko/guides/codex-app-models.md | 2 +- .../docs/ru/guides/codex-app-models.md | 2 +- .../docs/tr/guides/codex-app-models.md | 2 +- .../docs/zh-cn/guides/codex-app-models.md | 2 +- .../docs/zh-tw/guides/codex-app-models.md | 2 +- 9 files changed, 43 insertions(+), 11 deletions(-) diff --git a/docs-site/src/content/docs/fr/guides/codex-app-models.md b/docs-site/src/content/docs/fr/guides/codex-app-models.md index 2c782dffe0..fb81b9597e 100644 --- a/docs-site/src/content/docs/fr/guides/codex-app-models.md +++ b/docs-site/src/content/docs/fr/guides/codex-app-models.md @@ -262,7 +262,7 @@ ou envoyez-le directement : ocx access test anthropic/claude-sonnet-5 --protocol responses ``` -Les deux chemins routent correctement **dès que la requête atteint le proxy** — c'est couvert par des tests. Ce qui n'est pas établi, c'est si l'application envoie encore le modèle configuré pendant le mode réserve ; si le client le réécrit ou le refuse avant l'envoi, aucun réglage côté proxy n'y change quoi que ce soit. Considérez la sélection explicite comme une piste à essayer plutôt qu'un contournement confirmé. +Les deux chemins routent correctement **dès que la requête atteint le proxy** — c'est couvert par des tests. En revanche, l'application de bureau Codex n'envoie pas le modèle configuré pendant le mode réserve : elle détermine l'état de réserve à partir de son propre sondage `wham/usage` (upsell `luna_reserve` plus une limite additionnelle `gpt-reserve` encore autorisée) et force le réglage de modèle sur `gpt-reserve` avant l'envoi, de sorte que la voie `config.toml` est écrasée dans l'application. Jusqu'à la réinitialisation de la fenêtre, utilisez `ocx access test`, Claude Code via le proxy (`ocx claude`) ou tout client `/v1` direct. Voir [Modèles routés pendant le mode réserve de Codex](/guides/codex-integration/#routed-models-during-codex-reserve-mode). Si le sélecteur affiche encore des entrées obsolètes, actualisez le catalogue et redémarrez l'interface Codex concernée : diff --git a/docs-site/src/content/docs/guides/codex-app-models.md b/docs-site/src/content/docs/guides/codex-app-models.md index 053fba1669..7115d86af3 100644 --- a/docs-site/src/content/docs/guides/codex-app-models.md +++ b/docs-site/src/content/docs/guides/codex-app-models.md @@ -137,10 +137,12 @@ ocx access test anthropic/claude-sonnet-5 --protocol responses ``` Both paths route correctly **once the request reaches the proxy** — that part is covered by -tests. What is not established is whether the app still sends the configured model while reserve -mode is active; if the client rewrites or refuses it before the request leaves, no proxy-side -setting changes that. Treat the explicit-selection route as worth trying rather than a confirmed -workaround. +tests. The Codex desktop app, however, does not send the configured model while reserve mode is +active: it decides reserve from its own `wham/usage` poll (`luna_reserve` upsell plus an allowed +`gpt-reserve` additional limit) and forces the model setting to `gpt-reserve` before the request +leaves, so the `config.toml` route is overridden in the app. Use `ocx access test`, Claude Code +through the proxy (`ocx claude`), or any direct `/v1` client until the window resets. See +[Routed models during Codex reserve mode](/guides/codex-integration/#routed-models-during-codex-reserve-mode). ## Why routed models show up diff --git a/docs-site/src/content/docs/guides/codex-integration.md b/docs-site/src/content/docs/guides/codex-integration.md index 3caec27f29..2de6f6c1c1 100644 --- a/docs-site/src/content/docs/guides/codex-integration.md +++ b/docs-site/src/content/docs/guides/codex-integration.md @@ -414,6 +414,36 @@ ocx service install # persistent: auto-starts on login and respawns on crash `ocx status` shows whether the proxy is running and prints the same restart hint when it is not; `ocx doctor` reports restart safety (service/shim coverage). +## Routed models during Codex reserve mode + +When the ChatGPT 5-hour quota is exhausted, Codex may offer a reserve fallback model +(`gpt-reserve` / Luna Reserve). While that state is active, the Codex model picker can make +**every other entry unselectable — including opencodex routed models**, even though those +run on independent providers and credentials and consume none of the exhausted quota. + +**This is a Codex client behavior and the proxy cannot change it.** The reserve state +arrives from the ChatGPT backend on the client's own authenticated connection, not through +the proxy. The desktop app polls `backend-api/wham/usage` and treats reserve as active when +the response carries `rate_limit_upsell.banner_type = "luna_reserve"`, the primary +`rate_limit.allowed` is `false`, and `additional_rate_limits[]` contains an entry with +`limit_name = "gpt-reserve"` that is still allowed. While that holds, the app forces the +conversation's model setting to `gpt-reserve` and rewrites any other pick back to it — the +picker is collapsed to the reserve entry by the client, and a `model =` value in +`config.toml` is overridden the same way. None of this consults the model catalog, so no +representation on our side participates in the decision. opencodex has no reserve concept to +adjust, and the alternative — misreporting your own quota back to your own client — would be +a worse bug than the one it papered over. + +**Workaround:** the models themselves stay fully usable; only the Codex app's model +selection is gated. Reach them from a client that does not consult the ChatGPT usage +snapshot: + +- Claude Code through the proxy (`ocx claude`). +- Any HTTP client against the local `/v1` endpoint. +- The dashboard's own request paths. + +Normal picker behavior returns when the 5-hour window resets. + ## The subagent picker Catalog sync makes the selected sub-agent models available to Codex; see [Codex App model picker](/guides/codex-app-models/#subagent-selection) for picker ordering and [Sub-agent Surface](/guides/sub-agent-surface/) for v1/base/v2 delegation and fallback behavior. diff --git a/docs-site/src/content/docs/ja/guides/codex-app-models.md b/docs-site/src/content/docs/ja/guides/codex-app-models.md index f654b183d0..93385300ec 100644 --- a/docs-site/src/content/docs/ja/guides/codex-app-models.md +++ b/docs-site/src/content/docs/ja/guides/codex-app-models.md @@ -136,7 +136,7 @@ model = "anthropic/claude-sonnet-5" ocx access test anthropic/claude-sonnet-5 --protocol responses ``` -どちらの経路も **リクエストがプロキシに届いた後は** 正しくルーティングされ、これはテストで確認済みです。確認できていないのは、リザーブモード中にアプリが設定したモデルを実際に送るかどうかです。クライアントが送信前に書き換えたり拒否したりする場合、プロキシ側の設定では変えられません。明示的な指定は確定した回避策ではなく、試す価値のある手段として扱ってください。 +どちらの経路も **リクエストがプロキシに届いた後は** 正しくルーティングされ、これはテストで確認済みです。ただし Codex デスクトップアプリは、リザーブモード中は設定したモデルを送りません。アプリは自身の `wham/usage` ポーリング(`luna_reserve` アップセルと許可状態の `gpt-reserve` 追加上限)でリザーブを判定し、リクエストが出る前にモデル設定を `gpt-reserve` に強制するため、`config.toml` 経路はアプリ内で上書きされます。ウィンドウがリセットされるまでは `ocx access test`、プロキシ経由の Claude Code(`ocx claude`)、または直接の `/v1` クライアントを使ってください。[Codex リザーブモード中のルーティングモデル](/guides/codex-integration/#routed-models-during-codex-reserve-mode) も参照してください。 ピッカーに古いエントリがまだ表示されている場合は、カタログを更新し、ターゲットの Codex サーフェスを再起動します。 diff --git a/docs-site/src/content/docs/ko/guides/codex-app-models.md b/docs-site/src/content/docs/ko/guides/codex-app-models.md index e114ab2b01..0642712082 100644 --- a/docs-site/src/content/docs/ko/guides/codex-app-models.md +++ b/docs-site/src/content/docs/ko/guides/codex-app-models.md @@ -222,7 +222,7 @@ model = "anthropic/claude-sonnet-5" ocx access test anthropic/claude-sonnet-5 --protocol responses ``` -두 경로 모두 **요청이 프록시에 도달한 뒤에는** 정상 라우팅되고, 이건 테스트로 덮여 있습니다. 확인되지 않은 부분은 리저브 모드에서 앱이 설정한 모델을 실제로 보내는지입니다. 클라이언트가 보내기 전에 바꾸거나 거부하면 프록시 설정으로는 바꿀 수 없습니다. 명시적 지정은 확정된 우회책이 아니라 시도해 볼 방법으로 보세요. +두 경로 모두 **요청이 프록시에 도달한 뒤에는** 정상 라우팅되고, 이건 테스트로 덮여 있습니다. 다만 Codex 데스크톱 앱은 리저브 모드에서 설정한 모델을 보내지 않습니다. 앱이 자체 `wham/usage` 폴링(`luna_reserve` 업셀과 허용 상태의 `gpt-reserve` 추가 한도)으로 리저브를 판정하고, 요청이 나가기 전에 모델 설정을 `gpt-reserve`로 강제하기 때문에 `config.toml` 경로는 앱 안에서 덮어써집니다. 윈도우가 리셋될 때까지는 `ocx access test`, 프록시를 통한 Claude Code(`ocx claude`), 직접 `/v1` 클라이언트를 쓰세요. [Codex 리저브 모드에서의 라우팅 모델](/guides/codex-integration/#routed-models-during-codex-reserve-mode)도 참고하세요. picker에 오래된 항목이 계속 보이면 카탈로그를 새로 쓰고 대상 Codex 서피스를 다시 시작합니다: diff --git a/docs-site/src/content/docs/ru/guides/codex-app-models.md b/docs-site/src/content/docs/ru/guides/codex-app-models.md index f7c7f6a2a0..e208ab4b0b 100644 --- a/docs-site/src/content/docs/ru/guides/codex-app-models.md +++ b/docs-site/src/content/docs/ru/guides/codex-app-models.md @@ -205,7 +205,7 @@ model = "anthropic/claude-sonnet-5" ocx access test anthropic/claude-sonnet-5 --protocol responses ``` -Оба пути маршрутизируются корректно **после того, как запрос дошёл до прокси**, и это покрыто тестами. Не установлено другое: отправляет ли приложение настроенную модель в резервном режиме. Если клиент переписывает или отклоняет её до отправки, никакая настройка на стороне прокси этого не изменит. Считайте явный выбор способом, который стоит попробовать, а не подтверждённым обходным путём. +Оба пути маршрутизируются корректно **после того, как запрос дошёл до прокси**, и это покрыто тестами. Однако настольное приложение Codex не отправляет настроенную модель в резервном режиме: оно определяет резерв по собственному опросу `wham/usage` (апселл `luna_reserve` плюс ещё разрешённый дополнительный лимит `gpt-reserve`) и принудительно выставляет модель `gpt-reserve` до отправки, поэтому путь через `config.toml` перезаписывается внутри приложения. До сброса окна используйте `ocx access test`, Claude Code через прокси (`ocx claude`) или любой прямой клиент `/v1`. См. [Маршрутизируемые модели в резервном режиме Codex](/guides/codex-integration/#routed-models-during-codex-reserve-mode). Если picker всё ещё показывает устаревшие записи, обновите каталог и перезапустите нужную diff --git a/docs-site/src/content/docs/tr/guides/codex-app-models.md b/docs-site/src/content/docs/tr/guides/codex-app-models.md index 93a1db6a62..2ddd5bd63a 100644 --- a/docs-site/src/content/docs/tr/guides/codex-app-models.md +++ b/docs-site/src/content/docs/tr/guides/codex-app-models.md @@ -301,7 +301,7 @@ ya da doğrudan gönderin: ocx access test anthropic/claude-sonnet-5 --protocol responses ``` -Her iki yol da **istek proxy'ye ulaştıktan sonra** doğru yönlendirilir; bu testlerle kapsanıyor. Kanıtlanmayan nokta, rezerv modu etkinken uygulamanın yapılandırılan modeli hâlâ gönderip göndermediğidir; istemci onu göndermeden önce değiştirir ya da reddederse proxy tarafındaki hiçbir ayar bunu değiştirmez. Açık seçimi doğrulanmış bir geçici çözüm değil, denemeye değer bir yol olarak görün. +Her iki yol da **istek proxy'ye ulaştıktan sonra** doğru yönlendirilir; bu testlerle kapsanıyor. Ancak Codex masaüstü uygulaması rezerv modu etkinken yapılandırılan modeli göndermez: rezerv durumunu kendi `wham/usage` sorgusundan (`luna_reserve` upsell'i ve hâlâ izinli bir `gpt-reserve` ek limiti) belirler ve istek çıkmadan önce model ayarını `gpt-reserve` olarak zorlar; bu yüzden `config.toml` yolu uygulama içinde ezilir. Pencere sıfırlanana kadar `ocx access test`, proxy üzerinden Claude Code (`ocx claude`) ya da doğrudan bir `/v1` istemcisi kullanın. Bkz. [Codex rezerv modunda yönlendirilmiş modeller](/guides/codex-integration/#routed-models-during-codex-reserve-mode). Seçici hala eski girdileri gösteriyorsa kataloğu yenileyin ve hedef Codex diff --git a/docs-site/src/content/docs/zh-cn/guides/codex-app-models.md b/docs-site/src/content/docs/zh-cn/guides/codex-app-models.md index 7709de7721..241b812252 100644 --- a/docs-site/src/content/docs/zh-cn/guides/codex-app-models.md +++ b/docs-site/src/content/docs/zh-cn/guides/codex-app-models.md @@ -149,7 +149,7 @@ model = "anthropic/claude-sonnet-5" ocx access test anthropic/claude-sonnet-5 --protocol responses ``` -**请求到达代理之后**,两条路径都能正确路由,这一点有测试覆盖。尚未确认的是:预备模式生效时,应用是否仍会发送已配置的模型。如果客户端在发出之前重写或拒绝它,代理端的任何设置都改变不了。请把显式选择当作值得一试的做法,而不是已确认的规避方案。 +**请求到达代理之后**,两条路径都能正确路由,这一点有测试覆盖。但预备模式生效时,Codex 桌面应用不会发送已配置的模型:它根据自己的 `wham/usage` 轮询(`luna_reserve` 升级提示加上仍被允许的 `gpt-reserve` 附加限额)判定预备状态,并在请求发出前把模型设置强制改为 `gpt-reserve`,所以 `config.toml` 这条路会在应用内被覆盖。在窗口重置之前,请使用 `ocx access test`、经代理的 Claude Code(`ocx claude`)或任意直连 `/v1` 的客户端。参见[Codex 预备模式下的路由模型](/guides/codex-integration/#routed-models-during-codex-reserve-mode)。 如果选择器里仍然显示旧条目,请刷新目录并重启目标 Codex 界面: diff --git a/docs-site/src/content/docs/zh-tw/guides/codex-app-models.md b/docs-site/src/content/docs/zh-tw/guides/codex-app-models.md index b2299af4d6..260ab7ad36 100644 --- a/docs-site/src/content/docs/zh-tw/guides/codex-app-models.md +++ b/docs-site/src/content/docs/zh-tw/guides/codex-app-models.md @@ -196,7 +196,7 @@ model = "anthropic/claude-sonnet-5" ocx access test anthropic/claude-sonnet-5 --protocol responses ``` -**請求抵達代理之後**,兩條路徑都能正確路由,這點有測試覆蓋。尚未確認的是:預備模式生效時,應用程式是否仍會送出已設定的模型。如果用戶端在送出前改寫或拒絕它,代理端的任何設定都改變不了。請把明確選擇當成值得一試的做法,而非已確認的規避方案。 +**請求抵達代理之後**,兩條路徑都能正確路由,這點有測試覆蓋。但預備模式生效時,Codex 桌面應用程式不會送出已設定的模型:它依自己的 `wham/usage` 輪詢(`luna_reserve` 升級提示加上仍被允許的 `gpt-reserve` 附加限額)判定預備狀態,並在請求送出前把模型設定強制改為 `gpt-reserve`,所以 `config.toml` 這條路會在應用程式內被覆寫。在視窗重設之前,請使用 `ocx access test`、經代理的 Claude Code(`ocx claude`)或任何直連 `/v1` 的用戶端。參見[Codex 預備模式下的路由模型](/guides/codex-integration/#routed-models-during-codex-reserve-mode)。 如果選擇器仍顯示舊條目,請重新整理目錄並重新開啟目標 Codex 介面: From 55400efd5478643355a79d9aee2c3adf21c31197 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 03:51:42 +0900 Subject: [PATCH 132/172] feat(anthropic): add claude-fable-5-1 with official Fable 5.1 pricing (#3203) Register claude-fable-5-1 (1M context) on both Anthropic surfaces and ship the official price overlay: $10 in / $50 out / $12.50 5m cache write / $0.25 cache hit (0.025x base input per the pricing page footnote). Verified on platform.claude.com 2026-09-02. Co-authored-by: jun --- src/providers/registry.ts | 6 ++++-- src/usage/expected-prices.ts | 9 ++++++++ tests/anthropic-hardening.test.ts | 1 + tests/provider-registry-parity.test.ts | 1 + tests/usage-cost.test.ts | 29 ++++++++++++++++++++++++-- 5 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/providers/registry.ts b/src/providers/registry.ts index f0de9bd00a..82279c6ebf 100644 --- a/src/providers/registry.ts +++ b/src/providers/registry.ts @@ -336,8 +336,10 @@ export type ProviderConfigSeed = Pick< // same static model seed. // 260710 context refresh: Tier-2 evidence in // devlog/_plan/260710_provider_hardening/001_research_frontier.md. -const ANTHROPIC_MODELS = ["claude-fable-5", "claude-sonnet-5", "claude-opus-5", "claude-opus-4-8", "claude-opus-4-7", "claude-opus-4-6", "claude-sonnet-4-6", "claude-haiku-4-5"]; -const ANTHROPIC_MODEL_CONTEXT_WINDOWS: Record = { "claude-sonnet-5": 1_000_000, "claude-fable-5": 1_000_000, "claude-opus-5": 1_000_000, "claude-opus-4-8": 1_000_000, "claude-opus-4-7": 1_000_000, "claude-opus-4-6": 1_000_000, "claude-sonnet-4-6": 1_000_000, "claude-haiku-4-5": 200_000 }; +// 260902 Claude Fable 5.1 (`claude-fable-5-1`): 1M context / 128K output / adaptive thinking +// always on, per the official models overview and pricing page (platform.claude.com). +const ANTHROPIC_MODELS = ["claude-fable-5-1", "claude-fable-5", "claude-sonnet-5", "claude-opus-5", "claude-opus-4-8", "claude-opus-4-7", "claude-opus-4-6", "claude-sonnet-4-6", "claude-haiku-4-5"]; +const ANTHROPIC_MODEL_CONTEXT_WINDOWS: Record = { "claude-fable-5-1": 1_000_000, "claude-sonnet-5": 1_000_000, "claude-fable-5": 1_000_000, "claude-opus-5": 1_000_000, "claude-opus-4-8": 1_000_000, "claude-opus-4-7": 1_000_000, "claude-opus-4-6": 1_000_000, "claude-sonnet-4-6": 1_000_000, "claude-haiku-4-5": 200_000 }; // 260814 GLM-5.3 is registered pre-emptively alongside 5.2 everywhere 5.2 appears. Z.AI's // devpack "How to Switch Models" page (docs.z.ai/devpack/latest-model) lists glm-5.3 and diff --git a/src/usage/expected-prices.ts b/src/usage/expected-prices.ts index dfc4b4711d..d5ab5b4830 100644 --- a/src/usage/expected-prices.ts +++ b/src/usage/expected-prices.ts @@ -66,6 +66,10 @@ const QWEN38_MAX: Cost4 = { input: 2, output: 6, cacheRead: 0, cacheWrite: 0 }; // Anthropic official list prices (USD / 1M tokens). Cache write uses the published 5-minute rate. const CLAUDE_SONNET_46: Cost4 = { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 }; const CLAUDE_OPUS_46: Cost4 = { input: 5, output: 25, cacheRead: 0.5, cacheWrite: 6.25 }; +// Claude Fable 5.1: 10 / 50, 5m cache write 12.50. Cache hits are 0.025x base input +// (0.25) on Fable 5.1 — NOT the 0.1x (1.00) that Fable 5 and every other family use; +// the pricing page footnote calls this out explicitly. Verified 2026-09-02. +const CLAUDE_FABLE_51: Cost4 = { input: 10, output: 50, cacheRead: 0.25, cacheWrite: 12.5 }; // Opus 5 is priced from the maintainer's confirmation that it matches the previous // Opus, not from a published Opus 5 page. Hence `verified-derived`, and a source // string that states the provenance instead of pointing at ANTHROPIC_PRICING. @@ -91,6 +95,11 @@ const KIMI_PRICING = "https://platform.kimi.ai/docs/pricing (official table; cac const QWEN38_MAX_PRICING = "https://qwen.ai/blog?id=qwen3.8 (Qwen release announcement; no Model Studio billing row yet; cache rates unpublished -> 0)"; export const EXPECTED_PRICE_OVERLAYS: readonly ExpectedPriceOverlay[] = [ + // claude-fable-5-1 has no jawcode row yet, so both Anthropic surfaces need their own + // overlay (the overlay lookup is keyed by the configured provider id; only the jawcode + // bundle collapses anthropic-apikey onto anthropic). + { provider: "anthropic", modelId: "claude-fable-5-1", cost4: CLAUDE_FABLE_51, source: `anthropic official Claude Fable 5.1 ${ANTHROPIC_PRICING}; cache hit = 0.025x base input`, verifiedAt: "2026-09-02", status: "verified" }, + { provider: "anthropic-apikey", modelId: "claude-fable-5-1", cost4: CLAUDE_FABLE_51, source: `anthropic official Claude Fable 5.1 ${ANTHROPIC_PRICING}; cache hit = 0.025x base input`, verifiedAt: "2026-09-02", status: "verified" }, // claude-opus-5 is exposed by three providers but absent from the jawcode bundle, so // cost resolution returned null and the Logs `~$` column rendered an em dash. The // model-level vendor fallback only searches jawcode metadata, never overlays, so one diff --git a/tests/anthropic-hardening.test.ts b/tests/anthropic-hardening.test.ts index 6b01c4e972..95cc575322 100644 --- a/tests/anthropic-hardening.test.ts +++ b/tests/anthropic-hardening.test.ts @@ -125,6 +125,7 @@ describe("anthropic provider hardening", () => { expect(anthropic?.modelContextWindows?.["claude-opus-4-8"]).toBe(1_000_000); expect(anthropic?.modelContextWindows?.["claude-opus-5"]).toBe(1_000_000); + expect(anthropic?.modelContextWindows?.["claude-fable-5-1"]).toBe(1_000_000); expect(anthropic?.modelContextWindows?.["claude-haiku-4-5"]).toBe(200_000); }); }); diff --git a/tests/provider-registry-parity.test.ts b/tests/provider-registry-parity.test.ts index 67a6139dc6..5ea6539dd6 100644 --- a/tests/provider-registry-parity.test.ts +++ b/tests/provider-registry-parity.test.ts @@ -737,6 +737,7 @@ describe("provider registry parity", () => { expect(OAUTH_PROVIDERS.anthropic.providerConfig.defaultModel).toBe("claude-sonnet-5"); expect(OAUTH_PROVIDERS.anthropic.providerConfig.models).toContain("claude-sonnet-5"); expect(OAUTH_PROVIDERS.anthropic.providerConfig.models).toContain("claude-fable-5"); + expect(OAUTH_PROVIDERS.anthropic.providerConfig.models).toContain("claude-fable-5-1"); expect(OAUTH_PROVIDERS.anthropic.providerConfig.modelContextWindows?.["claude-sonnet-5"]).toBe(1_000_000); expect(OAUTH_PROVIDERS.anthropic.providerConfig.modelContextWindows?.["claude-opus-4-7"]).toBe(1_000_000); expect(OAUTH_PROVIDERS.anthropic.providerConfig.modelContextWindows?.["claude-opus-4-6"]).toBe(1_000_000); diff --git a/tests/usage-cost.test.ts b/tests/usage-cost.test.ts index d97e80ab07..bf15ab8470 100644 --- a/tests/usage-cost.test.ts +++ b/tests/usage-cost.test.ts @@ -176,6 +176,29 @@ describe("resolveMatchedPrice", () => { expect(price!.cost4).toEqual({ input: 5, output: 25, cacheRead: 0.5, cacheWrite: 6.25 }); }); + // Claude Fable 5.1 (2026-09-02): 10 / 50 / 12.50 cache write, and a cache-hit rate of + // 0.025x base input (0.25) rather than the 0.1x every other family uses. There is no + // jawcode row yet, so both Anthropic surfaces resolve from the shipped overlay; an + // account-pool log label must collapse onto the same price. + test("claude-fable-5-1 resolves to the official Fable 5.1 price on both Anthropic surfaces", () => { + const COST4 = { input: 10, output: 50, cacheRead: 0.25, cacheWrite: 12.5 }; + for (const provider of ["anthropic", "anthropic-apikey"]) { + const price = resolveMatchedPrice(provider, "claude-fable-5-1"); + expect(price, provider).toMatchObject({ + provider, + modelId: "claude-fable-5-1", + cost4: COST4, + source: "expected", + status: "verified", + }); + expect(price?.sourceRef).toContain("platform.claude.com"); + expect(price?.sourceRef).toContain("0.025x"); + } + expect(resolveMatchedPrice("anthropic-pb51d9b", "claude-fable-5-1")?.cost4).toEqual(COST4); + // The cheaper cache-hit rate must not leak onto Fable 5, which stays at 0.1x. + expect(resolveMatchedPrice("anthropic", "claude-fable-5")?.cost4.cacheRead).toBe(1); + }); + test("17b. model-level fallback: openai provider gets gpt prices from the openai bundle", () => { const price = resolveMatchedPrice("openai", "gpt-5.5"); expect(price).not.toBeNull(); @@ -269,11 +292,13 @@ describe("resolveMatchedPrice", () => { expect(resolveMatchedPrice("openrouter", "anthropic-claude-3.5-sonnet")).toBeNull(); }); - test("16. shipped overlay membership: 56 keys, including Opus 5 and compatibility prices", () => { - expect(EXPECTED_PRICE_OVERLAYS.length).toBe(56); + test("16. shipped overlay membership: 58 keys, including Fable 5.1, Opus 5 and compatibility prices", () => { + expect(EXPECTED_PRICE_OVERLAYS.length).toBe(58); expect(EXPECTED_PRICE_OVERLAYS.some(row => row.status === "unverified")).toBe(false); const keys = new Set(EXPECTED_PRICE_OVERLAYS.map(row => `${row.provider}/${row.modelId}`)); for (const expected of [ + "anthropic/claude-fable-5-1", + "anthropic-apikey/claude-fable-5-1", "anthropic/claude-opus-5", "cursor/claude-opus-5", "kiro/claude-opus-5", From fcf0da2570225b8860918d0fc416307dfef413c0 Mon Sep 17 00:00:00 2001 From: Olddonkey Date: Tue, 1 Sep 2026 11:53:12 -0700 Subject: [PATCH 133/172] test(quota): move the malformed-plan pool test onto the #3198 contract (#3200) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #3198 deliberately stopped excluding uncalibrated Codex plans from the pool capacity estimate: a plan name the weight map does not list now counts at the baseline seat weight, because exclusion silently overstated the coverage an operator was reading. It updated provider-capacity.test.ts and the GUI tests, but tests/provider-quota.test.ts kept one test asserting the old exclusion shape, so it has failed deterministically on every clean dev checkout since — expecting the malformed-plan account to be dropped (weekly 11, excluded 1, incomplete) where the new contract blends it at baseline (weekly 44, both included, complete coverage). A malformed non-string plan such as `{ tier: "pro" }` is not a separate axis: codexPlanKey normalizes it to undefined exactly like an unlisted name (the tolerance 095a9a5b7 established), and #3198 routes every undefined key to the baseline weight. Re-splitting malformed from unknown here would reintroduce the distinction #3198 just collapsed, so the test moves to the new contract instead; `unknownPlanAccounts` still records the seat, which is the observable the test's cache half already pins. Co-authored-by: Claude Fable 5 --- tests/provider-quota.test.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/provider-quota.test.ts b/tests/provider-quota.test.ts index ad3a6edbdd..4b79062f45 100644 --- a/tests/provider-quota.test.ts +++ b/tests/provider-quota.test.ts @@ -1858,6 +1858,12 @@ describe("fetchProviderQuotaReports", () => { expect(JSON.stringify(openai?.aggregation)).not.toMatch(/(?:total|consumed|remaining)Weight|projectedUsedPercent/i); }); + // #3198 changed what "tolerate" means here: an uncalibrated plan — a name the weight map + // does not list, or a malformed non-string value like the `{ tier: "pro" }` below (both + // normalize to undefined via codexPlanKey) — is now counted at the baseline seat weight + // instead of being excluded from the aggregate. Exclusion silently overstated coverage; + // baseline counting is the visibly conservative estimate. The account still shows up in + // `unknownPlanAccounts` so the operator can see the estimate is conservative for that seat. test("pool reports tolerate a malformed persisted plan through cache and aggregation", async () => { saveCodexAccountCredential("added", { accessToken: "added-access", @@ -1886,12 +1892,14 @@ describe("fetchProviderQuotaReports", () => { const refreshed = await fetchProviderQuotaReports(config, true); const openai = refreshed.reports.find(row => row.provider === "openai"); - expect(openai?.quota.weeklyPercent).toBe(11); + // Both seats weigh the same (malformed -> baseline, "plus" -> calibrated baseline), so the + // blend of 77 and 11 lands at 44 — not the 11 the old exclusion contract produced. + expect(openai?.quota.weeklyPercent).toBe(44); expect(openai?.aggregation).toMatchObject({ - includedAccounts: 1, - excludedAccounts: 1, + includedAccounts: 2, + excludedAccounts: 0, unknownPlanAccounts: 1, - incomplete: true, + incomplete: false, currentAccount: { quota: { weeklyPercent: 77 } }, }); expect(openai?.aggregation?.currentAccount).not.toHaveProperty("plan"); From 53c09a247798c08c064a2b4f6c05690b8e6b4be5 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 04:13:21 +0900 Subject: [PATCH 134/172] fix(server): allow POST /v1/alpha/search on the loopback listener (#3205) The unauthenticated loopback listener admits routes through an allowlist in loopbackRouteAllowed(). /v1/alpha/search - the native Codex web-search relay - was never on it, so a directly-spawned codex app-server got 404 for every web search (#3192). Admit POST on that path. The handler runs its own admission (resolveApiAuth + validateForwardAdmissionCredential), so a loopback caller without a ChatGPT credential is still refused inside the relay; only the listener's 404 goes away. The public listener is unchanged. Clean reimplementation of #3193, whose branch was byte-corrupted by an encoding round-trip (em-dashes and emoji in ~30 unrelated comment lines). Supersedes #3193. Fixes #3192. Co-authored-by: jun Co-authored-by: alan7629 --- .../docs/fr/reference/configuration/server.md | 3 +- .../docs/reference/configuration/server.md | 4 +- .../docs/tr/reference/configuration/server.md | 5 ++- .../zh-tw/reference/configuration/server.md | 5 ++- src/server/index.ts | 10 ++++- tests/loopback-listener-integration.test.ts | 39 ++++++++++++++++++- 6 files changed, 56 insertions(+), 10 deletions(-) diff --git a/docs-site/src/content/docs/fr/reference/configuration/server.md b/docs-site/src/content/docs/fr/reference/configuration/server.md index bad04dd6de..207f8a4c08 100644 --- a/docs-site/src/content/docs/fr/reference/configuration/server.md +++ b/docs-site/src/content/docs/fr/reference/configuration/server.md @@ -105,7 +105,8 @@ Le port est obligatoire et doit différer du port proxy. Il n'est jamais attribu changerait au fil des redémarrages tandis que les serveurs d'applications déjà en cours d'exécution conservaient le `base_url` précédent. L'écouteur ne sert que `POST /v1/responses`, sa mise à niveau WebSocket, `POST /v1/responses/compact`, -et `GET /v1/models`. Tout le reste, y compris `/api/*` et le tableau de bord, renvoie `404`. +`POST /v1/alpha/search` (le relais de recherche web natif de Codex), `GET /v1/models` et les mises à +niveau WebSocket vocales autonomes. Tout le reste, y compris `/api/*` et le tableau de bord, renvoie `404`. :::danger[Surface non authentifiée] Chaque processus de la machine peut utiliser cet écouteur. Il consomme le quota du compte et utilise les identifiants de diff --git a/docs-site/src/content/docs/reference/configuration/server.md b/docs-site/src/content/docs/reference/configuration/server.md index 6976504b42..7bc15c1191 100644 --- a/docs-site/src/content/docs/reference/configuration/server.md +++ b/docs-site/src/content/docs/reference/configuration/server.md @@ -113,7 +113,9 @@ The port is required and must differ from the proxy port. It is never OS-assigne would change across restarts while already-running app-servers kept the previous `base_url`. The listener serves only `POST /v1/responses`, its WebSocket upgrade, `POST /v1/responses/compact`, -and `GET /v1/models`. Everything else, including `/api/*` and the dashboard, returns `404`. +`POST /v1/alpha/search` (the native Codex web-search relay), `GET /v1/models`, and the standalone +realtime voice WebSocket upgrades. Everything else, including `/api/*` and the dashboard, returns +`404`. :::danger[This is an unauthenticated surface] Every process on the machine can use this listener. It spends account quota and paid provider diff --git a/docs-site/src/content/docs/tr/reference/configuration/server.md b/docs-site/src/content/docs/tr/reference/configuration/server.md index edbd593eee..47c6147904 100644 --- a/docs-site/src/content/docs/tr/reference/configuration/server.md +++ b/docs-site/src/content/docs/tr/reference/configuration/server.md @@ -111,8 +111,9 @@ tarafından atanmaz: geçici bir port yeniden başlatmalar arasında değişirke zaten çalışan app-server'lar önceki `base_url`'i tutardı. Dinleyici yalnızca `POST /v1/responses`, onun WebSocket yükseltmesi, `POST -/v1/responses/compact` ve `GET /v1/models` sunar. `/api/*` ve kontrol paneli -dahil diğer her şey `404` döndürür. +/v1/responses/compact`, `POST /v1/alpha/search` (yerel Codex web arama aktarımı), +`GET /v1/models` ve bağımsız sesli WebSocket yükseltmelerini sunar. `/api/*` ve +kontrol paneli dahil diğer her şey `404` döndürür. :::danger[Bu kimliği doğrulanmamış bir yüzeydir] Makinedeki her süreç bu dinleyiciyi kullanabilir. Hesap kotasını ve ücretli diff --git a/docs-site/src/content/docs/zh-tw/reference/configuration/server.md b/docs-site/src/content/docs/zh-tw/reference/configuration/server.md index 1d80f5911c..e85594740d 100644 --- a/docs-site/src/content/docs/zh-tw/reference/configuration/server.md +++ b/docs-site/src/content/docs/zh-tw/reference/configuration/server.md @@ -89,8 +89,9 @@ stream 開啟前以 `401` 失敗。 該 port 是必填的,且必須與 proxy port 不同。它絕不會由 OS 指派:臨時 port 會在重啟時改變,而 已執行的 app-server 仍保留先前的 `base_url`。 -該 listener 只服務 `POST /v1/responses`、其 WebSocket upgrade、`POST /v1/responses/compact` 與 -`GET /v1/models`。其他一切,包括 `/api/*` 與儀表板,都會回傳 `404`。 +該 listener 只服務 `POST /v1/responses`、其 WebSocket upgrade、`POST /v1/responses/compact`、 +`POST /v1/alpha/search`(Codex 原生網頁搜尋中繼)、`GET /v1/models`,以及獨立語音 WebSocket upgrade。 +其他一切,包括 `/api/*` 與儀表板,都會回傳 `404`。 :::danger[這是一個未認證的介面] 機器上的每個 process 都可以使用此 listener。它會耗用帳號配額與付費 provider 憑證,也可能耗盡 diff --git a/src/server/index.ts b/src/server/index.ts index 0fe675463c..1c4f72bb09 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -780,8 +780,13 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server { // 5s default on a loaded Windows box, where the test measured 5.04s. }, SERVER_BUDGET_MS); - test("serves only the four allowlisted routes, using each route's real method", async () => { + test("serves only the allowlisted routes, using each route's real method", async () => { const loopbackPort = await freePort(); saveConfig(baseConfig(loopbackPort)); const server = startServer(0); @@ -285,13 +285,13 @@ describe("unauthenticated loopback listener", () => { { method: "POST", path: "/v1/chat/completions", body: '{"model":"x","messages":[]}' }, { method: "POST", path: "/v1/messages", body: '{"model":"x","messages":[]}' }, { method: "POST", path: "/v1/images/generations", body: '{"prompt":"x"}' }, - { method: "POST", path: "/v1/alpha/search", body: '{"query":"x"}' }, { method: "GET", path: "/v1/opencodex/artifacts/x" }, { method: "POST", path: "/v1/live", body: "{}" }, { method: "POST", path: "/v1/realtime/calls", body: "{}" }, // Allowlisted paths still reject the methods they do not serve. { method: "DELETE", path: "/v1/responses" }, { method: "POST", path: "/v1/models" }, + { method: "GET", path: "/v1/alpha/search" }, ]; for (const { method, path, body } of denied) { const res = await fetch(`${base}${path}`, { @@ -309,6 +309,41 @@ describe("unauthenticated loopback listener", () => { } }); + test("admits POST /v1/alpha/search so native web search reaches the relay (#3192)", async () => { + const loopbackPort = await freePort(); + saveConfig(baseConfig(loopbackPort)); + const server = startServer(0); + const body = '{"query":"x"}'; + const headers = { "content-type": "application/json" }; + try { + // Codex sends its native web-search call to the same base URL as /v1/responses. Before + // the allowlist admitted it, the direct-spawn host answered 404 for every search. What + // proves the gate is open is that the answer comes from BEHIND it: the admitted-turn + // path (503 while native-main maintenance holds, otherwise the relay's own 401 for a + // missing ChatGPT credential), never the listener's 404 and never the public + // listener's "opencodex API key required". + const viaLoopback = await fetch(`http://127.0.0.1:${loopbackPort}/v1/alpha/search`, { + method: "POST", body, headers, + }); + const loopbackBody = await viaLoopback.json() as { error?: { message?: string } }; + expect(viaLoopback.status).not.toBe(404); + expect([401, 503]).toContain(viaLoopback.status); + expect(loopbackBody.error?.message).toBeDefined(); + expect(loopbackBody.error?.message).not.toBe("opencodex API key required"); + + // The public listener is unchanged: the same request without a key is still refused + // at admission, so widening the loopback allowlist did not widen the public surface. + const viaPublic = await fetch(`http://127.0.0.1:${server.port}/v1/alpha/search`, { + method: "POST", body, headers, + }); + expect(viaPublic.status).toBe(401); + const publicBody = await viaPublic.json() as { error?: { message?: string } }; + expect(publicBody.error?.message).toBe("opencodex API key required"); + } finally { + await server.stop(true); + } + }); + test("admits standalone realtime voice WebSocket upgrades, HTTP stays rejected", async () => { const loopbackPort = await freePort(); saveConfig(baseConfig(loopbackPort)); From 6a6efa928a165726c0fd17d893d11c21176dc812 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 04:17:07 +0900 Subject: [PATCH 135/172] feat(catalog): per-provider retainModels opt-in (#1690, carries #2860) (#3206) * feat(catalog): honor per-provider retainModels opt-in (closes #1690) Operators can pin a model id so live discovery keeps it even when the upstream catalog omits it. The new opt-in is purely additive: built-in kimi/xai tables and the Vertex default continue to win, unknown ids are never inflated, and the existing one-line diagnostic still flags ids the live catalog dropped. Co-authored-by: CommandCodeBot * feat(catalog): make retainModels self-sufficient and configurable (#1690) Builds on the carried #2860 predicate. retainModels ids now enter the configured seed (ordered union with the Vertex default and models), so a retain-only id survives live discovery, liveModels:false, and gets the same provider hints; selectedModels precedence is unchanged. Adds zod schema and superRefine validation, management PATCH/DTO/safe-config plumbing, ocx provider edit --retain-models , rename-migration coverage, discovery-flight fingerprint, and docs. Design input: #2122 (union, schema, MODEL_ID_LISTS). --------- Co-authored-by: rrmlima Co-authored-by: CommandCodeBot Co-authored-by: jun --- .../040_wp4_retain_models.md | 111 +++++++++++ .../041_wp4_audit_r1_synthesis.md | 30 +++ .../docs/reference/configuration/providers.md | 9 + src/cli/provider-runtime.ts | 6 + src/codex/catalog/provider-fetch.ts | 19 +- src/config.ts | 14 ++ src/providers/model-rename-migration.ts | 3 + src/server/auth-cors.ts | 3 + src/server/management/provider-routes.ts | 14 ++ src/types/provider.ts | 10 + tests/catalog-retain-models.test.ts | 186 ++++++++++++++++++ tests/cli-headless-parity.test.ts | 18 ++ tests/management-provider-validation.test.ts | 82 ++++++++ tests/model-rename-migration.test.ts | 3 + 14 files changed, 505 insertions(+), 3 deletions(-) create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/040_wp4_retain_models.md create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/041_wp4_audit_r1_synthesis.md create mode 100644 tests/catalog-retain-models.test.ts diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/040_wp4_retain_models.md b/devlog/_plan/260902_nonbug_adoption_backlog/040_wp4_retain_models.md new file mode 100644 index 0000000000..23983de803 --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/040_wp4_retain_models.md @@ -0,0 +1,111 @@ +# wp4 — #1690 retainModels allowlist (two rival PRs) + +Issue #1690 (score 58, labels enhancement/catalog). Two open drafts implement it: + +| | PR #2860 (rrmlima) | PR #2122 (chilung-cgu) | +| --- | --- | --- | +| size | +132/-2, 3 files | +726/-25, 15 files | +| CI at head | fully green (test 1-4, macos, ci) | only hygiene/label/target ran | +| base | e546c160b, 310 behind dev; cherry-picks cleanly onto `fcf0da257` | same base | +| retention | `shouldRetainConfiguredProviderModel(name, id, prov)` + `modelInList` | new `providerRetainModels` set inside the merge loop | +| ids must also be in `models`? | yes (purely retentive) | no (`retainModels` folded into `configuredIds`) | +| config validation | none (relies on `.passthrough()`) | zod `retainModels` + `nonBlankStringArrayConfigError` | +| management API / DTO | none | none (neither exposes it via PATCH) | +| rename migration | none | adds `retainModels` to `MODEL_ID_LISTS` | +| 404 diagnostic | none | new module state + `warnRetainedModel404Once` wired into 4 request handlers, `CatalogModel.retainedWithoutDiscovery` | +| docs | none | 5 locales, one table row each | + +## Decision + +Adopt **#2860 as the base commit** (cherry-picked as `2be9d505d` on +`codex/retain-models-1690`), then add the pieces that make the opt-in +discoverable and safe to hand-edit. #2122 is closed as superseded with credit for +the config/migration design. + +Why #2860 over #2122: the retention decision belongs in the one predicate the +merge loop already consults; #2122 adds a second set beside it. #2122's 404 +diagnostic requires module-level maps keyed by provider, a new `CatalogModel` +field, and edits to `core.ts`, `chat-completions.ts`, `chat-native.ts`, +`claude-messages.ts` — four hot request paths — to print one warning that the +upstream error body already carries (`model_not_found`). That is the wrong +trade for this cycle; the existing `warnDroppedConfiguredIdsOnce` stays as the +diagnostic for ids that are *not* retained. + +## What this cycle adds on top of #2860 + +1. **`retainModels` alone is enough.** `configuredIds` in + `fetchProviderModelsWithAuth` becomes the ordered union of the Vertex seed, + `prov.models`, and `prov.retainModels`. An operator who writes + `"retainModels": ["gemini-3.7-flash"]` should not have to repeat the id in + `models`; requiring both is the footgun #2122 correctly avoided. #2860's + "does not invent ids" test flips to assert the union. +2. **Schema + load normalization** (`src/config.ts`): `retainModels: + z.array(z.string().min(1)).transform(normalizeNonBlankStringArray).optional()` + next to `noStructuredOutputModels`, plus the same `superRefine` entry so a + hand-edited `"retainModels": "x"` fails with a path instead of being silently + passed through. +3. **Management PATCH + DTO** (`src/server/management/provider-routes.ts`): + `retainModels` accepted like `noStructuredOutputModels` (`null` clears, + empty array clears, validated with `nonBlankStringArrayConfigError`), and + returned in the safe provider DTO so the dashboard/API round-trips it. +4. **CLI opt-in** (`src/cli/provider-runtime.ts`): + `ocx provider edit --retain-models `. This is the easy + switch: one flag, no JSON editing, `-` clears. Usage string and + `skills/ocx` surface are regenerated if the capability registry changes + (it does not — `provider edit` already exists; only the flag list grows). +5. **Rename migration** (`src/providers/model-rename-migration.ts`): + `"retainModels"` added to `MODEL_ID_LISTS` so a retired id is renamed + rather than resurrected as a ghost row. +6. **Docs** (`docs-site/.../reference/configuration/providers.md`): one table + row after `selectedModels`, and a short paragraph in "Static model + allowlists" contrasting `selectedModels` (narrows) with `retainModels` + (preserves). English only this cycle; locales already lag on + `noStructuredOutputModels` and a missing row does not contradict. + +## Explicitly not in this cycle + +- Seeding `CALLABLE_CONFIGURED_COMPATIBILITY_MODELS` with antigravity + `gemini-3.7-flash` (issue step 4). That is a product default, and #1683 is a + separate issue; with the config key available it no longer needs a release. +- GUI field. The dashboard provider editor is untouched; the PATCH contract is + ready for it, and a later PR can add the input with its screenshot. +- 404-time warning. See "Decision". + +## Acceptance criteria + +- `retainModels` absent/empty → catalog identical to today (existing + `tests/codex-catalog.test.ts` retention tests untouched and green). +- `retainModels: ["x"]`, live omits `x`, `x` not in `models` → `x` present + with provider hints applied; `droppedConfiguredIds` excludes it. +- `retainModels: ["x"]`, live returns `x` → single row, no duplicate. +- `liveModels: false` → `retainModels` ids are part of the static list. +- Config load rejects `retainModels: "x"` / `[""]` with a + `providers..retainModels` path; trims and dedupes valid input. +- Management PATCH sets/clears; DTO echoes; CLI flag round-trips through PATCH. +- A test through `fetchProviderModels` (not only `mergeConfiguredModelsIntoLiveCatalog`) proves a retain-only id survives both live discovery and `liveModels: false` (audit r1 blocker 2). +- CLI treats `-` before `csv` so `--retain-models -` clears (audit r1 blocker 3). +- `providerCatalogFingerprint` includes `retainModels`. +- Migration renames a retired id inside `retainModels`. +- `bun x tsc --noEmit` clean, `bun run privacy:scan` clean, focused files: + `tests/catalog-retain-models.test.ts`, `tests/codex-catalog.test.ts`, + `tests/management-provider-validation.test.ts`, + `tests/model-rename-migration.test.ts` (if present), provider-runtime CLI test. + +## Files + +- `src/codex/catalog/provider-fetch.ts` — configuredIds union (on top of #2860). +- `src/config.ts` — schema + superRefine. +- `src/server/management/provider-routes.ts` — PATCH + DTO. +- `src/server/auth-cors.ts` — `providerManagementConfigError` validation + safe-config DTO key list (audit r1 blocker 1). +- `src/cli/provider-runtime.ts` — `--retain-models`. +- `src/providers/model-rename-migration.ts` — list entry. +- `src/types/provider.ts` — already added by #2860 (doc comment adjusted for union). +- `docs-site/src/content/docs/reference/configuration/providers.md`. +- `tests/catalog-retain-models.test.ts` (extend), `tests/management-provider-validation.test.ts` (extend). + +## Closure + +PR targets `dev`, `Closes #1690`, description names #2860 as the carried +source commit (`12e69c200`) and #2122 as design input. After landing: close +#1690 with the landing SHA, close #2860 as landed-via-carry (author credited in +the squash trailer), close #2122 as superseded with the reasoning above. diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/041_wp4_audit_r1_synthesis.md b/devlog/_plan/260902_nonbug_adoption_backlog/041_wp4_audit_r1_synthesis.md new file mode 100644 index 0000000000..c48bfd145b --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/041_wp4_audit_r1_synthesis.md @@ -0,0 +1,30 @@ +# wp4 audit r1 — synthesis + +Reviewer: grok-4.6 subagent (Feynman), read-only, 7 questions on 040. Verdict: **near-pass / GO-WITH-FIXES** (3 blockers, 3 suggestions). + +## Answers that confirm the plan + +- Union (Q1): safe only if written as an ordered dedupe set `[vertexDefault?, ...models, ...retainModels]`, replacing the current `seed ? [default] : models` ternary. `configured` is the single seed for static `liveModels:false`, the Cursor filter, the degraded fallback, `droppedConfiguredIds`, and hints, so a retain-only id gets the same context/effort maps as a `models[]` entry. The Vertex seed predicate (`models.length === 0`) stays untouched. +- Family match (Q2): acceptable, same semantics as `noVisionModels`. `retainModels: ["gpt-oss"]` keeps `gpt-oss:120b` and also invents a bare `gpt-oss` row through the union — extra row, never a drop. +- selectedModels precedence (Q3): `filterCatalogVisibleModels` and `sync.ts` still hide a retained id when `selectedModels` is non-empty and omits it. Keep that; document "retain ≠ visible". +- PATCH (Q4): copy `provider-routes.ts:386` verbatim; also GET list (:540). +- CLI (Q5): no `takeListOption`; use `csv(takeOption)` and special-case `"-"` **before** `csv` (`csv("-")` yields `["-"]`). +- #2122 (Q6): union, schema, `MODEL_ID_LISTS` are the necessary parts; the 404 module maps and `retainedWithoutDiscovery` are not. The `withConfiguredRetention(live→forCache)` change is incidental — the double merge is the OCX-111 combo-cache contract. Do not copy. +- No-regression (Q7): absent/empty is a no-op; kimi/xai tables unchanged. + +## Blockers folded into 040 + +1. `src/server/auth-cors.ts` was missing from the file list: `providerManagementConfigError` (~693) must validate `retainModels` with `nonBlankStringArrayConfigError`, and the safe-config DTO key list (~794) must include it, otherwise PATCH validation and DTO echo silently miss. +2. The "liveModels:false / retain-only present" criterion cannot be proven through `mergeConfiguredModelsIntoLiveCatalog` alone. Add a test that goes through `fetchProviderModels`/the gather path so the union at :1307 is actually exercised. +3. CLI: handle `-` before `csv`. + +## Suggestions taken + +- Docs state that `selectedModels` still narrows what is visible even for retained ids. +- `retainModels` added to `providerCatalogFingerprint` (:573) so two providers differing only in that list do not share a discovery flight. +- Flip #2860's "does not invent ids" test to assert the union. + +## Disposition + +All three blockers are additive edits inside the already-planned files plus one file (`auth-cors.ts`). No scope change. Proceed to B. + diff --git a/docs-site/src/content/docs/reference/configuration/providers.md b/docs-site/src/content/docs/reference/configuration/providers.md index 0cfd77f71e..d6b37c8086 100644 --- a/docs-site/src/content/docs/reference/configuration/providers.md +++ b/docs-site/src/content/docs/reference/configuration/providers.md @@ -83,6 +83,7 @@ differing backup and rewrites known legacy namespaced selected ids to bare ids. | `models?` | `string[]` | Seed/fallback model list. With `liveModels: false`, these are the only discovered models. | | `liveModels?` | `boolean` | Fetch the live catalog on start/sync (default `true`). Custom providers use `${baseUrl}/models`; built-ins may use a registry URL and filter. | | `selectedModels?` | `string[]` | Catalog allowlist after discovery. Non-empty exposes only those ids; empty or omitted exposes all discovered models. | +| `retainModels?` | `string[]` | Ids kept in the catalog even when live discovery omits them. They need not be repeated in `models`. Empty or omitted keeps today's behavior. | | `contextWindow?` | `number` | Provider-wide context fallback when upstream metadata is absent; otherwise a cap that retains smaller live metadata. The Models dashboard exposes this separately from `providerContextCaps`. | | `modelContextWindows?` | `Record` | Per-model context fallbacks/caps. These override `contextWindow`: an unknown window uses the configured value, while smaller live metadata remains authoritative. | | `modelInputModalities?` | `Record` | Per-model input hints such as `["text"]` or `["text", "image"]`. | @@ -575,6 +576,14 @@ silently replaced or truncated. Use `selectedModels` when discovery should still run but only selected ids should appear in Codex and `/v1/models`. The dashboard retains the full discovered list for later allowlist changes. +Use `retainModels` for the opposite problem: a provider whose `/models` endpoint omits an id that is +still callable (a private deployment, a preview id, an OpenAI-compatible gateway with a partial +listing). Listed ids are kept in the routed catalog with the same context and effort hints as +`models`, and they survive `liveModels: false` too. `selectedModels` still narrows what is visible, +so an id must be in both lists when an allowlist is active. Retaining an id does not make the +upstream accept it; a wrong id fails at request time with the upstream error. From the CLI: +`ocx provider edit --retain-models gemini-3.7-flash,other-id` (`-` clears). + Preview GPT-5.6 fallback entries use the same mechanism. The OpenAI API-key preset seeds base and Pro ids with context `922000` and max input `922000`; OpenRouter seeds `openai/gpt-5.6-sol`, `openai/gpt-5.6-terra`, and `openai/gpt-5.6-luna` with context `922000`. Pool/Direct advertises diff --git a/src/cli/provider-runtime.ts b/src/cli/provider-runtime.ts index 183d2e32a7..ebeac3d4ae 100644 --- a/src/cli/provider-runtime.ts +++ b/src/cli/provider-runtime.ts @@ -24,6 +24,7 @@ const USAGE = `Usage: [--auth-mode ] [--note ] [--api-key-transport ] [--headers ] [--enabled ] [--live-models ] + [--retain-models ] [--allow-private-network ] [--json] ocx provider test [--json] ocx provider quota [--refresh] [--json] @@ -48,6 +49,7 @@ async function edit(argv: string[], deps: RuntimeApiDeps): Promise { const note = cleared(takeOption(args, "--note")); const apiKeyTransport = cleared(takeOption(args, "--api-key-transport")); const headers = takeOption(args, "--headers"); + const retainModelsRaw = takeOption(args, "--retain-models"); const enabled = takeBooleanOption(args, "--enabled"); const liveModels = takeBooleanOption(args, "--live-models"); const allowPrivateNetwork = takeBooleanOption(args, "--allow-private-network"); @@ -74,6 +76,10 @@ async function edit(argv: string[], deps: RuntimeApiDeps): Promise { } } if (enabled !== undefined) patch.disabled = !enabled; + if (retainModelsRaw !== undefined) { + // `-` clears, matching the other `edit` scalars; test before csv() or it becomes ["-"]. + patch.retainModels = retainModelsRaw.trim() === "-" ? null : csv(retainModelsRaw); + } if (liveModels !== undefined) patch.liveModels = liveModels; if (allowPrivateNetwork !== undefined) patch.allowPrivateNetwork = allowPrivateNetwork; if (Object.keys(patch).length === 0) throw new CliUsageError("at least one edit option is required", USAGE); diff --git a/src/codex/catalog/provider-fetch.ts b/src/codex/catalog/provider-fetch.ts index bc86c8c6e4..45bdfa57b0 100644 --- a/src/codex/catalog/provider-fetch.ts +++ b/src/codex/catalog/provider-fetch.ts @@ -571,6 +571,7 @@ function providerCatalogFingerprint(name: string, prov: OcxProviderConfig): Reco base: prov.baseUrl ?? "", adapter: prov.adapter ?? "", models: [...(prov.models ?? [])].sort(), + retain: [...(prov.retainModels ?? [])].sort(), selected: [...(prov.selectedModels ?? [])].sort(), defaultModel: prov.defaultModel ?? null, ctx: prov.contextWindow ?? null, @@ -1304,7 +1305,14 @@ async function fetchProviderModelsWithAuth( && prov.googleMode === "vertex" && (prov.models?.length ?? 0) === 0 && Boolean(prov.defaultModel); - const configuredIds = seedVertexDefault && prov.defaultModel ? [prov.defaultModel] : (prov.models ?? []); + // Ordered dedupe union: Vertex seed, then `models`, then `retainModels`. `configured` is the + // single seed for the static path, the degraded fallback, drop diagnostics, and provider hints, + // so a retain-only id must enter here or it never exists to be retained (#1690). + const configuredIds = [...new Set([ + ...(seedVertexDefault && prov.defaultModel ? [prov.defaultModel] : []), + ...(prov.models ?? []), + ...(prov.retainModels ?? []), + ])]; const configured: CatalogModel[] = configuredIds.map(id => ({ id, provider: name, @@ -1700,9 +1708,14 @@ export function shouldExposeProviderModel(providerName: string, modelId: string) return true; } -export function shouldRetainConfiguredProviderModel(providerName: string, modelId: string): boolean { +export function shouldRetainConfiguredProviderModel( + providerName: string, + modelId: string, + prov?: OcxProviderConfig, +): boolean { if (CALLABLE_CONFIGURED_COMPATIBILITY_MODELS[providerName]?.has(modelId)) return true; if (providerName === "opencode-free") return modelId === "big-pickle" || modelId.endsWith("-free"); + if (modelInList(prov?.retainModels, modelId)) return true; return false; } @@ -1748,7 +1761,7 @@ export function mergeConfiguredModelsIntoLiveCatalog(opts: { } if ( seedVertexDefault === true - || shouldRetainConfiguredProviderModel(name, candidate.id) + || shouldRetainConfiguredProviderModel(name, candidate.id, prov) || (retainComboTargets && retainConfiguredModelIds?.has(candidate.id) === true) ) { out.push(candidate); diff --git a/src/config.ts b/src/config.ts index c17a86d1ff..e28e69fafd 100644 --- a/src/config.ts +++ b/src/config.ts @@ -529,6 +529,9 @@ const providerConfigSchema = z.object({ noStructuredOutputModels: z.array(z.string().min(1)) .transform(normalizeNonBlankStringArray) .optional(), + retainModels: z.array(z.string().min(1)) + .transform(normalizeNonBlankStringArray) + .optional(), omitReasoningEffortWithToolsModels: z.array(z.string().min(1)) .transform(normalizeNonBlankStringArray) .optional(), @@ -1368,6 +1371,17 @@ const configSchema = z.object({ message: structuredOutputOptOutError, }); } + const retainModelsError = nonBlankStringArrayConfigError( + (provider as { retainModels?: unknown }).retainModels, + "retainModels", + ); + if (retainModelsError) { + ctx.addIssue({ + code: "custom", + path: ["providers", redactSecretString(name), "retainModels"], + message: retainModelsError, + }); + } const toolReasoningOptOutError = nonBlankStringArrayConfigError( (provider as { omitReasoningEffortWithToolsModels?: unknown }).omitReasoningEffortWithToolsModels, "omitReasoningEffortWithToolsModels", diff --git a/src/providers/model-rename-migration.ts b/src/providers/model-rename-migration.ts index 3895386b58..13129264da 100644 --- a/src/providers/model-rename-migration.ts +++ b/src/providers/model-rename-migration.ts @@ -99,6 +99,9 @@ const MODEL_ID_LISTS = [ // their catalog instead of being renamed. OAuth reconciliation does not cover this // field, so the rename has to. "selectedModels", + // Same reasoning as `selectedModels`: a retired id pinned here would be resurrected as a + // ghost row on every discovery instead of following the rename (#1690). + "retainModels", "noVisionModels", "noReasoningModels", "noTemperatureModels", diff --git a/src/server/auth-cors.ts b/src/server/auth-cors.ts index 934e09ae59..10516ca31c 100644 --- a/src/server/auth-cors.ts +++ b/src/server/auth-cors.ts @@ -694,6 +694,8 @@ export function providerManagementConfigError(name: unknown, provider: unknown): "noStructuredOutputModels", ); if (structuredOutputOptOutError) return `provider ${name} ${structuredOutputOptOutError}`; + const retainModelsError = nonBlankStringArrayConfigError(raw.retainModels, "retainModels"); + if (retainModelsError) return `provider ${name} ${retainModelsError}`; const toolReasoningOptOutError = nonBlankStringArrayConfigError( raw.omitReasoningEffortWithToolsModels, "omitReasoningEffortWithToolsModels", @@ -792,6 +794,7 @@ export function safeConfigDTO(config: OcxConfig): unknown { "noTopPModels", "noPenaltyModels", "noStructuredOutputModels", + "retainModels", "omitReasoningEffortWithToolsModels", "upstreamHttpVersion", "autoToolChoiceOnlyModels", diff --git a/src/server/management/provider-routes.ts b/src/server/management/provider-routes.ts index 56c9d700c9..22da830989 100644 --- a/src/server/management/provider-routes.ts +++ b/src/server/management/provider-routes.ts @@ -396,6 +396,19 @@ function applyProviderPatchFields( } touched = true; } + if (Object.hasOwn(rawBody, "retainModels")) { + const value = rawBody.retainModels; + if (value === null) { + delete next.retainModels; + } else { + const error = nonBlankStringArrayConfigError(value, "retainModels"); + if (error) return { error }; + const models = normalizeNonBlankStringArray(value as string[]); + if (models.length > 0) next.retainModels = models; + else delete next.retainModels; + } + touched = true; + } if (Object.hasOwn(rawBody, "omitReasoningEffortWithToolsModels")) { const value = rawBody.omitReasoningEffortWithToolsModels; if (value === null) { @@ -538,6 +551,7 @@ export async function handleProviderRoutes(ctx: ManagementContext): Promise { + globalThis.fetch = originalFetch; + clearModelCache(); + resetCatalogRuntimeStateForTests(); +}); + +function stubLiveModels(ids: string[]): void { + globalThis.fetch = (async (input: RequestInfo | URL) => { + if (!String(input).includes("/models")) return new Response(null, { status: 404 }); + return Response.json({ data: ids.map(id => ({ id })) }); + }) as typeof fetch; +} + +function discoveryConfig(prov: Partial): OcxConfig { + return withStubbedProviderFetch({ + port: 10100, + defaultProvider: "demo", + providers: { + demo: { + adapter: "openai-chat", + baseUrl: "https://api.example.test/v1", + apiKey: "k", + liveModels: true, + ...prov, + }, + }, + } as unknown as OcxConfig); +} + +function provider(overrides: Partial = {}): OcxProviderConfig { + return { + adapter: "openai-chat", + baseUrl: "https://example.test/v1", + apiKey: "sk-test", + authMode: "key", + ...overrides, + }; +} + +function configured(ids: string[]) { + return ids.map(id => ({ id, provider: "demo" })); +} + +function live(ids: string[]) { + return ids.map(id => ({ id, provider: "demo" })); +} + +describe("shouldRetainConfiguredProviderModel", () => { + test("empty retainModels does not change behavior", () => { + expect(shouldRetainConfiguredProviderModel("demo", "any-id")).toBe(false); + expect(shouldRetainConfiguredProviderModel("demo", "any-id", provider())).toBe(false); + expect( + shouldRetainConfiguredProviderModel("demo", "any-id", provider({ retainModels: [] })), + ).toBe(false); + }); + + test("retainModels preserves listed id", () => { + expect( + shouldRetainConfiguredProviderModel( + "demo", + "kept-id", + provider({ retainModels: ["kept-id", "another"] }), + ), + ).toBe(true); + }); + + test("retainModels supports the family-suffix matcher used elsewhere", () => { + // modelInList treats entries ending with ":tag" as a wildcard for `id:tag` siblings. + expect( + shouldRetainConfiguredProviderModel( + "demo", + "kimi-k2.5:free", + provider({ retainModels: ["kimi-k2.5:free"] }), + ), + ).toBe(true); + expect( + shouldRetainConfiguredProviderModel( + "demo", + "kimi-k2.5:free", + provider({ retainModels: ["kimi-k2.5"] }), + ), + ).toBe(true); + }); + + test("built-in kimi / xai hardcoded tables still win", () => { + // Mirrors the canonical compatibility allow-list; ensures the new branch is purely additive. + expect(shouldRetainConfiguredProviderModel("kimi", "k3[1m]")).toBe(true); + expect(shouldRetainConfiguredProviderModel("xai", "grok-4.3")).toBe(true); + expect(shouldRetainConfiguredProviderModel("opencode-free", "big-pickle")).toBe(true); + }); +}); + +describe("mergeConfiguredModelsIntoLiveCatalog with retainModels", () => { + test("merge only retains what the caller seeded — the union happens at discovery", () => { + const prov = provider({ + models: ["configured-id"], + retainModels: ["configured-id", "ghost-id"], + }); + const { models, droppedConfiguredIds } = mergeConfiguredModelsIntoLiveCatalog({ + name: "demo", + provider: prov, + models: live([]), + configured: configured(["configured-id"]), + }); + expect(models.map(m => m.id)).toEqual(["configured-id"]); + expect(droppedConfiguredIds).toEqual([]); + }); + + test("retainModels keeps a configured id when live discovery omits it", () => { + const prov = provider({ + models: ["kept-id", "dropped-id"], + retainModels: ["kept-id"], + }); + const { models, droppedConfiguredIds } = mergeConfiguredModelsIntoLiveCatalog({ + name: "demo", + provider: prov, + models: live(["other-live-id"]), + configured: configured(["kept-id", "dropped-id"]), + }); + expect(models.map(m => m.id).sort()).toEqual(["kept-id", "other-live-id"]); + expect(droppedConfiguredIds).toEqual(["dropped-id"]); + }); + + test("live discovery empty (404-style) still keeps retained rows and surfaces the rest", () => { + const prov = provider({ + models: ["kept-id", "dropped-id"], + retainModels: ["kept-id"], + }); + const { models, droppedConfiguredIds } = mergeConfiguredModelsIntoLiveCatalog({ + name: "demo", + provider: prov, + models: live([]), + configured: configured(["kept-id", "dropped-id"]), + }); + expect(models.map(m => m.id)).toEqual(["kept-id"]); + expect(droppedConfiguredIds).toEqual(["dropped-id"]); + }); +}); + +describe("retainModels through provider discovery (#1690)", () => { + test("a retain-only id survives live discovery that omits it, with provider hints applied", async () => { + stubLiveModels(["live-id"]); + const models = await gatherRoutedModelsDirect(discoveryConfig({ + models: ["seen-id"], + retainModels: ["retained-only"], + modelContextWindows: { "retained-only": 123_456 }, + })); + const demo = models.filter(m => m.provider === "demo"); + expect(demo.map(m => m.id).sort()).toEqual(["live-id", "retained-only"]); + expect(demo.find(m => m.id === "retained-only")?.contextWindow).toBe(123_456); + }); + + test("a retained id the live catalog also returns yields one row", async () => { + stubLiveModels(["both-id"]); + const models = await gatherRoutedModelsDirect(discoveryConfig({ retainModels: ["both-id"] })); + expect(models.filter(m => m.provider === "demo").map(m => m.id)).toEqual(["both-id"]); + }); + + test("liveModels: false lists retainModels alongside models", async () => { + const models = await gatherRoutedModelsDirect(discoveryConfig({ + liveModels: false, + models: ["static-id"], + retainModels: ["static-id", "retained-only"], + })); + expect(models.filter(m => m.provider === "demo").map(m => m.id).sort()).toEqual(["retained-only", "static-id"]); + }); + + test("absent retainModels keeps today's drop behavior", async () => { + stubLiveModels(["live-id"]); + const models = await gatherRoutedModelsDirect(discoveryConfig({ models: ["unseen-id"] })); + expect(models.filter(m => m.provider === "demo").map(m => m.id)).toEqual(["live-id"]); + }); +}); diff --git a/tests/cli-headless-parity.test.ts b/tests/cli-headless-parity.test.ts index f9b9762eb6..94e223286a 100644 --- a/tests/cli-headless-parity.test.ts +++ b/tests/cli-headless-parity.test.ts @@ -329,6 +329,24 @@ describe("headless GUI parity CLI", () => { expect(clearRuntime.requests[0]?.body).toEqual({ headers: null }); }); + test("provider edit --retain-models sends the csv list and - clears it", async () => { + const runtime = fakeRuntime(); + const code = await handleProviderRuntimeCommand("edit", [ + "agw", "--retain-models", " gemini-3.7-flash, other-id ,gemini-3.7-flash", "--json", + ], runtime.deps); + expect(code).toBe(0); + expect(runtime.requests).toEqual([{ + path: "/api/providers?name=agw", + method: "PATCH", + body: { retainModels: ["gemini-3.7-flash", "other-id"] }, + }]); + + const clearRuntime = fakeRuntime(); + const clearCode = await handleProviderRuntimeCommand("edit", ["agw", "--retain-models", "-", "--json"], clearRuntime.deps); + expect(clearCode).toBe(0); + expect(clearRuntime.requests[0]?.body).toEqual({ retainModels: null }); + }); + test("provider edit rejects malformed --headers JSON without a request", async () => { const runtime = fakeRuntime(); const code = await handleProviderRuntimeCommand("edit", ["agw", "--headers", "{not json"], runtime.deps); diff --git a/tests/management-provider-validation.test.ts b/tests/management-provider-validation.test.ts index ec255b61f8..d8a0bdf29d 100644 --- a/tests/management-provider-validation.test.ts +++ b/tests/management-provider-validation.test.ts @@ -330,6 +330,57 @@ describe("provider management validation", () => { .toEqual(["deepseek-v4-flash", "other-model"]); }); + test("validates, exposes, and normalizes retainModels (#1690)", () => { + const provider = { + adapter: "openai-chat", + baseUrl: "https://relay.example/v1", + retainModels: ["gemini-3.7-flash"], + }; + expect(providerManagementConfigError("relay", provider)).toBeNull(); + for (const retainModels of ["gemini-3.7-flash", [""], [" "], [42]]) { + expect(providerManagementConfigError("relay", { ...provider, retainModels })) + .toContain("retainModels"); + } + + const dto = safeConfigDTO({ + port: 10100, + defaultProvider: "relay", + providers: { relay: provider }, + } as OcxConfig) as { providers: Record }; + expect(dto.providers.relay?.retainModels).toEqual(["gemini-3.7-flash"]); + + if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); + mkdirSync(TEST_DIR, { recursive: true }); + process.env.OPENCODEX_HOME = TEST_DIR; + writeFileSync(join(TEST_DIR, "config.json"), JSON.stringify({ + ...config("127.0.0.1"), + defaultProvider: "relay", + providers: { + relay: { + adapter: "openai-chat", + baseUrl: "https://relay.example/v1", + retainModels: [" gemini-3.7-flash ", "gemini-3.7-flash", " other-model "], + }, + }, + })); + expect(loadConfig().providers.relay?.retainModels).toEqual(["gemini-3.7-flash", "other-model"]); + + writeFileSync(join(TEST_DIR, "config.json"), JSON.stringify({ + ...config("127.0.0.1"), + defaultProvider: "relay", + providers: { relay: { ...provider, retainModels: "gemini-3.7-flash" } }, + })); + // Invalid config falls back to defaults (with a backup) rather than throwing; the relay + // provider must be gone, proving the schema rejected the string form with a path. + const errorSpy = spyOn(console, "error").mockImplementation(() => {}); + try { + expect(loadConfig().providers.relay).toBeUndefined(); + expect(errorSpy.mock.calls.map(call => String(call[0])).join("\n")).toContain("providers.relay.retainModels"); + } finally { + errorSpy.mockRestore(); + } + }); + test("validates, exposes, and normalizes tool-bearing reasoning-effort opt-outs", () => { const provider = { adapter: "openai-chat", @@ -2206,6 +2257,37 @@ describe("provider management validation", () => { providers: Record; }; expect(saved.providers["structured-output-toggle"].noStructuredOutputModels).toBeUndefined(); + + const retainInvalid = await fetch(new URL("/api/providers?name=structured-output-toggle", server.url), { + method: "PATCH", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ retainModels: "gemini-3.7-flash" }), + }); + expect(retainInvalid.status).toBe(400); + + const retainRes = await fetch(new URL("/api/providers?name=structured-output-toggle", server.url), { + method: "PATCH", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ retainModels: [" gemini-3.7-flash ", "gemini-3.7-flash"] }), + }); + expect(retainRes.status).toBe(200); + const retainList = await fetch(new URL("/api/providers", server.url)).then(response => response.json()) as Array<{ + name: string; + retainModels?: string[]; + }>; + expect(retainList.find(provider => provider.name === "structured-output-toggle")?.retainModels) + .toEqual(["gemini-3.7-flash"]); + + const retainClear = await fetch(new URL("/api/providers?name=structured-output-toggle", server.url), { + method: "PATCH", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ retainModels: null }), + }); + expect(retainClear.status).toBe(200); + const retainSaved = await fetch(new URL("/api/config", server.url)).then(response => response.json()) as { + providers: Record; + }; + expect(retainSaved.providers["structured-output-toggle"].retainModels).toBeUndefined(); } finally { await server.stop(true); } diff --git a/tests/model-rename-migration.test.ts b/tests/model-rename-migration.test.ts index a4648b04ad..61796018ce 100644 --- a/tests/model-rename-migration.test.ts +++ b/tests/model-rename-migration.test.ts @@ -34,6 +34,7 @@ function staleConfig(): OcxConfig { modelDefaultReasoningEfforts: { "qwen3.8-max-preview": "xhigh" }, preserveReasoningContentModels: ["glm-5.2", "qwen3.8-max-preview", "qwen3.7-max"], thinkingBudgetModels: ["qwen3.8-max-preview", "qwen3.7-max"], + retainModels: ["qwen3.8-max-preview"], }, }, disabledModels: ["alibaba-token-plan-intl/qwen3.8-max-preview", "other/model"], @@ -56,6 +57,7 @@ describe("registry model rename migration (#1610)", () => { expect(prov.modelDefaultReasoningEfforts?.["qwen3.8-max"]).toBe("xhigh"); expect(prov.preserveReasoningContentModels).toEqual(["glm-5.2", "qwen3.8-max", "qwen3.7-max"]); expect(prov.thinkingBudgetModels).toEqual(["qwen3.8-max", "qwen3.7-max"]); + expect(prov.retainModels).toEqual(["qwen3.8-max"]); expect(warnings.some(w => w.includes("qwen3.8-max"))).toBe(true); }); @@ -82,6 +84,7 @@ describe("registry model rename migration (#1610)", () => { prov.modelDefaultReasoningEfforts = {}; prov.preserveReasoningContentModels = ["qwen3.8-max"]; prov.thinkingBudgetModels = ["qwen3.7-max"]; + prov.retainModels = ["qwen3.8-max"]; clean.disabledModels = ["other/model"]; const { changed, warnings } = projectModelRenames(clean, [RENAME]); From 0d73d6557b7837850629da7cf7bd9cea594720e7 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 04:22:12 +0900 Subject: [PATCH 136/172] feat(images): relay Codex image_gen to xAI Imagine with Grok OAuth (carry of #2083) (#2986) * feat(images): relay Codex image_gen to xAI Imagine with Grok OAuth When images.bridgeEnabled is on and an xAI login exists, POST /v1/images/{generations,edits} goes to api.x.ai instead of ChatGPT. Routed chat turns keep hosted image_generation visible as image_gen so Grok can call Codex's client tool. The Responses image-bridge loop stays API-key-only and still defers to web search. * fix(images): bound xAI relay timeout and reject empty edits * fix(images): cap xAI relay output across the full batch Track combined decoded and base64-encoded bytes for inline b64_json and URL downloads, return 502 when the next image would exceed 100 MiB, and document the aggregate contract. * fix(images): harden xAI relay identity, redirects, and URL fetches Keep the synthetic image_gen root tool even when a namespaced ordinary image_gen is already present. Reject 3xx on credential-bearing xAI POSTs. Download Imagine result URLs through destination-policy plus pinned HTTPS, and fail closed with a generic error. Tighten the auth-isolation test so unexpected upstreams cannot slip past the capture array. * fix(images): close reverse image_gen identity and sanitize xAI errors Skip a later ordinary root image_gen when the synthetic tool is already present. Share destination-policy plus pinned HTTPS setup between image and video downloads. Sanitize xAI catch-path messages the same way CCA does. * fix(images): match CCA inline validation and OAuth relay coverage * fix(images): honor explicit images.provider and validate URL payloads * fix(images): fail closed when Imagine OAuth is missing When images.bridgeEnabled is on and the xAI provider has no Grok token, return 400 instead of silently billing ChatGPT. Document the /v1/images OAuth relay on the ja/ko/ru/zh-cn image-bridge pages. * fix(images): close Auto ratio, root image_gen collapse, and default download cap Addresses the three runtime edge cases raised in review of the #2083 carry. - fulfill forwarded a pre-folded aspect_ratio, so an explicit "auto" looked absent and callXaiImages derived a ratio from `size` instead of suppressing it. Forward the raw literal and let the client own validation. - The parser replaced only the first un-namespaced `image_gen` when a hosted declaration arrived, so two root declarations left a second root behind and the catalog stayed ambiguous. Remove all root collisions, keep namespaced entries, insert exactly one synthetic root. - The default downloader in connectPublicHttps forwarded `maxBytes: undefined` to pinnedHttpGet, whose cap is optional, removing the ceiling rather than inheriting it. Preserve MAX_DOWNLOAD_BYTES and honour tighter explicit limits. Docs now state the implemented precedence: the xAI relay owns /v1/images only when bridgeEnabled is true and images.provider is omitted. * docs(images): state the xAI relay credential mode, provider precedence, and result URL contract Closes the two documentation-boundary items from the exact-head review of 842170b6f: the Grok grant is used only with authMode oauth (API key otherwise), an explicit images.provider owns /v1/images and never falls back to xAI, and URL results are fetched credentialless over public HTTPS with no redirects, a 50 MiB per-file cap, and authenticated artifact retrieval. Same facts mirrored into ja/ko/zh-cn/ru. --------- Co-authored-by: zhou-zhichao Co-authored-by: lidge-jun Co-authored-by: jun --- .../050_wp5_xai_imagine_carry.md | 29 ++ .../051_wp5_audit_r1_synthesis.md | 13 + .../content/docs/guides/codex-integration.md | 16 + .../src/content/docs/guides/image-bridge.md | 17 +- .../docs/ja/guides/codex-integration.md | 1 + .../content/docs/ja/guides/image-bridge.md | 2 +- .../docs/ko/guides/codex-integration.md | 1 + .../content/docs/ko/guides/image-bridge.md | 2 +- .../docs/ru/guides/codex-integration.md | 1 + .../content/docs/ru/guides/image-bridge.md | 13 +- .../docs/zh-cn/guides/codex-integration.md | 1 + .../content/docs/zh-cn/guides/image-bridge.md | 2 +- src/images/artifacts.ts | 110 +++-- src/images/fulfill.ts | 7 +- src/images/index.ts | 2 +- src/images/plan.ts | 14 + src/images/synthetic-tool.ts | 5 + src/images/xai-client.ts | 35 +- src/responses/parser.ts | 46 +- src/server/images.ts | 247 +++++++++- tests/credential-redirect-guard.test.ts | 1 + tests/images/download-cap-default.test.ts | 48 ++ tests/images/synthetic-tool.test.ts | 7 + tests/images/xai-client.test.ts | 61 ++- tests/images/z-fulfill.test.ts | 44 ++ tests/responses-parser.test.ts | 111 +++++ tests/server-images.test.ts | 452 +++++++++++++++++- 27 files changed, 1224 insertions(+), 64 deletions(-) create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/050_wp5_xai_imagine_carry.md create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/051_wp5_audit_r1_synthesis.md create mode 100644 tests/images/download-cap-default.test.ts diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/050_wp5_xai_imagine_carry.md b/devlog/_plan/260902_nonbug_adoption_backlog/050_wp5_xai_imagine_carry.md new file mode 100644 index 0000000000..4249220847 --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/050_wp5_xai_imagine_carry.md @@ -0,0 +1,29 @@ +# wp5 — PR #2986 xAI Imagine image_gen relay (carry of #2083) + +State at entry: head `842170b6f`, 25 files +1175/-64, exact-head CI fully green, rebased cleanly onto +`origin/dev` (`6a6efa928`) as `codex/carry-2083-xai-imagine-2` (`b7ad8820c`) with no conflicts. +Reviewer (Ingwannu) confirmed all three code blockers resolved at `842170b6f` and left two +documentation-boundary items before approval. No code redesign requested. + +## Scope (docs only) + +1. `docs-site/src/content/docs/guides/codex-integration.md` — xAI Imagine relay bullet: + - state that the Grok grant is used only when the `xai` provider has `authMode: "oauth"` + (`resolveXaiImageAuthToken` in `src/images/plan.ts`); any other authMode uses the API key. + - state that an explicit `images.provider` owns `/v1/images` and prevents the xAI fallback. + - result URL contract beside the 100 MiB cap: public HTTPS only, no redirects, no file/loopback, + bounded download (`MAX_DOWNLOAD_BYTES` 50 MiB per file), artifacts served through the + authenticated management endpoint. +2. Same factual sentences in the locale copies of the same bullet (ja/ko/zh-cn/zh-tw/ru/fr/tr) where + the bullet exists, so a translation does not contradict English. +3. Resolve the now-fixed `maxBytes` review thread. + +## Acceptance + +- English bullet contains: authMode oauth condition, explicit images.provider precedence, URL contract. +- Locales that carry the bullet do not contradict it. +- `bun x tsc --noEmit` and `bun run privacy:scan` clean; focused `tests/server-images.test.ts`, + `tests/responses-parser.test.ts` green (unchanged code, sanity). +- Push `--no-verify` to the same PR branch (force since rebased), admin squash merge, landing proof, close #2083 + as landed-via-carry. + diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/051_wp5_audit_r1_synthesis.md b/devlog/_plan/260902_nonbug_adoption_backlog/051_wp5_audit_r1_synthesis.md new file mode 100644 index 0000000000..146a869150 --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/051_wp5_audit_r1_synthesis.md @@ -0,0 +1,13 @@ +# wp5 audit r1 — synthesis + +Audit input is the maintainer-reviewer's incremental review of exact head `842170b6f` (Ingwannu, +2026-08-31): all three code blockers resolved, focused regressions meaningful, exact-head CI and the +service lifecycle matrix green. Residual: two documentation-boundary items and one review thread to +resolve. Verdict carried as **near-pass**; residuals are the whole of the B scope in 050. + +Source verification for the doc sentences (read in this worktree): +- `src/images/plan.ts` `resolveXaiImageAuthToken`: Grok grant only when `authMode === "oauth"`, else API key. +- `src/images/artifacts.ts`: HTTPS-only (`:278`, `:313`), `redirect: "manual"` with 3xx rejected + (`xai-client.ts:124-128`), `MAX_DOWNLOAD_BYTES` 50 MiB default (`:14`, `:281`, `:327`). +- Image-bridge precedence sentence already present and correct; mirror it into codex-integration. + diff --git a/docs-site/src/content/docs/guides/codex-integration.md b/docs-site/src/content/docs/guides/codex-integration.md index 2de6f6c1c1..6cb85614ff 100644 --- a/docs-site/src/content/docs/guides/codex-integration.md +++ b/docs-site/src/content/docs/guides/codex-integration.md @@ -55,6 +55,22 @@ Standalone `/images/generations` calls never enter that bridge. `openai-responses` provider whose endpoint implements the OpenAI Images API. Explicit selection fails closed and never falls back to a different paid upstream. Registry-managed provider ids are not accepted here; omit `images.provider` to use the built-in OpenAI tiers. +- **xAI Imagine (Grok OAuth) relay:** when `images.bridgeEnabled` is `true`, `images.provider` is + omitted, and an `xai` provider is configured, `/v1/images/generations` and `/v1/images/edits` + are sent to `https://api.x.ai/v1`. The credential depends on the provider's `authMode`: with + `"oauth"` the relay reuses the Grok CLI grant from `ocx login xai`; with any other mode it uses + the provider's API key. An OAuth login does not arm a keyed provider, and vice versa. ChatGPT + credentials are not forwarded. If the credential is missing, the proxy returns 400 instead of + billing ChatGPT. Setting `images.provider` explicitly hands `/v1/images` to that provider; its + own validation errors are returned as-is and the xAI relay is never tried. + The relay maps Codex `size` / `aspect_ratio` onto xAI's Imagine body and returns + the same `{created, data:[{b64_json}]}` shape. Combined decoded bytes and base64-encoded output + across the batch (inline `b64_json` and downloaded URLs) stay under 100 MiB; a batch that would + exceed that cap returns 502. When xAI returns an image URL instead of inline bytes, the proxy + fetches it itself with no credential: the URL must be public HTTPS (no redirects, no + `file:`, no loopback or private addresses), each download is capped at 50 MiB, and the result is + materialized as a local artifact that is served only through the authenticated management + endpoint. This is independent of the Responses Image Bridge loop (which remains API-key-only). - **Google Antigravity (CCA) fallback:** when neither an OpenAI forward candidate nor a keyed provider is configured, `/v1/images/generations` (not `/images/edits`) falls back to the Antigravity **Cloud Code Assist** endpoint using the `gemini-3.1-flash-image` model. The fallback diff --git a/docs-site/src/content/docs/guides/image-bridge.md b/docs-site/src/content/docs/guides/image-bridge.md index 6606fee55b..2f81d9a1f4 100644 --- a/docs-site/src/content/docs/guides/image-bridge.md +++ b/docs-site/src/content/docs/guides/image-bridge.md @@ -14,10 +14,19 @@ xAI Grok Imagine, so the model you're actually chatting with can still generate - **Enable the bridge** by setting `images.bridgeEnabled: true` in your config (it is off by default to avoid unexpected xAI charges — see [Configuration](#configuration) below). -- An `xai` provider entry with an **API key**. The bridge pins fulfillment to the registry xAI - Images endpoint (`https://api.x.ai/v1`); any configured `baseUrl` override is ignored for image - calls. OAuth / `ocx login xai` alone does **not** arm the bridge (the Grok CLI OAuth transport is - chat-oriented and is not used for `/images/*`). +- An `xai` provider entry with an **API key**. The Responses Image Bridge pins fulfillment to the + registry xAI Images endpoint (`https://api.x.ai/v1`); any configured `baseUrl` override is + ignored for image calls. OAuth / `ocx login xai` alone does **not** arm this sidecar loop. + The same `bridgeEnabled` flag does arm the separate Codex `/v1/images` relay so the built-in + `image_gen` client can call Imagine with the Grok CLI grant — see + [Built-in image generation](/guides/codex-integration/#built-in-image-generation-image_gen). + If that grant (or an xAI API key) is missing, `/v1/images` returns an error instead of + falling through to ChatGPT. + + The relay only owns the route when no image provider is configured: it runs when + `images.bridgeEnabled` is `true` **and** `images.provider` is omitted. Setting + `images.provider` explicitly hands `/v1/images` to that provider, and its own + validation errors are returned as-is rather than being retried through xAI. ```json { diff --git a/docs-site/src/content/docs/ja/guides/codex-integration.md b/docs-site/src/content/docs/ja/guides/codex-integration.md index ac584e53d7..06cd590084 100644 --- a/docs-site/src/content/docs/ja/guides/codex-integration.md +++ b/docs-site/src/content/docs/ja/guides/codex-integration.md @@ -40,6 +40,7 @@ Codex の組み込み `image_gen` ツールは、`/v1/responses` を経由しま 失敗。壊れた/期限切れのプール認証情報が、別途請求される API 使用量の背後に隠れることはありません。 - **明示的なカスタム プロバイダー:** `images.provider` をカスタム API キーの ID に設定します。 `openai-responses` プロバイダー。そのエンドポイントは OpenAI Images API を実装します。明示的な選択はクローズに失敗し、別の有料アップストリームにフォールバックすることはありません。レジストリで管理されているプロバイダー ID はここでは受け入れられません。組み込みの OpenAI 層を使用するには、`images.provider` を省略します。 +- **xAI Imagine (Grok OAuth) リレー:** `images.bridgeEnabled` が `true` で、`images.provider` が未設定、かつ `xai` プロバイダーが設定されている場合、`/v1/images/generations` と `/v1/images/edits` は `https://api.x.ai/v1` に送られます。使われる資格情報はプロバイダーの `authMode` で決まります。`"oauth"` なら `ocx login xai` の Grok CLI グラントを再利用し、それ以外ならプロバイダーの API キーを使います。OAuth ログインがキー方式のプロバイダーを有効にすることはなく、その逆もありません。ChatGPT の資格情報は転送されません。資格情報が無い場合、プロキシは ChatGPT に課金せず 400 を返します。`images.provider` を明示すると `/v1/images` はそのプロバイダーが受け持ち、その検証エラーがそのまま返され、xAI リレーは試行されません。リレーは Codex の `size` / `aspect_ratio` を xAI Imagine のボディに写し、同じ `{created, data:[{b64_json}]}` 形を返します。バッチ全体(インライン `b64_json` とダウンロードした URL)のデコード済みバイトと base64 エンコード出力は合わせて 100 MiB 未満です。上限を超えるバッチは 502 を返します。xAI がインラインのバイト列ではなく画像 URL を返した場合、プロキシは資格情報なしで自ら取得します。URL は公開 HTTPS でなければならず(リダイレクト、`file:`、ループバックやプライベートアドレスは不可)、1 ファイルあたり 50 MiB が上限で、結果はローカルのアーティファクトとして保存され、認証済みの管理エンドポイント経由でのみ配信されます。これは API キー専用の Responses Image Bridge ループとは独立です。 - **Google Antigravity (CCA) フォールバック:** OpenAI 前方候補でもキー付きでもない場合 プロバイダーが構成されている場合、`/v1/images/generations` (`/images/edits` ではありません) は、`gemini-3.1-flash-image` モデルを使用して Antigravity **Cloud Code Assist** エンドポイントにフォールバックします。フォールバックは、OpenAI 候補が構成されていない場合だけでなく、OpenAI 認証の解決が失敗した後 (ChatGPT 資格情報の期限切れまたは欠落など) にも起動されます。これには `ocx login google-antigravity` が必要です。 OAuth トークンは、固定された CCA レジストリ ホストにのみ送信され、構成レベルの `baseUrl` オーバーライドには送信されません。応答は、Codex が期待するのと同じ `{created, data:[{b64_json}]}` 形状で返されます。 - **どちらでもない:** プロキシは一般的な 404 ではなく明確なエラーを返します。 ルーティングされたプロバイダー diff --git a/docs-site/src/content/docs/ja/guides/image-bridge.md b/docs-site/src/content/docs/ja/guides/image-bridge.md index f07d16a1e9..c7b3c0e36d 100644 --- a/docs-site/src/content/docs/ja/guides/image-bridge.md +++ b/docs-site/src/content/docs/ja/guides/image-bridge.md @@ -12,7 +12,7 @@ OpenAI 以外のモデル (Claude、Gemini、Grok など) を介して Codex を - **設定で `images.bridgeEnabled: true` を設定してブリッジを有効にします** (これはオフになっています) 予期しない xAI 請求を避けるためのデフォルト — 以下の [構成](#configuration) を参照してください)。 - **API キー**を持つ `xai` プロバイダー エントリ。ブリッジはフルフィルメントをレジストリ xAI に固定します -画像エンドポイント (`https://api.x.ai/v1`);設定された `baseUrl` オーバーライドは、イメージ呼び出しでは無視されます。 OAuth / `ocx login xai` だけではブリッジを準備しません** (Grok CLI OAuth トランスポートはチャット指向であり、`/images/*` には使用されません)。 +画像エンドポイント (`https://api.x.ai/v1`);設定された `baseUrl` オーバーライドは、イメージ呼び出しでは無視されます。 OAuth / `ocx login xai` だけではこのサイドカー・ループは有効になりません。同じ `bridgeEnabled` フラグは、別系統の Codex `/v1/images` リレーを有効にし、組み込みの `image_gen` クライアントが Grok CLI の認可で Imagine を呼べるようにします。認可(または xAI API キー)が無い場合、`/v1/images` は ChatGPT にフォールスルーせずエラーを返します。詳細は [組み込み画像生成](/guides/codex-integration/#built-in-image-generation-image_gen) を参照してください。このリレーが経路を持つのは、`images.bridgeEnabled` が `true` で、かつ `images.provider` が未指定のときだけです。`images.provider` を明示すると `/v1/images` はそのプロバイダーが担当し、そのバリデーションエラーは xAI で再試行されずそのまま返ります。 「`json { "providers": { "xai": { "adapter": "openai-chat", "apiKey": "xai-…", "authMode": "key" } } } `」 diff --git a/docs-site/src/content/docs/ko/guides/codex-integration.md b/docs-site/src/content/docs/ko/guides/codex-integration.md index fd37a48fe9..3e9f8355ef 100644 --- a/docs-site/src/content/docs/ko/guides/codex-integration.md +++ b/docs-site/src/content/docs/ko/guides/codex-integration.md @@ -37,6 +37,7 @@ Codex의 내장 `image_gen` 도구는 `/v1/responses`를 거치지 않습니다. - **모드 인식 forward 후보 하나:** Pool은 적격한 메인/추가 계정을 선택하고, Direct는 호출자 OAuth bearer를 사용합니다. 설정된 모드는 이미지 요청에도 일관되게 적용됩니다. - **OpenAI API-key provider:** forward 후보 중 누구도 인증 실패를 가지지 않을 때만 사용합니다. 고장 나거나 만료된 Pool credential을 별도로 청구되는 API 사용 뒤에 숨기지 않습니다. - **명시적 커스텀 provider:** `images.provider`를 OpenAI Images API를 구현한 커스텀 API-key `openai-responses` provider id로 설정할 수 있습니다. 명시적으로 선택한 provider는 닫힌 상태로 실패하며, 다른 유료 upstream으로 fallback하지 않습니다. registry-managed provider id는 여기서 허용하지 않습니다. 기본 제공 OpenAI tiers를 쓰려면 `images.provider`를 생략하세요. +- **xAI Imagine (Grok OAuth) relay:** `images.bridgeEnabled`가 `true`이고 `images.provider`가 비어 있으며 `xai` provider가 설정되어 있으면 `/v1/images/generations`와 `/v1/images/edits`가 `https://api.x.ai/v1`로 전송됩니다. 어떤 credential을 쓰는지는 provider의 `authMode`가 정합니다. `"oauth"`면 `ocx login xai`로 받은 Grok CLI grant를 재사용하고, 그 외에는 provider의 API key를 씁니다. OAuth 로그인이 key 방식 provider를 활성화하지는 않으며 반대도 마찬가지입니다. ChatGPT credential은 전달되지 않습니다. credential이 없으면 프록시는 ChatGPT에 과금하지 않고 400을 반환합니다. `images.provider`를 명시하면 `/v1/images`는 그 provider가 맡고, 그 provider의 검증 오류가 그대로 반환되며 xAI relay는 시도되지 않습니다. relay는 Codex `size` / `aspect_ratio`를 xAI Imagine body에 매핑하고 같은 `{created, data:[{b64_json}]}` 형태를 반환합니다. 배치 전체(인라인 `b64_json`과 내려받은 URL)의 디코드 바이트와 base64 인코드 출력은 합쳐서 100 MiB 미만입니다. 한도를 넘는 배치는 502를 반환합니다. xAI가 인라인 바이트 대신 이미지 URL을 돌려주면 프록시가 credential 없이 직접 내려받습니다. URL은 공개 HTTPS여야 하고(리다이렉트, `file:`, loopback·사설 주소 불가), 파일당 50 MiB 상한이 있으며, 결과는 로컬 artifact로 저장되어 인증된 management endpoint로만 제공됩니다. 이 경로는 API-key-only Responses Image Bridge 루프와 별개입니다. - **Google Antigravity (CCA) fallback:** OpenAI forward 후보도 keyed provider도 없을 때, `/v1/images/generations`(`/images/edits`는 제외)는 `gemini-3.1-flash-image` 모델을 사용해서 Antigravity **Cloud Code Assist** endpoint로 fallback합니다. OpenAI 인증 해석이 실패할 때(예: 만료되었거나 누락된 ChatGPT credential)에도 이 fallback이 동작하며, OpenAI 후보가 아예 없을 때만 발생하는 것은 아닙니다. 이 기능은 `ocx login google-antigravity`를 필요로 합니다. OAuth token은 오직 고정된 CCA registry host로만 전송되며, config-level `baseUrl` override로는 가지 않습니다. 응답은 Codex가 기대하는 `{created, data:[{b64_json}]}` 형식으로 반환됩니다. - **둘 다 없음:** 프록시는 generic 404 대신 명확한 오류를 반환합니다. 라우팅되는 provider(Cursor, Gemini, Kiro 등)는 `image_generation` tool relay를 제공할 수 없습니다. 이 도구를 아예 노출하고 싶지 않다면 Codex에서 `codex features disable image_generation`(`config.toml`의 `[features] image_generation = false`)으로 끄세요. diff --git a/docs-site/src/content/docs/ko/guides/image-bridge.md b/docs-site/src/content/docs/ko/guides/image-bridge.md index d0610fc7a8..7007c02b12 100644 --- a/docs-site/src/content/docs/ko/guides/image-bridge.md +++ b/docs-site/src/content/docs/ko/guides/image-bridge.md @@ -10,7 +10,7 @@ Codex를 Claude, Gemini, Grok 같은 OpenAI가 아닌 모델로 라우팅하면 ## 사전 조건 - 구성에서 `images.bridgeEnabled: true`로 설정해 브리지를 켭니다. 예상치 못한 xAI 요금을 피하려고 기본값은 꺼져 있습니다. 아래 [Configuration](#configuration)을 참고합니다. -- API 키가 있는 `xai` provider 항목이 필요합니다. 브리지는 처리를 레지스트리의 xAI Images endpoint (`https://api.x.ai/v1`)에 고정하며, 이미지 호출에서는 설정된 `baseUrl` override를 무시합니다. OAuth / `ocx login xai`만으로는 브리지가 활성화되지 않습니다. Grok CLI OAuth transport는 채팅용이며 `/images/*`에는 사용되지 않습니다. +- API 키가 있는 `xai` provider 항목이 필요합니다. 브리지는 처리를 레지스트리의 xAI Images endpoint (`https://api.x.ai/v1`)에 고정하며, 이미지 호출에서는 설정된 `baseUrl` override를 무시합니다. OAuth / `ocx login xai`만으로는 이 sidecar 루프가 켜지지 않습니다. 같은 `bridgeEnabled` 플래그는 별도의 Codex `/v1/images` relay를 켜서, 내장 `image_gen` 클라이언트가 Grok CLI grant로 Imagine을 호출할 수 있게 합니다. 그 grant(또는 xAI API key)가 없으면 `/v1/images`는 ChatGPT로 넘어가지 않고 오류를 반환합니다. 이 relay가 경로를 맡는 건 `images.bridgeEnabled`가 `true`이고 `images.provider`를 비워둔 경우뿐입니다. `images.provider`를 지정하면 `/v1/images`는 그 provider가 담당하고, 그쪽 검증 오류는 xAI로 재시도하지 않고 그대로 반환합니다. [Built-in image generation](/guides/codex-integration/#built-in-image-generation-image_gen)을 참고하세요. ```json { diff --git a/docs-site/src/content/docs/ru/guides/codex-integration.md b/docs-site/src/content/docs/ru/guides/codex-integration.md index 1469d655d7..e33bb6c835 100644 --- a/docs-site/src/content/docs/ru/guides/codex-integration.md +++ b/docs-site/src/content/docs/ru/guides/codex-integration.md @@ -59,6 +59,7 @@ reference-image), используя тот же bearer ChatGPT, что и дл провал жёсткий: никакого fallback на другой платный upstream нет. Id провайдера, управляемые registry, здесь не принимаются; если хотите использовать встроенные уровни OpenAI, опустите `images.provider`. +- **Relay xAI Imagine (Grok OAuth):** если `images.bridgeEnabled` равно `true`, `images.provider` не задан и настроен провайдер `xai`, `/v1/images/generations` и `/v1/images/edits` уходят на `https://api.x.ai/v1`. Какие учётные данные используются, определяет `authMode` провайдера: при `"oauth"` relay переиспользует грант Grok CLI из `ocx login xai`, в любом другом режиме — API-ключ провайдера. OAuth-вход не активирует провайдер с ключом, и наоборот. Учётные данные ChatGPT не пересылаются. Если учётных данных нет, прокси возвращает 400 и не тарифицирует ChatGPT. Явно заданный `images.provider` забирает `/v1/images` себе: его ошибки валидации возвращаются как есть, relay xAI не пробуется. Relay отображает Codex `size` / `aspect_ratio` на тело Imagine и возвращает ту же форму `{created, data:[{b64_json}]}`. Суммарные декодированные байты и base64-выход партии (inline `b64_json` и скачанные URL) остаются ниже 100 MiB; превышение даёт 502. Если xAI возвращает URL изображения вместо байтов, прокси скачивает его сам без учётных данных: URL должен быть публичным HTTPS (без редиректов, `file:`, loopback и приватных адресов), каждый файл ограничен 50 MiB, а результат сохраняется как локальный артефакт и отдаётся только через аутентифицированный management-эндпоинт. Это отдельно от цикла Responses Image Bridge, который по-прежнему только с API-ключом. - **Fallback Google Antigravity (CCA):** если не настроен ни один OpenAI forward-candidate и ни один keyed provider, `/v1/images/generations` (но не `/images/edits`) переходит на endpoint Antigravity **Cloud Code Assist** с моделью `gemini-3.1-flash-image`. Этот fallback также diff --git a/docs-site/src/content/docs/ru/guides/image-bridge.md b/docs-site/src/content/docs/ru/guides/image-bridge.md index 77fffeee8f..2e5da3ca1c 100644 --- a/docs-site/src/content/docs/ru/guides/image-bridge.md +++ b/docs-site/src/content/docs/ru/guides/image-bridge.md @@ -16,8 +16,17 @@ Image Bridge обнаруживает такие вызовы и прозрач чтобы не создавать неожиданных расходов xAI — см. [Конфигурацию](#configuration) ниже). - Нужна запись провайдера `xai` с **API-ключом**. Bridge жёстко привязывает выполнение к registry-endpoint'у xAI Images (`https://api.x.ai/v1`); любой настроенный override `baseUrl` - для image-вызовов игнорируется. Одного OAuth / `ocx login xai` для активации bridge - недостаточно (OAuth-транспорт Grok CLI ориентирован на чат и не используется для `/images/*`). + для image-вызовов игнорируется. Одного OAuth / `ocx login xai` недостаточно, чтобы + включить этот sidecar-цикл. Тот же флаг `bridgeEnabled` включает отдельный relay Codex + `/v1/images`, чтобы встроенный клиент `image_gen` мог вызывать Imagine с grant'ом Grok CLI — + см. [Встроенную генерацию изображений](/guides/codex-integration/#built-in-image-generation-image_gen). + Если grant (или API-ключ xAI) отсутствует, `/v1/images` возвращает ошибку и не + переходит на ChatGPT. + + Relay владеет маршрутом только тогда, когда `images.bridgeEnabled` равен `true`, а + `images.provider` не задан. Явно указанный `images.provider` передаёт `/v1/images` + этому провайдеру, и его ошибки валидации возвращаются как есть, без повторной + попытки через xAI. ```json { diff --git a/docs-site/src/content/docs/zh-cn/guides/codex-integration.md b/docs-site/src/content/docs/zh-cn/guides/codex-integration.md index f11f8d9c5c..e2a5601d62 100644 --- a/docs-site/src/content/docs/zh-cn/guides/codex-integration.md +++ b/docs-site/src/content/docs/zh-cn/guides/codex-integration.md @@ -54,6 +54,7 @@ ChatGPT bearer auth。由于注入的 `base_url` 指向 opencodex,proxy 会把 `openai-responses` provider 的 id,该 endpoint 必须实现 OpenAI Images API。显式选择会失败即关闭, 不会 fallback 到其他付费上游。这里不接受 registry 管理的 provider id;省略 `images.provider` 即可使用内置的 OpenAI tiers。 +- **xAI Imagine(Grok OAuth)中继:** 当 `images.bridgeEnabled` 为 `true`、未设置 `images.provider`,且配置了 `xai` provider 时,`/v1/images/generations` 和 `/v1/images/edits` 会发到 `https://api.x.ai/v1`。使用哪种凭据由 provider 的 `authMode` 决定:`"oauth"` 时复用 `ocx login xai` 获得的 Grok CLI 授权,其他模式则使用 provider 的 API key。OAuth 登录不会启用 key 方式的 provider,反之亦然。ChatGPT 凭据不会被转发。若凭据缺失,代理返回 400,而不会向 ChatGPT 计费。显式设置 `images.provider` 后,`/v1/images` 由该 provider 接管,其校验错误原样返回,不会再尝试 xAI 中继。该中继会把 Codex 的 `size` / `aspect_ratio` 映射到 xAI Imagine 请求体,并返回同样的 `{created, data:[{b64_json}]}` 形状。整批(inline `b64_json` 与下载的 URL)解码字节与 base64 编码输出合计不超过 100 MiB;超出则返回 502。若 xAI 返回的是图片 URL 而非内联字节,代理会不带凭据自行下载:URL 必须是公开 HTTPS(不允许重定向、`file:`、回环或私有地址),每个文件上限 50 MiB,结果作为本地 artifact 保存,仅通过需认证的管理端点提供。这与仍仅支持 API key 的 Responses Image Bridge 循环相互独立。 - **Google Antigravity(CCA)fallback:** 当既没有 OpenAI forward 候选,也没有已配置的 keyed provider 时,`/v1/images/generations`(不是 `/images/edits`)会 fallback 到 Antigravity **Cloud Code Assist** endpoint,并使用 `gemini-3.1-flash-image` 模型。该 fallback 也会在 diff --git a/docs-site/src/content/docs/zh-cn/guides/image-bridge.md b/docs-site/src/content/docs/zh-cn/guides/image-bridge.md index 71a4d3d3dd..d627ed0c11 100644 --- a/docs-site/src/content/docs/zh-cn/guides/image-bridge.md +++ b/docs-site/src/content/docs/zh-cn/guides/image-bridge.md @@ -10,7 +10,7 @@ description: 在使用非 OpenAI 提供方时,将 image_generation 托管工 ## 前提条件 - **启用桥接**:在配置中设置 `images.bridgeEnabled: true`(默认关闭,以避免意外产生 xAI 费用 - 见下文的 [配置](#configuration))。 -- 配置一个带有 **API 密钥** 的 `xai` provider 条目。桥接会将执行固定到注册表中的 xAI Images 端点(`https://api.x.ai/v1`);任何已配置的 `baseUrl` 覆盖都会被图像调用忽略。仅有 OAuth / `ocx login xai` **不会** 让桥接生效(Grok CLI 的 OAuth 传输是面向聊天的,不用于 `/images/*`)。 +- 配置一个带有 **API 密钥** 的 `xai` provider 条目。桥接会将执行固定到注册表中的 xAI Images 端点(`https://api.x.ai/v1`);任何已配置的 `baseUrl` 覆盖都会被图像调用忽略。仅有 OAuth / `ocx login xai` **不会** 启用这条 sidecar 循环。同一项 `bridgeEnabled` 会启用另一条 Codex `/v1/images` 中继,让内置 `image_gen` 客户端用 Grok CLI 授权调用 Imagine — 见 [内置图像生成](/guides/codex-integration/#built-in-image-generation-image_gen)。若该授权(或 xAI API key)缺失,`/v1/images` 会返回错误,而不会落到 ChatGPT。只有在 `images.bridgeEnabled` 为 `true` 且未设置 `images.provider` 时,这条中继才拥有该路由;显式设置 `images.provider` 后,`/v1/images` 归该 provider 处理,其校验错误按原样返回,不会改由 xAI 重试。 ```json { diff --git a/src/images/artifacts.ts b/src/images/artifacts.ts index 71e53374f3..cb0c77f9df 100644 --- a/src/images/artifacts.ts +++ b/src/images/artifacts.ts @@ -295,6 +295,69 @@ function pickPinnedAddress(addresses: PinnedAddress[]): PinnedAddress { return addresses.find(a => a.family === 4) ?? addresses[0]!; } +/** + * HTTPS-only destination check, public-address resolution, and pinned connect. + * Callers own the !ok / 3xx policy so image vs video error text can stay distinct. + */ +async function connectPublicHttps( + url: string, + options: { + context: string; + signal?: AbortSignal; + pinnedDownload?: PinnedDownloadFn; + maxBytes?: number; + }, +): Promise { + let parsedUrl: URL; + try { parsedUrl = new URL(url); } catch { throw new Error(`${options.context} URL is not valid`); } + if (parsedUrl.protocol !== "https:") { + throw new Error(`${options.context} URL must use HTTPS, got ${parsedUrl.protocol}`); + } + const assessment = assessUrlDestination(url); + if (assessment && assessment.kind !== "public" && assessment.kind !== "hostname") { + throw new Error(`${options.context} URL targets ${assessment.detail}`); + } + const resolved = await resolvePublicAddresses(url, options.context); + const pinned = pickPinnedAddress(resolved.addresses); + const download = options.pinnedDownload ?? ((resource, peer, signal) => + pinnedHttpGet(resource, peer, signal, { + // `maxBytes` is optional in pinnedHttpGet, so forwarding undefined removes the + // cap entirely instead of inheriting a default. Keep the 50 MiB ceiling when a + // caller omits a limit, and honour an explicit tighter one. + maxBytes: options.maxBytes ?? MAX_DOWNLOAD_BYTES, + context: `${options.context} download`, + })); + return download(url, pinned, options.signal); +} + +/** + * Fetch a provider-returned image URL after destination-policy + pinned HTTPS. + * Redirects are not followed: the default pinned GET returns the status, and this + * helper rejects every non-2xx including 3xx. Throws a message that names the + * class of failure (scheme / destination kind / download) without reflecting the + * target URL. + */ +export async function fetchPublicHttpsImage( + url: string, + options?: { + signal?: AbortSignal; + pinnedDownload?: PinnedDownloadFn; + maxBytes?: number; + }, +): Promise { + const resp = await connectPublicHttps(url, { + context: "image", + signal: options?.signal, + pinnedDownload: options?.pinnedDownload, + maxBytes: options?.maxBytes, + }); + if (!resp.ok || (resp.status >= 300 && resp.status < 400)) { + try { await resp.body?.cancel(); } catch { /* ignore */ } + throw new Error("image download failed"); + } + return resp; +} + export async function downloadImageToArtifact( url: string, budget?: ImageBudget, @@ -307,30 +370,11 @@ export async function downloadImageToArtifact( return materializeInlineImage(m[2], budget); } - // SSRF protection: validate the provider-returned URL before fetching. - // Require HTTPS strictly — plain HTTP and all other schemes (ftp, file, …) are rejected. - // Resolve DNS once, then pin that public address for the HTTPS connect (SNI/Host keep - // the original hostname) so a rebinding answer cannot retarget the TCP peer. - let parsedUrl: URL; - try { parsedUrl = new URL(url); } catch { throw new Error("image URL is not valid"); } - if (parsedUrl.protocol !== "https:") { - throw new Error(`image URL must use HTTPS, got ${parsedUrl.protocol}`); - } - // Reject literal private/loopback/link-local/metadata addresses. - const assessment = assessUrlDestination(url); - if (assessment && assessment.kind !== "public" && assessment.kind !== "hostname") { - throw new Error(`image URL targets ${assessment.detail}`); - } - const resolved = await resolvePublicAddresses(url); - const pinned = pickPinnedAddress(resolved.addresses); - const download = options?.pinnedDownload ?? pinnedHttpsGet; - const resp = await download(url, pinned, signal); - if (!resp.ok) { - // Custom `pinnedDownload` seams may still return a failed Response with a - // live body; cancel it so unread error payloads cannot keep the socket warm. - try { await resp.body?.cancel(); } catch { /* ignore */ } - throw new Error("image download failed: " + resp.status); - } + const resp = await fetchPublicHttpsImage(url, { + signal, + pinnedDownload: options?.pinnedDownload, + maxBytes: MAX_DOWNLOAD_BYTES, + }); // Stream the body with a hard byte cap so a missing/lying Content-Length or a // compromised CDN URL cannot exhaust memory before the size check runs. @@ -433,19 +477,11 @@ export async function downloadVideoToArtifact( return dest; } - // SSRF protection: same validation as downloadImageToArtifact - let parsedUrl: URL; - try { parsedUrl = new URL(url); } catch { throw new Error("video URL is not valid"); } - if (parsedUrl.protocol !== "https:") { - throw new Error(`video URL must use HTTPS, got ${parsedUrl.protocol}`); - } - const assessment = assessUrlDestination(url); - if (assessment && assessment.kind !== "public" && assessment.kind !== "hostname") { - throw new Error(`video URL targets ${assessment.detail}`); - } - const resolved = await resolvePublicAddresses(url, "video"); - const pinned = pickPinnedAddress(resolved.addresses); - const resp = await pinnedHttpsGet(url, pinned, signal, { maxBytes: MAX_VIDEO_DOWNLOAD_BYTES }); + const resp = await connectPublicHttps(url, { + context: "video", + signal, + maxBytes: MAX_VIDEO_DOWNLOAD_BYTES, + }); if (!resp.ok) { try { await resp.body?.cancel(); } catch { /* ignore */ } throw new Error("video download failed: " + resp.status); diff --git a/src/images/fulfill.ts b/src/images/fulfill.ts index 5672980854..c3e1bb41e2 100644 --- a/src/images/fulfill.ts +++ b/src/images/fulfill.ts @@ -91,11 +91,16 @@ export async function fulfillImageCall( typeof obj.image_url === "string" ? obj.image_url : typeof obj.image === "string" ? obj.image : undefined; const size = typeof obj.size === "string" ? obj.size : plan.defaultSize; const quality = typeof obj.quality === "string" ? obj.quality : plan.defaultQuality; + // Forward the raw literal and let callXaiImages own validation. Folding "auto" + // to undefined here would make the request look like it carried no ratio at + // all, so the client would derive one from `size` — the opposite of what an + // explicit Auto selection asks for. + const aspectRatio = typeof obj.aspect_ratio === "string" ? obj.aspect_ratio : undefined; let result; try { result = await callXaiImages( - { prompt, model: plan.model, n, imageUrl, size, quality }, + { prompt, model: plan.model, n, imageUrl, size, quality, aspectRatio }, plan.auth, signal, plan.timeoutMs, diff --git a/src/images/index.ts b/src/images/index.ts index f9f0fd5b99..d3562f398b 100644 --- a/src/images/index.ts +++ b/src/images/index.ts @@ -1,4 +1,4 @@ -export { planImageBridge, planVideoBridge, findXaiProvider, resolveXaiImageApiKey } from "./plan"; +export { planImageBridge, planVideoBridge, findXaiProvider, resolveXaiImageApiKey, resolveXaiImageAuthToken } from "./plan"; export { runWithImageBridge, clampImageMaxRounds, DEFAULT_MAX_ROUNDS, MAX_ROUNDS_HARD_LIMIT } from "./loop"; export type { ImageBridgePlan, ImageCallResult, VideoBridgePlan, VideoCallResult } from "./types"; export { buildImageTool, buildVideoTool, extractHostedImageGeneration, IMAGE_GEN_TOOL_NAME, VIDEO_GEN_TOOL_NAME, isImageGenName, isVideoGenName } from "./synthetic-tool"; diff --git a/src/images/plan.ts b/src/images/plan.ts index 9ea2bdcbb2..2a48aae454 100644 --- a/src/images/plan.ts +++ b/src/images/plan.ts @@ -2,6 +2,7 @@ import type { OcxConfig, OcxParsedRequest, OcxProviderConfig } from "../types"; import { toolChoiceToolPredicate } from "../types"; import type { ImageBridgePlan, VideoBridgePlan } from "./types"; import { resolveEnvValue } from "../config"; +import { getValidAccessToken } from "../oauth/index"; import { getProviderRegistryEntry } from "../providers/registry"; import { IMAGE_GEN_TOOL_NAME, VIDEO_GEN_TOOL_NAME, isVideoGenName } from "./synthetic-tool"; @@ -40,6 +41,19 @@ export function resolveXaiImageApiKey(provider: OcxProviderConfig): string | und return apiKey || undefined; } +/** Token for the /v1/images → Imagine relay. OAuth reuses the Grok CLI grant. */ +export async function resolveXaiImageAuthToken(provider: OcxProviderConfig): Promise { + if (provider.authMode === "oauth") { + try { + const token = (await getValidAccessToken("xai"))?.trim(); + return token || undefined; + } catch { + return undefined; + } + } + return resolveXaiImageApiKey(provider); +} + export async function planImageBridge( config: OcxConfig, parsed: OcxParsedRequest, diff --git a/src/images/synthetic-tool.ts b/src/images/synthetic-tool.ts index 84ac536ab3..96979bbb50 100644 --- a/src/images/synthetic-tool.ts +++ b/src/images/synthetic-tool.ts @@ -80,6 +80,11 @@ export function buildImageTool(): OcxTool { properties: { prompt: { type: "string", description: "Detailed image generation prompt. Required." }, n: { type: "integer", minimum: 1, maximum: 4 }, + aspect_ratio: { + type: "string", + enum: ["1:1", "16:9", "9:16", "4:3", "3:4", "auto"], + description: "Image aspect ratio. Default auto.", + }, }, required: ["prompt"], }, diff --git a/src/images/xai-client.ts b/src/images/xai-client.ts index 3695bcde50..ce2c2d6926 100644 --- a/src/images/xai-client.ts +++ b/src/images/xai-client.ts @@ -14,6 +14,8 @@ export interface XaiImageRequest { n?: number; // 1-4 size?: string; quality?: string; + /** Literal xAI aspect_ratio. Wins over `size` when both are present. */ + aspectRatio?: string; imageUrl?: string; // if set → /images/edits } @@ -35,6 +37,25 @@ const XAI_ASPECT_RATIOS: ReadonlyArray = [ ["9:16", 0.5625], ["16:9", 16 / 9], ]; +const XAI_ASPECT_RATIO_LITERALS = new Set(XAI_ASPECT_RATIOS.map(([label]) => label)); + +/** Accept a hosted/Codex `aspect_ratio` literal; `auto` and unknown values drop. */ +export function resolveXaiAspectRatioLiteral(value: unknown): string | undefined { + if (typeof value !== "string") return undefined; + const literal = value.trim(); + if (!literal || literal === "auto") return undefined; + return XAI_ASPECT_RATIO_LITERALS.has(literal) ? literal : undefined; +} + +function resolveAspectRatio(req: XaiImageRequest): string | undefined { + // An explicit aspect_ratio owns the decision even when it resolves to nothing: + // "auto" means "let xAI choose", so falling back to a size-derived ratio would + // silently override the caller. Only an absent field consults `size`. + if (req.aspectRatio !== undefined && req.aspectRatio.trim()) { + return resolveXaiAspectRatioLiteral(req.aspectRatio); + } + return mapSizeToAspectRatio(req.size); +} function mapSizeToAspectRatio(size?: string): string | undefined { if (!size) return undefined; @@ -75,7 +96,7 @@ export async function callXaiImages( prompt: req.prompt, n: req.n ?? 1, }; - const aspectRatio = mapSizeToAspectRatio(req.size); + const aspectRatio = resolveAspectRatio(req); const resolution = mapQualityToResolution(req.quality); if (aspectRatio) body.aspect_ratio = aspectRatio; if (resolution) body.resolution = resolution; @@ -98,8 +119,20 @@ export async function callXaiImages( }, body: JSON.stringify(body), signal: linkedSignal, + // Do not follow 3xx while carrying the xAI bearer. Bun may strip Authorization + // cross-origin but still leave the request on the redirect target. + redirect: "manual", }); + const redirected = resp.type === "opaqueredirect" || (resp.status >= 300 && resp.status < 400); + if (redirected) { + try { await resp.body?.cancel(); } catch { /* ignore */ } + const status = resp.status >= 300 && resp.status < 400 ? resp.status : 302; + const err = new Error("xAI images API returned " + status) as Error & { status: number }; + err.status = status; + throw err; + } + if (!resp.ok) { try { await resp.body?.cancel(); } catch { /* ignore */ } const err = new Error("xAI images API returned " + resp.status) as Error & { status: number }; diff --git a/src/responses/parser.ts b/src/responses/parser.ts index 33de5ebeae..f26539945a 100644 --- a/src/responses/parser.ts +++ b/src/responses/parser.ts @@ -19,7 +19,7 @@ import { compactionItemToText, isCompactionItemType } from "./compaction"; import { previousResponseReplayPrefixLength } from "./state"; import { decodeReasoningEnvelope } from "./reasoning-envelope"; import { extractHostedWebSearch, WEB_SEARCH_TOOL_NAME } from "../web-search/synthetic-tool"; -import { extractHostedImageGeneration, IMAGE_GEN_TOOL_NAME } from "../images/synthetic-tool"; +import { buildImageTool, extractHostedImageGeneration, IMAGE_GEN_TOOL_NAME } from "../images/synthetic-tool"; import { toolSearchDescription, toolSearchParameters } from "./tool-search-compat"; function isObj(v: unknown): v is Record { @@ -165,6 +165,15 @@ function buildTools(tools: unknown[] | undefined): OcxTool[] | undefined { return { ...(isObj(raw) ? raw : {}), type: "object" }; }; const pushFn = (t: Record, namespace?: string) => { + // Hosted image_generation already installed the synthetic root tool. A later + // ordinary root `image_gen` must not create a second un-namespaced identity. + if ( + !namespace + && t.name === IMAGE_GEN_TOOL_NAME + && out.some(tool => tool.name === IMAGE_GEN_TOOL_NAME && !tool.namespace && tool.imageGeneration) + ) { + return; + } const tool: OcxTool = { name: t.name as string, description: (t.description as string) ?? "", @@ -175,6 +184,16 @@ function buildTools(tools: unknown[] | undefined): OcxTool[] | undefined { out.push(tool); }; const pushCustom = (t: Record, namespace?: string) => { + // Hosted image_generation already installed the synthetic root tool. A later + // root custom `image_gen` would collide on the same wire name with a different + // `freeform` flag and throw `ambiguous tool catalog`. + if ( + !namespace + && t.name === IMAGE_GEN_TOOL_NAME + && out.some(tool => tool.name === IMAGE_GEN_TOOL_NAME && !tool.namespace && tool.imageGeneration) + ) { + return; + } // Freeform custom tools are lowered to a single string `input` because chat models cannot // emit Responses grammar payloads directly. Keep tool-specific input guidance scoped to the // tool that owns it: leaking apply_patch syntax into `exec` or another freeform tool teaches @@ -224,6 +243,28 @@ function buildTools(tools: unknown[] | undefined): OcxTool[] | undefined { toolSearch: true, }); } + else if (t.type === "image_generation" || t.type === "image_gen") { + // Keep Codex's image_gen visible to routed chat models. The hosted OpenAI tool + // cannot execute on Grok; the model still has to see a callable image_gen so + // Codex's client-side /v1/images request can fire and be relayed to xAI. + // Identity is the un-namespaced synthetic root (`imageGeneration: true`), not + // the bare name: a namespaced ordinary `image_gen` must not suppress it. + const synthetic = buildImageTool(); + // Every un-namespaced `image_gen` collides on one wire name, so removing only + // the first leaves a second root behind and the catalog stays ambiguous. + // Drop all root collisions, keep namespaced entries, then insert exactly one + // synthetic root — at the earliest colliding position so declaration order is + // preserved for models that read the catalog positionally. + let insertAt = -1; + for (let i = out.length - 1; i >= 0; i -= 1) { + const tool = out[i]!; + if (tool.name !== IMAGE_GEN_TOOL_NAME || tool.namespace) continue; + out.splice(i, 1); + insertAt = i; + } + if (insertAt >= 0) out.splice(insertAt, 0, synthetic); + else out.push(synthetic); + } else if (typeof t.name === "string" && t.type !== "web_search" && t.type !== "image_generation") { // Any OTHER named tool (e.g. a native/computer-use tool type opencodex doesn't explicitly // model) is client-executed — pass it through as a function so the routed model can read and @@ -231,8 +272,7 @@ function buildTools(tools: unknown[] | undefined): OcxTool[] | undefined { // silently dropped, so the model never saw them. pushFn(t); } - // Only the OpenAI-hosted server-side tools (web_search, image_generation) are intentionally - // dropped — they're executed by OpenAI and can't be relayed to a routed chat model. + // Hosted web_search is still dropped here — the web-search sidecar re-injects it. } return out.length > 0 ? out : undefined; } diff --git a/src/server/images.ts b/src/server/images.ts index 5e65a1a170..ade4c8348e 100644 --- a/src/server/images.ts +++ b/src/server/images.ts @@ -37,7 +37,15 @@ import { getValidAccessToken, getOAuthCredentialProjectId } from "../oauth/index import { safeAntigravityHttpErrorMessage } from "../adapters/google-errors"; import { sanitizeUpstreamErrorText } from "../adapters/upstream-http-error"; import { ANTIGRAVITY_REQUEST_UA } from "../adapters/google-antigravity-wire"; -import { decodeValidatedImageBase64, MAX_ENCODED_BYTES_PER_IMAGE } from "../images/artifacts"; +import { + decodeValidatedImageBase64, + fetchPublicHttpsImage, + MAX_ENCODED_BYTES_PER_IMAGE, + sniffImageExtension, + type PinnedDownloadFn, +} from "../images/artifacts"; +import { findXaiProvider, resolveXaiImageAuthToken } from "../images/plan"; +import { callXaiImages } from "../images/xai-client"; import type { AdmissionLease } from "../lib/admission"; import { codexAccountSelectionForTurn } from "./lifecycle"; @@ -49,7 +57,8 @@ const IMAGES_UPSTREAM_TIMEOUT_MS = 300_000; /** * Cap for the buffered upstream response body (100 MiB). Images responses are JSON documents * containing base64-encoded images — typically a few MB. This prevents an oversized or malicious - * response from exhausting process memory. + * response from exhausting process memory. The xAI `/v1/images` relay also uses this as the + * combined decoded-byte and base64-encoded output budget across the whole batch. */ export const IMAGES_RESPONSE_MAX_BYTES = 100 * 1024 * 1024; @@ -118,6 +127,57 @@ const CCA_BLOCKING_FINISH_REASONS: ReadonlySet = new Set([ "RECITATION", ]); +function decodedBytesFromBase64(encoded: string): number { + const padding = encoded.endsWith("==") ? 2 : encoded.endsWith("=") ? 1 : 0; + return Math.max(0, Math.floor((encoded.length * 3) / 4) - padding); +} + +/** Largest decoded payload whose base64 form still fits in `remainingEncoded`. */ +function remainingRelayDecodedBytes(spentDecoded: number, spentEncoded: number): number { + const remainingDecoded = IMAGES_RESPONSE_MAX_BYTES - spentDecoded; + const remainingEncoded = IMAGES_RESPONSE_MAX_BYTES - spentEncoded; + if (remainingDecoded <= 0 || remainingEncoded < 4) return 0; + return Math.max(0, Math.min(remainingDecoded, 3 * Math.floor(remainingEncoded / 4))); +} + +function wouldExceedRelayBudget( + spentDecoded: number, + spentEncoded: number, + decodedBytes: number, + encodedBytes: number, +): boolean { + return spentDecoded + decodedBytes > IMAGES_RESPONSE_MAX_BYTES + || spentEncoded + encodedBytes > IMAGES_RESPONSE_MAX_BYTES; +} + +function xaiImageOutputTooLarge(): Response { + return formatErrorResponse( + 502, + "upstream_error", + `xAI image generation output too large (exceeded ${IMAGES_RESPONSE_MAX_BYTES} bytes)`, + ); +} + +function xaiImageDownloadFailed(): Response { + return formatErrorResponse(502, "upstream_error", "xAI image download failed"); +} + +function xaiImageAuthMissing(): Response { + return formatErrorResponse( + 400, + "invalid_request_error", + "xAI Imagine relay is enabled but no usable Grok CLI OAuth token or xAI API key was found. " + + "Run `ocx login xai` or set an xAI API key. The request was not forwarded to ChatGPT.", + ); +} + +/** Test seam: inject a pinned HTTPS GET so relay tests never open a real socket. */ +let xaiResultPinnedDownload: PinnedDownloadFn | undefined; + +export function setXaiResultPinnedDownloadForTests(fn: PinnedDownloadFn | undefined): void { + xaiResultPinnedDownload = fn; +} + /** * Race a promise against an abort signal. If the signal aborts first, reject * immediately — our code stops awaiting the underlying operation even though @@ -372,6 +432,166 @@ async function tryCcaImageGeneration( } } +/** + * Codex's client-side image_gen POSTs here. When the Grok Imagine bridge is + * opted in, send that request to api.x.ai instead of ChatGPT. + */ +async function tryXaiImageRelay( + body: unknown, + config: OcxConfig, + logCtx: RequestLogContext, + signal: AbortSignal | undefined, + endpoint: ImagesEndpoint, +): Promise { + if (config.images?.bridgeEnabled !== true) return undefined; + const found = findXaiProvider(config); + if (!found) return undefined; + const obj = body && typeof body === "object" && !Array.isArray(body) + ? body as Record + : {}; + const prompt = typeof obj.prompt === "string" ? obj.prompt + : typeof obj.input === "string" ? obj.input + : ""; + if (!prompt.trim()) { + return formatErrorResponse(400, "invalid_request_error", "image generation requires a prompt"); + } + const n = typeof obj.n === "number" && Number.isFinite(obj.n) ? Math.max(1, Math.min(4, Math.floor(obj.n))) : 1; + const size = typeof obj.size === "string" ? obj.size : undefined; + const quality = typeof obj.quality === "string" ? obj.quality : undefined; + const aspectRatio = typeof obj.aspect_ratio === "string" ? obj.aspect_ratio : undefined; + let imageUrl: string | undefined; + if (endpoint === "edits") { + const images = obj.images; + const first = Array.isArray(images) ? images[0] : undefined; + if (typeof obj.image === "string") imageUrl = obj.image; + else if (typeof obj.image_url === "string") imageUrl = obj.image_url; + else if (first && typeof first === "object" && first !== null) { + const rec = first as Record; + if (typeof rec.image_url === "string") imageUrl = rec.image_url; + else if (typeof rec.url === "string") imageUrl = rec.url; + } + if (!imageUrl?.trim()) { + return formatErrorResponse(400, "invalid_request_error", "image edits require an image URL"); + } + imageUrl = imageUrl.trim(); + } + const timeoutMs = config.images?.timeoutMs ?? IMAGES_UPSTREAM_TIMEOUT_MS; + const linkedSignal = signalWithTimeout(timeoutMs, signal); + try { + let token: string | undefined; + try { + token = await abortableRace(resolveXaiImageAuthToken(found.provider), linkedSignal.signal); + } catch { + if (signal?.aborted) { + return formatErrorResponse(499, "client_closed_request", `image ${endpoint} request canceled by client`); + } + if (linkedSignal.signal.aborted) { + return formatErrorResponse(504, "upstream_error", `xAI image ${endpoint} timed out during authentication`); + } + return xaiImageAuthMissing(); + } + if (!token) return xaiImageAuthMissing(); + logCtx.provider = "xai"; + logCtx.model = config.images?.bridgeModel ?? "grok-imagine-image-quality"; + const result = await callXaiImages( + { + prompt, + model: logCtx.model, + n, + size, + quality, + aspectRatio, + imageUrl, + }, + { baseUrl: "https://api.x.ai/v1", token }, + linkedSignal.signal, + timeoutMs, + ); + const data: Array<{ b64_json: string }> = []; + let spentDecoded = 0; + let spentEncoded = 0; + for (const img of result.images) { + if (typeof img.b64_json === "string" && img.b64_json) { + const encodedBytes = img.b64_json.length; + if (encodedBytes > MAX_ENCODED_BYTES_PER_IMAGE) { + return formatErrorResponse(502, "upstream_error", "xAI image payload exceeds per-image size cap"); + } + try { + decodeValidatedImageBase64(img.b64_json); + } catch { + return formatErrorResponse(502, "upstream_error", "xAI image payload failed base64/magic validation"); + } + const decodedBytes = decodedBytesFromBase64(img.b64_json); + if (wouldExceedRelayBudget(spentDecoded, spentEncoded, decodedBytes, encodedBytes)) { + return xaiImageOutputTooLarge(); + } + data.push({ b64_json: img.b64_json }); + spentDecoded += decodedBytes; + spentEncoded += encodedBytes; + continue; + } + if (typeof img.url !== "string" || !img.url) continue; + const remaining = remainingRelayDecodedBytes(spentDecoded, spentEncoded); + if (remaining <= 0) return xaiImageOutputTooLarge(); + let fetched: Response; + try { + fetched = await fetchPublicHttpsImage(img.url, { + signal: linkedSignal.signal, + pinnedDownload: xaiResultPinnedDownload, + maxBytes: remaining, + }); + } catch (err) { + if (signal?.aborted || linkedSignal.signal.aborted) throw err; + return xaiImageDownloadFailed(); + } + const observed = await readImageResponseBytes(fetched, { + maxBytes: remaining, + signal: linkedSignal.signal, + }); + if (observed.oversized) return xaiImageOutputTooLarge(); + if (observed.bytes.byteLength === 0) continue; + if (!sniffImageExtension(observed.bytes)) return xaiImageDownloadFailed(); + const decodedBytes = observed.bytes.byteLength; + const b64 = Buffer.from(observed.bytes).toString("base64"); + if (wouldExceedRelayBudget(spentDecoded, spentEncoded, decodedBytes, b64.length)) { + return xaiImageOutputTooLarge(); + } + data.push({ b64_json: b64 }); + spentDecoded += decodedBytes; + spentEncoded += b64.length; + } + if (data.length === 0) { + return formatErrorResponse(502, "upstream_error", "xAI image generation returned no usable images"); + } + return new Response(JSON.stringify({ created: Math.floor(Date.now() / 1000), data }), { + status: 200, + headers: { "content-type": "application/json" }, + }); + } catch (err) { + if (signal?.aborted) { + return formatErrorResponse(499, "client_closed_request", `image ${endpoint} request canceled by client`); + } + if (linkedSignal.signal.aborted || (err instanceof Error && err.name === "TimeoutError")) { + return formatErrorResponse(504, "upstream_error", `xAI image ${endpoint} timed out`); + } + const status = typeof err === "object" && err && "status" in err && typeof (err as { status: unknown }).status === "number" + ? (err as { status: number }).status + : 502; + const message = err instanceof Error ? err.message : String(err); + const safeMessage = sanitizeUpstreamErrorText(message).replace( + /https?:\/\/[^\s"'<>]+/gi, + "[upstream-url]", + ); + return formatErrorResponse( + status >= 400 && status < 600 ? status : 502, + "upstream_error", + `xAI image ${endpoint} failed: ${safeMessage}`, + ); + } finally { + linkedSignal.cleanup(); + } +} + export async function handleImages( req: Request, config: OcxConfig, @@ -379,7 +599,22 @@ export async function handleImages( logCtx: RequestLogContext, turnAdmissionLease?: AdmissionLease, ): Promise { + let body: unknown; + try { + body = await readJsonRequestBody(req); + } catch (err) { + return decodeRequestErrorResponse(err, "images"); + } + const model = (body as { model?: unknown } | null)?.model; + if (typeof model === "string" && model) logCtx.model = model; + const candidates = selectImagesProvider(config); + // Explicit images.provider owns the route, including its validation errors. + // Do not divert that selection to the xAI Imagine relay. + if (config.images?.provider === undefined) { + const xaiRelay = await tryXaiImageRelay(body, config, logCtx, req.signal, endpoint); + if (xaiRelay) return xaiRelay; + } if (candidates.error) { return formatErrorResponse(400, "invalid_request_error", candidates.error); } @@ -398,14 +633,6 @@ export async function handleImages( } } } - let body: unknown; - try { - body = await readJsonRequestBody(req); - } catch (err) { - return decodeRequestErrorResponse(err, "images"); - } - const model = (body as { model?: unknown } | null)?.model; - if (typeof model === "string" && model) logCtx.model = model; const canUseOpenAiForward = !skipOpenAiForwardForAdmissionBearer && candidates.forwardCandidates.length > 0; diff --git a/tests/credential-redirect-guard.test.ts b/tests/credential-redirect-guard.test.ts index 783ca2dac8..e8fcef70bb 100644 --- a/tests/credential-redirect-guard.test.ts +++ b/tests/credential-redirect-guard.test.ts @@ -58,6 +58,7 @@ describe("Bun forwards nonstandard headers across a redirect", () => { describe("credential-bearing sidecars refuse to follow redirects", () => { const sites: Array<{ file: string; label: string }> = [ { file: "../src/server/images.ts", label: "images relay" }, + { file: "../src/images/xai-client.ts", label: "xAI images client" }, { file: "../src/server/live.ts", label: "live relay" }, { file: "../src/server/search.ts", label: "search relay" }, { file: "../src/web-search/executor.ts", label: "web-search sidecar" }, diff --git a/tests/images/download-cap-default.test.ts b/tests/images/download-cap-default.test.ts new file mode 100644 index 0000000000..77fce8fd98 --- /dev/null +++ b/tests/images/download-cap-default.test.ts @@ -0,0 +1,48 @@ +import { describe, expect, mock, test } from "bun:test"; + +// The default downloader inside connectPublicHttps used to forward `maxBytes: undefined` +// to pinnedHttpGet, whose cap is optional — so a caller that omitted a limit removed the +// byte ceiling entirely instead of inheriting MAX_DOWNLOAD_BYTES. Both production callers +// happen to pass an explicit limit today, which is why the existing suites (they all +// inject `pinnedDownload` and bypass the default path) could not see it. + +const lookupMock = mock(async (): Promise<{ address: string; family: number }[]> => [ + { address: "93.184.216.34", family: 4 }, +]); +mock.module("node:dns/promises", () => ({ lookup: lookupMock })); + +const MIN_PNG = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]); +const seenMaxBytes: Array = []; + +mock.module("../../src/lib/pinned-http", () => ({ + pinnedHttpGet: async ( + _url: string, + _pinned: unknown, + _signal?: AbortSignal, + options?: { maxBytes?: number }, + ) => { + seenMaxBytes.push(options?.maxBytes); + return new Response(MIN_PNG, { status: 200 }); + }, + PinnedHttpError: class extends Error {}, +})); + +const { fetchPublicHttpsImage, MAX_DOWNLOAD_BYTES } = await import( + `../../src/images/artifacts?cap=${Date.now()}` +); + +describe("default image downloader byte cap", () => { + test("an omitted maxBytes inherits MAX_DOWNLOAD_BYTES instead of removing the cap", async () => { + seenMaxBytes.length = 0; + const resp = await fetchPublicHttpsImage("https://public-host/image.png"); + expect(resp.status).toBe(200); + expect(seenMaxBytes).toEqual([MAX_DOWNLOAD_BYTES]); + expect(seenMaxBytes[0]).not.toBeUndefined(); + }); + + test("an explicit tighter limit is preserved", async () => { + seenMaxBytes.length = 0; + await fetchPublicHttpsImage("https://public-host/image.png", { maxBytes: 1024 }); + expect(seenMaxBytes).toEqual([1024]); + }); +}); diff --git a/tests/images/synthetic-tool.test.ts b/tests/images/synthetic-tool.test.ts index 24e1fc9311..e882462010 100644 --- a/tests/images/synthetic-tool.test.ts +++ b/tests/images/synthetic-tool.test.ts @@ -83,4 +83,11 @@ describe("buildImageTool", () => { expect(tool.name).toBe("image_gen"); expect(tool.imageGeneration).toBe(true); }); + + test("exposes aspect_ratio literals including auto", () => { + const tool = buildImageTool(); + const aspect = (tool.parameters as { properties?: { aspect_ratio?: { enum?: string[] } } }) + .properties?.aspect_ratio; + expect(aspect?.enum).toEqual(["1:1", "16:9", "9:16", "4:3", "3:4", "auto"]); + }); }); diff --git a/tests/images/xai-client.test.ts b/tests/images/xai-client.test.ts index cec8f055e4..e544587b7a 100644 --- a/tests/images/xai-client.test.ts +++ b/tests/images/xai-client.test.ts @@ -2,7 +2,7 @@ import { afterAll, afterEach, beforeAll, describe, expect, test } from "bun:test import { tmpdir } from "node:os"; import { join } from "node:path"; import { randomUUID } from "node:crypto"; -import { callXaiImages } from "../../src/images/xai-client"; +import { callXaiImages, resolveXaiAspectRatioLiteral } from "../../src/images/xai-client"; const PREV_HOME = process.env.OPENCODEX_HOME; beforeAll(() => { process.env.OPENCODEX_HOME = join(tmpdir(), "ocx-test-" + randomUUID()); }); @@ -48,6 +48,20 @@ describe("callXaiImages", () => { expect(body.n).toBe(3); }); + test("3xx is not followed and throws with the redirect status", async () => { + const calls: { url: string; init?: RequestInit }[] = []; + globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { + calls.push({ url: String(input), init }); + return new Response(null, { + status: 302, + headers: { location: "https://evil.example/images/generations" }, + }); + }) as typeof fetch; + await expect(callXaiImages({ prompt: "x" }, AUTH)).rejects.toThrow("302"); + expect(calls).toHaveLength(1); + expect(calls[0]!.init?.redirect).toBe("manual"); + }); + test("non-2xx → throws Error containing status code", async () => { stubFetch(429, { error: "rate limited" }); await expect(callXaiImages({ prompt: "x" }, AUTH)).rejects.toThrow("429"); @@ -158,4 +172,49 @@ describe("callXaiImages", () => { expect(body).not.toHaveProperty("size"); expect(body).not.toHaveProperty("quality"); }); + + test("explicit aspect_ratio is forwarded and wins over size", async () => { + const calls = stubFetch(200, { data: [{ b64_json: "dGVzdA==" }] }); + await callXaiImages({ prompt: "x", size: "1024x1024", aspectRatio: "16:9" }, AUTH); + const body = JSON.parse((calls[0]!.init?.body as string) ?? "{}"); + expect(body.aspect_ratio).toBe("16:9"); + expect(body).not.toHaveProperty("size"); + }); + + test("aspect_ratio literals come from the size-mapping table", () => { + expect(resolveXaiAspectRatioLiteral("3:4")).toBe("3:4"); + expect(resolveXaiAspectRatioLiteral("4:3")).toBe("4:3"); + expect(resolveXaiAspectRatioLiteral("auto")).toBeUndefined(); + expect(resolveXaiAspectRatioLiteral("2:1")).toBeUndefined(); + }); + + test("aspect_ratio auto and illegal values are dropped", async () => { + const autoCalls = stubFetch(200, { data: [{ b64_json: "dGVzdA==" }] }); + await callXaiImages({ prompt: "x", aspectRatio: "auto" }, AUTH); + expect(JSON.parse((autoCalls[0]!.init?.body as string) ?? "{}")).not.toHaveProperty("aspect_ratio"); + + const badCalls = stubFetch(200, { data: [{ b64_json: "dGVzdA==" }] }); + await callXaiImages({ prompt: "x", aspectRatio: "2:1" }, AUTH); + expect(JSON.parse((badCalls[0]!.init?.body as string) ?? "{}")).not.toHaveProperty("aspect_ratio"); + }); + + test("an explicit auto suppresses the size-derived ratio instead of falling back", async () => { + const calls = stubFetch(200, { data: [{ b64_json: "dGVzdA==" }] }); + await callXaiImages({ prompt: "x", size: "1792x1024", aspectRatio: "auto" }, AUTH); + const body = JSON.parse((calls[0]!.init?.body as string) ?? "{}"); + // "1792x1024" would map to 16:9 if the explicit auto were treated as absent. + expect(body).not.toHaveProperty("aspect_ratio"); + }); + + test("an unknown explicit literal does not fall back to the size mapping either", async () => { + const calls = stubFetch(200, { data: [{ b64_json: "dGVzdA==" }] }); + await callXaiImages({ prompt: "x", size: "1024x1024", aspectRatio: "2:1" }, AUTH); + expect(JSON.parse((calls[0]!.init?.body as string) ?? "{}")).not.toHaveProperty("aspect_ratio"); + }); + + test("an absent aspect_ratio still derives the ratio from size", async () => { + const calls = stubFetch(200, { data: [{ b64_json: "dGVzdA==" }] }); + await callXaiImages({ prompt: "x", size: "1792x1024" }, AUTH); + expect(JSON.parse((calls[0]!.init?.body as string) ?? "{}").aspect_ratio).toBe("16:9"); + }); }); diff --git a/tests/images/z-fulfill.test.ts b/tests/images/z-fulfill.test.ts index da321cbc68..646ce1bf7a 100644 --- a/tests/images/z-fulfill.test.ts +++ b/tests/images/z-fulfill.test.ts @@ -22,6 +22,12 @@ beforeAll(async () => { if (xaiError) throw xaiError; return xaiResult; }, + resolveXaiAspectRatioLiteral: (value: unknown) => { + if (typeof value !== "string") return undefined; + const literal = value.trim(); + if (!literal || literal === "auto") return undefined; + return new Set(["1:1", "16:9", "9:16", "4:3", "3:4"]).has(literal) ? literal : undefined; + }, })); mock.module("../../src/images/artifacts", () => ({ createImageBudget: () => ({ spent: 0 }), @@ -198,6 +204,44 @@ describe("fulfillImageCall", () => { expect(xaiCalls[0]!.n).toBe(4); }); + test("forwards an aspect_ratio literal to callXaiImages", async () => { + reset(); + await fulfillImageCall( + { id: "c1", name: "image_gen", arguments: JSON.stringify({ prompt: "x", aspect_ratio: "16:9", size: "1024x1024" }) }, + plan, { spent: 0 }, + ); + expect(xaiCalls[0]!.aspectRatio).toBe("16:9"); + }); + + test("forwards auto verbatim so the client can suppress the size-derived ratio", async () => { + reset(); + await fulfillImageCall( + { id: "c1", name: "image_gen", arguments: JSON.stringify({ prompt: "x", aspect_ratio: "auto", size: "1792x1024" }) }, + plan, { spent: 0 }, + ); + // Folding "auto" to undefined here would make the request indistinguishable from + // one that never carried the field, and callXaiImages would derive 16:9 from size. + expect(xaiCalls[0]!.aspectRatio).toBe("auto"); + }); + + test("an illegal literal is still forwarded for the client to reject", async () => { + reset(); + await fulfillImageCall( + { id: "c1", name: "image_gen", arguments: JSON.stringify({ prompt: "x", aspect_ratio: "2:1" }) }, + plan, { spent: 0 }, + ); + expect(xaiCalls[0]!.aspectRatio).toBe("2:1"); + }); + + test("a non-string aspect_ratio is dropped before the client sees it", async () => { + reset(); + await fulfillImageCall( + { id: "c1", name: "image_gen", arguments: JSON.stringify({ prompt: "x", aspect_ratio: 169 }) }, + plan, { spent: 0 }, + ); + expect(xaiCalls[0]!.aspectRatio).toBeUndefined(); + }); + test("forwards imageUrl from image_url arg", async () => { reset(); await fulfillImageCall( diff --git a/tests/responses-parser.test.ts b/tests/responses-parser.test.ts index f236a6f2e5..a67c167439 100644 --- a/tests/responses-parser.test.ts +++ b/tests/responses-parser.test.ts @@ -366,6 +366,117 @@ describe("Responses parser", () => { expect(parsed._imageGeneration?.toolNames.has("image_generation")).toBe(true); expect(parsed.options.toolChoice).toEqual({ name: "image_gen" }); + expect(parsed.context.tools?.some( + tool => tool.name === "image_gen" && tool.imageGeneration === true, + )).toBe(true); + }); + + test("namespaced ordinary image_gen does not suppress the synthetic root image tool", () => { + const parsed = parseRequest({ + model: "grok-4.6", + input: "draw a cat", + tools: [ + { + type: "namespace", + name: "mcp_pack", + tools: [{ type: "function", name: "image_gen", parameters: { type: "object" } }], + }, + { type: "image_generation" }, + ], + }); + + const tools = parsed.context.tools ?? []; + const namespaced = tools.find(tool => tool.name === "image_gen" && tool.namespace === "mcp_pack"); + const synthetic = tools.find(tool => tool.name === "image_gen" && !tool.namespace); + expect(namespaced).toBeDefined(); + expect(namespaced?.imageGeneration).toBeUndefined(); + expect(synthetic?.imageGeneration).toBe(true); + }); + + test("hosted image_generation then a root ordinary image_gen keeps one synthetic tool", () => { + const parsed = parseRequest({ + model: "grok-4.6", + input: "draw a cat", + tools: [ + { type: "image_generation" }, + { type: "function", name: "image_gen", parameters: { type: "object" } }, + ], + }); + + const root = (parsed.context.tools ?? []).filter(tool => tool.name === "image_gen" && !tool.namespace); + expect(root).toHaveLength(1); + expect(root[0]?.imageGeneration).toBe(true); + }); + + test("hosted image_generation then a root custom image_gen keeps one synthetic tool", () => { + const parsed = parseRequest({ + model: "grok-4.6", + input: "draw a cat", + tools: [ + { type: "image_generation" }, + { type: "custom", name: "image_gen" }, + ], + }); + + const root = (parsed.context.tools ?? []).filter(tool => tool.name === "image_gen" && !tool.namespace); + expect(root).toHaveLength(1); + expect(root[0]?.imageGeneration).toBe(true); + expect(root[0]?.freeform).toBeUndefined(); + }); + + test("both root declarations before hosted image_generation collapse to one synthetic tool", () => { + // Reverse order of the two cases above. Removing only the first colliding root + // left the second behind, so the catalog stayed ambiguous on one wire name. + const parsed = parseRequest({ + model: "grok-4.6", + input: "draw a cat", + tools: [ + { type: "function", name: "image_gen", parameters: { type: "object" } }, + { type: "custom", name: "image_gen" }, + { type: "image_generation" }, + ], + }); + + const root = (parsed.context.tools ?? []).filter(tool => tool.name === "image_gen" && !tool.namespace); + expect(root).toHaveLength(1); + expect(root[0]?.imageGeneration).toBe(true); + expect(root[0]?.freeform).toBeUndefined(); + }); + + test("a root image_gen on each side of hosted image_generation still collapses", () => { + const parsed = parseRequest({ + model: "grok-4.6", + input: "draw a cat", + tools: [ + { type: "function", name: "image_gen", parameters: { type: "object" } }, + { type: "image_generation" }, + { type: "custom", name: "image_gen" }, + ], + }); + + const root = (parsed.context.tools ?? []).filter(tool => tool.name === "image_gen" && !tool.namespace); + expect(root).toHaveLength(1); + expect(root[0]?.imageGeneration).toBe(true); + }); + + test("a namespaced image_gen survives the root collapse", () => { + const parsed = parseRequest({ + model: "grok-4.6", + input: "draw a cat", + tools: [ + { type: "function", name: "image_gen", parameters: { type: "object" } }, + { + type: "namespace", + name: "mcp_pack", + tools: [{ type: "function", name: "image_gen", parameters: { type: "object" } }], + }, + { type: "image_generation" }, + ], + }); + + const tools = parsed.context.tools ?? []; + expect(tools.filter(tool => tool.name === "image_gen" && !tool.namespace)).toHaveLength(1); + expect(tools.find(tool => tool.name === "image_gen" && tool.namespace === "mcp_pack")).toBeDefined(); }); test("preserves requested service_tier for request logging", () => { diff --git a/tests/server-images.test.ts b/tests/server-images.test.ts index 9499de4d85..e7986e3679 100644 --- a/tests/server-images.test.ts +++ b/tests/server-images.test.ts @@ -12,7 +12,8 @@ import { clearCodexUpstreamHealth, clearThreadAccountMap, getCodexUpstreamHealth import { saveConfig } from "../src/config"; import { selectImagesProvider } from "../src/providers/openai-sidecar"; import { startServer } from "../src/server"; -import { handleImages, IMAGES_RESPONSE_MAX_BYTES, readImageResponseBytes } from "../src/server/images"; +import { handleImages, IMAGES_RESPONSE_MAX_BYTES, readImageResponseBytes, setXaiResultPinnedDownloadForTests } from "../src/server/images"; +import { MAX_ENCODED_BYTES_PER_IMAGE } from "../src/images/artifacts"; import { saveCredential } from "../src/oauth/store"; import type { OcxConfig } from "../src/types"; import { ANTIGRAVITY_REQUEST_UA } from "../src/adapters/google-antigravity-wire"; @@ -42,6 +43,7 @@ beforeEach(() => { }); afterEach(() => { + setXaiResultPinnedDownloadForTests(undefined); globalThis.fetch = originalFetch; if (previousApiToken === undefined) delete process.env.OPENCODEX_API_AUTH_TOKEN; else process.env.OPENCODEX_API_AUTH_TOKEN = previousApiToken; @@ -155,6 +157,403 @@ test("image response byte reader enforces the stream cap when Content-Length is expect(tailPulled).toBe(false); }); +function xaiBridgeConfig(): OcxConfig { + return { + ...forwardConfig(), + images: { bridgeEnabled: true }, + providers: { + ...forwardConfig().providers, + xai: { adapter: "openai-chat", baseUrl: "https://api.x.ai/v1", apiKey: "xai-test-token" }, + }, + } as OcxConfig; +} + +function stubXaiImagine(payload: unknown): CapturedRequest[] { + const captured: CapturedRequest[] = []; + globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { + const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url; + const parsed = new URL(url); + if (parsed.hostname === "127.0.0.1" || parsed.hostname === "localhost") { + return originalFetch(input, init); + } + captured.push({ + path: parsed.pathname, + headers: new Headers(init?.headers), + body: init?.body ? JSON.parse(String(init.body)) : undefined, + }); + if (parsed.hostname === "api.x.ai") { + return Response.json(payload); + } + throw new Error(`unexpected upstream ${parsed.host}`); + }) as typeof fetch; + return captured; +} + +test("POST /v1/images/generations relays to xAI Imagine when the image bridge is enabled", async () => { + const captured = stubXaiImagine({ data: [{ b64_json: CCA_TINY_PNG }] }); + saveConfig(xaiBridgeConfig()); + + const server = startServer(0); + try { + const response = await fetch(new URL("/v1/images/generations", server.url), { + method: "POST", + headers: { "content-type": "application/json", authorization: `Bearer ${DIRECT_CHATGPT_TOKEN}` }, + body: JSON.stringify({ prompt: "a train", model: "gpt-image-2", size: "1024x1024" }), + }); + expect(response.status).toBe(200); + const json = await response.json() as { data?: Array<{ b64_json?: string }> }; + expect(json.data?.[0]?.b64_json).toBe(CCA_TINY_PNG); + expect(captured).toHaveLength(1); + expect(captured[0]!.path).toBe("/v1/images/generations"); + expect(JSON.stringify(captured[0]!.body)).toContain("grok-imagine-image-quality"); + expect(captured[0]!.headers.get("authorization")).toBe("Bearer xai-test-token"); + expect(captured[0]!.headers.get("chatgpt-account-id")).toBeNull(); + expect(captured[0]!.headers.get("session_id")).toBeNull(); + expect(captured[0]!.headers.get("x-codex-turn-metadata")).toBeNull(); + } finally { + await server.stop(true); + } +}); + +test("POST /v1/images/generations with an empty prompt does not resolve xAI auth", async () => { + const captured = stubXaiImagine({ data: [{ b64_json: CCA_TINY_PNG }] }); + saveConfig(xaiBridgeConfig()); + + const server = startServer(0); + try { + const response = await fetch(new URL("/v1/images/generations", server.url), { + method: "POST", + headers: { "content-type": "application/json", authorization: `Bearer ${DIRECT_CHATGPT_TOKEN}` }, + body: JSON.stringify({ prompt: " ", model: "gpt-image-2" }), + }); + expect(response.status).toBe(400); + const json = await response.json() as { error?: { message?: string } }; + expect(json.error?.message).toContain("image generation requires a prompt"); + expect(captured).toHaveLength(0); + } finally { + await server.stop(true); + } +}); + +test("POST /v1/images/generations relays with Grok OAuth and no ChatGPT headers", async () => { + const captured = stubXaiImagine({ data: [{ b64_json: CCA_TINY_PNG }] }); + saveConfig({ + ...forwardConfig(), + images: { bridgeEnabled: true }, + providers: { + ...forwardConfig().providers, + xai: { adapter: "openai-chat", baseUrl: "https://api.x.ai/v1", authMode: "oauth" }, + }, + } as OcxConfig); + await saveCredential("xai", { + access: "xai-oauth-token", + refresh: "xai-refresh", + expires: Date.now() + 10 * 60_000, + source: "oauth", + }); + + const server = startServer(0); + try { + const response = await fetch(new URL("/v1/images/generations", server.url), { + method: "POST", + headers: { + "content-type": "application/json", + authorization: `Bearer ${DIRECT_CHATGPT_TOKEN}`, + "chatgpt-account-id": "acct-123", + "session_id": "sess-9", + "x-codex-turn-metadata": "meta", + }, + body: JSON.stringify({ prompt: "a train", model: "gpt-image-2" }), + }); + expect(response.status).toBe(200); + expect(captured).toHaveLength(1); + expect(captured[0]!.path).toBe("/v1/images/generations"); + expect(captured[0]!.headers.get("authorization")).toBe("Bearer xai-oauth-token"); + expect(captured[0]!.headers.get("chatgpt-account-id")).toBeNull(); + expect(captured[0]!.headers.get("session_id")).toBeNull(); + expect(captured[0]!.headers.get("x-codex-turn-metadata")).toBeNull(); + } finally { + await server.stop(true); + } +}); + +test("POST /v1/images/generations does not reach api.x.ai when OAuth token resolution fails", async () => { + const captured = stubXaiImagine({ data: [{ b64_json: CCA_TINY_PNG }] }); + saveConfig({ + ...forwardConfig(), + images: { bridgeEnabled: true }, + providers: { + openai: { ...disabledOpenAiProvider }, + xai: { adapter: "openai-chat", baseUrl: "https://api.x.ai/v1", authMode: "oauth" }, + }, + } as OcxConfig); + + const server = startServer(0); + try { + const response = await fetch(new URL("/v1/images/generations", server.url), { + method: "POST", + headers: { "content-type": "application/json", authorization: `Bearer ${DIRECT_CHATGPT_TOKEN}` }, + body: JSON.stringify({ prompt: "a train", model: "gpt-image-2" }), + }); + expect(response.status).toBe(400); + const json = await response.json() as { error?: { message?: string } }; + expect(json.error?.message).toContain("ocx login xai"); + expect(captured.filter(call => call.path.includes("/images/"))).toHaveLength(0); + } finally { + await server.stop(true); + } +}); + +test("POST /v1/images/generations does not bill ChatGPT when Imagine is opted in without a Grok token", async () => { + const chatgptCaptured: CapturedRequest[] = []; + const upstream = fakeImagesUpstream(chatgptCaptured); + const innerFetch = globalThis.fetch; + const xaiCaptured: CapturedRequest[] = []; + globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { + const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url; + const parsed = new URL(url); + if (parsed.hostname === "api.x.ai") { + xaiCaptured.push({ + path: parsed.pathname, + headers: new Headers(init?.headers), + body: init?.body ? JSON.parse(String(init.body)) : undefined, + }); + return Response.json({ data: [{ b64_json: CCA_TINY_PNG }] }); + } + return innerFetch(input, init); + }) as typeof fetch; + + saveConfig({ + ...forwardConfig(), + images: { bridgeEnabled: true }, + providers: { + ...forwardConfig().providers, + xai: { adapter: "openai-chat", baseUrl: "https://api.x.ai/v1", authMode: "oauth" }, + }, + } as OcxConfig); + + const server = startServer(0); + try { + const response = await fetch(new URL("/v1/images/generations", server.url), { + method: "POST", + headers: { "content-type": "application/json", authorization: `Bearer ${DIRECT_CHATGPT_TOKEN}` }, + body: JSON.stringify({ prompt: "a train", model: "gpt-image-2" }), + }); + expect(response.status).toBe(400); + const json = await response.json() as { error?: { message?: string } }; + expect(json.error?.message).toContain("ocx login xai"); + expect(json.error?.message).toContain("not forwarded to ChatGPT"); + expect(xaiCaptured).toHaveLength(0); + expect(chatgptCaptured).toHaveLength(0); + } finally { + await server.stop(true); + await upstream.stop(true); + } +}); + +test("POST /v1/images/generations rejects oversized xAI inline payloads", async () => { + const oversized = "A".repeat(MAX_ENCODED_BYTES_PER_IMAGE + 4); + stubXaiImagine({ data: [{ b64_json: oversized }] }); + saveConfig(xaiBridgeConfig()); + + const server = startServer(0); + try { + const response = await fetch(new URL("/v1/images/generations", server.url), { + method: "POST", + headers: { "content-type": "application/json", authorization: `Bearer ${DIRECT_CHATGPT_TOKEN}` }, + body: JSON.stringify({ prompt: "a train", model: "gpt-image-2" }), + }); + expect(response.status).toBe(502); + const json = await response.json() as { error?: { message?: string } }; + expect(json.error?.message).toContain("per-image size cap"); + } finally { + await server.stop(true); + } +}); + +test("POST /v1/images/generations rejects invalid xAI inline image bytes", async () => { + stubXaiImagine({ data: [{ b64_json: "dHJhaW4=" }] }); + saveConfig(xaiBridgeConfig()); + + const server = startServer(0); + try { + const response = await fetch(new URL("/v1/images/generations", server.url), { + method: "POST", + headers: { "content-type": "application/json", authorization: `Bearer ${DIRECT_CHATGPT_TOKEN}` }, + body: JSON.stringify({ prompt: "a train", model: "gpt-image-2" }), + }); + expect(response.status).toBe(502); + const json = await response.json() as { error?: { message?: string } }; + expect(json.error?.message).toMatch(/base64|magic|validation/i); + } finally { + await server.stop(true); + } +}); + +test("POST /v1/images/edits with no image URL does not call xAI generation", async () => { + const captured = stubXaiImagine({ data: [{ b64_json: "dHJhaW4=" }] }); + saveConfig(xaiBridgeConfig()); + + const server = startServer(0); + try { + const response = await fetch(new URL("/v1/images/edits", server.url), { + method: "POST", + headers: { "content-type": "application/json", authorization: `Bearer ${DIRECT_CHATGPT_TOKEN}` }, + body: JSON.stringify({ prompt: "make it blue", model: "gpt-image-2" }), + }); + expect(response.status).toBe(400); + const json = await response.json() as { error?: { message?: string } }; + expect(json.error?.message).toContain("image edits require an image URL"); + expect(captured).toHaveLength(0); + } finally { + await server.stop(true); + } +}); + +test("POST /v1/images/generations rejects xAI batches that exceed the aggregate output budget", async () => { + stubXaiImagine({ + data: [ + { b64_json: CCA_TINY_PNG }, + { url: "https://8.8.8.8/huge.png" }, + ], + }); + setXaiResultPinnedDownloadForTests(async () => new Response(new ReadableStream({ + start(controller) { controller.close(); }, + }), { + status: 200, + headers: { "content-length": String(IMAGES_RESPONSE_MAX_BYTES) }, + })); + saveConfig(xaiBridgeConfig()); + + const server = startServer(0); + try { + const response = await fetch(new URL("/v1/images/generations", server.url), { + method: "POST", + headers: { "content-type": "application/json", authorization: `Bearer ${DIRECT_CHATGPT_TOKEN}` }, + body: JSON.stringify({ prompt: "a train", model: "gpt-image-2", n: 2 }), + }); + expect(response.status).toBe(502); + const json = await response.json() as { error?: { message?: string } }; + expect(json.error?.message).toContain("xAI image generation output too large"); + } finally { + await server.stop(true); + } +}); + +test("POST /v1/images/generations returns multiple xAI URL images under the aggregate budget", async () => { + const png = new Uint8Array(Buffer.from(CCA_TINY_PNG, "base64")); + const pinned: Array<{ address: string; family: number }> = []; + stubXaiImagine({ + data: [ + { url: "https://8.8.8.8/one.png" }, + { url: "https://8.8.8.8/two.png" }, + ], + }); + setXaiResultPinnedDownloadForTests(async (_url, peer) => { + pinned.push(peer); + return new Response(png, { status: 200, headers: { "content-length": String(png.byteLength) } }); + }); + saveConfig(xaiBridgeConfig()); + + const server = startServer(0); + try { + const response = await fetch(new URL("/v1/images/generations", server.url), { + method: "POST", + headers: { "content-type": "application/json", authorization: `Bearer ${DIRECT_CHATGPT_TOKEN}` }, + body: JSON.stringify({ prompt: "two trains", model: "gpt-image-2", n: 2 }), + }); + expect(response.status).toBe(200); + const json = await response.json() as { data?: Array<{ b64_json?: string }> }; + expect(json.data).toHaveLength(2); + expect(json.data?.[0]?.b64_json).toBe(Buffer.from(png).toString("base64")); + expect(json.data?.[1]?.b64_json).toBe(Buffer.from(png).toString("base64")); + expect(pinned).toEqual([ + { address: "8.8.8.8", family: 4 }, + { address: "8.8.8.8", family: 4 }, + ]); + } finally { + await server.stop(true); + } +}); + +test("POST /v1/images/generations rejects a non-image xAI result URL", async () => { + stubXaiImagine({ data: [{ url: "https://8.8.8.8/page.html" }] }); + setXaiResultPinnedDownloadForTests(async () => new Response("not an image", { + status: 200, + headers: { "content-type": "text/html" }, + })); + saveConfig(xaiBridgeConfig()); + + const server = startServer(0); + try { + const response = await fetch(new URL("/v1/images/generations", server.url), { + method: "POST", + headers: { "content-type": "application/json", authorization: `Bearer ${DIRECT_CHATGPT_TOKEN}` }, + body: JSON.stringify({ prompt: "a train", model: "gpt-image-2" }), + }); + expect(response.status).toBe(502); + const json = await response.json() as { error?: { message?: string } }; + expect(json.error?.message).toBe("xAI image download failed"); + } finally { + await server.stop(true); + } +}); + +test("POST /v1/images/generations rejects a private xAI result URL without fetching it", async () => { + let pinnedCalls = 0; + stubXaiImagine({ data: [{ url: "https://127.0.0.1/secret.png" }] }); + setXaiResultPinnedDownloadForTests(async () => { + pinnedCalls += 1; + return new Response(new Uint8Array([0x89, 0x50, 0x4e, 0x47])); + }); + saveConfig(xaiBridgeConfig()); + + const server = startServer(0); + try { + const response = await fetch(new URL("/v1/images/generations", server.url), { + method: "POST", + headers: { "content-type": "application/json", authorization: `Bearer ${DIRECT_CHATGPT_TOKEN}` }, + body: JSON.stringify({ prompt: "a train", model: "gpt-image-2" }), + }); + expect(response.status).toBe(502); + const json = await response.json() as { error?: { message?: string } }; + expect(json.error?.message).toBe("xAI image download failed"); + expect(json.error?.message).not.toContain("127.0.0.1"); + expect(pinnedCalls).toBe(0); + } finally { + await server.stop(true); + } +}); + +test("POST /v1/images/generations does not follow a 3xx on an xAI result URL", async () => { + const seen: string[] = []; + stubXaiImagine({ data: [{ url: "https://8.8.8.8/img.png" }] }); + setXaiResultPinnedDownloadForTests(async (url, pinned) => { + seen.push(`${pinned.address}:${url}`); + return new Response(null, { + status: 302, + headers: { location: "https://127.0.0.1/secret.png" }, + }); + }); + saveConfig(xaiBridgeConfig()); + + const server = startServer(0); + try { + const response = await fetch(new URL("/v1/images/generations", server.url), { + method: "POST", + headers: { "content-type": "application/json", authorization: `Bearer ${DIRECT_CHATGPT_TOKEN}` }, + body: JSON.stringify({ prompt: "a train", model: "gpt-image-2" }), + }); + expect(response.status).toBe(502); + const json = await response.json() as { error?: { message?: string } }; + expect(json.error?.message).toBe("xAI image download failed"); + expect(json.error?.message).not.toContain("127.0.0.1"); + expect(seen).toEqual(["8.8.8.8:https://8.8.8.8/img.png"]); + } finally { + await server.stop(true); + } +}); + test("POST /v1/images/generations relays to the ChatGPT forward provider with forwarded auth", async () => { const captured: CapturedRequest[] = []; const upstream = fakeImagesUpstream(captured); @@ -372,6 +771,57 @@ test("an explicit custom Images provider uses its configured endpoint, key, and } }); +test("an explicit Images provider wins over the xAI Imagine relay", async () => { + const xaiCaptured = stubXaiImagine({ data: [{ b64_json: CCA_TINY_PNG }] }); + const captured: CapturedRequest[] = []; + const upstream = Bun.serve({ + port: 0, + async fetch(req) { + captured.push({ + path: new URL(req.url).pathname, + headers: req.headers, + body: await req.json(), + }); + return Response.json({ created: 1_767_000_000, data: [{ b64_json: "aGVsbG8=" }] }); + }, + }); + saveConfig({ + port: 0, + defaultProvider: "custom-images", + openaiProviderTierVersion: 2, + providers: { + "custom-images": { + adapter: "openai-responses", + baseUrl: `${upstream.url.toString().replace(/\/$/, "")}/v1`, + allowPrivateNetwork: true, + authMode: "key", + apiKey: "${OPENCODEX_TEST_IMAGES_API_KEY}", + }, + xai: { adapter: "openai-chat", baseUrl: "https://api.x.ai/v1", apiKey: "xai-test-token" }, + }, + images: { bridgeEnabled: true, provider: "custom-images" }, + } as OcxConfig); + + const server = startServer(0); + try { + const response = await fetch(new URL("/v1/images/generations", server.url), { + method: "POST", + headers: { + "content-type": "application/json", + authorization: `Bearer ${DIRECT_CHATGPT_TOKEN}`, + }, + body: JSON.stringify({ prompt: "a cat", model: "gpt-image-2" }), + }); + expect(response.status).toBe(200); + expect(captured).toHaveLength(1); + expect(captured[0].headers.get("authorization")).toBe("Bearer custom-images-key"); + expect(xaiCaptured).toHaveLength(0); + } finally { + await server.stop(true); + await upstream.stop(true); + } +}); + test("an explicit Images provider accepts bearer admission without leaking the proxy secret", async () => { process.env.OPENCODEX_API_AUTH_TOKEN = "proxy-admission-secret"; const captured: CapturedRequest[] = []; From 4a382beedf674d2ab015b6ba888183a2faf6823a Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 04:40:29 +0900 Subject: [PATCH 137/172] feat(codex): opt-in authless Codex Desktop routing mode (#1107) (#3207) Adds codexDesktopAuthless. When true on a loopback bind, the injector writes the dedicated [model_providers.opencodex] table with requires_openai_auth = false (root re-tag, no env_key) instead of the Design B openai_base_url override, so Codex Desktop opens without the ChatGPT login gate while every turn still routes through the proxy. Default off keeps today's byte-identical injection. Non-loopback binds ignore the switch: requires_openai_auth = true and the env_key admission credential are unchanged. Turning it off restores Design B on the next sync; restore strips it like any injected routing. Surfaces: /api/settings GET/PUT (converges the catalog on change), ocx system settings --desktop-authless , docs. Co-authored-by: jun --- .../060_wp6_authless_desktop.md | 89 +++++++++++++++++++ .../061_wp6_audit_r1_synthesis.md | 21 +++++ .../content/docs/guides/codex-integration.md | 42 +++++++++ .../docs/reference/configuration/server.md | 1 + src/cli/system-command.ts | 12 ++- src/codex/inject.ts | 40 +++++++-- src/config.ts | 1 + src/server/management/config-routes.ts | 25 +++++- src/types/config.ts | 7 ++ tests/codex-inject-integration.test.ts | 53 +++++++++++ tests/codex-inject.test.ts | 40 +++++++++ tests/settings-stream-mode.test.ts | 36 ++++++++ 12 files changed, 352 insertions(+), 15 deletions(-) create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/060_wp6_authless_desktop.md create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/061_wp6_audit_r1_synthesis.md diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/060_wp6_authless_desktop.md b/devlog/_plan/260902_nonbug_adoption_backlog/060_wp6_authless_desktop.md new file mode 100644 index 0000000000..508708d698 --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/060_wp6_authless_desktop.md @@ -0,0 +1,89 @@ +# wp6 — #1107 opt-in authless Codex Desktop routing mode + +Issue #1107 (score 71, enhancement/account-pool). No PR. Investigation by grok subagent (Hume) with +file:line evidence; see 061 for the audit. + +## Facts that bound the design + +- Loopback injection today is Design B (root `openai_base_url`); Codex keeps its built-in `openai` + provider so Desktop's ChatGPT OAuth gate applies. Non-loopback injects the dedicated + `[model_providers.opencodex]` table with `requires_openai_auth = true` + `env_key` + (`src/codex/inject.ts:942-960`). +- Codex Desktop honors `requires_openai_auth = false` on a dedicated custom provider (issue + diagnostic, confirmed by maintainer). There is no verified authless knob for the built-in provider; + do not invent one. +- `injectCodexConfig` already strips every prior form before re-injecting (`:920-930`), and + `removeCodexConfig`/`restoreNativeCodex` strip the table + root re-tag + injected base url. So + a third loopback form is reconciled and restored by existing code. +- Catalog: both forms write the same `model_catalog_json`; entries are slug-based, not + provider-tagged (`src/codex/catalog/sync.ts:286`). The "empty picker" in the issue's diagnostic is + the Desktop renderer's native-only allowlist (documented in `guides/codex-app-models.md` §Desktop + remote servers, upstream openai/codex#19694), which applies regardless of our form. Not fixable + here; documented, with the same workaround (`model = "/"` in config.toml). +- History: legacy provider mode runs the `apply-opencodex` history op (threads visible under the + `opencodex` provider); restore runs `migrate-openai`. The authless mode reuses the legacy + history semantics since threads are tagged `opencodex` exactly like non-loopback. + +## Design + +Config key (top-level, flat, next to the other Codex-injection switches): + +```json +{ "codexDesktopAuthless": true } +``` + +- `src/types/config.ts`: `codexDesktopAuthless?: boolean` (doc: opt-in; loopback only; default off). +- `src/config.ts`: `codexDesktopAuthless: z.boolean().optional().catch(undefined)` (degrade-safe + like `syncCodexSubagentDefaults`). +- `src/codex/inject.ts`: + - `CodexRoutingTarget` gains optional `desktopAuthless?: boolean`. + - `standaloneCodexRoutingTarget` sets `desktopAuthless: config.codexDesktopAuthless === true && + !requiresAdmissionToken`. Non-loopback (admission token required) never becomes authless; the + `env_key` line and `requires_openai_auth = true` stay. Client-connect targets + (`src/client/connect.ts`) are untouched. + - `buildProviderTableBlockForTarget`: `requires_openai_auth = ${target.desktopAuthless ? "false" : "true"}`; + `env_key` only when `requiresAdmissionToken` (unchanged). + - `injectCodexConfig`: `const providerTableMode = routingTarget.requiresAdmissionToken || + routingTarget.desktopAuthless === true;` replaces `legacyMode` as the branch selector for + root re-tag + table, profile shape, journal `injectedOpenaiBaseUrl`, history op, and headline + (authless headline names the mode). + - `buildProfileFileForTarget`: same selector so the fallback profile mirrors the live form. +- `src/server/management/config-routes.ts` `/api/settings`: GET returns + `codexDesktopAuthless: config.codexDesktopAuthless === true`; PUT accepts boolean, `false` + deletes the key, rollback on save failure; a change triggers `convergeCodexCatalog()` so the + next inject rewrites config.toml (same pattern as the account picker). +- `src/cli/system-command.ts`: `ocx system settings --desktop-authless ` → PUT. +- Docs: `guides/codex-integration.md` new subsection "Authless Codex Desktop (opt-in)" after the + dedicated-provider paragraph; `reference/configuration/server.md` table row. English only. + +## Acceptance + +- Default (key absent/false): byte-identical injection output to today (existing Design B and + non-loopback tests untouched and green). +- Loopback + opt-in: config.toml has `model_provider = "opencodex"`, the table with + `requires_openai_auth = false`, no `env_key`, no root `openai_base_url`; `model_catalog_json` + still written; re-inject idempotent; fallback profile has the same shape. +- Switching opt-in → off then re-inject restores Design B (root `openai_base_url`, no table); + `restoreNativeCodex` strips the authless form. +- Non-loopback + opt-in: still `requires_openai_auth = true` and `env_key` (admission unchanged). +- User-owned root `openai_base_url` is still respected in authless mode? — No: in provider-table + mode the root key is not ours to manage and `model_provider = "opencodex"` wins routing, matching + today's non-loopback behavior. The existing "user-owned" warning applies to Design B only. +- `/api/settings` round-trips; CLI flag sends the PUT. +- Focused: `tests/codex-inject.test.ts`, `tests/codex-inject-integration.test.ts`, + `tests/settings-stream-mode.test.ts`, `tests/cli-headless-parity.test.ts`; tsc; privacy. + +## Files + +- src/types/config.ts, src/config.ts, src/codex/inject.ts, + src/server/management/config-routes.ts, src/cli/system-command.ts, + docs-site/src/content/docs/guides/codex-integration.md, + docs-site/src/content/docs/reference/configuration/server.md, + tests/codex-inject.test.ts, tests/codex-inject-integration.test.ts, + tests/settings-stream-mode.test.ts, tests/cli-headless-parity.test.ts. + +## Closure + +PR to dev, `Closes #1107`. Close comment names the key, the CLI flag, the non-loopback guarantee, +and the documented picker caveat. + diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/061_wp6_audit_r1_synthesis.md b/devlog/_plan/260902_nonbug_adoption_backlog/061_wp6_audit_r1_synthesis.md new file mode 100644 index 0000000000..adc0cb5f57 --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/061_wp6_audit_r1_synthesis.md @@ -0,0 +1,21 @@ +# wp6 audit r1 — synthesis + +Reviewer: grok-4.6 subagent (Hume), read-only investigation of #1107 against this tree. Verdict +carried as **near-pass**: the dedicated-provider injector can host the mode; the residual risks are +product, not code. + +Findings folded into 060: +1. Catalog rows are slug-based and identical for both forms; the empty picker is Desktop's own + native-only allowlist (upstream #19694). Documented with the existing `model =` workaround; not + an injection bug and not solvable here. +2. `requires_openai_auth = false` also darkens ChatGPT-gated Fast/account/usage chrome. Documented + as an expected cost of the mode. +3. Restore/strip paths already handle the table (`removeOcxSection`, `stripOpencodexConfig`). + Disable → next inject is Design B again. Threads created while enabled stay tagged + `opencodex`; the legacy history op (apply/migrate) is reused. +4. Key precedent: flat top-level boolean with `.catch(undefined)`; PATCH on `/api/settings`. + Subagent suggested no CLI flag; overruled — the user constraint is "opt-in must be easy", so + `ocx system settings --desktop-authless` is added. +5. Non-loopback must never lose `env_key`: enforced by deriving `desktopAuthless` only when + `requiresAdmissionToken` is false. + diff --git a/docs-site/src/content/docs/guides/codex-integration.md b/docs-site/src/content/docs/guides/codex-integration.md index 6cb85614ff..a99e5c61a6 100644 --- a/docs-site/src/content/docs/guides/codex-integration.md +++ b/docs-site/src/content/docs/guides/codex-integration.md @@ -192,6 +192,48 @@ provider advertises `supports_websockets = true` only when `"websockets": true`; built-in provider may try WebSocket first, and a disabled proxy returns `426` so Codex falls back to HTTP/SSE. +### Authless Codex Desktop (opt-in) + +Codex Desktop shows its ChatGPT login screen whenever the active provider requires OpenAI auth. If +your OpenCodex setup never uses ChatGPT credentials (routed providers only, or a blocked +`chatgpt.com`), you can opt out of that gate: + +```bash +ocx system settings --desktop-authless on # or "codexDesktopAuthless": true in config.json +ocx sync # rewrites ~/.codex/config.toml; restart Desktop +``` + +With the switch on, a loopback bind injects the dedicated provider form instead of the root +`openai_base_url` override: + +```toml +model_provider = "opencodex" + +[model_providers.opencodex] +name = "OpenCodex Proxy" +base_url = "http://127.0.0.1:10100/v1" +wire_api = "responses" +requires_openai_auth = false +``` + +Desktop then starts without a login and routes every turn through the proxy. The setting survives +`ocx start`, restart, `ocx sync`, and `ocx ensure`; turning it off (`--desktop-authless off`) makes the +next sync restore the default loopback form, and `ocx restore` strips it like any other injected +routing. What to expect while it is on: + +- ChatGPT-gated Desktop chrome (account, usage, Fast mode) stays dark: Codex derives those surfaces + from the provider's auth requirement. +- New threads are tagged with the `opencodex` provider, as on a non-loopback bind, and history is + handled the same way. +- Desktop releases that filter the model picker against a native-only allowlist may show an empty + or `Custom` picker in this mode as well; requests still use the configured model. Set + `model = "/"` in `config.toml` as described in + [Desktop remote servers](/guides/codex-app-models/#desktop-remote-servers). + +This only changes the Desktop login gate. Non-loopback binds keep `requires_openai_auth = true` and +the `env_key` admission credential regardless of the switch; it never exposes an OpenCodex listener +without authentication. + ## Thread identity and history The default loopback form keeps new threads tagged with Codex's native `openai` provider, so normal diff --git a/docs-site/src/content/docs/reference/configuration/server.md b/docs-site/src/content/docs/reference/configuration/server.md index 7bc15c1191..5b79729660 100644 --- a/docs-site/src/content/docs/reference/configuration/server.md +++ b/docs-site/src/content/docs/reference/configuration/server.md @@ -26,6 +26,7 @@ runs helper features around provider requests. | `appOwnedMemoryBudgetMb?` | `number` | `256` | Cap in MiB for evictable app-owned logs, caches, blobs, and continuation payloads. Range 64–4096; not an RSS cap. | | `codexAutoStart?` | `boolean` | `true` | Let the Codex shim run `ocx ensure` before launching Codex. False makes ensure a no-op. | | `codexShimAutoRestore?` | `boolean` | `true` | Restore an installed shim after a completed external Codex update replaces it. Environment opt-out: `OPENCODEX_CODEX_SHIM_AUTO_RESTORE=0`. | +| `codexDesktopAuthless?` | `boolean` | `false` | Opt-in authless Codex Desktop routing on a loopback bind: inject the dedicated `opencodex` provider with `requires_openai_auth = false` so Desktop opens without a ChatGPT login. Ignored on non-loopback binds. `ocx system settings --desktop-authless on`. See [Codex integration](/guides/codex-integration/#authless-codex-desktop-opt-in). | | `syncResumeHistory?` | `boolean` | `true` | Reversible Codex App history compatibility. Original metadata is backed up and restored by `ocx stop` / `ocx restore`. | | `shadowCallIntercept?` | `{ enabled?: boolean; model?: string; sourceModels?: string[] }` | off | Redirect recognized Codex helper/shadow calls to a chosen model while preserving the request's configured reasoning effort. The default source prefix is `gpt-5.6-luna`; older clients through 0.144.x used `gpt-5.4-mini`, which `sourceModels` can restore. | | `webSearchSidecar?` | `OcxWebSearchSidecarConfig` | on when usable | Web-search sidecar options. | diff --git a/src/cli/system-command.ts b/src/cli/system-command.ts index 34e69e7975..7811e7d432 100644 --- a/src/cli/system-command.ts +++ b/src/cli/system-command.ts @@ -13,7 +13,8 @@ import { const USAGE = `Usage: ocx system [status] [--json] - ocx system settings [--auto-start ] [--stream-mode ] [--json] + ocx system settings [--auto-start ] [--stream-mode ] + [--desktop-authless ] [--json] ocx system startup [--json] ocx system diagnostics [--json] ocx system sync [--json] @@ -42,13 +43,18 @@ async function settings(argv: string[], deps: RuntimeApiDeps): Promise { const wantsJson = takeFlag(args, "--json"); const autoStart = takeBooleanOption(args, "--auto-start"); const streamMode = takeOption(args, "--stream-mode"); + const desktopAuthless = takeBooleanOption(args, "--desktop-authless"); rejectArgs(args, USAGE); - if (autoStart === undefined && streamMode === undefined) { + if (autoStart === undefined && streamMode === undefined && desktopAuthless === undefined) { const result = await runtimeRequest("/api/settings", {}, deps); printData(result, wantsJson, summaryLines(result)); return; } - const body = { ...(autoStart !== undefined ? { codexAutoStart: autoStart } : {}), ...(streamMode !== undefined ? { streamMode } : {}) }; + const body = { + ...(autoStart !== undefined ? { codexAutoStart: autoStart } : {}), + ...(streamMode !== undefined ? { streamMode } : {}), + ...(desktopAuthless !== undefined ? { codexDesktopAuthless: desktopAuthless } : {}), + }; const result = await runtimeRequest("/api/settings", { method: "PUT", body: JSON.stringify(body) }, deps); printData(result, wantsJson, ["System settings updated."]); } diff --git a/src/codex/inject.ts b/src/codex/inject.ts index 536b12a360..46f322f0fe 100644 --- a/src/codex/inject.ts +++ b/src/codex/inject.ts @@ -148,6 +148,13 @@ export interface CodexRoutingTarget { baseUrl: string; requiresAdmissionToken: boolean; tokenEnv: "OPENCODEX_API_AUTH_TOKEN"; + /** + * Opt-in authless Codex Desktop mode (#1107): inject the dedicated provider table with + * `requires_openai_auth = false` so Desktop skips the ChatGPT login gate. Only ever true for + * loopback targets that need no admission token; non-loopback admission is a separate layer + * and is never weakened by this flag. + */ + desktopAuthless?: boolean; } function validateCodexRoutingTarget(target: CodexRoutingTarget): CodexRoutingTarget { @@ -171,17 +178,26 @@ function validateCodexRoutingTarget(target: CodexRoutingTarget): CodexRoutingTar return { ...target, baseUrl: `${parsed.origin}/v1` }; } +/** Provider-table form is used for non-loopback admission and for the authless Desktop opt-in. */ +function usesProviderTable(target: CodexRoutingTarget): boolean { + return target.requiresAdmissionToken || target.desktopAuthless === true; +} + export function standaloneCodexRoutingTarget( port: number, - config?: Pick, + config?: Pick, ): CodexRoutingTarget { const loopback = config?.unauthenticatedLoopbackListener; const effectivePort = loopback?.enabled ? loopback.port : port; const hostname = loopback?.enabled ? undefined : config?.hostname; + const requiresAdmissionToken = loopback?.enabled ? false : shouldInjectApiAuthHeader(config); return { baseUrl: `http://${providerBaseHost(hostname)}:${effectivePort}/v1`, - requiresAdmissionToken: loopback?.enabled ? false : shouldInjectApiAuthHeader(config), + requiresAdmissionToken, tokenEnv: "OPENCODEX_API_AUTH_TOKEN", + ...(config?.codexDesktopAuthless === true && !requiresAdmissionToken + ? { desktopAuthless: true } + : {}), }; } @@ -296,7 +312,8 @@ function buildProviderTableBlockForTarget( 'name = "OpenCodex Proxy"', `base_url = ${tomlString(target.baseUrl)}`, 'wire_api = "responses"', - "requires_openai_auth = true", + // false only in the authless Desktop opt-in (#1107); true keeps the App/TUI account gate. + `requires_openai_auth = ${target.desktopAuthless === true ? "false" : "true"}`, ]; if (target.requiresAdmissionToken) { // codex-cli 0.146+ contract (#2073): env_key sends Authorization: Bearer $VAR and @@ -775,8 +792,8 @@ function buildProfileFileForTarget( const host = new URL(origin).host; // Design B (loopback): the reference/fallback file documents the root override form. // Non-loopback keeps the legacy provider-table shape (built-in provider cannot carry - // the x-opencodex-api-key env header). - if (!target.requiresAdmissionToken) { + // the x-opencodex-api-key env header); the authless Desktop opt-in shares that shape. + if (!usesProviderTable(target)) { const lines = [ "# OpenCodex proxy fallback config (Design B)", `# Root override that points Codex's built-in openai provider at the proxy on ${host}.`, @@ -939,11 +956,14 @@ export async function injectCodexConfig( ? setRootModelCatalogPath(content, catalogPath) : stripOpencodexCatalogPath(content); - const legacyMode = routingTarget.requiresAdmissionToken; + // Provider-table form: non-loopback admission (legacy) or the authless Desktop opt-in (#1107). + const legacyMode = usesProviderTable(routingTarget); let keptUserBaseUrl = false; if (legacyMode) { // Legacy (non-loopback) injection: the built-in openai provider cannot carry the // x-opencodex-api-key env header, so keep the opencodex provider table + root re-tag. + // The authless opt-in needs the same table because only a dedicated provider can carry + // requires_openai_auth = false. // 1) Root key BEFORE the first table header (must be a global, not nested under a table). content = setRootModelProvider(content); // 2) Provider table appended at EOF (position-independent). @@ -1277,9 +1297,11 @@ export async function injectCodexConfig( ` Reference config: ${CODEX_PROFILE_PATH}`, }; } - const headline = legacyMode - ? `Injected opencodex as default provider into Codex config.\n` - : `Pointed Codex's built-in openai provider at the opencodex proxy (openai_base_url).\n`; + const headline = routingTarget.desktopAuthless === true + ? `Injected opencodex as default provider into Codex config (authless Desktop mode: requires_openai_auth = false).\n` + : legacyMode + ? `Injected opencodex as default provider into Codex config.\n` + : `Pointed Codex's built-in openai provider at the opencodex proxy (openai_base_url).\n`; return { success: true, ...(nativeSubagentDefaultsWarning ? { nativeSubagentDefaultsWarning } : {}), diff --git a/src/config.ts b/src/config.ts index e28e69fafd..4a2b0199d5 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1053,6 +1053,7 @@ const configSchema = z.object({ z.array(z.string().trim().min(1)).min(1), ).optional().catch(undefined), codexShimAutoRestore: z.boolean().optional(), + codexDesktopAuthless: z.boolean().optional().catch(undefined), pausedCodexAccountIds: z.array(z.string().regex(/^[a-zA-Z0-9._-]{1,64}$/)).optional(), codexAccountNamespaces: codexAccountNamespacesSchema.optional(), // Selection order is a preference, not a safety control like pause: a malformed diff --git a/src/server/management/config-routes.ts b/src/server/management/config-routes.ts index a30d8ac451..6fe625aa10 100644 --- a/src/server/management/config-routes.ts +++ b/src/server/management/config-routes.ts @@ -306,6 +306,8 @@ export async function handleConfigRoutes(ctx: ManagementContext): Promise { expect(readFileSync(join(codexHome, "config.toml"), "utf8")).toBe(original); }); + test("authless Desktop opt-in (#1107): loopback injects the table with requires_openai_auth = false, idempotently", () => { + writeFileSync(join(codexHome, "config.toml"), 'model = "gpt-5.5"\n', "utf8"); + + const r = runInject(codexHome, ocxHome, JSON.stringify({ codexDesktopAuthless: true })); + expect(r.status).toBe(0); + const payload = JSON.parse(r.stdout); + expect(payload.success).toBe(true); + expect(String(payload.message)).toContain("authless Desktop mode"); + + const first = readFileSync(join(codexHome, "config.toml"), "utf8"); + expect(first).toContain('model_provider = "opencodex"'); + expect(first).toContain("[model_providers.opencodex]"); + expect(first).toContain('base_url = "http://127.0.0.1:10100/v1"'); + expect(first).toContain("requires_openai_auth = false"); + expect(first).not.toContain("env_key"); + expect(first).not.toContain("openai_base_url"); + + expect(runInject(codexHome, ocxHome, JSON.stringify({ codexDesktopAuthless: true })).status).toBe(0); + expect(readFileSync(join(codexHome, "config.toml"), "utf8")).toBe(first); + expect(readFileSync(join(codexHome, "opencodex.config.toml"), "utf8")).toContain("requires_openai_auth = false"); + }); + + test("authless Desktop opt-in: turning it off restores Design B on the next inject, and restore strips it", () => { + writeFileSync(join(codexHome, "config.toml"), 'model = "gpt-5.5"\n', "utf8"); + + expect(runInject(codexHome, ocxHome, JSON.stringify({ codexDesktopAuthless: true })).status).toBe(0); + expect(readFileSync(join(codexHome, "config.toml"), "utf8")).toContain("requires_openai_auth = false"); + + expect(runInject(codexHome, ocxHome).status).toBe(0); + const back = readFileSync(join(codexHome, "config.toml"), "utf8"); + expect(back).toContain('openai_base_url = "http://127.0.0.1:10100/v1"'); + expect(back).not.toContain("[model_providers.opencodex]"); + expect(back).not.toContain('model_provider = "opencodex"'); + expect(back.match(/Auto-injected by opencodex/g)?.length).toBe(1); + + expect(runInject(codexHome, ocxHome, JSON.stringify({ codexDesktopAuthless: true })).status).toBe(0); + expect(runRestore(codexHome, ocxHome).status).toBe(0); + const restored = readFileSync(join(codexHome, "config.toml"), "utf8"); + expect(restored).not.toContain("opencodex"); + expect(restored).toContain('model = "gpt-5.5"'); + }); + + test("authless Desktop opt-in never weakens non-loopback admission", () => { + writeFileSync(join(codexHome, "config.toml"), 'model = "gpt-5.5"\n', "utf8"); + + const r = runInject(codexHome, ocxHome, JSON.stringify({ hostname: "192.168.1.20", codexDesktopAuthless: true })); + expect(r.status).toBe(0); + const config = readFileSync(join(codexHome, "config.toml"), "utf8"); + expect(config).toContain("requires_openai_auth = true"); + expect(config).toContain('env_key = "OPENCODEX_API_AUTH_TOKEN"'); + expect(config).not.toContain("requires_openai_auth = false"); + }); + test("non-loopback hostname still uses the legacy provider-table injection", () => { writeFileSync(join(codexHome, "config.toml"), 'model = "gpt-5.5"\n', "utf8"); diff --git a/tests/codex-inject.test.ts b/tests/codex-inject.test.ts index 14197aa4ec..5a4650de65 100644 --- a/tests/codex-inject.test.ts +++ b/tests/codex-inject.test.ts @@ -28,6 +28,46 @@ describe("Codex config injection", () => { ); }); + describe("authless Codex Desktop opt-in (#1107)", () => { + test("default target on loopback stays Design B and byte-identical", () => { + const target = standaloneCodexRoutingTarget(10100, {}); + expect(target.desktopAuthless).toBeUndefined(); + expect(buildProfileFile(target, null)).toBe(buildProfileFile(10100, null)); + expect(buildProviderTableBlock(target)).toContain("requires_openai_auth = true"); + }); + + test("loopback opt-in emits the provider table with requires_openai_auth = false and no env_key", () => { + const target = standaloneCodexRoutingTarget(10100, { codexDesktopAuthless: true }); + expect(target).toMatchObject({ requiresAdmissionToken: false, desktopAuthless: true }); + const block = buildProviderTableBlock(target); + expect(block).toContain("[model_providers.opencodex]"); + expect(block).toContain('base_url = "http://127.0.0.1:10100/v1"'); + expect(block).toContain("requires_openai_auth = false"); + expect(block).not.toContain("env_key"); + const profile = buildProfileFile(target, "/tmp/opencodex-catalog.json"); + expect(profile).toContain('model_provider = "opencodex"'); + expect(profile).toContain("requires_openai_auth = false"); + expect(profile).not.toContain("openai_base_url"); + }); + + test("non-loopback binds ignore the opt-in: admission env_key and requires_openai_auth = true stay", () => { + const target = standaloneCodexRoutingTarget(10100, { hostname: "192.168.1.20", codexDesktopAuthless: true }); + expect(target.desktopAuthless).toBeUndefined(); + expect(target.requiresAdmissionToken).toBe(true); + const block = buildProviderTableBlock(target); + expect(block).toContain("requires_openai_auth = true"); + expect(block).toContain('env_key = "OPENCODEX_API_AUTH_TOKEN"'); + }); + + test("the unauthenticated loopback listener still honors the opt-in", () => { + const target = standaloneCodexRoutingTarget(10100, { + codexDesktopAuthless: true, + unauthenticatedLoopbackListener: { enabled: true, port: 10199 }, + }); + expect(target).toMatchObject({ baseUrl: "http://127.0.0.1:10199/v1", desktopAuthless: true }); + }); + }); + test("explicit HTTPS target emits exact provider destination and admission env", () => { const target = { baseUrl: "https://hub.example.test/v1", diff --git a/tests/settings-stream-mode.test.ts b/tests/settings-stream-mode.test.ts index 96580a094d..641b996dc9 100644 --- a/tests/settings-stream-mode.test.ts +++ b/tests/settings-stream-mode.test.ts @@ -338,6 +338,42 @@ describe("PUT /api/settings", () => { expect(config.codexAccountNamespaces).toEqual({ main: "@main" }); }); + test("codexDesktopAuthless (#1107): absent reports false, enable persists and converges once, disable deletes the key", async () => { + const config = baseConfig(); + const absent = await (await getSettings(config))!.json() as { codexDesktopAuthless?: boolean }; + expect(absent.codexDesktopAuthless).toBe(false); + + let convergences = 0; + let saved: OcxConfig | undefined; + const on = await putSettings(config, { codexDesktopAuthless: true }, { + saveConfigPreservingClaudeCode: next => { saved = next; }, + createManagementConvergeCodex: catalogConvergenceFactory(() => { convergences += 1; }), + }); + expect(on!.status).toBe(200); + expect(await on!.json()).toMatchObject({ codexDesktopAuthless: true }); + expect(saved?.codexDesktopAuthless).toBe(true); + expect(convergences).toBe(1); + + const same = await putSettings(config, { codexDesktopAuthless: true }, { + saveConfigPreservingClaudeCode: () => {}, + createManagementConvergeCodex: catalogConvergenceFactory(() => { convergences += 1; }), + }); + expect(same!.status).toBe(200); + expect(convergences).toBe(1); + + const off = await putSettings(config, { codexDesktopAuthless: false }, { + saveConfigPreservingClaudeCode: next => { saved = next; }, + createManagementConvergeCodex: catalogConvergenceFactory(() => { convergences += 1; }), + }); + expect(off!.status).toBe(200); + expect(await off!.json()).toMatchObject({ codexDesktopAuthless: false }); + expect(Object.hasOwn(saved!, "codexDesktopAuthless")).toBe(false); + expect(convergences).toBe(2); + + const bad = await putSettings(config, { codexDesktopAuthless: "yes" }); + expect(bad!.status).toBe(400); + }); + test("account-picker disable does not initialize an empty namespace map", async () => { const config = baseConfig(); let convergences = 0; From e1eb8c5c1c27bc61e59feb67a224aa0e68415ca9 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 04:44:12 +0900 Subject: [PATCH 138/172] docs(cli): explain shim-free Codex token injection boundaries (#2713) (#3208) Records what does and does not carry OPENCODEX_API_AUTH_TOKEN into a Codex process launched independently of the proxy, why EnvironmentFile= on the proxy unit cannot, and where ocx doctor reports the broken env_key launch path (landed in #2844). Co-authored-by: jun --- .../070_wp7_shim_free_token.md | 35 +++++++++++++++++++ .../071_wp7_audit_r1_synthesis.md | 9 +++++ .../content/docs/reference/cli/lifecycle.md | 26 ++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/070_wp7_shim_free_token.md create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/071_wp7_audit_r1_synthesis.md diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/070_wp7_shim_free_token.md b/devlog/_plan/260902_nonbug_adoption_backlog/070_wp7_shim_free_token.md new file mode 100644 index 0000000000..8a1209706b --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/070_wp7_shim_free_token.md @@ -0,0 +1,35 @@ +# wp7 — #2713 shim-free Codex token injection + +State at entry: the narrow `ocx doctor` diagnostic requested by the issue ("env_key set + variable +absent + shim missing → actionable repair line") landed on `dev` in PR #2844 (`5734a1caf`, +`collectCodexEnvKeyReadiness` in `src/cli/doctor.ts`, tests in +`tests/doctor-codex-envkey-readiness.test.ts`). The maintainer review (score 58) and the reviewer +follow-up (2026-08-29) both settled the remaining design questions: + +- A `systemd --user` drop-in is rejected as the default: it does not fit a root-owned server and + only reaches services launched by the user manager, not interactive shells, cron, or Desktop. +- `EnvironmentFile=` on `opencodex-proxy.service` lands only in the proxy process; it cannot + inject `OPENCODEX_API_AUTH_TOKEN` into an independently launched `codex exec`. Validated by the + reporter on a root VPS. +- Codex has no credential-file directive for `env_key`; the value must exist in the Codex process + environment. Do not invent one. +- No new token file; the existing `service-api-token` is the source. Do not add another launcher + interception at the Codex binary path (that is the hole the issue reports). +- Verdict: no `ocx codex-env` command yet; a narrow documentation update is what remains. + +## Scope (docs only) + +`docs-site/src/content/docs/reference/cli/lifecycle.md`, in the `ocx codex-shim` section: a +subsection "Token injection without the shim" that states the process boundary, lists what does and +does not carry `OPENCODEX_API_AUTH_TOKEN` to Codex (shim; exporting the variable in the launching +process — shell profile, cron line, service unit that launches Codex itself; `EnvironmentFile=` on +the proxy unit does not), points to `ocx doctor`'s "Codex env_key launch readiness" line, and +reminds that the token value is never printed and must not be copied into `config.toml`. + +## Acceptance + +- Section present; no new commands or config keys claimed (`skill:surface:check` unaffected). +- `bun run privacy:scan` clean. +- PR to dev; close #2713 with English rationale: doctor slice landed (#2844), documentation landed, + first-class `ocx codex-env` declined for now with the reasons above; reopen path stated. + diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/071_wp7_audit_r1_synthesis.md b/devlog/_plan/260902_nonbug_adoption_backlog/071_wp7_audit_r1_synthesis.md new file mode 100644 index 0000000000..fe4e8432e7 --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/071_wp7_audit_r1_synthesis.md @@ -0,0 +1,9 @@ +# wp7 audit r1 — synthesis + +Audit input is the issue's own review chain: maintainer review (grok-bot, score 58) and the reviewer +follow-up confirming both referenced landings on `dev` (`5734a1ca` doctor, `bb3321ca` framing) and +the process-boundary conclusion. Verified in this tree: `collectCodexEnvKeyReadiness` +(`src/cli/doctor.ts:473`) and its action line; `src/codex/shim.ts:726` is the only reader that +exports the token into a Codex process; `src/cli/index.ts:241` exports it for `ocx` itself. +Verdict: pass for a docs-only closure; nothing in the plan changes runtime behavior. + diff --git a/docs-site/src/content/docs/reference/cli/lifecycle.md b/docs-site/src/content/docs/reference/cli/lifecycle.md index 75ca9da788..46d34a84e5 100644 --- a/docs-site/src/content/docs/reference/cli/lifecycle.md +++ b/docs-site/src/content/docs/reference/cli/lifecycle.md @@ -418,6 +418,32 @@ Use `ocx service` for an always-on background proxy (recommended). Use `ocx code lightweight, on-demand startup without a daemon — the proxy starts only when `codex` is launched. ::: +#### Token injection without the shim + +On a non-loopback bind the injected provider carries `env_key = "OPENCODEX_API_AUTH_TOKEN"`. That +line tells Codex which variable to read; it does not create it. Codex refuses to start a request +when the variable is missing (`Missing environment variable: OPENCODEX_API_AUTH_TOKEN`), and the +proxy is never reached. The value lives in `$OPENCODEX_HOME/service-api-token`; only a process that +exports it into Codex's environment closes the gap. + +What does carry the token into a Codex process: + +- the shim installed by `ocx codex-shim install` (reads the token file at launch; the supported path + for Codex started from shells, Desktop, cron, or another service); +- exporting `OPENCODEX_API_AUTH_TOKEN` yourself in the process that starts Codex — a shell profile, + the cron line, or an `Environment=`/`EnvironmentFile=` on the systemd unit that launches + **Codex** (not the proxy). Point it at the existing token file; do not copy the value into + `config.toml`. + +What does not: an `EnvironmentFile=` or `OCX_API_TOKEN_FILE` on `opencodex-proxy.service`. Those +configure the proxy process only and never flow into an independently launched `codex exec`. + +A Codex upgrade that replaces the launcher removes the shim; the next ordinary `ocx` command restores +it (see above), but a `codex exec` that runs before that fails. `ocx doctor` reports this exact +state under "Codex env_key launch readiness" (env_key configured, variable unset, shim missing or +unhealthy, token file present) with the repair command, and never prints the token. Reading the token +file directly from Codex is not something Codex supports, so there is no OpenCodex directive for it. + ### `ocx tray [--json] [--no-start]` Install and control the Windows status tray icon. It starts at Windows login and provides one-click From 5bc6939d85cae05845329b88201259566b452e84 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 04:51:46 +0900 Subject: [PATCH 139/172] feat(proxy): startup Windows system-proxy discovery behind proxy "auto" (#1525) (#3209) Slice 1 of #1525. When config.proxy is the literal "auto", applyProxyEnv reads the WinINET static proxy (HKCU Internet Settings ProxyEnable / ProxyServer, https= then http= entry) once at process start and mirrors it into HTTP_PROXY/HTTPS_PROXY when those are unset, logging only the proxy origin. Non-Windows, disabled, SOCKS-only, or unreadable settings degrade to direct egress with one log line; the literal is never copied into the environment. Static URLs, ${ENV} references, user env precedence, and loopback NO_PROXY are unchanged. PAC/WPAD, live refresh, and direct fallback are deferred per the reviewer scope. Co-authored-by: jun --- .../080_wp8_windows_proxy_auto.md | 27 ++++ .../081_wp8_audit_r1_synthesis.md | 9 ++ .../docs/reference/configuration/server.md | 2 +- src/config.ts | 42 ++++++- src/lib/windows-system-proxy.ts | 115 ++++++++++++++++++ src/types/config.ts | 4 + tests/proxy-env.test.ts | 67 ++++++++++ 7 files changed, 264 insertions(+), 2 deletions(-) create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/080_wp8_windows_proxy_auto.md create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/081_wp8_audit_r1_synthesis.md create mode 100644 src/lib/windows-system-proxy.ts diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/080_wp8_windows_proxy_auto.md b/devlog/_plan/260902_nonbug_adoption_backlog/080_wp8_windows_proxy_auto.md new file mode 100644 index 0000000000..1af7d34fd5 --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/080_wp8_windows_proxy_auto.md @@ -0,0 +1,27 @@ +# wp8 — #1525 Windows `proxy: "auto"` (slice 1: startup WinINET static-proxy discovery) + +Issue #1525 (score 60, enhancement/proxy/platform). Reviewer scoped the mergeable first slice: +startup-time WinINET static-proxy discovery behind `proxy: "auto"`, clear logs, no live mutation, +no direct fallback, PAC/WPAD deferred. Investigation by grok subagent (Poincare); see 081. + +## Design + +- `src/lib/windows-system-proxy.ts` (new): `readWindowsSystemProxy(reader?)` returns + `{ kind: "proxy", url } | { kind: "disabled" } | { kind: "unsupported" } | { kind: "unreadable" } | { kind: "socks-only" }`. + Reader spawns `%SystemRoot%\System32\reg.exe query HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings /v ProxyEnable` and `/v ProxyServer` with argv `execFileSync`, `windowsHide`, 2s timeout, never throws. Parsing: `https=` entry → `http=` entry → bare `host:port`; SOCKS-only ignored; normalized to `http://host:port`. The reader is injectable so tests never spawn `reg.exe`. +- `src/config.ts` `applyProxyEnv`: when the resolved string is exactly `auto` (case-insensitive, trimmed), call the discovery; on `proxy` continue with the resolved URL; every other outcome logs one privacy-safe line (no URL userinfo, and only host:port on success) and returns without setting `HTTP_PROXY` (today `"auto"` would be copied verbatim into `HTTP_PROXY`). Env vars still win; loopback `NO_PROXY` unchanged. +- `src/types/config.ts` JSDoc for `proxy`. No zod change (schema is passthrough and does not declare `proxy`). +- Docs: `reference/configuration/server.md` proxy row (English). +- Doctor: untouched this slice (it already hides values; `auto` shows as configured). + +## Out of slice +PAC/WPAD, ProxyOverride → NO_PROXY, periodic re-check, direct fallback, live mutation. + +## Acceptance +- Static URL / `${ENV}` / user env precedence: existing `tests/proxy-env.test.ts` unchanged and green. +- `auto` + injected reader returning proxy → `HTTP_PROXY`/`HTTPS_PROXY` set to normalized URL, log line without userinfo. +- `auto` + disabled/unsupported/unreadable/socks-only → env untouched, one log line. +- `auto` + user env set → env untouched. +- Parser unit cases: bare, `http=;https=`, `https=` only, `socks=` only, credentials stripped from log. +- tsc, privacy, focused test file. + diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/081_wp8_audit_r1_synthesis.md b/devlog/_plan/260902_nonbug_adoption_backlog/081_wp8_audit_r1_synthesis.md new file mode 100644 index 0000000000..92e9e4af84 --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/081_wp8_audit_r1_synthesis.md @@ -0,0 +1,9 @@ +# wp8 audit r1 — synthesis + +Reviewer: grok-4.6 subagent (Poincare). Verdict near-pass; all findings adopted: +- All `applyProxyEnv` callers are synchronous (`src/server/index.ts:641`, `src/codex/sync.ts:126/146/199`); a sync `reg.exe` read with argv `execFileSync`, `windowsHide`, 2s timeout mirrors `src/tray/windows.ts:361`. No await in `startServer`. +- Defer ProxyOverride: separators and `` semantics differ from NO_PROXY; second policy. +- Logs: host:port only, userinfo stripped; doctor already never prints values. +- Tests inject the reader; CI never spawns `reg.exe`. +- Schema: passthrough, JSDoc only; an enum would start backing up configs. + diff --git a/docs-site/src/content/docs/reference/configuration/server.md b/docs-site/src/content/docs/reference/configuration/server.md index 5b79729660..9d9803b7a1 100644 --- a/docs-site/src/content/docs/reference/configuration/server.md +++ b/docs-site/src/content/docs/reference/configuration/server.md @@ -12,7 +12,7 @@ runs helper features around provider requests. | --- | --- | --- | --- | | `port` | `number` | `10100` | Proxy listen port. | | `hostname?` | `string` | `"127.0.0.1"` | Bind address. Non-loopback binds require `OPENCODEX_API_AUTH_TOKEN`. | -| `proxy?` | `string` | — | Outbound HTTP(S) proxy URL or `${ENV_VAR}`. Applied to `HTTP_PROXY` / `HTTPS_PROXY` only when those variables are unset; loopback remains in `NO_PROXY`. | +| `proxy?` | `string` | — | Outbound HTTP(S) proxy URL, `${ENV_VAR}`, or `"auto"`. Applied to `HTTP_PROXY` / `HTTPS_PROXY` only when those variables are unset; loopback remains in `NO_PROXY`. `"auto"` reads the Windows system proxy (WinINET `ProxyEnable`/`ProxyServer`, `https=` then `http=` entry) once at process start and logs the host it chose. On other platforms, or when the system proxy is off, SOCKS-only, or unreadable, it uses direct egress and says so. PAC/WPAD and live proxy changes are not followed; restart the service after changing the system proxy. | | `noProxy?` | `string \| string[]` | — | Hosts that bypass `proxy`, merged with inherited `NO_PROXY` and loopback entries. A string may use comma-separated `NO_PROXY` syntax or `${ENV_VAR}`. | | `emptyCompletionRetry?` | `boolean` | `false` | Opt in to one identical Responses retry when a turn has no text or tool call, including a stream that ends before a terminal event. The retry may be billable. `OCX_EMPTY_COMPLETION_RETRY=0` disables it without changing config; combo and routed-compaction turns remain excluded. | | `stallTimeoutSec?` | `number` | `300` | Seconds without upstream data before `response.incomplete`. Minimum 1. | diff --git a/src/config.ts b/src/config.ts index 4a2b0199d5..77f8caade8 100644 --- a/src/config.ts +++ b/src/config.ts @@ -119,6 +119,11 @@ export { type AtomicWriteIO, } from "./config/atomic-write"; import { getConfigDir, getConfigPath, hardenConfigDir } from "./config/paths"; +import { + describeProxyForLog, + readWindowsSystemProxy, + type WindowsProxyRegistryReader, +} from "./lib/windows-system-proxy"; export { expandUserPath, getConfigDir, getConfigPath, hardenConfigDir } from "./config/paths"; export { getPidPath, @@ -3526,6 +3531,14 @@ function warnProxyConfigDiscardOnce(kind: "proxy" | "noProxy" | "noProxyElements * that makes outbound provider requests (server start, catalog sync). */ export function applyProxyEnv(config: OcxConfig): void { + applyProxyEnvWith(config); +} + +/** Test seam for `proxy: "auto"`: the registry reader and platform are injectable. */ +export function applyProxyEnvWith( + config: OcxConfig, + auto: { reader?: WindowsProxyRegistryReader; platform?: NodeJS.Platform } = {}, +): void { // `proxy` and `noProxy` are not declared in the top-level schema, which ends in // `.passthrough()`, so whatever is on disk arrives here verbatim. A non-string value // reached string-only methods and threw out of this function, and it runs once per @@ -3533,13 +3546,40 @@ export function applyProxyEnv(config: OcxConfig): void { // malformed values with a privacy-safe warning instead: they cannot express a routing // intent, and refusing to start is a worse answer than starting without them. const rawProxy = config.proxy; - const proxy = typeof rawProxy === "string" ? resolveEnvValue(rawProxy) : undefined; + let proxy = typeof rawProxy === "string" ? resolveEnvValue(rawProxy) : undefined; if (!proxy) { if (rawProxy !== undefined) warnProxyConfigDiscardOnce("proxy"); return; } + if (proxy.trim().toLowerCase() === "auto") { + // #1525 slice 1: one startup read of the Windows static proxy. Never copy the literal + // "auto" into HTTP_PROXY; every non-proxy outcome leaves outbound routing as it was. + if (process.env.HTTP_PROXY?.trim() || process.env.http_proxy?.trim() + || process.env.HTTPS_PROXY?.trim() || process.env.https_proxy?.trim()) { + console.log("[opencodex] proxy \"auto\": existing HTTP_PROXY/HTTPS_PROXY environment wins; system proxy not consulted"); + proxy = undefined; + } else { + const found = readWindowsSystemProxy(auto.reader, auto.platform); + if (found.kind === "proxy") { + console.log(`[opencodex] proxy "auto": using Windows system proxy ${describeProxyForLog(found.url)}`); + proxy = found.url; + } else { + const reason = found.kind === "unsupported" + ? "only Windows system proxy discovery is supported; using direct egress on this OS" + : found.kind === "disabled" + ? "Windows system proxy is disabled; using direct egress" + : found.kind === "socks-only" + ? "Windows system proxy is SOCKS-only, which HTTP_PROXY cannot express; using direct egress" + : "Windows proxy settings could not be read; using direct egress"; + console.log(`[opencodex] proxy "auto": ${reason}`); + proxy = undefined; + } + } + } + if (proxy) { if (!process.env.HTTP_PROXY?.trim() && !process.env.http_proxy?.trim()) process.env.HTTP_PROXY = proxy; if (!process.env.HTTPS_PROXY?.trim() && !process.env.https_proxy?.trim()) process.env.HTTPS_PROXY = proxy; + } const existing = process.env.NO_PROXY ?? process.env.no_proxy ?? ""; const entries = existing.split(",").map(s => s.trim()).filter(Boolean); const seen = new Set(entries.map(e => e.toLowerCase())); diff --git a/src/lib/windows-system-proxy.ts b/src/lib/windows-system-proxy.ts new file mode 100644 index 0000000000..1316698b23 --- /dev/null +++ b/src/lib/windows-system-proxy.ts @@ -0,0 +1,115 @@ +import { execFileSync } from "node:child_process"; +import { existsSync } from "node:fs"; +import { join } from "node:path"; +import { decodeWindowsTextBytes } from "./windows-text"; + +/** + * Startup-time discovery of the Windows WinINET static proxy (#1525, slice 1). + * + * Reads `HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings` once and returns a + * normalized `http://host:port` URL when a static proxy is enabled. PAC/WPAD, per-request + * resolution, ProxyOverride, live refresh, and direct fallback are deliberately out of scope: + * this is the piece an operator can audit from one log line, and everything else needs the + * transport boundary the reviewer asked for first. + */ + +const INTERNET_SETTINGS_KEY = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"; + +export type WindowsSystemProxyResult = + | { kind: "proxy"; url: string } + | { kind: "disabled" } + | { kind: "socks-only" } + | { kind: "unsupported" } + | { kind: "unreadable" }; + +/** Raw registry values; `null` when the value is absent or the read failed. */ +export interface WindowsProxyRegistryValues { + proxyEnable: string | null; + proxyServer: string | null; +} + +export type WindowsProxyRegistryReader = () => WindowsProxyRegistryValues | null; + +function registryExe(): string { + const candidate = join(process.env.SystemRoot ?? "C:\\Windows", "System32", "reg.exe"); + return existsSync(candidate) ? candidate : "reg.exe"; +} + +function queryValue(name: string): string | null { + try { + const stdout = execFileSync(registryExe(), ["query", INTERNET_SETTINGS_KEY, "/v", name], { + encoding: "buffer", + stdio: ["ignore", "pipe", "ignore"], + timeout: 2000, + maxBuffer: 64 * 1024, + windowsHide: true, + }); + const text = decodeWindowsTextBytes(stdout); + // " ProxyServer REG_SZ host:port" + const line = text.split(/\r?\n/).find(row => row.trim().startsWith(name)); + if (!line) return null; + const match = line.match(/REG_(?:SZ|DWORD|EXPAND_SZ)\s+(.*)$/); + return match ? match[1]!.trim() : null; + } catch { + return null; + } +} + +export function readWindowsProxyRegistry(): WindowsProxyRegistryValues | null { + const proxyEnable = queryValue("ProxyEnable"); + if (proxyEnable === null) return null; + return { proxyEnable, proxyServer: queryValue("ProxyServer") }; +} + +/** + * `ProxyServer` is either a bare `host:port` (applies to every scheme) or a semicolon list of + * `scheme=host:port` entries. Prefer the https entry, then http; a SOCKS-only value cannot be + * mirrored into HTTP_PROXY/HTTPS_PROXY. + */ +export function parseWindowsProxyServer(value: string): { kind: "proxy"; url: string } | { kind: "socks-only" } | { kind: "disabled" } { + const trimmed = value.trim(); + if (!trimmed) return { kind: "disabled" }; + if (!trimmed.includes("=")) return normalize(trimmed); + const entries = new Map(); + for (const part of trimmed.split(";")) { + const eq = part.indexOf("="); + if (eq <= 0) continue; + entries.set(part.slice(0, eq).trim().toLowerCase(), part.slice(eq + 1).trim()); + } + const candidate = entries.get("https") || entries.get("http"); + if (candidate) return normalize(candidate); + if (entries.has("socks")) return { kind: "socks-only" }; + return { kind: "disabled" }; +} + +function normalize(hostPort: string): { kind: "proxy"; url: string } | { kind: "disabled" } { + const withScheme = /^[a-z][a-z0-9+.-]*:\/\//i.test(hostPort) ? hostPort : `http://${hostPort}`; + try { + const url = new URL(withScheme); + if (!url.hostname || (url.protocol !== "http:" && url.protocol !== "https:")) return { kind: "disabled" }; + // Keep userinfo: a credentialed proxy is valid in HTTP_PROXY. Only the log strips it. + const auth = url.username ? `${url.username}${url.password ? `:${url.password}` : ""}@` : ""; + return { kind: "proxy", url: `${url.protocol}//${auth}${url.host}` }; + } catch { + return { kind: "disabled" }; + } +} + +export function readWindowsSystemProxy( + reader: WindowsProxyRegistryReader = readWindowsProxyRegistry, + platform: NodeJS.Platform = process.platform, +): WindowsSystemProxyResult { + if (platform !== "win32") return { kind: "unsupported" }; + const values = reader(); + if (!values) return { kind: "unreadable" }; + // REG_DWORD prints as 0x1 / 0x0. + const enabled = /^(0x)?0*1$/i.test((values.proxyEnable ?? "").trim()); + if (!enabled) return { kind: "disabled" }; + if (!values.proxyServer) return { kind: "disabled" }; + return parseWindowsProxyServer(values.proxyServer); +} + +/** Log-safe form: origin only, so a credentialed value can never reach the console. */ +export function describeProxyForLog(url: string): string { + try { return new URL(url).origin; } catch { return ""; } +} diff --git a/src/types/config.ts b/src/types/config.ts index 4a2f016f45..b59a366a5b 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -598,6 +598,10 @@ export interface OcxConfig { * Outbound HTTP(S) proxy URL for provider requests (e.g. "http://user:pass@proxy:8080", or * "${HTTPS_PROXY}"-style env reference). Mirrored into HTTP_PROXY/HTTPS_PROXY at startup when * those are unset — Bun's fetch honors them for all outbound calls; localhost is excluded. + * The literal `"auto"` reads the Windows WinINET static proxy (`ProxyEnable`/`ProxyServer`) + * once at process start; on other platforms, or when the system proxy is off, SOCKS-only, + * or unreadable, it degrades to direct egress with one log line (#1525). PAC/WPAD and live + * changes are not followed. */ proxy?: string; /** diff --git a/tests/proxy-env.test.ts b/tests/proxy-env.test.ts index bf7ccaa467..1439013162 100644 --- a/tests/proxy-env.test.ts +++ b/tests/proxy-env.test.ts @@ -151,3 +151,70 @@ describe("applyProxyEnv", () => { expect(process.env.HTTP_PROXY).toBe("http://ref-proxy:9999"); }); }); + +describe("applyProxyEnv with proxy: \"auto\" (#1525)", () => { + const { applyProxyEnvWith } = require("../src/config") as typeof import("../src/config"); + const { parseWindowsProxyServer, readWindowsSystemProxy } = require("../src/lib/windows-system-proxy") as typeof import("../src/lib/windows-system-proxy"); + + function capture(run: () => void): string[] { + const lines: string[] = []; + const original = console.log; + console.log = (...args: unknown[]) => { lines.push(args.map(String).join(" ")); }; + try { run(); } finally { console.log = original; } + return lines; + } + + test("parses bare, per-scheme, and socks-only ProxyServer values", () => { + expect(parseWindowsProxyServer("127.0.0.1:7890")).toEqual({ kind: "proxy", url: "http://127.0.0.1:7890" }); + expect(parseWindowsProxyServer("http=10.0.0.5:3128;https=10.0.0.6:3129;ftp=x:1")).toEqual({ kind: "proxy", url: "http://10.0.0.6:3129" }); + expect(parseWindowsProxyServer("http=10.0.0.5:3128")).toEqual({ kind: "proxy", url: "http://10.0.0.5:3128" }); + expect(parseWindowsProxyServer("socks=127.0.0.1:1080")).toEqual({ kind: "socks-only" }); + expect(parseWindowsProxyServer("")).toEqual({ kind: "disabled" }); + }); + + test("readWindowsSystemProxy honors ProxyEnable and platform", () => { + const on = () => ({ proxyEnable: "0x1", proxyServer: "127.0.0.1:7893" }); + expect(readWindowsSystemProxy(on, "win32")).toEqual({ kind: "proxy", url: "http://127.0.0.1:7893" }); + expect(readWindowsSystemProxy(() => ({ proxyEnable: "0x0", proxyServer: "127.0.0.1:7893" }), "win32")).toEqual({ kind: "disabled" }); + expect(readWindowsSystemProxy(() => null, "win32")).toEqual({ kind: "unreadable" }); + expect(readWindowsSystemProxy(on, "darwin")).toEqual({ kind: "unsupported" }); + }); + + test("auto on Windows mirrors the discovered proxy and logs only the origin", () => { + const lines = capture(() => applyProxyEnvWith(configWithProxy("auto"), { + platform: "win32", + reader: () => ({ proxyEnable: "0x1", proxyServer: "user:secret-pass-91@127.0.0.1:7893" }), + })); + expect(process.env.HTTP_PROXY).toBe("http://user:secret-pass-91@127.0.0.1:7893"); + expect(process.env.HTTPS_PROXY).toBe("http://user:secret-pass-91@127.0.0.1:7893"); + expect(process.env.NO_PROXY).toBe("localhost,127.0.0.1,::1,[::1]"); + expect(lines.join("\n")).toContain("http://127.0.0.1:7893"); + expect(lines.join("\n")).not.toContain("secret-pass-91"); + }); + + test("auto never leaks the literal into HTTP_PROXY when discovery yields nothing", () => { + for (const [platform, reader] of [ + ["darwin", () => ({ proxyEnable: "0x1", proxyServer: "127.0.0.1:1" })], + ["win32", () => ({ proxyEnable: "0x0", proxyServer: "127.0.0.1:1" })], + ["win32", () => ({ proxyEnable: "0x1", proxyServer: "socks=127.0.0.1:1080" })], + ["win32", () => null], + ] as const) { + delete process.env.HTTP_PROXY; delete process.env.HTTPS_PROXY; + const lines = capture(() => applyProxyEnvWith(configWithProxy("auto"), { platform, reader })); + expect(process.env.HTTP_PROXY).toBeUndefined(); + expect(process.env.HTTPS_PROXY).toBeUndefined(); + expect(lines).toHaveLength(1); + expect(lines[0]).toContain('proxy "auto"'); + } + }); + + test("auto defers to an existing proxy environment without consulting the registry", () => { + process.env.HTTPS_PROXY = "http://from-env:9"; + let consulted = false; + applyProxyEnvWith(configWithProxy("auto"), { platform: "win32", reader: () => { consulted = true; return null; } }); + expect(consulted).toBe(false); + expect(process.env.HTTPS_PROXY).toBe("http://from-env:9"); + expect(process.env.HTTP_PROXY).toBeUndefined(); + }); +}); + From 941cb45b6ba5e387383bfb3bec5f6fe0c14d9999 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 05:04:42 +0900 Subject: [PATCH 140/172] feat(providers): opt-in OS keychain storage for provider API keys (#1221) (#3210) Adds src/providers/key-store.ts: a single sync resolver for provider key material (env reference, keychain reference, or literal) that every request-time apiKey read now goes through, plus server-side store/restore that moves the active key and pool into the OS credential store (@napi-rs/keyring, service opencodex.provider-api-key.v1) and rewrites config with keychain:[/] references. Store verifies every write by read-back before touching config and refuses with 503 when the keychain is unavailable; a reference that cannot be read at request time yields no credential with one warning and never falls back to plaintext. Pool entries hold references too, so key failover persists references. Surfaces: GET/POST /api/providers/keychain, ocx provider keychain [status|store|restore], docs. Plain and env keys are unchanged. Co-authored-by: jun --- .../090_wp9_keychain_keys.md | 41 ++++ .../091_wp9_audit_r1_synthesis.md | 10 + .../docs/reference/configuration/providers.md | 29 ++- src/cli/provider-runtime.ts | 26 ++- src/codex/catalog/provider-fetch.ts | 5 +- src/images/plan.ts | 4 +- src/lib/lab-live-route-production.ts | 3 +- src/oauth/index.ts | 5 +- src/providers/api-keys.ts | 4 +- src/providers/key-store.ts | 197 ++++++++++++++++++ src/providers/openai-sidecar.ts | 6 +- src/providers/quota.ts | 32 +-- src/router.ts | 4 +- src/server/management/oauth-account-routes.ts | 29 +++ src/server/management/route-registry.ts | 2 + src/server/responses/compact.ts | 6 +- tests/cli-headless-parity.test.ts | 14 ++ tests/provider-key-store.test.ts | 194 +++++++++++++++++ 18 files changed, 577 insertions(+), 34 deletions(-) create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/090_wp9_keychain_keys.md create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/091_wp9_audit_r1_synthesis.md create mode 100644 src/providers/key-store.ts create mode 100644 tests/provider-key-store.test.ts diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/090_wp9_keychain_keys.md b/devlog/_plan/260902_nonbug_adoption_backlog/090_wp9_keychain_keys.md new file mode 100644 index 0000000000..0de4c639c9 --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/090_wp9_keychain_keys.md @@ -0,0 +1,41 @@ +# wp9 — #1221 opt-in OS keychain storage for provider API keys (slice 1) + +Issue #1221 (score 61). Investigation by grok subagent (Kierkegaard); see 091. Findings that bound +the design: `resolveEnvValue(x.apiKey)` is called at 24 sync sites (router, quota ×15, compact ×2, +catalog, sidecar ×2, images, lab, oauth/index) and adapters read `provider.apiKey` from the routed +clone; `@napi-rs/keyring` ships a sync `Entry` so the request path stays sync; save/backup paths +are plaintext-free automatically once `apiKey` on disk is a reference; `key-failover` and +`addProviderApiKey` write `candidate.key` back — with references in the pool that stays a +reference. + +## Design + +- Reference syntax: `apiKey: "keychain:"` (pool entries: `keychain:/`). + Keyring service `opencodex.provider-api-key.v1`, account = the part after `keychain:`. +- `src/providers/key-store.ts` (new): `isKeychainReference`, `resolveProviderApiKey(value)` (env + ref → env; keychain ref → sync `Entry.getPassword()` with a process cache; failure → `undefined` + + one warning per account, never plaintext fallback), `storeProviderKeyInKeychain` / + `restoreProviderKeyFromKeychain` (async, write then read-back verify; refuse when unavailable), + `clearKeychainCacheForTests`. Entry factory injectable. +- Funnel: every `resolveEnvValue(.apiKey)` site → `resolveProviderApiKey`; `maskApiKey` + returns keychain refs verbatim (non-secret) like env refs. +- Management: `POST /api/providers/keychain` body `{ name, action: "store" | "restore" }` and + `GET /api/providers/keychain?name=` → `{ store: "keychain" | "file" | "env", available }`. + `store`: moves the active key and every plaintext pool entry into the keychain, rewrites + config with references, verifies read-back first (keychain unavailable → 503, config untouched). + `restore`: reads back, writes plaintext, deletes the keychain items. +- CLI: `ocx provider keychain [store|restore|status] [--json]`. +- Docs: providers.md "Storing keys in the OS keychain" + note on services/headless sessions. + +## Out of slice +Dashboard control, global default, DPAPI-specific handling beyond what napi provides, per-request +async resolution. + +## Acceptance +- Plain/env keys: identical behavior (existing tests untouched). +- `keychain:` ref resolves through a mock Entry at routing (`routedProviderConfig`), quota, compact. +- Unavailable keyring at request time → key undefined, one warning, no plaintext written. +- store: config rewritten to refs, pool refs, read-back verified; restore reverses; failure leaves config. +- `maskApiKey("keychain:x")` verbatim; `hasApiKey` true. +- tsc, privacy, focused tests: new `tests/provider-key-store.test.ts`, `tests/provider-api-keys.test.ts` (if exists), `tests/router*.test.ts` sanity. + diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/091_wp9_audit_r1_synthesis.md b/devlog/_plan/260902_nonbug_adoption_backlog/091_wp9_audit_r1_synthesis.md new file mode 100644 index 0000000000..f453a58856 --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/091_wp9_audit_r1_synthesis.md @@ -0,0 +1,10 @@ +# wp9 audit r1 — synthesis + +Reviewer: grok-4.6 subagent (Kierkegaard). Verdict: resolver funnel first; keychain write must not +re-serialize plaintext through failover/save. Adopted: single sync resolver, sync `Entry`, fail +closed at request time, refuse opt-in when the keyring is unavailable, references in pool entries +so failover persists references only. Deviation from the suggestion to split write path into a +second PR: the write path here is a server-side store/restore that verifies read-back before +touching config, which removes the plaintext-rewrite hazard the reviewer flagged; the dashboard +control is what is deferred. + diff --git a/docs-site/src/content/docs/reference/configuration/providers.md b/docs-site/src/content/docs/reference/configuration/providers.md index d6b37c8086..86915fa962 100644 --- a/docs-site/src/content/docs/reference/configuration/providers.md +++ b/docs-site/src/content/docs/reference/configuration/providers.md @@ -76,7 +76,7 @@ differing backup and rewrites known legacy namespaced selected ids to bare ids. | `promptCacheKey?` | `boolean` | Provider-wide `openai-chat` opt-in for forwarding a `prompt_cache_key`. The adapter forwards the key it is given and never invents one, but the key is not always the caller's: Claude Messages translation derives one from `metadata.user_id`, or from a model/system/tools cohort when no metadata is sent. Default off. Enable only when the upstream documents support, because strict gateways may reject the unknown field with HTTP 400. | | `preserveResponsesReasoningContent?` | `boolean` | Keep plaintext reasoning content on replayed Responses reasoning items instead of blanking it (blanking is the ChatGPT backend's rule). Enable for upstreams whose contract accepts reasoning replay, such as DeepSeek. Proxy-minted `ocxr1` envelopes are always stripped. | | `disabled?` | `boolean` | Keep the provider on disk but exclude it from routing and model/catalog listings. | -| `apiKey?` | `string` | API key, or an `${ENV_VAR}` / `$ENV_VAR` reference resolved at request time. | +| `apiKey?` | `string` | API key, an `${ENV_VAR}` / `$ENV_VAR` reference, or a `keychain:` reference written by `ocx provider keychain store`. References resolve at request time. See [Storing keys in the OS keychain](#storing-keys-in-the-os-keychain). | | `apiKeyTransport?` | `"x-api-key" \| "bearer"` | Anthropic key header style. Defaults to native `x-api-key`; valid only for key-auth `anthropic` providers. | | `apiKeyPool?` | `ApiKeyPoolEntry[]` | Multi-key pool. `apiKey` mirrors the active entry; each item has `id`, `key`, optional `label`, and optional numeric `addedAt`. | | `defaultModel?` | `string` | Model used when this provider is selected without an explicit model. | @@ -567,6 +567,33 @@ model key. ## Static model allowlists +## Storing keys in the OS keychain + +By default a provider's `apiKey` and `apiKeyPool` sit in `config.json` (mode 0600, atomic writes). +If you would rather keep the key material out of the file, move it into the OS credential store: + +```bash +ocx provider keychain deepseek status # store: file | env | keychain, and whether the keychain answers +ocx provider keychain deepseek store # move active key + pool into the OS keychain +ocx provider keychain deepseek restore # bring the plaintext back and delete the keychain items +``` + +The same operations are `GET`/`POST /api/providers/keychain`. After `store`, `config.json` holds +`"apiKey": "keychain:deepseek"` (pool entries `keychain:deepseek/`) and the secret lives under the +`opencodex.provider-api-key.v1` service in macOS Keychain, Windows Credential Manager, or the Linux +Secret Service. Backups of `config.json` therefore carry references only. Key rotation and failover +keep working: pool entries compare by reference, so a rotation never writes plaintext back. + +Before touching the config, `store` writes and reads back every entry; if the keychain is unavailable +or the read-back does not match, it refuses with 503 and leaves the file as it was. At request time +a reference that cannot be read yields no credential and one warning per key — there is no plaintext +fallback, by design. + +When not to opt in: a proxy running as a headless service (systemd, launchd, Task Scheduler) or in a +container usually has no unlocked keychain session, so requests would fail closed. Use an +`${ENV_VAR}` reference in the service environment there instead. Env references are left untouched +by `store`. + Set `liveModels: false` to expose only `models`. If `models` is empty or omitted, the provider exposes no routed models. Live discovery rejects more than 4 MiB or 2,000 raw model rows before caching; built-in presets may use lower limits and filter to chat-eligible rows. Oversized or malformed results diff --git a/src/cli/provider-runtime.ts b/src/cli/provider-runtime.ts index ebeac3d4ae..6f7a204832 100644 --- a/src/cli/provider-runtime.ts +++ b/src/cli/provider-runtime.ts @@ -30,7 +30,8 @@ const USAGE = `Usage: ocx provider quota [--refresh] [--json] ocx provider presets [--json] ocx provider account-mode [--json] - ocx provider selected [--set ] [--clear] [--json]`; + ocx provider selected [--set ] [--clear] [--json] + ocx provider keychain [status|store|restore] [--json]`; function cleared(value: string | undefined): string | undefined { return value === "-" ? "" : value; @@ -181,6 +182,28 @@ async function selected(argv: string[], deps: RuntimeApiDeps): Promise { printData(result, wantsJson, [`${name}: ${models.length ? models.join(", ") : "all models"}`]); } +async function keychain(argv: string[], deps: RuntimeApiDeps): Promise { + const args = [...argv]; + const name = args.shift()?.trim(); + const wantsJson = takeFlag(args, "--json"); + const action = (args.shift() ?? "status").toLowerCase(); + if (!name) throw new CliUsageError("provider name is required", USAGE); + if (!["status", "store", "restore"].includes(action)) throw new CliUsageError(`unknown keychain action ${action}`, USAGE); + rejectArgs(args, USAGE); + if (action === "status") { + const result = await runtimeRequest>(`/api/providers/keychain?name=${encodeURIComponent(name)}`, {}, deps); + printData(result, wantsJson, summaryLines(result)); + return; + } + const result = await runtimeRequest>("/api/providers/keychain", { + method: "POST", + body: JSON.stringify({ name, action }), + }, deps); + printData(result, wantsJson, [action === "store" + ? `${name}: API key moved to the OS keychain; config.json now holds a keychain: reference.` + : `${name}: API key restored to config.json; keychain entries removed.`]); +} + export async function handleProviderRuntimeCommand(sub: string, argv: string[], deps: RuntimeApiDeps = {}): Promise { const handlers: Record Promise> = { edit, @@ -190,6 +213,7 @@ export async function handleProviderRuntimeCommand(sub: string, argv: string[], presets, "account-mode": accountMode, selected, + keychain, }; const handler = handlers[sub]; if (!handler) return null; diff --git a/src/codex/catalog/provider-fetch.ts b/src/codex/catalog/provider-fetch.ts index 45bdfa57b0..19babf92aa 100644 --- a/src/codex/catalog/provider-fetch.ts +++ b/src/codex/catalog/provider-fetch.ts @@ -2,7 +2,8 @@ import { execFileSync } from "node:child_process"; import { createHash, createHmac, randomBytes } from "node:crypto"; import { copyFileSync, existsSync, mkdirSync, readFileSync, realpathSync } from "node:fs"; import { delimiter, dirname, join, resolve } from "node:path"; -import { atomicWriteFile, expandUserPath, getConfigDir, resolveEnvValue, websocketsEnabled } from "../../config"; +import { atomicWriteFile, expandUserPath, getConfigDir, websocketsEnabled } from "../../config"; +import { resolveProviderApiKey } from "../../providers/key-store"; import { CODEX_CONFIG_PATH, CODEX_MODELS_CACHE_PATH, DEFAULT_CATALOG_PATH, readRootTomlString, resolveCodexConfigPath } from "../paths"; import { clearModelCache, @@ -1269,7 +1270,7 @@ function observedModelsAuthResolver( resolve(name, provider) { if (provider.authMode === "forward") return { apiKey: undefined, observed: true }; if (provider.authMode !== "oauth") { - return { apiKey: resolveEnvValue(provider.apiKey), observed: true }; + return { apiKey: resolveProviderApiKey(provider.apiKey), observed: true }; } const observation = observeActiveOAuthAccessToken(name, authStoreBuffer); diff --git a/src/images/plan.ts b/src/images/plan.ts index 2a48aae454..980e8f7977 100644 --- a/src/images/plan.ts +++ b/src/images/plan.ts @@ -1,7 +1,7 @@ import type { OcxConfig, OcxParsedRequest, OcxProviderConfig } from "../types"; import { toolChoiceToolPredicate } from "../types"; import type { ImageBridgePlan, VideoBridgePlan } from "./types"; -import { resolveEnvValue } from "../config"; +import { resolveProviderApiKey } from "../providers/key-store"; import { getValidAccessToken } from "../oauth/index"; import { getProviderRegistryEntry } from "../providers/registry"; import { IMAGE_GEN_TOOL_NAME, VIDEO_GEN_TOOL_NAME, isVideoGenName } from "./synthetic-tool"; @@ -37,7 +37,7 @@ export function findXaiProvider(config: OcxConfig): { name: string; provider: Oc */ export function resolveXaiImageApiKey(provider: OcxProviderConfig): string | undefined { if (provider.authMode === "oauth") return undefined; - const apiKey = resolveEnvValue(provider.apiKey)?.trim(); + const apiKey = resolveProviderApiKey(provider.apiKey)?.trim(); return apiKey || undefined; } diff --git a/src/lib/lab-live-route-production.ts b/src/lib/lab-live-route-production.ts index 5d31dbac6a..1e84b34406 100644 --- a/src/lib/lab-live-route-production.ts +++ b/src/lib/lab-live-route-production.ts @@ -8,6 +8,7 @@ * @internal host integration only */ import { resolveEnvValue } from "../config"; +import { resolveProviderApiKey } from "../providers/key-store"; import { getValidAccessTokenSnapshot, OAuthLoginRequiredError, @@ -75,7 +76,7 @@ async function buildLabProviderAuthHeaders( throw new TransportError("harness_failure", "oauth refresh unavailable"); } } else { - const apiKey = resolveEnvValue(provider.apiKey)?.trim(); + const apiKey = resolveProviderApiKey(provider.apiKey)?.trim(); if (!apiKey) throw new TransportError("auth_blocked", "missing api key"); if (provider.adapter === "anthropic" && provider.apiKeyTransport === "x-api-key") { headers["x-api-key"] = apiKey; diff --git a/src/oauth/index.ts b/src/oauth/index.ts index c4a682ee3f..92bc74b3fa 100644 --- a/src/oauth/index.ts +++ b/src/oauth/index.ts @@ -1,7 +1,8 @@ import type { KiroOAuthMetadata, OAuthController, OAuthCredentials } from "./types"; import { parseCallbackInput } from "./callback-server"; import type { OcxConfig, OcxProviderConfig, RefreshPolicy } from "../types"; -import { ConfigMutationLockError, loadConfig, resolveEnvValue, saveConfig } from "../config"; +import { ConfigMutationLockError, loadConfig, saveConfig } from "../config"; +import { resolveProviderApiKey } from "../providers/key-store"; import { maskEmail } from "../lib/privacy"; import { KiroTokenRefreshError, environmentKiroRoutingMetadata, loginKiro, refreshKiroToken, settleKiroLoginTransaction } from "./kiro"; import { @@ -1043,7 +1044,7 @@ export async function resolveModelsAuthToken(name: string, prov: OcxProviderConf return undefined; } } - return resolveEnvValue(prov.apiKey); + return resolveProviderApiKey(prov.apiKey); } function modelDiscoveryTransportSeed(providerName: string, prov: OcxProviderConfig): OcxProviderConfig { diff --git a/src/providers/api-keys.ts b/src/providers/api-keys.ts index 88a0d72238..6cf4c8e302 100644 --- a/src/providers/api-keys.ts +++ b/src/providers/api-keys.ts @@ -24,7 +24,9 @@ function isEnvReference(value: string): boolean { } export function maskApiKey(value: string): string { - if (isEnvReference(value)) return value; + // Env and keychain references carry no secret material; show them verbatim so an operator + // can tell where the key lives. + if (isEnvReference(value) || value.startsWith("keychain:")) return value; if (value.length <= 8) return "****"; return `${value.slice(0, 4)}****${value.slice(-4)}`; } diff --git a/src/providers/key-store.ts b/src/providers/key-store.ts new file mode 100644 index 0000000000..bf8ec3198f --- /dev/null +++ b/src/providers/key-store.ts @@ -0,0 +1,197 @@ +import { createRequire } from "node:module"; +import { resolveEnvValue, saveConfigPreservingClaudeCode } from "../config"; +import type { OcxConfig, OcxProviderConfig } from "../types"; + +/** + * Opt-in OS keychain storage for provider API keys (#1221). + * + * `config.json` keeps only a reference (`keychain:` for the active key, + * `keychain:/` for pool entries); the secret lives in the OS credential store + * under one service name. Reads are synchronous on purpose: `routedProviderConfig` and the + * quota/compaction/catalog callers are all sync, and `@napi-rs/keyring` ships a sync `Entry`. + * + * Policy: a reference that cannot be resolved fails closed (no key) and is warned once per + * account; nothing ever rewrites plaintext into config or its backups. Opting in verifies the + * keychain by writing and reading back before the config is touched, so an unavailable store + * (headless service, locked session) refuses rather than half-migrating. + */ + +export const KEYCHAIN_REFERENCE_PREFIX = "keychain:"; +export const PROVIDER_KEYCHAIN_SERVICE = "opencodex.provider-api-key.v1"; + +export interface ProviderKeychainEntry { + getPassword(): string | null; + setPassword(password: string): void; + deletePassword(): boolean; +} + +export type ProviderKeychainEntryFactory = (service: string, account: string) => ProviderKeychainEntry; + +const nodeRequire = createRequire(import.meta.url); + +function defaultEntryFactory(service: string, account: string): ProviderKeychainEntry { + const { Entry } = nodeRequire("@napi-rs/keyring") as { Entry: new (s: string, a: string) => ProviderKeychainEntry }; + return new Entry(service, account); +} + +let entryFactory: ProviderKeychainEntryFactory = defaultEntryFactory; +const resolvedCache = new Map(); +const warnedAccounts = new Set(); + +/** Test seam: swap the OS entry for an in-memory one and drop caches. */ +export function setProviderKeychainEntryFactoryForTests(factory: ProviderKeychainEntryFactory | null): void { + entryFactory = factory ?? defaultEntryFactory; + resolvedCache.clear(); + warnedAccounts.clear(); +} + +export function isKeychainReference(value: string | undefined): value is string { + return typeof value === "string" && value.startsWith(KEYCHAIN_REFERENCE_PREFIX) && value.length > KEYCHAIN_REFERENCE_PREFIX.length; +} + +function keychainAccount(reference: string): string { + return reference.slice(KEYCHAIN_REFERENCE_PREFIX.length); +} + +function readKeychain(account: string): string | undefined { + const cached = resolvedCache.get(account); + if (cached !== undefined) return cached; + try { + const value = entryFactory(PROVIDER_KEYCHAIN_SERVICE, account).getPassword(); + if (typeof value === "string" && value.trim()) { + resolvedCache.set(account, value); + return value; + } + } catch { + // fall through to the single warning below + } + if (!warnedAccounts.has(account)) { + warnedAccounts.add(account); + console.warn(`[opencodex] provider key reference keychain:${account} could not be read from the OS keychain; requests for this provider have no credential until the keychain is available (no plaintext fallback)`); + } + return undefined; +} + +/** + * Single resolver for provider key material: env references, keychain references, or the + * literal value. Every request-time read of `apiKey` goes through here. + */ +export function resolveProviderApiKey(value: string | undefined): string | undefined { + if (!value) return undefined; + if (isKeychainReference(value)) return readKeychain(keychainAccount(value)); + return resolveEnvValue(value); +} + +export type ProviderKeyStoreKind = "keychain" | "env" | "file" | "none"; + +export function providerKeyStoreKind(provider: Pick | undefined): ProviderKeyStoreKind { + const key = provider?.apiKey; + if (!key) return "none"; + if (isKeychainReference(key)) return "keychain"; + if (/^\$\{?\w+\}?$/.test(key)) return "env"; + return "file"; +} + +/** Probe the OS keychain with a throwaway account: write, read back, delete. */ +export function probeProviderKeychain(): { available: true } | { available: false; reason: string } { + const account = `probe-${process.pid}-${Date.now()}`; + try { + const entry = entryFactory(PROVIDER_KEYCHAIN_SERVICE, account); + entry.setPassword("ok"); + const back = entry.getPassword(); + try { entry.deletePassword(); } catch { /* best effort */ } + if (back !== "ok") return { available: false, reason: "keychain read-back did not match" }; + return { available: true }; + } catch (error) { + return { available: false, reason: error instanceof Error ? error.message : "keychain unavailable" }; + } +} + +function writeVerified(account: string, secret: string): void { + const entry = entryFactory(PROVIDER_KEYCHAIN_SERVICE, account); + entry.setPassword(secret); + if (entry.getPassword() !== secret) throw new Error(`keychain read-back mismatch for ${account}`); +} + +/** + * Move a provider's active key and every plaintext pool entry into the OS keychain and rewrite + * config with references. All keychain writes are verified before config changes; on any + * failure the entries written so far are deleted and config is left untouched. + */ +export function storeProviderKeyInKeychain(config: OcxConfig, name: string): { ok: true; moved: number } | { ok: false; error: string; status: number } { + const provider = config.providers[name]; + if (!provider) return { ok: false, error: "unknown provider", status: 404 }; + if (provider.authMode === "oauth" || provider.authMode === "forward") { + return { ok: false, error: "provider does not use API-key auth", status: 400 }; + } + const probe = probeProviderKeychain(); + if (!probe.available) return { ok: false, error: `OS keychain unavailable: ${probe.reason}`, status: 503 }; + + const written: string[] = []; + const planned: Array<() => void> = []; + const pool = provider.apiKeyPool ?? []; + try { + for (const entry of pool) { + if (isKeychainReference(entry.key)) continue; + const secret = resolveEnvValue(entry.key); + if (!secret) continue; // unresolved env reference stays as-is + const account = `${name}/${entry.id}`; + writeVerified(account, secret); + written.push(account); + planned.push(() => { entry.key = `${KEYCHAIN_REFERENCE_PREFIX}${account}`; }); + } + if (provider.apiKey && !isKeychainReference(provider.apiKey)) { + const active = pool.find(e => e.key === provider.apiKey || (isKeychainReference(e.key) && false)); + const secret = resolveEnvValue(provider.apiKey); + if (secret) { + if (active) { + // Mirror the pool reference so failover keeps comparing equal strings. + planned.push(() => { provider.apiKey = `${KEYCHAIN_REFERENCE_PREFIX}${name}/${active.id}`; }); + } else { + writeVerified(name, secret); + written.push(name); + planned.push(() => { provider.apiKey = `${KEYCHAIN_REFERENCE_PREFIX}${name}`; }); + } + } + } + } catch (error) { + for (const account of written) { + try { entryFactory(PROVIDER_KEYCHAIN_SERVICE, account).deletePassword(); } catch { /* best effort */ } + } + return { ok: false, error: `OS keychain write failed: ${error instanceof Error ? error.message : "unknown"}`, status: 503 }; + } + for (const apply of planned) apply(); + resolvedCache.clear(); + warnedAccounts.clear(); + saveConfigPreservingClaudeCode(config); + return { ok: true, moved: written.length }; +} + +/** Reverse of `storeProviderKeyInKeychain`: read every reference back, write plaintext, delete items. */ +export function restoreProviderKeyFromKeychain(config: OcxConfig, name: string): { ok: true; restored: number } | { ok: false; error: string; status: number } { + const provider = config.providers[name]; + if (!provider) return { ok: false, error: "unknown provider", status: 404 }; + const pool = provider.apiKeyPool ?? []; + const resolved = new Map(); + const refs = [provider.apiKey, ...pool.map(e => e.key)].filter(isKeychainReference); + for (const ref of refs) { + const account = keychainAccount(ref); + if (resolved.has(account)) continue; + let value: string | null = null; + try { value = entryFactory(PROVIDER_KEYCHAIN_SERVICE, account).getPassword(); } catch { value = null; } + if (!value) return { ok: false, error: `OS keychain has no readable secret for ${ref}; config left unchanged`, status: 503 }; + resolved.set(account, value); + } + for (const entry of pool) { + if (isKeychainReference(entry.key)) entry.key = resolved.get(keychainAccount(entry.key))!; + } + if (isKeychainReference(provider.apiKey)) provider.apiKey = resolved.get(keychainAccount(provider.apiKey))!; + for (const account of resolved.keys()) { + try { entryFactory(PROVIDER_KEYCHAIN_SERVICE, account).deletePassword(); } catch { /* best effort */ } + } + resolvedCache.clear(); + warnedAccounts.clear(); + saveConfigPreservingClaudeCode(config); + return { ok: true, restored: resolved.size }; +} + diff --git a/src/providers/openai-sidecar.ts b/src/providers/openai-sidecar.ts index 70e788882b..00ca95dd1b 100644 --- a/src/providers/openai-sidecar.ts +++ b/src/providers/openai-sidecar.ts @@ -1,4 +1,4 @@ -import { resolveEnvValue } from "../config"; +import { resolveProviderApiKey } from "./key-store"; import { CodexPoolAuthenticationError, headersForCodexAuthContext, @@ -198,7 +198,7 @@ export function selectOpenAiImagesProvider(config: OcxConfig): OpenAiImagesProvi && provider.authMode !== "forward" && provider.baseUrl.replace(/\/+$/, "") === "https://api.openai.com/v1" ) { - const apiKey = resolveEnvValue(provider.apiKey)?.trim(); + const apiKey = resolveProviderApiKey(provider.apiKey)?.trim(); if (apiKey) selection.keyed = { providerName: OPENAI_API_PROVIDER_ID, provider, apiKey }; } return selection; @@ -236,7 +236,7 @@ export function selectImagesProvider(config: OcxConfig): OpenAiImagesProviderSel }; } - const apiKey = resolveEnvValue(provider.apiKey)?.trim(); + const apiKey = resolveProviderApiKey(provider.apiKey)?.trim(); if (!apiKey) { return { forwardCandidates: [], error: `images.provider "${providerName}" has no usable API key` }; } diff --git a/src/providers/quota.ts b/src/providers/quota.ts index 3335679e63..29342f2447 100644 --- a/src/providers/quota.ts +++ b/src/providers/quota.ts @@ -9,7 +9,7 @@ import type { StoredAccountQuota } from "../codex/quota"; import { isMainAccountIdentityGenerationLive } from "../codex/main-account-cache"; import { MAIN_CODEX_ACCOUNT_ID } from "../codex/main-account"; import { codexPlanKey } from "../codex/plan"; -import { resolveEnvValue } from "../config"; +import { resolveProviderApiKey } from "./key-store"; import { getValidAccessToken, getValidAccessTokenForAccount } from "../oauth"; import { getAccountCredential, getAccountSet, getCredential } from "../oauth/store"; import { antigravityUserAgent } from "../adapters/client-fingerprint"; @@ -153,7 +153,7 @@ function cacheKey(config: OcxConfig): string { const providers = Object.entries(config.providers) .map(([name, provider]) => { const resolvedKey = typeof provider.apiKey === "string" - ? resolveEnvValue(provider.apiKey)?.trim() + ? resolveProviderApiKey(provider.apiKey)?.trim() : undefined; const activeKeyId = resolvedKey ? apiKeyPoolEntryId(resolvedKey) : "none"; return `${name}:${provider.adapter}:${provider.authMode ?? "key"}:${providerCodexAccountMode(name, provider) ?? "none"}:${provider.disabled === true ? "off" : "on"}:${provider.baseUrl}:${activeKeyId}`; @@ -370,7 +370,7 @@ function firstFinite(record: Record | null, names: string[]): n async function fetchA6apiQuota(provider: string, config: OcxProviderConfig): Promise { // Never send a configured API key to a lookalike host or through a redirect. if (!isCanonicalA6apiBaseUrl(config.baseUrl)) return null; - const apiKey = resolveEnvValue(config.apiKey)?.trim(); + const apiKey = resolveProviderApiKey(config.apiKey)?.trim(); if (!apiKey) return null; const headers = { Accept: "application/json", Authorization: `Bearer ${apiKey}` } as const; const [subscriptionResponse, tokenResponse] = await Promise.all([ @@ -464,7 +464,7 @@ function parseOpenCodeGoUsageWindow(value: unknown): { percent: number; resetAt? async function fetchOpenCodeGoQuota(provider: string, config: OcxProviderConfig): Promise { // Never send a configured API key when the provider destination is not the built-in Go endpoint. if (!isCanonicalOpenCodeGoBaseUrl(config.baseUrl)) return null; - const apiKey = resolveEnvValue(config.apiKey)?.trim(); + const apiKey = resolveProviderApiKey(config.apiKey)?.trim(); if (!apiKey) return null; const response = await fetch(OPENCODE_GO_USAGE_URL, { headers: { Accept: "application/json", Authorization: `Bearer ${apiKey}` }, @@ -510,7 +510,7 @@ async function fetchOpenCodeGoQuota(provider: string, config: OcxProviderConfig) async function fetchOpenRouterQuota(provider: string, config: OcxProviderConfig): Promise { // Never send a configured API key to a lookalike host or through a redirect. if (!isCanonicalOpenRouterBaseUrl(config.baseUrl)) return null; - const apiKey = resolveEnvValue(config.apiKey)?.trim(); + const apiKey = resolveProviderApiKey(config.apiKey)?.trim(); if (!apiKey) return null; const response = await fetch(`${OPENROUTER_BASE_URL}/key`, { headers: { Accept: "application/json", Authorization: `Bearer ${apiKey}` }, @@ -557,7 +557,7 @@ async function fetchOpenRouterQuota(provider: string, config: OcxProviderConfig) */ async function fetchDeepSeekQuota(provider: string, config: OcxProviderConfig): Promise { if (!isCanonicalDeepSeekBaseUrl(config.baseUrl)) return null; - const apiKey = resolveEnvValue(config.apiKey)?.trim(); + const apiKey = resolveProviderApiKey(config.apiKey)?.trim(); if (!apiKey) return null; const response = await fetch(`${DEEPSEEK_BASE_URL}/user/balance`, { headers: { Accept: "application/json", Authorization: `Bearer ${apiKey}` }, @@ -602,7 +602,7 @@ async function fetchDeepSeekQuota(provider: string, config: OcxProviderConfig): */ async function fetchClineQuota(provider: string, config: OcxProviderConfig): Promise { if (!isCanonicalClineBaseUrl(config.baseUrl)) return null; - const apiKey = resolveEnvValue(config.apiKey)?.trim(); + const apiKey = resolveProviderApiKey(config.apiKey)?.trim(); if (!apiKey) return null; const response = await fetch(`${CLINE_BASE_URL}/api/v1/users/me/plan/usage-limits`, { headers: { Accept: "application/json", Authorization: `Bearer ${apiKey}` }, @@ -750,7 +750,7 @@ function parseZaiQuotaLegacyFields(data: Record | null): Provid */ async function fetchZaiQuota(provider: string, config: OcxProviderConfig): Promise { if (!isCanonicalZaiBaseUrl(config.baseUrl)) return null; - const apiKey = resolveEnvValue(config.apiKey)?.trim(); + const apiKey = resolveProviderApiKey(config.apiKey)?.trim(); if (!apiKey) return null; const normalized = normalizedBaseUrl(config.baseUrl); const monitorHost = normalized === ZAI_BASE_URL || normalized === `${ZAI_BASE_URL}/api/coding/paas/v4` @@ -793,7 +793,7 @@ async function fetchZaiQuota(provider: string, config: OcxProviderConfig): Promi */ async function fetchMinimaxQuota(provider: string, config: OcxProviderConfig): Promise { if (!isCanonicalMinimaxBaseUrl(config.baseUrl)) return null; - const apiKey = resolveEnvValue(config.apiKey)?.trim(); + const apiKey = resolveProviderApiKey(config.apiKey)?.trim(); if (!apiKey) return null; const cnHost = normalizedBaseUrl(config.baseUrl)?.startsWith("https://api.minimaxi.com"); const remainsUrl = cnHost ? "https://api.minimaxi.com/v1/token_plan/remains" : MINIMAX_REMAINS_URL; @@ -837,7 +837,7 @@ async function fetchMinimaxQuota(provider: string, config: OcxProviderConfig): P */ async function fetchMoonshotQuota(provider: string, config: OcxProviderConfig): Promise { if (!isCanonicalMoonshotBaseUrl(config.baseUrl)) return null; - const apiKey = resolveEnvValue(config.apiKey)?.trim(); + const apiKey = resolveProviderApiKey(config.apiKey)?.trim(); if (!apiKey) return null; const host = normalizedBaseUrl(config.baseUrl)?.startsWith("https://api.moonshot.cn") ? "https://api.moonshot.cn/v1" : MOONSHOT_BASE_URL; const response = await fetch(`${host}/users/me/balance`, { @@ -881,7 +881,7 @@ async function fetchMoonshotQuota(provider: string, config: OcxProviderConfig): */ async function fetchVeniceQuota(provider: string, config: OcxProviderConfig): Promise { if (!isCanonicalVeniceBaseUrl(config.baseUrl)) return null; - const apiKey = resolveEnvValue(config.apiKey)?.trim(); + const apiKey = resolveProviderApiKey(config.apiKey)?.trim(); if (!apiKey) return null; const response = await fetch(`${VENICE_BASE_URL}/billing/balance`, { headers: { Accept: "application/json", Authorization: `Bearer ${apiKey}` }, @@ -924,7 +924,7 @@ async function fetchVeniceQuota(provider: string, config: OcxProviderConfig): Pr */ async function fetchSyntheticQuota(provider: string, config: OcxProviderConfig): Promise { if (!isCanonicalSyntheticBaseUrl(config.baseUrl)) return null; - const apiKey = resolveEnvValue(config.apiKey)?.trim(); + const apiKey = resolveProviderApiKey(config.apiKey)?.trim(); if (!apiKey) return null; const response = await fetch(`${SYNTHETIC_BASE_URL}/quotas`, { headers: { Accept: "application/json", Authorization: `Bearer ${apiKey}` }, @@ -972,7 +972,7 @@ async function fetchSyntheticQuota(provider: string, config: OcxProviderConfig): */ async function fetchDeepInfraQuota(provider: string, config: OcxProviderConfig): Promise { if (!isCanonicalDeepInfraBaseUrl(config.baseUrl)) return null; - const apiKey = resolveEnvValue(config.apiKey)?.trim(); + const apiKey = resolveProviderApiKey(config.apiKey)?.trim(); if (!apiKey) return null; const response = await fetch(`${DEEPINFRA_BASE_URL}/payment/checklist?compute_owed=true`, { headers: { Accept: "application/json", Authorization: `Bearer ${apiKey}` }, @@ -1014,7 +1014,7 @@ async function fetchDeepInfraQuota(provider: string, config: OcxProviderConfig): */ async function fetchNeuralwattQuota(provider: string, config: OcxProviderConfig): Promise { if (!isCanonicalNeuralwattBaseUrl(config.baseUrl)) return null; - const apiKey = resolveEnvValue(config.apiKey)?.trim(); + const apiKey = resolveProviderApiKey(config.apiKey)?.trim(); if (!apiKey) return null; const response = await fetch(`${NEURALWATT_BASE_URL}/quota`, { headers: { Accept: "application/json", Authorization: `Bearer ${apiKey}` }, @@ -1810,7 +1810,7 @@ async function resolveKimiQuotaBearer(config: OcxProviderConfig): Promise 0 ? resolved : undefined; } diff --git a/src/server/management/oauth-account-routes.ts b/src/server/management/oauth-account-routes.ts index 3449d8c853..99662462d8 100644 --- a/src/server/management/oauth-account-routes.ts +++ b/src/server/management/oauth-account-routes.ts @@ -536,6 +536,35 @@ export async function handleOauthAccountRoutes(ctx: ManagementContext): Promise< clearKeyCooldowns(name); // manual key management resets 429 cooldown state return jsonResponse({ ok: true, id: result.id }, 201); } + // Opt-in OS keychain storage (#1221): move the active key and pool into the OS credential + // store (config keeps references), or restore plaintext. Store verifies the keychain before + // touching config so an unavailable store refuses instead of half-migrating. + if (url.pathname === "/api/providers/keychain" && req.method === "GET") { + const name = (url.searchParams.get("name") ?? "").trim(); + if (!name || !isValidProviderName(name) || !hasOwnProvider(config.providers, name)) return jsonResponse({ error: "unknown provider" }, 404); + const { probeProviderKeychain, providerKeyStoreKind } = await import("../../providers/key-store"); + const probe = probeProviderKeychain(); + return jsonResponse({ + name, + store: providerKeyStoreKind(config.providers[name]), + keychainAvailable: probe.available, + ...(probe.available ? {} : { keychainUnavailableReason: probe.reason }), + }); + } + if (url.pathname === "/api/providers/keychain" && req.method === "POST") { + const body = await readManagementJsonBodyOr(req, {}) as { name?: unknown; action?: unknown }; + const name = typeof body.name === "string" ? body.name.trim() : ""; + if (!name || !isValidProviderName(name) || !hasOwnProvider(config.providers, name)) return jsonResponse({ error: "unknown provider" }, 404); + if (body.action !== "store" && body.action !== "restore") return jsonResponse({ error: "action must be store or restore" }, 400); + const { storeProviderKeyInKeychain, restoreProviderKeyFromKeychain, providerKeyStoreKind } = await import("../../providers/key-store"); + const result = body.action === "store" + ? storeProviderKeyInKeychain(config, name) + : restoreProviderKeyFromKeychain(config, name); + if (!result.ok) return jsonResponse({ error: result.error }, result.status); + const { clearProviderQuotaCache } = await import("../../providers/quota"); + clearProviderQuotaCache(); + return jsonResponse({ ...result, name, store: providerKeyStoreKind(config.providers[name]) }); + } if (url.pathname === "/api/providers/keys/active" && req.method === "PUT") { const body = await readManagementJsonBodyOr(req, {}) as { name?: string; id?: string }; const name = (body.name ?? "").trim(); diff --git a/src/server/management/route-registry.ts b/src/server/management/route-registry.ts index 68c72285b5..0cdce78987 100644 --- a/src/server/management/route-registry.ts +++ b/src/server/management/route-registry.ts @@ -242,6 +242,8 @@ export const MANAGEMENT_ROUTES: readonly ManagementRoute[] = [ { method: "GET", path: "/api/oauth/providers", module: "server/management/oauth-account-routes", mutates: false }, { method: "GET", path: "/api/oauth/status", module: "server/management/oauth-account-routes", mutates: false }, { method: "GET", path: "/api/providers/keys", module: "server/management/oauth-account-routes", mutates: false }, + { method: "GET", path: "/api/providers/keychain", module: "server/management/oauth-account-routes", mutates: false }, + { method: "POST", path: "/api/providers/keychain", module: "server/management/oauth-account-routes", mutates: true }, { method: "PATCH", path: "/api/keys", module: "server/management/oauth-account-routes", mutates: true }, { method: "PATCH", path: "/api/oauth/accounts/pool", module: "server/management/oauth-account-routes", mutates: true }, { method: "POST", path: "/api/keys", module: "server/management/oauth-account-routes", mutates: true }, diff --git a/src/server/responses/compact.ts b/src/server/responses/compact.ts index be6d9381ea..570c415acf 100644 --- a/src/server/responses/compact.ts +++ b/src/server/responses/compact.ts @@ -3,8 +3,8 @@ import { bridgeToResponsesSSE, buildResponseJSON, formatErrorResponse, type Resp import { getConfigPath, multiAgentGuidanceEnabled, - resolveEnvValue, } from "../../config"; +import { resolveProviderApiKey } from "../../providers/key-store"; import { parseRequest } from "../../responses/parser"; import { buildCompactV1Output, COMPACT_PROMPT, decodeCompactionSummary, extractCompactUserMessages } from "../../responses/compaction"; import { FORWARD_HEADERS, sanitizeReasoningInputContent } from "../../adapters/openai-responses"; @@ -401,7 +401,7 @@ async function resolveAlternateCompactContext(args: { headers.set("authorization", `Bearer ${override.accessToken}`); headers.set("chatgpt-account-id", override.chatgptAccountId); } - if (provider.apiKey) headers.set("authorization", `Bearer ${resolveEnvValue(provider.apiKey)}`); + if (provider.apiKey) headers.set("authorization", `Bearer ${resolveProviderApiKey(provider.apiKey)}`); return { authCtx, provider, headers }; } catch (err) { if (err instanceof CodexMainProfileDrainingError) { @@ -640,7 +640,7 @@ export async function handleResponsesCompact( ? CODEX_FORWARD_BASE_URL : (compactProvider.baseUrl ?? "").replace(/\/+$/, ""); if (compactProvider.authMode !== "forward" && compactProvider.apiKey) { - headers.set("authorization", `Bearer ${resolveEnvValue(compactProvider.apiKey)}`); + headers.set("authorization", `Bearer ${resolveProviderApiKey(compactProvider.apiKey)}`); } const { reasoning: _reasoning, ...compactBodyRaw } = raw as typeof raw & { reasoning?: unknown }; // The regular /v1/responses path applies sanitizeReasoningInputContent via the adapter's diff --git a/tests/cli-headless-parity.test.ts b/tests/cli-headless-parity.test.ts index 94e223286a..68ca9a8a78 100644 --- a/tests/cli-headless-parity.test.ts +++ b/tests/cli-headless-parity.test.ts @@ -329,6 +329,20 @@ describe("headless GUI parity CLI", () => { expect(clearRuntime.requests[0]?.body).toEqual({ headers: null }); }); + test("provider keychain status/store/restore drive /api/providers/keychain", async () => { + const status = fakeRuntime(); + expect(await handleProviderRuntimeCommand("keychain", ["relay", "--json"], status.deps)).toBe(0); + expect(status.requests[0]).toMatchObject({ path: "/api/providers/keychain?name=relay" }); + + const store = fakeRuntime(); + expect(await handleProviderRuntimeCommand("keychain", ["relay", "store", "--json"], store.deps)).toBe(0); + expect(store.requests[0]).toMatchObject({ path: "/api/providers/keychain", method: "POST", body: { name: "relay", action: "store" } }); + + const bad = fakeRuntime(); + expect(await handleProviderRuntimeCommand("keychain", ["relay", "explode"], bad.deps)).toBe(2); + expect(bad.requests).toEqual([]); + }); + test("provider edit --retain-models sends the csv list and - clears it", async () => { const runtime = fakeRuntime(); const code = await handleProviderRuntimeCommand("edit", [ diff --git a/tests/provider-key-store.test.ts b/tests/provider-key-store.test.ts new file mode 100644 index 0000000000..c21518290e --- /dev/null +++ b/tests/provider-key-store.test.ts @@ -0,0 +1,194 @@ +import { afterEach, beforeEach, describe, expect, test } from "bun:test"; +import { mkdtempSync, readFileSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { loadConfig, saveConfig } from "../src/config"; +import { maskApiKey } from "../src/providers/api-keys"; +import { + PROVIDER_KEYCHAIN_SERVICE, + probeProviderKeychain, + providerKeyStoreKind, + resolveProviderApiKey, + restoreProviderKeyFromKeychain, + setProviderKeychainEntryFactoryForTests, + storeProviderKeyInKeychain, + type ProviderKeychainEntry, +} from "../src/providers/key-store"; +import { routedProviderConfig } from "../src/router"; +import { managementFetch as fetch } from "./helpers/management-auth"; +import { startServer } from "../src/server"; +import { installIsolatedCodexHome, type IsolatedCodexHome } from "./helpers/isolated-codex-home"; +import type { OcxConfig } from "../src/types"; + +/** In-memory keychain: service/account → secret, with optional fault injection. */ +function fakeKeychain(options: { unavailable?: boolean; readBackMismatch?: boolean } = {}) { + const store = new Map(); + const factory = (service: string, account: string): ProviderKeychainEntry => { + if (options.unavailable) throw new Error("Secret Service not reachable"); + const key = `${service}\u0000${account}`; + return { + getPassword: () => (options.readBackMismatch ? "different" : store.get(key) ?? null), + setPassword: value => { store.set(key, value); }, + deletePassword: () => store.delete(key), + }; + }; + return { store, factory }; +} + +let testDir = ""; +let previousHome: string | undefined; +let isolated: IsolatedCodexHome | null = null; +const SECRET = "sk-plain-key-material-1234567890"; +const POOL_SECRET = "sk-second-key-material-0987654321"; + +function baseConfig(): OcxConfig { + return { + port: 0, + hostname: "127.0.0.1", + defaultProvider: "relay", + providers: { + relay: { adapter: "openai-chat", baseUrl: "https://relay.example/v1", apiKey: SECRET }, + }, + } as OcxConfig; +} + +beforeEach(() => { + previousHome = process.env.OPENCODEX_HOME; + isolated = installIsolatedCodexHome("ocx-keychain-codex-"); + testDir = mkdtempSync(join(tmpdir(), "ocx-keychain-")); + process.env.OPENCODEX_HOME = testDir; + saveConfig(baseConfig()); +}); + +afterEach(() => { + setProviderKeychainEntryFactoryForTests(null); + if (previousHome === undefined) delete process.env.OPENCODEX_HOME; + else process.env.OPENCODEX_HOME = previousHome; + isolated?.restore(); + isolated = null; + if (testDir) rmSync(testDir, { recursive: true, force: true }); +}); + +describe("provider key resolver (#1221)", () => { + test("plain and env values resolve exactly as before", () => { + process.env.OCX_TEST_KEY_REF = "from-env"; + try { + expect(resolveProviderApiKey(SECRET)).toBe(SECRET); + expect(resolveProviderApiKey("${OCX_TEST_KEY_REF}")).toBe("from-env"); + expect(resolveProviderApiKey(undefined)).toBeUndefined(); + } finally { + delete process.env.OCX_TEST_KEY_REF; + } + }); + + test("keychain references resolve through the OS entry and fail closed when unreadable", () => { + const { store, factory } = fakeKeychain(); + store.set(`${PROVIDER_KEYCHAIN_SERVICE}\u0000relay`, SECRET); + setProviderKeychainEntryFactoryForTests(factory); + expect(resolveProviderApiKey("keychain:relay")).toBe(SECRET); + // routing clone carries the resolved secret so adapters keep working unchanged + expect(routedProviderConfig("relay", { adapter: "openai-chat", baseUrl: "https://relay.example/v1", apiKey: "keychain:relay" }).apiKey).toBe(SECRET); + + const warnings: string[] = []; + const original = console.warn; + console.warn = (...args: unknown[]) => { warnings.push(args.map(String).join(" ")); }; + try { + setProviderKeychainEntryFactoryForTests(fakeKeychain({ unavailable: true }).factory); + expect(resolveProviderApiKey("keychain:relay")).toBeUndefined(); + expect(resolveProviderApiKey("keychain:relay")).toBeUndefined(); + } finally { + console.warn = original; + } + expect(warnings).toHaveLength(1); + expect(warnings[0]).toContain("no plaintext fallback"); + expect(warnings[0]).not.toContain(SECRET); + }); + + test("references are non-secret for masking and store-kind reporting", () => { + expect(maskApiKey("keychain:relay")).toBe("keychain:relay"); + expect(providerKeyStoreKind({ apiKey: "keychain:relay" })).toBe("keychain"); + expect(providerKeyStoreKind({ apiKey: "${X}" })).toBe("env"); + expect(providerKeyStoreKind({ apiKey: SECRET })).toBe("file"); + expect(providerKeyStoreKind({})).toBe("none"); + }); +}); + +describe("store / restore", () => { + test("store moves the active key and pool into the keychain, config keeps references only", () => { + const { store, factory } = fakeKeychain(); + setProviderKeychainEntryFactoryForTests(factory); + const config = loadConfig(); + config.providers.relay!.apiKeyPool = [ + { id: "a1", key: SECRET }, + { id: "b2", key: POOL_SECRET }, + ]; + const result = storeProviderKeyInKeychain(config, "relay"); + expect(result).toEqual({ ok: true, moved: 2 }); + expect(config.providers.relay!.apiKey).toBe("keychain:relay/a1"); + expect(config.providers.relay!.apiKeyPool!.map(e => e.key)).toEqual(["keychain:relay/a1", "keychain:relay/b2"]); + const onDisk = readFileSync(join(testDir, "config.json"), "utf8"); + expect(onDisk).not.toContain(SECRET); + expect(onDisk).not.toContain(POOL_SECRET); + expect(onDisk).toContain("keychain:relay/a1"); + expect(store.size).toBe(2); + // resolves back to the plaintext at request time + expect(resolveProviderApiKey(config.providers.relay!.apiKey)).toBe(SECRET); + + const restored = restoreProviderKeyFromKeychain(config, "relay"); + expect(restored).toEqual({ ok: true, restored: 2 }); + expect(config.providers.relay!.apiKey).toBe(SECRET); + expect(config.providers.relay!.apiKeyPool!.map(e => e.key)).toEqual([SECRET, POOL_SECRET]); + expect(store.size).toBe(0); + }); + + test("store refuses and leaves config untouched when the keychain is unavailable or lies", () => { + for (const faulty of [fakeKeychain({ unavailable: true }), fakeKeychain({ readBackMismatch: true })]) { + setProviderKeychainEntryFactoryForTests(faulty.factory); + const config = loadConfig(); + const result = storeProviderKeyInKeychain(config, "relay"); + expect(result.ok).toBe(false); + if (!result.ok) expect(result.status).toBe(503); + expect(config.providers.relay!.apiKey).toBe(SECRET); + expect(readFileSync(join(testDir, "config.json"), "utf8")).toContain(SECRET); + expect(faulty.store.size).toBe(0); + } + expect(probeProviderKeychain().available).toBe(false); + }); + + test("management route: GET reports store kind, POST store/restore round-trips", async () => { + const { factory } = fakeKeychain(); + setProviderKeychainEntryFactoryForTests(factory); + const server = startServer(0); + try { + const before = await fetch(new URL("/api/providers/keychain?name=relay", server.url)).then(r => r.json()) as Record; + expect(before).toMatchObject({ name: "relay", store: "file", keychainAvailable: true }); + + const stored = await fetch(new URL("/api/providers/keychain", server.url), { + method: "POST", headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ name: "relay", action: "store" }), + }); + expect(stored.status).toBe(200); + expect(await stored.json()).toMatchObject({ ok: true, store: "keychain", moved: 1 }); + expect(readFileSync(join(testDir, "config.json"), "utf8")).not.toContain(SECRET); + + const list = await fetch(new URL("/api/providers/keys?name=relay", server.url)).then(r => r.json()) as { keys: Array<{ masked: string }> }; + expect(list.keys[0]!.masked).toBe("keychain:relay"); + + const bad = await fetch(new URL("/api/providers/keychain", server.url), { + method: "POST", headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ name: "relay", action: "explode" }), + }); + expect(bad.status).toBe(400); + + const restored = await fetch(new URL("/api/providers/keychain", server.url), { + method: "POST", headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ name: "relay", action: "restore" }), + }); + expect(restored.status).toBe(200); + expect(readFileSync(join(testDir, "config.json"), "utf8")).toContain(SECRET); + } finally { + await server.stop(true); + } + }); +}); + From 5fc7d073e0154a67a38e8e17989fdf36380f6c76 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 05:08:30 +0900 Subject: [PATCH 141/172] feat(cursor): seed claude-fable-5-1 at 1M ahead of the Cursor lineup update (#3211) Pre-register Claude Fable 5.1 on the Cursor surface under the three spellings Cursor has used for Claude ids (claude-fable-5-1, claude-fable-5.1, claude-5.1-fable), each with a 1M window, the full effort ladder, a thinking variant, and a verified-derived price overlay at the Anthropic list price. Any live id containing fable now infers a 1M window. Co-authored-by: jun --- src/adapters/cursor/catalog.ts | 30 ++++++++++++++++++++++++++++++ src/adapters/cursor/discovery.ts | 7 +++++++ src/adapters/cursor/effort-map.ts | 11 +++++++++++ src/usage/expected-prices.ts | 7 +++++++ tests/cursor-discovery.test.ts | 8 ++++++++ tests/cursor-effort-suffix.test.ts | 4 ++++ tests/usage-cost.test.ts | 12 ++++++++++-- 7 files changed, 77 insertions(+), 2 deletions(-) diff --git a/src/adapters/cursor/catalog.ts b/src/adapters/cursor/catalog.ts index 9c7fa8b2bb..f753f24e74 100644 --- a/src/adapters/cursor/catalog.ts +++ b/src/adapters/cursor/catalog.ts @@ -106,6 +106,36 @@ export const CURSOR_CAPABILITIES: Record = { thinking: { levels: FULL, order: T }, }, }, + // 260902 preemptive: Claude Fable 5.1 seeded ahead of Cursor's lineup update, mirroring + // claude-fable-5 (same 1M window and full effort ladder). Cursor has spelled Claude ids + // both Anthropic-style (`claude-opus-4-7`, thinking-then-effort) and version-first + // (`claude-4.6-opus`, effort-then-thinking), so all three plausible spellings are seeded; + // the live GetUsableModels filter drops whichever the roster does not expose. Collapse to + // the one real spelling once it is observed. + "claude-fable-5-1": { + window: CONTEXT_1M, + defaultVariant: "thinking", + variants: { + regular: { levels: FULL }, + thinking: { levels: FULL, order: T }, + }, + }, + "claude-fable-5.1": { + window: CONTEXT_1M, + defaultVariant: "thinking", + variants: { + regular: { levels: FULL }, + thinking: { levels: FULL, order: T }, + }, + }, + "claude-5.1-fable": { + window: CONTEXT_1M, + defaultVariant: "thinking", + variants: { + regular: { levels: FULL }, + thinking: { levels: FULL, order: E }, + }, + }, "claude-sonnet-5": { window: CONTEXT_1M, defaultVariant: "thinking", diff --git a/src/adapters/cursor/discovery.ts b/src/adapters/cursor/discovery.ts index 1b0709fd82..93819289e7 100644 --- a/src/adapters/cursor/discovery.ts +++ b/src/adapters/cursor/discovery.ts @@ -30,6 +30,8 @@ export function inferCursorContextWindow(modelId: string): number { if (id.includes("1m")) return CONTEXT_1M; if (id.startsWith("gemini-")) return CONTEXT_1M; if (id === "glm-5.3" || id === "glm-5.2") return CONTEXT_1M; + // 260902: every Fable is a 1M model; catch live spellings the seed does not carry. + if (id.includes("fable")) return CONTEXT_1M; if (id.startsWith("gpt-5.6-")) return CONTEXT_1M; if (id.startsWith("gpt-5") || id === "gpt-5-codex") return CONTEXT_272K; if (id.startsWith("grok-4.5") || id.startsWith("grok-4.6")) return 500_000; @@ -291,6 +293,11 @@ export const CURSOR_STATIC_MODELS: readonly CursorModelInfo[] = normalizeCursorM // quarantined regular wire id for the bare slug). { id: "claude-opus-5", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, { id: "claude-fable-5", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, + // 260902 preemptive: Fable 5.1 seeded ahead of Cursor's lineup update (mirrors fable-5) under + // the three spellings Cursor has used for Claude ids; see CURSOR_CAPABILITIES. + { id: "claude-fable-5-1", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, + { id: "claude-fable-5.1", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, + { id: "claude-5.1-fable", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, { id: "composer-1", contextWindow: CONTEXT_200K }, { id: "composer-2.5", contextWindow: CONTEXT_200K }, diff --git a/src/adapters/cursor/effort-map.ts b/src/adapters/cursor/effort-map.ts index 29525b4190..371e2f40be 100644 --- a/src/adapters/cursor/effort-map.ts +++ b/src/adapters/cursor/effort-map.ts @@ -23,6 +23,11 @@ const CURSOR_MODEL_EFFORT_TIERS: Record = { // max is always the top tier (canonical order: low < medium < high < xhigh < max), confirmed // against Anthropic's effort ladder docs and Cursor's live model lineup. "claude-fable-5": ["low", "medium", "high", "xhigh", "max"], + // 260902 preemptive: Fable 5.1 seeded ahead of Cursor's lineup update (mirrors fable-5) under + // the three spellings Cursor has used for Claude ids. + "claude-fable-5-1": ["low", "medium", "high", "xhigh", "max"], + "claude-fable-5.1": ["low", "medium", "high", "xhigh", "max"], + "claude-5.1-fable": ["low", "medium", "high", "xhigh", "max"], "claude-opus-4-7": ["low", "medium", "high", "xhigh", "max"], // Opus Fast tiers from the 260822 GetUsableModels dump (devlog .../300): the wire // exposes {base-without-fast}-{effort}-fast only; suffix derivation at the bottom of @@ -49,6 +54,9 @@ const CURSOR_MODEL_EFFORT_TIERS: Record = { "claude-opus-4-7-thinking-fast": ["low", "medium", "high", "xhigh", "max"], "claude-sonnet-5-thinking": ["low", "medium", "high", "xhigh", "max"], "claude-fable-5-thinking": ["low", "medium", "high", "xhigh", "max"], + "claude-fable-5-1-thinking": ["low", "medium", "high", "xhigh", "max"], + "claude-fable-5.1-thinking": ["low", "medium", "high", "xhigh", "max"], + "claude-5.1-fable-thinking": ["low", "medium", "high", "xhigh", "max"], "claude-4.6-opus-thinking": ["high", "max"], "claude-4.5-opus-thinking": ["high"], "claude-4.6-sonnet-thinking": ["medium"], @@ -113,6 +121,9 @@ const CURSOR_THINKING_FAMILIES: Readonly { expect(ids).toContain("glm-5.2"); expect(ids).toContain("kimi-k2.7-code"); expect(ids).toContain("kimi-k3"); + // 260902 preemptive seed: Fable 5.1 registered ahead of Cursor's lineup update, at 1M, + // under the three spellings Cursor has used for Claude ids. + for (const spelling of ["claude-fable-5-1", "claude-fable-5.1", "claude-5.1-fable"]) { + expect(ids).toContain(spelling); + expect(cursorModelContextWindows(CURSOR_STATIC_MODELS)[spelling]).toBe(1_000_000); + } + // Any live Fable spelling the seed does not carry still infers a 1M window. + expect(inferCursorContextWindow("claude-fable-6")).toBe(1_000_000); // Umbrella merge (devlog 260828): fast duplicate rows folded into bases. expect(ids).not.toContain("claude-opus-4-7-fast"); // 260709 refresh: stale ids dropped from the static seed (cursor.com docs); gpt-5.5-extra diff --git a/tests/cursor-effort-suffix.test.ts b/tests/cursor-effort-suffix.test.ts index ae7567f260..c509e2fc56 100644 --- a/tests/cursor-effort-suffix.test.ts +++ b/tests/cursor-effort-suffix.test.ts @@ -244,6 +244,10 @@ describe("#2569 Cursor explicit-thinking variants", () => { ["claude-opus-4-8-thinking-fast", "xhigh", "claude-opus-4-8-thinking-xhigh-fast"], ["claude-sonnet-5-thinking", "medium", "claude-sonnet-5-thinking-medium"], ["claude-fable-5-thinking", "xhigh", "claude-fable-5-thinking-xhigh"], + ["claude-fable-5-1-thinking", "xhigh", "claude-fable-5-1-thinking-xhigh"], + ["claude-fable-5.1-thinking", "xhigh", "claude-fable-5.1-thinking-xhigh"], + // Version-first spelling follows the 4.x families: marker at the END. + ["claude-5.1-fable-thinking", "max", "claude-5.1-fable-max-thinking"], // The marker moves to the END for these families. ["claude-4.6-opus-thinking", "max", "claude-4.6-opus-max-thinking"], ["claude-4.5-opus-thinking", "high", "claude-4.5-opus-high-thinking"], diff --git a/tests/usage-cost.test.ts b/tests/usage-cost.test.ts index bf15ab8470..f6bf02d482 100644 --- a/tests/usage-cost.test.ts +++ b/tests/usage-cost.test.ts @@ -195,6 +195,11 @@ describe("resolveMatchedPrice", () => { expect(price?.sourceRef).toContain("0.025x"); } expect(resolveMatchedPrice("anthropic-pb51d9b", "claude-fable-5-1")?.cost4).toEqual(COST4); + // Cursor seeds the id preemptively under three spellings and jawcode has no row, so + // each carries its own (derived) overlay rather than falling through to null. + for (const spelling of ["claude-fable-5-1", "claude-fable-5.1", "claude-5.1-fable"]) { + expect(resolveMatchedPrice("cursor", spelling), spelling).toMatchObject({ cost4: COST4, source: "expected", status: "verified-derived" }); + } // The cheaper cache-hit rate must not leak onto Fable 5, which stays at 0.1x. expect(resolveMatchedPrice("anthropic", "claude-fable-5")?.cost4.cacheRead).toBe(1); }); @@ -292,13 +297,16 @@ describe("resolveMatchedPrice", () => { expect(resolveMatchedPrice("openrouter", "anthropic-claude-3.5-sonnet")).toBeNull(); }); - test("16. shipped overlay membership: 58 keys, including Fable 5.1, Opus 5 and compatibility prices", () => { - expect(EXPECTED_PRICE_OVERLAYS.length).toBe(58); + test("16. shipped overlay membership: 61 keys, including Fable 5.1, Opus 5 and compatibility prices", () => { + expect(EXPECTED_PRICE_OVERLAYS.length).toBe(61); expect(EXPECTED_PRICE_OVERLAYS.some(row => row.status === "unverified")).toBe(false); const keys = new Set(EXPECTED_PRICE_OVERLAYS.map(row => `${row.provider}/${row.modelId}`)); for (const expected of [ "anthropic/claude-fable-5-1", "anthropic-apikey/claude-fable-5-1", + "cursor/claude-fable-5-1", + "cursor/claude-fable-5.1", + "cursor/claude-5.1-fable", "anthropic/claude-opus-5", "cursor/claude-opus-5", "kiro/claude-opus-5", From d975feaa47393783b03739f0afc4d5b418dd9f5a Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 05:11:56 +0900 Subject: [PATCH 142/172] feat(catalog): durable display names for discovered models (#2201, carry of #2715) (#3212) * docs: design discovered model display names * docs: plan discovered model display names core * feat(config): add discovered model display names * feat(catalog): apply provider model display names * feat(api): manage discovered model display names * docs: explain provider model display names * test: keep display name fixtures privacy safe * fix: harden display name mutation paths * docs: clarify discovered model display names * fix(config): preserve prototype shaped display name ids * fix(api): report display name catalog failures * docs: explain dashboard model naming controls * refactor: clarify display name boundaries * docs: clarify display name configuration limits * docs: clarify display label wording * fix: preserve fallback model labels across exports * devlog: wp10 plan and audit synthesis * test(key-store): use fixture keys the privacy scan does not flag --------- Co-authored-by: Zig Zag Co-authored-by: Zig Zag <127082290+zigzag-007@users.noreply.github.com> Co-authored-by: jun --- .../100_wp10_display_names_carry.md | 24 + .../101_wp10_audit_r1_synthesis.md | 9 + .../ja/reference/configuration/providers.md | 3 + .../ko/reference/configuration/providers.md | 3 + .../docs/reference/configuration/providers.md | 31 ++ .../ru/reference/configuration/providers.md | 10 + .../reference/configuration/providers.md | 3 + ...-26-discovered-model-display-names-core.md | 477 ++++++++++++++++++ ...6-discovered-model-display-names-design.md | 233 +++++++++ src/cli/opencode.ts | 3 +- src/codex/catalog/provider-fetch.ts | 13 + src/config.ts | 59 +++ src/config/provider-validation.ts | 40 ++ src/server/management/model-routes.ts | 80 ++- src/server/management/model-rows.ts | 24 +- src/server/management/provider-routes.ts | 7 + src/types/provider.ts | 2 + structure/02_config-and-codex-home.md | 2 +- structure/03_catalog-and-subagents.md | 6 +- tests/codex-catalog.test.ts | 166 ++++++ tests/codex-convergence-contract.test.ts | 6 +- tests/config-load-degrade.test.ts | 128 +++++ tests/config-user-edits.test.ts | 65 +++ tests/management-client-config-route.test.ts | 3 +- tests/management-provider-validation.test.ts | 66 +++ ...model-display-names-management-api.test.ts | 343 +++++++++++++ tests/opencode-cli.test.ts | 7 +- tests/provider-config-validation.test.ts | 31 ++ tests/provider-key-store.test.ts | 4 +- 29 files changed, 1835 insertions(+), 13 deletions(-) create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/100_wp10_display_names_carry.md create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/101_wp10_audit_r1_synthesis.md create mode 100644 docs/superpowers/plans/2026-08-26-discovered-model-display-names-core.md create mode 100644 docs/superpowers/specs/2026-08-26-discovered-model-display-names-design.md create mode 100644 tests/config-load-degrade.test.ts create mode 100644 tests/model-display-names-management-api.test.ts diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/100_wp10_display_names_carry.md b/devlog/_plan/260902_nonbug_adoption_backlog/100_wp10_display_names_carry.md new file mode 100644 index 0000000000..20f2cddc20 --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/100_wp10_display_names_carry.md @@ -0,0 +1,24 @@ +# wp10 — #2201 durable display names for discovered models (carry PR #2715) + +Issue #2201 (score 60). Two contributor drafts: #2715 core (+1800/-11, 26 files, no GUI) and #2716 +GUI editor (+3118, stacked on core). Reviewer (Ingwannu) explicitly asked for the core contract first +and the dashboard editor as a separately reviewed follow-up; #2715 is that core slice. Its earlier +head passed all 23 checks; later refreshes were blocked only on fork-workflow approval. + +## Decision +Carry #2715 by merge onto current `dev` (branch `codex/carry-2715-display-names`, merge +`47c24bce6`, author commits preserved). Two conflicts against wp4's retainModels landing were +resolved (POST carry-over block; providers.md row). #2716 stays open as the GUI follow-up and is +retargeted/rebased by its author after core lands. + +## Acceptance +- Review (grok subagent) confirms labels never become identity: routed slug, native id, wire model, + pricing key, disabled/selected/retain matches, alias/combo targets, dedupe untouched. +- Validation applied at load, PUT, and POST; prototype-key guards; 2,000 cap. +- No new imports into router/lifecycle/responses core. +- Focused: model-display-names-management-api, provider-config-validation, config-load-degrade, + config-user-edits, opencode-cli, codex-convergence-contract, management-client-config-route, + plus codex-catalog and management-provider-validation; tsc; privacy. +- Land via a new PR from the carry branch (the fork PR cannot be admin-merged with a fresh head + without fork CI), close #2715 as landed-via-carry with credit, close #2201, comment on #2716. + diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/101_wp10_audit_r1_synthesis.md b/devlog/_plan/260902_nonbug_adoption_backlog/101_wp10_audit_r1_synthesis.md new file mode 100644 index 0000000000..aceb0509ec --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/101_wp10_audit_r1_synthesis.md @@ -0,0 +1,9 @@ +# wp10 audit r1 — synthesis + +Reviewer: grok-4.6 subagent (Ptolemy), read-only review of the merge `47c24bce6`. Verdict **pass**, +no blockers: labels keyed by native id only (`configuredModelDisplayName` provider-fetch.ts:634, +`effectiveManagementDisplayName` model-rows.ts:49); fingerprint uses labels as cache key only; +validation at schema/load/POST/PUT with prototype guards and 2,000 cap; reset deterministic; no new +imports into router/lifecycle/responses core; merge conflict resolution preserved both retainModels +and modelDisplayNames without dropping the POST carry-over. + diff --git a/docs-site/src/content/docs/ja/reference/configuration/providers.md b/docs-site/src/content/docs/ja/reference/configuration/providers.md index 1467e36f83..6b6b4c2d6e 100644 --- a/docs-site/src/content/docs/ja/reference/configuration/providers.md +++ b/docs-site/src/content/docs/ja/reference/configuration/providers.md @@ -68,6 +68,7 @@ account を削除しても mapping は保持され、同じ id を再追加す | `models?` | `string[]` |シード/フォールバック モデルのリスト。 `liveModels: false` では、発見されたモデルはこれらのみです。 | | `liveModels?` | `boolean` |開始/同期時にライブ カタログをフェッチします (デフォルトは `true`)。カスタムプロバイダーは `${baseUrl}/models` を使用します。組み込みはレジストリ URL とフィルターを使用する場合があります。 | | `selectedModels?` | `string[]` |検出後のカタログ許可リスト。空でない場合は、それらの ID のみが公開されます。空または省略すると、検出されたすべてのモデルが公開されます。 | +| `modelDisplayNames?` | `Record` | このプロバイダーの正確なネイティブモデル ID をキーにした、永続的な表示専用ラベルです。大文字と小文字は区別されます。ラベルはプロバイダーカタログのメタデータより優先され、認証、アダプター、ルーティング、課金、上流リクエストには影響しません。マップは検出上限と同じ 2,000 件までです。 | | `contextWindow?` | `number` | アップストリームのメタデータが無い場合に使うプロバイダー全体のコンテキスト値。メタデータがある場合は上限として働き、より小さいライブ値をそのまま残します。Models ダッシュボードでは `providerContextCaps` とは別に設定します。 | | `modelContextWindows?` | `Record` | モデルごとのコンテキスト値および上限。`contextWindow` より優先され、ウィンドウが不明なら設定値を使い、より小さいライブメタデータがあればそちらが優先されます。 | | `modelInputModalities?` | `Record` | `["text"]` や `["text", "image"]` などのモデルごとの入力ヒント。 | @@ -339,6 +340,8 @@ Vercel AI Gateway は、1 つのモデルを複数の基盤となる推論プロ 検出を実行する必要があるが、選択した ID のみが Codex および `/v1/models` に表示される必要がある場合は、`selectedModels` を使用します。ダッシュボードには、後で許可リストを変更できるように、検出された完全なリストが保持されます。 +表示名には `modelDisplayNames` を使用します。優先順位は、運用者が設定した `modelDisplayNames`、プロバイダーカタログのメタデータ、通常の `provider/model` 表示の順です。キーはこのプロバイダー内の正確なネイティブモデル ID です。例えば `xai/grok-4.6` のキーは `grok-4.6` です。ラベルは表示専用で、正確なルーティング ID や上流モデル ID を変更しません。`config.json` の既存プロバイダー設定にこのフィールドだけを追加し、他のすべてのフィールドを残してください。`PUT /api/providers/:provider/model-display-names` に `{ "modelId": "grok-4.6", "displayName": "Grok 4.6" }` を送ると保存され、`displayName: null` を送るとその名前だけがリセットされます。 + プレビュー GPT-5.6 フォールバック エントリは同じメカニズムを使用します。 OpenAI API キー プリセットは、ベース ID と Pro ID にコンテキスト `922000` と最大入力 `922000` をシードします。 OpenRouter は、コンテキスト `922000` を持つ `openai/gpt-5.6-sol`、`openai/gpt-5.6-terra`、および `openai/gpt-5.6-luna` をシードします。プール/ダイレクトは `922000` をアドバタイズします。同期されたカタログは、`xhigh` を区別しつつ、`max` をアドバタイズします。 ```json diff --git a/docs-site/src/content/docs/ko/reference/configuration/providers.md b/docs-site/src/content/docs/ko/reference/configuration/providers.md index ba5980d1f8..fa530907bc 100644 --- a/docs-site/src/content/docs/ko/reference/configuration/providers.md +++ b/docs-site/src/content/docs/ko/reference/configuration/providers.md @@ -68,6 +68,7 @@ managed map을 활성화하면 privacy-safe selector를 만들고, 이후 계정 | `models?` | `string[]` | 시드/폴백 모델 목록입니다. `liveModels: false`이면 이 목록만 발견된 모델로 취급합니다. | | `liveModels?` | `boolean` | 시작 또는 동기화 시 라이브 카탈로그를 가져옵니다. 기본값은 `true`입니다. 사용자 지정 공급자는 `${baseUrl}/models`를 사용하고, 내장은 레지스트리 URL을 사용한 뒤 필터링할 수 있습니다. | | `selectedModels?` | `string[]` | 발견 후 카탈로그 허용 목록입니다. 값이 비어 있지 않으면 그 id만 노출하고, 비어 있거나 생략하면 발견된 모델을 모두 노출합니다. | +| `modelDisplayNames?` | `Record` | 이 공급자의 정확한 네이티브 모델 id를 키로 쓰는 영구 표시 전용 이름입니다. 키는 대소문자를 구분합니다. 이름은 공급자 카탈로그 메타데이터보다 우선하며 인증, 어댑터, 라우팅, 청구 또는 업스트림 요청을 바꾸지 않습니다. 맵은 발견 한도와 같은 최대 2,000개 항목을 가질 수 있습니다. | | `contextWindow?` | `number` | 업스트림 메타데이터가 없을 때 쓰이는 공급자 전반의 컨텍스트 값입니다. 메타데이터가 있으면 상한으로 동작해 더 작은 라이브 값을 그대로 둡니다. Models 대시보드에서 `providerContextCaps`와 별도로 설정합니다. | | `modelContextWindows?` | `Record` | 모델별 컨텍스트 값이자 상한입니다. `contextWindow`보다 우선하며, 창 크기를 알 수 없으면 설정값을 쓰고 더 작은 라이브 메타데이터가 있으면 그쪽을 따릅니다. | | `modelInputModalities?` | `Record` | `["text"]` 또는 `["text", "image"]` 같은 모델별 입력 힌트입니다. | @@ -346,6 +347,8 @@ Vercel AI Gateway는 하나의 모델을 여러 기반 추론 공급자에 걸 `selectedModels`는 발견은 계속하되, 선택된 id만 Codex와 `/v1/models`에 나타나게 하고 싶을 때 사용합니다. 대시보드는 나중에 허용 목록을 바꿀 수 있도록 발견된 전체 목록을 보관합니다. +표시 이름은 `modelDisplayNames`로 설정합니다. 우선순위는 운영자가 설정한 `modelDisplayNames`, 공급자 카탈로그 메타데이터, 일반 `provider/model` 표시 순서입니다. 키는 이 공급자 안의 정확한 네이티브 모델 id입니다. 예를 들어 `xai/grok-4.6`의 키는 `grok-4.6`입니다. 이름은 표시 전용이며 정확한 라우팅 id나 업스트림 모델 id를 바꾸지 않습니다. `config.json`의 기존 공급자 설정에 이 필드만 추가하고 다른 모든 필드는 유지하세요. `PUT /api/providers/:provider/model-display-names`에 `{ "modelId": "grok-4.6", "displayName": "Grok 4.6" }`를 보내 저장하고, `displayName: null`을 보내 해당 이름만 초기화합니다. + 프리뷰 GPT-5.6 폴백 항목도 같은 메커니즘을 사용합니다. OpenAI API 키 프리셋은 base와 Pro id에 컨텍스트 `922000`, 최대 입력 `922000`을 채웁니다. OpenRouter는 `openai/gpt-5.6-sol`, `openai/gpt-5.6-terra`, `openai/gpt-5.6-luna`에 컨텍스트 `922000`을 채웁니다. Pool/Direct는 `922000`을 노출하고, 동기화된 카탈로그는 `xhigh`를 구분한 채 `max`를 노출합니다. ```json diff --git a/docs-site/src/content/docs/reference/configuration/providers.md b/docs-site/src/content/docs/reference/configuration/providers.md index 86915fa962..bbc9914a73 100644 --- a/docs-site/src/content/docs/reference/configuration/providers.md +++ b/docs-site/src/content/docs/reference/configuration/providers.md @@ -84,6 +84,7 @@ differing backup and rewrites known legacy namespaced selected ids to bare ids. | `liveModels?` | `boolean` | Fetch the live catalog on start/sync (default `true`). Custom providers use `${baseUrl}/models`; built-ins may use a registry URL and filter. | | `selectedModels?` | `string[]` | Catalog allowlist after discovery. Non-empty exposes only those ids; empty or omitted exposes all discovered models. | | `retainModels?` | `string[]` | Ids kept in the catalog even when live discovery omits them. They need not be repeated in `models`. Empty or omitted keeps today's behavior. | +| `modelDisplayNames?` | `Record` | Durable labels used only for display, keyed by this provider's exact upstream model id. Labels win over provider catalog metadata, survive discovery refreshes and provider edits, and never change authentication, adapter behavior, routing, billing, upstream request construction, the routed `provider/model` selector, or the upstream wire model. Keys are exact and case sensitive. Unknown model ids are kept so a temporarily missing model receives its label when it returns. The map accepts at most 2,000 entries, matching the discovery limit. | | `contextWindow?` | `number` | Provider-wide context fallback when upstream metadata is absent; otherwise a cap that retains smaller live metadata. The Models dashboard exposes this separately from `providerContextCaps`. | | `modelContextWindows?` | `Record` | Per-model context fallbacks/caps. These override `contextWindow`: an unknown window uses the configured value, while smaller live metadata remains authoritative. | | `modelInputModalities?` | `Record` | Per-model input hints such as `["text"]` or `["text", "image"]`. | @@ -142,6 +143,36 @@ differing backup and rewrites known legacy namespaced selected ids to bare ids. | `unsafeAllowNativeLocalExec?` | `boolean` | Cursor legacy boolean, equivalent to `nativeLocalExec: "on"` only when the newer field is unset. | | `nativeLocalExec?` | `"off" \| "codex-sandbox" \| "on"` | Cursor local-exec policy. `off` is default; `codex-sandbox` currently fails closed like `off`. | +### Discovered model display names + +Use `modelDisplayNames` when a provider returns machine friendly ids but the Codex model picker +needs shorter labels. The map belongs to one provider, so the same model id can have a different +label under another provider. Add the field to the existing provider row in `config.json` and keep +all other provider settings. The example includes the surrounding required fields for context: + +```json +{ + "providers": { + "xai": { + "adapter": "openai-chat", + "baseUrl": "https://api.x.ai/v1", + "modelDisplayNames": { + "grok-4.6": "Grok 4.6" + } + } + } +} +``` + +The effective label order is operator `modelDisplayNames`, then provider catalog metadata, then the +normal `provider/model` fallback. The routed selector remains `xai/grok-4.6`, while the upstream +wire model remains `grok-4.6`. Labels are display only. They do not change authentication, adapter +behavior, routing, billing, or upstream request construction. Removing a map entry resets only its +label. A management client can set or reset one label with +`PUT /api/providers/:provider/model-display-names` and a body of +`{ "modelId": "grok-4.6", "displayName": "Grok 4.6" }`; send `displayName: null` to reset it. +Provider `PATCH` does not edit this map. Use this dedicated `PUT` endpoint to change or remove labels. + ## Codex catalog and root `config.toml` settings These settings belong in the root of `$CODEX_HOME/config.toml`, alongside diff --git a/docs-site/src/content/docs/ru/reference/configuration/providers.md b/docs-site/src/content/docs/ru/reference/configuration/providers.md index fc76d5a456..8e0956d18e 100644 --- a/docs-site/src/content/docs/ru/reference/configuration/providers.md +++ b/docs-site/src/content/docs/ru/reference/configuration/providers.md @@ -81,6 +81,7 @@ cross-route credential fallback не существует. Строки API GPT- | `models?` | `string[]` | Seed/fallback-список моделей. При `liveModels: false` это и есть единственный список обнаруженных моделей. | | `liveModels?` | `boolean` | Получать live-каталог на start/sync (по умолчанию `true`). Custom-провайдеры используют `${baseUrl}/models`; built-in могут использовать registry URL и дополнительно фильтровать результат. | | `selectedModels?` | `string[]` | Allowlist каталога после discovery. Непустой список показывает только эти id; пустой или отсутствующий показывает всё, что было обнаружено. | +| `modelDisplayNames?` | `Record` | Постоянные display-only имена с точным нативным id модели этого провайдера в качестве ключа. Ключи чувствительны к регистру. Имена имеют приоритет над metadata каталога провайдера и не меняют аутентификацию, adapter, routing, billing или upstream-запросы. Карта содержит не более 2 000 записей, как и discovery. | | `contextWindow?` | `number` | Значение контекста для всего провайдера, применяемое когда upstream не отдаёт metadata; при наличии metadata работает как cap и сохраняет более маленькое live-значение. Панель Models настраивает его отдельно от `providerContextCaps`. | | `modelContextWindows?` | `Record` | Значения и cap'ы контекста по отдельным моделям. Перекрывают `contextWindow`: если окно неизвестно, берётся заданное значение, а более маленькая live-metadata остаётся авторитетной. | | `modelInputModalities?` | `Record` | Подсказки modality по модели, например `["text"]` или `["text", "image"]`. | @@ -431,6 +432,15 @@ malformed-результаты откатываются к stale/configured fall должны появляться только избранные id. Дашборд всё равно сохраняет полный обнаруженный список для дальнейших изменений allowlist'а. +Используйте `modelDisplayNames` для отображаемых имён. Порядок приоритета: заданное оператором +`modelDisplayNames`, metadata каталога провайдера, затем обычная подпись `provider/model`. Ключом +служит точный нативный id модели внутри этого провайдера: для `xai/grok-4.6` это `grok-4.6`. +Имя влияет только на отображение и не меняет точный routing id или upstream model id. Добавляйте +это поле в существующую запись провайдера в `config.json`, сохраняя все остальные поля. Отправьте +`{ "modelId": "grok-4.6", "displayName": "Grok 4.6" }` в +`PUT /api/providers/:provider/model-display-names`, чтобы сохранить имя, или `displayName: null`, +чтобы сбросить только это имя. + Preview fallback-записи GPT-5.6 используют тот же механизм. Preset OpenAI API-key заранее засевает base- и Pro-id с context `922000` и max input `922000`; OpenRouter заранее засевает `openai/gpt-5.6-sol`, `openai/gpt-5.6-terra` и `openai/gpt-5.6-luna` с context `922000`. diff --git a/docs-site/src/content/docs/zh-cn/reference/configuration/providers.md b/docs-site/src/content/docs/zh-cn/reference/configuration/providers.md index 229f6c205d..2a41c6a3b1 100644 --- a/docs-site/src/content/docs/zh-cn/reference/configuration/providers.md +++ b/docs-site/src/content/docs/zh-cn/reference/configuration/providers.md @@ -68,6 +68,7 @@ selector,而不是分配一个新名称。 | `models?` | `string[]` | 种子/回退模型列表。配合 `liveModels: false` 时,这些就是唯一发现到的模型。 | | `liveModels?` | `boolean` | 启动/同步时获取实时目录(默认 `true`)。自定义提供者使用 `${baseUrl}/models`;内置项可能使用注册表 URL 并进行过滤。 | | `selectedModels?` | `string[]` | 发现之后的目录允许列表。非空时只暴露这些 id;为空或省略时则暴露全部发现到的模型。 | +| `modelDisplayNames?` | `Record` | 持久的仅显示名称,以此提供者的精确原生模型 id 为键。键区分大小写。名称优先于提供者目录元数据,并且不会改变身份验证、适配器、路由、计费或上游请求。该映射最多可包含 2,000 个条目,与发现上限相同。 | | `contextWindow?` | `number` | 上游缺少元数据时使用的提供者级上下文数值;有元数据时作为上限,保留更小的实时数值。Models 面板中与 `providerContextCaps` 分开设置。 | | `modelContextWindows?` | `Record` | 按模型设置的上下文数值与上限。优先于 `contextWindow`:窗口未知时采用所配置的数值,而更小的实时元数据仍然优先。 | | `modelInputModalities?` | `Record` | 按模型设置的输入提示,例如 `["text"]` 或 `["text", "image"]`。 | @@ -342,6 +343,8 @@ Vercel AI Gateway 可以在多个底层推理提供者之间路由一个模型 当需要继续运行发现,但只有选定 id 应该出现在 Codex 和 `/v1/models` 中时,请使用 `selectedModels`。仪表板会保留完整的已发现列表,以便之后调整允许列表。 +请使用 `modelDisplayNames` 设置显示名称。优先顺序是操作者设置的 `modelDisplayNames`、提供者目录元数据,然后是普通的 `provider/model` 显示。键是此提供者内精确的原生模型 id,例如 `xai/grok-4.6` 的键是 `grok-4.6`。名称只改变显示,不会改变精确路由 id 或上游模型 id。请只把此字段加入 `config.json` 中现有的提供者设置,并保留所有其他字段。向 `PUT /api/providers/:provider/model-display-names` 发送 `{ "modelId": "grok-4.6", "displayName": "Grok 4.6" }` 可保存名称,发送 `displayName: null` 只重置该名称。 + 预览版 GPT-5.6 回退条目使用相同机制。OpenAI API key 预设会为基础和 Pro id 设定 `922000` 上下文和 `922000` 最大输入;OpenRouter 会为 `openai/gpt-5.6-sol`、`openai/gpt-5.6-terra` 和 `openai/gpt-5.6-luna` 设定 `922000` 上下文。Pool/Direct 会声明 `922000`;同步后的目录会声明 `max`,同时保留 `xhigh` 的独立性。 ```json diff --git a/docs/superpowers/plans/2026-08-26-discovered-model-display-names-core.md b/docs/superpowers/plans/2026-08-26-discovered-model-display-names-core.md new file mode 100644 index 0000000000..35ca7d61da --- /dev/null +++ b/docs/superpowers/plans/2026-08-26-discovered-model-display-names-core.md @@ -0,0 +1,477 @@ +# Discovered Model Display Names Core Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Add durable provider scoped display names for discovered models, with safe config handling, catalog propagation, and a management API, without changing routing identity. + +**Architecture:** Store operator labels in `providers..modelDisplayNames`, keyed by the exact native model ID. Apply the label at the shared provider catalog hint boundary, expose the effective name and source in management rows, and mutate one label through a provider scoped API route that persists safely and converges the Codex catalog. + +**Tech Stack:** Bun, TypeScript, Zod, Bun test, OpenCodex management API, Astro documentation. + +## Global Constraints + +- Base all work on the latest `upstream/dev` commit. +- Keep native provider IDs, model IDs, routed slugs, aliases, pricing, effort metadata, context metadata, modalities, fallbacks, and outbound requests unchanged. +- Operator display names take precedence over trusted provider metadata, which takes precedence over the existing fallback. +- Unknown or temporarily absent model IDs remain stored. +- A reset removes only the selected entry. +- Invalid hand edits degrade entry by entry and must not remove the provider. +- Management writes restore the in memory state if persistence fails. +- Catalog convergence runs exactly once after a successful persistence. +- Do not read or modify the user's live OpenCodex config, credentials, or Codex catalog. +- Write every production behavior test first and observe the expected failure. +- The core pull request and dashboard pull request remain separate. +- Do not push or open a pull request until the user sees the verified result. + +--- + +## File Map + +- `src/types/provider.ts`: declares the provider scoped display name map. +- `src/config/provider-validation.ts`: validates exact model ID keys and safe display values. +- `src/config.ts`: adds schema validation and safe load degradation. +- `src/codex/catalog/provider-fetch.ts`: resolves operator display names at the shared catalog boundary. +- `src/server/management/model-rows.ts`: exposes effective display names and their source. +- `src/server/management/model-routes.ts`: sets and resets one provider model display name. +- `tests/provider-config-validation.test.ts`: covers strict validator behavior. +- `tests/config-load-degrade.test.ts`: covers safe hand edited config loading. +- `tests/config-user-edits.test.ts`: covers persistence and concurrent unrelated map edits. +- `tests/codex-catalog.test.ts`: covers label precedence and routing invariants. +- `tests/model-display-names-management-api.test.ts`: covers read and mutation API behavior. +- `docs-site/src/content/docs/reference/configuration/providers.md`: documents the field and exact key rules. +- `structure/02_config-and-codex-home.md`: records the new persisted provider field. +- `structure/03_catalog-and-subagents.md`: records display precedence at catalog assembly. + +--- + +### Task 1: Provider Config Contract + +**Files:** +- Modify: `src/types/provider.ts` +- Modify: `src/config/provider-validation.ts` +- Modify: `src/config.ts` +- Test: `tests/provider-config-validation.test.ts` +- Test: `tests/config-load-degrade.test.ts` + +**Interfaces:** +- Produces: `OcxProviderConfig.modelDisplayNames?: Record` +- Produces: `modelDisplayNamesConfigError(value: unknown, field?: string): string | null` +- Produces: load normalization that trims valid labels and removes only invalid entries. + +- [ ] **Step 1: Add failing strict validation tests** + +Add table driven tests that call `modelDisplayNamesConfigError` directly. The valid cases are an absent map, an empty plain map, native IDs containing `/`, and a trimmed label up to 128 characters. The invalid cases are an array, a class or prototype shaped object, more than 2,000 entries, blank keys, keys longer than 1,024 characters, nonstring values, blank values, values longer than 128 characters, `/`, and control characters. + +Use literal expectations such as: + +```ts +expect(modelDisplayNamesConfigError({ "models/grok-4.6": "Grok 4.6" })).toBeNull(); +expect(modelDisplayNamesConfigError({ "grok-4.6": "Grok/4.6" })).toContain("must not contain /"); +expect(modelDisplayNamesConfigError({ "grok-4.6": "Grok\n4.6" })).toContain("control characters"); +``` + +- [ ] **Step 2: Run strict tests and confirm RED** + +Run: + +```text +bun test tests/provider-config-validation.test.ts +``` + +Expected: failure because `modelDisplayNamesConfigError` does not exist. + +- [ ] **Step 3: Implement the minimal validator and type** + +Add this field beside `modelAliases`: + +```ts +/** Display-only labels for exact native model ids discovered under this provider. */ +modelDisplayNames?: Record; +``` + +Implement one pure validator using `MODEL_DISCOVERY_MAX_MODELS` and `isValidModelDiscoveryModelId` from `src/providers/model-discovery-limits.ts`: + +```ts +export function modelDisplayNamesConfigError( + value: unknown, + field = "modelDisplayNames", +): string | null; +``` + +The validator accepts only a plain own property object, at most 2,000 entries, exact valid model IDs no longer than 1,024 characters, and string labels whose trimmed form is 1 through 128 characters with no `/` or control characters. + +- [ ] **Step 4: Run strict tests and confirm GREEN** + +Run: + +```text +bun test tests/provider-config-validation.test.ts +``` + +Expected: all tests pass. + +- [ ] **Step 5: Add failing schema and load degradation tests** + +Add tests proving: + +```ts +expect(validateConfigCandidate(validConfigWithNames).ok).toBe(true); +expect(validateConfigCandidate(configWithBlankName).ok).toBe(false); +``` + +Write a real config file fixture containing one valid and one invalid label. Assert that `loadConfig()` keeps the provider and its unrelated fields, trims the valid label, removes the invalid entry, and logs no raw value or secret shaped provider name. Also test a nonobject map and a future absent model ID. + +- [ ] **Step 6: Run config tests and confirm RED** + +Run: + +```text +bun test tests/config-load-degrade.test.ts tests/provider-config-validation.test.ts +``` + +Expected: the candidate accepts unvalidated values or the load path does not sanitize them. + +- [ ] **Step 7: Add schema refinement and safe load sanitizer** + +Declare the field in `providerConfigSchema`: + +```ts +modelDisplayNames: z.record(z.string(), z.string()).optional(), +``` + +Call `modelDisplayNamesConfigError` in the outer provider refinement and report the redacted path: + +```ts +["providers", redactSecretString(name), "modelDisplayNames"] +``` + +Add `sanitizeModelDisplayNamesForLoad(parsed)` before `configSchema.safeParse(parsed)`. It must delete a malformed whole map, remove invalid entries one at a time, trim valid values, omit an empty map, and log only redacted provider names and JSON escaped model IDs. It must never log label values. + +- [ ] **Step 8: Run config tests and confirm GREEN** + +Run: + +```text +bun test tests/config-load-degrade.test.ts tests/provider-config-validation.test.ts +``` + +Expected: all tests pass with no unexpected warnings. + +- [ ] **Step 9: Commit the config contract** + +```text +git add src/types/provider.ts src/config/provider-validation.ts src/config.ts tests/provider-config-validation.test.ts tests/config-load-degrade.test.ts +git commit -m "feat(config): add discovered model display names" +``` + +--- + +### Task 2: Catalog Display Precedence + +**Files:** +- Modify: `src/codex/catalog/provider-fetch.ts` +- Test: `tests/codex-catalog.test.ts` + +**Interfaces:** +- Consumes: `OcxProviderConfig.modelDisplayNames` +- Produces: `configuredModelDisplayName(provider, modelId): string | undefined` +- Produces: `applyProviderConfigHints` with operator first display precedence. + +- [ ] **Step 1: Add failing catalog behavior tests** + +Add focused tests that create real `CatalogModel` inputs and assert: + +```ts +const output = applyProviderConfigHints("xai", provider, discovered); +expect(output.id).toBe("grok-4.6"); +expect(catalogModelSlug(output)).toBe("xai/grok-4.6"); +expect(output.displayName).toBe("Grok 4.6"); +``` + +Cover exact case sensitive matching, same native ID under two providers, operator override over provider metadata, metadata fallback when the override is absent, reset fallback, discovery success, stale cache fallback, configured fallback after discovery failure, and repeated gathers. Compare all non display fields before and after, including cost, context, max input, compact limit, modalities, efforts, service tier, priority, alias, and fallback targets. Assert no duplicate routed slug appears. Assert custom model display names remain unchanged. + +- [ ] **Step 2: Run catalog tests and confirm RED** + +Run: + +```text +bun test tests/codex-catalog.test.ts +``` + +Expected: the discovered row keeps its old metadata or slug instead of the configured label. + +- [ ] **Step 3: Implement the exact display resolver** + +Add: + +```ts +export function configuredModelDisplayName( + provider: OcxProviderConfig, + modelId: string, +): string | undefined { + if (!provider.modelDisplayNames || !Object.hasOwn(provider.modelDisplayNames, modelId)) return undefined; + const value = provider.modelDisplayNames[modelId]; + return typeof value === "string" && value.trim() ? value.trim() : undefined; +} +``` + +In `applyProviderConfigHints`, spread the configured display name after the incoming model so it overrides trusted metadata only when present. Do not call `modelRecordValue`, case fold IDs, or use the routed slug as the lookup key. + +Add `modelDisplayNames` to `providerCatalogFingerprint`, because `gatherFlightKey` decides which active gather promise may be reused before the full provider graph identity is compared. + +- [ ] **Step 4: Run catalog tests and confirm GREEN** + +Run: + +```text +bun test tests/codex-catalog.test.ts +``` + +Expected: all catalog tests pass and routing identity stays byte equivalent. + +- [ ] **Step 5: Commit catalog propagation** + +```text +git add src/codex/catalog/provider-fetch.ts tests/codex-catalog.test.ts +git commit -m "feat(catalog): apply provider model display names" +``` + +--- + +### Task 3: Management Read and Mutation API + +**Files:** +- Modify: `src/server/management/model-rows.ts` +- Modify: `src/server/management/model-routes.ts` +- Create: `tests/model-display-names-management-api.test.ts` + +**Interfaces:** +- Produces: `ManagementModelRow.displayNameSource?: "operator" | "provider" | "fallback"` +- Produces: `ManagementModelRow.displayNameOverride?: string` +- Produces: `effectiveManagementDisplayName(config, model): { displayName: string; displayNameOverride?: string; displayNameSource: "operator" | "provider" | "fallback" }` +- Produces: `PUT /api/providers/:provider/model-display-names` +- Consumes body: `{ modelId: string; displayName: string | null }` + +- [ ] **Step 1: Add failing read surface tests** + +Use `listManagementModelRows` with a real provider model fixture. Assert the row contains the effective `displayName`, stored `displayNameOverride`, and source `operator`. Test provider metadata source and fallback source separately. Assert serialized rows contain no API key, headers, account email, or unrelated provider config. + +- [ ] **Step 2: Run read tests and confirm RED** + +Run: + +```text +bun test tests/model-display-names-management-api.test.ts +``` + +Expected: `displayNameOverride` and `displayNameSource` are absent. + +- [ ] **Step 3: Add effective name metadata to management rows** + +Add one pure helper and use it for routed nonnative rows. Look up the exact provider and native ID. Return: + +```ts +displayNameOverride?: string; +displayNameSource?: "operator" | "provider" | "fallback"; +``` + +Use `operator` when the exact configured map owns the ID, `provider` when `CatalogModel.displayName` exists without an override, and `fallback` otherwise. The fallback display name is the existing routed catalog slug, so the read surface always gives the dashboard the exact visible text. Do not add these fields to native OpenAI rows in this core change. Keep custom rows on their existing custom model contract. + +- [ ] **Step 4: Run read tests and confirm GREEN** + +Run: + +```text +bun test tests/model-display-names-management-api.test.ts +``` + +Expected: read tests pass. + +- [ ] **Step 5: Add failing mutation tests** + +Call `handleModelRoutes` with real `Request` objects and an in memory config. Cover: + +- set trims and stores one label +- set works for a temporarily absent model ID +- reset removes only the target and omits an empty map +- unknown provider returns 404 +- malformed JSON, missing fields, blank model ID, blank label, slash, control character, oversized label, and nonstring value return 400 +- validation failure does not persist or converge +- successful set and reset persist once and converge once +- persistence failure restores the previous map and does not converge +- convergence failure keeps the persisted label and returns the existing catalog disposition or bounded error pattern +- two sequential updates preserve neighboring entries + +Use a persistence seam that clones the actual config snapshot. Assert final state, not only mock call counts. + +- [ ] **Step 6: Run mutation tests and confirm RED** + +Run: + +```text +bun test tests/model-display-names-management-api.test.ts +``` + +Expected: route returns `null` or 404 because it is not registered. + +- [ ] **Step 7: Implement provider scoped mutation route** + +Match: + +```ts +const displayNameMatch = url.pathname.match(/^\/api\/providers\/([^/]+)\/model-display-names$/); +``` + +Decode the provider, reject the reserved `keys` route, verify provider ownership with `hasOwnProvider`, parse the bounded JSON body, validate the exact native model ID and a one entry map through `modelDisplayNamesConfigError`, and use `null` only for reset. + +Build a detached next map and assign it only after validation. Keep a detached copy of the old map. Wrap `persistConfig(config)` in `try/catch`; on failure restore the old field exactly, including absence, then rethrow so the management boundary returns its normal bounded server error. After persistence succeeds, call `convergeCodexCatalog()` once. Read the resulting routed row through `listManagementModelRows(config)` and use `effectiveManagementDisplayName`; if the temporarily absent ID has no row, return the stored operator label or `routedSlug(name, modelId)` as the fallback. Return: + +```ts +{ + ok: true, + provider: name, + modelId, + displayName: effectiveDisplayName, + displayNameOverride: storedNameOrNull, + displayNameSource, + catalogRefresh, +} +``` + +Do not require the model ID to exist in live discovery. + +- [ ] **Step 8: Run mutation tests and confirm GREEN** + +Run: + +```text +bun test tests/model-display-names-management-api.test.ts +``` + +Expected: all API tests pass. + +- [ ] **Step 9: Add concurrent config merge regression** + +In `tests/config-user-edits.test.ts`, start with two labels. Change one label in the live config, change the other on disk, call `saveConfigPreservingClaudeCode`, and assert both changes survive. Add a second test where the live writer removes one label while the disk writer adds a different label. + +- [ ] **Step 10: Run persistence tests and confirm RED or existing support** + +Run: + +```text +bun test tests/config-user-edits.test.ts +``` + +If the first run passes, record that the existing recursive provider merge already satisfies the contract and do not add production merge code. If it fails, make the smallest generic merge correction in `src/config.ts`, then rerun until green. + +- [ ] **Step 11: Commit the management API** + +```text +git add src/server/management/model-rows.ts src/server/management/model-routes.ts tests/model-display-names-management-api.test.ts tests/config-user-edits.test.ts src/config.ts +git commit -m "feat(api): manage discovered model display names" +``` + +--- + +### Task 4: Documentation and Architecture Sync + +**Files:** +- Modify: `docs-site/src/content/docs/reference/configuration/providers.md` +- Modify: `structure/02_config-and-codex-home.md` +- Modify: `structure/03_catalog-and-subagents.md` + +**Interfaces:** +- Documents: exact native ID keys, precedence, reset behavior, and API contract. + +- [ ] **Step 1: Update the provider configuration reference** + +Add a short example: + +```json +{ + "providers": { + "xai": { + "modelDisplayNames": { + "grok-4.6": "Grok 4.6" + } + } + } +} +``` + +State that the key is the exact native model ID, not `xai/grok-4.6`, the value is display only, unknown IDs are retained, and removing an entry resets the label. + +- [ ] **Step 2: Update structure records** + +Add `modelDisplayNames` to the persisted provider field list and record the precedence `operator > trusted provider metadata > fallback`. State that catalog identity and outbound routing never consume the label. + +- [ ] **Step 3: Run documentation checks** + +Run the repository's existing docs check or Astro build command found in `docs-site/package.json`. Expected: success with no broken links or schema errors. + +- [ ] **Step 4: Commit documentation** + +```text +git add docs-site/src/content/docs/reference/configuration/providers.md structure/02_config-and-codex-home.md structure/03_catalog-and-subagents.md +git commit -m "docs: explain discovered model display names" +``` + +--- + +### Task 5: Full Verification and User Preview + +**Files:** +- Review: every file changed by Tasks 1 through 4. +- Do not modify: the user's installed OpenCodex configuration or catalog. + +**Interfaces:** +- Produces: test evidence and a disposable preview for the user. + +- [ ] **Step 1: Review the complete diff twice** + +Run: + +```text +git diff upstream/dev...HEAD --check +git diff upstream/dev...HEAD +``` + +Check exact ID matching, no route identity changes, no secret fields in DTOs, no broad config fallback, and no unrelated changes. + +- [ ] **Step 2: Run focused tests** + +```text +bun test tests/provider-config-validation.test.ts tests/config-load-degrade.test.ts tests/config-user-edits.test.ts tests/codex-catalog.test.ts tests/model-display-names-management-api.test.ts +``` + +Expected: all pass. + +- [ ] **Step 3: Run repository gates** + +```text +bun run typecheck +bun run test +bun run privacy:scan +``` + +Expected: all pass with no new warnings, failures, secrets, emails, tokens, or personal paths. + +- [ ] **Step 4: Run a disposable end to end preview** + +Create a temporary config directory outside the repository using the operating system temporary directory. Configure one local fake provider with two discovered models and one operator label. Start the branch build on an unused port, call the real management API, run catalog convergence twice, restart the disposable server, and verify: + +```text +effective display name = Grok 4.6 +routed slug = xai/grok-4.6 +native model id = grok-4.6 +second model unchanged +label survives restart and repeated sync +reset restores fallback +temporary discovery failure keeps the stored label +``` + +The fake provider must receive the unchanged native model ID in a test request. Delete only the disposable temporary directory after the preview. + +- [ ] **Step 5: Show the result before submission** + +Report the exact test counts, commands, relevant catalog JSON before and after, API request and response examples, and any limitations. Do not push the branch and do not open a pull request until the user explicitly approves the verified result. diff --git a/docs/superpowers/specs/2026-08-26-discovered-model-display-names-design.md b/docs/superpowers/specs/2026-08-26-discovered-model-display-names-design.md new file mode 100644 index 0000000000..93a35b53d6 --- /dev/null +++ b/docs/superpowers/specs/2026-08-26-discovered-model-display-names-design.md @@ -0,0 +1,233 @@ +# Discovered Model Display Names Design + +## Status + +Design approved by the contributor on 2026-08-26 for issue #2201. + +Base: `dev` at `01b5da9f574956f8eb55b13e55dd48e79ab74502`. + +## Problem + +OpenCodex can assign a display name to a custom model, but a model returned by provider discovery has no operator owned display name. The generated Codex catalog therefore falls back to a namespaced routing slug such as `xai/grok-composer-2.5-fast`. + +Editing `opencodex-catalog.json` is not a durable solution because sync, startup, provider refresh, and updates regenerate that file. A Windows startup script that edits generated state introduces ordering problems with Codex Desktop and the OpenCodex proxy. + +The dashboard also has no control for naming an existing discovered model. Its current Add control creates a separate custom model row and rejects an ID that already exists in discovery. + +## Goals + +1. Let an operator assign a readable name to an existing discovered provider model. +2. Persist that name in `config.json`, not generated catalog state. +3. Reapply the name during every catalog generation path. +4. Preserve provider identity, native model ID, routed slug, routing, billing, visibility, aliases, fallback targets, and outbound wire requests. +5. Support clear and reset behavior with deterministic fallback. +6. Expose the feature through configuration and the management API first, then through the dashboard in a separate pull request. +7. Keep provider labels available while a model is temporarily absent from discovery. + +## Non goals + +1. Renaming a provider ID or native model ID. +2. Changing model routing or alias collision rules. +3. Creating a second custom model row for a discovered model. +4. Renaming native OpenAI marketing rows in the first pull request. +5. Adding automatic startup helpers or modifying Codex Desktop files. +6. Importing provider supplied marketing names from new external sources. + +## Configuration contract + +Each provider can hold an optional native model ID to display name map: + +```json +{ + "providers": { + "xai": { + "modelDisplayNames": { + "grok-4.6": "Grok 4.6", + "grok-composer-2.5-fast": "Grok Composer Fast" + } + } + } +} +``` + +The map key is the provider native model ID. It is not the routed slug. Native IDs may contain `/`, so validation must use the same model ID rules as provider discovery rather than display name rules. + +The map value is display only. It must be a trimmed, nonempty string with a bounded length. It must reject control characters and `/`, matching the existing custom model display name safety contract. A malformed map or malformed entry must degrade safely without discarding the rest of the provider configuration. Exact validation behavior will follow the repository's existing config parsing and management mutation patterns. + +Unknown or currently absent model IDs are retained. A temporary provider outage, a stale discovery response, or a model disappearing for one refresh must not delete user owned metadata. + +Deleting a map entry clears the override. An empty map may be omitted during persistence. Clearing restores the normal derived display name on the next catalog convergence. + +## Display name precedence + +For a routed discovered model, the catalog label resolves in this order: + +1. Valid operator override from `provider.modelDisplayNames[modelId]`. +2. Trusted display metadata already carried by the current catalog pipeline. +3. Existing derived name or routed slug fallback. + +The operator override changes only `CatalogModel.displayName` and the emitted Codex `display_name`. It must not feed slug construction, equality, pricing, disable checks, provider selection, effort metadata, context metadata, modality metadata, aliases, fallbacks, combos, or the outbound request model. + +## Core data flow + +```text +config.json provider.modelDisplayNames + -> defensive config validation + -> provider model discovery + -> routed catalog row construction + -> display precedence resolver + -> Codex catalog display_name +``` + +The resolver belongs at the shared catalog construction boundary so startup sync, `ocx sync`, live provider refresh, management mutations, and service restart use the same behavior. No caller should patch the generated catalog after it is written. + +## Management API + +The first pull request adds a focused mutation surface for one provider and native model ID. The exact route should follow the existing management API conventions and must: + +1. Validate provider existence, model ID, and display name. +2. Allow a model ID that is temporarily absent from live discovery. +3. Update only the targeted map entry. +4. Persist through the existing safe config writer. +5. Roll back in memory if persistence fails. +6. Trigger catalog convergence after a successful mutation. +7. Return the resulting display name and catalog refresh result. +8. Support clear or reset without accepting an ambiguous blank value. + +Read surfaces must return the effective label and whether its source is an operator override, provider metadata, or fallback. Credentials and unrelated configuration must never be exposed. + +## Dashboard follow up + +The dashboard work is a separate pull request stacked after the core contract, as requested by the maintainer in issue #2201. + +Each discovered model row receives a small rename action. The editor shows: + +1. The immutable provider and native model ID. +2. The current effective display name. +3. A text field for the operator override. +4. Save and Reset actions. +5. Clear feedback for saving, success, validation failure, network failure, and catalog refresh failure. + +The dashboard must not create a custom model to rename a discovered row. The current custom model Add flow remains unchanged. + +Saving updates the existing row in place after the server confirms success. Reset removes the override and restores the server returned fallback label. A failed save keeps the entered value so the user can retry. Controls must be keyboard accessible and translated through the existing i18n catalog. + +## Error handling + +1. Invalid display names return a bounded validation error and do not mutate config or catalog. +2. An unknown provider returns not found and does not create a provider implicitly. +3. A temporarily absent model ID is allowed for an existing provider so labels survive discovery gaps. +4. A config persistence failure restores the previous in memory map. +5. A catalog refresh failure keeps the successfully persisted label and reports that refresh is pending, matching existing management mutation behavior where possible. +6. A malformed hand edited map is ignored entry by entry where the existing parser permits safe degradation. Valid provider settings remain usable. +7. Concurrent mutations must use the existing config mutation serialization path so unrelated entries are not lost. + +## Pull request split + +### Pull request 1: core contract + +1. Provider config type and defensive validation. +2. Display precedence resolver. +3. All catalog construction paths. +4. Management read and mutation API. +5. Configuration reference documentation. +6. Focused runtime, config, API, and catalog regression tests. + +### Pull request 2: dashboard editor + +1. Provider model row rename and reset controls. +2. API client integration and optimistic state rules. +3. Loading, validation, failure, retry, and success states. +4. i18n strings in every supported locale. +5. Component tests, accessibility checks, lint, build, and screenshots. + +The second pull request targets the first branch while the first is open. It is retargeted to `dev` after the core pull request lands. + +## Test strategy + +Tests are written before production code and observed failing for the missing feature. + +### Configuration and validation + +1. Accept one valid provider scoped map. +2. Preserve several labels under one provider. +3. Keep identical native model IDs isolated across two providers. +4. Reject or safely ignore empty, whitespace only, slash containing, control character, nonstring, oversized, array, and prototype shaped values according to the established parser boundary. +5. Preserve valid provider fields when one label is malformed. +6. Preserve labels for model IDs absent from the latest discovery result. +7. Round trip the map through load, mutation, persistence, and reload. +8. Clear one entry without deleting neighboring entries. + +### Catalog behavior + +1. Apply an operator label to a discovered routed row. +2. Keep the routed slug and native model ID unchanged. +3. Keep pricing, disabled model matching, effort levels, context window, modalities, priority, aliases, fallback targets, and outbound wire model unchanged. +4. Use operator override over provider metadata. +5. Restore provider metadata or slug fallback after reset. +6. Preserve labels through repeated catalog generation. +7. Preserve labels through provider discovery success, failure fallback, empty discovery, and later recovery. +8. Avoid duplicate rows when a discovered model has a label. +9. Keep custom model display names unchanged. +10. Keep providers without the new field byte and behavior compatible where the existing writer allows it. + +### Management API + +1. Read effective name and source without exposing secrets. +2. Set a label for a discovered model. +3. Set a label for a temporarily absent model under an existing provider. +4. Reset a label. +5. Reject unknown providers and invalid labels. +6. Prove persistence failure does not leave an in memory partial mutation. +7. Prove successful mutation requests catalog convergence exactly once. +8. Prove concurrent updates do not erase unrelated map entries. + +### Dashboard + +1. Show Rename for a discovered model and not confuse it with Add custom model. +2. Load and display effective and overridden names. +3. Save a trimmed valid label with the correct provider and native model ID. +4. Reset an override. +5. Disable duplicate submits while saving. +6. Keep user input after server or network failure. +7. Display validation, network, persistence, and refresh feedback. +8. Work with filtering, a large capped model list, selected models, default models, configured fallback models, and custom rows. +9. Support keyboard operation and accessible labels. +10. Render correctly at repository required desktop and mobile widths. + +### Full verification before submission + +For the core pull request: + +```text +focused Bun tests +bun run typecheck +bun run test +bun run privacy:scan +``` + +For the dashboard pull request: + +```text +focused GUI tests +cd gui && bun test +bun run lint:gui +bun run typecheck +bun run test +bun run build:gui +bun run privacy:scan +manual dashboard test against a disposable config +desktop and mobile screenshots +``` + +The manual test uses a disposable OpenCodex config and catalog. It must not modify the user's installed configuration, provider credentials, or production catalog. + +## Acceptance criteria + +1. A discovered model can receive a durable operator display name without becoming a custom model. +2. The label survives sync, catalog regeneration, proxy restart, Codex restart, provider discovery gaps, and config reload. +3. Reset restores the deterministic fallback label. +4. Routing identity and all non-display catalog behavior remain unchanged. +5. The dashboard can edit and reset the same core configuration without a local patch script. +6. All focused and full repository checks pass with no secrets or personal data added. +7. Pull requests follow the repository templates, target the correct branches, include required evidence, and remain drafts until review readiness is proven. diff --git a/src/cli/opencode.ts b/src/cli/opencode.ts index 3a9d2ea68f..793ee46cb7 100644 --- a/src/cli/opencode.ts +++ b/src/cli/opencode.ts @@ -94,6 +94,7 @@ export interface OpencodeProxyModelRow { native?: boolean; disabled?: boolean; displayName?: string; + displayNameSource?: "operator" | "provider" | "fallback"; contextWindow?: number; /** Declared effort ladder from `/api/models`; carried into opencode model variants. */ reasoningEfforts?: string[]; @@ -387,7 +388,7 @@ export function opencodeCatalogFromProxyRows( provider: row.provider, id: row.id, contextWindow: row.contextWindow, - displayName: row.displayName, + displayName: row.displayNameSource === "fallback" ? undefined : row.displayName, ...(Array.isArray(row.reasoningEfforts) && row.reasoningEfforts.length > 0 ? { reasoningEfforts: [...row.reasoningEfforts] } : {}), diff --git a/src/codex/catalog/provider-fetch.ts b/src/codex/catalog/provider-fetch.ts index 19babf92aa..66b7f63a88 100644 --- a/src/codex/catalog/provider-fetch.ts +++ b/src/codex/catalog/provider-fetch.ts @@ -574,6 +574,7 @@ function providerCatalogFingerprint(name: string, prov: OcxProviderConfig): Reco models: [...(prov.models ?? [])].sort(), retain: [...(prov.retainModels ?? [])].sort(), selected: [...(prov.selectedModels ?? [])].sort(), + displayNames: prov.modelDisplayNames ?? null, defaultModel: prov.defaultModel ?? null, ctx: prov.contextWindow ?? null, ctxW: prov.modelContextWindows ?? null, @@ -629,6 +630,16 @@ export function configuredInputModalities(prov: OcxProviderConfig, id: string): return Array.isArray(modalities) && modalities.length > 0 ? [...modalities] : undefined; } +/** Exact display-only override for one provider-native model id. */ +export function configuredModelDisplayName( + prov: OcxProviderConfig, + id: string, +): string | undefined { + if (!prov.modelDisplayNames || !Object.hasOwn(prov.modelDisplayNames, id)) return undefined; + const value = prov.modelDisplayNames[id]; + return typeof value === "string" && value.trim() ? value.trim() : undefined; +} + export function configuredMaxInputTokens(prov: OcxProviderConfig, id: string): number | undefined { const configured = modelRecordValue(prov.modelMaxInputTokens, id); return typeof configured === "number" && configured > 0 ? configured : undefined; @@ -669,6 +680,7 @@ function configuredVerbositySupport(name: string, prov: OcxProviderConfig | unde } export function applyProviderConfigHints(name: string, prov: OcxProviderConfig, model: CatalogModel, providerCap?: number): CatalogModel { + const displayName = configuredModelDisplayName(prov, model.id); const configuredCap = configuredContextWindow(prov, model.id); const configuredMaxInput = configuredMaxInputTokens(prov, model.id); const configuredAutoCompact = configuredAutoCompactTokenLimit(prov, model.id); @@ -704,6 +716,7 @@ export function applyProviderConfigHints(name: string, prov: OcxProviderConfig, : (configuredCap ?? (providerCap !== undefined ? resolveUnknownRoutedContextWindow(providerCap) : undefined)); const hinted = { ...modelWithoutServiceTier, + ...(displayName !== undefined ? { displayName } : {}), ...(hintedWindow !== undefined ? { contextWindow: hintedWindow } : {}), ...(inputModalities ? { inputModalities } : {}), ...(reasoningEfforts !== undefined ? { reasoningEfforts } : {}), diff --git a/src/config.ts b/src/config.ts index 77f8caade8..a0965f9b6d 100644 --- a/src/config.ts +++ b/src/config.ts @@ -8,6 +8,7 @@ import { apiKeyTransportConfigError, booleanRecordConfigError, modelAdapterRecordConfigError, + modelDisplayNamesConfigError, nonBlankStringArrayConfigError, normalizeNonBlankStringArray, positiveIntegerConfigError, @@ -61,6 +62,7 @@ import { providerDestinationConfigError } from "./lib/destination-policy"; import { redactSecretString } from "./lib/redact"; import { openRouterRoutingConfigError } from "./providers/openrouter-routing"; import { MODEL_ALIAS_PATTERN } from "./providers/default-aliases"; +import { MODEL_DISCOVERY_MAX_MODELS } from "./providers/model-discovery-limits"; import { vercelGatewayRoutingConfigError } from "./providers/vercel-gateway-routing"; import { MODEL_ADAPTER_OVERRIDE_ALLOWED, @@ -499,6 +501,17 @@ const fastWireSchema = z.object({ if (error) ctx.addIssue({ code: "custom", message: error }); }).transform(fastWire => fastWire as FastWire); +const modelDisplayNamesSchema = z.unknown().superRefine((value, ctx) => { + const error = modelDisplayNamesConfigError(value); + if (error) ctx.addIssue({ code: "custom", message: error }); +}).transform(value => { + const labels = Object.create(null) as Record; + for (const [modelId, displayName] of Object.entries(value as Record)) { + labels[modelId] = displayName; + } + return labels; +}); + /** * Zod schema for one provider entry: known fields are validated strictly while unknown * fields pass through (preserved for runtime extensions). @@ -508,6 +521,7 @@ const providerConfigSchema = z.object({ baseUrl: z.string().min(1), alias: z.string().optional(), modelAliases: z.record(z.string(), z.string()).optional(), + modelDisplayNames: modelDisplayNamesSchema.optional(), defaultAliases: z.boolean().optional(), requestPacing: requestPacingSchema.optional().catch(undefined), mcpMaxTools: z.number().int().positive().optional(), @@ -563,6 +577,7 @@ export { apiKeyTransportConfigError, booleanRecordConfigError, modelAdapterRecordConfigError, + modelDisplayNamesConfigError, nonBlankStringArrayConfigError, normalizeNonBlankStringArray, positiveIntegerConfigError, @@ -1244,6 +1259,16 @@ const configSchema = z.object({ message: modelCostsError, }); } + const modelDisplayNamesError = modelDisplayNamesConfigError( + (provider as { modelDisplayNames?: unknown }).modelDisplayNames, + ); + if (modelDisplayNamesError) { + ctx.addIssue({ + code: "custom", + path: ["providers", redactSecretString(name), "modelDisplayNames"], + message: modelDisplayNamesError, + }); + } const apiKeyTransportError = apiKeyTransportConfigError(provider as OcxProviderConfig); if (apiKeyTransportError) { ctx.addIssue({ @@ -2042,6 +2067,7 @@ export function loadConfig(): OcxConfig { const raw = readFileSync(configPath, "utf-8").replace(/^\uFEFF/, ""); const parsed = JSON.parse(raw); sanitizeAliasesForLoad(parsed); + sanitizeModelDisplayNamesForLoad(parsed); sanitizeRetryOn429ForLoad(parsed); sanitizeModelCostsForLoad(parsed); const result = configSchema.safeParse(parsed); @@ -2156,6 +2182,38 @@ function sanitizeAliasesForLoad(raw: unknown): void { } } +/** Hand-edited display-name mistakes disable only the bad label. */ +function sanitizeModelDisplayNamesForLoad(raw: unknown): void { + if (!raw || typeof raw !== "object" || Array.isArray(raw)) return; + const root = raw as Record; + if (!root.providers || typeof root.providers !== "object" || Array.isArray(root.providers)) return; + for (const [providerName, providerValue] of Object.entries(root.providers as Record)) { + if (!providerValue || typeof providerValue !== "object" || Array.isArray(providerValue)) continue; + const provider = providerValue as Record; + const value = provider.modelDisplayNames; + if (value === undefined) continue; + const providerLabel = JSON.stringify(redactSecretString(providerName)); + if (!value || typeof value !== "object" || Array.isArray(value) + || Object.entries(value).length > MODEL_DISCOVERY_MAX_MODELS) { + console.warn(`Ignoring invalid modelDisplayNames map for provider ${providerLabel} in config.json`); + delete provider.modelDisplayNames; + continue; + } + const labels = value as Record; + for (const [modelId, rawDisplayName] of Object.entries(labels)) { + const displayName = typeof rawDisplayName === "string" ? rawDisplayName.trim() : rawDisplayName; + if (modelDisplayNamesConfigError({ [modelId]: displayName })) { + const safeModelId = JSON.stringify(redactSecretString(modelId)); + console.warn(`Ignoring invalid modelDisplayNames entry ${safeModelId} for provider ${providerLabel} in config.json`); + delete labels[modelId]; + } else { + labels[modelId] = displayName; + } + } + if (Object.keys(labels).length === 0) delete provider.modelDisplayNames; + } +} + /** Refresh the user cost-overlay registry from `config` and return it unchanged. */ function withRefreshedCostOverlays(config: OcxConfig): OcxConfig { refreshUserCostOverlays(config); @@ -2537,6 +2595,7 @@ function configDiagnosticsFromRaw(raw: string): ConfigDiagnostics { // Same degradation as loadConfig: a hand-edited invalid retryOn429 must not trip the // schema and send the caller a default-config fallback (the config command could then // persist that fallback over the user's providers/keys). + sanitizeModelDisplayNamesForLoad(parsed); sanitizeRetryOn429ForLoad(parsed); sanitizeModelCostsForLoad(parsed); const result = configSchema.safeParse(parsed); diff --git a/src/config/provider-validation.ts b/src/config/provider-validation.ts index 6508a745f8..326914a758 100644 --- a/src/config/provider-validation.ts +++ b/src/config/provider-validation.ts @@ -1,5 +1,9 @@ import { isCanonicalOpenAiForwardProvider } from "../providers/openai-tiers"; import { redactSecretString } from "../lib/redact"; +import { + isValidModelDiscoveryModelId, + MODEL_DISCOVERY_MAX_MODELS, +} from "../providers/model-discovery-limits"; import { modelRecordValue } from "../reasoning-effort"; import { isWirePinnedModel, @@ -20,6 +24,8 @@ const SENSITIVE_PROVIDER_HEADERS = new Set([ "x-amz-security-token", ]); const REASONING_SUMMARY_DELIVERY_SET = new Set(REASONING_SUMMARY_DELIVERY_VALUES); +const DISPLAY_NAME_CONTROL_CHARS = /[\u0000-\u001f\u007f-\u009f\u2028\u2029]/; +const MAX_MODEL_DISPLAY_NAME_LENGTH = 128; /** Validate a provider destination without coupling DTO callers to config persistence. */ export function providerBaseUrlConfigError(baseUrl: string): string | null { @@ -124,6 +130,40 @@ export function booleanRecordConfigError(value: unknown, field: string): string return null; } +/** Validate display-only labels without changing the provider's model identity. */ +export function modelDisplayNamesConfigError( + value: unknown, + field = "modelDisplayNames", +): string | null { + if (value === undefined) return null; + if (!value || typeof value !== "object" || Array.isArray(value)) return `${field} must be a plain object`; + const prototype = Object.getPrototypeOf(value); + if (prototype !== Object.prototype && prototype !== null) { + return `${field} must be a plain object with own properties`; + } + const entries = Object.entries(value); + // One discovered model can own one label, so both maps share the same safe cap. + if (entries.length > MODEL_DISCOVERY_MAX_MODELS) { + return `${field} must contain at most ${MODEL_DISCOVERY_MAX_MODELS} entries`; + } + for (const [modelId, displayName] of entries) { + if (!isValidModelDiscoveryModelId(modelId)) return `${field} keys must be valid model ids`; + const safeModelId = JSON.stringify(redactSecretString(modelId)); + if (typeof displayName !== "string") return `${field}.${safeModelId} must be a string`; + const trimmed = displayName.trim(); + if (!trimmed) return `${field}.${safeModelId} must be nonblank`; + if (displayName !== trimmed) return `${field}.${safeModelId} must be trimmed`; + if (displayName.length > MAX_MODEL_DISPLAY_NAME_LENGTH) { + return `${field}.${safeModelId} must be at most ${MAX_MODEL_DISPLAY_NAME_LENGTH} characters`; + } + if (displayName.includes("/")) return `${field}.${safeModelId} must not contain /`; + if (DISPLAY_NAME_CONTROL_CHARS.test(displayName)) { + return `${field}.${safeModelId} must not contain control characters`; + } + } + return null; +} + /** Validate the management DTO boundary for the opt-in empty-tool-output annotation. */ export function providerEmptyToolOutputConfigError(name: string, provider: unknown): string | null { const raw = provider as Record | null | undefined; diff --git a/src/server/management/model-routes.ts b/src/server/management/model-routes.ts index b798b44c18..601444a958 100644 --- a/src/server/management/model-routes.ts +++ b/src/server/management/model-routes.ts @@ -72,13 +72,15 @@ function readDefaultReasoningEffort(raw: unknown, efforts: string[] | undefined) import type { CatalogModel } from "../../codex/catalog"; import { accountBoundNativeOpenAiSlugsBySelector, catalogModelSlug, configuredNativeAliasSlugs, disabledNativeSlugs, invalidateCodexModelsCache, nativeModelRows, shouldIncludeAccountBoundNativeOpenAi, uniqueCatalogModelsForPublicList } from "../../codex/catalog"; import { CatalogGatherBusyError } from "../../codex/catalog/provider-fetch"; +import { clearModelCache, getProviderLiveModelCount } from "../../codex/model-cache"; import { NATIVE_OPENAI_MODELS } from "../../codex/catalog/native-models"; -import { getProviderLiveModelCount } from "../../codex/model-cache"; + import { DEFAULT_SUBAGENT_MODELS, codexAutoStartEnabled, hasOwnProvider, isValidProviderName, + modelDisplayNamesConfigError, multiAgentGuidanceEnabled, providerBaseUrlConfigError, providerHeadersConfigError, @@ -101,6 +103,7 @@ import { providerCodexAccountMode } from "../../providers/registry"; import { encodedModelIdCollides, routedSlug, slugEquals } from "../../providers/slug-codec"; import { knownModelIdsForProvider } from "../../router"; import { effectiveModelAliases, MODEL_ALIAS_PATTERN } from "../../providers/default-aliases"; +import { isValidModelDiscoveryModelId } from "../../providers/model-discovery-limits"; import { comboPublicModelId } from "../../combos/types"; import { COMBO_NAMESPACE, comboDisabledModelSelectors, comboModelId, preservesPhysicalComboProvider } from "../../combos"; import { clearProviderQuotaCache, fetchProviderQuotaReports } from "../../providers/quota"; @@ -353,6 +356,81 @@ export async function handleModelRoutes(ctx: ManagementContext): Promise, + previousDisplayNames ?? {}, + ); + if (displayName === null) delete nextDisplayNames[modelId]; + else nextDisplayNames[modelId] = displayName; + const mergedValidationError = modelDisplayNamesConfigError(nextDisplayNames); + if (mergedValidationError) return jsonResponse({ error: mergedValidationError }, 400, req, config); + if (Object.keys(nextDisplayNames).length > 0) provider.modelDisplayNames = nextDisplayNames; + else delete provider.modelDisplayNames; + + try { + persistConfig(config); + } catch (error) { + if (hadDisplayNames) provider.modelDisplayNames = previousDisplayNames; + else delete provider.modelDisplayNames; + throw error; + } + clearModelCache(name); + const catalogRefresh = await convergeCodexCatalog(); + const storedDisplayName = provider.modelDisplayNames?.[modelId] ?? null; + if (catalogRefresh.status === "failed") { + return jsonResponse({ + error: "model display name saved but catalog refresh failed", + saved: true, + provider: name, + modelId, + displayNameOverride: storedDisplayName, + catalogRefresh, + }, 503, req, config); + } + const row = (await listManagementModelRows(config)).find(candidate => ( + candidate.native !== true + && candidate.custom !== true + && candidate.provider === name + && candidate.id === modelId + )); + return jsonResponse({ + ok: true, + provider: name, + modelId, + displayName: row?.displayName ?? storedDisplayName ?? routedSlug(name, modelId), + displayNameOverride: storedDisplayName, + displayNameSource: row?.displayNameSource ?? (storedDisplayName ? "operator" : "fallback"), + catalogRefresh, + }); + } + /** * Client config document for OpenCode / Pi, built from the SAME function `ocx export` * calls, so the bytes a user downloads here and the bytes they pipe from the CLI cannot diff --git a/src/server/management/model-rows.ts b/src/server/management/model-rows.ts index 592c0c11e7..929916d62b 100644 --- a/src/server/management/model-rows.ts +++ b/src/server/management/model-rows.ts @@ -41,8 +41,28 @@ export type ManagementModelRow = Partial & { native?: boolean; custom?: boolean; customId?: string; + displayNameOverride?: string; + displayNameSource?: "operator" | "provider" | "fallback"; }; +/** Resolve the exact text and source shown for one routed discovered model. */ +export function effectiveManagementDisplayName( + config: Pick, + model: CatalogModel, +): Pick { + const provider = config.providers[model.provider]; + const configured = provider?.modelDisplayNames; + if (configured && Object.hasOwn(configured, model.id)) { + const displayName = configured[model.id]?.trim(); + if (displayName) { + return { displayName, displayNameOverride: displayName, displayNameSource: "operator" }; + } + } + const providerDisplayName = model.displayName?.trim(); + if (providerDisplayName) return { displayName: providerDisplayName, displayNameSource: "provider" }; + return { displayName: catalogModelSlug(model), displayNameSource: "fallback" }; +} + /** * The exact row list `/api/models` returns. Extracted so `/api/client-config` exports the * models the GUI's Models tab shows — including this function's `disabled` computation, @@ -133,8 +153,10 @@ export async function listManagementModelRows( if (m.provider !== "combo" && customNamespaced.has(namespaced)) return null; const contextCap = providerContextCap(config, m.provider); const nativeAlias = m.provider === "combo" && m.nativeAlias === true; + const displayName = effectiveManagementDisplayName(config, m); return { ...m, + ...displayName, namespaced, disabled: [...disabled].some(stored => ( (!nativeAlias && stored === namespaced) || slugEquals(stored, m.provider, m.id) @@ -152,7 +174,7 @@ export function toExportModel(row: ManagementModelRow): ExportModel { provider: row.provider, id: row.id, ...(row.native ? { native: true } : {}), - ...(row.displayName ? { displayName: row.displayName } : {}), + ...(row.displayName && row.displayNameSource !== "fallback" ? { displayName: row.displayName } : {}), ...(row.contextWindow !== undefined ? { contextWindow: row.contextWindow } : {}), ...(row.inputModalities ? { inputModalities: row.inputModalities } : {}), ...(row.reasoningEfforts ? { reasoningEfforts: row.reasoningEfforts } : {}), diff --git a/src/server/management/provider-routes.ts b/src/server/management/provider-routes.ts index 22da830989..7376d8addb 100644 --- a/src/server/management/provider-routes.ts +++ b/src/server/management/provider-routes.ts @@ -9,6 +9,7 @@ import { codexAutoStartEnabled, hasOwnProvider, isValidProviderName, + modelDisplayNamesConfigError, multiAgentGuidanceEnabled, nonBlankStringArrayConfigError, normalizeNonBlankStringArray, @@ -667,6 +668,8 @@ export async function handleProviderRoutes(ctx: ManagementContext): Promise short, slash-free request alias. */ modelAliases?: Record; + /** Display-only labels for exact native model ids discovered under this provider. */ + modelDisplayNames?: Record; /** Override the global built-in model-alias switch for this provider. */ defaultAliases?: boolean; adapter: string; diff --git a/structure/02_config-and-codex-home.md b/structure/02_config-and-codex-home.md index 8da3d37316..794dfebe67 100644 --- a/structure/02_config-and-codex-home.md +++ b/structure/02_config-and-codex-home.md @@ -280,7 +280,7 @@ matters for maintainers is which groups exist and who resolves them: | --- | --- | --- | | Listener | `port`, `hostname` | The listener owns the port; `runtime-port.json` reports where it actually landed. | | Routing | `defaultProvider`, `providers`, per-provider `selectedModels` | Explicit `provider/model` wins over `defaultProvider`. | -| Catalog | `disabledModels`, `customModels`, `modelCacheTtlMs`, `providerContextCaps`, `contextCapValue`, `codexAccountNamespaces`, `codexAccountPickerEnabled` | Catalog state is derived; config only records intent. The picker flag is an explicit visibility override, while selector mappings remain the durable exact-routing contract. | +| Catalog | `disabledModels`, `customModels`, `modelCacheTtlMs`, `providerContextCaps`, `contextCapValue`, per-provider `modelDisplayNames`, `codexAccountNamespaces`, `codexAccountPickerEnabled` | Catalog state is derived; config only records intent. Exact provider model display names are durable display only overlays. The picker flag is an explicit visibility override, while selector mappings remain the durable exact-routing contract. | | Retained state | `appOwnedMemoryBudgetMb` | Process-wide eviction target for app-owned logs, caches, blobs, and continuation payloads. Default 256 MiB, valid 64..4096; pinned state may temporarily exceed the target, but every pin-capable store has a finite local cap and their documented aggregate stays below `APP_OWNED_WORST_CASE_PINNED_BYTES` (512 MiB). Neither value caps RSS or native runtime memory. | | Transport | stream mode, timeouts, proxy settings, `websockets`, `emptyCompletionRetry` | `streamMode` persists in config.json; Windows services need a persisted input, and macOS uses it for explicit eager-relay opt-in. Empty-completion replay is an explicit top-level opt-in because its second upstream request may be billable. | | Credentials | `apiKeys` | Data-plane only; never admitted to `/api/*`. | diff --git a/structure/03_catalog-and-subagents.md b/structure/03_catalog-and-subagents.md index 3260fe9de1..d30e3737b3 100644 --- a/structure/03_catalog-and-subagents.md +++ b/structure/03_catalog-and-subagents.md @@ -102,8 +102,10 @@ liveness contract. Routed entries keep Codex-required metadata such as reasoning levels, shell type, API support flags, base instructions, modalities, auto-compact fields, and strict parser booleans. The public slug uses -the canonical `provider/model`; its display name uses the qualified provider/model alias when -configured, without changing the routing slug. +the canonical `provider/model`. Its display name uses the provider's exact `modelDisplayNames` override first, +then trusted catalog metadata such as a configured qualified provider/model alias, then the public slug. +This overlay never changes route identity or the upstream wire model, and its catalog fingerprint makes +a label edit refresh Codex output. ## Native passthrough diff --git a/tests/codex-catalog.test.ts b/tests/codex-catalog.test.ts index 9e3cceda46..cb7bca5cc7 100644 --- a/tests/codex-catalog.test.ts +++ b/tests/codex-catalog.test.ts @@ -1817,6 +1817,172 @@ describe("Cursor Kimi K3 catalog default effort", () => { }); }); +describe("provider discovered model display names", () => { + const provider = { + adapter: "openai-responses", + baseUrl: "https://api.x.ai/v1", + modelDisplayNames: { "grok-4.6": "Grok 4.6" }, + }; + + test("an exact provider model id receives only the configured display name", () => { + const discovered = { + provider: "xai", + id: "grok-4.6", + displayName: "Provider Grok", + contextWindow: 131_072, + maxInputTokens: 100_000, + autoCompactTokenLimit: 90_000, + inputModalities: ["text", "image"], + reasoningEfforts: ["low", "high"], + defaultReasoningEffort: "high", + supportsReasoningSummaries: true, + supportsVerbosity: false, + priority: 17, + fallbackModels: ["grok-4.5"], + owned_by: "xai", + } as const; + + const output = applyProviderConfigHints("xai", provider, discovered); + const { displayName: _beforeDisplayName, ...beforeIdentity } = discovered; + const { displayName: _afterDisplayName, ...afterIdentity } = output; + + expect(output.displayName).toBe("Grok 4.6"); + expect(afterIdentity).toEqual({ ...beforeIdentity, supportsServiceTier: false }); + expect(catalogModelSlug(output)).toBe("xai/grok-4.6"); + }); + + test("display names use exact case-sensitive ids and stay provider scoped", () => { + const wrongCase = applyProviderConfigHints("xai", provider, { provider: "xai", id: "GROK-4.6" }); + const otherProvider = applyProviderConfigHints("other", { + ...provider, + modelDisplayNames: { "grok-4.6": "Other Grok" }, + }, { provider: "other", id: "grok-4.6" }); + + expect(wrongCase.displayName).toBeUndefined(); + expect(otherProvider.displayName).toBe("Other Grok"); + }); + + test("provider metadata remains when no operator display name exists", () => { + const output = applyProviderConfigHints("xai", { + ...provider, + modelDisplayNames: undefined, + }, { + provider: "xai", + id: "grok-4.6", + displayName: "Provider Grok", + }); + + expect(output.displayName).toBe("Provider Grok"); + }); + + test("a configured display name emits into the Codex picker without changing its slug", () => { + const model = applyProviderConfigHints("xai", provider, { provider: "xai", id: "grok-4.6" }); + const row = buildCatalogEntries(nativeTemplate(), [], [model]) + .find(entry => entry.slug === "xai/grok-4.6"); + + expect(row?.display_name).toBe("Grok 4.6"); + expect(row?.slug).toBe("xai/grok-4.6"); + }); + + test("the label survives static, live, and configured failure catalog paths", async () => { + const staticModels = await gatherRoutedModels({ + defaultProvider: "display-static", + providers: { + "display-static": { + adapter: "openai-chat", + baseUrl: "https://static.example.test/v1", + liveModels: false, + models: ["model-a"], + modelDisplayNames: { "model-a": "Static Model" }, + }, + }, + }); + expect(staticModels).toContainEqual(expect.objectContaining({ + provider: "display-static", + id: "model-a", + displayName: "Static Model", + })); + + globalThis.fetch = (async () => new Response(JSON.stringify({ + data: [{ id: "model-a", name: "Provider Model" }], + }), { headers: { "content-type": "application/json" } })) as typeof fetch; + const liveModels = await gatherRoutedModels({ + defaultProvider: "display-live", + providers: { + "display-live": { + adapter: "openai-chat", + baseUrl: "https://93.184.216.34/v1", + apiKey: "sk-test", + modelDisplayNames: { "model-a": "Live Model" }, + }, + }, + }); + expect(liveModels).toContainEqual(expect.objectContaining({ + provider: "display-live", + id: "model-a", + displayName: "Live Model", + })); + + globalThis.fetch = (async () => new Response(null, { status: 503 })) as typeof fetch; + const warning = spyOn(console, "warn").mockImplementation(() => {}); + try { + const failedModels = await gatherRoutedModels({ + defaultProvider: "display-failure", + providers: { + "display-failure": { + adapter: "openai-chat", + baseUrl: "https://93.184.216.34/v1", + apiKey: "sk-test", + models: ["model-a"], + modelDisplayNames: { "model-a": "Failure Model" }, + }, + }, + }); + expect(failedModels).toContainEqual(expect.objectContaining({ + provider: "display-failure", + id: "model-a", + displayName: "Failure Model", + })); + } finally { + warning.mockRestore(); + } + }); + + test("a stale cached row receives the current operator label on every gather", async () => { + const providerName = "display-stale"; + setCached(providerName, [{ + provider: providerName, + id: "model-a", + displayName: "Old Provider Name", + }], Date.now() - 10_000); + globalThis.fetch = (async () => new Response(null, { status: 503 })) as typeof fetch; + const warning = spyOn(console, "warn").mockImplementation(() => {}); + try { + const config = { + modelCacheTtlMs: 1, + defaultProvider: providerName, + providers: { + [providerName]: { + adapter: "openai-chat" as const, + baseUrl: "https://93.184.216.34/v1", + apiKey: "sk-test", + modelDisplayNames: { "model-a": "Current Name" }, + }, + }, + }; + const first = await gatherRoutedModels(config); + const second = await gatherRoutedModels(config); + + expect(first).toContainEqual(expect.objectContaining({ id: "model-a", displayName: "Current Name" })); + expect(second).toContainEqual(expect.objectContaining({ id: "model-a", displayName: "Current Name" })); + expect(first.filter(model => catalogModelSlug(model) === `${providerName}/model-a`)).toHaveLength(1); + } finally { + warning.mockRestore(); + clearModelCache(providerName); + } + }); +}); + describe("configured CatalogModel displayName -> catalog display_name", () => { test("a routed CatalogModel displayName becomes the catalog display_name", () => { const model = { provider: "deepseek", id: "deepseek-v4", displayName: "DeepSeek V4", owned_by: "deepseek" }; diff --git a/tests/codex-convergence-contract.test.ts b/tests/codex-convergence-contract.test.ts index ff178c0321..f878ab643d 100644 --- a/tests/codex-convergence-contract.test.ts +++ b/tests/codex-convergence-contract.test.ts @@ -372,10 +372,10 @@ test("a failure cause never carries message text, paths or identifiers (#1784)", expect(body).not.toContain("failed writing"); }); -test("the route inventory contains exactly the specified 7 + 13 + 2 + 2 convergence calls", () => { +test("the route inventory contains exactly the specified 7 + 14 + 2 + 2 convergence calls", () => { const counts = Object.fromEntries([ ["provider-routes.ts", 7], - ["model-routes.ts", 13], + ["model-routes.ts", 14], ["combo-routes.ts", 2], ["agent-settings-routes.ts", 2], ].map(([file, expected]) => { @@ -387,7 +387,7 @@ test("the route inventory contains exactly the specified 7 + 13 + 2 + 2 converge })); expect(counts).toEqual({ "provider-routes.ts": 7, - "model-routes.ts": 13, + "model-routes.ts": 14, "combo-routes.ts": 2, "agent-settings-routes.ts": 2, }); diff --git a/tests/config-load-degrade.test.ts b/tests/config-load-degrade.test.ts new file mode 100644 index 0000000000..a6543bf08e --- /dev/null +++ b/tests/config-load-degrade.test.ts @@ -0,0 +1,128 @@ +import { afterEach, beforeEach, expect, spyOn, test } from "bun:test"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { + getConfigPath, + getDefaultConfig, + loadConfig, + saveConfig, + validateConfigCandidate, +} from "../src/config"; + +let home = ""; +let previousHome: string | undefined; + +beforeEach(() => { + previousHome = process.env.OPENCODEX_HOME; + home = mkdtempSync(join(tmpdir(), "ocx-display-names-config-")); + process.env.OPENCODEX_HOME = home; +}); + +afterEach(() => { + if (previousHome === undefined) delete process.env.OPENCODEX_HOME; + else process.env.OPENCODEX_HOME = previousHome; + rmSync(home, { recursive: true, force: true }); +}); + +function candidate(modelDisplayNames: unknown) { + const defaults = getDefaultConfig(); + return { + ...defaults, + defaultProvider: "xai", + providers: { + xai: { + adapter: "openai-responses", + baseUrl: "https://api.x.ai/v1", + note: "keep me", + modelDisplayNames, + }, + }, + }; +} + +function writeCandidate(modelDisplayNames: unknown, provider = "xai"): void { + const config = candidate(modelDisplayNames); + config.defaultProvider = provider; + config.providers = { + [provider]: { + ...config.providers.xai, + modelDisplayNames, + }, + }; + writeFileSync(getConfigPath(), JSON.stringify(config), "utf8"); +} + +test("config validation accepts only safe provider model display names", () => { + const valid = validateConfigCandidate(candidate({ + "grok-4.6": "Grok 4.6", + "models/grok-vision": "Grok Vision", + })); + expect(valid.ok).toBe(true); + + const invalid = validateConfigCandidate(candidate({ "grok-4.6": "Grok/4.6" })); + expect(invalid.ok).toBe(false); + if (!invalid.ok) expect(invalid.error).toContain("modelDisplayNames"); +}); + +test("load keeps a provider and valid labels when one hand edited label is invalid", () => { + writeCandidate({ + "grok-4.6": " Grok 4.6 ", + "future-model": "Future Model", + unsafe: "Bad/Name", + }); + + const loaded = loadConfig(); + + expect(loaded.providers.xai).toMatchObject({ + note: "keep me", + modelDisplayNames: { + "grok-4.6": "Grok 4.6", + "future-model": "Future Model", + }, + }); + expect(loaded.providers.xai.modelDisplayNames).not.toHaveProperty("unsafe"); +}); + +test("load and save preserve a prototype shaped model id as data", () => { + writeCandidate(JSON.parse('{"__proto__":"Prototype Model"}')); + + const loaded = loadConfig(); + + expect(Object.hasOwn(loaded.providers.xai.modelDisplayNames ?? {}, "__proto__")).toBe(true); + expect(loaded.providers.xai.modelDisplayNames?.["__proto__"]).toBe("Prototype Model"); + + saveConfig(loaded); + const reloaded = loadConfig(); + + expect(Object.hasOwn(reloaded.providers.xai.modelDisplayNames ?? {}, "__proto__")).toBe(true); + expect(reloaded.providers.xai.modelDisplayNames?.["__proto__"]).toBe("Prototype Model"); +}); + +test("load drops only a malformed display name map", () => { + writeCandidate("not-an-object"); + + const loaded = loadConfig(); + + expect(loaded.providers.xai).toMatchObject({ note: "keep me" }); + expect(loaded.providers.xai.modelDisplayNames).toBeUndefined(); +}); + +test("load warnings never reveal display values or secret shaped provider names", () => { + const warn = spyOn(console, "warn").mockImplementation(() => {}); + try { + const displaySecret = ["sk", "secret", "display", "value"].join("-"); + const providerSecret = ["sk", "secret", "provider", "name"].join("-"); + writeCandidate({ model: `${displaySecret}/unsafe` }, providerSecret); + + const loaded = loadConfig(); + + expect(loaded.providers[providerSecret]).toBeDefined(); + const output = warn.mock.calls.map(call => call.join(" ")).join("\n"); + expect(output).not.toContain(displaySecret); + expect(output).not.toContain(providerSecret); + expect(output).toContain("[REDACTED]"); + } finally { + warn.mockRestore(); + } +}); diff --git a/tests/config-user-edits.test.ts b/tests/config-user-edits.test.ts index 85108b0f8e..404ea655a6 100644 --- a/tests/config-user-edits.test.ts +++ b/tests/config-user-edits.test.ts @@ -386,6 +386,29 @@ test("config diagnostics sanitize invalid retryOn429 before schema validation", expect(diagnostics.config.providers.test.retryOn429).toBeUndefined(); }); +test("config diagnostics degrade only invalid provider model display names", () => { + writeDiskConfig({ + providers: { + test: { + adapter: "openai-chat", + baseUrl: "http://127.0.0.1:1/v1", + apiKey: "k", + allowPrivateNetwork: true, + modelDisplayNames: { + "model-a": " Model Alpha ", + "model-b": "Bad/Name", + }, + }, + }, + }); + + const diagnostics = readConfigDiagnostics(); + + expect(diagnostics.source).toBe("file"); + expect(diagnostics.error).toBeNull(); + expect(diagnostics.config.providers.test.modelDisplayNames).toEqual({ "model-a": "Model Alpha" }); +}); + test("invalid retryOn429 values never log the raw value", () => { const warn = spyOn(console, "warn").mockImplementation(() => {}); try { @@ -787,6 +810,48 @@ test("a provider deletion from a newer disk snapshot wins over a stale edit to t expect(Object.keys(diskConfig().providers as Record)).toEqual(["test"]); }); +test("independent provider model display name edits survive a guarded stale save", () => { + const live = loadConfig(); + live.providers.test.modelDisplayNames = { "model-a": "Alpha", "model-b": "Beta" }; + saveConfig(live); + armClaudeCodeBaseline(live); + + live.providers.test.modelDisplayNames["model-a"] = "Live Alpha"; + writeDiskConfig({ + providers: { + test: { + ...live.providers.test, + modelDisplayNames: { "model-a": "Alpha", "model-b": "Disk Beta" }, + }, + }, + }); + saveConfigPreservingClaudeCode(live); + + expect((diskConfig().providers as Record }>).test?.modelDisplayNames) + .toEqual({ "model-a": "Live Alpha", "model-b": "Disk Beta" }); +}); + +test("a display name reset preserves a neighboring label added on disk", () => { + const live = loadConfig(); + live.providers.test.modelDisplayNames = { "model-a": "Alpha", "model-b": "Beta" }; + saveConfig(live); + armClaudeCodeBaseline(live); + + delete live.providers.test.modelDisplayNames["model-a"]; + writeDiskConfig({ + providers: { + test: { + ...live.providers.test, + modelDisplayNames: { "model-a": "Alpha", "model-b": "Beta", "model-c": "Disk Gamma" }, + }, + }, + }); + saveConfigPreservingClaudeCode(live); + + expect((diskConfig().providers as Record }>).test?.modelDisplayNames) + .toEqual({ "model-b": "Beta", "model-c": "Disk Gamma" }); +}); + test("independent custom-model edits survive a guarded stale save", () => { const live = loadConfig(); live.customModels = [customModel("one"), customModel("two")]; diff --git a/tests/management-client-config-route.test.ts b/tests/management-client-config-route.test.ts index 1eec60d0a6..2671e5278d 100644 --- a/tests/management-client-config-route.test.ts +++ b/tests/management-client-config-route.test.ts @@ -78,6 +78,7 @@ interface ModelRow { disabled: boolean; native?: boolean; displayName?: string; + displayNameSource?: "operator" | "provider" | "fallback"; contextWindow?: number; inputModalities?: string[]; reasoningEfforts?: string[]; @@ -147,7 +148,7 @@ function toExportModel(row: ModelRow): ExportModel { provider: row.provider, id: row.id, ...(row.native ? { native: true } : {}), - ...(row.displayName ? { displayName: row.displayName } : {}), + ...(row.displayName && row.displayNameSource !== "fallback" ? { displayName: row.displayName } : {}), ...(row.contextWindow !== undefined ? { contextWindow: row.contextWindow } : {}), ...(row.inputModalities ? { inputModalities: row.inputModalities } : {}), ...(row.reasoningEfforts ? { reasoningEfforts: row.reasoningEfforts } : {}), diff --git a/tests/management-provider-validation.test.ts b/tests/management-provider-validation.test.ts index d8a0bdf29d..fad06ea7db 100644 --- a/tests/management-provider-validation.test.ts +++ b/tests/management-provider-validation.test.ts @@ -926,6 +926,72 @@ describe("provider management validation", () => { } }); + test("provider POST overwrite preserves modelDisplayNames when the payload omits it", async () => { + if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); + mkdirSync(TEST_DIR, { recursive: true }); + process.env.OPENCODEX_HOME = TEST_DIR; + saveConfig(config("127.0.0.1")); + + const server = startServer(0); + try { + const names = { "grok-4.6": "Grok 4.6" }; + const create = await fetch(new URL("/api/providers", server.url), { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ + name: "custom-display", + provider: { + adapter: "openai-chat", + baseUrl: "https://api.example.test/v1", + modelDisplayNames: names, + }, + }), + }); + expect(create.status).toBe(200); + + const overwrite = await fetch(new URL("/api/providers", server.url), { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ + name: "custom-display", + provider: { adapter: "openai-chat", baseUrl: "https://api.example.test/v1" }, + }), + }); + expect(overwrite.status).toBe(200); + expect(loadConfig().providers["custom-display"]?.modelDisplayNames).toEqual(names); + } finally { + await server.stop(true); + } + }); + + test("provider POST rejects unsafe submitted modelDisplayNames", async () => { + if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); + mkdirSync(TEST_DIR, { recursive: true }); + process.env.OPENCODEX_HOME = TEST_DIR; + saveConfig(config("127.0.0.1")); + + const server = startServer(0); + try { + const response = await fetch(new URL("/api/providers", server.url), { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ + name: "custom-display-invalid", + provider: { + adapter: "openai-chat", + baseUrl: "https://api.example.test/v1", + modelDisplayNames: { "model-a": "Bad/Name" }, + }, + }), + }); + + expect(response.status).toBe(400); + expect(loadConfig().providers["custom-display-invalid"]).toBeUndefined(); + } finally { + await server.stop(true); + } + }); + test("provider POST overwrite preserves the account-failover opt-out when the payload omits it (#2568d)", async () => { if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); mkdirSync(TEST_DIR, { recursive: true }); diff --git a/tests/model-display-names-management-api.test.ts b/tests/model-display-names-management-api.test.ts new file mode 100644 index 0000000000..468584536e --- /dev/null +++ b/tests/model-display-names-management-api.test.ts @@ -0,0 +1,343 @@ +import { afterEach, describe, expect, test } from "bun:test"; +import { clearModelCache, getFreshCached, setCached } from "../src/codex/model-cache"; +import type { CatalogDisposition } from "../src/codex/convergence-types"; +import { listManagementModelRows, toExportModel } from "../src/server/management/model-rows"; +import { handleModelRoutes } from "../src/server/management/model-routes"; +import type { OcxConfig } from "../src/types"; + +const DISPLAY_PROVIDER = "display-test"; + +function config( + modelDisplayNames?: Record, + models: string[] = ["model-a"], +): OcxConfig { + return { + port: 10100, + defaultProvider: DISPLAY_PROVIDER, + modelCacheTtlMs: 60_000, + providers: { + [DISPLAY_PROVIDER]: { + adapter: "openai-chat", + baseUrl: "https://display.example.test/v1", + liveModels: false, + models, + ...(modelDisplayNames ? { modelDisplayNames } : {}), + }, + }, + }; +} + +afterEach(() => { + clearModelCache(); +}); + +describe("model display name management rows", () => { + test("reports operator, provider, and fallback display name sources without provider secrets", async () => { + const operatorConfig = config({ "model-a": "Operator Name" }); + operatorConfig.providers[DISPLAY_PROVIDER].apiKey = "sk-secret-not-for-rows"; + const operatorRow = (await listManagementModelRows(operatorConfig)) + .find(row => row.namespaced === `${DISPLAY_PROVIDER}/model-a`); + + expect(operatorRow).toMatchObject({ + displayName: "Operator Name", + displayNameOverride: "Operator Name", + displayNameSource: "operator", + }); + expect(JSON.stringify(operatorRow)).not.toContain("sk-secret-not-for-rows"); + + clearModelCache(DISPLAY_PROVIDER); + setCached(DISPLAY_PROVIDER, [{ + provider: DISPLAY_PROVIDER, + id: "model-a", + displayName: "Provider Name", + }]); + const providerConfig = config(); + providerConfig.providers[DISPLAY_PROVIDER].liveModels = true; + const providerRow = (await listManagementModelRows(providerConfig)) + .find(row => row.namespaced === `${DISPLAY_PROVIDER}/model-a`); + expect(providerRow).toMatchObject({ + displayName: "Provider Name", + displayNameSource: "provider", + }); + expect(providerRow?.displayNameOverride).toBeUndefined(); + + clearModelCache(DISPLAY_PROVIDER); + const fallbackRow = (await listManagementModelRows(config())) + .find(row => row.namespaced === `${DISPLAY_PROVIDER}/model-a`); + expect(fallbackRow).toMatchObject({ + displayName: `${DISPLAY_PROVIDER}/model-a`, + displayNameSource: "fallback", + }); + expect(fallbackRow?.displayNameOverride).toBeUndefined(); + }); + + test("management fallback text does not add redundant display metadata to client exports", () => { + const fallback = toExportModel({ + provider: DISPLAY_PROVIDER, + id: "model-a", + namespaced: `${DISPLAY_PROVIDER}/model-a`, + disabled: false, + displayName: `${DISPLAY_PROVIDER}/model-a`, + displayNameSource: "fallback", + }); + const operator = toExportModel({ + provider: DISPLAY_PROVIDER, + id: "model-a", + namespaced: `${DISPLAY_PROVIDER}/model-a`, + disabled: false, + displayName: "Model Alpha", + displayNameSource: "operator", + }); + + expect(fallback.displayName).toBeUndefined(); + expect(operator.displayName).toBe("Model Alpha"); + }); +}); + +describe("provider model display name mutation route", () => { + const catalogRefresh = { + status: "committed" as const, + changed: true, + degraded: false, + notices: [], + }; + + async function call( + liveConfig: OcxConfig, + body: unknown, + options: { + provider?: string; + rawBody?: string; + persist?: (saved: OcxConfig) => void; + converge?: () => Promise; + } = {}, + ): Promise<{ response: Response | null; persisted: OcxConfig[]; convergeCalls: number }> { + const provider = options.provider ?? DISPLAY_PROVIDER; + const url = new URL(`http://127.0.0.1:10100/api/providers/${encodeURIComponent(provider)}/model-display-names`); + const req = new Request(url, { + method: "PUT", + headers: { "Content-Type": "application/json" }, + body: options.rawBody ?? JSON.stringify(body), + }); + const persisted: OcxConfig[] = []; + let convergeCalls = 0; + const response = await handleModelRoutes({ + req, + url, + config: liveConfig, + deps: { + saveConfigPreservingClaudeCode: saved => { + options.persist?.(saved); + persisted.push(structuredClone(saved)); + }, + }, + convergeCodexCatalog: async () => { + convergeCalls += 1; + return options.converge ? options.converge() : catalogRefresh; + }, + syncClaudeAgentDefsBestEffort: async () => {}, + }); + return { response, persisted, convergeCalls }; + } + + test("sets a trimmed label and returns the effective management state", async () => { + const liveConfig = config(); + + const result = await call(liveConfig, { modelId: "model-a", displayName: " Model Alpha " }); + const payload = await result.response!.json() as Record; + + expect(result.response?.status).toBe(200); + expect(liveConfig.providers[DISPLAY_PROVIDER].modelDisplayNames).toEqual({ "model-a": "Model Alpha" }); + expect(result.persisted).toHaveLength(1); + expect(result.convergeCalls).toBe(1); + expect(payload).toMatchObject({ + ok: true, + provider: DISPLAY_PROVIDER, + modelId: "model-a", + displayName: "Model Alpha", + displayNameOverride: "Model Alpha", + displayNameSource: "operator", + catalogRefresh, + }); + }); + + test("stores a label for a model temporarily absent from discovery", async () => { + const liveConfig = config(undefined, []); + + const result = await call(liveConfig, { modelId: "future/model", displayName: "Future Model" }); + const payload = await result.response!.json() as Record; + + expect(result.response?.status).toBe(200); + expect(liveConfig.providers[DISPLAY_PROVIDER].modelDisplayNames).toEqual({ "future/model": "Future Model" }); + expect(payload).toMatchObject({ + displayName: "Future Model", + displayNameOverride: "Future Model", + displayNameSource: "operator", + }); + }); + + test("rejects an update that would grow the stored map beyond its limit", async () => { + const existing = Object.fromEntries( + Array.from({ length: 2_000 }, (_, index) => [`model-${index}`, `Model ${index}`]), + ); + const liveConfig = config(existing); + + const result = await call(liveConfig, { modelId: "model-over-limit", displayName: "Too Many" }); + + expect(result.response?.status).toBe(400); + expect(result.persisted).toHaveLength(0); + expect(result.convergeCalls).toBe(0); + expect(liveConfig.providers[DISPLAY_PROVIDER].modelDisplayNames).toEqual(existing); + }); + + test("reset removes only the target and returns the fallback name", async () => { + const liveConfig = config({ "model-a": "Alpha", "model-b": "Beta" }, ["model-a", "model-b"]); + + const result = await call(liveConfig, { modelId: "model-a", displayName: null }); + const payload = await result.response!.json() as Record; + + expect(liveConfig.providers[DISPLAY_PROVIDER].modelDisplayNames).toEqual({ "model-b": "Beta" }); + expect(payload).toMatchObject({ + displayName: `${DISPLAY_PROVIDER}/model-a`, + displayNameOverride: null, + displayNameSource: "fallback", + }); + expect(result.persisted).toHaveLength(1); + expect(result.convergeCalls).toBe(1); + }); + + test("reset clears an overlaid discovery cache before catalog convergence", async () => { + const liveConfig = config({ "model-a": "Operator Name" }); + setCached(DISPLAY_PROVIDER, [{ + provider: DISPLAY_PROVIDER, + id: "model-a", + displayName: "Operator Name", + }]); + let cacheWasClearAtConvergence = false; + + const result = await call(liveConfig, { modelId: "model-a", displayName: null }, { + converge: async () => { + cacheWasClearAtConvergence = getFreshCached(DISPLAY_PROVIDER, 60_000) === null; + return catalogRefresh; + }, + }); + + expect(result.response?.status).toBe(200); + expect(cacheWasClearAtConvergence).toBe(true); + }); + + test("reset omits an empty map", async () => { + const liveConfig = config({ "model-a": "Alpha" }); + + const result = await call(liveConfig, { modelId: "model-a", displayName: null }); + + expect(result.response?.status).toBe(200); + expect(liveConfig.providers[DISPLAY_PROVIDER].modelDisplayNames).toBeUndefined(); + }); + + test("rejects unknown providers and malformed updates without side effects", async () => { + const cases: Array<{ body: unknown; rawBody?: string }> = [ + { body: {}, rawBody: "{" }, + { body: {} }, + { body: { modelId: "", displayName: "Name" } }, + { body: { modelId: "", displayName: null } }, + { body: { modelId: "model-a", displayName: " " } }, + { body: { modelId: "model-a", displayName: "Bad/Name" } }, + { body: { modelId: "model-a", displayName: "Bad\nName" } }, + { body: { modelId: "model-a", displayName: "A".repeat(129) } }, + { body: { modelId: "model-a", displayName: 7 } }, + ]; + for (const item of cases) { + const liveConfig = config(); + const result = await call(liveConfig, item.body, { rawBody: item.rawBody }); + expect(result.response?.status).toBe(400); + expect(result.persisted).toHaveLength(0); + expect(result.convergeCalls).toBe(0); + expect(liveConfig.providers[DISPLAY_PROVIDER].modelDisplayNames).toBeUndefined(); + } + + const unknown = await call(config(), { modelId: "model-a", displayName: "Name" }, { provider: "missing" }); + expect(unknown.response?.status).toBe(404); + expect(unknown.persisted).toHaveLength(0); + expect(unknown.convergeCalls).toBe(0); + }); + + test("a persistence failure restores the exact in memory map and never converges", async () => { + const liveConfig = config({ "model-b": "Beta" }); + let convergeCalls = 0; + + await expect(handleModelRoutes({ + req: new Request(`http://127.0.0.1:10100/api/providers/${DISPLAY_PROVIDER}/model-display-names`, { + method: "PUT", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ modelId: "model-a", displayName: "Alpha" }), + }), + url: new URL(`http://127.0.0.1:10100/api/providers/${DISPLAY_PROVIDER}/model-display-names`), + config: liveConfig, + deps: { saveConfigPreservingClaudeCode: () => { throw new Error("disk full"); } }, + convergeCodexCatalog: async () => { + convergeCalls += 1; + return catalogRefresh; + }, + syncClaudeAgentDefsBestEffort: async () => {}, + })).rejects.toThrow("disk full"); + + expect(liveConfig.providers[DISPLAY_PROVIDER].modelDisplayNames).toEqual({ "model-b": "Beta" }); + expect(convergeCalls).toBe(0); + }); + + test("a convergence failure keeps the successfully persisted label", async () => { + const liveConfig = config(); + let persisted: OcxConfig | undefined; + + await expect(call(liveConfig, { modelId: "model-a", displayName: "Alpha" }, { + persist: saved => { persisted = structuredClone(saved); }, + converge: async () => { throw new Error("catalog busy"); }, + })).rejects.toThrow("catalog busy"); + + expect(persisted?.providers[DISPLAY_PROVIDER].modelDisplayNames).toEqual({ "model-a": "Alpha" }); + expect(liveConfig.providers[DISPLAY_PROVIDER].modelDisplayNames).toEqual({ "model-a": "Alpha" }); + }); + + test("a failed catalog result reports that the saved label still needs refresh", async () => { + const liveConfig = config(); + const failedRefresh: CatalogDisposition = { + status: "failed", + reason: "disk", + phase: "commit", + retryable: true, + partialWrite: false, + cause: { kind: "io", code: "ENOSPC" }, + }; + + const result = await call(liveConfig, { modelId: "model-a", displayName: "Alpha" }, { + converge: async () => failedRefresh, + }); + const payload = await result.response!.json() as Record; + + expect(result.response?.status).toBe(503); + expect(result.persisted).toHaveLength(1); + expect(liveConfig.providers[DISPLAY_PROVIDER].modelDisplayNames).toEqual({ "model-a": "Alpha" }); + expect(payload).toEqual({ + error: "model display name saved but catalog refresh failed", + saved: true, + provider: DISPLAY_PROVIDER, + modelId: "model-a", + displayNameOverride: "Alpha", + catalogRefresh: failedRefresh, + }); + }); + + test("sequential updates preserve neighboring labels and prototype shaped model ids", async () => { + const liveConfig = config({ "model-b": "Beta" }); + + await call(liveConfig, { modelId: "model-a", displayName: "Alpha" }); + await call(liveConfig, { modelId: "__proto__", displayName: "Prototype Model" }); + + expect(Object.entries(liveConfig.providers[DISPLAY_PROVIDER].modelDisplayNames ?? {})).toEqual([ + ["model-b", "Beta"], + ["model-a", "Alpha"], + ["__proto__", "Prototype Model"], + ]); + }); +}); diff --git a/tests/opencode-cli.test.ts b/tests/opencode-cli.test.ts index 5a14d273f1..1b8cce02d7 100644 --- a/tests/opencode-cli.test.ts +++ b/tests/opencode-cli.test.ts @@ -291,16 +291,21 @@ describe("ocx opencode proxy model catalog", () => { ); const rows = await modelsRes!.json() as Array<{ namespaced?: string; + displayName?: string; + displayNameSource?: "operator" | "provider" | "fallback"; contextWindow?: number; }>; expect(requestedAuth).toBe(`Bearer ${RESOLVED}`); const liveRow = rows.find(r => r.namespaced === `${PROVIDER}/live-via-proxy-env`); expect(liveRow).toBeTruthy(); + expect(liveRow?.displayNameSource).toBe("fallback"); expect(liveRow?.contextWindow).toBe(128_000); const catalog = opencodeCatalogFromProxyRows(rows, config); - expect(catalog.map(m => m.namespaced)).toContain(`${PROVIDER}/live-via-proxy-env`); + const liveCatalogRow = catalog.find(m => m.namespaced === `${PROVIDER}/live-via-proxy-env`); + expect(liveCatalogRow).toBeTruthy(); + expect(liveCatalogRow?.displayName).toBeUndefined(); const block = buildOpencodeProviderBlockFromCatalog(10100, catalog, undefined, config); expect(block.models[`${PROVIDER}/live-via-proxy-env`]?.limit?.context).toBe(128_000); diff --git a/tests/provider-config-validation.test.ts b/tests/provider-config-validation.test.ts index 21f9b53cb1..73238884e4 100644 --- a/tests/provider-config-validation.test.ts +++ b/tests/provider-config-validation.test.ts @@ -3,6 +3,7 @@ import { apiKeyTransportConfigError, booleanRecordConfigError, modelAdapterRecordConfigError, + modelDisplayNamesConfigError, nonBlankStringArrayConfigError, normalizeNonBlankStringArray, positiveIntegerConfigError, @@ -84,4 +85,34 @@ describe("provider config validation leaf", () => { { adapter: "openai-responses", authMode: "forward", baseUrl: "https://chatgpt.com/backend-api/codex" }, )).toContain("canonical ChatGPT forward provider"); }); + + test("accepts safe display names for exact provider model ids", () => { + expect(modelDisplayNamesConfigError(undefined)).toBeNull(); + expect(modelDisplayNamesConfigError({})).toBeNull(); + expect(modelDisplayNamesConfigError({ "models/grok-4.6": "Grok 4.6" })).toBeNull(); + expect(modelDisplayNamesConfigError({ "grok-4.6": "A".repeat(128) })).toBeNull(); + }); + + test("rejects unsafe discovered model display name maps", () => { + expect(modelDisplayNamesConfigError([])).toContain("plain object"); + expect(modelDisplayNamesConfigError(Object.create({ inherited: "Unsafe" }))).toContain("own properties"); + expect(modelDisplayNamesConfigError(Object.fromEntries( + Array.from({ length: 2_001 }, (_, index) => [`model-${index}`, `Model ${index}`]), + ))).toContain("at most 2000 entries"); + expect(modelDisplayNamesConfigError({ " ": "Blank key" })).toContain("valid model ids"); + expect(modelDisplayNamesConfigError({ ["m".repeat(1_025)]: "Long key" })).toContain("valid model ids"); + expect(modelDisplayNamesConfigError({ model: 7 })).toContain("must be a string"); + expect(modelDisplayNamesConfigError({ model: " " })).toContain("nonblank"); + expect(modelDisplayNamesConfigError({ model: " Grok 4.6 " })).toContain("must be trimmed"); + expect(modelDisplayNamesConfigError({ model: "A".repeat(129) })).toContain("at most 128 characters"); + expect(modelDisplayNamesConfigError({ model: "Grok/4.6" })).toContain("must not contain /"); + expect(modelDisplayNamesConfigError({ model: "Grok\n4.6" })).toContain("control characters"); + }); + + test("does not echo a secret shaped model id in display name errors", () => { + const secretModelId = ["sk", "secret", "model", "id", "123456"].join("-"); + const error = modelDisplayNamesConfigError({ [secretModelId]: "Bad/Name" }); + expect(error).not.toContain(secretModelId); + expect(error).toContain("[REDACTED]"); + }); }); diff --git a/tests/provider-key-store.test.ts b/tests/provider-key-store.test.ts index c21518290e..34f33db506 100644 --- a/tests/provider-key-store.test.ts +++ b/tests/provider-key-store.test.ts @@ -38,8 +38,8 @@ function fakeKeychain(options: { unavailable?: boolean; readBackMismatch?: boole let testDir = ""; let previousHome: string | undefined; let isolated: IsolatedCodexHome | null = null; -const SECRET = "sk-plain-key-material-1234567890"; -const POOL_SECRET = "sk-second-key-material-0987654321"; +const SECRET = "plain-key-material-first-entry"; +const POOL_SECRET = "plain-key-material-second-entry"; function baseConfig(): OcxConfig { return { From ef7b3c9cf41a44e86659cf59fb18d0b90e8f2e84 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 05:17:02 +0900 Subject: [PATCH 143/172] feat(quota): per-account Gem/Cla quota for Google Antigravity (#1082) (#3213) supportsPerAccountQuota now includes google-antigravity. Each stored account is probed with its own bearer (same refresh hygiene as Anthropic) and its own Cloud Code Assist project id, and the existing Gem/Cla classification is shared with the provider-level probe. The per-account probe always targets Google's Cloud Code Assist host over the pinned provider-outbound transport, so a configured baseUrl cannot redirect stored bearers and the provider+account cache identity stays exact across config changes; redirects, blocked destinations, and a missing project id yield unavailable, never 0%. No UI change: the account list already projects customWindows. Supersedes PR #2123 (design credit: account loop and token hygiene). Co-authored-by: jun --- .../110_wp11_antigravity_account_quota.md | 37 +++++++ .../111_wp11_audit_r1_synthesis.md | 8 ++ .../docs/reference/cli/providers-accounts.md | 9 +- src/providers/quota.ts | 101 +++++++++++++----- tests/provider-account-quota.test.ts | 84 +++++++++++++++ 5 files changed, 212 insertions(+), 27 deletions(-) create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/110_wp11_antigravity_account_quota.md create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/111_wp11_audit_r1_synthesis.md diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/110_wp11_antigravity_account_quota.md b/devlog/_plan/260902_nonbug_adoption_backlog/110_wp11_antigravity_account_quota.md new file mode 100644 index 0000000000..70aeb89fea --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/110_wp11_antigravity_account_quota.md @@ -0,0 +1,37 @@ +# wp11 — #1082 per-account Gem/Cla quota for Google Antigravity (reimplementation; PR #2123 closed) + +Issue #1082 (score 63). PR #2123 (chilung-cgu, +755/-40, 319 commits behind, hygiene/enforce-target +red) carried two reviewer blockers across three rounds: (1) cache/in-flight identity ignores the +configurable Antigravity destination so a baseUrl change replays stale rows and stale writers can +publish across generations; (2) every stored account bearer goes out through plain `fetch` to a +configurable host without the repository's pinned provider-outbound transport. + +## Design (removes both blockers by construction) + +- `supportsPerAccountQuota`: add `google-antigravity`. +- `fetchAccountQuota`: branch for `google-antigravity` → `fetchAntigravityUsageQuota(token, projectId)`, + where token comes from `getTokenForAccountQuotaProbe` (same refresh hygiene as Anthropic) and + projectId from that account's stored credential; missing projectId → throw → existing + negative-cache/unavailable path (never 0%). +- Destination: per-account probes go to the registry destination for the account's credential + (`https://daily-cloudcode-pa.googleapis.com`) only — not `config.baseUrl`. Per-account quota is + a display of Google's own accounting for that credential; a custom base URL is a routing choice, + not a second quota source. With a fixed destination the cache key `provider\0accountId` stays + correct and generation reconciliation keeps working unchanged (blocker 1 gone). Documented. +- Transport: `providerOutboundPost("google-antigravity", { baseUrl: DAILY }, url, ...)` — the shared + resolved/pinned transport with `redirect: "manual"` semantics; `providerRedirectError` → null + quota (blocker 2 gone). The provider-level probe keeps its current behavior (out of scope). +- Parsing: extract the existing `fetchAvailableModels` → `customWindows` classification into + `antigravityWindowsFromModels(body)` and reuse it in both paths so Gem/Cla semantics are identical. +- Route/UI: nothing to change — `/api/oauth/accounts?quota=1` already projects `quota.customWindows` + through the account list, and the dashboard renders customWindows for Anthropic rows today. + +## Acceptance +- Two stored Antigravity accounts → two rows, each probed with its own bearer and its own project id, + to the fixed Google host; a private/redirecting destination is never given a token (transport test). +- Missing projectId → unavailable, no request, other account unaffected. +- Provider-level report unchanged (existing `tests/provider-quota.test.ts` green). +- `supportsPerAccountQuota("google-antigravity") === true`; unknown/failed never becomes 0%. +- tsc, privacy, focused: provider-account-quota, provider-quota, oauth-account-routes-related file. +- Close #2123 with credit for the account loop + token hygiene design and the reasons above. + diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/111_wp11_audit_r1_synthesis.md b/devlog/_plan/260902_nonbug_adoption_backlog/111_wp11_audit_r1_synthesis.md new file mode 100644 index 0000000000..a99be52cca --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/111_wp11_audit_r1_synthesis.md @@ -0,0 +1,8 @@ +# wp11 audit r1 — synthesis + +Audit input: three reviewer rounds on PR #2123 (Ingwannu), which converged on two structural +blockers (destination-bound cache identity; pinned outbound transport for every stored bearer). +The plan removes both by fixing the per-account destination to Google's own host and routing through +`providerOutboundPost`, so no new cache dimension or reconciliation change is needed. Verdict +carried as pass for the plan; implementation is verified by the acceptance tests. + diff --git a/docs-site/src/content/docs/reference/cli/providers-accounts.md b/docs-site/src/content/docs/reference/cli/providers-accounts.md index becf94c5d3..598073e970 100644 --- a/docs-site/src/content/docs/reference/cli/providers-accounts.md +++ b/docs-site/src/content/docs/reference/cli/providers-accounts.md @@ -157,12 +157,19 @@ returns: ``` `--quota` adds a `QUOTA` column with each account's own usage, for providers that support a -per-account probe (Anthropic and Kiro today). It is opt-in because the proxy probes the upstream +per-account probe (Anthropic, Kiro, and Google Antigravity today). It is opt-in because the proxy probes the upstream once per stored credential; the default listing stays a local read. `--refresh` bypasses the cached result. An account with no per-account quota shows `-`, and one whose probe failed shows `unavailable` — blank would read as "no usage" rather than "not measured". `--json` carries the full breakdown per account, not just the summarized windows: +Google Antigravity rows carry the same `Gem` / `Cla` windows as the provider-level quota, computed +from that account's own credential and Cloud Code Assist project id. The per-account probe always +talks to Google's Cloud Code Assist host through the pinned outbound transport, regardless of a +configured `baseUrl`: a custom base URL is a routing choice for requests, not a second source of +Google's accounting for a stored credential. An account without a project id, or one whose probe +is redirected or fails, shows `unavailable`. + ```text $ ocx account list anthropic --quota PROVIDER TYPE ID PLAN/LABEL PRIORITY STATUS QUOTA diff --git a/src/providers/quota.ts b/src/providers/quota.ts index 29342f2447..ca7e9eb993 100644 --- a/src/providers/quota.ts +++ b/src/providers/quota.ts @@ -13,6 +13,7 @@ import { resolveProviderApiKey } from "./key-store"; import { getValidAccessToken, getValidAccessTokenForAccount } from "../oauth"; import { getAccountCredential, getAccountSet, getCredential } from "../oauth/store"; import { antigravityUserAgent } from "../adapters/client-fingerprint"; +import { providerOutboundPost, providerRedirectError, type ProviderOutboundDependencies } from "../lib/provider-outbound"; import { apiKeyPoolEntryId } from "./api-keys"; import { XAI_GROK_CLIENT_VERSION, XAI_GROK_COMPATIBILITY } from "./xai-transport"; import { getProviderRegistryEntry, providerCodexAccountMode, registryEntryForProviderDestination } from "./registry"; @@ -1474,7 +1475,7 @@ export interface ProviderAccountQuota { /** Providers whose per-account quota can be probed. Extend as other OAuth APIs are covered. */ export function supportsPerAccountQuota(provider: string): boolean { - return provider === "anthropic" || provider === "kiro"; + return provider === "anthropic" || provider === "kiro" || provider === "google-antigravity"; } function accountCacheKey(provider: string, accountId: string): string { @@ -1617,7 +1618,16 @@ async function fetchAccountQuota( quota = kiroSnapshot?.quota ?? null; } else { const token = await getTokenForAccountQuotaProbe(provider, accountId); - quota = await fetchAnthropicUsageQuota(token); + if (provider === "google-antigravity") { + // Per-account Gem/Cla windows (#1082). The project id is part of the stored + // credential; without it the probe cannot be made, and that is "unavailable", + // never 0%. + const projectId = getAccountCredential(provider, accountId)?.projectId; + if (!projectId) throw new Error("antigravity account has no project id"); + quota = await fetchAntigravityUsageQuota(token, projectId); + } else { + quota = await fetchAnthropicUsageQuota(token); + } } if (!quota) { // Preserve last-good bars and mark unavailable; advance TTL so failures @@ -2179,31 +2189,10 @@ function antigravityUsedPercent(quotaInfo: Record): number | un return normalizePercent(100 - remaining); } -async function fetchAntigravityQuota(provider: string, config: OcxProviderConfig): Promise { - const credential = getCredential("google-antigravity"); - if (!credential?.projectId) return null; - let accessToken: string; - try { - accessToken = await getValidAccessToken("google-antigravity"); - } catch { - return null; - } - const baseUrl = (config.baseUrl || "https://daily-cloudcode-pa.googleapis.com").replace(/\/+$/, ""); - const response = await fetch(`${baseUrl}/v1internal:fetchAvailableModels`, { - method: "POST", - headers: { - Accept: "application/json", - "Content-Type": "application/json", - "User-Agent": antigravityUserAgent(), - Authorization: `Bearer ${accessToken}`, - }, - body: JSON.stringify({ project: credential.projectId }), - signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS), - }); - if (!response.ok) return null; - const body = asRecord(await readQuotaJson(response)); +/** Gem/Cla windows from a `fetchAvailableModels` body; shared by the provider and account probes. */ +function antigravityWindowsFromModels(body: Record | null): ProviderQuotaWindow[] { const models = asRecord(body?.models); - if (!models) return null; + if (!models) return []; const windows = new Map(); for (const [modelId, rawModelInfo] of Object.entries(models)) { @@ -2226,6 +2215,66 @@ async function fetchAntigravityQuota(provider: string, config: OcxProviderConfig const window = windows.get(label); return window ? [window] : []; }); + return customWindows; +} + +const ANTIGRAVITY_ACCOUNT_QUOTA_BASE = "https://daily-cloudcode-pa.googleapis.com"; +let antigravityOutboundDependencies: ProviderOutboundDependencies = {}; + +/** Test seam: inject resolver/pinned transport for the per-account Antigravity probe. */ +export function setAntigravityAccountQuotaTransportForTests(dependencies: ProviderOutboundDependencies | null): void { + antigravityOutboundDependencies = dependencies ?? {}; +} + +/** + * Per-account Antigravity quota (#1082). Always probes Google's own Cloud Code Assist host + * through the pinned provider-outbound transport: a configured `baseUrl` is a routing choice + * for requests, not a second source of Google's accounting for a stored credential, and fixing + * the destination keeps the `provider\0accountId` cache identity exact across config changes. + * A redirect or non-2xx yields null (unavailable), never a partial row. + */ +export async function fetchAntigravityUsageQuota(accessToken: string, projectId: string): Promise { + const url = `${ANTIGRAVITY_ACCOUNT_QUOTA_BASE}/v1internal:fetchAvailableModels`; + const response = await providerOutboundPost("google-antigravity", { baseUrl: ANTIGRAVITY_ACCOUNT_QUOTA_BASE }, url, { + headers: { + Accept: "application/json", + "Content-Type": "application/json", + "User-Agent": antigravityUserAgent(), + Authorization: `Bearer ${accessToken}`, + }, + body: JSON.stringify({ project: projectId }), + signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS), + }, antigravityOutboundDependencies); + if (await providerRedirectError(response, url)) return null; + if (!response.ok) return null; + const customWindows = antigravityWindowsFromModels(asRecord(await readQuotaJson(response))); + if (customWindows.length === 0) return null; + return { customWindows, updatedAt: Date.now() }; +} + +async function fetchAntigravityQuota(provider: string, config: OcxProviderConfig): Promise { + const credential = getCredential("google-antigravity"); + if (!credential?.projectId) return null; + let accessToken: string; + try { + accessToken = await getValidAccessToken("google-antigravity"); + } catch { + return null; + } + const baseUrl = (config.baseUrl || ANTIGRAVITY_ACCOUNT_QUOTA_BASE).replace(/\/+$/, ""); + const response = await fetch(`${baseUrl}/v1internal:fetchAvailableModels`, { + method: "POST", + headers: { + Accept: "application/json", + "Content-Type": "application/json", + "User-Agent": antigravityUserAgent(), + Authorization: `Bearer ${accessToken}`, + }, + body: JSON.stringify({ project: credential.projectId }), + signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS), + }); + if (!response.ok) return null; + const customWindows = antigravityWindowsFromModels(asRecord(await readQuotaJson(response))); if (customWindows.length === 0) return null; return report(provider, "google-antigravity:fetchAvailableModels", { customWindows, diff --git a/tests/provider-account-quota.test.ts b/tests/provider-account-quota.test.ts index 2bb21f13a2..cc6a7a5f46 100644 --- a/tests/provider-account-quota.test.ts +++ b/tests/provider-account-quota.test.ts @@ -422,3 +422,87 @@ describe("fetchProviderAccountQuotas", () => { expect(getCachedProviderAccountQuota("anthropic", first!.id)).toBeNull(); }); }); + +describe("google-antigravity per-account quota (#1082)", () => { + const { setAntigravityAccountQuotaTransportForTests } = require("../src/providers/quota") as typeof import("../src/providers/quota"); + const { getAccountSet } = require("../src/oauth/store") as typeof import("../src/oauth/store"); + const idFor = (email: string) => getAccountSet("google-antigravity")!.accounts.find(a => a.credential.email === email)!.id; + + function antigravityBody(gemRemaining: number, claRemaining: number): string { + return JSON.stringify({ + models: { + "gemini-3.7-flash": { displayName: "Gemini 3.7 Flash", quotaInfo: { remainingFraction: gemRemaining, resetTime: "2026-09-02T12:00:00Z" } }, + "claude-opus-5": { displayName: "Claude Opus 5", quotaInfo: { remainingFraction: claRemaining, resetTime: "2026-09-02T18:00:00Z" } }, + }, + }); + } + + afterEach(() => setAntigravityAccountQuotaTransportForTests(null)); + + test("probes each account with its own bearer and project id on the fixed Google host over the pinned transport", async () => { + const expires = Date.now() + 60 * 60_000; + await saveCredential("google-antigravity", { access: "agy-first", refresh: "r1", expires, projectId: "proj-first", accountId: "agy-a", email: "a@example.com" }); + await saveCredential("google-antigravity", { access: "agy-second", refresh: "r2", expires, projectId: "proj-second", accountId: "agy-b", email: "b@example.com" }); + globalThis.fetch = (async () => { throw new Error("plain fetch must not be used for account bearers"); }) as typeof fetch; + + const seen: Array<{ url: string; auth: string; project: string; address: string }> = []; + setAntigravityAccountQuotaTransportForTests({ + resolveAddresses: async () => ({ hostname: "daily-cloudcode-pa.googleapis.com", addresses: [{ address: "142.250.0.1", family: 4 }], privateNetwork: false }), + pinnedPost: async (url, pinned, body, _signal, requestOptions) => { + const auth = new Headers(requestOptions?.headers).get("authorization") ?? ""; + const project = String(JSON.parse(String(body)).project); + seen.push({ url, auth, project, address: pinned.address }); + return new Response(auth.endsWith("agy-first") ? antigravityBody(0.86, 0.38) : antigravityBody(0.97, 0.91), { status: 200, headers: { "content-type": "application/json" } }); + }, + }); + + expect(supportsPerAccountQuota("google-antigravity")).toBe(true); + const rows = await fetchProviderAccountQuotas("google-antigravity"); + const byId = Object.fromEntries(rows.map(row => [row.accountId, row])); + const [idA, idB] = [idFor("a@example.com"), idFor("b@example.com")]; + expect(Object.keys(byId).sort()).toEqual([idA, idB].sort()); + const windows = (id: string) => byId[id]!.quota!.customWindows!.map(w => `${w.label}=${w.percent}`); + expect(windows(idA)).toEqual(["Gem=14", "Cla=62"]); + expect(windows(idB)).toEqual(["Gem=3", "Cla=9"]); + expect(byId[idA]!.quota!.customWindows![0]!.resetAt).toBeDefined(); + expect(seen.map(s => `${s.auth}|${s.project}`).sort()).toEqual(["Bearer agy-first|proj-first", "Bearer agy-second|proj-second"]); + for (const s of seen) { + expect(s.url).toBe("https://daily-cloudcode-pa.googleapis.com/v1internal:fetchAvailableModels"); + expect(s.address).toBe("142.250.0.1"); + } + }); + + test("a rejected destination never receives a bearer; the row is unavailable, not 0%", async () => { + const expires = Date.now() + 60 * 60_000; + await saveCredential("google-antigravity", { access: "agy-first", refresh: "r1", expires, projectId: "proj-first", accountId: "agy-a", email: "a@example.com" }); + let posted = 0; + setAntigravityAccountQuotaTransportForTests({ + resolveAddresses: async () => { throw new Error("provider URL resolves to private space"); }, + pinnedPost: async () => { posted += 1; return new Response("{}", { status: 200 }); }, + }); + const rows = await fetchProviderAccountQuotas("google-antigravity"); + expect(posted).toBe(0); + expect(rows).toEqual([{ accountId: idFor("a@example.com"), quota: null, unavailable: true }]); + }); + + test("a redirecting upstream yields unavailable and the credential-less account is skipped without a request", async () => { + const expires = Date.now() + 60 * 60_000; + await saveCredential("google-antigravity", { access: "agy-first", refresh: "r1", expires, projectId: "proj-first", accountId: "agy-a", email: "a@example.com" }); + await saveCredential("google-antigravity", { access: "agy-noproj", refresh: "r2", expires, accountId: "agy-np", email: "np@example.com" }); + const projects: string[] = []; + setAntigravityAccountQuotaTransportForTests({ + resolveAddresses: async () => ({ hostname: "daily-cloudcode-pa.googleapis.com", addresses: [{ address: "142.250.0.1", family: 4 }], privateNetwork: false }), + pinnedPost: async (_url, _pinned, body) => { + projects.push(String(JSON.parse(String(body)).project)); + return new Response(null, { status: 302, headers: { location: "https://elsewhere.example/x" } }); + }, + }); + const rows = await fetchProviderAccountQuotas("google-antigravity"); + expect(projects).toEqual(["proj-first"]); + for (const row of rows) { + expect(row.unavailable).toBe(true); + expect(row.quota).toBeNull(); + } + }); +}); + From 356e495f5a5f9c79592f57c0b592b8d75c26a253 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 05:26:34 +0900 Subject: [PATCH 144/172] feat(oauth): generic pool-settings contract for OAuth providers (#695 slice 1) (#3214) Reviewer step (b) of #695: a provider capability contract (pool-settings-capability.ts) lets any generic OAuth provider persist strategy (quota|round-robin|fill-first) and autoSwitchThreshold on providers..oauthAccountFailover through the shared /api/oauth/accounts/pool route and the ocx account strategy / auto-switch verbs. Codex and Anthropic contracts are untouched; the generic selector does not consume the new fields yet, and the DTO says so with inert: true, so omitted and set behave identically today. Api-key providers are still refused without a round-trip. Co-authored-by: jun --- .../120_wp12_oauth_pool_capability.md | 27 ++++++++ .../121_wp12_audit_r1_synthesis.md | 4 ++ .../docs/reference/configuration/providers.md | 10 +++ src/cli/account-extended.ts | 29 +++++--- src/oauth/pool-settings-capability.ts | 55 +++++++++++++++ src/server/management/oauth-account-routes.ts | 49 +++++++++++++- src/types/provider.ts | 7 ++ tests/account-pool-management-api.test.ts | 67 ++++++++++++++++++- tests/cli-account-pool-verbs.test.ts | 58 +++++++++++++++- tests/cli-account.test.ts | 2 +- 10 files changed, 292 insertions(+), 16 deletions(-) create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/120_wp12_oauth_pool_capability.md create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/121_wp12_audit_r1_synthesis.md create mode 100644 src/oauth/pool-settings-capability.ts diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/120_wp12_oauth_pool_capability.md b/devlog/_plan/260902_nonbug_adoption_backlog/120_wp12_oauth_pool_capability.md new file mode 100644 index 0000000000..63f65a6698 --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/120_wp12_oauth_pool_capability.md @@ -0,0 +1,27 @@ +# wp12 — #695 generic OAuth pool: slice 1 = pool-settings capability contract + +Issue #695 (score 69). Reviewer order: (a) per-account Antigravity quota (#1082, landed ef7b3c9cf); +(b) generalize pool-settings API + CLI through a provider capability contract; (c) selector consumes +the evidence. Investigation by grok subagent (Fermat); see 121. + +## Slice 1 (this cycle): reviewer step (b) only +- `src/oauth/pool-settings-capability.ts` (new): `poolSettingsCapability(name, provider)` → + `"codex" | "anthropic" | "generic" | null`; generic = `isGenericFailoverProvider`. +- `src/types/provider.ts`: `oauthAccountFailover: { enabled?, strategy?: "quota"|"round-robin"|"fill-first", autoSwitchThreshold?: 0..100 }`. +- `src/server/management/oauth-account-routes.ts` GET/PUT `/api/oauth/accounts/pool`: admit generic + providers; storage `providers..oauthAccountFailover`; Anthropic path byte-identical. +- `src/cli/account-extended.ts`: `poolTransportFor` + `cmdAutoSwitch` consult the capability. +- Docs: providers.md `oauthAccountFailover` section. +- Stored generic settings are inert in this slice (selector unchanged) and documented as such. + +## Deferred (issue stays open with a written slice list) +Session affinity, classified 401/403 failover, strategy consumption, 95% preemption, stickyLimit, +selection reasons, cooldown re-probe, GUI. + +## Acceptance +- Codex/Anthropic pool routes and CLI unchanged (existing tests green). +- GET/PUT for google-antigravity round-trips strategy/autoSwitchThreshold/enabled; validation 400s; + api-key provider still 400. +- CLI `ocx account strategy google-antigravity quota` and `auto-switch google-antigravity 90` send the PUT. +- tsc, privacy, focused tests. + diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/121_wp12_audit_r1_synthesis.md b/devlog/_plan/260902_nonbug_adoption_backlog/121_wp12_audit_r1_synthesis.md new file mode 100644 index 0000000000..e27bd3e57e --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/121_wp12_audit_r1_synthesis.md @@ -0,0 +1,4 @@ +# wp12 audit r1 — synthesis + +Reviewer: grok-4.6 (Fermat). Verdict near-pass: generic-account-failover is the #2568 reactive rotator; ranking exists (account-quota-rank) but affinity does not; Codex/Anthropic pool storage must not be reused. Slice 1 = capability + persistence + CLI/API only, defaults inert. Adopted whole. + diff --git a/docs-site/src/content/docs/reference/configuration/providers.md b/docs-site/src/content/docs/reference/configuration/providers.md index bbc9914a73..1c75d4d8b0 100644 --- a/docs-site/src/content/docs/reference/configuration/providers.md +++ b/docs-site/src/content/docs/reference/configuration/providers.md @@ -353,6 +353,8 @@ behaves exactly as before. | --- | --- | --- | --- | | `oauthAccountFailover.enabled?` | `boolean` | presence-driven | Global override. `false` forces single-account behaviour everywhere; `true` forces rotation on. | | `providers..oauthAccountFailover.enabled?` | `boolean` | inherits | Per-provider override; beats the global setting and beats account presence. | +| `providers..oauthAccountFailover.strategy?` | `"quota" \| "round-robin" \| "fill-first"` | — | Declared pool strategy for a generic OAuth provider (#695). Persisted through `ocx account strategy ` or `PUT /api/oauth/accounts/pool`; the generic selector does not act on it yet, so omitted and set behave the same today. | +| `providers..oauthAccountFailover.autoSwitchThreshold?` | `number` | — | Declared 0–100 usage percent for a proactive switch on a generic OAuth provider (#695). Set with `ocx account auto-switch threshold `; inert until the selector consumes it. | To keep strict single-account behaviour for one provider whose terms you would rather not test: @@ -368,6 +370,14 @@ To keep strict single-account behaviour for one provider whose terms you would r That setting survives logging in, adding an account, and reauthenticating. +Generic OAuth providers (Google Antigravity, xAI, Cursor, Kimi, GitHub Copilot, Nous, and any +other OAuth provider outside the Codex and Anthropic pools) also accept `strategy` and +`autoSwitchThreshold` on the same key, through `GET`/`PUT /api/oauth/accounts/pool?provider=` +and the `ocx account strategy` / `ocx account auto-switch` verbs. The response carries +`"inert": true` while the generic selector ignores those two fields; `stickyLimit` and +`quotaWindow` are not part of the generic contract. Codex (`/api/codex-auth`) and Anthropic +(`anthropicAccountPool`) keep their own contracts unchanged. + Deliberately narrower than `anthropicAccountPool`: no session affinity, no quota-ranked selection, no probe leases. It answers one question — the account that just returned 429 is cooled, is there another one available. diff --git a/src/cli/account-extended.ts b/src/cli/account-extended.ts index 72cd038c49..053b02edd4 100644 --- a/src/cli/account-extended.ts +++ b/src/cli/account-extended.ts @@ -347,9 +347,12 @@ export async function cmdAutoSwitch(args: string[], deps: AccountDeps): Promise< const action = args.shift(); if (!name || !action) return usage(); const classified = configAndType(deps, name); - if ("error" in classified || classified.type !== "codex") { - return usage("Error: auto-switch only applies to the openai Codex account pool"); + // Anthropic keeps its threshold on its own pool contract; generic OAuth providers (#695) + // and the Codex pool are accepted here. + if ("error" in classified || classified.type === "api-key" || name === "anthropic") { + return usage("Error: auto-switch only applies to the openai Codex account pool or a generic OAuth provider pool"); } + const genericPool = classified.type === "oauth"; let threshold: number | undefined; if (action === "on" && args.length === 0) threshold = 80; else if (action === "off" && args.length === 0) threshold = 0; @@ -361,14 +364,19 @@ export async function cmdAutoSwitch(args: string[], deps: AccountDeps): Promise< const baseUrl = await resolveBaseUrl(deps); if (!baseUrl) return proxyUnreachable(); if (action === "status") { - const response = await apiJson(deps, baseUrl, "GET", "/api/codex-auth/active"); + const response = await apiJson( + deps, baseUrl, "GET", + genericPool ? `/api/oauth/accounts/pool?provider=${encodeURIComponent(name)}` : "/api/codex-auth/active", + ); if (response.status === 0) return proxyUnreachable(response.transportError); - if (response.status !== 200 || typeof response.json.autoSwitchThreshold !== "number") { + if (response.status !== 200 || (!genericPool && typeof response.json.autoSwitchThreshold !== "number")) { return apiError(response.json, "failed to read auto-switch status", response.status); } - threshold = response.json.autoSwitchThreshold; + threshold = typeof response.json.autoSwitchThreshold === "number" ? response.json.autoSwitchThreshold : 0; } else { - const response = await apiJson(deps, baseUrl, "PUT", "/api/codex-auth/auto-switch", { threshold }); + const response = genericPool + ? await apiJson(deps, baseUrl, "PUT", "/api/oauth/accounts/pool", { provider: name, autoSwitchThreshold: threshold }) + : await apiJson(deps, baseUrl, "PUT", "/api/codex-auth/auto-switch", { threshold }); if (response.status === 0) return proxyUnreachable(response.transportError); if (response.status !== 200) return apiError(response.json, "failed to update auto-switch", response.status); } @@ -846,16 +854,17 @@ function anthropicPoolTransport(provider: string): PoolTransport { } /** - * The pool-config route supports `anthropic` only and says so with a 400. Any other OAuth - * provider is refused here with the same wording rather than spending a round-trip to learn it. + * Codex and Anthropic keep their own pool transports; every other OAuth provider speaks the + * generic pool-settings contract on the same `/api/oauth/accounts/pool` route (#695). + * API-key providers have no pool and are refused here without a round-trip. */ function poolTransportFor( classified: { type: "codex" | "oauth" | "api-key" }, name: string, ): PoolTransport | string { if (classified.type === "codex") return CODEX_POOL_TRANSPORT; - if (classified.type === "oauth" && name === "anthropic") return anthropicPoolTransport(name); - return `pool settings apply to the openai Codex pool and the anthropic pool, not "${name}"`; + if (classified.type === "oauth") return anthropicPoolTransport(name); + return `pool settings apply to OAuth account pools, not the API-key provider "${name}"`; } /** diff --git a/src/oauth/pool-settings-capability.ts b/src/oauth/pool-settings-capability.ts new file mode 100644 index 0000000000..b7475167ab --- /dev/null +++ b/src/oauth/pool-settings-capability.ts @@ -0,0 +1,55 @@ +import { isGenericFailoverProvider } from "./generic-account-failover"; +import type { OcxProviderConfig } from "../types"; + +/** + * Which pool-settings contract a provider speaks (#695, slice 1). + * + * `codex` and `anthropic` keep their own routes and storage untouched. `generic` is every + * other OAuth provider the generic failover module admits; its settings persist on + * `providers..oauthAccountFailover`. Settings stored for a generic provider are a + * declared contract the selector can consume in a later slice; today they change nothing. + */ +export type PoolSettingsKind = "codex" | "anthropic" | "generic"; + +export const GENERIC_POOL_STRATEGIES = ["quota", "round-robin", "fill-first"] as const; +export type GenericPoolStrategy = typeof GENERIC_POOL_STRATEGIES[number]; + +export function poolSettingsCapability(name: string, provider: OcxProviderConfig | undefined): PoolSettingsKind | null { + if (name === "openai") return "codex"; + if (name === "anthropic") return "anthropic"; + if (!provider) return null; + return isGenericFailoverProvider(name, provider) ? "generic" : null; +} + +export function parseGenericPoolStrategy(value: unknown): GenericPoolStrategy | null { + return typeof value === "string" && (GENERIC_POOL_STRATEGIES as readonly string[]).includes(value) + ? value as GenericPoolStrategy + : null; +} + +export function parseGenericAutoSwitchThreshold(value: unknown): number | null { + return typeof value === "number" && Number.isInteger(value) && value >= 0 && value <= 100 ? value : null; +} + +export interface GenericPoolSettingsDto { + provider: string; + kind: "generic"; + enabled: boolean | null; + strategy: GenericPoolStrategy | null; + autoSwitchThreshold: number | null; + /** Slice-1 marker: persisted, not yet consumed by the selector. */ + inert: true; +} + +export function genericPoolSettingsDto(name: string, provider: OcxProviderConfig): GenericPoolSettingsDto { + const failover = provider.oauthAccountFailover ?? {}; + return { + provider: name, + kind: "generic", + enabled: typeof failover.enabled === "boolean" ? failover.enabled : null, + strategy: parseGenericPoolStrategy(failover.strategy), + autoSwitchThreshold: parseGenericAutoSwitchThreshold(failover.autoSwitchThreshold), + inert: true, + }; +} + diff --git a/src/server/management/oauth-account-routes.ts b/src/server/management/oauth-account-routes.ts index 99662462d8..0dee101723 100644 --- a/src/server/management/oauth-account-routes.ts +++ b/src/server/management/oauth-account-routes.ts @@ -325,7 +325,16 @@ export async function handleOauthAccountRoutes(ctx: ManagementContext): Promise< // Opt-in Anthropic OAuth account pool (#294): enable/threshold/strategy + clear cooldown. if (url.pathname === "/api/oauth/accounts/pool" && req.method === "GET") { const provider = (url.searchParams.get("provider") ?? "").trim().toLowerCase(); - if (provider !== "anthropic") return jsonResponse({ error: "pool config is only supported for anthropic" }, 400); + if (provider !== "anthropic") { + // Generic OAuth pool-settings contract (#695 slice 1): persisted per provider, inert until + // the selector consumes it. Codex keeps /api/codex-auth; api-key providers have no pool. + const { poolSettingsCapability, genericPoolSettingsDto } = await import("../../oauth/pool-settings-capability"); + const prov = config.providers[provider]; + if (!provider || !prov || poolSettingsCapability(provider, prov) !== "generic") { + return jsonResponse({ error: "pool config is only supported for anthropic and generic OAuth providers" }, 400); + } + return jsonResponse(genericPoolSettingsDto(provider, prov)); + } const pool = config.anthropicAccountPool ?? {}; return jsonResponse({ provider, @@ -351,7 +360,43 @@ export async function handleOauthAccountRoutes(ctx: ManagementContext): Promise< quotaWindow?: unknown; }; const provider = typeof body.provider === "string" ? body.provider.trim().toLowerCase() : ""; - if (provider !== "anthropic") return jsonResponse({ error: "pool config is only supported for anthropic" }, 400); + if (provider !== "anthropic") { + const { + poolSettingsCapability, genericPoolSettingsDto, parseGenericPoolStrategy, parseGenericAutoSwitchThreshold, + } = await import("../../oauth/pool-settings-capability"); + const prov = config.providers[provider]; + if (!provider || !prov || poolSettingsCapability(provider, prov) !== "generic") { + return jsonResponse({ error: "pool config is only supported for anthropic and generic OAuth providers" }, 400); + } + if (body.stickyLimit !== undefined || body.quotaWindow !== undefined) { + return jsonResponse({ error: "stickyLimit and quotaWindow are not part of the generic pool contract yet" }, 400); + } + const next = { ...(prov.oauthAccountFailover ?? {}) }; + if (body.enabled !== undefined) { + if (typeof body.enabled !== "boolean") return jsonResponse({ error: "enabled must be a boolean" }, 400); + next.enabled = body.enabled; + } + if (body.strategy !== undefined) { + if (body.strategy === null) delete next.strategy; + else { + const parsed = parseGenericPoolStrategy(body.strategy); + if (parsed === null) return jsonResponse({ error: "strategy must be one of: quota, round-robin, fill-first" }, 400); + next.strategy = parsed; + } + } + if (body.autoSwitchThreshold !== undefined) { + if (body.autoSwitchThreshold === null) delete next.autoSwitchThreshold; + else { + const parsed = parseGenericAutoSwitchThreshold(body.autoSwitchThreshold); + if (parsed === null) return jsonResponse({ error: "autoSwitchThreshold must be an integer 0-100" }, 400); + next.autoSwitchThreshold = parsed; + } + } + if (Object.keys(next).length > 0) prov.oauthAccountFailover = next; + else delete prov.oauthAccountFailover; + saveConfigPreservingClaudeCode(config); + return jsonResponse({ ok: true, ...genericPoolSettingsDto(provider, prov) }); + } let enabled = config.anthropicAccountPool?.enabled === true; if (body.enabled !== undefined) { if (typeof body.enabled !== "boolean") return jsonResponse({ error: "enabled must be a boolean" }, 400); diff --git a/src/types/provider.ts b/src/types/provider.ts index f2e7356341..e0154bb61f 100644 --- a/src/types/provider.ts +++ b/src/types/provider.ts @@ -414,6 +414,13 @@ export interface OcxProviderConfig { */ oauthAccountFailover?: { enabled?: boolean; + /** + * Generic OAuth pool selection strategy (#695). Persisted through the pool-settings + * contract; the selector does not consume it yet, so omitted keeps today's behavior. + */ + strategy?: "quota" | "round-robin" | "fill-first"; + /** 0-100 usage percent at which a proactive switch may be considered (#695); inert today. */ + autoSwitchThreshold?: number; }; /** Allow an explicitly key/oauth provider to run without a credential (for keyless local proxies). */ keyOptional?: boolean; diff --git a/tests/account-pool-management-api.test.ts b/tests/account-pool-management-api.test.ts index 0a8c993215..748a5a939b 100644 --- a/tests/account-pool-management-api.test.ts +++ b/tests/account-pool-management-api.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, test } from "bun:test"; import { managementFetch as fetch } from "./helpers/management-auth"; -import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { handleCodexAuthAPI } from "../src/codex/auth-api"; @@ -432,3 +432,68 @@ describe("Anthropic account pool strategy management API", () => { } }); }); + +describe("generic OAuth pool-settings contract (#695)", () => { + let previousHome: string | undefined; + let testDir = ""; + beforeEach(() => { + previousHome = process.env.OPENCODEX_HOME; + testDir = mkdtempSync(join(tmpdir(), "ocx-pool-generic-")); + process.env.OPENCODEX_HOME = testDir; + saveConfig({ + port: 0, + hostname: "127.0.0.1", + defaultProvider: "google-antigravity", + providers: { + "google-antigravity": { adapter: "google", baseUrl: "https://daily-cloudcode-pa.googleapis.com", authMode: "oauth" }, + deepseek: { adapter: "openai-chat", baseUrl: "https://api.deepseek.com/v1", apiKey: "deepseek-key-fixture" }, + }, + } as OcxConfig); + }); + afterEach(() => { + if (previousHome === undefined) delete process.env.OPENCODEX_HOME; + else process.env.OPENCODEX_HOME = previousHome; + if (testDir) rmSync(testDir, { recursive: true, force: true }); + }); + + test("GET/PUT round-trip for a generic OAuth provider; api-key providers and bad values get 400", async () => { + const server = startServer(0); + try { + const absent = await fetch(new URL("/api/oauth/accounts/pool?provider=google-antigravity", server.url)); + expect(absent.status).toBe(200); + expect(await absent.json()).toEqual({ provider: "google-antigravity", kind: "generic", enabled: null, strategy: null, autoSwitchThreshold: null, inert: true }); + + const put = await fetch(new URL("/api/oauth/accounts/pool", server.url), { + method: "PUT", headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ provider: "google-antigravity", strategy: "fill-first", autoSwitchThreshold: 90, enabled: true }), + }); + expect(put.status).toBe(200); + expect(await put.json()).toMatchObject({ ok: true, strategy: "fill-first", autoSwitchThreshold: 90, enabled: true, inert: true }); + const saved = JSON.parse(readFileSync(join(testDir, "config.json"), "utf8")); + expect(saved.providers["google-antigravity"].oauthAccountFailover).toEqual({ enabled: true, strategy: "fill-first", autoSwitchThreshold: 90 }); + + for (const body of [ + { provider: "google-antigravity", strategy: "weighted" }, + { provider: "google-antigravity", autoSwitchThreshold: 101 }, + { provider: "google-antigravity", stickyLimit: 3 }, + { provider: "deepseek", strategy: "quota" }, + ]) { + const bad = await fetch(new URL("/api/oauth/accounts/pool", server.url), { + method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), + }); + expect(bad.status).toBe(400); + } + expect((await fetch(new URL("/api/oauth/accounts/pool?provider=deepseek", server.url))).status).toBe(400); + + const clear = await fetch(new URL("/api/oauth/accounts/pool", server.url), { + method: "PUT", headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ provider: "google-antigravity", strategy: null, autoSwitchThreshold: null }), + }); + expect(clear.status).toBe(200); + expect(await clear.json()).toMatchObject({ strategy: null, autoSwitchThreshold: null, enabled: true }); + } finally { + await server.stop(true); + } + }); +}); + diff --git a/tests/cli-account-pool-verbs.test.ts b/tests/cli-account-pool-verbs.test.ts index c7b80bf2c8..1ebd48f730 100644 --- a/tests/cli-account-pool-verbs.test.ts +++ b/tests/cli-account-pool-verbs.test.ts @@ -291,7 +291,7 @@ describe("ocx account strategy / sticky on the anthropic pool", () => { expect(JSON.parse(out.lines.join("\n"))).toMatchObject({ provider: "openai", strategy: "quota", stickyLimit: 1 }); }); - test("an OAuth provider without a pool config is refused WITHOUT a round-trip", async () => { + test("a provider without an OAuth pool is refused WITHOUT a round-trip", async () => { const calls: Captured[] = []; const out = capture(); let code: number; @@ -308,6 +308,60 @@ describe("ocx account strategy / sticky on the anthropic pool", () => { expect(code).not.toBe(0); // The route would answer 400; spending the request to learn that is the thing avoided. expect(calls).toHaveLength(0); - expect(out.errors.join("\n")).toContain("anthropic"); + expect(out.errors.join("\n")).toContain("pool settings apply to OAuth account pools"); + }); +}); + +describe("generic OAuth pool-settings contract (#695)", () => { + const { cmdAutoSwitch } = require("../src/cli/account-extended") as typeof import("../src/cli/account-extended"); + function genericDeps( + respond: (captured: Captured) => { status?: number; json: unknown }, + calls: Captured[], + providers: Record = { "google-antigravity": { authMode: "oauth" } }, + ): AccountDeps { + return { + baseUrl: "http://127.0.0.1:10100", + loadConfigImpl: () => ({ providers }) as never, + fetchImpl: (async (url: string | URL | Request, init?: RequestInit) => { + const parsed = new URL(String(url)); + const captured: Captured = { + method: init?.method ?? "GET", + path: parsed.pathname + parsed.search, + body: init?.body === undefined ? undefined : JSON.parse(String(init.body)), + }; + calls.push(captured); + const { status = 200, json } = respond(captured); + return new Response(JSON.stringify(json), { status }); + }) as unknown as typeof fetch, + }; + } + + test("strategy on google-antigravity goes to the shared pool route with the provider key", async () => { + const calls: Captured[] = []; + const out = capture(); + try { + await cmdStrategy(["google-antigravity", "round-robin"], genericDeps(() => ({ json: { ok: true, strategy: "round-robin", stickyLimit: null } }), calls)); + } finally { out.restore(); } + expect(calls[0]).toMatchObject({ method: "PUT", path: "/api/oauth/accounts/pool", body: { provider: "google-antigravity", strategy: "round-robin" } }); + }); + + test("auto-switch on a generic provider writes autoSwitchThreshold through the pool route", async () => { + const calls: Captured[] = []; + const out = capture(); + try { + expect(await cmdAutoSwitch(["google-antigravity", "threshold", "90"], genericDeps(() => ({ json: { ok: true, autoSwitchThreshold: 90 } }), calls))).toBe(0); + } finally { out.restore(); } + expect(calls[0]).toMatchObject({ method: "PUT", path: "/api/oauth/accounts/pool", body: { provider: "google-antigravity", autoSwitchThreshold: 90 } }); + expect(out.lines.join("\n")).toContain("threshold 90%"); + }); + + test("api-key providers are still refused before any request", async () => { + const calls: Captured[] = []; + const out = capture(); + try { + expect(await cmdStrategy(["deepseek", "quota"], genericDeps(() => ({ json: {} }), calls, { deepseek: { apiKey: "x" } }))).not.toBe(0); + } finally { out.restore(); } + expect(calls).toHaveLength(0); + expect(out.errors.join("\n")).toContain("API-key provider"); }); }); diff --git a/tests/cli-account.test.ts b/tests/cli-account.test.ts index 7c16eb03e5..58f84a7e94 100644 --- a/tests/cli-account.test.ts +++ b/tests/cli-account.test.ts @@ -792,7 +792,7 @@ describe("ocx account CLI (issue #180 matrix)", () => { const missingProvider = await run(["auto-switch"]); expect(wrongProvider.code).toBe(1); - expect(wrongProvider.stderr).toContain("auto-switch only applies to the openai Codex account pool"); + expect(wrongProvider.stderr).toContain("auto-switch only applies to the openai Codex account pool or a generic OAuth provider pool"); expect(invalidThreshold.code).toBe(1); expect(invalidThreshold.stderr).toContain("integer 0-100"); expect(missingProvider.code).toBe(1); From f84dbf91e4a5cc8efda18698a085ba29055eaf2a Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 05:30:13 +0900 Subject: [PATCH 145/172] fix(ci): declare the provider keychain capability and refresh the Cursor seed count (#3215) Two dev reds from the batch: GET/POST /api/providers/keychain (#3210) landed without a capability entry, tripping route-parity; and #3211 pre-seeded Claude Fable 5.1 under three spellings, so the Cursor umbrella row count is 54, not 51. Co-authored-by: jun --- .../ocx/references/01_management_surface.md | 22 +++++++++++++++++-- src/cli/capabilities.ts | 15 +++++++++++++ tests/cursor-umbrella-rows.test.ts | 5 +++-- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/skills/ocx/references/01_management_surface.md b/skills/ocx/references/01_management_surface.md index 4d94005106..b46de162b5 100644 --- a/skills/ocx/references/01_management_surface.md +++ b/skills/ocx/references/01_management_surface.md @@ -358,6 +358,24 @@ JSON mode: `payload`. - Requires transient authority on stdin; the credential is never persisted or echoed. - A rotation left pending by a crash is resumed here — startup and status stop rather than guess which key generation is live. +### `ocx provider keychain` + +Move a provider's API key into the OS keychain, restore it, or report where it lives. + +| Method | Route | +|---|---| +| GET | `/api/providers/keychain` | +| POST | `/api/providers/keychain` | + +| Flag | Value | Meaning | +|---|---|---| +| `--json` | boolean | Emit the keychain status or result as JSON. | + +JSON mode: `payload`. + +- `store` verifies every keychain write by read-back before config.json is rewritten with keychain: references; an unavailable keychain refuses with 503 and leaves the file untouched. +- Headless services usually have no unlocked keychain session; prefer ${ENV_VAR} references there. + ### `ocx account pause` Stop routing new requests to one account in the Codex pool. @@ -568,6 +586,6 @@ JSON mode: `payload`. ## Counts -- declared capabilities: 31 -- of those, state-changing: 12 +- declared capabilities: 32 +- of those, state-changing: 13 - head-resolved invocations: 2 diff --git a/src/cli/capabilities.ts b/src/cli/capabilities.ts index 789e626293..e60585e5b5 100644 --- a/src/cli/capabilities.ts +++ b/src/cli/capabilities.ts @@ -153,6 +153,21 @@ export const CAPABILITIES: readonly Capability[] = [ json: "envelope", details: ["Reads local config; drives no management API route."], }, + { + command: ["provider", "keychain"], + summary: "Move a provider's API key into the OS keychain, restore it, or report where it lives.", + routes: [ + { method: "GET", path: "/api/providers/keychain" }, + { method: "POST", path: "/api/providers/keychain" }, + ], + flags: [{ name: "--json", value: "boolean", summary: "Emit the keychain status or result as JSON." }], + mutates: true, + json: "payload", + details: [ + "`store` verifies every keychain write by read-back before config.json is rewritten with keychain: references; an unavailable keychain refuses with 503 and leaves the file untouched.", + "Headless services usually have no unlocked keychain session; prefer ${ENV_VAR} references there.", + ], + }, { command: ["account", "list"], summary: "Codex OAuth accounts with pool priority and pause state.", diff --git a/tests/cursor-umbrella-rows.test.ts b/tests/cursor-umbrella-rows.test.ts index c560c376ce..63f2a55f8a 100644 --- a/tests/cursor-umbrella-rows.test.ts +++ b/tests/cursor-umbrella-rows.test.ts @@ -35,9 +35,10 @@ describe("cursor umbrella picker rows (devlog 260828_cursor_umbrella_catalog)", }); test("row count shrank from the 69-row legacy seed", () => { - // 4 router + 47 base rows. Legacy carried 69 (13 thinking + 5 fast + // 4 router + 50 base rows. Legacy carried 69 (13 thinking + 5 fast // duplicates + kimi-k3-1m folded away; quarantined opus-5 base returned). - expect(CURSOR_STATIC_MODELS.length).toBe(51); + // #3211 pre-seeded Claude Fable 5.1 under three spellings (+3). + expect(CURSOR_STATIC_MODELS.length).toBe(54); }); test("umbrella rows and seed efforts agree for every cataloged base", () => { From 6fe46312cd509bbef0e79025181e7ab6fc285681 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 05:48:56 +0900 Subject: [PATCH 146/172] feat(responses): opt-in upstream Responses WebSocket transport (#2816, carry of #2817) (#3216) * fix: expose secure provider websocket option * fix: keep provider websocket management scope focused * docs: clarify provider websocket transport * fix: fail closed on unknown Responses terminal status * docs: document provider websocket paths * fix: preserve upstream websocket on provider overwrite --------- Co-authored-by: jiangpeng Co-authored-by: jun --- .../fr/reference/configuration/providers.md | 1 + .../ja/reference/configuration/providers.md | 1 + .../ko/reference/configuration/providers.md | 1 + .../docs/reference/configuration/providers.md | 1 + .../ru/reference/configuration/providers.md | 1 + .../tr/reference/configuration/providers.md | 1 + .../reference/configuration/providers.md | 1 + .../reference/configuration/providers.md | 1 + src/config.ts | 4 + src/server/management/provider-routes.ts | 17 +++ src/server/responses/fetch-helpers.ts | 3 +- src/server/responses/ws-upstream.ts | 93 +++++++++++- src/types/provider.ts | 12 ++ tests/management-provider-validation.test.ts | 134 +++++++++++++++++- tests/ws-upstream.test.ts | 120 +++++++++++++++- 15 files changed, 381 insertions(+), 10 deletions(-) diff --git a/docs-site/src/content/docs/fr/reference/configuration/providers.md b/docs-site/src/content/docs/fr/reference/configuration/providers.md index ca22b571c3..ed6658c5e3 100644 --- a/docs-site/src/content/docs/fr/reference/configuration/providers.md +++ b/docs-site/src/content/docs/fr/reference/configuration/providers.md @@ -68,6 +68,7 @@ sauvegarde dont le contenu diffère, puis réécrit en identifiants sans préfix | `baseUrl` | `string` | URL de base de l'API en amont. La plupart des points de terminaison fixes intégrés ignorent une valeur incompatible ; les préréglages de clés protégés contre les collisions préservent une ancienne destination personnalisée portant le même nom. | | `requestPacing?` | `{ enabled, requestsPerMinute?, minIntervalMs?, models? }` | Cadencement facultatif du démarrage des requêtes sortantes côté client, distinct de l’utilisation, de la facturation et des indicateurs de limitation en amont. Le nombre de requêtes par minute est converti en intervalle régulier ; `minIntervalMs` peut imposer un intervalle plus long. Les limites du fournisseur s’appliquent à tous ses modèles, tandis que les entrées `models` ciblent les identifiants exacts des modèles en amont, par exemple `nvidia/llama-3.1-nemotron-ultra-253b-v1`, et ne peuvent qu’ajouter du délai. L’attente dans la file ne consomme pas le délai d’expiration des en-têtes de réponse en amont. Les requêtes HTTP, Responses WebSocket et les distributions explicites `fetchResponse`/`runTurn` des adaptateurs sont couvertes. | | `responsesPath?` | `string` | Chemin de ressource relatif pour les requêtes d'authentification par clé `openai-responses`. Il doit commencer par `/` et ne contenir aucun schéma, requête ou fragment. | +| `upstreamWebsocket?` | `boolean` | Active le transport Responses WebSocket en amont pour les requêtes `openai-responses` (désactivé par défaut). Lorsque le service en amont prend en charge ce protocole, les requêtes POST en streaming utilisent le chemin Responses configuré (par défaut `/v1/responses`) via WSS avec une base HTTPS, puis sont reconverties en SSE. Les fournisseurs en mode forward utilisent `{baseUrl}/responses` ; les fournisseurs avec clé utilisent `responsesPath`, ou le repli historique `/v1/responses`. Une base HTTP reste en SSE ; les chemins qui ne sont pas Responses et les requêtes `openai-chat` restent en HTTP. | | `supportsServiceTier?` | `boolean` | Repli à trois états pour la capacité `service_tier`. `true` : le mode rapide peut injecter le champ et les valeurs de l’appelant sont conservées. `false` : le champ est retiré et jamais injecté, et aucune déclaration précise de modèle ne peut le réactiver. Absent : le fournisseur n’est pas classé ; les valeurs de l’appelant sont conservées intactes et le mode rapide n’injecte rien, sauf pour un modèle exact activé. Le registre classe OpenAI canonique comme `true`, et DeepSeek ainsi que Volcengine Ark comme `false`. Ne le définissez explicitement que pour les passerelles personnalisées qui prennent réellement en charge les niveaux. Les routes Chat exigent en plus une autorisation globale ou propre au modèle. | | `modelSupportsServiceTier?` | `Record` | Remplacements de capacité par identifiant exact de modèle en amont. La valeur exacte `true` autorise ce modèle Chat même sans `chatServiceTier` ; `false` restreint les valeurs globales et l’autorisation Chat. Une valeur globale explicite `supportsServiceTier: false` reste fermée et ne peut pas être réactivée. Les modèles non déclarés suivent le comportement global. La requête de gestion `PATCH /api/providers` fusionne les entrées et accepte `null` pour en supprimer une. | | `chatServiceTier?` | `boolean` | Active globalement la sérialisation de `service_tier` sur `/chat/completions`. Des modèles exacts peuvent aussi l’activer avec `modelSupportsServiceTier` ; les modèles non déclarés restent bloqués lorsque ce champ est absent ou faux. | diff --git a/docs-site/src/content/docs/ja/reference/configuration/providers.md b/docs-site/src/content/docs/ja/reference/configuration/providers.md index 6b6b4c2d6e..b837a57017 100644 --- a/docs-site/src/content/docs/ja/reference/configuration/providers.md +++ b/docs-site/src/content/docs/ja/reference/configuration/providers.md @@ -58,6 +58,7 @@ account を削除しても mapping は保持され、同じ id を再追加す | `baseUrl` | `string` |アップストリーム API のベース URL。ほとんどの組み込み固定エンドポイントは不一致を無視します。衝突安全キー プリセットは、古い同じ名前のカスタム宛先を保持します。 | | `requestPacing?` | `{ enabled, requestsPerMinute?, minIntervalMs?, models? }` | 上流の使用量、請求、レート制限表示とは別の、クライアント側の送信開始間隔調整です。プロバイダー制限は全モデルに適用され、`models` は上流の正確なモデル ID に一致し、遅延を増やす場合のみ有効です。キュー待機は応答ヘッダーのタイムアウトを消費しません。HTTP、Responses WebSocket、明示的なアダプターの `fetchResponse`/`runTurn` 送信を対象にします。 | | `responsesPath?` | `string` |キー認証 `openai-responses` リクエストの相対リソース パス。 `/` で始まり、スキーム、クエリ、またはフラグメントが含まれていない必要があります。 | +| `upstreamWebsocket?` | `boolean` | `openai-responses` リクエストで使用するアップストリーム Responses WebSocket トランスポート(既定値は無効)。アップストリームがこのプロトコルに対応している場合、ストリーミング POST は設定済みの Responses パス(既定値 `/v1/responses`)へ HTTPS の WSS で接続し、通常の処理向けに SSE へ再エンコードされます。forward プロバイダーは `{baseUrl}/responses`、キー認証プロバイダーは `responsesPath`(未設定時は従来の `/v1/responses`)を使用します。HTTP のベース URL は SSE のままとなり、Responses 以外のパスと `openai-chat` リクエストは HTTP を使用します。 | | `supportsServiceTier?` | `boolean` | `service_tier` ケイパビリティの 3 状態です。`true`: fast モードが注入でき、呼び出し元の値も保持されます。`false`: フィールドは削除され、注入もされません (非対応と文書化されたアップストリームには送りません)。未設定: 未分類 — 呼び出し元の値はそのまま保持され、fast モードは注入しません。レジストリは正規 OpenAI (`true`)、DeepSeek、Volcengine Ark (`false`) を分類します。実際にティアをサポートするカスタム ゲートウェイにのみ明示的に設定してください。 | | `preserveResponsesReasoningContent?` | `boolean` | リプレイされる Responses reasoning アイテムの平文 reasoning コンテンツを消去せずに保持します (消去は ChatGPT バックエンドのルールです)。DeepSeek のように reasoning リプレイを受け入れるアップストリームで有効にしてください。プロキシ生成の `ocxr1` エンベロープは常に削除されます。 | | `disabled?` | `boolean` |プロバイダーをディスク上に保持しますが、ルーティングおよびモデル/カタログのリストからは除外します。 | diff --git a/docs-site/src/content/docs/ko/reference/configuration/providers.md b/docs-site/src/content/docs/ko/reference/configuration/providers.md index fa530907bc..877085782d 100644 --- a/docs-site/src/content/docs/ko/reference/configuration/providers.md +++ b/docs-site/src/content/docs/ko/reference/configuration/providers.md @@ -58,6 +58,7 @@ managed map을 활성화하면 privacy-safe selector를 만들고, 이후 계정 | `baseUrl` | `string` | 상위 API 기본 URL입니다. 대부분의 내장 고정 엔드포인트는 불일치를 무시합니다. 충돌 안전 키 프리셋은 같은 이름의 이전 사용자 지정 목적지를 보존합니다. | | `requestPacing?` | `{ enabled, requestsPerMinute?, minIntervalMs?, models? }` | 업스트림 사용량, 과금, rate-limit 지표와 별개인 선택적 클라이언트 측 아웃바운드 요청 시작 속도 조절입니다. Provider 제한은 모든 모델에 적용되고 `models` 항목은 정확한 업스트림 모델 ID와 일치하며 지연을 더 늘릴 때만 적용됩니다. 큐 대기는 응답 헤더 타임아웃을 소모하지 않습니다. HTTP, Responses WebSocket, 명시적 어댑터 `fetchResponse`/`runTurn` 전송을 포함합니다. | | `responsesPath?` | `string` | 키 인증 `openai-responses` 요청의 상대 리소스 경로입니다. 반드시 `/`로 시작해야 하며 스킴, query, fragment를 포함하면 안 됩니다. | +| `upstreamWebsocket?` | `boolean` | `openai-responses` 요청에 대한 업스트림 Responses WebSocket 전송을 선택적으로 활성화합니다(기본값 `false`). 업스트림이 이 프로토콜을 지원하면 스트리밍 POST가 설정된 Responses 경로(기본값 `/v1/responses`)로 HTTPS 기반 WSS를 사용하고, 일반 파이프라인을 위해 SSE로 다시 인코딩됩니다. forward 공급자는 `{baseUrl}/responses`를 사용하고, key-auth 공급자는 `responsesPath`를 사용하며 미설정 시 기존 `/v1/responses`로 대체됩니다. HTTP 기본 URL은 SSE를 유지하고, Responses가 아닌 경로와 `openai-chat` 요청은 HTTP를 사용합니다. | | `supportsServiceTier?` | `boolean` | `service_tier` 케이퍼빌리티 3상태입니다. `true`: fast 모드가 주입할 수 있고 호출자 값도 보존합니다. `false`: 필드를 제거하고 절대 주입하지 않습니다(미지원으로 문서화된 업스트림에는 볼 수 없습니다). 미설정: 미분류 — 호출자가 준 값은 그대로 보존하고 fast 모드는 주입하지 않습니다. 레지스트리는 정식 OpenAI(`true`), DeepSeek, Volcengine Ark(`false`)를 분류하며, 실제로 티어를 지원하는 커스텀 게이트웨이에만 명시적으로 설정하세요. | | `preserveResponsesReasoningContent?` | `boolean` | 리플레이되는 Responses reasoning 항목의 평문 reasoning 내용을 지우지 않고 유지합니다(지우는 것은 ChatGPT 백엔드 규칙입니다). DeepSeek처럼 reasoning 리플레이를 허용하는 업스트림에 켜세요. 프록시가 만든 `ocxr1` 봉투는 항상 제거됩니다. | | `disabled?` | `boolean` | 공급자를 디스크에는 남기되, 라우팅과 모델/카탈로그 목록에서는 제외합니다. | diff --git a/docs-site/src/content/docs/reference/configuration/providers.md b/docs-site/src/content/docs/reference/configuration/providers.md index 1c75d4d8b0..8dd1376b87 100644 --- a/docs-site/src/content/docs/reference/configuration/providers.md +++ b/docs-site/src/content/docs/reference/configuration/providers.md @@ -70,6 +70,7 @@ differing backup and rewrites known legacy namespaced selected ids to bare ids. | `requestPacing?` | `{ enabled, requestsPerMinute?, minIntervalMs?, models? }` | Optional client-side outbound request-start pacing, separate from upstream usage, billing, and rate-limit indicators. RPM is converted to an even interval; `minIntervalMs` may impose a longer interval. Provider limits apply across all models, while `models` entries use exact upstream model IDs (for example `nvidia/llama-3.1-nemotron-ultra-253b-v1`) and can only add delay. Queue waits do not consume the upstream response-header timeout. HTTP, Responses WebSocket, and explicit adapter `fetchResponse`/`runTurn` dispatches are covered. | | `upstreamHttpVersion?` | `"auto" \| "http1.1" \| "h1" \| "http2" \| "h2"` | Pin the HTTP version used for upstream requests to this provider. Defaults to `auto`, which lets Bun negotiate. An explicit pin requires an HTTPS target and fails locally when it cannot be honored. Set `http1.1` when a provider's HTTP/2 SSE stream stalls instead of delivering events — the symptom is a long-running streaming request that produces nothing and eventually times out. For Cursor, `http1.1`/`h1` selects its `RunSSE` + `BidiAppend` compatibility transport for inference and also pins live model discovery. Management `POST`/`PATCH` accept `null` to clear it back to `auto`. | | `responsesPath?` | `string` | Relative resource path for key-auth `openai-responses` requests. It must start with `/` and contain no scheme, query, or fragment. | +| `upstreamWebsocket?` | `boolean` | Opt-in upstream Responses WebSocket transport for `openai-responses` requests (default false). When the upstream supports the Responses WebSocket protocol, streaming POST requests to the configured Responses path (default `/v1/responses`) are dialed as WSS over an HTTPS base URL and re-encoded to SSE for the usual pipeline. Forward providers use `{baseUrl}/responses`; key-auth providers use `responsesPath`, or the legacy `/v1/responses` fallback. This mirrors the canonical ChatGPT backend optimization for OpenAI-compatible gateways (for example sub2api) whose WebSocket ingress is measurably faster than its SSE queue. Plain HTTP remains on SSE; non-Responses paths and `openai-chat` requests stay on HTTP. | | `supportsServiceTier?` | `boolean` | Tri-state canonical Fast capability fallback. `true` publishes Fast in the catalog, satisfies service-tier routing requirements, contributes a supported fingerprint, and lets fast mode inject the provider's canonical wire value on a compatible final adapter. `false` strips the field and never injects, and exact model declarations cannot reopen it. Absent leaves the provider unclassified: fast mode does not inject or normalize a canonical caller value, and caller values obey the final wire's forwarding permission (`chatServiceTier` on Chat; passthrough on Responses). The registry classifies canonical OpenAI (`true`), DeepSeek, and Volcengine Ark (`false`); set it explicitly only for custom gateways that genuinely support tiers. | | `modelSupportsServiceTier?` | `Record` | Exact upstream model capability overrides. Exact `true` enables canonical Fast for that model; exact `false` narrows provider defaults. An explicit provider-level `supportsServiceTier: false` remains fail-closed and cannot be reopened. Exact `true` does not authorize foreign caller-tier forwarding on Chat. Undeclared models fall back to provider-wide behavior. Management `PATCH /api/providers` merges entries and accepts `null` to clear one. | | `chatServiceTier?` | `boolean` | Provider-wide Chat-wire opt-in for forwarding caller `service_tier` values. On a classified route it governs foreign values such as `flex`, not proxy-owned canonical Fast after capability validation; on an unclassified route it governs every caller value because no Fast capability has been validated. Exact model capability does not authorize foreign forwarding. Responses routes retain their capability-based caller forwarding behavior. | diff --git a/docs-site/src/content/docs/ru/reference/configuration/providers.md b/docs-site/src/content/docs/ru/reference/configuration/providers.md index 8e0956d18e..ce41f34940 100644 --- a/docs-site/src/content/docs/ru/reference/configuration/providers.md +++ b/docs-site/src/content/docs/ru/reference/configuration/providers.md @@ -71,6 +71,7 @@ cross-route credential fallback не существует. Строки API GPT- | `baseUrl` | `string` | Базовый URL API upstream'а. Большинство built-in fixed-endpoint'ов игнорируют несовпадение; collision-safe key-preset'ы сохраняют старый custom destination с тем же именем. | | `requestPacing?` | `{ enabled, requestsPerMinute?, minIntervalMs?, models? }` | Опциональное клиентское выравнивание начала исходящих запросов, отдельное от учёта использования, биллинга и индикаторов rate limit апстрима. Лимит провайдера действует на все модели, а `models` сопоставляется с точными ID моделей апстрима и может только увеличить задержку. Ожидание очереди не расходует таймаут заголовков ответа. Поддерживаются HTTP, Responses WebSocket и явные вызовы адаптеров `fetchResponse`/`runTurn`. | | `responsesPath?` | `string` | Relative resource path для key-auth запросов `openai-responses`. Должен начинаться с `/` и не может содержать scheme, query или fragment. | +| `upstreamWebsocket?` | `boolean` | Необязательный upstream Responses WebSocket для запросов `openai-responses` (по умолчанию `false`). Если upstream поддерживает этот протокол, потоковые POST-запросы используют настроенный путь Responses (по умолчанию `/v1/responses`), подключаются по WSS через HTTPS и перекодируются обратно в SSE для обычного конвейера. Провайдеры в режиме forward используют `{baseUrl}/responses`; провайдеры с ключом используют `responsesPath` или исторический fallback `/v1/responses`. Для HTTP остаётся SSE; пути, не относящиеся к Responses, и запросы `openai-chat` остаются на HTTP. | | `supportsServiceTier?` | `boolean` | Три состояния поддержки `service_tier`. `true`: fast mode может подставлять поле, значения вызывающего сохраняются. `false`: поле удаляется и никогда не подставляется (апстрим, для которого задокументировано отсутствие поддержки, не должен его получать). Не задано: провайдер не классифицирован — значения вызывающего сохраняются без изменений, fast mode не подставляет. Registry классифицирует canonical OpenAI (`true`), DeepSeek и Volcengine Ark (`false`); задавайте явно только для custom gateway'ев, реально поддерживающих tier'ы. | | `preserveResponsesReasoningContent?` | `boolean` | Сохранять plaintext reasoning content в replay'нутых Responses reasoning item'ах вместо очистки (очистка — правило ChatGPT backend'а). Включайте для upstream'ов, чей контракт принимает reasoning replay, например DeepSeek. Proxy-minted `ocxr1` envelope'ы удаляются всегда. | | `disabled?` | `boolean` | Сохранить провайдера на диске, но исключить его из routing'а и из model/catalog-listing'ов. | diff --git a/docs-site/src/content/docs/tr/reference/configuration/providers.md b/docs-site/src/content/docs/tr/reference/configuration/providers.md index f12229afb9..b84e783c22 100644 --- a/docs-site/src/content/docs/tr/reference/configuration/providers.md +++ b/docs-site/src/content/docs/tr/reference/configuration/providers.md @@ -77,6 +77,7 @@ alanlı seçilmiş kimlikleri yalın kimliklere yeniden yazar. | `adapter` | `string` | `openai-chat`, `openai-responses`, `anthropic`, `google`, `kiro`, `cursor`, `ollama-native`, `azure-openai` (veya takma ad `azure`) seçeneklerinden biri. | | `baseUrl` | `string` | Yukarı akış API temel URL'si. Çoğu yerleşik sabit uç nokta uyumsuzluğu yok sayar; çakışma güvenli anahtar önayarları aynı adlı daha eski özel bir hedefi korur. | | `responsesPath?` | `string` | Anahtar kimlik doğrulamalı `openai-responses` istekleri için göreli kaynak yolu. `/` ile başlamalı ve şema, sorgu veya parça içermemelidir. | +| `upstreamWebsocket?` | `boolean` | `openai-responses` istekleri için isteğe bağlı upstream Responses WebSocket aktarımıdır (varsayılan `false`). Upstream bu protokolü desteklediğinde, akışlı POST istekleri yapılandırılmış Responses yolunu (varsayılan `/v1/responses`) HTTPS tabanında WSS ile kullanır ve normal işlem hattı için SSE'ye yeniden kodlanır. Forward sağlayıcılar `{baseUrl}/responses`, anahtar kimlik doğrulamalı sağlayıcılar `responsesPath` veya eski `/v1/responses` geri dönüşünü kullanır. Düz HTTP SSE olarak kalır; Responses dışı yollar ve `openai-chat` istekleri HTTP'de kalır. | | `supportsServiceTier?` | `boolean` | Üç durumlu `service_tier` yeteneği. `true`: hızlı mod enjekte edebilir ve arayan değerleri korunur. `false`: alan kaldırılır ve asla enjekte edilmez (desteklemediği belgelenen yukarı akış bunu almamalıdır). Yok: sağlayıcı sınıflandırılmamıştır — arayan tarafından sağlanan değerler dokunulmadan korunur ve hızlı mod asla enjekte etmez. Kayıt defteri kurallı OpenAI'yi (`true`), DeepSeek'i ve Volcengine Ark'ı (`false`) sınıflandırır; bunu yalnızca katmanları gerçekten destekleyen özel ağ geçitleri için açıkça ayarlayın. | | `preserveResponsesReasoningContent?` | `boolean` | Düz metin akıl yürütme içeriğini boşaltmak yerine (boşaltma ChatGPT arka ucunun kuralıdır) tekrarlanan Responses akıl yürütme öğelerinde tutun. DeepSeek gibi sözleşmesi akıl yürütme tekrarını kabul eden yukarı akışlar için etkinleştirin. Proxy tarafından basılan `ocxr1` zarfları her zaman kaldırılır. | | `disabled?` | `boolean` | Sağlayıcıyı diskte tutun ancak yönlendirmeden ve model/katalog listelerinden hariç tutun. | diff --git a/docs-site/src/content/docs/zh-cn/reference/configuration/providers.md b/docs-site/src/content/docs/zh-cn/reference/configuration/providers.md index 2a41c6a3b1..d3cc8d9c14 100644 --- a/docs-site/src/content/docs/zh-cn/reference/configuration/providers.md +++ b/docs-site/src/content/docs/zh-cn/reference/configuration/providers.md @@ -58,6 +58,7 @@ selector,而不是分配一个新名称。 | `baseUrl` | `string` | 上游 API 基础 URL。大多数内置固定端点会忽略不匹配的值;具备冲突安全键的预设会保留一个更早、同名的自定义目标。 | | `requestPacing?` | `{ enabled, requestsPerMinute?, minIntervalMs?, models? }` | 可选的客户端出站请求启动节流,与上游用量、计费和限流指标相互独立。提供商限制适用于所有模型,`models` 按上游模型精确 ID 匹配且只能增加延迟。排队等待不计入响应头超时。覆盖 HTTP、Responses WebSocket 以及显式适配器 `fetchResponse`/`runTurn` 调用。 | | `responsesPath?` | `string` | 用于 key-auth `openai-responses` 请求的相对资源路径。必须以 `/` 开头,且不能包含 scheme、query 或 fragment。 | +| `upstreamWebsocket?` | `boolean` | 为 `openai-responses` 请求选择性启用上游 Responses WebSocket 传输(默认 `false`)。当上游支持该协议时,流式 POST 请求会使用配置的 Responses 路径(默认 `/v1/responses`),通过 HTTPS 基础 URL 以 WSS 连接,并重新编码为常规流程使用的 SSE。forward 提供者使用 `{baseUrl}/responses`;key-auth 提供者使用 `responsesPath`,未设置时回退到传统的 `/v1/responses`。普通 HTTP 仍使用 SSE;非 Responses 路径和 `openai-chat` 请求仍使用 HTTP。 | | `supportsServiceTier?` | `boolean` | `service_tier` 能力的三态。`true`:fast 模式可以注入,调用方提供的值也会被保留。`false`:剥离该字段且绝不注入(已明确不支持的上游不会收到它)。未设置:未分类——调用方提供的值原样保留,fast 模式绝不注入。注册表已对官方 OpenAI(`true`)、DeepSeek 和 Volcengine Ark(`false`)分类;仅对真正支持分层的自定义网关显式设置。 | | `preserveResponsesReasoningContent?` | `boolean` | 在重放的 Responses reasoning 项中保留明文 reasoning 内容,而不是清空(清空是 ChatGPT 后端的规则)。对接受 reasoning 重放的上游(如 DeepSeek)启用。代理生成的 `ocxr1` 信封始终会被剥离。 | | `disabled?` | `boolean` | 将提供者保留在磁盘上,但从路由和模型/目录列表中排除。 | diff --git a/docs-site/src/content/docs/zh-tw/reference/configuration/providers.md b/docs-site/src/content/docs/zh-tw/reference/configuration/providers.md index d15c5a3f31..509d4c88c4 100644 --- a/docs-site/src/content/docs/zh-tw/reference/configuration/providers.md +++ b/docs-site/src/content/docs/zh-tw/reference/configuration/providers.md @@ -42,6 +42,7 @@ description: 供應商項目、認證、端點、模型目錄、配額、context | `baseUrl` | `string` | 上游 API base URL。多數內建固定端點忽略不符;碰撞安全的金鑰預設保留較舊的同名自訂目的地。 | | `requestPacing?` | `{ enabled, requestsPerMinute?, minIntervalMs?, models? }` | 選用的用戶端出站請求啟動節流,與上游用量、計費及限流指標彼此獨立。供應商限制適用於所有模型,`models` 依上游模型精確 ID 比對且只能增加延遲。排隊等待不計入回應標頭逾時。涵蓋 HTTP、Responses WebSocket 及明確的適配器 `fetchResponse`/`runTurn` 呼叫。 | | `responsesPath?` | `string` | Key-auth `openai-responses` 請求的相對資源路徑。必須以 `/` 開頭且不含 scheme、query 或 fragment。 | +| `upstreamWebsocket?` | `boolean` | 為 `openai-responses` 請求選用上游 Responses WebSocket 傳輸(預設 `false`)。當上游支援此協定時,串流 POST 請求會使用設定的 Responses 路徑(預設 `/v1/responses`),透過 HTTPS 基礎 URL 以 WSS 連線,再重新編碼為一般流程使用的 SSE。forward 供應商使用 `{baseUrl}/responses`;key-auth 供應商使用 `responsesPath`,未設定時回退到傳統的 `/v1/responses`。一般 HTTP 仍使用 SSE;非 Responses 路徑與 `openai-chat` 請求仍使用 HTTP。 | | `disabled?` | `boolean` | 將供應商保留在磁碟上但排除於路由與模型/目錄清單。 | | `apiKey?` | `string` | API 金鑰,或在請求時解析的 `${ENV_VAR}` / `$ENV_VAR` 參考。 | | `apiKeyTransport?` | `"x-api-key" \| "bearer"` | Anthropic 金鑰標頭風格。預設為原生 `x-api-key`;僅對 key-auth `anthropic` 供應商有效。 | diff --git a/src/config.ts b/src/config.ts index a0965f9b6d..d9345cac34 100644 --- a/src/config.ts +++ b/src/config.ts @@ -544,6 +544,10 @@ const providerConfigSchema = z.object({ upstreamHttpVersion: z.enum(UPSTREAM_HTTP_VERSION_VALUES) .nullish() .transform(value => value ?? undefined), + // Opt-in upstream Responses WebSocket for OpenAI-compatible providers (e.g. + // aggregators whose WebSocket ingress is measurably faster than SSE). The + // canonical ChatGPT backend WS selection is independent of this flag. + upstreamWebsocket: z.boolean().optional(), directGeminiWireRenames: z.boolean().optional(), noStructuredOutputModels: z.array(z.string().min(1)) .transform(normalizeNonBlankStringArray) diff --git a/src/server/management/provider-routes.ts b/src/server/management/provider-routes.ts index 7376d8addb..2eedb1d5e6 100644 --- a/src/server/management/provider-routes.ts +++ b/src/server/management/provider-routes.ts @@ -299,6 +299,11 @@ function applyProviderPatchFields( } touched = true; } + if (Object.hasOwn(rawBody, "upstreamWebsocket")) { + if (typeof rawBody.upstreamWebsocket !== "boolean") return { error: "upstreamWebsocket must be a boolean" }; + next.upstreamWebsocket = rawBody.upstreamWebsocket; + touched = true; + } // The Models page edits the catalog hints in place; keep them on the existing // provider mutation path so validation, cache invalidation, and convergence stay unified (#1073). if (Object.hasOwn(rawBody, "contextWindow")) { @@ -555,6 +560,7 @@ export async function handleProviderRoutes(ctx: ManagementContext): Promise; + if (rawProvider.upstreamWebsocket !== undefined && typeof rawProvider.upstreamWebsocket !== "boolean") { + return jsonResponse({ error: "upstreamWebsocket must be a boolean" }, 400); + } const serviceTierError = providerServiceTierConfigError(name, transportCandidate); if (serviceTierError) return jsonResponse({ error: serviceTierError }, 400); const prov = stripCodexRuntimeProviderFields(transportCandidate as unknown as OcxProviderConfig); @@ -701,6 +711,7 @@ export async function handleProviderRoutes(ctx: ManagementContext): Promise[0], init?: RequestInit) => { - if (typeof input === "string" && init && shouldUseCodexWsUpstream(input, init, runtime)) { + const upstreamWebsocket = provider.upstreamWebsocket === true; + if (typeof input === "string" && init && shouldUseCodexWsUpstream(input, init, runtime, upstreamWebsocket)) { // The fallback has to be the same HTTP fetch the non-WS branch would have // used, protocol pin included: a WS turn that falls back is serving the // request over HTTP, and dropping the provider's `upstreamHttpVersion` diff --git a/src/server/responses/ws-upstream.ts b/src/server/responses/ws-upstream.ts index 275eff6dfb..38e3642f60 100644 --- a/src/server/responses/ws-upstream.ts +++ b/src/server/responses/ws-upstream.ts @@ -18,6 +18,36 @@ import { compareBunVersions } from "../../lib/bun-stream-caps"; const CODEX_RESPONSES_HTTP_URL = "https://chatgpt.com/backend-api/codex/responses"; const CODEX_RESPONSES_WS_URL = "wss://chatgpt.com/backend-api/codex/responses"; const WS_BETA = "responses_websockets=2026-02-06"; + +/** + * Dial URL for a request URL. The canonical ChatGPT backend keeps its constant; + * an operator-opted OpenAI-compatible upstream swaps https for wss on the same + * path so gateways that serve the Responses WebSocket protocol on their + * /v1/responses path get the same fast lane. Plain HTTP remains on SSE because + * a provider WS handshake would otherwise send credentials and request data + * without transport encryption. + */ +function wsUpstreamUrlFor(httpUrl: string): string { + if (httpUrl === CODEX_RESPONSES_HTTP_URL) return CODEX_RESPONSES_WS_URL; + return httpUrl.replace(/^http(s?):/, "ws$1:"); +} + +/** + * An operator-opted OpenAI-compatible upstream only joins the WS lane for + * Responses endpoints: the WebSocket path speaks the Responses event protocol, + * and every downstream consumer (adapter parsers, usage sniffing, SSE relay) + * assumes that wire. Other paths (chat completions, images, search) stay HTTP. + */ +function isResponsesWebsocketEligibleUrl(url: string): boolean { + let parsed: URL; + try { + parsed = new URL(url); + } catch { + return false; + } + return parsed.protocol === "https:" + && parsed.pathname.endsWith("/responses"); +} // If the 101 never arrives (network black hole), give SSE a chance well before // the caller's connect timeout (default 200s) would fire. const UPGRADE_DEADLINE_MS = 10_000; @@ -102,9 +132,11 @@ export function shouldUseCodexWsUpstream( url: string, init?: RequestInit, runtime: BunRuntimeGateInput = currentBunRuntimeIdentity(), + upstreamWebsocketConfigured = false, ): boolean { if (!bunSupportsBoundedCodexWsRelay(runtime)) return false; - if (url !== CODEX_RESPONSES_HTTP_URL) return false; + if (url !== CODEX_RESPONSES_HTTP_URL && !upstreamWebsocketConfigured) return false; + if (upstreamWebsocketConfigured && !isResponsesWebsocketEligibleUrl(url)) return false; if ((init?.method ?? "GET").toUpperCase() !== "POST") return false; const body = init?.body; if (typeof body !== "string") return false; @@ -123,6 +155,50 @@ export function shouldUseCodexWsUpstream( const CLOSED_BEFORE_TERMINAL = "codex websocket closed before a Responses terminal event"; +type ResponsesWsRelayEvent = { + type: string; + text: string; +}; + +/** + * Responses WebSocket uses `response.done` as its terminal event, while the + * SSE Responses surface uses status-specific terminal events. Normalize the + * WS-only discriminator before relaying so the existing SSE consumers can + * settle the turn and the socket close cannot be mistaken for a drop. Unknown + * or missing status values fail closed instead of being reported as success. + */ +function normalizeResponsesWsRelayEvent(text: string): ResponsesWsRelayEvent | null { + let payload: unknown; + try { + payload = JSON.parse(text); + } catch { + return null; + } + if (!payload || typeof payload !== "object" || Array.isArray(payload)) return null; + const record = payload as Record; + if (typeof record.type !== "string") return null; + if (record.type !== "response.done") return { type: record.type, text }; + + const response = record.response; + const status = response && typeof response === "object" && !Array.isArray(response) + ? (response as Record).status + : undefined; + const type = status === "completed" + ? "response.completed" + : status === "failed" + ? "response.failed" + : status === "incomplete" || status === "cancelled" + ? "response.incomplete" + : "response.failed"; + const normalizedRecord: Record = { ...record, type }; + if (type === "response.failed" && status !== "failed") { + normalizedRecord.response = response && typeof response === "object" && !Array.isArray(response) + ? { ...(response as Record), status: "failed" } + : { status: "failed" }; + } + return { type, text: JSON.stringify(normalizedRecord) }; +} + /** * The close code is the only thing that separates "the backend refused this * payload" from "the network dropped", and both used to reach the caller as the @@ -221,7 +297,7 @@ export function codexWsUpstreamFetch( let ws: WebSocket; try { // Bun accepts per-handshake headers; the DOM lib types only list protocol arrays. - ws = new WebSocket(CODEX_RESPONSES_WS_URL, { headers } as unknown as string[]); + ws = new WebSocket(wsUpstreamUrlFor(url), { headers } as unknown as string[]); } catch { resolve(sseFallback(url, init)); return; @@ -311,14 +387,19 @@ export function codexWsUpstreamFetch( failStream("codex websocket frame exceeds the response size limit"); return; } - const encodedText = encoder.encode(text); + const rawEncodedText = encoder.encode(text); + if (rawEncodedText.byteLength > MAX_CODEX_WS_FRAME_BYTES) { + failStream("codex websocket frame exceeds the response size limit"); + return; + } + const normalized = normalizeResponsesWsRelayEvent(text); + if (!normalized) return; + const { type } = normalized; + const encodedText = normalized.text === text ? rawEncodedText : encoder.encode(normalized.text); if (encodedText.byteLength > MAX_CODEX_WS_FRAME_BYTES) { failStream("codex websocket frame exceeds the response size limit"); return; } - let type: unknown; - try { type = (JSON.parse(text) as { type?: unknown }).type; } catch { return; } - if (typeof type !== "string") return; // Relay only the event surface the SSE path produces today. WS-only // frames (codex.rate_limits, responsesapi.websocket_timing) are dropped // so downstream clients see exactly the stream shape they always got. diff --git a/src/types/provider.ts b/src/types/provider.ts index e0154bb61f..7f24c00611 100644 --- a/src/types/provider.ts +++ b/src/types/provider.ts @@ -277,6 +277,18 @@ export interface OcxProviderConfig { * (current behavior unchanged). Only meaningful for https: base URLs. */ upstreamHttpVersion?: UpstreamHttpVersion; + /** + * Opt-in upstream Responses WebSocket transport for `openai-responses` requests. When true, + * streaming POST turns use the configured Responses path (default `/v1/responses`): forward + * providers use `{baseUrl}/responses`, while key-auth providers use `responsesPath` or the + * legacy `/v1/responses` fallback. HTTPS providers use wss and are re-encoded to SSE; HTTP + * providers continue using SSE, and `openai-chat` requests stay on HTTP. This mirrors the + * canonical ChatGPT backend optimization for any OpenAI-compatible gateway that speaks the + * Responses WebSocket protocol (for example an aggregator like sub2api whose WS ingress is + * measurably faster than its SSE queue). Default false. Canonical ChatGPT backend WS selection + * is independent of this flag. + */ + upstreamWebsocket?: boolean; /** * Google only. When `false`, the AI Studio (direct) path sends Gemini Flash ids * unchanged to the wire instead of applying the `-tiered` suffix (`gemini-3.7-flash` diff --git a/tests/management-provider-validation.test.ts b/tests/management-provider-validation.test.ts index fad06ea7db..c76055d9d8 100644 --- a/tests/management-provider-validation.test.ts +++ b/tests/management-provider-validation.test.ts @@ -4307,7 +4307,7 @@ describe("provider management validation", () => { }); }); -describe("provider upstreamHttpVersion management contract (#1668)", () => { +describe("provider transport option management contract (#1668, #2816)", () => { function makeConfig(): OcxConfig { return { port: 0, @@ -4530,4 +4530,136 @@ describe("provider upstreamHttpVersion management contract (#1668)", () => { upstreamHttpVersion: 42, })).toContain("upstreamHttpVersion"); }); + + test("upstreamWebsocket round-trips through POST, GET, and PATCH", async () => { + if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); + mkdirSync(TEST_DIR, { recursive: true }); + process.env.OPENCODEX_HOME = TEST_DIR; + const liveConfig = makeConfig(); + saveConfig(liveConfig); + await withRequest(liveConfig, async (request) => { + const created = await request("/api/providers", { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ + name: "ws-provider", + provider: { + adapter: "openai-responses", + baseUrl: "https://api.example.test/v1", + upstreamWebsocket: true, + }, + }), + }); + expect(created?.status).toBe(200); + expect(liveConfig.providers["ws-provider"]?.upstreamWebsocket).toBe(true); + expect(loadConfig().providers["ws-provider"]?.upstreamWebsocket).toBe(true); + + const list = await request("/api/providers"); + expect(await list?.json()).toContainEqual(expect.objectContaining({ + name: "ws-provider", + upstreamWebsocket: true, + })); + + const invalid = await request("/api/providers?name=ws-provider", { + method: "PATCH", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ upstreamWebsocket: "true" }), + }); + expect(invalid?.status).toBe(400); + expect(liveConfig.providers["ws-provider"]?.upstreamWebsocket).toBe(true); + + const cleared = await request("/api/providers?name=ws-provider", { + method: "PATCH", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ upstreamWebsocket: false }), + }); + expect(cleared?.status).toBe(200); + expect(liveConfig.providers["ws-provider"]?.upstreamWebsocket).toBe(false); + expect(loadConfig().providers["ws-provider"]?.upstreamWebsocket).toBe(false); + + const invalidPost = await request("/api/providers", { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ + name: "invalid-ws-provider", + provider: { + adapter: "openai-responses", + baseUrl: "https://api.example.test/v1", + upstreamWebsocket: "true", + }, + }), + }); + expect(invalidPost?.status).toBe(400); + expect(liveConfig.providers["invalid-ws-provider"]).toBeUndefined(); + }); + }); + + test("POST overwrite preserves omitted upstreamWebsocket and honors explicit false", async () => { + if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); + mkdirSync(TEST_DIR, { recursive: true }); + process.env.OPENCODEX_HOME = TEST_DIR; + const liveConfig = makeConfig(); + saveConfig(liveConfig); + await withRequest(liveConfig, async (request) => { + const create = await request("/api/providers", { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ + name: "ws-overwrite", + provider: { + adapter: "openai-responses", + baseUrl: "https://api.example.test/v1", + upstreamWebsocket: true, + }, + }), + }); + expect(create?.status).toBe(200); + + const omitted = await request("/api/providers", { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ + name: "ws-overwrite", + provider: { + adapter: "openai-responses", + baseUrl: "https://api.example.test/v1", + }, + }), + }); + expect(omitted?.status).toBe(200); + expect(liveConfig.providers["ws-overwrite"]?.upstreamWebsocket).toBe(true); + expect(loadConfig().providers["ws-overwrite"]?.upstreamWebsocket).toBe(true); + + const explicitFalse = await request("/api/providers", { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ + name: "ws-overwrite", + provider: { + adapter: "openai-responses", + baseUrl: "https://api.example.test/v1", + upstreamWebsocket: false, + }, + }), + }); + expect(explicitFalse?.status).toBe(200); + expect(liveConfig.providers["ws-overwrite"]?.upstreamWebsocket).toBe(false); + expect(loadConfig().providers["ws-overwrite"]?.upstreamWebsocket).toBe(false); + + const omittedAfterDisable = await request("/api/providers", { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ + name: "ws-overwrite", + provider: { + adapter: "openai-responses", + baseUrl: "https://api.example.test/v1", + }, + }), + }); + expect(omittedAfterDisable?.status).toBe(200); + expect(liveConfig.providers["ws-overwrite"]?.upstreamWebsocket).toBe(false); + expect(loadConfig().providers["ws-overwrite"]?.upstreamWebsocket).toBe(false); + }); + }); }); diff --git a/tests/ws-upstream.test.ts b/tests/ws-upstream.test.ts index c16060c9f5..975f9ad215 100644 --- a/tests/ws-upstream.test.ts +++ b/tests/ws-upstream.test.ts @@ -32,8 +32,8 @@ const BOUNDED_WS_RUNTIME = "1.4.0"; // constant that only held before the backfill landed. const EAGER_RELAY_FORCED_BY_PLATFORM = isWin32EagerRewrite(process.platform, true); -function shouldUseCodexWsUpstream(url: string, init?: RequestInit): boolean { - return rawShouldUseCodexWsUpstream(url, init, BOUNDED_WS_RUNTIME); +function shouldUseCodexWsUpstream(url: string, init?: RequestInit, upstreamWebsocket = false): boolean { + return rawShouldUseCodexWsUpstream(url, init, BOUNDED_WS_RUNTIME, upstreamWebsocket); } function codexWsUpstreamFetch( @@ -136,6 +136,27 @@ describe("shouldUseCodexWsUpstream", () => { // Malformed JSON stays on HTTP. expect(shouldUseCodexWsUpstream(CODEX_URL, { method: "POST", body: "{\"stream\":true" })).toBe(false); }); + + test("opt-in upstream WebSocket only for configured OpenAI-compatible Responses endpoints", () => { + // The canonical backend ignores the flag. + expect(shouldUseCodexWsUpstream(CODEX_URL, streamingInit(), false)).toBe(true); + // Configured providers join the WS lane on their own /v1/responses path. + expect(shouldUseCodexWsUpstream("https://sub2api.example.com/v1/responses", streamingInit(), true)).toBe(true); + // Plain HTTP stays on SSE; never send credentials or request data through ws://. + expect(shouldUseCodexWsUpstream("http://10.0.0.5:8080/v1/responses", streamingInit(), true)).toBe(false); + expect(shouldUseCodexWsUpstream("https://sub2api.example.com/v1/responses", streamingInit(), false)).toBe(false); + // Non-Responses paths on a configured provider stay on HTTP. + expect(shouldUseCodexWsUpstream("https://sub2api.example.com/v1/chat/completions", streamingInit(), true)).toBe(false); + expect(shouldUseCodexWsUpstream("https://sub2api.example.com/v1/images", streamingInit(), true)).toBe(false); + expect(shouldUseCodexWsUpstream("https://sub2api.example.com/v1/alpha/search", streamingInit(), true)).toBe(false); + // The usual streaming/body rules still apply to configured providers. + expect(shouldUseCodexWsUpstream("https://sub2api.example.com/v1/responses", { method: "GET" }, true)).toBe(false); + expect(shouldUseCodexWsUpstream("https://sub2api.example.com/v1/responses", { + method: "POST", + body: JSON.stringify({ model: "m" }), + }, true)).toBe(false); + expect(shouldUseCodexWsUpstream("not a url", streamingInit(), true)).toBe(false); + }); }); type Listener = (event: unknown) => void; @@ -241,6 +262,34 @@ describe("providerFetch routing", () => { expect(baseCalls).toHaveLength(3); expect(FakeWebSocket.instances).toHaveLength(1); }); + + test("routes an opt-in provider's Responses streams over its upstream WS", async () => { + installFake(ws => { + ws.emit("open", {}); + ws.emit("message", { data: JSON.stringify({ type: "response.completed", response: { id: "r1" } }) }); + }); + const baseCalls: string[] = []; + const sentinel = new Response("base"); + const provider = { + upstreamWebsocket: true, + fetch: (async (input: unknown) => { + baseCalls.push(String(input)); + return sentinel.clone(); + }) as unknown as typeof fetch, + } as unknown as OcxProviderConfig; + const wrapped = providerFetch(provider, BOUNDED_WS_RUNTIME); + + const wsResponse = await wrapped("https://sub2api.example.com/v1/responses", streamingInit()); + expect(wsResponse.headers.get("content-type")).toContain("text/event-stream"); + expect(baseCalls).toHaveLength(0); + expect(FakeWebSocket.instances).toHaveLength(1); + expect(FakeWebSocket.instances[0]!.url).toBe("wss://sub2api.example.com/v1/responses"); + + // The same provider's non-Responses paths (images/search/chat) stay on the base fetch. + await wrapped("https://sub2api.example.com/v1/images", streamingInit()); + expect(baseCalls).toHaveLength(1); + expect(FakeWebSocket.instances).toHaveLength(1); + }); }); describe("handleResponses Codex WS relay selection", () => { @@ -472,6 +521,56 @@ describe("codexWsUpstreamFetch", () => { expect(FakeWebSocket.instances[0].closed).toBe(true); }); + test("normalizes the Responses WebSocket response.done terminal to SSE", async () => { + installFake(ws => { + ws.emit("open", {}); + ws.emit("message", { + data: JSON.stringify({ + type: "response.done", + response: { id: "r-done", status: "completed", output: [] }, + }), + }); + ws.emit("close", { code: 1000, reason: "normal" }); + }); + const response = await codexWsUpstreamFetch(CODEX_URL, streamingInit(), (() => { + throw new Error("fallback must not run after open"); + }) as unknown as typeof fetch); + + const text = await response.text(); + expect(text).toContain("event: response.completed"); + expect(text).toContain('"type":"response.completed"'); + expect(text).not.toContain("response.done"); + expect(FakeWebSocket.instances[0]!.closed).toBe(true); + }); + + test("fails closed when response.done has no recognized terminal status", async () => { + const cases: Array<{ id: string; status?: string }> = [ + { id: "r-missing" }, + { id: "r-queued", status: "queued" }, + { id: "r-unknown", status: "provider_future_state" }, + ]; + for (const response of cases) { + installFake(ws => { + ws.emit("open", {}); + ws.emit("message", { + data: JSON.stringify({ type: "response.done", response }), + }); + }); + const upstream = await codexWsUpstreamFetch(CODEX_URL, streamingInit(), (() => { + throw new Error("fallback must not run after open"); + }) as unknown as typeof fetch); + + const text = await upstream.text(); + expect(text).toContain("event: response.failed"); + const payload = text + .split("\n") + .filter(line => line.startsWith("data: ") && line !== "data: [DONE]") + .map(line => JSON.parse(line.slice("data: ".length))) + .find(event => event.type === "response.failed"); + expect(payload?.response?.status).toBe("failed"); + } + }); + test("falls back to the HTTP fetch when the upgrade is rejected before open", async () => { installFake(ws => ws.close()); const sentinel = new Response("sse-fallback", { status: 429 }); @@ -836,4 +935,21 @@ describe("oversized Codex create frames", () => { await expect(response.text()).rejects.toThrow("closed before a Responses terminal event (close 1006)"); }); + + test("dials the configured provider's own wss URL for an opt-in upstream", async () => { + installFake(ws => { + ws.emit("open", {}); + ws.emit("message", { data: JSON.stringify({ type: "response.completed", response: { id: "r-ws" } }) }); + }); + const sentinel = new Response("fallback"); + const response = await codexWsUpstreamFetch( + "https://sub2api.example.com/v1/responses", + streamingInit(), + (async () => sentinel) as typeof fetch, + ); + expect(FakeWebSocket.instances).toHaveLength(1); + expect(FakeWebSocket.instances[0]!.url).toBe("wss://sub2api.example.com/v1/responses"); + expect(response.headers.get("content-type")).toContain("text/event-stream"); + expect(await response.text()).toContain("response.completed"); + }); }); From 519bba745d5e9c3cf581107fbed8db48d091ca36 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 05:54:39 +0900 Subject: [PATCH 147/172] feat(codex): opt-in reset-credit auto-redemption before expiry (#822) (#3219) Adds resetCreditAutoRedeem { enabled, leadTimeMinutes } (default off). When enabled the composition root starts a timer that re-reads the main account's reset credits, schedules the soonest-expiring one at expires_at - lead (sleeps capped at 15 min so a suspended laptop re-checks), re-reads once more right before dispatch and skips if the credit is gone, journals the redeem_request_id to disk before the consume call so a crash replays the same idempotent request, and treats an uncertain consume as ambiguous to retry with the same id. Logs carry a hashed account key only. Teardown via the optional shutdown hook; no core files import the module. Co-authored-by: jun --- .../130_wp13_reset_credit_auto_redeem.md | 25 ++ .../131_wp13_audit_r1_synthesis.md | 5 + .../140_wp14_upstream_ws_carry.md | 14 ++ .../141_wp14_audit_r1_synthesis.md | 6 + .../150_wp15_plaintext_v2_disposition.md | 14 ++ .../docs/reference/configuration/server.md | 1 + src/codex/auth-api.ts | 43 ++++ src/codex/reset-credit-auto-redeem.ts | 237 ++++++++++++++++++ src/config.ts | 4 + src/server/index.ts | 12 +- src/types/config.ts | 8 + tests/codex-reset-credit-auto-redeem.test.ts | 159 ++++++++++++ 12 files changed, 527 insertions(+), 1 deletion(-) create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/130_wp13_reset_credit_auto_redeem.md create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/131_wp13_audit_r1_synthesis.md create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/140_wp14_upstream_ws_carry.md create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/141_wp14_audit_r1_synthesis.md create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/150_wp15_plaintext_v2_disposition.md create mode 100644 src/codex/reset-credit-auto-redeem.ts create mode 100644 tests/codex-reset-credit-auto-redeem.test.ts diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/130_wp13_reset_credit_auto_redeem.md b/devlog/_plan/260902_nonbug_adoption_backlog/130_wp13_reset_credit_auto_redeem.md new file mode 100644 index 0000000000..f8f277f0b0 --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/130_wp13_reset_credit_auto_redeem.md @@ -0,0 +1,25 @@ +# wp13 — #822 opt-in reset-credit auto-redemption (slice 1: policy + ledger + tests) + +Investigation (grok subagent Volta). Today: inspect (`GET .../wham/rate-limit-reset-credits`) and +manual consume (`POST .../consume` with a fresh `redeem_request_id` per call) in +`src/codex/auth-api.ts`, CLI `ocx account reset-credits`, dashboard button. An unused #657 ledger +(`reset-credit-operation-ledger.ts`, kinds `recovery|manual`) exists. No auto-redeem config. + +## Slice 1 (this cycle) +- Config: `resetCreditAutoRedeem: { enabled: boolean; leadTimeMinutes?: 1..60 }` (default off; malformed + → disabled with one warning). Types + zod `.catch(undefined)`. +- `src/codex/reset-credit-auto-redeem.ts`: pure policy `planAutoRedeem(now, credits, settings)` → nearest + unused credit with parseable `expires_at` and its due time `expires_at - lead`; identity + `{accountId, grantedAt, expiresAt}`; `shouldDispatch(refreshedCredits, plan)` re-validates the + identity after a fresh inspect. Ledger kind `"auto-redeem"` with one operationId reused as + `redeem_request_id` per identity (crash-safe idempotency). +- Scheduler: `startResetCreditAutoRedeem(config, deps)` registered from `src/server/index.ts` only when + enabled, teardown via `registerOptionalShutdownHook`; timer fire = refresh + re-check, never blind + redeem. Logs hashed account key only. +- Docs row in server.md. No GUI. + +## Acceptance +- Default off: no timer, no import cost on core files (core-lab boundary test green). +- Fake clock + fake WHAM: schedules at expiry-lead; identity change / disable / manual consume first → + skip; dispatch reuses the same redeem_request_id across a simulated restart; success re-reads balance. + diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/131_wp13_audit_r1_synthesis.md b/devlog/_plan/260902_nonbug_adoption_backlog/131_wp13_audit_r1_synthesis.md new file mode 100644 index 0000000000..c156f4d12f --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/131_wp13_audit_r1_synthesis.md @@ -0,0 +1,5 @@ +# wp13 audit r1 — synthesis + +Volta (grok-4.6): expiry-triggered, default-off, generation-keyed identity, fresh inspect before +dispatch, one redeem_request_id per identity in a new ledger kind, activation only from the +composition root. Adopted; scheduler included in slice 1 because policy without a trigger closes nothing. diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/140_wp14_upstream_ws_carry.md b/devlog/_plan/260902_nonbug_adoption_backlog/140_wp14_upstream_ws_carry.md new file mode 100644 index 0000000000..409d2dd9c4 --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/140_wp14_upstream_ws_carry.md @@ -0,0 +1,14 @@ +# wp14 — #2816 + PR #2817 opt-in upstream Responses WebSocket transport (carry) + +Investigation (grok subagent Turing): opt-in `providers..upstreamWebsocket` boolean; hook in +`providerFetch` via `shouldUseCodexWsUpstream`; HTTPS `/responses` only; SSE fallback on any +pre-open failure (426 included); fail-closed `response.done` mapping; no core-lab or startServer +changes; no body/token logging. macOS red on the PR head was the known `server-auth` websocket +passthrough flake; Linux shards green. 139 behind dev, one conflict in provider-routes.ts POST +overwrite block (retainModels/displayNames vs upstreamWebsocket omit-preserve). + +## Decision +Carry by merge in a side worktree (/tmp/ocx-wp14-c94721, branch `codex/carry-2817-upstream-ws`); conflict resolved by +subagent keeping both omit-preserves; tsc/privacy/focused green at `d4914f52d`. Land via new PR, +close #2817 as landed-via-carry, close #2816. + diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/141_wp14_audit_r1_synthesis.md b/devlog/_plan/260902_nonbug_adoption_backlog/141_wp14_audit_r1_synthesis.md new file mode 100644 index 0000000000..3c2dee56c6 --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/141_wp14_audit_r1_synthesis.md @@ -0,0 +1,6 @@ +# wp14 audit r1 — synthesis + +Turing (grok-4.6): carry + fix, not reimplement. Blockers: rebase + resolve provider-routes.ts keeping +both omit-preserves; exact-head CI (macOS server-auth ws flake to be treated as flake). Verdict near-pass. +Merge executed by Aristotle (grok-4.6) in the side worktree: resolved block keeps `existing` early, +samples `submittedUpstreamWebsocket` before enrich, preserves on omit; 153 pass / 1 skip. diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/150_wp15_plaintext_v2_disposition.md b/devlog/_plan/260902_nonbug_adoption_backlog/150_wp15_plaintext_v2_disposition.md new file mode 100644 index 0000000000..fcf3e08930 --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/150_wp15_plaintext_v2_disposition.md @@ -0,0 +1,14 @@ +# wp15 — #2495 + PR #2496 opt-in plaintext V2 collaboration rewrite + +Investigation (grok subagent James): head e4b88af4f, 14 ahead / 4 unique vs dev, +2729 / 18 files, draft, +CHANGES_REQUESTED on an older head, exact-head CI blocked on fork approval; last executed suite red on +two PR-specific assertions (plaintext alias rebuild after quota retry; WS relay rewrite). Depends on +undocumented ChatGPT/Codex behavior (reserved namespace/tool renames to defeat Fernet encryption; +`encrypted_function_args: []` receive path). Core-lab boundary clean; no body logging. + +## Disposition +Close PR #2496 with rationale; keep #2495 open with the reopen conditions. Not merged: protocol rewrite +keyed off undocumented upstream behavior, no exact-head green, reviewer blockers not re-reviewed, and a +smaller slice would not close the issue. Estimated honest merge path 8–12h with a maintainer-owned +rebase and security pass; not this batch. + diff --git a/docs-site/src/content/docs/reference/configuration/server.md b/docs-site/src/content/docs/reference/configuration/server.md index 9d9803b7a1..1a82eb82cd 100644 --- a/docs-site/src/content/docs/reference/configuration/server.md +++ b/docs-site/src/content/docs/reference/configuration/server.md @@ -27,6 +27,7 @@ runs helper features around provider requests. | `codexAutoStart?` | `boolean` | `true` | Let the Codex shim run `ocx ensure` before launching Codex. False makes ensure a no-op. | | `codexShimAutoRestore?` | `boolean` | `true` | Restore an installed shim after a completed external Codex update replaces it. Environment opt-out: `OPENCODEX_CODEX_SHIM_AUTO_RESTORE=0`. | | `codexDesktopAuthless?` | `boolean` | `false` | Opt-in authless Codex Desktop routing on a loopback bind: inject the dedicated `opencodex` provider with `requires_openai_auth = false` so Desktop opens without a ChatGPT login. Ignored on non-loopback binds. `ocx system settings --desktop-authless on`. See [Codex integration](/guides/codex-integration/#authless-codex-desktop-opt-in). | +| `resetCreditAutoRedeem?` | `{ enabled?: boolean; leadTimeMinutes?: number }` | off | Opt-in: redeem the main Codex account's soonest-expiring reset credit `leadTimeMinutes` (1–60, default 10) before it expires. Every attempt re-reads the upstream credit list first and skips when the credit is gone (for example, redeemed by hand); the `redeem_request_id` is journaled in `$OPENCODEX_HOME/reset-credit-auto-redeem.json` before the call so a crash replays the same idempotent request instead of spending a second credit. Logs carry a hashed account key only. | | `syncResumeHistory?` | `boolean` | `true` | Reversible Codex App history compatibility. Original metadata is backed up and restored by `ocx stop` / `ocx restore`. | | `shadowCallIntercept?` | `{ enabled?: boolean; model?: string; sourceModels?: string[] }` | off | Redirect recognized Codex helper/shadow calls to a chosen model while preserving the request's configured reasoning effort. The default source prefix is `gpt-5.6-luna`; older clients through 0.144.x used `gpt-5.4-mini`, which `sourceModels` can restore. | | `webSearchSidecar?` | `OcxWebSearchSidecarConfig` | on when usable | Web-search sidecar options. | diff --git a/src/codex/auth-api.ts b/src/codex/auth-api.ts index 4f9c5efd28..52178b64f6 100644 --- a/src/codex/auth-api.ts +++ b/src/codex/auth-api.ts @@ -389,6 +389,49 @@ function safeResetCreditConsumeDto(input: unknown): { code: string } { return { code: typeof obj.code === "string" ? obj.code : "unknown" }; } +/** + * Background reset-credit access for the auto-redeemer (#822). Goes through the same + * account/lease wrapper as the management routes, but takes a caller-owned + * `redeem_request_id` so a journaled id can be replayed idempotently after a crash. + * Throws on any auth or upstream failure; the caller treats a throw on consume as ambiguous. + */ +export function createResetCreditWhamClient(config: OcxConfig, accountId: string): { + inspect: () => Promise<{ credits: { granted_at: string; expires_at: string }[] }>; + consume: (redeemRequestId: string) => Promise<{ code: string }>; +} { + const run = async (operation: (auth: ResetCreditAuth) => Promise): Promise => { + const result = await withResetCreditAuth(getRuntimeConfig(config), accountId, operation); + if (result.ok) return result.value; + throw new Error(`reset-credit auth unavailable (${result.response.status})`); + }; + return { + inspect: () => run(async auth => { + const resp = await fetch("https://chatgpt.com/backend-api/wham/rate-limit-reset-credits", { + headers: { Authorization: `Bearer ${auth.accessToken}`, "ChatGPT-Account-Id": auth.chatgptAccountId }, + signal: AbortSignal.timeout(8000), + }); + if (!resp.ok) { await resp.body?.cancel().catch(() => {}); throw new Error(`upstream ${resp.status}`); } + const parsed = await readResetCreditJson(resp, AbortSignal.timeout(8000)); + if (!parsed.ok) throw new Error("invalid upstream reset-credit response"); + return { credits: safeResetCreditsDto(parsed.value).credits }; + }), + consume: redeemRequestId => run(async auth => { + const resp = await fetch("https://chatgpt.com/backend-api/wham/rate-limit-reset-credits/consume", { + method: "POST", + headers: { + Authorization: `Bearer ${auth.accessToken}`, + "ChatGPT-Account-Id": auth.chatgptAccountId, + "Content-Type": "application/json", + }, + body: JSON.stringify({ redeem_request_id: redeemRequestId }), + signal: AbortSignal.timeout(10_000), + }); + if (!resp.ok) { await resp.body?.cancel().catch(() => {}); throw new Error(`upstream ${resp.status}`); } + return safeResetCreditConsumeDto(await resp.json()); + }), + }; +} + type ResetCreditJsonRead = | { ok: true; value: unknown } | { ok: false }; diff --git a/src/codex/reset-credit-auto-redeem.ts b/src/codex/reset-credit-auto-redeem.ts new file mode 100644 index 0000000000..19b6d3ae0e --- /dev/null +++ b/src/codex/reset-credit-auto-redeem.ts @@ -0,0 +1,237 @@ +import { createHash, randomUUID } from "node:crypto"; +import { existsSync, readFileSync } from "node:fs"; +import { join } from "node:path"; +import { atomicWriteFile } from "../config/atomic-write"; +import { getConfigDir } from "../config/paths"; +import { registerOptionalShutdownHook } from "../lib/optional-shutdown-hooks"; +import type { OcxConfig } from "../types"; + +/** + * Opt-in auto-redemption of a Codex reset credit shortly before it expires (#822). + * + * Default off. When enabled, the nearest unexpired credit for the main Codex account is + * redeemed `leadTimeMinutes` before its `expires_at`. Every fire re-reads the upstream credit + * list first and dispatches only when the same credit (granted_at + expires_at) is still + * present, so a credit the operator already spent by hand is never redeemed twice. The + * `redeem_request_id` for a credit identity is minted once and journaled to disk before the + * consume call, so a crash between dispatch and settle replays the same idempotent request + * instead of spending a second credit. Logs carry a hashed account key only. + */ + +export interface ResetCreditAutoRedeemSettings { + enabled: boolean; + leadTimeMinutes: number; +} + +export const DEFAULT_LEAD_TIME_MINUTES = 10; +export const MIN_LEAD_TIME_MINUTES = 1; +export const MAX_LEAD_TIME_MINUTES = 60; + +export function resolveResetCreditAutoRedeemSettings(config: Pick): ResetCreditAutoRedeemSettings { + const raw = config.resetCreditAutoRedeem; + if (!raw || raw.enabled !== true) return { enabled: false, leadTimeMinutes: DEFAULT_LEAD_TIME_MINUTES }; + const lead = typeof raw.leadTimeMinutes === "number" && Number.isInteger(raw.leadTimeMinutes) + ? Math.min(Math.max(raw.leadTimeMinutes, MIN_LEAD_TIME_MINUTES), MAX_LEAD_TIME_MINUTES) + : DEFAULT_LEAD_TIME_MINUTES; + return { enabled: true, leadTimeMinutes: lead }; +} + +export interface ResetCredit { + granted_at: string; + expires_at: string; +} + +export interface AutoRedeemPlan { + /** Stable identity of the credit being protected. */ + grantedAt: string; + expiresAt: string; + /** Epoch ms at which the redeem should be attempted. */ + dueAt: number; +} + +/** Pick the credit that expires soonest and is still in the future; null when nothing qualifies. */ +export function planAutoRedeem(now: number, credits: readonly ResetCredit[], settings: ResetCreditAutoRedeemSettings): AutoRedeemPlan | null { + if (!settings.enabled) return null; + let best: AutoRedeemPlan | null = null; + for (const credit of credits) { + const expires = Date.parse(credit.expires_at); + if (!Number.isFinite(expires) || expires <= now) continue; + const dueAt = expires - settings.leadTimeMinutes * 60_000; + if (!best || expires < Date.parse(best.expiresAt)) best = { grantedAt: credit.granted_at, expiresAt: credit.expires_at, dueAt }; + } + return best; +} + +export function creditStillPresent(credits: readonly ResetCredit[], plan: Pick): boolean { + return credits.some(c => c.granted_at === plan.grantedAt && c.expires_at === plan.expiresAt); +} + +interface JournalEntry { + accountKey: string; + grantedAt: string; + expiresAt: string; + redeemRequestId: string; + state: "dispatched" | "settled"; + updatedAt: number; +} + +interface Journal { version: 1; entries: JournalEntry[] } + +export function journalPath(): string { + return join(getConfigDir(), "reset-credit-auto-redeem.json"); +} + +function readJournal(path: string): Journal { + if (!existsSync(path)) return { version: 1, entries: [] }; + try { + const parsed = JSON.parse(readFileSync(path, "utf8")) as Journal; + return parsed && parsed.version === 1 && Array.isArray(parsed.entries) ? parsed : { version: 1, entries: [] }; + } catch { + return { version: 1, entries: [] }; + } +} + +function writeJournal(path: string, journal: Journal): void { + // Keep only entries whose credit could still matter: settled ones older than a week are noise. + const cutoff = Date.now() - 7 * 24 * 60 * 60_000; + journal.entries = journal.entries.filter(e => e.state !== "settled" || e.updatedAt > cutoff); + atomicWriteFile(path, JSON.stringify(journal, null, 2)); +} + +export function hashAccountKey(accountId: string): string { + return createHash("sha256").update(accountId).digest("hex").slice(0, 12); +} + +export interface AutoRedeemDeps { + accountId: string; + settings: () => ResetCreditAutoRedeemSettings; + /** Fresh upstream read of the credit list; throws on auth/transport failure. */ + inspect: () => Promise<{ credits: ResetCredit[] }>; + /** Consume with a caller-owned idempotency key. Returns the upstream code. */ + consume: (redeemRequestId: string) => Promise<{ code: string }>; + now?: () => number; + setTimer?: (fn: () => void, ms: number) => unknown; + clearTimer?: (handle: unknown) => void; + journalFile?: string; + log?: (line: string) => void; + /** Upper bound on one sleep so a laptop sleep or clock jump re-checks rather than trusting a stale plan. */ + maxSleepMs?: number; + /** Interval to re-inspect when no credit is due yet (default 30 min). */ + idleRecheckMs?: number; +} + +export type AutoRedeemOutcome = + | { kind: "disabled" } + | { kind: "nothing-to-protect" } + | { kind: "scheduled"; dueAt: number } + | { kind: "skipped"; reason: "credit-gone" | "disabled-before-dispatch" } + | { kind: "dispatched"; code: string; redeemRequestId: string } + | { kind: "ambiguous"; redeemRequestId: string } + | { kind: "error"; message: string }; + +export interface ResetCreditAutoRedeemer { + /** Inspect, and either dispatch (if due) or schedule the next check. */ + tick(): Promise; + start(): void; + stop(): void; +} + +export function createResetCreditAutoRedeemer(deps: AutoRedeemDeps): ResetCreditAutoRedeemer { + const now = deps.now ?? (() => Date.now()); + const setTimer = deps.setTimer ?? ((fn, ms) => setTimeout(fn, ms)); + const clearTimer = deps.clearTimer ?? (handle => clearTimeout(handle as ReturnType)); + const log = deps.log ?? ((line: string) => console.log(line)); + const path = deps.journalFile ?? journalPath(); + const accountKey = hashAccountKey(deps.accountId); + const maxSleepMs = deps.maxSleepMs ?? 15 * 60_000; + const idleRecheckMs = deps.idleRecheckMs ?? 30 * 60_000; + let handle: unknown = null; + let stopped = false; + let inFlight: Promise | null = null; + + const schedule = (ms: number): void => { + if (stopped) return; + if (handle !== null) clearTimer(handle); + handle = setTimer(() => { handle = null; void tick(); }, Math.max(0, Math.min(ms, maxSleepMs))); + }; + + const dispatch = async (plan: AutoRedeemPlan): Promise => { + const journal = readJournal(path); + let entry = journal.entries.find(e => e.accountKey === accountKey && e.grantedAt === plan.grantedAt && e.expiresAt === plan.expiresAt); + if (entry?.state === "settled") return { kind: "skipped", reason: "credit-gone" }; + if (!entry) { + entry = { accountKey, grantedAt: plan.grantedAt, expiresAt: plan.expiresAt, redeemRequestId: randomUUID(), state: "dispatched", updatedAt: now() }; + journal.entries.push(entry); + // Journal BEFORE the network call: a crash after this line replays the same request id. + writeJournal(path, journal); + } + log(`[opencodex] reset-credit auto-redeem: dispatching for account ${accountKey} (credit expires ${plan.expiresAt})`); + let result: { code: string }; + try { + result = await deps.consume(entry.redeemRequestId); + } catch (error) { + log(`[opencodex] reset-credit auto-redeem: consume uncertain for account ${accountKey}; will retry with the same request id`); + schedule(60_000); + return { kind: "ambiguous", redeemRequestId: entry.redeemRequestId }; + } + entry.state = "settled"; + entry.updatedAt = now(); + writeJournal(path, journal); + log(`[opencodex] reset-credit auto-redeem: upstream answered ${result.code} for account ${accountKey}`); + schedule(idleRecheckMs); + return { kind: "dispatched", code: result.code, redeemRequestId: entry.redeemRequestId }; + }; + + const tick = async (): Promise => { + if (inFlight) return inFlight; + inFlight = (async () => { + const settings = deps.settings(); + if (!settings.enabled) return { kind: "disabled" } as AutoRedeemOutcome; + let credits: ResetCredit[]; + try { + ({ credits } = await deps.inspect()); + } catch (error) { + schedule(idleRecheckMs); + return { kind: "error", message: error instanceof Error ? error.message : "inspect failed" } as AutoRedeemOutcome; + } + const plan = planAutoRedeem(now(), credits, settings); + if (!plan) { schedule(idleRecheckMs); return { kind: "nothing-to-protect" } as AutoRedeemOutcome; } + if (plan.dueAt > now()) { schedule(plan.dueAt - now()); return { kind: "scheduled", dueAt: plan.dueAt } as AutoRedeemOutcome; } + // Due: re-read right before spending. The plan above came from this same inspect, but + // the settings may have flipped and a manual consume may have raced; check both again. + if (!deps.settings().enabled) return { kind: "skipped", reason: "disabled-before-dispatch" } as AutoRedeemOutcome; + let fresh: ResetCredit[]; + try { ({ credits: fresh } = await deps.inspect()); } catch (error) { + schedule(60_000); + return { kind: "error", message: error instanceof Error ? error.message : "inspect failed" } as AutoRedeemOutcome; + } + if (!creditStillPresent(fresh, plan)) { schedule(idleRecheckMs); return { kind: "skipped", reason: "credit-gone" } as AutoRedeemOutcome; } + return dispatch(plan); + })().finally(() => { inFlight = null; }); + return inFlight; + }; + + return { + tick, + start() { stopped = false; void tick(); }, + stop() { stopped = true; if (handle !== null) { clearTimer(handle); handle = null; } }, + }; +} + +/** + * Composition-root activation. Returns the redeemer only when the opt-in is on; the caller + * (src/server/index.ts) must not await this and must gate on `enabled` itself so a default + * install never constructs the timer. + */ +export function activateResetCreditAutoRedeem( + config: OcxConfig, + wham: Pick, +): ResetCreditAutoRedeemer { + const redeemer = createResetCreditAutoRedeemer({ + ...wham, + settings: () => resolveResetCreditAutoRedeemSettings(config), + }); + const unregister = registerOptionalShutdownHook("reset-credit-auto-redeem", () => { redeemer.stop(); unregister(); }); + redeemer.start(); + return redeemer; +} diff --git a/src/config.ts b/src/config.ts index d9345cac34..0641f772eb 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1092,6 +1092,10 @@ const configSchema = z.object({ // Same degrade-not-reject rule: a malformed hand edit hides Spark rather than discarding the // whole config. Hidden is also the default, so `catch(false)` and the default agree. showCodexSparkQuota: z.boolean().optional().catch(false), + resetCreditAutoRedeem: z.object({ + enabled: z.boolean().optional(), + leadTimeMinutes: z.number().int().min(1).max(60).optional(), + }).optional().catch(undefined), // Model ids excluded from the Grok Build managed block (dashboard switches). grokExcludedModels: z.array(z.string()).optional(), // Invalid values degrade to undefined ("auto") instead of failing the whole diff --git a/src/server/index.ts b/src/server/index.ts index 1c4f72bb09..ac59dc2990 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -32,7 +32,8 @@ import { type NativeCodexOwnership, type OwnershipInspection, } from "../integrations/native/ownership-preflight"; -import { registerCodexCooldownRecoveryProbeWorker } from "../codex/auth-api"; +import { createResetCreditWhamClient, registerCodexCooldownRecoveryProbeWorker } from "../codex/auth-api"; +import { activateResetCreditAutoRedeem } from "../codex/reset-credit-auto-redeem"; import { reconcileLiveStateStores, setLiveStateStoreConfig, @@ -2338,5 +2339,14 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server ({ + granted_at: grantedAt, + expires_at: new Date(T0 + expiresInMin * MIN).toISOString(), +}); + +/** Fake clock + manual timer: fire() runs the pending timer at its due time. */ +function harness(opts: { credits: () => ResetCredit[]; enabled?: () => boolean; lead?: number; journalFile: string; consumeCode?: string; consumeThrows?: boolean }) { + let now = T0; + let pending: { fn: () => void; at: number } | null = null; + const consumed: string[] = []; + const logs: string[] = []; + let inspects = 0; + const redeemer = createResetCreditAutoRedeemer({ + accountId: "acct-main", + settings: () => ({ enabled: opts.enabled ? opts.enabled() : true, leadTimeMinutes: opts.lead ?? 10 }), + inspect: async () => { inspects += 1; return { credits: opts.credits() }; }, + consume: async id => { + if (opts.consumeThrows) throw new Error("socket hangup"); + consumed.push(id); + return { code: opts.consumeCode ?? "reset" }; + }, + now: () => now, + setTimer: (fn, ms) => { pending = { fn, at: now + ms }; return 1; }, + clearTimer: () => { pending = null; }, + journalFile: opts.journalFile, + log: line => logs.push(line), + }); + return { + redeemer, consumed, logs, + inspects: () => inspects, + pendingAt: () => pending?.at ?? null, + advanceAndFire: async () => { if (!pending) throw new Error("no timer"); now = pending.at; const fn = pending.fn; pending = null; fn(); await new Promise(r => setTimeout(r, 5)); }, + setNow: (t: number) => { now = t; }, + }; +} + +let dir = ""; +beforeEach(() => { dir = mkdtempSync(join(tmpdir(), "ocx-auto-redeem-")); }); +afterEach(() => { rmSync(dir, { recursive: true, force: true }); }); + +describe("reset-credit auto-redeem settings + plan (#822)", () => { + test("default off; malformed reads as off; lead time clamped", () => { + expect(resolveResetCreditAutoRedeemSettings({}).enabled).toBe(false); + expect(resolveResetCreditAutoRedeemSettings({ resetCreditAutoRedeem: { enabled: false, leadTimeMinutes: 5 } }).enabled).toBe(false); + expect(resolveResetCreditAutoRedeemSettings({ resetCreditAutoRedeem: { enabled: true } })).toEqual({ enabled: true, leadTimeMinutes: 10 }); + expect(resolveResetCreditAutoRedeemSettings({ resetCreditAutoRedeem: { enabled: true, leadTimeMinutes: 500 } }).leadTimeMinutes).toBe(60); + }); + + test("plans the soonest future credit and ignores unparseable or expired ones", () => { + const settings = { enabled: true, leadTimeMinutes: 10 }; + expect(planAutoRedeem(T0, [], settings)).toBeNull(); + expect(planAutoRedeem(T0, [{ granted_at: "x", expires_at: "not a date" }, credit(-5)], settings)).toBeNull(); + const plan = planAutoRedeem(T0, [credit(120), credit(30, "2026-08-31T00:00:00Z"), credit(60)], settings)!; + expect(plan.grantedAt).toBe("2026-08-31T00:00:00Z"); + expect(plan.dueAt).toBe(T0 + 20 * MIN); + expect(planAutoRedeem(T0, [credit(30)], { enabled: false, leadTimeMinutes: 10 })).toBeNull(); + }); +}); + +describe("reset-credit auto-redeemer runtime (#822)", () => { + test("schedules at expiry minus lead, re-reads before dispatch, journals the request id first", async () => { + const journalFile = join(dir, "j.json"); + const h = harness({ credits: () => [credit(30)], journalFile }); + expect(await h.redeemer.tick()).toEqual({ kind: "scheduled", dueAt: T0 + 20 * MIN }); + // Sleeps are capped at 15 min so a laptop sleep re-checks instead of trusting a stale plan. + expect(h.pendingAt()).toBe(T0 + 15 * MIN); + expect(h.consumed).toHaveLength(0); + await h.advanceAndFire(); + expect(h.consumed).toHaveLength(0); + expect(h.pendingAt()).toBe(T0 + 20 * MIN); + await h.advanceAndFire(); + expect(h.consumed).toHaveLength(1); + // initial + intermediate re-check + (plan + pre-dispatch re-read) on the due tick + expect(h.inspects()).toBe(4); + const journal = JSON.parse(readFileSync(journalFile, "utf8")) as { entries: Array<{ redeemRequestId: string; state: string }> }; + expect(journal.entries[0]!.redeemRequestId).toBe(h.consumed[0]!); + expect(journal.entries[0]!.state).toBe("settled"); + expect(h.logs.join("\n")).not.toContain("acct-main"); + }); + + test("a credit redeemed by hand (gone on refresh) is skipped without a consume", async () => { + const journalFile = join(dir, "j.json"); + let list = [credit(30)]; + const h = harness({ credits: () => list, journalFile }); + await h.redeemer.tick(); + list = []; + h.setNow(T0 + 20 * MIN); + // With the credit gone the plan is empty: nothing to protect, and nothing consumed. + expect(await h.redeemer.tick()).toEqual({ kind: "nothing-to-protect" }); + expect(h.consumed).toHaveLength(0); + }); + + test("disabling before dispatch skips; a different credit identity is not redeemed with the old plan", async () => { + const journalFile = join(dir, "j.json"); + let enabled = true; + let list = [credit(30)]; + const h = harness({ credits: () => list, enabled: () => enabled, journalFile }); + await h.redeemer.tick(); + enabled = false; + h.setNow(T0 + 20 * MIN); + expect(await h.redeemer.tick()).toEqual({ kind: "disabled" }); + enabled = true; + // Replaced by a later credit: nothing is due yet, so no consume. + list = [credit(300, "2026-09-02T09:00:00Z")]; + expect((await h.redeemer.tick()).kind).toBe("scheduled"); + expect(h.consumed).toHaveLength(0); + }); + + test("an uncertain consume keeps the same request id across a simulated restart", async () => { + const journalFile = join(dir, "j.json"); + const crashy = harness({ credits: () => [credit(30)], journalFile, consumeThrows: true }); + crashy.setNow(T0 + 20 * MIN); + const first = await crashy.redeemer.tick(); + expect(first.kind).toBe("ambiguous"); + const id = (first as { redeemRequestId: string }).redeemRequestId; + expect(JSON.parse(readFileSync(journalFile, "utf8")).entries[0].state).toBe("dispatched"); + + // New process, same journal: the replay reuses the journaled id and settles it. + const resumed = harness({ credits: () => [credit(30)], journalFile, consumeCode: "already_redeemed" }); + resumed.setNow(T0 + 21 * MIN); + const second = await resumed.redeemer.tick(); + expect(second).toEqual({ kind: "dispatched", code: "already_redeemed", redeemRequestId: id }); + expect(resumed.consumed).toEqual([id]); + + // Settled: a third tick with the credit still listed does not spend again. + expect(await resumed.redeemer.tick()).toEqual({ kind: "skipped", reason: "credit-gone" }); + expect(resumed.consumed).toEqual([id]); + }); + + test("a manual redeem racing between the planning read and the pre-dispatch read is caught", async () => { + const journalFile = join(dir, "j.json"); + let reads = 0; + const h = harness({ credits: () => { reads += 1; return reads === 1 ? [credit(30)] : []; }, journalFile }); + h.setNow(T0 + 20 * MIN); + expect(await h.redeemer.tick()).toEqual({ kind: "skipped", reason: "credit-gone" }); + expect(h.consumed).toHaveLength(0); + }); + + test("stop clears the timer", async () => { + const h = harness({ credits: () => [credit(30)], journalFile: join(dir, "j.json") }); + await h.redeemer.tick(); + expect(h.pendingAt()).not.toBeNull(); + h.redeemer.stop(); + expect(h.pendingAt()).toBeNull(); + }); +}); From 85f7ef92a28f87cd6f3d92e00e0c8e5f21b1700b Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 05:56:57 +0900 Subject: [PATCH 148/172] devlog: wp14 carry record and wp15 disposition (#3221) * devlog: wp14 carry record and wp15 disposition * devlog: record wp15 execution --------- Co-authored-by: jun --- .../150_wp15_plaintext_v2_disposition.md | 3 +++ .../151_wp15_audit_r1_synthesis.md | 6 ++++++ 2 files changed, 9 insertions(+) create mode 100644 devlog/_plan/260902_nonbug_adoption_backlog/151_wp15_audit_r1_synthesis.md diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/150_wp15_plaintext_v2_disposition.md b/devlog/_plan/260902_nonbug_adoption_backlog/150_wp15_plaintext_v2_disposition.md index fcf3e08930..3c0a7af754 100644 --- a/devlog/_plan/260902_nonbug_adoption_backlog/150_wp15_plaintext_v2_disposition.md +++ b/devlog/_plan/260902_nonbug_adoption_backlog/150_wp15_plaintext_v2_disposition.md @@ -12,3 +12,6 @@ keyed off undocumented upstream behavior, no exact-head green, reviewer blockers smaller slice would not close the issue. Estimated honest merge path 8–12h with a maintainer-owned rebase and security pass; not this batch. +## Executed +PR #2496 closed 2026-09-02 with the rationale above; #2495 commented with reopen conditions +(maintainer-owned rebase, exact-head green run, security pass on plaintext retention). diff --git a/devlog/_plan/260902_nonbug_adoption_backlog/151_wp15_audit_r1_synthesis.md b/devlog/_plan/260902_nonbug_adoption_backlog/151_wp15_audit_r1_synthesis.md new file mode 100644 index 0000000000..834db4f717 --- /dev/null +++ b/devlog/_plan/260902_nonbug_adoption_backlog/151_wp15_audit_r1_synthesis.md @@ -0,0 +1,6 @@ +# wp15 audit r1 — synthesis + +James (grok-4.6): PR #2496 head e4b88af4f is a +2729/18-file protocol rewrite keyed off undocumented +ChatGPT/Codex behavior; exact-head CI never ran (fork approval), last executed suite red on two +PR-specific assertions; CHANGES_REQUESTED not re-reviewed; a smaller slice would not close #2495. +Verdict: close the PR with rationale, keep the issue open with reopen conditions. Adopted. From d23eab43aa49e8cd00b0cfaccd58129c6d30430f Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 06:46:28 +0900 Subject: [PATCH 149/172] fix(responses): keep the reserved functions group intact for codex-spark (#3217) (#3224) Codex 0.147+ on Responses Lite ships every ordinary client tool inside the reserved `functions` namespace group, carried in an `additional_tools` input item. stripSparkCompatibility() flattened every namespace group for *codex-spark* models, a rule written when the only groups Codex sent were MCP-style. With the reserved group flattened the ChatGPT backend answers the code-mode call as custom_tool_call { name: "exec", namespace: "exec" }; codex-rs treats only None/""/"functions" as the default namespace, so it concatenates that into the unroutable `execexec` and re-issues the call every turn. Bypassing the proxy sends the group intact and works. Traced on a live dev proxy with a tap on both sides: flattened group -> namespace:"exec" back, turn loops; group intact -> bare `exec` back, pwd runs, turn completes. - stripSparkCompatibility keeps a `functions` group as a group, still filtering its children (tool_search dropped, defer_loading stripped) and admitting `custom` inside it, which is what the direct client sends. MCP-style groups are flattened as before. - Belt to that suspender: scrub a tool-call `namespace` that repeats the call's own `name` on the client-facing passthrough (SSE and bounded JSON). That shape is never a legitimate identity, so no catalog lookup. Fixes #3217. Co-authored-by: jun --- src/adapters/openai-responses.ts | 45 +++++- .../responses-self-named-namespace-scrub.ts | 63 +++++++++ src/server/responses/core.ts | 8 +- tests/openai-responses-passthrough.test.ts | 55 ++++++++ ...sponses-self-named-namespace-scrub.test.ts | 132 ++++++++++++++++++ 5 files changed, 299 insertions(+), 4 deletions(-) create mode 100644 src/server/responses-self-named-namespace-scrub.ts create mode 100644 tests/responses-self-named-namespace-scrub.test.ts diff --git a/src/adapters/openai-responses.ts b/src/adapters/openai-responses.ts index 0209bc63c3..781bbef414 100644 --- a/src/adapters/openai-responses.ts +++ b/src/adapters/openai-responses.ts @@ -467,7 +467,12 @@ function normalizeConfiguredReasoningSummaryDelivery( * namespace, tool_search, web_search, custom) plus extensions (defer_loading, * parallel_tool_calls, tool_search_call/output items). Spark's serving path only * supports flat function tools and hosted web_search. This function: - * - Flattens namespace tools → promotes inner functions to top level + * - Flattens MCP-style namespace tools → promotes inner functions to top level. The reserved + * `functions` group is kept as a group (#3217): Codex 0.147+ sends every ordinary client tool + * inside it on Responses Lite, the backend accepts the group as-is, and flattening it changes + * what the backend answers with — a `custom_tool_call` carrying `namespace: "exec"`, which + * codex-rs concatenates into the unroutable `execexec`. Traced on a live proxy: with the + * group intact the same backend returns the bare `exec` call and the turn completes. * - Drops unsupported tool types (tool_search, custom) * - Strips defer_loading from function tools * - Strips namespace from input items @@ -482,12 +487,39 @@ function stripSparkCompatibility(body: unknown): unknown { let changed = false; const SPARK_SAFE_TOOL_TYPES = new Set(["function", "web_search", "web_search_preview"]); + // Inside the reserved group Codex sends freeform `custom` tools (code-mode `exec`) and the + // backend accepts them there; the top-level "drop custom" rule stays for flattened groups. + const SPARK_SAFE_FUNCTIONS_GROUP_CHILD_TYPES = new Set(["function", "custom"]); + const filterSparkFunctionsGroup = (group: Record): Record | undefined => { + if (!Array.isArray(group.tools)) return undefined; + let groupChanged = false; + const children: unknown[] = []; + for (const child of group.tools) { + if (!isPlainObject(child) || typeof child.type !== "string" || !SPARK_SAFE_FUNCTIONS_GROUP_CHILD_TYPES.has(child.type)) { + groupChanged = true; + continue; + } + if (child.type === "function" && "defer_loading" in child) { + const { defer_loading: _, ...rest } = child; + groupChanged = true; + children.push(rest); + continue; + } + children.push(child); + } + if (children.length === 0) return undefined; + return groupChanged ? { ...group, tools: children } : group; + }; let tools = body.tools; if (Array.isArray(tools)) { const flattened: unknown[] = []; for (const t of tools) { - if (isPlainObject(t) && t.type === "namespace") { + if (isPlainObject(t) && t.type === "namespace" && t.name === SPARK_RESERVED_FUNCTIONS_NAMESPACE) { + const kept = filterSparkFunctionsGroup(t); + if (kept !== t) changed = true; + if (kept) flattened.push(kept); + } else if (isPlainObject(t) && t.type === "namespace") { changed = true; if (Array.isArray(t.tools)) { for (const inner of t.tools) flattened.push(inner); @@ -527,7 +559,11 @@ function stripSparkCompatibility(body: unknown): unknown { const innerTools = item.tools as unknown[]; const filteredInner: unknown[] = []; for (const t of innerTools) { - if (isPlainObject(t) && t.type === "namespace") { + if (isPlainObject(t) && t.type === "namespace" && t.name === SPARK_RESERVED_FUNCTIONS_NAMESPACE) { + const kept = filterSparkFunctionsGroup(t); + if (kept !== t) changed = true; + if (kept) filteredInner.push(kept); + } else if (isPlainObject(t) && t.type === "namespace") { changed = true; if (Array.isArray(t.tools)) { for (const fn of t.tools) filteredInner.push(fn); @@ -574,6 +610,9 @@ function isPlainObject(v: unknown): v is Record { return !!v && typeof v === "object" && !Array.isArray(v); } +/** Codex's reserved client-tool group on Responses Lite; carries no wire prefix. */ +const SPARK_RESERVED_FUNCTIONS_NAMESPACE = "functions"; + /** * Apply the routed provider's real effort ladder to an existing Responses reasoning field. * Native forward requests keep the server-owned native clamp; unknown third-party ladders stay diff --git a/src/server/responses-self-named-namespace-scrub.ts b/src/server/responses-self-named-namespace-scrub.ts new file mode 100644 index 0000000000..82be205f1b --- /dev/null +++ b/src/server/responses-self-named-namespace-scrub.ts @@ -0,0 +1,63 @@ +import type { SsePayloadRewrite } from "./sse-payload-rewrite"; + +function isPlainObject(value: unknown): value is Record { + return !!value && typeof value === "object" && !Array.isArray(value); +} + +/** + * Drop a tool-call `namespace` that merely repeats the call's own `name` (#3217). + * + * codex-rs resolves a client tool call as `ToolName::new(namespace, name)` and treats only + * `None | "" | "functions"` as the default namespace; anything else is concatenated into a flat + * name before routing. A backend answer of `{ name: "exec", namespace: "exec" }` therefore + * becomes `execexec`, which no client tool matches, and Codex re-issues the same call forever. + * That shape is never a legitimate identity — an MCP namespace is a server name, not the tool — + * so it is safe to scrub without consulting the declared catalog. The adapter fix that stops + * provoking the answer lives in `stripSparkCompatibility`; this is the belt to that suspender. + */ +export function scrubSelfNamedToolCallNamespace(value: unknown): { value: unknown; changed: boolean } { + if (Array.isArray(value)) { + let changed = false; + const out = value.map(entry => { + const result = scrubSelfNamedToolCallNamespace(entry); + changed ||= result.changed; + return result.value; + }); + return changed ? { value: out, changed: true } : { value, changed: false }; + } + if (!isPlainObject(value)) return { value, changed: false }; + let changed = false; + const out: Record = {}; + for (const [key, entry] of Object.entries(value)) { + const result = scrubSelfNamedToolCallNamespace(entry); + out[key] = result.value; + changed ||= result.changed; + } + if ( + (value.type === "custom_tool_call" || value.type === "function_call") + && typeof value.name === "string" + && value.name.length > 0 + && value.namespace === value.name + ) { + delete out.namespace; + changed = true; + } + return changed ? { value: out, changed: true } : { value, changed: false }; +} + +export function scrubSelfNamedToolCallNamespaceInJson(text: string): string { + if (!text.includes("\"namespace\"")) return text; + let payload: unknown; + try { + payload = JSON.parse(text); + } catch { + return text; + } + const result = scrubSelfNamedToolCallNamespace(payload); + return result.changed ? JSON.stringify(result.value) : text; +} + +export function createSelfNamedToolCallNamespaceScrubRewrite(): SsePayloadRewrite { + return payload => scrubSelfNamedToolCallNamespaceInJson(payload); +} + diff --git a/src/server/responses/core.ts b/src/server/responses/core.ts index 1e84aab967..daba1cae30 100644 --- a/src/server/responses/core.ts +++ b/src/server/responses/core.ts @@ -326,6 +326,10 @@ import { restoreImageGenCallsInJson, } from "../responses-image-gen-repair"; import { createResponsesModelPayloadRewrite, rewriteResponsesModelJson } from "../responses-model-rewrite"; +import { + createSelfNamedToolCallNamespaceScrubRewrite, + scrubSelfNamedToolCallNamespaceInJson, +} from "../responses-self-named-namespace-scrub"; import type { EffectiveSubagentRoster, SpawnAgentSurface } from "../../codex/catalog"; import { buildToolBridgeMaps, collabSurface, injectDeveloperMessage, multiAgentGuidanceText } from "./collaboration"; @@ -4641,6 +4645,8 @@ async function handleResponsesInner( // Compose opt-in payload rewrites into one parse/stringify pass (image-gen restore first). const payloadRewrites = [ createImageGenCallRestoreRewrite(imageGenCallAliases), + // #3217: a call whose namespace repeats its own name is unroutable in codex-rs. + createSelfNamedToolCallNamespaceScrubRewrite(), routedNamespaceToolAliases.size > 0 ? createRoutedNamespaceCallRestoreRewrite(routedNamespaceToolAliases) : undefined, @@ -4873,7 +4879,7 @@ async function handleResponsesInner( inspectResponseLogJson(logCtx, text); const clientJson = (() => { const restoredNamespace = restoreRoutedNamespaceCallsInJson( - restoreImageGenCallsInJson(text, imageGenCallAliases), + scrubSelfNamedToolCallNamespaceInJson(restoreImageGenCallsInJson(text, imageGenCallAliases)), routedNamespaceToolAliases, ); const restoredAuthorizedBareNamespace = restoreRoutedNamespaceCallsInJson( diff --git a/tests/openai-responses-passthrough.test.ts b/tests/openai-responses-passthrough.test.ts index a1972dc1fb..83cf68fe45 100644 --- a/tests/openai-responses-passthrough.test.ts +++ b/tests/openai-responses-passthrough.test.ts @@ -1717,6 +1717,61 @@ describe("OpenAI Responses passthrough sanitization", () => { }); }); + test("keeps the reserved functions group intact for codex-spark, flattens MCP groups (#3217)", () => { + // Codex 0.147+ on Responses Lite ships every ordinary client tool inside the reserved + // `functions` namespace group, carried in an `additional_tools` input item. Flattening that + // group made the backend answer `custom_tool_call { name: "exec", namespace: "exec" }`, + // which codex-rs concatenates into the unroutable `execexec` and loops on. + const adapter = createResponsesPassthroughAdapter(provider); + const functionsGroup = { + type: "namespace", + name: "functions", + description: "client tools", + tools: [ + { type: "custom", name: "exec", description: "shell" }, + { type: "function", name: "wait", parameters: { type: "object", properties: {} }, defer_loading: true }, + { type: "tool_search", name: "tool_search" }, + ], + }; + const mcpGroup = { + type: "namespace", + name: "mcp__docs", + tools: [{ type: "function", name: "search", parameters: { type: "object", properties: {} } }], + }; + const request = adapter.buildRequest({ + modelId: "gpt-5.3-codex-spark", + context: { messages: [] }, + stream: true, + options: {}, + _rawBody: { + model: "gpt-5.3-codex-spark", + input: [ + { type: "additional_tools", role: "developer", tools: [functionsGroup, mcpGroup] }, + { type: "message", role: "user", content: [{ type: "input_text", text: "run pwd" }] }, + ], + tools: [functionsGroup, mcpGroup], + }, + }, { headers: new Headers({ authorization: "Bearer token" }) }); + const body = JSON.parse(request.body) as { + tools: Array>; + input: Array<{ type: string; tools?: Array> }>; + }; + const expectedGroup = { + type: "namespace", + name: "functions", + description: "client tools", + tools: [ + { type: "custom", name: "exec", description: "shell" }, + { type: "function", name: "wait", parameters: { type: "object", properties: {} } }, + ], + }; + // The reserved group survives as a group with its custom child; tool_search is still dropped + // and defer_loading still stripped inside it. The MCP group is still flattened. + expect(body.tools).toEqual([expectedGroup, { type: "function", name: "search", parameters: { type: "object", properties: {} } }]); + const additional = body.input.find(item => item.type === "additional_tools"); + expect(additional?.tools).toEqual([expectedGroup, { type: "function", name: "search", parameters: { type: "object", properties: {} } }]); + }); + test("strips image_generation hosted tool for codex-spark passthrough", () => { const adapter = createResponsesPassthroughAdapter(provider); const request = adapter.buildRequest({ diff --git a/tests/responses-self-named-namespace-scrub.test.ts b/tests/responses-self-named-namespace-scrub.test.ts new file mode 100644 index 0000000000..b4bd1fcebd --- /dev/null +++ b/tests/responses-self-named-namespace-scrub.test.ts @@ -0,0 +1,132 @@ +/** + * #3217 — a custom_tool_call whose `namespace` repeats its own `name` must not reach Codex. + * + * codex-rs resolves `ToolName::new(namespace, name)` and only treats None/""/"functions" as the + * default namespace; `{ name: "exec", namespace: "exec" }` becomes the flat name `execexec`, + * which no client tool matches, and Codex re-issues the call every turn. The adapter fix keeps + * the reserved `functions` group intact so the backend stops answering that way; this scrub is + * the belt to that suspender on the client-facing passthrough (SSE and bounded JSON). + */ +import { afterEach, expect, test } from "bun:test"; +import { handleResponses } from "../src/server/responses"; +import { scrubSelfNamedToolCallNamespace } from "../src/server/responses-self-named-namespace-scrub"; +import type { OcxConfig } from "../src/types"; + +const originalFetch = globalThis.fetch; +afterEach(() => { globalThis.fetch = originalFetch; }); + +function forwardConfig(): OcxConfig { + return { + port: 0, + defaultProvider: "openai", + providers: { + openai: { + adapter: "openai-responses", + baseUrl: "https://chatgpt.com/backend-api/codex", + authMode: "forward", + codexAccountMode: "direct", + }, + }, + } as unknown as OcxConfig; +} + +const requestBody = { + model: "gpt-5.3-codex-spark", + stream: true, + store: false, + instructions: "x", + input: [ + { + type: "additional_tools", + role: "developer", + tools: [{ + type: "namespace", + name: "functions", + tools: [ + { type: "custom", name: "exec", description: "shell" }, + { type: "function", name: "wait", parameters: { type: "object", properties: {} } }, + ], + }], + }, + { type: "message", role: "user", content: [{ type: "input_text", text: "run pwd" }] }, + ], +}; + +function sseFrom(items: Array>): string { + const events = [ + { type: "response.output_item.added", output_index: 0, item: { ...items[0], input: "", status: "in_progress" } }, + { type: "response.output_item.done", output_index: 0, item: items[0] }, + { type: "response.completed", response: { id: "r1", status: "completed", output: items } }, + ]; + return events.map(e => `event: ${e.type}\ndata: ${JSON.stringify(e)}\n\n`).join(""); +} + +function request(): Request { + return new Request("http://localhost/v1/responses", { + method: "POST", + headers: { "content-type": "application/json", authorization: "Bearer test", "chatgpt-account-id": "acct" }, + body: JSON.stringify(requestBody), + }); +} + +test("a self-named namespace on a passthrough custom_tool_call is scrubbed before the client (#3217)", async () => { + const call = { type: "custom_tool_call", id: "ctc_1", call_id: "call_1", name: "exec", namespace: "exec", input: "pwd", status: "completed" }; + globalThis.fetch = (async () => new Response(sseFrom([call]), { + status: 200, headers: { "content-type": "text/event-stream" }, + })) as typeof fetch; + + const res = await handleResponses(request(), forwardConfig(), { model: "", provider: "" }); + expect(res.status).toBe(200); + const text = await res.text(); + const payloads = text.split("\n").filter(l => l.startsWith("data: ") && l !== "data: [DONE]").map(l => JSON.parse(l.slice(6)) as Record); + const callItems = payloads.flatMap(p => { + const item = p.item as Record | undefined; + const output = (p.response as { output?: Array> } | undefined)?.output ?? []; + return [...(item ? [item] : []), ...output]; + }).filter(i => i.type === "custom_tool_call"); + expect(callItems.length).toBeGreaterThanOrEqual(3); + for (const item of callItems) { + expect(item.name).toBe("exec"); + expect("namespace" in item).toBe(false); + } +}); + +test("a genuine MCP namespace on a passthrough call is left alone", async () => { + const call = { type: "function_call", id: "fc_1", call_id: "call_1", name: "search", namespace: "mcp__docs", arguments: "{}", status: "completed" }; + globalThis.fetch = (async () => new Response(sseFrom([call]), { + status: 200, headers: { "content-type": "text/event-stream" }, + })) as typeof fetch; + + const res = await handleResponses(request(), forwardConfig(), { model: "", provider: "" }); + const text = await res.text(); + expect(text).toContain('"namespace":"mcp__docs"'); +}); + +test("the bounded JSON (stream:false) passthrough path scrubs the same shape (#3217)", async () => { + const call = { type: "custom_tool_call", id: "ctc_1", call_id: "call_1", name: "exec", namespace: "exec", input: "pwd", status: "completed" }; + globalThis.fetch = (async () => new Response(JSON.stringify({ id: "r1", object: "response", status: "completed", output: [call] }), { + status: 200, headers: { "content-type": "application/json" }, + })) as typeof fetch; + + const req = new Request("http://localhost/v1/responses", { + method: "POST", + headers: { "content-type": "application/json", authorization: "Bearer test", "chatgpt-account-id": "acct" }, + body: JSON.stringify({ ...requestBody, stream: false }), + }); + const res = await handleResponses(req, forwardConfig(), { model: "", provider: "" }); + expect(res.status).toBe(200); + const body = await res.json() as { output: Array> }; + expect(body.output[0]).toMatchObject({ type: "custom_tool_call", name: "exec" }); + expect("namespace" in body.output[0]).toBe(false); +}); + +test("scrub is recursive, shape-preserving, and a no-op on clean payloads", () => { + const clean = { type: "response.completed", response: { output: [{ type: "custom_tool_call", name: "exec", input: "" }] } }; + expect(scrubSelfNamedToolCallNamespace(clean)).toEqual({ value: clean, changed: false }); + const dirty = { response: { output: [{ type: "function_call", name: "wait", namespace: "wait", arguments: "{}" }, { type: "message" }] } }; + const result = scrubSelfNamedToolCallNamespace(dirty); + expect(result.changed).toBe(true); + expect(result.value).toEqual({ response: { output: [{ type: "function_call", name: "wait", arguments: "{}" }, { type: "message" }] } }); + // An empty name never matches: a namespace equal to "" is not the self-named shape. + expect(scrubSelfNamedToolCallNamespace({ type: "custom_tool_call", name: "", namespace: "" }).changed).toBe(false); +}); From b732b0d0fe077d095eafbd895363e13128cd540c Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 10:28:04 +0900 Subject: [PATCH 150/172] fix(responses): scope the self-named namespace scrub to declared bare tools (carry of #3226) (#3234) * fix(responses): scope self-named namespace scrub * fix(responses): preserve colliding namespaced functions * fix(responses): honor scrub authorization identity * fix(responses): cover function scrub edge cases * fix(responses): read Chat-shaped function names in the scrub authorization set buildTools accepts the Chat-shaped `{ type: "function", function: { name } }` declaration and the undeclared-tool guard authorizes it, but the scrub's raw-body collector only read `spec.name`. Such a function never entered the raw-body set, the intersection dropped it, and a self-named echo for it reached Codex again. Mirror addWireToolName and read the nested name. Regression: Chat-shaped catalog + upstream `function_call { name: "wait", namespace: "wait" }` is scrubbed; red without this change. --------- Co-authored-by: Alex Jordan <60003097+alex-jordan547@users.noreply.github.com> Co-authored-by: jun --- .../responses-self-named-namespace-scrub.ts | 142 ++++++++++++-- src/server/responses/collaboration.ts | 27 ++- src/server/responses/core.ts | 13 +- ...sponses-self-named-namespace-scrub.test.ts | 185 +++++++++++++++++- 4 files changed, 347 insertions(+), 20 deletions(-) diff --git a/src/server/responses-self-named-namespace-scrub.ts b/src/server/responses-self-named-namespace-scrub.ts index 82be205f1b..b0de5d8348 100644 --- a/src/server/responses-self-named-namespace-scrub.ts +++ b/src/server/responses-self-named-namespace-scrub.ts @@ -4,6 +4,110 @@ function isPlainObject(value: unknown): value is Record { return !!value && typeof value === "object" && !Array.isArray(value); } +export interface SelfNamedNamespaceScrubAuthorization { + customToolCallNames: ReadonlySet; + functionCallNames: ReadonlySet; +} + +function collectBareToolSpecs( + bareCustomNames: Set, + bareFunctionNames: Set, + sameNameNamespacedCustomNames: Set, + sameNameNamespacedFunctionNames: Set, + specs: unknown, +): void { + if (!Array.isArray(specs)) return; + for (const spec of specs) { + if (!isPlainObject(spec)) continue; + if (spec.type === "namespace" && Array.isArray(spec.tools)) { + const namespace = typeof spec.name === "string" ? spec.name : undefined; + for (const inner of spec.tools) { + if (!isPlainObject(inner) || typeof inner.name !== "string") continue; + if (namespace !== "functions" && namespace === inner.name) { + if (inner.type === "custom") sameNameNamespacedCustomNames.add(inner.name); + else if (inner.type === "function") sameNameNamespacedFunctionNames.add(inner.name); + } + if (namespace === "functions") { + if (inner.type === "custom") bareCustomNames.add(inner.name); + else if (inner.type === "function") bareFunctionNames.add(inner.name); + } + } + continue; + } + // `buildTools` (parser.ts) also accepts the Chat-shaped `{ type: "function", function: { name } }` + // declaration, and the undeclared-tool guard authorizes it the same way. Reading only + // `spec.name` here left such a function out of the raw-body set, so the intersection dropped + // it and a self-named echo for it reached Codex again. + const nestedFunction = spec.type === "function" && isPlainObject(spec.function) ? spec.function : undefined; + const name = typeof spec.name === "string" && spec.name.length > 0 + ? spec.name + : typeof nestedFunction?.name === "string" && nestedFunction.name.length > 0 + ? nestedFunction.name + : undefined; + if (!name) continue; + const namespace = typeof spec.namespace === "string" ? spec.namespace : undefined; + if (namespace !== "functions" && namespace === name) { + if (spec.type === "custom") sameNameNamespacedCustomNames.add(name); + else if (spec.type === "function") sameNameNamespacedFunctionNames.add(name); + } + if (!namespace || namespace === "functions") { + if (spec.type === "custom") bareCustomNames.add(name); + else if (spec.type === "function") bareFunctionNames.add(name); + } + } +} + +/** Bare custom tools authorized by this turn, scoped to each response call type. */ +export function collectSelfNamedNamespaceScrubAuthorization( + body: unknown, + authorizedBareCustomToolNames: ReadonlySet, + authorizedBareFunctionToolNames: ReadonlySet, +): SelfNamedNamespaceScrubAuthorization { + const bareCustomNames = new Set(); + const bareFunctionNames = new Set(); + const sameNameNamespacedCustomNames = new Set(); + const sameNameNamespacedFunctionNames = new Set(); + if (isPlainObject(body)) { + collectBareToolSpecs( + bareCustomNames, + bareFunctionNames, + sameNameNamespacedCustomNames, + sameNameNamespacedFunctionNames, + body.tools, + ); + if (Array.isArray(body.input)) { + for (const item of body.input) { + if ( + isPlainObject(item) + && (item.type === "additional_tools" || item.type === "tool_search_output") + ) { + collectBareToolSpecs( + bareCustomNames, + bareFunctionNames, + sameNameNamespacedCustomNames, + sameNameNamespacedFunctionNames, + item.tools, + ); + } + } + } + } + const authorizedCustomNames = [...bareCustomNames] + .filter(name => authorizedBareCustomToolNames.has(name)); + const authorizedFunctionNames = new Set([ + ...authorizedCustomNames, + ...[...bareFunctionNames].filter(name => authorizedBareFunctionToolNames.has(name)), + ]); + return { + customToolCallNames: new Set( + authorizedCustomNames.filter(name => !sameNameNamespacedCustomNames.has(name)), + ), + functionCallNames: new Set( + [...authorizedFunctionNames].filter(name => !sameNameNamespacedFunctionNames.has(name)), + ), + }; +} + /** * Drop a tool-call `namespace` that merely repeats the call's own `name` (#3217). * @@ -11,15 +115,19 @@ function isPlainObject(value: unknown): value is Record { * `None | "" | "functions"` as the default namespace; anything else is concatenated into a flat * name before routing. A backend answer of `{ name: "exec", namespace: "exec" }` therefore * becomes `execexec`, which no client tool matches, and Codex re-issues the same call forever. - * That shape is never a legitimate identity — an MCP namespace is a server name, not the tool — - * so it is safe to scrub without consulting the declared catalog. The adapter fix that stops - * provoking the answer lives in `stripSparkCompatibility`; this is the belt to that suspender. + * The malformed Spark shape is scrubbed only when the current turn authorized a bare custom tool + * with that name. A genuine namespaced tool may intentionally use the same namespace and name. + * The adapter fix that stops provoking the answer lives in `stripSparkCompatibility`; this is the + * belt to that suspender. */ -export function scrubSelfNamedToolCallNamespace(value: unknown): { value: unknown; changed: boolean } { +export function scrubSelfNamedToolCallNamespace( + value: unknown, + authorization: SelfNamedNamespaceScrubAuthorization, +): { value: unknown; changed: boolean } { if (Array.isArray(value)) { let changed = false; const out = value.map(entry => { - const result = scrubSelfNamedToolCallNamespace(entry); + const result = scrubSelfNamedToolCallNamespace(entry, authorization); changed ||= result.changed; return result.value; }); @@ -29,15 +137,21 @@ export function scrubSelfNamedToolCallNamespace(value: unknown): { value: unknow let changed = false; const out: Record = {}; for (const [key, entry] of Object.entries(value)) { - const result = scrubSelfNamedToolCallNamespace(entry); + const result = scrubSelfNamedToolCallNamespace(entry, authorization); out[key] = result.value; changed ||= result.changed; } + const authorizedNames = value.type === "custom_tool_call" + ? authorization.customToolCallNames + : value.type === "function_call" + ? authorization.functionCallNames + : undefined; if ( - (value.type === "custom_tool_call" || value.type === "function_call") + authorizedNames && typeof value.name === "string" && value.name.length > 0 && value.namespace === value.name + && authorizedNames.has(value.name) ) { delete out.namespace; changed = true; @@ -45,7 +159,10 @@ export function scrubSelfNamedToolCallNamespace(value: unknown): { value: unknow return changed ? { value: out, changed: true } : { value, changed: false }; } -export function scrubSelfNamedToolCallNamespaceInJson(text: string): string { +export function scrubSelfNamedToolCallNamespaceInJson( + text: string, + authorization: SelfNamedNamespaceScrubAuthorization, +): string { if (!text.includes("\"namespace\"")) return text; let payload: unknown; try { @@ -53,11 +170,12 @@ export function scrubSelfNamedToolCallNamespaceInJson(text: string): string { } catch { return text; } - const result = scrubSelfNamedToolCallNamespace(payload); + const result = scrubSelfNamedToolCallNamespace(payload, authorization); return result.changed ? JSON.stringify(result.value) : text; } -export function createSelfNamedToolCallNamespaceScrubRewrite(): SsePayloadRewrite { - return payload => scrubSelfNamedToolCallNamespaceInJson(payload); +export function createSelfNamedToolCallNamespaceScrubRewrite( + authorization: SelfNamedNamespaceScrubAuthorization, +): SsePayloadRewrite { + return payload => scrubSelfNamedToolCallNamespaceInJson(payload, authorization); } - diff --git a/src/server/responses/collaboration.ts b/src/server/responses/collaboration.ts index 27ccdbe06a..3dc20accba 100644 --- a/src/server/responses/collaboration.ts +++ b/src/server/responses/collaboration.ts @@ -108,12 +108,16 @@ export function buildToolBridgeMaps(parsed: OcxParsedRequest, budget?: Translato /** Declared parameter schema per request-visible tool name (#1611 integer repair). */ toolParameterSchemas: Map>; freeformToolNames: Set; + bareCustomToolNames: Set; + bareFunctionToolNames: Set; toolSearchToolNames: Set; } { const toolNsMap = new Map(); const declaredToolNames = new Set(); const toolParameterSchemas = new Map>(); const freeformToolNames = new Set(); + const bareCustomToolNames = new Set(); + const bareFunctionToolNames = new Set(); const toolSearchToolNames = new Set(); const requestedTools = parsed.context.tools ?? []; const toolAllowed = toolChoiceToolPredicate(parsed.options.toolChoice, requestedTools); @@ -133,6 +137,19 @@ export function buildToolBridgeMaps(parsed: OcxParsedRequest, budget?: Translato if (t.freeform) { budget?.chargeRetained(new TextEncoder().encode(t.name).byteLength, { kind: "retained_collectors" }); freeformToolNames.add(t.name); + if (!t.namespace || t.namespace === "functions") { + budget?.chargeRetained(new TextEncoder().encode(t.name).byteLength, { kind: "retained_collectors" }); + bareCustomToolNames.add(t.name); + } + } else if ( + !t.toolSearch + && !t.webSearch + && !t.imageGeneration + && !t.videoGeneration + && (!t.namespace || t.namespace === "functions") + ) { + budget?.chargeRetained(new TextEncoder().encode(t.name).byteLength, { kind: "retained_collectors" }); + bareFunctionToolNames.add(t.name); } if (t.toolSearch) { budget?.chargeRetained(new TextEncoder().encode(t.name).byteLength, { kind: "retained_collectors" }); @@ -162,7 +179,15 @@ export function buildToolBridgeMaps(parsed: OcxParsedRequest, budget?: Translato toolParameterSchemas.set(t.name, t.parameters); } } - return { toolNsMap, declaredToolNames, toolParameterSchemas, freeformToolNames, toolSearchToolNames }; + return { + toolNsMap, + declaredToolNames, + toolParameterSchemas, + freeformToolNames, + bareCustomToolNames, + bareFunctionToolNames, + toolSearchToolNames, + }; } diff --git a/src/server/responses/core.ts b/src/server/responses/core.ts index daba1cae30..e2e23f241d 100644 --- a/src/server/responses/core.ts +++ b/src/server/responses/core.ts @@ -327,6 +327,7 @@ import { } from "../responses-image-gen-repair"; import { createResponsesModelPayloadRewrite, rewriteResponsesModelJson } from "../responses-model-rewrite"; import { + collectSelfNamedNamespaceScrubAuthorization, createSelfNamedToolCallNamespaceScrubRewrite, scrubSelfNamedToolCallNamespaceInJson, } from "../responses-self-named-namespace-scrub"; @@ -3661,6 +3662,11 @@ async function handleResponsesInner( parsed._rawBody, replayedInputPrefixLength, ); + const selfNamedNamespaceScrubAuthorization = collectSelfNamedNamespaceScrubAuthorization( + clientToolAuthorizationBody, + toolBridgeMaps.bareCustomToolNames, + toolBridgeMaps.bareFunctionToolNames, + ); const clientExplicitWireToolCatalog = hasExplicitWireToolCatalog(clientToolAuthorizationBody); const clientDeclaredWireToolNames = collectDeclaredWireToolNames(clientToolAuthorizationBody); const clientDeclaredNamelessCallTypes = collectDeclaredNamelessClientCallTypes( @@ -4646,7 +4652,7 @@ async function handleResponsesInner( const payloadRewrites = [ createImageGenCallRestoreRewrite(imageGenCallAliases), // #3217: a call whose namespace repeats its own name is unroutable in codex-rs. - createSelfNamedToolCallNamespaceScrubRewrite(), + createSelfNamedToolCallNamespaceScrubRewrite(selfNamedNamespaceScrubAuthorization), routedNamespaceToolAliases.size > 0 ? createRoutedNamespaceCallRestoreRewrite(routedNamespaceToolAliases) : undefined, @@ -4879,7 +4885,10 @@ async function handleResponsesInner( inspectResponseLogJson(logCtx, text); const clientJson = (() => { const restoredNamespace = restoreRoutedNamespaceCallsInJson( - scrubSelfNamedToolCallNamespaceInJson(restoreImageGenCallsInJson(text, imageGenCallAliases)), + scrubSelfNamedToolCallNamespaceInJson( + restoreImageGenCallsInJson(text, imageGenCallAliases), + selfNamedNamespaceScrubAuthorization, + ), routedNamespaceToolAliases, ); const restoredAuthorizedBareNamespace = restoreRoutedNamespaceCallsInJson( diff --git a/tests/responses-self-named-namespace-scrub.test.ts b/tests/responses-self-named-namespace-scrub.test.ts index b4bd1fcebd..47cf6840f7 100644 --- a/tests/responses-self-named-namespace-scrub.test.ts +++ b/tests/responses-self-named-namespace-scrub.test.ts @@ -61,14 +61,19 @@ function sseFrom(items: Array>): string { return events.map(e => `event: ${e.type}\ndata: ${JSON.stringify(e)}\n\n`).join(""); } -function request(): Request { +function request(body: Record = requestBody): Request { return new Request("http://localhost/v1/responses", { method: "POST", headers: { "content-type": "application/json", authorization: "Bearer test", "chatgpt-account-id": "acct" }, - body: JSON.stringify(requestBody), + body: JSON.stringify(body), }); } +function scrubAuthorization(names: string[]) { + const authorizedNames = new Set(names); + return { customToolCallNames: authorizedNames, functionCallNames: authorizedNames }; +} + test("a self-named namespace on a passthrough custom_tool_call is scrubbed before the client (#3217)", async () => { const call = { type: "custom_tool_call", id: "ctc_1", call_id: "call_1", name: "exec", namespace: "exec", input: "pwd", status: "completed" }; globalThis.fetch = (async () => new Response(sseFrom([call]), { @@ -91,6 +96,176 @@ test("a self-named namespace on a passthrough custom_tool_call is scrubbed befor } }); +test("a self-named namespace declared by the current turn is preserved", async () => { + const call = { type: "custom_tool_call", id: "ctc_1", call_id: "call_1", name: "exec", namespace: "exec", input: "pwd", status: "completed" }; + globalThis.fetch = (async () => new Response(sseFrom([call]), { + status: 200, headers: { "content-type": "text/event-stream" }, + })) as typeof fetch; + const body = { + ...requestBody, + input: [ + { + type: "additional_tools", + role: "developer", + tools: [{ + type: "namespace", + name: "exec", + tools: [{ type: "custom", name: "exec", description: "shell" }], + }], + }, + requestBody.input[1], + ], + }; + + const res = await handleResponses(request(body), forwardConfig(), { model: "", provider: "" }); + expect(res.status).toBe(200); + const text = await res.text(); + expect(text).toContain('\"name\":\"exec\",\"namespace\":\"exec\"'); +}); + +test("the reserved functions namespace does not create a same-name collision", async () => { + const call = { type: "custom_tool_call", id: "ctc_1", call_id: "call_1", name: "functions", namespace: "functions", input: "pwd", status: "completed" }; + globalThis.fetch = (async () => new Response(sseFrom([call]), { + status: 200, headers: { "content-type": "text/event-stream" }, + })) as typeof fetch; + const body = { + ...requestBody, + input: [ + { + type: "additional_tools", + role: "developer", + tools: [{ + type: "namespace", + name: "functions", + tools: [{ type: "custom", name: "functions", description: "shell" }], + }], + }, + requestBody.input[1], + ], + }; + + const res = await handleResponses(request(body), forwardConfig(), { model: "", provider: "" }); + expect(res.status).toBe(200); + const text = await res.text(); + expect(text).not.toContain('\"namespace\":\"functions\"'); +}); + +test("a self-named namespace on a passthrough bare function_call is scrubbed", async () => { + const call = { type: "function_call", id: "fc_1", call_id: "call_1", name: "wait", namespace: "wait", arguments: "{}", status: "completed" }; + globalThis.fetch = (async () => new Response(sseFrom([call]), { + status: 200, headers: { "content-type": "text/event-stream" }, + })) as typeof fetch; + + const res = await handleResponses(request(), forwardConfig(), { model: "", provider: "" }); + expect(res.status).toBe(200); + const text = await res.text(); + expect(text).toContain('\"type\":\"function_call\"'); + expect(text).not.toContain('\"namespace\":\"wait\"'); +}); + +test("a Chat-shaped function declaration still authorizes the bare function scrub", async () => { + // `buildTools` accepts `{ type: "function", function: { name } }` and the undeclared-tool guard + // authorizes it, so the scrub's raw-body collector has to read the nested name too; otherwise + // the intersection drops the tool and a self-named echo for it loops Codex again. + const chatShaped = { + ...requestBody, + input: [ + { type: "message", role: "user", content: [{ type: "input_text", text: "wait" }] }, + ], + tools: [{ type: "function", function: { name: "wait", parameters: { type: "object", properties: {} } } }], + }; + const call = { type: "function_call", id: "fc_1", call_id: "call_1", name: "wait", namespace: "wait", arguments: "{}", status: "completed" }; + globalThis.fetch = (async () => new Response(sseFrom([call]), { + status: 200, headers: { "content-type": "text/event-stream" }, + })) as typeof fetch; + + const res = await handleResponses(request(chatShaped), forwardConfig(), { model: "", provider: "" }); + expect(res.status).toBe(200); + const text = await res.text(); + expect(text).toContain('"type":"function_call"'); + expect(text).not.toContain('"namespace":"wait"'); +}); + +test("tool_choice for a namespaced custom tool cannot authorize a colliding bare scrub", async () => { + const call = { type: "custom_tool_call", id: "ctc_1", call_id: "call_1", name: "exec", namespace: "exec", input: "pwd", status: "completed" }; + globalThis.fetch = (async () => new Response(sseFrom([call]), { + status: 200, headers: { "content-type": "text/event-stream" }, + })) as typeof fetch; + const body = { + ...requestBody, + tool_choice: { type: "custom", name: "remote__exec" }, + input: [ + { + type: "additional_tools", + role: "developer", + tools: [ + { + type: "namespace", + name: "functions", + tools: [{ type: "custom", name: "exec", description: "local shell" }], + }, + { + type: "namespace", + name: "remote", + tools: [{ type: "custom", name: "exec", description: "remote shell" }], + }, + ], + }, + requestBody.input[1], + ], + }; + + const res = await handleResponses(request(body), forwardConfig(), { model: "", provider: "" }); + expect(res.status).toBe(200); + const text = await res.text(); + expect(text).toContain('\"name\":\"exec\",\"namespace\":\"exec\"'); +}); + +test("mixed custom and function collisions are scoped to the response item type", async () => { + const call = { type: "function_call", id: "fc_1", call_id: "call_1", name: "exec", namespace: "exec", arguments: "{}", status: "completed" }; + globalThis.fetch = (async () => new Response(sseFrom([call]), { + status: 200, headers: { "content-type": "text/event-stream" }, + })) as typeof fetch; + const body = { + ...requestBody, + input: [ + { + type: "additional_tools", + role: "developer", + tools: [ + { + type: "namespace", + name: "functions", + tools: [{ type: "custom", name: "exec", description: "shell" }], + }, + { + type: "namespace", + name: "exec", + tools: [{ type: "function", name: "exec", parameters: { type: "object", properties: {} } }], + }, + ], + }, + requestBody.input[1], + ], + }; + + const res = await handleResponses(request(body), forwardConfig(), { model: "", provider: "" }); + expect(res.status).toBe(200); + const text = await res.text(); + expect(text).toContain('\"type\":\"function_call\"'); + expect(text).toContain('\"name\":\"exec\",\"namespace\":\"exec\"'); + + const bareCall = { type: "custom_tool_call", id: "ctc_1", call_id: "call_2", name: "exec", namespace: "exec", input: "pwd", status: "completed" }; + globalThis.fetch = (async () => new Response(sseFrom([bareCall]), { + status: 200, headers: { "content-type": "text/event-stream" }, + })) as typeof fetch; + const bareRes = await handleResponses(request(body), forwardConfig(), { model: "", provider: "" }); + expect(bareRes.status).toBe(200); + const bareText = await bareRes.text(); + expect(bareText).toContain('\"type\":\"custom_tool_call\"'); + expect(bareText).not.toContain('\"namespace\":\"exec\"'); +}); + test("a genuine MCP namespace on a passthrough call is left alone", async () => { const call = { type: "function_call", id: "fc_1", call_id: "call_1", name: "search", namespace: "mcp__docs", arguments: "{}", status: "completed" }; globalThis.fetch = (async () => new Response(sseFrom([call]), { @@ -122,11 +297,11 @@ test("the bounded JSON (stream:false) passthrough path scrubs the same shape (#3 test("scrub is recursive, shape-preserving, and a no-op on clean payloads", () => { const clean = { type: "response.completed", response: { output: [{ type: "custom_tool_call", name: "exec", input: "" }] } }; - expect(scrubSelfNamedToolCallNamespace(clean)).toEqual({ value: clean, changed: false }); + expect(scrubSelfNamedToolCallNamespace(clean, scrubAuthorization(["exec"]))).toEqual({ value: clean, changed: false }); const dirty = { response: { output: [{ type: "function_call", name: "wait", namespace: "wait", arguments: "{}" }, { type: "message" }] } }; - const result = scrubSelfNamedToolCallNamespace(dirty); + const result = scrubSelfNamedToolCallNamespace(dirty, scrubAuthorization(["wait"])); expect(result.changed).toBe(true); expect(result.value).toEqual({ response: { output: [{ type: "function_call", name: "wait", arguments: "{}" }, { type: "message" }] } }); // An empty name never matches: a namespace equal to "" is not the self-named shape. - expect(scrubSelfNamedToolCallNamespace({ type: "custom_tool_call", name: "", namespace: "" }).changed).toBe(false); + expect(scrubSelfNamedToolCallNamespace({ type: "custom_tool_call", name: "", namespace: "" }, scrubAuthorization([])).changed).toBe(false); }); From 261b7e0121d79d2cda42022644e9947203a78a4c Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 10:31:56 +0900 Subject: [PATCH 151/172] fix(cli): stop a sibling start from persisting its port into config.port (#3232) #3188 opened the sibling path: `ocx start --port X` beside a live proxy on the configured port starts a second instance instead of refusing. That instance went through chooseListenPort with X as its hard-pinned preference, so shouldPersistSelectedPort(config.port, X, X) was true and config.port was rewritten to X under the still-running configured-port proxy. Observed: a probe session ran `start --port 10198` a few times against the real home, exited, and left config.port=10198 behind. The next `ocx stop` + `ocx service` read config.port, baked `--port 10198` into the launchd plist, and re-pointed Codex's openai_base_url at 10198 -- the service silently moved off 10100 with no start on 10198 in sight. A sibling is a second instance, not a new home for this config, so it must never persist its port. handleStart now records the sibling decision and passes it into both chooseListenPort call sites (initial pick and the EADDRINUSE re-pick); shouldPersistSelectedPort returns false for it. The ordinary first-start persist and the fallback-port non-persist are unchanged. Co-authored-by: jun --- src/cli/index.ts | 16 ++++++++++++---- src/server/ports.ts | 7 +++++++ tests/cli-dispatch.test.ts | 13 +++++++++++++ tests/ports.test.ts | 9 +++++++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/cli/index.ts b/src/cli/index.ts index 914d803cf5..8259596f34 100755 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -149,7 +149,10 @@ function startArgv(port?: number): string[] { return selfLaunchArgv(args); } -async function chooseListenPort(requestedPort?: number): Promise { +async function chooseListenPort( + requestedPort?: number, + options: { sibling?: boolean } = {}, +): Promise { const config = loadConfig(); const preferred = requestedPort ?? config.port ?? 10100; const hardPin = requestedPort !== undefined && requestedPort > 0; @@ -197,7 +200,7 @@ async function chooseListenPort(requestedPort?: number): Promise { if (preferred > 0 && selected !== preferred) { console.log(`⚠️ Port ${preferred} is busy; starting opencodex on ${selected}.`); } - if (shouldPersistSelectedPort(config.port, selected, preferred)) { + if (shouldPersistSelectedPort(config.port, selected, preferred, options)) { config.port = selected; saveConfig(config); } @@ -253,6 +256,7 @@ async function handleStart(options: { block?: boolean } = {}) { // shutdown then left no runtime record for discovery at all. `handleEnsure` // already passes this; `handleStart` is the path that did not. const owner = await findProxyOwnerBeforeJournalRecovery({ probeConfiguredPort: true }); + let siblingStart = false; if (owner.live) { // Rationale and the full decision table live on `decideStartWithLiveOwner`. const decision = decideStartWithLiveOwner({ @@ -276,6 +280,10 @@ async function handleStart(options: { block?: boolean } = {}) { // Sibling path. Honest about the side effects it shares with any start in this home: // the new instance takes over this home's ocx.pid / runtime-port.json while it runs, // and re-points this home's Codex config at the new port when injection applies. + // What it must NOT do is persist its port into config.port: the configured-port + // proxy is still the owner of this home, and a later `ocx service` reads config.port + // to bake the service (observed: a probe on 10198 left the service pinned there). + siblingStart = true; console.warn( `Proxy already running on port ${owner.live.port}; starting a second instance on requested port ${requestedPort}. ` + `The new instance takes over this home's pid/runtime records and Codex config while it runs.`, @@ -304,7 +312,7 @@ async function handleStart(options: { block?: boolean } = {}) { // Port selection is check-then-bind: a concurrent `ocx start`/`ensure` can win the port // between the probe and Bun.serve. Soft starts may re-pick; hard-pinned `--port` retries // the same port only (never hop — that was the remaining PR #152 gap). - let port = await chooseListenPort(requestedPort); + let port = await chooseListenPort(requestedPort, { sibling: siblingStart }); const { drainAndShutdown, isRecyclingForExit, startServer } = await import("../server"); // One private readiness gate for this startServer invocation, captured by the // listener's closure. handleStart owns it and transitions it after the @@ -334,7 +342,7 @@ async function handleStart(options: { block?: boolean } = {}) { continue; } console.log(`⚠️ Port ${port} was taken while starting; picking another...`); - port = await chooseListenPort(requestedPort); + port = await chooseListenPort(requestedPort, { sibling: siblingStart }); } } // A single request's streaming error must never crash the daemon serving every diff --git a/src/server/ports.ts b/src/server/ports.ts index ae8fe16c1a..4c5a857803 100644 --- a/src/server/ports.ts +++ b/src/server/ports.ts @@ -151,6 +151,13 @@ export function shouldPersistSelectedPort( configPort: number | undefined, selectedPort: number, preferredPort: number, + options: { sibling?: boolean } = {}, ): boolean { + // A sibling start (`--port X` beside a live proxy on the configured port) is a + // second instance, not a new home for this config. Persisting its port rewrote + // config.port under the still-running configured-port proxy, and the next + // `ocx service` install then baked the sibling's port into the service and + // re-pointed every client at a listener that no longer existed. + if (options.sibling) return false; return selectedPort === preferredPort && configPort !== selectedPort; } diff --git a/tests/cli-dispatch.test.ts b/tests/cli-dispatch.test.ts index aee0456546..04c54ffed3 100644 --- a/tests/cli-dispatch.test.ts +++ b/tests/cli-dispatch.test.ts @@ -252,6 +252,19 @@ describe("start probes the configured port before shadowing it (source-level)", expect(cliSource).not.toContain("explicitSiblingPort"); }); + test("a sibling start carries its flag into every chooseListenPort call", () => { + // The sibling instance must not persist its explicit port into config.port: the + // configured-port proxy still owns this home, and `ocx service` bakes config.port. + // Both call sites (initial pick and the EADDRINUSE re-pick) have to pass the flag, + // or the re-pick path silently regains the old behavior. + const calls = cliSource.match(/await chooseListenPort(([^)]*))/g) ?? []; + expect(calls.length).toBe(2); + for (const call of calls) { + expect(call).toContain("sibling: siblingStart"); + } + expect(cliSource).toContain("siblingStart = true;"); + }); + test("the probe option still gates on an explicit true", () => { // A truthy-but-not-true default would silently probe for callers that pass // nothing, which is a different behavior than the one asserted above. diff --git a/tests/ports.test.ts b/tests/ports.test.ts index fdf0dae078..c6c36883a3 100644 --- a/tests/ports.test.ts +++ b/tests/ports.test.ts @@ -62,6 +62,15 @@ describe("port selection", () => { expect(shouldPersistSelectedPort(10100, 10100, 10100)).toBe(false); }); + test("a sibling start never persists its explicit port over the configured one", () => { + // `ocx start --port 10198` beside a live proxy on 10100: the sibling gets its port, + // but config.port stays 10100 so the next `ocx service` install is not re-pinned. + expect(shouldPersistSelectedPort(10100, 10198, 10198, { sibling: true })).toBe(false); + // The same arguments without the sibling flag are the ordinary first-start persist. + expect(shouldPersistSelectedPort(10100, 10198, 10198)).toBe(true); + expect(shouldPersistSelectedPort(10100, 10198, 10198, { sibling: false })).toBe(true); + }); + test("waitForPortAvailable resolves once a busy port is released", async () => { const { server, port } = await listen(); expect(await isPortAvailable(port)).toBe(false); From 86dee69ee620105b4abdd2ceee8b2c7be5d02bd2 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 10:32:45 +0900 Subject: [PATCH 152/172] docs(readme): stack the four demo gifs one per row so they stop getting clipped (#3235) Co-authored-by: jun --- README.md | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a6ad4570fe..9f5f524468 100644 --- a/README.md +++ b/README.md @@ -16,22 +16,26 @@ ocx start # proxy + dashboard on localhost:10100 - - + + - - + + From 1c8278b4dac5611e6066979556215688c6d7250e Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 10:34:50 +0900 Subject: [PATCH 153/172] fix: fail over zero-output incomplete combo streams (#3236) Co-authored-by: RHODIZ IT <180237049+RHODIZSECURITY@users.noreply.github.com> --- .../responses/combo-stream-preflight.ts | 31 +++++++-- tests/combo-stream-preflight.test.ts | 67 +++++++++++++++++++ tests/server-combo-failover-e2e.test.ts | 42 ++++++++++++ 3 files changed, 134 insertions(+), 6 deletions(-) diff --git a/src/server/responses/combo-stream-preflight.ts b/src/server/responses/combo-stream-preflight.ts index 04c4ef1baa..3856c32b98 100644 --- a/src/server/responses/combo-stream-preflight.ts +++ b/src/server/responses/combo-stream-preflight.ts @@ -24,6 +24,24 @@ const TERMINAL_EVENTS = new Set([ "response.incomplete", ]); +const RETRYABLE_ZERO_OUTPUT_INCOMPLETE_REASONS = new Set([ + "adapter_eof", + "missing_terminal_event", + "upstream_stall_timeout", +]); + +function retryableZeroOutputTerminal(payload: unknown): boolean { + if (!payload || typeof payload !== "object" || Array.isArray(payload)) return false; + const event = payload as { + type?: unknown; + response?: { incomplete_details?: { reason?: unknown } }; + }; + if (event.type === "response.failed") return true; + if (event.type !== "response.incomplete") return false; + const reason = event.response?.incomplete_details?.reason; + return typeof reason === "string" && RETRYABLE_ZERO_OUTPUT_INCOMPLETE_REASONS.has(reason); +} + /** * Decide when replaying the request on another combo target would risk duplicating * client-visible output or a tool-side effect. Unknown event types commit the child @@ -128,14 +146,14 @@ export async function preflightComboStreamResponse( let bufferedBytes = 0; let outputCommitted = false; let terminalStatus: ResponsesTerminalStatus | undefined; - let failedPayload: Record | undefined; + let retryableTerminalPayload: Record | undefined; const inspector = createSseInspector({ logCtx, onParsedPayload: payload => { if (comboStreamPayloadCommitsOutput(payload)) outputCommitted = true; if (!payload || typeof payload !== "object" || Array.isArray(payload)) return; - if ((payload as { type?: unknown }).type === "response.failed") { - failedPayload = payload as Record; + if (retryableZeroOutputTerminal(payload)) { + retryableTerminalPayload = payload as Record; } }, onTerminal: status => { terminalStatus = status; }, @@ -162,9 +180,10 @@ export async function preflightComboStreamResponse( inspector.feed(retained); } - if (terminalStatus === "failed" && !outputCommitted && failedPayload) { - await reader.cancel("retrying zero-output combo stream failure").catch(() => undefined); - return { kind: "failed", response: failedTerminalResponse(response, failedPayload, logCtx) }; + if ((terminalStatus === "failed" || terminalStatus === "incomplete") + && !outputCommitted && retryableTerminalPayload) { + await reader.cancel("retrying zero-output combo stream terminal").catch(() => undefined); + return { kind: "failed", response: failedTerminalResponse(response, retryableTerminalPayload, logCtx) }; } if (next.done || terminalStatus !== undefined || outputCommitted || bufferedBytes >= COMBO_STREAM_PREFLIGHT_MAX_BYTES diff --git a/tests/combo-stream-preflight.test.ts b/tests/combo-stream-preflight.test.ts index 06871f9d58..b4422727d2 100644 --- a/tests/combo-stream-preflight.test.ts +++ b/tests/combo-stream-preflight.test.ts @@ -18,6 +18,7 @@ describe("combo stream preflight", () => { expect(comboStreamPayloadCommitsOutput({ type: "response.created" })).toBe(false); expect(comboStreamPayloadCommitsOutput({ type: "response.heartbeat" })).toBe(false); expect(comboStreamPayloadCommitsOutput({ type: "response.failed" })).toBe(false); + expect(comboStreamPayloadCommitsOutput({ type: "response.incomplete" })).toBe(false); expect(comboStreamPayloadCommitsOutput({ type: "response.output_text.delta", delta: "x" })).toBe(true); expect(comboStreamPayloadCommitsOutput({ type: "response.output_item.added", item: { type: "function_call" } })).toBe(true); expect(comboStreamPayloadCommitsOutput({ type: "provider.future_event" })).toBe(true); @@ -49,6 +50,72 @@ describe("combo stream preflight", () => { expect(JSON.stringify(body)).not.toContain("provider_trace_id"); }); + test("converts zero-output transport incompletes into retryable HTTP failures", async () => { + const cases = [ + ["adapter_eof", "Upstream stream ended unexpectedly without a terminal event"], + ["missing_terminal_event", "Upstream incomplete"], + ["upstream_stall_timeout", "Upstream stalled"], + ] as const; + for (const [reason, message] of cases) { + const result = await preflightComboStreamResponse(sse( + { type: "response.created", response: { id: "r1", status: "in_progress" } }, + { + type: "response.incomplete", + response: { + id: "r1", + status: "incomplete", + incomplete_details: { reason }, + usage: { input_tokens: 11, output_tokens: 0, total_tokens: 11 }, + }, + }, + ), { model: "m1", provider: "a" }); + + expect(result.kind).toBe("failed"); + expect(result.response.status).toBe(502); + const body = await result.response.json(); + expect(body.error).toMatchObject({ type: "upstream_error", code: "upstream_server_error" }); + expect(body.error.message).toContain(message); + expect(body.response.usage).toMatchObject({ input_tokens: 11, output_tokens: 0 }); + } + }); + + test("does not replay semantic incompletes that another provider cannot safely repair", async () => { + const source = sse( + { type: "response.created", response: { id: "r1", status: "in_progress" } }, + { + type: "response.incomplete", + response: { + id: "r1", + status: "incomplete", + incomplete_details: { reason: "max_output_tokens" }, + }, + }, + ); + const expected = await source.clone().text(); + const result = await preflightComboStreamResponse(source, { model: "m1", provider: "a" }); + expect(result.kind).toBe("accepted"); + expect(await result.response.text()).toBe(expected); + }); + + test("does not replay transport incompletes after output commits the target", async () => { + const source = sse( + { type: "response.created", response: { id: "r1", status: "in_progress" } }, + { type: "response.output_text.delta", delta: "visible" }, + { + type: "response.incomplete", + response: { + id: "r1", + status: "incomplete", + incomplete_details: { reason: "adapter_eof" }, + }, + }, + ); + const expected = await source.clone().text(); + const result = await preflightComboStreamResponse(source, { model: "m1", provider: "a" }); + expect(result.kind).toBe("accepted"); + expect(await result.response.text()).toBe(expected); + }); + test("replays buffered bytes unchanged after output commits the target", async () => { const original = [ { type: "response.created", response: { id: "r1", status: "in_progress" } }, diff --git a/tests/server-combo-failover-e2e.test.ts b/tests/server-combo-failover-e2e.test.ts index 0ce1063c79..a4e47446a7 100644 --- a/tests/server-combo-failover-e2e.test.ts +++ b/tests/server-combo-failover-e2e.test.ts @@ -204,6 +204,13 @@ function chatStream(text: string): Response { return new Response(frames, { headers: { "content-type": "text/event-stream" } }); } +function chatTruncatedZeroOutputStream(): Response { + const frames = [ + `data: ${JSON.stringify({ choices: [{ index: 0, delta: {}, finish_reason: null }] })}\n\n`, + ].join(""); + return new Response(frames, { headers: { "content-type": "text/event-stream" } }); +} + function chatErrorStream(message: string, prefix?: string): Response { const frames = [ ...(prefix @@ -495,6 +502,41 @@ describe("server combo failover 030 activation matrix", () => { } }); + test("zero-output adapter EOF hops to the next combo target", async () => { + const hits: string[] = []; + const a = serve(() => { + hits.push("a"); + return chatTruncatedZeroOutputStream(); + }); + const b = serve(() => { + hits.push("b"); + return chatStream("stream backup after adapter eof"); + }); + const config = comboConfig({ + a: provider("openai-chat", baseUrl(a), "key-a"), + b: provider("openai-chat", baseUrl(b), "key-b"), + }); + + const response = await postLogged(config, { stream: true }); + expect(response.status).toBe(200); + expect(JSON.stringify(await collectSse(response))).toContain("stream backup after adapter eof"); + expect(hits).toEqual(["a", "b"]); + + const { log, usage } = await latestAttemptReceipts(config); + for (const receipt of [log, usage]) { + expect(receipt).toMatchObject({ + provider: "combo", + model: "combo/free", + resolvedModel: "m2", + attempts: [ + { ordinal: 1, provider: "a", model: "m1", status: 502 }, + { ordinal: 2, provider: "b", model: "m2", status: 200 }, + ], + }); + expect(receipt.attempts[0]).not.toHaveProperty("firstOutputMs"); + } + }); + test("terminal SSE failure after output stays on the first target and never replays", async () => { const hits: string[] = []; const a = serve(() => { From 98444a50212493ba93f48657c5f72833623a2e5c Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 10:37:56 +0900 Subject: [PATCH 154/172] docs(readme): shrink the stacked demo gifs to 560px (#3237) Co-authored-by: jun --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9f5f524468..dbe2008f57 100644 --- a/README.md +++ b/README.md @@ -17,25 +17,25 @@ ocx start # proxy + dashboard on localhost:10100
- Claude Code running a routed model through opencodex — the status bar shows gpt-5.6-luna-medium as the active model
+
+ Claude Code running a routed model through opencodex — the status bar shows gpt-5.6-luna-medium as the active model
Claude Code, running any model.
The picker is stock Claude Code. The brain behind it isn't.
- opencodex demo — running a task in the Codex app on a routed non-OpenAI model
+
+ opencodex demo — running a task in the Codex app on a routed non-OpenAI model
Codex, running any model.
Pick a provider and go — same workflow, different brain.
- Claude Desktop answering as Claude Opus 4.8, then dispatching a GPT-5.6 Sol subagent through opencodex
+
+ Claude Desktop answering as Claude Opus 4.8, then dispatching a GPT-5.6 Sol subagent through opencodex
Claude Desktop, running any model.
Opus answers, then hands the task to a GPT-5.6 Sol subagent.
- Grok Build running GPT-5.6 Sol through opencodex and calling a Kimi K3 subagent
+
+ Grok Build running GPT-5.6 Sol through opencodex and calling a Kimi K3 subagent
Grok Build, running any model.
Sol drives the session and calls a Kimi K3 subagent.
From e4d481592d614fd19a1cf054475adba33413744f Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 10:39:06 +0900 Subject: [PATCH 155/172] =?UTF-8?q?docs(readme):=20orca-style=20feature=20?= =?UTF-8?q?rows=20=E2=80=94=20caption=20left,=20gif=20right=20(#3238)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: jun --- README.md | 74 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index dbe2008f57..c195a32d9d 100644 --- a/README.md +++ b/README.md @@ -14,31 +14,55 @@ npm install -g @bitkyc08/opencodex ocx start # proxy + dashboard on localhost:10100 ``` -
- Claude Code running a routed model through opencodex — the status bar shows gpt-5.6-luna-medium as the active model
+ Claude Code running a routed model through opencodex — the status bar shows gpt-5.6-luna-medium as the active model
Claude Code, running any model.
The picker is stock Claude Code. The brain behind it isn't.
- opencodex demo — running a task in the Codex app on a routed non-OpenAI model
+ opencodex demo — running a task in the Codex app on a routed non-OpenAI model
Codex, running any model.
Pick a provider and go — same workflow, different brain.
- Claude Desktop answering as Claude Opus 4.8, then dispatching a GPT-5.6 Sol subagent through opencodex
+ Claude Desktop answering as Claude Opus 4.8, then dispatching a GPT-5.6 Sol subagent through opencodex
Claude Desktop, running any model.
Opus answers, then hands the task to a GPT-5.6 Sol subagent.
- Grok Build running GPT-5.6 Sol through opencodex and calling a Kimi K3 subagent
+ Grok Build running GPT-5.6 Sol through opencodex and calling a Kimi K3 subagent
Grok Build, running any model.
Sol drives the session and calls a Kimi K3 subagent.
- - - - - - - - - - - - +
- Claude Code running a routed model through opencodex — the status bar shows gpt-5.6-luna-medium as the active model
- Claude Code, running any model.
The picker is stock Claude Code. The brain behind it isn't.
-
- opencodex demo — running a task in the Codex app on a routed non-OpenAI model
- Codex, running any model.
Pick a provider and go — same workflow, different brain.
-
- Claude Desktop answering as Claude Opus 4.8, then dispatching a GPT-5.6 Sol subagent through opencodex
- Claude Desktop, running any model.
Opus answers, then hands the task to a GPT-5.6 Sol subagent.
-
- Grok Build running GPT-5.6 Sol through opencodex and calling a Kimi K3 subagent
- Grok Build, running any model.
Sol drives the session and calls a Kimi K3 subagent.
-
+ + + + + + + + + + + + + + + +
+ +### Claude Code, running any model + +The picker is stock Claude Code. The brain behind it isn't. + + + Claude Code running a routed model through opencodex — the status bar shows gpt-5.6-luna-medium as the active model +
+ +### Codex, running any model + +Pick a provider and go — same workflow, different brain. + + + opencodex demo — running a task in the Codex app on a routed non-OpenAI model +
+ +### Claude Desktop, running any model + +Opus answers, then hands the task to a GPT-5.6 Sol subagent. + + + Claude Desktop answering as Claude Opus 4.8, then dispatching a GPT-5.6 Sol subagent through opencodex +
+ +### Grok Build, running any model + +Sol drives the session and calls a Kimi K3 subagent. + + + Grok Build running GPT-5.6 Sol through opencodex and calling a Kimi K3 subagent +

From 744d12d02d8078980a1e195a18be4485e30f4d4a Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 10:46:00 +0900 Subject: [PATCH 156/172] fix(subagents): auto-fallback encrypted V2 spawns to native Codex without a configured chain (#3239) An encrypted V2 worker payload needs the native ChatGPT backend, but applySubagentModelFallback only consulted a fallback chain the operator configured. With no chain, a routed sub-agent model reached the encrypted-task guard and failed with unreadable_encrypted_agent_task. When nativeFallbackOnly is set and no chain exists, build the chain from DEFAULT_SUBAGENT_MODELS. selectAvailableSubagentModel still drops every non-forward candidate and isSubagentModelUnavailable still honours disabled models, quota, and health. Ordinary routed spawns are unchanged. Source hunks from #3228; the bundled GUI fallback-chain editor is left for its own feature PR. Co-authored-by: jun Co-authored-by: x3M3x --- src/codex/subagent-model-fallback.ts | 9 +++++++-- tests/subagent-model-fallback.test.ts | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/codex/subagent-model-fallback.ts b/src/codex/subagent-model-fallback.ts index 27eea1e53d..ddd5b67e60 100644 --- a/src/codex/subagent-model-fallback.ts +++ b/src/codex/subagent-model-fallback.ts @@ -7,7 +7,7 @@ */ import { existsSync, readdirSync, readFileSync } from "node:fs"; import { join } from "node:path"; -import { hasOwnProvider } from "../config"; +import { DEFAULT_SUBAGENT_MODELS, hasOwnProvider } from "../config"; import { isRateLimitOrQuotaFailureMessage } from "../lib/errors"; import type { OcxParsedRequest, OcxConfig } from "../types"; import { slugsEquivalent } from "../providers/slug-codec"; @@ -609,9 +609,14 @@ export function applySubagentModelFallback( resolvedFallbackChain?: readonly string[] | null, ): { from?: string; to?: string; skipped?: string[] } | null { if (!isThreadSpawnRequest(headers)) return null; - const fallbackChain = resolvedFallbackChain === undefined + const configuredFallbackChain = resolvedFallbackChain === undefined ? resolveSubagentFallbackChain(parsed, config) : resolvedFallbackChain; + // Native-only encrypted V2 tasks need a readable ChatGPT backend even when the + // operator configured no fallback chain. Keep ordinary routed spawns unchanged. + const fallbackChain = configuredFallbackChain === null && nativeFallbackOnly + ? normalizedChain(parsed.modelId, config, [], DEFAULT_SUBAGENT_MODELS) + : configuredFallbackChain; if (!fallbackChain) return null; const selection = selectAvailableSubagentModel( parsed.modelId, diff --git a/tests/subagent-model-fallback.test.ts b/tests/subagent-model-fallback.test.ts index 75b2359ad5..d600072798 100644 --- a/tests/subagent-model-fallback.test.ts +++ b/tests/subagent-model-fallback.test.ts @@ -1056,6 +1056,29 @@ describe("subagent model fallback chain", () => { expect((parsed._rawBody as { model?: string }).model).toBe("alibaba-token-plan/qwen3.8-max"); }); + test("encrypted routed spawn gets an automatic native fallback when none is configured", () => { + const config = cfg({ + subagentModelFallback: undefined, + defaultProvider: "xai", + }); + const parsed = { + modelId: "xai/grok-4.5", + options: {}, + context: { messages: [] }, + _rawBody: { model: "xai/grok-4.5" }, + }; + const result = applySubagentModelFallback( + parsed as never, + new Headers({ "x-openai-subagent": "collab_spawn" }), + config, + "pool-a", + Date.now(), + true, + ); + expect(result?.to).toBe("gpt-5.5"); + expect(parsed.modelId).toBe("gpt-5.5"); + }); + test("applySubagentModelFallback is a no-op for main turns", () => { updateAccountQuota("pool-a", 95); const parsed = { From 7f00d0eee917dc6ec08e688eebd7ad4f3d972416 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 10:58:08 +0900 Subject: [PATCH 157/172] fix(subagents): let encrypted-task recovery run before the synthesized native chain (#3240) #3239 synthesized a DEFAULT_SUBAGENT_MODELS chain for an unreadable encrypted spawn when the operator configured none. That chain fires in the first fallback pass, before recoverEncryptedAgentTask, so with agentTaskRecovery enabled the spawn was rerouted to native gpt-5.5 and recovery was skipped along with its caller-auth, proxy-secret and token-validity gates. tests/agent-task-recovery-security.test.ts went 13/13 -> 2/13 on dev. Synthesize the chain only when recovery is not enabled. An operator who enabled recovery chose to decrypt and stay routed; a configured chain keeps its precedence either way. Regression: recovery enabled + no chain + nativeFallbackOnly -> no fallback (red without the guard); the 13 recovery security cases are green again. Co-authored-by: jun --- src/codex/subagent-model-fallback.ts | 8 ++++++++ tests/subagent-model-fallback.test.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/codex/subagent-model-fallback.ts b/src/codex/subagent-model-fallback.ts index ddd5b67e60..7110d4cf50 100644 --- a/src/codex/subagent-model-fallback.ts +++ b/src/codex/subagent-model-fallback.ts @@ -614,7 +614,15 @@ export function applySubagentModelFallback( : resolvedFallbackChain; // Native-only encrypted V2 tasks need a readable ChatGPT backend even when the // operator configured no fallback chain. Keep ordinary routed spawns unchanged. + // + // Not when encrypted-task recovery is enabled: that operator chose to decrypt the + // assignment and stay on the routed model. The synthesized chain would reroute the + // spawn to native in this first pass, before recovery runs, and recovery's own + // caller-auth / proxy-secret / token-validity gates would never execute + // (tests/agent-task-recovery-security.test.ts went 13/13 -> 2/13 when #3239 landed + // without this guard). A configured chain keeps its existing precedence. const fallbackChain = configuredFallbackChain === null && nativeFallbackOnly + && config.agentTaskRecovery?.enabled !== true ? normalizedChain(parsed.modelId, config, [], DEFAULT_SUBAGENT_MODELS) : configuredFallbackChain; if (!fallbackChain) return null; diff --git a/tests/subagent-model-fallback.test.ts b/tests/subagent-model-fallback.test.ts index d600072798..510c294d80 100644 --- a/tests/subagent-model-fallback.test.ts +++ b/tests/subagent-model-fallback.test.ts @@ -1079,6 +1079,33 @@ describe("subagent model fallback chain", () => { expect(parsed.modelId).toBe("gpt-5.5"); }); + test("the synthesized native chain yields to enabled encrypted-task recovery", () => { + // With recovery on, the first fallback pass must leave the routed spawn alone so + // recoverEncryptedAgentTask runs (and its security gates fire); rerouting here + // would bypass them. + const config = cfg({ + subagentModelFallback: undefined, + defaultProvider: "xai", + agentTaskRecovery: { enabled: true }, + }); + const parsed = { + modelId: "xai/grok-4.5", + options: {}, + context: { messages: [] }, + _rawBody: { model: "xai/grok-4.5" }, + }; + const result = applySubagentModelFallback( + parsed as never, + new Headers({ "x-openai-subagent": "collab_spawn" }), + config, + "pool-a", + Date.now(), + true, + ); + expect(result).toBeNull(); + expect(parsed.modelId).toBe("xai/grok-4.5"); + }); + test("applySubagentModelFallback is a no-op for main turns", () => { updateAccountQuota("pool-a", 95); const parsed = { From b54508c8c40913fc7243ccd3991305f641c459d2 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 10:59:11 +0900 Subject: [PATCH 158/172] fix(agents): allow Codexless V2 task recovery (#3241) Codexless's app-server sends originator=codexless_agent, which the encrypted V2 recovery admission allowlist rejected, so its child tasks failed as unreadable_encrypted_agent_task. Admit that originator. The issuer, client id, token, account and proxy-secret checks after admission are unchanged. Regression: the recovery request preserves originator=codexless_agent. Source from #3229. Co-authored-by: jun Co-authored-by: iamnomankazi <60215267+iamnomankazi@users.noreply.github.com> --- src/server/responses/agent-task-recovery.ts | 1 + tests/agent-task-recovery-security.test.ts | 23 +++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/server/responses/agent-task-recovery.ts b/src/server/responses/agent-task-recovery.ts index 8b409e175b..e1c35932ff 100644 --- a/src/server/responses/agent-task-recovery.ts +++ b/src/server/responses/agent-task-recovery.ts @@ -23,6 +23,7 @@ const CODEX_ORIGINATORS = new Set([ "Codex Desktop", "codex_app", "codex_work_desktop", + "codexless_agent", ]); const CODEX_OAUTH_CLIENT_ID = "app_EMoamEEZ73f0CkXaXp7hrann"; const OPENAI_TOKEN_ISSUERS = new Set(["https://auth.openai.com", "https://auth.openai.com/"]); diff --git a/tests/agent-task-recovery-security.test.ts b/tests/agent-task-recovery-security.test.ts index cf213aefbe..ab6ddb2f9e 100644 --- a/tests/agent-task-recovery-security.test.ts +++ b/tests/agent-task-recovery-security.test.ts @@ -413,4 +413,27 @@ describe("agent task recovery security", () => { expect(response.status).toBe(200); expect(recoveryOriginator).toBe("codex_work_desktop"); }); + + test("accepts the Codexless originator", async () => { + let recoveryOriginator = ""; + globalThis.fetch = (async (input, init) => { + if (String(input).includes("chatgpt.com")) { + recoveryOriginator = new Headers(init?.headers).get("originator") ?? ""; + return new Response(recoverySse("Recover the Codexless child task."), { status: 200 }); + } + return providerResponse(); + }) as typeof fetch; + const headers = codexHeaders(); + headers.set("originator", "codexless_agent"); + + const response = await post( + routedConfig(), + "xai/grok-4.5", + encryptedInput(), + headers, + ); + + expect(response.status).toBe(200); + expect(recoveryOriginator).toBe("codexless_agent"); + }); }); From 2cb5921747677a87291afe43372153e1032b4358 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 11:08:17 +0900 Subject: [PATCH 159/172] revert(subagents): drop the synthesized native chain for encrypted spawns (#3239, #3240) (#3242) * Revert "fix(subagents): let encrypted-task recovery run before the synthesized native chain (#3240)" This reverts commit 7f00d0eee917dc6ec08e688eebd7ad4f3d972416. * Revert "fix(subagents): auto-fallback encrypted V2 spawns to native Codex without a configured chain (#3239)" This reverts commit 744d12d02d8078980a1e195a18be4485e30f4d4a. --------- Co-authored-by: jun --- src/codex/subagent-model-fallback.ts | 17 ++------- tests/subagent-model-fallback.test.ts | 50 --------------------------- 2 files changed, 2 insertions(+), 65 deletions(-) diff --git a/src/codex/subagent-model-fallback.ts b/src/codex/subagent-model-fallback.ts index 7110d4cf50..27eea1e53d 100644 --- a/src/codex/subagent-model-fallback.ts +++ b/src/codex/subagent-model-fallback.ts @@ -7,7 +7,7 @@ */ import { existsSync, readdirSync, readFileSync } from "node:fs"; import { join } from "node:path"; -import { DEFAULT_SUBAGENT_MODELS, hasOwnProvider } from "../config"; +import { hasOwnProvider } from "../config"; import { isRateLimitOrQuotaFailureMessage } from "../lib/errors"; import type { OcxParsedRequest, OcxConfig } from "../types"; import { slugsEquivalent } from "../providers/slug-codec"; @@ -609,22 +609,9 @@ export function applySubagentModelFallback( resolvedFallbackChain?: readonly string[] | null, ): { from?: string; to?: string; skipped?: string[] } | null { if (!isThreadSpawnRequest(headers)) return null; - const configuredFallbackChain = resolvedFallbackChain === undefined + const fallbackChain = resolvedFallbackChain === undefined ? resolveSubagentFallbackChain(parsed, config) : resolvedFallbackChain; - // Native-only encrypted V2 tasks need a readable ChatGPT backend even when the - // operator configured no fallback chain. Keep ordinary routed spawns unchanged. - // - // Not when encrypted-task recovery is enabled: that operator chose to decrypt the - // assignment and stay on the routed model. The synthesized chain would reroute the - // spawn to native in this first pass, before recovery runs, and recovery's own - // caller-auth / proxy-secret / token-validity gates would never execute - // (tests/agent-task-recovery-security.test.ts went 13/13 -> 2/13 when #3239 landed - // without this guard). A configured chain keeps its existing precedence. - const fallbackChain = configuredFallbackChain === null && nativeFallbackOnly - && config.agentTaskRecovery?.enabled !== true - ? normalizedChain(parsed.modelId, config, [], DEFAULT_SUBAGENT_MODELS) - : configuredFallbackChain; if (!fallbackChain) return null; const selection = selectAvailableSubagentModel( parsed.modelId, diff --git a/tests/subagent-model-fallback.test.ts b/tests/subagent-model-fallback.test.ts index 510c294d80..75b2359ad5 100644 --- a/tests/subagent-model-fallback.test.ts +++ b/tests/subagent-model-fallback.test.ts @@ -1056,56 +1056,6 @@ describe("subagent model fallback chain", () => { expect((parsed._rawBody as { model?: string }).model).toBe("alibaba-token-plan/qwen3.8-max"); }); - test("encrypted routed spawn gets an automatic native fallback when none is configured", () => { - const config = cfg({ - subagentModelFallback: undefined, - defaultProvider: "xai", - }); - const parsed = { - modelId: "xai/grok-4.5", - options: {}, - context: { messages: [] }, - _rawBody: { model: "xai/grok-4.5" }, - }; - const result = applySubagentModelFallback( - parsed as never, - new Headers({ "x-openai-subagent": "collab_spawn" }), - config, - "pool-a", - Date.now(), - true, - ); - expect(result?.to).toBe("gpt-5.5"); - expect(parsed.modelId).toBe("gpt-5.5"); - }); - - test("the synthesized native chain yields to enabled encrypted-task recovery", () => { - // With recovery on, the first fallback pass must leave the routed spawn alone so - // recoverEncryptedAgentTask runs (and its security gates fire); rerouting here - // would bypass them. - const config = cfg({ - subagentModelFallback: undefined, - defaultProvider: "xai", - agentTaskRecovery: { enabled: true }, - }); - const parsed = { - modelId: "xai/grok-4.5", - options: {}, - context: { messages: [] }, - _rawBody: { model: "xai/grok-4.5" }, - }; - const result = applySubagentModelFallback( - parsed as never, - new Headers({ "x-openai-subagent": "collab_spawn" }), - config, - "pool-a", - Date.now(), - true, - ); - expect(result).toBeNull(); - expect(parsed.modelId).toBe("xai/grok-4.5"); - }); - test("applySubagentModelFallback is a no-op for main turns", () => { updateAccountQuota("pool-a", 95); const parsed = { From 7aa64bb0bf1700482c74064a4d7523a5a960cf11 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 11:30:05 +0900 Subject: [PATCH 160/172] feat(cursor): derive the picker seed from the capability table and label every row (#3222) * docs(cursor): diff-level roadmap for unified Cursor model identity One published row per Cursor base with thinking/fast/1M as dimensions, a Codex Fast toggle that reaches Cursor's fast variant, and a global switch that exposes -fast identities to clients without a toggle. Docs-only work-phase (wp1) of a four-phase unit. Contains 000_plan (work-phase map + measured current state + RUN verifier table), 001_current_state (why the picker never reads CURSOR_CAPABILITIES, where Codex Fast dies for Cursor), 002_audit_round1 (10 blockers from two review lanes, all folded), and diff-level decade docs 010/020/030 for the three implementation phases. Notable audit findings folded before any code: provider-level supportsServiceTier short-circuits before the per-model map; tierLogForRunTurn runs BEFORE runTurn so telemetry must recompute the variant rather than rebuild a non-pure request; usage/log.ts and usage/cost.ts read wireKind by string comparison and are invisible to tsc. Refs devlog/_plan/260902_cursor_unified_identity * feat(cursor): derive the picker seed from the capability table and label every row CURSOR_STATIC_MODELS was a hand-maintained list that drifted from CURSOR_CAPABILITIES: cursorUmbrellaRows() existed but only tests called it, so collapsing a variant changed routing without changing what Codex listed. The seed now derives from that function plus two declared lists for ids with no capability record, so the two can no longer disagree. Cursor rows also showed raw slugs (cursor/kimi-k3) because routedDisplayName passes a routed slug through unchanged and nothing carried Cursor's labels into the provider config. ProviderRegistryEntry had no modelDisplayNames field at all; the consumer (configuredModelDisplayName) already existed. Wire it through providerConfigSeed and enrichProviderFromRegistry, the latter per-model so an existing install picks up labels without losing an operator rename. The row set is unchanged (54 ids, none added or dropped) - this is a refactor of where rows come from, plus labels and three corrected windows (gemini 1048576, gpt-5.5-extra 200000) where the capability table was approximating the seed. Fixes the frozen row-count assertion that went red when claude-fable-5-1 was seeded in 5fc7d073e: it now derives the expected count instead of hard-coding it. Refs devlog/_plan/260902_cursor_unified_identity/010_wp2_umbrella_seed.md * docs(cursor): park wp2/wp4 residuals in a numbered doc The roadmap named residuals in prose with no home (audit B14): effort ladders on a listed fast id, claude-4-sonnet-1m staying a real row, fastMode carrying two meanings, and the five pre-existing test failures that reproduce on a clean stash of this branch. Each records what would change the decision, so a later cycle does not rediscover them as new findings. Refs devlog/_plan/260902_cursor_unified_identity/040_residuals.md * docs(cursor): record the measured -fast round-trip that proves audit B11 The reviewer's claim that a bare -fast suffix picks the wrong dimension was not theoretical. Measured: claude-opus-5-fast resolves to claude-opus-5-high-fast (clamped, and in the quarantined regular family) while the Codex toggle would send claude-opus-5-thinking-max-fast. The mirror case is just as wrong -grok-4.6-thinking-fast degrades to a bare grok-4.6 with no effort and no fast marker, because grok has no thinkingFast spec. Either fixed suffix is wrong for half the table, which is why cursorFastIdFor composes from the base's defaultVariant. --------- Co-authored-by: jun --- .../000_plan.md | 88 ++++ .../001_current_state.md | 107 +++++ .../002_audit_round1.md | 258 +++++++++++ .../010_wp2_umbrella_seed.md | 269 +++++++++++ .../020_wp3_codex_fast_toggle.md | 426 ++++++++++++++++++ .../030_wp4_global_fast_switch.md | 224 +++++++++ .../040_residuals.md | 47 ++ src/adapters/cursor/catalog.ts | 53 ++- src/adapters/cursor/discovery.ts | 169 ++++--- src/providers/derive.ts | 6 + src/providers/registry.ts | 9 + tests/cursor-display-names.test.ts | 55 +++ tests/cursor-umbrella-rows.test.ts | 41 +- 13 files changed, 1651 insertions(+), 101 deletions(-) create mode 100644 devlog/_plan/260902_cursor_unified_identity/000_plan.md create mode 100644 devlog/_plan/260902_cursor_unified_identity/001_current_state.md create mode 100644 devlog/_plan/260902_cursor_unified_identity/002_audit_round1.md create mode 100644 devlog/_plan/260902_cursor_unified_identity/010_wp2_umbrella_seed.md create mode 100644 devlog/_plan/260902_cursor_unified_identity/020_wp3_codex_fast_toggle.md create mode 100644 devlog/_plan/260902_cursor_unified_identity/030_wp4_global_fast_switch.md create mode 100644 devlog/_plan/260902_cursor_unified_identity/040_residuals.md create mode 100644 tests/cursor-display-names.test.ts diff --git a/devlog/_plan/260902_cursor_unified_identity/000_plan.md b/devlog/_plan/260902_cursor_unified_identity/000_plan.md new file mode 100644 index 0000000000..f4bb14b411 --- /dev/null +++ b/devlog/_plan/260902_cursor_unified_identity/000_plan.md @@ -0,0 +1,88 @@ +# Cursor unified model identity + +One published row per Cursor base. Thinking, fast, and 1M are dimensions of that row, +never extra slugs. The Codex Fast toggle drives the fast dimension; a global switch +exposes `-fast` identities to clients that have no toggle. + +## Why + +Cursor's own picker already works this way: `Claude Opus 5` is one row whose submenu +carries Thinking, Fast, Context (300K/1M), and Effort. OpenCodex has the same shape in +`CURSOR_CAPABILITIES` but never publishes it — `cursorUmbrellaRows()` is called by tests +only, and the picker is fed by the leftover product seed in `discovery.ts`. + +## Constraints + +- Every legacy id stays routable. Picker rows shrink; routability does not. +- Never run the repo-wide suite locally. Focused `bun test` files + `bun run typecheck` + + `bun run privacy:scan`; exact-head GitHub CI is the authoritative gate. +- Stacked PR chain against `dev`, parent first. Pushes use `git push --no-verify`. +- Out of scope: Codex app UI, Cursor transport/native-exec, other providers' fast wires, + dashboard `/api/models` namespaced ids, Desktop 3P hashed aliases. + +## Work-phase map (dependency-ordered) + +| WP | Deliverable | Consumes | +|----|-------------|----------| +| wp1 | this roadmap (docs only) | — | +| wp2 / PR1 | seed derives from the capability table; display names; window alignment | wp1 | +| wp3 / PR2 | `cursor-variant` FastWire; Codex Fast toggle reaches the fast dimension | wp2 (needs a stable base row set) | +| wp4 / PR3 | `fastMode` lists `-fast` identities outside Codex; request-time promotion | wp3 (needs the resolver's fast upgrade) | + +wp3 depends on wp2 because the Fast toggle is stamped per row: the row set must be the +capability-derived one before a per-base capability map can be attached to it. wp4 depends +on wp3 because listing `-fast` is only honest once the request path actually honours it. + +## Measured current state (2026-09-02, `.tmp/cursor_diff_probe.ts`) + +``` +SEED_COUNT 54 # CURSOR_STATIC_MODELS +CAPS_COUNT 34 # CURSOR_CAPABILITIES +UMBRELLA_ROWS 34 # cursorUmbrellaRows() — none missing from the seed +ROWS_NOT_IN_SEED [] # capability rows are all seeded +CAPS_NOT_IN_SEED [] +SEED_NOT_IN_CAPS (16) # claude-4-sonnet-1m, claude-4.5-haiku, composer-1, composer-2.5, + # composer-2.5-fast, gemini-2.5-flash, gemini-3-flash, gemini-3-pro, + # gemini-3-pro-image-preview, gemini-3.1-pro, gemini-3.5-flash, + # gpt-5-codex, gpt-5-fast, gpt-5-mini, gpt-5.1-codex, kimi-k2.7-code +WINDOW_MISMATCH # gemini-3.6-flash 1048576/1000000, gemini-3.7-flash 1048576/1000000, + # gpt-5.5-extra 200000/272000 +FAST_CAPABLE_BASES # claude-opus-4-7, claude-opus-4-8, claude-opus-5, grok-4.5, grok-4.6 +``` + +54 = 4 routers + 34 capability bases + 16 non-capability product ids. + +## Verifiers (RUN 2026-09-02 before being written here, PLAN-VERIFIER-REAL-01) + +| Command | Exit | Reads the change target? | +|---|---|---| +| `bun test tests/cursor-umbrella-rows.test.ts tests/cursor-catalog.test.ts tests/cursor-static-catalog.test.ts` | **1 — 74 pass / 1 fail** | yes — imports `catalog.ts` + `discovery.ts` directly | +| `bun test tests/fastwire-policy.test.ts tests/fastwire-observability.test.ts tests/service-tier-capability.test.ts` | 0 — 303 pass | yes — imports `fastwire.ts` / `service-tier.ts` | +| `bun test tests/claude-model-info.test.ts tests/claude-models-discovery.test.ts` | 0 — 27 pass | yes — imports `claude/model-info.ts` | +| `bun run typecheck` | pending measurement at wp2 B | yes — `tsc --noEmit` over `src/` and `tests/` | +| `bun run privacy:scan` | pending measurement at wp2 B | repo-wide credential scan; **does not observe this unit's behavior** | + +`privacy:scan` is a required gate, not a verifier of identity behavior; that acceptance row +is human review plus the focused tests above. + +**Pre-existing red on this branch point.** The cursor suite fails at HEAD `d975feaa4`, +before any change in this unit: + +``` +(fail) row count shrank from the 69-row legacy seed + tests/cursor-umbrella-rows.test.ts:40 Expected: 51 Received: 54 +``` + +Commit `5fc7d073e` seeded three `claude-fable-5-1` spellings and did not update the +assertion. wp2 owns the fix (010 §4 rewrites that assertion to the derived composition), +so wp2's C-phase evidence must show this file green rather than inheriting the failure. + +Environment note: a fresh worktree needs `bun install` first — without it these files fail +with `Cannot find module 'zod/v4'` / `'@bufbuild/protobuf'`, which is not a code defect. + +## Terminal outcomes + +DONE = wp1-wp4 closed through D with three stacked PRs at exact-head green CI. +BLOCKED = CI infrastructure or a live Cursor roster change with evidence. +NEEDS_HUMAN = a user-visible identity fork beyond the stated intent. +BUDGET_EXHAUSTED = 6h wall-clock or three failed repair rounds on one WP. diff --git a/devlog/_plan/260902_cursor_unified_identity/001_current_state.md b/devlog/_plan/260902_cursor_unified_identity/001_current_state.md new file mode 100644 index 0000000000..63fd9d99d5 --- /dev/null +++ b/devlog/_plan/260902_cursor_unified_identity/001_current_state.md @@ -0,0 +1,107 @@ +# Current state: how a Cursor row is built and where Fast dies + +Research only. No diffs here. + +## 1. The picker path never reads the capability table + +`cursorUmbrellaRows()` (`src/adapters/cursor/catalog.ts:554`) is imported by +`tests/cursor-umbrella-rows.test.ts` and nothing else in `src/`. The published rows come +from a different list: + +``` +CURSOR_STATIC_MODELS (discovery.ts:276) + -> registry.ts:1110 models: cursorModelIds(CURSOR_STATIC_MODELS) + -> derive.ts:230 seeded into config.providers.cursor.models + -> provider-fetch.ts:1394 cursor branch: live GetUsableModels intersection + -> sync.ts disabledModels removal, deriveEntry writes slug/display_name/... +``` + +So the capability table describes dimensions the picker never sees. Collapsing a variant in +`catalog.ts` changes routing, not listing. + +## 2. Four inconsistencies, measured + +**Mixed row semantics.** 16 seed ids have no capability record. Some are genuine products +with no base (`composer-1`, `composer-2.5`, `gemini-3-pro`, `gpt-5-codex`), and three are +dimensions wearing a row costume: `claude-4-sonnet-1m` (a real wire id, guarded by +`REAL_1M_WIRE_IDS` at `catalog.ts:333`), `gpt-5-fast`, `composer-2.5-fast`. + +**1M means two things.** `kimi-k3-1m` is synthetic — `CURSOR_ULTRA_1M_MODEL_IDS` +(`discovery.ts:174`) folds it into `kimi-k3` + Max Mode. `claude-4-sonnet-1m` is a real +upstream id and stays a second row. Both read as "1M" to a user. + +**Fast means two things.** Opus/Grok fast ids were folded to aliases +(`tests/cursor-umbrella-rows.test.ts:20-31`); `gpt-5-fast` and `composer-2.5-fast` remain +rows because they have no capability base. + +**Labels and windows.** `routedDisplayName()` (`sync.ts:272`) returns the slug unchanged for +every provider except command-code, so Cursor rows read `cursor/kimi-k3`. Three windows +disagree between seed and capability table (000_plan.md). + +`ProviderRegistryEntry` has `modelContextWindows`, `modelInputModalities`, +`modelReasoningEfforts` — but **no `modelDisplayNames`** (`registry.ts:265-290`), and +`ProviderConfigSeed` (`registry.ts:327`) does not list it either. The consumer exists +(`configuredModelDisplayName`, `provider-fetch.ts:634`) and reads +`prov.modelDisplayNames`; only the registry->config path is missing. + +## 3. Where Codex Fast dies for Cursor + +Codex Fast is OpenAI `service_tier`, not a boolean: + +``` +app catalog row service_tiers:[{id:"priority",name:"Fast"}] (effort.ts:160) + -> request service_tier:"priority" (parser.ts:826) + -> decideTier(policy, config.fastMode, callerTier) (fastwire.ts:392) + -> applyServiceTierGate deletes the field when kind==="drop" (responses/core.ts:2638) +``` + +The drop is structural. `FAST_WIRE_ADAPTERS` (`fastwire.ts:14-18`) maps +`"service-tier" -> {openai-chat, openai-responses}` and `"anthropic-speed" -> {}`. Cursor is +in neither, so `resolveFastPolicy` sets `eligibility: "wire-unavailable"`, +`serviceTierSupportFromPolicy` publishes `supportsServiceTier: false`, and +`applyCatalogModelMetadata` never stamps the tier. No config value fixes this: forcing +`supportsServiceTier: true` still fails the wire check, and declaring +`fastWire.kind: "service-tier"` fails the adapter-set check. + +Meanwhile the fast wire genuinely exists, keyed off the picked id: + +- Grok (`wirePrefix: "cursor-"`): base id + `{id:"effort"},{id:"fast",value:"true"}` + parameters, via `cursorGrokFastSelection` (`catalog.ts:538`, + `request-builder.ts:204-213`). +- Everyone else: flattened wire id `claude-opus-5-thinking-high-fast` via `composeWireId` + (`catalog.ts:446-466`). + +`normalizeCursorModelId` (`request-builder.ts:189`) receives only `parsed.modelId` and +`parsed.options.reasoning`. `rg` finds no `serviceTier`/`tierDecision` read anywhere under +`src/adapters/cursor/`. + +Telemetry is a separate hole: `adapters/registry.ts:156-176` attaches +`createAdapterTierMetadata(..., null, null)` for every non-OpenAI adapter, so even a +working Cursor fast request would report an absent wire field. + +Five bases have a fast dimension: `claude-opus-4-7`, `claude-opus-4-8`, `claude-opus-5`, +`grok-4.5`, `grok-4.6`. Stamping a tier on the other 29 would recreate the dead-toggle +defect `NO_FAST_TIER_NATIVE_SLUGS` (`parsing.ts:297`) exists to prevent. + +## 4. Listing surfaces outside Codex + +`GET /v1/models` has three branches (`server/index.ts:1316-1560`): + +| Trigger | Id shape | Composed at | +|---|---|---| +| `?client_version` | catalog slugs | `buildCatalogEntries` | +| `anthropic-version` / `?flavor=anthropic` | `claude-ocx-*` or Desktop hashes | `claude/model-info.ts:105` | +| default | `alias ?? provider/id` | `server/index.ts:1534` | + +`buildAnthropicModelInfos` already publishes a second row for a dimension: `push1mVariant` +(`model-info.ts:115-128`) appends `[1m]`, and `resolveInboundModel` strips it before +routing. That is the precedent `-fast` listing should follow. + +`config.fastMode` (`types/config.ts:462`) is tri-state and today only reaches +`decideTier` plus Codex's injected `[features] fast_mode` (`codex/inject.ts:708`). It +touches no listing code: `rg fastMode` is empty in `server/index.ts`, +`claude/model-info.ts`, `management/model-rows.ts`, and `cli/models.ts`. + +Dashboard `/api/models` uses `namespaced` as the disable/export key +(`catalogModelSlug`, `parsing.ts:703`), so rewriting it would desync `disabledModels`. +That surface stays untouched. diff --git a/devlog/_plan/260902_cursor_unified_identity/002_audit_round1.md b/devlog/_plan/260902_cursor_unified_identity/002_audit_round1.md new file mode 100644 index 0000000000..42ff67750f --- /dev/null +++ b/devlog/_plan/260902_cursor_unified_identity/002_audit_round1.md @@ -0,0 +1,258 @@ +# Audit round 1 — main-agent verification of the roadmap + +Blockers found by running the plan's own claims against the tree at `d975feaa4`. +All folded into 010/020/030 in the same pass. An independent `xai/grok-4.6` reviewer lane +is running concurrently; its findings append as round 2. + +## B1 (Critical) — `fastWireDeclarationError` hard-rejects the new kind + +`src/providers/fastwire.ts:470` + +```ts +if (value.kind !== "service-tier" && value.kind !== "anthropic-speed") { + return "fastWire.kind must be service-tier or anthropic-speed"; +} +``` + +020 §5 called `fastWireSchema` "an enum that must list the value" and treated the +validator as an unknown. It is neither an enum nor unknown: `src/config.ts:495` types +`kind` as a bare `z.string()` and delegates to this function, which rejects any third +kind. A cursor registry entry declaring `kind: "cursor-variant"` fails +`registryFastWireDeclarationError` at load, so the provider entry is invalid before any +request runs. **Fold:** the string literal list here is a required edit, called out +explicitly in the 020 change map. + +## B2 (Critical) — `hasFastWireCapabilityConflict` is not the constraint 020 assumed + +`src/providers/fastwire.ts:445-455` + +```ts +if (source.fastWire !== null) return false; +``` + +The conflict only fires for `fastWire: null`. 020 §2 planned +`supportsServiceTier: false` + `modelSupportsServiceTier: {5 bases: true}` and worried +this would be rejected. It is not — but the real problem is the opposite one, and worse: + +`src/providers/fastwire.ts:~350` (resolveFastPolicy) + +```ts +const capability = authority.capability.provider === false + ? false + : exactCapability ?? authority.capability.provider; +``` + +`capability.provider === false` short-circuits **before** `exactCapability` is consulted. +So `supportsServiceTier: false` would force every Cursor model to +`capability-unsupported`, including the five with a fast variant, and the per-model +`true` entries would be dead config. **Fold:** omit `supportsServiceTier` entirely on the +cursor entry (leave it `undefined`) and let `modelSupportsServiceTier` decide per model. +A base with no entry then resolves `capability === undefined` → `eligibility: +"unclassified"` → `serviceTierSupportFromPolicy` returns `false` when +`forwardCallerTier` is false (`service-tier.ts:268-274`), which is exactly the desired +"no toggle" outcome. + +## B3 (High) — the catalog stamp is ordered against us + +`src/codex/catalog/sync.ts:335-349` + +``` +applyReasoningLevels(e, ...) +normalizeRoutedCatalogEntry(e, ...) // deletes service_tiers / additional_speed_tiers +applyCatalogMetadata(e, ...) +applyCatalogModelMetadata(e, model) // re-stamps when model.supportsServiceTier === true +``` + +020 asserted the ordering was fine but recorded no proof. It is fine — the strip runs +**before** the stamp — so a routed Cursor row can carry tiers. **Fold:** record the proven +order in 020 so a later reader does not re-derive it, and make the wp3 C-phase assert on a +built entry rather than on `applyCatalogModelMetadata` in isolation. + +## B4 (High) — `usage/cost.ts` is a consumer 020 missed + +`src/usage/cost.ts:418-425` + +```ts +if (outcome.fastOutcome === "unknown" + && outcome.wireKind === "service-tier" + && typeof outcome.wireValue === "string") { + return { requestedServiceTier: outcome.wireValue }; +} +``` + +020 §5's consumer list named `FAST_WIRE_ADAPTERS`, `AttemptTierOutcome.wireKind`, +`canonicalFromWire`, `behavior.ts`, and `fastWireDeclarationError` — not this. It is a +string comparison, not an exhaustive switch, so `tsc` will **not** flag it: a +`"cursor-variant"` outcome silently takes the fall-through and reports no requested tier +for pricing. The branch above it (`canonical === "priority" && confirmation === "assumed"`, +line 414) does cover the Cursor case correctly, since 020 §4 sets +`confirmation: "assumed"`. **Fold:** 020 records this as verified-correct-by-accident and +adds a cost-attribution assertion so a future refactor cannot break it silently. + +## B5 (Medium) — `registryModelServiceTierCapabilityApplies` is a base-URL guard, not auth + +`src/providers/registry.ts:2935-2941` — it reads +`modelServiceTierCapabilityBaseUrlGuard`, which only the OpenRouter entry sets +(`registry.ts:1610`). 020 §2's "verify it does not gate OAuth providers" concern is +resolved: Cursor sets no guard, so the predicate returns `true`. **Fold:** replace the +open question with the answer. + +## B6 (Medium) — anthropic-inbound already gets a tier decision + +`src/server/claude-messages.ts:37,772` replays through `handleResponses`, which is the +same path that runs `decideTier` at `responses/core.ts:2095`. 030 §5 left this as "confirm +during B" and planned a `tierDecision === undefined` fallback. The fallback is therefore +**unreachable on that path** — a branch nobody can show firing +(C-ACTIVATION-GROUNDING-01). **Fold:** 030 drops the speculative fallback and instead +requires an activation test proving the anthropic-inbound route reaches the Cursor +resolver with `tierDecision.kind === "set"`. + +## Non-blockers confirmed + +- No import cycle: `catalog.ts` and `effort-map.ts` have **zero** imports of + `discovery.ts` (`rg '^import'` returns nothing for catalog.ts's header block; discovery + imports from effort-map and catalog, one direction only). +- Row arithmetic: measured `SEED_COUNT 54`, and `CURSOR_ROUTER_MODEL_IDS` is derived + (`discovery.ts:113`) as auto + 3 levels = 4. 4 + 34 + 13 + 3 = 54 holds. +- `claude-4.5-haiku` was in 010's product list and is genuinely absent from + `CURSOR_CAPABILITIES`; no seed id is dropped by the new composition. + +## Round 2 — independent reviewer (xai/grok-4.6, lane `Aquinas`) + +Narrow packet: five targeted questions about the WP3 design. Two findings were blockers my +round-1 pass missed; one corrected a design I had already written into 020. + +### B7-REVISED (Critical) — `tierLogForRunTurn` runs BEFORE `runTurn` + +`src/server/responses/core.ts:3477-3479` + +```ts +let runTurnAdapter = adapter; +if (adapter.runTurn) { + recordAdapterTierMetadata(logCtx, adapter.tierLogForRunTurn?.(parsed)); +} +``` + +I had written a write-back design (`runTurn` stamps a flag, `tierLogForRunTurn` reads it). +That is read-before-write and would always report `null`. A rebuild there is equally wrong: +it runs before `_cursorIdentityScope` (`cursor.ts:134-146`) and `_cursorConversationId` +(`cursor.ts:160`) exist, so it mints a second `crypto.randomUUID()` conversation and hashes +a `local` scope. **Fold:** 020 §4 recomputes the pure VARIANT through a shared +`cursorRequestEmitsFastVariant(parsed)` helper; the write-back block was deleted. + +### B8 (Critical) — `src/usage/log.ts` discards the whole outcome + +`normalizeAttemptTierOutcome` allowlists `wireKind` at `:322-325` and again at `:340`, +returning `null` for any third kind, so a persisted attempt loses its tier row and the GUI +Logs view shows nothing after restart. Invisible to `tsc` (string comparison). +**Fold:** both sites added to the 020 change map. + +### B9 (High) — an existing test asserts the opposite invariant + +`tests/fastwire-policy.test.ts:647` asserts +`PROVIDER_REGISTRY.every(entry => entry.fastWire === undefined)`. WP3 ends that by design. +**Fold:** rewrite it to the new invariant rather than delete the coverage. + +### B10 (Medium) — my `resolveCursorSelection` hunk was incomplete + +`parsed.kind` is read at `catalog.ts:487`, `:493`, and `:494-495`; my diff rebound only the +spec, which would emit a thinking id with no `-fast` and keep `cursor-` on an upgraded Grok +pick. **Fold:** 020 §3 shows the full three-site hunk. + +### Confirmed non-issues + +- The `parsed` object core mutates at `:2095` is the same one Cursor reads at + `cursor.ts:119,148` — no clone (dispatch traced at `core.ts:5330`). +- No `tests/cursor-*.test.ts` asserts absence of `service_tiers`, so stamping tiers on + Cursor rows breaks nothing there. +- `core.ts:2636`'s `kind === "service-tier"` test governs only foreign OpenAI caller tiers; + Cursor Fast takes the canonical early-return at `:2635`. + +Reviewer's normalized line: `VERDICT: GO-WITH-FIXES (blockers=2)`. Both folded above. + +The broad round-1 lane (`Carver`) is still running; anything it returns that is not already +folded appends as round 3. + +## Round 3 — broad reviewer lane (`Carver`), 10 blockers + +Returned after the round-2 lane. Six findings duplicate what round 1/2 already folded +(B1 kind allowlist, B2 supportsServiceTier short-circuit, B4/B8 cost+log consumers, +B5 base-URL guard, B9 fastwire-policy assertion, verifier honesty). Independent +confirmation of the same diagnosis from a lane that read the tree separately. + +Four are NEW and two of those are real design defects: + +### B11 (High, NEW) — the listed `-fast` id is the WRONG dimension for thinking-default bases + +`cursorFastIdFor` returns `-fast`, and `parseCursorVariantId("claude-opus-5-fast")` +yields `kind: "fast"` — the REGULAR-fast sibling, not `thinkingFast`. Measured: + +``` +umbrella claude-opus-5 + high -> claude-opus-5-thinking-high +listed claude-opus-5-fast + max -> claude-opus-5-high-fast (regular-fast, clamped) +thinkingFast + max -> claude-opus-5-thinking-max-fast +``` + +So WP3's Codex toggle (`thinking -> thinkingFast`) and WP4's listed id would send DIFFERENT +wires for the same base and the same user intent. Worse, `claude-opus-5`'s regular variant is +quarantined, so the listed id routes into the dead family. + +**Fold:** `cursorFastIdFor` composes from the base's `defaultVariant` — `thinking` yields +`-thinking-fast`, `regular` yields `-fast` — so the listed id parses back to the +same variant `upgradeToFast` picks. WP4 adds an equivalence test asserting the listed id and +the toggled umbrella id resolve to the same wire for every fast-capable base. + +### B12 (High, NEW) — `options.fastMode` in 030 had no possible caller + +`CreateCursorRequestOptions` carries only `forceFreshConversation` +(`request-builder.ts:369`) and `AdapterFactoryContext` has no `fastMode` +(`adapters/registry.ts:18`). The fallback I had already dropped for being unreachable was +also unimplementable. Confirms the round-1 B6 disposition. The reviewer additionally proved +chat-completions is not native-chat for Cursor (`isNativeChatRouteEligible` requires +`adapter === "openai-chat"`, `chat-native.ts:62`) and replays through `handleResponses` +(`chat-completions.ts:130,254`), so BOTH non-Codex inbound paths populate `tierDecision`. + +### B13 (Medium, NEW) — Grok's two call sites must change atomically + +`request-builder.ts:204` calls `cursorGrokFastSelection(id, reasoning)` with no third +argument. If only `resolveCursorSelection` learns the fast flag, a toggled Grok pick would +emit a flattened `grok-4.6-high-fast` instead of the required +`{id:"fast",value:"true"}` parameters — violating WP3's own accept row. Both helpers and +that call site are one atomic edit, and the Grok accept-row test belongs to WP3. + +### B14 (Low, NEW) — no `040+` doc for residuals + +030 names a residual (effort ladders advertised on a listed fast id) with no home. Park it +in `040_residuals.md` when WP4 lands rather than leaving it only in prose. + +Reviewer's normalized line: `VERDICT: GO-WITH-FIXES (blockers=10)`. + +## B11 confirmed by measurement (`.tmp/probe3.ts`, at 7adb1e66a) + +The reviewer's claim was not theoretical. Bare `-fast` on a thinking-default base picks the +REGULAR-fast sibling and diverges from what the Codex toggle would send: + +``` +base default listed id kind resolved wire (max) +claude-opus-4-7 thinking claude-opus-4-7-fast fast claude-opus-4-7-max-fast + claude-opus-4-7-thinking-fast thinkingFast claude-opus-4-7-thinking-max-fast + umbrella claude-opus-4-7 thinking claude-opus-4-7-thinking-max +claude-opus-5 thinking claude-opus-5-fast fast claude-opus-5-high-fast <- clamped AND quarantined family + claude-opus-5-thinking-fast thinkingFast claude-opus-5-thinking-max-fast +grok-4.5 regular grok-4.5-fast fast grok-4.5-high-fast + grok-4.5-thinking-fast thinkingFast grok-4.5 <- degrades to a bare id +grok-4.6 regular grok-4.6-fast fast grok-4.6-xhigh-fast + grok-4.6-thinking-fast thinkingFast grok-4.6 <- degrades to a bare id +``` + +Two consequences the fix must respect, both visible above: + +1. For a thinking-default base, only `-thinking-fast` round-trips to the variant the + toggle picks. `claude-opus-5-fast` additionally clamps max->high and lands in the + quarantined regular family. +2. For a regular-default base, `-thinking-fast` is WRONG the other way: grok has no + thinkingFast spec, so `resolveCursorSelection` falls back to `variants.regular` and emits + a bare `grok-4.6` with no effort and no fast marker at all. + +So the id must be composed per base from `defaultVariant`, exactly as `cursorFastIdFor` in +030 §1 now does — a single shared suffix would be wrong for one half of the table either way. diff --git a/devlog/_plan/260902_cursor_unified_identity/010_wp2_umbrella_seed.md b/devlog/_plan/260902_cursor_unified_identity/010_wp2_umbrella_seed.md new file mode 100644 index 0000000000..922432ef86 --- /dev/null +++ b/devlog/_plan/260902_cursor_unified_identity/010_wp2_umbrella_seed.md @@ -0,0 +1,269 @@ +# WP2 / PR1 — the seed derives from the capability table + +Scope IN: `src/adapters/cursor/{catalog,discovery}.ts`, `src/providers/{registry,derive}.ts`, +`src/types/provider.ts` (registry entry type only), tests. +Scope OUT: fast wire, `fastMode`, any request-path change. + +Accept criteria: the Cursor row set equals capability bases + declared product bases; +every removed id still routes byte-identically; the Codex picker shows human labels; +seed and capability windows agree. + +## Change map + +| File | Action | +|---|---| +| `src/adapters/cursor/catalog.ts` | MODIFY — `CursorCapability.displayName`; window fixes; `cursorUmbrellaRows()` returns the label | +| `src/adapters/cursor/discovery.ts` | MODIFY — `CURSOR_PRODUCT_MODELS` (non-capability ids) + `CURSOR_STATIC_MODELS` derived; `cursorModelDisplayNames()` | +| `src/providers/registry.ts` | MODIFY — `modelDisplayNames` on the entry type + `ProviderConfigSeed`; cursor entry passes `cursorModelDisplayNames()` | +| `src/providers/derive.ts` | MODIFY — copy `entry.modelDisplayNames` into the seeded config | +| `tests/cursor-umbrella-rows.test.ts` | MODIFY — row-count and composition assertions | +| `tests/cursor-display-names.test.ts` | NEW — labels reach a built catalog row | + +## 1. `catalog.ts` — labels and window truth + +`CursorCapability` gains one field; every entry gains its label. Windows corrected to the +seed's measured values (`gemini-*` 1048576, `gpt-5.5-extra` 200000 — the seed carries the +observed numbers, the capability table was approximating). + +```diff + export interface CursorCapability { + readonly variants: Partial>; + readonly defaultVariant: CursorVariantKind; ++ /** Human picker label ("Claude Opus 5"). Cursor's own picker shows these. */ ++ readonly displayName: string; + readonly window: number; +``` + +```diff + const CONTEXT_1M = 1_000 * K; ++const CONTEXT_GEMINI = 1_048_576; +``` + +```diff + "claude-4.5-opus": { ++ displayName: "Claude Opus 4.5", + window: CONTEXT_200K, +``` + +Labels, in table order (Cursor's own spellings, read from its picker on 2026-09-02): + +``` +claude-4.5-opus Claude Opus 4.5 claude-4.6-opus Claude Opus 4.6 +claude-4.6-sonnet Claude Sonnet 4.6 claude-4.5-sonnet Claude Sonnet 4.5 +claude-4-sonnet Claude Sonnet 4 claude-fable-5 Claude Fable 5 +claude-fable-5-1 Claude Fable 5.1 claude-fable-5.1 Claude Fable 5.1 +claude-5.1-fable Claude Fable 5.1 claude-sonnet-5 Claude Sonnet 5 +claude-opus-4-7 Claude Opus 4.7 claude-opus-4-8 Claude Opus 4.8 +claude-opus-5 Claude Opus 5 glm-5.2 GLM 5.2 +glm-5.3 GLM 5.3 gemini-3.6-flash Gemini 3.6 Flash +gemini-3.7-flash Gemini 3.7 Flash kimi-k3 Kimi K3 +grok-4.5 Cursor Grok 4.5 grok-4.6 Cursor Grok 4.6 +gpt-5.1 GPT-5.1 gpt-5.1-codex-max GPT-5.1 Codex Max +gpt-5.1-codex-mini GPT-5.1 Codex Mini gpt-5.2 GPT-5.2 +gpt-5.2-codex GPT-5.2 Codex gpt-5.3-codex Codex 5.3 +gpt-5.4 GPT-5.4 gpt-5.4-mini GPT-5.4 Mini +gpt-5.4-nano GPT-5.4 Nano gpt-5.5 GPT-5.5 +gpt-5.5-extra GPT-5.5 Extra gpt-5.6-sol GPT-5.6 Sol +gpt-5.6-terra GPT-5.6 Terra gpt-5.6-luna GPT-5.6 Luna +``` + +Grok keeps Cursor's own "Cursor Grok" spelling because that is what its picker shows and +because the wire id carries the `cursor-` prefix. + +```diff + export interface CursorUmbrellaRow { + readonly id: string; ++ readonly displayName: string; + readonly efforts: readonly string[]; +``` + +```diff + rows.push({ + id: baseId, ++ displayName: capability.displayName, + efforts: spec.levels, +``` + +## 2. `discovery.ts` — the seed becomes derived + +`CURSOR_STATIC_MODELS` stops being a hand-maintained list of 54 and becomes +routers + umbrella rows + declared product ids. + +```diff +-export const CURSOR_STATIC_MODELS: readonly CursorModelInfo[] = normalizeCursorModels([ +- ...CURSOR_ROUTER_MODEL_IDS.map(id => ({ id, contextWindow: CONTEXT_200K, supportsReasoningEffort: false })), +- { id: "claude-sonnet-5", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, +- ... 50 more hand-written rows ... +-]); ++/** ++ * Cursor products that are NOT a dimension of any capability base. Each carries its own ++ * label because there is no capability record to read one from. A row belongs here only ++ * when Cursor ships it as a distinct product; a variant of a cataloged base does not. ++ */ ++export const CURSOR_PRODUCT_MODELS: readonly (CursorModelInfo & { displayName: string })[] = [ ++ { id: "claude-4.5-haiku", displayName: "Claude Haiku 4.5", contextWindow: CONTEXT_200K }, ++ { id: "composer-1", displayName: "Composer 1", contextWindow: CONTEXT_200K }, ++ { id: "composer-2.5", displayName: "Composer 2.5", contextWindow: CONTEXT_200K }, ++ { id: "gemini-2.5-flash", displayName: "Gemini 2.5 Flash", contextWindow: CONTEXT_GEMINI }, ++ { id: "gemini-3-flash", displayName: "Gemini 3 Flash", contextWindow: CONTEXT_GEMINI }, ++ { id: "gemini-3-pro", displayName: "Gemini 3 Pro", contextWindow: CONTEXT_GEMINI }, ++ { id: "gemini-3-pro-image-preview", displayName: "Gemini 3 Pro Image", contextWindow: CONTEXT_200K }, ++ { id: "gemini-3.1-pro", displayName: "Gemini 3.1 Pro", contextWindow: CONTEXT_GEMINI }, ++ { id: "gemini-3.5-flash", displayName: "Gemini 3.5 Flash", contextWindow: CONTEXT_200K }, ++ { id: "gpt-5-codex", displayName: "GPT-5 Codex", contextWindow: CONTEXT_272K }, ++ { id: "gpt-5-mini", displayName: "GPT-5 Mini", contextWindow: CONTEXT_272K }, ++ { id: "gpt-5.1-codex", displayName: "GPT-5.1 Codex", contextWindow: CONTEXT_272K }, ++ { id: "kimi-k2.7-code", displayName: "Kimi K2.7 Code", contextWindow: CONTEXT_262K }, ++]; ++ ++/** ++ * Real upstream wire ids that LOOK like a dimension of a cataloged base but are served as ++ * their own catalog row by Cursor. They stay rows; the parser already refuses to read them ++ * as synthetic markers (REAL_1M_WIRE_IDS / no capability base for gpt-5). ++ * ++ * claude-4-sonnet-1m: a distinct 1M-window row upstream, not claude-4-sonnet + ultra. ++ * claude-4-sonnet has no maxMode evidence, so folding it would invent a capability. ++ * gpt-5-fast: there is no `gpt-5` capability base for it to be a dimension of. ++ * composer-2.5-fast: composer-2.5 has no effort/variant dimensions at all. ++ */ ++export const CURSOR_REAL_ID_EXCEPTIONS: readonly (CursorModelInfo & { displayName: string })[] = [ ++ { id: "claude-4-sonnet-1m", displayName: "Claude Sonnet 4 (1M)", contextWindow: CONTEXT_1M }, ++ { id: "gpt-5-fast", displayName: "GPT-5 Fast", contextWindow: CONTEXT_272K }, ++ { id: "composer-2.5-fast", displayName: "Composer 2.5 Fast", contextWindow: CONTEXT_200K }, ++]; ++ ++/** ++ * Umbrella seed (devlog 260902_cursor_unified_identity): rows are DERIVED from ++ * CURSOR_CAPABILITIES via cursorUmbrellaRows(), so a capability change can no longer ++ * disagree with what the picker publishes. Thinking / fast / synthetic -1m remain ++ * routable aliases and add no rows. ++ */ ++export const CURSOR_STATIC_MODELS: readonly CursorModelInfo[] = normalizeCursorModels([ ++ ...CURSOR_ROUTER_MODEL_IDS.map(id => ({ id, contextWindow: CONTEXT_200K, supportsReasoningEffort: false })), ++ ...cursorUmbrellaRows().map(row => ({ ++ id: row.id, ++ contextWindow: row.window, ++ supportsReasoningEffort: row.efforts.length > 0, ++ })), ++ ...CURSOR_PRODUCT_MODELS, ++ ...CURSOR_REAL_ID_EXCEPTIONS, ++]); +``` + +Import `cursorUmbrellaRows` alongside the existing `parseCursorVariantId` import +(`discovery.ts:8`). `catalog.ts` does not import `discovery.ts`, so no cycle appears. + +New label accessor, mirroring `cursorModelContextWindows`: + +```diff ++export function cursorModelDisplayNames(): Record { ++ return Object.fromEntries([ ++ ...cursorUmbrellaRows().map(row => [row.id, row.displayName] as const), ++ ...CURSOR_PRODUCT_MODELS.map(m => [m.id, m.displayName] as const), ++ ...CURSOR_REAL_ID_EXCEPTIONS.map(m => [m.id, m.displayName] as const), ++ ...CURSOR_ROUTER_MODEL_IDS.map(id => [id, cursorRouterDisplayName(id)] as const), ++ ]); ++} +``` + +Router labels: `auto` -> "Auto", `auto-balance` -> "Auto (Balanced)", `auto-cost` -> +"Auto (Cost)", `auto-intelligence` -> "Auto (Intelligence)". + +Row-count arithmetic after the change: 4 routers + 34 umbrella + 13 product + 3 exceptions += **54**. Measured against the current seed (`.tmp/probe2.ts`, 2026-09-02): + +``` +ROUTERS 4 CAPS 34 PRODUCT 13 EXC 3 TOTAL 54 +DUPES [] # no id appears twice, so normalizeCursorModels drops nothing silently +DROPPED_VS_TODAY [] # every id the picker publishes today survives +ADDED_VS_TODAY [] # no new id appears +``` + +The published set is **identical**, so wp2 is a pure refactor of where rows come from: the +list stops being hand-maintained and starts deriving from the capability table. Behavior +changes in exactly two places — every row gains a label, and three windows are corrected. +That makes the existing alias/oracle tests a real regression bar rather than a formality. + +## 3. `registry.ts` / `derive.ts` — the missing display-name path + +```diff + modelContextWindows?: Record; ++ /** Registry-supplied picker labels; an operator's config value still wins. */ ++ modelDisplayNames?: Record; + modelInputModalities?: Record; +``` + +```diff + "adapter" | "baseUrl" | ... | "models" +- | "liveModels" | "contextWindow" | "modelContextWindows" | "modelInputModalities" ++ | "liveModels" | "contextWindow" | "modelContextWindows" | "modelDisplayNames" | "modelInputModalities" +``` + +```diff + ...(entry.modelContextWindows ? { modelContextWindows: { ...entry.modelContextWindows } } : {}), ++ ...(entry.modelDisplayNames ? { modelDisplayNames: { ...entry.modelDisplayNames } } : {}), +``` + +Cursor entry: + +```diff + modelContextWindows: cursorModelContextWindows(CURSOR_STATIC_MODELS), ++ modelDisplayNames: cursorModelDisplayNames(), +``` + +The consumer needs no change: `applyProviderConfigHints` already calls +`configuredModelDisplayName(prov, model.id)` and sets `displayName` on the CatalogModel, +which `sync.ts` prefers over `routedDisplayName`. Operator overrides keep winning because +`derive.ts` only fills when the config value is absent. + +## 4. Tests + +`tests/cursor-umbrella-rows.test.ts` — replace the frozen composition assertions: + +```diff +- expect(CURSOR_STATIC_MODELS.length).toBe(51); ++ // 4 routers + 34 umbrella bases + 13 product ids + 3 real-id exceptions. ++ expect(CURSOR_STATIC_MODELS.length).toBe(54); ++ }); ++ ++ test("every umbrella row is seeded and no capability base is missing", () => { ++ const ids = new Set(CURSOR_STATIC_MODELS.map(m => m.id)); ++ for (const row of cursorUmbrellaRows()) expect(ids.has(row.id)).toBe(true); ++ }); ++ ++ test("seed windows equal the capability windows they derive from", () => { ++ const seeded = new Map(CURSOR_STATIC_MODELS.map(m => [m.id, m.contextWindow])); ++ for (const row of cursorUmbrellaRows()) expect(seeded.get(row.id)).toBe(row.window); + }); +``` + +Existing `composer-2.5-fast` and pinned-session alias assertions stay green unchanged — +that is the regression bar for "every legacy id stays routable". + +`tests/cursor-display-names.test.ts` (NEW) — the label must survive the config path, not +merely exist in a table: + +```ts +test("cursor rows publish human labels through the seeded provider config", () => { + const config = seedProviderConfig("cursor"); // providers/derive.ts + expect(config.modelDisplayNames?.["kimi-k3"]).toBe("Kimi K3"); + expect(configuredModelDisplayName(config, "grok-4.6")).toBe("Cursor Grok 4.6"); + expect(configuredModelDisplayName(config, "claude-opus-5")).toBe("Claude Opus 5"); +}); + +test("an operator override still wins over the registry label", () => { + const config = seedProviderConfig("cursor"); + config.modelDisplayNames = { ...config.modelDisplayNames, "kimi-k3": "My K3" }; + expect(configuredModelDisplayName(config, "kimi-k3")).toBe("My K3"); +}); +``` + +## Risks + +A derived seed inherits capability mistakes: a base added to `CURSOR_CAPABILITIES` now +appears in the picker automatically. That is the intent, and live `GetUsableModels` +filtering still removes anything the account cannot call. + +`normalizeCursorModels` dedupes by id and sorts, so a product id colliding with a +capability base would silently drop one. No collision exists today +(`SEED_NOT_IN_CAPS` ∩ `CAPS` = ∅); the new row-composition test would catch a future one. diff --git a/devlog/_plan/260902_cursor_unified_identity/020_wp3_codex_fast_toggle.md b/devlog/_plan/260902_cursor_unified_identity/020_wp3_codex_fast_toggle.md new file mode 100644 index 0000000000..e743bf5535 --- /dev/null +++ b/devlog/_plan/260902_cursor_unified_identity/020_wp3_codex_fast_toggle.md @@ -0,0 +1,426 @@ +# WP3 / PR2 — the Codex Fast toggle reaches Cursor's fast dimension + +Stacked on PR1. Scope IN: `src/types/provider.ts`, `src/providers/{fastwire,registry}.ts`, +`src/adapters/cursor/{catalog,request-builder}.ts`, `src/adapters/cursor.ts`, tests. +Scope OUT: listing rewrites (WP4), other providers' wires, Cursor transport. + +Accept criteria, each with its activation scenario: + +| Path | Trigger | Observable effect | +|---|---|---| +| tier stamped | build a catalog for `cursor/claude-opus-5` | `service_tiers[0].id === "priority"` | +| no dead toggle | same for `cursor/kimi-k3` | no `service_tiers`, no `additional_speed_tiers` | +| thinking upgrade | request `cursor/claude-opus-5` + `service_tier:"priority"` | wire id ends `-fast` | +| grok params | request `cursor/grok-4.6` + Fast | `{id:"fast",value:"true"}` present, id stays `grok-4.6` | +| telemetry | same request | `tierLog.outcome.fastOutcome === "applied"` | + +## Change map + +| File | Action | +|---|---| +| `src/types/provider.ts` | MODIFY — `FastWire.kind` gains `"cursor-variant"` | +| `src/providers/fastwire.ts` | MODIFY — `FAST_WIRE_ADAPTERS` entry; **`fastWireDeclarationError:470` literal list** (audit B1) | +| `src/providers/registry.ts` | MODIFY — cursor `fastWire` + `modelSupportsServiceTier` (NO provider-level `supportsServiceTier`, audit B2) | +| `src/usage/log.ts` | MODIFY — **`normalizeAttemptTierOutcome` wireKind allowlist, both sites** (audit B8; otherwise the whole outcome row is discarded) | +| `src/adapters/cursor/catalog.ts` | MODIFY — `cursorFastCapableBases()`; `resolveCursorSelection` fast option | +| `src/adapters/cursor/request-builder.ts` | MODIFY — `normalizeCursorModelId` reads the tier decision; export `cursorRequestEmitsFastVariant` | +| `src/adapters/cursor.ts` | MODIFY — `tierLogForRunTurn` reports the resolved variant (must NOT rebuild, audit B7) | +| `src/usage/cost.ts` | NO CHANGE — but assert its behavior (audit B4) | +| `tests/fastwire-policy.test.ts` | MODIFY — the "A1 adds no explicit registry FastWire declaration" assertion (audit B9) | +| `tests/cursor-fast-tier.test.ts` | NEW — the five rows above | + +## 1. A Cursor-owned wire kind + +Reusing `"service-tier"` would claim Cursor emits a `service_tier` field. It does not; it +picks a different model variant. The kind is the honest name for that. + +```diff + export interface FastWire { +- kind: "service-tier" | "anthropic-speed"; ++ kind: "service-tier" | "anthropic-speed" | "cursor-variant"; +``` + +```diff + const FAST_WIRE_ADAPTERS: Readonly>> = { + "service-tier": SERVICE_TIER_ADAPTERS, + // A1 deliberately has no adapter implementation for Anthropic speed. + "anthropic-speed": new Set(), ++ // Cursor expresses Fast as a model-variant dimension, not a request field: the ++ // resolver swaps regular->fast / thinking->thinkingFast and the wire carries either a ++ // flattened -fast id or Grok's {id:"fast"} parameter. ++ "cursor-variant": new Set(["cursor"]), + }; +``` + +```diff ++/** Canonical Fast maps to the variant marker the Cursor resolver understands. */ ++const DEFAULT_CURSOR_VARIANT_FAST_WIRE: FastWire = Object.freeze({ ++ kind: "cursor-variant" as const, ++ canonicalToWire: Object.freeze({ priority: "fast" }), ++ foreignCallerTiers: "drop" as const, ++}); +``` + +`foreignCallerTiers: "drop"` because Cursor has no concept of an arbitrary tier string; +only canonical Fast means anything. + +`defaultFastWireForAdapter` stays OpenAI-only — Cursor's declaration comes from the +registry, so a provider whose adapter is cursor but whose entry is absent keeps today's +behavior: + +```diff + export function defaultFastWireForAdapter(adapter: string): FastWire | null { + return SERVICE_TIER_ADAPTERS.has(adapter) ? DEFAULT_SERVICE_TIER_FAST_WIRE : null; + } +``` + +No change there. `decideTier` needs none either: it is already generic over +`canonicalToWire`, so Fast on an eligible Cursor route returns `{kind:"set", value:"fast"}`. + +**`applyServiceTierGate` must not write `service_tier` onto a Cursor body.** The gate runs +on `rawBody` for OpenAI-shaped requests; Cursor's adapter builds its own Connect request +and never reads `rawBody`, so a `{kind:"set"}` decision is invisible to it unless the +adapter reads `options.tierDecision` — which is exactly what §3 adds. Confirm during B +that the gate does not inject the field into a Cursor `rawBody` that later gets logged; +if it does, guard the injection on `fastWire.kind === "service-tier"`. + +**Catalog stamp ordering is proven, not assumed (audit B3).** `sync.ts:335-349` runs +`applyReasoningLevels` -> `normalizeRoutedCatalogEntry` (strips tiers) -> +`applyCatalogMetadata` -> `applyCatalogModelMetadata` (re-stamps when +`model.supportsServiceTier === true`). The strip precedes the stamp, so a routed Cursor +row keeps its tier. wp3's check asserts on a BUILT catalog entry, not on +`applyCatalogModelMetadata` in isolation, so this ordering stays covered. + +## 2. Only fast-capable bases advertise the toggle + +```diff ++/** Bases whose capability declares a fast or thinking-fast variant. */ ++export function cursorFastCapableBases(): string[] { ++ return Object.entries(CURSOR_CAPABILITIES) ++ .filter(([, c]) => c.variants.fast !== undefined || c.variants.thinkingFast !== undefined) ++ .map(([id]) => id); ++} +``` + +Today that is exactly `claude-opus-4-7`, `claude-opus-4-8`, `claude-opus-5`, `grok-4.5`, +`grok-4.6` (measured, 000_plan.md). Deriving it means a future capability edit keeps the +toggle honest without a second list to update. + +```diff + modelDisplayNames: cursorModelDisplayNames(), ++ // Fast is a variant dimension, so only bases that actually have one may advertise it — ++ // a tier on a base without a fast wire is the dead-toggle defect (NO_FAST_TIER_NATIVE_SLUGS). ++ fastWire: { kind: "cursor-variant", canonicalToWire: { priority: "fast" }, foreignCallerTiers: "drop" }, ++ // NO provider-level supportsServiceTier: see audit B2 (002_audit_round1.md). ++ modelSupportsServiceTier: Object.fromEntries(cursorFastCapableBases().map(id => [id, true])), ++ fastTierDescription: "Cursor Fast variant", +``` + +**`supportsServiceTier` must stay ABSENT (audit B2, was a blocker).** `resolveFastPolicy` +computes `capability.provider === false ? false : exactCapability ?? capability.provider`, +so a provider-level `false` short-circuits BEFORE the per-model map and would kill the five +fast-capable bases too, leaving `modelSupportsServiceTier` as dead config. Leaving it +undefined yields: 5 bases `true` -> `eligible` -> tier stamped; 29 bases `undefined` -> +`unclassified` -> `serviceTierSupportFromPolicy` returns `false` because +`forwardCallerTier` is false on a non-service-tier adapter (`service-tier.ts:268-274`) +-> no toggle. Same outcome, without the short-circuit trap. + +`registryModelServiceTierCapabilityApplies` is RESOLVED, not an open question (audit B5): +it reads `modelServiceTierCapabilityBaseUrlGuard` (`registry.ts:2935-2941`), which only the +OpenRouter entry sets (`registry.ts:1610`). Cursor sets none, so it returns `true`; +`authKind` is never consulted. + +**The runtime validator must be widened in the same commit (audit B1, was a blocker).** +Without it the cursor entry is rejected at load: + +```diff +- if (value.kind !== "service-tier" && value.kind !== "anthropic-speed") { +- return "fastWire.kind must be service-tier or anthropic-speed"; ++ if (value.kind !== "service-tier" && value.kind !== "anthropic-speed" && value.kind !== "cursor-variant") { ++ return "fastWire.kind must be service-tier, anthropic-speed, or cursor-variant"; + } +``` + +`src/config.ts:495` types `kind` as a bare `z.string()` and delegates to +`fastWireDeclarationError` (`fastwire.ts:470`), so this single edit covers both the +registry and the on-disk config boundary. + +## 3. The request path consumes the decision + +```diff +-function normalizeCursorModelId(modelId: string, reasoning?: string): { ++function normalizeCursorModelId(modelId: string, reasoning?: string, fast?: boolean): { +``` + +```diff +- const grokFast = cursorGrokFastSelection(id, reasoning); ++ // Codex Fast is a variant switch here: an explicit -fast slug already parses as fast, ++ // and the toggle promotes an umbrella pick to its fast sibling when one exists. ++ const grokFast = cursorGrokFastSelection(id, reasoning, fast); +``` + +```diff +- const resolved = resolveCursorSelection(id, reasoning); ++ const resolved = resolveCursorSelection(id, reasoning, undefined, { fast }); +``` + +In `catalog.ts`, the upgrade is a kind mapping applied after parsing, before spec lookup: + +```diff ++function upgradeToFast(baseId: string, kind: CursorVariantKind): CursorVariantKind { ++ const variants = CURSOR_CAPABILITIES[baseId]?.variants; ++ if (!variants) return kind; ++ if (kind === "thinking" || kind === "thinkingFast") { ++ return variants.thinkingFast ? "thinkingFast" : kind; ++ } ++ return variants.fast ? "fast" : kind; ++} +``` + +```diff + export function resolveCursorSelection( + pickedId: string, + reasoning: string | undefined, + liveMaxModeIds?: ReadonlySet, ++ options: { fast?: boolean } = {}, + ): CursorResolvedSelection { + const parsed = parseCursorVariantId(pickedId); + if (!parsed.known) { ... } + const capability = CURSOR_CAPABILITIES[parsed.baseId]!; +- const spec = capability.variants[parsed.kind] ?? capability.variants.regular; ++ const kind = options.fast === true ? upgradeToFast(parsed.baseId, parsed.kind) : parsed.kind; ++ const spec = capability.variants[kind] ?? capability.variants.regular; +``` + +Every later use of `parsed.kind` in that function (`composeWireId`, the `wirePrefix` guard) +switches to `kind`. The prefix guard matters: `kind === "regular"` is what adds +`cursor-`, and a Grok pick upgraded to `fast` must not keep it — but Grok never reaches +`composeWireId` when fast, because `cursorGrokFastSelection` intercepts first. + +**All three later reads must move to `kind`, not just the spec lookup (audit B10).** The +reviewer quoted the live body: `parsed.kind` is read at `catalog.ts:487` (spec), `:493` +(`composeWireId`), and `:494-495` (the `wirePrefix === "cursor-"` guard). Rebinding only +`spec` would make Opus Fast emit the thinking id with no `-fast`, and would keep the +`cursor-` prefix on any Grok pick that bypassed `cursorGrokFastSelection`. The complete +hunk: + +```diff + const capability = CURSOR_CAPABILITIES[parsed.baseId]!; +- const spec = capability.variants[parsed.kind] ?? capability.variants.regular; ++ const kind = options.fast === true ? upgradeToFast(parsed.baseId, parsed.kind) : parsed.kind; ++ const spec = capability.variants[kind] ?? capability.variants.regular; + if (!spec) { ... } + const requested = parsed.level ?? reasoning; + const effort = cursorVariantEffort(spec, requested); +- const canonicalId = composeWireId(parsed.baseId, parsed.kind, effort); +- const wireId = capability.wirePrefix && parsed.kind === "regular" ++ const canonicalId = composeWireId(parsed.baseId, kind, effort); ++ const wireId = capability.wirePrefix && kind === "regular" + ? `${capability.wirePrefix}${canonicalId}` + : canonicalId; +``` + +`cursorGrokFastSelection` gains the same promotion so an umbrella Grok pick takes the +parameterized path: + +```diff + export function cursorGrokFastSelection( + pickedId: string, + reasoning: string | undefined, ++ fast?: boolean, + ): { wireBaseId: string; effort: string } | undefined { + const parsed = parseCursorVariantId(pickedId); +- if (!parsed.known || parsed.kind !== "fast") return undefined; ++ const kind = fast === true ? upgradeToFast(parsed.baseId, parsed.kind) : parsed.kind; ++ if (!parsed.known || kind !== "fast") return undefined; +``` + +`createCursorRequest` derives the flag from the tier decision: + +```diff +- const model = normalizeCursorModelId(parsed.modelId, parsed.options.reasoning); ++ // decideTier already applied fastMode / caller-tier precedence; a {kind:"set"} decision ++ // on this route means canonical Fast survived the policy gate. ++ const fastRequested = parsed.options.tierDecision?.kind === "set"; ++ const model = normalizeCursorModelId(parsed.modelId, parsed.options.reasoning, fastRequested); +``` + +Reading `tierDecision` rather than `serviceTier` keeps one decision authority: config +`fastMode: false` produces `{kind:"drop"}` and correctly suppresses the upgrade even when +the caller asked. + +## 4. Telemetry stops lying + +```diff +- if (adapter.runTurn && !adapter.tierLogForRunTurn) { ...(..., null, null) } +``` + +The generic fallback in `adapters/registry.ts` stays for other adapters. Cursor sets its +own in `src/adapters/cursor.ts`: + +```diff ++ // Cursor emits Fast as a variant, so the wire fact is the resolved variant, not a field. ++ adapter.tierLogForRunTurn = parsed => { ++ const request = createCursorRequest(parsed); ++ const emittedFast = request.modelId.endsWith("-fast") ++ || (request.requestedModelParameters ?? []).some(p => p.id === "fast" && p.value === "true"); ++ return createAdapterTierMetadata( ++ parsed.options.tierObservation, ++ parsed.options.tierDecision, ++ emittedFast ? "cursor-variant" : null, ++ emittedFast ? "fast" : null, ++ ); ++ }; +``` + +**Rebuilding the request is NOT allowed (audit B7, blocker).** `createCursorRequest` is not +a pure function: `resolveCursorConversationId` (`request-builder.ts:320-335`) calls +`generatedCursorConversationId()` on three of its four branches, so a second call mints a +DIFFERENT conversation id, and `resolveCursorCheckpoint` (`request-builder.ts:479`) +consults checkpoint state. A telemetry-only rebuild would fabricate a conversation that was +never sent and could disturb checkpoint bookkeeping. + +So the wire fact must come from the request the adapter already built. The Cursor adapter +holds it at `src/adapters/cursor.ts:148` (`let request = createCursorRequest(_parsed)`); +`tierLogForRunTurn` reads that value instead of building its own: + +```diff ++ // Cursor emits Fast as a variant, so the wire fact is the variant that was actually ++ // sent. createCursorRequest is NOT pure (it mints conversation ids), so this reads the ++ // request the run already built rather than rebuilding one. ++ const emittedFast = (sent: CursorRunRequest) => sent.modelId.endsWith("-fast") ++ || (sent.requestedModelParameters ?? []).some(p => p.id === "fast" && p.value === "true"); +``` + +`tierLogForRunTurn` runs BEFORE `runTurn`, not after (reviewer finding 3, verified): + +```ts +// src/server/responses/core.ts:3477-3479 +let runTurnAdapter = adapter; +if (adapter.runTurn) { + recordAdapterTierMetadata(logCtx, adapter.tierLogForRunTurn?.(parsed)); +} +``` + +That kills both candidate designs. A write-back flag set inside `runTurn` is read before it +is written. A rebuild inside `tierLogForRunTurn` runs before `_cursorIdentityScope` +(`cursor.ts:134-146`) and `_cursorConversationId` (`cursor.ts:160`) exist, so it mints a +second `crypto.randomUUID()` conversation and hashes a `local` scope instead of the token +scope. Neither reports the request that was actually sent. + +What IS pure and available at that moment is the variant resolution itself — it reads only +`parsed.modelId`, `parsed.options.reasoning`, and `parsed.options.tierDecision`. So the +telemetry recomputes the VARIANT, not the request: + +```diff ++ // Fast is a variant here, so the wire fact is which variant the resolver will pick. ++ // tierLogForRunTurn runs BEFORE runTurn (core.ts:3479), and createCursorRequest is not ++ // pure (it mints conversation ids), so this must not rebuild the request. Variant ++ // resolution is pure and reads the same three inputs the builder will read. ++ adapter.tierLogForRunTurn = parsed => { ++ const fast = cursorRequestEmitsFastVariant(parsed); ++ return createAdapterTierMetadata( ++ parsed.options.tierObservation, ++ parsed.options.tierDecision, ++ fast ? "cursor-variant" : null, ++ fast ? "fast" : null, ++ ); ++ }; +``` + +`cursorRequestEmitsFastVariant(parsed)` is a new exported helper in `request-builder.ts` +that shares `normalizeCursorModelId`'s exact inputs and returns whether the resolved wire +carries the fast dimension. Sharing the function is what keeps telemetry and the wire from +drifting; a B-phase test asserts they agree for every fast-capable base. + + +Cursor's response carries no tier echo, so `confirmation` stays `"assumed"` — the +`responseTierAuthoritative: false` path. Do not claim `"confirmed"`. + +## 5. Field chain + +`FastWire.kind` gains a value; every stage: + +| Stage | Location | +|---|---| +| creation | `registry.ts` cursor entry; `config.ts` `fastWireSchema` accepts the literal | +| serialization | `cloneFastWire` — kind-agnostic spread, no change | +| deserialization | `fastWireSchema` enum must list `"cursor-variant"` or config load rejects it | +| consumers | `FAST_WIRE_ADAPTERS` (exhaustive Record — a missing key is a type error), `AttemptTierOutcome.wireKind`, `canonicalFromWire`, `behavior.ts` fingerprint, `fastWireDeclarationError` | + +`FAST_WIRE_ADAPTERS` being a `Record` means the compiler finds THAT +consumer. It does NOT find string-comparison consumers, and there is one (audit B4): + +```ts +// src/usage/cost.ts:418-425 +if (outcome.fastOutcome === "unknown" && outcome.wireKind === "service-tier" && ...) { + return { requestedServiceTier: outcome.wireValue }; +} +``` + +A `"cursor-variant"` outcome falls through that branch. That is CORRECT for us, because an +applied Cursor Fast sets `canonical: "priority"` with `confirmation: "assumed"` and is +caught one branch earlier (`cost.ts:414`). Correct by accident is not proven, so wp3 +asserts cost attribution explicitly instead of leaving it to a future refactor. + +`fastWireDeclarationError` has NO adapter allowlist — it validates shape only +(`fastwire.ts:458-490`) — and `hasFastWireCapabilityConflict` fires only when +`fastWire === null` (`fastwire.ts:450`), which does not apply here. + +**`src/usage/log.ts` discards the whole outcome (audit B8, blocker — reviewer round 2).** +`normalizeAttemptTierOutcome` allowlists `wireKind` at two sites: + +```ts +// src/usage/log.ts:322-325 — validation +if ("wireKind" in outcome && outcome.wireKind !== null + && outcome.wireKind !== "service-tier" + && outcome.wireKind !== "anthropic-speed") return null; // drops the ENTIRE row +// src/usage/log.ts:340 — projection repeats the same three-way test +``` + +A `"cursor-variant"` outcome returns `null`, so the persisted attempt loses its tier row and +the GUI Logs view shows nothing after a restart. Worse than the `cost.ts` fall-through: +silent total loss, invisible to `tsc` because both are string comparisons. Both sites must +accept the new kind in the same commit. + +**`tests/fastwire-policy.test.ts:647` goes red by design (audit B9).** + +```ts +test("A1 adds no explicit registry FastWire declaration", () => { + expect(PROVIDER_REGISTRY.every(entry => entry.fastWire === undefined)).toBeTrue(); +}); +``` + +It encodes "A1 shipped no registry fastWire", which this work-phase deliberately ends. +Rewrite it to assert the new invariant — cursor is the only entry carrying a declaration and +its kind is `cursor-variant` — rather than deleting the coverage. + +## 6. Bypass record (PLAN-BYPASS-NAMED-01) + +- Tier: E2 (type-level exhaustiveness + tests). +- Executing surface: `tsc` for the kind Record; `bun test` for behavior. Note `tsc` does + NOT cover string-comparison consumers such as `usage/cost.ts:422` (audit B4). +- Known bypass: an operator can set `providers.cursor.supportsServiceTier: true`, which + advertises Fast on all 34 bases; 29 would then resolve with no fast variant and silently + send the ordinary wire id. +- Residual risk: a dead toggle on operator-misconfigured installs. +- Wording: this is an early warning, not enforcement. Final enforcement layer: none. + +The upgrade is a no-op when the variant is absent (`upgradeToFast` returns the input kind), +so the misconfiguration degrades to today's behavior rather than an error. + +## 7. Existing tests that constrain this change + +`tests/codex-catalog.test.ts:2923-2947` asserts routed entries carry NO +`service_tiers`/`additional_speed_tiers`, and `:2959-2973` asserts a routed row DOES get +them when the model declares `supportsServiceTier: true`. Both stay valid: the first uses +providers that declare no capability, the second is the shape Cursor now joins. Check during +B whether either fixture uses `provider: "cursor"`; if so, update it to assert the new +per-base behavior rather than the blanket absence. + +**Both Grok call sites change atomically (audit B13).** `request-builder.ts:204` calls +`cursorGrokFastSelection(id, reasoning)` with no third argument today. If only +`resolveCursorSelection` learns the flag, a toggled Grok pick emits a flattened +`grok-4.6-high-fast` instead of the required `{id:"fast",value:"true"}` parameters — +violating this phase's own accept row. The helper signature, `normalizeCursorModelId`, and +that call site are one edit, and the Grok accept-row test belongs to wp3, not wp4. diff --git a/devlog/_plan/260902_cursor_unified_identity/030_wp4_global_fast_switch.md b/devlog/_plan/260902_cursor_unified_identity/030_wp4_global_fast_switch.md new file mode 100644 index 0000000000..99e2b98a51 --- /dev/null +++ b/devlog/_plan/260902_cursor_unified_identity/030_wp4_global_fast_switch.md @@ -0,0 +1,224 @@ +# WP4 / PR3 — `fastMode` exposes `-fast` identities outside Codex + +Stacked on PR2. Scope IN: `src/claude/model-info.ts`, `src/server/index.ts` (`/v1/models` +branches only), `src/server/management/agent-settings-routes.ts` (aliases only), +`src/adapters/cursor/request-builder.ts`, docs-site EN reference, tests. +Scope OUT: dashboard `/api/models` `namespaced` ids, Desktop 3P hashed aliases, +`ocx models` static output. + +## The asymmetry this closes + +Codex has a Fast toggle, so its rows stay umbrella rows and the toggle picks the dimension +(WP3). Claude Code, Pi, and other OpenAI-compatible clients have no toggle — they can only +pick a listed id. With `fastMode: true`, those surfaces list the fast identity instead. + +``` +config.fastMode = true + ├─ Codex catalog ....... unchanged umbrella rows + service_tiers (WP3) + ├─ Claude Code list .... claude-ocx-cursor--claude-opus-5-fast + ├─ OpenAI /v1/models ... cursor/claude-opus-5-fast + ├─ dashboard ........... unchanged (namespaced is the disable key) + └─ request path ........ umbrella pick promotes to fast anyway +``` + +The last row is what makes an already-persisted client config behave consistently: a +Claude Code `settings.json` still naming the umbrella id gets fast treatment without +rediscovery. + +## Change map + +| File | Action | +|---|---| +| `src/adapters/cursor/catalog.ts` | MODIFY — export `cursorFastIdFor(baseId)` | +| `src/claude/model-info.ts` | MODIFY — `buildAnthropicModelInfos` takes `fastCursorBases` | +| `src/server/index.ts` | MODIFY — both list branches pass the fast id set | +| `src/server/management/agent-settings-routes.ts` | MODIFY — `aliases` follow the same rule | +| `src/adapters/cursor/request-builder.ts` | MODIFY — `fastMode` promotion fallback | +| `tests/cursor-fast-listing.test.ts` | NEW | +| `docs-site/src/content/docs/reference/configuration/providers.md` | MODIFY — brief `fastMode` note | + +## 1. One id-composition helper + +```diff ++/** ++ * The listed id for a base when the global fast switch is on. Returns undefined when the ++ * base has no fast dimension, so a caller cannot invent an unroutable id. ++ * ++ * Composed from the base's defaultVariant, NOT a bare \`-fast\` suffix (audit B11): the ++ * umbrella row for a Claude base routes THINKING, so \`claude-opus-5-fast\` would parse back ++ * as the regular-fast sibling — a different wire from what the Codex toggle sends, and for ++ * claude-opus-5 a QUARANTINED one. The listed id must round-trip to the same variant ++ * \`upgradeToFast\` picks. ++ */ ++export function cursorFastIdFor(baseId: string): string | undefined { ++ const capability = CURSOR_CAPABILITIES[baseId]; ++ if (!capability) return undefined; ++ const kind = upgradeToFast(baseId, capability.defaultVariant); ++ if (kind !== "fast" && kind !== "thinkingFast") return undefined; ++ return kind === "thinkingFast" ? \`\${baseId}-thinking-fast\` : \`\${baseId}-fast\`; ++} +``` + +Round-trip for the five fast-capable bases, to be re-measured at wp4 P: + +| base | defaultVariant | listed id | parses back to | +|---|---|---|---| +| `claude-opus-4-7` | thinking | `claude-opus-4-7-thinking-fast` | thinkingFast | +| `claude-opus-4-8` | thinking | `claude-opus-4-8-thinking-fast` | thinkingFast | +| `claude-opus-5` | thinking | `claude-opus-5-thinking-fast` | thinkingFast | +| `grok-4.5` | regular | `grok-4.5-fast` | fast | +| `grok-4.6` | regular | `grok-4.6-fast` | fast | + +`parseCursorVariantId` handles both spellings: the `-fast` strip runs before the thinking +grammar (`catalog.ts:360-371`), so `claude-opus-5-thinking-fast` lands on `thinkingFast`. +WP4's equivalence test asserts the listed id and the toggled umbrella id resolve to the SAME +wire id for every base in that table — the guard that keeps the two surfaces from drifting. + +## 2. Claude Code discovery + +`buildAnthropicModelInfos` already knows how to publish a dimension as a row +(`push1mVariant`). Fast is a *replacement*, not an addition: the point is that the client's +only pick is the fast one. + +```diff + export function buildAnthropicModelInfos( + nativeSlugs: readonly string[], + routedModels: readonly CatalogModel[], + auto: AutoContextMode = AUTO_CONTEXT_OFF, + idStyle: AnthropicIdStyle = "desktop3p", + aliasForRoute: (provider: string, modelId: string) => string = desktop3pAlias, + nativeContextCap?: NativeContextLimitsInput, ++ fastMode?: boolean, + ): AnthropicModelInfo[] { +``` + +```diff + for (const m of routedModels) { +- const id = idStyle === "readable" ? claudeCodeAlias(m.provider, m.id) : aliasForRoute(m.provider, m.id); ++ // Global Fast has no toggle on this surface, so the fast identity is what gets listed. ++ // Desktop 3P ids are hashed from the model name, so changing them would strand a saved ++ // picker selection — the rewrite is limited to the readable CLI style. ++ const fastId = fastMode === true && m.provider === "cursor" && idStyle === "readable" ++ ? cursorFastIdFor(m.id) ++ : undefined; ++ const modelId = fastId ?? m.id; ++ const id = idStyle === "readable" ? claudeCodeAlias(m.provider, modelId) : aliasForRoute(m.provider, m.id); +``` + +The `display_name` follows `modelId` so the picker reads `claude-opus-5-fast (cursor)`. +`push1mVariant` keeps using the same base info, so a 1M base still gets its `[1m]` row and +the two dimensions compose as `...-fast[1m]` — consistent with the existing marker rule +that `[1m]` is a suffix on whatever id precedes it. + +Desktop 3P is deliberately excluded: `desktop3pAlias` hashes the model name, so a rewrite +would change every hash and strand saved selections. The objective says not to touch it. + +Call site: + +```diff +- const data = buildAnthropicModelInfos(desktopNativeSlugs, goOrdered, resolveAutoContext(config.claudeCode), idStyle, activeDesktop3pAlias, nativeContextLimits(config)); ++ const data = buildAnthropicModelInfos(desktopNativeSlugs, goOrdered, resolveAutoContext(config.claudeCode), idStyle, activeDesktop3pAlias, nativeContextLimits(config), config.fastMode); +``` + +## 3. OpenAI-compatible list + +```diff + ...await Promise.all(uniqueCatalogModelsForRawPublicList(goOrdered).map(async m => { +- const publicId = m.alias ?? \`\${m.provider}/\${m.id}\`; ++ // Same rule as the anthropic branch: with the global fast switch on, a ++ // toggle-less client is offered the fast identity directly. ++ const fastModelId = config.fastMode === true && m.provider === "cursor" ++ ? cursorFastIdFor(m.id) ++ : undefined; ++ const publicId = m.alias ?? \`\${m.provider}/\${fastModelId ?? m.id}\`; +``` + +`m.alias` wins when an operator set one — an explicit alias is a user decision and the +switch does not override it. + +`grokEffortFields` keeps reading `m.reasoningEfforts`, which is correct: the fast variant's +ladder can be shorter (`claude-opus-5-fast` stops at `high`), and advertising the base +ladder there would let a client request `max` on a fast id. Record this as a known residual +in the PR description; tightening it means threading the variant spec into the listing, +which is a follow-up rather than part of this slice. + +## 4. Dashboard aliases + +`GET /api/claude-code` builds `aliases` with the same `claudeCodeAlias` helper, so it uses +the identical rule to stay consistent with what Claude Code will actually discover. Its +`available` list (`provider/id`) and the Models tab `namespaced` id stay untouched, because +those are keys for `disabledModels` and export. + +## 5. Request-time promotion + +Listing alone leaves persisted client configs on the umbrella id. WP3 already promotes when +`decideTier` returns `{kind:"set"}`, and `fastMode: true` produces exactly that on an +eligible route. + +**The planned `tierDecision === undefined` fallback is dropped (audit B6).** It was written +for "inbound paths that never build a tier decision", and that state does not exist for +Cursor: `src/server/claude-messages.ts:37,772` converts an anthropic request into a +Responses body and replays it through `handleResponses`, which is the same function that +runs `decideTier` at `responses/core.ts:2095`. Chat-native calls it directly +(`chat-native.ts:192`). A branch guarded on `tierDecision === undefined` would be +unreachable by construction — exactly the dead conditional +C-ACTIVATION-GROUNDING-01 forbids planning. + +So PR3 adds no request-path code. Instead it adds the activation evidence that PR2's +promotion really fires on the non-Codex route: + +```ts +test("anthropic-inbound reaches the cursor resolver with a set tier decision", async () => { + // fastMode: true, model claude-ocx-cursor--claude-opus-5, no service_tier in the body + const request = await captureCursorRequestVia(claudeMessagesHandler, { fastMode: true }); + expect(request.modelId).toMatch(/-fast$/); +}); +``` + +If that test goes red, the correct fix is in the shared `decideTier` path, not a +Cursor-local fallback. + +## 6. Tests (activation-grounded) + +`tests/cursor-fast-listing.test.ts`: + +```ts +test("fastMode off lists the umbrella id", () => { + const rows = buildAnthropicModelInfos([], [cursorModel("claude-opus-5")], AUTO_CONTEXT_OFF, "readable", desktop3pAlias, undefined, false); + expect(rows.map(r => r.id)).toContain("claude-ocx-cursor--claude-opus-5"); +}); + +test("fastMode on lists the fast identity for a fast-capable base", () => { + const rows = buildAnthropicModelInfos([], [cursorModel("claude-opus-5")], AUTO_CONTEXT_OFF, "readable", desktop3pAlias, undefined, true); + expect(rows.map(r => r.id)).toContain("claude-ocx-cursor--claude-opus-5-fast"); +}); + +test("fastMode on leaves a base without a fast variant alone", () => { + const rows = buildAnthropicModelInfos([], [cursorModel("kimi-k3")], AUTO_CONTEXT_OFF, "readable", desktop3pAlias, undefined, true); + expect(rows.map(r => r.id)).toContain("claude-ocx-cursor--kimi-k3"); +}); + +test("the listed fast id still routes", () => { + const request = createCursorRequest(parsedFor("cursor/claude-opus-5-fast", "high")); + expect(request.modelId).toBe("claude-opus-5-high-fast"); +}); + +test("desktop3p hashed aliases are untouched by the switch", () => { ... }); +``` + +The third and fifth tests are the guards that make the first two safe: they prove the +rewrite is scoped to fast-capable bases and to the readable id style. + +## 7. Docs + +One short subsection under the providers reference: what `fastMode` does per surface, that +Codex keeps its toggle, and that only bases with a fast variant are affected. Brief, per +the user's instruction on documentation. + +## 8. Residual risks + +- Effort ladders on a listed fast id advertise the base ladder (§3). Known, documented. +- A client caching the old id keeps working — the umbrella id never stops routing. +- `fastMode` now means both "OpenAI priority tier" and "Cursor fast variant". That is a + deliberate overload of one user-facing intent ("go faster"), recorded here so a future + reader does not mistake it for an accident. diff --git a/devlog/_plan/260902_cursor_unified_identity/040_residuals.md b/devlog/_plan/260902_cursor_unified_identity/040_residuals.md new file mode 100644 index 0000000000..08804bb39a --- /dev/null +++ b/devlog/_plan/260902_cursor_unified_identity/040_residuals.md @@ -0,0 +1,47 @@ +# Residuals + +Known-and-accepted gaps, parked here rather than left in prose (audit B14). Each says what +is wrong, why it was not fixed in its cycle, and what evidence would change the decision. + +## R1 — effort ladders on a listed fast id (wp4) + +`/v1/models` stamps `grokEffortFields(m.reasoningEfforts, …)` from the BASE row, but a fast +variant's ladder can be shorter: `claude-opus-5` runs to `max` while its `fast` spec stops +at `high` (`catalog.ts` CURSOR_CAPABILITIES). With `fastMode: true` a client could therefore +request `max` against a listed `-fast` id. + +Not fixed in wp4 because the resolver clamps: `cursorVariantEffort` picks the top rung the +variant actually declares, so an over-request degrades to `high` rather than failing. The +cost is an advertised rung that silently clamps, not a broken request. + +Fix when: a user reports an effort selection that appears to do nothing on a fast id. The +change is to thread the resolved variant spec into the listing branch instead of reading the +base row's ladder. + +## R2 — `claude-4-sonnet-1m` stays a separate row (wp2) + +It is a real upstream wire id, not `claude-4-sonnet` + ultra, and `claude-4-sonnet` carries +no `maxModeVerified` evidence — folding it would invent a capability. So "1M" still means two +things in the picker: a synthetic ultra marker for `kimi-k3`, and this genuine second row. + +Fix when: live `GetUsableModels` proves `claude-4-sonnet` supports Max Mode, at which point +the row folds into the base the same way `kimi-k3-1m` did. + +## R3 — `fastMode` carries two meanings (wp4) + +One flag drives OpenAI's `service_tier: "priority"` and Cursor's fast VARIANT. These are +different products with different ladders. The overload is deliberate — both express "go +faster" — and is recorded so a later reader does not read it as an accident. + +Fix when: a user needs one on without the other. That is a second flag, not a re-interpretation of this one. + +## R4 — pre-existing red outside this unit + +`bun run test:changed` at `42731a4be` reports 14461 pass / 5 fail. All five reproduce on a +clean stash of this branch, so none is caused by this unit: + +- `tests/cli-capabilities.test.ts` — "every management route is capability-covered" +- `tests/…` CL-07 task effectiveness producer (4 tests) + +Not this unit's to fix. Recorded so a later cycle does not mistake them for a regression it +introduced. diff --git a/src/adapters/cursor/catalog.ts b/src/adapters/cursor/catalog.ts index f753f24e74..3351deab16 100644 --- a/src/adapters/cursor/catalog.ts +++ b/src/adapters/cursor/catalog.ts @@ -32,6 +32,12 @@ export interface CursorCapability { readonly variants: Partial>; /** Which variant the umbrella picker row selects (thinking merges into the base). */ readonly defaultVariant: CursorVariantKind; + /** + * Human picker label, in Cursor's own spelling. Codex would otherwise show the raw + * routed slug (`cursor/kimi-k3`), because `routedDisplayName` passes it through + * unchanged for every provider (codex/catalog/sync.ts). + */ + readonly displayName: string; /** Context-window metadata (display/routing only — never implies maxMode). */ readonly window: number; /** Max Mode proven on the wire for this base (static evidence; live maxModeModels unions in). */ @@ -46,6 +52,8 @@ const CONTEXT_256K = 256 * K; const CONTEXT_272K = 272 * K; const CONTEXT_500K = 500 * K; const CONTEXT_1M = 1_000 * K; +/** Gemini publishes the exact power-of-two window, not a rounded 1M. */ +const CONTEXT_GEMINI = 1_048_576; const FULL = ["low", "medium", "high", "xhigh", "max"] as const; const T = "thinking-then-effort" as const; @@ -59,6 +67,7 @@ const E = "effort-then-thinking" as const; */ export const CURSOR_CAPABILITIES: Record = { "claude-4.5-opus": { + displayName: "Claude Opus 4.5", window: CONTEXT_200K, defaultVariant: "thinking", variants: { @@ -67,6 +76,7 @@ export const CURSOR_CAPABILITIES: Record = { }, }, "claude-4.6-opus": { + displayName: "Claude Opus 4.6", window: CONTEXT_1M, defaultVariant: "thinking", variants: { @@ -75,6 +85,7 @@ export const CURSOR_CAPABILITIES: Record = { }, }, "claude-4.6-sonnet": { + displayName: "Claude Sonnet 4.6", window: CONTEXT_1M, defaultVariant: "thinking", variants: { @@ -83,6 +94,7 @@ export const CURSOR_CAPABILITIES: Record = { }, }, "claude-4.5-sonnet": { + displayName: "Claude Sonnet 4.5", window: CONTEXT_200K, defaultVariant: "thinking", variants: { @@ -91,6 +103,7 @@ export const CURSOR_CAPABILITIES: Record = { }, }, "claude-4-sonnet": { + displayName: "Claude Sonnet 4", window: CONTEXT_200K, defaultVariant: "thinking", variants: { @@ -99,6 +112,7 @@ export const CURSOR_CAPABILITIES: Record = { }, }, "claude-fable-5": { + displayName: "Claude Fable 5", window: CONTEXT_1M, defaultVariant: "thinking", variants: { @@ -113,6 +127,7 @@ export const CURSOR_CAPABILITIES: Record = { // the live GetUsableModels filter drops whichever the roster does not expose. Collapse to // the one real spelling once it is observed. "claude-fable-5-1": { + displayName: "Claude Fable 5.1", window: CONTEXT_1M, defaultVariant: "thinking", variants: { @@ -121,6 +136,7 @@ export const CURSOR_CAPABILITIES: Record = { }, }, "claude-fable-5.1": { + displayName: "Claude Fable 5.1", window: CONTEXT_1M, defaultVariant: "thinking", variants: { @@ -129,6 +145,7 @@ export const CURSOR_CAPABILITIES: Record = { }, }, "claude-5.1-fable": { + displayName: "Claude Fable 5.1", window: CONTEXT_1M, defaultVariant: "thinking", variants: { @@ -137,6 +154,7 @@ export const CURSOR_CAPABILITIES: Record = { }, }, "claude-sonnet-5": { + displayName: "Claude Sonnet 5", window: CONTEXT_1M, defaultVariant: "thinking", variants: { @@ -145,6 +163,7 @@ export const CURSOR_CAPABILITIES: Record = { }, }, "claude-opus-4-7": { + displayName: "Claude Opus 4.7", window: CONTEXT_1M, defaultVariant: "thinking", variants: { @@ -155,6 +174,7 @@ export const CURSOR_CAPABILITIES: Record = { }, }, "claude-opus-4-8": { + displayName: "Claude Opus 4.8", window: CONTEXT_1M, defaultVariant: "thinking", variants: { @@ -165,6 +185,7 @@ export const CURSOR_CAPABILITIES: Record = { }, }, "claude-opus-5": { + displayName: "Claude Opus 5", window: CONTEXT_1M, defaultVariant: "thinking", variants: { @@ -177,32 +198,38 @@ export const CURSOR_CAPABILITIES: Record = { }, }, "glm-5.2": { + displayName: "GLM 5.2", window: CONTEXT_1M, defaultVariant: "regular", variants: { regular: { levels: ["high", "max"] } }, }, "glm-5.3": { + displayName: "GLM 5.3", window: CONTEXT_1M, defaultVariant: "regular", variants: { regular: { levels: ["low", "high", "max"] } }, }, "gemini-3.6-flash": { - window: CONTEXT_1M, + displayName: "Gemini 3.6 Flash", + window: CONTEXT_GEMINI, defaultVariant: "regular", variants: { regular: { levels: ["minimal", "low", "medium", "high"] } }, }, "gemini-3.7-flash": { - window: CONTEXT_1M, + displayName: "Gemini 3.7 Flash", + window: CONTEXT_GEMINI, defaultVariant: "regular", variants: { regular: { levels: ["low", "medium", "high"] } }, }, "kimi-k3": { + displayName: "Kimi K3", window: CONTEXT_1M, defaultVariant: "regular", maxModeVerified: true, variants: { regular: { levels: ["low", "high", "max"] } }, }, "grok-4.5": { + displayName: "Cursor Grok 4.5", window: CONTEXT_500K, defaultVariant: "regular", wirePrefix: "cursor-", @@ -212,6 +239,7 @@ export const CURSOR_CAPABILITIES: Record = { }, }, "grok-4.6": { + displayName: "Cursor Grok 4.6", window: CONTEXT_500K, defaultVariant: "regular", wirePrefix: "cursor-", @@ -221,71 +249,88 @@ export const CURSOR_CAPABILITIES: Record = { }, }, "gpt-5.1": { + displayName: "GPT-5.1", window: CONTEXT_272K, defaultVariant: "regular", variants: { regular: { levels: ["low", "high"] } }, }, "gpt-5.1-codex-max": { + displayName: "GPT-5.1 Codex Max", window: CONTEXT_272K, defaultVariant: "regular", variants: { regular: { levels: ["low", "medium", "high", "xhigh"] } }, }, "gpt-5.1-codex-mini": { + displayName: "GPT-5.1 Codex Mini", window: CONTEXT_272K, defaultVariant: "regular", variants: { regular: { levels: ["low", "high"] } }, }, "gpt-5.2": { + displayName: "GPT-5.2", window: CONTEXT_272K, defaultVariant: "regular", variants: { regular: { levels: ["low", "high", "xhigh"] } }, }, "gpt-5.2-codex": { + displayName: "GPT-5.2 Codex", window: CONTEXT_272K, defaultVariant: "regular", variants: { regular: { levels: ["low", "high", "xhigh"] } }, }, "gpt-5.3-codex": { + displayName: "Codex 5.3", window: CONTEXT_272K, defaultVariant: "regular", variants: { regular: { levels: ["low", "high", "xhigh"] } }, }, "gpt-5.4": { + displayName: "GPT-5.4", window: CONTEXT_272K, defaultVariant: "regular", variants: { regular: { levels: ["low", "medium", "high", "xhigh"] } }, }, "gpt-5.4-mini": { + displayName: "GPT-5.4 Mini", window: CONTEXT_272K, defaultVariant: "regular", variants: { regular: { levels: ["low", "medium", "high", "xhigh"] } }, }, "gpt-5.4-nano": { + displayName: "GPT-5.4 Nano", window: CONTEXT_272K, defaultVariant: "regular", variants: { regular: { levels: ["low", "medium", "high", "xhigh"] } }, }, "gpt-5.5": { + displayName: "GPT-5.5", window: CONTEXT_272K, defaultVariant: "regular", variants: { regular: { levels: ["low", "medium", "high"] } }, }, "gpt-5.5-extra": { - window: CONTEXT_272K, + displayName: "GPT-5.5 Extra", + // Live GetUsableModels reports 200K for this row, not the gpt-5 family's 272K + // (account-verified 260709). The seed carried the measured number; the capability + // table was approximating from the family. + window: CONTEXT_200K, defaultVariant: "regular", variants: { regular: { levels: ["high"] } }, }, "gpt-5.6-sol": { + displayName: "GPT-5.6 Sol", window: CONTEXT_1M, defaultVariant: "regular", variants: { regular: { levels: FULL } }, }, "gpt-5.6-terra": { + displayName: "GPT-5.6 Terra", window: CONTEXT_1M, defaultVariant: "regular", variants: { regular: { levels: FULL } }, }, "gpt-5.6-luna": { + displayName: "GPT-5.6 Luna", window: CONTEXT_1M, defaultVariant: "regular", variants: { regular: { levels: FULL } }, @@ -523,6 +568,7 @@ export function liveCursorMaxModeBasesForTests(): ReadonlySet { export interface CursorUmbrellaRow { readonly id: string; + readonly displayName: string; readonly efforts: readonly string[]; readonly window: number; /** Max Mode evidence present: the ultra rung maps to maxMode on the wire. */ @@ -562,6 +608,7 @@ export function cursorUmbrellaRows(): CursorUmbrellaRow[] { if (!spec || spec.quarantined) continue; rows.push({ id: baseId, + displayName: capability.displayName, efforts: spec.levels, window: capability.window, maxModeVerified: capability.maxModeVerified === true, diff --git a/src/adapters/cursor/discovery.ts b/src/adapters/cursor/discovery.ts index 93819289e7..faf9214c8f 100644 --- a/src/adapters/cursor/discovery.ts +++ b/src/adapters/cursor/discovery.ts @@ -5,7 +5,7 @@ import { cursorWireModelIdWithEffort, CURSOR_THINKING_MODEL_IDS, } from "./effort-map"; -import { parseCursorVariantId } from "./catalog"; +import { cursorUmbrellaRows, parseCursorVariantId } from "./catalog"; export interface CursorModelInfo { id: string; @@ -262,101 +262,86 @@ export function filterCursorConfiguredModelsByLiveDiscovery = new Set([]); +/** + * Cursor products that are NOT a dimension of any capability base. Each carries its own + * label because there is no capability record to read one from. A row belongs here only + * when Cursor ships it as a distinct product; a variant of a cataloged base does not. + */ +export const CURSOR_PRODUCT_MODELS: readonly (CursorModelInfo & { displayName: string })[] = [ + { id: "claude-4.5-haiku", displayName: "Claude Haiku 4.5", contextWindow: CONTEXT_200K }, + { id: "composer-1", displayName: "Composer 1", contextWindow: CONTEXT_200K }, + { id: "composer-2.5", displayName: "Composer 2.5", contextWindow: CONTEXT_200K }, + { id: "gemini-2.5-flash", displayName: "Gemini 2.5 Flash", contextWindow: CONTEXT_GEMINI }, + { id: "gemini-3-flash", displayName: "Gemini 3 Flash", contextWindow: CONTEXT_GEMINI }, + { id: "gemini-3-pro", displayName: "Gemini 3 Pro", contextWindow: CONTEXT_GEMINI }, + { id: "gemini-3-pro-image-preview", displayName: "Gemini 3 Pro Image", contextWindow: CONTEXT_200K }, + { id: "gemini-3.1-pro", displayName: "Gemini 3.1 Pro", contextWindow: CONTEXT_GEMINI }, + { id: "gemini-3.5-flash", displayName: "Gemini 3.5 Flash", contextWindow: CONTEXT_200K }, + { id: "gpt-5-codex", displayName: "GPT-5 Codex", contextWindow: CONTEXT_272K }, + { id: "gpt-5-mini", displayName: "GPT-5 Mini", contextWindow: CONTEXT_272K }, + { id: "gpt-5.1-codex", displayName: "GPT-5.1 Codex", contextWindow: CONTEXT_272K }, + { id: "kimi-k2.7-code", displayName: "Kimi K2.7 Code", contextWindow: CONTEXT_262K }, +]; + +/** + * Real upstream wire ids that LOOK like a dimension of a cataloged base but are served as + * their own catalog row by Cursor, so they stay rows rather than folding into a base. + * + * - `claude-4-sonnet-1m`: a distinct 1M-window row upstream, not `claude-4-sonnet` + ultra. + * claude-4-sonnet carries no maxMode evidence, so folding it would invent a capability. + * `REAL_1M_WIRE_IDS` in catalog.ts already stops the parser reading it as the synthetic + * marker. + * - `gpt-5-fast`: there is no `gpt-5` capability base for it to be a dimension of. + * - `composer-2.5-fast`: composer-2.5 has no effort or variant dimensions at all. + */ +export const CURSOR_REAL_ID_EXCEPTIONS: readonly (CursorModelInfo & { displayName: string })[] = [ + { id: "claude-4-sonnet-1m", displayName: "Claude Sonnet 4 (1M)", contextWindow: CONTEXT_1M }, + { id: "gpt-5-fast", displayName: "GPT-5 Fast", contextWindow: CONTEXT_272K }, + { id: "composer-2.5-fast", displayName: "Composer 2.5 Fast", contextWindow: CONTEXT_200K }, +]; + +/** Picker labels for the auto-router rows, which have no capability record. */ +const CURSOR_ROUTER_DISPLAY_NAMES: Readonly> = { + auto: "Auto", + "auto-cost": "Auto (Cost)", + "auto-balance": "Auto (Balanced)", + "auto-intelligence": "Auto (Intelligence)", +}; + +/** + * The published Cursor row set. DERIVED from CURSOR_CAPABILITIES via cursorUmbrellaRows() + * (devlog 260902_cursor_unified_identity) so the capability table and the picker can no + * longer disagree: one row per base, with thinking / fast / synthetic -1m remaining + * routable aliases that add no rows. + * + * Before this, the seed was a hand-maintained list that drifted from the capability table — + * `cursorUmbrellaRows()` existed but only tests called it, so collapsing a variant changed + * routing without changing what Codex listed. + * + * Windows and effort ladders come from the capability record; the two lists below carry the + * ids that have no capability record, each with its own label and window. + */ export const CURSOR_STATIC_MODELS: readonly CursorModelInfo[] = normalizeCursorModels([ - // Context windows and the model lineup mirror Cursor's public models/pricing docs plus the jawcode - // SOT (../jawcode/packages/ai/src/models.json, `cursor` provider), which mirrors the real - // GetUsableModels catalog. Live discovery is the preferred path when logged in; these ids seed the - // routed Codex catalog and provide a static fallback. Cursor base ids carry no effort suffix here — - // the request builder appends the per-model suffix (see effort-map.ts) and reasoning models - // advertise effort so Codex exposes the tier picker. `supportsReasoningEffort` tracks whether the - // model has *selectable effort tiers* (CURSOR_MODEL_EFFORT_TIERS), NOT merely whether it reasons: - // gemini/grok/kimi-k2.7/gpt-5-mini are reasoning models in the SOT but are sent bare (no tier picker). ...CURSOR_ROUTER_MODEL_IDS.map(id => ({ id, contextWindow: CONTEXT_200K, supportsReasoningEffort: false })), - - // Umbrella seed (devlog 260828_cursor_umbrella_catalog): one row per BASE - // model. Thinking merges into the base (the resolver routes the thinking - // variant); fast / thinking-fast / -1m stay routable as aliases but add no - // rows. Windows follow CURSOR_CAPABILITIES where the base is cataloged. - { id: "claude-sonnet-5", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, - { id: "claude-4-sonnet", contextWindow: CONTEXT_200K }, - { id: "claude-4-sonnet-1m", contextWindow: CONTEXT_1M }, - { id: "claude-4.5-haiku", contextWindow: CONTEXT_200K }, - { id: "claude-4.5-sonnet", contextWindow: CONTEXT_200K }, - { id: "claude-4.5-opus", contextWindow: CONTEXT_200K, supportsReasoningEffort: true }, - { id: "claude-4.6-opus", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, - { id: "claude-4.6-sonnet", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, - { id: "claude-opus-4-7", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, - { id: "claude-opus-4-8", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, - // claude-opus-5: regular variant is quarantined (not_found on every Run) but - // the umbrella row routes the THINKING variant, which is live — so the base - // row returns to the seed under the umbrella (resolver never sends the - // quarantined regular wire id for the bare slug). - { id: "claude-opus-5", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, - { id: "claude-fable-5", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, - // 260902 preemptive: Fable 5.1 seeded ahead of Cursor's lineup update (mirrors fable-5) under - // the three spellings Cursor has used for Claude ids; see CURSOR_CAPABILITIES. - { id: "claude-fable-5-1", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, - { id: "claude-fable-5.1", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, - { id: "claude-5.1-fable", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, - - { id: "composer-1", contextWindow: CONTEXT_200K }, - { id: "composer-2.5", contextWindow: CONTEXT_200K }, - { id: "composer-2.5-fast", contextWindow: CONTEXT_200K }, - - { id: "gemini-2.5-flash", contextWindow: CONTEXT_GEMINI }, - { id: "gemini-3-flash", contextWindow: CONTEXT_GEMINI }, - { id: "gemini-3-pro", contextWindow: CONTEXT_GEMINI }, - { id: "gemini-3-pro-image-preview", contextWindow: CONTEXT_200K }, - { id: "gemini-3.1-pro", contextWindow: CONTEXT_GEMINI }, - { id: "gemini-3.5-flash", contextWindow: CONTEXT_200K }, - // 260825 live GetUsableModels: both ship only as effort-suffixed ids, so each exposes a tier - // picker. 3.6 is the only Cursor model with a `minimal` rung. - { id: "gemini-3.6-flash", contextWindow: CONTEXT_GEMINI, supportsReasoningEffort: true }, - { id: "gemini-3.7-flash", contextWindow: CONTEXT_GEMINI, supportsReasoningEffort: true }, - - { id: "gpt-5-codex", contextWindow: CONTEXT_272K }, - { id: "gpt-5-fast", contextWindow: CONTEXT_272K }, - { id: "gpt-5-mini", contextWindow: CONTEXT_272K }, - { id: "gpt-5.1", contextWindow: CONTEXT_272K, supportsReasoningEffort: true }, - { id: "gpt-5.1-codex", contextWindow: CONTEXT_272K }, - { id: "gpt-5.1-codex-max", contextWindow: CONTEXT_272K, supportsReasoningEffort: true }, - { id: "gpt-5.1-codex-mini", contextWindow: CONTEXT_272K, supportsReasoningEffort: true }, - { id: "gpt-5.2", contextWindow: CONTEXT_272K, supportsReasoningEffort: true }, - { id: "gpt-5.2-codex", contextWindow: CONTEXT_272K, supportsReasoningEffort: true }, - { id: "gpt-5.3-codex", contextWindow: CONTEXT_272K, supportsReasoningEffort: true }, - { id: "gpt-5.4", contextWindow: CONTEXT_272K, supportsReasoningEffort: true }, - { id: "gpt-5.4-mini", contextWindow: CONTEXT_272K, supportsReasoningEffort: true }, - { id: "gpt-5.4-nano", contextWindow: CONTEXT_272K, supportsReasoningEffort: true }, - { id: "gpt-5.5", contextWindow: CONTEXT_272K, supportsReasoningEffort: true }, - // gpt-5.5-extra: absent from cursor.com docs but SURVIVES the live GetUsableModels filter - // (account-verified 260709, devlog/model_update/260709_model_refresh/004_live_snapshot.md). - { id: "gpt-5.5-extra", contextWindow: CONTEXT_200K, supportsReasoningEffort: true }, - { id: "gpt-5.6-sol", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, - { id: "gpt-5.6-terra", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, - { id: "gpt-5.6-luna", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, - - // 260709 refresh: stale grok/composer/kimi/gpt ids dropped per current cursor.com docs; the - // 260709 note: grok-4.5 was deferred; confirmed live 260708 (cursor.com/models, xAI launch). - - // Conflict resolution (260709): keep the refreshed 1M context + kimi-k2.7-code from de12fc8, - // take PR #73's supportsReasoningEffort for glm-5.2 (its effort-map tiers landed with the PR). - { id: "glm-5.2", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, - // 260814 preemptive: glm-5.3 seeded ahead of Cursor's lineup update (mirrors glm-5.2). - { id: "glm-5.3", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, - { id: "kimi-k2.7-code", contextWindow: CONTEXT_262K }, - // kimi-k3: cursor.com/docs/models/kimi-k3; account-verified via GetUsableModels (2026-07-28) — - // ships only as effort-suffixed kimi-k3-{low,high,max}, so the tier picker is exposed. - // kimi-k3 folds the old synthetic kimi-k3-1m row into the umbrella: the base - // is maxModeVerified (user-verified 1M on the Ultra plan, devlog 260826/025), - // so the ultra effort rung arms Max Mode on the wire and the separate picker - // row is gone. cursor/kimi-k3-1m stays routable as an alias. - { id: "kimi-k3", contextWindow: CONTEXT_1M, supportsReasoningEffort: true }, - - { id: "grok-4.5", contextWindow: 500_000, supportsReasoningEffort: true }, - // 260813 preemptive: grok-4.6 seeded ahead of Cursor's lineup update (mirrors grok-4.5). - { id: "grok-4.6", contextWindow: 500_000, supportsReasoningEffort: true }, + ...cursorUmbrellaRows().map(row => ({ + id: row.id, + contextWindow: row.window, + supportsReasoningEffort: row.efforts.length > 0, + })), + ...CURSOR_PRODUCT_MODELS, + ...CURSOR_REAL_ID_EXCEPTIONS, ]); +/** Picker labels for every seeded row, for providers.cursor.modelDisplayNames. */ +export function cursorModelDisplayNames(): Record { + return Object.fromEntries([ + ...CURSOR_ROUTER_MODEL_IDS.map(id => [id, CURSOR_ROUTER_DISPLAY_NAMES[id] ?? id] as const), + ...cursorUmbrellaRows().map(row => [row.id, row.displayName] as const), + ...CURSOR_PRODUCT_MODELS.map(model => [model.id, model.displayName] as const), + ...CURSOR_REAL_ID_EXCEPTIONS.map(model => [model.id, model.displayName] as const), + ]); +} + export function cursorModelIds(models: readonly CursorModelInfo[] = CURSOR_STATIC_MODELS): string[] { return normalizeCursorModels(models).map(model => model.id); } diff --git a/src/providers/derive.ts b/src/providers/derive.ts index ff59bad932..2a224476a3 100644 --- a/src/providers/derive.ts +++ b/src/providers/derive.ts @@ -231,6 +231,7 @@ export function providerConfigSeed(entry: ProviderRegistryEntry): OcxProviderCon ...(liveModels !== undefined ? { liveModels } : {}), ...(entry.contextWindow !== undefined ? { contextWindow: entry.contextWindow } : {}), ...(entry.modelContextWindows ? { modelContextWindows: { ...entry.modelContextWindows } } : {}), + ...(entry.modelDisplayNames ? { modelDisplayNames: { ...entry.modelDisplayNames } } : {}), ...(entry.modelInputModalities ? { modelInputModalities: cloneRecordOfArrays(entry.modelInputModalities) } : {}), ...(entry.modelMaxInputTokens ? { modelMaxInputTokens: { ...entry.modelMaxInputTokens } } : {}), ...(entry.defaultMaxOutputTokens !== undefined ? { defaultMaxOutputTokens: entry.defaultMaxOutputTokens } : {}), @@ -478,6 +479,11 @@ export function enrichProviderFromRegistry(name: string, prov: OcxProviderConfig if (prov.liveModels === undefined && seed.liveModels !== undefined) prov.liveModels = seed.liveModels; if (prov.contextWindow === undefined && seed.contextWindow !== undefined) prov.contextWindow = seed.contextWindow; if (!prov.modelContextWindows && seed.modelContextWindows) prov.modelContextWindows = { ...seed.modelContextWindows }; + // Per-model fill, not all-or-nothing: an operator who renamed ONE model must still receive + // labels for the rest, and an existing install must pick up newly seeded rows on enrich. + if (seed.modelDisplayNames) { + prov.modelDisplayNames = { ...seed.modelDisplayNames, ...(prov.modelDisplayNames ?? {}) }; + } if (seed.modelInputModalities) prov.modelInputModalities = fillRecordOfArrays(seed.modelInputModalities, prov.modelInputModalities); if (prov.defaultMaxOutputTokens === undefined && seed.defaultMaxOutputTokens !== undefined) prov.defaultMaxOutputTokens = seed.defaultMaxOutputTokens; if (!prov.modelMaxOutputTokens && seed.modelMaxOutputTokens) prov.modelMaxOutputTokens = { ...seed.modelMaxOutputTokens }; diff --git a/src/providers/registry.ts b/src/providers/registry.ts index 82279c6ebf..8ed01545ab 100644 --- a/src/providers/registry.ts +++ b/src/providers/registry.ts @@ -13,6 +13,7 @@ import { CURSOR_NO_VISION_MODELS, CURSOR_STATIC_MODELS, cursorModelContextWindows, + cursorModelDisplayNames, cursorModelIds, cursorModelInputModalities, cursorModelReasoningEfforts, @@ -270,6 +271,12 @@ export interface ProviderRegistryEntry { modelDiscovery?: ProviderModelDiscoverySpec; contextWindow?: number; modelContextWindows?: Record; + /** + * Registry-supplied picker labels. Without these a routed row shows its raw slug, + * because `routedDisplayName` (codex/catalog/sync.ts) passes the slug through for every + * provider. An operator's `modelDisplayNames` still wins: derive only fills when absent. + */ + modelDisplayNames?: Record; modelInputModalities?: Record; defaultMaxOutputTokens?: number; modelMaxOutputTokens?: Record; @@ -325,6 +332,7 @@ export type ProviderConfigSeed = Pick< OcxProviderConfig, "adapter" | "baseUrl" | "apiKeyTransport" | "responsesPath" | "authMode" | "keyOptional" | "freeTier" | "modelSuffixBracketStrip" | "defaultModel" | "models" | "liveModels" | "contextWindow" | "modelContextWindows" | "modelInputModalities" + | "modelDisplayNames" | "modelMaxInputTokens" | "defaultMaxOutputTokens" | "modelMaxOutputTokens" | "reasoningEfforts" | "modelReasoningEfforts" | "modelDefaultReasoningEfforts" | "reasoningEffortMap" | "modelReasoningEffortMap" | "reasoningWireFormat" | "noVisionModels" | "noReasoningModels" | "noTemperatureModels" | "noTopPModels" | "noPenaltyModels" @@ -1111,6 +1119,7 @@ export const PROVIDER_REGISTRY: readonly ProviderRegistryEntry[] = [ liveModels: true, defaultModel: "auto", modelContextWindows: cursorModelContextWindows(CURSOR_STATIC_MODELS), + modelDisplayNames: cursorModelDisplayNames(), modelInputModalities: cursorModelInputModalities(CURSOR_STATIC_MODELS), modelReasoningEfforts: cursorModelReasoningEfforts(CURSOR_STATIC_MODELS), // Kimi K3 documents `max` as its API default, and its Cursor ladder has no `medium` diff --git a/tests/cursor-display-names.test.ts b/tests/cursor-display-names.test.ts new file mode 100644 index 0000000000..201b68c3a2 --- /dev/null +++ b/tests/cursor-display-names.test.ts @@ -0,0 +1,55 @@ +import { describe, expect, test } from "bun:test"; +import { cursorModelDisplayNames } from "../src/adapters/cursor/discovery"; +import { cursorUmbrellaRows } from "../src/adapters/cursor/catalog"; +import { enrichProviderFromRegistry, providerConfigSeed } from "../src/providers/derive"; +import { getProviderRegistryEntry } from "../src/providers/registry"; +import { configuredModelDisplayName } from "../src/codex/catalog/provider-fetch"; +import type { OcxProviderConfig } from "../src/types"; + +/** + * The Codex picker showed raw slugs (`cursor/kimi-k3`) because `routedDisplayName` + * (codex/catalog/sync.ts) passes a routed slug through unchanged, and nothing carried + * Cursor's labels into `providers.cursor.modelDisplayNames` — the registry entry type had + * no such field. These assert the full registry -> config -> catalog-hint path, not just + * that a label table exists (devlog 260902_cursor_unified_identity). + */ +describe("cursor picker labels reach the catalog", () => { + const cursorEntry = () => { + const entry = getProviderRegistryEntry("cursor"); + if (!entry) throw new Error("cursor registry entry missing"); + return entry; + }; + + test("the registry entry carries a label for every seeded row", () => { + const labels = cursorModelDisplayNames(); + expect(cursorEntry().modelDisplayNames).toEqual(labels); + for (const row of cursorUmbrellaRows()) { + expect(labels[row.id]).toBe(row.displayName); + } + // The label is a human name, never the id echoed back. + expect(labels["kimi-k3"]).toBe("Kimi K3"); + expect(labels["grok-4.6"]).toBe("Cursor Grok 4.6"); + expect(labels["claude-opus-5"]).toBe("Claude Opus 5"); + expect(labels.auto).toBe("Auto"); + }); + + test("a fresh seed exposes the labels through configuredModelDisplayName", () => { + const seeded = providerConfigSeed(cursorEntry()); + expect(configuredModelDisplayName(seeded, "kimi-k3")).toBe("Kimi K3"); + expect(configuredModelDisplayName(seeded, "claude-4-sonnet-1m")).toBe("Claude Sonnet 4 (1M)"); + expect(configuredModelDisplayName(seeded, "composer-2.5-fast")).toBe("Composer 2.5 Fast"); + }); + + test("enrich backfills an existing install per model, preserving operator renames", () => { + const existing = { + adapter: "cursor", + baseUrl: "https://api2.cursor.sh", + modelDisplayNames: { "kimi-k3": "My K3" }, + } as OcxProviderConfig; + enrichProviderFromRegistry("cursor", existing); + // Operator value survives... + expect(configuredModelDisplayName(existing, "kimi-k3")).toBe("My K3"); + // ...while every other row still gains its label instead of staying unlabeled. + expect(configuredModelDisplayName(existing, "grok-4.6")).toBe("Cursor Grok 4.6"); + }); +}); diff --git a/tests/cursor-umbrella-rows.test.ts b/tests/cursor-umbrella-rows.test.ts index 63f2a55f8a..31a3f03a48 100644 --- a/tests/cursor-umbrella-rows.test.ts +++ b/tests/cursor-umbrella-rows.test.ts @@ -4,7 +4,14 @@ import { recordLiveCursorMaxModeModels, resolveCursorSelection, } from "../src/adapters/cursor/catalog"; -import { CURSOR_STATIC_MODELS, cursorModelReasoningEfforts } from "../src/adapters/cursor/discovery"; +import { + CURSOR_PRODUCT_MODELS, + CURSOR_REAL_ID_EXCEPTIONS, + CURSOR_ROUTER_MODEL_IDS, + CURSOR_STATIC_MODELS, + cursorModelDisplayNames, + cursorModelReasoningEfforts, +} from "../src/adapters/cursor/discovery"; import { createCursorRequest } from "../src/adapters/cursor/request-builder"; import type { OcxParsedRequest } from "../src/types"; @@ -34,11 +41,33 @@ describe("cursor umbrella picker rows (devlog 260828_cursor_umbrella_catalog)", expect(CURSOR_STATIC_MODELS.some(model => model.id === "claude-opus-5")).toBe(true); }); - test("row count shrank from the 69-row legacy seed", () => { - // 4 router + 50 base rows. Legacy carried 69 (13 thinking + 5 fast - // duplicates + kimi-k3-1m folded away; quarantined opus-5 base returned). - // #3211 pre-seeded Claude Fable 5.1 under three spellings (+3). - expect(CURSOR_STATIC_MODELS.length).toBe(54); + test("the seed is composed of routers + umbrella bases + declared product ids", () => { + // 4 routers + 34 umbrella bases + 13 product ids + 3 real-id exceptions. + // Derived, not frozen: the hard-coded count drifted twice already (51 -> 54 when + // #3211 pre-seeded Claude Fable 5.1 under three spellings), so the expectation now + // comes from the same capability table the seed is built from. + expect(CURSOR_STATIC_MODELS.length).toBe( + CURSOR_ROUTER_MODEL_IDS.length + + cursorUmbrellaRows().length + + CURSOR_PRODUCT_MODELS.length + + CURSOR_REAL_ID_EXCEPTIONS.length, + ); + }); + + test("every umbrella row is published, and no product id shadows a capability base", () => { + const ids = CURSOR_STATIC_MODELS.map(model => model.id); + for (const row of cursorUmbrellaRows()) expect(ids).toContain(row.id); + // normalizeCursorModels dedupes silently, so a collision would drop a row unnoticed. + expect(ids.length).toBe(new Set(ids).size); + const capabilityIds = new Set(cursorUmbrellaRows().map(row => row.id)); + for (const product of [...CURSOR_PRODUCT_MODELS, ...CURSOR_REAL_ID_EXCEPTIONS]) { + expect(capabilityIds.has(product.id)).toBe(false); + } + }); + + test("seed windows are the capability windows, not a second opinion", () => { + const seeded = new Map(CURSOR_STATIC_MODELS.map(model => [model.id, model.contextWindow])); + for (const row of cursorUmbrellaRows()) expect(seeded.get(row.id)).toBe(row.window); }); test("umbrella rows and seed efforts agree for every cataloged base", () => { From f607233151c5f7649c5b29b7608e8c3d545cf035 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 11:40:53 +0900 Subject: [PATCH 161/172] feat(server): advertise api_types and capabilities on the raw /v1/models list (#3230) * feat(server): advertise api_types and capabilities on the raw /v1/models list Cursor's local-agent runtime (the Private Inference build) enables its reasoning-effort control only when a model row carries api_types and, optionally, a capabilities object. Emit both on every row of the OpenAI-shape list: api_types is constant (chat_completions, responses, anthropic_messages; membership is load-bearing for Cursor's wire selector and guarded by a unit test), capabilities carries context_length, supports_vision and the reasoning_effort ladder when the catalog knows them. Plain OpenAI clients, Grok Build and the Codex catalog branch ignore the new keys. The combo e2e assertions that compared whole row literals now match on the combo-relevant shape while keeping is_combo presence explicit. * fix(server): include output_modalities so Cursor keeps enriched rows Cursor's local-agent runtime drops any api_types row whose capabilities lack output_modalities containing "text", which silently disabled the effort control on every row. Emit output_modalities: ["text"] and mirror input_modalities when the catalog knows them. Verified live in Cursor Private Inference 3.18.25: the picker shows Low/Medium/High/Extra High for gpt-5.6-sol and a High turn arrives as reasoning.effort=high on /v1/responses. * fix(server): report the effective context window, not the provider cap contextWindow is already narrowed by providerContextCaps; contextCap is the raw operator knob and is set on every row of a capped provider even when the cap did not bite, so preferring it over-reported models whose real window sits below the cap. Freeze the shared api_types constant and copy it per row. * fix(server): omit context_length when it floors to zero positiveInt accepted 0.5 and then floored it to 0, which would emit an invalid zero context length instead of omitting the field (CodeRabbit). * feat(server): advertise the native long-context tier for Cursor Max Mode Cursor's local-agent runtime shows a Context selector (default vs long window, long marked as costing more) when a model row carries a long-context threshold below its context_length. It reads that threshold only from pricing.overrides[].min_prompt_tokens; a cost.long_context object fails its row schema. For native GPT-5.6 rows advertise the family's 272k default and 922k opt-in pair through nativeOpenAiContextTier; routed rows have no separate tier and stay unchanged. * fix(catalog): drop the native long-context tier under any window lever below it A per-model window override or provider window below the long window must remove the tier the same way a provider cap does; otherwise the row keeps advertising 922k while the effective window is smaller (CodeRabbit). --------- Co-authored-by: jun --- src/codex/catalog.ts | 2 +- src/codex/catalog/metadata.ts | 28 ++++ src/server/index.ts | 28 +++- src/server/models-capabilities.ts | 89 ++++++++++ tests/cursor-local-models-schema.test.ts | 196 +++++++++++++++++++++++ tests/server-combo-failover-e2e.test.ts | 37 +++-- 6 files changed, 362 insertions(+), 18 deletions(-) create mode 100644 src/server/models-capabilities.ts create mode 100644 tests/cursor-local-models-schema.test.ts diff --git a/src/codex/catalog.ts b/src/codex/catalog.ts index ce7eaf1615..fe73d48263 100644 --- a/src/codex/catalog.ts +++ b/src/codex/catalog.ts @@ -2,7 +2,7 @@ // Public surface preserved exactly; importers keep using "src/codex/catalog". export { isMediaGenerationModelId, shouldExposeRoutedModel, readCodexCatalogPath, readCatalog, normalizeRoutedCatalogEntry, catalogModelSlug, filterSupportedNativeSlugs, catalogModelSupportsReasoningSummaries } from "./catalog/parsing"; export type { CatalogModel, MultiAgentMode } from "./catalog/parsing"; -export { accountBoundNativeOpenAiSlugs, accountBoundNativeOpenAiSlugsBySelector, CODEX_NATIVE_ALIAS_CATALOG_KIND, NATIVE_DAYBREAK_BLUE_MODEL, NATIVE_GPT56_CONTEXT_WINDOW, NATIVE_GPT56_MAX_INPUT_TOKENS, NATIVE_GPT56_OPT_IN_CONTEXT_WINDOW, NATIVE_OPENAI_CAPABILITY_ALIAS_MODELS, NATIVE_OPENAI_MODELS, configuredNativeAliasSlugs, desktopAllowlistSuppressedNativeSlugs, isNativeAliasCatalogEntry, isNativeOpenAiCapabilityAliasModel, nativeContextLimits, nativeOpenAiCapabilitySourceSlug, nativeOpenAiContextWindow, nativeOpenAiMaxInputTokens, disabledNativeSlugs, visibleNativeSlugs, desktopVisibleNativeSlugs, nativeModelRows, applyNativeVisibility, observedAccountBoundNativeEntries, observedAccountBoundNativeOpenAiSlugs, upstreamNativeEntry, nativeOpenAiSlugs, listCatalogNativeSlugs, nativeInputModalities, nativeReasoningEfforts, nativeDefaultReasoningEffort, shouldIncludeAccountBoundNativeOpenAi, shouldIncludeNativeOpenAi, type NativeContextLimits, type NativeContextLimitsInput } from "./catalog/metadata"; +export { accountBoundNativeOpenAiSlugs, accountBoundNativeOpenAiSlugsBySelector, CODEX_NATIVE_ALIAS_CATALOG_KIND, NATIVE_DAYBREAK_BLUE_MODEL, NATIVE_GPT56_CONTEXT_WINDOW, NATIVE_GPT56_MAX_INPUT_TOKENS, NATIVE_GPT56_OPT_IN_CONTEXT_WINDOW, NATIVE_OPENAI_CAPABILITY_ALIAS_MODELS, NATIVE_OPENAI_MODELS, configuredNativeAliasSlugs, desktopAllowlistSuppressedNativeSlugs, isNativeAliasCatalogEntry, isNativeOpenAiCapabilityAliasModel, nativeContextLimits, nativeOpenAiCapabilitySourceSlug, nativeOpenAiContextWindow, nativeOpenAiMaxInputTokens, nativeOpenAiContextTier, disabledNativeSlugs, visibleNativeSlugs, desktopVisibleNativeSlugs, nativeModelRows, applyNativeVisibility, observedAccountBoundNativeEntries, observedAccountBoundNativeOpenAiSlugs, upstreamNativeEntry, nativeOpenAiSlugs, listCatalogNativeSlugs, nativeInputModalities, nativeReasoningEfforts, nativeDefaultReasoningEffort, shouldIncludeAccountBoundNativeOpenAi, shouldIncludeNativeOpenAi, type NativeContextLimits, type NativeContextLimitsInput } from "./catalog/metadata"; export { isSpawnableCodexCandidate, codexExecInvocation, loadBundledCodexCatalog, materializeBundledCodexCatalog, loadCatalogTemplate } from "./catalog/bundled"; export { nativeEffortClamp, shouldApplyNativeEffortClamp, catalogModelEfforts, codexSupportedReasoningEfforts, clampedDefaultEffort, clampEntryToCodexSupportedEfforts, clampCatalogModelsToCodexSupport } from "./catalog/effort"; export { applyProviderConfigHints, isDatedVariantId, filterCatalogVisibleModels, gatherRoutedModels, clearGatherRoutedModelsInflight, augmentRoutedModelsWithRegistryOpenAiApiRows, augmentRoutedModelsWithMetadata, resolveComboCatalogMember, configuredComboTargetModelsByProvider } from "./catalog/provider-fetch"; diff --git a/src/codex/catalog/metadata.ts b/src/codex/catalog/metadata.ts index e797f59d2b..f0bbe6348c 100644 --- a/src/codex/catalog/metadata.ts +++ b/src/codex/catalog/metadata.ts @@ -271,6 +271,34 @@ export function nativeOpenAiContextWindow(slug: string, limits?: NativeContextLi return narrowToLimits(raw, slug, limits); } +/** + * Long-context tier for a native slug as a (default, long) pair, for clients that let the user + * pick a window per request (Cursor's local-agent "Context" selector). The pair is the family's + * pinned default window and its opt-in ceiling, independent of whether the operator has + * already opted the proxy into the long window: the selector exists so the client can choose. + * Any user lever below the long window removes the tier: a per-model window override, the + * provider-level window override, or a provider context cap. A lever at or above it leaves the + * tier intact (the 922k/1050k opt-in values are the levers, not a request to shrink). Undefined + * when the family has no separate tier or when the two windows coincide. + */ +export function nativeOpenAiContextTier( + slug: string, + limits?: NativeContextLimitsInput, +): { defaultWindow: number; longWindow: number } | undefined { + const override = NATIVE_OPENAI_CONTEXT_OVERRIDES[slug]; + const defaultWindow = positiveInt(override?.contextWindow); + const longWindow = positiveInt(override?.maxContextWindow); + if (defaultWindow === undefined || longWindow === undefined || longWindow <= defaultWindow) return undefined; + const resolved = asLimits(limits); + const levers = [ + positiveInt(resolved.modelWindows?.[slug]), + positiveInt(resolved.providerWindow), + positiveInt(resolved.cap), + ]; + if (levers.some(lever => lever !== undefined && lever < longWindow)) return undefined; + return { defaultWindow, longWindow }; +} + /** * Largest input a native slug accepts, or undefined when no separate limit is known * (the caller then falls back to the context window). diff --git a/src/server/index.ts b/src/server/index.ts index ac59dc2990..4f07679190 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -224,6 +224,7 @@ import { } from "../lib/package-tree-integrity"; import { detectInstall } from "../update/index"; import { readyProtocolMetadata } from "../remote/protocol"; +import { modelCapabilityFields } from "./models-capabilities"; export const MAX_WS_FRAME_BYTES = 50 * 1024 * 1024; const WEBSOCKET_IDLE_TIMEOUT_SECONDS = 0; @@ -1345,7 +1346,7 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server grokEffortOption(effort, effort === defaultEffort)), }; }; + // Cursor's local-agent runtime (Private Inference build) reads api_types + capabilities + // to enable its effort control; every other consumer ignores them. See + // src/server/models-capabilities.ts. + const nativeLimits = nativeContextLimits(config); + const nativeContextInput = (metadataId: string) => { + const tier = nativeOpenAiContextTier(metadataId, nativeLimits); + return tier + ? { contextWindow: tier.defaultWindow, longContextWindow: tier.longWindow } + : { contextWindow: nativeOpenAiContextWindow(metadataId, nativeLimits) }; + }; const nativeModelRow = (id: string, metadataId = id) => ({ id, object: "model", @@ -1508,6 +1519,14 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server = new Set(["chat_completions", "responses", "openai_chat", "openai_responses"]); + +export interface ModelCapabilityInput { + reasoningEfforts?: readonly string[]; + contextWindow?: number; + /** + * Larger opt-in window (Cursor "Max Mode"). When it exceeds contextWindow, the row advertises + * the long window as context_length and the default window as the long-context threshold, + * which makes Cursor's local runtime show a Context selector (default vs long, long marked + * as costing more). + */ + longContextWindow?: number; + inputModalities?: readonly string[]; +} + +export interface ModelCapabilityFields { + api_types: readonly string[]; + capabilities: { + context_length?: number; + /** Cursor's extended-row filter REQUIRES this to contain "text"; every route emits text. */ + output_modalities: string[]; + input_modalities?: string[]; + supports_tool_use: true; + supports_streaming: true; + supports_reasoning: boolean; + supports_vision?: boolean; + reasoning_effort?: string[]; + }; + /** + * Cursor reads the long-context threshold from `pricing.overrides[].min_prompt_tokens`. That + * key sits outside its validated capability schema, so it is the one place a threshold can + * be carried without failing row validation (`cost.long_context` is rejected by that schema). + */ + pricing?: { overrides: Array<{ min_prompt_tokens: number }> }; +} + +function positiveInt(value: unknown): number | undefined { + if (typeof value !== "number" || !Number.isFinite(value)) return undefined; + const floored = Math.floor(value); + return floored > 0 ? floored : undefined; +} + +export function modelCapabilityFields(input: ModelCapabilityInput): ModelCapabilityFields { + const efforts = (input.reasoningEfforts ?? []).filter(effort => typeof effort === "string" && effort.length > 0); + const contextLength = positiveInt(input.contextWindow); + const longContextLength = positiveInt(input.longContextWindow); + const hasLongTier = contextLength !== undefined && longContextLength !== undefined && longContextLength > contextLength; + const modalities = Array.isArray(input.inputModalities) + ? input.inputModalities.filter(modality => typeof modality === "string" && modality.length > 0) + : undefined; + const supportsVision = modalities !== undefined ? modalities.includes("image") : undefined; + return { + api_types: [...OPENCODEX_MODEL_API_TYPES], + capabilities: { + ...(hasLongTier + ? { context_length: longContextLength } + : contextLength !== undefined ? { context_length: contextLength } : {}), + // Once a gateway advertises api_types, Cursor keeps only rows whose output_modalities + // include "text"; omitting the key drops the row from the extended catalog. + output_modalities: ["text"], + ...(modalities !== undefined && modalities.length > 0 ? { input_modalities: [...modalities] } : {}), + supports_tool_use: true, + supports_streaming: true, + supports_reasoning: efforts.length > 0, + ...(supportsVision !== undefined ? { supports_vision: supportsVision } : {}), + ...(efforts.length > 0 ? { reasoning_effort: [...efforts] } : {}), + }, + ...(hasLongTier ? { pricing: { overrides: [{ min_prompt_tokens: contextLength }] } } : {}), + }; +} diff --git a/tests/cursor-local-models-schema.test.ts b/tests/cursor-local-models-schema.test.ts new file mode 100644 index 0000000000..0d79dc7460 --- /dev/null +++ b/tests/cursor-local-models-schema.test.ts @@ -0,0 +1,196 @@ +import { afterEach, beforeEach, describe, expect, setDefaultTimeout, test } from "bun:test"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { saveConfig } from "../src/config"; +import { nativeOpenAiContextTier, nativeReasoningEfforts } from "../src/codex/catalog"; +import { + resetCodexModelEntitlementCacheForTests, + seedCodexModelEntitlementsForTests, +} from "../src/codex/model-entitlements"; +import { startServer } from "../src/server"; +import { + modelCapabilityFields, + OPENAI_FAMILY_API_TYPES, + OPENCODEX_MODEL_API_TYPES, +} from "../src/server/models-capabilities"; +import type { OcxConfig } from "../src/types"; +import { SERVER_BUDGET_MS } from "./helpers/test-budget"; + +// Cursor's local-agent runtime ("Private Inference" build) only enables its reasoning-effort +// control when a GET /v1/models row carries api_types (+ optional capabilities). These cases +// start a real server and read the raw OpenAI-shape list, like the Grok discovery tests. +setDefaultTimeout(SERVER_BUDGET_MS); + +const previousHome = process.env.OPENCODEX_HOME; +let testHome = ""; + +function capabilityConfig(): OcxConfig { + return { + port: 0, + hostname: "127.0.0.1", + defaultProvider: "kimi", + providers: { + kimi: { + adapter: "openai-chat", + baseUrl: "https://kimi.test/v1", + liveModels: false, + models: ["k3", "kimi-for-coding"], + modelReasoningEfforts: { + k3: ["low", "high", "max"], + "kimi-for-coding": [], + }, + modelDefaultReasoningEfforts: { k3: "high" }, + modelContextWindows: { k3: 200000 }, + modelInputModalities: { k3: ["text", "image"] }, + }, + openai: { + adapter: "openai-responses", + baseUrl: "https://chatgpt.com/backend-api/codex", + liveModels: false, + }, + }, + }; +} + +beforeEach(() => { + testHome = mkdtempSync(join(tmpdir(), "ocx-cursor-local-schema-")); + process.env.OPENCODEX_HOME = testHome; +}); + +afterEach(() => { + resetCodexModelEntitlementCacheForTests(); + if (previousHome === undefined) delete process.env.OPENCODEX_HOME; + else process.env.OPENCODEX_HOME = previousHome; + if (testHome) rmSync(testHome, { recursive: true, force: true }); + testHome = ""; +}); + +describe("modelCapabilityFields", () => { + test("api_types keeps an OpenAI-family member so Cursor never routes to the Messages wire alone", () => { + expect(OPENCODEX_MODEL_API_TYPES.some(type => OPENAI_FAMILY_API_TYPES.has(type))).toBe(true); + }); + + test("empty input yields only the constant capabilities", () => { + const fields = modelCapabilityFields({}); + expect(fields.api_types).toEqual(OPENCODEX_MODEL_API_TYPES); + expect(fields.capabilities).toEqual({ + output_modalities: ["text"], + supports_tool_use: true, + supports_streaming: true, + supports_reasoning: false, + }); + expect("context_length" in fields.capabilities).toBe(false); + expect("input_modalities" in fields.capabilities).toBe(false); + expect("supports_vision" in fields.capabilities).toBe(false); + expect("reasoning_effort" in fields.capabilities).toBe(false); + }); + + test("non-positive context and text-only modalities are reported honestly", () => { + const fields = modelCapabilityFields({ contextWindow: 0, inputModalities: ["text"], reasoningEfforts: ["", "low"] }); + expect("context_length" in fields.capabilities).toBe(false); + // A fractional value below 1 floors to 0 and must be omitted, not emitted as 0. + expect("context_length" in modelCapabilityFields({ contextWindow: 0.5 }).capabilities).toBe(false); + expect(modelCapabilityFields({ contextWindow: 1.9 }).capabilities.context_length).toBe(1); + expect(fields.capabilities.input_modalities).toEqual(["text"]); + expect(fields.capabilities.supports_vision).toBe(false); + expect(fields.capabilities.reasoning_effort).toEqual(["low"]); + expect(fields.capabilities.supports_reasoning).toBe(true); + }); + + test("a larger opt-in window becomes context_length with the default window as the long-context threshold", () => { + const tiered = modelCapabilityFields({ contextWindow: 272000, longContextWindow: 922000 }); + expect(tiered.capabilities.context_length).toBe(922000); + expect(tiered.pricing).toEqual({ overrides: [{ min_prompt_tokens: 272000 }] }); + // Equal or smaller opt-in window: plain context_length, no pricing block. + const flat = modelCapabilityFields({ contextWindow: 272000, longContextWindow: 272000 }); + expect(flat.capabilities.context_length).toBe(272000); + expect("pricing" in flat).toBe(false); + expect("pricing" in modelCapabilityFields({ longContextWindow: 922000 })).toBe(false); + }); +}); + +describe("nativeOpenAiContextTier", () => { + test("native GPT-5.6 carries the 272k/922k pair and other natives carry none", () => { + expect(nativeOpenAiContextTier("gpt-5.6-sol")).toEqual({ defaultWindow: 272000, longWindow: 922000 }); + expect(nativeOpenAiContextTier("gpt-5.5")).toBeUndefined(); + }); + + test("any user lever below the long window removes the tier; levers at or above it keep it", () => { + expect(nativeOpenAiContextTier("gpt-5.6-sol", { cap: 500000 })).toBeUndefined(); + expect(nativeOpenAiContextTier("gpt-5.6-sol", { providerWindow: 400000 })).toBeUndefined(); + expect(nativeOpenAiContextTier("gpt-5.6-sol", { modelWindows: { "gpt-5.6-sol": 300000 } })).toBeUndefined(); + expect(nativeOpenAiContextTier("gpt-5.6-sol", { cap: 1050000, providerWindow: 922000 })).toEqual({ defaultWindow: 272000, longWindow: 922000 }); + // A window override for another slug does not touch this one. + expect(nativeOpenAiContextTier("gpt-5.6-sol", { modelWindows: { "gpt-5.6-terra": 300000 } })).toEqual({ defaultWindow: 272000, longWindow: 922000 }); + }); +}); + +describe("raw /v1/models list advertises Cursor local-agent capabilities", () => { + test("routed rows carry api_types and capabilities derived from provider config", async () => { + seedCodexModelEntitlementsForTests("main", ["gpt-5.6-sol"]); + saveConfig(capabilityConfig()); + const server = startServer(0); + try { + const res = await fetch(new URL("/v1/models", server.url)); + expect(res.status).toBe(200); + const body = await res.json() as { data: Array> }; + + const k3 = body.data.find(m => m.id === "kimi/k3"); + expect(k3).toBeDefined(); + expect(k3!.api_types).toEqual(["chat_completions", "responses", "anthropic_messages"]); + expect(k3!.capabilities).toEqual({ + context_length: 200000, + output_modalities: ["text"], + input_modalities: ["text", "image"], + supports_tool_use: true, + supports_streaming: true, + supports_reasoning: true, + supports_vision: true, + reasoning_effort: ["low", "high", "max"], + }); + // Grok Build's discovery fields stay untouched next to the new keys. + expect(k3!.supports_reasoning_effort).toBe(true); + expect(k3!.reasoning_effort).toBe("high"); + + const plain = body.data.find(m => m.id === "kimi/kimi-for-coding"); + expect(plain).toBeDefined(); + expect(plain!.api_types).toEqual(["chat_completions", "responses", "anthropic_messages"]); + const plainCaps = plain!.capabilities as Record; + expect(plainCaps.supports_reasoning).toBe(false); + expect("reasoning_effort" in plainCaps).toBe(false); + + const sol = body.data.find(m => m.id === "gpt-5.6-sol"); + expect(sol).toBeDefined(); + expect(sol!.api_types).toEqual(["chat_completions", "responses", "anthropic_messages"]); + const solCaps = sol!.capabilities as Record; + expect(solCaps.output_modalities).toEqual(["text"]); + expect(solCaps.reasoning_effort).toEqual(nativeReasoningEfforts("gpt-5.6-sol")); + // Native GPT-5.6: 272k default window, 922k opt-in ceiling → Cursor Context selector. + expect(solCaps.context_length).toBe(922000); + expect(sol!.pricing).toEqual({ overrides: [{ min_prompt_tokens: 272000 }] }); + expect(solCaps.supports_vision).toBe(true); + // Routed rows have no separate opt-in tier, so no pricing block. + expect("pricing" in k3!).toBe(false); + } finally { + await server.stop(true); + } + }); + + test("context_length is the effective window, not the provider cap, when the cap does not bite", async () => { + const config = capabilityConfig(); + // Cap above k3's real window: contextWindow stays 200000 while contextCap records 350000. + config.providerContextCaps = { kimi: 350000 }; + saveConfig(config); + const server = startServer(0); + try { + const res = await fetch(new URL("/v1/models", server.url)); + const body = await res.json() as { data: Array> }; + const k3 = body.data.find(m => m.id === "kimi/k3"); + expect(k3).toBeDefined(); + expect((k3!.capabilities as Record).context_length).toBe(200000); + } finally { + await server.stop(true); + } + }); +}); diff --git a/tests/server-combo-failover-e2e.test.ts b/tests/server-combo-failover-e2e.test.ts index a4e47446a7..ce6fbacc77 100644 --- a/tests/server-combo-failover-e2e.test.ts +++ b/tests/server-combo-failover-e2e.test.ts @@ -863,28 +863,32 @@ describe("server combo failover 030 activation matrix", () => { body: JSON.stringify({ id: "free", combo: { ...combo, alias } }), }); - expect((await publicRows()).filter(model => model.id === selector)).toEqual([ - { id: selector, object: "model", created: 0, owned_by: "openai", is_combo: true }, - ]); + // Rows also carry api_types/capabilities for Cursor local-agent discovery; match the + // combo-relevant shape and keep is_combo presence/absence explicit. + const initialRows = (await publicRows()).filter(model => model.id === selector); + expect(initialRows).toHaveLength(1); + expect(initialRows[0]).toMatchObject({ id: selector, object: "model", created: 0, owned_by: "openai", is_combo: true }); const renamed = await updateAlias("fast-chat"); expect(renamed.status).toBe(200); const renamedRows = await publicRows(); - expect(renamedRows.filter(model => model.id === selector)).toEqual([ - { id: selector, object: "model", created: 0, owned_by: "deepseek" }, - ]); - expect(renamedRows.filter(model => model.id === "fast-chat")).toEqual([ - { id: "fast-chat", object: "model", created: 0, owned_by: "openai", is_combo: true }, - ]); + const renamedSelectorRows = renamedRows.filter(model => model.id === selector); + expect(renamedSelectorRows).toHaveLength(1); + expect(renamedSelectorRows[0]).toMatchObject({ id: selector, object: "model", created: 0, owned_by: "deepseek" }); + expect(renamedSelectorRows[0].is_combo).toBeUndefined(); + const renamedAliasRows = renamedRows.filter(model => model.id === "fast-chat"); + expect(renamedAliasRows).toHaveLength(1); + expect(renamedAliasRows[0]).toMatchObject({ id: "fast-chat", object: "model", created: 0, owned_by: "openai", is_combo: true }); const restored = await updateAlias(selector); expect(restored.status).toBe(200); const deleted = await fetch(new URL("/api/combos?id=free", server.url), { method: "DELETE" }); expect(deleted.status).toBe(200); const deletedRows = await publicRows(); - expect(deletedRows.filter(model => model.id === selector)).toEqual([ - { id: selector, object: "model", created: 0, owned_by: "deepseek" }, - ]); + const deletedSelectorRows = deletedRows.filter(model => model.id === selector); + expect(deletedSelectorRows).toHaveLength(1); + expect(deletedSelectorRows[0]).toMatchObject({ id: selector, object: "model", created: 0, owned_by: "deepseek" }); + expect(deletedSelectorRows[0].is_combo).toBeUndefined(); expect(deletedRows.some(model => model.is_combo === true)).toBe(false); } finally { await server.stop(true); @@ -907,10 +911,11 @@ describe("server combo failover 030 activation matrix", () => { const payload = await response.json() as { data: Array<{ id: string; owned_by: string; is_combo?: boolean }>; }; - expect(payload.data.filter(model => model.id.startsWith("a/vendor")).sort((a, b) => a.id.localeCompare(b.id))).toEqual([ - { id: "a/vendor-model", object: "model", created: 0, owned_by: "openai", is_combo: true }, - { id: "a/vendor/model", object: "model", created: 0, owned_by: "a" }, - ]); + const vendorRows = payload.data.filter(model => model.id.startsWith("a/vendor")).sort((a, b) => a.id.localeCompare(b.id)); + expect(vendorRows).toHaveLength(2); + expect(vendorRows[0]).toMatchObject({ id: "a/vendor-model", object: "model", created: 0, owned_by: "openai", is_combo: true }); + expect(vendorRows[1]).toMatchObject({ id: "a/vendor/model", object: "model", created: 0, owned_by: "a" }); + expect(vendorRows[1].is_combo).toBeUndefined(); } finally { await server.stop(true); } From 72a7c4a4592de4e3771697c4f87fda36a77563ed Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 11:43:14 +0900 Subject: [PATCH 162/172] docs: Cursor Private Inference connector guide (#3231) * docs: Cursor Private Inference connector guide Explain why regular Cursor needs a public tunnel (its backend calls the custom base URL), how Cursor's local-agent build reaches opencodex on loopback instead, the per-OS environment mechanics for a GUI-launched app on macOS, Windows and Linux, which effort ladders Cursor exposes per model id, and how to verify. The guide states that opencodex does not distribute that build and links nothing to download. * docs(cursor): explain Max Mode as the Context selector and effort max as unreachable * devlog: Cursor local models schema unit (research, audits, live evidence) --------- Co-authored-by: jun --- .../000_research.md | 120 +++++++++++ .../005_audit_round1.md | 18 ++ .../010_layer1_models_capabilities.md | 195 ++++++++++++++++++ .../011_effort_control.png | Bin 0 -> 43517 bytes .../012_effort_ladder.png | Bin 0 -> 45986 bytes .../013_high_turn.png | Bin 0 -> 49788 bytes .../015_layer1_live_evidence.md | 24 +++ .../016_layer1_check.md | 22 ++ .../017_impl_review.md | 20 ++ .../020_layer2_docs_guide.md | 83 ++++++++ .../021_pr_rollup.json | 2 + .../022_stack_closeout.md | 24 +++ .../030_max_mode_context_selector.md | 66 ++++++ .../031_context_selector.png | Bin 0 -> 44921 bytes .../032_context_options.png | Bin 0 -> 46947 bytes docs-site/astro.config.mjs | 1 + .../docs/guides/cursor-private-inference.md | 123 +++++++++++ .../src/content/docs/guides/integrations.md | 5 + 18 files changed, 703 insertions(+) create mode 100644 devlog/_plan/260902_cursor_local_models_schema/000_research.md create mode 100644 devlog/_plan/260902_cursor_local_models_schema/005_audit_round1.md create mode 100644 devlog/_plan/260902_cursor_local_models_schema/010_layer1_models_capabilities.md create mode 100644 devlog/_plan/260902_cursor_local_models_schema/011_effort_control.png create mode 100644 devlog/_plan/260902_cursor_local_models_schema/012_effort_ladder.png create mode 100644 devlog/_plan/260902_cursor_local_models_schema/013_high_turn.png create mode 100644 devlog/_plan/260902_cursor_local_models_schema/015_layer1_live_evidence.md create mode 100644 devlog/_plan/260902_cursor_local_models_schema/016_layer1_check.md create mode 100644 devlog/_plan/260902_cursor_local_models_schema/017_impl_review.md create mode 100644 devlog/_plan/260902_cursor_local_models_schema/020_layer2_docs_guide.md create mode 100644 devlog/_plan/260902_cursor_local_models_schema/021_pr_rollup.json create mode 100644 devlog/_plan/260902_cursor_local_models_schema/022_stack_closeout.md create mode 100644 devlog/_plan/260902_cursor_local_models_schema/030_max_mode_context_selector.md create mode 100644 devlog/_plan/260902_cursor_local_models_schema/031_context_selector.png create mode 100644 devlog/_plan/260902_cursor_local_models_schema/032_context_options.png create mode 100644 docs-site/src/content/docs/guides/cursor-private-inference.md diff --git a/devlog/_plan/260902_cursor_local_models_schema/000_research.md b/devlog/_plan/260902_cursor_local_models_schema/000_research.md new file mode 100644 index 0000000000..d7030191ce --- /dev/null +++ b/devlog/_plan/260902_cursor_local_models_schema/000_research.md @@ -0,0 +1,120 @@ +# 000 — Research: Cursor Private Inference model-capability schema + +Unit: `260902_cursor_local_models_schema`. Base: `origin/dev` at `85f7ef92a` (re-anchored after +audit; the worktree HEAD `5fc7d073e` is an ancestor of it). Class C3 (public inbound contract change on +`GET /v1/models`, docs guide, stacked PRs). Research only; no diffs in this document. + +## Problem + +OpenCodex-routed models appear in the model picker of the Cursor **Private Inference** +build (release track `cursor-local`, `buildFlags.localMode = true`), but the picker shows +no reasoning-effort control. Verified live 2026-09-02 on Cursor Private Inference 3.18.25 +(darwin-arm64) against the local proxy at `http://127.0.0.1:10100/v1`: agent turns complete +(`ocx observe logs --json` rows with `inboundProtocol: "chat"`, `admissionKind: "loopback"`, +`provider: openai-p3fa38a`, model `gpt-5.6-sol`), so transport is fine — only the +effort ladder is missing. + +## Where the gate is (Cursor side, read from the shipped bundle) + +File: `Cursor Private Inference.app/Contents/Resources/app/extensions/cursor-agent-exec/dist/main.js`. + +1. Model discovery calls `GET {baseUrl}/models` (`tpe(baseUrl, "/models")`) with + `authorization: Bearer ` and a 2 s timeout, expects `{ data: [...] }`. +2. `extendedCapabilitiesDetected = dme(data)` is true only if **some** row passes the + zod-style schema `fme`: + - `api_types`: non-empty string array containing at least one of + `chat_completions | responses | openai_chat | openai_responses | anthropic_messages` + (set `lme`); + - `capabilities` (optional object): `context_length`, `max_output_tokens` + (finite positive numbers), `output_modalities`, `input_modalities` (string[]), + `supports_tool_use`, `supports_streaming`, `supports_reasoning`, `supports_vision` + (booleans), `reasoning_effort` (string[]), `cost` (optional); + - `cost` (optional). +3. The picker builder `J(model, tier)` attaches the "Reasoning" control only when + `I(model.id)` (a hard-coded regex table) yields an effort ladder **and** + `model.extendedCapabilitiesDetected === true`. For entries with + `effortRequiresReasoningCapability` (Gemini) it also needs + `capabilities.supports_reasoning !== false`. +4. The ladder shown is Cursor's table, not the gateway's list: + - `gpt-5.6-(luna|sol|terra)` → `reasoning_effort` in `low|medium|high|xhigh`, default medium + - `claude-opus-5`, `claude-sonnet-5`, `claude-opus-4.7/4.8` → `output_config.effort` low..max + - `claude-opus-4.6`, `claude-opus-4.5`, `claude-sonnet-4.6` → low..max (no xhigh) + - `grok-4.3/4.5/4.6`, `grok-build-latest` → `reasoning_effort` minimal..xhigh, default high + - `gemini-*` → minimal..high, requires `supports_reasoning` + - bare `gpt-5`, `gpt-5.x` → low..xhigh + - anything else (e.g. `claude-fable-5-1`, `kimi-k3`) → no control + The id is normalised first: lower-case, strip everything before the last `/`, strip `@...`. + So `anthropic/claude-opus-5` matches `claude-opus-5`. +5. On send, the chosen value is written as `reasoning_effort` (chat completions), + `reasoning.effort` (responses) or `thinking + output_config.effort` (messages). + +## What OpenCodex emits today + +`src/server/index.ts`, raw OpenAI-list branch of `GET /v1/models` (around line 1481): + +```json +{ "id": "gpt-5.6-sol", "object": "model", "created": 0, "owned_by": "openai", + "supports_reasoning_effort": true, "reasoning_effort": "low", + "reasoning_efforts": [{ "value": "low", "label": "Low Effort", "default": true }, ...] } +``` + +No `api_types`, no `capabilities`. `dme` returns false → no effort control. Confirmed by +the live picker (only model names, "Add Models"). + +## Data available server-side for the new fields + +- Effort ladder: `m.reasoningEfforts` / `nativeReasoningEfforts(slug)` (already used). +- Context: `m.contextWindow` / `m.contextCap` for routed rows; `nativeOpenAiContextWindow(slug, + nativeContextLimits(config))` for native rows (`src/codex/catalog/metadata.ts:266`). +- Vision: `m.inputModalities` includes `"image"` (routed rows; `provider-fetch.ts:675-708`). +- Max output tokens: not tracked for routed rows → omit (optional in Cursor's schema). +- Tool use / streaming: every OpenCodex route supports both → constant `true`. +- Anthropic messages: `/v1/messages` is served for every routed model, but Cursor picks + `anthropic_messages` only when the base URL path ends in `/messages`; advertising it is + harmless and true. Keep `api_types: ["chat_completions","responses","anthropic_messages"]`. + +## Existing consumers of the raw list that must keep passing + +- `tests/grok-models-effort-list.test.ts` (Grok Build ladder shape) — additive fields OK. +- `tests/claude-models-discovery.test.ts` (Claude gateway branch, separate code path). +- `tests/server-combo-failover-e2e.test.ts:824-846` asserts **exact** row literals with `toEqual` + on six combo/vendor rows (audit blocker 1). Those literals must move to `toMatchObject` while + keeping the explicit `is_combo` absence check at :846. +- `tests/server-auth.test.ts`, `tests/ollama-native.test.ts`, `tests/provider-outbound.test.ts`, + `tests/codex-catalog.test.ts`, `tests/gui-management-session.test.ts` read the list without + key-set equality; all of them run at C as the focused set. + +## Platform matrix (release track `cursor-local`) + +The update endpoint answers 200 for `darwin-arm64`, `darwin-x64`, `darwin-universal`, +`win32-x64`, `win32-arm64`, `linux-x64`, `linux-arm64` (3.18.25, 2026-09-02). Windows ships +a system-setup installer, Linux an AppImage. Product identity is shared with regular Cursor +(`applicationName: cursor`, `dataFolderName: .cursor`, same bundle id), so the two builds +share `~/Library/Application Support/Cursor` / `%APPDATA%\Cursor` / `~/.config/Cursor` +unless launched with `--user-data-dir`. + +Configuration surfaces (same on all platforms): Settings → Models → Gateway +(Base URL, API Key), or env `CURSOR_LOCAL_AGENT_BASE_URL`, `CURSOR_LOCAL_AGENT_API_KEY`, +`CURSOR_LOCAL_AGENT_HEADERS`. Env is read via the shell-environment service, so a +GUI-launched app needs the variable in the login environment, not just an interactive rc file. + +Cursor sign-in is still required (login wall before the gateway modal). Cursor's own catalog, +Tab completion and cloud agents are unavailable in local mode. + +## Distribution stance + +Cursor does not document this build (docs, changelog, staff forum answers through 2026-08 all +say inference is cloud-side). The guide must describe how to use the build if the user +already has it and must not host, link or script its download. + +## Verifiers (PLAN-VERIFIER-REAL-01, run 2026-09-02) + +| Command | Exit | Reads the change target? | +|---|---|---| +| `bun run typecheck` | 0 (baseline) | yes — tsc over `src/**` incl. `src/server/index.ts` | +| `bun test tests/grok-models-effort-list.test.ts` | 0 (baseline) | yes — starts the server and fetches `/v1/models` | +| `bun test tests/cursor-local-models-schema.test.ts` | n/a (new in 010) | yes — asserts the new fields | +| `bun run privacy:scan` | 0 (baseline) | reads docs-site + devlog | +| docs guide | — | human review + `rg downloads.cursor.com docs-site` must return 0 hits | + +Full `bun run test` is forbidden by the user for this unit; exact-head CI is the gate. diff --git a/devlog/_plan/260902_cursor_local_models_schema/005_audit_round1.md b/devlog/_plan/260902_cursor_local_models_schema/005_audit_round1.md new file mode 100644 index 0000000000..89cc15f97c --- /dev/null +++ b/devlog/_plan/260902_cursor_local_models_schema/005_audit_round1.md @@ -0,0 +1,18 @@ +# 005 — Audit round 1 (wp1 roadmap) + +Reviewer: independent subagent, Claude Opus 5 (decorrelated from the planning model). +Verdict: **GO-WITH-FIXES (blockers=4)**. Disposition of each item: + +| # | Finding | Disposition | Where folded | +|---|---|---|---| +| 1 | `tests/server-combo-failover-e2e.test.ts:824-846` uses `toEqual` on complete row literals; the new keys would fail six assertions | folded | 000 consumer list; 010 file map (MODIFY → `toMatchObject` + explicit `is_combo` absence) | +| 2 | Plan's focused test list would not have caught #1 under the no-full-suite constraint | folded | 010 accept criteria now enumerate all nine raw-list consumers | +| 3 | Config-key verification pointed at the `src/types.ts` barrel | folded | 010 cites `src/types/provider.ts:362/364/440/442`; fallback clause deleted | +| 4 | Base SHA `6fe46312c` stale; `origin/dev` is `85f7ef92a` | folded | 000 + 010 re-anchored; branch created from `origin/dev` | +| n1 | `anthropic_messages` is safe only because OpenAI-family types are also advertised | folded | 010 helper comment + unit-test line | +| n2 | Prefer a static import of the helper over `await import` | folded | 010 index.ts diff | +| n3 | DEV-STACK-01: two-layer stack justified on revert independence | accepted | 020 unchanged | + +Verifier evidence at this round: `bun run typecheck` exit 0; `bun test tests/grok-models-effort-list.test.ts` +5 pass (starts a server and fetches `/v1/models`); `bun run privacy:scan` fails on `tests/provider-key-store.test.ts:41-42` +at baseline dev, unrelated to this unit (verified by stashing the unit and rerunning). diff --git a/devlog/_plan/260902_cursor_local_models_schema/010_layer1_models_capabilities.md b/devlog/_plan/260902_cursor_local_models_schema/010_layer1_models_capabilities.md new file mode 100644 index 0000000000..c5b32d9618 --- /dev/null +++ b/devlog/_plan/260902_cursor_local_models_schema/010_layer1_models_capabilities.md @@ -0,0 +1,195 @@ +# 010 — Layer 1: advertise `api_types` + `capabilities` on the raw `/v1/models` list + +Branch: `codex/cursor-local-models-schema` (base: `origin/dev` at `85f7ef92a`, created with +`git switch -c codex/cursor-local-models-schema origin/dev`). +PR 1 of the stack, targets `dev`. Thesis: one additive schema change on the OpenAI-shape +model list so Cursor's local-agent runtime detects extended capabilities. + +## File change map + +| Path | Action | Why | +|---|---|---| +| `src/server/models-capabilities.ts` | NEW | Pure helper: build the `api_types` + `capabilities` fields from catalog data. Keeps `index.ts` from growing another inline lambda. | +| `src/server/index.ts` | MODIFY | Spread the helper's output into `nativeModelRow` and the routed-row object in the raw-list branch. | +| `tests/cursor-local-models-schema.test.ts` | NEW | Regression test: server start, GET `/v1/models`, assert schema on a native row and a routed row; assert Grok fields unchanged; assert `OPENCODEX_MODEL_API_TYPES` keeps an OpenAI-family member (load-bearing: Cursor routes to the Messages wire only when NO OpenAI-family type is present). | +| `tests/server-combo-failover-e2e.test.ts` | MODIFY | Six `toEqual` row literals at :824-846 become `toMatchObject`; keep `is_combo` absence explicit (`expect(row.is_combo).toBeUndefined()`) so the combo-off path stays verified. | + +Scope OUT: the Codex-catalog `{ models: [...] }` branch, Claude gateway branch, GUI, docs (020). + +## `src/server/models-capabilities.ts` (NEW) + +```ts +/** + * Extended capability advertisement for the OpenAI-shape `GET /v1/models` list. + * + * Cursor's local-agent runtime (the "Private Inference" build) only enables its reasoning + * effort control when at least one row in `data[]` carries `api_types` (a non-empty array + * naming an API family it can speak) and, optionally, a `capabilities` object. Plain OpenAI + * clients ignore both keys. Every OpenCodex route serves chat completions, Responses and + * Anthropic Messages, streams, and accepts tool calls, so those are constants; context and + * vision come from catalog data when known and are omitted otherwise. + */ +// Membership is load-bearing for Cursor: its selector picks the Anthropic Messages wire only +// when NO OpenAI-family type (chat_completions/responses/openai_chat/openai_responses) is +// present. Keep at least one OpenAI-family entry. Guarded by a unit test. +export const OPENCODEX_MODEL_API_TYPES = ["chat_completions", "responses", "anthropic_messages"] as const; + +export interface ModelCapabilityInput { + reasoningEfforts?: readonly string[]; + contextWindow?: number; + inputModalities?: readonly string[]; +} + +export interface ModelCapabilityFields { + api_types: readonly string[]; + capabilities: { + context_length?: number; + supports_tool_use: true; + supports_streaming: true; + supports_reasoning: boolean; + supports_vision?: boolean; + reasoning_effort?: string[]; + }; +} + +function positiveInt(value: unknown): number | undefined { + return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.floor(value) : undefined; +} + +export function modelCapabilityFields(input: ModelCapabilityInput): ModelCapabilityFields { + const efforts = (input.reasoningEfforts ?? []).filter(e => typeof e === "string" && e.length > 0); + const contextLength = positiveInt(input.contextWindow); + const modalities = input.inputModalities; + const supportsVision = Array.isArray(modalities) ? modalities.includes("image") : undefined; + return { + api_types: OPENCODEX_MODEL_API_TYPES, + capabilities: { + ...(contextLength !== undefined ? { context_length: contextLength } : {}), + supports_tool_use: true, + supports_streaming: true, + supports_reasoning: efforts.length > 0, + ...(supportsVision !== undefined ? { supports_vision: supportsVision } : {}), + ...(efforts.length > 0 ? { reasoning_effort: [...efforts] } : {}), + }, + }; +} +``` + +## `src/server/index.ts` (MODIFY, raw-list branch ~L1481-1553) + +Before: + +```ts + const nativeModelRow = (id: string, metadataId = id) => ({ + id, + object: "model", + created: 0, + owned_by: "openai", + ...grokEffortFields( + nativeReasoningEfforts(metadataId), + nativeDefaultReasoningEffort(metadataId), + ), + }); +``` + +After: + +```ts + // modelCapabilityFields is a static import at the top of index.ts (pure helper, no + // startup side effects). nativeOpenAiContextWindow / nativeInputModalities join the + // existing catalog destructuring at ~L1347. + const nativeLimits = nativeContextLimits(config); + const nativeModelRow = (id: string, metadataId = id) => ({ + id, + object: "model", + created: 0, + owned_by: "openai", + ...grokEffortFields( + nativeReasoningEfforts(metadataId), + nativeDefaultReasoningEffort(metadataId), + ), + // Cursor local-agent discovery (Private Inference build) reads api_types + + // capabilities; other OpenAI clients ignore them. See src/server/models-capabilities.ts. + ...modelCapabilityFields({ + reasoningEfforts: nativeReasoningEfforts(metadataId), + contextWindow: nativeOpenAiContextWindow(metadataId, nativeLimits), + inputModalities: nativeInputModalities(metadataId), + }), + }); +``` + +`nativeOpenAiContextWindow` and `nativeInputModalities` are added to the existing `await import("../codex/catalog")` +destructuring at the top of the branch (both re-exported by `src/codex/catalog.ts:5`, verified). Native modalities come from the pinned upstream entry via `nativeInputModalities` (metadata.ts). + +Routed row, before: + +```ts + ...(effective ? { alias_of: `${provider?.alias || m.provider}/${effective.alias}` } : {}), + ...grokEffortFields(m.reasoningEfforts ?? [], m.defaultReasoningEffort), + }; +``` + +After: + +```ts + ...(effective ? { alias_of: `${provider?.alias || m.provider}/${effective.alias}` } : {}), + ...grokEffortFields(m.reasoningEfforts ?? [], m.defaultReasoningEffort), + ...modelCapabilityFields({ + reasoningEfforts: m.reasoningEfforts, + contextWindow: m.contextCap ?? m.contextWindow, + inputModalities: m.inputModalities, + }), + }; +``` + +`contextCap` wins over `contextWindow` because it is the operator-narrowed effective limit +(`CatalogModel.contextCap`, `parsing.ts:117`). + +## `tests/cursor-local-models-schema.test.ts` (NEW) + +Modelled on `tests/grok-models-effort-list.test.ts` (same fixture: `kimi` provider with +`liveModels: false`, seeded native entitlements, `OPENCODEX_HOME` tmp dir, `SERVER_BUDGET_MS`). + +Assertions: + +1. Routed row `kimi/k3` (config: `modelReasoningEfforts.k3 = ["low","high","max"]`, + `modelContextWindows.k3 = 200000`, `modelInputModalities.k3 = ["text","image"]`): + - `api_types` equals `["chat_completions","responses","anthropic_messages"]` + - `capabilities` equals `{ context_length: 200000, supports_tool_use: true, supports_streaming: true, supports_reasoning: true, supports_vision: true, reasoning_effort: ["low","high","max"] }` + - Grok fields still present and unchanged: `supports_reasoning_effort === true`, + `reasoning_efforts[1].default === true`. +2. Routed row `kimi/kimi-for-coding` (no efforts): `capabilities.supports_reasoning === false`, + no `reasoning_effort` key. +3. Native row `gpt-5.6-sol`: `api_types` present; `capabilities.reasoning_effort` equals + `nativeReasoningEfforts("gpt-5.6-sol")`; `capabilities.context_length` is a positive number. +4. Unit test of `modelCapabilityFields` directly: empty input yields + `{ api_types, capabilities: { supports_tool_use: true, supports_streaming: true, supports_reasoning: false } }` with no + `context_length`/`supports_vision`/`reasoning_effort` keys (activation scenario for the + omit branches, C-ACTIVATION-GROUNDING-01). + +Config keys confirmed on `OcxProviderConfig` in `src/types/provider.ts`: `modelContextWindows` +(:362), `modelInputModalities` (:364), `modelReasoningEfforts` (:440), +`modelDefaultReasoningEfforts` (:442). No fallback needed. + +## Accept criteria + +- `bun run typecheck` exit 0. +- Focused set covering every raw-list consumer (audit blocker 2): `bun test tests/cursor-local-models-schema.test.ts tests/grok-models-effort-list.test.ts tests/server-combo-failover-e2e.test.ts tests/claude-models-discovery.test.ts tests/server-auth.test.ts tests/ollama-native.test.ts tests/provider-outbound.test.ts tests/codex-catalog.test.ts tests/gui-management-session.test.ts` exit 0. The full suite stays forbidden; exact-head CI is the gate. +- Restarted local proxy: `curl -s http://127.0.0.1:10100/v1/models | jq '.data[] | select(.id=="gpt-5.6-sol") | {api_types, capabilities}'` shows both keys. +- Live: Cursor Private Inference → Settings → Models → "Refresh model list" → composer + model row for `gpt-5.6-sol` shows a Reasoning control with low/medium/high/xhigh. + Screenshot saved as `devlog/_plan/260902_cursor_local_models_schema/011_effort_control.png`. +- Live: send a turn with effort `high`; `ocx observe logs --json` last `gpt-5.6-sol` row + shows the effort (field name to record at C — the log has `requestedEffort`/`reasoningEffort` + or similar; if the log does not carry it, capture via `ocx debug provider on` request dump). + +## Bypass / enforcement (PLAN-BYPASS-NAMED-01) + +Not an enforcement change; no gate added. n/a. + +## Field chain (PLAN-FIELD-CHAIN-01) + +New output keys only (`api_types`, `capabilities`). Creation: helper. Serialization: +`jsonResponse`. Deserialization: none server-side (`N/A` — inbound consumers are external +clients). Consumers: Cursor local runtime (target), Grok Build (ignores), Codex (uses the other +branch), Claude gateway (other branch), GUI API-keys page (`gui/src/pages/ApiKeys.tsx:170`) reads this list and only displays ids — additive keys are ignored. diff --git a/devlog/_plan/260902_cursor_local_models_schema/011_effort_control.png b/devlog/_plan/260902_cursor_local_models_schema/011_effort_control.png new file mode 100644 index 0000000000000000000000000000000000000000..befe68ad08f99047ceefe69e88014973d8ca0ef0 GIT binary patch literal 43517 zcmeFZ1z227x-Pn!#@*dLxVyWA;BLV!xVt2f1eX8-f&>T>XbA3v;7;(+NN|D$mjHp& zN#@T#Gka$4-uIrf&z|SG)l{!{)%xmN^;K8Zdh4rN+$mk0Mp3RpD{{2Twgc()G-Y1q4Zxq8~W zx>56TZ~;Ox%4+aGG{C}7%>g%+hHg#_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Q4HM~7vS_-JfU0N&+D8<6{Kz#zZZeRB! znb+rNB5pim+0BH+51g(8WFNfavH(VfK{&IbTWWiK2!M!Wyxooirrdq;gd%Tif`ZP+ zSYu+=W`lE^^#V`}82q?wbuIWxu9xEQUV5Izv}DX63}amM*-0MrhOMdWtlb5A7!fImQETY3bsGG{DCO3d(v?aadX{^L1E~eVTx^DlS{nor<|(gAIR0H z7i1f4&Nn*&tuHH={VWZ;X$^?p0<#ixEHr+Q1G`PD>FBDNkud; zd|A9OH)lx6RI@}j(A~Qk`leSdG<%rv8pVSg^rGmtO9^K*I;d&A%9)zDGR&ULxm3=# x`_VhEFVj7me%G5t-E3O07>Q$pV8j6?bs~Te+C<6p)^q{63;^iN!udJ>zW}W559|N{ literal 0 HcmV?d00001 diff --git a/devlog/_plan/260902_cursor_local_models_schema/012_effort_ladder.png b/devlog/_plan/260902_cursor_local_models_schema/012_effort_ladder.png new file mode 100644 index 0000000000000000000000000000000000000000..0ee14b531fa4a086b8a4d2f838f15402ea9100cb GIT binary patch literal 45986 zcmeFZ1z227x-Pn!#@*dLxVyWA;BLV!xVt2f1eX8-f&>T>XbA3v;7;(+NN|D$mjHp& zN#@T#Gka$4-uIrf&z|SG)l{!{)%xmN^;K8Zdh4rN+$mk0Mp3RpD{{2Twgc()G-Y1q4Zxq8~W zx>56TZ~;Ox%4+aGG{C}7%>g%+hHg#_bz2Z?p86Rlx1bjHMG_2qN=oS_pb^w4b1xu&)v9g2dL4OFtTrSoQ zE-;)3!xGNUuGTR85r&yzGHx*ZL%)#aU+J;?D{Nt5`CF%jh23xb%NAHp*p1~JJl!5y z`2Rfl-}&YI$QxE)KLa!D8{OVRNegy_wH(l)ldI})>|*&q{f~49n00?(dv7J(Kd_aj zEUYen<@J1|t^5bJwvbc(1KYUBO8-qq_uvog>7@O;YytMNztcUuwSL=W z7)HSua}3;?{qgWjo)?XXrc0>Km5!Kr~}r3 zKi~=b*uaj|fFd9a@B-QZtc82o_`%vU0LZ$!`Fl9n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brN)d@Cdt_R?tZ)M7|);Ev`vR!wMu)M3US@s~qjM_ij)y z8)@ZDchUSe_A#A43V8xyo)K^=EziuxwfcDc2N<&ef@ zjU*^~sbvQT>6}*|AH%N;M|8IW2~0}NeI|aJEZju(^)eS*46SG@Ht^Hckc`01lPdd@S)T--s=BXlIgn;jq0h+evck zB<&GJVZG1=VhQRq$9n1Y&jWaOU%dX%y@o`(DyY*zD@4w#&^FVC$6`iDV_O<7g{63U z&+Ix(0vL-IZc8({>deebF+QZw1iH1=^_7ITT6M46CWr>|H*9B7_kx@sn;i#p#bD?@ zEngDeGU-;5{e~38_cN)8tp5P>$x((_ukjv(-n$9_1Asp&04d>eI#8&`wgp%K0f9gt F|Jf_KhSLB5 literal 0 HcmV?d00001 diff --git a/devlog/_plan/260902_cursor_local_models_schema/013_high_turn.png b/devlog/_plan/260902_cursor_local_models_schema/013_high_turn.png new file mode 100644 index 0000000000000000000000000000000000000000..4c230a611e14f0ec8f743f9b43b7c65e92d69c3e GIT binary patch literal 49788 zcmeFY1ymi)wl3O>g}b{;AZTzx&=5RGa3=(JcZc8(0RjXG?u6h3cPF^Jy9HPY_Ez%$ z$v(388Ry+`-+1qhdupxf{(5%R?3%M?RnL-sn0;6UFl8j9B>)fz0HmNl;9(6A5qC2; z0RTBUfDr%yL;w_G4!}YYGzmZqjQ{|X1@oH%mdyhH!M|=k907caX0}eYj%K!Yl$@+j z0KVtaaWhL7pL!3G9z{_r#~F#Uu7k_DO*`rsGlj&@cC?!PYn zcOI;*oT2*q6&RpLR5J%DCFmJyIiOQZTiHL@=8e3hiC= zj#kRjzp;_Q3)$b;*hWnBH#RYs_>;!QO8%!!{CcjrleEfjZ0snm{2M#iD*Y*giNinm z7&t&v{GHd?QuPmiGh^vLJUz@*)PJXOF;|oSjU6qO|CG(cOzaQc!CC2#T*fw%≀k zagg|<6Kh8(_3!7JnM?o4>tv<^ZJU3|VrcM>@*CTT{)4BZ#-Fko8NU$!o!;0+^^d%E zPS5{5%g*Xoo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2`^i*=1Qj*+W}Wcw!dh;l;!RdNAw9>7UMJO!!ShyihA$-}+C~IwnZoqRC%r5D zHTZijk7?oQd^M#$nPDN8*3qpaFvBBtWJ0(Md!GI11Lltid{*&Jt?_lE)@HkX7wyq# z)=LG-LkyBhVI9z#OCu4L)=kWO!ywpjFh@1^ZF9rIJ{Dbdd``MWkk!&50^bM|?IIvB z5J`=}K;s$dT@3&go2=f+r)f8r@TT{-h2&NBDl#idk(FIaFa=m(gUBGz2hH~r_`b^F zb;z~HnO98y)Rsq=5CT9e5sN!aY);XI1a;k>E9jd^+UC|9mO~kqG?1a{rIZ{Wrg2<+ ze-6AY9n#zgB{3`ci)zz3wO7vUh>6?RoPFH(uHt|`dGR-mAx|4?8b-T!sM}d-!r3jB z!sa_=W|lDQ(r+rv**F+%06JG;@Uz6Xejv5gp`JabTY1Y4Nnjjm<-LaiV-U@JjY;+yZ6@#GowR}r>%cWXN_83zW t+|H&Vv;P3kB}N%yzr=bEdh93w4gmb90H=k>=|ZC(*cD&^1_c0r|Ji?E#Y+GH literal 0 HcmV?d00001 diff --git a/devlog/_plan/260902_cursor_local_models_schema/015_layer1_live_evidence.md b/devlog/_plan/260902_cursor_local_models_schema/015_layer1_live_evidence.md new file mode 100644 index 0000000000..aefb952716 --- /dev/null +++ b/devlog/_plan/260902_cursor_local_models_schema/015_layer1_live_evidence.md @@ -0,0 +1,24 @@ +# 015 — Layer 1 live evidence (Cursor Private Inference 3.18.25, darwin-arm64) + +Setup: worktree at `49d4447a0` + the `output_modalities` fix (committed next), isolated +scratch proxy (not the machine service on :10100) with a request tap in front logging +request bodies, Cursor gateway Base URL through the tap, isolated `--user-data-dir`. + +## What the first attempt taught (activation grounding) + +With `api_types` + `capabilities` but no `output_modalities`, Cursor still showed no control. +Reading `fetchLocalProviderModels` (`vye`) in `cursor-agent-exec/dist/main.js`: once a row +carries `api_types`, the runtime keeps it only if `capabilities.output_modalities` includes +`"text"` (`Tye`/inline filter) and `supports_tool_use === true`. Rows failing that filter are +dropped from the enriched picker, so the whole list fell back to plain names. Adding +`output_modalities: ["text"]` fixed it. Also: Cursor caches `/models` per base-URL string +(`npe` map), so a changed schema needs a different base URL or an app restart to re-fetch. + +## Evidence + +- Tap log: `GET /v1/models auth=yes ua=Cursor/3.18.25 -> 200 rows=16 sol_api_types=["chat_completions","responses","anthropic_messages"]` +- Composer picker after refresh: `gpt-5.6-sol Medium` → menu "Reasoning: Medium / Model: gpt-5.6-sol" → Reasoning options **Low / Medium / High / Extra High** (Cursor's GPT-5.6 table; ocx's max/ultra are not exposed, as documented). Screenshots: `011_effort_control.png`, `012_effort_ladder.png`, `013_high_turn.png`. +- Selected High, sent "Reply with exactly the word HIGHPONG2": tap logged + `REQ /v1/responses model= gpt-5.6-sol reasoning= {"effort":"high"} keys= model,input,store,tools,tool_choice,stream,reasoning` → `POST /v1/responses -> 200`; reply `HIGHPONG2`. +- Because `api_types` advertises `responses`, Cursor switched from `/chat/completions` to + `/v1/responses` for this gateway (inbound protocol `responses`, `reasoning.effort` form). diff --git a/devlog/_plan/260902_cursor_local_models_schema/016_layer1_check.md b/devlog/_plan/260902_cursor_local_models_schema/016_layer1_check.md new file mode 100644 index 0000000000..96c35a1dd6 --- /dev/null +++ b/devlog/_plan/260902_cursor_local_models_schema/016_layer1_check.md @@ -0,0 +1,22 @@ +# 016 — Layer 1 check (wp2 C) + +Tip: `f1a9a2c43` on `codex/cursor-local-models-schema` (2 commits over `origin/dev` `85f7ef92a`). + +| Check | Result | +|---|---| +| `bun run typecheck` | exit 0 | +| focused set (9 files: cursor-local-models-schema, grok-models-effort-list, server-combo-failover-e2e, claude-models-discovery, server-auth, ollama-native, provider-outbound, codex-catalog, gui-management-session) | 496 pass / 0 fail, receipt `.codexclaw/evidence//test-receipt.json` | +| extra consumers found by `rg 'object: "model"'`: ollama-show-enrichment, catalog-llamacpp-capabilities | upstream fixtures, not readers of our list; ran anyway: 25 pass | +| `bun run privacy:scan` | passed | +| `bun run skill:surface:check` | current | +| live Cursor Private Inference | Reasoning Low/Medium/High/Extra High shown; High turn → `reasoning.effort: "high"` on `/v1/responses` (015) | + +Adversarial review: an Opus reviewer was dispatched with the diff and a six-point checklist +but produced no output in ~7 minutes and was retired (DISPATCH-RETIRE-01). The main session ran +the same checklist directly: no other test asserts full row equality on the raw list; the +`api_types` OpenAI-family invariant is unit-tested; native rows always get +`supports_vision: true` via `nativeInputModalities`'s text+image fallback (documented in 010); +routed rows with unknown modalities omit `supports_vision`/`input_modalities`; `contextCap` +precedence over `contextWindow` exposes only the operator-narrowed limit already visible on +the Codex catalog branch; the combo e2e rewrites keep `is_combo` presence and absence explicit. +Full suite deliberately not run (user constraint); exact-head CI is the gate at publish. diff --git a/devlog/_plan/260902_cursor_local_models_schema/017_impl_review.md b/devlog/_plan/260902_cursor_local_models_schema/017_impl_review.md new file mode 100644 index 0000000000..9400811e17 --- /dev/null +++ b/devlog/_plan/260902_cursor_local_models_schema/017_impl_review.md @@ -0,0 +1,20 @@ +# 017 — Implementation review (late-arriving, folded at wp3) + +The Opus implementation reviewer dispatched at wp2 C returned after the retirement window +(reported in 016). Its verdict was **GO-WITH-FIXES (blockers=1)**; the findings were real and +are folded here rather than discarded. + +| # | Finding | Severity | Disposition | +|---|---|---|---| +| 1 | `contextCap ?? contextWindow` over-reports `context_length`: `contextCap` is the raw operator knob and is set even when the cap did not bite (`provider-fetch.ts:747`; fixture `codex-catalog.test.ts:5739` shows 64k window / 350k cap) | High | folded — commit `379d95fd7` uses `m.contextWindow`; regression test added and shown red before the fix | +| 2 | native rows always claim vision via `nativeInputModalities` fallback; routed rows omit the key when unknown (matches `config-export.ts:1323`) | Medium | accepted as-is; already documented in 010 | +| 3 | `api_types` shared mutable array leaked into every row | Low | folded — frozen constant, copied per row | +| 4 | no test drove the `contextCap` vs `contextWindow` divergence | Medium | folded — new test with `providerContextCaps: { kimi: 350000 }` | + +Confirmed clean by the reviewer: no other strict row-shape assertions in `tests/`, GUI +`classifyExternalModel` reads keys by name, `toMatchObject` rewrites preserve cardinality, +values and `is_combo` absence, static import placement is consistent, privacy scan passes. + +Layer 2 (`codex/cursor-private-inference-guide`) was rebased onto the new layer-1 tip +(DEV-STACK-02); `git log codex/cursor-local-models-schema..codex/cursor-private-inference-guide` +shows only the docs commit. diff --git a/devlog/_plan/260902_cursor_local_models_schema/020_layer2_docs_guide.md b/devlog/_plan/260902_cursor_local_models_schema/020_layer2_docs_guide.md new file mode 100644 index 0000000000..726b86d901 --- /dev/null +++ b/devlog/_plan/260902_cursor_local_models_schema/020_layer2_docs_guide.md @@ -0,0 +1,83 @@ +# 020 — Layer 2: docs guide "Cursor Private Inference" + stack publish + +Branch: `codex/cursor-private-inference-guide` (base: `codex/cursor-local-models-schema`). +PR 2 of the stack, targets the layer-1 branch; retarget to `dev` after PR 1 lands. +Thesis: a connector guide so a user who already has the Private Inference build can point it +at OpenCodex on macOS, Windows and Linux, and understand the limits. + +## File change map + +| Path | Action | +|---|---| +| `docs-site/src/content/docs/guides/cursor-private-inference.md` | NEW | +| `docs-site/src/content/docs/guides/integrations.md` | MODIFY — add a short "Cursor Private Inference" pointer paragraph after the Aside paragraph (this client is configured inside Cursor, not by the Integrations tab; say so). | +| `docs-site/astro.config.mjs` | MODIFY — the Guides sidebar is an explicit list (L84-92); add `{ label: "Cursor Private Inference", slug: "guides/cursor-private-inference" }` after the Factory Droid Bridge entry (label translations optional; ko: "Cursor Private Inference"). | + +Scope OUT: locales (fr/ja/ko/ru/tr/zh-*); they must not contradict, and absence is fine. + +## Guide content (diff-level outline; final prose written at B) + +Front matter: `title: Cursor Private Inference`, `description: Use OpenCodex-routed models inside Cursor's local-agent build without a tunnel.` + +Sections, in order: + +1. **What this is.** Cursor ships a second desktop build, "Cursor Private Inference", whose + agent runs locally and calls an OpenAI-compatible gateway you configure. Regular Cursor + cannot do this: its backend calls your endpoint, so loopback and LAN URLs are rejected and + a public HTTPS tunnel is required (link the existing tunnel note in `reference/cli.md`). + The Private Inference build is not documented by Cursor, may change or disappear, and + OpenCodex does not distribute it — no download link. If you do not have it, use the + community bridge (`npx ocx-cursor`) with a tunnel instead. +2. **What you give up.** Cursor sign-in still required; Cursor's own model catalog, Tab + completion and cloud agents are unavailable in local mode; every turn carries Cursor's + local system prompt (~23k tokens) — budget accordingly. +3. **Configure the gateway.** Two equivalent ways: + - Settings → Models → Gateway → Base URL `http://127.0.0.1:10100/v1`, API Key: the + value from `~/.opencodex/service-api-token` when the service uses API auth, otherwise + any placeholder (loopback needs no key). Click "Refresh model list". + - Environment: `CURSOR_LOCAL_AGENT_BASE_URL`, `CURSOR_LOCAL_AGENT_API_KEY`, + optional `CURSOR_LOCAL_AGENT_HEADERS`. + Per-OS env mechanics (the app is GUI-launched; interactive shell rc files are not read): + - macOS: `launchctl setenv CURSOR_LOCAL_AGENT_BASE_URL http://127.0.0.1:10100/v1` (session + only) or a LaunchAgent `EnvironmentVariables`; or start from a terminal. + - Windows: `setx CURSOR_LOCAL_AGENT_BASE_URL http://127.0.0.1:10100/v1` (user scope; new + processes only) or System Properties → Environment Variables. + - Linux: `~/.profile` / `~/.pam_environment` or `systemctl --user set-environment`, then + relaunch; AppImage launched from a terminal inherits the shell env. + Base URL must include `/v1`; `http://` loopback is accepted, no TLS needed. +4. **Keep the two builds apart.** Same app id and data folder as regular Cursor + (`~/Library/Application Support/Cursor`, `%APPDATA%\Cursor`, `~/.config/Cursor`). Launch + with `--user-data-dir

` to isolate, and disable "Import data from existing Cursor + installation" on first run if you do not want it to copy your settings. +5. **Models and reasoning effort.** The picker lists OpenCodex's `/v1/models`. The effort + control appears when OpenCodex advertises capabilities (v2.41+, layer 1) **and** the model + id matches Cursor's built-in table. Table: GPT-5.6 Sol/Terra/Luna low..xhigh (Max/Ultra + not exposed); Claude Opus 5 / Sonnet 5 low..max; Grok 4.x minimal..xhigh; Gemini + minimal..high; Claude Fable 5.1, Kimi K3 and other ids get no control — set a default + effort in OpenCodex instead (`modelDefaultReasoningEfforts`). Cursor matches on the part + after the last `/`, so `anthropic/claude-opus-5` works. +6. **Verify.** `ocx observe logs` shows rows with `inboundProtocol: chat` and + `admissionKind: loopback`. Troubleshooting: 401 → key mismatch with + `OPENCODEX_API_AUTH_TOKEN`; empty picker → Refresh model list / check `ocx models`; + no effort control → check the id against the table above and that `/v1/models` rows + carry `api_types`. + +Constraint: `rg -n 'downloads.cursor.com|cursor-local/' docs-site/src/content/docs/guides/cursor-private-inference.md` must return 0 hits. + +## Stack publish steps (B of wp3) + +1. On layer 1: `git push -u origin codex/cursor-local-models-schema` (hooks run; no `--no-verify`). +2. `gh pr create --base dev --head codex/cursor-local-models-schema` with the repo template + (Summary / Verification / Checklist) and the DEV-STACK-03 map. +3. On layer 2: `git push -u origin codex/cursor-private-inference-guide`; + `gh pr create --base codex/cursor-local-models-schema --head codex/cursor-private-inference-guide`. +4. Wait for CI on the exact head SHA of each PR (`gh pr view --json headRefOid,statusCheckRollup`); + green rollup is the accept criterion. No merge. + +## Accept criteria + +- Guide renders in the docs build: `cd docs-site && bun run build` exit 0 (verifier — run at B; + if the docs build is too slow locally, CI's docs job is the gate and this becomes human review). +- `bun run privacy:scan` exit 0. +- Both PRs open, correct bases, CI green on head SHA. + diff --git a/devlog/_plan/260902_cursor_local_models_schema/021_pr_rollup.json b/devlog/_plan/260902_cursor_local_models_schema/021_pr_rollup.json new file mode 100644 index 0000000000..1bcabb70ab --- /dev/null +++ b/devlog/_plan/260902_cursor_local_models_schema/021_pr_rollup.json @@ -0,0 +1,2 @@ +{"base":"dev","failing":[],"head":"bc186b59e866da130b22fbcebf106b267ce17ad1","number":3230,"rollup":{"SKIPPED":1,"SUCCESS":26}} +{"base":"codex/cursor-local-models-schema","failing":[],"head":"af8b45cb53ab469848274922709f28b42eb96873","number":3231,"rollup":{"SKIPPED":8,"SUCCESS":9}} diff --git a/devlog/_plan/260902_cursor_local_models_schema/022_stack_closeout.md b/devlog/_plan/260902_cursor_local_models_schema/022_stack_closeout.md new file mode 100644 index 0000000000..8e3cb64574 --- /dev/null +++ b/devlog/_plan/260902_cursor_local_models_schema/022_stack_closeout.md @@ -0,0 +1,24 @@ +# 022 — Stack closeout (wp3 D) + +| PR | Base | Head | CI rollup | +|---|---|---|---| +| #3230 feat(server): advertise api_types and capabilities on the raw /v1/models list | `dev` | `bc186b59e` | 26 success, 1 skipped, 0 failing (Linux 4 shards, macOS, Windows/macOS/Ubuntu npm-global, keyring x3, gates, storage policy, api usage, enforce-target) | +| #3231 docs: Cursor Private Inference connector guide | `codex/cursor-local-models-schema` | `af8b45cb5` | 9 success, 8 skipped, 0 failing | + +Raw `gh pr view --json` output: `021_pr_rollup.json`. + +Pushes used `--no-verify` per the user's clarified instruction (skip the local full suite; CI on +the exact head is the gate). The repo's `prepush` script runs the full suite, which is why the +hook had to be bypassed rather than run. + +Late fold-ins after the first push: CodeRabbit's `positiveInt(0.5) → 0` finding (commit +`bc186b59e`, regression assertion added); layer 2 was cascaded onto the new layer-1 tip +(`git rebase`, `--force-with-lease`) so `git log layer1..layer2` shows only the docs commit. + +Not done, by design: no merge (user authorised opening PRs only). After #3230 lands, retarget +#3231 to `dev`. Terminal outcome for this unit: **DONE** for the three work-phases; merging is +the next human action. + +Loose ends outside the repo: the Cursor Private Inference spike app/profile under `/tmp` +were scratch only. Do not start a sibling proxy against the machine OpenCodex home; +the service port stays 10100. diff --git a/devlog/_plan/260902_cursor_local_models_schema/030_max_mode_context_selector.md b/devlog/_plan/260902_cursor_local_models_schema/030_max_mode_context_selector.md new file mode 100644 index 0000000000..6c3acdbe51 --- /dev/null +++ b/devlog/_plan/260902_cursor_local_models_schema/030_max_mode_context_selector.md @@ -0,0 +1,66 @@ +# 030 — "Max": what regular Cursor shows vs what the local runtime can show + +The user asked why the picker has no "Max". Two different things carry that name. + +## Reasoning-effort max / ultra: not reachable + +cursor-agent-exec/dist/main.js builds the Reasoning ladder from a hard-coded regex table +(b[], 000 §4). For gpt-5.6-(luna|sol|terra) it is ["low","medium","high","xhigh"]. The +gateway's capabilities.reasoning_effort only decides whether supports_reasoning is true; the +values are never read into the ladder. So opencodex's max/ultra cannot appear without a +Cursor-side change. Document, do not fight. + +## Max Mode (long context): reachable as a "Context" selector + +Regular Cursor's "Max" toggle is Max Mode = larger context window. The local runtime has the +same concept: J() adds a **Context** parameter (id:"context") with two values when +longContextThresholdTokens < contextLength: + + s = ({contextLength:t, longContextThresholdTokens:n}) => (t===undefined||n===undefined||n>=t) ? undefined : {defaultTokens:n, longTokens:t} + // values: [{value:String(defaultTokens), displayName:"272K"}, {value:String(longTokens), displayName:"922K", increasesModelCost:true}] + +On send, the chosen value caps the request's context length (R="context" lookup in +modelParameters, then Math.min(chosen, contextLength)). The threshold comes from +long_context_threshold_tokens, which the row parser cme() derives, in order, from: + +1. cost.long_context.threshold_tokens — BUT cost must pass zod mme (numbers or + record-of-numbers only); a nested object fails it and the row loses + extendedCapabilitiesDetected. Unusable. +2. capabilities.cost.long_context.threshold_tokens — same schema, same failure. +3. pricing.overrides[].min_prompt_tokens (smallest positive) — pricing is not in the schema, + so validation ignores it and only this reader sees it. **This is the encoding.** + +So each row gains, when the catalog knows a default window and a larger opt-in window: + + "pricing": { "overrides": [ { "min_prompt_tokens": 272000 } ] } + +## Data + +- Native GPT-5.6 family: default NATIVE_GPT56_CONTEXT_WINDOW 272_000, opt-in + nativeOpenAiMaxInputTokens(slug, limits) 922_000 (metadata.ts:130-142, 281). For the + selector: context_length = opt-in window (922k), threshold = default window (272k) when + they differ. +- Routed rows: no separate opt-in window in CatalogModel today (contextWindow only; + maxInputTokens is a hard input cap, not a long-context tier). No threshold → no selector, + which matches what a plain OpenAI gateway would advertise. + +## File change map + +| Path | Action | +|---|---| +| src/server/models-capabilities.ts | MODIFY — ModelCapabilityInput.longContextWindow?; when longContextWindow > contextWindow, set capabilities.context_length = longContextWindow and add pricing.overrides[{min_prompt_tokens: contextWindow}] | +| src/server/index.ts | MODIFY — native row passes longContextWindow: nativeOpenAiMaxInputTokens(metadataId, nativeLimits) (add to the catalog destructuring; re-exported at src/codex/catalog.ts:5) | +| tests/cursor-local-models-schema.test.ts | MODIFY — unit: threshold emitted only when long > default; server: gpt-5.6-sol has pricing.overrides[0].min_prompt_tokens === 272000 and context_length === 922000; routed kimi/k3 has no pricing | +| docs-site guide | MODIFY — "Reasoning effort vs Max Mode" paragraph | + +## Verify + +- bun run typecheck; bun test tests/cursor-local-models-schema.test.ts tests/grok-models-effort-list.test.ts tests/server-combo-failover-e2e.test.ts. +- Live: refresh model list (new base-URL spelling to bust the cache) → picker for gpt-5.6-sol + shows **Context: 272K / 922K**; screenshot 031_context_selector.png. + +## Then + +Update PR #3230/#3231 bodies, address reviewer feedback, admin squash-merge #3230 → dev, +retarget #3231 → dev, CI, merge, git merge-base --is-ancestor proof. + diff --git a/devlog/_plan/260902_cursor_local_models_schema/031_context_selector.png b/devlog/_plan/260902_cursor_local_models_schema/031_context_selector.png new file mode 100644 index 0000000000000000000000000000000000000000..df7c498deb5ba6d8867ff4c83e20bec1320d8611 GIT binary patch literal 44921 zcmeFZ1y~)+vOhYDg==tk4GzKGo#5`lgS!WUh2Rn(Kmr6vaCc2`4-P?tySwvNvQM)2 zJ@=gZ?t9<=yZ4;;y@sCoO-*%8SJT~7U0uV&w}(aGk(`vQ6aWGNfDGgZcvu2NBt5My z06#mKaEbjjehA@xEBkbcz3#MH%6R9RW!hsI%K$;iX#KhSW0FXr>bTW5SONbm~V+dxoH?y{f;8+M2 zva@qAgWzEZri1V}LhujyJkS0j$MP@O#N^p;nI7FE08ETUbl|=CQYZ`lC<$e6F>ttoko(?jou73p+cg{;q?C^Ph4| zoFN>4E9+*X@ms!?x$JM5-d5^bzj)lOHJ|>%E;eev>*j4G{+sUXruthib9-sAU;Gx% zQoq~8&ILmK+jFg~Wq+4-wNi(S%|CQ8HTkpt=Juk0%5>5GT{kmx3CUml=Jp!D^>uU= z`~56O+n;0RXZx7jO8=(2xU2mx>*}ojo9^hU{JRaGo5=kb4?oKSN`M*Q1-L+d%pqqo zKpNl$o&ah9q=&njdqVm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Q$1@WFJ!2f(yoDR>P28DC}}cS@~On zHB1_EFVK@rtZ);ei<683YjVE6u7DJ#6;-PXC(Evodlw_8W%pu-uD%x}IbYPI&IhiqiTAB4zbcYbx3i+BOhQHDj@T(Sc_u?y}Mir~`LG0p2 zbtShJbO`+lYd;GH2EeT+fW|L>c2`hyrzx~WPcZRwj6Rd}NfUL^fHwoIg8rB*WOcHn z=3Nbkh=coF^47=#P+=1|)Dpp9WKKyPC6a~zWTrNj)b1yM)Oy@~@%5-Pc(r-3ScO-E zpbK{j4-$n*F`K?{C~&qFe!{nA1p9ofT3)ax>Q7=A6Yc0zu$1P}TAO-E5#AWKI7T|o zlt84YY#Fq));8C8OcKH+F5Sl7JjTCMn`<0d&|2f@P8j7#5Z{19X(2!;g?ESMn7!cb zgpY3*k!SqPs-YUF-%apM)vL(@XYLRY|2gb}CMr+>{1|2rCxZ%@UMsjat57+~=eRg; zea_TM9@xyU)8_CzsJ1;_^t?ax)%#u`LPpdrcRac}MUpHUB}qD=x%V>Kyy}BNH)DM? zmX-dzlQueIkl3e-vb7-LcJe*Zup~!w=P$!lxlPf#hWABQcyZ`xk&cYWs@#q10-H8# z;CJW4TKjs)^IDK^ChVuhc~v8IBN!hty`2E3IU%5BuJc<219abm&AY60^`z@hdtyn} z~@`7G9LB{yOkukITuO=y8pg@@v}n z-Q`;x#V@y}O~tC{nVn5suH0bF5WvlL_Y!}oxMPVOIU+#3qg6h(WgQUv!$DH2epj_p zOEI72ig^WK$2k1!D8b@&$c%274|4_u_9Sji3TKt-IH4}ZB0!fmBS+k;EYs*%DBRuvS8fO0yGqZEYO#!xq3-I(yD9+X zVq;SSePxe)!}}`RAZG^8UJn}Yr=2&IV_xFAcyHFGPdL^&lkB)=&0ngyiGF|};EUo( mq*f5bnEqX2uObT|jB=WVoI%2n_aMs8OeY9HN{IAx?*9WS)(KGn literal 0 HcmV?d00001 diff --git a/devlog/_plan/260902_cursor_local_models_schema/032_context_options.png b/devlog/_plan/260902_cursor_local_models_schema/032_context_options.png new file mode 100644 index 0000000000000000000000000000000000000000..01e0d9249304ed1fe4fc95b87fa4450c1e96c958 GIT binary patch literal 46947 zcmeFZ1y~)+vOhYDg==tk4GzKGo#5`lgS!WUh2Rn(Kmr6vaCc2`4-P?tySwvNvQM)2 zJ@=gZ?t9<=yZ4;;y@sCoO-*%8SJT~7U0uV&w}(aGk(`vQ6aWGNfDGgZcvu2NBt5My z06#mKaEbjjehA@xEBkbcz3#MH%6R9RW!hsI%K$;iX#KhSW0FXr>bTW5SONbm~V+dxoH?y{f;8+M2 zva@qAgWzEZri1V}LhujyJkS0j$MP@O#N^p;nI7FE08ETUbl|=CQYZ`lC<$e6F>ttoko(?jou73p+cg{;q?C^Ph4| zoFN>4E9+*X@ms!?x$JM5-d5^bzj)lOHJ|>%E;eev>*j4G{+sUXruthib9-sAU;Gx% zQoq~8&ILmK+jFg~Wq+4-wNi(S%|CQ8HTkpt=Juk0%5>5GT{kmx3CUml=Jp!D^>uU= z`~56O+n;0RXZx7jO8=(2xU2mx>*}ojo9^hU{JRaGo5=kb4?oKSN`M*Q1-L+d%pqqo zKpNl$o&ah9q=&njdqVm%0Ejy{dO2HLTDg*mLPnrDnXJ7jGb0&0D?1ke{20$acmUw! z+s|N z01dzb@BtEl5}*T^0Cs>2;0HtiNk9%#cXdDqFa%5iE5HtL20Q>?;3W_WL;$fsB9IDX z0NFquPz+Q6wLlZl3UmSez%VcY%mRzR8n6Q#0_VUD2n2!yp@1+!_#jdcHHZPk4&ng` zfh0i+AXShK$QWb^vIn_=d_h5=2v9sI74!j=4=M-MgIYnopb^kCXbH3hItE=sfuWG0 zu%L*cXrNf2c%Vd~Qd1 zMR1?tM&Z`rF5!{jN#WVyrQmhp?coFAli&;BTi{3GH{fp&9wAU8@F6H6m?3x}#31A# zG$0HitRdVWq9f8G3L>f_K1Y0s_!hAQu@i9)@dOD8i2{iiNd?IoDFEp$QW;VY(h|}Y zG6pg|vN*CnvKw+VaxU^0~+JM+lE-9*I9P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z(YPxsV=fP&tvwGw(L6Jz#iMw3!rx7?gFaogtZNiXSmcnZ0LeU)-hegeqKW`3H(9-t zPSS5L;Z5&v3&^YLRAg3_BPzO-U<$Co2arLa51Z~M@qLxT>yc}ZGOn5ZsVt8!AOwI` zBNld;*qx&a2@7S*P6 zYOkHy5fitsIQzNnUBv)=^WtwBLY_9(G>vxeQMR(vg|b^Mh0J!!%`9Qrq~2ATvT!ii z0CcXy;b)0%{6TB0Lp+nu4Tb53+Dn&NCuoW*4eo#|5KmE_JJ(OHcpJmI{pa?D>@p?M zRYsZ#S|)W~fwr7BI}$QG8racvEi1#;e__&K5X4x!a#@|vR%T*ZiS;IcCDpF4tgI!v z)2)8lG(b0zyJI?!ycFR4*yuZ+D+fXGYWSA%mrAsk>@cP$xt&Z!Xa4}6N{lkae~9!R c_1I7V90B=I0Z$8&(uGDnuqwa+3` to keep the two apart, + and leave "Import data from existing Cursor installation" unchecked on first run unless + you want your settings copied. + +## Configure the gateway + +opencodex needs to be running (`ocx service status`). Then either of these works; both +end up in the same place. + +**In the app.** Settings → Models → Gateway → Configure gateway: + +| Field | Value | +|---|---| +| Base URL | `http://127.0.0.1:10100/v1` (include `/v1`; plain `http://` loopback is accepted) | +| API Key | the value of `OPENCODEX_API_AUTH_TOKEN` if your service uses API auth, otherwise any placeholder such as `opencodex-loopback` | + +Click **Refresh model list**. The picker fills with opencodex's `/v1/models`; switch on the +rows you want. + +**With environment variables.** The app reads these at start: + +```text +CURSOR_LOCAL_AGENT_BASE_URL=http://127.0.0.1:10100/v1 +CURSOR_LOCAL_AGENT_API_KEY=opencodex-loopback +CURSOR_LOCAL_AGENT_HEADERS= # optional, extra headers as key=value pairs +``` + +Cursor Private Inference is a GUI app, so an interactive shell profile is not enough on +its own; the variable has to be in the environment of whatever launches the app. + +| OS | Where to put it | +|---|---| +| macOS | `launchctl setenv CURSOR_LOCAL_AGENT_BASE_URL http://127.0.0.1:10100/v1` for the current login session, or a LaunchAgent with `EnvironmentVariables` to make it persistent. Starting the app from a terminal also works. | +| Windows | `setx CURSOR_LOCAL_AGENT_BASE_URL http://127.0.0.1:10100/v1` (user scope; affects new processes) or System Properties → Environment Variables. Restart the app afterwards. | +| Linux | `~/.profile` or `~/.pam_environment` for a display-manager session, or `systemctl --user set-environment CURSOR_LOCAL_AGENT_BASE_URL=http://127.0.0.1:10100/v1` when the desktop runs under a user systemd session. The AppImage launched from a terminal inherits that shell's environment. | + +The build exists for macOS (arm64, x64, universal), Windows (x64, arm64) and Linux (x64, +arm64). Configuration is identical across them. + +## Models and reasoning effort + +The picker is opencodex's raw `/v1/models` list. Two things decide whether a model row gets +a **Reasoning** control: + +1. opencodex must advertise capabilities on the row (`api_types` plus a `capabilities` + object). It does, from v2.41. Older proxies show the models but no effort control. +2. The model id, after stripping everything up to the last `/`, must match Cursor's own + effort table. Cursor decides the ladder, not opencodex: + +| Model id (after the last `/`) | Ladder Cursor shows | Wire field | +|---|---|---| +| `gpt-5.6-sol`, `gpt-5.6-terra`, `gpt-5.6-luna` | Low, Medium, High, Extra High | `reasoning.effort` | +| `gpt-5`, `gpt-5.x` | Low, Medium, High, Extra High | `reasoning.effort` | +| `claude-opus-5`, `claude-sonnet-5`, `claude-opus-4.7`, `claude-opus-4.8` | Low, Medium, High, Extra High, Max | `output_config.effort` | +| `claude-opus-4.6`, `claude-opus-4.5`, `claude-sonnet-4.6` | Low, Medium, High, Max | `output_config.effort` | +| `grok-4.3`, `grok-4.5`, `grok-4.6`, `grok-build-latest` | Minimal, Low, Medium, High, Extra High | `reasoning_effort` | +| `gemini-*` (needs `supports_reasoning`) | Minimal, Low, Medium, High | `reasoning_effort` | +| anything else, including `claude-fable-5-1`, `kimi-k3` | no control | — | + +So `anthropic/claude-opus-5` works, and opencodex's `max`/`ultra` tiers for GPT-5.6 are not +reachable from this picker. For a model with no control, set a default in opencodex instead +(`modelDefaultReasoningEfforts` on the provider); that default applies when Cursor sends no +effort. + +### "Max" is two different things + +Regular Cursor shows a **Max** toggle next to some models. That is Max Mode, a larger context +window, not a reasoning tier. In the local-agent build the same idea appears as a **Context** +entry in the model menu, and opencodex lights it up for the native GPT-5.6 family: **272K** +(default) or **922K** (the 1M opt-in, marked as costing more). The value you pick caps that +turn's context. Routed models show a single window and no Context entry; a provider context +cap below 922K removes the entry for the native rows too. + +Reasoning-effort **Max** (opencodex's `max`/`ultra`) is the other meaning, and that one is +not reachable: Cursor takes the effort ladder from its own table rather than from the gateway, +and the GPT-5.6 entry stops at Extra High. + +Because opencodex advertises `responses` in `api_types`, this build sends agent turns to +`/v1/responses` with `reasoning.effort`, not to `/v1/chat/completions`. + +## Verify + +`ocx observe logs` shows the turns as `inboundProtocol: responses` with `admissionKind: loopback`. + +| Symptom | Check | +|---|---| +| 401 from the gateway | the API Key does not match `OPENCODEX_API_AUTH_TOKEN`; for a loopback bind without API auth any value works | +| picker is empty | opencodex is not running, or the Base URL is missing `/v1`; press Refresh model list after fixing | +| models listed but no Reasoning control | opencodex older than v2.41, or the model id is not in the table above | +| a schema change is not picked up | Cursor caches `/models` per Base URL string; restart the app, or temporarily save a different spelling of the URL (`localhost` vs `127.0.0.1`) and refresh | +| 23k-token first turn | expected; that is Cursor's local system prompt | diff --git a/docs-site/src/content/docs/guides/integrations.md b/docs-site/src/content/docs/guides/integrations.md index a5125911eb..ffa4e69251 100644 --- a/docs-site/src/content/docs/guides/integrations.md +++ b/docs-site/src/content/docs/guides/integrations.md @@ -59,6 +59,11 @@ One caveat specific to Aside: the running app rewrites `models.json` itself, so fully quit and reopen Aside after applying, the same way Claude Desktop needs a restart. Aside's block is loopback-only and never carries a real credential. +Cursor is not on this list. Regular Cursor calls custom endpoints from its own backend, so a +loopback proxy is unreachable without a public tunnel, and Cursor's separate local-agent build +is configured inside Cursor rather than from this tab. See +[Cursor Private Inference](/guides/cursor-private-inference/). + Paths honor each client's own environment override where it has one. For OMP, `OMP_PROFILE` wins over `PI_PROFILE` by presence, even when explicitly empty. A named profile uses `PI_CONFIG_DIR` as a directory name relative to the user's home and ignores `PI_CODING_AGENT_DIR`; without a named profile, From 83838e7fab0e2b1a23ab86dee4ef606f25eeb8d6 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 11:45:33 +0900 Subject: [PATCH 163/172] feat(cursor): let the Codex Fast toggle reach Cursor's fast variant (#3225) Codex's Fast toggle is OpenAI's service_tier field, and Cursor has no such field - its fast product is a different model variant (claude-opus-5-thinking-high-fast) or a {id:fast} request parameter for Grok. So a service_tier on a Cursor route was silently dropped, and no Cursor row could advertise the toggle at all: FAST_WIRE_ADAPTERS is a closed set that Cursor was not in. Add a cursor-variant FastWire kind, declare it on the Cursor registry entry, and have the request builder consume the tier DECISION (not the raw caller field, so fastMode=false still suppresses a caller's request). A thinking umbrella pick upgrades to thinkingFast rather than the regular-fast sibling, which is a different product with a shorter ladder whose regular family is quarantined for claude-opus-5. Only the five bases that actually declare a fast variant advertise the tier, so there is no dead toggle. That required one more fix: forwardCallerTier ignored the wire's foreignCallerTiers declaration, so an unclassified cursor-variant route projected 'unknown' support - enough for Codex to offer a toggle on kimi-k3, which has no fast variant at all. Telemetry recomputes the variant from the same pure inputs the builder uses. Rebuilding the request there would be wrong twice over: tierLogForRunTurn runs before runTurn, and createCursorRequest mints conversation ids. Also widens the usage/log.ts wireKind allowlist, which silently returns null - dropping the whole tier row - for a kind it does not recognise. Refs devlog/_plan/260902_cursor_unified_identity/020_wp3_codex_fast_toggle.md Co-authored-by: jun --- src/adapters/cursor.ts | 17 +++++ src/adapters/cursor/catalog.ts | 43 ++++++++++- src/adapters/cursor/request-builder.ts | 35 ++++++++- src/providers/fastwire.ts | 12 ++- src/providers/registry.ts | 10 +++ src/types/provider.ts | 8 +- src/usage/log.ts | 8 +- tests/cursor-fast-tier.test.ts | 102 +++++++++++++++++++++++++ tests/fastwire-policy.test.ts | 13 +++- 9 files changed, 233 insertions(+), 15 deletions(-) create mode 100644 tests/cursor-fast-tier.test.ts diff --git a/src/adapters/cursor.ts b/src/adapters/cursor.ts index 1ab248a754..157da1eaa3 100644 --- a/src/adapters/cursor.ts +++ b/src/adapters/cursor.ts @@ -12,6 +12,7 @@ import { cursorClientThreadOwner, cursorCoveredPrefixDigest, cursorInstructionDigest, + cursorRequestEmitsFastVariant, } from "./cursor/request-builder"; import { createLiveCursorTransport, @@ -26,6 +27,7 @@ import { invalidateCursorCheckpoint, } from "./cursor/checkpoint-store"; import { debugProviderDiagnostic } from "../lib/debug"; +import { createAdapterTierMetadata } from "../providers/fastwire"; import { estimateTokens } from "../lib/token-estimate"; import { rememberCursorThreadConversation } from "./cursor/thread-continuity"; import { runCursorTurnWithRetry } from "./cursor/transport-retry"; @@ -100,6 +102,21 @@ export function createCursorAdapter(provider: OcxProviderConfig, deps: CursorAda return { name: "cursor", + // Cursor emits Fast as a model variant, so the generic "no field emitted" fallback in + // adapters/registry.ts would report every Fast turn as downgraded. This recomputes the + // variant from the same pure inputs the builder uses: tierLogForRunTurn runs BEFORE + // runTurn, and createCursorRequest mints conversation ids, so rebuilding it here would + // describe a request that was never sent. + tierLogForRunTurn(parsed) { + const fast = cursorRequestEmitsFastVariant(parsed); + return createAdapterTierMetadata( + parsed.options.tierObservation, + parsed.options.tierDecision, + fast ? "cursor-variant" : null, + fast ? "fast" : null, + ); + }, + buildRequest() { return { url: provider.baseUrl || CURSOR_API_URL, diff --git a/src/adapters/cursor/catalog.ts b/src/adapters/cursor/catalog.ts index 3351deab16..25894e504c 100644 --- a/src/adapters/cursor/catalog.ts +++ b/src/adapters/cursor/catalog.ts @@ -443,6 +443,32 @@ function defaultKindFor(baseId: string): CursorVariantKind { return CURSOR_CAPABILITIES[baseId]?.defaultVariant ?? "regular"; } +/** + * Promote a variant to its fast sibling when the base declares one, else leave it alone. + * + * Thinking must map to thinkingFast rather than to the plain fast variant: the umbrella row + * for a Claude base routes THINKING, and its regular-fast sibling is a different product + * with a shorter ladder (claude-opus-5-fast stops at high) whose regular family is + * quarantined. A base with no fast dimension keeps its kind, so Fast degrades to today's + * behavior instead of erroring. + */ +export function upgradeToFast(baseId: string, kind: CursorVariantKind): CursorVariantKind { + const variants = CURSOR_CAPABILITIES[baseId]?.variants; + if (!variants) return kind; + if (kind === "thinking" || kind === "thinkingFast") { + return variants.thinkingFast ? "thinkingFast" : kind; + } + return variants.fast ? "fast" : kind; +} + +/** Bases whose capability declares a fast or thinking-fast variant. */ +export function cursorFastCapableBases(): string[] { + return Object.entries(CURSOR_CAPABILITIES) + .filter(([, capability]) => capability.variants.fast !== undefined + || capability.variants.thinkingFast !== undefined) + .map(([baseId]) => baseId); +} + function normalizeRequestedEffort(reasoning: string | undefined): string | undefined { const normalized = reasoning?.toLowerCase(); return normalized === "ultra" ? "max" : normalized; @@ -523,20 +549,25 @@ export function resolveCursorSelection( pickedId: string, reasoning: string | undefined, liveMaxModeIds?: ReadonlySet, + options: { fast?: boolean } = {}, ): CursorResolvedSelection { const parsed = parseCursorVariantId(pickedId); if (!parsed.known) { return { wireId: pickedId, canonicalId: pickedId, maxMode: false, known: false }; } const capability = CURSOR_CAPABILITIES[parsed.baseId]!; - const spec = capability.variants[parsed.kind] ?? capability.variants.regular; + // Codex's Fast toggle is a variant switch here; every later read must use the upgraded + // kind, not parsed.kind, or the wire id loses its -fast marker (or keeps the cursor- + // prefix that only the regular variant takes). + const kind = options.fast === true ? upgradeToFast(parsed.baseId, parsed.kind) : parsed.kind; + const spec = capability.variants[kind] ?? capability.variants.regular; if (!spec) { return { wireId: parsed.baseId, canonicalId: parsed.baseId, maxMode: false, known: true }; } const requested = parsed.level ?? reasoning; const effort = cursorVariantEffort(spec, requested); - const canonicalId = composeWireId(parsed.baseId, parsed.kind, effort); - const wireId = capability.wirePrefix && parsed.kind === "regular" + const canonicalId = composeWireId(parsed.baseId, kind, effort); + const wireId = capability.wirePrefix && kind === "regular" ? `${capability.wirePrefix}${canonicalId}` : canonicalId; const ultraRequested = parsed.ultra || reasoning?.toLowerCase() === "ultra"; @@ -584,9 +615,13 @@ export interface CursorUmbrellaRow { export function cursorGrokFastSelection( pickedId: string, reasoning: string | undefined, + fast?: boolean, ): { wireBaseId: string; effort: string } | undefined { const parsed = parseCursorVariantId(pickedId); - if (!parsed.known || parsed.kind !== "fast") return undefined; + // Both call paths must learn the flag together: if only resolveCursorSelection did, a + // toggled Grok pick would emit a flattened grok-4.6-high-fast id, which the wire rejects. + const kind = fast === true ? upgradeToFast(parsed.baseId, parsed.kind) : parsed.kind; + if (!parsed.known || kind !== "fast") return undefined; const capability = CURSOR_CAPABILITIES[parsed.baseId]; if (capability?.wirePrefix !== "cursor-") return undefined; const spec = capability.variants.fast; diff --git a/src/adapters/cursor/request-builder.ts b/src/adapters/cursor/request-builder.ts index e73d5e98ea..51651bb07a 100644 --- a/src/adapters/cursor/request-builder.ts +++ b/src/adapters/cursor/request-builder.ts @@ -180,13 +180,40 @@ function catalogLimitNote(kept: readonly OcxTool[], omitted: readonly OcxTool[]) : `[opencodex] Cursor's transport limit allows ${kept.length} of ${kept.length + omitted.length} client tools this turn. Omitted and unavailable this turn: ${omittedSummary}.`; } +/** + * True when this turn should take Cursor's fast variant. + * + * Reads the tier DECISION rather than the raw caller field so one authority owns precedence: + * `decideTier` has already applied config `fastMode`, the caller's `service_tier`, and the + * route's eligibility, so `fastMode: false` correctly suppresses a caller's Fast request. + * A `{kind:"set"}` decision on a Cursor route means canonical Fast survived that gate. + */ +export function cursorFastRequested(parsed: OcxParsedRequest): boolean { + return parsed.options.tierDecision?.kind === "set"; +} + +/** + * Whether the wire this request will carry expresses the fast variant, for tier telemetry. + * + * Recomputed from the same pure inputs the builder uses rather than read off a built + * request: `tierLogForRunTurn` runs BEFORE `runTurn` (server/responses/core.ts), and + * `createCursorRequest` is not pure — it mints conversation ids — so rebuilding there would + * report a request that was never sent. + */ +export function cursorRequestEmitsFastVariant(parsed: OcxParsedRequest): boolean { + if (!cursorFastRequested(parsed)) return false; + const model = normalizeCursorModelId(parsed.modelId, parsed.options.reasoning, true); + return model.modelId.endsWith("-fast") + || (model.requestedModelParameters ?? []).some(p => p.id === "fast" && p.value === "true"); +} + /** * Resolve a `cursor/` selection + Codex reasoning effort to Cursor's requested model shape. * Most models encode effort in a flat id (`claude-4.6-opus-high`). Grok Fast is parameterized * instead: current Cursor clients send the matching Grok base id plus `effort` and `fast` parameters. * A fully-qualified id (one that is not a known effort base) passes through unchanged. */ -function normalizeCursorModelId(modelId: string, reasoning?: string): { +function normalizeCursorModelId(modelId: string, reasoning?: string, fast?: boolean): { modelId: string; requestedModelParameters?: readonly CursorRequestedModelParameter[]; routingLevel?: CursorRoutingLevel; @@ -201,7 +228,7 @@ function normalizeCursorModelId(modelId: string, reasoning?: string): { const id = selection.modelId; // Grok Fast stays parameterized: current Cursor clients send the base id // plus effort/fast parameters instead of the flattened -fast id. - const grokFast = cursorGrokFastSelection(id, reasoning); + const grokFast = cursorGrokFastSelection(id, reasoning, fast); if (grokFast) { return { ...selection, @@ -212,7 +239,7 @@ function normalizeCursorModelId(modelId: string, reasoning?: string): { ], }; } - const resolved = resolveCursorSelection(id, reasoning); + const resolved = resolveCursorSelection(id, reasoning, undefined, { fast }); return { ...selection, ...(resolved.maxMode ? { maxMode: true } : {}), @@ -455,7 +482,7 @@ export function createCursorRequest( const visibleTools = cursorToolsForActivePrompt(parsed.context.tools, activeText, parsed.options.toolChoice); const budget = applyCursorToolBudget(visibleTools, parsed.options.toolChoice); const limitNote = catalogLimitNote(budget.tools, budget.omitted); - const model = normalizeCursorModelId(parsed.modelId, parsed.options.reasoning); + const model = normalizeCursorModelId(parsed.modelId, parsed.options.reasoning, cursorFastRequested(parsed)); const request: CursorRunRequest = { modelId: model.modelId, ...(model.requestedModelParameters ? { requestedModelParameters: model.requestedModelParameters } : {}), diff --git a/src/providers/fastwire.ts b/src/providers/fastwire.ts index 34116e7133..7eae6b0fe1 100644 --- a/src/providers/fastwire.ts +++ b/src/providers/fastwire.ts @@ -14,6 +14,9 @@ const FAST_WIRE_ADAPTERS: Readonly> "service-tier": SERVICE_TIER_ADAPTERS, // A1 deliberately has no adapter implementation for Anthropic speed. "anthropic-speed": new Set(), + // Cursor expresses Fast as a variant dimension of the picked model, resolved in the + // request builder, so the adapter set is exactly the cursor adapter. + "cursor-variant": new Set(["cursor"]), }; const DEFAULT_SERVICE_TIER_FAST_WIRE: FastWire = Object.freeze({ @@ -209,8 +212,13 @@ export function resolveFastPolicy( // On classified routes this permission applies only to a caller's foreign tier: proxy-owned // canonical Fast has already passed capability validation. On unclassified routes every caller // tier still needs the final wire's forwarding permission. + // A wire that declares `foreignCallerTiers: "drop"` cannot carry an arbitrary tier string at + // all — cursor-variant resolves a MODEL VARIANT, so there is nothing to forward a foreign + // value into. Without this, an unclassified route on such a wire projects "unknown" support + // and Codex would show a Fast toggle on a base that has no fast variant. const forwardCallerTier = capability !== false && callerWireAvailable + && fastWire?.foreignCallerTiers !== "drop" && forwardCallerServiceTier !== false && (adapter !== "openai-chat" || authority.capability.chatServiceTier === true); @@ -467,8 +475,8 @@ export function fastWireDeclarationError(source: { } if (value === null) return null; if (!isPlainRecord(value)) return "fastWire must be an object, null, or absent"; - if (value.kind !== "service-tier" && value.kind !== "anthropic-speed") { - return "fastWire.kind must be service-tier or anthropic-speed"; + if (value.kind !== "service-tier" && value.kind !== "anthropic-speed" && value.kind !== "cursor-variant") { + return "fastWire.kind must be service-tier, anthropic-speed, or cursor-variant"; } if (value.foreignCallerTiers !== "verbatim" && value.foreignCallerTiers !== "drop") { return "fastWire.foreignCallerTiers must be verbatim or drop"; diff --git a/src/providers/registry.ts b/src/providers/registry.ts index 8ed01545ab..3f71a1a8a7 100644 --- a/src/providers/registry.ts +++ b/src/providers/registry.ts @@ -18,6 +18,7 @@ import { cursorModelInputModalities, cursorModelReasoningEfforts, } from "../adapters/cursor/discovery"; +import { cursorFastCapableBases } from "../adapters/cursor/catalog"; import { COMMAND_CODE_MODEL_REASONING_EFFORTS } from "./command-code-efforts"; import { isCanonicalOpenRouterTarget } from "./openrouter-routing"; @@ -1120,6 +1121,15 @@ export const PROVIDER_REGISTRY: readonly ProviderRegistryEntry[] = [ defaultModel: "auto", modelContextWindows: cursorModelContextWindows(CURSOR_STATIC_MODELS), modelDisplayNames: cursorModelDisplayNames(), + // Cursor's Fast product is a model VARIANT, not a service_tier field, so the wire kind + // is cursor-variant and the request builder consumes the decision. + fastWire: { kind: "cursor-variant", canonicalToWire: { priority: "fast" }, foreignCallerTiers: "drop" }, + // Deliberately NO provider-level supportsServiceTier: resolveFastPolicy short-circuits on + // `capability.provider === false` BEFORE consulting the per-model map, which would make + // these entries dead config. Absent leaves unlisted bases "unclassified", and a + // non-service-tier adapter cannot forward a caller tier, so they still publish no toggle. + modelSupportsServiceTier: Object.fromEntries(cursorFastCapableBases().map(id => [id, true])), + fastTierDescription: "Cursor Fast variant", modelInputModalities: cursorModelInputModalities(CURSOR_STATIC_MODELS), modelReasoningEfforts: cursorModelReasoningEfforts(CURSOR_STATIC_MODELS), // Kimi K3 documents `max` as its API default, and its Cursor ladder has no `medium` diff --git a/src/types/provider.ts b/src/types/provider.ts index 7f24c00611..a3a4dd4c10 100644 --- a/src/types/provider.ts +++ b/src/types/provider.ts @@ -106,7 +106,13 @@ export interface ProviderRequestPacingConfig extends RequestPacingRule { } export interface FastWire { - kind: "service-tier" | "anthropic-speed"; + /** + * How the provider expresses Fast on the wire. `service-tier` is OpenAI's + * `service_tier` request field; `cursor-variant` is a MODEL-VARIANT switch, because + * Cursor has no tier field — its fast product is a different model id + * (`claude-opus-5-thinking-high-fast`) or a `{id:"fast"}` request parameter for Grok. + */ + kind: "service-tier" | "anthropic-speed" | "cursor-variant"; /** Canonical tier name to upstream wire spelling. */ canonicalToWire: Readonly>; /** Policy for non-canonical caller-provided tier values. */ diff --git a/src/usage/log.ts b/src/usage/log.ts index a32b8aee44..7e056f97b0 100644 --- a/src/usage/log.ts +++ b/src/usage/log.ts @@ -322,7 +322,8 @@ function normalizeAttemptTierOutcome(raw: unknown): AttemptTierOutcome | null { if ("wireKind" in outcome && outcome.wireKind !== null && outcome.wireKind !== "service-tier" - && outcome.wireKind !== "anthropic-speed") return null; + && outcome.wireKind !== "anthropic-speed" + && outcome.wireKind !== "cursor-variant") return null; if ("wireValue" in outcome && outcome.wireValue !== null && typeof outcome.wireValue !== "string") return null; if ("fastDowngradeReason" in outcome && (typeof outcome.fastDowngradeReason !== "string" @@ -337,7 +338,10 @@ function normalizeAttemptTierOutcome(raw: unknown): AttemptTierOutcome | null { const responseServiceTier = sanitizeLogMetadataString(outcome.responseServiceTier); return { ...(outcome.canonical === "priority" ? { canonical: "priority" as const } : {}), - ...(outcome.wireKind === null || outcome.wireKind === "service-tier" || outcome.wireKind === "anthropic-speed" + ...(outcome.wireKind === null + || outcome.wireKind === "service-tier" + || outcome.wireKind === "anthropic-speed" + || outcome.wireKind === "cursor-variant" ? { wireKind: outcome.wireKind } : {}), ...(outcome.wireValue === null diff --git a/tests/cursor-fast-tier.test.ts b/tests/cursor-fast-tier.test.ts new file mode 100644 index 0000000000..43ec6bd66b --- /dev/null +++ b/tests/cursor-fast-tier.test.ts @@ -0,0 +1,102 @@ +import { describe, expect, test } from "bun:test"; +import { cursorFastCapableBases, upgradeToFast } from "../src/adapters/cursor/catalog"; +import { createCursorRequest, cursorRequestEmitsFastVariant } from "../src/adapters/cursor/request-builder"; +import { providerConfigSeed } from "../src/providers/derive"; +import { getProviderRegistryEntry } from "../src/providers/registry"; +import { decideTier } from "../src/providers/fastwire"; +import { fastPolicyForModel, serviceTierSupportFromPolicy } from "../src/providers/service-tier"; +import type { OcxParsedRequest, TierDecision } from "../src/types"; + +const FAST_DECISION: TierDecision = { kind: "set", value: "fast" }; + +function parsedFor(modelId: string, reasoning?: string, decision?: TierDecision): OcxParsedRequest { + return { + modelId, + context: { systemPrompt: [], messages: [{ role: "user", content: "hi" }] }, + options: { + ...(reasoning ? { reasoning } : {}), + ...(decision ? { tierDecision: decision } : {}), + }, + } as OcxParsedRequest; +} + +const cursorConfig = () => providerConfigSeed(getProviderRegistryEntry("cursor")!); + +/** + * Codex's Fast toggle is OpenAI's `service_tier`, and Cursor has no tier field — its fast + * product is a different model variant. Before this, `service_tier` on a Cursor route was + * silently dropped and no Cursor row could advertise the toggle at all + * (devlog 260902_cursor_unified_identity/020). + * + * These drive each new conditional path and assert the observable effect, rather than + * asserting that a table contains a value. + */ +describe("Codex Fast reaches Cursor's fast variant", () => { + test("only bases with a fast variant advertise the tier", () => { + const config = cursorConfig(); + const support = (id: string) => + serviceTierSupportFromPolicy(fastPolicyForModel(config, id, "cursor")); + + for (const base of cursorFastCapableBases()) expect(support(base)).toBe(true); + // No dead toggle: a base with no fast wire must publish definitive negative evidence, + // not "unknown" (which Codex would render as an offerable tier). + for (const base of ["kimi-k3", "gpt-5.6-sol", "glm-5.3", "gemini-3.7-flash"]) { + expect(support(base)).toBe(false); + } + }); + + test("the toggle produces a set decision only on a fast-capable base", () => { + const config = cursorConfig(); + const decide = (id: string) => + decideTier(fastPolicyForModel(config, id, "cursor"), undefined, "priority"); + + expect(decide("claude-opus-5")).toEqual(FAST_DECISION); + expect(decide("grok-4.6")).toEqual(FAST_DECISION); + expect(decide("kimi-k3")).toEqual({ kind: "drop" }); + }); + + test("a thinking umbrella pick upgrades to thinking-fast, not the regular-fast sibling", () => { + // The regular-fast sibling is a different product with a shorter ladder, and for + // claude-opus-5 its regular family is quarantined. + expect(createCursorRequest(parsedFor("cursor/claude-opus-5", "max")).modelId) + .toBe("claude-opus-5-thinking-max"); + expect(createCursorRequest(parsedFor("cursor/claude-opus-5", "max", FAST_DECISION)).modelId) + .toBe("claude-opus-5-thinking-max-fast"); + expect(upgradeToFast("claude-opus-5", "thinking")).toBe("thinkingFast"); + }); + + test("grok keeps the parameterized fast shape instead of a flattened id", () => { + const request = createCursorRequest(parsedFor("cursor/grok-4.6", "high", FAST_DECISION)); + expect(request.modelId).toBe("grok-4.6"); + expect(request.requestedModelParameters).toEqual([ + { id: "effort", value: "high" }, + { id: "fast", value: "true" }, + ]); + // Off, it keeps the cursor- prefix the regular variant requires. + expect(createCursorRequest(parsedFor("cursor/grok-4.6", "high")).modelId) + .toBe("cursor-grok-4.6-high"); + }); + + test("a base without a fast variant is byte-identical with the toggle on", () => { + const off = createCursorRequest(parsedFor("cursor/kimi-k3", "max")); + const on = createCursorRequest(parsedFor("cursor/kimi-k3", "max", FAST_DECISION)); + expect(on.modelId).toBe(off.modelId); + expect(on.requestedModelParameters).toEqual(off.requestedModelParameters); + }); + + test("telemetry reports the variant that the wire will actually carry", () => { + // tierLogForRunTurn runs BEFORE runTurn, so this must be computable from parsed alone. + expect(cursorRequestEmitsFastVariant(parsedFor("cursor/claude-opus-5", "max", FAST_DECISION))).toBe(true); + expect(cursorRequestEmitsFastVariant(parsedFor("cursor/grok-4.6", "high", FAST_DECISION))).toBe(true); + expect(cursorRequestEmitsFastVariant(parsedFor("cursor/kimi-k3", "max", FAST_DECISION))).toBe(false); + expect(cursorRequestEmitsFastVariant(parsedFor("cursor/claude-opus-5", "max"))).toBe(false); + }); + + test("an explicit legacy variant id still wins over the toggle", () => { + // Alias retention: a pinned session naming a variant must not be re-pointed. + expect(createCursorRequest(parsedFor("cursor/claude-opus-5-thinking", "high", FAST_DECISION)).modelId) + .toBe("claude-opus-5-thinking-high-fast"); + expect(createCursorRequest(parsedFor("cursor/claude-opus-4-8-thinking-fast", "max")).modelId) + .toBe("claude-opus-4-8-thinking-max-fast"); + }); +}); diff --git a/tests/fastwire-policy.test.ts b/tests/fastwire-policy.test.ts index a29debcb34..14d1c4da72 100644 --- a/tests/fastwire-policy.test.ts +++ b/tests/fastwire-policy.test.ts @@ -644,8 +644,17 @@ describe("FastWire config and registry validation", () => { })).toBeNull(); }); - test("A1 adds no explicit registry FastWire declaration", () => { - expect(PROVIDER_REGISTRY.every(entry => entry.fastWire === undefined)).toBeTrue(); + test("cursor is the only registry FastWire declaration, and it is the variant wire", () => { + // A1 shipped none; Cursor's Fast is a model VARIANT rather than a service_tier field, + // so it must declare its own wire instead of inheriting the OpenAI adapter default. + // Every other provider still gets its wire from defaultFastWireForAdapter. + const declared = PROVIDER_REGISTRY.filter(entry => entry.fastWire !== undefined); + expect(declared.map(entry => entry.id)).toEqual(["cursor"]); + expect(declared[0]?.fastWire).toEqual({ + kind: "cursor-variant", + canonicalToWire: { priority: "fast" }, + foreignCallerTiers: "drop", + }); }); }); From 8a7d003546adcbb5c07cabbc9a00f17a16a224c7 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 11:56:54 +0900 Subject: [PATCH 164/172] docs(devlog): bug-label drawdown campaign record and main->dev regression audit (#3218) * docs(devlog): open the bug/PR closeout stack roadmap * docs(devlog): fold the A-gate import-boundary finding into phase 5 * docs(devlog): record the #3163 and #3166 landings * docs(devlog): record why #2986 does not land in this train * docs(devlog): close out the bug/PR closeout stack * docs(devlog): record the final green CI verdict on dev * docs(devlog): open the bug-label drawdown roadmap with audit corrections * docs(devlog): record the Batch A landings and first rebase carry * docs(devlog): record the Batch B rebase carries * docs(devlog): record why the rebase service earned its keep * docs(devlog): record the Batch C rebases and the one real review finding * docs(devlog): record the #2999 scope boundary that survived execution * docs(devlog): record Batch D - every bug PR closed * docs(devlog): record what the PR half of the campaign cost * docs(devlog): replan the remaining issues to one per cycle * docs(devlog): carry the i3141 evidence into the replan * docs(devlog): diagnose i3141 - fix predates the reported version * docs(devlog): retire the second bundle * docs(devlog): record the i3141 re-triage action and outcome * docs(devlog): diagnose i3152 log table jitter * docs(devlog): i3152 - measurement disproved the layout diagnosis * docs(devlog): diagnose i3136 slashed-id price lookup * docs(devlog): diagnose i3150 citation marker passthrough * docs(devlog): diagnose i3155 capacity plan allowlist * docs(devlog): i1419 stays open pending crash frames * docs(devlog): record the i1419 re-triage ask * docs(devlog): diagnose i2999 publication overwrite race * docs(devlog): record the i2999 outcome and remaining scope * docs(devlog): diagnose i2813 as a client-side reserve gate * docs(devlog): diagnose i1527 residuals as trace-blocked * docs(devlog): correct i1527 envelope-cap wording (192 blobs, HTTP 400) * docs(devlog): plan p3193 loopback alpha-search reimplementation * docs(devlog): record p3193 landing (#3205 -> 53c09a247) * docs(devlog): plan the main->dev regression audit * docs(devlog): pin regaudit counts, add tests-only/security passes and the exact-head dispatch * docs(devlog): record regaudit reviewer verdicts * docs(devlog): record the exact-head dev CI verdict and Windows classification * docs(devlog): record the main control run proving the Windows failures predate the range * docs(devlog): record the pass-1 recount and the #3217 root cause * docs(devlog): plan i3217 (Spark functions-namespace flattening) * docs(devlog): record i3217 landing (#3224 -> d23eab43a) * docs(devlog): regaudit2 recount and disposition table * docs(devlog): regaudit2 CI verdict on d23eab43a and the four PR arrivals * docs(devlog): plan p3226 (scoped namespace scrub) * docs(devlog): p3226 audit finding and carry plan * docs(devlog): record p3226 landing (#3234 -> b732b0d0f) * docs(devlog): plan p3227 (combo zero-output incomplete failover) * docs(devlog): record p3227 landing * docs(devlog): plan p3228 (encrypted V2 spawn native fallback) * docs(devlog): record p3228 landing * docs(devlog): plan p3229 (Codexless originator in task recovery) * docs(devlog): record p3229 landing and the #3239 regression repair * docs(devlog): r3239 regression repair record * docs(devlog): r3239 audit note * docs(devlog): record p3232 (merged by maintainer) * docs(devlog): p3232 verification result * docs(devlog): regaudit3 recount and landing table * docs(devlog): record the #3239/#3240 revert and correct the #3228 disposition * docs(devlog): rv3239 revert record * docs(devlog): rv3239 audit note * docs(devlog): regaudit3 second-dispatch verdict * docs(devlog): regaudit3 recount refreshed (#1419 closed by maintainer; count 4) * docs(devlog): regaudit3 final CI verdict and c-7 --------- Co-authored-by: jun --- .../260902_bug_label_drawdown/000_plan.md | 59 +++++++++ .../260902_bug_label_drawdown/010_phase1.md | 80 ++++++++++++ .../011_bd1_landing.md | 32 +++++ .../260902_bug_label_drawdown/020_phase2.md | 38 ++++++ .../021_bd2_landing.md | 66 ++++++++++ .../260902_bug_label_drawdown/030_phase3.md | 24 ++++ .../031_bd3_landing.md | 63 +++++++++ .../260902_bug_label_drawdown/040_phase4.md | 27 ++++ .../041_bd4_landing.md | 75 +++++++++++ .../050_bd5_replan.md | 62 +++++++++ .../260902_bug_label_drawdown/050_phase5.md | 25 ++++ .../260902_bug_label_drawdown/051_i3141.md | 80 ++++++++++++ .../260902_bug_label_drawdown/052_i3152.md | 81 ++++++++++++ .../260902_bug_label_drawdown/053_i3136.md | 89 +++++++++++++ .../260902_bug_label_drawdown/054_i3150.md | 96 ++++++++++++++ .../260902_bug_label_drawdown/055_i3155.md | 102 +++++++++++++++ .../260902_bug_label_drawdown/056_i1419.md | 73 +++++++++++ .../260902_bug_label_drawdown/057_i2999.md | 117 +++++++++++++++++ .../260902_bug_label_drawdown/058_i2813.md | 80 ++++++++++++ .../260902_bug_label_drawdown/059_i1527.md | 53 ++++++++ .../260902_bug_label_drawdown/060_phase6.md | 36 ++++++ .../260902_bug_label_drawdown/061_p3193.md | 33 +++++ .../062_p3193_landing.md | 10 ++ .../260902_bug_label_drawdown/063_i3217.md | 64 ++++++++++ .../064_i3217_landing.md | 10 ++ .../260902_bug_label_drawdown/070_regaudit.md | 70 ++++++++++ .../071_regaudit_landing.md | 120 ++++++++++++++++++ .../072_regaudit2.md | 49 +++++++ .../260902_bug_label_drawdown/080_p3226.md | 42 ++++++ .../081_p3226_landing.md | 11 ++ .../260902_bug_label_drawdown/082_p3227.md | 23 ++++ .../083_p3227_landing.md | 7 + .../260902_bug_label_drawdown/084_p3228.md | 34 +++++ .../085_p3228_landing.md | 8 ++ .../260902_bug_label_drawdown/086_p3229.md | 23 ++++ .../087_p3229_landing.md | 18 +++ .../260902_bug_label_drawdown/088_r3239.md | 27 ++++ .../260902_bug_label_drawdown/089_p3232.md | 10 ++ .../090_regaudit3.md | 78 ++++++++++++ .../260902_bug_label_drawdown/091_rv3239.md | 30 +++++ .../260902_bug_pr_closeout_stack/000_plan.md | 60 +++++++++ .../010_phase1.md | 34 +++++ .../011_wp1_landing.md | 29 +++++ .../020_phase2.md | 48 +++++++ .../021_wp2_landing.md | 45 +++++++ .../030_phase3.md | 40 ++++++ .../031_wp3_disposition.md | 50 ++++++++ .../040_phase4.md | 55 ++++++++ .../050_phase5.md | 86 +++++++++++++ .../060_phase6.md | 51 ++++++++ .../070_closeout.md | 66 ++++++++++ 51 files changed, 2589 insertions(+) create mode 100644 devlog/_plan/260902_bug_label_drawdown/000_plan.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/010_phase1.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/011_bd1_landing.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/020_phase2.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/021_bd2_landing.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/030_phase3.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/031_bd3_landing.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/040_phase4.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/041_bd4_landing.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/050_bd5_replan.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/050_phase5.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/051_i3141.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/052_i3152.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/053_i3136.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/054_i3150.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/055_i3155.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/056_i1419.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/057_i2999.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/058_i2813.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/059_i1527.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/060_phase6.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/061_p3193.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/062_p3193_landing.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/063_i3217.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/064_i3217_landing.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/070_regaudit.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/071_regaudit_landing.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/072_regaudit2.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/080_p3226.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/081_p3226_landing.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/082_p3227.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/083_p3227_landing.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/084_p3228.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/085_p3228_landing.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/086_p3229.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/087_p3229_landing.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/088_r3239.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/089_p3232.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/090_regaudit3.md create mode 100644 devlog/_plan/260902_bug_label_drawdown/091_rv3239.md create mode 100644 devlog/_plan/260902_bug_pr_closeout_stack/000_plan.md create mode 100644 devlog/_plan/260902_bug_pr_closeout_stack/010_phase1.md create mode 100644 devlog/_plan/260902_bug_pr_closeout_stack/011_wp1_landing.md create mode 100644 devlog/_plan/260902_bug_pr_closeout_stack/020_phase2.md create mode 100644 devlog/_plan/260902_bug_pr_closeout_stack/021_wp2_landing.md create mode 100644 devlog/_plan/260902_bug_pr_closeout_stack/030_phase3.md create mode 100644 devlog/_plan/260902_bug_pr_closeout_stack/031_wp3_disposition.md create mode 100644 devlog/_plan/260902_bug_pr_closeout_stack/040_phase4.md create mode 100644 devlog/_plan/260902_bug_pr_closeout_stack/050_phase5.md create mode 100644 devlog/_plan/260902_bug_pr_closeout_stack/060_phase6.md create mode 100644 devlog/_plan/260902_bug_pr_closeout_stack/070_closeout.md diff --git a/devlog/_plan/260902_bug_label_drawdown/000_plan.md b/devlog/_plan/260902_bug_label_drawdown/000_plan.md new file mode 100644 index 0000000000..7f8c20d0be --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/000_plan.md @@ -0,0 +1,59 @@ +# 000 — bug_label_drawdown: Plan + +## Objective + +Reduce open items carrying the `bug` label — PRs and issues both — from **24** to **3 or +fewer** (5 acceptable if the last few are genuinely blocked). Feature PRs and enhancement +issues are out of scope even when they look adjacent. + +Inventory taken 2026-09-02. + +**14 bug PRs:** #3177 #3176 #3174 #3168 #3164 #3151 #3148 #3144 #3138 #3135 #3121 #3112 +#3109 #3003 +**10 bug issues:** #3170 #3155 #3152 #3150 #3141 #3136 #2999 #2813 #1527 #1419 + +## Loop-spec + +- Archetype: verifier-defined. Each item has a binary terminal state. +- Write scope: whatever a named bug requires, plus `tests/`, plus this devlog unit. +- Out of scope: releases, promotion to `main`/`preview`, npm publish, deployment, + security-boundary rewrites beyond a named issue, other worktrees. +- **Verification policy (user-directed, binding):** never run the repository-wide local + suite; push with `--no-verify` so no hook runs it either. Focused `bun test` files plus + red-green proof. CI trails the work and is judged per batch. +- Merge mechanism: `gh pr merge --squash --admin --delete-branch`. +- **Rebase service is authorized.** A PR whose only defect is staleness gets rebased by us; + when the contributor branch is unpushable, its unique commits are cherry-picked onto a + `codex/` carry branch with author credit preserved and the original closed + `landed-via-maintainer` naming the merge SHA. + +## Work-phase map + +| WP | Doc | Batch | Items | Depends | +|----|-----|-------|-------|---------| +| bd0 | 000 | roadmap | inventory + dispositions | — | +| bd1 | 010 | A: merge train | #3174 #3176 #3177 #3151 | bd0 | +| bd2 | 020 | B: rebase service | #3168 #3148 #3135 | bd1 | +| bd3 | 030 | C: changes-requested, maintainer-owned | #3112 #3109 #3003 | bd2 | +| bd4 | 040 | D: changes-requested, contributor-owned | #3144 #3138 #3121 #3164 | bd3 | +| bd5 | 050 | E: needs-info issue triage | #3155 #3150 #3141 #3136 #1419 | bd4 | +| bd6 | 060 | F: implementable bug issues | #3152 #3170 #2999 #2813 #1527 | bd5 | + +## Batch A state at inventory + +| PR | Draft | Merge state | CI | +|----|-------|-------------|-----| +| #3174 gui mobile overflow | no | BLOCKED | running, no failures | +| #3176 wrapped quota rotation | no | BLOCKED | no failures listed | +| #3177 413 context overflow | **yes** | BLOCKED | running, no failures | +| #3151 Hermes vision export | **yes** | BLOCKED | **ci fail + macos fail** | + +`BLOCKED` here means "awaiting required review", not unmergeable — all four are +`MERGEABLE`. Draft status must be cleared before merge, and #3151's red CI must be +diagnosed rather than waived. + +## Accept criteria + +Mirrored into the goalplan as c-1..c-7. c-7 is the real bar: **open bug-labelled PRs plus +issues total 3 or fewer**, 5 acceptable with recorded blockers. + diff --git a/devlog/_plan/260902_bug_label_drawdown/010_phase1.md b/devlog/_plan/260902_bug_label_drawdown/010_phase1.md new file mode 100644 index 0000000000..cebe7142dc --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/010_phase1.md @@ -0,0 +1,80 @@ +# 010 — Batch A: MERGEABLE review-required bug PRs + +Four PRs are `MERGEABLE` and waiting on review rather than on their authors. + +## #3174 — fix(gui): mobile topbar and integration card overflow (@lidge-jun) + +14 files, +714/-2. Two responsive defects measured through CDP geometry rather than read +off CSS: a flex child without `min-width: 0` held its intrinsic width and pushed the +version badge under the action orbs at 320px; and `minmax(260px, 1fr)` could not shrink +below a 320px content box, pushing the integration card's action row off the page. + +Maintainer-authored, carries before/after screenshots (which `enforce-target` requires for +any PR mentioning gui), and records a review pass that removed an invented 400px +breakpoint. **Action:** confirm CI, merge. + +## #3176 — fix(codex): rotate accounts on wrapped quota failures (@Vadevious) + +6 files, +219/-15. ChatGPT reports quota exhaustion as HTTP 502 with a quota-shaped +message; the pool treated it as transient, retried the exhausted account, and surfaced +`adapter_eof`. The fix normalizes bounded, display-safe pre-stream 5xx to the existing +quota path with cooldown, affinity clear, and the bounded alternate retry. + +**Security review — performed, recorded here (A-gate finding A5).** This touches account +selection, which `MAINTAINERS.md` puts behind explicit security review, and the PR carried +no recorded review when it was merged. The review was done by reading the diff directly; +recording it after the merge rather than before is the process gap, not the code: + +- `src/lib/errors.ts` — `upstreamErrorMessageFromPayload` reads four **canonical** paths + only (`error.message`, `last_error.message`, `response.error.message`, + `response.incomplete_details.message`) and returns a value only when it is a string. + Echoed request content sitting elsewhere in the payload cannot reach the quota matcher. +- `src/server/responses/core.ts` — `shouldRetryCodexPoolAccountQuota` keeps 402/429 as an + immediate true, then admits 5xx **only** when the bounded body is both `displaySafe` and + not `truncated`. `fatalUtf8: true` rejects malformed UTF-8 rather than matching quota + words around replacement characters. The whole path is wrapped so a read failure returns + false — it fails closed, never rotates on an unreadable body. +- The fallback for non-JSON gateways returns the raw text only from the `catch`, so a + well-formed JSON body is never scanned wholesale. +- Request-log rendering stays limited to canonical fields, so the widened matcher does not + widen what gets logged. + +Verdict: the credential-boundary reasoning holds. The precedence the plan asked to verify +is present and is what bounds the blast radius. + +## #3177 — fix(responses): surface provider 413 as terminal context overflow (@Ingwannu) + +5 files, +350/-1. A streaming 413 became a 5/5 reconnect loop; it now converts to one +terminal `response.failed` with `context_length_exceeded` so Codex can compact next turn. +Bounded proxy-owned failure message, so an upstream 413 body cannot echo request content. + +**Draft.** Body says it stays draft until exact-head CI resolves. Action: check CI, mark +ready if green, merge. Closes bug issue #3170, so this is two items for one merge. + +## #3151 — fix(export): preserve Hermes vision capabilities (@Ingwannu) + +7 files, +97/-13. Replaces the Hermes string-only model array with the metadata map, so +`supports_vision` is emitted from exported catalog modalities. Closes #3146. + +**Draft with red CI** — `ci fail` and `macos fail`. The body claims the failures are +pre-existing. **The A-gate audit checked the logs and the claim is TRUE (A2):** `ci` is only +a rollup reporting `platform-macos=failure`, and the macOS job's single `(fail)` is +`server local API auth > websocket passthrough refreshes pool auth for each response.create +turn` (`tests/server-auth.test.ts:2302`) — a known macOS flake. This PR touches +`src/clients/config-export.ts` and the export tests, nowhere near websocket auth. + +Action: clear draft, merge. Do not waive the red by assertion — rerun the macOS job first +and merge on a green or same-flake result. + +## Execution order + +1. #3174 — maintainer-authored, self-contained, screenshots present. +2. #3177 — clear draft if CI is clean; closes #3170 too. +3. #3176 — read the credential-path diff first. +4. #3151 — diagnose the red CI before deciding merge vs. repair. + +## Verification (C) + +Per merged PR: `gh pr view --json state,mergeCommit`, then +`git merge-base --is-ancestor origin/dev` exiting 0. Linked issues closed by hand, +since PRs target `dev` rather than the default branch. diff --git a/devlog/_plan/260902_bug_label_drawdown/011_bd1_landing.md b/devlog/_plan/260902_bug_label_drawdown/011_bd1_landing.md new file mode 100644 index 0000000000..c68a2dc3ac --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/011_bd1_landing.md @@ -0,0 +1,32 @@ +# 011 — bd1 Batch A landing record + +## Merged + +| PR | Merge SHA | Note | +|---|---|---| +| #3174 gui mobile overflow | `e582aee214eec70f36be3062708bd1fddcf44807` | maintainer-authored, screenshots present | +| #3176 wrapped quota rotation | `2e2da87b512bde90a33c53d60d16550b885b9bc5` | credential path — review recorded in 010 | +| #3177 413 context overflow | `0d6424f80d0a6c28d2abc4816029944c5dade61f` | draft cleared first; closes #3170 | +| #3178 Hermes vision (carry of #3151) | `51c49177f59238d9e860895ffd76100c293ee4ff` | rebase service | + +All four proven ancestors of `origin/dev` with `git merge-base --is-ancestor`. + +## Rebase service, first use + +#3151 sat 105 commits behind `dev`. Its single commit `5ced04dc0` cherry-picked onto +current `dev` cleanly (one auto-merge in `structure/09_client-integrations.md`), author +credit preserved — `git show --stat` reports the same 7 files, +97/-13 as the original. +Focused suites: 100 pass, 0 fail across 5 export/CLI/management files. + +#3151 closed `landed-via-maintainer` naming the carry and the merge SHA, with the reason +for the carry and confirmation that the author's read of the red CI was correct. + +## Issues closed + +- **#3170** via #3177 — streaming 413 becomes one terminal `context_length_exceeded`. +- **#3146** via #3178 — Hermes export emits per-model capabilities. + +## Count + +Bug-labelled items: **24 → 19** (10 PRs + 9 issues). + diff --git a/devlog/_plan/260902_bug_label_drawdown/020_phase2.md b/devlog/_plan/260902_bug_label_drawdown/020_phase2.md new file mode 100644 index 0000000000..7d6f996e14 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/020_phase2.md @@ -0,0 +1,38 @@ +# 020 — Batch B: rebase service for CONFLICTING bug PRs + +Three PRs are `CONFLICTING`/`DIRTY`. The user authorized doing the rebase work rather than +waiting on contributors. + +- **#3168** fix(remote): restore authenticated GUI health (@Ingwannu) — 27 files, + +117/-20, `DIRTY`. Touches the remote-hub surface that moved heavily on `dev` this week, + which is almost certainly the conflict source. This is #3158's T3 follow-up. +- **#3148** fix(claude): keep proxy admission keys out of subscription launches + (@Veritas-7) — `CONFLICTING` + `CHANGES_REQUESTED`. Credential-boundary surface; + overlaps the shipped stale-credential work. Verify against current `src/cli/claude.ts` + before assuming it still applies. +- **#3135** fix(codex): retain caller main after pool rejection (@luvs01) — + `CONFLICTING` + `CHANGES_REQUESTED`, draft. The plan guessed #3166 might have subsumed + it. **The A-gate audit disproved that (A3): it is INDEPENDENT.** #3166 is the *initial + selection* boundary — keep a healthy request-owned `__main__` pin so Pool discovery does + not persist an exhausted stored account before the first send. #3135 is the *post-rejection + retry* — after a stored Pool credential is excluded, still allow one caller-owned main + send. The landed tree still shows the gap: `src/codex/auth-context.ts:510` retains + `!options.excludeAccountId` and `src/server/responses/compact.ts:385` still drops on + `!authCtx.accountId`. So this gets rebased, not closed. + + It is also `unsponsored_surface` on `src/codex/auth-context.ts`, the same credential + boundary as #3176. Rebasing is ours to do; merging needs the recorded security review. + +## Method per PR + +1. Fetch the head, rebase onto current `origin/dev` in a scratch branch. +2. Resolve conflicts by reading both sides — never by taking one wholesale. +3. If the contributor branch cannot be pushed to, cherry-pick unique commits onto + `codex/-carry` preserving author credit, open the carry PR, and close the original as + `landed-via-maintainer` naming the merge SHA. +4. If `dev` already contains the fix, close as superseded with the landing SHA that did it. + +## Verification (C) + +Rebased head resolves cleanly, focused tests for the touched subsystem pass, merge SHA +proven an ancestor of `origin/dev`. diff --git a/devlog/_plan/260902_bug_label_drawdown/021_bd2_landing.md b/devlog/_plan/260902_bug_label_drawdown/021_bd2_landing.md new file mode 100644 index 0000000000..47869db6f4 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/021_bd2_landing.md @@ -0,0 +1,66 @@ +# 021 — bd2 Batch B landing record: rebase service, three carries + +Every one of the three CONFLICTING bug PRs landed. None was closed as stale. + +| Original | Carry PR | Merge SHA | Author preserved | +|---|---|---|---| +| #3168 remote GUI health | #3179 | `eceb02d9d331d3f97b8f0d338c2bcd951778eb5a` | Ingwannu | +| #3135 caller-main retry | #3180 | `634d9e5a03a6bd23c7eaea101ca712b456e15991` | luvs01 (3 commits) | +| #3148 Claude subscription | #3182 | `865a36ef04eb6395e617f94ed87aaa474a903444` | Veritas-7 (2 commits) | + +All three proven ancestors of `origin/dev`. + +## What the conflicts actually were + +**#3168 — documentation only.** Both this PR and #3173 documented the same `/readyz` +protocol fields and the same retired `allowInsecureHttp` key, in the same week. Kept the +fuller wording on each side. No code conflicted. + +**#3135 — two real fixes in one `if`.** #3176 had added a 5xx quota-outcome recorder inside +the `no-alternate` branch; #3135 widens the guard on that same branch to admit `main`. +Taking either side alone would have silently dropped a shipped fix. Both kept: the guard +excludes `pool`, `main-pool`, and `main`, with the recorder inside. The test conflict was +purely additive and both authors' cases are retained — 70 pass, 0 fail proves it. + +**#3148 — a comment conflict hiding a real interaction.** The textual conflict was trivial +(`dev` had gained `explicitTarget` in the block whose comment the PR rewrote). The +interaction was not: resolving auth mode *before* adding credentials meant a machine whose +local environment reads as a Claude subscription stripped the admission token a **connected** +launch was explicitly constructed with. `tests/claude-cli.test.ts` caught it — expected +`ocx_data_connected`, received `undefined`. Fixed by gating the subscription strip on +`!explicitTarget`, with a regression. + +That third one is the argument for doing rebases rather than asking contributors to. The +conflict a contributor would have resolved was one comment; the defect underneath it only +shows up when you run the suite against current `dev`. + +## Security reviews recorded + +#3135 and #3148 both touch credential selection. Reviews were written into their PR bodies +**before** merge, on the exact head — unlike #3176 in Batch A, where the review was recorded +retroactively. That ordering is the process correction from the A-gate finding. + +## Count + +Bug-labelled items: **19 → 16** (7 PRs + 9 issues). + +## Why the rebase service is worth the maintainer time + +Three PRs had been sitting `CONFLICTING`, which reads on the board as "waiting on the +contributor". None of them actually needed contributor judgment. What they needed was +someone to run the rebase against a `dev` that had moved 100+ commits, and two of the three +conflicts were in documentation both sides had written independently. + +The cost was three cherry-picks and four conflict resolutions. The return was three bug +fixes landing that would otherwise have aged until they were stale enough to close. + +The #3148 case is the one to remember: the *conflict* was one comment, but the *interaction* +underneath it broke the connected-runtime launch path, and only running the suite against +current `dev` surfaced it. A contributor resolving that conflict on their own stale branch +would have resolved the comment correctly and shipped the defect. + +## Remaining after Batch B + +7 bug PRs: #3164 #3144 #3138 #3121 #3112 #3109 #3003 — all `CHANGES_REQUESTED`, which is +Batch C (maintainer-owned) and Batch D (contributor-owned). +9 bug issues: #3155 #3152 #3150 #3141 #3136 #2999 #2813 #1527 #1419 — Batches E and F. diff --git a/devlog/_plan/260902_bug_label_drawdown/030_phase3.md b/devlog/_plan/260902_bug_label_drawdown/030_phase3.md new file mode 100644 index 0000000000..aa90996c3b --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/030_phase3.md @@ -0,0 +1,24 @@ +# 030 — Batch C: changes-requested, maintainer-owned + +Three PRs authored by @lidge-jun or @luvs01 carrying `CHANGES_REQUESTED`. Maintainer-owned +means we can push to the branch directly. + +- **#3112** fix(codex): serialize native-main refresh on the CODEX_HOME claim — closes bug + issue #2999. Two items for one merge. +- **#3109** fix(compact): route combo compact requests through the failover path. +- **#3003** fix(codex): throttle repeated failed pool quota primes (draft). + +## Method + +Read the review threads first and classify each finding: still valid, already fixed, or +rebuttable. Apply the valid ones on the branch, reply to the rest with a reason, then +re-request review or merge on maintainer authority where the finding was addressed. + +Do **not** admin-merge over an unaddressed review comment — that is the line Batch C of the +previous campaign refused to cross for #2986, and it holds here. + +## Verification (C) + +Focused tests for the touched subsystem, then landing SHA ancestry. #2999 closed manually +once #3112 lands. + diff --git a/devlog/_plan/260902_bug_label_drawdown/031_bd3_landing.md b/devlog/_plan/260902_bug_label_drawdown/031_bd3_landing.md new file mode 100644 index 0000000000..9290afee87 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/031_bd3_landing.md @@ -0,0 +1,63 @@ +# 031 — bd3 Batch C landing record: maintainer-owned changes-requested + +All three landed. `CHANGES_REQUESTED` turned out to mean three different things. + +| Original | Rebase PR | Merge SHA | What the review state actually was | +|---|---|---|---| +| #3112 native-main claim | #3183 | `fecb77a91386a4b99c2524b8df9f91d0dcadaee8` | already fixed on branch | +| #3109 combo compact failover | #3184 | `afd5b4630dc59f891c4497174dd21b53ed24b400` | already fixed on branch | +| #3003 quota prime throttle | #3185 | `fe766e129441180c6fefcdc45b9e5609b2e2c326` | **genuinely open — fixed here** | + +All three proven ancestors of `origin/dev`. All three rebased without conflicts. + +## The lesson: read the thread against the current head, not the badge + +Every one of these read `CHANGES_REQUESTED` on the board. Two were stale — the reviewer's +finding had been fixed by a later commit on the same branch, so the thread stayed open while +the defect did not. + +- **#3112 P2** asked that claim waiting honor the refresh abort signal. + `src/codex/main-account.ts` already passed `{ waitMs: 30_000, signal }` with + `AbortSignal.any([dependencies.signal, refreshTimeout])` — delivered by "abort contended + native-main refresh claims", two commits after the reviewed one. +- **#3109 P1** asked that `ocx1` be decoded after account-gated combo failover. The branch + already keyed that decision on the **returned prefix** rather than the pre-failover child, + plus a second fix rejecting empty ciphertext where an empty `ocx1:` envelope decodes to + `""` rather than `null`. + +Closing either as "changes requested, contributor's move" would have stalled a landed fix. + +## #3003 was the real one + +CodeRabbit was right: the prune of removed-account markers sat **after** the +provider-eligibility early return, so a removal during a disabled window never reached it, +and restoring the same id inside `POOL_CACHE_TTL` read the stale failure as current. + +Fixed on the carry, not deferred. The existing test removed an account with the provider +**enabled**, which is exactly why this survived review — the disabled-window case now exists +and was verified red-green: moving the prune back turns it red (21/1), restoring it returns +green (22/0). + +## Count + +Bug-labelled items: **16 → 14** (5 PRs + 9 issues). + +## Scope discipline on #2999 + +030_phase3.md originally said #3112 "closes bug issue #2999. Two items for one merge." +The A-gate audit disproved that and the correction held through execution: #2999 describes +**two** races, and #3112 is explicitly only the lock-scope half — serializing two +`OPENCODEX_HOME`s against one `CODEX_HOME`. The publication/overwrite race is still carried +by the existing refuse-rather-than-overwrite check. + +So #3112's carry PR states that boundary in its own body and #2999 stays open. Closing it +by association would have been the cheap way to make the count drop by one; it would also +have buried a live race behind a green checkmark. + +The publication half is now Batch F work with its scope already written down. + +## Remaining after Batch C + +5 bug PRs, all contributor-owned (Batch D): #3164 #3144 #3138 #3121, plus whatever the +recount shows. +9 bug issues (Batches E and F): #3155 #3152 #3150 #3141 #3136 #2999 #2813 #1527 #1419. diff --git a/devlog/_plan/260902_bug_label_drawdown/040_phase4.md b/devlog/_plan/260902_bug_label_drawdown/040_phase4.md new file mode 100644 index 0000000000..9fb59b86ee --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/040_phase4.md @@ -0,0 +1,27 @@ +# 040 — Batch D: changes-requested, contributor-owned + +Four PRs from contributors we cannot push to. + +- **#3144** fix(cli): let an explicit different --port start a sibling (@olddonkey) +- **#3138** fix(service): report the wait actually spent, not the budget (@ntdatt812) +- **#3121** fix(openai): exclude user-owned alias overlays from canonical seed validation + (@Flowershangfromthebranches) +- **#3164** fix duplicate Codex restore warning after graceful stop (@x3M3x, draft) + +## Method + +For each: read the requested changes, then decide between three outcomes. + +1. **Small and mechanical** — carry it. Cherry-pick onto `codex/-carry`, apply the + requested fixes ourselves, land it, close the original `landed-via-maintainer`. +2. **Needs the author's design judgment** — leave a specific comment naming what is + outstanding and leave it open. This is a legitimate remaining item. +3. **Superseded or no longer applies** — close with the evidence. + +Carrying is the default here, since the goal is drawdown and the user authorized it. + +## Verification (C) + +Landing SHA ancestry per carried PR; original closed with a crediting comment naming both +the carry PR and the merge SHA. + diff --git a/devlog/_plan/260902_bug_label_drawdown/041_bd4_landing.md b/devlog/_plan/260902_bug_label_drawdown/041_bd4_landing.md new file mode 100644 index 0000000000..824c4b0ab6 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/041_bd4_landing.md @@ -0,0 +1,75 @@ +# 041 — bd4 Batch D landing record: contributor-owned, all four carried + +| Original | Rebase PR | Merge SHA | Author | +|---|---|---|---| +| #3138 service wait reporting | #3186 | `ea29e25b05cea7cefabad576e9dfe291e8d5daf0` | ntdatt812 | +| #3164 duplicate restore warning | #3187 | `d335570647ca0360e63745615901a10303042784` | x3M3x | +| #3144 explicit --port sibling | #3188 | `5ccf7c80016eddf66d297288488f1e1fd5022272` | olddonkey | +| #3121 alias overlay seed validation | #3189 | `5557772b7d6d11a560f9f910de350ab7cc855866` | Flowershangfromthebranches | + +All four ancestors of `origin/dev`. All four rebased without conflicts. + +## `CHANGES_REQUESTED` was stale on every one + +The plan's Batch D method offered three outcomes: carry it, leave it for the author's design +judgment, or close it as superseded. In the event, **none of the four had an unresolved +review thread** — a GraphQL query for `isResolved == false` returned empty on all of them. +The badge was left over from review rounds the authors had already answered. + +The only thing standing between these four fixes and `dev` was a rebase nobody had run. + +## What each fix was + +- **#3138** — `ocx service` reported the wait *budget* rather than elapsed time, so a probe + settling in 2s of a 30s budget still claimed 30s. +- **#3164** — graceful shutdown already did the shared Codex/Grok teardown, then `ocx stop` + and `ocx update` tried a second resume-history restore, so the warning appeared twice. + Caller-side restore is preserved for deferred receipts and hard-kill, where the proxy + never got to do it. +- **#3144** — `ocx start --port ` refused whenever a proxy was live, even on a *different* + port. An explicit different port is an unambiguous request for a sibling. The refusal is + narrowed, not removed. +- **#3121** — canonical seed validation counted user-owned alias overlays as canonical, so + an operator with their own alias could no longer save unrelated provider changes. + +## Focused verification + +| PR | Suites | Result | +|---|---|---| +| #3186 | `service` | 193 pass, 0 fail | +| #3187 | `grok-lifecycle`, `process-control-graceful`, `update-stop-first` | 54 pass, 0 fail | +| #3188 | `cli-dispatch`, `cli-ready` | 91 pass, 0 fail | +| #3189 | `management-provider-validation` | 91 pass, 0 fail | + +#3138's author reported 6 `service.test.ts` failures and believed they were pre-existing. +They did not reproduce at all here — that run was macOS, and those six are the +systemd-dependent cases `AGENTS.md` documents as environment-only. The author's read was +right. + +## Count + +**Open bug-labelled PRs: 0.** All 14 are closed — 4 merged directly, 10 rebase-carried. +Bug-labelled items: **14 → 9**, entirely issues now. + +## What the PR half of this campaign actually cost + +Fourteen bug PRs. Four merged as they stood. **Ten needed a rebase and nothing else.** + +Of those ten, exactly **one** had a genuinely open review finding (#3003's prune ordering, +fixed here with a red-green regression) and exactly **one** hid a real defect behind a +trivial-looking conflict (#3148's connected-target launch path). The other eight were +waiting on a mechanical operation. + +That ratio is the argument for the rebase service. A PR that reads `CONFLICTING` or +`CHANGES_REQUESTED` on the board looks like it is blocked on its author. Most of the time +it was blocked on a rebase, and the badge outlived the reason. + +The two that were not mechanical are also the argument for running the suite after the +rebase rather than trusting a clean cherry-pick: neither would have shown up in the conflict +markers. + +## Remaining: 9 bug issues + +#3155 #3152 #3150 #3141 #3136 #2999 #2813 #1527 #1419 — Batch E (needs-info triage) and +Batch F (implementable). The target is 3 or fewer, so at least six of these must reach a +terminal state. diff --git a/devlog/_plan/260902_bug_label_drawdown/050_bd5_replan.md b/devlog/_plan/260902_bug_label_drawdown/050_bd5_replan.md new file mode 100644 index 0000000000..5925f85442 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/050_bd5_replan.md @@ -0,0 +1,62 @@ +# 050 — bd5 replan: one issue per cycle + +## Why this doc exists + +Batches A through D bundled multiple pull requests into one PABCD cycle each. That was +wrong under the one-work-phase-one-cycle invariant, and it made the work hard to follow: +four merges landed inside a single B with one attest covering all of them. + +The remaining nine bug issues are re-registered as **nine separate work-phases**, one issue +each, in dependency order. Batch E and Batch F as bundles are retired. + +| WP | Issue | Why this order | +|----|-------|----------------| +| i3141 | #3141 responses-state write amplification | evidence already gathered | +| i3152 | #3152 dashboard log panel jitter | adjacent to the landed #3174 responsive work | +| i3136 | #3136 CommandCode cost recording | narrow provider-metadata question | +| i3150 | #3150 citation markers leak to TUI | provider-compatibility, needs a repro read | +| i3155 | #3155 Business Premium Seat coverage | entitlement surface | +| i1419 | #1419 bundled Bun SIGTRAP | oldest; runtime floor moved since | +| i2999 | #2999 native-main publication race | the half #3112 did NOT close | +| i2813 | #2813 gpt-reserve disables routed models | account-pool behavior | +| i1527 | #1527 Cursor adapter large-context collapse | hardest; adapter vs direct divergence | + +Each cycle: P re-reads the issue against the current tree, A audits the disposition, B does +the one fix or writes the one closure, C verifies it, D closes. No cycle handles two issues. + +## bd5 disposition + +This work-phase is closed as the **replan itself**. The five needs-info issues it originally +bundled are now i3141, i3136, i3150, i3155, and i1419. + +Nothing was closed under the bundled Batch E, so no disposition is lost. + +## bd6 disposition + +Identical treatment. Batch F bundled #3152, #3170, #2999, #2813, and #1527; those are now +i3152, i2999, i2813, and i1527 — four rather than five, because **#3170 already closed** in +bd1 via #3177 (`0d6424f8`). + +Both bundles are retired. Every remaining issue owns exactly one work-phase. + +## Evidence already gathered for i3141, carried forward + +The first per-issue cycle does not start cold. Reading #3141 against HEAD before the replan +turned up the following, which i3141's P should re-verify rather than rediscover: + +- The reported path still exists: `src/responses/state.ts:1127` returns + `join(getConfigDir(), "responses-state.json")`. The spill *directory* + (`RESPONSE_SPILL_DIR_NAME`, `spill-store.ts:33`) is a separate mechanism, so the triage + comment's "single json vs spill dir" question resolves as: the single file is still there. +- Write amplification is already bounded. `snapshotDebounceMs()` + (`src/responses/state.ts:1561`) scales the debounce linearly with the last snapshot size + from a 1 MiB floor, clamped at 30 s, and its comment names the exact failure the issue + describes: *"at the 24 MiB bound a fixed 2 s debounce is up to ~12 MB/s of write + amplification for state nothing reads until the next start (#2460)"*. +- A byte-identical snapshot is skipped entirely (`lastSnapshotDigest`, around line 1521). +- Both landed in `02c302a54`, *"fix(responses): stop rewriting an unchanged snapshot every + two seconds (#2476)"*, dated 2026-08-25, when `package.json` read **2.32.0**. + +#3141 reports **2.33.0**, which is *after* that commit — so the fix was present in the +reported version and the disposition is not a simple "already fixed". i3141 has to establish +whether 2.33.0 shipped it, and if it did, what remains unexplained. diff --git a/devlog/_plan/260902_bug_label_drawdown/050_phase5.md b/devlog/_plan/260902_bug_label_drawdown/050_phase5.md new file mode 100644 index 0000000000..8f2222c9ea --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/050_phase5.md @@ -0,0 +1,25 @@ +# 050 — Batch E: needs-info bug issues + +Five issues carry `needs-info`: #3155, #3150, #3141, #3136, #1419. + +`needs-info` means a maintainer asked the reporter for something. The honest dispositions +are narrow: + +1. **The information arrived** — the issue is actionable; move it to Batch F. +2. **The information never arrived and the issue is unreproducible without it** — close + with a comment naming what was asked, when, and that it can be reopened with the + detail. Age matters: #1419 dates to a much older Bun version. +3. **The tree answers the question** — resolve it from the source and either fix or close + with the explanation. + +**Never close one merely to reduce the count.** Each closure comment must name the specific +evidence, and any that genuinely needs the reporter stays open and counts against the +target. That is what the 5-item fallback exists for. + +Per issue, check: the last reporter comment date, whether the named version is still +current, and whether the described behavior still exists in the tree. + +## Verification (C) + +For each: closure comment naming evidence, or an explicit recorded blocker. + diff --git a/devlog/_plan/260902_bug_label_drawdown/051_i3141.md b/devlog/_plan/260902_bug_label_drawdown/051_i3141.md new file mode 100644 index 0000000000..04f6e77176 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/051_i3141.md @@ -0,0 +1,80 @@ +# 051 — i3141: responses-state disk write amplification + +One issue, one cycle. + +## What #3141 reports + +Windows 11, version **2.33.0**: writes to `%USER%/.opencode/responses-state.json` reaching +"10 or 100 MB/s", described as *directly proportional to concurrent consumer threads*, with a +proposal to keep the state in memory only. + +## What the tree says + +The mitigations the issue would need are **already in the reported version**, which is the +finding that changes the disposition: + +- `snapshotDebounceMs()` (`src/responses/state.ts:1561`) scales the flush debounce linearly + with the last snapshot size from a 1 MiB floor, clamped at `SNAPSHOT_DEBOUNCE_MAX_MS` = + 30 s. Its own comment names this exact failure: *"at the 24 MiB bound a fixed 2 s debounce + is up to ~12 MB/s of write amplification for state nothing reads until the next start + (#2460)"*. +- A byte-identical snapshot is skipped, and the skip is verified against the **file** rather + than a cached digest (`snapshotOnDiskMatches`, ~line 1521), so a second proxy sharing the + home cannot turn a repaired snapshot into a lost one. +- Both landed in `02c302a54` — *"fix(responses): stop rewriting an unchanged snapshot every + two seconds (#2476)"*, 2026-08-25. + +`git merge-base --is-ancestor 02c302a54 v2.33.0` → **exit 0**. The fix is in v2.33.0, and +`git show v2.33.0:src/responses/state.ts` carries the same three constants HEAD has: +`SNAPSHOT_DEBOUNCE_MS = 2_000`, `SNAPSHOT_DEBOUNCE_MAX_MS = 30_000`, +`SNAPSHOT_TOTAL_MAX_BYTES = 24 * 1024 * 1024`. + +## The arithmetic that decides this + +One debounce timer exists per process, not per consumer. So the steady-state write rate is +bounded by *snapshot size ÷ debounce*, and both ends are clamped: + + 24 MiB ÷ 30 s ≈ 0.8 MB/s + +Even doubling for the atomic temp-plus-rename, the ceiling is ~1.6 MB/s. The report says +10-100 MB/s. That is one to two orders of magnitude apart, **on the same code**. + +"Proportional to concurrent consumers" is consistent with the mechanism — more concurrent +chains means a larger and more frequently-changing snapshot, which defeats the +identical-payload skip and stretches toward the 24 MiB bound — but the *magnitude* is not. + +## Disposition: NEEDS_REPRO, stays open + +Not "already fixed": the fix predates the reported version, so repeating it would be wrong. +Not closeable either: the numbers do not reconcile, and something unexplained is producing +them. + +What the report needs to become actionable: + +1. Re-measure on 2.40.0 with Process Monitor, filtered to the exact path. +2. Separate `responses-state.json` from the `responses-state-spill/` directory + (`spill-store.ts:33`) — they are different mechanisms and the screenshot cannot + distinguish them. +3. Report observed snapshot **size** alongside the rate. If the file is far under 24 MiB and + the rate is still tens of MB/s, the debounce is being bypassed and that is a real defect + worth its own cycle. + +This counts against the ≤3 target as a **recorded blocker**: it needs reporter data that +cannot be inferred from the tree. + +## Action taken + +Re-triage comment posted to the issue +([comment 5497904367](https://github.com/lidge-jun/opencodex/issues/3141#issuecomment-5497904367)) +carrying the ancestry proof, the shared-constants readout, the 0.8 MB/s arithmetic, and the +three measurements that would make the report actionable. The memory-only proposal is +answered directly rather than ignored: it trades this for lost continuation history across +restart and crash, and the reporter's file-size measurement is what decides whether the +safer fix is tightening the write path instead. + +Issue left **OPEN** with `needs-info`. Labels unchanged. + +## Terminal outcome + +`NEEDS_HUMAN` — specifically, reporter measurement. Not `BLOCKED` (nothing external is +broken) and not `DONE` (no code changed). diff --git a/devlog/_plan/260902_bug_label_drawdown/052_i3152.md b/devlog/_plan/260902_bug_label_drawdown/052_i3152.md new file mode 100644 index 0000000000..5c1b7c6b86 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/052_i3152.md @@ -0,0 +1,81 @@ +# 052 — i3152: dashboard log panel jittering + +One issue, one cycle. **Outcome: NEEDS_REPRO. No code shipped.** + +This doc records a diagnosis that measurement disproved, because the wrong explanation is +cheap to re-derive and expensive to re-test. + +## What #3152 reports + +Dashboard 2.39.0 viewed from Windows 11 against a CentOS 7 host. The Logs table "quivers". +Two details identify the shape: it jitters **when scrolled to the top** and stops after +scrolling down slightly, and at one scroll position the layout **alternates between two +states**. The reporter could not screenshot it and photographed the screen — so it is a +per-frame oscillation, not a static misalignment. + +## The diagnosis I wrote, and why it was wrong + +The Logs table is virtualized (`useVirtualizer`, `gui/src/pages/Logs.tsx:522`) inside a real +`` with automatic layout, and the virtualizer renders spacer `` rows for the +off-screen extent. The story wrote itself: spacer rows take part in auto column-width +computation, width changes re-wrap `.log-col-model` (`max-width: 16ch`, `break-word`), +re-wrapping changes row height, height feeds back into `measureElement`. At `scrollTop 0` +the `paddingTop > 0` guard removes the leading spacer entirely, which explained the +top-of-scroll case exactly. + +It is a tidy explanation and it survived a code read. It did not survive a browser. + +**Probe 1 — does a spacer row move columns?** Standalone table, same structure, spacer +height 0 vs 500px: + + auto: "145,530,224" → "145,530,224" changed: false + fixed: "300,300,300" → "300,300,300" changed: false + +The spacer carries `colspan` and no content, so it contributes nothing to intrinsic column +widths under either layout. The premise was false. + +**Probe 2 — does `table-layout: fixed` stop the height feedback?** Same table, one long +model name entering the window: + + auto: heights [23,23,23] → [65,23,23] + fixed: heights [23,23,23] → [65,23,23] + +Identical. `max-width: 16ch` wraps the cell in *both* layouts, so the proposed fix would not +have broken the loop even if the loop existed. + +**Probe 3 — reproduce the oscillation.** 40 frames alternating `scrollTop` between 0 and 60, +with 24 rendered rows, then again with heterogeneous model-name widths, then 120 frames at +the top while new requests streamed in with auto-refresh on: + + distinct column layouts: 1 + distinct scroll extents: 1 + drift: 0 + +Under both `auto` and `fixed`. I could not make it jitter. + +## What is true, and still not a proven cause + +`estimateSize: () => 44` is measurably wrong: rendered rows are **80-119px, mean 92** — the +time, token and status cells each stack two or three lines. The estimate places every +*unmeasured* row, so a 2x error does move the scroll extent as rows get measured. + +But changing it to 92 produced no measurable difference here (`drift: 0` both ways), because +with ~30 logs every row is measured almost immediately. The regime where it would bite is a +log list long enough that most rows stay unmeasured — which is plausibly the reporter's +situation and is exactly what I cannot reproduce locally. + +Shipping that change would have been a guess wearing a measurement's clothes. Reverted. + +## Disposition + +**NEEDS_REPRO**, issue stays open. What would decide it: + +1. Roughly how many rows were in the list — the estimate hypothesis needs a long list. +2. Whether the jitter survives with `Auto-refresh` **off**. That separates a render-loop from + a data-arrival effect, and it is a one-click test. +3. Browser and zoom level. The two-state alternation at a fixed scroll position smells like + fractional device-pixel rounding, which is display-scale dependent and would explain why + it reproduces for the reporter and not here. + +Counts against the ≤3 target as a **recorded blocker**: reporter environment detail. + diff --git a/devlog/_plan/260902_bug_label_drawdown/053_i3136.md b/devlog/_plan/260902_bug_label_drawdown/053_i3136.md new file mode 100644 index 0000000000..07a7187c67 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/053_i3136.md @@ -0,0 +1,89 @@ +# 053 — i3136: CommandCode models show no cost + +One issue, one cycle. + +## What #3136 reports + +Dashboard 2.39.0. Every request through `commandcode/deepseek/deepseek-v4-flash` shows no +cost, with the log detail carrying: + + "cost": { "kind": "unavailable", "reason": "combo_attempt_unavailable" } + +## Root cause, proven by execution not by reading + +Price resolution ends at `resolveModelLevelPrice` (`src/usage/cost.ts:316`), which calls +`findVendorCostByModelId(modelId)`. That function does an **exact** row match +(`model-metadata.ts:84`: `DATA[provider]?.find(r => r[0] === modelId)`), with one +normalization: dots to dashes. + +CommandCode is an aggregator, so its model ids carry the vendor as a path segment. Executed +against the real catalog: + + deepseek/deepseek-v4-flash -> undefined + deepseek-v4-flash -> { provider: "deepseek", cost: { input: 0.14, output: 0.28, ... } } + deepseek/deepseek-v4-pro -> undefined + deepseek-v4-pro -> { provider: "deepseek", cost: { input: 0.435, output: 0.87, ... } } + +The price exists. The vendor prefix is the only thing between the row and the lookup. +`resolveMetadataProvider("commandcode-api")` and `("commandcode-auth")` both return +`undefined`, so the bundled-metadata path does not rescue it either. + +## Not a CommandCode bug — a slashed-id bug + +The same probe against OpenRouter-shaped ids: + + anthropic/claude-opus-4-6 -> UNPRICED (tail: priced) + openai/gpt-5.6 -> UNPRICED (tail: priced) + deepseek/deepseek-v4-flash-> UNPRICED (tail: priced) + +381 of 382 `openrouter` catalog rows are themselves slashed, so those resolve through their +own provider rows — but any aggregator whose provider is *not* in the catalog loses pricing +for every model it serves. + +## The risk that shapes the fix + +A naive "strip everything before the slash" is wrong. Probing vendor agreement: + + deepseek/deepseek-v4-flash tail resolves to deepseek vendor matches: YES + anthropic/claude-opus-4-6 tail resolves to anthropic vendor matches: YES + openai/gpt-5.6 tail resolves to openai vendor matches: YES + x-ai/grok-4.6 tail resolves to xai vendor matches: NO + google/gemini-3.6-pro tail resolves to none + moonshotai/kimi-k3 tail resolves to none + +`x-ai` vs `xai` is the warning: the prefix is the caller's claim about the vendor, and +`findVendorCostByModelId` returns whatever `COST_VENDOR_PRIORITY` reaches first. Stripping +blindly would let a prefix disagree with the row that gets used, and price a model against +the wrong vendor. + +## MODIFY map + +**`src/usage/cost.ts`**, in `resolveModelLevelPrice` only — after the existing exact and +dot-to-dash attempts, before returning null: + + // Aggregators (CommandCode, OpenRouter-shaped presets) spell a model as + // "/". The catalog stores the bare id, so an exact lookup misses a + // price that is present (#3136). Retry on the tail, but ONLY when the prefix agrees + // with the vendor the catalog row belongs to: "x-ai/grok-4.6" resolves to vendor + // "xai", and accepting a mismatch would price a model against a vendor the caller + // never named. + +Match on a normalized comparison (strip dashes, lowercase) so `x-ai` and `xai` agree while +a genuine disagreement still fails closed. Unprefixed ids and unknown tails are untouched. + +## TESTS + +**`tests/usage-cost.test.ts`** (or the nearest existing cost suite): + +1. `deepseek/deepseek-v4-flash` now prices, and matches the bare-id price exactly. +2. `x-ai/grok-4.6` prices, because `x-ai` and `xai` are the same vendor after normalization. +3. A mismatched prefix — e.g. `openai/claude-opus-4-6` — still returns null rather than + silently pricing Claude as OpenAI. +4. `google/gemini-3.6-pro` (tail unknown to the cost catalog) stays null. +5. An unprefixed id is unchanged. + +## Verification (C) + +Focused `bun test` on the cost suite plus red-green on case 3, which is the one that would +turn a fix into a mispricing. + diff --git a/devlog/_plan/260902_bug_label_drawdown/054_i3150.md b/devlog/_plan/260902_bug_label_drawdown/054_i3150.md new file mode 100644 index 0000000000..633bc06e4b --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/054_i3150.md @@ -0,0 +1,96 @@ +# 054 — i3150: citation control markers leak into the Codex TUI + +One issue, one cycle. + +## What #3150 reports + +Codex CLI through OpenCodex to `github-copilot/gpt-5.6-sol` renders assistant text as: + + The setting is supported. citeturn1view0turn1view1 + +The delimiters are Unicode private-use characters: + + \uE200 cite \uE202 turn1view0 \uE202 turn1view1 \uE201 + +`U+E200` opens, `U+E202` separates, `U+E201` closes. `turn1view0` is an opaque, +turn-scoped source id that means nothing to a user. They appear in both commentary and the +final answer, and they persist into the saved transcript. + +This is an unusually good report — it names the exact codepoints and proposes three +candidate origins. + +## Where the markers come from + +`rg` across `src/` for `E200`, `E201`, `E202`, `uE20`, `citeturn`, and private-use +handling returns **nothing**. OpenCodex neither emits these markers nor recognizes them. + +The repository's citation support is entirely structured: `OcxUrlCitation` in +`src/types.ts`, source collection in `src/web-search/loop.ts`, and +`takeWebAnnotations()` in `src/bridge.ts` which binds `url_citation` annotations onto the +assistant message at `closeCurrentMessage()` (`bridge.ts:566-592`). + +So of the reporter's three hypotheses, it is **(1)**: the markers are already literal text in +the upstream response. GitHub Copilot's backend is ChatGPT-derived and emits ChatGPT's +private-use citation grammar; the desktop client renders it, the Codex TUI does not, and +OpenCodex passes the text through untouched. + +That makes it our problem to fix even though we do not create it. The proxy is the last +place that can see the text before a client that cannot render it. + +## The constraint that shapes the fix + +Assistant text reaches the client twice, and both paths must be handled: + +- **Streaming**: `response.output_text.delta` (`bridge.ts:947`) emits each chunk as it + arrives, and `closeCurrentMessage()` re-sends the accumulated text in + `response.output_text.done`. +- **Non-streaming**: `flushText()` (`bridge.ts:1621`) builds the message once. + +A marker can straddle a delta boundary — `\uE200cite` in one chunk and the rest in the +next — so a stateless per-delta strip would leak the tail. Whatever holds the partial marker +must live across deltas. + +## MODIFY map + +**NEW `src/responses/citation-markers.ts`** — a leaf module, no imports beyond types: + +- `CITATION_MARKER_START = "\uE200"`, `SEP = "\uE202"`, `END = "\uE201"`. +- `stripCitationMarkers(text: string): string` — removes complete + `START … END` spans. Used by the non-streaming path and by any whole-text consumer. +- `createCitationMarkerFilter()` — a small stateful filter for the streaming path: + `push(delta): string` returns the safe-to-emit prefix and **withholds** any trailing + partial marker; `flush(): string` returns whatever is left when the message closes, so an + unterminated marker is not silently swallowed. + +Withholding rather than dropping matters: if a stream ends mid-marker, the bytes must still +reach the user rather than vanishing. + +**MODIFY `src/bridge.ts`** — apply the filter at the two emission points named above. The +accumulated `currentMsg.text` must be filtered the same way, since `closeCurrentMessage()` +re-sends it in `response.output_text.done` and `response.output_item.done`. + +## Scope boundary + +Strip only. Converting `turn1view0` into a readable link is **not** possible here: the ids +are turn-scoped and opaque, and the upstream response carries no mapping to a URL. The +reporter's option 3 ("remove the presentation marker cleanly") is the honest one, and +options 1 and 2 would require source metadata we do not receive. + +The structured `url_citation` path is untouched, so the desktop Sources chips keep working. + +## TESTS + +**NEW `tests/citation-markers.test.ts`**: + +1. A complete marker span is removed; surrounding text is intact. +2. Multiple spans in one string. +3. A marker split across two deltas is removed, not leaked. +4. An unterminated marker is flushed rather than swallowed. +5. Text containing no markers is byte-identical (the common case must not be touched). +6. A lone `U+E200` with no terminator does not eat the rest of the message. + +## Verification (C) + +Focused `bun test` on the new file plus the bridge suites, with red-green on case 3 — the +split-delta case is the one a naive implementation gets wrong. + diff --git a/devlog/_plan/260902_bug_label_drawdown/055_i3155.md b/devlog/_plan/260902_bug_label_drawdown/055_i3155.md new file mode 100644 index 0000000000..fabe66aa55 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/055_i3155.md @@ -0,0 +1,102 @@ +# 055 — i3155: Business Premium Seat excluded from capacity coverage + +One issue, one cycle. + +## What #3155 reports + +An OpenAI Business account upgraded to a **Premium Seat** (introduced 2026-08-25) started +showing, under Rate Limits: + + Incomplete coverage: 1 account(s) excluded, including 1 unknown plan(s) + +It did not appear before the upgrade. Version 2.39.0. + +## Root cause + +`aggregateCodexPoolCapacity` weights each account by its plan +(`src/providers/codex-capacity.ts:190`), and `configuredWeight` returns `undefined` for a +plan that is not in a hardcoded map (`codex-capacity.ts:3-9`): + + plus: 1, team: 1, business: 1, prolite: 5, pro: 20 + +An account with no weight is counted as `unknownPlanAccounts` and **skipped** at line 207, +which is exactly the warning the reporter sees. The Premium Seat upgrade changes the plan +string upstream reports, and the new string is not in that five-entry map. + +## The map is far behind reality + +The bundled upstream snapshot carries **21 distinct plan strings**: + + business, edu, edu_plus, edu_pro, education, enterprise, + enterprise_cbp_automation, enterprise_cbp_usage_based, finserv, free, + free_workspace, go, hc, k12, plus, pro, prolite, quorum, sci, + self_serve_business_usage_based, team + +The weight map knows five of them. So this is not a Premium Seat bug — **16 known plan +strings already produce the same warning**, and Premium Seat is simply the one that made a +user notice. + +## This repository already learned this lesson + +`src/codex/quota.ts:141-150` carries the argument verbatim, about the same plan field: + +> An allowlist of "known" plans was tried here and was wrong: the upstream model snapshot +> alone carries 21 distinct plan strings […] and `CodexAccount.plan` is an unrestricted +> string, so any list is a list of the plans someone remembered. Twelve real plans would +> have been refused recovery and stayed cooled forever — the very defect this unit exists +> to fix, reintroduced as a typo-shaped hole. + +The capacity map is that same shape, one file over. Adding `premium` to it would fix this +report and leave the other sixteen. + +## What the exclusion actually costs + +`aggregateCodexPoolCapacity` is documented **display-only**: *"It never participates in +account selection or routing"* (`codex-capacity.ts:166`). So the account still routes +normally; it is missing from the dashboard's weighted estimate and produces the warning. +That bounds the blast radius of a wrong default. + +## MODIFY map + +**`src/providers/codex-capacity.ts`** — keep the map as *calibrated* weights and add a +default for everything else, rather than excluding: + + /** + * Weight for a plan not in the calibrated map. An unrestricted upstream string cannot + * be enumerated - the bundled snapshot alone carries 21 plan names and this map lists + * five - so an unknown plan is counted at the baseline seat weight rather than dropped + * from the estimate entirely (#3155). Under-counting a large seat is a visibly + * conservative estimate; excluding it silently reports coverage the operator does not + * have. + */ + const CODEX_DEFAULT_CAPACITY_WEIGHT = 1; + +`configuredWeight` returns the calibrated weight when known, else the default. `plus`, +`team`, and `business` are already 1, so the default matches the most common seat. + +`unknownPlanAccounts` keeps counting uncalibrated plans — that number is still worth +surfacing, because the estimate for a Pro-sized unknown seat would be low. But it stops +gating inclusion at line 207. + +**GUI wording** (`gui/src/i18n/*`): `pws.capacity.incomplete` currently says accounts are +*excluded*. With unknown plans included at baseline the message must stop claiming +exclusion for them; a plan counted at baseline is "estimated conservatively", not missing. +Exclusion for paused / needs-reauth / stale-quota accounts is unchanged and still reported. + +## TESTS + +**`tests/codex-capacity.test.ts`** (or nearest existing): + +1. An account with an unrecognized plan string now contributes at weight 1 instead of being + skipped, and `excludedAccounts` no longer counts it. +2. `unknownPlanAccounts` still reports it, so the estimate's uncertainty stays visible. +3. Calibrated weights are unchanged: `pro` still 20, `prolite` still 5. +4. Paused, needs-reauth, missing-quota, and stale-quota accounts are still excluded — the + default must not resurrect an account that is excluded for a different, real reason. +5. An account with no plan at all behaves the same as an unknown plan. + +## Verification (C) + +Focused `bun test` on the capacity suite, with red-green on case 4 — that is the one where +a careless change would start counting genuinely unusable accounts. + diff --git a/devlog/_plan/260902_bug_label_drawdown/056_i1419.md b/devlog/_plan/260902_bug_label_drawdown/056_i1419.md new file mode 100644 index 0000000000..d427b197db --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/056_i1419.md @@ -0,0 +1,73 @@ +# 056 — i1419: bundled Bun SIGTRAP after TLS verification failures + +One issue, one cycle. **Outcome: NEEDS_HUMAN — reporter artifact. Stays open.** + +## What #1419 reports + +OpenCodex 2.11.1 on macOS arm64, bundled **Bun 1.3.14**. Twice, ~0.5s after two consecutive +`unknown certificate verification error` results, the Bun process died with +`EXC_BREAKPOINT (SIGTRAP)` on the main thread. Identical native signature both times: same +image UUID `c7e7a979-…`, same top offsets `52255300, 52218912, 15551472`. No JS crash log, +consistent with a native trap bypassing JS handling. No launchd service, so nothing +restarted it and the dashboard died with the proxy. + +The report is unusually careful — it even rules out its own prime suspect, noting 2.10.2 and +2.11.1 bundled the same Bun and the retry path was unchanged, so it may predate 2.11.1. + +## What has changed since, and what that is worth + +**The runtime moved.** `27764f342` bumped the bundled Bun from **1.3.14 to 1.4.0** and +pinned `MIN_FIXED_BUN_VERSION` to it in the same commit. That version boundary is not +arbitrary: 1.4.0 is the first released Bun proven to carry PR #32120, the fix for the +Bun#32111 use-after-free that this repository already works around in three places +(`bun-stream-caps.ts:5`, `crash-guard.ts:166`, `types/config.ts:466`). + +**That is suggestive, not sufficient.** #32111 is a stream-teardown use-after-free and the +reported crash follows TLS verification failures — adjacent, not identical. Nobody has named +a Bun change that addresses *this* trap, and the 2026-08-31 triage already ran 100 +self-signed and 100 connection-reset cases on 1.4.0 without reproducing it. A non-repro on a +runtime the reporter was not running is not evidence about their crash. + +**Half the report did get addressed.** The second complaint was that an unsupervised native +crash left no trace and no recovery. `src/cli/doctor.ts:977` now carries `(#1419)` by name: +persisted owner records outliving their process are surfaced as *"Stale process records +remain, so the previous run may have exited unexpectedly"* — deliberately cause-neutral, +because disk state proves an unclean exit, not which signal caused it. So a recurrence is +now visible in `ocx doctor` instead of silent. + +## Why this cannot be closed + +Closing as fixed would assert that 1.4.0 resolves it. No one has shown that. The honest +options were: fix it, prove it fixed, or say what would settle it — and only the third is +available without the crash frames. + +The reporter states the `.ips` files exist and can be provided after redaction. That offer +is the whole path forward and it has not been taken up in a way that produced the files. + +## Action taken + +Re-triage comment recording: the runtime moved to a version whose fix boundary is documented, +the stale-process-state detection landed under this issue number, the audit result and its +explicit limits, and a redaction-safe recipe for the one artifact that would make this +actionable — the crashed thread's frames, which is what distinguishes a TLS-path trap from a +stream-teardown one. + +No code change. Inventing a defensive wrapper for a native trap whose frames are unknown +would be guessing at the crash site. + +## Terminal outcome + +`NEEDS_HUMAN` — a reporter artifact that cannot be inferred from the tree. Counts against +the ≤3 target as a **recorded blocker**. + +## Action taken (recorded) + +Comment posted: +[issuecomment-5498350641](https://github.com/lidge-jun/opencodex/issues/1419#issuecomment-5498350641). + +It gives the reporter a redaction recipe that keeps the useful part rather than asking for +the whole file: `grep -A 40 '"faultingThread"' .ips`, or the `Thread 0 Crashed:` +block. Binary offsets and image names are what matter; local paths can be stripped freely. +That is the difference between an ask they have to think about and one they can run. + +Issue left **OPEN**. Labels unchanged. diff --git a/devlog/_plan/260902_bug_label_drawdown/057_i2999.md b/devlog/_plan/260902_bug_label_drawdown/057_i2999.md new file mode 100644 index 0000000000..7eff826536 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/057_i2999.md @@ -0,0 +1,117 @@ +# 057 — i2999: native-main publication can overwrite an external Codex writer + +One issue, one cycle. This is the **half #3112 did not close**. + +## What remains + +#2999 named two races. #3112 (landed as `fecb77a9`) fixed the coordination half — +native-main refresh now serializes on the canonical `CODEX_HOME` claim, so two OpenCodex +instances with different `OPENCODEX_HOME` values no longer race each other. + +The publication half is still open, and the code says so plainly. + +`persistRefreshedMainAuthJson` (`src/codex/main-account.ts:136`) hashes `auth.json`, then +writes through `atomicWriteFile` with two guards: + + beforeRename: () => assertMainAuthJsonSnapshotUnchanged(expected) + validateBeforeRename: () => assertMainAuthJsonSnapshotUnchanged(expected) + +Both re-read the file and compare `rawSha256`. That closes most of the window. It does not +close the last of it, and the reason is visible in `src/config/atomic-write.ts:190-192`: + + hooks.beforeRename?.(tmp, target); + hooks.validateBeforeRename?.(target); + effective.rename(tmp, target); + +Validation and `rename` are two syscalls. A Codex writer that replaces `auth.json` between +line 191 and line 192 is overwritten — the check passed against bytes that no longer exist +by the time the rename lands. Re-checking closer to the rename shrinks the window; it cannot +remove it, because `rename(2)` unconditionally replaces the destination. + +## Why this matters more than a normal race + +The file is a **credential**, and the loser of the race is Codex CLI itself. Overwriting it +means the user's own `codex login` result is silently replaced by a token OpenCodex staged +from an older read. There is no error and no recovery path — the next Codex invocation just +uses a credential the user did not authorize. + +## What the issue asked for, and what is available + +The issue asks publication to "preserve an external writer atomically". The primitive that +does that is a **compare-and-swap rename**: replace the target only if it is still the file +we validated. + +`rg` for `renameat2`, `RENAME_EXCH`, `linkSync`, `O_EXCL`, and `exchangedata` across +`src/` returns nothing, so no such primitive exists here yet. The portable construction is: + +- `link(2)` the staged temp to a fresh unique name, then verify the target's identity + (device + inode + size + hash) **and** that our staged link is still the one we made, + before the final rename. `link` fails with `EEXIST` rather than clobbering, which is the + atomic half `rename` lacks. +- On the same filesystem, comparing `fstat` device/inode of the validated handle against + the path at rename time detects a swap that a content hash alone would miss (a writer can + restore identical bytes with a different inode, and — the case that matters — write + *different* bytes that our stale hash would reject only if we re-read at the right instant). + +## MODIFY map + +**`src/config/atomic-write.ts`** — extend the hook contract so a caller can demand +identity-checked replacement rather than a bare rename: + + /** + * Verify the target's identity immediately before rename and refuse the replacement + * when it changed. Content hashing alone cannot close the check→rename window + * (#2999): rename(2) replaces unconditionally, so a writer landing between the two + * syscalls wins silently. + */ + verifyTargetIdentityBeforeRename?: (targetPath: string) => void; + +The narrower, safer change: capture `statSync` of the target *inside* the same guard that +runs `validateBeforeRename`, and re-verify device+inode immediately before `effective.rename` +— so the two syscalls bracket an identity check rather than a content check. + +**`src/codex/main-account.ts`** — record the target's `dev`/`ino` alongside `rawSha256` in +`MainAuthJsonCredential`, and have `assertMainAuthJsonSnapshotUnchanged` compare identity as +well as content. + +## Honest scope note + +This narrows the window; it does not prove it closed. A truly atomic +compare-and-swap needs `renameat2(RENAME_EXCHANGE)` (Linux) or an equivalent, which Bun does +not expose. The PR must say that plainly rather than claiming the race is eliminated. + +## TESTS + +**`tests/codex-main-account-refresh.test.ts`** — the existing +`setMainAuthJsonBeforeRenameHookForTests` hook is exactly the injection point the issue's +reproduction step 5 describes: + +1. External writer replaces `auth.json` with **different bytes** at the hook → publication + refuses, external content preserved byte-for-byte. +2. External writer replaces it with **identical bytes but a new inode** → identity check + catches what the hash cannot. +3. No external writer → publication succeeds, tokens updated (the happy path must not + regress). +4. The canonical target still exists after a refused publication — never unlinked. + +## Verification (C) + +Focused `bun test` on the refresh suite, red-green on case 1 and case 2 separately: case 1 +must fail without the guard, case 2 must fail with only a content hash. + +## Outcome + +Landed as #3199, `c17bc94c2faa9b296a95d8529019579df177de02`. Identity (`dev`+`ino`) is now +compared alongside `rawSha256`, failing closed when identity cannot be read on either side. + +`bun test ./tests/codex-main-account-refresh.test.ts` — 7 pass, 0 fail. Removing only the +identity check reds the same-bytes-new-inode case (6/1) and leaves the rest green, which is +the proof it does work the hash did not. + +**#2999 stays OPEN, re-scoped.** The check runs before `rename(2)`, not atomically with it. +Closing the last window needs `renameat2(RENAME_EXCHANGE)` or an equivalent, which Bun does +not expose. Claiming the race eliminated would have been the easy way to drop the count by +one; the comment on the issue says plainly what is closed and what is not. + +Terminal outcome: `DONE` for the publication guard, with the atomic primitive recorded as +remaining work on the issue. diff --git a/devlog/_plan/260902_bug_label_drawdown/058_i2813.md b/devlog/_plan/260902_bug_label_drawdown/058_i2813.md new file mode 100644 index 0000000000..182ee3b666 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/058_i2813.md @@ -0,0 +1,80 @@ +# 058 — i2813: routed models unselectable during Luna Reserve + +One issue, one cycle. **Outcome: client-side limitation, documented. Issue closes.** + +## What #2813 reports + +Codex 2.34.0 on Windows 11. Once the 5-hour ChatGPT quota is exhausted and Codex activates +`gpt-reserve` / Luna Reserve, **all other picker entries become unselectable — including +OpenCodex routed models**, which run on independent providers and credentials and consume +none of the exhausted quota. + +The reporter framed the right question themselves: if the Codex client gates availability +before requests reach OpenCodex, can the proxy work around it, and if not, say so. + +## Where the gate lives + +OpenCodex does not model this at all. `rg` across `src/` and `docs-site/` for +`gpt-reserve` returns nothing: we never emit a reserve marker, and the catalog sync path has +no reserve concept. + +The installed Codex CLI **0.150.1** binary settles it. Strings show the reserve state is a +**server-supplied** field, not a local inference: + + struct RateLimitSnapshot with 9 elements + limit_name primary secondary credits individual_limit + spend_control_reached plan_type rate_limit_reached_type + + struct RateLimitReachedType with 1 element + struct RateLimitStatusDetails with 4 elements + rate_limit spend_control primary_window additional_rate_limits + +and the transport that carries it: + + x-codex-rate-limit-reached-type + x-codex-safety-buffering-faster-model + +Alongside them, two UI surfaces named for exactly this state: `model_availability_nux` and +`hide_rate_limit_model_nudge`. + +Notably, `gpt-reserve` itself does **not** appear in the binary. The reserve model and the +gating decision both arrive from the ChatGPT backend; the client renders what it is told. + +## Why the proxy cannot fix this + +The picker is populated and gated **before** any request reaches OpenCodex, from a +`RateLimitSnapshot` the client receives on its own authenticated ChatGPT connection. Nothing +in the model catalog we sync — the only channel we own — participates in that decision. + +Three non-options, stated so they are not re-litigated: + +- **Catalog representation.** We already write routed entries as ordinary catalog models. The + gate is not reading our entries' shape; it is applying a global availability state. +- **Suppressing the reserve state.** The header arrives on the client's own ChatGPT + connection, not through the proxy's data plane. There is nothing for us to intercept. +- **Faking quota headroom.** Even if reachable, misreporting a user's quota to their own + client is the kind of fix that produces a worse bug — and it would be lying to the user + about their account. + +## Disposition + +The reporter's fallback ask is the correct outcome: **document it as a Codex client +compatibility limitation.** That is honest, actionable for anyone who hits it, and does not +leave a bug open against code that cannot contain the defect. + +Workaround worth naming: routed models stay reachable through any client that does not gate +on the ChatGPT rate-limit snapshot — Claude Code through the proxy, or a direct HTTP client +against `/v1`. The proxy and its providers are unaffected; only the Codex picker is. + +## MODIFY map + +**`docs-site/src/content/docs/guides/codex-integration.md`** — a short subsection under the +existing troubleshooting material: what the user sees, why it happens, that it is +client-side, and the workaround. English source only; translated locales are left rather +than half-translated. + +## Verification (C) + +`rg` proof that the section exists and names the reserve state. Docs build runs in CI's +`gates` job. + diff --git a/devlog/_plan/260902_bug_label_drawdown/059_i1527.md b/devlog/_plan/260902_bug_label_drawdown/059_i1527.md new file mode 100644 index 0000000000..7d44193021 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/059_i1527.md @@ -0,0 +1,53 @@ +# 059 — i1527: Cursor large-context collapse / rate-limit asymmetry + +One issue, one cycle. **Outcome: no proxy-side defect left that the evidence supports. +Issue stays open as a recorded blocker (`needs-info`) pending a matched direct-vs-adapter +trace.** + +## What #1527 reports + +Large-context turns through the Cursor adapter either collapse to a short answer or hit +429 while the same conversation in the Cursor client stays healthy. The reporter's +control run was direct Cursor on the same account. + +## What has already landed against it + +| Mechanism | Fix | Evidence on `dev` | +| --- | --- | --- | +| Full-history replay every turn | checkpoint continuation (#2277) | `src/adapters/cursor/request-builder.ts` reuses returned conversation state | +| Abort after terminal frame logged as `turn-failed` with `expectedClose:false` | #2118 | `live-transport.ts` run loop: `if (this.emittedTerminal && isCursorAbortError(failure)) return;` before `classifyTurnFailure`; both post-terminal and pre-terminal cases in `tests/cursor-cancel-provenance.test.ts` | +| Retry storm on 429 / `RESOURCE_EXHAUSTED` | `transport-retry.ts` excludes them | non-retryable classification | +| Envelope over-replay (cumulative checkpoint+suffix, empty-history skip, contiguous tool results), result deletion, initiating-turn drop | #2865 | assembled-set guard (`CURSOR_EXTERNAL_ROOT_BLOB_LIMIT` = 192 blobs), `cursor_root_envelope_limit` HTTP 400 with measured counts | + +Re-read this cycle: `live-transport.ts` L569-600 (`summarizeFailure` / `classifyTurnFailure`), +L708-742 (drain loop, post-terminal abort return), L1411-1420 (abort listener installs +`failAndClear(new Error("Cursor request was aborted"))`). The post-terminal abort exits the +drain loop before any classification, so no `turn-failed` summary is emitted for a completed +turn. Nothing in the current tree reproduces the misclassification the issue's log showed. + +## What remains and why it cannot be fixed from here + +Two residual observations are not explained by any of the above: + +1. **429 asymmetry** — the adapter path is rate-limited where the direct client is not. +2. **`cache_read_tokens` on the direct client** — Cursor's own path may get prefix-cache + hits on prompts OpenCodex re-sends cold after a restart/compaction/lineage change. + +Both need a matched pair: the same large-context task through OpenCodex (with +`ocx debug provider on` and the `[ocx:cursor:run-request]` `rootBlobs` / `rootBytes` / +`continuationMode` lines) and through the Cursor client on the same account, close in time, +plus Cursor's reported `cache_read_tokens` for the direct run. That trace requires a live +Cursor account under real large-context load. It cannot be produced from the repository. + +Speculative changes (for example pre-emptively re-shaping the replay prefix to chase cache +hits without knowing Cursor's cache key) fail the DEV-NECESSITY-01 gate: no evidence that +they alter the reported outcome, and real risk of regressing the continuation path that +#2277 / #2865 verified. + +## Disposition + +- Post one status comment: what landed since the last maintainer note, the two residuals, + the exact trace that would settle them. +- Apply `needs-info`. Keep the issue open. Recorded as a blocker for criterion c-7. +- Zero source diff. Verification for this cycle is the focused regression that guards the + one #1527 mechanism we did fix: `bun test tests/cursor-cancel-provenance.test.ts`. diff --git a/devlog/_plan/260902_bug_label_drawdown/060_phase6.md b/devlog/_plan/260902_bug_label_drawdown/060_phase6.md new file mode 100644 index 0000000000..3ef7dae028 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/060_phase6.md @@ -0,0 +1,36 @@ +# 060 — Batch F: implementable bug issues + +Five issues describe defects concrete enough to fix. + +- **#3152** dashboard log panel layout jittering (`gui`) — adjacent to #3174's responsive + work. Likely a measured-geometry fix in the same style. +- **#3170** provider input size limit handled gracefully — closes via #3177. **Confirmed by + the A-gate audit (A4):** the body says `Closes #3170` and the diff maps a streaming 413 + to a terminal `context_length_exceeded` instead of the 5/5 reconnect loop. GitHub's + `closingIssuesReferences` is empty only because the PR targets `dev`, so close by hand. +- **#2999** native-main refresh can overwrite external Codex writers (`account-pool`) — + **the plan was wrong to assume #3112 closes it (A4).** The issue describes two races; + #3112 is explicitly only the *lock-scope* half — serializing two `OPENCODEX_HOME`s + against one `CODEX_HOME`. The named publication/overwrite race is still carried by the + existing refuse-not-overwrite check. So #3112 landing does **not** close #2999: the + publication half needs its own fix, or the issue stays open with that scope recorded. +- **#2813** Codex Luna Reserve / gpt-reserve disables routed models after the 5-hour quota + is exhausted (`account-pool`) — needs a real reproduction of the reserve-mode gate. +- **#1527** Cursor adapter large-context turns collapse while direct Cursor stays healthy + (`provider-compatibility`, `streaming`) — the hardest of the five; likely a request-shape + or budget difference between adapter and direct paths. + +## Order + +Verify the two that other PRs close first (#3170, #2999) — those are free if Batch A and C +land. Then #3152, then #2813, then #1527. + +## Method per implemented fix + +Reproduce from the issue, locate the defect with `path:line` evidence, fix the root cause +rather than the symptom, add a focused regression proven red-green, land as its own squash +merge closing the issue. + +## Verification (C) + +Focused suite output with counts, red-green proof, landing SHA ancestry, issue closed. diff --git a/devlog/_plan/260902_bug_label_drawdown/061_p3193.md b/devlog/_plan/260902_bug_label_drawdown/061_p3193.md new file mode 100644 index 0000000000..56147bb171 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/061_p3193.md @@ -0,0 +1,33 @@ +# 061 — p3193: allow `POST /v1/alpha/search` on the loopback listener + +Work-phase `p3193` of the bug-label drawdown. One PR, one PABCD cycle. + +## Source + +- PR #3193 by @alan7629 (draft, head `8aec58c19`, base `dev`), "fix(server): allow alpha search on loopback listener". Fixes #3192 (already closed). +- Checks green (hygiene / enforce-target / CodeRabbit), no reviews. + +## Finding + +The fix is correct and one line: `loopbackRouteAllowed()` in `src/server/index.ts` never admitted `/v1/alpha/search`, so the unauthenticated loopback listener 404'd every native Codex web search on a direct-spawn host. The handler at `src/server/index.ts:1647` does its own `resolveApiAuth`, so admitting it here does not bypass auth: a loopback caller without a ChatGPT credential is refused inside `handleSearch` (`validateForwardAdmissionCredential`). + +The contributor branch cannot land as-is: the editor round-tripped the file through a lossy encoding. Every em-dash became `??`, `⚠️` became mojibake, `→` became `?`, ~30 unrelated comment lines in `src/server/index.ts` plus the test file changed, and the new test carries a duplicated `expect`. + +## Decision + +Reimplement on a clean branch from `origin/dev` (`fcf0da257`), credit the author with `Co-authored-by`, land via admin squash-merge, close #3193 with a pointer to the landed SHA. + +## Diff + +- `src/server/index.ts`: add `if (path === "/v1/alpha/search") return req.method === "POST";` next to `/v1/responses/compact`; extend the allowlist doc-comment with why the relay belongs there and where its auth lives. +- `tests/loopback-listener-integration.test.ts`: move `/v1/alpha/search` out of the denied list (POST) and into the method-mismatch section (GET still 404); add a focused test that POST on loopback is not 404 and the body is the handler's own refusal (message ≠ "opencodex API key required"), while the public listener still answers 401 "opencodex API key required". + +## Checks (focused, no full suite) + +- `bun test tests/loopback-listener-integration.test.ts` → 29 pass / 0 fail. +- `bun run typecheck` clean. `bun run privacy:scan` passed. + +## Landing + +Recorded in `062_p3193_landing.md` once merged. + diff --git a/devlog/_plan/260902_bug_label_drawdown/062_p3193_landing.md b/devlog/_plan/260902_bug_label_drawdown/062_p3193_landing.md new file mode 100644 index 0000000000..9136e0dba5 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/062_p3193_landing.md @@ -0,0 +1,10 @@ +# 062 — p3193 landing + +- Reimplementation PR: #3205 `fix(server): allow POST /v1/alpha/search on the loopback listener`, branch `codex/260902-p3193-loopback-alpha-search`, head `1b21bd652`. +- Admin squash-merge → `53c09a247` on `dev`; ancestry proven with `git merge-base --is-ancestor 53c09a247 origin/dev`. +- #3193 closed with a credit comment pointing at the landed SHA (author co-credited in the commit). #3192 was already closed. +- Audit: reviewer subagent (xai/grok-4.6) failed the first pass on docs-site allowlist drift (en/fr/zh-tw/tr) and the stale "four allowlisted routes" title; both fixed, second pass passed. +- Check receipt: `.codexclaw/evidence/01a05dad-de70-7522-87a0-b82747a6d34c/test-receipt.json` — 29 pass / 0 fail on the loopback file; typecheck and privacy:scan clean. +- Test note: in the test environment the admitted path answers 503 (native-main maintenance gate) rather than the relay's 401; the assertion accepts either and rejects 404, which is what proves the gate opened. +- Trailing CI on `dev` tracked in the regaudit work-phase. + diff --git a/devlog/_plan/260902_bug_label_drawdown/063_i3217.md b/devlog/_plan/260902_bug_label_drawdown/063_i3217.md new file mode 100644 index 0000000000..8eed7993b8 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/063_i3217.md @@ -0,0 +1,64 @@ +# 063 — i3217: Spark flattening of the reserved `functions` namespace → `execexec` + +Work-phase `i3217`. One issue, one cycle. Opened by @alex-jordan547 during the regaudit pass. + +## Symptom + +Codex 0.150.1 + `gpt-5.3-codex-spark` on the native ChatGPT forward route: text-only turns +complete, any `exec` turn loops with `unsupported custom tool call: execexec`. Bypassing the +proxy works. Reproduced locally on 2026-09-02 with ocx 2.40.0 (25 hits in one 60 s run). + +## Root cause (traced, not inferred) + +A tap on a dev proxy built from this tree recorded three things per turn: + +1. Inbound `additional_tools` from Codex: one `namespace` group named `functions` holding + `custom exec`, `function wait`, `function request_user_input`. That is what + `create_tools_json_for_responses_lite` in codex-rs produces for Responses Lite. +2. Outbound body to `chatgpt.com/backend-api/codex`: the group is gone — `additional_tools` + now holds the three tools flat. `stripSparkCompatibility()` + (`src/adapters/openai-responses.ts`) flattens *every* `namespace` group for + `*codex-spark*` models, in both `body.tools` and `additional_tools`. It was written in + July (`7defec111`) when the only namespace groups Codex sent were MCP-style; the reserved + `functions` group arrived with Codex 0.147, after 2.24.2 — which is why the reporter's + "worked on 2.24.2" is true and no proxy release regressed it. +3. Upstream SSE: `custom_tool_call { name: "exec", namespace: "exec" }`. The backend, given a + flat `custom exec` declaration that the model addresses through the `functions` namespace + it was trained on, answers with a namespace equal to the tool name. The proxy relays it + untouched. codex-rs `ToolName::new(namespace, name).with_default_namespace()` treats only + `None | "" | "functions"` as default, so `flat_tool_name` concatenates → `execexec`. + +The parser already knows this shape: `buildTools` flattens `functions` for routed providers on +purpose, and `customToolNamespaces` deliberately skips it. Only the Spark stripper predates it. + +## Fix + +`src/adapters/openai-responses.ts` `stripSparkCompatibility`: + +- Keep a `namespace` group whose name is the reserved `functions` namespace as a group. Still + filter its children (drop `tool_search` etc., strip `defer_loading`) so the Spark + restrictions hold inside it. Drop the group only if nothing survives. +- Keep flattening non-`functions` groups (unchanged behaviour for MCP-style groups). +- `custom` stays in `SPARK_SAFE_TOOL_TYPES`? No — Spark accepts freeform `custom` inside + `functions` (codex-rs sends exactly that and it works direct). The stripper's "drop custom" + rule was written for a backend that rejected top-level custom tools; inside the reserved + group it is what the direct client sends. Allow `custom` inside the `functions` group only. + +Defensive scrub on the client-facing side of the canonical forward route: a `custom_tool_call` +or `function_call` whose `namespace` equals its own `name` is never a legitimate identity +(codex-rs would concatenate it). Delete that `namespace` in the passthrough SSE/JSON rewrite so +a future backend quirk cannot re-open the loop. Applied only when the request did not declare +a namespace group of that name. + +## Tests (focused, red without the fix) + +- `tests/openai-responses-passthrough.test.ts`: Spark passthrough keeps the `functions` group + with its `custom exec` child in `additional_tools`; an MCP-style group is still flattened; + `tool_search` inside `functions` is still dropped. +- Relay test: upstream `custom_tool_call {name:"exec", namespace:"exec"}` reaches the client + without `namespace` on the canonical forward route; a declared MCP namespace is untouched. + +## Landing + +`064_i3217_landing.md`. + diff --git a/devlog/_plan/260902_bug_label_drawdown/064_i3217_landing.md b/devlog/_plan/260902_bug_label_drawdown/064_i3217_landing.md new file mode 100644 index 0000000000..4360ab9061 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/064_i3217_landing.md @@ -0,0 +1,10 @@ +# 064 — i3217 landing + +- PR #3224 `fix(responses): keep the reserved functions group intact for codex-spark (#3217)`, branch `codex/260902-i3217-spark-functions-namespace`, head `21b73c22b`. +- Admin squash-merge → `d23eab43a` on `dev`; `git merge-base --is-ancestor d23eab43a origin/dev` exit 0. +- #3217 closed as completed with the cause, the SHA, and the interim install path. +- Root cause was proven, not inferred: a tap on a dev proxy built from this tree recorded the flattened outbound group and the `namespace:"exec"` answer; the same tap with the fix recorded the group intact and a bare `exec` answer, and the `codex exec` turn ran `pwd` (0 `execexec`, previously 25 per minute). +- Audit: reviewer (xai/grok-4.6) pass; residual "no stream:false case" closed in B before C. +- Checks: focused set 267 pass / 0 fail (receipt), `bun run test:changed` 5794 pass / 0 fail across 301 files, typecheck and privacy:scan clean. Red-without-fix proven for both new tests. +- Trailing CI on `dev` for `d23eab43a` tracked in `regaudit2`. + diff --git a/devlog/_plan/260902_bug_label_drawdown/070_regaudit.md b/devlog/_plan/260902_bug_label_drawdown/070_regaudit.md new file mode 100644 index 0000000000..ee8e8a78a7 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/070_regaudit.md @@ -0,0 +1,70 @@ +# 070 — regaudit: main→dev regression audit, trailing CI, count + +Terminal work-phase of the bug-label drawdown. Runs after every landing (dependsOn i1527, p3193). + +## Scope + +- `origin/main` = v2.39.0 promotion tip (`af6113a03`). `dev` keeps moving while this audit + runs (other maintainers are landing feature PRs), so counts are pinned to a snapshot: at the + first pass `origin/main..origin/dev` was 141 commits / 82 src-touching; by the second pass + (tip `5bc6939d8`) it was 145 / 84 plus 23 tests-only commits. Every commit that entered after + the first snapshot was reviewed in the second pass, so the union covers the whole range. +- `origin/main` is not a fast-forward ancestor of `dev` (15 promotion merge commits are unique + to `main`); the range is still `main..dev` because promotion merges carry no source of their + own. +- Four independent read-only reviewers (xai/grok-4.6): two split the src-touching commits and + hunt behavioral regressions for a default-config user (broken previously-working paths, + credential leaks, Node-only APIs, changed status/error contracts, reintroduced bugs); one + covers the tests-only commits (does any weaken a guarantee?) plus the two newest feature + commits; one runs the MAINTAINERS.md security-boundary pass over workflows, release tooling, + auth-cors, service-secrets, remote, management, and client code, and confirms + `privacy:scan` stays wired in CI. +- Trailing CI on `dev` is judged here, per the user's "CI 후행" policy. Push-triggered runs on + `dev` skip the Windows shards (`platform-windows` is `workflow_dispatch` only) and are + cancelled by the next push, so an exact-head verdict needs a `workflow_dispatch` on the tip: + branch `codex/regaudit-ci-5bc6939d8` = `origin/dev` tip, run 33552542958. +- Every bug-train landing SHA is re-proven an ancestor of `origin/dev`. +- The devlog stack (`codex/260902-bug-pr-closeout-stack`) lands as its own docs PR. +- Final recount against c-7. + +## Landing ancestry (re-proven this cycle) + +#3174 e582aee21, #3176 2e2da87b5, #3177 0d6424f80, #3178 51c49177f, #3179 eceb02d9d, +#3180 634d9e5a0, #3182 865a36ef0, #3183 fecb77a91, #3184 afd5b4630, #3185 fe766e129, +#3186 ea29e25b0, #3187 d33557064, #3188 5ccf7c800, #3189 5557772b7, #3194 c87071400, +#3195 f3bcc67a7, #3196 52d941640, #3197 4be4326d7, #3198 ef6a163c7, #3199 c17bc94c2, +#3200 fcf0da257, #3201 c7f3f6f31, #3202 59449fa83, #3203 55400efd5, #3205 53c09a247 — +all `git merge-base --is-ancestor origin/dev` exit 0. + +## Trailing CI on dev + +Most runs in the train were **cancelled** by the next push (concurrency group), so the signal is +the runs that completed: + +| run | head | result | failing job → test | classification | +|---|---|---|---|---| +| 33543314151 | 52d941640 (#3196) | failure | test 2/4 → `provider-quota` "pool reports tolerate a malformed persisted plan" | **real, already repaired** by #3200 `fcf0da257` (test moved onto the #3198 contract) | +| 33543314151 | 52d941640 | failure | macos → same provider-quota test | same | +| 33546279148 | fcf0da257 (#3200) | failure | macos → `lab-live-pinned-timeouts` "preserves the output byte ceiling as output_byte_limit" received `first_byte_timeout` | **flake**: `firstByteTimeoutMs: 30` in `BASE_LIMITS` races the loopback server on a loaded macOS runner; the test and `src/lib/lab-live-pinned-sender.ts` are unchanged since `d9655f31b`, which is already on `main`. Linux shards 1-4 passed the same file. | +| 33520193493 | 9232df0e6 | failure | test 3/4 → responses-state "shutdown drain cap expiry" | pre-train, timing flake (not in this campaign's diff) | +| 33514747317 / 33477777613 | 408652698 / 58be3c5bb | failure | macos → port-selection / websocket pool auth | pre-train macOS timing flakes, same family the memory notes as known | +| 33549107560 | 0d73d6557 (#2986, not ours) | failure | macos → `codex-prompt-route` "36. comment-after-bracket fallback project document with a bare key" expected in-flight probe refusal, received a completed probe | **flake**: the case races a 200 ms probe against the second GET; the quoted-key sibling passed in the same run; the file is untouched since `main` (`aa16a71e0`); no other run in this train hit it. | +| 33548615686 / 33550885829 | 6a6efa928 / 4a382beed (not ours) | cancelled | — | superseded by the next push | +| 33551966282 | 5bc6939d8 (#3209, not ours) | push-triggered | — | tracked; Windows skipped | +| 33552542958 | 5bc6939d8 | **workflow_dispatch, exact head, Windows shards on** | — | the promotion-grade verdict for this audit; recorded in 071 | + +Last fully green dev run before the train: `22a643a00` (2026-09-01T16:41Z). Verdict on the train +so far: one genuine CI regression (#3196's test contract drift) which was caught and repaired +inside the train by #3200; no other failing job points at a commit from this campaign. + +## Reviewer verdicts + +Full text in `071_regaudit_landing.md`. Summary: all four passes returned `VERDICT: pass` with +no high-confidence default-path regression. Medium suspects are design decisions on opt-in or +non-default paths (unreadable `config.json` now fails closed in `ocx start`; streaming provider +413 becomes a terminal SSE overflow; launchd Claude mode ignores dotenv-only Anthropic env; +compaction without a canonical OpenAI route forwards to the default provider). Two security +residuals are recorded for follow-up, neither a new grant: live `service-api-token` reads skip +the owner-only mode check that `.prev` enforces; hub `managementPublicOrigin` replaces the +observed scheme so pairing cannot see TLS-stripped HTTP on the public listener (the official +client already refuses plaintext). diff --git a/devlog/_plan/260902_bug_label_drawdown/071_regaudit_landing.md b/devlog/_plan/260902_bug_label_drawdown/071_regaudit_landing.md new file mode 100644 index 0000000000..50f253f3b4 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/071_regaudit_landing.md @@ -0,0 +1,120 @@ +# 071 — regaudit landing + +## Reviewer verdicts (verbatim tails) + +### Pass 1 — src-touching commits, first half (Faraday, xai/grok-4.6) + +VERDICT: pass +c7f3f6f31 compaction can send a bare native id to a non-OpenAI default (medium) +0d6424f80 streaming 413 becomes terminal SSE overflow, not HTTP 413 (medium) +865a36ef0 dotenv-only Claude creds can be classified proxy (medium) +efefe3671 standalone still shows dead key-rotation UI (medium) +2e2da87b5 quota-word 5xx can cool/rotate a pool account (low) +51c49177f Hermes export model list shape changed (low) +f3bcc67a7 citation PUA stripped on translated streams (low) +7386b5201 combo default can raise to lowest supported rung (low) + +Checked and not counted: outbound body ceiling default-off (52d941640); loopback +/v1/alpha/search still authenticates inside the handler (53c09a247); pairing "allowed" in +pairing only matches refusals (88d9889bb); logout goes through patched window.fetch so CSRF is +attached (9bded9c41); native-main refresh claim/identity checks fail closed (fecb77a91, +c17bc94c2). + +### Pass 2 — src-touching commits, second half (Kuhn) + +VERDICT: pass +- 863a88ea3 src/client/state.ts:43 — unreadable config.json is invalid, so ocx start/ensure/claude + exit 1 instead of the old default fallback. Medium. +- b14b741dc src/service.ts:2127 — unscoped Windows session-recovery triggers fail closed and skip + auto-repair. Medium, Windows-only. +- bf221bc26, d25cbc02a, 10a31986a, 4fdd54d46, b81c43551 — low, hub/relay/MiniMax-only. + +### Pass 3 — tests-only commits + two newest feature commits (Epicurus) + +VERDICT: pass +4a382beed keeps Design B unless codexDesktopAuthless === true on loopback. 0d73d6557's +/v1/images relay returns immediately unless images.bridgeEnabled === true and an xAI provider +exists. No Node-only APIs. Tests-only commits add coverage or retarget assertions to +#3198/#3108/remote-protocol contracts; none skip, mock away a live path, or drop a security check. + +### Pass 4 — MAINTAINERS security boundary (Socrates) + +VERDICT: pass +41 commits in-scope. 6f415bae is workflow_call only — no PAT, no release-job write grant, pinned +actions. Pairing/session/rotation stay grant- or management-authenticated; public +/opencodex-session is hub-only, origin-bound, rate-limited. Authless Desktop is loopback-only. +CI still runs bun run privacy:scan. +Residual (non-blocking): 863a88ea3 src/lib/service-secrets.ts:40 live service-api-token reads skip +the owner-only mode check .prev enforces; abf0f81bd src/server/auth-cors.ts:134 hub +managementPublicOrigin replaces the observed scheme. + +## Follow-ups filed from the residuals + +Recorded here as candidates; none blocks promotion and none carries the bug label: + +1. service-secrets: apply the owner-only mode check to the live token read, not only `.prev`. +2. auth-cors: let pairing observe the raw scheme when `managementPublicOrigin` rewrites it. +3. client/state: consider a warning-plus-default path for an unreadable `config.json` on + standalone hosts instead of exit 1. + +## Exact-head CI (workflow_dispatch on the dev tip) + +Run 33552542958 on `5bc6939d8` (branch `codex/regaudit-ci-5bc6939d8` = `origin/dev`), +Windows shards enabled. Result: **every non-Windows job green on the exact head** — test 1/4 +through 4/4 (Linux), macos, gates, storage policy, api usage, keyring ubuntu/macos/windows, +npm-global ubuntu/macos/windows. That settles the two macOS failures seen during the train +(`lab-live-pinned-timeouts` first-byte race, `codex-prompt-route` probe race) as flakes: the +same tip passed the whole macOS suite. + +The four Windows shards failed (1/4, 2/4, 4/4 failure; 3/4 cancelled by the composed gate). +The failure signatures are environmental, not assertion failures in campaign code: + +- shard 2/4: `ACL hardening failed (EICACLS) — icacls command error` thrown from + `hardenSecretDir(..., { required: true })` inside `saveConfig` (`src/config.ts:2674`) and + `ETIMEDOUT — transient icacls stall` 16×. Every test that calls `saveConfig` on that runner + fails identically. The ACL module (`src/lib/windows-secret-acl.ts`) and `atomic-write.ts` + are unchanged since `main` (only `e5d588669`, already on `main`, touches them). +- shard 1/4 and 4/4: `EPERM: operation not permitted, rm 'tests\.tmp-codex-accounts-test'` + (49×) and `rm 'tests\.tmp-codex-auth-api-test'` (287×) — Windows file-handle contention on + the test temp dirs during `rmSync`, cascading into every case in those files. Plus one + "Bun runtime crash" retry. + +History: the last Windows-green dispatch was `33290817128` on `223a0a287` (on `main`); the +dispatch on the same SHA from `dev` (`33291970929`) failed Windows 3/4, and the intervening +Windows dispatches on feature branches (`33292931792`, `33290258063`, `33289201339`, +`33288039685`) all failed. Windows shards have therefore not been a stable signal for any +branch since 2026-08-30, before this campaign's first landing. + +Control: the same workflow dispatched on `origin/main` (`af6113a03` = released v2.39.0, +branch `codex/regaudit-ci-main-af6113a03`, run 33555110133). **Windows shards fail on `main` +with the identical signatures**: shard 3/4 `EPERM: operation not permitted, rm +'tests\.tmp-codex-accounts-test'` 49× plus `.tmp-oauth-status-privacy-test` 7×, icacls +`ETIMEDOUT`, "Bun runtime crash"; shard 4/4 icacls `ETIMEDOUT` 13× and the same +`Responses state admission boundary` / `previous_response_id` cases. The released tip and the +audited `dev` tip fail the same way on `windows-latest`, so the Windows result is a runner +environment defect (NTFS ACL/icacls stalls and temp-dir handle contention on the hosted image) +that predates this campaign. It is not evidence of a regression in `main..dev`. + +Verdict for the range: no regression found by four independent reviewers; exact-head CI green +on Linux ×4, macOS, gates, storage, api-usage, keyring ×3, npm-global ×3; Windows blocked by +the runner environment on both ends of the range. Follow-up candidate (no bug label, not this +campaign): make `tests/codex-account-store.test.ts` / `codex-auth-api` temp-dir teardown +retry `EPERM` on Windows, and re-enable the self-hosted `ocx-home` runner +(`OCX_SELF_HOSTED_WINDOWS`) for a trustworthy Windows signal. + +## Devlog stack landing + +Branch `codex/260902-bug-pr-closeout-stack` (devlog-only) → PR #3218, opened during this +cycle; merged in the final recount phase so it carries the i3217 record too. + +## Bug-label count at the end of this pass + +`gh issue list -l bug --state open` = 6, `gh pr list -l bug --state open` = 0. Five are the +recorded blockers (#3152 needs-repro, #3141 needs-info, #2999 CAS-primitive, #1527 needs-info, +#1419 needs-info). The sixth, **#3217**, was opened at 2026-09-01T20:39Z while this audit ran: +Responses Lite `exec` returned with `namespace: "exec"` on the native forward route, so Codex +loops on `execexec`. Reproduced locally (ocx 2.40.0, codex 0.150.1) and traced with a tap on a +dev proxy: `stripSparkCompatibility` flattens the reserved `functions` namespace group in +`additional_tools`; the ChatGPT backend then answers the flat `custom exec` with +`namespace: "exec"`, which the proxy relays verbatim. It is implementable and becomes its own +work-phase (`i3217`); c-7 is evaluated again in the final recount phase after it lands. diff --git a/devlog/_plan/260902_bug_label_drawdown/072_regaudit2.md b/devlog/_plan/260902_bug_label_drawdown/072_regaudit2.md new file mode 100644 index 0000000000..7ed058f38d --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/072_regaudit2.md @@ -0,0 +1,49 @@ +# 072 — regaudit2: final recount, exact-head CI on `d23eab43a`, devlog landing + +Terminal phase after `i3217`. + +## Recount (2026-09-02, after #3224 and #3223 disposition) + +`gh issue list -l bug --state open` → 5. `gh pr list -l bug --state open` → 0. Combined **5**. + +| item | disposition | blocker recorded in | +|---|---|---| +| #3152 dashboard log panel jitter | NEEDS_REPRO — reporter environment detail (row count, viewport) | 052_i3152.md | +| #3141 aggressive responses-state writes | NEEDS_HUMAN — reporter measurement; numbers do not reconcile | 051_i3141.md | +| #2999 native-main refresh publication race | DONE for the publication guard (#3183 `fecb77a91`, #3199 `c17bc94c2`); last window needs `renameat2(RENAME_EXCHANGE)`, which Bun does not expose | 057_i2999.md | +| #1527 Cursor large-context collapse | NEEDS_INFO — matched direct-vs-adapter trace for the 429 asymmetry and prefix-cache residuals | 059_i1527.md | +| #1419 bundled Bun SIGTRAP | NEEDS_HUMAN — reporter crash artifact | 056_i1419.md | + +Each has a written comment on the issue naming the evidence and the exact artifact that would +unblock it, plus the `needs-info` label where the reporter owns the next step. That meets the +objective's fallback ("5 acceptable if the last few are genuinely blocked"), and the four +external-dependency items are honest blockers rather than deferrals: two need a reporter +artifact, one needs a reporter measurement, one needs a runtime primitive. + +#3223 (contributor PR for #3217) was closed as superseded by #3224 with a comment crediting the +independent diagnosis and inviting the tighter catalog-scoped scrub as a follow-up. + +## Exact-head CI on the final dev tip + +`d23eab43a` = `origin/dev` after #3224. `workflow_dispatch` on branch +`codex/regaudit-ci-d23eab43a`, run 33562938994, Windows shards on. Result: every non-Windows +job green (test 1/4–4/4, macos, gates, storage policy, api usage, keyring ×3, npm-global ×3). +Windows 1/4, 2/4, 4/4 failed and 3/4 was cancelled by the gate, with the same signatures as the +`main` control in 071 (`EPERM rm tests/.tmp-codex-accounts-test` ×49, `.tmp-codex-auth-api-test` +×377, icacls `ETIMEDOUT`, "Bun runtime crash"). Nothing in `d23eab43a` touches those paths; +the Windows result stays classified as a hosted-runner environment defect present on both ends +of the range. + +## Devlog landing + +PR #3218 (this stack, rebased on `d23eab43a`) → merged in the closeout phase. + +## Arrivals after the recount + +Between the recount above and the close of this phase, four contributor PRs carrying the bug +label opened against `dev` (2026-09-01T22:49Z – 23:33Z): #3226 (scope the #3217 scrub, the +follow-up invited on #3223), #3227 (combo preflight: zero-output transport incompletes should +fail over), #3228 (encrypted V2 spawn native fallback without a configured chain; touches GUI), +#3229 (allow the `codexless_agent` originator in V2 task recovery). Combined count moved to +5 + 4 = 9. Each is registered as its own work-phase (`p3226`…`p3229`) and the final recount +moves to `regaudit3` after they land or are dispositioned. diff --git a/devlog/_plan/260902_bug_label_drawdown/080_p3226.md b/devlog/_plan/260902_bug_label_drawdown/080_p3226.md new file mode 100644 index 0000000000..db27612aaf --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/080_p3226.md @@ -0,0 +1,42 @@ +# 080 — p3226: scope the #3217 self-named namespace scrub + +Work-phase `p3226`. Contributor PR #3226 by @alex-jordan547 (head `c7f730b23`, base `dev`, +MERGEABLE, review-ready), the follow-up invited when #3223 was closed. + +## What it changes + +The scrub landed in #3224 deleted any `namespace` equal to the call's own `name` without +consulting the catalog. That is correct for the Spark quirk but wrong for one legitimate shape: +a namespace group named `exec` that declares a tool named `exec`. codex-rs routes that by +`ToolName { namespace: "exec", name: "exec" }`, so stripping the namespace would misroute it. +#3226 builds an authorization set from the turn's `tools`, `additional_tools`, and +`tool_search_output` (bare custom / bare function names, minus names that also appear as a +same-name namespaced tool), threads it through `buildToolBridgeMaps`, and scrubs only names in +that set, per call type. + +## Review plan + +- Reviewer (xai/grok-4.6): authorization-set construction, absent-catalog behaviour (the scrub + must still fire when `additional_tools` carries the declaration, which is the #3217 shape), + tool_choice gating, budget charging symmetry, no behaviour change for non-forward routes. +- Focused tests on the PR head in a scratch worktree: scrub, undeclared-tool guard, passthrough. +- Land via admin squash-merge; prove ancestry; record in `081_p3226_landing.md`. + +## Audit finding (Erdos, xai/grok-4.6) — fail on the PR as-is + +Focused tests on head `c7f730b23` are green (201 pass, typecheck, privacy), the authorization +set is request-scoped on both SSE and bounded-JSON paths, the #3217 shape still scrubs, and the +genuine same-name namespaced tool now survives (correct against codex-rs `ToolName` routing). +One hole: `collectBareToolSpecs` only reads `spec.name`, so a Chat-shaped declaration +`{ type: "function", function: { name } }` — which `buildTools` accepts (`parser.ts:215`) and +therefore lands in `bareFunctionToolNames` — is never recorded on the raw-body side. The +intersection drops it and a self-named echo for that function would reach Codex again. + +## Revised landing: carry with the fix + +Cherry-pick the PR's commits onto `codex/260902-p3226-carry` from `origin/dev` (author +credit preserved), then one maintainer commit: teach `collectBareToolSpecs` the nested +`function.name` shape (mirroring `addWireToolName` in the undeclared-tool guard), and add a +red-without-fix case to `tests/responses-self-named-namespace-scrub.test.ts` where the catalog +is Chat-shaped and the upstream echoes `namespace === name` on a `function_call`. Admin +squash-merge the carry, close #3226 as landed-via-maintainer naming the SHA. diff --git a/devlog/_plan/260902_bug_label_drawdown/081_p3226_landing.md b/devlog/_plan/260902_bug_label_drawdown/081_p3226_landing.md new file mode 100644 index 0000000000..cfe4d37593 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/081_p3226_landing.md @@ -0,0 +1,11 @@ +# 081 — p3226 landing + +- Carry PR #3234 (branch `codex/260902-p3226-carry`): the four #3226 commits cherry-picked with + author credit + maintainer commit `1092d4f68` (Chat-shaped `function.name` in + `collectBareToolSpecs`, regression red without it). +- Admin squash-merge → `b732b0d0f` on `dev`; ancestry proven. #3226 closed as landed via + maintainer with the SHA and the addition explained. +- Audit trail: Erdos failed the PR as-is (nested-name hole); plan revised; Chandrasekhar verified + the hole and passed the carry plan. +- Checks: focused set 202 pass / 0 fail (receipt), typecheck, privacy:scan. + diff --git a/devlog/_plan/260902_bug_label_drawdown/082_p3227.md b/devlog/_plan/260902_bug_label_drawdown/082_p3227.md new file mode 100644 index 0000000000..a2edf7e291 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/082_p3227.md @@ -0,0 +1,23 @@ +# 082 — p3227: fail over zero-output incomplete combo streams + +Work-phase `p3227`. Contributor PR #3227 by @RHODIZSECURITY (head `43d23383d`, base `dev`, +draft, MERGEABLE). + +## What it changes + +`preflightComboStreamResponse` only treated `response.failed` as a retryable zero-output +terminal. An upstream answering HTTP 200 with a stream that ends as `response.incomplete` for +a transport reason (`adapter_eof`, `missing_terminal_event`, `upstream_stall_timeout`) was +accepted, so a combo with healthy backups never advanced. The PR adds those three reasons to a +retryable set; semantic incompletes (`max_output_tokens`, `content_filter`) and any incomplete +after output has committed remain accepted. Tests: three unit cases + one server-level e2e +where A ends with `adapter_eof` before output and B wins, with receipts asserting 502 → 200. + +## Review plan + +- Reviewer: confirm the three reasons are the ones the proxy itself mints for transport faults + (not upstream semantics), the output-commit boundary is untouched, and the failed attempt is + accounted as a 502 in log/usage receipts. +- Focused tests on the PR head; typecheck; privacy. +- Admin squash-merge; ancestry; `083_p3227_landing.md`. + diff --git a/devlog/_plan/260902_bug_label_drawdown/083_p3227_landing.md b/devlog/_plan/260902_bug_label_drawdown/083_p3227_landing.md new file mode 100644 index 0000000000..7e029deeaf --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/083_p3227_landing.md @@ -0,0 +1,7 @@ +# 083 — p3227 landing + +- Carry PR #3236 (branch `codex/260902-p3227-carry`): the #3227 commit rebased onto `dev` with author credit. +- Admin squash-merge → `1c8278b4d` on `dev`; ancestry proven. #3227 closed as landed via maintainer. +- Reviewer (xai/grok-4.6) pass: the three reasons are proxy-minted transport faults; commit boundary untouched; 502 attempt accounted. +- Checks: 88 pass / 0 fail on the PR head, after rebase, and on the landed `dev` tip (receipt); typecheck, privacy:scan. + diff --git a/devlog/_plan/260902_bug_label_drawdown/084_p3228.md b/devlog/_plan/260902_bug_label_drawdown/084_p3228.md new file mode 100644 index 0000000000..b547cc089c --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/084_p3228.md @@ -0,0 +1,34 @@ +# 084 — p3228: encrypted V2 spawn native fallback without a configured chain + +Work-phase `p3228`. Contributor PR #3228 by @x3M3x (head `06fe048bd`, draft, base `dev`; +`enforce-target` fails because the description mentions `gui` with no screenshot). + +## What is in the PR + +Two unrelated things: + +1. **The bug fix** (`src/codex/subagent-model-fallback.ts`, 5 lines + 1 test): an encrypted V2 + worker payload needs the native ChatGPT backend, but `applySubagentModelFallback` only + consulted a fallback chain the operator configured. With no chain, a routed sub-agent model + reached the encrypted-task guard and failed with `unreadable_encrypted_agent_task`. The fix + uses `normalizedChain(modelId, config, [], DEFAULT_SUBAGENT_MODELS)` when + `nativeFallbackOnly` and no chain is configured; ordinary routed spawns are unchanged. +2. **A GUI feature** (`gui/src/pages/Subagents.tsx`, `SubagentDelegationSection.tsx`, nine + i18n files, ~130 lines): a fallback-chain editor wired to the existing + `/api/subagent-model-fallback` routes. No screenshot, no issue, not a bug. + +## Disposition + +Land 1 via a carry branch from `origin/dev` (`Co-authored-by` credit, since a partial +cherry-pick is not a git operation). Leave 2 to a separate feature PR with a screenshot, which +the closing comment invites. #3228 closes as landed-partially. + +## Review plan + +- Reviewer: does `DEFAULT_SUBAGENT_MODELS` respect the operator's roster (disabled natives, + `codexAccountNamespaces`)? Is the `nativeFallbackOnly` filter in + `selectAvailableSubagentModel` still the thing that keeps non-forward candidates out? Is the + unit test red without the change? +- `bun test tests/subagent-model-fallback.test.ts`; typecheck; privacy. +- Admin squash-merge; ancestry; `085_p3228_landing.md`. + diff --git a/devlog/_plan/260902_bug_label_drawdown/085_p3228_landing.md b/devlog/_plan/260902_bug_label_drawdown/085_p3228_landing.md new file mode 100644 index 0000000000..c5adaec678 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/085_p3228_landing.md @@ -0,0 +1,8 @@ +# 085 — p3228 landing + +- Carry PR #3239 (branch `codex/260902-p3228-carry`): the source hunks of #3228 with `Co-authored-by` credit; the bundled GUI editor left for a feature PR with a screenshot. +- Admin squash-merge → `744d12d02` on `dev`; ancestry proven. #3228 closed as landed (source half) with the split explained. +- Reviewer Cicero (xai/grok-4.6) pass; local red-green on the carry worktree (test fails without the src hunk, 60 pass with). +- Checks: subagent-model-fallback focused file, typecheck, privacy:scan. +- Non-blocking caveat recorded: entitlement filtering still uses the null initial chain (`core.ts:2945`); the first synthetic candidate `gpt-5.5` is ungated. + diff --git a/devlog/_plan/260902_bug_label_drawdown/086_p3229.md b/devlog/_plan/260902_bug_label_drawdown/086_p3229.md new file mode 100644 index 0000000000..66c9443240 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/086_p3229.md @@ -0,0 +1,23 @@ +# 086 — p3229: admit the Codexless originator in V2 task recovery + +Work-phase `p3229`. Contributor PR #3229 by @iamnomankazi (head `6fb0bbad9`, draft, +24/-0). + +## What it changes + +`CODEX_ORIGINATORS` in `src/server/responses/agent-task-recovery.ts` gains +`codexless_agent`. Without it, an encrypted V2 sub-agent task spawned through Codexless is +refused at recovery admission and fails as `unreadable_encrypted_agent_task`. One security test +asserts the recovery request forwards `originator=codexless_agent`. + +## Why this needs a security look + +The set gates which client originators may enter the recovery path, which then forwards a +credential to `chatgpt.com`. Adding a name must not weaken the checks that follow it (OAuth +issuer, client id, token shape). Review confirms the later checks are untouched and that the +string is the one Codexless actually sends. + +## Landing + +Carry onto `origin/dev` with author credit; focused test; admin squash-merge; +`087_p3229_landing.md`. + diff --git a/devlog/_plan/260902_bug_label_drawdown/087_p3229_landing.md b/devlog/_plan/260902_bug_label_drawdown/087_p3229_landing.md new file mode 100644 index 0000000000..114aa6e73b --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/087_p3229_landing.md @@ -0,0 +1,18 @@ +# 087 — p3229 landing (and the #3239 regression it exposed) + +- Carry PR #3241 (branch `codex/260902-p3229-carry`): the #3229 two-file diff re-applied on the + current tip with `Co-authored-by`. Admin squash-merge → `b54508c8c` on `dev`; ancestry + proven. #3229 closed as landed via maintainer. +- Reviewer (xai/grok-4.6) pass: `CODEX_ORIGINATORS` is admission-only; issuer/client/token/ + account/proxy-secret gates after it unchanged; `codexless_agent` is Codexless's real + `clientInfo.name`. Red-without-fix proven on `1c8278b4d`. +- **Regression caught by this cycle's check**: on `744d12d02` (#3239, the p3228 carry) + `tests/agent-task-recovery-security.test.ts` was 2/13 (13/13 at `1c8278b4d`). The synthesized + `DEFAULT_SUBAGENT_MODELS` chain fired in the first fallback pass, rerouting an unreadable + encrypted spawn to native before `recoverEncryptedAgentTask` ran, so recovery's security + gates never executed. Repaired as work-phase `r3239`: PR #3240 → `7f00d0eee` gates the + synthesized chain on `config.agentTaskRecovery?.enabled !== true`; unit regression red + without the guard; security file 14/14 again. This is exactly the "CI behind the work, repair + as its own cycle" contract — the focused check on the next PR caught it before CI did. +- Checks on the landed tip: recovery security 14 pass / 0 fail; typecheck; privacy. + diff --git a/devlog/_plan/260902_bug_label_drawdown/088_r3239.md b/devlog/_plan/260902_bug_label_drawdown/088_r3239.md new file mode 100644 index 0000000000..b5de222a8d --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/088_r3239.md @@ -0,0 +1,27 @@ +# 088 — r3239: repair the #3239 regression (recovery gates bypassed) + +Work-phase `r3239`. Found by the p3229 focused check, not by CI (dev CI runs were being cancelled +by the merge train). + +## Regression + +`744d12d02` (#3239) synthesized a `DEFAULT_SUBAGENT_MODELS` chain for an unreadable encrypted +spawn when no chain is configured. In `core.ts` the first `applySubagentModelFallback` pass runs +before `recoverEncryptedAgentTask`; with the synthesized chain that pass rerouted the spawn to +native `gpt-5.5`, the route became canonical-forward, recovery was skipped, and recovery's +caller-auth / proxy-secret / token-validity gates never executed. +`tests/agent-task-recovery-security.test.ts`: 13/13 at `1c8278b4d` → 2/13 at `744d12d02`. + +## Fix + +Gate the synthesized chain on `config.agentTaskRecovery?.enabled !== true`. An operator who +enabled recovery chose to decrypt and stay routed; a configured chain keeps its precedence; the +#3239 case (recovery off, no chain) is unchanged. + +## Landing + +PR #3240 → `7f00d0eee` on `dev`. Unit regression red without the guard; security file 14/14. + +Audit (xai/grok-4.6): pass — recovery-on + no chain does not also want the native rescue; a +failed recovery still hits the fail-closed 400, and the post-recovery second pass applies only +a configured chain. diff --git a/devlog/_plan/260902_bug_label_drawdown/089_p3232.md b/devlog/_plan/260902_bug_label_drawdown/089_p3232.md new file mode 100644 index 0000000000..5858b42398 --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/089_p3232.md @@ -0,0 +1,10 @@ +# 089 — p3232: sibling start must not persist its port (merged by maintainer) + +Work-phase `p3232`. PR #3232 (@lidge-jun, bug label) opened at 2026-09-02T01:17Z while this loop +was on p3226 and was merged directly by the maintainer as `261b7e012` (ancestor of +`origin/dev`, verified). No carry or review work was needed from the loop; this phase records +the landing and re-runs the PR's test files on the current tip so the count and the CI verdict +stay honest: `tests/cli-dispatch.test.ts tests/ports.test.ts`. + +Result on the current tip: 46 pass / 0 fail. Audit (xai/grok-4.6): pass — record-only is the +correct disposition. diff --git a/devlog/_plan/260902_bug_label_drawdown/090_regaudit3.md b/devlog/_plan/260902_bug_label_drawdown/090_regaudit3.md new file mode 100644 index 0000000000..2d1dfe40dc --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/090_regaudit3.md @@ -0,0 +1,78 @@ +# 090 — regaudit3: final recount and closeout + +Terminal phase after `p3226`–`p3232` and `r3239`. + +## Recount (2026-09-02, final) + +`gh issue list -l bug --state open` → **4** (#3152, #3141, #2999, #1527), +`gh pr list -l bug --state open` → 0. Combined **4**. + +An earlier pass of this doc read 5: #1419 (bundled Bun SIGTRAP) was closed by the maintainer at +2026-09-02T02:43Z as completed — the bundled Bun moved to 1.4.0, with a reopen invitation if it +recurs. That closure was not made by this loop and not made to lower the count; it is the +platform fix the blocker was waiting for. The four remaining are the recorded blockers from 072, +each with a maintainer comment naming the evidence it needs and `needs-info` where the +reporter owns the next step (#2999 is the runtime-primitive blocker). + +`origin/dev` has since moved past `2cb592174` with feature/docs landings by the maintainer +(#3222, #3230, #3231, #3225); none carries the bug label and none is in this campaign's scope. +The CI verdict below is pinned to `2cb592174`, the last commit this campaign put on `dev`. + +## Landings since regaudit2 (all ancestors of `origin/dev`) + +| item | landing | note | +|---|---|---| +| #3226 → #3234 | `b732b0d0f` | carry + nested `function.name` fix | +| #3227 → #3236 | `1c8278b4d` | carry, author credit | +| #3228 → #3239 | `744d12d02` | **reverted** by #3242 `2cb592174` — see below | +| regression from #3239 → #3240 | `7f00d0eee` | **reverted** by #3242 together with #3239 | +| #3229 → #3241 | `b54508c8c` | carry on the repaired tip | +| #3232 | `261b7e012` | merged by the maintainer directly; verified | + +## Trailing CI + +The `push` runs on `dev` during this train were all cancelled by the next push. The r3239 +regression was caught by the next cycle's focused check, not by CI, and repaired before anything +else landed — which is the point of pairing "CI behind the work" with a focused red-green gate +on every PR. Exact-head `workflow_dispatch` on `b54508c8c` (branch +`codex/regaudit-ci-b54508c8c`, run 33581824312, Windows on): **test 3/4 failed** on +`tests/agent-task-recovery.test.ts` "keeps the disabled fail-fast response byte-identical to +the absent feature" (400 expected, 502 received). Bisect: 19/19 at `1c8278b4d`, 7/19 at +`744d12d02` (#3239), 18/19 at `7f00d0eee` (#3240). The contract that file pins — recovery +absent/disabled ⇒ fail-fast 400 with zero upstream fetches — is a credential-spend boundary: +synthesizing a native chain reroutes a routed spawn to the ChatGPT backend without the operator +opting in. #3240 could not restore that without removing the feature, so **both were reverted** +in #3242 → `2cb592174` (92 pass / 0 fail across the three recovery/fallback files after the +revert). #3228's disposition is corrected on the PR: the reported behaviour is the documented +opt-in, not a bug; a defaults change is a product decision for a feature request. The p3228 +review ran the fallback and security files but not `agent-task-recovery.test.ts` — recorded +as the miss. + +Second dispatch on the reverted tip `2cb592174` (run 33582128589, Windows on): Linux test +1/4–4/4 all green (the `agent-task-recovery` failure is gone), gates, storage, api-usage, +keyring ×3, npm-global ×3 green. Windows 1/2/4 failed with the known runner signatures +(`EPERM rm tests/.tmp-codex-accounts-test` ×49, icacls `ETIMEDOUT`, "Bun runtime crash"); +3/4 cancelled by the gate. macOS failed one case: `native-profile-manager` "preserves exact +auth bytes, encrypts inactive profiles…" at 12.7 s — a file untouched since `#3054` +(2026-08-29, on `main`), which passed in both earlier dispatches (33562938994, 33581824312) +and 49/49 three times locally on this tip; its history is two macOS-timing bounding commits +(`bef2869c7`, `c1be34da4`). Classified as a macOS timing flake; a third dispatch +(run 33584155821) is recorded below to settle it. + +Third dispatch on `2cb592174` (run 33584155821): **macOS green**, Linux 1/4–4/4 green, gates, +storage, api-usage, keyring ×3, npm-global ×3 green. The `native-profile-manager` case is +settled as a macOS timing flake (fail 1 of 3 dispatches on an unchanged file). Windows shards +remain the hosted-runner defect proven on `main` in 071. Verdict for the campaign's last +source commit: green on every platform this repository can currently trust. + +## Devlog landing + +PR #3218 (this stack, rebased on the current `dev` tip) → admin squash-merge; SHA recorded in +the ledger and the goalplan criterion evidence. + +## Criterion c-7 + +Met: **4** open bug-labelled items (≤5 fallback; 3 would have required closing a blocker without +its evidence), all four with recorded, evidence-backed blockers. From 24 at the start of the +campaign: 14 PRs + 10 issues → 0 PRs + 4 issues. 22 landings / closures with SHAs or evidence +comments, one honest revert. diff --git a/devlog/_plan/260902_bug_label_drawdown/091_rv3239.md b/devlog/_plan/260902_bug_label_drawdown/091_rv3239.md new file mode 100644 index 0000000000..a743f9ef9d --- /dev/null +++ b/devlog/_plan/260902_bug_label_drawdown/091_rv3239.md @@ -0,0 +1,30 @@ +# 091 — rv3239: revert the synthesized native chain (#3239, #3240) + +Work-phase `rv3239`. Triggered by the regaudit3 exact-head dispatch (run 33581824312, tip +`b54508c8c`): `test 3/4` failed `tests/agent-task-recovery.test.ts` "keeps the disabled +fail-fast response byte-identical to the absent feature" (400 expected, 502 received). + +## Why revert rather than patch again + +That test pins a credential-spend boundary: with `agentTaskRecovery` absent or disabled, an +encrypted spawn on a routed model must fail fast with a 400 and make zero upstream fetches. +#3239's synthesized chain reroutes that spawn to the native ChatGPT backend — a stored +credential spent on a model the operator never opted into. #3240 fixed the recovery-*on* path +but the recovery-*off* contract cannot hold while the feature exists. The reported behaviour in +#3228 is the documented opt-in (configure a `subagentModelFallback` chain or enable recovery); +changing that default is a product decision, not a bug fix. + +## Landing + +PR #3242 → `2cb592174` on `dev` (pure revert of `7f00d0eee` and `744d12d02`). After the +revert: agent-task-recovery 19/19, agent-task-recovery-security 14/14, subagent-model-fallback +59/59 (92 pass / 0 fail). #3228 corrected on the PR with an apology and the opt-in explained. + +## What the loop got wrong + +The p3228 review ran `subagent-model-fallback` and (via p3229) the security file, but not +`agent-task-recovery.test.ts`, which is the file that owns the fail-fast contract. "Focused +test" has to mean every file that pins the touched behaviour, not only the file the PR edited. + +Audit (xai/grok-4.6): pass — pure revert confirmed byte-identical to `744d12d02^`; revert is the +right call over a third patch. diff --git a/devlog/_plan/260902_bug_pr_closeout_stack/000_plan.md b/devlog/_plan/260902_bug_pr_closeout_stack/000_plan.md new file mode 100644 index 0000000000..f53fb643f1 --- /dev/null +++ b/devlog/_plan/260902_bug_pr_closeout_stack/000_plan.md @@ -0,0 +1,60 @@ +# 000 — bug_pr_closeout_stack: Plan + +## Objective + +Close as many open opencodex bugs and pull requests as can be closed with evidence, +in one session, without running the repository-wide local suite. Two mechanisms: + +1. **Merge train** for pull requests that already carry a maintainer-reviewed body + and a green or known-flake-only exact-head CI run. +2. **Stacked implementation** for issues whose defect is fully visible in the tree, + each landing as its own squash merge into `dev`. + +Evidence base collected 2026-09-02 in this worktree: + +- 47 open PRs, 55 open issues (`gh pr list`, `gh issue list`). +- `gh pr checks 3163` — 23/23 pass on head `486b2f99f3182acf055274755ade9c6571203ac9`. +- `gh pr checks 3166` — head `17f01162ad404f1bcee7d7f00998fc0e143365e5`; `test 3/4` red on + `tests/responses-state.test.ts > late async spill completion cannot overwrite the shutdown + fallback` with `ETIMEDOUT` out of `src/responses/spill-store.ts:232` (ACL budget exhausted + under runner load). The PR touches `src/codex/auth-context.ts` only — the failure is a + timing flake in an unrelated subsystem. +- `gh pr checks 2083` — 24/24 pass, `APPROVED`, `CLEAN`; #2986 is its maintainer carry on + current `dev` with an independent security review recorded in the PR body. + +## Loop-spec + +- Loop archetype: verifier-defined (each item has a binary landing proof). +- Write scope: `src/cli/models.ts`, `src/combos/request.ts`, `src/server/responses/core.ts` + (read-only for phase 5), `docs-site/src/content/docs/reference/cli/lifecycle.md`, + `docs-site/src/content/docs/reference/configuration/server.md`, matching `tests/` files, + and this devlog unit. +- Out of scope: releases, promotion to `main`/`preview`, npm publish, deployment, auth or + credential rewrites beyond what a named issue requires, other worktrees. +- Verification policy (user-directed): **no repository-wide local suite**. CI runs behind the + work — each phase pushes, opens its PR, and merges by admin; CI is then tracked and judged + at the end of the train rather than blocking each merge. +- Merge mechanism: `gh pr merge --squash --admin`. + +## Work-phase map (one phase = one full PABCD cycle) + +| WP | Doc | Slice | Depends on | +|----|-----|-------|------------| +| wp0 | 000 | this roadmap; goalplan lock | — | +| wp1 | 010 | land PR #3163 (closes #3156) | wp0 | +| wp2 | 020 | land PR #3166 (closes #3157) | wp1 | +| wp3 | 030 | land carry PR #2986; close #2083 landed-via-maintainer | wp2 | +| wp4 | 040 | implement #3094 — `ocx models new-policy`/`new-arrivals` dispatch | wp3 | +| wp5 | 050 | implement #3108 — combo default reasoning effort reaches the target | wp4 | +| wp6 | 060 | implement #3158 T19/T21 — `/readyz` shape + three hub/remoteGui config keys | wp5 | + +wp4–wp6 are a stack: each branch is cut from the previous one's landed `dev`, so a lower +layer's merge is the upper layer's base (DEV-STACK-01). + +## Accept criteria + +- c-1..c-6: one per work-phase, each requiring a merge SHA proven an ancestor of + `origin/dev` via `git merge-base --is-ancestor FETCH_HEAD`, plus the issue closed. +- c-7: at least three bug/PR items closed with landing proof. +- Final CI judgment: `gh run list --branch dev` green on the last landed `dev` head, or every + remaining red identified as the known `responses-state` spill flake. diff --git a/devlog/_plan/260902_bug_pr_closeout_stack/010_phase1.md b/devlog/_plan/260902_bug_pr_closeout_stack/010_phase1.md new file mode 100644 index 0000000000..5bcda6b7b8 --- /dev/null +++ b/devlog/_plan/260902_bug_pr_closeout_stack/010_phase1.md @@ -0,0 +1,34 @@ +# 010 — Phase 1: land PR #3163 (closes #3156) + +## What lands + +PR #3163 `ingw/fix-copilot-context-3156` — head `486b2f99f3182acf055274755ade9c6571203ac9`. + +- MODIFY `src/codex/catalog/provider-fetch.ts` (+49) — read GitHub Copilot's live context + window at `capabilities.limits.max_context_window_tokens`, preserving the existing + metadata precedence and the safe-integer boundary for malformed values. +- MODIFY `tests/codex-catalog.test.ts` — routed catalog regression for accepted, + conflicting, and invalid Copilot payloads. + +No local code is written in this phase; the diff is the contributor's. + +## Why it is landable as-is + +`gh pr checks 3163` reports 23 checks, all pass, on the exact head above. The PR body +carries root cause, precedence reasoning, and per-suite verification counts +(`tests/codex-catalog.test.ts`: 255 pass / 0 fail). + +## Execution + +1. Re-read `gh pr view 3163 --json headRefOid,mergeStateStatus` to confirm no drift. +2. `gh pr merge 3163 --squash --admin --delete-branch`. +3. `git fetch origin dev` and `git merge-base --is-ancestor FETCH_HEAD`. +4. Confirm #3156 auto-closed; `Closes #3156` targets `dev`, which is not the default + branch, so close it by hand if GitHub did not. + +## Verification (C) + +- `gh pr view 3163 --json state,mergeCommit` reports MERGED with a SHA. +- ancestry check exits 0. +- `gh issue view 3156 --json state` reports CLOSED. + diff --git a/devlog/_plan/260902_bug_pr_closeout_stack/011_wp1_landing.md b/devlog/_plan/260902_bug_pr_closeout_stack/011_wp1_landing.md new file mode 100644 index 0000000000..1a73fb536b --- /dev/null +++ b/devlog/_plan/260902_bug_pr_closeout_stack/011_wp1_landing.md @@ -0,0 +1,29 @@ +# 011 — wp1 landing record: PR #3163 + +## Landed + +- PR: #3163 `ingw/fix-copilot-context-3156` by @Ingwannu +- Head audited: `486b2f99f3182acf055274755ade9c6571203ac9` +- Merge SHA on `dev`: `e236c36239c93f006a706aba3e7c84da167b5dd9` +- Mechanism: `gh pr merge 3163 --squash --admin --delete-branch` +- Closes: #3156 (closed manually — PRs target `dev`, not the default branch, so + GitHub does not auto-close) + +## Evidence at merge time + +`gh pr checks 3163` — 23 checks, every one `pass`, on the audited head. No waived check. + +## Ancestry proof + + git fetch origin dev + git merge-base --is-ancestor e236c36239c93f006a706aba3e7c84da167b5dd9 FETCH_HEAD + # exit 0 + +## What changed in the product + +`src/codex/catalog/provider-fetch.ts` now reads GitHub Copilot's live context window from +`capabilities.limits.max_context_window_tokens`. Before this, that field was unrecognized and +Copilot models fell back to the conservative 128K window. Existing metadata precedence and +the safe-integer rejection of malformed values are unchanged; +`tests/codex-catalog.test.ts` covers accepted, conflicting, and invalid payloads. + diff --git a/devlog/_plan/260902_bug_pr_closeout_stack/020_phase2.md b/devlog/_plan/260902_bug_pr_closeout_stack/020_phase2.md new file mode 100644 index 0000000000..c692fdfd63 --- /dev/null +++ b/devlog/_plan/260902_bug_pr_closeout_stack/020_phase2.md @@ -0,0 +1,48 @@ +# 020 — Phase 2: land PR #3166 (closes #3157) + +## What lands + +PR #3166 `ingw/fix-request-owned-main-pin-3157` — head `17f01162ad404f1bcee7d7f00998fc0e143365e5`. + +- MODIFY `src/codex/auth-context.ts` — honor an effective healthy manual `__main__` pin when + a Pool-mode request carries its own forwardable Codex bearer; validate that caller + credential's account-gated model roster; keep paused or quota-drained mains on the + ordinary Pool promotion path. +- MODIFY `structure/08_openai-provider-tiers.md` — document the request-owned credential and + pin boundary. +- MODIFY `tests/codex-auth-context.test.ts` — 68 passing cases including healthy main at 16% + vs Pool at 100%, drained main vs healthy Pool, and caller entitlement denial with a + model-only detour. + +## The red check + +`test 3/4` fails on `tests/responses-state.test.ts > Responses previous_response_id state > +late async spill completion cannot overwrite the shutdown fallback`: + + error: Response spill ACL budget exhausted + code: "ETIMEDOUT" + at nextSpillHardenDeadlineMs (src/responses/spill-store.ts:232:29) + +That is a wall-clock ACL budget expiring on a loaded runner. The PR's diff does not reach +`src/responses/`. Treat it as an unrelated flake: rerun the failed job, continue the train, +and judge the result at the end rather than blocking the merge on it. + +## Security note + +This is a credential-selection change, so MAINTAINERS.md requires explicit security review. +The PR body records the trust boundary: no caller bearer is persisted, no Pool +affinity/health/entitlement state is written, and the physical main credential is not read. +The merging maintainer accepts that review. + +## Execution + +1. `gh run rerun --failed` for the exact head, then continue and poll later. +2. `gh pr merge 3166 --squash --admin --delete-branch`. +3. ancestry proof plus `gh issue view 3157`. + +## Verification (C) + +- merge SHA is an ancestor of `origin/dev`. +- #3157 closed. +- rerun of the flaked job recorded: green, or still flaking with the same unrelated stack. + diff --git a/devlog/_plan/260902_bug_pr_closeout_stack/021_wp2_landing.md b/devlog/_plan/260902_bug_pr_closeout_stack/021_wp2_landing.md new file mode 100644 index 0000000000..21334e4703 --- /dev/null +++ b/devlog/_plan/260902_bug_pr_closeout_stack/021_wp2_landing.md @@ -0,0 +1,45 @@ +# 021 — wp2 landing record: PR #3166 + +## Landed + +- PR: #3166 `ingw/fix-request-owned-main-pin-3157` by @Ingwannu +- Head audited: `17f01162ad404f1bcee7d7f00998fc0e143365e5` +- Merge SHA on `dev`: `75090d4e0e26637a3db0157edf3090830ba00d52` +- Mechanism: `gh pr merge 3166 --squash --admin --delete-branch` +- Closes: #3157 (closed manually) + +## The flake, and how it was resolved rather than waived + +At first inspection `test 3/4` was red on +`tests/responses-state.test.ts > late async spill completion cannot overwrite the shutdown +fallback` with `ETIMEDOUT` from `src/responses/spill-store.ts:232` — a wall-clock ACL budget +expiring on a loaded runner, in a subsystem this PR does not touch. + +Rather than merge over a red check, the run was re-inspected: run `33527409692` had already +been re-run and reported `completed success`, and `gh pr checks 3166` returned zero `fail` +lines on the same head. The merge went in on a genuinely green rollup. + +## Ancestry proof + + git merge-base --is-ancestor 75090d4e0e26637a3db0157edf3090830ba00d52 origin/dev + # exit 0 + +## What changed in the product + +A Pool-mode request carrying its own forwardable Codex bearer no longer clears a healthy +manual `__main__` pin. Request-owned credentials are excluded from stored-account entitlement +discovery by design, and the shared-selection path had been reading that exclusion as evidence +the pinned main was dead — persisting a Pool account at 100% usage over a main at 16%. + +The fix validates the caller credential's own account-gated roster, gives an unentitled caller +a model-only detour that leaves the shared pin intact, and keeps paused or quota-drained mains +on the ordinary Pool promotion path. + +## Security boundary + +MAINTAINERS.md requires explicit security review for credential-selection changes. The PR +records the boundary: no caller bearer is persisted, no Pool affinity, health, or entitlement +state is written, and the physical main credential is not read. Documented in +`structure/08_openai-provider-tiers.md`; regressions in `tests/codex-auth-context.test.ts` +(68 pass). + diff --git a/devlog/_plan/260902_bug_pr_closeout_stack/030_phase3.md b/devlog/_plan/260902_bug_pr_closeout_stack/030_phase3.md new file mode 100644 index 0000000000..3fa81f6640 --- /dev/null +++ b/devlog/_plan/260902_bug_pr_closeout_stack/030_phase3.md @@ -0,0 +1,40 @@ +# 030 — Phase 3: land carry PR #2986, close #2083 + +## What lands + +PR #2986 `codex/carry-2083-xai-imagine` — maintainer carry of #2083 by @zhou-zhichao, +8 commits cherry-picked onto `dev` with author credit preserved. + +Surface: `src/images/` (artifacts, fulfill, index, plan, synthetic-tool, xai-client), +`src/responses/parser.ts`, `src/server/images.ts`, five locales of +`docs-site/.../guides/image-bridge.md` and `codex-integration.md`, plus six test files. + +Relays Codex `image_gen` tool calls to xAI Imagine using Grok OAuth, gated behind exact +`images.bridgeEnabled === true`. + +## Why the carry exists + +#2083 is APPROVED with 24/24 green checks, but its head had drifted 35 commits behind +`dev` — past the repository's 10-commit freshness boundary — so the green run no longer +describes what would land. A maintainer cannot push to a contributor branch, hence the carry. + +## Security review status + +Recorded in the PR body, performed on the exact head: credentials pinned to +`https://api.x.ai/v1`, `redirect: "manual"` on the credentialed fetch, no prompt or +credential logging, opt-in gate fails closed with a fixed 400 before any fallback, artifact +reads require API admission plus Origin validation. Verdict PASS WITH NOTES — artifact +authorization is proxy-wide, matching the single-operator trust model. + +## Execution + +1. `gh pr view 2986 --json headRefOid,mergeStateStatus`; rebase onto current `dev` if the + carry fell behind after phases 1-2. +2. `gh pr merge 2986 --squash --admin --delete-branch`. +3. `gh pr close 2083` with a landed-via-maintainer comment naming the merge SHA. + +## Verification (C) + +- merge SHA ancestor of `origin/dev`. +- #2986 MERGED, #2083 CLOSED with the crediting comment. + diff --git a/devlog/_plan/260902_bug_pr_closeout_stack/031_wp3_disposition.md b/devlog/_plan/260902_bug_pr_closeout_stack/031_wp3_disposition.md new file mode 100644 index 0000000000..067bb9aa43 --- /dev/null +++ b/devlog/_plan/260902_bug_pr_closeout_stack/031_wp3_disposition.md @@ -0,0 +1,50 @@ +# 031 — wp3 disposition: PR #2986 / #2083 do NOT land in this train + +## Decision + +**NEEDS_REWORK, not merged.** The roadmap (030_phase3.md) assumed #2986 was a clean carry +awaiting a maintainer merge. Refreshing the live state at execution time contradicted that. + +## Evidence at execution time + +- `gh pr view 2986` — `OPEN`, `mergeStateStatus: BLOCKED`, + head `842170b6f3d076a8274c1cba8824f3e3c56f0bb7`. +- `reviewDecision: CHANGES_REQUESTED`, from maintainer @Ingwannu — not a stale bot nit. +- `git rev-list --count 870a2adb6eaccc9da9ea9832a596e1b2650ab1ea..origin/dev` → **179**. + The PR base is 179 commits behind `dev`, so its green CI describes a tree that no longer + exists — the same freshness problem that caused the carry in the first place. + +## What the maintainer asked for + +Three runtime edge cases, each concrete and each still open: + +1. `src/images/fulfill.ts` resolves `aspect_ratio: "auto"` to `undefined` before calling + `callXaiImages`, so `resolveAspectRatio()` treats the field as absent and derives a ratio + from `size`. An explicit Auto selection therefore stops suppressing size-derived selection. +2. `src/responses/parser.ts` replaces only the *first* unnamespaced `image_gen` when a hosted + declaration arrives. With both an ordinary and a custom root declaration ahead of it, the + second survives and the catalog stays ambiguous. +3. The default downloader in `connectPublicHttps` passes `maxBytes: undefined` to + `pinnedHttpGet`, dropping the `MAX_DOWNLOAD_BYTES` cap when a caller omits a limit. + +Plus a docs correction: the xAI `/v1/images` relay runs only when `bridgeEnabled === true` +**and** `images.provider` is omitted; an explicit image provider owns the route. + +## Why this train does not do it + +Merging over an explicit maintainer `CHANGES_REQUESTED` with `--admin` would spend the +maintainer's review authority to bypass the maintainer. Item 3 is a byte-cap regression on a +credentialless download path — a security-boundary defect, exactly the class +`MAINTAINERS.md` says needs review rather than an override. + +The rework is tractable (four small edits plus a rebase) but it is a different unit of work +from "land a reviewed PR", and it belongs to the author on the same branch, which is what the +maintainer explicitly asked for: *"Please address these on the same branch and rerun the +focused image/parser suites."* + +## Outcome + +- #2986: left open, awaiting author rework. No admin merge. +- #2083: left open. Closing it as `landed-via-maintainer` would be false — nothing landed. +- Train continues to wp4 (#3094), wp5 (#3108), wp6 (#3158 docs). + diff --git a/devlog/_plan/260902_bug_pr_closeout_stack/040_phase4.md b/devlog/_plan/260902_bug_pr_closeout_stack/040_phase4.md new file mode 100644 index 0000000000..0c88ba8ebc --- /dev/null +++ b/devlog/_plan/260902_bug_pr_closeout_stack/040_phase4.md @@ -0,0 +1,55 @@ +# 040 — Phase 4: #3094 — ocx models new-policy / new-arrivals are unreachable + +## Defect + +`src/cli/models-runtime.ts:332-333` implements both subcommands: + + else if (sub === "new-policy") action = () => newPolicy(argv, deps); + else if (sub === "new-arrivals") action = () => newArrivals(argv, deps); + +and `src/cli/models-runtime.ts:27-28` lists them in USAGE. But `handleModels` in +`src/cli/models.ts:448` routes only a hardcoded list to the runtime module: + + if (["live", "edit", "enable", "disable", "provider", "selected", "preset", "context", "shadow"].includes(subcommand ?? "")) { + +Neither name is in it, so both fall through to `handleConfiguredModels`, which rejects the +argument: `Unexpected argument(s): new-policy, status`, exit 1. +`docs-site/src/content/docs/guides/model-routing.md:88-89` documents both commands. + +## Root cause, not just symptom + +Two lists name the same set and only one was updated. The fix removes the duplication: +the runtime module owns its subcommand set and `handleModels` consumes it. + +## MODIFY map + +**`src/cli/models-runtime.ts`** — export the set the dispatcher already encodes: + + export const MODELS_RUNTIME_SUBCOMMANDS = [ + "live", "edit", "enable", "disable", "provider", "selected", "preset", + "new-policy", "new-arrivals", "context", "shadow", + ] as const; + +and drive `handleModelsRuntimeCommand`'s guard from it, keeping the existing per-name +action mapping. + +**`src/cli/models.ts`** — replace the literal array with the imported set. The import must +stay lazy if the current dynamic `await import("./models-runtime")` exists to keep the CLI +startup path light; if so, import the constant from a leaf module rather than pulling the +whole runtime eagerly. + +## TESTS + +**NEW `tests/cli-models-runtime-dispatch.test.ts`**: + +1. every name in `MODELS_RUNTIME_SUBCOMMANDS` is routed by `handleModels` to the runtime + module rather than `handleConfiguredModels` — the general form of the defect, so a future + subcommand added without touching the dispatch fails here. +2. `new-policy` and `new-arrivals` specifically reach the runtime handler. +3. an unknown subcommand still falls through to `handleConfiguredModels`. + +## Verification (C) + +- `bun test tests/cli-models-runtime-dispatch.test.ts` focused. No repository-wide suite. +- typecheck and the rest are CI's job, judged at the end of the train. + diff --git a/devlog/_plan/260902_bug_pr_closeout_stack/050_phase5.md b/devlog/_plan/260902_bug_pr_closeout_stack/050_phase5.md new file mode 100644 index 0000000000..d4396daae3 --- /dev/null +++ b/devlog/_plan/260902_bug_pr_closeout_stack/050_phase5.md @@ -0,0 +1,86 @@ +# 050 — Phase 5: #3108 — combo default reasoning effort arrives as none + +## Reported behaviour + +Combo `combo/0` with default reasoning level `max` routed to `deepseek-v4-pro` sends +`none`; selecting `deepseek-v4-pro` directly with `max` sends `max`. OpenCodex 2.37.0. + +## Mechanism in the tree + +`src/server/responses/core.ts:2277` builds the child body: + + const childBody = concreteComboRequestBody( + body, + pick.target, + comboDefaultEffort(config, comboId), + supportedLadderFor({ provider: targetRoute.provider, modelId: targetRoute.modelId }), + ); + +`src/combos/request.ts:75` then refuses to inject: + + if (!targetReasoningEfforts?.includes(defaultEffort)) { /* debug log */ return clone; } + +So the default is dropped whenever the concrete target's ladder does not literally contain +the configured rung — including when the ladder is `undefined`. The comment calls this +deliberate fail-closed behaviour, but the catalog path disagrees: +`src/codex/catalog/aggregation.ts:168` advertises the combo's default through +`effectiveComboDefault`, which downgrades a too-high request to the nearest supported rung +at or below it (`aggregation.ts:86-93`) instead of dropping it. + +That asymmetry is the defect: the catalog promises `max` or the nearest rung below, the +runtime silently sends nothing, and the provider default — `none` — applies. + +## MODIFY map + +**`src/combos/request.ts`** — reuse the catalog's own resolution instead of exact membership: + + const resolved = targetReasoningEfforts === undefined + ? undefined + : effectiveComboDefault(defaultEffort, targetReasoningEfforts); + if (!resolved) { /* same warn shape */ return clone; } + +then inject `resolved` rather than `defaultEffort`. + +- an unknown (`undefined`) ladder stays fail-closed — that half of the behaviour is correct. +- an explicitly empty ladder still yields `undefined` from `effectiveComboDefault` + (`ranked.length === 0`), so a no-reasoning model is never given an effort. +- a caller-supplied `reasoning.effort` is still untouched; that check runs first. + +## Import boundary — RESOLVED AT AUDIT, the direct import is forbidden + +A-gate audit (independent explorer, grok-4.6) plus direct tracing settled this: +`src/codex/catalog/aggregation.ts` does NOT reach `src/lab/`, so `tests/core-lab-boundary.test.ts` +would stay green — but the import is still wrong for two harder reasons: + +1. **It is a cycle.** `aggregation.ts:22-29` already imports `../../combos`. Adding + `src/combos/request.ts` -> `aggregation.ts` closes the loop. +2. **It drags the catalog plane onto the request path.** `aggregation.ts:1-31` pulls + `node:child_process`, `../../oauth`, `../model-cache`, and + `../../adapters/cursor/live-models`. `src/server/responses/core.ts` imports `src/combos/`, + so every routed request would carry live-discovery and OAuth weight it never uses. + +**Therefore the fallback is the plan, not a contingency.** Lift the ranking helper into +`src/reasoning-effort.ts` — a genuine leaf whose only import is `./types`, and which already +owns `codexEffortRank` (`reasoning-effort.ts:79-81`), the exact primitive the helper needs. + +**MOVE**: `effectiveComboDefault` from `src/codex/catalog/aggregation.ts:80-94` to +`src/reasoning-effort.ts`, renamed `resolveEffortAtOrBelow(configured, supported)` to say what +it does independent of combos. `aggregation.ts` imports it from there (it already imports +`codexEffortRank` out of the same module at line 11) and keeps a local +`effectiveComboDefault` alias only if the existing call site reads better that way. +`src/combos/request.ts` imports the same leaf function. No cycle, no catalog weight. + +## TESTS + +**`tests/combos.test.ts`** already covers `concreteComboRequestBody`. Add: + +1. configured `max`, target ladder `["low","medium","high"]` -> injects `high`. +2. configured `max`, ladder includes `max` -> injects `max` (unchanged). +3. ladder `undefined` -> no injection (fail-closed, unchanged). +4. ladder `[]` -> no injection (unchanged). +5. caller-supplied `reasoning.effort` -> untouched (unchanged). + +## Verification (C) + +- `bun test tests/combos.test.ts` focused. +- CI judged at the end of the train. diff --git a/devlog/_plan/260902_bug_pr_closeout_stack/060_phase6.md b/devlog/_plan/260902_bug_pr_closeout_stack/060_phase6.md new file mode 100644 index 0000000000..0840126d1b --- /dev/null +++ b/devlog/_plan/260902_bug_pr_closeout_stack/060_phase6.md @@ -0,0 +1,51 @@ +# 060 — Phase 6: #3158 T19/T21 documentation debt + +Two of the four remote-hub follow-ups are documentation-only and close in one diff. +T2 and T3 are behaviour gaps and stay open on #3158. + +## T19 — /readyz gained protocol negotiation metadata + +`src/server/index.ts:1170-1178` builds the readiness body as: + + const body = { + service: "opencodex", version: VERSION, uptime: process.uptime(), + pid: process.pid, port: boundPort ?? listenPort, status, + ...readyProtocolMetadata(config, req), + }; + +`src/remote/protocol.ts:46-57` adds `protocol`, `minimumClientProtocol`, and `managementUrl` +— the configured `hub.managementPublicOrigin` when `runtimeRole === "hub"`, otherwise the +observed request origin. + +`docs-site/src/content/docs/reference/cli/lifecycle.md:164` still says the sanitized HTTP +identity is `{service, version, uptime, pid, port, status}`. + +**MODIFY `docs-site/src/content/docs/reference/cli/lifecycle.md`** — extend that sentence to +name the three added fields, say where `managementUrl` comes from in each runtime role, and +keep the CLI JSON shape `{ready, status, pid, port}` explicitly distinct from the HTTP body. + +## T21 — three config keys ship undocumented + +`src/types/config.ts:259,272,284` declare `hub.managementPublicOrigin`, +`remoteGui.allowedTailscaleUsers`, and `remoteGui.allowInsecureHttp`. + +`docs-site/src/content/docs/reference/configuration/server.md:268` mentions the first two in +one prose sentence about `runtimeRole`; `allowInsecureHttp` appears nowhere. The +`guides/remote-hub.md` page shows `ocx config set` examples, but the reference page is the +source of truth for key semantics. + +**MODIFY `docs-site/src/content/docs/reference/configuration/server.md`** — document each key +with type, default when absent, what it gates, and the failure mode of setting it wrong. +`allowInsecureHttp` in particular is a security-relevant opt-out and needs the warning. + +## Locales + +English is the source. Per AGENTS.md, translated locales must not contradict the English +source; the tr/ja/fr/ru/zh-cn pages already carry the `runtimeRole` sentence, so leave them +rather than half-translating. Note the gap in the PR body. + +## Verification (C) + +- `rg` proof that each key name now appears in the reference page. +- the docs build is a CI job (`gates`), judged with the rest of the train. + diff --git a/devlog/_plan/260902_bug_pr_closeout_stack/070_closeout.md b/devlog/_plan/260902_bug_pr_closeout_stack/070_closeout.md new file mode 100644 index 0000000000..8234c1acfc --- /dev/null +++ b/devlog/_plan/260902_bug_pr_closeout_stack/070_closeout.md @@ -0,0 +1,66 @@ +# 070 — closeout: what landed, what did not, and why + +## Landed on `dev` + +| Item | PR | Merge SHA | Issue | +|---|---|---|---| +| Copilot context window | #3163 | `e236c36239c93f006a706aba3e7c84da167b5dd9` | #3156 closed | +| Request-owned main pin | #3166 | `75090d4e0e26637a3db0157edf3090830ba00d52` | #3157 closed | +| `ocx models` dispatch | #3171 | `e92aa336a83c86283b500269a1d55779836114b0` | #3094 closed | +| Combo default effort | #3172 | `7386b52016be7b0246ca941d4e285ec340331431` | #3108 closed | +| Remote-hub reference docs | #3173 | `0d8147c2002e3e4e4adf39a03084d6a6ab18991e` | #3158 T19/T21 | +| Failover e2e assertion | #3175 | `22a643a00b5974fa53b084a04491f60d56ec9ee2` | follow-up to #3108 | + +Every SHA verified with `git merge-base --is-ancestor origin/dev` exiting 0. + +Six pull requests merged, four issues closed. The objective asked for at least three +bug/PR items; ten items moved. + +## Did not land, deliberately + +**#2986 / #2083 — xAI Imagine relay.** The roadmap assumed a clean carry awaiting a merge. +The refresh at execution time said otherwise: `BLOCKED`, `CHANGES_REQUESTED` from maintainer +@Ingwannu, and a base 179 commits behind `dev`. One of the three requested fixes is a +`MAX_DOWNLOAD_BYTES` cap dropped on a credentialless download path — a security-boundary +defect. Admin-merging over that would have spent maintainer authority to bypass the +maintainer. Recorded in `031_wp3_disposition.md`. + +**#3158 T2 and T3.** Behaviour gaps, not documentation. The issue stays open for them. + +## What the loop got wrong, and how it was caught + +Two plan claims did not survive contact: + +1. **The import the plan proposed was unsafe.** 050_phase5.md originally suggested importing + `effectiveComboDefault` from `aggregation.ts` into `request.ts`. The A-gate auditor and + an independent trace both found that closes a cycle (aggregation already imports + `src/combos`) and drags `node:child_process`, `oauth`, `model-cache`, and + `cursor/live-models` onto the request path. Repaired before implementation: the resolver + moved to `src/reasoning-effort.ts`, a leaf whose only import is `./types`. +2. **`allowInsecureHttp` is retired, not a live setting.** 060_phase6.md planned to document + it as a security-relevant opt-out. The source says it grants nothing and is parsed only so + an older config keeps loading. Documented as retired instead. + +And one implementation gap the local scope missed: + +3. **A stale assertion in the failover e2e suite.** The scoped local runs for #3172 covered + `combos.test.ts`, the catalog suite, and the boundary suite — not + `server-combo-failover-e2e.test.ts`, which held an assertion encoding the old + drop-on-miss behavior. CI on the merged `dev` head caught it within minutes and #3175 + corrected it. This is the cost of the no-local-suite policy, and it is a cheap one: the + trailing CI signal did exactly the job it was left to do. + +## Verification policy actually used + +No repository-wide local suite was run, per instruction. Each change was gated by focused +`bun test` files plus red-green proof that the new regression genuinely fails without the +fix, with CI trailing the train and judged at the end. + +## Final CI verdict + +Run `33533338305` on `dev` head `22a643a00b5974fa53b084a04491f60d56ec9ee2` — +**completed success**, zero failed jobs across the full matrix (Linux shards 1-4, macOS, +keyring, npm-global, gates, storage policy, api usage, hygiene). + +That head contains every landing in this train. The trailing-CI policy is therefore +discharged: nothing merged here leaves `dev` red. From 8d2dd66398450974e28ec158aed4a77862f0cdf7 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 12:00:22 +0900 Subject: [PATCH 165/172] feat(cursor): expose -fast identities to clients without a Fast toggle (#3233) * feat(cursor): expose -fast identities to clients without a Fast toggle Codex has a Fast toggle, so its rows stay umbrella rows and the toggle picks the dimension. Claude Code and other OpenAI-compatible clients have none - they can only pick a listed id - so with fastMode on they are offered the fast identity directly. The listed id is composed from the base's defaultVariant, not a bare -fast suffix. Measured: claude-opus-5-fast parses back as the REGULAR-fast sibling and resolves to claude-opus-5-high-fast, a shorter ladder in the quarantined regular family, which is a different wire from what the Codex toggle sends. The mirror case is equally wrong: grok has no thinkingFast spec, so grok-4.6-thinking-fast would fall back to the regular spec and emit a bare grok-4.6 with no effort and no fast marker. Either fixed suffix is wrong for half the table. A test asserts the two surfaces converge: for every fast-capable base, the listed id and the toggled umbrella id resolve to the same wire. Request-time promotion needed no new code. fastMode already produces a set tier decision on a fast-capable route with no caller service_tier, and every non-Codex inbound path replays through handleResponses, so PR2's builder already promotes a client whose saved config still names the umbrella id. Desktop 3P aliases and dashboard row ids are deliberately untouched: the former are hashed from the model name, the latter are enable/disable keys. Refs devlog/_plan/260902_cursor_unified_identity/030_wp4_global_fast_switch.md * perf(cursor): resolve the fast-id helper once per request, not per model The listing branches called await import() inside the row mapper, so a request with N models paid N dynamic imports on a hot path. Hoist it to one resolution per request; when the switch is off the value is null and the adapter module is never loaded at all. Raised in review of #3233. --------- Co-authored-by: jun --- .../docs/reference/configuration/providers.md | 29 +++++++ src/adapters/cursor/catalog.ts | 21 +++++ src/claude/model-info.ts | 15 +++- src/server/index.ts | 17 +++- .../management/agent-settings-routes.ts | 8 +- tests/cursor-fast-listing.test.ts | 86 +++++++++++++++++++ 6 files changed, 171 insertions(+), 5 deletions(-) create mode 100644 tests/cursor-fast-listing.test.ts diff --git a/docs-site/src/content/docs/reference/configuration/providers.md b/docs-site/src/content/docs/reference/configuration/providers.md index 8dd1376b87..cfd5135ae9 100644 --- a/docs-site/src/content/docs/reference/configuration/providers.md +++ b/docs-site/src/content/docs/reference/configuration/providers.md @@ -210,6 +210,35 @@ contract; existing configurations see these migration deltas: Explicit capability `false` and Responses caller-tier forwarding retain their existing contracts. +### Cursor Fast (`cursor-variant`) + +Cursor has no `service_tier` field. Its fast product is a different **model variant** — +`claude-opus-5-thinking-high-fast`, or a `{id:"fast",value:"true"}` request parameter for +Grok — so the Cursor entry declares `fastWire.kind: "cursor-variant"` and the request +builder resolves the variant instead of setting a request field. + +Only the bases that actually declare a fast variant advertise Fast: `claude-opus-4-7`, +`claude-opus-4-8`, `claude-opus-5`, `grok-4.5`, `grok-4.6`. Every other Cursor row publishes +`supportsServiceTier: false`, so Codex shows no toggle rather than a dead one. + +A base whose umbrella row routes thinking upgrades to its **thinking-fast** variant, not to +the plain fast sibling — that sibling is a different product with a shorter effort ladder, +and for `claude-opus-5` its regular family is quarantined upstream. + +`fastMode` behaves differently per surface, because only Codex has a Fast toggle of its own: + +| Surface | `fastMode: true` | +|---|---| +| Codex | rows stay umbrella rows; the app's Fast toggle selects the variant | +| Claude Code (`?ids=cli`) | lists the fast identity, e.g. `claude-ocx-cursor--claude-opus-5-thinking-fast` | +| OpenAI `/v1/models` | lists `cursor/claude-opus-5-thinking-fast` | +| Claude Desktop (3P) | unchanged — its aliases are hashed from the model name | +| Dashboard `/api/models` | row ids unchanged; they are the enable/disable keys | + +Requests are promoted either way: with `fastMode: true`, picking the umbrella id still +resolves to the fast variant, so a client whose saved config predates the switch does not +need to rediscover. Every legacy variant id keeps routing unchanged. + ### xAI Priority Processing The built-in `xai` preset advertises and injects Fast only when its effective transport uses diff --git a/src/adapters/cursor/catalog.ts b/src/adapters/cursor/catalog.ts index 25894e504c..d355708736 100644 --- a/src/adapters/cursor/catalog.ts +++ b/src/adapters/cursor/catalog.ts @@ -469,6 +469,27 @@ export function cursorFastCapableBases(): string[] { .map(([baseId]) => baseId); } +/** + * The id to LIST for a base when the global fast switch is on, for clients that have no + * Fast toggle of their own. Undefined when the base has no fast dimension, so a caller + * cannot advertise an id that would not route. + * + * Composed from the base's defaultVariant rather than a bare `-fast` suffix. Measured: for a + * thinking-default base, `claude-opus-5-fast` parses back as the REGULAR-fast sibling and + * resolves to `claude-opus-5-high-fast` — a shorter ladder, in the quarantined regular + * family, and a different wire from what the Codex toggle sends. The mirror case is equally + * wrong: grok has no thinkingFast spec, so `grok-4.6-thinking-fast` would fall back to the + * regular spec and emit a bare `grok-4.6` with no effort and no fast marker at all. + */ +export function cursorFastIdFor(baseId: string): string | undefined { + const capability = CURSOR_CAPABILITIES[baseId]; + if (!capability) return undefined; + const kind = upgradeToFast(baseId, capability.defaultVariant); + if (kind === "thinkingFast") return `${baseId}-thinking-fast`; + if (kind === "fast") return `${baseId}-fast`; + return undefined; +} + function normalizeRequestedEffort(reasoning: string | undefined): string | undefined { const normalized = reasoning?.toLowerCase(); return normalized === "ultra" ? "max" : normalized; diff --git a/src/claude/model-info.ts b/src/claude/model-info.ts index 2db284f797..fc58d499d2 100644 --- a/src/claude/model-info.ts +++ b/src/claude/model-info.ts @@ -17,6 +17,7 @@ */ import { catalogModelEfforts, nativeEffortClamp, nativeOpenAiContextWindow, nativeOpenAiMaxInputTokens, type CatalogModel, type NativeContextLimitsInput } from "../codex/catalog"; import { claudeCodeAlias, claudeCodeNativeAlias } from "./alias"; +import { cursorFastIdFor } from "../adapters/cursor/catalog"; import { desktop3pAlias } from "./desktop-3p"; import { AUTO_CONTEXT_OFF, type AutoContextMode } from "./context-windows"; @@ -109,6 +110,7 @@ export function buildAnthropicModelInfos( idStyle: AnthropicIdStyle = "desktop3p", aliasForRoute: (provider: string, modelId: string) => string = desktop3pAlias, nativeContextCap?: NativeContextLimitsInput, + fastMode?: boolean, ): AnthropicModelInfo[] { const out: AnthropicModelInfo[] = []; const seen = new Set(); @@ -151,7 +153,16 @@ export function buildAnthropicModelInfos( push1mVariant(info, nativeWindow, nativeMaxInput); } for (const m of routedModels) { - const id = idStyle === "readable" ? claudeCodeAlias(m.provider, m.id) : aliasForRoute(m.provider, m.id); + // Global Fast has no toggle on this surface, so the fast identity is what gets listed — + // a client here can only pick a listed id. Limited to the readable CLI style: Desktop 3P + // ids are hashed from the model name, so rewriting them would strand a saved selection. + const fastModelId = fastMode === true && m.provider === "cursor" && idStyle === "readable" + ? cursorFastIdFor(m.id) + : undefined; + const listedModelId = fastModelId ?? m.id; + const id = idStyle === "readable" + ? claudeCodeAlias(m.provider, listedModelId) + : aliasForRoute(m.provider, m.id); if (seen.has(id)) continue; seen.add(id); const ladder = Array.isArray(m.reasoningEfforts) ? m.reasoningEfforts : []; @@ -164,7 +175,7 @@ export function buildAnthropicModelInfos( ? Math.min(m.maxInputTokens, m.contextWindow) : m.maxInputTokens) : undefined; - const info = modelInfo(id, `${m.id} (${m.provider})`, ladder, imageInput, routedMaxInput ?? m.contextWindow); + const info = modelInfo(id, `${listedModelId} (${m.provider})`, ladder, imageInput, routedMaxInput ?? m.contextWindow); out.push(info); // Anthropic passthrough guard (audit 021 #3): never auto-widen canonical claude // routes — only a genuine >=1M window earns the variant row there. diff --git a/src/server/index.ts b/src/server/index.ts index 4f07679190..efbab1b93a 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -1432,7 +1432,7 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server { + const { cursorFastIdFor } = await import("../adapters/cursor/catalog"); + return (modelId: string, provider = "cursor") => provider === "cursor" ? cursorFastIdFor(modelId) : undefined; + })() + : null; // Selector-active discovery follows the same complete supported set as the Codex catalog // for both bare and qualified rows. Without selectors, the live catalog continues to own // bare availability. @@ -1552,7 +1561,11 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server nativeModelRow(id)), ...visibleAccountNatives.map(({ id, metadataId }) => nativeModelRow(id, metadataId)), ...await Promise.all(uniqueCatalogModelsForRawPublicList(goOrdered).map(async m => { - const publicId = m.alias ?? `${m.provider}/${m.id}`; + // Same rule as the anthropic branch: with the global fast switch on, a client + // that has no Fast toggle is offered the fast identity directly. An operator + // alias is an explicit decision and still wins. + const fastModelId = cursorFastIdForListing?.(m.id, m.provider); + const publicId = m.alias ?? `${m.provider}/${fastModelId ?? m.id}`; const isCombo = m.provider === "combo" && exactComboSlugs.has(publicId); const provider = config.providers[m.provider]; const effective = provider diff --git a/src/server/management/agent-settings-routes.ts b/src/server/management/agent-settings-routes.ts index d25e4895fb..193f9841fa 100644 --- a/src/server/management/agent-settings-routes.ts +++ b/src/server/management/agent-settings-routes.ts @@ -1041,6 +1041,11 @@ export async function handleAgentSettingsRoutes(ctx: ManagementContext): Promise ...models.filter(m => !isDisabled(m.provider, m.id)).map(m => `${m.provider}/${m.id}`), ]; const aliases: { id: string; display_name: string }[] = []; + // Resolved once, not per model: with the global fast switch on, Claude Code discovers the + // fast identity, so the dashboard must list the same id rather than the umbrella one. + const cursorFastIdFor = config.fastMode === true + ? (await import("../../adapters/cursor/catalog")).cursorFastIdFor + : undefined; for (const slug of listCatalogNativeSlugs()) { // Readable CLI-surface alias with hash fallback (devlog 050 / audit 051 #2) — // the same shared helper the /v1/models ?ids=cli path uses. @@ -1048,7 +1053,8 @@ export async function handleAgentSettingsRoutes(ctx: ManagementContext): Promise } for (const m of models) { if (isDisabled(m.provider, m.id)) continue; - aliases.push({ id: claudeCodeAlias(m.provider, m.id), display_name: `${m.id} (${m.provider})` }); + const listedId = (m.provider === "cursor" ? cursorFastIdFor?.(m.id) : undefined) ?? m.id; + aliases.push({ id: claudeCodeAlias(m.provider, listedId), display_name: `${listedId} (${m.provider})` }); } const contextWindows = buildClaudeContextWindows([...visibleNativeSlugs(config)], models, nativeContextLimits(config)); const webSearchOverride = config.claudeCode?.webSearchSidecar; diff --git a/tests/cursor-fast-listing.test.ts b/tests/cursor-fast-listing.test.ts new file mode 100644 index 0000000000..d764eb5799 --- /dev/null +++ b/tests/cursor-fast-listing.test.ts @@ -0,0 +1,86 @@ +import { describe, expect, test } from "bun:test"; +import { cursorFastCapableBases, cursorFastIdFor, resolveCursorSelection } from "../src/adapters/cursor/catalog"; +import { buildAnthropicModelInfos } from "../src/claude/model-info"; +import { AUTO_CONTEXT_OFF } from "../src/claude/context-windows"; +import { desktop3pAlias } from "../src/claude/desktop-3p"; +import { providerConfigSeed } from "../src/providers/derive"; +import { getProviderRegistryEntry } from "../src/providers/registry"; +import { decideTier } from "../src/providers/fastwire"; +import { fastPolicyForModel } from "../src/providers/service-tier"; +import type { CatalogModel } from "../src/codex/catalog"; + +function cursorModel(id: string, contextWindow = 1_000_000): CatalogModel { + return { provider: "cursor", id, contextWindow, reasoningEfforts: ["low", "high"] } as CatalogModel; +} + +const listIds = (models: CatalogModel[], fastMode?: boolean) => + buildAnthropicModelInfos([], models, AUTO_CONTEXT_OFF, "readable", desktop3pAlias, undefined, fastMode) + .map(info => info.id); + +/** + * Codex has a Fast toggle, so its rows stay umbrella rows. Claude Code and other + * OpenAI-compatible clients have none — they can only pick a listed id — so the global + * switch offers them the fast identity directly + * (devlog 260902_cursor_unified_identity/030). + */ +describe("global fast switch lists -fast identities outside Codex", () => { + test("the listed id and the Codex toggle converge on the same wire", () => { + // The guard against the whole point of the feature: two surfaces, one behaviour. + // A bare -fast suffix would NOT satisfy this for a thinking-default base. + for (const base of cursorFastCapableBases()) { + const listed = cursorFastIdFor(base); + expect(listed).toBeDefined(); + expect(resolveCursorSelection(listed!, "max").wireId) + .toBe(resolveCursorSelection(base, "max", undefined, { fast: true }).wireId); + } + }); + + test("a thinking-default base lists its thinking-fast id, a regular-default base its fast id", () => { + expect(cursorFastIdFor("claude-opus-5")).toBe("claude-opus-5-thinking-fast"); + expect(cursorFastIdFor("grok-4.6")).toBe("grok-4.6-fast"); + }); + + test("a base with no fast variant yields no fast id at all", () => { + for (const base of ["kimi-k3", "gpt-5.6-sol", "glm-5.3", "gemini-3.7-flash"]) { + expect(cursorFastIdFor(base)).toBeUndefined(); + } + }); + + test("Claude Code discovery lists the umbrella id with the switch off", () => { + expect(listIds([cursorModel("claude-opus-5")], false)) + .toContain("claude-ocx-cursor--claude-opus-5"); + expect(listIds([cursorModel("claude-opus-5")], undefined)) + .toContain("claude-ocx-cursor--claude-opus-5"); + }); + + test("Claude Code discovery lists the fast identity with the switch on", () => { + expect(listIds([cursorModel("claude-opus-5")], true)) + .toContain("claude-ocx-cursor--claude-opus-5-thinking-fast"); + expect(listIds([cursorModel("grok-4.6", 500_000)], true)) + .toContain("claude-ocx-cursor--grok-4.6-fast"); + }); + + test("the switch leaves a base without a fast variant alone", () => { + expect(listIds([cursorModel("kimi-k3")], true)).toContain("claude-ocx-cursor--kimi-k3"); + }); + + test("Desktop 3P hashed aliases are untouched by the switch", () => { + // Hashes are written into Desktop's config; rewriting them would strand a saved pick. + const off = buildAnthropicModelInfos([], [cursorModel("claude-opus-5")], AUTO_CONTEXT_OFF, "desktop3p", desktop3pAlias, undefined, false); + const on = buildAnthropicModelInfos([], [cursorModel("claude-opus-5")], AUTO_CONTEXT_OFF, "desktop3p", desktop3pAlias, undefined, true); + expect(on.map(i => i.id)).toEqual(off.map(i => i.id)); + }); + + test("fastMode alone promotes an umbrella request, with no caller service_tier", () => { + // The persisted-config case: a client still naming the umbrella id must go fast too. + const config = providerConfigSeed(getProviderRegistryEntry("cursor")!); + const decide = (id: string, fastMode?: boolean) => + decideTier(fastPolicyForModel(config, id, "cursor"), fastMode, undefined); + + expect(decide("claude-opus-5", true)).toEqual({ kind: "set", value: "fast" }); + expect(decide("grok-4.6", true)).toEqual({ kind: "set", value: "fast" }); + expect(decide("kimi-k3", true)).toEqual({ kind: "drop" }); + // And the switch off must not promote. + expect(decide("claude-opus-5", false)).toEqual({ kind: "drop" }); + }); +}); From 21416a7af208110fba397067e5bcdd784f18be4a Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 12:03:02 +0900 Subject: [PATCH 166/172] docs(cursor): record the stack landing SHAs and close residual R5 (#3243) The three Cursor identity PRs are on dev. Records each merged head and squash commit with the ancestry proof, and notes that --admin cleared only the review requirement - every merged head had zero failing checks. Also closes R5: the agent-task-recovery red was dev's own, and dev's #3242 (revert of #3239/#3240) fixed it. That file is 19/19 on the landed dev, so the follow-up fix PR this unit was going to open is unnecessary. Co-authored-by: jun --- .../040_residuals.md | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/devlog/_plan/260902_cursor_unified_identity/040_residuals.md b/devlog/_plan/260902_cursor_unified_identity/040_residuals.md index 08804bb39a..ddf198e633 100644 --- a/devlog/_plan/260902_cursor_unified_identity/040_residuals.md +++ b/devlog/_plan/260902_cursor_unified_identity/040_residuals.md @@ -45,3 +45,57 @@ clean stash of this branch, so none is caused by this unit: Not this unit's to fix. Recorded so a later cycle does not mistake them for a regression it introduced. + +## R5 — `agent-task-recovery` is red on dev itself (landing cycle, 2026-09-02) + +While landing this stack, `test 3/4` failed on the rebased PR #3222 head: + +``` +(fail) agent task recovery (opt-in, default off) + > keeps the disabled fail-fast response byte-identical to the absent feature + tests/agent-task-recovery.test.ts:53 Received: 502 +``` + +Not caused by this stack. Reproduced on a DETACHED checkout of pure `origin/dev` +HEAD `b54508c8c` (`fix(agents): allow Codexless V2 task recovery (#3241)`): same one +failure, 18 pass / 1 fail. The surrounding commits `#3239` -> `#3240` -> `#3241` are a +live repair chain in that area, so the red is theirs to close. + +Recorded so a later reader does not attribute it to the Cursor identity work, and so the +landing decision is auditable: the stack was merged with this pre-existing failure present +on the base branch, not introduced by it. + +**Closed 2026-09-02, by dev, not by this unit.** `#3242` +(`revert(subagents): drop the synthesized native chain for encrypted spawns`) reverted +`#3239` and `#3240`. On the resulting `origin/dev` the file is green: + +``` +$ bun test tests/agent-task-recovery.test.ts +19 pass / 0 fail +``` + +The mechanism matches the diagnosis recorded above: the synthesized native chain rewrote +`xai/grok-4.5` to `gpt-5.5` for BOTH the absent and the disabled config, so the final route +was native and the honest 400 gate (`core.ts` "encrypted child tasks may only reach the +canonical native backend") never fired - the request went out and the fixture's throwing +`fetch` turned it into a 502. With the chain gone, `applySubagentModelFallback` returns +`null` for all three config shapes and the 400 is restored. No follow-up PR needed. + +## Landing record (2026-09-02) + +The stack landed on `dev` in dependency order, each with exact-head CI green and ancestry +proven by `git merge-base --is-ancestor` against a freshly fetched `origin/dev`: + +| PR | merged head | squash commit | +|---|---|---| +| #3222 umbrella seed + labels | `419e89625` | `7aa64bb0bf1700482c74064a4d7523a5a960cf11` | +| #3225 cursor-variant Fast toggle | `61d6d38d9` | `83838e7fab0e2b1a23ab86dee4ef606f25eeb8d6` | +| #3233 fastMode -fast listing | `f26169712` | `8d2dd66398450974e28ec158aed4a77862f0cdf7` | + +Maintainer `--admin` cleared only the `Protect dev` ruleset's review requirement. No red +check was bypassed: every merged head reported zero FAILURE conclusions. + +Each child was re-stacked by CHERRY-PICKING its unique commits onto the landed parent, not +by rebasing. A parent squash absorbs the child's content under a different commit id, so a +plain rebase conflicts against work that is already in the base - the hazard the stacked-PR +rules warn about, observed here on #3225. From 8fb4e6e797d4e0d44425a0167b399fa573c3226d Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 12:08:07 +0900 Subject: [PATCH 167/172] docs(cursor): closeout verification on the landed dev (#3244) Co-authored-by: jun --- .../040_residuals.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/devlog/_plan/260902_cursor_unified_identity/040_residuals.md b/devlog/_plan/260902_cursor_unified_identity/040_residuals.md index ddf198e633..85a32869e7 100644 --- a/devlog/_plan/260902_cursor_unified_identity/040_residuals.md +++ b/devlog/_plan/260902_cursor_unified_identity/040_residuals.md @@ -99,3 +99,19 @@ Each child was re-stacked by CHERRY-PICKING its unique commits onto the landed p by rebasing. A parent squash absorbs the child's content under a different commit id, so a plain rebase conflicts against work that is already in the base - the hazard the stacked-PR rules warn about, observed here on #3225. + +### Closeout verification (landed dev `21416a7af`) + +``` +git merge-base --is-ancestor 7aa64bb0b FETCH_HEAD -> OK (#3222) +git merge-base --is-ancestor 83838e7fa FETCH_HEAD -> OK (#3225) +git merge-base --is-ancestor 8d2dd6639 FETCH_HEAD -> OK (#3233) +git merge-base --is-ancestor 21416a7af FETCH_HEAD -> OK (#3243, this record) + +bun run typecheck exit 0 +bun test (11 files: cursor-*, fastwire-policy, claude-*, codex-catalog, + agent-task-recovery) 662 pass / 0 fail +``` + +No PR from this unit is left open. Remote branch deletion is refused by the repository +ruleset, which is expected protection and does not affect the landings. From fd5ccab0bb462bc141474b7373ff2ae499588584 Mon Sep 17 00:00:00 2001 From: JUN Date: Wed, 2 Sep 2026 13:40:06 +0900 Subject: [PATCH 168/172] feat(cursor): read-only Private Inference status route for the dashboard (#3247) * feat(integrations): read-only Cursor Private Inference status route GET /api/native-integrations/cursor reports which Cursor builds are installed (product.json nameLong tells Private Inference from regular Cursor), the two values to paste into Cursor's gateway form, whether a Cursor client has called /v1/models since the proxy started, and which active models will show Cursor's Reasoning and Context controls. Nothing is written to Cursor: its settings live in a database the running app rewrites and its key in the OS keychain. The last-seen recorder keeps a validated User-Agent and a timestamp in memory only. * fix(cursor): filter the prediction table by catalog visibility The status route built its Model/Reasoning/Context prediction from the unfiltered catalog, so a model disabled in opencodex still appeared in the dashboard while the raw /v1/models list Cursor reads omitted it. Apply the same filterCatalogVisibleModels pass; regression test drives both endpoints with a disabled model. * docs(skill): the Cursor status route reports both builds, not only Private Inference --------- Co-authored-by: jun --- .../ocx/references/01_management_surface.md | 3 +- src/cli/capabilities.ts | 3 +- src/integrations/cursor-detect.ts | 133 +++++++++++ src/integrations/cursor-seen.ts | 31 +++ src/server/index.ts | 4 + src/server/management-api.ts | 2 + .../management/cursor-integration-routes.ts | 98 +++++++++ src/server/management/route-registry.ts | 2 + src/server/models-capabilities.ts | 35 +++ tests/cursor-integration-status.test.ts | 207 ++++++++++++++++++ 10 files changed, 516 insertions(+), 2 deletions(-) create mode 100644 src/integrations/cursor-detect.ts create mode 100644 src/integrations/cursor-seen.ts create mode 100644 src/server/management/cursor-integration-routes.ts create mode 100644 tests/cursor-integration-status.test.ts diff --git a/skills/ocx/references/01_management_surface.md b/skills/ocx/references/01_management_surface.md index b46de162b5..419a3a7af9 100644 --- a/skills/ocx/references/01_management_surface.md +++ b/skills/ocx/references/01_management_surface.md @@ -548,7 +548,7 @@ JSON mode: `payload`. ### `ocx integration native` -Show or toggle the native Claude, Claude Desktop, Codex, and Grok integrations. +Show or toggle the native Claude, Claude Desktop, Codex, and Grok integrations, and read the Cursor status (which builds are installed, gateway values, last request seen). | Method | Route | |---|---| @@ -557,6 +557,7 @@ Show or toggle the native Claude, Claude Desktop, Codex, and Grok integrations. | PUT | `/api/native-integrations/claude-desktop` | | PUT | `/api/native-integrations/codex` | | PUT | `/api/native-integrations/grok` | +| GET | `/api/native-integrations/cursor` | | Flag | Value | Meaning | |---|---|---| diff --git a/src/cli/capabilities.ts b/src/cli/capabilities.ts index e60585e5b5..accffd9fb9 100644 --- a/src/cli/capabilities.ts +++ b/src/cli/capabilities.ts @@ -498,13 +498,14 @@ export const CAPABILITIES: readonly Capability[] = [ }, { command: ["integration", "native"], - summary: "Show or toggle the native Claude, Claude Desktop, Codex, and Grok integrations.", + summary: "Show or toggle the native Claude, Claude Desktop, Codex, and Grok integrations, and read the Cursor status (which builds are installed, gateway values, last request seen).", routes: [ { method: "GET", path: "/api/native-integrations" }, { method: "PUT", path: "/api/native-integrations/claude" }, { method: "PUT", path: "/api/native-integrations/claude-desktop" }, { method: "PUT", path: "/api/native-integrations/codex" }, { method: "PUT", path: "/api/native-integrations/grok" }, + { method: "GET", path: "/api/native-integrations/cursor" }, ], flags: [{ name: "--json", value: "boolean", summary: "Emit the client rows or toggle result as JSON." }], mutates: true, diff --git a/src/integrations/cursor-detect.ts b/src/integrations/cursor-detect.ts new file mode 100644 index 0000000000..6e9cbc1b82 --- /dev/null +++ b/src/integrations/cursor-detect.ts @@ -0,0 +1,133 @@ +/** + * Detect Cursor desktop installs and tell the two builds apart. + * + * Cursor ships a second desktop distribution, "Cursor Private Inference", whose agent loop + * runs locally and calls an OpenAI-compatible gateway the user configures. That build can + * reach opencodex on loopback. Regular Cursor cannot: its backend calls the custom base URL + * and rejects private addresses. The two share a bundle id, data folder and URL scheme, so + * the only reliable discriminator is `nameLong` in the app's `product.json`. + * + * Detection is read-only and injectable: the proxy never writes anything into a Cursor + * install, its state database, or its keychain entries (the T20 exclusion in + * devlog/_plan/260822_senpi_cursor_transfer/090), and the tests run against a temp tree. + */ +import { existsSync, readdirSync, readFileSync } from "node:fs"; +import { homedir } from "node:os"; +import { posix, win32 } from "node:path"; + +export type CursorBuild = "private-inference" | "regular"; + +export interface CursorInstall { + build: CursorBuild; + /** The install root (the .app bundle, install directory, or AppImage extraction root). */ + path: string; + version: string | null; +} + +export interface CursorDetectDeps { + platform: string; + homedir: string; + env: Record; + readText(path: string): string | null; + listDir(path: string): string[]; +} + +export function realCursorDetectDeps(): CursorDetectDeps { + return { + platform: process.platform, + homedir: homedir(), + env: process.env, + readText: path => { + try { + return existsSync(path) ? readFileSync(path, "utf-8") : null; + } catch { + return null; + } + }, + listDir: path => { + try { + return readdirSync(path); + } catch { + return []; + } + }, + }; +} + +const PRIVATE_INFERENCE_NAME = "Cursor Private Inference"; +const REGULAR_NAME = "Cursor"; + +/** + * Candidate `product.json` paths per platform, each paired with the install root it + * belongs to. Only well-known locations; a custom install path is the user's to name. + */ +export function cursorProductJsonCandidates(deps: CursorDetectDeps): Array<{ root: string; productJson: string }> { + const out: Array<{ root: string; productJson: string }> = []; + // Join with the target platform's separator so the candidate list is stable in tests + // that describe another OS from this one. + const { join } = deps.platform === "win32" ? win32 : posix; + if (deps.platform === "darwin") { + for (const dir of ["/Applications", join(deps.homedir, "Applications")]) { + for (const entry of deps.listDir(dir)) { + if (!/^Cursor.*\.app$/i.test(entry)) continue; + const root = join(dir, entry); + out.push({ root, productJson: join(root, "Contents", "Resources", "app", "product.json") }); + } + } + return out; + } + if (deps.platform === "win32") { + const bases = [ + deps.env.LOCALAPPDATA ? join(deps.env.LOCALAPPDATA, "Programs") : null, + deps.env.ProgramFiles ?? null, + ].filter((value): value is string => value !== null); + for (const dir of bases) { + for (const entry of deps.listDir(dir)) { + if (!/^cursor/i.test(entry)) continue; + const root = join(dir, entry); + out.push({ root, productJson: join(root, "resources", "app", "product.json") }); + } + } + return out; + } + // Linux: AppImages carry product.json only once extracted, so this covers the tarball / + // package layouts and stays best-effort. + for (const dir of ["/opt", join(deps.homedir, ".local", "share")]) { + for (const entry of deps.listDir(dir)) { + if (!/^cursor/i.test(entry)) continue; + const root = join(dir, entry); + out.push({ root, productJson: join(root, "resources", "app", "product.json") }); + } + } + return out; +} + +function classify(productJson: string): { build: CursorBuild; version: string | null } | null { + let parsed: unknown; + try { + parsed = JSON.parse(productJson); + } catch { + return null; + } + if (!parsed || typeof parsed !== "object") return null; + const record = parsed as { nameLong?: unknown; version?: unknown }; + const version = typeof record.version === "string" ? record.version : null; + if (record.nameLong === PRIVATE_INFERENCE_NAME) return { build: "private-inference", version }; + if (record.nameLong === REGULAR_NAME) return { build: "regular", version }; + return null; +} + +export function detectCursorInstalls(deps: CursorDetectDeps = realCursorDetectDeps()): CursorInstall[] { + const found: CursorInstall[] = []; + const seen = new Set(); + for (const candidate of cursorProductJsonCandidates(deps)) { + if (seen.has(candidate.root)) continue; + const text = deps.readText(candidate.productJson); + if (text === null) continue; + const classified = classify(text); + if (!classified) continue; + seen.add(candidate.root); + found.push({ build: classified.build, path: candidate.root, version: classified.version }); + } + return found; +} diff --git a/src/integrations/cursor-seen.ts b/src/integrations/cursor-seen.ts new file mode 100644 index 0000000000..df4ba9a227 --- /dev/null +++ b/src/integrations/cursor-seen.ts @@ -0,0 +1,31 @@ +/** + * Remember the last time a Cursor client asked this proxy for its model list. + * + * The Integrations page cannot read Cursor's own settings (and must not write them), so + * "is Cursor pointed at me?" is answered from our side: Cursor's local-agent runtime sends + * `User-Agent: Cursor/` on `GET /v1/models`. Only that header value and a + * timestamp are kept, in memory, so a proxy restart forgets it and the card says so. + */ +// Attacker-controlled header: accept only the shape Cursor sends and keep it short. +const CURSOR_USER_AGENT = /^Cursor\/[\w.+-]{1,40}$/; + +export interface CursorSeen { + at: number; + userAgent: string; +} + +let last: CursorSeen | null = null; + +export function recordCursorSeen(headers: Headers, now = Date.now()): void { + const userAgent = headers.get("user-agent")?.trim() ?? ""; + if (!CURSOR_USER_AGENT.test(userAgent)) return; + last = { at: now, userAgent }; +} + +export function cursorLastSeen(): CursorSeen | null { + return last ? { ...last } : null; +} + +export function resetCursorSeenForTests(): void { + last = null; +} diff --git a/src/server/index.ts b/src/server/index.ts index efbab1b93a..9786068d66 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -225,6 +225,7 @@ import { import { detectInstall } from "../update/index"; import { readyProtocolMetadata } from "../remote/protocol"; import { modelCapabilityFields } from "./models-capabilities"; +import { recordCursorSeen } from "../integrations/cursor-seen"; export const MAX_WS_FRAME_BYTES = 50 * 1024 * 1024; const WEBSOCKET_IDLE_TIMEOUT_SECONDS = 0; @@ -1327,6 +1328,9 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server Models > Gateway), not by + * this proxy: its settings live in a SQLite database the running app rewrites and its API key + * in the OS keychain, both out of bounds for opencodex. So this route only answers the three + * questions the dashboard needs — which Cursor builds are installed, what to paste into the + * gateway form, and whether a Cursor client has actually called `/v1/models` since the proxy + * started — plus which active models will show Cursor's Reasoning and Context controls. + */ +import { readRuntimePort } from "../../config/process-state"; +import { filterCatalogVisibleModels, nativeContextLimits, nativeOpenAiContextTier, uniqueCatalogModelsForRawPublicList, visibleNativeSlugs } from "../../codex/catalog"; +import { cursorLastSeen, type CursorSeen } from "../../integrations/cursor-seen"; +import { detectCursorInstalls, type CursorInstall } from "../../integrations/cursor-detect"; +import { configuredApiAuthToken, isApiAuthRequired, jsonResponse } from "../auth-cors"; +import { fetchAllModels } from "../management-api"; +import { cursorEffortFamily } from "../models-capabilities"; +import type { ManagementContext } from "./context"; + +export const CURSOR_GATEWAY_PLACEHOLDER_KEY = "opencodex-loopback"; +export const CURSOR_GUIDE_URL = "https://lidge-jun.github.io/opencodex/guides/cursor-private-inference/"; + +export interface CursorIntegrationStatus { + privateInference: { installed: boolean; path: string | null; version: string | null }; + regularCursor: { installed: boolean; path: string | null }; + gateway: { baseUrl: string; apiKeyMode: "credential" | "placeholder"; placeholder: string }; + lastSeen: CursorSeen | null; + models: Array<{ + id: string; + reasoning: string[] | null; + context: { defaultWindow: number; longWindow: number } | null; + }>; + guideUrl: string; +} + +function pick(installs: CursorInstall[], build: CursorInstall["build"]): CursorInstall | undefined { + return installs.find(install => install.build === build); +} + +export async function buildCursorIntegrationStatus( + ctx: Pick & { url?: URL }, + installs: CursorInstall[] = detectCursorInstalls(), +): Promise { + const { config, deps } = ctx; + const privateInference = pick(installs, "private-inference"); + const regular = pick(installs, "regular"); + const runtime = (deps.readRuntimePort ?? readRuntimePort)(process.pid); + // The port the browser reached is the one Cursor on the same machine will reach too; the + // runtime record and config.port are fallbacks for a request that carries no port. + const port = runtime?.port ?? (Number(ctx.url?.port) || config.port); + // Describes the public bind. A second unauthenticated loopback listener may exist, but the + // value a user pastes into Cursor must work against the bind they will actually reach. + const credentialConfigured = !!configuredApiAuthToken(config) + || (config.apiKeys ?? []).some(entry => !!entry.key.trim()); + const apiKeyMode = isApiAuthRequired(config) || credentialConfigured ? "credential" : "placeholder"; + + const limits = nativeContextLimits(config); + // Same visibility rules as the raw /v1/models list Cursor will read: disabled models and + // provider allowlists drop out here too, or the prediction shows rows Cursor never gets. + const goModels = filterCatalogVisibleModels(await fetchAllModels(config), config); + const ids = [ + ...visibleNativeSlugs(config), + ...uniqueCatalogModelsForRawPublicList(goModels).map(model => model.alias ?? `${model.provider}/${model.id}`), + ]; + const models = ids.map(id => { + const tier = nativeOpenAiContextTier(id, limits); + return { + id, + reasoning: cursorEffortFamily(id), + context: tier ? { defaultWindow: tier.defaultWindow, longWindow: tier.longWindow } : null, + }; + }); + + return { + privateInference: { + installed: privateInference !== undefined, + path: privateInference?.path ?? null, + version: privateInference?.version ?? null, + }, + regularCursor: { installed: regular !== undefined, path: regular?.path ?? null }, + gateway: { + baseUrl: `http://127.0.0.1:${port}/v1`, + apiKeyMode, + placeholder: CURSOR_GATEWAY_PLACEHOLDER_KEY, + }, + lastSeen: cursorLastSeen(), + models, + guideUrl: CURSOR_GUIDE_URL, + }; +} + +export async function handleCursorIntegrationRoutes(ctx: ManagementContext): Promise { + const { req, url } = ctx; + if (url.pathname === "/api/native-integrations/cursor" && req.method === "GET") { + return jsonResponse(await buildCursorIntegrationStatus(ctx)); + } + return null; +} diff --git a/src/server/management/route-registry.ts b/src/server/management/route-registry.ts index 0cdce78987..95c72f5021 100644 --- a/src/server/management/route-registry.ts +++ b/src/server/management/route-registry.ts @@ -230,6 +230,8 @@ export const MANAGEMENT_ROUTES: readonly ManagementRoute[] = [ { method: "PUT", path: "/api/native-integrations/claude-desktop", module: "server/management/native-integration-routes", mutates: true }, { method: "PUT", path: "/api/native-integrations/codex", module: "server/management/native-integration-routes", mutates: true }, { method: "PUT", path: "/api/native-integrations/grok", module: "server/management/native-integration-routes", mutates: true }, + // server/management/cursor-integration-routes + { method: "GET", path: "/api/native-integrations/cursor", module: "server/management/cursor-integration-routes", mutates: false }, // server/management/oauth-account-routes { method: "DELETE", path: "/api/keys", module: "server/management/oauth-account-routes", mutates: true }, { method: "DELETE", path: "/api/keys/rotate", module: "server/management/oauth-account-routes", mutates: true }, diff --git a/src/server/models-capabilities.ts b/src/server/models-capabilities.ts index 55e3504f76..c7d6aa3c82 100644 --- a/src/server/models-capabilities.ts +++ b/src/server/models-capabilities.ts @@ -19,6 +19,41 @@ export const OPENCODEX_MODEL_API_TYPES: readonly string[] = Object.freeze(["chat export const OPENAI_FAMILY_API_TYPES: ReadonlySet = new Set(["chat_completions", "responses", "openai_chat", "openai_responses"]); +/** + * The reasoning-effort ladder Cursor's local-agent runtime attaches to a model, keyed by the + * model id after its last `/`. Cursor decides this from its own table rather than from the + * gateway's `reasoning_effort` list, so the dashboard can only PREDICT it; the values here + * mirror that table (read from the 3.18.25 bundle) and carry no Cursor behavior of their own. + * Null means Cursor shows no Reasoning control for the id. Distinct from + * `src/adapters/cursor/effort-map.ts`, which maps opencodex efforts onto Cursor's *backend* + * tiers for the outbound provider; this is what Cursor's *local* picker renders. + */ +const CURSOR_EFFORT_FAMILIES: ReadonlyArray<{ test: RegExp; ladder: readonly string[] }> = [ + { test: /^gpt-5[.-]6-(?:luna|sol|terra)$/u, ladder: ["low", "medium", "high", "xhigh"] }, + { test: /^gpt-5(?:\.\d+)?$/u, ladder: ["low", "medium", "high", "xhigh"] }, + { test: /^claude-opus-5$/u, ladder: ["low", "medium", "high", "xhigh", "max"] }, + { test: /^claude-opus-4[-.](?:7|8)$/u, ladder: ["low", "medium", "high", "xhigh", "max"] }, + { test: /^claude-sonnet-5$/u, ladder: ["low", "medium", "high", "xhigh", "max"] }, + { test: /^claude-opus-4[-.](?:5|6)$/u, ladder: ["low", "medium", "high", "max"] }, + { test: /^claude-sonnet-4[-.]6$/u, ladder: ["low", "medium", "high", "max"] }, + { test: /^grok-4[.-](?:3|5|6)(?:-(?:batch|build|nocomp))?$/u, ladder: ["minimal", "low", "medium", "high", "xhigh"] }, + { test: /^grok-build-latest$/u, ladder: ["minimal", "low", "medium", "high", "xhigh"] }, + { test: /^gemini-3\.[1-9].*flash-lite/u, ladder: [] }, + { test: /^gemini-/u, ladder: ["minimal", "low", "medium", "high"] }, +]; + +export function cursorEffortFamily(modelId: string): string[] | null { + let id = modelId.trim().toLowerCase(); + const slash = id.lastIndexOf("/"); + if (slash !== -1) id = id.slice(slash + 1); + const at = id.indexOf("@"); + if (at !== -1) id = id.slice(0, at); + for (const family of CURSOR_EFFORT_FAMILIES) { + if (family.test.test(id)) return family.ladder.length > 0 ? [...family.ladder] : null; + } + return null; +} + export interface ModelCapabilityInput { reasoningEfforts?: readonly string[]; contextWindow?: number; diff --git a/tests/cursor-integration-status.test.ts b/tests/cursor-integration-status.test.ts new file mode 100644 index 0000000000..9e82779dd8 --- /dev/null +++ b/tests/cursor-integration-status.test.ts @@ -0,0 +1,207 @@ +import { afterEach, beforeEach, describe, expect, setDefaultTimeout, test } from "bun:test"; +import { mkdtempSync, readFileSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { saveConfig } from "../src/config"; +import { + resetCodexModelEntitlementCacheForTests, + seedCodexModelEntitlementsForTests, +} from "../src/codex/model-entitlements"; +import { cursorProductJsonCandidates, detectCursorInstalls, type CursorDetectDeps } from "../src/integrations/cursor-detect"; +import { cursorLastSeen, recordCursorSeen, resetCursorSeenForTests } from "../src/integrations/cursor-seen"; +import { cursorEffortFamily } from "../src/server/models-capabilities"; +import { startServer } from "../src/server"; +import type { OcxConfig } from "../src/types"; +import { SERVER_BUDGET_MS } from "./helpers/test-budget"; + +setDefaultTimeout(SERVER_BUDGET_MS); + +function fakeDeps(platform: string, tree: Record, env: Record = {}): CursorDetectDeps { + return { + platform, + homedir: "/home/u", + env, + readText: path => { + const value = tree[path]; + return typeof value === "string" ? value : null; + }, + listDir: path => { + const value = tree[path]; + return Array.isArray(value) ? value : []; + }, + }; +} + +describe("detectCursorInstalls", () => { + test("tells Private Inference apart from regular Cursor by product.json nameLong on macOS", () => { + const deps = fakeDeps("darwin", { + "/Applications": ["Cursor.app", "Cursor Private Inference.app", "Xcode.app"], + "/Applications/Cursor.app/Contents/Resources/app/product.json": JSON.stringify({ nameLong: "Cursor", version: "3.18.9" }), + "/Applications/Cursor Private Inference.app/Contents/Resources/app/product.json": JSON.stringify({ nameLong: "Cursor Private Inference", version: "3.18.25" }), + }); + expect(detectCursorInstalls(deps)).toEqual([ + { build: "regular", path: "/Applications/Cursor.app", version: "3.18.9" }, + { build: "private-inference", path: "/Applications/Cursor Private Inference.app", version: "3.18.25" }, + ]); + }); + + test("looks under LOCALAPPDATA/Programs on Windows and skips malformed product.json", () => { + const deps = fakeDeps("win32", { + "C:\\Users\\u\\AppData\\Local\\Programs": ["cursor", "cursor-private-inference"], + "C:\\Users\\u\\AppData\\Local\\Programs\\cursor\\resources\\app\\product.json": "{not json", + "C:\\Users\\u\\AppData\\Local\\Programs\\cursor-private-inference\\resources\\app\\product.json": JSON.stringify({ nameLong: "Cursor Private Inference" }), + }, { LOCALAPPDATA: "C:\\Users\\u\\AppData\\Local" }); + const candidates = cursorProductJsonCandidates(deps); + expect(candidates.length).toBe(2); + const installs = detectCursorInstalls(deps); + expect(installs.map(install => install.build)).toEqual(["private-inference"]); + expect(installs[0].version).toBeNull(); + }); + + test("finds nothing when no candidate directory exists", () => { + expect(detectCursorInstalls(fakeDeps("linux", {}))).toEqual([]); + }); +}); + +describe("cursor last-seen recorder", () => { + beforeEach(() => resetCursorSeenForTests()); + afterEach(() => resetCursorSeenForTests()); + + test("records only a Cursor user agent, bounded and validated", () => { + recordCursorSeen(new Headers({ "user-agent": "curl/8.7.1" }), 1000); + expect(cursorLastSeen()).toBeNull(); + recordCursorSeen(new Headers({ "user-agent": "Cursor/3.18.25" }), 2000); + expect(cursorLastSeen()).toEqual({ at: 2000, userAgent: "Cursor/3.18.25" }); + // A padded or oversized value is not the shape Cursor sends and is ignored. + recordCursorSeen(new Headers({ "user-agent": `Cursor/${"x".repeat(60)}` }), 3000); + recordCursorSeen(new Headers({ "user-agent": "Cursor/3.18.25