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(chat): resolve POSIX-rooted Windows drive file refs
v_wyxgwang8a1f7e5889f5
diff --git a/apps/desktop/test/chat-ref-resolve.test.mjs b/apps/desktop/test/chat-ref-resolve.test.mjsindex 3d71456fd..93bf796aa 100644--- a/apps/desktop/test/chat-ref-resolve.test.mjs+++ b/apps/desktop/test/chat-ref-resolve.test.mjs@@ -101,6 +101,30 @@ test("an exact absolute path with spaces resolves only inside an allowed root",assert.equal(await isChatRefOutsideRoots(absolute, roots({ workspace })), false);assert.equal(await isChatRefOutsideRoots(join(dirname(workspace), "page.md"), roots({ workspace })), true);});+/**+ * A transcript link can root a drive path the POSIX way (`/c:/dir/file.md`),+ * the shape a Markdown link copies from a POSIX example. Windows reads the+ * leading `/` as a filesystem root and everything after it as directories, so+ * the click missed a file that exists; only that shape is repaired.+ */+test("a POSIX-rooted drive path resolves to the file the drive names", async (t) => {+ if (process.platform !== "win32") return t.skip("Windows drive spelling");+ const workspace = tempTree("posix-drive", ["docs/spec/page.md"]);+ const posixRooted = `/${workspace.slice(0, 2).toLowerCase()}${workspace+ .slice(2)+ .replaceAll("\\", "/")}/docs/spec/page.md`;+ const match = await resolve(posixRooted, { workspace });+ assert.equal(match?.root, "workspace");+ assert.equal(match?.matchedBy, "exact-absolute");+ assert.equal(match?.relativePath, "docs/spec/page.md");+ assert.equal(match?.absolutePath, join(workspace, "docs", "spec", "page.md"));+ assert.equal(await isChatRefOutsideRoots(posixRooted, roots({ workspace })), false);+ // A positioned reference still works: the line suffix is stripped first.+ assert.equal((await resolve(`${posixRooted}:12`, { workspace }))?.relativePath, "docs/spec/page.md");+ // A POSIX path without a drive letter is not a drive path anywhere.+ assert.equal(await isChatRefOutsideRoots("/root/dir/page.md", roots({ workspace })), true);+});+test("an absolute realpath alias resolves under the same registered root", async () => {const workspace = tempTree("alias-target", ["my project/page.md"]);diff --git a/docs/spec/03-runtime/01-ipc-protocol.md b/docs/spec/03-runtime/01-ipc-protocol.mdindex 1f98bfe24..a72c3d51d 100644--- a/docs/spec/03-runtime/01-ipc-protocol.md+++ b/docs/spec/03-runtime/01-ipc-protocol.md@@ -2021,6 +2021,10 @@ Renderer IPC kept for the Plan-safe preview facade and URL fallback:`fs/open`, the image reader) already accept it, so such a path completesinstead of reporting a restriction. A shorthand never completes from thatwider store; it still searches the session's own one. A reference that matches+ A Windows drive path rooted the POSIX way (`/c:/dir/file.md`, the shape a+ Markdown link copies from a POSIX example) is read as `C:\dir\file.md` before+ matching, so that spelling reaches the file its drive names instead of+ reporting a miss; a POSIX host keeps `/c:/...`, which is a real path there.nothing returns `match: null`. An exact absolute path outside every allowedroot never tries a same-name file inside a root; it is answered by the`fs/read` external-file rule instead (ADR 0339): an existing regular filediff --git a/docs/zh-CN/spec/03-runtime/01-ipc-protocol.md b/docs/zh-CN/spec/03-runtime/01-ipc-protocol.mdindex 186d74803..3419121c8 100644--- a/docs/zh-CN/spec/03-runtime/01-ipc-protocol.md+++ b/docs/zh-CN/spec/03-runtime/01-ipc-protocol.md@@ -1461,7 +1461,7 @@ Chrome 和代理 CDP 位于随应用打包的 `pi.browser` 插件中,通过 `p- `fs/readImageDataUrl({ref, mimeType?})` → `FsImageDataUrlResult`(`image` 带 `dataUrl`,或 `missing` / `notImage` / `tooLarge`)。只做根包含检查,不适用 ADR 0339 外部文件规则,因此模型写出的图片引用无法渲染任意本地图片。从不返回非图片字节。仅渲染器使用,不是插件宿主 API。- `fs/reveal({path})` → 在 Finder 中显示。规则与 `fs/read` 相同,包括 ADR 0339 外部文件规则。- `fs/open({path, mimeType?})` → 用系统默认应用打开已有的普通文件。与 `fs/read` 规则相同:校验真实路径包含范围、拒绝通过符号链接逃逸,并适用 ADR 0339 外部文件规则。对于声明为 `video/mp4` 的无后缀 `attachments/<sha256>` blob,宿主在私有应用数据目录建立 `.mp4` 符号链接后再交给系统,不复制视频字节。-- `fs/resolveRef({ref, sessionId?})` → `FsChatRefResolveResult`(`{ match: FsChatRefMatch | null }`,match 指出应答的 `root`(`workspace` / `scratch` / `attachments` / `external`)、相对该应答根的 `relativePath`、绝对路径 `absolutePath` 与 `matchedBy`(`exact-relative` / `exact-absolute` / `path-suffix` / `basename`),以及在 `workspace` 命中时给出的 `projectRoot`(`{ path, name, primary }`,指出是哪个文件夹应答的));`sessionId` 决定查哪个会话的临时目录。隔离会话以 `worktree.location` 而不是父项目 checkout 作为查找与读取的根;落在父 checkout 内的绝对引用会先映射到 worktree 中的同一相对路径(该文件存在时),从而打开分支上的副本,而不是报 `outside-allowed-roots`。它补全智能体在聊天里打印的文件引用,因为渲染器看不到会话自己的临时目录:已经在某个已知根内指向真实文件的绝对引用直接胜出,`attachments/<sha256>` blob 直接对附件库解析;否则按优先级顺序搜索各根——整个打开的项目、再会话自己的临时目录(`<data_dir>/scratch/<sessionId>/`,ADR 0124)、最后附件库——第一个给出结果的根胜出。项目指的是打开的工作区背后的文件夹组(ADR 0249):主文件夹先应答,其余文件夹随后按项目组自身顺序搜索(ADR 0263),因此简写落在同级文件夹里和落在主文件夹里一样自然,命中结果也指出是哪个文件夹应答的。同一个根内精确路径优先于简写;简写之间最长匹配尾优先,其次路径更浅者。文件面板的忽略集合同样生效。什么都没匹配到时返回 `match: null`。位于所有受管根之外的精确绝对路径不会回退到根内同名文件,而是按外部文件规则处理(ADR 0339):指向存在的普通文件时返回 `root: "external"`(`matchedBy: "exact-absolute"`,`relativePath` 为文件名);没有文件时返回 `match: null`;`reason: "outside-allowed-roots"` 只留给位于受管根之外、又不是精确绝对路径的引用。`fs/read`、`fs/reveal`、`fs/open` 采用同一规则,`fs/readImageDataUrl` 仍只做根包含检查。解析本身不打开任何东西(ADR 0262)。+- `fs/resolveRef({ref, sessionId?})` → `FsChatRefResolveResult`(`{ match: FsChatRefMatch | null }`,match 指出应答的 `root`(`workspace` / `scratch` / `attachments` / `external`)、相对该应答根的 `relativePath`、绝对路径 `absolutePath` 与 `matchedBy`(`exact-relative` / `exact-absolute` / `path-suffix` / `basename`),以及在 `workspace` 命中时给出的 `projectRoot`(`{ path, name, primary }`,指出是哪个文件夹应答的));`sessionId` 决定查哪个会话的临时目录。隔离会话以 `worktree.location` 而不是父项目 checkout 作为查找与读取的根;落在父 checkout 内的绝对引用会先映射到 worktree 中的同一相对路径(该文件存在时),从而打开分支上的副本,而不是报 `outside-allowed-roots`。它补全智能体在聊天里打印的文件引用,因为渲染器看不到会话自己的临时目录:已经在某个已知根内指向真实文件的绝对引用直接胜出,`attachments/<sha256>` blob 直接对附件库解析;否则按优先级顺序搜索各根——整个打开的项目、再会话自己的临时目录(`<data_dir>/scratch/<sessionId>/`,ADR 0124)、最后附件库——第一个给出结果的根胜出。项目指的是打开的工作区背后的文件夹组(ADR 0249):主文件夹先应答,其余文件夹随后按项目组自身顺序搜索(ADR 0263),因此简写落在同级文件夹里和落在主文件夹里一样自然,命中结果也指出是哪个文件夹应答的。同一个根内精确路径优先于简写;简写之间最长匹配尾优先,其次路径更浅者。文件面板的忽略集合同样生效。什么都没匹配到时返回 `match: null`。以 POSIX 方式给盘符路径加根的写法(`/c:/dir/file.md`,Markdown 链接常照 POSIX 示例写成这样)在匹配前按 `C:\dir\file.md` 读取,因此该写法命中的就是盘符所指的那个文件;POSIX 主机保留 `/c:/...`,在那里它就是真实路径。位于所有受管根之外的精确绝对路径不会回退到根内同名文件,而是按外部文件规则处理(ADR 0339):指向存在的普通文件时返回 `root: "external"`(`matchedBy: "exact-absolute"`,`relativePath` 为文件名);没有文件时返回 `match: null`;`reason: "outside-allowed-roots"` 只留给位于受管根之外、又不是精确绝对路径的引用。`fs/read`、`fs/reveal`、`fs/open` 采用同一规则,`fs/readImageDataUrl` 仍只做根包含检查。解析本身不打开任何东西(ADR 0262)。- `fs/list` 仍只限工作区;外面的遍历被拒绝(`INVALID_ARGUMENT`)。## 13b. 桌面菜单和窗口 APIdiff --git a/packages/host-runtime/src/chat-ref-resolve.ts b/packages/host-runtime/src/chat-ref-resolve.tsindex fb6aa4d1e..c5921b6a7 100644--- a/packages/host-runtime/src/chat-ref-resolve.ts+++ b/packages/host-runtime/src/chat-ref-resolve.ts@@ -92,12 +92,35 @@ function matchesSegment(candidate: string, wanted: string): boolean {return CASE_INSENSITIVE_FS && candidate.toLowerCase() === wanted.toLowerCase();}-/** Strip the composer sigil, optional quotes, and a file line reference. */+/**+ * Strip the composer sigil, optional quotes, and a file line reference, then+ * repair a Windows drive path that arrived rooted the POSIX way.+ */function cleanRef(raw: string): string {let value = stripRefDecorations(String(raw ?? ""));if (value.includes("\0")) return "";value = value.replace(/(?::\d+(?::\d+|-\d+)?|#L\d+(?:-L?\d+)?)$/i, "");- return value;+ return normalizeDriveRef(value);+}++/**+ * A chat link may root a Windows drive path the way a POSIX example does:+ * `/f:/project/file.md` instead of `F:\project\file.md`. Both the transcript's+ * Markdown links and the agent's own prose spell paths that way, and on Windows+ * the leading `/` turns the drive into an ordinary directory name — `resolve()`+ * builds `F:\f:\project\file.md`, `realpath` misses, and a file that exists is+ * reported as missing.+ *+ * Only that shape is repaired here. Existence, root priority, and containment+ * stay the resolver's own decisions, and a POSIX host keeps `/c:/...`, which is+ * a real path there.+ */+const POSIX_ROOTED_DRIVE = /^\/+([A-Za-z]):\//;++function normalizeDriveRef(value: string): string {+ if (process.platform !== "win32") return value;+ const match = POSIX_ROOTED_DRIVE.exec(value);+ return match ? `${match[1].toUpperCase()}:/${value.slice(match[0].length)}` : value;}function stripRefDecorations(raw: string): string {