import { afterEach, describe, expect, test } from "bun:test"; import { mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { prepareAgentSession, readAgentSessionRecord, recordAgentSessionId, } from "@bbbug/shared/agent_session"; const dirs: string[] = []; afterEach(() => { for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); }); function workspace(): string { const dir = mkdtempSync(path.join(tmpdir(), "streamline-agent-session-")); dirs.push(dir); return dir; } describe("security run agent session persistence", () => { test("Claude uses the Streamline UUID as its native session ID", () => { const dir = workspace(); const first = prepareAgentSession({ workspaceDir: dir, taskKind: "security", taskId: "security-1", provider: "claude" }); expect(first.resume).toBe(false); expect(first.args).toEqual(["--session-id", first.record.streamlineSessionId]); expect(first.record.agentSessionId).toBe(first.record.streamlineSessionId); const afterRestart = prepareAgentSession({ workspaceDir: dir, taskKind: "security", taskId: "security-1", provider: "claude" }); expect(afterRestart.resume).toBe(true); expect(afterRestart.args).toEqual(["--resume", first.record.streamlineSessionId]); }); for (const provider of ["cursor", "codex"] as const) { test(`${provider} persists the provider-generated native session ID`, () => { const dir = workspace(); const first = prepareAgentSession({ workspaceDir: dir, taskKind: "security", taskId: "security-1", provider }); expect(first.resume).toBe(false); expect(first.record.agentSessionId).toBeNull(); expect(recordAgentSessionId(first.recordPath, provider, first.record.streamlineSessionId, `${provider}-native-1`)).toBe(true); const afterRestart = prepareAgentSession({ workspaceDir: dir, taskKind: "security", taskId: "security-1", provider }); expect(afterRestart.resume).toBe(true); expect(afterRestart.args).toEqual(["--resume", first.record.streamlineSessionId]); expect(afterRestart.record.agentSessionId).toBe(`${provider}-native-1`); expect(readAgentSessionRecord(first.recordPath)?.agentSessionId).toBe(`${provider}-native-1`); }); } test("switching provider rotates the logical session instead of cross-resuming", () => { const dir = workspace(); const cursor = prepareAgentSession({ workspaceDir: dir, taskKind: "security", taskId: "security-1", provider: "cursor" }); recordAgentSessionId(cursor.recordPath, "cursor", cursor.record.streamlineSessionId, "cursor-native-1"); const codex = prepareAgentSession({ workspaceDir: dir, taskKind: "security", taskId: "security-1", provider: "codex" }); expect(codex.resume).toBe(false); expect(codex.record.streamlineSessionId).not.toBe(cursor.record.streamlineSessionId); expect(codex.record.agentSessionId).toBeNull(); }); });