import { threadRuntimeIsActive, type EnvironmentThreadShell } from "./models.ts"; import { toSortableTimestamp } from "./threadSort.ts"; // Working section beta, shared so web and mobile fold and order the inbox the // same way. Off by default; each client owns its own toggle. type WorkingThreadInput = Pick< EnvironmentThreadShell, | "hasActionableProposedPlan" | "hasPendingApprovals" | "hasPendingUserInput" | "interactionMode" | "latestRun" | "runtime" >; /** Threads busy with work that does not need the user fold into the Working section: a running run, or one stopped with background work that will wake it. Approvals, questions, plan prompts, and failures stay in the inbox. */ export function isThreadWorking(thread: WorkingThreadInput): boolean { if (thread.hasPendingApprovals || thread.hasPendingUserInput) return false; if (!threadRuntimeIsActive(thread.runtime) && thread.runtime?.status !== "idle") return false; // A plan prompt outranks lingering background work: the user has to act on it. const run = thread.latestRun; const runSettled = run !== null && run.status !== "preparing" && run.status !== "queued" && run.status !== "starting" && run.status !== "running" && run.status !== "waiting" && thread.runtime?.activeRunId !== run.runId; return !(thread.interactionMode === "plan" && thread.hasActionableProposedPlan && runSettled); } type InboxThreadInput = Pick< EnvironmentThreadShell, "id" | "environmentId" | "createdAt" | "unsettledAt" | "latestRun" >; /** The inbox lists threads newest first by when each last came back to the user, so a thread that leaves the Working section lands on top. `observedReturnAt` adds returns the server does not stamp, such as an approval request mid-turn or background work ending. */ export function sortInboxThreadsByReturn( threads: readonly T[], observedReturnAt?: (thread: T) => number | undefined, ): T[] { const timestamps = new Map( threads.map((thread) => [ thread, Math.max( toSortableTimestamp(thread.createdAt) ?? 0, toSortableTimestamp(thread.unsettledAt ?? undefined) ?? 0, toSortableTimestamp(thread.latestRun?.requestedAt ?? undefined) ?? 0, toSortableTimestamp(thread.latestRun?.completedAt ?? undefined) ?? 0, observedReturnAt?.(thread) ?? 0, ), ]), ); return [...threads].sort( (left, right) => timestamps.get(right)! - timestamps.get(left)! || left.id.localeCompare(right.id) || left.environmentId.localeCompare(right.environmentId), ); } /** * Remembers when this client saw each thread leave the Working section. Keep * one at module scope so the inbox order survives routes that unmount the * list. Call `observe` with every thread shell on each list rebuild, or with * null to reset while the beta is off. The first call only takes a baseline, * so mounting never reshuffles the inbox. */ export function createInboxReturnTracker() { const keyOf = (thread: Pick) => `${thread.environmentId}:${thread.id}`; let lastWorkingKeys: ReadonlySet | null = null; const returns = new Map(); return { observe(threads: ReadonlyArray | null): void { if (threads === null) { lastWorkingKeys = null; returns.clear(); return; } const working = new Set(); const present = new Set(); for (const thread of threads) { const key = keyOf(thread); present.add(key); if (isThreadWorking(thread)) working.add(key); } // Drop deleted threads so the map stays bounded by the live thread list. for (const key of returns.keys()) { if (!present.has(key)) returns.delete(key); } // The moment this client saw the change is the data; there is no // server stamp to read instead. // @effect-diagnostics-next-line globalDate:off const at = Date.now(); for (const key of lastWorkingKeys ?? []) { if (present.has(key) && !working.has(key)) returns.set(key, at); } lastWorkingKeys = working; }, returnedAt: (thread: Pick) => returns.get(keyOf(thread)), }; }