Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/platform/opencode/execution-attribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -496,7 +496,7 @@ export class ExecutionAttribution {
if (finish()) failed(primary);
};
if (!input.response.ok) {
fail();
primary.http = { phase: "rejected", requestSeen: true };
return;
}
try {
Expand Down Expand Up @@ -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.");
Expand Down
119 changes: 115 additions & 4 deletions tests/execution-attribution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand All @@ -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",
Expand Down Expand Up @@ -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");
});
24 changes: 17 additions & 7 deletions tests/fixtures/opencode-v2-scripted-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" } },
Expand Down
10 changes: 9 additions & 1 deletion tests/lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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<SessionMessageInfo, { type: "assistant" }> = {
Expand Down
46 changes: 46 additions & 0 deletions tests/live-opencode-smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
Loading