Skip to content

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

merge: update context calibration PR with latest main

vastsa6e1b786da429
diff --cc docs/spec/06-delivery/04-e2e-test-plan.md
index d15febf7d,c162253cb..8802f17a2
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@@ -7991,7 -8002,8 +8002,9 @@@ identify the platform validation still
| C — Conversation & stream (delegate context budget) | E2E-SUBAGENT-context-overflow-compacts-before-failing, E2E-SUBAGENT-context-overflow-reports-actionable-failure, E2E-SUBAGENT-resume-seeds-within-context-budget |
| Quality (delegate context budget) | E2E-SUBAGENT-context-overflow-compacts-before-failing, E2E-SUBAGENT-context-overflow-reports-actionable-failure, E2E-SUBAGENT-resume-seeds-within-context-budget |
| M6+ (delegate context budget) | E2E-SUBAGENT-context-overflow-compacts-before-failing, E2E-SUBAGENT-context-overflow-reports-actionable-failure, E2E-SUBAGENT-resume-seeds-within-context-budget |
+| C / F / Quality — The context estimate stays safe (calibration) | E2E-CONTEXT-estimate-calibration-stays-safe |
+ | C — Conversation & stream (unique tool-call ids) | E2E-RUNTIME-unique-tool-call-ids-per-request |
+ | Quality (unique tool-call ids) | E2E-RUNTIME-unique-tool-call-ids-per-request |
| G — Plugin host lifecycle (crash report) | E2E-PLUGIN-crash-report-names-the-exit-code |
| Quality (crash report) | E2E-PLUGIN-crash-report-names-the-exit-code |
@@@ -14147,27 -14161,30 +14162,51 @@@ the latest destination. These assertion
success fixture uses a child-only extra CA; it does not reproduce a specific
antivirus installation or claim native macOS/Linux verification.
+### E2E-CONTEXT-estimate-calibration-stays-safe
+
+- **Preconditions:** Deterministic provider fixture whose reported usage can be
+ scripted; a session whose occupancy is close to the hard boundary; no real
+ provider credentials.
+- **Steps:** Report usage below the prediction repeatedly and confirm the
+ displayed occupancy never falls below the floor and compaction still fires;
+ report usage far outside the plausible band and confirm the value does not
+ move; run a CJK-heavy session and compare the displayed occupancy with the
+ reported usage.
+- **Expected:** The calibrated occupancy stays within 0.85×–6× of the raw
+ estimate, moves up as soon as there is evidence, moves down only after
+ agreeing samples, and a projection at 1.18× the hard limit still compacts.
+ Misreports move it in neither direction. CJK text no longer reads at a
+ quarter of its measured cost.
+- **Specs linked:** `03-runtime/02-agent-runtime.md` §5.1,
+ `08-meta/decisions-log.md` D606
+- **Acceptance:** C (chat and stream), F (persistence), Quality
+- **Status:** Unit-covered (`context-calibration.test.ts`, including the CJK and
+ boundary cases); desktop E2E pending
+
+ ### E2E-RUNTIME-unique-tool-call-ids-per-request
+
+ - **Preconditions:** A session transcript that carries the same tool call twice
+ (a retried append leaves the call under a second row id), loaded into a
+ recreated runtime against a deterministic provider fixture. No real provider
+ credentials.
+ - **Steps:** Send one prompt so the runtime assembles and issues a request. Read
+ the outgoing request the fixture received, and the `agent` log channel. Repeat
+ with a transcript whose calls are already unique.
+ - **Expected:** The outgoing request carries exactly one `toolCall` for that id
+ and exactly one matching result, so no provider can answer
+ `tool_use ids must be unique`. The log channel gets one line naming the
+ session and the ids. The unique transcript's request is unchanged, byte for
+ byte, and logs nothing.
+ - **Specs:** `03-runtime/02-agent-runtime.md` §5,
+ `08-meta/decisions-log.md` D608.
+ - **Acceptance:** C (conversation and stream), Quality.
+ - **Milestone:** Post-MVP regression coverage.
+ - **Automation:** `packages/agent-runtime/src/runtime.test.ts` covers both halves
+ against a real runtime built from a duplicated history (drop + one log line)
+ and a unique one (identity, no log line).
+ - **Status:** Unit-covered; no end-to-end driver issues a real provider request
+ against a duplicated transcript.
+
### E2E-MCP-HTTP-ACK — HTTP acknowledgement and authorization status
- **Preconditions**: A local mock Streamable HTTP server returns JSON for
diff --cc docs/spec/08-meta/decisions-log.md
index fa000a98e,eb22d89eb..cd2031143
--- a/docs/spec/08-meta/decisions-log.md
+++ b/docs/spec/08-meta/decisions-log.md
@@@ -6471,25 -6476,26 +6476,45 @@@ that was sitting at the bottom — incl
state, protocol, persistence, theme schema, or permission change. See
`04-ux/08-component-spec.md` and E2E-CHAT-opaque-floating-decision-and-retry-surfaces.
+## 2026-09-21 — The context estimate is calibrated conservatively (D606)
+
+- Every budget threshold reads one number, and that number is corrected against
+ what requests actually cost. pi's estimator anchors on the last assistant
+ usage and estimates the rest as `chars / 4`, which under-counts CJK text by
+ roughly a factor of 2–4 and, with no anchor left, omits the system prompt and
+ tool schemas entirely.
+- The two errors are applied separately: the per-character bias as a
+ scale-free ratio over the guessed tail, and an unanchored residual as a ratio
+ only for observations taken at a comparable scale (0.5×–2×), otherwise as the
+ observed overhead capped at 32,000 tokens. A 100k-scale sample therefore
+ cannot be applied as a ratio to a 1M projection.
+- The correction is asymmetric: upward applies once three observations exist;
+ downward needs three agreeing samples, is capped at 15 % per step, and can
+ never take the value below 85 % of the raw estimate, so a projection at
+ 1.18× the hard limit still compacts. A report outside 0.5×–3× of what the
+ calibration predicted is a misreport, and two consecutive misreports freeze
+ the downward direction. See `03-runtime/02-agent-runtime.md` §5.1.
+
+ ## 2026-09-21 — A tool-call id is unique in every request (D608, issue #718)
+
+ - Anthropic-family endpoints, DeepSeek's included, reject a whole turn with
+ `tool_use ids must be unique` when the request carries a call id twice, and
+ the session cannot continue while that lasts. The transcript is an append-only
+ snapshot stream that tolerates a retried append, so the same call can reach
+ the assembled context twice: under one row id, which the host's keep-last read
+ already collapses, or under two, which it cannot.
+ - The last view before the wire therefore keeps the first occurrence of each
+ `toolCall` id and drops a later call or a later result for that id, so the
+ call/result pair the provider validates stays well-formed. A request with no
+ duplicates is returned unchanged (identity, not a copy). Nothing is rewritten
+ on disk and no compaction or retention rule changes.
+ - A drop is reported once on the `agent` log channel with the session and the
+ ids, so the next report of this names the writer instead of only the
+ provider's sentence. See `03-runtime/02-agent-runtime.md` §5.
+ - The guard is deliberately the request boundary rather than the history
+ rebuild: it also covers a duplicate that appears while the session runs, which
+ a rebuild-time filter cannot see.
+
## 2026-09-21 — A plugin crash reports its exit code without copying raw output (D607, issue #747)
- The host-process crash path reported only the plugin id, so a report read
diff --cc docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
index bdba645bd,2b3a9b4f2..b736bd847
--- a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
@@@ -5285,7 -5288,8 +5288,9 @@@ eleven-tool-round desktop paths are ver
| C — 对话和直播(委托上下文预算) | E2E-SUBAGENT-context-overflow-compacts-before-failing、E2E-SUBAGENT-context-overflow-reports-actionable-failure、E2E-SUBAGENT-resume-seeds-within-context-budget |
| 品质(委托上下文预算) | E2E-SUBAGENT-context-overflow-compacts-before-failing、E2E-SUBAGENT-context-overflow-reports-actionable-failure、E2E-SUBAGENT-resume-seeds-within-context-budget |
| M6+(委托上下文预算) | E2E-SUBAGENT-context-overflow-compacts-before-failing、E2E-SUBAGENT-context-overflow-reports-actionable-failure、E2E-SUBAGENT-resume-seeds-within-context-budget |
+| C / F / 品质 —— 上下文估算保持安全(校准) | E2E-CONTEXT-estimate-calibration-stays-safe |
+ | C — 对话与流式(工具调用 id 唯一) | E2E-RUNTIME-unique-tool-call-ids-per-request |
+ | 品质(工具调用 id 唯一) | E2E-RUNTIME-unique-tool-call-ids-per-request |
| G — 插件宿主生命周期(崩溃上报) | E2E-PLUGIN-crash-report-names-the-exit-code |
| 品质(崩溃上报) | E2E-PLUGIN-crash-report-names-the-exit-code |
@@@ -8335,16 -8341,19 +8342,29 @@@ the latest destination. These assertion
- **规格:** 04-ux/06-settings-ia、04-ux/08-component-spec、
04-ux/09-interaction-patterns;ADR turn-process-and-thinking-display。
+### E2E-CONTEXT-estimate-calibration-stays-safe
+
+- **先决条件:** 可脚本化上报用量的确定性提供商夹具;占用接近硬边界的会话;不使用真实凭据。
+- **步骤:** 连续上报低于预测的用量,确认显示占用不低于下限且压缩仍然触发;上报远离合理区间的用量,确认数值不动;跑一个
+ 中文为主的会话,比较显示占用与上报用量。
+- **预期:** 校准后的占用保持在原始估算的 0.85×–6× 之内;有证据即上调;只有方向一致的样本才下调;处在硬限制 1.18× 的
+ 投影仍触发压缩;误报不产生任何方向的移动;中文文本不再只有实测成本的四分之一。
+- **规格:** 03-runtime/02-agent-runtime §5.1、08-meta/decisions-log D606。**验收:** C(对话与流)、F(持久化)、品质。
+ **状态:** 单元测试覆盖(含中文与边界用例);桌面 E2E 待补。
+
+ ### E2E-RUNTIME-unique-tool-call-ids-per-request
+
+ - **先决条件:** 一份把同一次工具调用携带两次的会话转录(重试追加让该调用落在第二个行 id 上),在确定性提供商夹具下加载进
+ 重新创建的运行时;不使用真实凭据。
+ - **步骤:** 发一条提示,使运行时组装并发出请求。读取夹具收到的出站请求与 `agent` 日志通道。再用工具调用本就唯一的转录重复一次。
+ - **预期:** 出站请求对那个 id 只携带一个 `toolCall` 与恰好一个对应结果,因此提供商不可能回 `tool_use ids must be unique`;
+ 日志通道出现一行,带会话与 id。唯一转录的请求逐字不变,且不产生任何日志。
+ - **规格:** 03-runtime/02-agent-runtime §5、08-meta/decisions-log D608。**验收:** C(对话与流)、品质。
+ **里程碑:** Post-MVP 回归覆盖。
+ - **自动化:** `packages/agent-runtime/src/runtime.test.ts` 用真实的运行时覆盖两半:重复历史(丢弃 + 一行日志)与唯一历史
+ (同一对象、无日志)。
+ - **状态:** 单元测试覆盖;没有端到端驱动对重复转录发出真实提供商请求。
+
### E2E-MCP-HTTP-ACK — HTTP acknowledgement and authorization status
- **Preconditions**: A local mock Streamable HTTP server returns JSON for
diff --cc docs/zh-CN/spec/08-meta/decisions-log.md
index 036d0c6d3,ceaff29b5..63cffb59f
--- a/docs/zh-CN/spec/08-meta/decisions-log.md
+++ b/docs/zh-CN/spec/08-meta/decisions-log.md
@@@ -4600,15 -4599,16 +4599,26 @@@ that amendment are retired by ADR 0268
或权限。见 `04-ux/08-component-spec.md` 与
E2E-CHAT-opaque-floating-decision-and-retry-surfaces。
+## 2026-09-21 —— 上下文估算采用保守校准(D606)
+
+- 所有预算阈值读取同一个数字,该数字按请求的真实开销校正。pi 的估算器以最后一条助手用量为锚、其余按 `chars / 4` 估算,
+ 这会低估中文文本约 2–4 倍;没有锚点时还会完全漏掉系统提示与工具结构。
+- 两类误差分开应用:逐字符偏差是按比例(与量级无关)作用于猜测尾部;无锚点残差只在量级相当的样本(0.5×–2×)上按比例
+ 应用,否则只加观测到的固定开销(上限 32,000 词元)。因此 100k 量级的样本不会作为比例系数套到 1M 的投影上。
+- 校正不对称:向上修正有三个观测即生效;向下修正需要三个方向一致的样本、每次最多 15 %,且不会低于原始估算的 85 %——
+ 处在硬限制 1.18× 的投影仍会压缩。偏离预测 0.5×–3× 的报告视为误报,连续两次误报冻结向下修正。
- 见 `03-runtime/02-agent-runtime.md` §5.1。
++ 见 `03-runtime/02-agent-runtime §5.1`。
++
+ ## 2026-09-21 —— 每个请求里的工具调用 id 必须唯一(D608,issue #718)
+
+ - Anthropic 系端点(含 DeepSeek)在请求中同一个调用 id 出现两次时,会以 `tool_use ids must be unique` 拒绝整个回合,
+ 期间该会话无法继续。转录是仅追加的快照流、容忍重试造成的重复追加,因此同一次调用可能两次进入组装后的上下文:同一个行 id
+ (宿主按 keep-last 读取时已折叠)或两个不同行 id(宿主无法折叠)。
+ - 因此上线前的最后一个视图对每个 `toolCall` id 只保留第一次出现,丢弃其后重复的调用或结果,使提供商校验的「一调用一结果」
+ 配对保持完整;没有重复的请求原样返回(返回同一对象,而非副本)。磁盘上的内容不会被改写,压缩与保留规则也不变。
+ - 一旦发生丢弃,会在 `agent` 日志通道上报告一次,带上会话与 id,使下一次同类报障能指向写入方而不只是提供商那句话。
- 见 `03-runtime/02-agent-runtime.md` §5。
++ 见 `03-runtime/02-agent-runtime §5`。
+ - 守卫刻意放在请求边界而不是历史重建处:这样也能覆盖**会话运行期间**产生的重复,而重建期的过滤看不到它。
## 2026-09-21 —— 插件崩溃上报带上退出码但不复制原始输出(D607,issue #747)