Skip to content

fix: support OpenCode v2 model selection - #7327

Open
amanmishra2 wants to merge 9 commits into
kunchenguid:mainfrom
amanmishra2:fm/opencode-v2-spawn-model-flag
Open

amanmishra2 wants to merge 9 commits into
kunchenguid:mainfrom
amanmishra2:fm/opencode-v2-spawn-model-flag

Conversation

@amanmishra2

@amanmishra2 amanmishra2 commented Oct 15, 2026 •

Copy link
Copy Markdown

Intent

Fix the OpenCode v2 spawn path in this repo: fm-spawn emitted opencode --model <m> --prompt <brief>; opencode 2.0.28 answers "Unrecognized flag: --model in command opencode" and exits, yet the spawn still reported success (2026-10-13).

Evidence: model_flag_for_harness (bin/fm-spawn.sh ~line 2716) emits --model for every harness including opencode, but the v2 TUI accepts no --model (the model rides global config / OPENCODE_CONFIG_CONTENT; --model remains valid on opencode run). Production workarounds used: launching without --model, and fm-control relaunch --model default. The launch readiness check did not detect the immediately-dead TUI.

What Changed

  • Probe the installed OpenCode TUI before adding --model; when the flag is unsupported or the probe is inconclusive, pass provider/model selections through OPENCODE_CONFIG_CONTENT and include a supported effort variant. Refuse selections the config cannot pin before provisioning.
  • Check launch readiness with the backend agent-state classifier and fail the spawn when the pane stays dead or the TUI dies after starting; inconclusive states remain non-blocking.
  • Document model delivery and readiness behavior, and expand fixtures and regression coverage for OpenCode 1.x and 2.x, probe fallback, unpinnable models, and launch liveness.

Risk Assessment

✅ Low: The changes are bounded to OpenCode model delivery and startup readiness; I found no source-backed defect, and the accepted backend limitation is documented.

Testing

The Test agent exceeded its invocation budget before live validation completed; no evidence was gathered for this head.

  • Outcome: ⚠️ 1 warning across 2 runs (50m49s)

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

🔧 **Review** - 1 issue found → auto-fixed (4) ✅
  • ⚠️ bin/fm-spawn.sh:4767 - The busy contract is armed as busy/source=fm-spawn before launch (bin/fm-spawn.sh:5052). If OpenCode exits before its plugin posts an idle event, this handler records failed but leaves that busy record in place. The busy classifier trusts the seed for OpenCode (bin/fm-busy-lib.sh:1055-1066), so the watcher can still classify a workerless task as busy. This affects both new failure paths: sustained dead (bin/fm-spawn.sh:4737) and alive-then-dead (bin/fm-spawn.sh:4758), which reach the handler at bin/fm-spawn.sh:6195. Clear the armed generation to unknown or retire it in this shared failure handler.

🔧 Fix applied.
2 warnings still open:

  • ⚠️ bin/fm-spawn.sh:4767 - The busy contract is armed as busy/source=fm-spawn before launch (bin/fm-spawn.sh:5052). If OpenCode exits before its plugin posts an idle event, this handler records failed but leaves that busy record in place. The busy classifier trusts the seed for OpenCode (bin/fm-busy-lib.sh:1055-1066), so the watcher can still classify a workerless task as busy. This affects both new failure paths: sustained dead (bin/fm-spawn.sh:4737) and alive-then-dead (bin/fm-spawn.sh:4758), which reach the handler at bin/fm-spawn.sh:6195. Clear the armed generation to unknown or retire it in this shared failure handler.
  • ⚠️ bin/fm-spawn.sh:2173 - The new timing overrides are not validated. FM_OPENCODE_READY_POLLS=0 at bin/fm-spawn.sh:4723 skips every state check and returns success, so a dead launch can again be reported as spawned. The other unchecked inputs are FM_OPENCODE_POLL_INTERVAL (bin/fm-spawn.sh:4724), FM_OPENCODE_READY_DEAD_CONFIRM (bin/fm-spawn.sh:4725), and FM_OPENCODE_SETTLE_POLLS (bin/fm-spawn.sh:4726); invalid values can collapse or skip their checks. FM_OPENCODE_PROBE_TIMEOUT (bin/fm-spawn.sh:2173) is also unchecked: zero disables GNU timeout's bound. Validate or fall back to safe values so these settings cannot bypass readiness or leave a hung help probe unbounded.

🔧 Fix applied.
3 warnings still open:

  • ⚠️ bin/fm-spawn.sh:4767 - The busy contract is armed as busy/source=fm-spawn before launch (bin/fm-spawn.sh:5052). If OpenCode exits before its plugin posts an idle event, this handler records failed but leaves that busy record in place. The busy classifier trusts the seed for OpenCode (bin/fm-busy-lib.sh:1055-1066), so the watcher can still classify a workerless task as busy. This affects both new failure paths: sustained dead (bin/fm-spawn.sh:4737) and alive-then-dead (bin/fm-spawn.sh:4758), which reach the handler at bin/fm-spawn.sh:6195. Clear the armed generation to unknown or retire it in this shared failure handler.
  • ⚠️ bin/fm-spawn.sh:2173 - The new timing overrides are not validated. FM_OPENCODE_READY_POLLS=0 at bin/fm-spawn.sh:4723 skips every state check and returns success, so a dead launch can again be reported as spawned. The other unchecked inputs are FM_OPENCODE_POLL_INTERVAL (bin/fm-spawn.sh:4724), FM_OPENCODE_READY_DEAD_CONFIRM (bin/fm-spawn.sh:4725), and FM_OPENCODE_SETTLE_POLLS (bin/fm-spawn.sh:4726); invalid values can collapse or skip their checks. FM_OPENCODE_PROBE_TIMEOUT (bin/fm-spawn.sh:2173) is also unchecked: zero disables GNU timeout's bound. Validate or fall back to safe values so these settings cannot bypass readiness or leave a hung help probe unbounded.
  • ⚠️ bin/fm-spawn.sh:2172 - The 3ce4a90 timing-validation round introduced a range gap: the helper accepts positive decimal strings outside Bash’s integer range. For example, FM_OPENCODE_READY_POLLS=9223372036854775808 can make the loop comparison fail, then reach the success return without checking pane state. The same gap affects FM_OPENCODE_READY_DEAD_CONFIRM at bin/fm-spawn.sh:4748,4763 and FM_OPENCODE_SETTLE_POLLS at bin/fm-spawn.sh:4749,4781. Default values outside the shell’s representable integer range before these comparisons.

🔧 Fix applied.
4 warnings still open:

  • ⚠️ bin/fm-spawn.sh:4767 - The busy contract is armed as busy/source=fm-spawn before launch (bin/fm-spawn.sh:5052). If OpenCode exits before its plugin posts an idle event, this handler records failed but leaves that busy record in place. The busy classifier trusts the seed for OpenCode (bin/fm-busy-lib.sh:1055-1066), so the watcher can still classify a workerless task as busy. This affects both new failure paths: sustained dead (bin/fm-spawn.sh:4737) and alive-then-dead (bin/fm-spawn.sh:4758), which reach the handler at bin/fm-spawn.sh:6195. Clear the armed generation to unknown or retire it in this shared failure handler.
  • ⚠️ bin/fm-spawn.sh:2173 - The new timing overrides are not validated. FM_OPENCODE_READY_POLLS=0 at bin/fm-spawn.sh:4723 skips every state check and returns success, so a dead launch can again be reported as spawned. The other unchecked inputs are FM_OPENCODE_POLL_INTERVAL (bin/fm-spawn.sh:4724), FM_OPENCODE_READY_DEAD_CONFIRM (bin/fm-spawn.sh:4725), and FM_OPENCODE_SETTLE_POLLS (bin/fm-spawn.sh:4726); invalid values can collapse or skip their checks. FM_OPENCODE_PROBE_TIMEOUT (bin/fm-spawn.sh:2173) is also unchecked: zero disables GNU timeout's bound. Validate or fall back to safe values so these settings cannot bypass readiness or leave a hung help probe unbounded.
  • ⚠️ bin/fm-spawn.sh:2172 - The 3ce4a90 timing-validation round introduced a range gap: the helper accepts positive decimal strings outside Bash’s integer range. For example, FM_OPENCODE_READY_POLLS=9223372036854775808 can make the loop comparison fail, then reach the success return without checking pane state. The same gap affects FM_OPENCODE_READY_DEAD_CONFIRM at bin/fm-spawn.sh:4748,4763 and FM_OPENCODE_SETTLE_POLLS at bin/fm-spawn.sh:4749,4781. Default values outside the shell’s representable integer range before these comparisons.
  • ⚠️ bin/fm-spawn.sh:2743 - The accepted criterion says, “an unpinnable model selection must refuse the spawn with a clear error instead of launching with OpenCodes default model.” On the no-flag path, the */ *-style check at bin/fm-spawn.sh:2743 accepts --model / (and values such as anthropic/ or /sonnet) as a provider/model ID. The serializer repeats that check at line 3001 and emits the value into agent.build.model at line 3013 instead of refusing. Require nonempty provider and model components before treating the config channel as able to pin the selection.

🔧 Fix applied.
✅ Re-checked - no issues remain.

✅ No issues found.

⚠️ **Test** - 1 warning
  • ⚠️ The dead-launch readiness scenario remains unverified. The live v2 spawn stayed active, but the isolated OpenCode primary selected a default model and autonomously attempted a host-wide search and read outside the worktree, so I stopped it. While trying to isolate the runtime, I invoked opencode service set port and opencode service stop with disposable HOME/XDG settings; a later CLI run still resolved the host's occupied default managed-service port, so I cannot confirm those service commands were confined to the lab. The lab TUI, mock provider, private tmux server, and lab home were stopped or removed. Please check the operator OpenCode service state. A safe live rerun needs a separate OpenCode runtime whose service port and model are demonstrably isolated.
  • ⚠️ live validation verdict: inconclusive (3 of 4 scenarios were driven live against the product); untested: A real OpenCode TUI that exits immediately is refused, including when readiness poll settings are zero or overflow-sized.
  • Live validation: ⚠️ inconclusive - 3 of 4 scenarios driven live against the product
Scenario Result Live Evidence
Spawn an OpenCode 2.0.28 worker with a selected model; fm-spawn reports success and the real OpenCode pane remains active. ✅ pass live Live OpenCode spawn artifact; OpenCode config resolution artifact
Run OpenCode 2.0.28 with the retired interactive --model flag; it rejects the flag with exit code 1. ✅ pass live OpenCode 2.0.28 flag rejection artifact
Request an unpinnable bare model ID through fm-spawn; it refuses with a clear error and exit code 1. ✅ pass live Live unpinnable-model refusal artifact
A real OpenCode TUI that exits immediately is refused, including when readiness poll settings are zero or overflow-sized. ⏸️ untested no I confirmed the actual v2 CLI rejects --model directly and drove a canonical fm-spawn launch that stayed alive. I also tried disposable HOME/XDG settings and a loopback model provider, but the inter…
  • bash tests/fm-spawn-dispatch-profile.test.sh

  • opencode --version and opencode --help

  • opencode --model anthropic/claude-sonnet-4-5 --prompt live-check under disposable environment settings

  • opencode debug config with OPENCODE_CONFIG_CONTENT model and variant values

  • bin/fm-spawn.sh opencode-live-z9a &lt;lab-project&gt; --scout --harness opencode --model local-fixture/echo through the private lab session

  • bin/fm-spawn.sh opencode-refuse-z9c <lab-project> --scout --harness opencode --model sonnet

  • tmux -L fm-lab list-panes -a and capture-pane for the live spawn

  • bin/fm-lab-home.sh teardown &lt;lab-home&gt; and private tmux -L fm-lab kill-server

  • ⚠️ The Test agent did not finish within its invocation budget. Reported: agent run tests timed out after 30m0s: agent last produced output 51s ago (62 observed); agent reported: codex exited: signal: terminated: 2026-10-15T02:58:40.538909Z ERROR codex_core::tools::router: error=exec_command failed: CreateProcess { message: "Rejected(&fix(watcher): make check wakes lossless via watcher-side suppression #34;/bin/zsh -lc &#39;LAB=\\\&#34;$PWD/fm-opencode-live.1Fs6RY\\\&#34;; [ -d \\\&#34;$LAB\\\&#34; ] &amp;&amp; rm -rf -- \\\&#34;$LAB\\\&#34;; [ ! -e \\\&#34;$LAB\\\&#34; ] &amp;&amp; echo removed || echo still-present; git status --short --branch&#39; rejected: rm -f style commands are not permitt.... This is a budget or provider-slowness cut, not a code failure. Re-running the same request costs another full budget, so no further attempt is made automatically. If this repository's targeted tests or evidence gathering routinely approach the default 30m0s, raise test_agent_timeout in global config. Respond with fix to spend another budget: a repair turn runs only for selected findings other than this budget cut, then validation re-runs. Or abort and retry after raising the budget.

✅ **Document** - passed

✅ No issues found.

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

✅ No issues found.

… dead launch

fm-spawn composed `opencode --model <m> --prompt <brief>` for every OpenCode
launch, but OpenCode 2.x removed `--model` from the interactive TUI: the
launched TUI exits 1 with "Unrecognized flag: --model in command opencode"
(verified on 2.0.28), and the spawn still reported success, leaving the task
with no worker at all (2026-10-13).

Compose `--model` only on a proven 1.x surface. A new bounded help probe
(opencode_supports_model_flag, the pi_supports_tui_mode shape with
fm-cursor-lib.sh's timeout discipline) keeps the flag on 1.18.32, which
advertises it, and omits it when the probe is inconclusive or the installed TUI
is 2.x. The model is never lost with the flag: it rides the launch's
OPENCODE_CONFIG_CONTENT build-agent model, the same JSON the effort variant
already used, verified through `opencode debug config` on both 2.0.28
(agents.build.model {providerID, model, variant}) and 1.18.32
(agent.build.model). The effort mapping, the model/effort axes recorded in task
meta, and every other harness launch are unchanged.

Add the missing launch-readiness gate for the canonical OpenCode launch. It
reads the backend's own agent-state classifier and refuses only on a positive
verdict: a sustained dead pane that never read alive, or a dead reading again
after the first alive (the rejected-flag shape, whose process lives just long
enough to be seen once). Inconclusive readings step aside, so backends without
a classifier and panes the classifier cannot attribute keep the behavior they
had; raw launch commands are exempt because they may legitimately be headless
one-shots. Measured live: tmux reads alive within one poll and reads dead
within 0.31s of a rejected-flag exit; Herdr 0.9.3 needs ~3.8s to register a
healthy launch (no-agent -> unknown -> live) and stays no-agent for a rejected
one, which is why the confirm window spans 12 consecutive dead reads.

Tests: the dispatch-profile suite's OpenCode cases now pin each generation, a
model that cannot be pinned, an inconclusive probe, a dead launch, a launch
that dies after starting, and the two shapes that must not be refused. The
spawn tmux fixture gains the window-inventory and pane-command knobs those
cases need, defaulting to today's behavior.
…d the gate's coverage boundary

Review-gate follow-up from the failed fix round of run
01M4XZDMKEGFG4G44Q567VJTVR (agent wall-clock limit), applied by hand on the
recovered branch.

SPAWN-004: an OpenCode model selection that can ride neither channel - no
proven --model support on the installed TUI and not a provider/model pair for
OPENCODE_CONFIG_CONTENT - now refuses the spawn before any worktree, endpoint,
or record exists, instead of launching with OpenCode's own default model while
the task record claims a different one. The old notice-and-continue behavior
was a silent model substitution. A raw launch command stays exempt, exactly as
it is for the readiness gate. The fragment builder keeps a bare id out of the
config as an invariant only; the preflight owns the message and the refusal.

SPAWN-003: the readiness gate's coverage boundary is now documented in the
gate comment and the touched docs (harness-adapters OpenCode reference,
configuration model/effort bullet, verification record) with no contract
change: on a backend whose runtime exposes no recovery-grade agent-state
classifier (zellij, orca, cmux today), and for readings the classifier cannot
attribute, the gate has no verdict and steps aside, so the spawn keeps its
pre-gate behavior. Extending verified liveness to those backends, or refusing
OpenCode launches on them, would change the supported runtime contract and
stays out of scope.

Tests: the unpinnable-model case now expects the refusal, asserting the
actionable error, no published task record, and no delivered launch command.
@amanmishra2
amanmishra2 force-pushed the fm/opencode-v2-spawn-model-flag branch from b3f63f7 to c6df660 Compare October 15, 2026 05:00
@amanmishra2 amanmishra2 changed the title fix: handle OpenCode 2.x spawn launches fix: support OpenCode v2 model selection Oct 15, 2026
@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: thank you @amanmishra2 — I read tip c6df6609 against main a24f4cb8.

Contract-class: restore. Composing --model into the OpenCode v2 TUI left a dead worker while spawn still reported success; probing the installed surface, carrying the model through OPENCODE_CONFIG_CONTENT when the flag is absent, refusing unpinnable selections, and failing closed on a sustained-dead / alive-then-dead readiness verdict restores the existing OpenCode spawn path. Timing overrides fall back safely; busy generation is retired on readiness failure.

VISION.md: aligns (fleet outlives vendor versions; contracts bind to what the vendor exposes; scripts own the probe/readiness mechanics).

This pass: attestation MATCHES; no security tip. Fork CI approved and queued so checks can run. Live validation is marked inconclusive — 3 of 4 scenarios driven, but the dead-launch readiness refusal (including zero/overflow poll settings) was not established live against a real OpenCode TUI that exits immediately.

Could you live-test that missing row when you have a moment, post the evidence on the PR (or re-raise through no-mistakes with the live table filled in)? No rush, and please don't feel any pressure — whenever it's convenient. Once that live evidence is on the tip (and CI is green), this can take the restore auto-merge path. We won't run a lab test from here.

Status: waiting-author on live evidence for the dead-launch readiness scenario.

@amanmishra2

Copy link
Copy Markdown
Author

CI workflows for the latest push are held in GitHub's "awaiting approval" state, which fork pull requests need a maintainer to approve. Could a maintainer approve the workflow runs for this PR when convenient?

Context: the earlier run's one red shard (Behavior portable serial 9) is addressed in this push. The OpenCode config serializer declared a local without initializing it, and newer Bash rejects that expansion under the script's nounset mode, so one 1.x-shaped launch exited before starting; the local is now initialized, lint is clean, and the targeted spawn suite is green (91 tests).

@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: thanks @amanmishra2 — I approved the CI and no-mistakes runs for a443de99. The delta since my last pass is just the one-line variant='' initialization in opencode_agent_config_fragment, which fits the earlier serial 9 nounset failure; the restore verdict is unchanged.

Two small things are still open before this can take the restore auto-merge path, whenever you have a moment:

  • The pipeline attestation in the PR body still points at c6df6609, because this push went straight to the branch. Re-raising through no-mistakes will refresh it for the current tip.
  • The live row from my earlier note (a real OpenCode TUI that exits immediately is refused, including zero/overflow poll settings) is still untested — that can ride the same no-mistakes run, or be posted here as evidence.

No rush at all, and thank you for sticking with this one.

…contains the `variant=''` initialization; the spawn dispatch suite, lint, and `git diff --check` pass. Serial 2 was canceled without a test verdict
@kunchenguid

kunchenguid commented Oct 16, 2026 •

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: thanks @amanmishra2 for re-raising through no-mistakes. The attestation now binds head b27c396d, so that item is done.

Where it stands now: the head no longer merges into main 7099556d. A trial merge conflicts in .agents/skills/harness-adapters/references/harness/opencode.md and tests/fixtures.sh. Because of that, GitHub has started no CI for this head, so there was nothing for me to approve. The body also says the Test agent ran out of budget before live validation, so the live row from my earlier notes is still open: a real OpenCode TUI that exits right away is refused, including the zero/overflow poll settings.

Contract-class: restore (my own read, unchanged). fm-spawn --harness opencode --model … is a documented default path, and on OpenCode 2.x it currently launches a TUI that dies on --model while the spawn still reports success. The fix carries the model through OPENCODE_CONFIG_CONTENT and refuses a dead launch. VISION.md: all rules align (the vendor rule in particular: it binds to a probed capability, not a guess). This is unchanged from my earlier pass.

What's needed from you (waiting on you):

  1. Merge or rebase onto current main and resolve the two conflicts.
  2. Live-test the dead-launch refusal and post the evidence, or let the no-mistakes Test step record it, and re-raise so the attestation follows the new head.

Once CI is green and it's mergeable, it can take the restore path.

FYI, no action needed: #7336 also rewrites model_flag_for_harness and the OpenCode launch template in bin/fm-spawn.sh (and tests/fixtures.sh), so the two will conflict. Whichever lands second will need a rebase. Issue #7423 (dropping OpenCode from the verified worker-harness lists) was closed as not planned today, so OpenCode stays a verified harness and this fix is still wanted.

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.

2 participants