Skip to content

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

fix(agent-runtime): tag resumed delegate history with model.provider

xpeng5278-web53f4b24782ae
diff --git a/docs/adr/0279-resumable-subagent-delegations.md b/docs/adr/0279-resumable-subagent-delegations.md
index b699c20cb..442acb652 100644
--- a/docs/adr/0279-resumable-subagent-delegations.md
+++ b/docs/adr/0279-resumable-subagent-delegations.md
@@ -70,7 +70,12 @@ A new module (`packages/agent-runtime/src/delegation-history.ts`) owns:
- synthesizing the original `task` as the first user message from the first
`Task` tool row's persisted arguments,
- converting to `AgentMessage[]` with the delegate's own provider/model binding
- (a pinned delegation model is not the session model),
+ (a pinned delegation model is not the session model). Assistant transport
+ identity is `model.provider` / `model.id` — the same values live requests
+ use — not the account row id. When `vendorKey` is set, `model.provider`
+ differs from `provider.id`; tagging restored history with the account id
+ makes pi-ai treat same-model reasoning as foreign and empty
+ `reasoning_content` on resume,
- a read-file / line-count budget gate.
`SubagentRun` gains `initialMessages?: AgentMessage[]`; when present it seeds
diff --git a/docs/spec/03-runtime/02-agent-runtime.md b/docs/spec/03-runtime/02-agent-runtime.md
index 506edb8a8..e234265f6 100644
--- a/docs/spec/03-runtime/02-agent-runtime.md
+++ b/docs/spec/03-runtime/02-agent-runtime.md
@@ -770,7 +770,10 @@ criterion-by-criterion report of what was met and the evidence observed.
deltas every 16ms, and flushes before tool/terminal/abort/error/retry
boundaries. `message_end` is the authoritative snapshot (D412).
- Restored assistant history reconstructs separate text and thinking blocks
- before the next turn.
+ before the next turn. Assistant transport identity is `model.provider` /
+ `model.id` (same as live requests), not the account row id — including when
+ `seedDelegateMessages` rebuilds a Task(resume) history — so same-model
+ Completions reasoning stays in `reasoning_content` under a vendorKey binding.
- Restored history also reconstructs tool call/result pairs from persisted
tool rows (`toolCallId`/`toolArgs`/`toolResult`), so a recreated runtime
keeps its full working context — file contents read, command output —
@@ -1373,7 +1376,9 @@ model's provider identity and retain recorded model IDs. A different recorded
account or model remains distinct. Legacy rows without identity retain the
current-model fallback. Same-model Completions reasoning stays in its native
reasoning field, never appended to visible answer text because of an account
-UUID/vendor-name mismatch.
+UUID/vendor-name mismatch. The same rule applies when `seedDelegateMessages`
+rebuilds a Task(resume) history: tag restored assistants and synthetic
+tool-call carriers with `model.provider` / `model.id`, not the account row id.
### 7.0.1 User custom system prompt files (issue #542)
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 be236cdf2..070632d6a 100644
--- a/docs/zh-CN/spec/03-runtime/02-agent-runtime.md
+++ b/docs/zh-CN/spec/03-runtime/02-agent-runtime.md
@@ -585,7 +585,10 @@ Goal 批准所承诺的内容与 Plan 批准所承诺的内容完全相同:`mo
`message_update.deltaThinking`。他们从不附加到 `content` 或
`deltaText`。
- 恢复的助手历史重建单独的文本和思维块
- 在下一个回合之前。
+ 在下一个回合之前。助手传输身份使用 `model.provider` / `model.id`
+ (与实时请求相同),而不是账户行 id;`vendorKey` 绑定的模型上两者不同。
+ Task(resume) 的 `seedDelegateMessages` 同样遵守该规则,避免 pi-ai 把同模型
+ reasoning 当成跨模型历史而清空 `reasoning_content`。
- 恢复的历史记录还可以从持久保存的工具 call/result 对中重建工具
工具行(`off`/`minimal`/`low`),因此重新创建了运行时
保持其完整的工作上下文——读取的文件内容、命令输出——
diff --git a/packages/agent-runtime/src/delegation-history.ts b/packages/agent-runtime/src/delegation-history.ts
index 282d7d401..dca85fbf3 100644
--- a/packages/agent-runtime/src/delegation-history.ts
+++ b/packages/agent-runtime/src/delegation-history.ts
@@ -217,6 +217,12 @@ export function taskObjectiveFromArgs(args: unknown): string {
* result. An orphan tool row (its assistant lost to a truncated branch) gets a
* synthesized carrier so the pair stays well-formed — providers reject an
* unpaired `toolUse`.
+ *
+ * Assistant transport identity is `model.provider` / `model.id` (the same
+ * values live requests use), not the account row id. A vendorKey-bound model
+ * has `model.provider !== provider.id`; tagging history with the account id
+ * makes pi-ai treat same-model reasoning as foreign and empty
+ * `reasoning_content` on Task(resume).
*/
export function chainRowsToMessages(
rows: readonly UiMessage[],
@@ -268,8 +274,8 @@ export function chainRowsToMessages(
role: "assistant",
content,
api,
- provider: provider.id,
- model: provider.modelId,
+ provider: model.provider,
+ model: model.id,
usage: usageToPi(row.usage),
stopReason: "stop",
timestamp,
@@ -283,8 +289,8 @@ export function chainRowsToMessages(
role: "assistant",
content: [],
api,
- provider: provider.id,
- model: provider.modelId,
+ provider: model.provider,
+ model: model.id,
usage: usageToPi(undefined),
stopReason: "toolUse",
timestamp,
diff --git a/packages/agent-runtime/src/resume-provider-identity.test.ts b/packages/agent-runtime/src/resume-provider-identity.test.ts
new file mode 100644
index 000000000..1c8cbce7e
--- /dev/null
+++ b/packages/agent-runtime/src/resume-provider-identity.test.ts
@@ -0,0 +1,93 @@
+import { expect, it } from "vitest";
+import { convertMessages } from "@earendil-works/pi-ai/api/openai-completions";
+import type { UiMessage } from "@pi-desktop/shared";
+import {
+ buildProviderModel,
+ type RuntimeProviderConfig,
+} from "./provider-binding.js";
+import { seedDelegateMessages } from "./delegation-history.js";
+import { contextBudgetLimitsFor } from "./context-budget.js";
+
+/**
+ * Task(resume) must tag restored assistant history with model.provider so
+ * pi-ai's same-model check keeps DeepSeek (and similar) reasoning in
+ * reasoning_content. Tagging with the account row id instead makes the
+ * adapter treat the history as foreign-model and empties that field.
+ */
+function replay(vendorKey?: string) {
+ const provider: RuntimeProviderConfig = {
+ id: "account-row-uuid",
+ vendorKey,
+ name: "DeepSeek account",
+ baseUrl: "https://api.deepseek.com/v1",
+ modelId: "deepseek-reasoner",
+ apiKey: "unused-fixture",
+ supportsReasoning: true,
+ supportedThinkingLevels: ["off", "high"],
+ };
+ const model = buildProviderModel(provider);
+ const originalThinking =
+ "Inspect the authorization boundary before reading the file.";
+ const rows: UiMessage[] = [
+ {
+ id: "assistant-1",
+ role: "assistant",
+ content: "I will read permissions.ts.",
+ thinking: originalThinking,
+ createdAt: "2026-10-10T00:00:00.000Z",
+ },
+ {
+ id: "result-1",
+ role: "tool",
+ content: "file contents",
+ toolCallId: "call-1",
+ toolName: "Read",
+ toolArgs: { path: "permissions.ts" },
+ createdAt: "2026-10-10T00:00:01.000Z",
+ },
+ ];
+ const messages = seedDelegateMessages({
+ originalTask: "Inspect permissions.",
+ rows,
+ provider,
+ model,
+ budget: contextBudgetLimitsFor(model),
+ });
+ const compat = {
+ ...model.compat,
+ supportsDeveloperRole: false,
+ supportsOpenAIGrammarTools: false,
+ requiresToolResultName: false,
+ requiresAssistantAfterToolResult: false,
+ requiresThinkingAsText: false,
+ requiresReasoningContentOnAssistantMessages: true,
+ thinkingFormat: "deepseek",
+ deferredToolsMode: "none",
+ };
+ const wire = convertMessages(
+ model as never,
+ { messages } as never,
+ compat as never,
+ );
+ return {
+ messages,
+ model,
+ originalThinking,
+ assistant: wire.find((message) => message.role === "assistant") as
+ | { content?: unknown; reasoning_content?: string }
+ | undefined,
+ };
+}
+
+it("resuming on the same vendor account preserves its original reasoning field", () => {
+ const { assistant, originalThinking, messages, model } = replay("deepseek");
+ expect(assistant?.reasoning_content).toBe(originalThinking);
+ expect(
+ messages.find((message) => message.role === "assistant")?.provider,
+ ).toBe(model.provider);
+});
+
+it("control: the old no-vendor-key binding preserves reasoning", () => {
+ const { assistant, originalThinking } = replay();
+ expect(assistant?.reasoning_content).toBe(originalThinking);
+});