diff --git a/lib/session-reader.test.mjs b/lib/session-reader.test.mjs index b7bbe43ee..3026391d2 100644 --- a/lib/session-reader.test.mjs +++ b/lib/session-reader.test.mjs @@ -1254,6 +1254,79 @@ test("native watcher refreshes a session after content appends", { timeout: 1500 }); }); +// The XDG layout ($XDG_DATA_HOME/omp/sessions) is opt-in via the marker +// directory and only applies to the default agent dir, so this test sets it +// up by hand instead of withAgentDir (which pins PI_CODING_AGENT_DIR and +// therefore disables XDG). The watcher used to derive its directory from +// getAgentDir(), so under XDG it watched ~/.omp/agent/sessions — a directory +// the lister never reads — and external session changes never reached the +// sidebar. This test fails on that code: the warm-up loop never sees a +// notification, because no watch is ever placed on the XDG directory. +// XDG layout is Unix-only: getSessionsDir() ignores XDG_DATA_HOME on Windows, +// so the listing never sees this session there. Skip instead of failing. +test("native watcher follows the XDG sessions dir so external changes reach the sidebar", { timeout: 15000, skip: process.platform === "win32" }, async (t) => { + const xdgRoot = mkdtempSync(join(tmpdir(), "omp-web-xdg-")); + const xdgSessions = join(xdgRoot, "omp", "sessions", "-project"); + mkdirSync(xdgSessions, { recursive: true }); + const previous = { + agentDir: process.env.PI_CODING_AGENT_DIR, + xdg: process.env.XDG_DATA_HOME, + }; + delete process.env.PI_CODING_AGENT_DIR; + process.env.XDG_DATA_HOME = xdgRoot; + invalidateSessionListCache(); + try { + const filePath = writeSessionFile(xdgSessions, "2026-01-01_xdg.jsonl", { + id: "watch-xdg", + cwd: join(tmpdir(), "omp-web-missing-project"), + timestamp: "2026-01-01T00:00:00.000Z", + }, [userEntry("u1", null, "a")]); + // The sidebar listing reads the same directory the watcher must watch. + assert.equal((await listAllSessions()).some((s) => s.id === "watch-xdg"), true); + assert.equal(await resolveSessionIdByPath(filePath), "watch-xdg"); + const fixture = readFileSync(filePath); + const notifications = new EventEmitter(); + let nativeReady = false; + const unsubscribe = subscribeSessionFileChanges((ids) => { + if (ids.includes("watch-xdg")) nativeReady = true; + notifications.emit("change", ids); + }); + try { + while (!nativeReady) { + writeFileSync(filePath, fixture); + await setImmediate(undefined, { signal: t.signal }); + } + // Bun drops a native event that follows the previous one within a few milliseconds. + await new Promise((resolve) => setTimeout(resolve, 50)); + getSessionEntries(filePath); + assert.equal(globalThis.__ompSessionEntriesCache?.has(sessionPathKey(filePath)), true); + const changed = (async () => { + for (;;) { + const [ids] = await once(notifications, "change", { signal: t.signal }); + if (ids.includes("watch-xdg")) return; + } + })(); + appendFileSync(filePath, JSON.stringify(userEntry("u2", "u1", "a2")) + "\n"); + appendFileSync(filePath, JSON.stringify(userEntry("u3", "u2", "a3")) + "\n"); + await changed; + assert.equal(globalThis.__ompSessionEntriesCache?.has(sessionPathKey(filePath)), false, "written session's parse cache must be dropped"); + assert.equal(getSessionEntries(filePath).length, 3, "appended entries are visible after native invalidation"); + } finally { + unsubscribe(); + } + } finally { + for (const [key, value] of [ + ["PI_CODING_AGENT_DIR", previous.agentDir], + ["XDG_DATA_HOME", previous.xdg], + ]) { + if (value === undefined) delete process.env[key]; + else process.env[key] = value; + } + invalidateSessionListCache(); + rmSync(xdgRoot, { recursive: true, force: true }); + } +}); + // ============================================================================ // High-cost read dedup + oversize cache hygiene (TODO §2) // ============================================================================ diff --git a/lib/session-watcher.ts b/lib/session-watcher.ts index d3cf1b4cf..a3ceb8107 100644 --- a/lib/session-watcher.ts +++ b/lib/session-watcher.ts @@ -1,13 +1,13 @@ import { watch, type FSWatcher } from "fs"; import { join } from "path"; import { - getAgentDir, invalidateSessionEntriesCache, invalidateSessionListCache, invalidateSessionListMeta, listAllSessions, resolveSessionIdByPath, } from "./session-reader"; +import { getSessionsDir } from "./omp/paths"; import { invalidateLocalSessionLists } from "./hosts/session-index"; // omp owns the writes to a session's JSONL. ompweb streams RPC events only for @@ -100,7 +100,12 @@ function scheduleRetry(): void { function ensureWatcher(): void { if (watcher || retryTimer) return; - const sessionsDir = join(getAgentDir(), "sessions"); + // Watch the directory the session lister actually reads. Deriving this + // from getAgentDir() broke the XDG layout ($XDG_DATA_HOME/omp/sessions): + // the watcher then watched ~/.omp/agent/sessions, so external session + // changes never reached the sidebar for XDG users (or it retried a + // nonexistent directory forever). + const sessionsDir = getSessionsDir(); try { watcher = watch(sessionsDir, { recursive: true, persistent: false }, (_event, filename) => { if (!filename) {