From 5abec03d7cfe98fb0f88b6d6be1e1dd73ff9b0d4 Mon Sep 17 00:00:00 2001 From: Egor Shatalov Date: Sat, 10 Oct 2026 18:44:33 +0300 Subject: [PATCH] fix(opencode): the model is set once, without a retry OpenCode's ACP catalog used to be read before the location's plugins had registered their providers, so a model from OpenCode Console (`opencode-go/*`, `opencode/*`) or from `opencode.json` selected right after `session/new` was refused with -32602. Subturn set the model again after each `config_option_update` for up to 5 s. OpenCode 2.0.25 reads the catalog after the plugins have loaded (anomalyco/opencode#53358, closing #52472 and #52729), so the retry has nothing left to do. It is removed from the ACP turn, together with the fake agent's `latemodel` and `nomodel` modes and their two tests; `configure` takes the client and the session id again. README names 2.0.25 as the minimum OpenCode version. Verified on OpenCode 2.0.26: typecheck and the offline suite (34 pass, 3 live skipped); `subturn status` lists `opencode-go/*` from the first catalog; live smoke + resume on `zai-coding-plan/glm-5.3-flash` and `opencode-go/muse-spark-1.3-contributor`, where the model was acknowledged 19 ms after `session/new`; the user's `opencode.db` gained no session. Claude Fable 5.1 in Claude Code. Co-Authored-By: Claude Fable 5.1 --- README.md | 2 +- src/core/acp-turn.ts | 42 ++--------------------------------------- src/plugins/fake.ts | 12 +++++------- src/plugins/opencode.ts | 10 ++-------- test/core.test.ts | 31 ------------------------------ test/fake-agent.mjs | 18 ------------------ 6 files changed, 10 insertions(+), 105 deletions(-) diff --git a/README.md b/README.md index 6f7b78d..c493e32 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ npm install -g subturn claude mcp add subturn -- subturn serve ``` -Requires Node 22.13 or newer, and OpenCode 2 or newer for that harness. Until the package is published, `npx github:eshatalov/subturn` works the same way. From a checkout, `npm install` builds `dist/` and the server is `node dist/faces/mcp.js`. +Requires Node 22.13 or newer, and OpenCode 2.0.25 or newer for that harness. Until the package is published, `npx github:eshatalov/subturn` works the same way. From a checkout, `npm install` builds `dist/` and the server is `node dist/faces/mcp.js`. ## The caller's whole manual diff --git a/src/core/acp-turn.ts b/src/core/acp-turn.ts index 58479af..b350b1b 100644 --- a/src/core/acp-turn.ts +++ b/src/core/acp-turn.ts @@ -20,13 +20,6 @@ import path from "node:path"; const INITIALIZE_TIMEOUT_MS = 30_000; const SESSION_NEW_TIMEOUT_MS = 60_000; const SESSION_LOAD_TIMEOUT_MS = 120_000; // replay of a long history takes time -const CONFIG_SETTLE_MS = 5_000; - -/** Runs `attempt`; when the harness refuses it with -32602, runs it again - * after each config_option_update, giving up CONFIG_SETTLE_MS after the - * first try. For a harness that opens a session before its option list has - * loaded and announces the rest. */ -export type RetryOnConfigUpdate = (attempt: () => Promise) => Promise; export interface AcpTurnPlan { argv: string[]; @@ -34,11 +27,7 @@ export interface AcpTurnPlan { * acknowledged-bundle record for ack.json, or null when the harness * echoes nothing. Throw to fail. Runs on resumed turns too — this is * where model+effort are re-applied explicitly. */ - configure?: ( - client: AcpClient, - sessionId: string, - retryOnConfigUpdate: RetryOnConfigUpdate, - ) => Promise | null>; + configure?: (client: AcpClient, sessionId: string) => Promise | null>; /** Extract the acknowledged bundle from the session/new response (grok: * models.currentModelId + reasoningEffort meta). */ ackFromSessionNew?: (result: Record) => Record | null; @@ -113,8 +102,6 @@ export async function runAcpTurn(ctx: LaunchContext, plan: AcpTurnPlan): Promise const usageCandidates: unknown[] = []; let activeSessionId = ""; let collecting = false; // off until the prompt goes out (replay isn't ours) - let configUpdates = 0; - let onConfigUpdate: (() => void) | null = null; const client = new AcpClient(spawned.child, { emit: (e) => ctx.emit(e), @@ -124,10 +111,6 @@ export async function runAcpTurn(ctx: LaunchContext, plan: AcpTurnPlan): Promise return; } ctx.emit({ type: collecting ? "session_update" : "session_update_replay", update }); - if (update["sessionUpdate"] === "config_option_update") { - configUpdates++; - onConfigUpdate?.(); - } if (!collecting) return; if (update["sessionUpdate"] === "agent_message_chunk") { const content = update["content"]; @@ -164,27 +147,6 @@ export async function runAcpTurn(ctx: LaunchContext, plan: AcpTurnPlan): Promise client.onExtNotification(method, (params) => handler(params, sink)); } - const nextConfigUpdate = (timeoutMs: number): Promise => - new Promise((resolve) => { - const timer = setTimeout(() => { onConfigUpdate = null; resolve(false); }, timeoutMs); - onConfigUpdate = () => { clearTimeout(timer); onConfigUpdate = null; resolve(true); }; - }); - const retryOnConfigUpdate: RetryOnConfigUpdate = async (attempt) => { - const deadline = Date.now() + CONFIG_SETTLE_MS; - for (;;) { - const seen = configUpdates; // an update landing mid-attempt counts - try { - return await attempt(); - } catch (e) { - if (!(e instanceof RpcError) || e.code !== -32602) throw e; - const left = deadline - Date.now(); - if (left <= 0) throw e; - if (configUpdates === seen && !(await nextConfigUpdate(left))) throw e; - ctx.emit({ type: "configure_retry", refused: e.message }); - } - } - }; - try { const initResult = await client.request("initialize", { protocolVersion: 1, @@ -228,7 +190,7 @@ export async function runAcpTurn(ctx: LaunchContext, plan: AcpTurnPlan): Promise ackBase = plan.ackFromSessionNew(sessionNewResult); } if (plan.configure !== undefined) { - const configureAck = await plan.configure(client, activeSessionId, retryOnConfigUpdate); + const configureAck = await plan.configure(client, activeSessionId); if (configureAck !== null) ackBase = { ...(ackBase ?? {}), ...configureAck }; } if (currentAck() !== null) { diff --git a/src/plugins/fake.ts b/src/plugins/fake.ts index 4b306c4..9eb6cc6 100644 --- a/src/plugins/fake.ts +++ b/src/plugins/fake.ts @@ -41,13 +41,11 @@ export const fakePlugin: Plugin = { } return runAcpTurn(ctx, { argv: [process.execPath, script], - configure: async (client, sessionId, retryOnConfigUpdate) => { - const modelRes = await retryOnConfigUpdate(() => - client.request( - "session/set_config_option", - { sessionId, configId: "model", value: ctx.model }, - 15_000, - ), + configure: async (client, sessionId) => { + const modelRes = await client.request( + "session/set_config_option", + { sessionId, configId: "model", value: ctx.model }, + 15_000, ); const effortRes = await client.request( "session/set_config_option", diff --git a/src/plugins/opencode.ts b/src/plugins/opencode.ts index 44395c4..0dcc471 100644 --- a/src/plugins/opencode.ts +++ b/src/plugins/opencode.ts @@ -7,12 +7,6 @@ // session/load, handled in acp-turn) runs the same configure step, so the // bundle is re-applied explicitly on every turn — never server defaults. // -// The server lists models before its plugins have added theirs (the -// OpenCode Console providers, providers from the user's config) and -// announces the rest with config_option_update about a second later. A -// model refused right after the session opens is therefore set again after -// each such update (retryOnConfigUpdate in acp-turn). -// // Posture: `opencode acp` has no --auto flag; the permissive posture is the // Subturn's ACP client itself, which answers every session/request_permission // with the most permissive option (core/acp.ts). No prompt can fire @@ -184,9 +178,9 @@ export const opencodePlugin: Plugin = { launch(ctx: LaunchContext): Promise { return runAcpTurn(ctx, { argv: [ctx.binPath, "acp"], - configure: async (client, sessionId, retryOnConfigUpdate) => { + configure: async (client, sessionId) => { // Model FIRST — the effort option appears only after the model set. - const modelRes = await retryOnConfigUpdate(() => setConfig(client, sessionId, "model", ctx.model)); + const modelRes = await setConfig(client, sessionId, "model", ctx.model); const effortRes = await setConfig(client, sessionId, "effort", ctx.effort); // The effort response is the later echo of the whole option list, so // it is the authoritative currentValue for both. diff --git a/test/core.test.ts b/test/core.test.ts index f2f4806..9538119 100644 --- a/test/core.test.ts +++ b/test/core.test.ts @@ -194,37 +194,6 @@ test("posture: a permission request is auto-answered with the most permissive op } }); -test("a model the harness announces after the session opens is set once announced", async () => { - process.env["FAKE_MODE"] = "latemodel"; - try { - const res = await spawn(baseInput()); - const done = await awaitTurn(res.session_id, 30); - assert.equal(done.status, "completed"); - assert.deepEqual(done.bundle, { model: "fake-model-1", effort: "high" }); - const events = inspect(res.session_id).events.parsed as Array>; - assert.deepEqual( - events.filter((e) => e["type"] === "configure_retry").map((e) => e["refused"]), - ["model not found: fake-model-1"], - ); - } finally { - delete process.env["FAKE_MODE"]; - } -}); - -test("a model the harness never announces fails with the harness's refusal", async () => { - process.env["FAKE_MODE"] = "nomodel"; - try { - const res = await spawn(baseInput()); - const done = await awaitTurn(res.session_id, 30); - assert.equal(done.status, "failed"); - const evidence = inspect(res.session_id); - assert.ok(evidence.error.text?.includes("-32602")); - assert.ok(evidence.error.text?.includes("model not found: fake-model-1")); - } finally { - delete process.env["FAKE_MODE"]; - } -}); - test("configure failure after the id exists: failed turn with raw rpc evidence", async () => { process.env["FAKE_MODE"] = "badeffort"; try { diff --git a/test/fake-agent.mjs b/test/fake-agent.mjs index 8430f19..66789ab 100644 --- a/test/fake-agent.mjs +++ b/test/fake-agent.mjs @@ -4,9 +4,6 @@ // permission — asks for permission mid-prompt; completes only when allowed // hang — accepts the prompt request and never answers (deadline test) // badeffort — refuses the "effort" set_config with -32602 -// latemodel — refuses the "model" set_config with -32602 until it has -// announced its models with a config_option_update -// nomodel — refuses the "model" set_config with -32602, announces nothing // grok — never answers the prompt request; completes via the private // _x.ai/session/prompt_complete notification (race test) // die — exits immediately (launch-failure-before-session-id test) @@ -27,7 +24,6 @@ if (mode === "die") { let buf = ""; const config = { model: null, effort: null }; let loadedSessionId = null; -let modelsAnnounced = false; let pendingPermissionId = null; let pendingPromptId = null; let promptSessionId = null; @@ -94,20 +90,6 @@ function handle(msg) { send({ jsonrpc: "2.0", id, error: { code: -32602, message: `invalid effort: ${params.value}` } }); return; } - if ((mode === "latemodel" || mode === "nomodel") && params.configId === "model" && !modelsAnnounced) { - send({ jsonrpc: "2.0", id, error: { code: -32602, message: `model not found: ${params.value}` } }); - if (mode === "latemodel") { - setTimeout(() => { - modelsAnnounced = true; - send({ - jsonrpc: "2.0", - method: "session/update", - params: { sessionId: params.sessionId, update: { sessionUpdate: "config_option_update", configOptions: [] } }, - }); - }, 50); - } - return; - } config[params.configId] = params.value; result(id, { configId: params.configId, currentValue: params.value }); return;