Repository navigation
fix: cache Pi sessionId in shim so dashboard rows link correctly #284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ef61052
fix: cache pi sessionId in shim so dashboard rows have a session link
NiveditJain 613cba3
fix: discover Pi sessionId from on-disk transcript filename
NiveditJain c37af25
fix: per-cwd Pi sessionId cache + reset on new/resume/fork shutdown
NiveditJain b3b55c6
fix: ignore stale Pi transcripts in cold-start sessionId discovery
NiveditJain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| /** | ||
| * Tests for the pi-extension shim — focused on session-id resolution. | ||
| * | ||
| * Pi (verified empirically against pi-coding-agent v0.71.1) does NOT | ||
| * populate `event.sessionId` on any event. The shim recovers the | ||
| * sessionId by scanning the on-disk transcript at | ||
| * `~/.pi/agent/sessions/<encodedCwd>/<isoTimestamp>_<uuid>.jsonl` and | ||
| * caches the result. | ||
| */ | ||
| // @vitest-environment node | ||
| import { describe, it, expect, beforeEach, vi } from "vitest"; | ||
| import { mkdtempSync, mkdirSync, writeFileSync, rmSync, utimesSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
|
|
||
| interface CapturedCall { | ||
| payload: Record<string, unknown>; | ||
| args: string[]; | ||
| } | ||
|
|
||
| interface PiExtensionApi { | ||
| on(event: string, handler: (event: unknown) => unknown): void; | ||
| } | ||
|
|
||
| const captured: CapturedCall[] = []; | ||
|
|
||
| vi.mock("node:child_process", () => ({ | ||
| spawnSync: (_cmd: string, args: string[], opts: { input?: string }) => { | ||
| captured.push({ args: args ?? [], payload: JSON.parse(opts?.input ?? "{}") }); | ||
| return { pid: 0, output: [], status: 0, signal: null, stderr: "", stdout: "" }; | ||
| }, | ||
| })); | ||
|
|
||
| function piEncodeCwd(cwd: string): string { | ||
| return `--${cwd.replace(/^\/+/, "").replace(/\//g, "-")}--`; | ||
| } | ||
|
|
||
| describe("pi-extension shim — sessionId resolution via on-disk discovery", () => { | ||
| let handlers: Record<string, (event: unknown) => unknown> = {}; | ||
| let bridge: (pi: PiExtensionApi) => void; | ||
| let piRoot: string; | ||
| let originalEnv: string | undefined; | ||
|
|
||
| beforeEach(async () => { | ||
| captured.length = 0; | ||
| handlers = {}; | ||
| piRoot = mkdtempSync(join(tmpdir(), "pi-shim-test-")); | ||
| originalEnv = process.env.PI_SESSIONS_DIR; | ||
| process.env.PI_SESSIONS_DIR = piRoot; | ||
| vi.resetModules(); | ||
| const mod = await import("../../pi-extension/index"); | ||
| bridge = mod.default; | ||
| bridge({ on: (name, fn) => { handlers[name] = fn; } }); | ||
| }); | ||
|
|
||
| function writeSessionFile(cwd: string, sessionId: string, ts = "2026-05-04T20-00-00-000Z", mtime?: Date): void { | ||
| const dir = join(piRoot, piEncodeCwd(cwd)); | ||
| mkdirSync(dir, { recursive: true }); | ||
| const file = join(dir, `${ts}_${sessionId}.jsonl`); | ||
| writeFileSync(file, "{}\n"); | ||
| if (mtime) utimesSync(file, mtime, mtime); | ||
| } | ||
|
|
||
| it("uses event-level sessionId when present", () => { | ||
| writeSessionFile("/proj", "11111111-1111-1111-1111-111111111111"); | ||
| handlers.tool_call({ type: "tool_call", toolName: "bash", input: { command: "ls" }, cwd: "/proj", sessionId: "explicit" }); | ||
| expect(captured.at(-1)?.payload.session_id).toBe("explicit"); | ||
| }); | ||
|
|
||
| it("discovers sessionId from the on-disk transcript when event omits it", () => { | ||
| const sid = "22222222-2222-2222-2222-222222222222"; | ||
| writeSessionFile("/proj", sid); | ||
| handlers.tool_call({ type: "tool_call", toolName: "bash", input: { command: "ls" }, cwd: "/proj" }); | ||
| expect(captured.at(-1)?.payload.session_id).toBe(sid); | ||
| }); | ||
|
|
||
| it("picks the newest file when multiple sessions exist for a cwd", () => { | ||
| writeSessionFile("/proj", "33333333-3333-3333-3333-333333333333", "2026-05-01T00-00-00-000Z", new Date("2026-05-01T00:00:00Z")); | ||
| writeSessionFile("/proj", "44444444-4444-4444-4444-444444444444", "2026-05-04T20-00-00-000Z", new Date("2026-05-04T20:00:00Z")); | ||
| handlers.tool_call({ type: "tool_call", toolName: "bash", input: {}, cwd: "/proj" }); | ||
| expect(captured.at(-1)?.payload.session_id).toBe("44444444-4444-4444-4444-444444444444"); | ||
| }); | ||
|
|
||
| it("returns undefined when no transcript exists yet", () => { | ||
| handlers.tool_call({ type: "tool_call", toolName: "bash", input: {}, cwd: "/no-such-cwd" }); | ||
| expect(captured.at(-1)?.payload.session_id).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("session_start, input, tool_result, agent_end, session_shutdown all resolve", () => { | ||
| const sid = "55555555-5555-5555-5555-555555555555"; | ||
| writeSessionFile("/proj", sid); | ||
| handlers.session_start({ type: "session_start", cwd: "/proj" }); | ||
| expect(captured.at(-1)?.payload.session_id).toBe(sid); | ||
| handlers.input({ type: "input", text: "hi", cwd: "/proj" }); | ||
| expect(captured.at(-1)?.payload.session_id).toBe(sid); | ||
| handlers.tool_result({ type: "tool_result", toolName: "bash", input: {}, content: [], isError: false, cwd: "/proj" }); | ||
| expect(captured.at(-1)?.payload.session_id).toBe(sid); | ||
| handlers.agent_end({ type: "agent_end", cwd: "/proj" }); | ||
| expect(captured.at(-1)?.payload.session_id).toBe(sid); | ||
| handlers.session_shutdown({ type: "session_shutdown", reason: "quit", cwd: "/proj" }); | ||
| expect(captured.at(-1)?.payload.session_id).toBe(sid); | ||
| }); | ||
|
|
||
| it("clears the per-cwd cache on session_shutdown reason=new/resume/fork", () => { | ||
| const sid1 = "11111111-1111-1111-1111-111111111111"; | ||
| const sid2 = "22222222-2222-2222-2222-222222222222"; | ||
| writeSessionFile("/proj", sid1, "2026-05-01T00-00-00-000Z", new Date("2026-05-01T00:00:00Z")); | ||
| handlers.tool_call({ type: "tool_call", toolName: "bash", input: {}, cwd: "/proj" }); | ||
| expect(captured.at(-1)?.payload.session_id).toBe(sid1); | ||
| // Pi shuts down with reason="new" — next event in same process should | ||
| // re-discover (a newer file got added) instead of returning the cache. | ||
| handlers.session_shutdown({ type: "session_shutdown", reason: "new", cwd: "/proj" }); | ||
| writeSessionFile("/proj", sid2, "2026-05-04T00-00-00-000Z", new Date("2026-05-04T00:00:00Z")); | ||
| handlers.tool_call({ type: "tool_call", toolName: "bash", input: {}, cwd: "/proj" }); | ||
| expect(captured.at(-1)?.payload.session_id).toBe(sid2); | ||
| }); | ||
|
|
||
| it("does NOT clear the cache on session_shutdown reason=quit", () => { | ||
| const sid = "33333333-3333-3333-3333-333333333333"; | ||
| writeSessionFile("/proj", sid); | ||
| handlers.tool_call({ type: "tool_call", toolName: "bash", input: {}, cwd: "/proj" }); | ||
| expect(captured.at(-1)?.payload.session_id).toBe(sid); | ||
| handlers.session_shutdown({ type: "session_shutdown", reason: "quit", cwd: "/proj" }); | ||
| // After quit, the SessionEnd record itself uses the cached id (good). | ||
| expect(captured.at(-1)?.payload.session_id).toBe(sid); | ||
| }); | ||
|
|
||
| it("isolates caches per cwd — sessionId from /proj-A doesn't bleed into /proj-B", () => { | ||
| const a = "44444444-4444-4444-4444-444444444444"; | ||
| const b = "55555555-5555-5555-5555-555555555555"; | ||
| writeSessionFile("/proj-A", a); | ||
| writeSessionFile("/proj-B", b); | ||
| handlers.tool_call({ type: "tool_call", toolName: "bash", input: {}, cwd: "/proj-A" }); | ||
| expect(captured.at(-1)?.payload.session_id).toBe(a); | ||
| handlers.tool_call({ type: "tool_call", toolName: "bash", input: {}, cwd: "/proj-B" }); | ||
| expect(captured.at(-1)?.payload.session_id).toBe(b); | ||
| }); | ||
|
|
||
| // Cleanup | ||
| afterEach(() => { | ||
| if (originalEnv === undefined) delete process.env.PI_SESSIONS_DIR; | ||
| else process.env.PI_SESSIONS_DIR = originalEnv; | ||
| rmSync(piRoot, { recursive: true, force: true }); | ||
| }); | ||
| }); | ||
|
|
||
| import { afterEach } from "vitest"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.