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
This commit is contained in:
kumarabhirup 2026-03-18 16:56:13 -07:00
parent f04c1a5dc3
commit 295f5c008d
No known key found for this signature in database
GPG Key ID: DB7CA2289CAB0167
2 changed files with 63 additions and 59 deletions

View File

@ -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<string, unknown>)) ?? "An unknown error occurred");
}
};
if (ev.event === "error" && !agentErrorReported) {
agentErrorReported = true;
emitError(parseAgentErrorMessage(ev.data ?? (ev as unknown as Record<string, unknown>)) ?? "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<string, unknown>),
) ?? "An unknown error occurred",
);
}
};
// Top-level error event
if (ev.event === "error" && !agentErrorReported) {
agentErrorReported = true;
emitError(
parseAgentErrorMessage(
ev.data ??
(ev as unknown as Record<string, unknown>),
) ?? "An unknown error occurred",
);
}
};
const processParentSubscribeEvent = (ev: AgentEvent) => {
const gSeq = typeof (ev as Record<string, unknown>).globalSeq === "number"

View File

@ -875,7 +875,7 @@ class GatewayProcessHandle
const patchParams: Record<string, string> = {
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) {