Skip to content

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

fix(agent-runtime): skip failed and aborted turns when retaining reasoning

xpeng5278-web8a601f0dc273
diff --git a/docs/spec/03-runtime/04-data-storage.md b/docs/spec/03-runtime/04-data-storage.md
index 775f5fde7..c8632f8c7 100644
--- a/docs/spec/03-runtime/04-data-storage.md
+++ b/docs/spec/03-runtime/04-data-storage.md
@@ -434,7 +434,9 @@ Rules:
message. When the bound model requires DeepSeek-style reasoning replay,
`details.retainedReasoning` may hold a bounded list of prior thinking turns
(text + thinking only) so post-compaction Completions requests can echo
- usable reasoning without restoring tool-call pairs (ADR 0256 / #296). If the active message crossed the retention limit it is stored in
+ usable reasoning without restoring tool-call pairs (ADR 0256 / #296). Only
+ completed turns are harvested: an `error`, `aborted`, or `deferred` assistant
+ is left out, as it is from the rebuilt context. If the active message crossed the retention limit it is stored in
marked, truncated form; the original message lines stay complete and
authoritative for UI/diagnostics. An automatic compaction failure may store
`details.fallback = "retained_tail"` and a short recovery summary instead of
diff --git a/packages/agent-runtime/src/reasoning-replay.test.ts b/packages/agent-runtime/src/reasoning-replay.test.ts
index 22bf329d1..f8eb25425 100644
--- a/packages/agent-runtime/src/reasoning-replay.test.ts
+++ b/packages/agent-runtime/src/reasoning-replay.test.ts
@@ -26,6 +26,7 @@ function assistant(
thinking: string,
text = "ok",
thinkingSignature = "reasoning_content",
+ stopReason: "stop" | "error" | "aborted" | "deferred" = "stop",
): AgentMessage {
const content: unknown[] = [
{ type: "thinking", thinking, thinkingSignature },
@@ -44,7 +45,7 @@ function assistant(
totalTokens: 2,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
},
- stopReason: "stop",
+ stopReason,
timestamp: 1,
content,
} as AgentMessage;
@@ -111,6 +112,34 @@ describe("harvestRetainedReasoning", () => {
thinkingSignature: "reasoning_text",
});
});
+
+ it("skips aborted and error thinking that the live context never resends", () => {
+ const turns = harvestRetainedReasoning([
+ assistant("good"),
+ { role: "user", content: "retry", timestamp: 2 },
+ assistant("half-written plan", "partial", "reasoning_content", "aborted"),
+ assistant("failed attempt", "partial", "reasoning_content", "error"),
+ ]);
+ expect(turns.map((turn) => turn.thinking)).toEqual(["good"]);
+ });
+
+ it("does not let an aborted turn consume a retained-reasoning slot", () => {
+ const turns = harvestRetainedReasoning([
+ assistant("one"),
+ assistant("two"),
+ assistant("three"),
+ assistant("cut off", "partial", "reasoning_content", "aborted"),
+ ]);
+ expect(turns.map((turn) => turn.thinking)).toEqual(["one", "two", "three"]);
+ });
+
+ it("skips deferred thinking the same way context projection does", () => {
+ const turns = harvestRetainedReasoning([
+ assistant("kept"),
+ assistant("held back", "partial", "reasoning_content", "deferred"),
+ ]);
+ expect(turns.map((turn) => turn.thinking)).toEqual(["kept"]);
+ });
});
describe("retainedReasoning replay messages", () => {
diff --git a/packages/agent-runtime/src/reasoning-replay.ts b/packages/agent-runtime/src/reasoning-replay.ts
index 97fa73d3f..5efed66ba 100644
--- a/packages/agent-runtime/src/reasoning-replay.ts
+++ b/packages/agent-runtime/src/reasoning-replay.ts
@@ -104,9 +104,20 @@ function assistantThinkingText(message: AssistantMessage): {
};
}
+/**
+ * Stop reasons the live context never resends. session-context
+ * `isContextMessage` and compaction-tail `DROPPED_STOP_REASONS` treat error,
+ * aborted, and deferred assistants as transcript rows, not model context.
+ * A partial think from one of those turns is not completed reasoning, and it
+ * must not take a retained slot away from a finished turn.
+ */
+const DROPPED_STOP_REASONS = new Set<string>(["error", "aborted", "deferred"]);
+
/**
* Collect the newest assistant turns that still carry thinking, newest-last,
* for storage in checkpoint details. Tool-call blocks are dropped on purpose.
+ * Error, aborted, and deferred turns are skipped for the same reason the live
+ * context drops them.
*/
export function harvestRetainedReasoning(
messages: readonly AgentMessage[],
@@ -117,6 +128,7 @@ export function harvestRetainedReasoning(
const message = messages[index];
if (!message || message.role !== "assistant") continue;
const assistant = message as AssistantMessage;
+ if (DROPPED_STOP_REASONS.has(assistant.stopReason)) continue;
const parts = assistantThinkingText(assistant);
if (!parts.thinking) continue;
selected.push({