Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(agent-runtime): drop tool results orphaned by skipped delegate assistants (#1937)
xpeng5278-webc0b3c2bead29
diff --git a/packages/agent-runtime/src/compaction-tail.ts b/packages/agent-runtime/src/compaction-tail.tsindex 535f5b6aa..57f4504f3 100644--- a/packages/agent-runtime/src/compaction-tail.ts+++ b/packages/agent-runtime/src/compaction-tail.ts@@ -99,7 +99,7 @@ function assistantToolCallIds(messages: AgentMessage[]): Set<string> {* their tool calls with them, and a provider rejects a result whose call is* missing — so the tail cannot keep a result the window no longer explains.*/-function pruneOrphanToolResults(messages: AgentMessage[]): AgentMessage[] {+export function pruneOrphanToolResults(messages: AgentMessage[]): AgentMessage[] {const owned = assistantToolCallIds(messages);return messages.filter((message) =>diff --git a/packages/agent-runtime/src/subagent-context.test.ts b/packages/agent-runtime/src/subagent-context.test.tsindex 5bd13decd..7e7fe45a7 100644--- a/packages/agent-runtime/src/subagent-context.test.ts+++ b/packages/agent-runtime/src/subagent-context.test.ts@@ -482,6 +482,46 @@ describe("degradedDelegateMessages", () => {expect(degraded?.[1]).toEqual(assistantText("final answer"));});+ it("drops a tool result orphaned when its assistant row is skipped", () => {+ const brief = userMessage("Survey the module.");+ const older = assistantText("older note");+ const failed = {+ ...assistantToolCall("c1"),+ stopReason: "error",+ } as AssistantMessage;+ const orphan = toolResult("ok", "c1");+ const finalAnswer = assistantText("final");++ const degraded = degradedDelegateMessages(+ [brief, older, failed, orphan, finalAnswer],+ "Survey the module.",+ smallModel(),+ );++ // The skipped assistant takes tool call c1 with it. Its result is not a+ // leading prefix — an older assistant was kept — so a provider would see+ // an unmatched tool result.+ expect(degraded).toEqual([brief, older, finalAnswer]);+ });++ it("keeps a retained assistant tool call when its result is cut from the suffix", () => {+ const brief = userMessage("Survey the module.");+ // The result is older than the assistant and does not fit, so the+ // newest-first walk keeps the assistant and leaves the result out.+ // pruneOrphanToolResults drops unmatched results only; it does not strip+ // a tool call that stays on a retained assistant.+ const result = toolResult("r".repeat(12_000), "kept");+ const call = assistantToolCall("kept", "note");++ const degraded = degradedDelegateMessages(+ [brief, result, call],+ "Survey the module.",+ smallModel(),+ );++ expect(degraded).toEqual([brief, call]);+ });+it("returns undefined when the brief alone crosses the hard limit", () => {const brief = "x".repeat(12_000);expect(diff --git a/packages/agent-runtime/src/subagent-context.ts b/packages/agent-runtime/src/subagent-context.tsindex a26c1fb68..d2335e098 100644--- a/packages/agent-runtime/src/subagent-context.ts+++ b/packages/agent-runtime/src/subagent-context.ts@@ -51,6 +51,7 @@ import { createCompactionSummaryMessage, type CompactionSummaryMessage } from ".import type { CompactionSettings, Entry, MessageEntry } from "./pi-runtime-types.js";import type { UsageObserver } from "./request-usage.js";import { ESTIMATED_TEXT_CHARS_PER_TOKEN } from "./pi-runtime-estimates.js";+import { pruneOrphanToolResults } from "./compaction-tail.js";import { withCompactionRequestHeaders } from "./compaction-request.js";import {DEFAULT_MAX_TOKENS,@@ -415,12 +416,16 @@ export function degradedDelegateMessages(suffix.unshift(message);tokens += cost;}- // Tool results are contiguous with the assistant that requested them, so a- // suffix cut can only start inside that pair on the result side. Dropping- // leading results keeps the pair rule intact: no result reaches a provider- // without its call, and no call without its results.+ // A budget cut keeps a newest-first run, so it can only open on the result+ // side of a pair. Dropping that leading prefix leaves no result whose call+ // was too old to keep.while (suffix[0]?.role === "toolResult") suffix.shift();- return [...(brief ? [brief] : []), ...suffix];+ // A skipped assistant is different: the walk continues, so kept messages can+ // sit on both sides of its tool result. The call is gone and the result is+ // not a prefix. Drop every result whose call is no longer in the suffix,+ // with the same rule the retained tail uses. A tool call that stays on a+ // retained assistant is left in place.+ return [...(brief ? [brief] : []), ...pruneOrphanToolResults(suffix)];}/**