Skip to content

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

fix(agent-runtime): retry after compaction without failed assistant

vastsa77a706acb38b
diff --git a/docs/spec/03-runtime/02-agent-runtime.md b/docs/spec/03-runtime/02-agent-runtime.md
index 3ac749981..34f3a0a7e 100644
--- a/docs/spec/03-runtime/02-agent-runtime.md
+++ b/docs/spec/03-runtime/02-agent-runtime.md
@@ -230,6 +230,12 @@ independent recovered outages must not eventually stop because earlier tool
rounds consumed the budget. One-shot completions still use one bounded budget
for their single response. Persistent failures remain bounded and abortable.
+When compaction has already replaced a failed assistant with a summary or
+retained user message, a retry uses that current compacted transcript. It
+removes any trailing assistant rows if present, but does not require an
+assistant message at the transcript tail before calling `continue()` (issue
+#1768).
+
Before surfacing a pre-stream `PROVIDER_ERROR` for HTTP 400/422 whose message
ends in `(no body)`, the runtime makes at most one silent repair attempt with
the generated output-limit fields removed: `max_tokens`,
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index 4277e004a..5c6cfc16c 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -6949,6 +6949,10 @@ must keep splitting are covered by `markdown-blocks.test.mjs`.
loop on a second overflow. A recoverable first overflow emits no terminal
error message or `error` event; only a failed retry (or failed compaction)
closes the bubble with `CONTEXT_TOO_LARGE` / the actual terminal error.
+ - If a retryable provider stream failure arrives after compaction has already
+ replaced the failed assistant with a summary or retained user message, the
+ retry keeps that current compacted transcript and calls `continue()`; it
+ does not surface the missing-assistant guard error (issue #1768).
- If automatic summary generation fails, a durable retained-tail fallback
checkpoint is appended, the run stays active, and one warning explains
that older model context was reduced; the transcript row for that
diff --git a/docs/zh-CN/spec/03-runtime/02-agent-runtime.md b/docs/zh-CN/spec/03-runtime/02-agent-runtime.md
index bc62d1abc..67ad9cf89 100644
--- a/docs/zh-CN/spec/03-runtime/02-agent-runtime.md
+++ b/docs/zh-CN/spec/03-runtime/02-agent-runtime.md
@@ -207,6 +207,8 @@ not temporary retry activity. See the English source section 5d and ADR 0206.
只有失败的请求会被重放。会话、它的转录本以及它的工具状态都保持
不变:失败的助手会从下一个模型上下文中删除,并复用同一个可见的
助手消息 id,因此重试永远不会重启该回合或重新运行已完成的工具调用。
+如果压缩已经用摘要或保留的用户消息替换了失败的助手,重试会使用当前的压缩后转录本;
+存在尾部助手消息时会将其移除,但在调用 `continue()` 前不再要求转录本尾部必须是助手消息(issue #1768)。
每次重试都可中止,并通过规范化的 status 事件报告它当前的退避。主会话、内置子代理和
插件 one-shot 补全使用相同的错误码、预算大小和优先级。
diff --git a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
index 39a370cad..836ce1eb5 100644
--- a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
@@ -3804,6 +3804,8 @@ IPC 请求无法关闭。
上下文;压缩/重试等待期间,界面保留同一个助手气泡并保持运行态;
压缩后只重试一次,第二次溢出不循环。第一次可恢复溢出不发终态错误消息或
`error` 事件;只有重试/压缩失败时才以 `CONTEXT_TOO_LARGE` 或真实终态错误收口。
+ - 如果压缩已经用摘要或保留的用户消息替换了失败的助手,而之后发生可重试的提供程序流失败,
+ 重试会保留当前压缩后的转录本并调用 `continue()`;不会因为缺少失败助手而触发守卫错误(issue #1768)。
- 如果自动摘要生成失败,则持久保留尾部回退
附加检查点,运行保持活动状态,并有一个警告解释
旧模型上下文被减少;该检查点的转录行显示
diff --git a/packages/agent-runtime/src/runtime.test.ts b/packages/agent-runtime/src/runtime.test.ts
index 7e252daa9..fc0cc4b8e 100644
--- a/packages/agent-runtime/src/runtime.test.ts
+++ b/packages/agent-runtime/src/runtime.test.ts
@@ -5161,6 +5161,39 @@ describe("DesktopAgentRuntime assistant thinking events", () => {
await runtime.dispose();
});
+ it("retries from a compacted transcript when the failed assistant is gone (issue #1768)", async () => {
+ const runtime = createRuntime();
+ const agent = (runtime as any).agent;
+ const user = { role: "user", content: "hello", timestamp: 1 };
+ const compactedTail = {
+ role: "compactionSummary",
+ content: "Earlier work was summarized.",
+ timestamp: 2,
+ };
+ agent.state.messages = [user, compactedTail];
+ agent.continue = vi.fn(async () => undefined);
+ agent.waitForIdle = vi.fn(async () => undefined);
+ (runtime as any).pendingProviderRetry = {
+ code: "STREAM_FAILED",
+ message: "stream terminated",
+ retriable: true,
+ };
+ (runtime as any).providerTransientRetryAttempt = 1;
+
+ vi.useFakeTimers();
+ try {
+ const retry = (runtime as any).retryPendingProviderFailure();
+ await vi.runAllTimersAsync();
+ await retry;
+ } finally {
+ vi.useRealTimers();
+ }
+
+ expect(agent.state.messages).toEqual([user, compactedTail]);
+ expect(agent.continue).toHaveBeenCalledOnce();
+ await runtime.dispose();
+ });
+
it("does not continue steering after a recovery becomes pending", async () => {
const runtime = createRuntime({ onEvent: vi.fn() });
const agent = (runtime as any).agent;
diff --git a/packages/agent-runtime/src/runtime.ts b/packages/agent-runtime/src/runtime.ts
index aeeeedb62..7249de152 100644
--- a/packages/agent-runtime/src/runtime.ts
+++ b/packages/agent-runtime/src/runtime.ts
@@ -6408,14 +6408,12 @@ Do not invent objections or turn speculative risks into blockers. Stop when the
this.activeProviderRetryAttempt = retryAttempt;
this.pendingProviderRetry = undefined;
- const messages = [...this.agent.state.messages];
- if (messages.at(-1)?.role !== "assistant") {
- throw new Error("Cannot retry a provider stream without its failed assistant message");
- }
// A failed stream can be represented by more than one trailing assistant
- // message after a tool round. Remove the whole failed suffix before
- // continuing; pi-agent-core rejects any assistant-terminated transcript.
- while (messages.at(-1)?.role === "assistant") messages.pop();
+ // message after a tool round. Compaction may already have replaced the
+ // failed assistant with a summary or retained user message, so an
+ // assistant tail is optional. Remove any failed suffix before continuing;
+ // pi-agent-core rejects an assistant-terminated transcript.
+ const messages = removeTrailingAssistantMessages(this.agent.state.messages);
this.setAgentMessages(messages);
this.providerRetryInProgress = true;
diff --git a/packages/agent-runtime/src/subagent.test.ts b/packages/agent-runtime/src/subagent.test.ts
index e148894a1..fa4b4f884 100644
--- a/packages/agent-runtime/src/subagent.test.ts
+++ b/packages/agent-runtime/src/subagent.test.ts
@@ -1170,6 +1170,40 @@ describe("SubagentRun retries before fallback", () => {
expect(continued).toHaveBeenCalledOnce();
});
+ it("retries from a compacted transcript when the failed assistant is gone (issue #1768)", async () => {
+ const { run } = createRun();
+ const user = { role: "user", content: "task", timestamp: 1 };
+ const compactedTail = {
+ role: "compactionSummary",
+ content: "Earlier work was summarized.",
+ timestamp: 2,
+ };
+ const continued = vi.fn(async () => undefined);
+ run.agent = {
+ state: { ...run.agent.state, messages: [user, compactedTail] as any },
+ continue: continued,
+ waitForIdle: async () => {},
+ };
+ run.pendingProviderRetry = {
+ code: "STREAM_FAILED",
+ message: "stream terminated",
+ retriable: true,
+ };
+ run.providerTransientRetryAttempt = 1;
+
+ vi.useFakeTimers();
+ try {
+ const retry = run.retryPendingProviderFailure();
+ await vi.runAllTimersAsync();
+ await retry;
+ } finally {
+ vi.useRealTimers();
+ }
+
+ expect(run.agent.state.messages).toEqual([user, compactedTail]);
+ expect(continued).toHaveBeenCalledOnce();
+ });
+
it.each([429, 503])("exhausts the shared retry budget before switching after HTTP %s", async (status) => {
const fallback = { ...provider, id: "backup", modelId: "backup-model" };
const { run } = createRun({ fallbackModels: [{ key: "backup/backup-model", provider: fallback }] });
diff --git a/packages/agent-runtime/src/subagent.ts b/packages/agent-runtime/src/subagent.ts
index 910dd140a..8a5f60131 100644
--- a/packages/agent-runtime/src/subagent.ts
+++ b/packages/agent-runtime/src/subagent.ts
@@ -70,6 +70,7 @@ import {
dedupeToolCallMessages,
reportDuplicateToolCallDrop,
} from "./tool-call-dedupe.js";
+import { removeTrailingAssistantMessages } from "./system-transcript.js";
import {
classifyProviderError,
delayWithAbort,
@@ -617,13 +618,11 @@ export class SubagentRun {
const retryError = this.pendingProviderRetry;
if (!retryError) return;
this.pendingProviderRetry = undefined;
- const messages = [...this.agent.state.messages];
- if (messages.at(-1)?.role !== "assistant") {
- throw new Error("Cannot retry a subagent provider stream without its failed assistant message");
- }
// A failed provider stream can leave more than one assistant row after a
- // tool round. Remove the entire failed suffix before continuing.
- while (messages.at(-1)?.role === "assistant") messages.pop();
+ // tool round. Compaction may already have replaced the failed assistant
+ // with a summary or retained user message, so an assistant tail is
+ // optional. Remove any failed suffix before continuing.
+ const messages = removeTrailingAssistantMessages(this.agent.state.messages);
this.agent.state.messages = messages;
this.providerRetryInProgress = true;
try {