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: 末尾分页保留最后消息后的压缩检查点
geng yue4d4a17d7eaa7
diff --git a/crates/host-core/src/transcripts.rs b/crates/host-core/src/transcripts.rsindex 46c7ce55c..e42974be2 100644--- a/crates/host-core/src/transcripts.rs+++ b/crates/host-core/src/transcripts.rs@@ -1028,8 +1028,8 @@ pub fn read_transcript(data_dir: &Path, session_id: &str) -> Result<Vec<MessageR/// `message_start` and `message_limit` are physical message-line positions,/// the same coordinate space as [`TranscriptLayout::message_count`]. Unlike a/// sequential scan, the cost here is proportional to the window, not to the-/// history in front of it. Compaction lines are always returned in full: the-/// chain is small and the newest element is required for model context.+/// history in front of it. Bounded reads include the active checkpoint and+/// checkpoints inside the window, including trailing ones for a tail window.pub fn read_transcript_window_with_layout(data_dir: &Path,session_id: &str,@@ -1066,11 +1066,15 @@ pub fn read_transcript_window_with_layout({picked.push(active);}- let window_end = layout- .message_offsets- .get(end.saturating_sub(1))- .copied()- .unwrap_or(window_start);+ let window_end = if end == total && end > start {+ layout.file_len+ } else {+ layout+ .message_offsets+ .get(end.saturating_sub(1))+ .copied()+ .unwrap_or(window_start)+ };picked.extend(layout.compaction_offsets@@ -1832,6 +1836,48 @@ mod tests {assert_eq!(ids, sequential_ids);}+ #[test]+ fn layout_tail_window_keeps_trailing_compactions_after_restart() {+ let dir = tempdir().unwrap();+ append_message(dir.path(), "s1", "now", &record("m1", "one")).unwrap();+ append_compaction(dir.path(), "s1", "now", &compaction()).unwrap();+ append_message(dir.path(), "s1", "now", &record("m2", "two")).unwrap();+ let mut latest = compaction();+ latest.id = "cp2".into();+ latest.through_message_id = "m2".into();+ latest.details = Some(json!({ "generation": 2, "contextTokens": 26_136 }));+ append_compaction(dir.path(), "s1", "now", &latest).unwrap();+ let mut final_checkpoint = latest.clone();+ final_checkpoint.id = "cp3".into();+ final_checkpoint.details = Some(json!({ "generation": 3, "contextTokens": 20_619 }));+ append_compaction(dir.path(), "s1", "now", &final_checkpoint).unwrap();++ for _ in 0..2 {+ let layout = refresh_layout(dir.path(), "s1", TranscriptLayout::default()).unwrap();+ for start in [0, 1] {+ let read =+ read_transcript_window_with_layout(dir.path(), "s1", &layout, start, Some(100))+ .unwrap();+ assert_eq!(read.compactions.len(), 3);+ assert_eq!(read.compactions.last().unwrap().id, "cp3");+ assert_eq!(+ read.compactions.last().unwrap().details,+ final_checkpoint.details+ );+ }+ let middle =+ read_transcript_window_with_layout(dir.path(), "s1", &layout, 0, Some(1)).unwrap();+ assert!(middle.compactions.is_empty());+ let empty =+ read_transcript_window_with_layout(dir.path(), "s1", &layout, 1, Some(0)).unwrap();+ assert!(empty.messages.is_empty());+ assert!(empty+ .compactions+ .iter()+ .all(|checkpoint| checkpoint.id != "cp2" && checkpoint.id != "cp3"));+ }+ }+#[test]fn layout_window_always_returns_the_whole_compaction_chain() {let dir = tempdir().unwrap();diff --git a/docs/spec/03-runtime/04-data-storage.md b/docs/spec/03-runtime/04-data-storage.mdindex 71a4ba965..90459f5d4 100644--- a/docs/spec/03-runtime/04-data-storage.md+++ b/docs/spec/03-runtime/04-data-storage.md@@ -1622,6 +1622,11 @@ prefix length those offsets describe. Serving a window seeks to its firstselected line instead of parsing the history in front of it, so the commoncached read is proportional to the requested page. Consequences:+- A bounded tail page includes checkpoints appended after its last message,+ so reopening a just-compacted session returns the newest checkpoint and its+ context estimate. Middle pages retain the active checkpoint and only those+ within their message offsets; they do not load the complete chain (#2163).+- The layout is derived data, cached in memory and, when reliable file identityis available, in the sibling `<sessionId>.jsonl.layout` file. Unix uses thedevice and inode; Windows uses the volume serial number and file ID. If thediff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.mdindex 65cb9b8cc..b9ad78624 100644--- a/docs/spec/06-delivery/04-e2e-test-plan.md+++ b/docs/spec/06-delivery/04-e2e-test-plan.md@@ -20341,3 +20341,7 @@ Scenario: E2E-REVIEW-ROUNDS (local desktop, implemented)Existing Windows child/grandchild cleanup tests remain required; record nativefailures separately from protocol success.- Exercise a late stop with an already reaped child before the supervisor drains the pipes. Verify the original exit outcome, final output and stable elapsed time across reads. Stall capability discovery or the first Host observation to exercise the overall wait deadline. The Electron transcript fixture renders a pending Bash stop and a mixed job list.++#### E2E-ISSUE-2163++Bounded tail pages include compactions appended after the last message. Middle pages still read only the active checkpoint and records inside their offsets. Verify append-message/append-compaction, tail reopening and Host restart with `scripts/e2e-compaction-tail.mjs` and `layout_tail_window` tests.diff --git a/docs/zh-CN/spec/03-runtime/04-data-storage.md b/docs/zh-CN/spec/03-runtime/04-data-storage.mdindex c23e8a78c..676960c06 100644--- a/docs/zh-CN/spec/03-runtime/04-data-storage.md+++ b/docs/zh-CN/spec/03-runtime/04-data-storage.md@@ -1476,3 +1476,8 @@ An unknown price is distinct from a known zero price; partial known costs remainon the individual operations. Late usage targets its captured turn and does notrevive it or debit the currently active turn. Immediate nested parent and owningTask remain separate optional transcript/event fields.++### 末尾压缩检查点读取(#2163)++有界末尾页包含最后一条消息之后追加的压缩记录,重新打开或重启后立即返回+最新检查点和上下文估算。历史中间页只保留生效的检查点及页内记录,不读取全链。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.mdindex eeb95cae6..d4f712941 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@@ -10390,3 +10390,7 @@ Electron 一侧——工具自动签发、指示器、"停止"中止轮次、绘- runtime 测试补充新输入和父回合取消、延迟 RPC 错误、旧 Host/远程能力缺失、混合选择器拒绝、原委派控制及无子代理配置的工具注册。- Host 测试覆盖先完成后等待、重复读取、Unicode 截断、归属查找、幂等停止与停止请求待确认。Windows 子孙进程清理测试仍须执行,原生失败单独报告。- 在子进程已退出、监管任务尚未排空管道时请求停止,检查原退出结果、最终输出和重复读取的耗时。延迟能力探测或第一次 Host 观察,检查等待总时限。Electron 会话记录用例渲染尚待确认的 Bash 停止请求和混合作业列表。++#### E2E-ISSUE-2163++末尾分页包含最后一条消息之后追加的检查点,中间页仍只读取生效及页内记录。验证消息、压缩追加后读取及 Host 重启,最新检查点与上下文估算保留,历史用量不变。diff --git a/scripts/e2e-compaction-tail.mjs b/scripts/e2e-compaction-tail.mjsnew file mode 100644index 000000000..bde889371--- /dev/null+++ b/scripts/e2e-compaction-tail.mjs@@ -0,0 +1,39 @@+import assert from "node:assert/strict";+import { mkdtemp, mkdir, rm } from "node:fs/promises";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { Host, resolveHostBinary } from "./e2e/host.mjs";+import { createSession } from "./e2e/session.mjs";++const temp = await mkdtemp(join(tmpdir(), "pi-compaction-tail-"));+const workspace = join(temp, "workspace");+const host = new Host(resolveHostBinary(), join(temp, "data"));+try {+ await mkdir(workspace);+ await host.start();+ const session = await createSession(host, workspace, "Trailing checkpoints");+ const createdAt = "2026-11-07T00:00:00Z";+ const append = (id) => host.call("session.appendMessage", { sessionId: session.id,+ message: { id, role: "assistant", content: id, usage: { inputTokens: 267261, outputTokens: 0, totalTokens: 267261 }, createdAt } });+ const compact = (id, throughMessageId, contextTokens) => host.call("session.appendCompaction", {+ sessionId: session.id, compaction: { id, throughMessageId, summary: "Synthetic summary", tokensBefore: 267261,+ details: { generation: id === "cp1" ? 1 : 2, contextTokens }, createdAt } });+ await append("m1");+ await compact("cp1", "m1", 40000);+ await append("m2");+ await compact("cp2", "m2", 26136);+ const verify = async () => {+ const { session: page } = await host.call("session.get", { id: session.id, messageLimit: 100 });+ assert.deepEqual(page.compactions.map(checkpoint => checkpoint.id), ["cp1", "cp2"]);+ assert.equal(page.compaction.id, "cp2");+ assert.equal(page.compaction.details.contextTokens, 26136);+ assert.equal(page.messages.at(-1).usage.totalTokens, 267261);+ };+ await verify();+ await host.restart();+ await verify();+ console.log("COMPACTION_TAIL " + JSON.stringify({ ok: true, restart: true, contextTokens: 26136 }));+} finally {+ await host.stop();+ await rm(temp, { recursive: true, force: true });+}