Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: a refused turn is never sent again on its own

ogsamrat4282a199e225
diff --git a/apps/app/src/app/lib/good-session.ts b/apps/app/src/app/lib/good-session.ts
index f99308e1..79db442a 100644
--- a/apps/app/src/app/lib/good-session.ts
+++ b/apps/app/src/app/lib/good-session.ts
@@ -815,7 +815,10 @@ const REFUSAL = /(^|Message: )omnirush\.ai:|daily_grant_exhausted|request_policy
function errorEnding(error: Record<string, unknown>): { ended: LastTurnEnd; retryable: boolean } {
if (error.kind === "aborted") return { ended: "stopped", retryable: false };
// The gateway marking it final (`error.omnirush.retry === false`, `x-should-retry: false`) is a refusal whatever its words.
+ // Any 4xx but a timeout (408) or a rate limit (429) answers that very request: sending it again cannot help (as the CLI).
+ const status = Number(/^Status: (\d{3})$/m.exec(str(error.technicalDetails))?.[1]);
const refusal = record(error.omnirush).retry === false || error.noRetry === true
+ || (status >= 400 && status < 500 && status !== 408 && status !== 429)
|| REFUSAL.test(`${str(error.title)}\n${str(error.technicalDetails)}`) || Boolean(str(record(error.action).link));
if (/^Error type: APIError$/m.test(str(error.technicalDetails))) return { ended: "gateway", retryable: !refusal };
return { ended: "cut", retryable: !refusal && (error.kind === "provider-timeout" || error.kind === "provider-incomplete") };
diff --git a/apps/app/src/app/lib/opencode-v2-adapter.ts b/apps/app/src/app/lib/opencode-v2-adapter.ts
index e90efcdc..4ceba3cd 100644
--- a/apps/app/src/app/lib/opencode-v2-adapter.ts
+++ b/apps/app/src/app/lib/opencode-v2-adapter.ts
@@ -507,7 +507,7 @@ function mapV2Message(value: unknown, sessionID: string): V2MappedMessage | null
},
// A step the person stopped is the 1.x MessageAbortedError (stopped, never retried: good-session.ts).
...(role === "assistant" && error
- ? { error: { name: readString(error, "type") === "aborted" ? "MessageAbortedError" : "UnknownError", data: { message: errorMessage(error) } } }
+ ? { error: { name: readString(error, "type") === "aborted" ? "MessageAbortedError" : "UnknownError", data: { message: errorMessage(error), ...errorStatus(error) } } }
: {}),
},
parts,
@@ -659,6 +659,12 @@ function mapDefaultModels(value: unknown): Record<string, string> {
return defaults;
}
+/** The provider's HTTP status of a 2.x error, as the 1.x `statusCode` (good-session.ts reads it for auto retry). */
+function errorStatus(value: unknown): { statusCode?: number } {
+ const status = isRecord(value) ? readNumber(value, "status") ?? readNumber(readRecord(value, "error"), "status") : undefined;
+ return status === undefined ? {} : { statusCode: status };
+}
+
function errorMessage(value: unknown): string {
if (value instanceof Error) return value.message;
if (typeof value === "string") return value;
@@ -870,7 +876,7 @@ export function translateV2Event(
return [{ type, properties: {
...properties, sequence: readNumber(value.durable, "seq"),
...(type === "session.execution.failed" ? {
- error: { name: "UnknownError", data: { message: errorMessage(properties.error) } },
+ error: { name: "UnknownError", data: { message: errorMessage(properties.error), ...errorStatus(properties.error) } },
} : {}),
} }];
}
diff --git a/apps/app/tests/good-session.test.tsx b/apps/app/tests/good-session.test.tsx
index a8b9a70c..e6ce1ee9 100644
--- a/apps/app/tests/good-session.test.tsx
+++ b/apps/app/tests/good-session.test.tsx
@@ -609,6 +609,17 @@ describe("how the last turn ended (session_qc._completion)", () => {
expect(retryable([prompt(), assistant("a", [{ type: "text", text: "Half done" }], { finish: "length" })])).toBe(false);
});
+ test("retryable: any 4xx but 408 and 429 is final, whatever its words (as the CLI)", () => {
+ const retryable = (status: number, type = "APIError") => messageFacts([prompt(), errorMessage({
+ kind: "generic", title: "Claude Opus 5.5 isn't on your account yet", technicalDetails: `Error type: ${type}\nMessage: no\nStatus: ${status}`,
+ })]).retryable;
+ for (const status of [400, 401, 403, 404, 409, 413, 422]) expect(retryable(status)).toBe(false);
+ for (const status of [408, 429, 500, 502, 503]) expect(retryable(status)).toBe(true);
+ expect(messageFacts([prompt(), errorMessage({
+ kind: "provider-incomplete", title: "cut", technicalDetails: "Error type: UnknownError\nMessage: no\nStatus: 400",
+ })]).retryable).toBe(false);
+ });
+
test("retryable: never what the gateway marks final in error.omnirush, whatever its words", () => {
const retryable = (omnirush: Record<string, unknown>) => messageFacts([prompt(), errorMessage({
kind: "generic", title: "Something new", technicalDetails: "Error type: APIError\nStatus: 429", omnirush,
@@ -628,8 +639,8 @@ describe("how the last turn ended (session_qc._completion)", () => {
]) {
expect(retryable({ technicalDetails: details })).toBe(false);
}
- expect(retryable({ technicalDetails: "Error type: APIError\nStatus: 400", noRetry: true })).toBe(false);
- expect(retryable({ technicalDetails: "Error type: APIError\nStatus: 400" })).toBe(true);
+ expect(retryable({ technicalDetails: "Error type: APIError\nStatus: 503", noRetry: true })).toBe(false);
+ expect(retryable({ technicalDetails: "Error type: APIError\nStatus: 503" })).toBe(true);
});
test("replyOpen: the last reply has no end time and no error yet (the engine's idle comes first)", () => {
diff --git a/apps/app/tests/opencode-v2-adapter.test.ts b/apps/app/tests/opencode-v2-adapter.test.ts
index 8862189b..03898d28 100644
--- a/apps/app/tests/opencode-v2-adapter.test.ts
+++ b/apps/app/tests/opencode-v2-adapter.test.ts
@@ -627,6 +627,15 @@ describe("OpenCode v2 event translation", () => {
]);
});
+ test("a failed execution keeps the provider's HTTP status for the auto retry", () => {
+ expect(translateV2Event({
+ type: "session.execution.failed",
+ properties: { sessionID: "ses_2", error: { type: "provider.invalid-request", message: "This chat runs on gpt-6-astra.", status: 400 } },
+ }, createV2EventTranslationState())?.[0]?.properties).toMatchObject({
+ error: { name: "UnknownError", data: { message: "This chat runs on gpt-6-astra.", statusCode: 400 } },
+ });
+ });
+
test("translates captured permission events for the child session while preserving busy and idle", () => {
const state = createV2EventTranslationState();
const translated = [