Skip to content

fix(opencode): bridge factory default commands into CommandV2 for TUI launcher - #79

Merged
mdev34-lab merged 3 commits into
devfrom
arena/01a05925-alphacode
Aug 31, 2026
Merged

mdev34-lab merged 3 commits into
devfrom
arena/01a05925-alphacode

Conversation

@arena-ai-coding-agent

Copy link
Copy Markdown

Issue for this PR

Closes #78

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

The TUI command launcher reads GET /api/command, which is served by the V2 command store, but the factory init/review commands, configured commands, and MCP/skill commands only existed in the legacy Command service. The two stores were separate, so the launcher was missing the default commands.

This bridges the legacy command state into CommandV2 from the Command layer:

  • A scoped CommandV2 transform, registered when command state initializes, upserts each legacy command (name, template, description, agent, model, subtask). The transform owns only the entries it writes itself: each run removes the entries it bridged previously that are no longer present and upserts the current set. Entries contributed by other V2 producers (config and plugin command providers) are left untouched.
  • MCP templates stay lazy. Only MCP command templates are lazy promises, and reading one resolves the remote prompt. The bridge copies concrete templates as-is and leaves lazy templates unresolved in the V2 store, so command initialization does not execute MCP prompts — one slow, unavailable, or failing prompt can no longer affect command initialization. Templates are still materialized on demand when the command is actually executed (the session prompt path reads the lazy getter).
  • A configured command's model string (provider/model) is split with ModelV2.parse into the V2 model reference { id, providerID }.
  • Command.node now depends on CommandV2.node.

How did you verify your code works?

  • New test/command/bridge.test.ts covers: factory defaults in the V2 store (template content, review subtask), configured command bridging including the model mapping (id, providerID, and that no variant is invented), MCP prompt metadata bridged without executing the prompt (asserted against a real in-process MCP server that counts GetPrompt requests), and command initialization succeeding when an MCP prompt fails. The two MCP tests were verified to fail against the eager-resolving implementation, so they guard the laziness regression.
  • Ran the test/command, test/mcp, test/config, test/skill, test/acp, test/session, and test/server (v2 location) suites locally, plus the core command and config-plugin command tests: no new failures (two test/session failures are pre-existing on the base commit in this environment).
  • Typecheck clean for the changed package; oxlint and prettier clean on the changed files.

Screenshots / recordings

Not applicable — no UI change beyond the launcher listing the commands it was missing.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

mdev34-lab and others added 2 commits August 31, 2026 19:57
… launcher

The TUI command launcher reads GET /api/command, which is served by the
V2 command store, but the factory init/review commands, configured
commands, and MCP/skill commands only existed in the legacy Command
service. The two stores were never synchronized, so the launcher was
missing the default commands.

Bridge the legacy command state into CommandV2 via a scoped transform
that owns only the entries it writes itself, leaving entries contributed
by other V2 producers (config and plugin command providers) untouched.

Keep MCP template resolution lazy: only MCP command templates are lazy
promises, and reading one resolves the remote prompt. The bridge copies
concrete templates as-is and leaves lazy templates unresolved in the V2
store, so command initialization no longer executes MCP prompts and one
slow, unavailable, or failing prompt cannot fail the whole command
service. Templates are still materialized on demand when the command is
executed.

Add regression coverage for the factory defaults, configured command
bridging (including the model -> ModelV2 reference mapping), and MCP
template laziness against a real in-process MCP server.

Co-authored-by: arena-agent <[email protected]>
…mand store

The previous bridge targeted a CommandV2 compiled into the legacy app
graph, but the V2 API serves /api/command from the location layer of the
LocationServiceMap, so bridged entries were written to an instance the TUI
never reads.

Resolve the location layer for the instance's directory through the
ambient LocationServiceMap (soft lookup, so runtimes without a location
map are unaffected) and register the bridge transform on that CommandV2.
The location ref must be built exactly as the V2 location middleware
builds it, including the explicit undefined workspaceID, because the
location map keys entries by structural equality where a missing key
differs from a key set to undefined.

Add an e2e regression test proving MCP prompt commands reach
/api/command after legacy command init, and reframe the session command
execution tests around the path the TUI actually takes (command list
discovery -> SessionPrompt.command -> lazy MCP prompt resolution).

Co-authored-by: arena-agent <[email protected]>
@arena-ai-coding-agent

Copy link
Copy Markdown
Author

Addressed the review — execution path is now proven, and in the process I found (and fixed) a deeper problem: the first bridge version was writing to a CommandV2 instance the TUI never reads.

1. MCP execution path — proven

Two integration tests in test/session/prompt.test.ts (both passing) exercise the exact TUI flow — the launcher lists commands from the command endpoint, then executes the selection via POST /session/:id/command → SessionPrompt.command:

  • executes an MCP command from the command list through the session command path, resolving the prompt lazily — the MCP command (probe-mcp:review) is listed with zero GetPrompt calls (template stays lazy); executing it resolves the prompt exactly once (mcpPromptCalls == ["probe-mcp/review"]) and the MCP response text reaches the model.
  • keeps an MCP command listed when its prompt is unavailable and degrades execution to the arguments — failing GetPrompt is swallowed (existing legacy semantics), the command stays listed, and the user's arguments become the prompt instead of a silent empty command. (I deliberately did not change the error-swallowing semantics — happy to file a follow-up issue if you'd like to revisit it.)

2. Staleness / sync semantics

The bridge transform is registered inside the per-directory InstanceState init and bound to that entry's scope: when the legacy command state is invalidated or the instance is disposed, the transform unregisters and the V2 store reloads without the bridged entries — the two views rebuild together, so they can't diverge. One pre-existing caveat that affects both views equally: the command record is a snapshot at init time, so an MCP server that connects after legacy command init is not picked up until the instance state re-initializes. I've left that as-is (it predates this PR) but can file a follow-up.

3. Self-discovered: the bridge was targeting a dead store (fixed)

While writing an e2e probe I found that with the original wiring, GET /api/command never contained bridged content (e.g. lazy:my-prompt was in v1 /command but absent from /api/command). Root cause, proven with per-instance instrumentation:

  • The V2 API serves CommandV2 from the location layer of the LocationServiceMap (per-directory, RcMap-memoized), which is a different instance from the CommandV2 compiled into the legacy app graph that the first version bridged into.
  • The first version's CommandV2.node dependency therefore gave the legacy layer a private instance whose entries nothing ever read.

The fix (src/command/index.ts): the bridge now resolves LocationServiceMap.Service from the ambient context via a soft lookup (Context.getOption) — zero change to any layer composition, and runtimes without a location map (e.g. the CLI) simply don't bridge — and registers the transform on the CommandV2 of the location layer for the instance's directory, exactly as the legacy Agent service already resolves location services. Two subtleties this hinged on:

  1. The location ref must be built exactly as the V2 location middleware does, including an explicit workspaceID: undefined — the map keys entries by structural equality, where a missing key differs from a key set to undefined (verified against Equal/Hash directly). Without that, the bridge and the middleware land in different location entries.
  2. The transform is bound to the legacy state's entry scope, giving the disposal semantics described in (2).

New coverage:

  • test/server/e2e-command-bridge.test.ts — real webHandler() e2e: in-process MCP server, legacy init via v1 /command, then asserts the MCP prompt command is present in /api/command (plus a test that factory/configured commands are served). Fails against the pre-fix wiring, passes now.
  • test/command/bridge.test.ts — updated to model the location-map resolution (stub LocationServiceMap pointing at the same CommandV2 layer the assertions read); all 4 cases still pass, including lazy-template and failing-prompt isolation.

Also worth noting: in OPENCODE_FACTORY_DEFAULT mode the V2 plugin system is skipped entirely, so the bridge is the only source of init/review in the V2 store — this fix covers that mode too.

Verification: scoped tsc clean, oxlint 0 errors (0 warnings on src), test/command/ 4/4, e2e 2/2, the two new execution tests pass, and the full prompt.test.ts file is unchanged in outcome vs. base (98 pass + 2 pre-existing failures that also fail at 214c298).

… endpoint

Extend the e2e command-bridge test beyond discovery: create a session
and execute the bridged MCP command via POST /session/:id/command (the
path the TUI launcher takes), with a fake OpenAI-compatible provider so
the run completes. Asserts the MCP GetPrompt fires exactly once more
than discovery already triggered, and that the session's user message
contains the MCP prompt response rather than the empty template stored
in the V2 view.

The delta-based GetPrompt accounting is deliberate: the pre-existing v1
list response serializes the lazy MCP template getter as {} (evaluating
it), which is unrelated to this PR and out of scope.

Also trims the over-narrated bridge comments.

Co-authored-by: arena-agent <[email protected]>
@arena-ai-coding-agent

Copy link
Copy Markdown
Author

Good catch on the execution gap — the e2e now proves it. test/server/e2e-command-bridge.test.ts no longer stops at discovery; the MCP test now runs the full loop against the real webHandler():

What the test does, step by step (real server, in-process MCP server counting GetPrompt, minimal fake OpenAI-compatible provider so the run completes):

  1. MCP client connects → expect(mcp.getPromptCalls()).toBe(0) — connecting alone never resolves prompts.
  2. Discovery — v1 GET /command and v2 GET /api/command both contain lazy:my-prompt.
  3. Lets any in-flight GetPrompt settle into a baseline (see note below).
  4. Execution — POST /session (with the fake model), then POST /session/:id/command with {command: "lazy:my-prompt"} — the exact endpoint the TUI launcher calls — and expects 200 (the run completes).
  5. expect(mcp.getPromptCalls()).toBe(baseline + 1) — execution resolved the lazy MCP prompt exactly once.
  6. GET /session/:id/message — the session's user message text contains "prompt result" (the MCP response), i.e. what the model received is the expanded prompt, not the empty template that sits in the V2 view.

So the architecture is precisely the one you'd sign off: TUI → V2 metadata (discovery) → POST /session/:id/command → legacy Command → lazy MCP template → expanded prompt. The V2 template: "" is never used for execution.

One finding worth knowing about (pre-existing, out of scope, why the test uses delta-based counting): the v1 GET /command response is typed Schema.Array(Command.Info), and response-encoding evaluates the lazy MCP template getter — the response contains "template": {} and a GetPrompt is fired as a side effect of listing. That behavior exists at base 214c298 (verified), so every v1 list call in the TUI sync path has always materialized MCP prompts server-side; my test accounts for it by settling the counter into a baseline before executing and asserting exactly +1 from execution. Happy to file a follow-up issue about the v1 list side effect if you think it deserves one.

Also addressed the comment-nit: the bridge block in src/command/index.ts is trimmed (kept only the location-scoping rationale, the ownership model, the lazy-template reason, and the ref-equality gotcha).

Verification: e2e 2/2 (stable across 3 runs), bridge unit 4/4, the two session execution tests 2/2, scoped tsc clean, oxlint 0 errors.

@mdev34-lab
mdev34-lab merged commit e9fc5ea into dev Aug 31, 2026
5 of 6 checks passed
@mdev34-lab
mdev34-lab deleted the arena/01a05925-alphacode branch August 31, 2026 21:45
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.

TUI command launcher is missing the factory default init/review commands

1 participant