/** * Client-side state for T3 Code's orchestration (v2) streams, independent of * transport and rendering: the shell (projects + thread list) reducer, the * per-thread detail reducer, thread status / ordering for the glasses list, * one-line summaries of timeline items, and the dispatch-command builders. * * Mirrors T3's own client runtime (packages/client-runtime/src/state/ * shellReducer.ts, orchestrationV2Projection.ts, models.ts; apps/mobile's * threadListV2.ts and threadActivity.ts) in reduced form. Wire objects are * typed loosely: only the fields read here are declared, and everything is * treated as possibly missing so newer/older servers degrade gracefully. */ // --------------------------------------------------------------------------- // Wire shapes (subset) export type T3ProjectShell = { id: string; title: string; workspaceRoot?: string; defaultModelSelection?: T3ModelSelection | null; }; export type T3ModelSelection = { instanceId: string; model: string; options?: unknown[] }; export type T3ThreadShell = { id: string; projectId: string; title: string; providerInstanceId?: string | null; modelSelection?: T3ModelSelection | null; status?: string; activityRunStatus?: string | null; activeRunId?: string | null; latestRunId?: string | null; activeProviderThreadId?: string | null; latestRunRequestedAt?: string | null; latestRunCompletedAt?: string | null; lastVisitedAt?: string | null; pendingRuntimeRequest?: { id: string; kind: string; createdAt?: string } | null; pendingBackgroundTasks?: Array<{ kind?: string }>; lastError?: string | null; lastErrorClass?: string | null; usageLimitResetAt?: string | null; latestUserMessageAt?: string | null; latestUserAuthoredMessageAt?: string | null; hasActionableProposedPlan?: boolean; createdAt?: string; updatedAt?: string; archivedAt?: string | null; deletedAt?: string | null; settledOverride?: string | null; settledAt?: string | null; snoozedUntil?: string | null; snoozedAt?: string | null; lineage?: { relationshipToParent?: string | null } | null; }; export type T3Run = { id: string; status: string; userMessageId?: string | null; requestedAt?: string | null; queuePosition?: number | null; }; export type T3RunAttempt = { id: string; runId: string; rootNodeId?: string | null; status: string }; export type T3RuntimeRequest = { id: string; kind: string; status: string; responseCapability?: { type: string } | null; }; export type T3ConversationMessage = { id: string; role?: string; text?: string; delegatedCompletion?: unknown; notification?: unknown; }; export type T3PlanStep = { id?: string; text: string; status: string }; export type T3Plan = { id: string; kind: string; runId?: string | null; status?: string; steps?: T3PlanStep[]; }; export type T3ApprovalOption = { decision: string; label: string; warning?: string }; export type T3Question = { id: string; header?: string; question: string; options?: Array<{ label: string; description?: string; value?: string }>; multiSelect?: boolean; allowCustomAnswer?: boolean; required?: boolean; }; /** A timeline row. Fields beyond the common ones depend on `type`. */ export type T3TurnItem = { id: string; threadId: string; runId: string | null; nodeId?: string | null; ordinal: number; status: string; title?: string | null; type: string; // user_message / assistant_message / reasoning text?: string; streaming?: boolean; inputIntent?: string; // command_execution input?: unknown; exitCode?: number; outputIndicatesFailure?: boolean; // file_change fileName?: string; additions?: number; deletions?: number; changes?: unknown[]; // file_search / web_search pattern?: string; patterns?: string[]; // dynamic_tool toolName?: string | null; // approval_request / user_input_request requestId?: string; requestKind?: string; prompt?: string; appName?: string; options?: T3ApprovalOption[]; questions?: T3Question[]; responseMode?: string; // notification / system_notice / run_interrupt_result summary?: string; detail?: string; message?: string; // error failure?: { class?: string; message?: string; code?: string | null } | null; // compaction beforeTokenCount?: number; afterTokenCount?: number; // handoff toModel?: string; // proposed_plan markdown?: string; }; type ProjectedTurnItem = { visibility?: string; item: T3TurnItem }; // --------------------------------------------------------------------------- // Shell (projects + thread list) export class ShellModel { readonly projects = new Map(); readonly threads = new Map(); /** Last applied sequence (the resume cursor), or null before the first snapshot. */ sequence: number | null = null; /** The server finished replaying and the stream is live. */ synchronized = false; /** Apply one subscribeShell stream item; returns whether anything changed. */ apply(raw: unknown): boolean { const item = raw as Record; switch (item?.kind) { case "synchronized": this.synchronized = true; return true; case "snapshot": { const snapshot = item.snapshot ?? {}; if (Array.isArray(item.resolvedRepositoryIdentityRoots)) { // Metadata-only refresh (carries threads: []): never a replacement. return false; } this.projects.clear(); this.threads.clear(); for (const project of snapshot.projects ?? []) this.projects.set(project.id, project); for (const thread of snapshot.threads ?? []) this.threads.set(thread.id, thread); this.sequence = typeof snapshot.snapshotSequence === "number" ? snapshot.snapshotSequence : 0; return true; } case "project.updated": case "project.removed": case "thread.updated": case "thread.removed": { const sequence = typeof item.sequence === "number" ? item.sequence : null; if (sequence !== null && this.sequence !== null && sequence <= this.sequence) return false; if (item.kind === "project.updated" && item.project) this.projects.set(item.project.id, item.project); else if (item.kind === "project.removed") this.projects.delete(item.projectId); else if (item.kind === "thread.updated" && item.thread) { if (item.location === "archive") this.threads.delete(item.thread.id); else this.threads.set(item.thread.id, item.thread); } else if (item.kind === "thread.removed") this.threads.delete(item.threadId); if (sequence !== null) this.sequence = sequence; return true; } default: return false; } } } // --------------------------------------------------------------------------- // Thread status and list ordering (as T3's mobile thread list derives them) export type T3ThreadStatus = /** A provider approval is waiting on the user. */ | "approval" /** The provider asked the user a question. */ | "input" | "working" /** Parked on background work (subagent, monitor) that will wake it. */ | "waiting" | "failed" /** Failed on a usage limit. */ | "limited" | "ready"; const ACTIVE_RUN_STATUSES = new Set(["preparing", "queued", "starting", "running", "waiting"]); const WAKING_BACKGROUND_KINDS = new Set(["subagent", "monitor", "background_task"]); export function threadStatus(thread: T3ThreadShell): T3ThreadStatus { const request = thread.pendingRuntimeRequest; if (request && request.kind !== "user_input" && request.kind !== "auth_refresh") return "approval"; if (request?.kind === "user_input") return "input"; if (thread.latestRunId == null && thread.activeProviderThreadId == null) return "ready"; const background = thread.status !== "failed" && (thread.pendingBackgroundTasks ?? []).some((task) => WAKING_BACKGROUND_KINDS.has(task.kind ?? "")); const runStatus = background ? "idle" : (thread.activityRunStatus ?? thread.status ?? "idle"); if (ACTIVE_RUN_STATUSES.has(runStatus)) return "working"; if (runStatus === "idle") return "waiting"; if (runStatus === "failed") return thread.lastErrorClass === "usage_limit" ? "limited" : "failed"; return "ready"; } export function threadStatusLabel(status: T3ThreadStatus): string { switch (status) { case "approval": return "Needs approval"; case "input": return "Has a question"; case "working": return "Working"; case "waiting": return "Waiting"; case "failed": return "Failed"; case "limited": return "Usage limit"; case "ready": return "Ready"; } } const STATUS_MARKERS: Record = { approval: { marker: "!", value: 255 }, input: { marker: "?", value: 255 }, working: { marker: "…", value: 210 }, waiting: { marker: "…", value: 130 }, failed: { marker: "x", value: 200 }, limited: { marker: "x", value: 160 }, ready: { marker: "", value: 0 }, }; /** * The one-glyph status mark drawn left of a thread title (in the app's list * and on the Glanceboard), with its gray level: ! approval, ? question, * … working, x failed, ● finished since last visited, ✓ settled. */ export function threadMarker(status: T3ThreadStatus, flags: { settled?: boolean; unread?: boolean } = {}): { marker: string; value: number } { if (status === "ready" && flags.settled) return { marker: "✓", value: 110 }; if (status === "ready" && flags.unread) return { marker: "●", value: 220 }; return STATUS_MARKERS[status]; } function timeMs(value: string | null | undefined): number { if (!value) return 0; const parsed = Date.parse(value); return Number.isFinite(parsed) ? parsed : 0; } /** A run finished since the user (on any client) last looked at the thread. */ export function isThreadUnread(thread: T3ThreadShell): boolean { const completed = timeMs(thread.latestRunCompletedAt); const visited = timeMs(thread.lastVisitedAt); return completed > 0 && visited > 0 && completed > visited; } export function isThreadSettled(thread: T3ThreadShell): boolean { return thread.settledOverride === "settled"; } export function isThreadSnoozed(thread: T3ThreadShell, nowMs: number): boolean { if (timeMs(thread.snoozedUntil) <= nowMs) return false; // A raised hand (question, approval, failure, or a run finishing after the // snooze) brings it back. const status = threadStatus(thread); if (status === "approval" || status === "input" || status === "failed") return false; return !(timeMs(thread.latestRunCompletedAt) > timeMs(thread.snoozedAt)); } /** Archived, deleted and provider-run subagent threads stay out of the list. */ export function isThreadListed(thread: T3ThreadShell): boolean { return !thread.archivedAt && !thread.deletedAt && thread.lineage?.relationshipToParent !== "subagent"; } /** When the thread last saw activity worth sorting by. */ export function threadActivityMs(thread: T3ThreadShell): number { return timeMs(thread.latestUserMessageAt) || timeMs(thread.updatedAt) || timeMs(thread.createdAt); } function settledMs(thread: T3ThreadShell): number { return timeMs(thread.settledAt) || threadActivityMs(thread); } export type ThreadSection = "attention" | "working" | "recent" | "snoozed" | "settled"; export type ThreadSections = Record; /** * The glasses list: threads that need the user first, then running ones * (ordered by when the user last wrote, so finishing doesn't reshuffle * them), then everything else by recent activity; snoozed and settled * threads are kept apart. */ export function sectionThreads(threads: Iterable, nowMs: number): ThreadSections { const sections: ThreadSections = { attention: [], working: [], recent: [], snoozed: [], settled: [] }; for (const thread of threads) { if (!isThreadListed(thread)) continue; const status = threadStatus(thread); if (status === "approval" || status === "input") sections.attention.push(thread); else if (isThreadSnoozed(thread, nowMs)) sections.snoozed.push(thread); else if (isThreadSettled(thread)) sections.settled.push(thread); else if (status === "working" || status === "waiting") sections.working.push(thread); else sections.recent.push(thread); } const byId = (a: T3ThreadShell, b: T3ThreadShell) => a.id.localeCompare(b.id); sections.attention.sort((a, b) => timeMs(b.pendingRuntimeRequest?.createdAt) - timeMs(a.pendingRuntimeRequest?.createdAt) || byId(a, b)); const sentMs = (thread: T3ThreadShell) => timeMs(thread.latestUserAuthoredMessageAt) || timeMs(thread.latestRunRequestedAt); sections.working.sort((a, b) => sentMs(b) - sentMs(a) || byId(a, b)); sections.recent.sort((a, b) => threadActivityMs(b) - threadActivityMs(a) || byId(a, b)); sections.snoozed.sort((a, b) => timeMs(a.snoozedUntil) - timeMs(b.snoozedUntil) || byId(a, b)); sections.settled.sort((a, b) => settledMs(b) - settledMs(a) || byId(a, b)); return sections; } /** "now", "5m", "3h", "2d", "Mar 4" style age. */ export function formatAge(thenMs: number, nowMs: number): string { if (!thenMs) return ""; const seconds = Math.max(0, Math.round((nowMs - thenMs) / 1000)); if (seconds < 60) return "now"; const minutes = Math.round(seconds / 60); if (minutes < 60) return `${minutes}m`; const hours = Math.round(minutes / 60); if (hours < 24) return `${hours}h`; const days = Math.round(hours / 24); if (days < 14) return `${days}d`; const date = new Date(thenMs); const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; return `${months[date.getMonth()]} ${date.getDate()}`; } // --------------------------------------------------------------------------- // Thread detail (subscribeThread) const MAX_ITEMS_KEPT = 400; export class ThreadDetailModel { /** Last applied sequence (the resume cursor), or null before the first snapshot. */ sequence: number | null = null; synchronized = false; deleted = false; thread: Record | null = null; readonly runs = new Map(); readonly attempts = new Map(); readonly runtimeRequests = new Map(); readonly messages = new Map(); readonly plans = new Map(); /** This thread's own timeline rows, by id. */ readonly items = new Map(); /** Rows inherited from a forked-from thread: a frozen prefix. */ private inherited: T3TurnItem[] = []; /** Bounded-window bookkeeping: older history exists that we didn't load. */ hasMoreHistory = false; private historyCursor: string | null = null; private latestLocalTurnOrdinal: number | null = null; constructor(readonly threadId: string) {} /** Apply one subscribeThread stream item; returns whether anything changed. */ apply(raw: unknown): boolean { const item = raw as Record; switch (item?.kind) { case "synchronized": this.synchronized = true; return true; case "snapshot": this.applySnapshot(item); return true; case "event": { const sequence = typeof item.sequence === "number" ? item.sequence : null; if (sequence !== null && this.sequence !== null && sequence <= this.sequence) return false; if (sequence !== null) this.sequence = sequence; return this.applyEvent(item.event ?? {}); } default: return false; } } private applySnapshot(item: Record): void { const projection = item.projection ?? {}; this.sequence = typeof item.snapshotSequence === "number" ? item.snapshotSequence : 0; this.thread = projection.thread ?? null; this.deleted = Boolean(this.thread?.deletedAt); const fill = (map: Map, values: T[] | undefined) => { map.clear(); for (const value of values ?? []) map.set(value.id, value); }; fill(this.runs, projection.runs); fill(this.attempts, projection.attempts); fill(this.runtimeRequests, projection.runtimeRequests); fill(this.messages, projection.messages); fill(this.plans, projection.plans); this.items.clear(); this.inherited = []; for (const row of (projection.visibleTurnItems ?? []) as ProjectedTurnItem[]) { if (!row?.item) continue; if (row.visibility === "local") this.items.set(row.item.id, row.item); else this.inherited.push(row.item); } this.hasMoreHistory = Boolean(item.hasMoreHistory); this.historyCursor = typeof item.historyCursor === "string" ? item.historyCursor : null; this.latestLocalTurnOrdinal = typeof item.latestLocalTurnOrdinal === "number" ? item.latestLocalTurnOrdinal : null; } private applyEvent(event: Record): boolean { const payload = event.payload; const type = String(event.type ?? ""); if (type === "thread.deleted") { this.deleted = true; return true; } if (type.startsWith("thread.")) { if (payload && typeof payload === "object") this.thread = payload; return true; } switch (type) { case "run.created": case "run.updated": this.runs.set(payload.id, payload); return true; case "run-attempt.created": case "run-attempt.updated": this.attempts.set(payload.id, payload); return true; case "runtime-request.updated": this.runtimeRequests.set(payload.id, payload); return true; case "message.updated": this.messages.set(payload.id, payload); return true; case "plan.updated": this.plans.set(payload.id, payload); return true; case "turn-item.updated": return this.upsertItem(payload as T3TurnItem); default: // Unknown and uninteresting events still advance the cursor. return false; } } private upsertItem(item: T3TurnItem): boolean { if (!item?.id || item.threadId !== this.threadId) return false; const partial = this.hasMoreHistory || this.historyCursor !== null; if (!this.items.has(item.id) && partial) { // Updates to rows from history we never loaded. if (this.latestLocalTurnOrdinal !== null && item.ordinal <= this.latestLocalTurnOrdinal) return false; const oldest = this.oldestLocalOrdinal(); if (oldest !== null && item.ordinal < oldest) return false; } this.items.set(item.id, item); if (partial && (this.latestLocalTurnOrdinal === null || item.ordinal > this.latestLocalTurnOrdinal)) { this.latestLocalTurnOrdinal = item.ordinal; } if (this.items.size > MAX_ITEMS_KEPT) this.trimOldest(); return true; } private oldestLocalOrdinal(): number | null { let oldest: number | null = null; for (const item of this.items.values()) if (oldest === null || item.ordinal < oldest) oldest = item.ordinal; return oldest; } /** A long-lived subscription keeps growing; drop the oldest rows (they're history now). */ private trimOldest(): void { const sorted = [...this.items.values()].sort(compareItems); for (const item of sorted.slice(0, sorted.length - MAX_ITEMS_KEPT)) this.items.delete(item.id); this.hasMoreHistory = true; this.inherited = []; } /** Visible rows in display order (inherited fork history first). */ timeline(): T3TurnItem[] { const local = [...this.items.values()].filter((item) => this.isVisible(item)).sort(compareItems); return [...this.inherited, ...local]; } private isVisible(item: T3TurnItem): boolean { const run = item.runId ? this.runs.get(item.runId) : undefined; if (run?.status === "rolled_back") return false; if (item.type === "user_message" && item.inputIntent === "queued_turn" && run?.status === "cancelled") return false; if (item.type === "run_interrupt_result" && item.runId && item.nodeId) { const superseded = [...this.attempts.values()].some( (attempt) => attempt.runId === item.runId && attempt.rootNodeId === item.nodeId && attempt.status === "superseded", ); if (superseded) { const hasRequest = [...this.items.values()].some( (candidate) => candidate.type === "run_interrupt_request" && candidate.runId === item.runId, ); if (!hasRequest) return false; } } return true; } /** The run a Stop should interrupt, if any. */ activeRunId(): string | null { let active: T3Run | null = null; for (const run of this.runs.values()) { if (["preparing", "starting", "running", "waiting"].includes(run.status)) { if (!active || timeMs(run.requestedAt) >= timeMs(active.requestedAt)) active = run; } } return active?.id ?? null; } /** Pending approvals and questions, oldest first, paired with their timeline rows. */ pendingRequests(): PendingRequest[] { const result: PendingRequest[] = []; const items = [...this.items.values()].sort(compareItems); for (const request of this.runtimeRequests.values()) { if (request.status !== "pending") continue; if (request.kind === "auth_refresh" || request.kind === "dynamic_tool_call") continue; const type = request.kind === "user_input" ? "user_input_request" : "approval_request"; const item = [...items].reverse().find((candidate) => candidate.type === type && candidate.requestId === request.id); if (!item) continue; const capability = request.responseCapability?.type ?? "live"; result.push({ request, item, kind: request.kind === "user_input" ? "question" : "approval", answerable: request.kind === "user_input" ? capability !== "not_resumable" : capability === "live", messageMode: item.responseMode === "message" || capability === "message", }); } return result.sort((a, b) => compareItems(a.item, b.item)); } /** Follow-ups waiting for the active run to finish (they have no timeline row yet). */ queuedMessages(): string[] { return [...this.runs.values()] .filter((run) => run.status === "queued" && run.userMessageId) .sort((a, b) => (a.queuePosition ?? 0) - (b.queuePosition ?? 0)) .map((run) => this.messages.get(run.userMessageId!)) .filter((message): message is T3ConversationMessage => Boolean(message && !message.delegatedCompletion && !message.notification)) .map((message) => message.text ?? ""); } /** The current todo list (the latest run's, else the newest one). */ todoList(): T3Plan | null { const todos = [...this.plans.values()].filter((plan) => plan.kind === "todo_list" && plan.steps?.length); if (!todos.length) return null; const latestRunId = this.thread?.latestRunId ?? null; return todos.find((plan) => plan.runId === latestRunId) ?? todos[todos.length - 1]!; } } export type PendingRequest = { request: T3RuntimeRequest; item: T3TurnItem; kind: "approval" | "question"; /** False when the provider session can't take an answer any more (interrupt instead). */ answerable: boolean; /** Message-mode questions: answers must be non-empty strings. */ messageMode: boolean; }; function compareItems(a: T3TurnItem, b: T3TurnItem): number { return a.ordinal - b.ordinal || String(a.id).localeCompare(String(b.id)); } // --------------------------------------------------------------------------- // Timeline presentation export type TimelineEntry = | { kind: "user"; id: string; text: string; queued?: boolean } | { kind: "assistant"; id: string; text: string; streaming: boolean } | { kind: "activity"; id: string; label: string; detail: string; state: ActivityState } | { kind: "request"; id: string; label: string; detail: string; resolved: boolean }; export type ActivityState = "running" | "done" | "failed" | "stopped"; function activityState(item: T3TurnItem): ActivityState { switch (item.status) { case "pending": case "running": case "waiting": return "running"; case "failed": return "failed"; case "cancelled": case "interrupted": return "stopped"; default: return "done"; } } function capitalize(text: string): string { return text ? text[0]!.toUpperCase() + text.slice(1) : text; } function oneLine(text: string | null | undefined): string { return (text ?? "").replace(/\s+/g, " ").trim(); } function baseName(path: string): string { return path.split(/[\\/]/).filter(Boolean).pop() ?? path; } /** `bash -lc '