Skip to content

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

fix(files): open a scratch path another session wrote

vastsafa2bd8389ec1
diff --git a/apps/desktop/electron/main/chat-ref-resolve.ts b/apps/desktop/electron/main/chat-ref-resolve.ts
index 6dda83f0a..9d6afa874 100644
--- a/apps/desktop/electron/main/chat-ref-resolve.ts
+++ b/apps/desktop/electron/main/chat-ref-resolve.ts
@@ -38,9 +38,25 @@ export type ChatRefRoots = {
*/
project?: readonly FsChatRefProjectRoot[] | null;
scratch?: string | null;
+ /**
+ * Roots an absolute reference may name without being searched by shorthand.
+ *
+ * A generated image lives in the scratch store of the session that produced
+ * it, and the user clicks it from whatever conversation they are reading —
+ * usually a later one. Every read guard (`fsRead`, `fsOpen`, the image
+ * reader, `fsReveal`) already accepts the whole scratch store, so completing
+ * against the session's own store alone reported a restriction for a file the
+ * app can open. These entries widen the absolute-path check only: a shorthand
+ * still searches the session's own root, so it never lands in another
+ * session's files.
+ */
+ containment?: readonly ChatRefContainmentRoot[] | null;
attachments?: string | null;
};
+/** A root an absolute reference may name, though no shorthand searches it. */
+export type ChatRefContainmentRoot = { kind: FsChatRefRoot; path: string };
+
/** A root to search, and — for a project folder — which folder it is. */
type ChatRefRootEntry = {
kind: FsChatRefRoot;
@@ -147,6 +163,23 @@ function orderedRoots(roots: ChatRefRoots): ChatRefRootEntry[] {
return list;
}
+/**
+ * The containment-only roots, in the same vocabulary as the searched ones.
+ *
+ * They answer absolute references and nothing else: the shorthand walk below
+ * keeps searching `orderedRoots`, so a reference is never completed from
+ * another session's store.
+ */
+function containmentEntries(roots: ChatRefRoots): ChatRefRootEntry[] {
+ const list: ChatRefRootEntry[] = [];
+ for (const root of roots.containment ?? []) {
+ const path = String(root?.path ?? "").trim();
+ if (!path) continue;
+ list.push({ kind: root.kind, path: resolve(path) });
+ }
+ return list;
+}
+
async function isRegularFile(target: string): Promise<boolean> {
try {
return (await stat(target)).isFile();
@@ -203,7 +236,7 @@ export async function isChatRefOutsideRoots(ref: string, roots: ChatRefRoots): P
const cleaned = cleanRef(ref);
if (!isAbsolute(cleaned)) return true;
const absolutePath = resolve(cleaned);
- const rootList = await canonicalRoots(orderedRoots(roots));
+ const rootList = await canonicalRoots([...orderedRoots(roots), ...containmentEntries(roots)]);
if (!couldShareVolume(absolutePath, rootList)) return true;
const targetPath = await canonicalPathOrMissingTail(absolutePath);
if (!targetPath) return !rootList.some((root) => relativeInside(root.path, absolutePath) !== null);
@@ -343,7 +376,7 @@ export async function resolveChatFileRef(
const parsed = parseChatRef(ref);
if (!parsed) return null;
const rootList = orderedRoots(roots);
- if (rootList.length === 0) return null;
+ if (rootList.length === 0 && !(roots.containment ?? []).length) return null;
const cleanedPosixRef = toPosix(cleanRef(ref));
const isAttachmentRef = /^attachments(?:\/|$)/i.test(cleanedPosixRef);
@@ -353,7 +386,12 @@ export async function resolveChatFileRef(
const cleaned = cleanRef(ref);
if (!isAbsolute(cleaned)) return null;
const absolutePath = resolve(cleaned);
- const canonicalRootList = await canonicalRoots(rootList);
+ // An absolute reference may also name a store the app reads but no shorthand
+ // searches, such as an image another session generated.
+ const canonicalRootList = await canonicalRoots([
+ ...rootList,
+ ...containmentEntries(roots),
+ ]);
if (!couldShareVolume(absolutePath, canonicalRootList)) return null;
let targetPath: string;
try {
diff --git a/apps/desktop/electron/main/ipc/workspace-ipc.ts b/apps/desktop/electron/main/ipc/workspace-ipc.ts
index 19ca8c428..45160bcaf 100644
--- a/apps/desktop/electron/main/ipc/workspace-ipc.ts
+++ b/apps/desktop/electron/main/ipc/workspace-ipc.ts
@@ -922,6 +922,10 @@ export function registerWorkspaceIpc({
const roots = {
project: projectRootsFor(workspaceRoot),
scratch: await sessionScratchRoot(input.sessionId),
+ // The read guards accept the whole scratch store, so an absolute image
+ // path from an earlier conversation completes instead of reporting a
+ // restriction; shorthands still search the session's own store alone.
+ containment: [{ kind: "scratch" as const, path: join(dataDir, "scratch") }],
attachments: join(dataDir, "attachments"),
};
if (await isChatRefOutsideRoots(ref, roots)) {
diff --git a/apps/desktop/test/chat-ref-resolve.test.mjs b/apps/desktop/test/chat-ref-resolve.test.mjs
index dbbe30208..533d86584 100644
--- a/apps/desktop/test/chat-ref-resolve.test.mjs
+++ b/apps/desktop/test/chat-ref-resolve.test.mjs
@@ -374,3 +374,44 @@ test("a project folder never loses to the scratch store", async () => {
assert.equal(match?.root, "workspace");
assert.equal(match?.projectRoot?.path, sibling);
});
+
+/**
+ * A generated image belongs to the session that made it, but the chat that
+ * shows it belongs to whatever session the user is reading. The read guards
+ * (`fsRead`, `fsOpen`, the image reader) already accept the whole scratch
+ * store, so completion must not report a restriction for a file the app can
+ * open — while a shorthand still never crosses into another session's files.
+ */
+test("an absolute path in the wider store opens though another session wrote it", async () => {
+ const store = tempTree("scratch-store", []);
+ const other = join(store, "session-b");
+ mkdirSync(other, { recursive: true });
+ const image = join(other, "generated-1.png");
+ writeFileSync(image, "png\n");
+ const scratch = join(store, "session-a");
+ mkdirSync(scratch, { recursive: true });
+ const containment = [{ kind: "scratch", path: store }];
+
+ assert.equal(await isChatRefOutsideRoots(image, roots({ scratch, containment })), false);
+ const match = await resolve(image, { scratch, containment });
+ assert.equal(match?.root, "scratch");
+ assert.equal(match?.matchedBy, "exact-absolute");
+ assert.equal(match?.absolutePath, image);
+});
+
+test("a shorthand still searches the session's own store and not the wider one", async () => {
+ const store = tempTree("scratch-store-shorthand", ["session-b/generated-1.png"]);
+ const scratch = join(store, "session-a");
+ mkdirSync(scratch, { recursive: true });
+ const containment = [{ kind: "scratch", path: store }];
+ assert.equal(await resolve("generated-1.png", { scratch, containment }), null);
+});
+
+test("an absolute path outside every root is still reported as restricted", async () => {
+ const store = tempTree("scratch-store-outside", []);
+ const outside = tempTree("outside-store", ["generated-1.png"]);
+ const image = join(outside, "generated-1.png");
+ const options = roots({ scratch: null, containment: [{ kind: "scratch", path: store }] });
+ assert.equal(await isChatRefOutsideRoots(image, options), true);
+ assert.equal(await resolveChatFileRef(image, options), null);
+});
diff --git a/docs/spec/03-runtime/01-ipc-protocol.md b/docs/spec/03-runtime/01-ipc-protocol.md
index fd36aa224..3e252572b 100644
--- a/docs/spec/03-runtime/01-ipc-protocol.md
+++ b/docs/spec/03-runtime/01-ipc-protocol.md
@@ -1852,7 +1852,13 @@ Renderer IPC kept for the Plan-safe preview facade and URL fallback:
shallowest path. Relative and indexed candidates must resolve to regular files
whose real paths remain inside their answering root; an exact path through an
escaping or dangling link cannot fall back to a same-name indexed file. The
- files-panel ignore set applies. A reference that matches
+ files-panel ignore set applies. An absolute reference may also name a store
+ the app reads without searching it: the whole scratch store
+ (`<data_dir>/scratch/`) holds every session's files — a generated image is
+ clicked from whatever conversation is open — and the read guards (`fs/read`,
+ `fs/open`, the image reader) already accept it, so such a path completes
+ instead of reporting a restriction. A shorthand never completes from that
+ wider store; it still searches the session's own one. A reference that matches
nothing returns `match: null`; an absolute path outside every allowed root
also returns `reason: "outside-allowed-roots"`, without trying a same-name
file inside a root. Resolving never opens anything (ADR 0262).