import { EventId, MessageId, ThreadId, TurnId, type OrchestrationThreadActivity, } from "@t3tools/contracts"; import { describe, expect, it } from "vitest"; import { deriveActiveWorkStartedAt, deriveActivePlanState, PROVIDER_OPTIONS, derivePendingApprovals, derivePendingUserInputs, deriveTimelineEntries, deriveWorkLogEntries, findLatestProposedPlan, findSidebarProposedPlan, hasActionableProposedPlan, hasToolActivityForTurn, isLatestTurnSettled, } from "./session-logic"; function makeActivity(overrides: { id?: string; createdAt?: string; kind?: string; summary?: string; tone?: OrchestrationThreadActivity["tone"]; payload?: Record; turnId?: string; sequence?: number; }): OrchestrationThreadActivity { const payload = overrides.payload ?? {}; return { id: EventId.makeUnsafe(overrides.id ?? crypto.randomUUID()), createdAt: overrides.createdAt ?? "2026-02-23T00:00:00.000Z", kind: overrides.kind ?? "tool.started", summary: overrides.summary ?? "Tool call", tone: overrides.tone ?? "tool", payload, turnId: overrides.turnId ? TurnId.makeUnsafe(overrides.turnId) : null, ...(overrides.sequence !== undefined ? { sequence: overrides.sequence } : {}), }; } describe("derivePendingApprovals", () => { it("tracks open approvals and removes resolved ones", () => { const activities: OrchestrationThreadActivity[] = [ makeActivity({ id: "approval-open", createdAt: "2026-02-23T00:00:01.000Z", kind: "approval.requested", summary: "Command approval requested", tone: "approval", payload: { requestId: "req-1", requestKind: "command", detail: "bun run lint", }, }), makeActivity({ id: "approval-close", createdAt: "2026-02-23T00:00:02.000Z", kind: "approval.resolved", summary: "Approval resolved", tone: "info", payload: { requestId: "req-2" }, }), makeActivity({ id: "approval-closed-request", createdAt: "2026-02-23T00:00:01.500Z", kind: "approval.requested", summary: "File-change approval requested", tone: "approval", payload: { requestId: "req-2", requestKind: "file-change" }, }), ]; expect(derivePendingApprovals(activities)).toEqual([ { requestId: "req-1", requestKind: "command", createdAt: "2026-02-23T00:00:01.000Z", detail: "bun run lint", }, ]); }); it("maps canonical requestType payloads into pending approvals", () => { const activities: OrchestrationThreadActivity[] = [ makeActivity({ id: "approval-open-request-type", createdAt: "2026-02-23T00:00:01.000Z", kind: "approval.requested", summary: "Command approval requested", tone: "approval", payload: { requestId: "req-request-type", requestType: "command_execution_approval", detail: "pwd", }, }), ]; expect(derivePendingApprovals(activities)).toEqual([ { requestId: "req-request-type", requestKind: "command", createdAt: "2026-02-23T00:00:01.000Z", detail: "pwd", }, ]); }); it("clears stale pending approvals when provider reports unknown pending request", () => { const activities: OrchestrationThreadActivity[] = [ makeActivity({ id: "approval-open-stale", createdAt: "2026-02-23T00:00:01.000Z", kind: "approval.requested", summary: "Command approval requested", tone: "approval", payload: { requestId: "req-stale-1", requestKind: "command", }, }), makeActivity({ id: "approval-failed-stale", createdAt: "2026-02-23T00:00:02.000Z", kind: "provider.approval.respond.failed", summary: "Provider approval response failed", tone: "error", payload: { requestId: "req-stale-1", detail: "Unknown pending permission request: req-stale-1", }, }), ]; expect(derivePendingApprovals(activities)).toEqual([]); }); }); describe("derivePendingUserInputs", () => { it("tracks open structured prompts and removes resolved ones", () => { const activities: OrchestrationThreadActivity[] = [ makeActivity({ id: "user-input-open", createdAt: "2026-02-23T00:00:01.000Z", kind: "user-input.requested", summary: "User input requested", tone: "info", payload: { requestId: "req-user-input-1", questions: [ { id: "sandbox_mode", header: "Sandbox", question: "Which mode should be used?", options: [ { label: "workspace-write", description: "Allow workspace writes only", }, ], }, ], }, }), makeActivity({ id: "user-input-resolved", createdAt: "2026-02-23T00:00:02.000Z", kind: "user-input.resolved", summary: "User input submitted", tone: "info", payload: { requestId: "req-user-input-2", answers: { sandbox_mode: "workspace-write", }, }, }), makeActivity({ id: "user-input-open-2", createdAt: "2026-02-23T00:00:01.500Z", kind: "user-input.requested", summary: "User input requested", tone: "info", payload: { requestId: "req-user-input-2", questions: [ { id: "approval", header: "Approval", question: "Continue?", options: [ { label: "yes", description: "Continue execution", }, ], }, ], }, }), ]; expect(derivePendingUserInputs(activities)).toEqual([ { requestId: "req-user-input-1", createdAt: "2026-02-23T00:00:01.000Z", questions: [ { id: "sandbox_mode", header: "Sandbox", question: "Which mode should be used?", options: [ { label: "workspace-write", description: "Allow workspace writes only", }, ], }, ], }, ]); }); }); describe("deriveActivePlanState", () => { it("returns the latest plan update for the active turn", () => { const activities: OrchestrationThreadActivity[] = [ makeActivity({ id: "plan-old", createdAt: "2026-02-23T00:00:01.000Z", kind: "turn.plan.updated", summary: "Plan updated", tone: "info", turnId: "turn-1", payload: { explanation: "Initial plan", plan: [{ step: "Inspect code", status: "pending" }], }, }), makeActivity({ id: "plan-latest", createdAt: "2026-02-23T00:00:02.000Z", kind: "turn.plan.updated", summary: "Plan updated", tone: "info", turnId: "turn-1", payload: { explanation: "Refined plan", plan: [{ step: "Implement Codex user input", status: "inProgress" }], }, }), ]; expect(deriveActivePlanState(activities, TurnId.makeUnsafe("turn-1"))).toEqual({ createdAt: "2026-02-23T00:00:02.000Z", turnId: "turn-1", explanation: "Refined plan", steps: [{ step: "Implement Codex user input", status: "inProgress" }], }); }); }); describe("findLatestProposedPlan", () => { it("prefers the latest proposed plan for the active turn", () => { expect( findLatestProposedPlan( [ { id: "plan:thread-1:turn:turn-1", turnId: TurnId.makeUnsafe("turn-1"), planMarkdown: "# Older", implementedAt: null, implementationThreadId: null, createdAt: "2026-02-23T00:00:01.000Z", updatedAt: "2026-02-23T00:00:01.000Z", }, { id: "plan:thread-1:turn:turn-1", turnId: TurnId.makeUnsafe("turn-1"), planMarkdown: "# Latest", implementedAt: null, implementationThreadId: null, createdAt: "2026-02-23T00:00:01.000Z", updatedAt: "2026-02-23T00:00:02.000Z", }, { id: "plan:thread-1:turn:turn-2", turnId: TurnId.makeUnsafe("turn-2"), planMarkdown: "# Different turn", implementedAt: null, implementationThreadId: null, createdAt: "2026-02-23T00:00:03.000Z", updatedAt: "2026-02-23T00:00:03.000Z", }, ], TurnId.makeUnsafe("turn-1"), ), ).toEqual({ id: "plan:thread-1:turn:turn-1", turnId: "turn-1", planMarkdown: "# Latest", implementedAt: null, implementationThreadId: null, createdAt: "2026-02-23T00:00:01.000Z", updatedAt: "2026-02-23T00:00:02.000Z", }); }); it("falls back to the most recently updated proposed plan", () => { const latestPlan = findLatestProposedPlan( [ { id: "plan:thread-1:turn:turn-1", turnId: TurnId.makeUnsafe("turn-1"), planMarkdown: "# First", implementedAt: null, implementationThreadId: null, createdAt: "2026-02-23T00:00:01.000Z", updatedAt: "2026-02-23T00:00:01.000Z", }, { id: "plan:thread-1:turn:turn-2", turnId: TurnId.makeUnsafe("turn-2"), planMarkdown: "# Latest", implementedAt: null, implementationThreadId: null, createdAt: "2026-02-23T00:00:02.000Z", updatedAt: "2026-02-23T00:00:03.000Z", }, ], null, ); expect(latestPlan?.planMarkdown).toBe("# Latest"); }); }); describe("hasActionableProposedPlan", () => { it("returns true for an unimplemented proposed plan", () => { expect( hasActionableProposedPlan({ id: "plan-1", turnId: TurnId.makeUnsafe("turn-1"), planMarkdown: "# Plan", implementedAt: null, implementationThreadId: null, createdAt: "2026-02-23T00:00:00.000Z", updatedAt: "2026-02-23T00:00:01.000Z", }), ).toBe(true); }); it("returns false for a proposed plan already implemented elsewhere", () => { expect( hasActionableProposedPlan({ id: "plan-1", turnId: TurnId.makeUnsafe("turn-1"), planMarkdown: "# Plan", implementedAt: "2026-02-23T00:00:02.000Z", implementationThreadId: ThreadId.makeUnsafe("thread-implement"), createdAt: "2026-02-23T00:00:00.000Z", updatedAt: "2026-02-23T00:00:02.000Z", }), ).toBe(false); }); }); describe("findSidebarProposedPlan", () => { it("prefers the running turn source proposed plan when available on the same thread", () => { expect( findSidebarProposedPlan({ threads: [ { id: ThreadId.makeUnsafe("thread-1"), proposedPlans: [ { id: "plan-1", turnId: TurnId.makeUnsafe("turn-plan"), planMarkdown: "# Source plan", implementedAt: "2026-02-23T00:00:03.000Z", implementationThreadId: ThreadId.makeUnsafe("thread-2"), createdAt: "2026-02-23T00:00:01.000Z", updatedAt: "2026-02-23T00:00:02.000Z", }, ], }, { id: ThreadId.makeUnsafe("thread-2"), proposedPlans: [ { id: "plan-2", turnId: TurnId.makeUnsafe("turn-other"), planMarkdown: "# Latest elsewhere", implementedAt: null, implementationThreadId: null, createdAt: "2026-02-23T00:00:04.000Z", updatedAt: "2026-02-23T00:00:05.000Z", }, ], }, ], latestTurn: { turnId: TurnId.makeUnsafe("turn-implementation"), sourceProposedPlan: { threadId: ThreadId.makeUnsafe("thread-1"), planId: "plan-1", }, }, latestTurnSettled: false, threadId: ThreadId.makeUnsafe("thread-1"), }), ).toEqual({ id: "plan-1", turnId: "turn-plan", planMarkdown: "# Source plan", implementedAt: "2026-02-23T00:00:03.000Z", implementationThreadId: "thread-2", createdAt: "2026-02-23T00:00:01.000Z", updatedAt: "2026-02-23T00:00:02.000Z", }); }); it("falls back to the latest proposed plan once the turn is settled", () => { expect( findSidebarProposedPlan({ threads: [ { id: ThreadId.makeUnsafe("thread-1"), proposedPlans: [ { id: "plan-1", turnId: TurnId.makeUnsafe("turn-plan"), planMarkdown: "# Older", implementedAt: null, implementationThreadId: null, createdAt: "2026-02-23T00:00:01.000Z", updatedAt: "2026-02-23T00:00:02.000Z", }, { id: "plan-2", turnId: TurnId.makeUnsafe("turn-latest"), planMarkdown: "# Latest", implementedAt: null, implementationThreadId: null, createdAt: "2026-02-23T00:00:03.000Z", updatedAt: "2026-02-23T00:00:04.000Z", }, ], }, ], latestTurn: { turnId: TurnId.makeUnsafe("turn-implementation"), sourceProposedPlan: { threadId: ThreadId.makeUnsafe("thread-1"), planId: "plan-1", }, }, latestTurnSettled: true, threadId: ThreadId.makeUnsafe("thread-1"), })?.planMarkdown, ).toBe("# Latest"); }); }); describe("deriveWorkLogEntries", () => { it("omits tool started entries and keeps completed entries", () => { const activities: OrchestrationThreadActivity[] = [ makeActivity({ id: "tool-complete", createdAt: "2026-02-23T00:00:03.000Z", summary: "Tool call complete", kind: "tool.completed", }), makeActivity({ id: "tool-start", createdAt: "2026-02-23T00:00:02.000Z", summary: "Tool call", kind: "tool.started", }), ]; const entries = deriveWorkLogEntries(activities, undefined); expect(entries.map((entry) => entry.id)).toEqual(["tool-complete"]); }); it("omits task start and completion lifecycle entries", () => { const activities: OrchestrationThreadActivity[] = [ makeActivity({ id: "task-start", createdAt: "2026-02-23T00:00:01.000Z", kind: "task.started", summary: "default task started", tone: "info", }), makeActivity({ id: "task-progress", createdAt: "2026-02-23T00:00:02.000Z", kind: "task.progress", summary: "Updating files", tone: "info", }), makeActivity({ id: "task-complete", createdAt: "2026-02-23T00:00:03.000Z", kind: "task.completed", summary: "Task completed", tone: "info", }), ]; const entries = deriveWorkLogEntries(activities, undefined); expect(entries.map((entry) => entry.id)).toEqual(["task-progress"]); }); it("filters by turn id when provided", () => { const activities: OrchestrationThreadActivity[] = [ makeActivity({ id: "turn-1", turnId: "turn-1", summary: "Tool call", kind: "tool.started" }), makeActivity({ id: "turn-2", turnId: "turn-2", summary: "Tool call complete", kind: "tool.completed", }), makeActivity({ id: "no-turn", summary: "Checkpoint captured", tone: "info" }), ]; const entries = deriveWorkLogEntries(activities, TurnId.makeUnsafe("turn-2")); expect(entries.map((entry) => entry.id)).toEqual(["turn-2"]); }); it("omits checkpoint captured info entries", () => { const activities: OrchestrationThreadActivity[] = [ makeActivity({ id: "checkpoint", createdAt: "2026-02-23T00:00:01.000Z", summary: "Checkpoint captured", tone: "info", }), makeActivity({ id: "tool-complete", createdAt: "2026-02-23T00:00:02.000Z", summary: "Ran command", tone: "tool", kind: "tool.completed", }), ]; const entries = deriveWorkLogEntries(activities, undefined); expect(entries.map((entry) => entry.id)).toEqual(["tool-complete"]); }); it("omits ExitPlanMode lifecycle entries once the plan card is shown", () => { const activities: OrchestrationThreadActivity[] = [ makeActivity({ id: "exit-plan-updated", createdAt: "2026-02-23T00:00:01.000Z", kind: "tool.updated", summary: "Tool call", payload: { detail: 'ExitPlanMode: {"allowedPrompts":[{"tool":"Bash","prompt":"run tests"}]}', }, }), makeActivity({ id: "exit-plan-completed", createdAt: "2026-02-23T00:00:02.000Z", kind: "tool.completed", summary: "Tool call", payload: { detail: "ExitPlanMode: {}", }, }), makeActivity({ id: "real-work-log", createdAt: "2026-02-23T00:00:03.000Z", kind: "tool.completed", summary: "Ran command", payload: { itemType: "command_execution", detail: "Bash: bun test", }, }), ]; const entries = deriveWorkLogEntries(activities, undefined); expect(entries.map((entry) => entry.id)).toEqual(["real-work-log"]); }); it("orders work log by activity sequence when present", () => { const activities: OrchestrationThreadActivity[] = [ makeActivity({ id: "second", createdAt: "2026-02-23T00:00:03.000Z", sequence: 2, summary: "Tool call complete", kind: "tool.completed", }), makeActivity({ id: "first", createdAt: "2026-02-23T00:00:04.000Z", sequence: 1, summary: "Tool call complete", kind: "tool.completed", }), ]; const entries = deriveWorkLogEntries(activities, undefined); expect(entries.map((entry) => entry.id)).toEqual(["first", "second"]); }); it("extracts command text for command tool activities", () => { const activities: OrchestrationThreadActivity[] = [ makeActivity({ id: "command-tool", kind: "tool.completed", summary: "Ran command", payload: { itemType: "command_execution", data: { item: { command: ["bun", "run", "lint"], }, }, }, }), ]; const [entry] = deriveWorkLogEntries(activities, undefined); expect(entry?.command).toBe("bun run lint"); }); it("keeps compact Codex tool metadata used for icons and labels", () => { const activities: OrchestrationThreadActivity[] = [ makeActivity({ id: "tool-with-metadata", kind: "tool.completed", summary: "bash", payload: { itemType: "command_execution", title: "bash", status: "completed", detail: '{ "dev": "vite dev --port 3000" } ', data: { item: { command: ["bun", "run", "dev"], result: { content: '{ "dev": "vite dev --port 3000" } ', exitCode: 0, }, }, }, }, }), ]; const [entry] = deriveWorkLogEntries(activities, undefined); expect(entry).toMatchObject({ command: "bun run dev", detail: '{ "dev": "vite dev --port 3000" }', itemType: "command_execution", toolTitle: "bash", }); }); it("extracts changed file paths for file-change tool activities", () => { const activities: OrchestrationThreadActivity[] = [ makeActivity({ id: "file-tool", kind: "tool.completed", summary: "File change", payload: { itemType: "file_change", data: { item: { changes: [ { path: "apps/web/src/components/ChatView.tsx" }, { filename: "apps/web/src/session-logic.ts" }, ], }, }, }, }), ]; const [entry] = deriveWorkLogEntries(activities, undefined); expect(entry?.changedFiles).toEqual([ "apps/web/src/components/ChatView.tsx", "apps/web/src/session-logic.ts", ]); }); }); describe("deriveTimelineEntries", () => { it("includes proposed plans alongside messages and work entries in chronological order", () => { const entries = deriveTimelineEntries( [ { id: MessageId.makeUnsafe("message-1"), role: "assistant", text: "hello", createdAt: "2026-02-23T00:00:01.000Z", streaming: false, }, ], [ { id: "plan:thread-1:turn:turn-1", turnId: TurnId.makeUnsafe("turn-1"), planMarkdown: "# Ship it", implementedAt: null, implementationThreadId: null, createdAt: "2026-02-23T00:00:02.000Z", updatedAt: "2026-02-23T00:00:02.000Z", }, ], [ { id: "work-1", createdAt: "2026-02-23T00:00:03.000Z", label: "Ran tests", tone: "tool", }, ], ); expect(entries.map((entry) => entry.kind)).toEqual(["message", "proposed-plan", "work"]); expect(entries[1]).toMatchObject({ kind: "proposed-plan", proposedPlan: { planMarkdown: "# Ship it", implementedAt: null, implementationThreadId: null, }, }); }); }); describe("hasToolActivityForTurn", () => { it("returns false when turn id is missing", () => { const activities: OrchestrationThreadActivity[] = [ makeActivity({ id: "tool-1", turnId: "turn-1", kind: "tool.completed", tone: "tool" }), ]; expect(hasToolActivityForTurn(activities, undefined)).toBe(false); expect(hasToolActivityForTurn(activities, null)).toBe(false); }); it("returns true only for matching tool activity in the target turn", () => { const activities: OrchestrationThreadActivity[] = [ makeActivity({ id: "tool-1", turnId: "turn-1", kind: "tool.completed", tone: "tool" }), makeActivity({ id: "info-1", turnId: "turn-2", kind: "turn.completed", tone: "info" }), ]; expect(hasToolActivityForTurn(activities, TurnId.makeUnsafe("turn-1"))).toBe(true); expect(hasToolActivityForTurn(activities, TurnId.makeUnsafe("turn-2"))).toBe(false); }); }); describe("isLatestTurnSettled", () => { const latestTurn = { turnId: TurnId.makeUnsafe("turn-1"), startedAt: "2026-02-27T21:10:00.000Z", completedAt: "2026-02-27T21:10:06.000Z", } as const; it("returns false while the same turn is still active in a running session", () => { expect( isLatestTurnSettled(latestTurn, { orchestrationStatus: "running", activeTurnId: TurnId.makeUnsafe("turn-1"), }), ).toBe(false); }); it("returns false while any turn is running to avoid stale latest-turn banners", () => { expect( isLatestTurnSettled(latestTurn, { orchestrationStatus: "running", activeTurnId: TurnId.makeUnsafe("turn-2"), }), ).toBe(false); }); it("returns true once the session is no longer running that turn", () => { expect( isLatestTurnSettled(latestTurn, { orchestrationStatus: "ready", activeTurnId: undefined, }), ).toBe(true); }); it("returns false when turn timestamps are incomplete", () => { expect( isLatestTurnSettled( { turnId: TurnId.makeUnsafe("turn-1"), startedAt: null, completedAt: "2026-02-27T21:10:06.000Z", }, null, ), ).toBe(false); }); }); describe("deriveActiveWorkStartedAt", () => { const latestTurn = { turnId: TurnId.makeUnsafe("turn-1"), startedAt: "2026-02-27T21:10:00.000Z", completedAt: "2026-02-27T21:10:06.000Z", } as const; it("prefers the in-flight turn start when the latest turn is not settled", () => { expect( deriveActiveWorkStartedAt( latestTurn, { orchestrationStatus: "running", activeTurnId: TurnId.makeUnsafe("turn-1"), }, "2026-02-27T21:11:00.000Z", ), ).toBe("2026-02-27T21:10:00.000Z"); }); it("falls back to sendStartedAt once the latest turn is settled", () => { expect( deriveActiveWorkStartedAt( latestTurn, { orchestrationStatus: "ready", activeTurnId: undefined, }, "2026-02-27T21:11:00.000Z", ), ).toBe("2026-02-27T21:11:00.000Z"); }); it("uses sendStartedAt for a fresh send after the prior turn completed", () => { expect( deriveActiveWorkStartedAt( { turnId: TurnId.makeUnsafe("turn-1"), startedAt: "2026-02-27T21:10:00.000Z", completedAt: "2026-02-27T21:10:06.000Z", }, null, "2026-02-27T21:11:00.000Z", ), ).toBe("2026-02-27T21:11:00.000Z"); }); }); describe("PROVIDER_OPTIONS", () => { it("advertises Claude as available while keeping Cursor as a placeholder", () => { const claude = PROVIDER_OPTIONS.find((option) => option.value === "claudeAgent"); const cursor = PROVIDER_OPTIONS.find((option) => option.value === "cursor"); expect(PROVIDER_OPTIONS).toEqual([ { value: "codex", label: "Codex", available: true }, { value: "claudeAgent", label: "Claude", available: true }, { value: "cursor", label: "Cursor", available: false }, ]); expect(claude).toEqual({ value: "claudeAgent", label: "Claude", available: true, }); expect(cursor).toEqual({ value: "cursor", label: "Cursor", available: false, }); }); });