A desktop coding agent with free access to frontier models.
TypeScript
fix: show streamed refusals once
ogsamrat877705eb62e1
diff --git a/apps/server/src/omnirush-gateway-broker.test.ts b/apps/server/src/omnirush-gateway-broker.test.tsindex 96bc2434..91a67305 100644--- a/apps/server/src/omnirush-gateway-broker.test.ts+++ b/apps/server/src/omnirush-gateway-broker.test.ts@@ -968,6 +968,39 @@ describe("readable gateway errors", () => {}});+ test("a streamed refusal (capped session, Windows cut-off) becomes a 403 the engine shows once, word for word", async () => {+ const sse = { "content-type": "text/event-stream", "x-should-retry": "false" };+ for (const [detail, message] of [+ ["session_quality_cap", "omnirush.ai: this session is paused: it used a lot of tokens without becoming usable. Start a new session in a project folder: https://gateway.example/console/account"],+ ["windows_cutoff", "omnirush.ai: sessions from native Windows are paused. Switch to WSL: https://omnirush.ai/console/wsl"],+ ]) {+ const frame = `event: error\ndata: ${JSON.stringify({ type: "error", sequence_number: -1, detail, error: { type: "invalid_request_error", code: "omnirush.ai", message } })}\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(response.headers.get("content-type")).toBe("application/json");+ expect(await response.json()).toEqual({ error: { message, type: "invalid_request_error", code: detail } });+ }+ });++ test("a stream marked not-to-retry that is not a single refusal frame passes unchanged", async () => {+ const sse = { "content-type": "text/event-stream", "x-should-retry": "false" };+ const created = 'event: response.created\ndata: {"response":{"id":"resp_1"},"sequence_number":0,"type":"response.created"}\n\n';+ const done = 'event: response.completed\ndata: {"response":{"id":"resp_1"},"sequence_number":1,"type":"response.completed"}\n\n';+ const streamed = await refusingBroker(200, created + done, sse).handle(gatewayRequest({ model: "gpt-6-astra", input: "hi", stream: true }), "responses");+ expect(streamed.status).toBe(200);+ expect(await streamed.text()).toBe(created + done);+ // No `detail`: an ordinary stream error, normalized as before.+ const plain = 'event: error\ndata: {"type":"error","sequence_number":4,"code":"server_error","message":"The server had an error while processing your request.","param":null}\n\n';+ const errored = await refusingBroker(200, plain, sse).handle(gatewayRequest({ model: "gpt-6-astra", input: "hi", stream: true }), "responses");+ expect(errored.status).toBe(200);+ expect(await errored.text()).toBe(plain);+ // Without the header the refusal frame streams through as before.+ const frame = `event: error\ndata: ${JSON.stringify({ type: "error", sequence_number: -1, detail: "windows_cutoff", error: { type: "invalid_request_error", code: "omnirush.ai", message: "omnirush.ai: paused." } })}\n\n`;+ const unmarked = await refusingBroker(200, frame, { "content-type": "text/event-stream" }).handle(gatewayRequest({ model: "gpt-6-astra", input: "hi", stream: true }), "responses");+ expect(unmarked.status).toBe(200);+ });+test("a spent grant says which limit ran out and when it resets, and is never resent", async () => {const spent = (headers: Record<string, string>) => refusingBroker(429, JSON.stringify({ detail: "daily_grant_exhausted" }), {"content-type": "application/json",diff --git a/apps/server/src/omnirush-gateway-broker.ts b/apps/server/src/omnirush-gateway-broker.tsindex 519792d7..3fa32100 100644--- a/apps/server/src/omnirush-gateway-broker.ts+++ b/apps/server/src/omnirush-gateway-broker.ts@@ -548,6 +548,84 @@ async function readableErrorResponse(response: Response, consoleUrl: string | nu});}+/** A streamed refusal is one small error frame; anything longer is a real stream. */+const MAX_STREAMED_REFUSAL_BYTES = 64 * 1024;++/**+ * A streamed refusal as a 403 the engine shows once: the gateway refuses a+ * capped session or a Windows cut-off with HTTP 200, one `event: error`+ * frame naming its `detail`, and `x-should-retry: false`. The engine+ * retries any stream error it does not recognise (and never sees that+ * header), so the same refusal would be sent again and again. The message+ * is kept word for word: one ending on the console link keeps the app's+ * "Open console" button. Null for anything but exactly one such frame.+ */+function streamedRefusalResponse(text: string, headers: Headers): Response | null {+ const events = text.split(/\r\n\r\n|\n\n|\r\r/).filter((block) => block.trim());+ if (events.length !== 1) return null;+ const { name, data } = sseEventFields(new TextEncoder().encode(events[0]));+ if (data === null) return null;+ let payload: unknown;+ try {+ payload = JSON.parse(data);+ } catch {+ return null;+ }+ if (!isRecord(payload) || (name !== "error" && payload.type !== "error")) return null;+ const detail = stringField(payload.detail);+ if (!detail) return null;+ const nested = isRecord(payload.error) ? payload.error : null;+ const message = stringField(nested?.message) ?? stringField(payload.message)+ ?? gatewayErrorMessage(detail) ?? `omnirush.ai could not complete the model request (${detail}).`;+ 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 } }), {+ status: 403,+ headers: result,+ });+}++/**+ * A stream marked `x-should-retry: false`: the refusal as a 403+ * (streamedRefusalResponse), or the stream's bytes unchanged.+ */+async function streamedRefusalOrBody(response: Response, body: ReadableStream<Uint8Array>): Promise<Response | ReadableStream<Uint8Array>> {+ const reader = body.getReader();+ const chunks: Uint8Array[] = [];+ let total = 0;+ let done = false;+ while (total <= MAX_STREAMED_REFUSAL_BYTES) {+ const result = await reader.read();+ if (result.done) {+ done = true;+ break;+ }+ chunks.push(result.value);+ total += result.value.byteLength;+ }+ if (done) {+ const decoder = new TextDecoder();+ const text = chunks.map((chunk) => decoder.decode(chunk, { stream: true })).join("") + decoder.decode();+ const refusal = streamedRefusalResponse(text, response.headers);+ if (refusal) return refusal;+ }+ return new ReadableStream<Uint8Array>({+ start(controller) {+ for (const chunk of chunks) controller.enqueue(chunk);+ if (done) controller.close();+ },+ async pull(controller) {+ const result = await reader.read();+ if (result.done) controller.close();+ else controller.enqueue(result.value);+ },+ cancel(reason) {+ return reader.cancel(reason);+ },+ });+}+async function boundedText(response: Response, maxBytes: number): Promise<string | null> {if (!response.body) return "";const reader = response.body.getReader();@@ -1088,11 +1166,17 @@ export class OmniRushGatewayBroker {return await readableErrorResponse(response, this.consoleUrl);}const streamed = response.ok && response.body && contentType.includes("text/event-stream");- const responseBody = streamed && response.body- ? guardEventStream(response.body, {+ let upstreamBody: ReadableStream<Uint8Array> | null = response.body;+ if (streamed && upstreamBody && response.headers.get("x-should-retry") === "false") {+ const refusal = await streamedRefusalOrBody(response, upstreamBody);+ if (refusal instanceof Response) return refusal;+ upstreamBody = refusal;+ }+ const responseBody = streamed && upstreamBody+ ? guardEventStream(upstreamBody, {onInterrupted: (reason) => this.log?.("warn", "omnirush.ai model stream interrupted", { reason, path: normalizedPath }),})- : response.body;+ : upstreamBody;return new Response(responseBody, {status: response.status,statusText: response.statusText,