Skip to content

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

fix(desktop): reject dangling chat reference ancestors

cauliflower90f5838b6321
diff --git a/apps/desktop/electron/main/chat-ref-resolve.ts b/apps/desktop/electron/main/chat-ref-resolve.ts
index b5ff39093..6dda83f0a 100644
--- a/apps/desktop/electron/main/chat-ref-resolve.ts
+++ b/apps/desktop/electron/main/chat-ref-resolve.ts
@@ -155,14 +155,6 @@ async function isRegularFile(target: string): Promise<boolean> {
}
}
-async function isSymbolicLink(target: string): Promise<boolean> {
- try {
- return (await lstat(target)).isSymbolicLink();
- } catch {
- return false;
- }
-}
-
/** Root-relative POSIX path, or null when `absolute` is outside `rootPath`. */
function relativeInside(rootPath: string, absolute: string): string | null {
const rel = relative(rootPath, absolute);
@@ -278,6 +270,21 @@ async function isContainedRegularFile(root: string, relativePath: string): Promi
return realPath !== null && isRegularFile(realPath);
}
+async function hasInvalidLinkAncestor(root: string, segments: readonly string[]): Promise<boolean> {
+ for (let length = 1; length <= segments.length; length += 1) {
+ const prefixSegments = segments.slice(0, length);
+ try {
+ if ((await lstat(join(root, ...prefixSegments))).isSymbolicLink()
+ && !(await resolveRealPathWithinRoot(root, prefixSegments.join("/")))) return true;
+ } catch (error) {
+ const code = (error as NodeJS.ErrnoException).code;
+ // Only ordinary missing paths can fall back to an indexed suffix match.
+ if (code !== "ENOENT" && code !== "ENOTDIR") return true;
+ }
+ }
+ return false;
+}
+
/** Bounded walk for trees with no git index (session scratch, attachments). */
async function walkRootFiles(
rootPath: string,
@@ -414,7 +421,7 @@ export async function resolveChatFileRef(
...(root.projectRoot ? { projectRoot: root.projectRoot } : {}),
};
}
- if (await isSymbolicLink(absolutePath)) return null;
+ if (await hasInvalidLinkAncestor(root.path, parsed.segments)) return null;
const candidates = fuzzyCandidates(
await listRootFiles(root.kind, root.path),
tails,
diff --git a/apps/desktop/test/chat-ref-resolve.test.mjs b/apps/desktop/test/chat-ref-resolve.test.mjs
index c1dbec649..7b96f7a4a 100644
--- a/apps/desktop/test/chat-ref-resolve.test.mjs
+++ b/apps/desktop/test/chat-ref-resolve.test.mjs
@@ -158,10 +158,18 @@ test("a stale fuzzy index skips escaped and missing candidates in priority order
assert.equal((await resolve("page.md", { workspace }))?.relativePath, "c/page.md");
});
-test("a dangling relative link does not report a match", async () => {
- const workspace = tempTree("dangling-link", ["other/linked"]);
+test("a dangling relative link, including a middle directory, does not fall back", async () => {
+ const workspace = tempTree("dangling-link", ["other/linked/page.md"]);
symlinkSync(join(workspace, "missing"), join(workspace, "linked"), process.platform === "win32" ? "junction" : "dir");
- assert.equal(await resolve("linked", { workspace }), null);
+ assert.equal(await resolve("linked/page.md", { workspace }), null);
+
+ const terminal = tempTree("dangling-terminal", ["other/linked"]);
+ symlinkSync(join(terminal, "missing"), join(terminal, "linked"), process.platform === "win32" ? "junction" : "dir");
+ assert.equal(await resolve("linked", { workspace: terminal }), null);
+
+ const scratch = tempTree("dangling-scratch", ["other/linked/page.md"]);
+ symlinkSync(join(scratch, "missing"), join(scratch, "linked"), process.platform === "win32" ? "junction" : "dir");
+ assert.equal(await resolve("linked/page.md", { scratch }), null);
});
test("a longer matching tail beats a bare leaf name", async () => {