Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: keep error codes and stop resending refusals

ogsamrat33f923604d9a
diff --git a/apps/server/src/omnirush-gateway-broker.test.ts b/apps/server/src/omnirush-gateway-broker.test.ts
index d4068b75..db0bda9b 100644
--- a/apps/server/src/omnirush-gateway-broker.test.ts
+++ b/apps/server/src/omnirush-gateway-broker.test.ts
@@ -928,6 +928,51 @@ describe("upstream stream guard with recorded Muse streams", () => {
}
});
+ test("a final refusal inside the stream ends the turn once, its code and error.omnirush in param", async () => {
+ const created = 'event: response.created\ndata: {"response":{"id":"resp_1"},"sequence_number":0,"type":"response.created"}\n\n';
+ const frames = async (frame: string) => eventPayloads((await replayStream(new TextEncoder().encode(created + frame), 32)).text);
+ type Frame = { type: string; sequence_number: number; error: { type: string; code: string; message: string; param: string; omnirush?: unknown } };
+ // The request policy cutting a relayed stream (parallel enforce).
+ const policy = { code: "request_policy_refused", retry: false };
+ const cut = `event: error\ndata: ${JSON.stringify({ type: "error", sequence_number: 3, detail: "request_policy_refused", error: { type: "invalid_request_error", code: "omnirush.ai", message: "OmniRush can't help with this request.", omnirush: policy } })}\n\n`;
+ const cutFrame = (await frames(cut))[1] as Frame;
+ // invalid_prompt: the one stream error code the engine shows and never retries (provider/error.ts).
+ expect(cutFrame).toMatchObject({ type: "error", sequence_number: 3, error: { type: "invalid_request_error", code: "invalid_prompt", message: "OmniRush can't help with this request.", omnirush: policy } });
+ // The engine's AI SDK keeps only type, code, message and param of a stream error: the rest rides in param.
+ expect(JSON.parse(cutFrame.error.param)).toEqual({ code: "omnirush.ai", detail: "request_policy_refused", omnirush: policy });
+ // An older gateway's cut (no object) and the model's own safety check end the same way.
+ const older = `event: error\ndata: ${JSON.stringify({ type: "error", sequence_number: 3, detail: "request_policy_refused", error: { type: "invalid_request_error", code: "omnirush.ai", message: "No." } })}\n\n`;
+ expect(JSON.parse(((await frames(older))[1] as Frame).error.param)).toEqual({ code: "omnirush.ai", detail: "request_policy_refused" });
+ const cyber = `event: error\ndata: ${JSON.stringify({ type: "error", sequence_number: 4, error: { type: "invalid_request", code: "cyber_policy", message: "This content was flagged for possible cybersecurity risk.", param: null } })}\n\n`;
+ const cyberFrame = (await frames(cyber))[1] as Frame;
+ expect(cyberFrame.error).toMatchObject({ code: "invalid_prompt", message: "This content was flagged for possible cybersecurity risk." });
+ expect(JSON.parse(cyberFrame.error.param)).toEqual({ code: "cyber_policy" });
+
+ // Tokens spent after the early commit: the app's copy; a timing field that spells a retry pattern is left out of param.
+ const tokens = { code: "limit_tokens", scope: "day", resets_at: "2026-10-20T00:00:00Z", retry: false, retry_after: 15000 };
+ const spent = `event: error\ndata: ${JSON.stringify({ type: "error", sequence_number: -1, error: { type: "invalid_request_error", code: "daily_grant_exhausted", message: "OmniRush could not complete this request (daily_grant_exhausted).", omnirush: tokens } })}\n\n`;
+ const spentFrame = (await frames(spent))[1] as Frame;
+ expect(spentFrame.error.code).toBe("invalid_prompt");
+ expect(spentFrame.error.message).toStartWith("omnirush.ai: you have used today's model allowance");
+ expect(JSON.parse(spentFrame.error.param)).toEqual({ code: "daily_grant_exhausted", omnirush: { code: "limit_tokens", scope: "day", resets_at: "2026-10-20T00:00:00Z", retry: false } });
+ for (const frame of [cutFrame, cyberFrame, spentFrame]) {
+ expect(/429|500|502|503|504|524|retry your request|try again/i.test(JSON.stringify(frame))).toBe(false);
+ }
+
+ // Retryable ones: a queue timeout still ends the turn with its own copy; a busy model stays retryable, its object in param.
+ const queued = { code: "queue_timeout", retry: true, retry_after: 15 };
+ const queue = `event: error\ndata: ${JSON.stringify({ type: "error", sequence_number: -1, error: { type: "server_error", code: "omni_queue_timeout", message: "x", omnirush: queued } })}\n\n`;
+ expect((await frames(queue))[1]).toMatchObject({ error: { code: "invalid_prompt", param: "omni_queue_timeout", omnirush: queued } });
+ const busy = { code: "limit_concurrency", scope: "model", retry: true };
+ const flat = `event: error\ndata: ${JSON.stringify({ error: { code: "model_concurrency_limited", message: "busy", omnirush: busy } })}\n\n`;
+ const busyFrame = (await frames(flat))[1] as Frame;
+ expect(busyFrame).toMatchObject({ type: "error", error: { code: "model_concurrency_limited", omnirush: busy } });
+ expect(JSON.parse(busyFrame.error.param)).toEqual({ omnirush: busy });
+ // A frame with its own param and nothing final keeps it.
+ const own = `event: error\ndata: ${JSON.stringify({ type: "error", sequence_number: 3, error: { type: "server_error", code: "x", message: "No.", param: "input", omnirush: busy } })}\n\n`;
+ expect((await frames(own))[1]).toMatchObject({ error: { param: "input", omnirush: busy } });
+ });
+
test("a relay error frame that was not rewritten becomes one the engine parses", async () => {
const created = 'event: response.created\ndata: {"response":{"id":"resp_1"},"sequence_number":0,"type":"response.created"}\n\n';
const relay = 'event: error\ndata: {"error":{"code":"idle_timeout","message":"Upstream stopped sending data","request_id":"req_1","type":"timeout"}}\n\n';
@@ -980,10 +1025,11 @@ describe("readable gateway errors", () => {
}
test("turn the backend's and the relay's refusal codes into copy the engine shows, keeping status and Retry-After", async () => {
- const cases: Array<{ status: number; body: unknown; headers?: Record<string, string>; code: string; copy: string }> = [
+ const cases: Array<{ status: number; body: unknown; headers?: Record<string, string>; code: string; copy: string; shown?: number }> = [
{ status: 400, body: { detail: "model_unavailable" }, code: "model_unavailable", copy: "not available on your account" },
{ status: 429, body: { detail: "model_concurrency_limited" }, headers: { "retry-after": "5" }, code: "model_concurrency_limited", copy: "Wait for one to finish" },
- { status: 429, body: { detail: "daily_grant_exhausted" }, code: "daily_grant_exhausted", copy: "today's model allowance" },
+ // Final: a 403 the engine does not retry (the gateway's 429 in x-omnirush-gateway-status).
+ { status: 429, body: { detail: "daily_grant_exhausted" }, code: "daily_grant_exhausted", copy: "today's model allowance", shown: 403 },
{ status: 502, body: { detail: "model_upstream_auth_failed" }, code: "model_upstream_auth_failed", copy: "temporarily unavailable" },
{
status: 503,
@@ -1003,7 +1049,7 @@ describe("readable gateway errors", () => {
for (const entry of cases) {
const response = await refusingBroker(entry.status, JSON.stringify(entry.body), { "content-type": "application/json", ...entry.headers })
.handle(gatewayRequest({ model: "meta-muse-spark", input: "hi", stream: true }), "responses");
- expect([entry.code, response.status]).toEqual([entry.code, entry.status]);
+ expect([entry.code, response.status]).toEqual([entry.code, entry.shown ?? entry.status]);
expect(response.headers.get("retry-after")).toBe(entry.headers?.["retry-after"] ?? null);
const payload = await response.json() as { error: { message: string; code: string } };
expect(payload.error.code).toBe(entry.code);
@@ -1056,8 +1102,10 @@ describe("readable gateway errors", () => {
const resetsAt = new Date(Date.now() + 3 * 3_600_000).toISOString();
const day = await spent({ "x-omnirush-grant-scope": "day", "x-omnirush-grant-resets-at": resetsAt });
- expect(day.status).toBe(429);
- // The engine would otherwise resend it ten times, a Retry-After apart.
+ // The engine retries any 429 after its Retry-After and never reads
+ // x-should-retry: a final refusal reaches it as a 403.
+ expect(day.status).toBe(403);
+ expect(day.headers.get("x-omnirush-gateway-status")).toBe("429");
expect(day.headers.get("x-should-retry")).toBe("false");
expect(day.headers.get("retry-after")).toBe("60");
expect(day.headers.get("x-omnirush-grant-scope")).toBe("day");
@@ -1082,6 +1130,91 @@ describe("readable gateway errors", () => {
);
});
+ test("error.omnirush passes through unchanged where the broker rewrites the error", async () => {
+ const tokens = { code: "limit_tokens", scope: "day", resets_at: "2026-10-20T00:00:00Z", retry: false, retry_after: 10800 };
+ const spent = await refusingBroker(429, JSON.stringify({ detail: "daily_grant_exhausted", error: { code: "daily_grant_exhausted", omnirush: tokens } }), {
+ "content-type": "application/json", "retry-after": "10800", "x-should-retry": "false", "x-omnirush-grant-scope": "day", "x-omnirush-grant-resets-at": tokens.resets_at,
+ }).handle(gatewayRequest({ model: "gpt-6-astra", input: "hi", stream: true }), "responses");
+ expect([spent.status, spent.headers.get("x-omnirush-gateway-status"), spent.headers.get("retry-after")]).toEqual([403, "429", "10800"]);
+ expect(spent.headers.get("x-should-retry")).toBe("false");
+ const spentError = (await spent.json() as { error: { code: string; message: string; omnirush: unknown } }).error;
+ expect(spentError.code).toBe("daily_grant_exhausted");
+ expect(spentError.message).toStartWith("omnirush.ai: today's tokens are used up.");
+ expect(spentError.omnirush).toEqual(tokens);
+
+ const policy = { code: "request_policy_refused", retry: false };
+ const refused = await refusingBroker(403, JSON.stringify({ error: { type: "invalid_request_error", code: "request_policy_refused", message: "omnirush.ai: OmniRush can't help with this request.", guide_url: "", omnirush: policy } }), {
+ "content-type": "application/json", "x-should-retry": "false",
+ }).handle(gatewayRequest({ model: "gpt-6-astra", input: "hi" }), "responses");
+ expect(refused.status).toBe(403);
+ expect(refused.headers.get("x-should-retry")).toBe("false");
+ expect((await refused.json() as { error: { omnirush: unknown } }).error.omnirush).toEqual(policy);
+
+ // Retryable limits keep their Retry-After and are not marked final.
+ const busy = { code: "limit_concurrency", scope: "account", retry: true, retry_after: 5 };
+ const concurrent = await refusingBroker(429, JSON.stringify({ detail: "too_many_concurrent_requests", error: { code: "too_many_concurrent_requests", omnirush: busy } }), {
+ "content-type": "application/json", "retry-after": "5",
+ }).handle(gatewayRequest({ model: "gpt-6-astra", input: "hi" }), "responses");
+ expect([concurrent.status, concurrent.headers.get("retry-after"), concurrent.headers.get("x-should-retry")]).toEqual([429, "5", null]);
+ expect((await concurrent.json() as { error: { omnirush: unknown } }).error.omnirush).toEqual(busy);
+ const queued = { code: "queue_timeout", retry: true, retry_after: 15 };
+ const queue = await refusingBroker(503, JSON.stringify({ detail: "omni_queue_timeout", error: { code: "omni_queue_timeout", omnirush: queued } }), {
+ "content-type": "application/json", "retry-after": "15",
+ }).handle(gatewayRequest({ model: "gpt-6-astra", input: "hi" }), "responses");
+ expect((await queue.json() as { error: { omnirush: unknown } }).error.omnirush).toEqual(queued);
+ });
+
+ test("a streamed refusal keeps error.omnirush in its 403", async () => {
+ const sse = { "content-type": "text/event-stream", "x-should-retry": "false" };
+ for (const [detail, omnirush] of [
+ ["request_policy_refused", { code: "request_policy_refused", retry: false }],
+ ["session_quality_cap", { code: "limit_session_cap", scope: "session", retry: false }],
+ ] as const) {
+ const message = "OmniRush can't help with this request.";
+ const frame = `event: error\ndata: ${JSON.stringify({ type: "error", sequence_number: -1, detail, error: { type: "invalid_request_error", code: "omnirush.ai", message, omnirush } })}\n\n`;
+ const response = await refusingBroker(200, frame, sse).handle(gatewayRequest({ model: "gpt-6-astra", input: "hi", stream: true }), "responses");
+ expect(response.status).toBe(403);
+ expect(response.headers.get("x-should-retry")).toBe("false");
+ expect(await response.json()).toEqual({ error: { message, type: "invalid_request_error", code: detail, omnirush } });
+ }
+ });
+
+ test("an error the gateway marks final is never marked for a resend", async () => {
+ // A code the broker has no copy for, final by its object or by the header.
+ const body = JSON.stringify({ error: { type: "invalid_request_error", code: "something_new", message: "No.", omnirush: { code: "something_new", retry: false } } });
+ const byObject = await refusingBroker(400, body).handle(gatewayRequest({ model: "gpt-6-astra", input: "hi" }), "responses");
+ expect(byObject.headers.get("x-should-retry")).toBe("false");
+ expect(await byObject.text()).toBe(body);
+ const byHeader = await refusingBroker(400, JSON.stringify({ detail: "model_unavailable" }), { "content-type": "application/json", "x-should-retry": "false" })
+ .handle(gatewayRequest({ model: "gpt-6-astra", input: "hi" }), "responses");
+ expect(byHeader.headers.get("x-should-retry")).toBe("false");
+ // retry: true, or no object at all (an older gateway), changes nothing.
+ const retryable = await refusingBroker(429, JSON.stringify({ detail: "rate_limited", error: { code: "rate_limited", omnirush: { code: "limit_rate", scope: "account", retry: true } } }), { "content-type": "application/json", "retry-after": "12" })
+ .handle(gatewayRequest({ model: "gpt-6-astra", input: "hi" }), "responses");
+ expect(retryable.headers.get("x-should-retry")).toBeNull();
+ const older = await refusingBroker(400, JSON.stringify({ detail: "model_unavailable" })).handle(gatewayRequest({ model: "gpt-6-astra", input: "hi" }), "responses");
+ expect(older.headers.get("x-should-retry")).toBeNull();
+ });
+
+ test("a final refusal never spells what the engine retries on, and keeps code, scope and retry", async () => {
+ // retry_after 15000 holds "500", one of the engine's retry patterns.
+ const tokens = { code: "limit_tokens", scope: "day", resets_at: "2026-10-20T00:00:00.500Z", retry: false, retry_after: 15000 };
+ const spent = await refusingBroker(429, JSON.stringify({ detail: "daily_grant_exhausted", error: { code: "daily_grant_exhausted", omnirush: tokens } }), {
+ "content-type": "application/json", "retry-after": "15000", "x-should-retry": "false", "x-omnirush-grant-scope": "day", "x-omnirush-grant-resets-at": tokens.resets_at,
+ }).handle(gatewayRequest({ model: "gpt-6-astra", input: "hi" }), "responses");
+ const text = await spent.text();
+ expect(/429|500|502|503|504|524/.test(text)).toBe(false);
+ expect((JSON.parse(text) as { error: { omnirush: unknown } }).error.omnirush).toEqual({ code: "limit_tokens", scope: "day", retry: false });
+ // The timing is still in the headers.
+ expect([spent.headers.get("retry-after"), spent.headers.get("x-omnirush-grant-resets-at")]).toEqual(["15000", tokens.resets_at]);
+ // A retryable limit keeps its 429 and every field.
+ const rate = { code: "limit_rate", scope: "account", retry: true, retry_after: 1500 };
+ const limited = await refusingBroker(429, JSON.stringify({ detail: "rate_limited", error: { code: "rate_limited", omnirush: rate } }), { "content-type": "application/json", "retry-after": "1500" })
+ .handle(gatewayRequest({ model: "gpt-6-astra", input: "hi" }), "responses");
+ expect(limited.status).toBe(429);
+ expect((await limited.json() as { error: { omnirush: unknown } }).error.omnirush).toEqual(rate);
+ });
+
test("grant copy on the user's clock: local time, then how long until it", () => {
const now = Date.parse("2026-09-30T21:00:00Z");
const headers = (scope: string, resetsAt?: string) => new Headers({
@@ -1360,7 +1493,7 @@ describe("OmniRush gateway broker: sub-agent model fallback", () => {
test("account-wide refusals, other errors and requests without the header pass through untouched", async () => {
const account = fallbackBroker(() => refuse(429, "daily_grant_exhausted"));
const refused = await account.broker.handle(gatewayRequest({ model: "gpt-6-sol", input: "a" }, subagentHeaders), "responses");
- expect(refused.status).toBe(429);
+ expect(refused.status).toBe(403);
expect(account.calls).toHaveLength(1);
expect(((await refused.json()) as { error: { code: string } }).error.code).toBe("daily_grant_exhausted");
@@ -1374,6 +1507,16 @@ describe("OmniRush gateway broker: sub-agent model fallback", () => {
expect(same.events).toEqual([]);
});
+ test("a sub-agent refusal the gateway marks final is not moved to the main model", async () => {
+ const final = fallbackBroker(() => Response.json({ detail: "model_concurrency_limited", error: { code: "model_concurrency_limited", omnirush: { code: "limit_concurrency", scope: "model", retry: false } } }, { status: 429 }));
+ expect((await final.broker.handle(gatewayRequest({ model: "gpt-6-sol", input: "a" }, subagentHeaders), "responses")).status).toBe(403);
+ expect(final.calls).toHaveLength(1);
+ const marked = fallbackBroker(() => Response.json({ detail: "model_unavailable" }, { status: 400, headers: { "x-should-retry": "false" } }));
+ expect((await marked.broker.handle(gatewayRequest({ model: "gpt-6-sol", input: "a" }, subagentHeaders), "responses")).status).toBe(400);
+ expect(marked.calls).toHaveLength(1);
+ expect(marked.events).toEqual([]);
+ });
+
test("when the main model refuses too, its error reaches the engine readably", async () => {
const { broker, calls, events } = fallbackBroker(() => refuse(400, "model_unavailable"));
const response = await broker.handle(gatewayRequest({ model: "gpt-6-sol", input: "a" }, subagentHeaders), "responses");
diff --git a/apps/server/src/omnirush-gateway-broker.ts b/apps/server/src/omnirush-gateway-broker.ts
index a745fc24..7180541e 100644
--- a/apps/server/src/omnirush-gateway-broker.ts
+++ b/apps/server/src/omnirush-gateway-broker.ts
@@ -253,6 +253,117 @@ function stringField(value: unknown): string | null {
return typeof value === "string" && value.trim() ? value.trim() : null;
}
+/**
+ * omnirush.ai's stable error object, `error.omnirush`
+ * (`{code, scope?, resets_at?, retry, retry_after?}`), as a field to spread
+ * into an error the broker rewrites: passed on exactly as it came, so the
+ * app can key on it. Empty when the gateway sent none (an older backend,
+ * or an error relayed from upstream).
+ */
+function omnirushField(error: Record<string, unknown> | null): { omnirush?: Record<string, unknown> } {
+ return error && isRecord(error.omnirush) ? { omnirush: error.omnirush } : {};
+}
+
+/** The gateway says the same request cannot succeed if sent again (`error.omnirush.retry === false`). */
+function retryRefused(error: Record<string, unknown> | null): boolean {
+ return Boolean(error && isRecord(error.omnirush) && error.omnirush.retry === false);
+}
+
+/**
+ * What the engine (1.18.32, session/retry.ts) retries an API error on: any
+ * of these in its message or response body, a 5xx, or a status the AI SDK
+ * marks retryable (408, 409, 429). It never reads `x-should-retry`.
+ */
+const ENGINE_RETRY_PATTERNS = [
+ /429|500|502|503|504|524/i,
+ /rate increased too quickly|rate limit|rate-limit|rate_limit|too many requests/i,
+ /overloaded|service unavailable|service_unavailable|service-unavailable|internal error|internal_error|internal server error|server error|server_error|server-error|provider returned error|provider_returned_error|provider-returned-error/i,
+ /terminated|fetch failed|failed to fetch|network[-_\s]error|upstream connect|connection error|connection refused|connection lost|socket connection was closed|socket hang up|reset before headers|getaddrinfo|enotfound|eai_again|econnrefused|econnreset|etimedout/i,
+ /^timeout$|\b(?:request|response|connection|network|stream|read) (?:timeout|timed out|time out)\b/i,
+ /try your request again|retry your request|resource exhausted|resource_exhausted/i,
+ /\btry again (?:later|in\b)|\b(?:currently|temporarily) at capacity\b/i,
+];
+const ENGINE_RETRY_STATUSES = new Set([408, 409, 429]);
+/** The status a final refusal reaches the engine with when its own would be retried; the gateway's is kept in this header. */
+const FINAL_REFUSAL_STATUS = 403;
+const GATEWAY_STATUS_HEADER = "x-omnirush-gateway-status";
+
+function engineWouldRetry(text: string): boolean {
+ return ENGINE_RETRY_PATTERNS.some((pattern) => pattern.test(text));
+}
+
+/**
+ * A final refusal's error body the engine shows once: `error.omnirush` is
+ * passed on as it came unless one of its timing fields (`retry_after`,
+ * `resets_at`, both also in the response headers) happens to spell a
+ * pattern the engine retries on (`retry_after: 15000` holds "500"); only
+ * that field is then left out, so `code`, `scope` and `retry` always arrive.
+ */
+function finalErrorBody(error: Record<string, unknown>): string {
+ const body = JSON.stringify({ error });
+ if (!isRecord(error.omnirush) || !engineWouldRetry(body)) return body;
+ return JSON.stringify({ error: { ...error, omnirush: withoutRetryWords(error.omnirush) } });
+}
+
+/** `error.omnirush` without a timing field that spells one of the engine's retry patterns. */
+function withoutRetryWords(omnirush: Record<string, unknown>): Record<string, unknown> {
+ const kept = { ...omnirush };
+ for (const field of ["retry_after", "resets_at"]) {
+ if (field in kept && engineWouldRetry(JSON.stringify(kept[field]))) delete kept[field];
+ }
+ return kept;
+}
+
+/**
+ * What a stream `error` frame carries that the engine would drop: its AI
+ * SDK keeps only an error's type, code, message and param, so the gateway's
+ * `detail` and `error.omnirush` ride in `param` (as JSON) when the frame has
+ * none. Null when there is nothing to carry or `param` is taken.
+ */
+function carriedParam(payload: Record<string, unknown>, nested: Record<string, unknown> | null): string | null {
+ if (stringField(nested ? nested.param : payload.param)) return null;
+ return carriedFields(payload, nested, {});
+}
+
+function carriedFields(payload: Record<string, unknown>, nested: Record<string, unknown> | null, extra: Record<string, unknown>): string | null {
+ const detail = stringField(payload.detail);
+ const omnirush = nested && isRecord(nested.omnirush) ? nested.omnirush : null;
+ if (!detail && !omnirush && Object.keys(extra).length === 0) return null;
+ const final = omnirush?.retry === false;
+ return JSON.stringify({ ...extra, ...(detail ? { detail } : {}), ...(omnirush ? { omnirush: final ? withoutRetryWords(omnirush) : omnirush } : {}) });
+}
+
+/**
+ * Stream refusals the same request meets again: the request policy cutting
+ * a stream, a capped session, a spent allowance, the model's own safety
+ * check. Inside a stream (after any output) the engine takes an error frame
+ * for a passing fault and sends the request again, up to five times.
+ */
+const FINAL_STREAM_CODES = new Set(["request_policy_refused", "session_quality_cap", "daily_grant_exhausted", "cyber_policy"]);
+
+/**
+ * A final refusal inside a stream as the frame the engine shows once and
+ * never retries (`invalid_prompt`, like the queue timeout): its message as
+ * the user would have read it, and its own code, `detail` and
+ * `error.omnirush` in `param` (JSON) for the app.
+ */
+function finalStreamEvent(payload: Record<string, unknown>, nested: Record<string, unknown> | null, code: string | null, message: string): Uint8Array {
+ const param = stringField(nested ? nested.param : payload.param);
+ const sequence = payload.sequence_number;
+ const frame = {
+ type: "error",
+ sequence_number: typeof sequence === "number" && Number.isFinite(sequence) ? sequence : -1,
+ error: {
+ type: "invalid_request_error",
+ code: "invalid_prompt",
+ message,
+ param: carriedFields(payload, nested, { code: code ?? "omnirush_refused", ...(param ? { param } : {}) }),
+ ...(nested && isRecord(nested.omnirush) ? { omnirush: withoutRetryWords(nested.omnirush) } : {}),
+ },
+ };
+ return new TextEncoder().encode(`event: error\ndata: ${JSON.stringify(frame)}\n\n`);
+}
+
/**
* An error frame the engine can show: the gateway's
* `{type:"error",sequence_number,error:{...}}`, OpenAI's flat
@@ -277,11 +388,11 @@ function stringField(value: unknown): string | null {
const TURN_ENDING_STREAM_CODES = new Set(["omni_queue_timeout"]);
const QUEUE_TIMEOUT_COPY = "omnirush.ai: every model seat is taken right now, and this turn waited in line too long. Send your message again; your place in line is kept for a few minutes.";
-function turnEndingEvent(code: string, sequence: unknown): Uint8Array {
+function turnEndingEvent(code: string, sequence: unknown, omnirush: { omnirush?: Record<string, unknown> }): Uint8Array {
const frame = {
type: "error",
sequence_number: typeof sequence === "number" && Number.isFinite(sequence) ? sequence : -1,
- error: { type: "invalid_request_error", code: "invalid_prompt", message: QUEUE_TIMEOUT_COPY, param: code },
+ error: { type: "invalid_request_error", code: "invalid_prompt", message: QUEUE_TIMEOUT_COPY, param: code, ...omnirush },
};
return new TextEncoder().encode(`event: error\ndata: ${JSON.stringify(frame)}\n\n`);
}
@@ -290,17 +401,24 @@ function errorEvent(block: Uint8Array, payload: Record<string, unknown>): Uint8A
const nested = isRecord(payload.error) ? payload.error : null;
const code = stringField(nested ? nested.code : payload.code);
const baseCode = code?.replace(/^upstream_/, "") ?? null;
- if (baseCode && TURN_ENDING_STREAM_CODES.has(baseCode)) return turnEndingEvent(baseCode, payload.sequence_number);
+ const omnirush = omnirushField(nested);
+ if (baseCode && TURN_ENDING_STREAM_CODES.has(baseCode)) return turnEndingEvent(baseCode, payload.sequence_number, omnirush);
const original = stringField(nested ? nested.message : payload.message);
const readable = code ? gatewayErrorMessage(code, original) : null;
+ const detail = stringField(payload.detail);
+ if (retryRefused(nested) || (detail && FINAL_STREAM_CODES.has(detail)) || (baseCode && FINAL_STREAM_CODES.has(baseCode))) {
+ const named = detail ?? baseCode ?? "omnirush_refused";
+ return finalStreamEvent(payload, nested, code, readable ?? original ?? `omnirush.ai could not complete the model request (${named}).`);
+ }
const sequence = payload.sequence_number;
const parsed = payload.type === "error"
&& typeof sequence === "number"
&& (nested
? typeof nested.type === "string" && typeof nested.code === "string" && typeof nested.message === "string"
: typeof payload.message === "string");
- if (parsed && !readable) return block;
- const param = stringField(nested ? nested.param : payload.param);
+ const carried = carriedParam(payload, nested);
+ if (parsed && !readable && !carried) return block;
+ const param = stringField(nested ? nested.param : payload.param) ?? carried;
const frame = {
type: "error",
sequence_number: typeof sequence === "number" && Number.isFinite(sequence) ? sequence : -1,
@@ -309,6 +427,7 @@ function errorEvent(block: Uint8Array, payload: Record<string, unknown>): Uint8A
code: code ?? "upstream_error",
message: readable ?? original ?? `omnirush.ai: the model request failed (${code ?? "upstream_error"}).`,
...(param ? { param } : {}),
+ ...omnirush,
},
};
return new TextEncoder().encode(`event: error\ndata: ${JSON.stringify(frame)}\n\n`);
@@ -533,7 +652,7 @@ export function guardEventStream(
* reads its message from. Null leaves the body as it came. `grant` is a
* spent grant's copy (grantExhaustedCopy), used for `daily_grant_exhausted`.
*/
-function readableErrorBody(text: string, grant: string | null): { body: string; code: string } | null {
+function readableErrorBody(text: string, grant: string | null): { error: Record<string, unknown>; code: string; final: boolean } | null {
let payload: unknown;
try {
payload = JSON.parse(text);
@@ -553,26 +672,46 @@ function readableErrorBody(text: string, grant: string | null): { body: string;
if (!message) return null;
const param = stringField(nested?.param);
return {
- body: JSON.stringify({
- error: { message, type: stringField(nested?.type) ?? "omnirush_error", code, ...(param ? { param } : {}) },
- }),
+ error: { message, type: stringField(nested?.type) ?? "omnirush_error", code, ...(param ? { param } : {}), ...omnirushField(nested) },
code,
+ final: retryRefused(nested),
};
}
+/** Whether a JSON error body carries `error.omnirush.retry === false`. */
+function errorBodyRefusesRetry(text: string): boolean {
+ try {
+ const payload: unknown = JSON.parse(text);
+ return isRecord(payload) && retryRefused(isRecord(payload.error) ? payload.error : null);
+ } catch {
+ return false;
+ }
+}
+
async function readableErrorResponse(response: Response, consoleUrl: string | null): Promise<Response> {
const text = await response.text().catch(() => "");
const readable = readableErrorBody(text, grantExhaustedCopy(response.headers, consoleUrl));
const headers = responseHeaders(response.headers);
// A spent grant stays spent until its reset: the engine would otherwise
// resend the request ten times, each after the 429's Retry-After (60 s).
- // An outdated app stays refused until it is updated.
- if (readable?.code === "daily_grant_exhausted" || readable?.code === UPDATE_REQUIRED_CODE || response.status === 426) {
- headers.set("x-should-retry", "false");
+ // An outdated app stays refused until it is updated. Whatever the
+ // gateway marks final (`x-should-retry: false`, `error.omnirush.retry`
+ // false) is never sent again either.
+ const final = readable?.code === "daily_grant_exhausted" || readable?.code === UPDATE_REQUIRED_CODE || response.status === 426
+ || Boolean(readable?.final) || response.headers.get("x-should-retry") === "false" || (!readable && errorBodyRefusesRetry(text));
+ if (!final) {
+ return new Response(readable ? JSON.stringify({ error: readable.error }) : text, { status: response.status, statusText: response.statusText, headers });
}
- return new Response(readable?.body ?? text, {
- status: response.status,
- statusText: response.statusText,
+ headers.set("x-should-retry", "false");
+ // The engine never reads that header and retries a 429 (or 408, 409, 5xx)
+ // whatever it says, after its Retry-After: a spent grant then sat as
+ // "retrying in 3 h". Such a final refusal goes on as a 403, like a
+ // streamed one, with the gateway's status in its own header.
+ const retriedStatus = ENGINE_RETRY_STATUSES.has(response.status) || response.status >= 500;
+ if (retriedStatus) headers.set(GATEWAY_STATUS_HEADER, String(response.status));
+ return new Response(readable ? finalErrorBody(readable.error) : text, {
+ status: retriedStatus ? FINAL_REFUSAL_STATUS : response.status,
+ statusText: retriedStatus ? "Forbidden" : response.statusText,
headers,
});
}
@@ -609,7 +748,8 @@ function streamedRefusalResponse(text: string, headers: Headers): Response | nul
const result = responseHeaders(headers);
result.set("content-type", "application/json");
result.set("x-should-retry", "false");
- return new Response(JSON.stringify({ error: { message, type: stringField(nested?.type) ?? "invalid_request_error", code: detail } }), {
+ const error = { message, type: stringField(nested?.type) ?? "invalid_request_error", code: detail, ...omnirushField(nested) };
+ return new Response(finalErrorBody(error), {
status: 403,
headers: result,
});
@@ -979,25 +1119,27 @@ const FALLBACK_MODEL_ID = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/;
type SubagentRefusal = { move: "now" | "after_retry" | "never"; reason: string };
/** The gateway error code of a refused response, read from a clone so the body stays readable. */
-async function refusalCode(response: Response): Promise<string | null> {
- if (!(response.headers.get("content-type") ?? "").includes("application/json")) return null;
+async function refusalCode(response: Response): Promise<{ code: string | null; final: boolean }> {
+ const marked = response.headers.get("x-should-retry") === "false";
+ if (!(response.headers.get("content-type") ?? "").includes("application/json")) return { code: null, final: marked };
const text = await response.clone().text().catch(() => "");
let payload: unknown;
try {
payload = JSON.parse(text);
} catch {
- return null;
+ return { code: null, final: marked };
}
- if (!isRecord(payload)) return null;
+ if (!isRecord(payload)) return { code: null, final: marked };
const nested = isRecord(payload.error) ? payload.error : null;
const code = stringField(payload.detail) ?? stringField(payload.error) ?? stringField(nested?.code) ?? stringField(payload.code);
- return code ? code.replace(/^upstream_/, "") : null;
+ return { code: code ? code.replace(/^upstream_/, "") : null, final: marked || retryRefused(nested) };
}
async function subagentRefusal(response: Response): Promise<SubagentRefusal> {
- const code = await refusalCode(response);
+ const { code, final } = await refusalCode(response);
const reason = code ?? `http_${response.status}`;
- if (code && ACCOUNT_REFUSED.has(code)) return { move: "never", reason };
+ // Marked final by the gateway: not sent again, on this model or another.
+ if (final || (code && ACCOUNT_REFUSED.has(code))) return { move: "never", reason };
if (code && SUBAGENT_MODEL_REFUSED.has(code)) return { move: "now", reason };
if ((code && SUBAGENT_MODEL_BUSY.has(code)) || SUBAGENT_BUSY_STATUSES.has(response.status)) return { move: "after_retry", reason };
return { move: "never", reason };