Skip to content

fix(cli): agent run refuses an --adapter that differs from the token file - #2099

Merged
lilyshen0722 merged 2 commits into
mainfrom
fix/cli-run-adapter-mismatch
Oct 9, 2026
Merged

lilyshen0722 merged 2 commits into
mainfrom
fix/cli-run-adapter-mismatch

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

Fixes #2098. commonly agent run <name> --adapter X only ever chose the CLI at first-run bootstrap. Once ~/.commonly/tokens/<name>.json exists, the record's adapter runs and a different --adapter was dropped without a word, so switching an agent from Claude to Codex kept running Claude and looked like Codex misbehaving. Growth measured it on prod with two throwaway agents (CLI 0.1.85).

What changes (cli/src/commands/agent.js):

  • checkRunAdapterRequest({ record, requestedAdapter, tokenPath }), a pure check: no flag → run; same adapter (case- and whitespace-insensitive) → run; different adapter → refuse with a message naming the bound adapter, the token path, and the remedy for each path. A record with no adapter is left to the existing "Unknown adapter" error.
  • The run action calls it after the token file is loaded (or bootstrapped) and exits 1 on a refusal, before anything is spawned. The token file is not edited.
  • The option help no longer says the flag is "ignored"; it says an existing token file bound to a different adapter stops the run.

Two corrections to the issue's proposed remedy, from reading the code:

  • Plain commonly agent detach <name> also uninstalls the agent from its pod on the backend and needs a user login. For "switch CLIs", the local-only form is right, so the message says commonly agent detach <name> --force, then re-run with the Connect-page env exports and --adapter codex.
  • A daemon-managed seat already has a switch: commonly agent config <name> --adapter codex patches the registry, and daemon-supervisor.js applies it on its next pass (updates the record, returns changed, the child restarts). A foreground agent run never reads the registry, which is why it needs the first form. The message names both.

Not changed: the flag does not rewrite the token file. No fleet launch command passes --adapter to agent run (0 of the launchd plists and seat scripts on the operator host), so the refusal changes no live seat's behaviour.

Version: cli 0.1.86 → 0.1.87 (package.json + both lockfile fields).

Tests (cli/__tests__/run-adapter-mismatch.test.mjs, 6): the pure check (no flag, matching, matching with case/space, mismatch names binding + token path + both remedies and never suggests a bare detach, no-adapter record) and the wiring: the real agent run action built by registerAgent, run against a token file in a throwaway home, with process.exit mocked to throw. The mismatch exits 1 with the remedy on stderr and leaves the token file untouched. A help-text assertion pins the corrected description.

  • Mutation A, wiring disabled (if (false && !adapterRequest.ok)): exactly the wiring test fails, 5 pass.
  • Mutation B, comparison disabled (bound === requested → true): exactly the two mismatch tests fail, 4 pass.
  • Full cli suite at the pre-rebase tree: 56 suites, 1046 passed, 10 skipped. eslint src/commands/agent.js clean.

Gate: Vera.

🤖 Generated with Claude Code

https://claude.ai/code/session_013geJsV4RJMujdvpf3JV2Cv

lilyshen0722 and others added 2 commits October 9, 2026 00:29
…file

`--adapter` chose the CLI only at first-run bootstrap; once a token file
existed, the record's adapter ran and a different flag was dropped without
a word, so switching an agent from Claude to Codex kept running Claude
(#2098). The run now exits 1 with the binding, the token path and the
remedy for each path: `detach --force` and a re-bootstrap for a foreground
run, `agent config --adapter` for a daemon-managed seat. The option help
no longer says the flag is ignored.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_013geJsV4RJMujdvpf3JV2Cv
cli/src changed, so the published version must rise ("Source changed ⇒
version bumped").

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_013geJsV4RJMujdvpf3JV2Cv

@lilyshen0722 lilyshen0722 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9892141

Vera — verification gate, head 98921416

CLEARED. Base e968b39e, current with main.

The refusal is in the right place and normalises the way the layer below does

  • Placement. The guard sits after the bootstrap branch and before getAdapter and the spawn (cli/src/commands/agent.js:2746). A genuine first run still bootstraps with --adapter applied as adapterOverride, so there is no false refusal on the path the flag was built for; and bootstrapAgentRecordFromEnv throws on an unknown adapter or one absent from PATH, so a bootstrap cannot succeed into an immediate refusal.
  • Normalisation matches the consumer, which is the part I check hardest here. The predicate trims and lowercases both sides, and daemon-supervisor.js:207 reads row.runtime.adapter.trim().toLowerCase(). The two agree, so --adapter Codex against a codex binding is a match rather than a refusal — the test "a matching --adapter runs, whatever its case" pins exactly that. This repo has shipped two defects from a refusal predicate that normalised differently from the layer acting on the value; this one does not.
  • Both named remedies exist and do what the message says. detach <name> carries --force (:2826, :2828), and config <name> carries --adapter (:3026, :3028) — which writes through the API and updates the local token file, so a subsequent run --adapter <new> passes the guard. The daemon applies a registry change on its next pass (daemon-supervisor.js:209, adapterChanged). Preferring --force in the foreground remedy is right: plain detach also uninstalls from the pod, which is not what "switch adapters" should cost.

Mutations reproduce exactly as reported

mutation result
A — wiring off (if (false) in the run handler) 1 of 6 fails: "a mismatched --adapter exits 1 with the remedy before anything runs"
B — comparison off (if (true) return { ok: true }) 2 of 6 fail: that one plus "a mismatched --adapter refuses and names the binding and both remedies"

So the wiring and the predicate are separately witnessed, which is what makes the pair worth having rather than one end-to-end arm.

Version and suites

  • main 0.1.86 → head 0.1.87, bumped in package.json and in both places the lockfile records it. Registry latest is 0.1.86 and 0.1.87 is unpublished, so the publish comparison resolves to publish.
  • cli: 56 suites, 1051 passed, 10 skipped, 0 failed, at this exact head.

One residual, checked and not reachable

The message names the agent via record.agentName while the path comes from tokenFile(name). For a token file lacking agentName the message would read 'undefined' is bound to … and hand over commonly agent detach undefined --force — a wrong instruction in the one place a reader is most likely to paste it verbatim.

I censused all three saveAgentToken writers (:2575 attach, :2737 bootstrap, :3053 config) and every one carries agentName, so this is not reachable through any supported path — it needs a hand-edited or foreign token file. Stated rather than filed: name is already in scope, is what the user typed, and is what the path is keyed on, so using it would remove the only way the two can disagree.

@lilyshen0722
lilyshen0722 added this pull request to the merge queue Oct 9, 2026
Merged via the queue into main with commit e6ecfe0 Oct 9, 2026
16 checks passed
samxu01 pushed a commit that referenced this pull request Oct 9, 2026
Gate review on #2102: since #2099 (cli 0.1.87) a later --adapter against a
token file bound to another adapter is refused, so 'add --adapter codex to run
Codex' taught a refusal after the first run. Say the first run binds the CLI,
and name the two ways to switch later, matching the CLI's own error text.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018ajWuL8XiGBSHWiurjqm7B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cli: agent run silently ignores --adapter once a token file exists — switching an agent from Claude to Codex keeps running Claude

1 participant