Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
refactor(agent-runtime): pass project paths through native session list
Frank-zhu2000243ee1058f71
diff --git a/docs/adr/0254-native-pi-session-continuation.md b/docs/adr/0254-native-pi-session-continuation.mdindex e2e3c5146..dfce7b8fc 100644--- a/docs/adr/0254-native-pi-session-continuation.md+++ b/docs/adr/0254-native-pi-session-continuation.md@@ -101,12 +101,13 @@ addition to the default agent session root (`<agentDir>/sessions`).directory, not from the Desktop process cwd. Remote project ids are ignored.At most 500 local project paths are considered.- Custom directories are scanned non-recursively, matching Pi's flat `*.jsonl`- layout. A file is admitted only when its real path stays inside the default- root or a resolved `sessionDir`. Symlink escapes are dropped. An in-directory- symlink is not a second copy of its target. A nested file is omitted only- when the custom directory is outside the default root; a directory inside- that root is not scanned again, and the recursive default walk can still- list nested JSONL there.+ layout. Sessions in subdirectories of a configured `sessionDir` are ignored+ by that flat scan. A file is admitted only when its real path stays inside+ the default root or a resolved `sessionDir`. Symlink escapes are dropped.+ An in-directory symlink is not a second copy of its target. A nested file+ is omitted only when the custom directory is outside the default root; a+ directory inside that root is not scanned again, and the recursive default+ walk can still list nested JSONL there.- Detail, prompt, and fork authorize the file against the default root, thesession directories for the current project list, and any session directorythat already admitted a file. A later empty project list does not revokediff --git a/docs/spec/03-runtime/04-data-storage.md b/docs/spec/03-runtime/04-data-storage.mdindex a08e06939..6d3f413bd 100644--- a/docs/spec/03-runtime/04-data-storage.md+++ b/docs/spec/03-runtime/04-data-storage.md@@ -1815,15 +1815,16 @@ Discovery always walks the Pi agent session root recursively(`<agentDir>/sessions`, default `~/.pi/agent/sessions`). It also scans, withoutrecursion, each `sessionDir` the pinned coding-agent `SettingsManager` resolves:an absolute or `~` value from global settings, and the merged global-plus-project-setting for each known local project. A relative path resolves from that-project's working directory, not from the Desktop process cwd. Remote project-ids are skipped, and at most 500 local project paths are considered. A-configured directory whose real path is the default root, or is inside it, is-not scanned again. A file is listed only when its real path stays inside the-default root or one of those directories. A symlink that escapes is omitted, and-an in-directory symlink alias of a session file is not a second row. A nested-file under a custom `sessionDir` is omitted when that directory is outside the-default root. A `sessionDir` inside the default root is not scanned again, and+setting for each known local project. Session files in a subdirectory of a+configured `sessionDir` are ignored by that scan. A relative path resolves+from that project's working directory, not from the Desktop process cwd.+Remote project ids are skipped, and at most 500 local project paths are+considered. A configured directory whose real path is the default root, or+is inside it, is not scanned again. A file is listed only when its real+path stays inside the default root or one of those directories. A symlink+that escapes is omitted, and an in-directory symlink alias of a session+file is not a second row. A nested file under a custom `sessionDir` is+omitted when that directory is outside the default root. A `sessionDir` inside the default root is not scanned again, andthe recursive default walk can still list nested JSONL there. Detail, prompt,and fork authorize a file against the default root, the session directories forthe current project list, and any session directory that already admitted adiff --git a/packages/agent-runtime/src/native-pi-session.test.ts b/packages/agent-runtime/src/native-pi-session.test.tsindex 11fe81ad0..4c3dd9395 100644--- a/packages/agent-runtime/src/native-pi-session.test.ts+++ b/packages/agent-runtime/src/native-pi-session.test.ts@@ -2,7 +2,7 @@ import { existsSync, mkdtempSync, mkdirSync, readFileSync, readdirSync, realpathimport { tmpdir } from "node:os";import { basename, join } from "node:path";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";-import { AgentSession, ModelRuntime, SessionManager } from "@earendil-works/pi-coding-agent";+import { AgentSession, ModelRuntime, SessionManager, SettingsManager } from "@earendil-works/pi-coding-agent";import { createAssistantMessageEventStream, type AssistantMessage } from "@earendil-works/pi-ai";import { NativePiSessionService } from "./native-pi-session.js";import {@@ -176,6 +176,44 @@ describe("NativePiSessionService", () => {expect(service.fork({ id: local!.id }).messages.map((message) => message.content)).toContain("project local session");});+ it("keeps interleaved list() calls on their own configured session dirs (#1732)", async () => {+ const f = fixture();+ const projectB = join(f.root, "project-b");+ mkdirSync(projectB);+ const dirA = join(f.project, ".pi", "sessions");+ const dirB = join(projectB, ".pi", "sessions");+ mkdirSync(dirA, { recursive: true });+ mkdirSync(dirB, { recursive: true });+ const settings = `${JSON.stringify({ sessionDir: ".pi/sessions" })}\n`;+ writeFileSync(join(f.project, ".pi", "settings.json"), settings);+ writeFileSync(join(projectB, ".pi", "settings.json"), settings);+ writeFileSync(join(dirA, "a.jsonl"), sessionText("id-a", f.project, "session from project a"));+ writeFileSync(join(dirB, "b.jsonl"), sessionText("id-b", projectB, "session from project b"));+ const service = new NativePiSessionService({ agentDir: f.agentDir, sessionRoot: f.sessionRoot });+ const original = SettingsManager.create;+ let other: Promise<Awaited<ReturnType<NativePiSessionService["list"]>>> | undefined;+ let armInterleaving = true;+ // Discovery reads settings before it walks project paths. A second list()+ // that starts during that read must not replace the first call's roots.+ vi.spyOn(SettingsManager, "create").mockImplementation((cwd, agentDir, options) => {+ const manager = original.call(SettingsManager, cwd, agentDir, options);+ if (armInterleaving) {+ armInterleaving = false;+ other = service.list({ projectPaths: [projectB] });+ }+ return manager;+ });++ const fromA = await service.list({ projectPaths: [f.project] });+ const fromB = await other;+ const titlesA = fromA.map((session) => session.title);+ const titlesB = fromB?.map((session) => session.title) ?? [];+ expect(titlesA).toContain("session from project a");+ expect(titlesA).not.toContain("session from project b");+ expect(titlesB).toContain("session from project b");+ expect(titlesB).not.toContain("session from project a");+ });+it("logs an unreadable sessionDir settings file once per path and still lists the default root (#1732)", async () => {const f = fixture();writeFileSync(join(f.agentDir, "settings.json"), "{");diff --git a/packages/agent-runtime/src/native-pi-session.ts b/packages/agent-runtime/src/native-pi-session.tsindex 21d84c189..931dba714 100644--- a/packages/agent-runtime/src/native-pi-session.ts+++ b/packages/agent-runtime/src/native-pi-session.ts@@ -149,7 +149,11 @@ function projectSessionDir(projectPath: string, agentDir: string, seen: Set<strireturn isAbsolute(raw) ? resolve(raw) : resolve(projectPath, raw);}-/** Custom session directories are flat. Pi does not encode a cwd subdirectory. */+/**+ * Custom session directories are flat. Pi writes `*.jsonl` in `sessionDir`+ * itself and does not encode a cwd subdirectory, so session files in+ * subdirectories of a configured sessionDir are ignored.+ */function topLevelJsonl(dir: string): string[] {try {const files: string[] = [];@@ -538,7 +542,6 @@ export class NativePiSessionService {private readonly opening = new Set<string>();private modelRuntime?: ModelRuntime;private readonly modelRuntimeFactory: () => Promise<ModelRuntime>;- private projectPaths: string[] = [];/** Real directories that already admitted a file. Open and fork keep them after a later empty project list. */private readonly admittedRoots: string[] = [];@@ -560,7 +563,7 @@ export class NativePiSessionService {}));}- private configuredSessionDirs(): string[] {+ private configuredSessionDirs(projectPaths: readonly string[]): string[] {const dirs: string[] = [];const seen = new Set<string>();const settingsErrors = new Set<string>();@@ -572,11 +575,11 @@ export class NativePiSessionService {dirs.push(resolved);};add(globalAbsoluteSessionDir(this.agentDir, settingsErrors));- for (const projectPath of this.projectPaths) add(projectSessionDir(projectPath, this.agentDir, settingsErrors));+ for (const projectPath of projectPaths) add(projectSessionDir(projectPath, this.agentDir, settingsErrors));return dirs;}- private sessionRoots(): string[] {+ private sessionRoots(projectPaths: readonly string[] = []): string[] {const roots: string[] = [];const add = (dir: string) => {try {@@ -587,13 +590,14 @@ export class NativePiSessionService {}};add(this.root);- for (const dir of this.configuredSessionDirs()) add(dir);+ for (const dir of this.configuredSessionDirs(projectPaths)) add(dir);for (const dir of this.admittedRoots) add(dir);return roots;}async list(options?: { projectPaths?: readonly string[] }): Promise<SessionSummary[]> {- this.projectPaths = normalizeProjectPaths(options?.projectPaths);+ // Kept local so two overlapping list() calls cannot swap each other's roots.+ const projectPaths = normalizeProjectPaths(options?.projectPaths);let defaultRoot: string | undefined;try {defaultRoot = realpathSync(this.root);@@ -601,7 +605,7 @@ export class NativePiSessionService {defaultRoot = undefined;}const extraRoots: string[] = [];- for (const dir of this.configuredSessionDirs()) {+ for (const dir of this.configuredSessionDirs(projectPaths)) {try {const real = realpathSync(dir);if (defaultRoot && (real === defaultRoot || inside(defaultRoot, real))) continue;