Skip to content

Commit 914b86d

Browse files
committed
fix(agents): talk to opencode over the v1 session endpoints
opencode 1.18.x serve drops provider auth (auth.json keys) from LLM requests made on behalf of sessions created through the v2 API (/api/session/*): the turn 401s at the provider, the assistant message lands with finish:"error", and no error event reaches the event stream, so SDK clients stall until timeout (anomalyco/opencode#47888, reported here as Waishnav#302). The v1 endpoints (/session/*) do not have this problem: the same serve instance, providers, and auth complete turns normally. Switch the opencode runtime to the v1 client while keeping the shared server launcher: - create session via POST /session?directory=<root> - send prompt via POST /session/{id}/message with parts + agent + inline model (model refs use modelID instead of id; variant/effort still rides along and is accepted by 1.18.x) - read messages via GET /session/{id}/message (full history, no cursor pagination; reversed to chronological order) - the v1 prompt blocks until the turn completes, so the wait loop now only confirms a terminal assistant message - health probe uses the cheap session list (v1 has no /health) - permission config trimmed to the fields the v1 agent schema accepts (edit/bash/webfetch/external_directory) Verified end to end against a real opencode 1.18.29 serve instance with a zhipuai-coding-plan (auth.json) provider: agent run completes with a correct final response where the v2 path 401'd.
1 parent c34de7e commit 914b86d

5 files changed

Lines changed: 412 additions & 279 deletions

File tree

‎scripts-dbg/dbg-resume.mjs‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// dbg-resume.mjs — isolate which pool.run hangs: run the exact scenario sequence stepwise
2+
import { OpencodeLocalAgentDriver } from "../src/local-agent-opencode.ts";
3+
import { LocalAgentRuntimePool } from "../src/local-agent-runtime-pool.ts";
4+
5+
let sessionNumber = 0;
6+
const sessionHistories = new Map();
7+
const client = {
8+
session: {
9+
async list() { return { data: [] }; },
10+
async create() {
11+
sessionNumber += 1;
12+
return { data: { id: `session_${sessionNumber}` } };
13+
},
14+
async get(input) {
15+
const sid = input.path.id;
16+
return { data: { id: sid, modelID: "glm-5.2", providerID: "zhipuai-coding-plan" } };
17+
},
18+
async prompt(input) {
19+
const sid = input.path.id;
20+
const history = sessionHistories.get(sid) ?? [];
21+
const assistantMessage = {
22+
info: { role: "assistant", id: `assistant_${sid}_${history.length}`, finish: "stop", time: { created: 1, completed: 2 } },
23+
parts: [{ type: "text", text: `response:${sid}` }],
24+
};
25+
history.push({ info: { role: "user", id: `user_${history.length}` } }, assistantMessage);
26+
sessionHistories.set(sid, history);
27+
return { data: assistantMessage };
28+
},
29+
async messages(input) {
30+
const sid = input.path.id;
31+
return { data: sessionHistories.get(sid) ?? [] };
32+
},
33+
},
34+
};
35+
const driver = new OpencodeLocalAgentDriver(async () => ({ client, server: { close: () => undefined } }));
36+
const pool = new LocalAgentRuntimePool();
37+
38+
const t = (label, p) => p.then(r => console.log(`[${label}] isOk=${r.isOk()} resp=${r.isOk() ? r.value.finalResponse : JSON.stringify(r.error?.code)}`)).catch(e => console.log(`[${label}] THREW ${String(e).slice(0, 120)}`));
39+
40+
const timer = setTimeout(() => { console.log("HUNG — printing progress"); process.exit(2); }, 20000);
41+
42+
await t("1-first", pool.run(driver, { agentId: "a", provider: "opencode", workspaceRoot: "/tmp/project" }, { prompt: "first", workspaceRoot: "/tmp/project", model: "anthropic/sonnet", effort: "high" }));
43+
await t("2-second", pool.run(driver, { agentId: "b", provider: "opencode", workspaceRoot: "/tmp/project" }, { prompt: "second", workspaceRoot: "/tmp/project" }));
44+
await t("3-effort-resume", pool.run(driver, { agentId: "a", provider: "opencode", workspaceRoot: "/tmp/project" }, { prompt: "effort override", workspaceRoot: "/tmp/project", providerSessionId: "session_1", effort: "low" }));
45+
console.log("ALL DONE");
46+
clearTimeout(timer);
47+
process.exit(0);

‎scripts-dbg/dbg-resume2.mjs‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// dbg-resume2.mjs — continue stepping through the remaining test scenarios to find the hang
2+
import { OpencodeLocalAgentDriver } from "../src/local-agent-opencode.ts";
3+
import { LocalAgentRuntimePool } from "../src/local-agent-runtime-pool.ts";
4+
5+
const timer = setTimeout(() => { console.log("HUNG"); process.exit(2); }, 30000);
6+
const t = (label, p) => p.then(r => console.log(`[${label}] isOk=${r.isOk()} ${r.isOk() ? r.value.finalResponse : JSON.stringify(r.error?.code)}`));
7+
8+
// applicationError scenario
9+
let promptFailureCount = 0;
10+
const appClient = {
11+
session: {
12+
async list() { return { data: [] }; },
13+
async create() { return { data: { id: "session_app_error" } }; },
14+
async prompt() {
15+
promptFailureCount += 1;
16+
if (promptFailureCount === 1) throw new Error("server rejected invalid input");
17+
return { data: { info: { role: "assistant", id: "assistant_app_error", finish: "stop" }, parts: [{ type: "text", text: "ok" }] } };
18+
},
19+
async messages() { return { data: [{ info: { role: "assistant", id: "assistant_app_error", finish: "stop" }, parts: [{ type: "text", text: "ok" }] }] }; },
20+
},
21+
};
22+
const appPool = new LocalAgentRuntimePool();
23+
const appDriver = new OpencodeLocalAgentDriver(async () => ({ client: appClient, server: { close: () => undefined } }));
24+
await t("app-fail-1", appPool.run(appDriver, { agentId: "x", provider: "opencode", workspaceRoot: "/tmp/p" }, { prompt: "bad input", workspaceRoot: "/tmp/p" }));
25+
await t("app-recover-2", appPool.run(appDriver, { agentId: "x", provider: "opencode", workspaceRoot: "/tmp/p" }, { prompt: "valid input", workspaceRoot: "/tmp/p" }));
26+
await appPool.close();
27+
console.log("app scenario done");
28+
29+
// health-death scenario
30+
let healthAvailable = true;
31+
let hist = new Map();
32+
const hClient = {
33+
session: {
34+
async list() { if (!healthAvailable) throw new Error("down"); return { data: [] }; },
35+
async create() { return { data: { id: "session_dead" } }; },
36+
async prompt(input) {
37+
const sid = input.path.id;
38+
const m = { info: { role: "assistant", id: `a_${hist.size}`, finish: "stop" }, parts: [{ type: "text", text: "r" }] };
39+
hist.set(sid, [m]);
40+
return { data: m };
41+
},
42+
async messages(input) { return { data: hist.get(input.path.id) ?? [] }; },
43+
},
44+
};
45+
let factoryCalls = 0;
46+
const hDriver = new OpencodeLocalAgentDriver(async () => { factoryCalls += 1; healthAvailable = true; return { client: hClient, server: { close: () => undefined } }; });
47+
const hPool = new LocalAgentRuntimePool();
48+
await t("h-initial", hPool.run(hDriver, { agentId: "h", provider: "opencode", workspaceRoot: "/tmp/p" }, { prompt: "initial", workspaceRoot: "/tmp/p" }));
49+
healthAvailable = false;
50+
await t("h-dead", hPool.run(hDriver, { agentId: "h", provider: "opencode", workspaceRoot: "/tmp/p" }, { prompt: "dead", workspaceRoot: "/tmp/p" }));
51+
await t("h-recreated", hPool.run(hDriver, { agentId: "h", provider: "opencode", workspaceRoot: "/tmp/p" }, { prompt: "recreated", workspaceRoot: "/tmp/p" }));
52+
console.log("factoryCalls:", factoryCalls);
53+
await hPool.close();
54+
console.log("ALL DONE");
55+
clearTimeout(timer);
56+
process.exit(0);

0 commit comments

Comments
 (0)