From d9d4fd07cd6d4ab7556424ff8cd1178c25711c48 Mon Sep 17 00:00:00 2001 From: Douwe de Vries Date: Wed, 28 Oct 2026 00:34:29 +0100 Subject: [PATCH] fix(opencode): preserve input authority across native HTTP retries --- .../opencode/execution-attribution.ts | 7 +- tests/execution-attribution.test.ts | 119 +++++++++++++++++- .../fixtures/opencode-v2-scripted-provider.ts | 24 ++-- tests/lifecycle.test.ts | 10 +- tests/live-opencode-smoke.test.ts | 46 +++++++ 5 files changed, 191 insertions(+), 15 deletions(-) diff --git a/src/platform/opencode/execution-attribution.ts b/src/platform/opencode/execution-attribution.ts index ca4e3481..94346a5d 100644 --- a/src/platform/opencode/execution-attribution.ts +++ b/src/platform/opencode/execution-attribution.ts @@ -43,7 +43,7 @@ type HttpResource = | Readonly<{ phase: "context"; requestSeen: false }> | Readonly<{ phase: "ended"; requestSeen: false }> | Readonly<{ - phase: "request" | "streaming" | "ended"; + phase: "request" | "rejected" | "streaming" | "ended"; requestSeen: true; }>; type Lifetime = @@ -440,7 +440,7 @@ export class ExecutionAttribution { const snapshot = this.#evidence(primary); const record = this.#session(primary.sessionID); if ( - primary.http.phase !== "request" || + (primary.http.phase !== "request" && primary.http.phase !== "rejected") || !same(nativeInputIDs(native), snapshot.inputs) ) return false; @@ -496,7 +496,7 @@ export class ExecutionAttribution { if (finish()) failed(primary); }; if (!input.response.ok) { - fail(); + primary.http = { phase: "rejected", requestSeen: true }; return; } try { @@ -557,6 +557,7 @@ export class ExecutionAttribution { if ( !record || !primary?.http.requestSeen || + primary.http.phase === "rejected" || primary.agent !== execution.agent ) throw new Error("Flow tool has no attributable primary request."); diff --git a/tests/execution-attribution.test.ts b/tests/execution-attribution.test.ts index 26986f67..1f8ec12c 100644 --- a/tests/execution-attribution.test.ts +++ b/tests/execution-attribution.test.ts @@ -226,7 +226,7 @@ test("an old assistant is rejected after a later context", async () => { }); test.each([false, true])( - "HTTP 400 preserves the native provider error and releases authority when already revoked is %s", + "HTTP 400 preserves the native provider error and denies tools when already revoked is %s", async (alreadyRevoked) => { const f = fixture(); if (alreadyRevoked) f.revoke(); @@ -265,16 +265,21 @@ test.each([false, true])( "/tmp", "shell", ), - ).toThrow("revoked epoch"); + ).toThrow("no attributable primary"); }, ); -test("a failed HTTP response does not leave the next fresh request overlapping", () => { +test("native terminal failure after rejected HTTP permits the next fresh request", () => { let current = "user-1"; const attribution = new ExecutionAttribution((candidate) => { if (candidate.id !== current) throw new Error("revoked origin"); }); - attribution.freeze(context, [user], { kind: "flow", origin: origin }, clock); + const failed = attribution.freeze( + context, + [user], + { kind: "flow", origin: origin }, + clock, + ); const request = new Request("http://localhost/failed"); attribution.request({ sessionID: sid, @@ -299,6 +304,9 @@ test("a failed HTTP response does not leave the next fresh request overlapping", current = "user-2"; }, ); + expect(current).toBe("user-1"); + attribution.retire(failed); + current = "user-2"; const nextUser: SessionMessageInfo = { ...user, id: "user-2", @@ -1385,3 +1393,106 @@ for (const outcome of ["throws", "undefined"] as const) expect(calls).toBe(1); expect(f.attribution.current(sid)).toBe(f.primary); }); + +for (const status of [400, 429, 503]) { + test(`HTTP ${status} waits for native retry evidence without granting tools`, () => { + const f = fixture(); + let failures = 0; + f.attribution.response( + { + sessionID: sid, + agent, + model, + kind: "primary", + request: f.request, + response: new Response("native error", { status }), + }, + () => { + failures++; + f.revoke(); + }, + ); + expect(failures).toBe(0); + expect(() => f.attribution.assertCurrent(f.primary)).not.toThrow(); + expect(() => + f.attribution.verify( + execution, + [user, assistant], + session, + "/tmp", + "shell", + ), + ).toThrow("no attributable primary"); + expect(() => + f.attribution.prepareReplacement(context, [user], f.primary, false), + ).toThrow("overlap"); + const retry: SessionMessageInfo = { + ...assistant, + content: [], + retry: { + attempt: 1, + at: 2, + error: { type: "provider.transport", message: "Retry" }, + }, + }; + const permit = f.attribution.prepareReplacement( + context, + [user, retry], + f.primary, + false, + ); + expect(permit.retry).toBe(true); + const next = f.attribution.freeze( + context, + [user, retry], + { kind: "flow", origin }, + clock, + permit, + ); + f.attribution.request({ + sessionID: sid, + agent, + model, + kind: "primary", + request: new Request("http://localhost/retry"), + }); + expect( + f.attribution.verify( + execution, + [user, assistant], + session, + "/tmp", + "shell", + ).primary, + ).toBe(next); + expect(() => f.attribution.assertCurrent(f.primary)).toThrow("superseded"); + }); +} + +test("a rejected HTTP request cannot revive a revoked input on native retry", () => { + const f = fixture(); + f.attribution.response( + { + sessionID: sid, + agent, + model, + kind: "primary", + request: f.request, + response: new Response("retry", { status: 503 }), + }, + () => {}, + ); + f.revoke(); + const retry: SessionMessageInfo = { + ...assistant, + content: [], + retry: { + attempt: 1, + at: 2, + error: { type: "provider.transport", message: "Retry" }, + }, + }; + expect(() => + f.attribution.prepareReplacement(context, [user, retry], f.primary, false), + ).toThrow("revoked epoch"); +}); diff --git a/tests/fixtures/opencode-v2-scripted-provider.ts b/tests/fixtures/opencode-v2-scripted-provider.ts index 05be649a..5484bec2 100644 --- a/tests/fixtures/opencode-v2-scripted-provider.ts +++ b/tests/fixtures/opencode-v2-scripted-provider.ts @@ -29,14 +29,24 @@ export function localScriptedProvider() { requests.push({ model: body.model, tools, - scenario: text?.includes("FLOW_SMOKE_PROVIDER_ERROR") - ? "error" - : text?.includes("FLOW_SMOKE_DELAY") - ? "delay" - : text?.includes("FLOW_SMOKE_ACTIONS") - ? "actions" - : "plain", + scenario: text?.includes("FLOW_SMOKE_RETRY") + ? "retry" + : text?.includes("FLOW_SMOKE_PROVIDER_ERROR") + ? "error" + : text?.includes("FLOW_SMOKE_DELAY") + ? "delay" + : text?.includes("FLOW_SMOKE_ACTIONS") + ? "actions" + : "plain", }); + if ( + text?.includes("FLOW_SMOKE_RETRY") && + requests.filter((entry) => entry.scenario === "retry").length === 2 + ) + return Response.json( + { error: { message: "Local transient error" } }, + { status: 503 }, + ); if (text?.includes("FLOW_SMOKE_PROVIDER_ERROR")) return Response.json( { error: { message: "Local scripted provider error" } }, diff --git a/tests/lifecycle.test.ts b/tests/lifecycle.test.ts index 263f4814..48a5c828 100644 --- a/tests/lifecycle.test.ts +++ b/tests/lifecycle.test.ts @@ -1519,7 +1519,7 @@ test("a delayed terminal session read cannot revoke a replacement primary", asyn } }); -test("native retry keeps its unfinished assistant and late old responses cannot revoke it", async () => { +test("HTTP retry keeps its unfinished assistant and late old responses cannot revoke it", async () => { const f = await fixture(); try { await f.command("flow-auto", "Inspect original"); @@ -1532,6 +1532,14 @@ test("native retry keeps its unfinished assistant and late old responses cannot kind: "primary", request: oldRequest, }); + await f.emit("http.response", { + sessionID, + agent, + model, + kind: "primary", + request: oldRequest, + response: new Response("transient", { status: 503 }), + }); const assistantID = SessionMessage.ID.make("msg_retry_assistant"); const callID = CallID.make("call_retry_status"); const assistant: Extract = { diff --git a/tests/live-opencode-smoke.test.ts b/tests/live-opencode-smoke.test.ts index 4bc81e35..3faea1ee 100644 --- a/tests/live-opencode-smoke.test.ts +++ b/tests/live-opencode-smoke.test.ts @@ -500,6 +500,52 @@ test.skipIf(!LIVE)( validationEvidence, ); + const retrySession = await client.session.create( + { + title: "Native HTTP retry control", + agent: "build", + model: { providerID: "flow-smoke", id: "catalog-model" }, + location: { directory: project }, + }, + request(), + ); + await client.session.command( + { + sessionID: retrySession.id, + name: "flow-auto", + text: `FLOW_SMOKE_RETRY FLOW_SMOKE_ACTIONS ${JSON.stringify([ + { tool: "flow_status", input: { request: { view: "compact" } } }, + { tool: "flow_status", input: { request: { view: "compact" } } }, + ])}`, + }, + request(), + ); + await client.session.wait({ sessionID: retrySession.id }, request()); + expect( + (await client.session.get({ sessionID: retrySession.id }, request())) + .outcome, + ).toBe("succeeded"); + expect( + provider.requests.filter((entry) => entry.scenario === "retry"), + ).toHaveLength(4); + const retryCalls = await tools(client, retrySession.id); + expect(retryCalls.map((call) => [call.name, call.state.status])).toEqual([ + ["flow_status", "completed"], + ["flow_status", "completed"], + ]); + for (const call of retryCalls) { + if (call.state.status !== "completed") + throw new Error("Retry tool did not complete"); + expect( + JSON.parse( + call.state.content + .filter((item) => item.type === "text") + .map((item) => item.text) + .join("\n"), + ), + ).toMatchObject({ status: "ok" }); + } + try { const errorSession = await client.session.create( {