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(agent-runtime): discover native sessions in project sessionDir
Frank-zhu2000d6a76405cfe1
diff --git a/apps/desktop/electron/main/ipc/session-ipc.ts b/apps/desktop/electron/main/ipc/session-ipc.tsindex f62be5b1e..326a3d2d5 100644--- a/apps/desktop/electron/main/ipc/session-ipc.ts+++ b/apps/desktop/electron/main/ipc/session-ipc.ts@@ -26,7 +26,7 @@ import type { Logger } from "../logger";import type { PersistenceOutbox } from "../persistence-outbox";import type { PluginRuntime } from "../plugin-runtime";import { readSessionCollaboration } from "../services/session-collaboration";-import { searchSessionsAcrossSources } from "../services/session-search";+import { localNativeProjectPaths, searchSessionsAcrossSources } from "../services/session-search";import type { IpcRegistrar } from "./types";import { getActiveRemoteHostsBoot } from "../bootstrap/remote-hosts.js";@@ -203,12 +203,18 @@ export function registerSessionIpc({});handle(IPC.invoke.sessionList, async () => {if (!host) throw new Error("host unavailable");+ const projectPathsPromise = localNativeProjectPaths(host);+ const desktopPromise = host.call<{ sessions: RuntimeSession[] }>("session.list");+ const capabilityPromise = sessionCapabilityContext();+ const projectPaths = await projectPathsPromise;const [result, native, { providers, defaults }] = await Promise.all([- host.call<{ sessions: RuntimeSession[] }>("session.list"),+ desktopPromise,sidecar- ? sidecar.call<{ sessions: RuntimeSession[] }>("native.session.list").catch(() => ({ sessions: [] }))+ ? sidecar+ .call<{ sessions: RuntimeSession[] }>("native.session.list", { projectPaths })+ .catch(() => ({ sessions: [] })): Promise.resolve({ sessions: [] }),- sessionCapabilityContext(),+ capabilityPromise,]);return {...result,diff --git a/apps/desktop/electron/main/live-voice/live-work-backend-port.ts b/apps/desktop/electron/main/live-voice/live-work-backend-port.tsindex 66e2faa1d..d7b49d36d 100644--- a/apps/desktop/electron/main/live-voice/live-work-backend-port.ts+++ b/apps/desktop/electron/main/live-voice/live-work-backend-port.ts@@ -11,6 +11,7 @@ import { DESKTOP_PRINCIPAL } from "../agent-host-bridge";import type { BackendRouter, RemoteBackend } from "../remote/backend-router";import type { RemoteHostsBoot } from "../bootstrap/remote-hosts";import type { LiveWorkAdmission, LiveWorkPort, WorkSnapshot } from "@pi-desktop/host-runtime";+import { localNativeProjectPaths } from "../services/session-search";export type LiveWorkSessionRecord = {id: string;@@ -55,10 +56,13 @@ export async function listLiveWorkSessions(input: {if (!host) throw Object.assign(new Error("Local Host is unavailable"), { errorCode: "LIVE_WORK_NOT_READY" });const sidecar = input.getSidecar();const remoteHosts = input.getRemoteHosts();+ const projectPathsPromise = localNativeProjectPaths(host);+ const desktopPromise = host.call<{ sessions?: Array<Record<string, unknown>> }>("session.list");+ const projectPaths = await projectPathsPromise;const [desktopResult, nativeResult, remoteResult] = await Promise.all([- host.call<{ sessions?: Array<Record<string, unknown>> }>("session.list"),+ desktopPromise,sidecar- ? sidecar.call<{ sessions?: Array<Record<string, unknown>> }>("native.session.list")+ ? sidecar.call<{ sessions?: Array<Record<string, unknown>> }>("native.session.list", { projectPaths }): Promise.resolve({ sessions: [] as Array<Record<string, unknown>> }),remoteHosts ? remoteHosts.listSessions() : Promise.resolve([]),]);diff --git a/apps/desktop/electron/main/services/session-search.ts b/apps/desktop/electron/main/services/session-search.tsindex 72be6d555..c56ce3710 100644--- a/apps/desktop/electron/main/services/session-search.ts+++ b/apps/desktop/electron/main/services/session-search.ts@@ -5,6 +5,33 @@ type RpcClient = {};const SEARCH_PAGE_SIZE = 30;+const MAX_NATIVE_PROJECT_PATHS = 500;++type NativeProjectHost = {+ call(method: string, params?: unknown): Promise<unknown>;+};++/** Local project paths whose Pi `sessionDir` native discovery must honor. */+export async function localNativeProjectPaths(host: NativeProjectHost): Promise<string[]> {+ try {+ const listed = (await host.call("projects.list")) as { projects?: Array<{ path?: unknown }> } | null;+ const projects =+ listed && typeof listed === "object" && Array.isArray(listed.projects) ? listed.projects : [];+ const paths: string[] = [];+ const seen = new Set<string>();+ for (const project of projects) {+ if (!project || typeof project.path !== "string") continue;+ const path = project.path.trim();+ if (!path || path.startsWith("remote-project:") || seen.has(path)) continue;+ seen.add(path);+ paths.push(path);+ if (paths.length >= MAX_NATIVE_PROJECT_PATHS) break;+ }+ return paths;+ } catch {+ return [];+ }+}function compareHits(left: SessionSearchPage["hits"][number],@@ -41,10 +68,12 @@ export async function searchSessionsAcrossSources(const offset = Number.isInteger(rawOffset) ? Math.max(0, rawOffset) : 0;const requiredHostRows = offset + SEARCH_PAGE_SIZE;+ const projectPaths = await localNativeProjectPaths(host);const nativeSearch = sidecar? sidecar.call<SessionSearchPage>("native.session.search", {query: Reflect.get(request, "query"),+ projectPaths,}).catch(() => ({ hits: [], nextOffset: null })): Promise.resolve<SessionSearchPage>({ hits: [], nextOffset: null });diff --git a/apps/desktop/test/native-pi-sessions.test.mjs b/apps/desktop/test/native-pi-sessions.test.mjsindex 899b597c4..6f6c3067a 100644--- a/apps/desktop/test/native-pi-sessions.test.mjs+++ b/apps/desktop/test/native-pi-sessions.test.mjs@@ -48,7 +48,7 @@ const { register } = await import("node:module");register(new URL("./helpers/ts-import-hooks.mjs", import.meta.url));const { IPC, isImageGenerationModel, imageGenerationBindings } = await import("@pi-desktop/shared");const { registerAgentIpc } = await import("../electron/main/ipc/agent-ipc.ts");-const { searchSessionsAcrossSources } = await import("../electron/main/services/session-search.ts");+const { localNativeProjectPaths, searchSessionsAcrossSources } = await import("../electron/main/services/session-search.ts");const { createEventsSlice } = await import("../src/stores/slices/events-slice.ts");const { createTranscriptSlice } = await import("../src/stores/slices/transcript-slice.ts");const { api } = await import("../src/lib/api.ts");@@ -226,7 +226,7 @@ function forkHarness({ host, sidecar, activeTurns = new Map() }) {"@pi-desktop/shared": sharedForIpc,"../importers": { convertSession() {}, scanAllSources() {}, scanModelConfigs() {} },"../services/session-collaboration": { readSessionCollaboration() {} },- "../services/session-search": { searchSessionsAcrossSources },+ "../services/session-search": { localNativeProjectPaths, searchSessionsAcrossSources },"../bootstrap/remote-hosts.js": { getActiveRemoteHostsBoot: () => null },});registerSessionIpc({@@ -246,6 +246,7 @@ function forkHarness({ host, sidecar, activeTurns = new Map() }) {});return {handle: handlers.get(IPC.invoke.sessionFork),+ list: handlers.get(IPC.invoke.sessionList),search: handlers.get(IPC.invoke.sessionSearch),hostCalls,sidecarCalls,@@ -267,6 +268,9 @@ test("global session search merges native sidecar hits with Desktop results", ashost: (calls) => ({call: async (method, input) => {calls.push({ method, input });+ if (method === "projects.list") {+ return { projects: [{ path: "/work/demo" }, { path: "remote-project:office" }] };+ }return { hits: [desktopHit], nextOffset: null };},}),@@ -279,11 +283,53 @@ test("global session search merges native sidecar hits with Desktop results", as});const result = await search({ query: "side chat", offset: 0 });assert.deepEqual(result.hits.map((hit) => hit.session.id), ["native-pi:child", "desktop-session"]);- assert.deepEqual(sidecarCalls, [{ method: "native.session.search", input: { query: "side chat" } }]);- assert.deepEqual(hostCalls, [{- method: "search.sessions",- input: { query: "side chat", offset: 0, limit: 30 },+ assert.deepEqual(sidecarCalls, [{+ method: "native.session.search",+ input: { query: "side chat", projectPaths: ["/work/demo"] },+ }]);+ assert.deepEqual(hostCalls, [+ { method: "projects.list", input: undefined },+ { method: "search.sessions", input: { query: "side chat", offset: 0, limit: 30 } },+ ]);+});++test("session list forwards local project paths to native discovery (#1732)", async () => {+ const { list, hostCalls, sidecarCalls } = forkHarness({+ host: (calls) => ({+ call: async (method, input) => {+ calls.push({ method, input });+ if (method === "projects.list") {+ return {+ projects: [+ { path: "/work/demo" },+ { path: "/work/demo" },+ { path: "remote-project:office" },+ { path: " " },+ ],+ };+ }+ if (method === "session.list") {+ return { sessions: [{ id: "desktop-1", updatedAt: "2026-09-14T00:00:01.000Z" }] };+ }+ return {};+ },+ }),+ sidecar: (calls) => ({+ call: async (method, input) => {+ calls.push({ method, input });+ return {+ sessions: [{ id: "native-pi:1", updatedAt: "2026-09-14T00:00:02.000Z", source: "pi-native" }],+ };+ },+ }),+ });+ const result = await list();+ assert.deepEqual(result.sessions.map((session) => session.id), ["native-pi:1", "desktop-1"]);+ assert.deepEqual(sidecarCalls, [{+ method: "native.session.list",+ input: { projectPaths: ["/work/demo"] },}]);+ assert.deepEqual(hostCalls.map((call) => call.method), ["projects.list", "session.list"]);});test("native fork routes to the sidecar and never to the Desktop host", async () => {diff --git a/docs/adr/0254-native-pi-session-continuation.md b/docs/adr/0254-native-pi-session-continuation.mdindex 2f1bc90d5..6081c09d1 100644--- a/docs/adr/0254-native-pi-session-continuation.md+++ b/docs/adr/0254-native-pi-session-continuation.md@@ -11,7 +11,7 @@ PI-Desktop historically imports Pi sessions by flattening them into Desktop-owne## Decision-1. The Node agent sidecar exposes a source-discriminated `pi-native` adapter. It discovers files below Pi's agent session directory, projects immutable byte snapshots through `SessionManager.inMemory`, and opens a persistent `SessionManager` only when a prompt is accepted.+1. The Node agent sidecar exposes a source-discriminated `pi-native` adapter. It discovers files below Pi's agent session directory and below each `sessionDir` Pi resolves from settings (see the amendment below), projects immutable byte snapshots through `SessionManager.inMemory`, and opens a persistent `SessionManager` only when a prompt is accepted.2. Native identity is an opaque composite derived from canonical file path plus native header id. Paths never cross the preload boundary. Electron merges native summaries with Rust-owned Desktop summaries; Rust remains the exclusive SQLite and Desktop-transcript owner.3. Native continuation uses `createAgentSession` with Pi `ModelRuntime`, `SettingsManager`, `DefaultResourceLoader`, saved provider/model/thinking, and the original `SessionManager`. There is no Desktop provider fallback and no `UiMessage` reconstruction for model context.4. The first slice supports list, detail, refresh, text-only Agent prompt with no tools (`noTools: "all"`), stop, and renderer-local pin/archive. Built-in and extension model tools stay disabled until an explicit Desktop permission bridge is designed; this is not full Pi tool parity. Rename, delete, move, revision, Plan/Goal, queue, collaboration, attachment, and transcript rewrite operations are rejected; fork and side chat are added by the amendment below.@@ -86,3 +86,24 @@ registration and tab; the child remains a native session in the sidebar and intitle/project search, and reopens as an ordinary conversation. A send while anative turn is running fails before the Desktop queue with a visible message anda preserved draft; native sessions still cannot enqueue.++## Amendment: configured sessionDir discovery (2026-10-18)++Pi writes a session into `sessionDir` when that setting is present in global+`~/.pi/agent/settings.json` or in `<cwd>/.pi/settings.json`. Desktop history+must discover and open those files with the same resolution the CLI uses, in+addition to the default agent session root (`<agentDir>/sessions`).++- The default root stays a recursive scan, so sessions written before a custom+ directory remain visible.+- An absolute or `~` global `sessionDir` is scanned even when no project is+ known. A relative value is resolved from each known local project working+ 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.+- Detail, prompt, and fork use those same roots. A fork still publishes the+ child beside its parent. Trust and provider gates are unchanged: a session+ discovered through a project `.pi/settings.json` stays browseable and+ read-only until that project is trusted.diff --git a/docs/spec/03-runtime/04-data-storage.md b/docs/spec/03-runtime/04-data-storage.mdindex df5d9d2aa..7b94f9a96 100644--- a/docs/spec/03-runtime/04-data-storage.md+++ b/docs/spec/03-runtime/04-data-storage.md@@ -1805,11 +1805,27 @@ migration is required.## 12. Native Pi session authority (ADR 0254)-Native Pi v3 sessions under the Pi agent session root are a second, explicitly-source-discriminated transcript authority owned by the Node agent sidecar. They-are never inserted into SQLite and never copied to the Desktop transcript-directory. `session.list` merges their projections with Rust-owned-Desktop summaries, and `session.get` routes by the opaque `native-pi:` id.+Native Pi v3 sessions are a second, explicitly source-discriminated transcript+authority owned by the Node agent sidecar. They are never inserted into SQLite+and never copied to the Desktop transcript directory. `session.list` merges+their projections with Rust-owned Desktop summaries, and `session.get` routes+by the opaque `native-pi:` id.++Discovery always walks the Pi agent session root recursively+(`<agentDir>/sessions`, default `~/.pi/agent/sessions`). It also scans, without+recursion, 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, and a nested+file under a custom `sessionDir`, are omitted. Detail, prompt, and fork use the+same roots. A child fork is still published beside its parent. A project Desktop+has not yet recorded does not contribute a relative `sessionDir`. An absolute+global `sessionDir` is scanned even when no project is known.+Discovery deduplicates native files that share the same JSONL `header.id`,keeping the projection with the newest transcript `updatedAt`. The selectedfile retains its path-derived opaque session id; duplicate files are notdiff --git a/docs/spec/05-security/01-security.md b/docs/spec/05-security/01-security.mdindex 9b404585c..4bc397a94 100644--- a/docs/spec/05-security/01-security.md+++ b/docs/spec/05-security/01-security.md@@ -358,8 +358,11 @@ host-core. They do not change the loopback-only rule above.Native session paths remain sidecar-private. Renderer-visible ids are opaquehashes of canonical path plus verified header id. Every discovery/open resolves-the real path below the configured Pi session root and revalidates header id and-cwd; path traversal and symlink escape are rejected.+the real path below an allowed Pi session root — the agent session directory,+or a `sessionDir` resolved from global or project settings — and revalidates+header id and cwd; path traversal and symlink escape are rejected. A relative+`sessionDir` is resolved only for a known local project. Custom directories are+not walked recursively.Writable continuation requires a mode-0600 cooperative PI-Desktop lease besidethe session and full-byte identity checks before each SDK append. After andiff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.mdindex 13fd6a8c6..c8d316c99 100644--- a/docs/spec/06-delivery/04-e2e-test-plan.md+++ b/docs/spec/06-delivery/04-e2e-test-plan.md@@ -16691,6 +16691,29 @@ plugin-form fixtures in an isolated temporary directory at runtime.- **Specs:** runtime §12; storage §12; ADR 0254.- **Status:** Source-regression covered; full Desktop journey Draft.+### E2E-SESSION-native-pi-project-session-dir++- **Preconditions:** A local project has `.pi/settings.json` with+ `"sessionDir": ".pi/sessions"` and a valid native v3 JSONL in that directory.+ The default Pi agent session root also contains a valid session. A nested+ JSONL and a symlink that leaves `.pi/sessions` are present. A second fixture+ sets an absolute global `sessionDir` and no project path. A third sets a+ relative global `sessionDir`.+- **Steps:** Refresh the session list, open the project-local session, and fork+ it. Repeat list and open for the absolute global directory. Refresh the+ relative global fixture once with no known project and once after that+ project is known, including a remote project id in the known set.+- **Expected:** The project-local session and the default-root session both+ appear. The nested file and the escaping symlink do not. Opening the+ project-local session returns its transcript. Fork publishes one new JSONL+ beside the parent and does not rewrite the parent. The absolute global+ directory is listed without a project path. The relative global directory is+ listed only for the known local project. Remote project ids are ignored.+ Untrusted project resources stay browseable and read-only.+- **Specs:** runtime §12; storage §12; security §12; ADR 0254.+- **Status:** Source-regression covered by+ `packages/agent-runtime/src/native-pi-session.test.ts` and+ `apps/desktop/test/native-pi-sessions.test.mjs`; full Desktop journey Draft.### E2E-SUBAGENT-ordered-model-fallback-preserves-workdiff --git a/packages/agent-runtime/src/native-pi-session.test.ts b/packages/agent-runtime/src/native-pi-session.test.tsindex 6bbed1ea9..e5be00e52 100644--- a/packages/agent-runtime/src/native-pi-session.test.ts+++ b/packages/agent-runtime/src/native-pi-session.test.ts@@ -1,4 +1,4 @@-import { existsSync, mkdtempSync, mkdirSync, readFileSync, readdirSync, realpathSync, rmSync, writeFileSync } from "node:fs";+import { existsSync, mkdtempSync, mkdirSync, readFileSync, readdirSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { basename, join } from "node:path";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";@@ -48,6 +48,20 @@ function fixture(options: { newline?: boolean } = {}) {return { root, agentDir, sessionRoot, project, file, text };}+function sessionText(id: string, cwd: string, userText: string): string {+ const entries = [+ { type: "session", version: 3, id, timestamp: "2026-09-14T00:00:00.000Z", cwd },+ {+ type: "message",+ id: `${id}-user`,+ parentId: null,+ timestamp: "2026-09-14T00:00:01.000Z",+ message: { role: "user", content: [{ type: "text", text: userText }], timestamp: 1 },+ },+ ];+ return `${entries.map((entry) => JSON.stringify(entry)).join("\n")}\n`;+}+describe("NativePiSessionService", () => {it("lists and projects a native v3 tree without changing its bytes", async () => {const f = fixture();@@ -61,6 +75,74 @@ describe("NativePiSessionService", () => {expect(readFileSync(f.file)).toEqual(before);});+ it("lists and opens sessions stored in a project sessionDir (#1732)", async () => {+ const f = fixture();+ const customDir = join(f.project, ".pi", "sessions");+ mkdirSync(customDir, { recursive: true });+ writeFileSync(+ join(f.project, ".pi", "settings.json"),+ `${JSON.stringify({ sessionDir: ".pi/sessions" })}\n`,+ );+ const localText = sessionText("local-id", f.project, "project local session");+ writeFileSync(join(customDir, "local.jsonl"), localText);+ mkdirSync(join(customDir, "nested"));+ writeFileSync(+ join(customDir, "nested", "hidden.jsonl"),+ sessionText("hidden-id", f.project, "nested hidden session"),+ );+ const outside = join(f.root, "outside.jsonl");+ writeFileSync(outside, sessionText("outside-id", f.project, "outside session"));+ symlinkSync(outside, join(customDir, "escape.jsonl"));++ const service = new NativePiSessionService({ agentDir: f.agentDir, sessionRoot: f.sessionRoot });+ const sessions = await service.list({ projectPaths: [f.project] });+ const titles = sessions.map((session) => session.title);+ expect(titles).toContain("project local session");+ expect(titles).not.toContain("nested hidden session");+ expect(titles).not.toContain("outside session");+ expect(titles).toContain("hello");++ const local = sessions.find((session) => session.title === "project local session");+ expect(local?.projectPath).toBe(f.project);+ const detail = service.detail(local!.id);+ expect(detail?.messages.map((message) => message.content)).toEqual(["project local session"]);+ expect(readFileSync(join(customDir, "local.jsonl"), "utf8")).toBe(localText);++ const forked = service.fork({ id: local!.id });+ expect(forked.source).toBe("pi-native");+ expect(forked.messages.map((message) => message.content)).toContain("project local session");+ const published = readdirSync(customDir).filter((name) => name.endsWith(".jsonl") && name !== "local.jsonl" && name !== "escape.jsonl");+ expect(published).toHaveLength(1);+ expect(readFileSync(join(customDir, "local.jsonl"), "utf8")).toBe(localText);+ });++ it("lists sessions from an absolute global sessionDir without a project path (#1732)", async () => {+ const f = fixture();+ const customDir = join(f.root, "global-sessions");+ mkdirSync(customDir);+ writeFileSync(join(f.agentDir, "settings.json"), `${JSON.stringify({ sessionDir: customDir })}\n`);+ const text = sessionText("global-id", f.project, "global session dir");+ writeFileSync(join(customDir, "global.jsonl"), text);+ const service = new NativePiSessionService({ agentDir: f.agentDir, sessionRoot: f.sessionRoot });+ const sessions = await service.list();+ expect(sessions.map((session) => session.title)).toContain("global session dir");+ const found = sessions.find((session) => session.title === "global session dir");+ expect(service.detail(found!.id)?.messages.map((message) => message.content)).toEqual(["global session dir"]);+ expect(readFileSync(join(customDir, "global.jsonl"), "utf8")).toBe(text);+ });++ it("resolves a relative global sessionDir from each known project (#1732)", async () => {+ const f = fixture();+ writeFileSync(join(f.agentDir, "settings.json"), `${JSON.stringify({ sessionDir: ".pi/sessions" })}\n`);+ const customDir = join(f.project, ".pi", "sessions");+ mkdirSync(customDir, { recursive: true });+ writeFileSync(join(customDir, "relative.jsonl"), sessionText("relative-id", f.project, "relative global session"));+ const service = new NativePiSessionService({ agentDir: f.agentDir, sessionRoot: f.sessionRoot });+ expect((await service.list()).map((session) => session.title)).not.toContain("relative global session");+ const sessions = await service.list({ projectPaths: [f.project, "remote-project:host/path"] });+ expect(sessions.map((session) => session.title)).toContain("relative global session");+ });+it("collapses copied session files onto one entry per native id (#1359)", async () => {const f = fixture();diff --git a/packages/agent-runtime/src/native-pi-session.ts b/packages/agent-runtime/src/native-pi-session.tsindex f0c10807c..73b5caebb 100644--- a/packages/agent-runtime/src/native-pi-session.ts+++ b/packages/agent-runtime/src/native-pi-session.ts@@ -91,6 +91,58 @@ function inside(root: string, candidate: string): boolean {return child === "" || (!child.startsWith("..") && !isAbsolute(child));}+const MAX_NATIVE_PROJECT_PATHS = 500;++function normalizeProjectPaths(paths: readonly string[] | undefined): string[] {+ if (!paths) return [];+ const seen = new Set<string>();+ const normalized: string[] = [];+ for (const value of paths) {+ if (typeof value !== "string") continue;+ const path = value.trim();+ if (!path || path.startsWith("remote-project:") || seen.has(path)) continue;+ seen.add(path);+ normalized.push(path);+ if (normalized.length >= MAX_NATIVE_PROJECT_PATHS) break;+ }+ return normalized;+}++function rawSessionDir(cwd: string, agentDir: string): string | undefined {+ try {+ return SettingsManager.create(cwd, agentDir).getSessionDir() || undefined;+ } catch {+ return undefined;+ }+}++/** Absolute `sessionDir` from global settings. Relative values stay project-scoped. */+function globalAbsoluteSessionDir(agentDir: string): string | undefined {+ const raw = rawSessionDir(agentDir, agentDir);+ return raw && isAbsolute(raw) ? resolve(raw) : undefined;+}++/** Pi resolves a relative `sessionDir` from the project working directory. */+function projectSessionDir(projectPath: string, agentDir: string): string | undefined {+ const raw = rawSessionDir(projectPath, agentDir);+ if (!raw) return undefined;+ return isAbsolute(raw) ? resolve(raw) : resolve(projectPath, raw);+}++/** Custom session directories are flat. Pi does not encode a cwd subdirectory. */+function topLevelJsonl(dir: string): string[] {+ try {+ const files: string[] = [];+ for (const entry of readdirSync(dir, { withFileTypes: true })) {+ if (entry.isDirectory()) continue;+ if (entry.name.endsWith(".jsonl")) files.push(join(dir, entry.name));+ }+ return files;+ } catch {+ return [];+ }+}+function textContent(content: unknown): { text: string; thinking?: string } {if (typeof content === "string") return { text: content };if (!Array.isArray(content)) return { text: "" };@@ -466,6 +518,7 @@ export class NativePiSessionService {private readonly opening = new Set<string>();private modelRuntime?: ModelRuntime;private readonly modelRuntimeFactory: () => Promise<ModelRuntime>;+ private projectPaths: string[] = [];constructor(options: {agentDir?: string;@@ -485,21 +538,73 @@ export class NativePiSessionService {}));}- async list(): Promise<SessionSummary[]> {- let root: string;+ private configuredSessionDirs(): string[] {+ const dirs: string[] = [];+ const seen = new Set<string>();+ const add = (dir: string | undefined) => {+ if (!dir) return;+ const resolved = resolve(dir);+ if (seen.has(resolved)) return;+ seen.add(resolved);+ dirs.push(resolved);+ };+ add(globalAbsoluteSessionDir(this.agentDir));+ for (const projectPath of this.projectPaths) add(projectSessionDir(projectPath, this.agentDir));+ return dirs;+ }++ private sessionRoots(): string[] {+ const roots: string[] = [];+ const add = (dir: string) => {+ try {+ const real = realpathSync(dir);+ if (!roots.includes(real)) roots.push(real);+ } catch {+ // A missing directory contributes no files and no containment root.+ }+ };+ add(this.root);+ for (const dir of this.configuredSessionDirs()) add(dir);+ return roots;+ }++ async list(options?: { projectPaths?: readonly string[] }): Promise<SessionSummary[]> {+ this.projectPaths = normalizeProjectPaths(options?.projectPaths);+ let defaultRoot: string | undefined;try {- root = realpathSync(this.root);+ defaultRoot = realpathSync(this.root);} catch {- return [];+ defaultRoot = undefined;+ }+ const extraRoots: string[] = [];+ for (const dir of this.configuredSessionDirs()) {+ try {+ const real = realpathSync(dir);+ if (defaultRoot && (real === defaultRoot || inside(defaultRoot, real))) continue;+ if (!extraRoots.includes(real)) extraRoots.push(real);+ } catch {+ // The configured directory does not exist yet.+ }}+ if (!defaultRoot && extraRoots.length === 0) return [];+ const allowed = [...(defaultRoot ? [defaultRoot] : []), ...extraRoots];+ const seenCandidates = new Set<string>();+ const candidates = [+ ...(defaultRoot ? walkJsonl(defaultRoot) : []),+ ...extraRoots.flatMap((dir) => topLevelJsonl(dir)),+ ].filter((candidate) => {+ if (seenCandidates.has(candidate)) return false;+ seenCandidates.add(candidate);+ return true;+ });const modelRuntime = await this.modelRuntimeFactory().catch(() => undefined);this.modelRuntime = modelRuntime;const trustStore = new ProjectTrustStore(this.agentDir);const summaries = await Promise.all(- walkJsonl(root).map(async (candidate) => {+ candidates.map(async (candidate) => {try {const path = realpathSync(candidate);- if (!inside(root, path) || lstatSync(path).isSymbolicLink()) return undefined;+ if (!allowed.some((root) => inside(root, path)) || lstatSync(path).isSymbolicLink()) return undefined;const snap = nativePiSnapshot(path);const entries = parseSessionEntries(snap.bytes.toString("utf8"));const header = entries.find((entry) => entry.type === "session") as any;@@ -599,12 +704,12 @@ export class NativePiSessionService {.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));}- async search(query: string): Promise<SessionSearchPage> {+ async search(query: string, options?: { projectPaths?: readonly string[] }): Promise<SessionSearchPage> {const normalized = query.trim();if (!normalized) return { hits: [], nextOffset: null };if (normalized.length > 500) throw new Error("query exceeds 500 characters");- const sessions = await this.list();+ const sessions = await this.list(options);const hits: SessionSearchPage["hits"] = [];for (const session of sessions) {const record = this.records.get(session.id);@@ -809,8 +914,8 @@ export class NativePiSessionService {let published: CompleteOwnedFile | undefined;try {const directory = realpathSync(dirname(record.path));- const root = realpathSync(this.root);- if (!inside(root, directory)) {+ const roots = this.sessionRoots();+ if (!roots.some((root) => inside(root, directory))) {throw Object.assign(new Error("Native Pi session path is outside the allowed root"), {errorCode: "PERMISSION_DENIED",});@@ -904,7 +1009,7 @@ export class NativePiSessionService {throw new Error("Native Pi fork produced an invalid session file");}const resolvedChild = realpathSync(childPath);- if (resolvedChild !== childPath || !inside(root, resolvedChild)) {+ if (resolvedChild !== childPath || !roots.some((root) => inside(root, resolvedChild))) {throw Object.assign(new Error("Native Pi fork path is outside the session root"), {errorCode: "PERMISSION_DENIED",});@@ -1056,9 +1161,8 @@ export class NativePiSessionService {if (!record) {throw Object.assign(new Error("Native Pi session not found; refresh sessions and try again"), { errorCode: "NOT_FOUND" });}- const root = realpathSync(this.root);const path = realpathSync(record.path);- if (!inside(root, path) || lstatSync(path).isSymbolicLink()) {+ if (!this.sessionRoots().some((root) => inside(root, path)) || lstatSync(path).isSymbolicLink()) {throw Object.assign(new Error("Native Pi session path is outside the allowed root"), { errorCode: "PERMISSION_DENIED" });}return { ...record, path };diff --git a/packages/agent-runtime/src/sidecar.ts b/packages/agent-runtime/src/sidecar.tsindex c591dabaf..1fdcd50f2 100644--- a/packages/agent-runtime/src/sidecar.ts+++ b/packages/agent-runtime/src/sidecar.ts@@ -62,6 +62,11 @@ async function getNativePiService() {return nativePiServiceFactory();}+function nativeProjectPaths(params: { projectPaths?: unknown } | undefined): string[] {+ const value = params?.projectPaths;+ return Array.isArray(value) ? value.filter((item): item is string => typeof item === "string") : [];+}+const runtimes: RuntimeMap = new Map();const hostProxy = new ParentHostProxy();const testRuntimeIds = new WeakMap<DesktopAgentRuntime, string>();@@ -396,11 +401,13 @@ async function handle(method: string, params: any): Promise<unknown> {return { ok: true, runtimes: runtimes.size };case "native.session.list": {const service = await getNativePiService();- return { sessions: await service.list() };+ return { sessions: await service.list({ projectPaths: nativeProjectPaths(params) }) };}case "native.session.search": {const service = await getNativePiService();- return service.search(String(params.query ?? ""));+ return service.search(String(params?.query ?? ""), {+ projectPaths: nativeProjectPaths(params),+ });}case "native.session.get": {const service = await getNativePiService();