From 295f5c008d7366a819d79de37212aa3d8b2a6ca9 Mon Sep 17 00:00:00 2001 From: kumarabhirup Date: Wed, 18 Mar 2026 16:56:13 -0700 Subject: [PATCH] fix(agent): lower thinking level to high, fix indentation, and handle terminated errors - Change thinkingLevel from xhigh to high for session patches - Fix indentation of event handler blocks in wireSubscribeOnlyProcess and wireChildProcess - Add user-friendly message when agent run is terminated by the gateway --- apps/web/lib/active-runs.ts | 116 +++++++++++++++++------------------ apps/web/lib/agent-runner.ts | 6 +- 2 files changed, 63 insertions(+), 59 deletions(-) diff --git a/apps/web/lib/active-runs.ts b/apps/web/lib/active-runs.ts index 19daea68363..d3aa6cb076e 100644 --- a/apps/web/lib/active-runs.ts +++ b/apps/web/lib/active-runs.ts @@ -623,7 +623,7 @@ export function startSubscribeRun(params: { "sessions.patch", { key: sessionKey, - thinkingLevel: "xhigh", + thinkingLevel: "high", verboseLevel: "full", reasoningLevel: "on", }, @@ -969,19 +969,19 @@ function wireSubscribeOnlyProcess( } } } - if (typeof ev.data?.stopReason === "string" && ev.data.stopReason === "error" && !agentErrorReported) { - agentErrorReported = true; - const errMsg = typeof ev.data?.errorMessage === "string" - ? parseErrorBody(ev.data.errorMessage) - : (parseAgentErrorMessage(ev.data) ?? "Agent stopped with an error"); - emitError(errMsg); - } + if (typeof ev.data?.stopReason === "string" && ev.data.stopReason === "error" && !agentErrorReported) { + agentErrorReported = true; + const errMsg = typeof ev.data?.errorMessage === "string" + ? parseErrorBody(ev.data.errorMessage) + : (parseAgentErrorMessage(ev.data) ?? "Agent stopped with an error"); + emitError(errMsg); } + } - if (ev.event === "agent" && ev.stream === "tool") { - const phase = typeof ev.data?.phase === "string" ? ev.data.phase : undefined; - const toolCallId = typeof ev.data?.toolCallId === "string" ? ev.data.toolCallId : ""; - const toolName = typeof ev.data?.name === "string" ? ev.data.name : ""; + if (ev.event === "agent" && ev.stream === "tool") { + const phase = typeof ev.data?.phase === "string" ? ev.data.phase : undefined; + const toolCallId = typeof ev.data?.toolCallId === "string" ? ev.data.toolCallId : ""; + const toolName = typeof ev.data?.name === "string" ? ev.data.name : ""; if (phase === "start") { liveStats.toolStartCount += 1; closeReasoning(); @@ -1080,19 +1080,19 @@ function wireSubscribeOnlyProcess( }, SUBSCRIBE_LIFECYCLE_END_GRACE_MS); } - if (ev.event === "agent" && ev.stream === "lifecycle" && ev.data?.phase === "error" && !agentErrorReported) { - agentErrorReported = true; - emitError(parseAgentErrorMessage(ev.data) ?? "Agent encountered an error"); - finalizeSubscribeRun(run, "error"); - } + if (ev.event === "agent" && ev.stream === "lifecycle" && ev.data?.phase === "error" && !agentErrorReported) { + agentErrorReported = true; + emitError(parseAgentErrorMessage(ev.data) ?? "Agent encountered an error"); + finalizeSubscribeRun(run, "error"); + } - if (ev.event === "error" && !agentErrorReported) { - agentErrorReported = true; - emitError(parseAgentErrorMessage(ev.data ?? (ev as unknown as Record)) ?? "An unknown error occurred"); - } - }; + if (ev.event === "error" && !agentErrorReported) { + agentErrorReported = true; + emitError(parseAgentErrorMessage(ev.data ?? (ev as unknown as Record)) ?? "An unknown error occurred"); + } +}; - const rl = createInterface({ input: child.stdout! }); +const rl = createInterface({ input: child.stdout! }); rl.on("line", (line: string) => { if (!line.trim()) { return; } @@ -1620,22 +1620,22 @@ function wireChildProcess(run: ActiveRun): void { } } } - // Agent error inline (stopReason=error) - if ( - typeof ev.data?.stopReason === "string" && - ev.data.stopReason === "error" && - !agentErrorReported - ) { - agentErrorReported = true; - const errMsg = typeof ev.data?.errorMessage === "string" - ? parseErrorBody(ev.data.errorMessage) - : (parseAgentErrorMessage(ev.data) ?? "Agent stopped with an error"); - emitError(errMsg); - } + // Agent error inline (stopReason=error) + if ( + typeof ev.data?.stopReason === "string" && + ev.data.stopReason === "error" && + !agentErrorReported + ) { + agentErrorReported = true; + const errMsg = typeof ev.data?.errorMessage === "string" + ? parseErrorBody(ev.data.errorMessage) + : (parseAgentErrorMessage(ev.data) ?? "Agent stopped with an error"); + emitError(errMsg); } + } - // Tool events - if (ev.event === "agent" && ev.stream === "tool") { + // Tool events + if (ev.event === "agent" && ev.stream === "tool") { const phase = typeof ev.data?.phase === "string" ? ev.data.phase @@ -1794,28 +1794,28 @@ function wireChildProcess(run: ActiveRun): void { closeText(); } - // Lifecycle error - if ( - ev.event === "agent" && - ev.stream === "lifecycle" && - ev.data?.phase === "error" && - !agentErrorReported - ) { - agentErrorReported = true; - emitError(parseAgentErrorMessage(ev.data) ?? "Agent encountered an error"); - } + // Lifecycle error + if ( + ev.event === "agent" && + ev.stream === "lifecycle" && + ev.data?.phase === "error" && + !agentErrorReported + ) { + agentErrorReported = true; + emitError(parseAgentErrorMessage(ev.data) ?? "Agent encountered an error"); + } - // Top-level error event - if (ev.event === "error" && !agentErrorReported) { - agentErrorReported = true; - emitError( - parseAgentErrorMessage( - ev.data ?? - (ev as unknown as Record), - ) ?? "An unknown error occurred", - ); - } - }; + // Top-level error event + if (ev.event === "error" && !agentErrorReported) { + agentErrorReported = true; + emitError( + parseAgentErrorMessage( + ev.data ?? + (ev as unknown as Record), + ) ?? "An unknown error occurred", + ); + } +}; const processParentSubscribeEvent = (ev: AgentEvent) => { const gSeq = typeof (ev as Record).globalSeq === "number" diff --git a/apps/web/lib/agent-runner.ts b/apps/web/lib/agent-runner.ts index 24d7e5c5671..b8dbd769824 100644 --- a/apps/web/lib/agent-runner.ts +++ b/apps/web/lib/agent-runner.ts @@ -875,7 +875,7 @@ class GatewayProcessHandle const patchParams: Record = { key: sessionKey, - thinkingLevel: "xhigh", + thinkingLevel: "high", verboseLevel: "full", reasoningLevel: "on", }; @@ -1353,6 +1353,10 @@ export function parseAgentErrorMessage( * Returns a clean, user-readable message. */ export function parseErrorBody(raw: string): string { + if (raw === "terminated") { + return "Agent run was terminated by the gateway. This is usually caused by the model provider dropping the connection mid-stream. Retry the message to continue."; + } + // Try to extract JSON body from "STATUS {json}" pattern const jsonIdx = raw.indexOf("{"); if (jsonIdx >= 0) {