Skip to content

Commit 89e70b8

Browse files
fix: keep OpenCode turns alive after rejected tools (#15)
Two failures when the model reaches for OpenCode's own tools, both on OpenCode 1.18.32. **500 on structured output.** A turn with several assistant messages was re-read through `GET /session/:id/message`. That listing also returns the user message, and OpenCode fails to encode a stored output format on it (HTTP 400, anomalyco/opencode#26929 and #40169), so any `json_schema` request where the model tried a tool became a 500. The earlier assistant messages are now fetched one by one with `GET /session/:id/message/:messageID`. If that still fails, the gateway answers from the final message instead of failing the request. The listing is kept only for the case where the event stream is down and the message ids are unknown, and the same fallback applies there. **Empty replies.** A plain permission rejection ends an OpenCode turn, and rejecting one request also rejects the rest of the session's pending requests. A model that called several tools at once could therefore stop before writing anything, and the follow-up replies for those siblings got 404. The managed backend now sets `experimental.continue_loop_on_deny`, so each rejection is handed back to the model. A 404 from a permission or question reply is logged at debug level as already answered. Attached servers keep their own config, so they need `continue_loop_on_deny` set themselves to get the second fix. ## Testing - `npm run lint`, `npm test` (332 pass). A new integration test drives a multi-message `json_schema` turn through a fake backend whose listing returns 400 when a stored message has a format, as OpenCode does. It fails on `main` and passes here. - `npm run test:live` against a managed OpenCode 1.18.32 backend: all checks pass. - Six tool-baiting prompts (read a file, list files, run `ls`, ask a question, "fix the bug", web search), each as text and as `json_schema`, on `big-pickle`: - `main`: four 500s, one empty reply. - This branch: 12 of 12 non-empty 200s, with valid JSON for every structured request.
1 parent aaf9507 commit 89e70b8

9 files changed

Lines changed: 144 additions & 19 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ Set values as environment variables or in the config file (same names; see [`con
229229
| `ALLOW_INSECURE_BACKEND_HTTP` | `false` | Allow a non-loopback `http://` server URL. |
230230
| `CONFIG_FILE` | see [First start](#first-start) | Where the config file lives (environment variable only). |
231231

232-
**Attach mode.** `OPENCODE_SERVER_URL` uses a server you run yourself. The gateway can only reject tool calls that server *asks* permission for; that happens in its own sessions and their subagents. It trusts the server's own permission config, plugins and MCP servers. If that config allows tools without asking, they will run. Use it only with a server configured like the managed one (every permission set to `"ask"`). The managed default is safer. Remote servers must use `https://` unless you set `ALLOW_INSECURE_BACKEND_HTTP`.
232+
**Attach mode.** `OPENCODE_SERVER_URL` uses a server you run yourself. The gateway can only reject tool calls that server *asks* permission for; that happens in its own sessions and their subagents. It trusts the server's own permission config, plugins and MCP servers. If that config allows tools without asking, they will run. Use it only with a server configured like the managed one: every permission set to `"ask"`, and `experimental.continue_loop_on_deny` set to `true` so a rejected tool does not end the turn with an empty reply. The managed default is safer. Remote servers must use `https://` unless you set `ALLOW_INSECURE_BACKEND_HTTP`.
233233

234234
## Deployment
235235

‎src/opencode/client.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export function createOpencodeClient({ baseUrl, username, password, fetchImpl =
6565
deleteSession: (sessionId, options) => request('DELETE', `/session/${id(sessionId)}`, options),
6666
abortSession: (sessionId, options) => request('POST', `/session/${id(sessionId)}/abort`, { timeoutMs: 5000, ...options }),
6767
messages: (sessionId, options) => request('GET', `/session/${id(sessionId)}/message`, options),
68+
message: (sessionId, messageId, options) => request('GET', `/session/${id(sessionId)}/message/${id(messageId)}`, options),
6869
/** Blocks until the assistant turn finishes; returns { info, parts }. */
6970
prompt: (sessionId, body, options) => request('POST', `/session/${id(sessionId)}/message`, { body, timeoutMs: 0, ...options }),
7071
replyPermission: (requestId, reply, message) =>

‎src/opencode/events.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,28 @@ export function createEventHub({ getClient, logger, ownsAllSessions }) {
4545
}
4646
}
4747

48+
/**
49+
* OpenCode answers 404 once a request is gone: rejecting one request of
50+
* a session also rejects the rest, and the sweep can race the event.
51+
*/
52+
const alreadyAnswered = (error) => error?.status === 404;
53+
4854
async function rejectPermission(id, sessionId) {
4955
try {
5056
await getClient().replyPermission(id, 'reject', TOOL_REJECTION_MESSAGE);
5157
logger.debug('Rejected OpenCode tool permission', { sessionId });
5258
} catch (error) {
53-
logger.warn('Failed to reject an OpenCode tool permission', { sessionId, error: error.message });
59+
if (alreadyAnswered(error)) logger.debug('OpenCode tool permission was already answered', { sessionId });
60+
else logger.warn('Failed to reject an OpenCode tool permission', { sessionId, error: error.message });
5461
}
5562
}
5663

5764
async function rejectQuestion(id, sessionId) {
5865
try {
5966
await getClient().rejectQuestion(id);
6067
} catch (error) {
61-
logger.warn('Failed to reject an OpenCode question', { sessionId, error: error.message });
68+
if (alreadyAnswered(error)) logger.debug('OpenCode question was already answered', { sessionId });
69+
else logger.warn('Failed to reject an OpenCode question', { sessionId, error: error.message });
6270
}
6371
}
6472

‎src/opencode/isolation.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ const PASSTHROUGH_ENV = [
1717
* OpenCode configuration for the managed backend. Every tool permission is
1818
* "ask" (the tool stays advertised exactly as in stock OpenCode) and the
1919
* gateway answers every ask with "reject", so tools never run.
20+
*
21+
* `continue_loop_on_deny` hands each rejection back to the model instead of
22+
* ending the turn. OpenCode rejects every other pending request of a session
23+
* along with the one answered, and without it those plain rejections stop
24+
* the turn before the model has written an answer.
2025
*/
2126
export function backendConfig() {
2227
return {
@@ -28,6 +33,7 @@ export function backendConfig() {
2833
'*': 'ask', read: 'ask', edit: 'ask', bash: 'ask', glob: 'ask', grep: 'ask', list: 'ask',
2934
task: 'ask', webfetch: 'ask', websearch: 'ask', external_directory: 'ask', skill: 'ask', lsp: 'ask',
3035
},
36+
experimental: { continue_loop_on_deny: true },
3137
};
3238
}
3339

‎src/opencode/runner.js‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@ export function createRunner({ getClient, hub, logger, agent }) {
8282
if (signal.aborted) throw signal.reason ?? error;
8383
throw mapBackendError(error);
8484
}
85-
// Several assistant messages mean OpenCode continued after a
86-
// rejected tool; without live events we cannot know, so re-read.
87-
const assistantMessages = tracker.assistantCount() > 1 || !hub.isConnected()
88-
? (await client.messages(sessionId, { signal })).filter((m) => m?.info?.role === 'assistant')
89-
: [last];
85+
const assistantMessages = await readTurn({ client, sessionId, last, ids: tracker.assistantIds(), connected: hub.isConnected(), signal });
9086
const result = summarize(assistantMessages);
9187
tracker.flushRemainder(result);
9288
return result;
@@ -98,6 +94,29 @@ export function createRunner({ getClient, hub, logger, agent }) {
9894
}
9995
}
10096

97+
/**
98+
* The turn's assistant messages. Several of them mean OpenCode continued
99+
* after a rejected tool, so the earlier ones are fetched one by one: the
100+
* session listing also returns the user message, which OpenCode fails to
101+
* encode (HTTP 400) when the prompt carried an output format. Without live
102+
* events the message ids are unknown and only the listing can find them.
103+
* If the re-read fails, the final message alone is still a valid answer.
104+
*/
105+
async function readTurn({ client, sessionId, last, ids, connected, signal }) {
106+
const earlier = ids.filter((messageId) => messageId !== last?.info?.id);
107+
if (connected && earlier.length === 0) return [last];
108+
try {
109+
if (!connected) return (await client.messages(sessionId, { signal })).filter((m) => m?.info?.role === 'assistant');
110+
const fetched = [];
111+
for (const messageId of earlier) fetched.push(await client.message(sessionId, messageId, { signal }));
112+
return [...fetched, last];
113+
} catch (error) {
114+
if (signal.aborted) throw signal.reason ?? error;
115+
logger.warn('Could not re-read the OpenCode turn; answering from its final message', { sessionId, error: error.message });
116+
return [last];
117+
}
118+
}
119+
101120
async function cleanup(client, sessionId) {
102121
for (let attempt = 0; attempt < 2; attempt += 1) {
103122
try {
@@ -169,7 +188,8 @@ function createStreamTracker(onDelta) {
169188
return {
170189
handle,
171190
flushRemainder,
172-
assistantCount: () => [...roles.values()].filter((role) => role === 'assistant').length,
191+
/** Assistant message ids in the order OpenCode created them. */
192+
assistantIds: () => [...roles].filter(([, role]) => role === 'assistant').map(([messageId]) => messageId),
173193
};
174194
}
175195

‎test/backend.test.mjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ describe('isolation', () => {
161161
assert.equal(config.permission.bash, 'ask');
162162
assert.equal(config.autoupdate, false);
163163
assert.equal(config.share, 'disabled');
164+
assert.equal(config.experimental.continue_loop_on_deny, true);
164165
});
165166

166167
it('creates and removes a private scratch tree', () => {

‎test/gateway.integration.test.mjs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,19 @@ describe('gateway over HTTP (fake OpenCode backend)', () => {
251251
assert.equal(body.usage.prompt_tokens, 24);
252252
});
253253

254+
it('joins several assistant messages of a structured-output turn', async () => {
255+
stack.fake.setBehavior(async (ctx) => {
256+
const first = await streamTurn(ctx, { text: 'Checking', settleMs: 0 });
257+
const second = await streamTurn(ctx, { text: 'Done', structured: { answer: 42 } });
258+
return { ...second, extraMessages: [first] };
259+
});
260+
const schema = { type: 'object', properties: { answer: { type: 'number' } } };
261+
const { status, body } = await chat({ response_format: { type: 'json_schema', json_schema: { name: 'a', schema } } });
262+
assert.equal(status, 200);
263+
assert.equal(body.choices[0].message.content, '{"answer":42}');
264+
assert.equal(body.usage.prompt_tokens, 24);
265+
});
266+
254267
it('maps upstream failures to OpenAI errors', async () => {
255268
stack.fake.setBehavior((ctx) => streamTurn(ctx, { error: { name: 'APIError', data: { statusCode: 429, message: 'slow down', responseHeaders: { 'retry-after': '7' } } } }));
256269
const { status, body, headers } = await chat({});

‎test/helpers/fake-opencode.mjs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ export async function startFakeOpencode({ providers = DEFAULT_PROVIDERS, pending
147147
const result = await behavior(ctx);
148148
abortWaiters.delete(sessionId);
149149
if (!result) return;
150-
if (session) session.messages.push(...(result.extraMessages || []), { info: result.info, parts: result.parts });
150+
const user = { info: { id: nextId('msg'), role: 'user', sessionID: sessionId, ...(body?.format ? { format: body.format } : {}) }, parts: [] };
151+
if (session) session.messages.push(user, ...(result.extraMessages || []), { info: result.info, parts: result.parts });
151152
sendJson(res, 200, { info: result.info, parts: result.parts });
152153
}
153154

@@ -202,8 +203,15 @@ export async function startFakeOpencode({ providers = DEFAULT_PROVIDERS, pending
202203
return sendJson(res, 200, true);
203204
}
204205
if (req.method === 'POST' && segments[2] === 'message') return handlePrompt(req, res, sessionId);
206+
if (req.method === 'GET' && segments[2] === 'message' && segments[3]) {
207+
const found = (state.sessions.get(sessionId)?.messages || []).find((m) => m?.info?.id === segments[3]);
208+
return found ? sendJson(res, 200, found) : sendJson(res, 404, { error: 'not found' });
209+
}
205210
if (req.method === 'GET' && segments[2] === 'message') {
206-
return sendJson(res, 200, state.sessions.get(sessionId)?.messages || []);
211+
// Like OpenCode 1.18, the listing fails to encode a stored output format.
212+
const messages = state.sessions.get(sessionId)?.messages || [];
213+
if (messages.some((m) => m.info?.format)) return sendJson(res, 400, { name: 'BadRequest', data: { message: 'Expected OutputFormatJsonSchema' } });
214+
return sendJson(res, 200, messages);
207215
}
208216
}
209217
return sendJson(res, 404, { error: 'not found' });

‎test/runner-events.test.mjs‎

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const assistant = (id, text, extra = {}) => ({
3535
parts: [{ type: 'reasoning', text: `think-${id}` }, { type: 'text', text }, { type: 'tool', tool: 'read' }],
3636
});
3737

38-
function setup({ prompt, messages = async () => [], createSession, deleteSession, hubOptions } = {}) {
38+
function setup({ prompt, messages = async () => [], message, createSession, deleteSession, hubOptions, logger = silentLogger } = {}) {
3939
const hub = fakeHub(hubOptions);
4040
const log = { aborted: [], deleted: [], bodies: [] };
4141
const client = {
@@ -45,10 +45,11 @@ function setup({ prompt, messages = async () => [], createSession, deleteSession
4545
return prompt({ sessionId, body, signal, emit: (event) => hub.emit(sessionId, event) });
4646
},
4747
messages,
48+
message: message || (async () => { throw new Error('unexpected single-message read'); }),
4849
abortSession: async (id) => { log.aborted.push(id); },
4950
deleteSession: deleteSession || (async (id) => { log.deleted.push(id); }),
5051
};
51-
const runner = createRunner({ getClient: () => client, hub, logger: silentLogger, agent: 'plan' });
52+
const runner = createRunner({ getClient: () => client, hub, logger, agent: 'plan' });
5253
return { runner, hub, log };
5354
}
5455

@@ -100,7 +101,8 @@ describe('runner', () => {
100101
assert.deepEqual(Object.keys(log.bodies[0]), ['model', 'agent', 'parts']);
101102
});
102103

103-
it('re-reads messages when the turn spans several assistant messages', async () => {
104+
it('re-reads the earlier assistant messages one by one when the turn spans several', async () => {
105+
const reads = [];
104106
const { runner } = setup({
105107
async prompt({ emit }) {
106108
emit({ type: 'message.updated', properties: { info: { id: 'a1', role: 'assistant' } } });
@@ -109,11 +111,16 @@ describe('runner', () => {
109111
emit({ type: 'message.updated', properties: { info: { id: 'a2', role: 'assistant' } } });
110112
emit({ type: 'message.part.updated', properties: { part: { id: 'p2', type: 'text' } } });
111113
emit({ type: 'message.part.delta', properties: { messageID: 'a2', partID: 'p2', field: 'text', delta: 'Second' } });
112-
return assistant('a2', 'Second');
114+
return assistant('a2', 'Second', { finish: 'length' });
115+
},
116+
messages: async () => { throw new BackendError('listing must not be used', { status: 400 }); },
117+
message: async (sessionId, messageId) => {
118+
reads.push([sessionId, messageId]);
119+
return assistant(messageId, 'First');
113120
},
114-
messages: async () => [{ info: { role: 'user' }, parts: [] }, assistant('a1', 'First'), assistant('a2', 'Second', { finish: 'length' }), null],
115121
});
116122
const { result, deltas } = await runCollect(runner);
123+
assert.deepEqual(reads, [['ses_1', 'a1']]);
117124
assert.deepEqual(deltas.filter(([kind]) => kind === 'text'), [['text', 'First'], ['text', '\n\nSecond']]);
118125
assert.equal(result.text, 'First\n\nSecond');
119126
assert.equal(result.reasoning, 'think-a1\n\nthink-a2');
@@ -128,6 +135,49 @@ describe('runner', () => {
128135
assert.deepEqual(deltas.sort(), [['reasoning', 'think-m'], ['text', 'fresh']]);
129136
});
130137

138+
it('answers from the final message when an earlier one cannot be read', async () => {
139+
const warnings = [];
140+
const logger = createLogger({ level: 'warn', sink: { out: () => {}, err: (line) => warnings.push(line) } });
141+
const { runner } = setup({
142+
logger,
143+
async prompt({ emit }) {
144+
emit({ type: 'message.updated', properties: { info: { id: 'a1', role: 'assistant' } } });
145+
emit({ type: 'message.updated', properties: { info: { id: 'a2', role: 'assistant' } } });
146+
return assistant('a2', 'Final');
147+
},
148+
message: async () => { throw new BackendError('OpenCode GET failed with HTTP 400', { status: 400 }); },
149+
});
150+
const { result } = await runCollect(runner);
151+
assert.equal(result.text, 'Final');
152+
assert.deepEqual(result.usage, { input: 3, output: 2, reasoning: 1, cacheRead: 1 });
153+
assert.ok(warnings.some((line) => /Could not re-read the OpenCode turn/.test(line)));
154+
});
155+
156+
it('answers from the final message when the listing fails with the event stream down', async () => {
157+
const { runner } = setup({
158+
hubOptions: { connected: false },
159+
prompt: async () => assistant('m', 'final'),
160+
messages: async () => { throw new BackendError('OpenCode GET failed with HTTP 400', { status: 400 }); },
161+
});
162+
assert.equal((await runCollect(runner)).result.text, 'final');
163+
});
164+
165+
it('does not fall back when the caller aborts during the re-read', async () => {
166+
const controller = new AbortController();
167+
const { runner } = setup({
168+
async prompt({ emit }) {
169+
emit({ type: 'message.updated', properties: { info: { id: 'a1', role: 'assistant' } } });
170+
emit({ type: 'message.updated', properties: { info: { id: 'a2', role: 'assistant' } } });
171+
return assistant('a2', 'Final');
172+
},
173+
message: async (sessionId, messageId, { signal }) => {
174+
controller.abort(new Error('client went away'));
175+
return abortable(signal);
176+
},
177+
});
178+
await assert.rejects(runCollect(runner, REQUEST, controller.signal), /client went away/);
179+
});
180+
131181
it('maps finish reasons and model errors', async () => {
132182
const run = async (info) => {
133183
const { runner } = setup({ prompt: async () => ({ info: { role: 'assistant', ...info }, parts: [] }) });
@@ -258,7 +308,7 @@ function controllableStream(signal) {
258308
};
259309
}
260310

261-
function hubSetup({ ownsAllSessions = false, pendingPermissions = async () => [], pendingQuestions = async () => [], replyPermission, openEvents } = {}) {
311+
function hubSetup({ ownsAllSessions = false, pendingPermissions = async () => [], pendingQuestions = async () => [], replyPermission, rejectQuestion, openEvents } = {}) {
262312
const streams = [];
263313
const log = { replies: [], questions: [], warnings: [] };
264314
const client = {
@@ -269,7 +319,7 @@ function hubSetup({ ownsAllSessions = false, pendingPermissions = async () => []
269319
return stream;
270320
},
271321
replyPermission: replyPermission || (async (id, reply, message) => { log.replies.push({ id, reply, message }); }),
272-
rejectQuestion: async (id) => { log.questions.push(id); },
322+
rejectQuestion: rejectQuestion || (async (id) => { log.questions.push(id); }),
273323
pendingPermissions,
274324
pendingQuestions,
275325
};
@@ -369,12 +419,30 @@ describe('event hub', () => {
369419
});
370420

371421
it('logs when a rejection fails', async (t) => {
372-
const { hub, streams, log } = hubSetup({ ownsAllSessions: true, replyPermission: async () => { throw new Error('backend down'); } });
422+
const { hub, streams, log } = hubSetup({
423+
ownsAllSessions: true,
424+
replyPermission: async () => { throw new Error('backend down'); },
425+
rejectQuestion: async () => { throw new Error('backend down'); },
426+
});
373427
t.after(() => hub.stop());
374428
hub.start();
375429
await hub.waitConnected(2000);
376430
streams[0].send({ type: 'permission.asked', properties: { id: 'p', sessionID: 's' } });
377431
await waitFor(() => log.warnings.some((line) => /Failed to reject an OpenCode tool permission/.test(line)));
432+
streams[0].send({ type: 'question.asked', properties: { id: 'q', sessionID: 's' } });
433+
await waitFor(() => log.warnings.some((line) => /Failed to reject an OpenCode question/.test(line)));
434+
});
435+
436+
it('treats a request that is already gone as answered', async (t) => {
437+
const gone = () => { throw new BackendError('OpenCode POST failed with HTTP 404', { status: 404 }); };
438+
const { hub, streams, log } = hubSetup({ ownsAllSessions: true, replyPermission: async () => gone(), rejectQuestion: async () => gone() });
439+
t.after(() => hub.stop());
440+
hub.start();
441+
await hub.waitConnected(2000);
442+
streams[0].send({ type: 'permission.asked', properties: { id: 'p', sessionID: 's' } });
443+
streams[0].send({ type: 'question.asked', properties: { id: 'q', sessionID: 's' } });
444+
await new Promise((resolve) => setTimeout(resolve, 30));
445+
assert.deepEqual(log.warnings, []);
378446
});
379447

380448
it('reconnects after the stream ends', async (t) => {

0 commit comments

Comments
 (0)