Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(agent-runtime): thread the delegate's prior summary past the system row

liuzixiang7e9b377a0f5b
diff --git a/packages/agent-runtime/src/subagent-context.test.ts b/packages/agent-runtime/src/subagent-context.test.ts
index 086ebb545..3ffebbd76 100644
--- a/packages/agent-runtime/src/subagent-context.test.ts
+++ b/packages/agent-runtime/src/subagent-context.test.ts
@@ -237,6 +237,44 @@ describe("prepareDelegateTurnContext", () => {
expect(prompts).toHaveLength(1);
});
+ it("threads the prior in-run summary past the system row at a second boundary", async () => {
+ // The delegate's state keeps its system row at the head. Detecting the
+ // prior summary at index 0 read that row instead, so the second (and
+ // every later) in-run boundary compaction lost previousSummary and
+ // re-summarized the earlier checkpoint summary under the from-scratch
+ // prompt — exactly what the previous-summary thread exists to avoid.
+ const messages: AgentMessage[] = [
+ { role: "system", content: "delegate system prompt", timestamp: 0 } as AgentMessage,
+ {
+ role: "compactionSummary",
+ summary: "PRIOR CHECKPOINT SUMMARY",
+ tokensBefore: 1_000,
+ timestamp: 1,
+ } as AgentMessage,
+ userMessage("continue the work"),
+ assistantToolCall("big"),
+ toolResult("x".repeat(7_000), "big"),
+ ];
+ const { factory, prompts } = summaryModels(() => summarySuccess("UPDATED SUMMARY"));
+
+ const outcome = await prepareDelegateTurnContext({
+ messages,
+ model: smallModel(),
+ taskBrief: "continue the work",
+ retentionMode: "active_turn",
+ summaryModels: factory,
+ signal: new AbortController().signal,
+ });
+
+ expect(outcome.kind).toBe("compacted");
+ expect(prompts).toHaveLength(1);
+ // The update prompt carries the prior summary forward instead of
+ // treating it as ordinary history under the from-scratch prompt.
+ expect(prompts[0]).toContain("<previous-summary>");
+ expect(prompts[0]).toContain("PRIOR CHECKPOINT SUMMARY");
+ expect(prompts[0]).not.toContain("delegate system prompt");
+ });
+
it("includes cached input in the delegate's usage anchor", async () => {
const taskBrief = "Read the permission flow.";
const messages: AgentMessage[] = [
diff --git a/packages/agent-runtime/src/subagent-context.ts b/packages/agent-runtime/src/subagent-context.ts
index d2335e098..913caaafc 100644
--- a/packages/agent-runtime/src/subagent-context.ts
+++ b/packages/agent-runtime/src/subagent-context.ts
@@ -276,11 +276,18 @@ async function compactDelegateContext(
> {
// A prior in-memory compaction left its summary message at the head; it
// threads into the next summary as `previousSummary` instead of being
- // summarized a second time.
- const head = input.messages[0];
+ // summarized a second time. The delegate's state keeps its system row at
+ // the head, so the detection — and the compactable range — start past
+ // it: reading index 0 made every in-run boundary compaction see the
+ // system row, `previousSummary` never threaded, and the earlier
+ // checkpoint summary was re-summarized under the from-scratch prompt.
+ // The system row itself is never compactable; the caller reinstalls it.
+ const historyOffset = input.messages[0]?.role === "system" ? 1 : 0;
+ const head = input.messages[historyOffset];
const previousSummary = isCompactionSummary(head) ? head.summary : undefined;
- const compactable =
- previousSummary === undefined ? input.messages : input.messages.slice(1);
+ const compactable = input.messages.slice(
+ historyOffset + (previousSummary === undefined ? 0 : 1),
+ );
const prepared = prepareCompaction(delegateMessageEntries(compactable), {
enabled: true,
reserveTokens: budget.requestHeadroom,