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): preserve compaction fallback message order
Kurt Jensendabbf76591f6
diff --git a/docs/adr/0064-codex-parity-context-compaction.md b/docs/adr/0064-codex-parity-context-compaction.mdindex ca03caeb3..2684cc387 100644--- a/docs/adr/0064-codex-parity-context-compaction.md+++ b/docs/adr/0064-codex-parity-context-compaction.md@@ -58,8 +58,11 @@ controls.### Deliberate deviations from Codex-- **Summary position.** pi-agent-core emits the summary before the retained tail,- and that order is not ours to choose.+- **Summary position.** The pi-agent-core context builder emitted the summary+ before the retained tail. PI-Desktop now owns the projection and keeps that+ order for reasoning replay and fallback recovery. A successful active-turn+ checkpoint with one retained user message and no reasoning to replay places+ that request before the nonempty summary (issue #2512).- **Task boundary.** ADR 0136 narrows the retained tail, so an active turn keepsonly its latest user message and a completed-turn checkpoint has an empty tail.- **`hardLimit` derivation.** We keep ADR 0030's window-minus-reserve number,diff --git a/docs/spec/03-runtime/02-agent-runtime.md b/docs/spec/03-runtime/02-agent-runtime.mdindex 16ae03353..a23c684b7 100644--- a/docs/spec/03-runtime/02-agent-runtime.md+++ b/docs/spec/03-runtime/02-agent-runtime.md@@ -584,6 +584,10 @@ decides what a *successful* checkpoint leaves behind:retention limit below; if that message crosses the limit it is truncatedrather than dropped (`[checkpoint truncated: this message crossed theretained context budget]`).+ When the summary is nonempty and there is no reasoning to replay, the model+ receives the retained request before the checkpoint and any newer messages+ after it. The checkpoint describes the current state of the original goal+ (issue #2512). Reasoning replay stays between the summary and the user tail.- A `completed_turn` checkpoint is created at a terminal turn boundary, beforea new user prompt, or for manual compaction. It carries no naked historicaluser messages. The summary is authoritative for completed work, and the next@@ -591,6 +595,7 @@ decides what a *successful* checkpoint leaves behind:- A retained-tail fallback carries its recent window in both modes, because thealternative is an empty context; the mode still decides whether it has tokeep the active task's user message as well.+ Its recovery notice precedes that window even if only the user message fits.- The `fresh_window` family remains the deliberate no-summary exception fromADR 0064 and always carries an empty tail.diff --git a/docs/spec/06-delivery/e2e/providers-and-models.md b/docs/spec/06-delivery/e2e/providers-and-models.mdindex 78e738fad..5e381ba7e 100644--- a/docs/spec/06-delivery/e2e/providers-and-models.md+++ b/docs/spec/06-delivery/e2e/providers-and-models.md@@ -1122,6 +1122,9 @@ with only Host API controlled. Running-turn configuration remains queued.retained tail. The request contains no tool call without its result, andexpanding the original transcript rows still shows their completepersisted results.+ - A successful active-turn checkpoint with one retained request and no+ reasoning to replay sends that request before the nonempty summary. Any+ newer user message follows the summary (issue #2512).- Restart restores the summary and the recorded active/completed retentionmode, and every earlier compaction row is still drawn. A regenerate/forkbefore a checkpoint boundary drops that record specifically; records@@ -1147,6 +1150,8 @@ with only Host API controlled. Running-turn configuration remains queued.once more with tool results cut to a short prefix rather than skipping themodel. If fallback persistence or the safe budget guard fails,`CONTEXT_COMPACTION_FAILED` is emitted once.+ The fallback's recovery notice precedes the retained window even when it+ contains only the active request.- If the newest checkpoint is already the transcript leaf when a follow-upprompt crosses the hard budget, the runtime rebuilds a smaller tail fromthe full transcript and carries the existing summary forward instead ofdiff --git a/packages/agent-runtime/src/session-context.test.ts b/packages/agent-runtime/src/session-context.test.tsindex 3cf8b4bba..7cb73d560 100644--- a/packages/agent-runtime/src/session-context.test.ts+++ b/packages/agent-runtime/src/session-context.test.ts@@ -138,6 +138,27 @@ describe("buildSessionContext", () => {expect(reopened[2]).toMatchObject({ role: "user", content: "Change direction" });});+ it("keeps the recovery notice before a fallback's sole retained user message", () => {+ const original = user("u1", "Set up the empty project", 1);+ const entry = compaction("c1", 3, [original.message]);+ entry.summary = "The summary request failed. Continue from the recent messages below.";+ entry.details = {+ fallback: "retained_tail",+ retainedTailMode: "active_turn",+ retainedTailShape: "recent_window",+ };++ const request = convertToLlm(buildSessionContext([original, entry]).messages);+ expect(request).toEqual([+ expect.objectContaining({+ role: "user",+ content: [{ type: "text", text: expect.stringContaining(entry.summary) }],+ }),+ original.message,+ ]);+ expect(JSON.stringify(request[0])).not.toContain("first unchecked step");+ });+it("does not add resume framing to completed or empty checkpoints", () => {const completed = compaction("c1", 2);completed.details = { retainedTailMode: "completed_turn" };diff --git a/packages/agent-runtime/src/session-context.ts b/packages/agent-runtime/src/session-context.tsindex 08ecade9e..8974f2907 100644--- a/packages/agent-runtime/src/session-context.ts+++ b/packages/agent-runtime/src/session-context.ts@@ -7,12 +7,15 @@ import { recoverMalformedToolCalls } from "./tool-call-recovery.js";* desktop-only entries and persisted host checkpoints. It keeps the* `{ messages }` shape consumed by the agent loop.*- * An active-turn summary follows the original request so the model resumes- * from the checkpoint rather than treating that request as new. Preserve the- * existing order when replaying provider-specific reasoning turns (#296).+ * Context starts at the latest compaction (D203). Summary-first order keeps+ * recovery notices ahead of the messages they describe and retained reasoning+ * between the summary and the user tail (#296). A successful active-turn+ * checkpoint without reasoning to replay can follow its retained request so+ * the model resumes from the checkpoint.*/import type { AgentMessage } from "@earendil-works/pi-agent-core";+import { checkpointFallback } from "@pi-desktop/shared";import {createBranchSummaryMessage,createCompactionSummaryMessage,@@ -70,6 +73,7 @@ export function sessionEntryToContextMessages(!(message.role === "system" && system.length > 0));const resume = details && typeof details === "object" &&"retainedTailMode" in details && details.retainedTailMode === "active_turn" &&+ checkpointFallback(details) === undefined &&entry.summary.trim().length > 0 && reasoning.length === 0 &&tail.length === 1 && tail[0]?.role === "user";const summary = createCompactionSummaryMessage(