From 2d09f8e61512650bb6c8e159b67a7334b859bf24 Mon Sep 17 00:00:00 2001 From: Episkey Date: Tue, 27 Jan 2026 11:16:23 +0800 Subject: [PATCH] fix(agents): add error handling to orphaned message cleanup (#2262) --- CHANGELOG.md | 1 + src/agents/pi-embedded-runner/run/attempt.ts | 28 +++++++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17bb4477c..02a8035f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,7 @@ Status: unreleased. - **BREAKING:** Gateway auth mode "none" is removed; gateway now requires token/password (Tailscale Serve identity still allowed). ### Fixes +- Agents: add error handling to orphaned message cleanup to prevent message processing queue from blocking. (#2262) - Gateway: suppress AbortError and transient network errors in unhandled rejections. (#2451) Thanks @Glucksberg. - TTS: keep /tts status replies on text-only commands and avoid duplicate block-stream audio. (#2451) Thanks @Glucksberg. - Security: pin npm overrides to keep tar@7.5.4 for install toolchains. diff --git a/src/agents/pi-embedded-runner/run/attempt.ts b/src/agents/pi-embedded-runner/run/attempt.ts index f1c487470..833224a97 100644 --- a/src/agents/pi-embedded-runner/run/attempt.ts +++ b/src/agents/pi-embedded-runner/run/attempt.ts @@ -713,18 +713,26 @@ export async function runEmbeddedAttempt( }); // Repair orphaned trailing user messages so new prompts don't violate role ordering. - const leafEntry = sessionManager.getLeafEntry(); - if (leafEntry?.type === "message" && leafEntry.message.role === "user") { - if (leafEntry.parentId) { - sessionManager.branch(leafEntry.parentId); - } else { - sessionManager.resetLeaf(); + try { + const leafEntry = sessionManager.getLeafEntry(); + if (leafEntry?.type === "message" && leafEntry.message.role === "user") { + if (leafEntry.parentId) { + sessionManager.branch(leafEntry.parentId); + } else { + sessionManager.resetLeaf(); + } + const sessionContext = sessionManager.buildSessionContext(); + activeSession.agent.replaceMessages(sessionContext.messages); + log.warn( + `Removed orphaned user message to prevent consecutive user turns. ` + + `runId=${params.runId} sessionId=${params.sessionId}`, + ); } - const sessionContext = sessionManager.buildSessionContext(); - activeSession.agent.replaceMessages(sessionContext.messages); - log.warn( - `Removed orphaned user message to prevent consecutive user turns. ` + + } catch (err) { + log.error( + `Failed to repair orphaned user message, continuing anyway. ` + `runId=${params.runId} sessionId=${params.sessionId}`, + { error: err instanceof Error ? err.message : String(err) }, ); }