A desktop coding agent with free access to frontier models.
TypeScript
fix(gateway): correlate retries and synthesized errors
Task Builder7d73b25fd01e
diff --cc HANDOFF-gui-113.mdindex 00000000,00000000..e2c5d15bnew file mode 100644--- /dev/null+++ b/HANDOFF-gui-113.md@@@ -1,0 -1,0 +1,13 @@@++# GUI #113 review fixes++++Merged current main while preserving parent-session forwarding and the streamed++refusal path. The broker now creates one validated client request ID per++incoming model request, forwards it on every model attempt, returns it in++synthetic 503 responses and response headers, includes it in interruption SSE++errors, and attaches it to retry/fallback logs and events. The original supplied++ID remains stable across attachment, auth, connection and sub-agent retries.++++Verification: bun test apps/server/src/omnirush-gateway-broker.test.ts — 66++passed, 0 failed, 1 existing recorded-stream test skipped because++OMNIRUSH_RECORDED_SSE_DIR was not provided. git diff --check passed. Not++deployed or released.diff --cc apps/server/src/omnirush-gateway-broker.test.tsindex 0cfd0a7c,e0b88b1f..5fcc7a03--- a/apps/server/src/omnirush-gateway-broker.test.ts+++ b/apps/server/src/omnirush-gateway-broker.test.ts@@@ -360,7 -343,7 +358,10 @@@ describe("upstream stream guard", () =test("appends an error event when the upstream closes before a terminal event", async () => {const partial = 'event: response.function_call_arguments.delta\ndata: {"type":"response.function_call_arguments.delta","delta":"{\\"patch"}\n\n';-- const response = await sseBroker([created, partial]).handle(gatewayRequest({ model: "gpt-6-astra", input: "hi", stream: true }), "responses");++ const response = await sseBroker([created, partial]).handle(gatewayRequest(++ { model: "gpt-6-astra", input: "hi", stream: true },++ { "x-omnirush-client-request-id": "stream-correlation-1" },++ ), "responses");const text = await readAll(response);expect(text.startsWith(created + partial)).toBe(true);const tailEvent = text.slice((created + partial).length);@@@ -369,6 -352,6 +370,8 @@@expect(payload.type).toBe("error");expect(payload.error.code).toBe("upstream_stream_interrupted");expect(payload.error.message).toContain("omnirush.ai");++ expect((payload.error as { client_request_id?: string }).client_request_id).toBe("stream-correlation-1");++ expect(response.headers.get("x-omnirush-client-request-id")).toBe("stream-correlation-1");});test("treats response.failed and response.incomplete as terminal", async () => {@@@ -1366,9 -1406,9 +1431,12 @@@ describe("OmniRush gateway broker: connconst response = await broker.handle(request(), "responses");expect(response.status).toBe(503);expect(response.headers.get("retry-after")).toBe("2");-- const body = (await response.json()) as { error: { code: string; message: string } };++ const body = (await response.json()) as { error: { code: string; message: string; client_request_id: string } };expect(body.error.code).toBe("gateway_unreachable");expect(body.error.message).toContain("could not be reached (UND_ERR_SOCKET)");++ expect(body.error.message).toContain("request id:");++ expect(body.error.client_request_id).toMatch(/^[A-Za-z0-9]{32}$/);++ expect(response.headers.get("x-omnirush-client-request-id")).toBe(body.error.client_request_id);});test("a refresh that cannot reach omnirush.ai keeps the session and answers every waiting request with a retryable 503", async () => {diff --cc apps/server/src/omnirush-gateway-broker.tsindex 161ab10d,62319788..34c3e001--- a/apps/server/src/omnirush-gateway-broker.ts+++ b/apps/server/src/omnirush-gateway-broker.ts@@@ -40,6 -41,6 +41,7 @@@ export type SubagentModelFallbackEventstatus: number;/** Whether the main model answered. */ok: boolean;++ clientRequestId?: string;at: number;};@@@ -132,14 -133,14 +134,14 @@@ export type StreamInterruption = "truncconst LF = 0x0a;const CR = 0x0d;--function interruptedEvent(reason: StreamInterruption): Uint8Array {++function interruptedEvent(reason: StreamInterruption, requestId?: string): Uint8Array {const message = reason === "idle"? "omnirush.ai: the model stream stalled and was closed. Retry the request.": "omnirush.ai: the model stream ended before the response completed. Retry the request.";const payload = {type: "error",sequence_number: -1,-- error: { type: "server_error", code: "upstream_stream_interrupted", message },++ error: { type: "server_error", code: "upstream_stream_interrupted", message, ...(requestId ? { client_request_id: requestId } : {}) },};return new TextEncoder().encode(`event: error\ndata: ${JSON.stringify(payload)}\n\n`);}@@@ -434,14 -460,14 +461,14 @@@ class SseEventFramerexport function guardEventStream(body: ReadableStream<Uint8Array>,-- options: { idleMs?: number; onInterrupted?: (reason: StreamInterruption) => void } = {},++ options: { idleMs?: number; requestId?: string; onInterrupted?: (reason: StreamInterruption) => void } = {},): ReadableStream<Uint8Array> {const reader = body.getReader();const idleMs = options.idleMs ?? STREAM_IDLE_TIMEOUT_MS;const events = new SseEventFramer();const interrupt = (controller: ReadableStreamDefaultController<Uint8Array>, reason: StreamInterruption) => {options.onInterrupted?.(reason);-- controller.enqueue(interruptedEvent(reason));++ controller.enqueue(interruptedEvent(reason, options.requestId));controller.close();};const read = async () => {@@@ -610,6 -714,6 +715,13 @@@ function bearerToken(request: Request)return authorization.startsWith("Bearer ") ? authorization.slice(7).trim() : "";}++function clientRequestId(request: Request): string {++ const supplied = request.headers.get("x-omnirush-client-request-id")?.trim();++ return supplied && /^[A-Za-z0-9._:-]{1,128}$/.test(supplied)++ ? supplied++ : randomUUID().replaceAll("-", "");++}++function secureEqual(left: string, right: string): boolean {const a = Buffer.from(left);const b = Buffer.from(right);@@@ -838,10 -950,10 +958,11 @@@ function fetchFailure(error: unknown):};}--function unreachableResponse(code: string, message: string): Response {-- return new Response(JSON.stringify({ error: { message, type: "omnirush_error", code } }), {++function unreachableResponse(code: string, message: string, requestId: string): Response {++ const body = message + " (request id: " + requestId + ")";++ return new Response(JSON.stringify({ error: { message: body, type: "omnirush_error", code, client_request_id: requestId } }), {status: 503,-- headers: { "content-type": "application/json", "retry-after": "2" },++ headers: { "content-type": "application/json", "retry-after": "2", "x-omnirush-client-request-id": requestId },});}const SUBAGENT_RETRY_DELAY_MS = 1_500;@@@ -912,7 -1024,7 +1033,7 @@@ function pause(ms: number, signal: Abor});}--function responseHeaders(headers: Headers): Headers {++function responseHeaders(headers: Headers, requestId?: string): Headers {const result = new Headers();for (const [name, value] of headers) {if (["content-type", "cache-control", "x-request-id", "openai-processing-ms", "retry-after"].includes(name)@@@ -921,6 -1033,6 +1042,7 @@@result.set(name, value);}}++ if (requestId) result.set("x-omnirush-client-request-id", requestId);return result;}@@@ -1067,6 -1183,6 +1194,7 @@@ export class OmniRushGatewayBrokerreturn unreachableResponse("device_refresh_unavailable","omnirush.ai: the session could not be renewed right now (the service did not answer). Retrying shortly.",++ requestId);}if (response.status === 401 && !this.state) {@@@ -1085,9 -1201,9 +1213,9 @@@body = retried.body;}if (skipped) {-- this.reportSubagentFallback(request, { ...skipped.event, status: response.status, ok: response.ok });++ this.reportSubagentFallback(request, { ...skipped.event, status: response.status, ok: response.ok, clientRequestId: requestId });} else if (!response.ok && response.status !== 401 && body !== undefined) {- response = await this.subagentFallback(request, normalizedPath, body, response);+ response = await this.subagentFallback(request, normalizedPath, body, response, requestId);}await this.noteUpdateSignals(response, true);const contentType = response.headers.get("content-type") ?? "";@@@ -1097,15 -1213,21 +1225,24 @@@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, {- onInterrupted: (reason) => this.log?.("warn", "omnirush.ai model stream interrupted", { reason, path: normalizedPath, clientRequestId: requestId }),+ 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 }),++ requestId,++ onInterrupted: (reason) => this.log?.("warn", "omnirush.ai model stream interrupted", {++ reason, path: normalizedPath, clientRequestId: requestId,++ }),})- : response.body;+ : upstreamBody;return new Response(responseBody, {status: response.status,statusText: response.statusText,-- headers: responseHeaders(response.headers),++ headers: responseHeaders(response.headers, requestId),});}@@@ -1125,7 -1247,7 +1262,7 @@@const prepared = this.rejectedAttachments.prepare(parsed);if (prepared.attachments.length === 0) return null;if (prepared.replaced.length > 0) {-- this.log?.("info", "omnirush.ai: left out attachments the model refused earlier", { count: prepared.replaced.length });++ this.log?.("info", "omnirush.ai: left out attachments the model refused earlier", { count: prepared.replaced.length, clientRequestId: requestId });return { parsed, attachments: prepared.attachments, body: JSON.stringify(prepared.body) };}return { parsed, attachments: prepared.attachments, body };@@@ -1155,8 -1276,8 +1291,8 @@@const prepared = this.rejectedAttachments.prepare(state.parsed);attachments = prepared.attachments;body = JSON.stringify(prepared.body);-- this.log?.("warn", "omnirush.ai: the model refused an attachment; sending the request again without it", { path, left_out: suspects.length });- response = await this.forwardModelRequest(request, path, body, this.state.accessToken);++ this.log?.("warn", "omnirush.ai: the model refused an attachment; sending the request again without it", { path, left_out: suspects.length, clientRequestId: requestId });+ response = await this.forwardModelRequest(request, path, body, this.state.accessToken, requestId);}if (response.ok) this.rejectedAttachments.accept(attachments.filter((item) => !this.rejectedAttachments.isBad(item.fingerprint)));return { response, body };@@@ -1382,13 -1587,7 +1602,7 @@@* goes to the main model instead, so the sub-agent's task continues rather* than failing. Account-wide refusals and every other error pass through.*/- private async subagentFallback(- request: Request,- path: string,- body: ArrayBuffer | string,- response: Response,- requestId: string,- ): Promise<Response> {- private async subagentFallback(request: Request, path: string, body: ArrayBuffer | string, response: Response): Promise<Response> {++ private async subagentFallback(request: Request, path: string, body: ArrayBuffer | string, response: Response, requestId: string): Promise<Response> {const fallback = request.headers.get(SUBAGENT_FALLBACK_MODEL_HEADER)?.trim() ?? "";if (!fallback || !FALLBACK_MODEL_ID.test(fallback) || (path !== "responses" && path !== "responses/compact")) return response;const requested = requestedModel(body);@@@ -1416,8 -1615,8 +1630,9 @@@used: fallback,reason: refusal.reason,status: next.status,++ clientRequestId: requestId,});-- this.reportSubagentFallback(request, { requested, used: fallback, effort, reason: refusal.reason, status: next.status, ok: next.ok });++ this.reportSubagentFallback(request, { requested, used: fallback, effort, reason: refusal.reason, status: next.status, ok: next.ok, clientRequestId: requestId });return next;}@@@ -1439,7 -1638,7 +1654,7 @@@private reportSubagentFallback(request: Request,-- event: { requested: string; used: string; effort: string | null; reason: string; status: number; ok: boolean },++ event: { requested: string; used: string; effort: string | null; reason: string; status: number; ok: boolean; clientRequestId?: string },): void {try {this.onSubagentFallback?.({@@@ -1466,8 -1665,7 +1681,8 @@@request: Request,path: string,body: ArrayBuffer | string | undefined,-- accessToken?: string,- requestId = clientRequestId(request),++ accessToken: string | undefined,++ requestId: string,): Promise<Response> {const forwardedBody = path === "responses" && body !== undefined? await withReasoningSummary(body, this.reasoningSummaryFor)@@@ -1499,23 -1696,16 +1713,18 @@@return unreachableResponse("gateway_unreachable",`omnirush.ai: the model service could not be reached${failure?.code ? ` (${failure.code})` : ""}. Retrying shortly.`,++ requestId);}- private forward(- request: Request,- path: string,- body: ArrayBuffer | string | undefined,- accessToken: string | undefined,- requestId: string,- ): Promise<Response> {- private forward(request: Request, path: string, body: ArrayBuffer | string | undefined, accessToken?: string): Promise<Response> {++ private forward(request: Request, path: string, body: ArrayBuffer | string | undefined, accessToken: string | undefined, requestId: string): Promise<Response> {if (!this.state) throw new Error("OmniRush gateway credentials are unavailable");const headers = new Headers();headers.set("Authorization", `Bearer ${accessToken ?? this.state.accessToken}`);headers.set("Content-Type", request.headers.get("content-type") || "application/json");headers.set("Accept", request.headers.get("accept") || "application/json");+ headers.set("x-omnirush-client-request-id", requestId);- for (const name of ["x-omnirush-session-id", "x-omnirush-task-id"]) {+ for (const name of ["x-omnirush-session-id", "x-omnirush-task-id", PARENT_SESSION_HEADER]) {const value = request.headers.get(name);if (value) headers.set(name, value);}