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(mcp): preserve small session/get details
vastsa5b04e47751c2
diff --git a/apps/desktop/electron/main/mcp-control.ts b/apps/desktop/electron/main/mcp-control.tsindex 63068789d..4f270d049 100644--- a/apps/desktop/electron/main/mcp-control.ts+++ b/apps/desktop/electron/main/mcp-control.ts@@ -527,12 +527,22 @@ export function stripSecretMaterial(value: unknown): unknown {return output;}-export function boundMcpResult(value: unknown): unknown {- let text: string;+function serializeMcpResult(value: unknown): string {try {- text = JSON.stringify(value ?? null);+ return JSON.stringify(value ?? null);} catch {- text = JSON.stringify({ value: String(value) });+ return JSON.stringify({ value: String(value) });+ }+}++export function boundMcpResult(+ value: unknown,+ projectOversized?: (value: unknown) => unknown,+): unknown {+ let text = serializeMcpResult(value);+ if (text.length > MAX_RESULT_CHARS && projectOversized) {+ value = projectOversized(value);+ text = serializeMcpResult(value);}if (text.length <= MAX_RESULT_CHARS) {try {@@ -1149,11 +1159,11 @@ export class McpControlServer {if (!tool) return { response: rpcError(id, -32602, `unknown tool: ${name}`) };try {const raw = await tool.execute(input);- // `pi_session_get` can carry an unbounded compaction record; project it- // to the compact control-plane shape before bounding so the transcript- // survives (mocode #495).+ // Preserve ordinary session details; project oversized session/get+ // compaction metadata only before falling back to the truncation envelope.const value = boundMcpResult(- name === "pi_session_get" ? projectSessionGetResult(raw) : raw,+ raw,+ name === "pi_session_get" ? projectSessionGetResult : undefined,);return {response: response(id, {diff --git a/apps/desktop/test/mcp-control.test.mjs b/apps/desktop/test/mcp-control.test.mjsindex 6e89ab451..acb5e1813 100644--- a/apps/desktop/test/mcp-control.test.mjs+++ b/apps/desktop/test/mcp-control.test.mjs@@ -82,6 +82,36 @@ test("local MCP control server authenticates, discovers, and invokes desktop opeconst dataDir = mkdtempSync(join(tmpdir(), "pi-mcp-control-"));const calls = [];const events = [];+ const sessionMessages = [+ { id: "m1", role: "user", content: "hello" },+ { id: "m2", role: "assistant", content: "hi" },+ ];+ const largeSession = {+ session: {+ id: "large-session",+ compaction: {+ id: "cp-large",+ createdAt: "2026-10-03T07:37:04.094Z",+ summary: "x".repeat(700_000),+ retainedTail: [{ content: "large" }],+ details: { generation: 13, modifiedFiles: ["src/a.ts"] },+ },+ messages: sessionMessages,+ },+ };+ const smallSession = {+ session: {+ id: "small-session",+ compaction: {+ id: "cp-small",+ createdAt: "2026-10-03T07:37:04.094Z",+ summary: "Keep this summary",+ retainedTail: [{ content: "Keep this reply" }],+ details: { generation: 2, modifiedFiles: ["src/b.ts"] },+ },+ messages: sessionMessages,+ },+ };const server = new McpControlServer({dataDir,port: 0,@@ -98,6 +128,10 @@ test("local MCP control server authenticates, discovers, and invokes desktop opeif (channel === "pi-desktop/session/create") {return { session: { id: "session-created", projectPath: args[0]?.projectPath ?? null } };}+ if (channel === "pi-desktop/session/get") {+ if (args[0]?.id === largeSession.session.id) return largeSession;+ if (args[0]?.id === smallSession.session.id) return smallSession;+ }return { ok: true };},onOperationComplete: (operation, result, args) => {@@ -393,6 +427,39 @@ test("local MCP control server authenticates, discovers, and invokes desktop opeassert.equal(readSession.body.result.isError, undefined);assert.equal(events.filter(Boolean).length, refreshCount);+ const readLargeSession = await post(+ info.url,+ info.token,+ {+ jsonrpc: "2.0",+ id: 16,+ method: "tools/call",+ params: { name: "pi_session_get", arguments: { id: largeSession.session.id } },+ },+ { "Mcp-Session-Id": sessionId },+ );+ const largeResult = readLargeSession.body.result.structuredContent;+ assert.equal(largeResult.truncated, undefined);+ assert.deepEqual(largeResult.session.messages, sessionMessages);+ assert.equal(largeResult.session.compaction.createdAt, largeSession.session.compaction.createdAt);+ assert.equal(largeResult.session.compaction.details.generation, 13);+ assert.equal(largeResult.session.compaction.summary, undefined);+ assert.equal(largeResult.session.compaction.retainedTail, undefined);+ assert.equal(largeResult.session.compaction.details.modifiedFiles, undefined);++ const readSmallSession = await post(+ info.url,+ info.token,+ {+ jsonrpc: "2.0",+ id: 17,+ method: "tools/call",+ params: { name: "pi_session_get", arguments: { id: smallSession.session.id } },+ },+ { "Mcp-Session-Id": sessionId },+ );+ assert.deepEqual(readSmallSession.body.result.structuredContent, smallSession);+const missingPath = await post(info.url,info.token,@@ -541,8 +608,7 @@ test("session/get compaction metadata is projected before bounding (mocode #495)const unprojected = boundMcpResult(raw);assert.equal(unprojected.truncated, true);- const projected = projectSessionGetResult(raw);- const bounded = boundMcpResult(projected);+ const bounded = boundMcpResult(raw, projectSessionGetResult);assert.notEqual(bounded.truncated, true, "projected answer must fit the limit");const session = bounded.session;@@ -562,15 +628,23 @@ test("session/get projection leaves a small session untouched (mocode #495)", ()const raw = {session: {id: "s1",- compaction: { createdAt: "t", details: { generation: 1 } },+ compaction: {+ id: "cp1",+ createdAt: "t",+ summary: "A small summary",+ retainedTail: [{ role: "assistant", content: "A retained reply" }],+ details: { generation: 1, modifiedFiles: ["src/a.ts"] },+ },messages: [],},};- const projected = projectSessionGetResult(raw);- assert.deepEqual(projected.session.compaction, raw.session.compaction);+ assert.deepEqual(boundMcpResult(raw, projectSessionGetResult), raw);// Non-session shapes and sessions without a compaction record pass through.- assert.deepEqual(projectSessionGetResult({ ok: true }), { ok: true });- assert.deepEqual(projectSessionGetResult({ session: { id: "s2" } }), { session: { id: "s2" } });+ assert.deepEqual(boundMcpResult({ ok: true }, projectSessionGetResult), { ok: true });+ assert.deepEqual(+ boundMcpResult({ session: { id: "s2" } }, projectSessionGetResult),+ { session: { id: "s2" } },+ );});test("renderer refresh events fire only for mutating control operations", () => {diff --git a/docs/spec/03-runtime/01-ipc-protocol.md b/docs/spec/03-runtime/01-ipc-protocol.mdindex 1ca52e571..98d7aa37f 100644--- a/docs/spec/03-runtime/01-ipc-protocol.md+++ b/docs/spec/03-runtime/01-ipc-protocol.md@@ -2281,13 +2281,14 @@ error model. Both the text payload and `structuredContent` are size-bounded to512 KiB (`MAX_RESULT_CHARS`). A larger answer is not returned verbatim: it isreplaced by `{truncated: true, reason: "MCP_RESULT_LIMIT", preview: "<the first512 KiB of the JSON>"}`, so an external caller can never receive a silently-shortened payload. `session/get` (`pi_session_get`) additionally projects the-session's `compaction` record down to the compact identity (`createdAt`, and-`details.generation`) before bounding: a long session's-`ContextCompactionRecord` (`summary` / `retainedTail` / `details.modifiedFiles`)-grows without bound and would otherwise push the whole answer — including-`messages` — over the limit, leaving external callers unable to read any-transcript.+shortened payload. If an oversized answer comes from `session/get`+(`pi_session_get`) and has a `compaction` record, Main projects that record to+the compact identity (`createdAt` and `details.generation`) and checks the size+again before returning the truncation envelope. This lets a long session's+transcript survive when its unbounded `ContextCompactionRecord` (`summary` /+`retainedTail` / `details.modifiedFiles`) alone caused the overflow. Results+already under the limit retain their full compaction details, and the desktop's+own session detail is unchanged.The six `session/collaboration/*` operations are first-party-plugin-only: theyrequire an authenticated plugin tool invocation context, so they appear indiff --git a/docs/zh-CN/spec/03-runtime/01-ipc-protocol.md b/docs/zh-CN/spec/03-runtime/01-ipc-protocol.mdindex 2cc1cefae..c5682c4a1 100644--- a/docs/zh-CN/spec/03-runtime/01-ipc-protocol.md+++ b/docs/zh-CN/spec/03-runtime/01-ipc-protocol.md@@ -1800,11 +1800,12 @@ Electron 等待主机关闭之前会停止服务,并将清单标记为非活仍会经过现有 IPC 处理器的校验、主机权限、工作区边界和错误模型。文本负载和`structuredContent` 都有大小上限:512 KiB(`MAX_RESULT_CHARS`)。超出上限的答复不会被原样返回,而是替换为 `{truncated: true, reason: "MCP_RESULT_LIMIT", preview: "<JSON 前-512 KiB>"}`,因此外部调用方永远不会收到被静默缩短的负载。`session/get`-(`pi_session_get`)在设限前还会把会话的 `compaction` 记录投影为精简身份(`createdAt`-与 `details.generation`):长会话的 `ContextCompactionRecord`(`summary` /-`retainedTail` / `details.modifiedFiles`)会无界增长,否则会把整包答复——包括-`messages`——推过上限,令外部调用方读不到任何转写。+512 KiB>"}`,因此外部调用方永远不会收到被静默缩短的负载。如果超限答复来自+`session/get`(`pi_session_get`)且含有 `compaction` 记录,Main 会先将该记录投影为精简身份+(`createdAt` 与 `details.generation`),再复查大小,然后才返回截断信封。这样,当长会话中+无界增长的 `ContextCompactionRecord`(`summary` / `retainedTail` / `details.modifiedFiles`)+本身导致超限时,转写仍可完整返回。未超限的答复保留完整 compaction 详情;桌面自己的会话详情+保持不变。六个 `session/collaboration/*` 操作仅限第一方插件:它们要求经过认证的插件工具调用上下文,因此会出现在 `pi.desktop.listOperations` 中并可通过 `pi.desktop.invoke` 调用,但被排除在