import { afterEach, describe, expect, test } from "bun:test"; import { existsSync, mkdtempSync, readdirSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { agentSessionRecordPath, prepareAgentSession, } from "@bbbug/shared/agent_session"; import { rotateSecurityAgentSession } from "../src/security_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-security-session-")); dirs.push(dir); return dir; } describe("security manual retry session boundary", () => { test("manual rotation creates a new session while later automatic retries resume it", () => { const dir = workspace(); const first = prepareAgentSession({ workspaceDir: dir, taskKind: "security", taskId: "security-1", provider: "claude", }); const rotated = rotateSecurityAgentSession(dir, "review-reject"); expect(rotated.rotated).toBe(true); expect(rotated.previousSessionId).toBe(first.record.streamlineSessionId); expect(existsSync(agentSessionRecordPath(dir))).toBe(false); expect(readdirSync(path.join(dir, "logs", "agent-session-history"))).toHaveLength(1); const manualRetry = prepareAgentSession({ workspaceDir: dir, taskKind: "security", taskId: "security-1", provider: "claude", }); expect(manualRetry.resume).toBe(false); expect(manualRetry.record.streamlineSessionId).not.toBe(first.record.streamlineSessionId); const automaticRetry = prepareAgentSession({ workspaceDir: dir, taskKind: "security", taskId: "security-1", provider: "claude", }); expect(automaticRetry.resume).toBe(true); expect(automaticRetry.record.streamlineSessionId).toBe(manualRetry.record.streamlineSessionId); }); test("a security action cannot rotate a non-security session", () => { const dir = workspace(); const feature = prepareAgentSession({ workspaceDir: dir, taskKind: "feature", taskId: "feature-1", provider: "claude", }); expect(() => rotateSecurityAgentSession(dir, "manual-reopen")).toThrow(); const next = prepareAgentSession({ workspaceDir: dir, taskKind: "feature", taskId: "feature-1", provider: "claude", }); expect(next.resume).toBe(true); expect(next.record.streamlineSessionId).toBe(feature.record.streamlineSessionId); }); });