@ordewell/core 0.5.5 → 0.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/IFileSystem-BkPX7mLD.d.mts +76 -0
- package/dist/IFileSystem-C0l-4MGT.d.ts +76 -0
- package/dist/{ModeResolver-D3XO0fT9.d.ts → ModeResolver--16lh7dS.d.mts} +1 -1
- package/dist/{ModeResolver-D-SUFRNF.d.mts → ModeResolver-CjpG5Wli.d.ts} +1 -1
- package/dist/Task-Dyxp67s2.d.mts +2086 -0
- package/dist/Task-Dyxp67s2.d.ts +2086 -0
- package/dist/{chunk-T2S5O36I.mjs → chunk-C44UWIAD.mjs} +6 -6
- package/dist/chunk-C44UWIAD.mjs.map +1 -0
- package/dist/{chunk-HD2FWPRV.mjs → chunk-EDGUFCIR.mjs} +6 -1
- package/dist/chunk-EDGUFCIR.mjs.map +1 -0
- package/dist/{chunk-UUBGVCGJ.mjs → chunk-JBEFAJ2W.mjs} +2 -2
- package/dist/{chunk-KLN7ELXO.mjs → chunk-ROVYWEBI.mjs} +1560 -1269
- package/dist/chunk-ROVYWEBI.mjs.map +1 -0
- package/dist/index.d.mts +1414 -785
- package/dist/index.d.ts +1414 -785
- package/dist/index.js +7008 -4389
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +6075 -3767
- package/dist/index.mjs.map +1 -1
- package/dist/order-labels.d.mts +3 -1
- package/dist/order-labels.d.ts +3 -1
- package/dist/{parsing-CDtRSxBY.d.mts → parsing-DPEpAszP.d.mts} +2 -2
- package/dist/{parsing-BTP4bwkk.d.ts → parsing-EcmCsF1y.d.ts} +2 -2
- package/dist/parsing.d.mts +5 -3
- package/dist/parsing.d.ts +5 -3
- package/dist/parsing.js.map +1 -1
- package/dist/parsing.mjs +2 -2
- package/dist/{plan-utils-pE4TBwxl.d.mts → plan-utils-BAvW3hvl.d.mts} +212 -43
- package/dist/{plan-utils-BFaPo-IT.d.ts → plan-utils-BMyEiDKv.d.ts} +212 -43
- package/dist/plan-utils.d.mts +5 -4
- package/dist/plan-utils.d.ts +5 -4
- package/dist/plan-utils.js +351 -64
- package/dist/plan-utils.js.map +1 -1
- package/dist/plan-utils.mjs +11 -5
- package/dist/testing.d.mts +54 -4
- package/dist/testing.d.ts +54 -4
- package/dist/testing.js +93 -2
- package/dist/testing.js.map +1 -1
- package/dist/testing.mjs +91 -2
- package/dist/testing.mjs.map +1 -1
- package/package.json +2 -1
- package/skills/grilling/SKILL.md +6 -16
- package/skills/improve-codebase-architecture/SKILL.md +1 -1
- package/dist/ApprovalPolicy-BVhGdECT.d.mts +0 -79
- package/dist/ApprovalPolicy-BVhGdECT.d.ts +0 -79
- package/dist/ITerminalRunner-BV9Rd2o9.d.ts +0 -563
- package/dist/ITerminalRunner-C77ZNZS9.d.mts +0 -563
- package/dist/Task-Vl5Zq_D-.d.mts +0 -825
- package/dist/Task-Vl5Zq_D-.d.ts +0 -825
- package/dist/chunk-HD2FWPRV.mjs.map +0 -1
- package/dist/chunk-KLN7ELXO.mjs.map +0 -1
- package/dist/chunk-T2S5O36I.mjs.map +0 -1
- /package/dist/{chunk-UUBGVCGJ.mjs.map → chunk-JBEFAJ2W.mjs.map} +0 -0
package/dist/plan-utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/plan-utils.ts","../src/services/TaskOps.ts","../src/services/researchStepSummary.ts","../src/services/SessionMessage.ts","../src/services/isolationRecord.ts","../src/models/Usage.ts","../src/services/terminalRender.ts","../src/conversation/format.ts","../src/conversation/records.ts","../src/conversation/reduce.ts","../src/conversation/transcript.ts","../src/conversation/promptHold.ts","../src/conversation/turnGate.ts","../src/conversation/detail.ts","../src/conversation/notices.ts"],"sourcesContent":["/**\n * Browser-safe entry point: pure plan-validation utilities with no Node or\n * AI-service dependencies. UI surfaces (the VS Code webview runs in a browser)\n * import runtime values from here instead of the main barrel, which bundles the\n * Gemini/OpenAI services and their `fs`/`child_process` imports.\n */\nexport { canMergeTasks, canSplitTask, canSetDependencies, dependencyCandidates, dependentsOf } from './services/TaskOps';\nexport type { TaskRef } from './services/TaskOps';\nexport { summarizeToolCall } from './services/researchStepSummary';\nexport { truncateCheckpointSummary, CHECKPOINT_TRUNCATE_LENGTH } from './services/SessionMessage';\nexport { capConflictFiles } from './services/isolationRecord';\n// The shared conversation view (#51) is pure too, so every surface — a webview\n// included — draws from the same reducer.\nexport * from './conversation';\n","import { v4 as uuidv4 } from 'uuid';\nimport {\n Task, RunnerId, flattenTasks,\n addTaskToPlan, removeTaskFromPlan, updateTaskInPlan, renumberTasks,\n createTask,\n} from '../models/Task';\nimport { extractObjectsWithKey, stripTrailingCommas, escapeControlCharsInStrings, PlanParseError, TASK_OPS_ENVELOPE_KEY } from './JsonExtractor';\nimport { validateTaskEdit, checkModelAndModeValidity, type EditCatalog } from './TaskEditValidator';\n\n/**\n * Targeted task edits emitted by the planner conversation (the 'task_ops'\n * ConversationTurn). Task references accept a task id, a \"#<order>\" ref, or a\n * bare order number — cheap models rarely echo UUIDs correctly.\n */\nexport type TaskOp =\n | { op: 'update'; taskId: string; changes: Partial<Task> }\n | { op: 'add'; task: Partial<Task>; handle?: string }\n | { op: 'remove'; taskId: string }\n | { op: 'reorder'; taskIds: string[] }\n | { op: 'merge'; taskIds: string[]; merged: Partial<Task>; handle?: string }\n | { op: 'split'; taskId: string; parts: Partial<Task>[]; handle?: string }\n | { op: 'rearm'; taskId: string; changes?: Partial<Task> };\n\n/** Fields the planner may change on an existing task. Everything else (id, status, verdict…) is system-owned. */\nexport const UPDATABLE_FIELDS: (keyof Task)[] = [\n 'title', 'description', 'prompt', 'dependencies', 'assignedRunner', 'assignedModel', 'taskMode', 'thinkingEffort', 'type', 'userSteps', 'autonomy', 'sliceType',\n];\n\n/**\n * The edit protocol as the planner reads it, kept beside the applier so the\n * prose can never undersell (or oversell) what {@link applyTaskOps} accepts.\n * `executionNote` rides on the ordering rule because that is the rule a\n * running execution changes.\n */\nexport function taskOpsProtocol(executionNote = ''): string[] {\n const updateChanges = UPDATABLE_FIELDS\n .map((f) => (f === 'dependencies' ? '\"dependencies\"?:[\"<id or #order>\"]' : `\"${f}\"?`))\n .join(',');\n return [\n '- To modify specific tasks, reply with ONLY this JSON object:',\n ' {\"taskOps\": [',\n ` {\"op\":\"update\",\"taskId\":\"<id or #order>\",\"changes\":{${updateChanges}}},`,\n ' {\"op\":\"add\",\"task\":{\"title\",\"description\",\"prompt\",\"dependencies\":[\"<id or #order>\"],\"assignedRunner\"?,\"assignedModel\"?},\"handle\"?:\"<name>\"},',\n ' {\"op\":\"remove\",\"taskId\":\"<id or #order>\"},',\n ' {\"op\":\"reorder\",\"taskIds\":[\"<id or #order>\", \"... every task exactly once\"]}, // only to re-prioritise INDEPENDENT tasks',\n ' {\"op\":\"merge\",\"taskIds\":[\"<id or #order>\", \"...\"],\"merged\":{\"title\",\"description\",\"prompt\"?,\"assignedRunner\"?,\"assignedModel\"?,...},\"handle\"?:\"<name>\"},',\n ' {\"op\":\"split\",\"taskId\":\"<id or #order>\",\"parts\":[{\"title\",\"description\",\"prompt\"?,\"assignedRunner\"?,\"assignedModel\"?,...}, ...],\"handle\"?:\"<name>\"},',\n ` {\"op\":\"rearm\",\"taskId\":\"<id or #order>\",\"changes\"?:{${updateChanges}}}`,\n ' ]}',\n '- For sweeping changes, you may instead emit a full {\"tasks\":[...]} plan JSON.',\n 'Every \"<id or #order>\" ref in a batch resolves against the plan shown above, before any op in the batch runs — an earlier remove/merge/split never shifts what a later \"#N\" means.',\n 'Give \"add\", \"merge\", or \"split\" a \"handle\" (any name you choose, unused elsewhere in this batch) to let a LATER op in the same batch reference the task it produces — for \"split\", the handle names its last part. A handle used before its defining op is rejected.',\n 'When creating or changing tasks, set \"assignedRunner\" and \"assignedModel\" ({\"modelId\",\"modelLabel\",\"thinkingEffort\"?}) using only the runners and models listed in <available_models>.',\n 'Keep dependencies consistent: no cycles, no references to removed tasks, and never touch running or completed tasks — \"rearm\" is the one exception, below.',\n 'Just declare the dependencies you want — display order is repaired for you afterwards, so a rewire or a newly added prerequisite never needs a \"reorder\" op. Only a task that is running or completed cannot be shifted, so an edit that would need one to move is rejected.' + executionNote,\n 'Flipping \"type\" between \"ai\" and \"user\" is a content change, not just a label: an update to \"user\" needs \"userSteps\" in the SAME op, and an update to \"ai\" needs \"prompt\" in the SAME op — the model/mode/effort/autonomy fields (flipping to \"user\") or the userSteps (flipping to \"ai\") are cleared automatically.',\n '\"rearm\" puts a failed OR completed task back to pending — verdict and output summary are cleared, any dependents it had blocked are released, and it may carry field changes (e.g. a corrected \"prompt\") applied in the same op. A running task cannot be re-armed. Never rearm a task just to relabel it — only when it should actually run again.',\n ];\n}\n\nexport function textHasTaskOps(text: string): boolean {\n return text.includes(`\"${TASK_OPS_ENVELOPE_KEY}\"`);\n}\n\n/** Parse a `{\"taskOps\":[...]}` reply. Throws PlanParseError when the JSON is unusable. */\nexport function parseTaskOpsJson(text: string): TaskOp[] {\n // Extract by the taskOps key specifically — a reply may also carry a\n // tasks-keyed object (schema echo, example), which must not shadow the ops.\n // Candidates are tried back to front: the last emission is the final answer.\n const { matches, fallback } = extractObjectsWithKey(text, TASK_OPS_ENVELOPE_KEY);\n const candidates = matches.length > 0 ? [...matches].reverse() : [fallback.json];\n\n let lastError: PlanParseError | null = null;\n for (const json of candidates) {\n try {\n return parseTaskOpsObject(json, text);\n } catch (err) {\n if (!(err instanceof PlanParseError)) throw err;\n if (!lastError) lastError = err;\n }\n }\n throw lastError ?? new PlanParseError('No JSON object found in task-ops reply', text);\n}\n\nfunction parseTaskOpsObject(json: string, text: string): TaskOp[] {\n let parsed: unknown;\n try {\n parsed = JSON.parse(escapeControlCharsInStrings(stripTrailingCommas(json)));\n } catch (err) {\n throw new PlanParseError(`Task-ops JSON is invalid: ${err instanceof Error ? err.message : String(err)}`, text);\n }\n const ops = (parsed as { taskOps?: unknown }).taskOps;\n if (!Array.isArray(ops) || ops.length === 0) {\n throw new PlanParseError('\"taskOps\" must be a non-empty array', text);\n }\n for (const op of ops) {\n if (typeof op !== 'object' || op === null || typeof (op as { op?: unknown }).op !== 'string') {\n throw new PlanParseError('Every task op needs an \"op\" field (update | add | remove | reorder | merge | split)', text);\n }\n }\n return ops as TaskOp[];\n}\n\ntype RefResolution = { id: string } | { error: string };\n\n/**\n * Resolve a task reference (id, \"#3\", \"3\", or a batch handle) to a task id.\n *\n * `#order` and title matches are pinned to `originalScope` — the plan as it\n * stood before the batch began — so an earlier op's renumbering can never\n * make a later op's \"#4\" land on a different task than the planner saw. A\n * literal id has no such drift (ids are stable across mutations), so id\n * matches check `liveScope` — the batch's current, mutated tasks — which is\n * also how a later op reaches a task created earlier in the same batch\n * without a handle (e.g. a split's own chained parts).\n */\nfunction resolveRef(\n ref: unknown,\n originalScope: Task[],\n liveScope: Task[],\n handleOwner: Map<string, number>,\n handleId: Map<string, string>,\n opIndex: number,\n): RefResolution {\n if (typeof ref !== 'string' && typeof ref !== 'number') return { error: `invalid reference \"${String(ref)}\"` };\n const s = String(ref).trim();\n\n const ownerIdx = handleOwner.get(s);\n if (ownerIdx !== undefined) {\n if (ownerIdx >= opIndex) {\n return { error: `handle \"${s}\" is defined later in this batch (op ${ownerIdx + 1}) and cannot be referenced yet` };\n }\n const id = handleId.get(s);\n if (!id) return { error: `handle \"${s}\" was never resolved` };\n return { id };\n }\n\n const byLiveId = liveScope.find((t) => t.id === s);\n if (byLiveId) return { id: byLiveId.id };\n const orderStr = s.startsWith('#') ? s.slice(1) : s;\n if (/^\\d+$/.test(orderStr)) {\n const order = Number(orderStr);\n const byOrder = originalScope.filter((t) => t.order === order);\n if (byOrder.length === 1) return { id: byOrder[0].id };\n }\n const byTitle = originalScope.find((t) => t.title === s);\n if (byTitle) return { id: byTitle.id };\n return { error: `task \"${s}\" not found` };\n}\n\n/**\n * The task references an op carries, before a batch handle resolves; an `add`\n * names none. Used to tell whether a batch reaches a task a runner is running.\n */\nexport function taskOpRefs(op: TaskOp): string[] {\n switch (op.op) {\n case 'update':\n case 'remove':\n case 'split':\n case 'rearm':\n return [op.taskId];\n case 'merge':\n case 'reorder':\n return op.taskIds;\n case 'add':\n return [];\n }\n}\n\n/** Whether a written reference names `task` by id, `#order`, bare order, or title. */\nexport function refMatchesTask(ref: unknown, task: Pick<Task, 'id' | 'order' | 'title'>): boolean {\n if (typeof ref !== 'string' && typeof ref !== 'number') return false;\n const s = String(ref).trim();\n if (task.id === s) return true;\n const orderStr = s.startsWith('#') ? s.slice(1) : s;\n return (/^\\d+$/.test(orderStr) && task.order === Number(orderStr)) || task.title === s;\n}\n\n/** Whether `name` already denotes an existing task by id, `#order`, or title. */\nfunction refCollidesWithExisting(name: string, originalFlatAll: Task[]): boolean {\n if (originalFlatAll.some((t) => t.id === name)) return true;\n const orderStr = name.startsWith('#') ? name.slice(1) : name;\n if (/^\\d+$/.test(orderStr) && originalFlatAll.some((t) => t.order === Number(orderStr))) return true;\n return originalFlatAll.some((t) => t.title === name);\n}\n\nfunction findInTasks(tasks: Task[], id: string): Task | undefined {\n return flattenTasks(tasks).find((t) => t.id === id);\n}\n\nfunction findTopLevel(tasks: Task[], id: string): Task | undefined {\n return tasks.find((t) => t.id === id);\n}\n\nfunction detectCycle(tasks: Task[]): string | null {\n const byId = new Map(tasks.map((t) => [t.id, t]));\n const visiting = new Set<string>();\n const done = new Set<string>();\n const visit = (id: string): string | null => {\n if (done.has(id)) return null;\n if (visiting.has(id)) return id;\n visiting.add(id);\n for (const dep of byId.get(id)?.dependencies ?? []) {\n const hit = visit(dep);\n if (hit) return hit;\n }\n visiting.delete(id);\n done.add(id);\n return null;\n };\n for (const t of tasks) {\n const hit = visit(t.id);\n if (hit) return byId.get(hit)?.title ?? hit;\n }\n return null;\n}\n\n/** A task the repair may not reposition: execution has already reached it. */\nfunction isPinned(t: Task): boolean {\n return t.status === 'in_progress' || t.status === 'completed';\n}\n\n/**\n * Reorder top-level tasks so every dependency comes before its dependents,\n * moving as little as possible: pinned tasks keep the exact slot they are in,\n * and each free slot goes to the earliest-listed movable task whose\n * dependencies are already placed — so tasks the graph does not constrain keep\n * their relative order. Assumes an acyclic graph (the caller checks first).\n */\nfunction repairOrder(tasks: Task[]): { tasks: Task[] } | { error: string } {\n const byId = new Map(tasks.map((t) => [t.id, t]));\n const placed = new Set<string>();\n const movable = tasks.filter((t) => !isPinned(t));\n const result: Task[] = [];\n // Dependencies outside this list (a subtask id) constrain nothing here.\n const blockerOf = (t: Task): Task | undefined => {\n const id = t.dependencies.find((d) => byId.has(d) && !placed.has(d));\n return id ? byId.get(id) : undefined;\n };\n const place = (t: Task) => { result.push(t); placed.add(t.id); };\n\n for (const occupant of tasks) {\n if (isPinned(occupant)) {\n const blocker = blockerOf(occupant);\n if (blocker) return { error: pinnedRefusal(blocker, occupant, 'before') };\n place(occupant);\n continue;\n }\n const next = movable.findIndex((t) => !blockerOf(t));\n if (next < 0) {\n // Nothing is schedulable and the graph is acyclic, so every movable task\n // left waits — directly or through others — on a pinned task further down\n // that would have to come up to free this slot.\n const [stuck, pin] = trailToPin(movable[0], blockerOf);\n return { error: pinnedRefusal(stuck, pin, 'after') };\n }\n place(movable.splice(next, 1)[0]);\n }\n return { tasks: result };\n}\n\n/** Walk the waiting-on chain from `task` down to the pinned task holding it up. */\nfunction trailToPin(task: Task, blockerOf: (t: Task) => Task | undefined): [Task, Task] {\n let at = task;\n for (;;) {\n const blocker = blockerOf(at);\n if (!blocker || isPinned(blocker)) return [at, blocker ?? at];\n at = blocker;\n }\n}\n\n/** `\"B\" #2→#1, \"A\" #1→#3` for every task the repair actually moved, or null. */\nfunction describeMoves(before: Task[], after: Task[]): string | null {\n const wasAt = new Map(before.map((t, i) => [t.id, i + 1]));\n const moved = after\n .map((t, i) => ({ t, from: wasAt.get(t.id)!, to: i + 1 }))\n .filter((m) => m.from !== m.to);\n return moved.length ? moved.map((m) => `\"${m.t.title}\" #${m.from}→#${m.to}`).join(', ') : null;\n}\n\nfunction pinnedRefusal(other: Task, pin: Task, side: 'before' | 'after'): string {\n const word = pin.status === 'in_progress' ? 'running' : 'completed';\n return `\"${other.title}\" would have to run ${side} \"${pin.title}\", which is ${word} and cannot be moved`;\n}\n\nexport interface ApplyTaskOpsResult {\n ok: boolean;\n tasks: Task[];\n errors: string[];\n /** Human-readable summary of what changed, for the chat transcript. */\n summary: string[];\n}\n\n/**\n * Pre-flight check for a merge: would collapsing the selected tasks into one\n * (rewiring every dependent of any selected task to the survivor) keep the\n * dependency graph valid? Rejects locked tasks and merges whose rewiring would\n * introduce a cycle. Display order is NOT checked here — neither\n * consecutiveness (a UI concern) nor a dependency the merge pulls out of order,\n * which {@link applyTaskOps} repairs; refusing on order would make this\n * pre-flight stricter than the applier it stands in for.\n */\nexport function canMergeTasks(tasks: Task[], selectedIds: string[]): { ok: boolean; error?: string } {\n if (selectedIds.length < 2) return { ok: false, error: 'Select at least two tasks to merge' };\n const idSet = new Set(selectedIds);\n const toMerge = tasks.filter((t) => idSet.has(t.id));\n if (toMerge.length < 2) return { ok: false, error: 'At least two valid tasks are required to merge' };\n for (const t of toMerge) {\n if (t.status === 'in_progress') return { ok: false, error: `\"${t.title}\" is running and cannot be merged` };\n if (t.status === 'completed') return { ok: false, error: `\"${t.title}\" is completed and cannot be merged` };\n }\n const ordered = [...toMerge].sort((a, b) => a.order - b.order);\n const survivorId = '__merge_sim__';\n const sim: Task[] = tasks\n .filter((t) => !idSet.has(t.id))\n .map((t) => ({ ...t, dependencies: t.dependencies.map((d) => (idSet.has(d) ? survivorId : d)).filter((d) => d !== t.id) }));\n const unionDeps = [...new Set(toMerge.flatMap((t) => t.dependencies))].filter((d) => !idSet.has(d));\n sim.push({ ...ordered[0], id: survivorId, title: '__merge_sim__', order: ordered[0].order, dependencies: unionDeps });\n const cycle = detectCycle(sim);\n if (cycle) return { ok: false, error: `Merging would create a dependency cycle involving \"${cycle}\"` };\n return { ok: true };\n}\n\n/**\n * The shape the dependency helpers below read. Structural rather than `Task`,\n * because the TUI projects tasks into its own `TaskView`: a surface's\n * dependency picker and the API's validation must agree on which dependencies\n * are legal, and a signature only core can satisfy would have forced the TUI\n * to keep a second copy of the rule.\n */\nexport interface TaskRef {\n id: string;\n order: number;\n title: string;\n dependencies: string[];\n}\n\n/** The tasks listing `taskId` as a dependency — exactly what removing it detaches. */\nexport function dependentsOf<T extends Pick<TaskRef, 'id' | 'dependencies'>>(tasks: T[], taskId: string): T[] {\n return tasks.filter((t) => t.id !== taskId && t.dependencies.includes(taskId));\n}\n\n/**\n * The tasks that may become dependencies of `taskId`: those displayed before it.\n *\n * Offering only earlier tasks is what keeps a hand-edited graph valid without a\n * cycle check — dependencies then only ever point backwards in display order,\n * the same invariant `applyTaskOps` enforces. Omit `taskId` for a task that does\n * not exist yet: it lands last, so every current task is a candidate.\n */\nexport function dependencyCandidates<T extends Pick<TaskRef, 'id' | 'order'>>(tasks: T[], taskId?: string): T[] {\n if (!taskId) return [...tasks];\n const target = tasks.find((t) => t.id === taskId);\n return target ? tasks.filter((t) => t.order < target.order) : [];\n}\n\n/** Pre-flight check for a hand-edited dependency list: every id exists and comes earlier. */\nexport function canSetDependencies<T extends TaskRef>(\n tasks: T[],\n taskId: string,\n dependencies: string[],\n): { ok: boolean; error?: string } {\n const target = tasks.find((t) => t.id === taskId);\n if (!target) return { ok: false, error: 'Task not found' };\n const allowed = new Set(dependencyCandidates(tasks, taskId).map((t) => t.id));\n for (const dep of dependencies) {\n if (dep === taskId) return { ok: false, error: `\"${target.title}\" cannot depend on itself` };\n const found = tasks.find((t) => t.id === dep);\n if (!found) return { ok: false, error: `Unknown dependency \"${dep}\"` };\n if (!allowed.has(dep)) {\n return { ok: false, error: `\"${target.title}\" cannot depend on \"${found.title}\", which comes after it in the plan` };\n }\n }\n return { ok: true };\n}\n\n/** Pre-flight check for a split: the task exists and is not locked by execution. */\nexport function canSplitTask(tasks: Task[], taskId: string): { ok: boolean; error?: string } {\n const target = tasks.find((t) => t.id === taskId);\n if (!target) return { ok: false, error: 'Task not found' };\n if (target.status === 'in_progress') return { ok: false, error: `\"${target.title}\" is running and cannot be split` };\n if (target.status === 'completed') return { ok: false, error: `\"${target.title}\" is completed and cannot be split` };\n return { ok: true };\n}\n\n/**\n * Apply task ops to a snapshot of the plan, atomically: either every op\n * applies and the result validates (deps resolve, no cycles, running and\n * completed tasks untouched), or nothing is returned and `errors` explains\n * why. The caller commits `tasks` on ok.\n */\nexport function applyTaskOps(currentTasks: Task[], ops: TaskOp[], runners: RunnerId[], catalog?: EditCatalog): ApplyTaskOpsResult {\n let tasks: Task[] = JSON.parse(JSON.stringify(currentTasks));\n const errors: string[] = [];\n const summary: string[] = [];\n\n // Frozen snapshot every ref resolves `#order`/title matches against, so an\n // earlier op's renumbering can never shift what a later op's \"#N\" means.\n const originalFlatAll = flattenTasks(currentTasks);\n\n // Batch handles: planner-chosen names for a task an add/merge/split op is\n // about to create, so a later op in the same batch can reference it before\n // it has a real id. Pre-scanned up front — a bad handle refuses the whole\n // batch before anything mutates.\n const handleOwner = new Map<string, number>();\n const handleId = new Map<string, string>();\n for (const [i, op] of ops.entries()) {\n const handle = (op as { handle?: unknown }).handle;\n if (handle === undefined) continue;\n if (typeof handle !== 'string' || !handle.trim()) {\n errors.push(`op ${i + 1} (${op.op}): handle must be a non-empty string`);\n continue;\n }\n if (handleOwner.has(handle)) {\n errors.push(`op ${i + 1} (${op.op}): handle \"${handle}\" is already used earlier in this batch`);\n continue;\n }\n if (refCollidesWithExisting(handle, originalFlatAll)) {\n errors.push(`op ${i + 1} (${op.op}): handle \"${handle}\" collides with an existing task reference`);\n continue;\n }\n handleOwner.set(handle, i);\n }\n if (errors.length) return { ok: false, tasks: currentTasks, errors, summary: [] };\n\n const resolveDeps = (deps: unknown, opIndex: number): { ids: string[]; bad: string[] } => {\n const ids: string[] = [];\n const bad: string[] = [];\n const liveFlat = flattenTasks(tasks);\n for (const d of Array.isArray(deps) ? deps : []) {\n const r = resolveRef(d, originalFlatAll, liveFlat, handleOwner, handleId, opIndex);\n if ('error' in r) { bad.push(`${String(d)} (${r.error})`); continue; }\n if (!liveFlat.some((t) => t.id === r.id)) { bad.push(`${String(d)} (no longer exists in this batch)`); continue; }\n ids.push(r.id);\n }\n return { ids, bad };\n };\n\n for (const [i, op] of ops.entries()) {\n const label = `op ${i + 1} (${op.op})`;\n switch (op.op) {\n case 'update': {\n const ref = resolveRef(op.taskId, originalFlatAll, flattenTasks(tasks), handleOwner, handleId, i);\n if ('error' in ref) { errors.push(`${label}: ${ref.error}`); break; }\n const target = findInTasks(tasks, ref.id);\n if (!target) { errors.push(`${label}: task \"${op.taskId}\" no longer exists in this batch (removed, merged, or split by an earlier op)`); break; }\n const changes: Partial<Task> = {};\n for (const key of UPDATABLE_FIELDS) {\n if (op.changes && key in op.changes) (changes as Record<string, unknown>)[key] = (op.changes as Record<string, unknown>)[key];\n }\n if (changes.assignedRunner && !runners.includes(changes.assignedRunner)) {\n errors.push(`${label}: runner \"${changes.assignedRunner}\" is not in this plan's runner set [${runners.join(', ')}]`);\n break;\n }\n // Dependency refs are still unresolved (e.g. \"#3\") at this point, so\n // this check runs on everything but them; resolveDeps below handles\n // dependency well-formedness once they're real ids.\n const changesForCheck: Partial<Task> = { ...changes };\n delete changesForCheck.dependencies;\n const check = validateTaskEdit('planner', flattenTasks(tasks), target.id, changesForCheck, catalog);\n if (!check.ok) { errors.push(`${label}: ${check.error}`); break; }\n for (const field of check.clear ?? []) (changes as Record<string, unknown>)[field] = undefined;\n if ('dependencies' in changes) {\n const { ids, bad } = resolveDeps(changes.dependencies, i);\n if (bad.length) { errors.push(`${label}: unknown dependencies: ${bad.join(', ')}`); break; }\n changes.dependencies = ids.filter((id) => id !== target.id);\n }\n if (Object.keys(changes).length === 0) { errors.push(`${label}: no valid changes provided`); break; }\n tasks = updateTaskInPlan(tasks, target.id, changes);\n const clearedNote = check.clear?.length ? `; cleared ${check.clear.join(', ')}` : '';\n summary.push(`Updated \"${target.title}\" (${Object.keys(changes).join(', ')})${clearedNote}`);\n break;\n }\n case 'add': {\n const spec = op.task ?? {};\n if (!spec.title) { errors.push(`${label}: new task needs a title`); break; }\n const { ids, bad } = resolveDeps(spec.dependencies, i);\n if (bad.length) { errors.push(`${label}: unknown dependencies: ${bad.join(', ')}`); break; }\n const runner = spec.assignedRunner && runners.includes(spec.assignedRunner) ? spec.assignedRunner : runners[0];\n const validity = checkModelAndModeValidity(runner, spec.assignedModel, spec.taskMode, catalog);\n if (!validity.ok) { errors.push(`${label}: ${validity.error}`); break; }\n const newId = uuidv4();\n tasks = addTaskToPlan(tasks, {\n ...spec,\n id: newId,\n dependencies: ids,\n assignedRunner: runner,\n description: spec.description ?? spec.title,\n prompt: spec.prompt ?? spec.description ?? spec.title,\n });\n if (op.handle) handleId.set(op.handle, newId);\n summary.push(`Added \"${spec.title}\"`);\n break;\n }\n case 'remove': {\n const ref = resolveRef(op.taskId, originalFlatAll, flattenTasks(tasks), handleOwner, handleId, i);\n if ('error' in ref) { errors.push(`${label}: ${ref.error}`); break; }\n const target = findInTasks(tasks, ref.id);\n if (!target) { errors.push(`${label}: task \"${op.taskId}\" no longer exists in this batch (removed, merged, or split by an earlier op)`); break; }\n if (target.status === 'in_progress') { errors.push(`${label}: \"${target.title}\" is running and cannot be removed`); break; }\n if (target.status === 'completed') { errors.push(`${label}: \"${target.title}\" is completed and cannot be removed`); break; }\n tasks = removeTaskFromPlan(tasks, target.id);\n summary.push(`Removed \"${target.title}\"`);\n break;\n }\n case 'reorder': {\n const refs = (op.taskIds ?? []).map((r) => resolveRef(r, currentTasks, tasks, handleOwner, handleId, i));\n const badRef = refs.find((r): r is { error: string } => 'error' in r);\n if (badRef) { errors.push(`${label}: ${badRef.error}`); break; }\n const resolved = (refs as { id: string }[]).map((r) => findTopLevel(tasks, r.id));\n if (resolved.some((t) => !t)) { errors.push(`${label}: unknown task in reorder list`); break; }\n const orderIds = resolved.map((t) => t!.id);\n if (new Set(orderIds).size !== tasks.length) {\n errors.push(`${label}: reorder must list every top-level task exactly once (${tasks.length} tasks)`);\n break;\n }\n const byId = new Map(tasks.map((t) => [t.id, t]));\n tasks = renumberTasks(orderIds.map((id) => byId.get(id)!));\n summary.push('Reordered tasks');\n break;\n }\n case 'merge': {\n const refs = (op.taskIds ?? []).map((r) => resolveRef(r, currentTasks, tasks, handleOwner, handleId, i));\n const badRef = refs.find((r): r is { error: string } => 'error' in r);\n if (badRef) { errors.push(`${label}: ${badRef.error}`); break; }\n const resolved = (refs as { id: string }[]).map((r) => findTopLevel(tasks, r.id));\n if (resolved.some((t) => !t)) { errors.push(`${label}: unknown task in merge list (already removed, merged, or split by an earlier op)`); break; }\n const toMerge = resolved.map((t) => t!).sort((a, b) => a.order - b.order);\n if (toMerge.length < 2) { errors.push(`${label}: merge needs at least two tasks`); break; }\n const locked = toMerge.find((t) => t.status === 'in_progress' || t.status === 'completed');\n if (locked) {\n errors.push(`${label}: \"${locked.title}\" is ${locked.status === 'in_progress' ? 'running' : 'completed'} and cannot be merged`);\n break;\n }\n const idSet = new Set(toMerge.map((t) => t.id));\n const unionDepsRaw = [...new Set(toMerge.flatMap((t) => t.dependencies))].filter((d) => !idSet.has(d));\n const { ids, bad } = resolveDeps(unionDepsRaw, i);\n if (bad.length) { errors.push(`${label}: unknown dependencies: ${bad.join(', ')}`); break; }\n const spec = op.merged ?? {};\n if (!spec.title) { errors.push(`${label}: merged task needs a title`); break; }\n const runner = spec.assignedRunner && runners.includes(spec.assignedRunner) ? spec.assignedRunner : toMerge[0].assignedRunner;\n const mergedTask = createTask({\n title: spec.title,\n description: spec.description ?? toMerge.map((t) => t.description).filter(Boolean).join('\\n\\n'),\n prompt: spec.prompt ?? (toMerge.map((t) => t.prompt).filter(Boolean).join('\\n\\n---\\n\\n') || undefined),\n type: spec.type ?? (toMerge.some((t) => t.type === 'user') ? 'user' : 'ai'),\n dependencies: ids,\n assignedRunner: runner,\n assignedModel: spec.assignedModel ?? toMerge.find((t) => t.assignedModel)?.assignedModel,\n thinkingEffort: spec.thinkingEffort ?? toMerge.find((t) => t.thinkingEffort)?.thinkingEffort,\n taskMode: spec.taskMode ?? toMerge[0].taskMode,\n autonomy: spec.autonomy ?? toMerge.find((t) => t.autonomy)?.autonomy,\n sliceType: spec.sliceType ?? toMerge.find((t) => t.sliceType)?.sliceType,\n userStoriesCovered: spec.userStoriesCovered ?? (toMerge.flatMap((t) => t.userStoriesCovered ?? []).length\n ? [...new Set(toMerge.flatMap((t) => t.userStoriesCovered ?? []))]\n : undefined),\n });\n const mergedId = mergedTask.id;\n const firstId = toMerge[0].id;\n const result: Task[] = [];\n let inserted = false;\n for (const t of tasks) {\n if (idSet.has(t.id)) {\n if (!inserted && t.id === firstId) { result.push(mergedTask); inserted = true; }\n continue;\n }\n result.push({ ...t, dependencies: t.dependencies.map((d) => (idSet.has(d) ? mergedId : d)) });\n }\n if (!inserted) result.push(mergedTask);\n tasks = renumberTasks(result);\n if (op.handle) handleId.set(op.handle, mergedId);\n summary.push(`Merged ${toMerge.length} tasks into \"${spec.title}\"`);\n break;\n }\n case 'split': {\n const ref = resolveRef(op.taskId, currentTasks, tasks, handleOwner, handleId, i);\n if ('error' in ref) { errors.push(`${label}: ${ref.error}`); break; }\n const target = findTopLevel(tasks, ref.id);\n if (!target) { errors.push(`${label}: task \"${op.taskId}\" not found`); break; }\n if (target.status === 'in_progress') { errors.push(`${label}: \"${target.title}\" is running and cannot be split`); break; }\n if (target.status === 'completed') { errors.push(`${label}: \"${target.title}\" is completed and cannot be split`); break; }\n const parts = op.parts ?? [];\n if (parts.length < 2) { errors.push(`${label}: split needs at least two parts`); break; }\n const newTasks: Task[] = [];\n let prevId: string | undefined;\n let buildError = false;\n for (let p = 0; p < parts.length; p++) {\n const spec = parts[p];\n if (!spec || !spec.title) { errors.push(`${label}: split part ${p + 1} needs a title`); buildError = true; break; }\n // Part 0 inherits the original's (external) dependencies, resolved\n // against the current plan. Later parts chain onto the previous part\n // — a sibling id created in this same op, so it needs no resolution.\n let deps: string[];\n if (p === 0) {\n const { ids, bad } = resolveDeps(target.dependencies, i);\n if (bad.length) { errors.push(`${label}: split part 1 unknown dependencies: ${bad.join(', ')}`); buildError = true; break; }\n deps = ids;\n } else {\n deps = prevId ? [prevId] : [];\n }\n const runner = spec.assignedRunner && runners.includes(spec.assignedRunner) ? spec.assignedRunner : target.assignedRunner;\n const nt = createTask({\n title: spec.title,\n description: spec.description ?? spec.title,\n prompt: spec.prompt ?? spec.description ?? spec.title,\n type: spec.type ?? target.type,\n dependencies: deps,\n assignedRunner: runner,\n assignedModel: spec.assignedModel ?? target.assignedModel,\n taskMode: spec.taskMode ?? target.taskMode,\n autonomy: spec.autonomy ?? target.autonomy,\n sliceType: spec.sliceType ?? target.sliceType,\n userStoriesCovered: spec.userStoriesCovered ?? target.userStoriesCovered,\n });\n newTasks.push(nt);\n prevId = nt.id;\n }\n if (buildError) break;\n const tailId = newTasks[newTasks.length - 1].id;\n const result: Task[] = [];\n for (const t of tasks) {\n if (t.id === target.id) { result.push(...newTasks); continue; }\n result.push({ ...t, dependencies: t.dependencies.map((d) => (d === target.id ? tailId : d)) });\n }\n tasks = renumberTasks(result);\n if (op.handle) handleId.set(op.handle, tailId);\n summary.push(`Split \"${target.title}\" into ${newTasks.length} tasks`);\n break;\n }\n case 'rearm': {\n const ref = resolveRef(op.taskId, originalFlatAll, flattenTasks(tasks), handleOwner, handleId, i);\n if ('error' in ref) { errors.push(`${label}: ${ref.error}`); break; }\n const target = findInTasks(tasks, ref.id);\n if (!target) { errors.push(`${label}: task \"${op.taskId}\" no longer exists in this batch (removed, merged, or split by an earlier op)`); break; }\n if (target.status === 'in_progress') { errors.push(`${label}: \"${target.title}\" is running and cannot be re-armed`); break; }\n const changes: Partial<Task> = {};\n for (const key of UPDATABLE_FIELDS) {\n if (op.changes && key in op.changes) (changes as Record<string, unknown>)[key] = (op.changes as Record<string, unknown>)[key];\n }\n if (changes.assignedRunner && !runners.includes(changes.assignedRunner)) {\n errors.push(`${label}: runner \"${changes.assignedRunner}\" is not in this plan's runner set [${runners.join(', ')}]`);\n break;\n }\n // Validated as a direct edit (not 'planner'): re-arming is the sanctioned\n // exception to the planner lock — its whole point is to touch a task the\n // lock would otherwise refuse. Well-formedness (type coherence, model/mode\n // validity, dependency shape) still applies, same as every other edit.\n const changesForCheck: Partial<Task> = { ...changes };\n delete changesForCheck.dependencies;\n const check = validateTaskEdit('direct', flattenTasks(tasks), target.id, changesForCheck, catalog);\n if (!check.ok) { errors.push(`${label}: ${check.error}`); break; }\n for (const field of check.clear ?? []) (changes as Record<string, unknown>)[field] = undefined;\n if ('dependencies' in changes) {\n const { ids, bad } = resolveDeps(changes.dependencies, i);\n if (bad.length) { errors.push(`${label}: unknown dependencies: ${bad.join(', ')}`); break; }\n changes.dependencies = ids.filter((id) => id !== target.id);\n }\n tasks = updateTaskInPlan(tasks, target.id, { ...changes, status: 'pending', verdict: undefined, outputSummary: undefined });\n // Dependents parked at 'blocked' by this task's earlier failure have\n // nothing else to release them — the scheduler reads `status` directly,\n // not a live recomputation of the dependency graph.\n for (const dep of flattenTasks(tasks)) {\n if (dep.status === 'blocked' && dep.dependencies.includes(target.id)) {\n tasks = updateTaskInPlan(tasks, dep.id, { status: 'pending' });\n }\n }\n const clearedNote = check.clear?.length ? `; cleared ${check.clear.join(', ')}` : '';\n const changedNote = Object.keys(changes).length ? ` (${Object.keys(changes).join(', ')})` : '';\n summary.push(`Re-armed \"${target.title}\"${changedNote}${clearedNote}`);\n break;\n }\n default:\n errors.push(`${label}: unknown op \"${(op as { op: string }).op}\"`);\n }\n if (errors.length) break;\n }\n\n if (!errors.length) {\n const flat = flattenTasks(tasks);\n const idSet = new Set(flat.map((t) => t.id));\n for (const t of flat) {\n for (const dep of t.dependencies) {\n if (!idSet.has(dep)) errors.push(`\"${t.title}\" depends on a task that no longer exists (${dep})`);\n }\n }\n const cycleAt = detectCycle(flat);\n if (cycleAt) errors.push(`Dependency cycle detected involving \"${cycleAt}\"`);\n // Display order is maintained, not demanded: whatever the batch produced,\n // the plan comes back with dependencies ahead of their dependents. The\n // planner never has to re-declare the whole order to move one task.\n if (!errors.length) {\n const repaired = repairOrder(tasks);\n if ('error' in repaired) {\n errors.push(repaired.error);\n } else {\n const moves = describeMoves(tasks, repaired.tasks);\n if (moves) summary.push(`Reordered to keep dependencies first: ${moves}`);\n tasks = renumberTasks(repaired.tasks);\n }\n }\n }\n\n return errors.length\n ? { ok: false, tasks: currentTasks, errors, summary: [] }\n : { ok: true, tasks, errors: [], summary };\n}\n","import type { ResearchStepOutcome } from '../models/Task';\n\n/**\n * One-line human summary of a research tool call, shared by every surface that\n * renders live planner progress (VS Code webview, CLI, TUI). Kept in core so\n * the arg-shape heuristics (file path vs. pattern vs. shell command) live in\n * one place instead of being re-derived per surface — which also means a new\n * tool becomes readable in all three UIs by being handled here once.\n */\n\nfunction ellipsize(text: string, max: number): string {\n const oneLine = text.replace(/\\s+/g, ' ').trim();\n return oneLine.length > max ? `${oneLine.slice(0, max)}…` : oneLine;\n}\n\n/**\n * @param toolLabel The agent's own name for the tool, when a harness planner\n * produced the call (ADR-0009). It replaces the member name in the summary, so\n * the timeline says `Edit` rather than the catch-all `agent_tool` — and says\n * `WebFetch` rather than claiming a shell command ran.\n */\nexport function summarizeToolCall(tool: string, argsJson: string, toolLabel?: string): string {\n const display = toolLabel?.trim() || tool;\n if (tool === 'agent_tool') {\n // No Ordewell equivalent, so no arg shape to rely on: show the agent's name\n // plus whichever conventional field the call happens to carry.\n try {\n const args = JSON.parse(argsJson);\n const hint = args.path ?? args.file_path ?? args.filePath ?? args.pattern ?? args.query ?? args.url ?? args.command;\n if (hint) return `${display} ${ellipsize(String(hint), 50)}`;\n } catch {\n // fall through to the bare name\n }\n return display;\n }\n\n if (tool === 'spawn_research_agent') {\n try {\n const args = JSON.parse(argsJson);\n const prompt = typeof args.prompt === 'string' ? args.prompt : '';\n const oneLine = prompt.replace(/\\s+/g, ' ').trim();\n return `spawn: \"${oneLine.slice(0, 60)}${oneLine.length > 60 ? '…' : ''}\"`;\n } catch {\n return 'spawn_research_agent';\n }\n }\n\n try {\n const args = JSON.parse(argsJson);\n\n // Ordered by specificity: a tool's defining argument wins over the generic\n // `path`, which several of the newer tools also accept as a search root.\n if (tool === 'find_symbol' && args.symbol) {\n return `find_symbol ${args.symbol}${args.language ? ` [${args.language}]` : ''}`;\n }\n if (tool === 'web_search' && args.query) {\n return `web_search \"${ellipsize(String(args.query), 50)}\"`;\n }\n if (tool === 'fetch' && args.url) {\n return `fetch ${ellipsize(String(args.url), 60)}`;\n }\n // The pattern is what a glob is about; the generic `path` below is only its\n // search root, and reporting that read as \"glob src\" for every call.\n if (tool === 'glob' && args.pattern) {\n return `glob ${ellipsize(String(args.pattern), 50)}`;\n }\n if (tool === 'grep' && args.pattern) {\n const mode = args.output_mode && args.output_mode !== 'content' ? ` (${args.output_mode})` : '';\n const scope = args.include ? ` in ${args.include}` : '';\n return `grep ${ellipsize(String(args.pattern), 40)}${scope}${mode}`;\n }\n\n const path = args.filePath || args.file_path || args.path;\n if (path) {\n const basename = String(path).split('/').pop() || String(path);\n return `${display} ${basename}`;\n }\n if (args.pattern) {\n return `${display} ${args.pattern}`;\n }\n if (args.command) {\n return `bash: ${String(args.command).slice(0, 40)}`;\n }\n } catch {\n // fall through to the bare tool name below\n }\n return display;\n}\n\n/**\n * Classify a tool outcome from its result text and success flag, so a surface\n * can render a refused `rm` or a denied `npm test` distinctly from a\n * successful run without re-deriving the refusal signatures per surface. The\n * refusal/denial strings come from `commandPolicy.ts`, `BaseFileSystem.ts`,\n * and the research-subagent wrapper — keeping the matching here means the\n * signatures stay in one place alongside the human summary.\n */\nexport function classifyOutcome(success: boolean, output: string): ResearchStepOutcome {\n if (!success) {\n if (/^Command refused:/.test(output) || /is not available\\. You are a read-only research subagent/.test(output)) return 'refused';\n if (/not approved/i.test(output) || /^Access denied:/.test(output) || /needs user approval/i.test(output)) return 'denied';\n return 'failure';\n }\n return 'success';\n}\n","import type { LegacyPlanState, QueuedMessage, ResearchStep, RunnerId, SubagentOutcome, Task, Verdict } from '../models/Task';\nimport type { UsageTotals } from '../models/Usage';\nimport type { ApprovalKind } from '../interfaces/IApproval';\nimport type { ApprovalSource } from './ApprovalPolicy';\nimport type { IsolationHandoff, IsolationMergeResult, TaskIsolation } from '../interfaces/IWorktreeIsolation';\n\nexport type SerializedTaskStatus = {\n id: string;\n status: string;\n verdict: { outcome: 'pass' | 'fail'; reason: string; checks: Verdict['checks'] } | null;\n /** Advisory silence timestamp from VerdictEngine — not part of task status semantics. */\n idleSince?: string | null;\n /** Absent unless the plan has an isolation run, so a shared-root plan's updates are unchanged. */\n isolation?: TaskIsolation;\n};\n\nexport type SerializedTask = {\n id: string;\n order: number;\n title: string;\n type: string;\n description: string;\n dependencies: string[];\n assignedRunner: RunnerId;\n assignedModel: Task['assignedModel'] | null;\n taskMode: string;\n prompt: string | null;\n subtasks: SerializedTask[];\n userSteps: Task['userSteps'];\n thinkingEffort: Task['thinkingEffort'];\n autonomy: Task['autonomy'];\n sliceType: Task['sliceType'];\n userStoriesCovered: Task['userStoriesCovered'];\n};\n\nexport type SerializedPlan = {\n tasks: SerializedTask[];\n runners: RunnerId[];\n generatedAt: string;\n conversationHistory?: LegacyPlanState['conversationHistory'];\n prdMarkdown?: string;\n queuedMessages?: QueuedMessage[];\n};\n\n/** How a planner turn ended: the reply kind it settled on, a user stop, or a failure. */\nexport type PlannerTurnOutcome = 'message' | 'plan' | 'task_ops' | 'stopped' | 'error';\n\n/**\n * Everything a session tells its surfaces, over one broadcast seam.\n *\n * A planner turn (#47) streams between `planner_turn_started` and\n * `planner_turn_ended` with the same `turnId`; every turn-scoped message in\n * between carries it. The stream is provisional and the settled messages are\n * authoritative:\n * - `planner_message` replaces the streamed text of the turn's final segment —\n * a surface drops what it accumulated and shows the message instead.\n * - A reply that is a JSON envelope (plan, taskOps, taskQuery) never arrives\n * as `planner_text_delta`; it streams as `plan_token`, the \"building plan\"\n * display.\n * - A subagent's own text never appears in the reply; its activity arrives\n * tagged with its `subagentId`.\n */\nexport type SessionMessage =\n /**\n * The plan, whole. `turnId` names the planner turn whose commit this\n * broadcast carries, so the turn's building plan becomes the marker; it is\n * absent on every other broadcast of the plan.\n */\n | { type: 'plan_generated'; plan: SerializedPlan; goal: string; runners: RunnerId[]; turnId?: string }\n /**\n * The settled reply of a planner turn, emitted by the session once the turn\n * is classified. Authoritative over any `planner_text_delta` of its turn's\n * final segment. `turnId` is absent for replies sent outside a streamed turn.\n */\n | { type: 'planner_message'; content: string; timestamp: string; turnId?: string }\n /**\n * A planner turn began. Emitted once per turn by whoever runs the turn,\n * before any other message carrying its `turnId`. `prompt` is the user's\n * message when the turn answers one.\n */\n | { type: 'planner_turn_started'; turnId: string; prompt?: string }\n /**\n * A planner turn is over; nothing more carries its `turnId`. Emitted exactly\n * once per `planner_turn_started`, stop and failure included, after the\n * turn's `planner_message` when it has one.\n */\n | { type: 'planner_turn_ended'; turnId: string; outcome: PlannerTurnOutcome }\n /**\n * Reply prose as it streams, appended in order within its segment. A segment\n * is one continuous run of model text; text before a tool call is its own\n * segment, and a later segment never rewrites an earlier one. Never carries\n * a JSON envelope or a subagent's text (see the union's invariants).\n */\n | { type: 'planner_text_delta'; turnId: string; segmentId: string; text: string }\n /**\n * Exposed reasoning as it streams, from the planner or — tagged with\n * `subagentId` — from one of its subagents. Never part of the reply. The one\n * thinking message for every backend: `segmentId` is set only where the\n * backend streams thinking in segments (the API loops; harness planners do\n * not), and `turnId` is absent for thinking outside a turn (one-shot plans).\n */\n | { type: 'planner_thinking_delta'; turnId?: string; segmentId?: string; subagentId?: string; text: string }\n /**\n * Text streamed for an attempt the turn discarded (a corrective retry) is\n * taken back: a surface removes it. With `segmentId`, only that segment;\n * without, all of the turn's text not yet settled by a `planner_message`.\n * A segment that streamed to the plan display (`plan_token`) takes the\n * turn's building plan with it.\n */\n | { type: 'planner_text_retracted'; turnId: string; segmentId?: string }\n /**\n * The planner's running usage for the session, subagents included, emitted\n * after a model call reports usage. `totals` already contains every\n * `bySubagent` entry. `contextFill` is the last planner prompt against the\n * model's window, omitted when the window is unknown. Cost appears only as\n * reported by a provider or runner (see `UsageRecord`).\n */\n | {\n type: 'planner_usage';\n turnId?: string;\n totals: UsageTotals;\n bySubagent?: Record<string, UsageTotals>;\n contextFill?: { usedTokens: number; windowTokens: number };\n }\n /**\n * A subagent began work on `brief`. Emitted by the planner backend that\n * spawned it (ADR-0005 research agents, or a harness planner's own), before\n * any message tagged with its `subagentId`.\n */\n | { type: 'subagent_started'; turnId?: string; subagentId: string; brief: string; model?: string }\n /**\n * A subagent is done; nothing more is tagged with its `subagentId`. Emitted\n * once per `subagent_started`. `digest` is what it handed back to the\n * planner; `usage` is its own share, already counted in `planner_usage`.\n */\n | { type: 'subagent_finished'; turnId?: string; subagentId: string; outcome: SubagentOutcome; digest: string; usage?: UsageTotals }\n | { type: 'status_update'; tasks: SerializedTaskStatus[] }\n | { type: 'review_needed'; tasks: SerializedTask[] }\n | { type: 'review_approved' }\n | { type: 'checkpoint'; taskId: string; taskTitle: string; summary: string }\n | { type: 'execution_complete'; summary: { total: number; completed: number; failed: number } }\n | { type: 'execution_stopped' }\n | { type: 'queue_ready' }\n | { type: 'task_updated'; taskId: string; changes: Record<string, unknown> }\n | { type: 'task_started'; taskId: string; order: number; title: string; runner: RunnerId; modelId?: string }\n | { type: 'task_output'; taskId: string; text: string }\n // A run did not start because tracked files are modified. It waits for the\n // user to stash (`continueWithStash`) or to run without isolation this once\n // (`continueWithoutIsolation`); nothing is spawned until then. `repos` names\n // the dirty repos of a group; a group of one names none.\n | { type: 'isolation_blocked'; reason: 'dirty'; repos?: string[]; message: string }\n // An isolated run settled: for each repo, the branch its work landed on and\n // the commit that branch forked from; and what landed, in plan order. Sent\n // before `execution_complete`, which surfaces treat as the end of the stream.\n | { type: 'isolation_handoff'; repos: IsolationHandoff['repos']; landed: IsolationHandoff['landed'] }\n // What \"Merge all\" did, for every surface watching rather than only the one\n // that asked: merged, blocked with each repo and why, or stopped part-way\n // with the repos that stay merged.\n | { type: 'isolation_merge'; result: IsolationMergeResult }\n // Carries no content — see `ResearchProgress['liveness']`. Exists only so a\n // surface's idle watchdog sees the harness process working even during a\n // stretch that produces nothing visible.\n | { type: 'planner_liveness' }\n // `toolLabel` carries a harness planner's own name for the tool (ADR-0009) —\n // always set when `tool` is `agent_tool`, so no surface has to render the\n // catch-all member name at the user.\n | { type: 'research_step'; tool: string; toolLabel?: string; args: string; subagentId?: string; toolCallId?: string; turnId?: string }\n // The \"building plan\" display only: a JSON envelope as it streams, never\n // reply prose. `turnId` is absent for a one-shot plan, which has no turn.\n // `segmentId` names the segment the envelope streams in; a turn's next\n // envelope is a new segment, and the display starts over with it.\n | { type: 'plan_token'; token: string; turnId?: string; segmentId?: string }\n | { type: 'research_step_done'; step: ResearchStep; subagentId?: string; turnId?: string }\n // Planner research wants something outside its default envelope and is\n // blocked until a human answers. Broadcast rather than returned, because the\n // human may be on any surface (or several at once) and the request outlives\n // whichever HTTP call triggered it.\n | { type: 'approval_request'; id: string; kind: ApprovalKind; subject: string; scope: string; detail?: string; turnId?: string }\n | { type: 'approval_settled'; id: string; granted: boolean }\n // A decision reached with no round-trip prompt: pre-approved via config,\n // remembered from earlier in this session, or the operator's mode floor\n // (allow/deny skipping the human entirely). The interactive path above\n // already has full visibility via approval_request/approval_settled; this\n // is the one that previously had none — a model silently auto-running\n // `npm test` because the scope was already granted looked identical, from\n // every UI, to it never having been asked in the first place.\n | {\n type: 'approval_decided';\n kind: ApprovalKind;\n subject: string;\n scope: string;\n detail?: string;\n granted: boolean;\n source: Exclude<ApprovalSource, 'asked'>;\n };\n\n/**\n * A line a run wants the user to read — how it isolates, what it shares. Not a\n * {@link SessionMessage}: hosts that show notices as toasts already do, and\n * widening the union would break every exhaustive switch over it. A host with\n * no toast channel (the daemon) hands these to its clients by its own means.\n */\nexport type SessionNotice = { type: 'notice'; level: 'info' | 'warn' | 'error'; message: string };\n\nexport type SessionBroadcaster = (msg: SessionMessage) => void;\n\nexport function serializeTask(t: Task): SerializedTask {\n return {\n id: t.id,\n order: t.order,\n title: t.title,\n type: t.type,\n description: t.description,\n dependencies: t.dependencies || [],\n assignedRunner: t.assignedRunner,\n assignedModel: t.assignedModel || null,\n taskMode: t.taskMode || 'build',\n prompt: t.prompt || null,\n subtasks: (t.subtasks || []).map(serializeTask),\n userSteps: t.userSteps || undefined,\n thinkingEffort: t.thinkingEffort || undefined,\n autonomy: t.autonomy || undefined,\n sliceType: t.sliceType || undefined,\n userStoriesCovered: t.userStoriesCovered || undefined,\n };\n}\n\nexport function serializeTaskStatus(t: Task, idleSince: string | null = null, isolation: TaskIsolation | null = null): SerializedTaskStatus {\n return {\n id: t.id,\n status: t.status,\n verdict: t.verdict\n ? { outcome: t.verdict.outcome, reason: t.verdict.reason, checks: t.verdict.checks || [] }\n : null,\n idleSince,\n ...(isolation ? { isolation } : {}),\n };\n}\n\nexport function serializePlan(plan: LegacyPlanState): SerializedPlan {\n return {\n tasks: plan.tasks.map(serializeTask),\n runners: plan.runners,\n generatedAt: plan.generatedAt,\n conversationHistory: plan.conversationHistory,\n prdMarkdown: plan.prdMarkdown,\n queuedMessages: plan.queuedMessages,\n };\n}\n\nexport function executionSummary(tasks: Task[]): { total: number; completed: number; failed: number } {\n return {\n total: tasks.length,\n completed: tasks.filter((t) => t.status === 'completed').length,\n failed: tasks.filter((t) => t.status === 'failed').length,\n };\n}\n\nexport const CHECKPOINT_TRUNCATE_LENGTH = 120;\n\n/** Truncate a checkpoint summary to a single line of at most CHECKPOINT_TRUNCATE_LENGTH chars. */\nexport function truncateCheckpointSummary(summary: string): string {\n // Take the first line only (checkpoint summaries often contain reasoning across lines)\n const firstLine = summary.split('\\n')[0].trim();\n if (firstLine.length <= CHECKPOINT_TRUNCATE_LENGTH) return firstLine;\n return firstLine.slice(0, CHECKPOINT_TRUNCATE_LENGTH - 1) + '…';\n}\n","import * as path from 'path';\nimport type {\n IsolationHandoff,\n IsolationLandedTask,\n IsolationRepo,\n IsolationRun,\n IsolationTaskRecord,\n IsolationTaskStatus,\n PlanIsolation,\n RepoGroupLayout,\n TaskIsolation,\n} from '../interfaces/IWorktreeIsolation';\n\n/** The path of the repo in a workspace that is itself the repository. */\nexport const SELF_REPO = '.';\n\nexport function integrationBranchFor(runId: string): string {\n return `ordewell/${runId}/integration`;\n}\n\nexport function repoRootOf(workspaceRoot: string, repoPath: string): string {\n return path.join(workspaceRoot, repoPath);\n}\n\n/** The group a run isolates, as the planner is told it. */\nexport function layoutOf(run: IsolationRun): RepoGroupLayout {\n return { repos: run.repos.map((r) => r.path), shared: [...run.shared] };\n}\n\n/** The run's integration branch for a one-line notice: every repo's has the same name. */\nexport function integrationBranchNameOf(run: IsolationRun): string {\n return [...new Set(run.repos.map((r) => r.integrationBranch))].join(', ');\n}\n\nconst ISOLATION_STATE: Record<IsolationTaskStatus, Exclude<TaskIsolation['state'], 'none'>> = {\n active: 'active',\n merged: 'integrated',\n conflict: 'conflict',\n repairing: 'repairing',\n kept: 'kept',\n failed: 'kept',\n};\n\n/** `repairLimit` is `conflictRepairAttempts` as it stands now, which is what the next repair will be held to. */\nexport function taskIsolationOf(record: IsolationTaskRecord, repairLimit: number): TaskIsolation {\n return {\n state: ISOLATION_STATE[record.status],\n branch: record.branch,\n worktree: record.workspace,\n repos: Object.entries(record.repos).filter(([, r]) => r.changed).map(([repoPath]) => repoPath),\n ...(record.conflictRepo ? { conflictRepo: record.conflictRepo } : {}),\n ...(record.conflictFiles?.length ? { conflictFiles: record.conflictFiles } : {}),\n ...(record.repairs ? { repair: { attempt: record.repairs, limit: repairLimit } } : {}),\n ...(record.repairedFiles?.length ? { repairedFiles: record.repairedFiles } : {}),\n };\n}\n\n/** A conflict's files as one surface shows them: every one, up to `max`, then how many more. */\nexport function capConflictFiles(files: string[], max = 5): string {\n return files.length <= max ? files.join(', ') : `${files.slice(0, max).join(', ')}, +${files.length - max} more`;\n}\n\n/** What a run hands over: each repo's integration branch and base, and what landed, in plan order. */\nexport function handoffOf(run: IsolationRun): IsolationHandoff {\n const merged = Object.values(run.tasks)\n .filter((r) => r.status === 'merged')\n .sort((a, b) => a.order - b.order);\n const entry = (r: IsolationTaskRecord): IsolationLandedTask => ({\n taskId: r.taskId, order: r.order, title: r.title,\n ...(r.repairedFiles?.length ? { repairedFiles: r.repairedFiles } : {}),\n });\n return {\n repos: run.repos.map((repo) => ({\n path: repo.path,\n integrationBranch: repo.integrationBranch,\n baseRef: repo.baseRef,\n landed: merged.filter((r) => r.repos[repo.path]?.changed).map(entry),\n })),\n landed: merged.map(entry),\n };\n}\n\n/** A task record as ADR-0013 persisted it, for one repository. */\nexport interface Adr0013TaskRecord {\n taskId: string;\n order: number;\n title: string;\n branch: string;\n worktree: string;\n status: IsolationTaskStatus;\n linked: string[];\n}\n\n/** A run as ADR-0013 persisted it (0.4.23): one repository, its refs on the run itself. */\nexport interface Adr0013IsolationRun {\n id: string;\n workspaceRoot: string;\n baseRef: string;\n baseBranch?: string;\n integrationBranch: string;\n tasks: Record<string, Adr0013TaskRecord>;\n}\n\nexport interface Adr0013PlanIsolation {\n run: Adr0013IsolationRun;\n resolvers: Record<string, string>;\n}\n\nfunction isAdr0013(run: IsolationRun | Adr0013IsolationRun): run is Adr0013IsolationRun {\n return 'integrationBranch' in run && typeof run.integrationBranch === 'string';\n}\n\nfunction fromAdr0013Task(r: Adr0013TaskRecord): IsolationTaskRecord {\n return {\n taskId: r.taskId,\n order: r.order,\n title: r.title,\n branch: r.branch,\n workspace: r.worktree,\n status: r.status,\n // A merged task's branch was merged into the one repo there was; what it\n // changed was never recorded, and landing is what `changed` stands for.\n repos: { [SELF_REPO]: { worktree: r.worktree, linked: r.linked ?? [], ...(r.status === 'merged' ? { changed: true } : {}) } },\n ...(r.status === 'conflict' ? { conflictRepo: SELF_REPO } : {}),\n };\n}\n\n/**\n * A persisted plan isolation in today's shape. A run saved in the ADR-0013\n * format — recognised by its own `integrationBranch` — becomes a group of one\n * at `.`, keeping its branches, so a session saved by 0.4.23 resumes and hands\n * off exactly as it would have.\n */\nexport function migratePlanIsolation(state: PlanIsolation | Adr0013PlanIsolation): PlanIsolation {\n const { run, resolvers } = state;\n if (!isAdr0013(run)) return { run, resolvers: resolvers ?? {} };\n const repo: IsolationRepo = {\n path: SELF_REPO,\n root: repoRootOf(run.workspaceRoot, SELF_REPO),\n baseRef: run.baseRef,\n ...(run.baseBranch ? { baseBranch: run.baseBranch } : {}),\n integrationBranch: run.integrationBranch,\n };\n const tasks = Object.fromEntries(Object.entries(run.tasks ?? {}).map(([id, r]) => [id, fromAdr0013Task(r)]));\n return { run: { id: run.id, workspaceRoot: run.workspaceRoot, repos: [repo], shared: [], sharedRepos: [], tasks }, resolvers: resolvers ?? {} };\n}\n\n/** Bring a loaded plan's isolation record, if any, to today's shape in place. */\nexport function migratePlanStateIsolation(plan: { isolation?: PlanIsolation | Adr0013PlanIsolation }): void {\n if (plan.isolation?.run) plan.isolation = migratePlanIsolation(plan.isolation);\n}\n","/**\n * What one model call consumed, as its provider or runner reported it. Shared\n * by the planner's usage line (#49) and per-attempt task usage (#26): a record\n * says nothing about who asked for the call, so either can hold a list of them.\n *\n * Every measure is optional because backends report different subsets. An\n * absent field means \"not reported\" — never zero — so a total can tell the two\n * apart.\n */\nexport interface UsageRecord {\n /** The provider or runner id that reported the call — `openai`, `claude-code`, … */\n source: string;\n model?: string;\n inputTokens?: number;\n outputTokens?: number;\n /** The share of `inputTokens` served from the provider's prompt cache. */\n cachedInputTokens?: number;\n /**\n * Filled only from a provider's or runner's own report, never from a price\n * table or an estimate: prices go stale, and a subscription runner has no\n * per-token price at all. No report means no cost, not a guessed one.\n */\n reportedCost?: { amount: number; currency: string };\n /** The model's context window, when the runner itself reports it. */\n contextWindow?: number;\n /** Set when a subagent made the call; its usage still counts toward the total. */\n subagentId?: string;\n}\n\n/**\n * A running sum of {@link UsageRecord}s. A measure stays absent until some\n * record reports it. Cost is kept per currency: two runners may bill in\n * different ones, and there is no honest exchange rate to fold them together.\n */\nexport interface UsageTotals {\n inputTokens?: number;\n outputTokens?: number;\n cachedInputTokens?: number;\n reportedCost?: Record<string, number>;\n}\n\n/**\n * The input side of a call whose provider reports its prompt in parts — the\n * uncached tail, with cache reads and cache writes beside it rather than inside\n * it (Anthropic, and OpenCode after it). The prompt the model saw is all three;\n * only the reads were served from cache, since a write is billed as fresh\n * input. A part left unreported adds nothing, and with no part reported there\n * is no measure at all.\n */\nexport function partedPromptUsage(parts: { uncached?: number; cacheRead?: number; cacheWrite?: number }): Pick<UsageRecord, 'inputTokens' | 'cachedInputTokens'> {\n const reported = [parts.uncached, parts.cacheRead, parts.cacheWrite].filter((n): n is number => n !== undefined);\n return {\n ...(reported.length > 0 ? { inputTokens: reported.reduce((a, b) => a + b, 0) } : {}),\n ...(parts.cacheRead !== undefined ? { cachedInputTokens: parts.cacheRead } : {}),\n };\n}\n\nconst TOKEN_FIELDS = ['inputTokens', 'outputTokens', 'cachedInputTokens'] as const;\n\nexport function addUsage(totals: UsageTotals, record: UsageRecord): UsageTotals {\n const next: UsageTotals = { ...totals };\n for (const field of TOKEN_FIELDS) {\n const reported = record[field];\n if (reported !== undefined) next[field] = (totals[field] ?? 0) + reported;\n }\n if (record.reportedCost) {\n const { amount, currency } = record.reportedCost;\n next.reportedCost = { ...totals.reportedCost, [currency]: (totals.reportedCost?.[currency] ?? 0) + amount };\n }\n return next;\n}\n\n/**\n * What the planner has consumed over a session (#49). `totals` includes every\n * `bySubagent` entry. `lastPromptTokens` and `contextWindow` track the\n * planner's own calls only — a subagent runs its own model, whose window says\n * nothing about the planner's.\n */\nexport interface PlannerUsage {\n totals: UsageTotals;\n bySubagent?: Record<string, UsageTotals>;\n lastPromptTokens?: number;\n contextWindow?: number;\n}\n\nexport function addPlannerUsage(usage: PlannerUsage, record: UsageRecord): PlannerUsage {\n const next: PlannerUsage = { ...usage, totals: addUsage(usage.totals, record) };\n if (record.subagentId) {\n next.bySubagent = { ...usage.bySubagent, [record.subagentId]: addUsage(usage.bySubagent?.[record.subagentId] ?? {}, record) };\n return next;\n }\n if (record.inputTokens !== undefined) next.lastPromptTokens = record.inputTokens;\n // A reported window of 0 means \"not known\", not \"no room\": keep it out so the\n // fill is omitted rather than shown against a guessed zero.\n if (record.contextWindow !== undefined && record.contextWindow > 0) next.contextWindow = record.contextWindow;\n return next;\n}\n\n/** Whether any measure was reported: a token line of nothing but blanks says nothing. */\nexport function isMeasured(totals: UsageTotals): boolean {\n return totals.inputTokens !== undefined || totals.outputTokens !== undefined || totals.cachedInputTokens !== undefined\n || Object.keys(totals.reportedCost ?? {}).length > 0;\n}\n\n/** What the token line shows of a ledger — live from its broadcast, or reloaded from the saved one. */\nexport interface UsageLine {\n totals: UsageTotals;\n bySubagent?: Record<string, UsageTotals>;\n contextFill?: { usedTokens: number; windowTokens: number };\n}\n\nexport function usageLine(usage: PlannerUsage): UsageLine {\n const contextFill = plannerContextFill(usage);\n return {\n totals: usage.totals,\n ...(usage.bySubagent ? { bySubagent: usage.bySubagent } : {}),\n ...(contextFill ? { contextFill } : {}),\n };\n}\n\n/**\n * The last planner prompt against its window, or undefined while either is\n * unknown. `usedTokens` is the prompt total as reported, cached tokens\n * included: a cached token still occupies the window, so subtracting the\n * cached share would understate how full the context is. A window of 0 is\n * treated as unknown — never guessed.\n */\nexport function plannerContextFill(usage: PlannerUsage): { usedTokens: number; windowTokens: number } | undefined {\n if (usage.lastPromptTokens === undefined || usage.contextWindow === undefined || usage.contextWindow <= 0) return undefined;\n return { usedTokens: usage.lastPromptTokens, windowTokens: usage.contextWindow };\n}\n","/*\n * Pure terminal rendering: turns raw PTY bytes into the text a user would see.\n * No state and no I/O, so the verdict scan and every output reader share it.\n */\n\n// eslint-disable-next-line no-control-regex\nexport const ANSI_OR_CTRL_RE = /\\x1b\\[[0-9;?]*[A-Za-z]|\\x1b\\][^\\x07\\x1b]*(?:\\x07|\\x1b\\\\)?|\\x1b[()][AB012]|\\x1b[=>]|[\\x00-\\x08\\x0b-\\x1f\\x7f]/g;\n\n/**\n * Collapse terminal rendering out of raw PTY output: strip ANSI/OSC escape\n * sequences, box-drawing gutter characters, and ALL whitespace. Interactive\n * TUIs soft-wrap long lines, so a completion marker arrives split across\n * lines with escapes interleaved — a raw `includes()` never matches it.\n * Markers contain no whitespace, so this flattened view is safe to scan.\n */\nexport function flattenTerminalOutput(raw: string): string {\n return raw\n .replace(ANSI_OR_CTRL_RE, '')\n .replace(/[─-▟]/g, '') // box-drawing + block elements (TUI borders/gutters)\n .replace(/\\s+/g, '');\n}\n\n/**\n * Reconstruct the small terminal screen represented by a PTY output tail.\n *\n * Full-screen TUIs do not emit answer text as one chronological stream. They\n * paint fragments at absolute cursor positions and repaint unrelated widgets\n * between those writes. OpenCode, for example, may emit:\n *\n * row 9: \"<<<ORDEW\"\n * row 23: spinner repaint\n * row 9: \"ELL_DONE_...\"\n *\n * Flattening that byte stream inserts the spinner inside the marker. Rendering\n * the cursor-positioned writes first recovers the token the user actually sees.\n * This intentionally implements only the common cursor/erase subset used by\n * coding-agent TUIs; the chronological scanner remains the fallback for plain\n * output and soft-wrapped lines.\n */\nexport function renderTerminalOutput(raw: string): string {\n const rows = new Map<number, string[]>();\n let row = 1;\n let col = 1;\n let savedRow = 1;\n let savedCol = 1;\n\n const cells = (r: number): string[] => {\n let line = rows.get(r);\n if (!line) {\n line = [];\n rows.set(r, line);\n }\n return line;\n };\n const firstParam = (params: number[], fallback = 1): number => params[0] || fallback;\n const eraseLine = (mode: number): void => {\n const line = cells(row);\n if (mode === 2) {\n rows.set(row, []);\n } else if (mode === 1) {\n for (let c = 0; c < col; c++) line[c] = ' ';\n } else {\n line.length = Math.max(0, col - 1);\n }\n };\n\n for (let i = 0; i < raw.length;) {\n const ch = raw[i];\n if (ch === '\\x1b') {\n const kind = raw[i + 1];\n if (kind === '[') {\n let end = i + 2;\n while (end < raw.length) {\n const code = raw.charCodeAt(end);\n if (code >= 0x40 && code <= 0x7e) break;\n end++;\n }\n if (end >= raw.length) break;\n const final = raw[end];\n const body = raw.slice(i + 2, end).replace(/^[?<>=!]+/, '');\n const params = body.split(';').map((p) => Number.parseInt(p, 10) || 0);\n switch (final) {\n case 'H':\n case 'f':\n row = params[0] || 1;\n col = params[1] || 1;\n break;\n case 'G': col = firstParam(params); break;\n case 'd': row = firstParam(params); break;\n case 'A': row = Math.max(1, row - firstParam(params)); break;\n case 'B': row += firstParam(params); break;\n case 'C': col += firstParam(params); break;\n case 'D': col = Math.max(1, col - firstParam(params)); break;\n case 'E': row += firstParam(params); col = 1; break;\n case 'F': row = Math.max(1, row - firstParam(params)); col = 1; break;\n case 's': savedRow = row; savedCol = col; break;\n case 'u': row = savedRow; col = savedCol; break;\n case 'K': eraseLine(params[0] || 0); break;\n case 'J':\n if ((params[0] || 0) === 2 || (params[0] || 0) === 3) rows.clear();\n break;\n case 'X': {\n const line = cells(row);\n for (let c = 0; c < firstParam(params); c++) line[col - 1 + c] = ' ';\n break;\n }\n case 'P': {\n cells(row).splice(col - 1, firstParam(params));\n break;\n }\n case '@': {\n cells(row).splice(col - 1, 0, ...Array(firstParam(params)).fill(' '));\n break;\n }\n default:\n break;\n }\n i = end + 1;\n continue;\n }\n if (kind === ']' || kind === 'P' || kind === '^' || kind === '_') {\n let end = i + 2;\n while (end < raw.length && raw[end] !== '\\x07' && !(raw[end] === '\\x1b' && raw[end + 1] === '\\\\')) end++;\n if (end >= raw.length) break;\n i = raw[end] === '\\x07' ? end + 1 : end + 2;\n continue;\n }\n if (kind === '7') {\n savedRow = row;\n savedCol = col;\n } else if (kind === '8') {\n row = savedRow;\n col = savedCol;\n } else if (kind === 'c') {\n rows.clear();\n row = 1;\n col = 1;\n }\n // Character-set selection sequences carry one extra byte.\n i += kind === '(' || kind === ')' ? 3 : 2;\n continue;\n }\n if (ch === '\\r') {\n col = 1;\n i++;\n continue;\n }\n if (ch === '\\n') {\n row++;\n i++;\n continue;\n }\n if (ch === '\\b') {\n col = Math.max(1, col - 1);\n i++;\n continue;\n }\n if (ch === '\\t') {\n col += 8 - ((col - 1) % 8);\n i++;\n continue;\n }\n if (ch < ' ' || ch === '\\x7f') {\n i++;\n continue;\n }\n cells(row)[col - 1] = ch;\n col++;\n i++;\n }\n\n const populated = [...rows.keys()].sort((a, b) => a - b);\n return populated.map((r) => cells(r).join('')).join('\\n');\n}\n\n/**\n * Reconstruct what the user actually saw, then stop at the completion marker.\n * Used for anything captured as a task's durable output (outputSummary.logTail,\n * and any future planner read channel): the raw chronological PTY tail is\n * dominated by TUI paint — spinner lines, status bars, box-drawing borders,\n * cursor-positioned fragments — because screen-painting agents emit answer\n * text at absolute positions and repaint unrelated widgets around it.\n *\n * The marker row is the cut anchor: everything below it is the TUI's\n * persistent chrome, not task output. Falls back to the chronological\n * (stripAnsi'd) tail when neither render exposes the marker — the common case\n * for headless runners, whose raw stream IS meaningful.\n */\nexport function renderCleanCapture(raw: string, doneToken?: string): string {\n const rendered = renderTerminalOutput(raw).replace(/[\\s─-▟]+$/gm, '');\n if (!doneToken) return rendered.trim();\n const lines = rendered.split('\\n');\n const flat = (s: string) => flattenTerminalOutput(s);\n for (let i = 0; i < lines.length; i++) {\n // The marker may be split across two rendered rows (painted at a row\n // boundary): the first row alone won't match, so a two-row window is\n // scanned too. The cut is the last row that carries marker text — rows\n // above it are content, rows below are TUI chrome.\n const own = flat(lines[i]).includes(doneToken);\n if (own) return lines.slice(0, i).join('\\n').trim();\n const window = flat(lines[i]) + (i + 1 < lines.length ? flat(lines[i + 1]) : '');\n if (window.includes(doneToken)) return lines.slice(0, i + 1).join('\\n').trim();\n }\n return rendered.trim();\n}\n","import type { ToolHeadline } from './blocks';\nimport { ANSI_OR_CTRL_RE } from '../services/terminalRender';\n\n/**\n * How a command row names an Ordewell research tool. A harness planner's call\n * keeps the agent's own name instead (`toolLabel`, ADR-0009), so the row says\n * which tool actually ran.\n */\nconst DISPLAY_NAMES: Record<string, string> = {\n read_file: 'Read',\n read_files: 'Read',\n list_dir: 'List',\n glob: 'Glob',\n grep: 'Grep',\n find_symbol: 'Symbol',\n bash: 'Bash',\n fetch: 'Fetch',\n web_search: 'WebSearch',\n spawn_research_agent: 'Agent',\n};\n\n// Tried in order for a tool with no rule of its own: the field that names what\n// a call is about comes before the free text around it.\nconst HINT_FIELDS = ['file_path', 'filePath', 'path', 'url', 'pattern', 'query', 'command', 'description', 'prompt'] as const;\n\nfunction parseArgs(args: string): Record<string, unknown> | null {\n try {\n const parsed: unknown = JSON.parse(args);\n return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed as Record<string, unknown> : null;\n } catch {\n return null;\n }\n}\n\nfunction oneLine(text: string): string {\n return text.replace(/\\s+/g, ' ').trim();\n}\n\n// A command's later lines can change what it does (a heredoc body, a chained\n// step), so they are counted rather than folded into the first.\nfunction firstLineOf(command: string): string {\n const lines = command.trim().split(/\\r?\\n/);\n const more = lines.length - 1;\n return more > 0 ? `${lines[0]} … +${more} line${more === 1 ? '' : 's'}` : lines[0];\n}\n\nfunction text(value: unknown): string {\n return typeof value === 'string' ? value : '';\n}\n\nfunction searchArg(pattern: string, path: string, include = ''): string {\n return `${pattern}${path ? ` in ${path}` : ''}${include ? ` (${include})` : ''}`;\n}\n\nfunction hintArg(args: Record<string, unknown>): string {\n for (const field of HINT_FIELDS) {\n const value = text(args[field]);\n if (value) return field === 'command' ? firstLineOf(value) : oneLine(value);\n }\n const firstText = Object.values(args).find((v): v is string => typeof v === 'string' && v.trim() !== '');\n return firstText ? oneLine(firstText) : '';\n}\n\nfunction keyArgOf(tool: string, args: Record<string, unknown>): string {\n switch (tool) {\n case 'bash':\n return firstLineOf(text(args.command));\n case 'read_file':\n case 'list_dir':\n return text(args.path);\n case 'read_files':\n return Array.isArray(args.paths) ? args.paths.map(String).join(', ') : '';\n case 'glob':\n return searchArg(text(args.pattern), text(args.path));\n case 'grep':\n return searchArg(text(args.pattern), text(args.path), text(args.include));\n case 'find_symbol':\n return `${text(args.symbol)}${text(args.language) ? ` [${text(args.language)}]` : ''}`;\n case 'fetch':\n return text(args.url);\n case 'web_search':\n return oneLine(text(args.query));\n case 'spawn_research_agent':\n return oneLine(text(args.prompt));\n default:\n return hintArg(args);\n }\n}\n\n/**\n * The one-line head of a command row, shared by every surface: the tool's name\n * and the argument that says what the call is about.\n *\n * @param args The call's arguments as announced (JSON). Anything that is not a\n * JSON object is shown as it came, on one line.\n * @param toolLabel A harness planner's own name for the tool (ADR-0009).\n */\nexport function toolHeadline(tool: string, args: string, toolLabel?: string): ToolHeadline {\n const name = toolLabel?.trim() || DISPLAY_NAMES[tool] || tool;\n const parsed = parseArgs(args);\n return { name, keyArg: parsed ? keyArgOf(tool, parsed) : oneLine(args) };\n}\n\nexport interface OutputPreview {\n lines: string[];\n hiddenLineCount: number;\n}\n\n/** The lines of a tool's output as a reader sees them — what {@link outputPreview} shows and counts. */\nexport function outputLines(output: string): string[] {\n // Line endings first: CR is itself a control character the escape pass drops.\n const lines = output.replace(/\\r\\n?/g, '\\n').replace(ANSI_OR_CTRL_RE, '').split('\\n');\n while (lines.length > 0 && lines[lines.length - 1].trim() === '') lines.pop();\n return lines;\n}\n\n/** The head of a tool's output for a collapsed row, and how many lines it leaves out. */\nexport function outputPreview(output: string, maxLines: number): OutputPreview {\n const lines = outputLines(output);\n const shown = lines.slice(0, Math.max(0, maxLines));\n return { lines: shown, hiddenLineCount: lines.length - shown.length };\n}\n","import type { ResearchStep, ResearchStepOutcome } from '../models/Task';\nimport type { UsageLine } from '../models/Usage';\nimport type { MessageBlock, MessageRole, PlanMarkerStatus, SubagentBlock, ToolBlock, ToolStatus, UsageBlock } from './blocks';\nimport { outputLines, toolHeadline } from './format';\n\n/*\n * How the session's records — messages, research steps, subagents, transcript\n * markers, usage totals — read as blocks. Shared by the live view and the\n * reload, so the two cannot disagree about what a record shows.\n */\n\n/** A line that is done: nothing streams into it any more. */\nexport function settledMessage(id: string, role: MessageRole, text: string, turnId?: string): MessageBlock {\n return { type: 'message', id, role, text, streaming: false, ...(turnId ? { turnId } : {}) };\n}\n\ntype SubagentFields = Omit<SubagentBlock, 'type' | 'id' | 'toolCallId' | 'model' | 'usage' | 'turnId'>\n & { toolCallId?: string; model?: string; usage?: SubagentBlock['usage']; turnId?: string };\n\nexport function subagentBlock(id: string, { toolCallId, model, usage, turnId, ...fields }: SubagentFields): SubagentBlock {\n return {\n type: 'subagent', id, ...fields,\n ...(toolCallId ? { toolCallId } : {}),\n ...(model ? { model } : {}),\n ...(usage ? { usage } : {}),\n ...(turnId ? { turnId } : {}),\n };\n}\n\nexport function usageBlock(id: string, { totals, bySubagent, contextFill }: UsageLine): UsageBlock {\n return { type: 'usage', id, totals, ...(bySubagent ? { bySubagent } : {}), ...(contextFill ? { contextFill } : {}) };\n}\n\nconst STATUS_OF_OUTCOME: Record<ResearchStepOutcome, ToolStatus> = {\n success: 'ok',\n failure: 'error',\n refused: 'denied',\n denied: 'denied',\n not_executed: 'interrupted',\n};\n\nexport function settledTool(call: ToolBlock, step: ResearchStep): ToolBlock {\n return { ...call, status: STATUS_OF_OUTCOME[step.outcome], outcome: step.outcome, output: step.result, outputLineCount: outputLines(step.result).length };\n}\n\nexport interface AnnouncedCall {\n tool: string;\n toolLabel?: string;\n args: string;\n toolCallId?: string;\n turnId?: string;\n}\n\nexport function pendingTool(id: string, { tool, toolLabel, args, toolCallId, turnId }: AnnouncedCall): ToolBlock {\n return {\n type: 'tool', id, tool, headline: toolHeadline(tool, args, toolLabel), args, status: 'pending', output: '', outputLineCount: 0,\n ...(toolCallId ? { toolCallId } : {}),\n ...(toolLabel ? { toolLabel } : {}),\n ...(turnId ? { turnId } : {}),\n };\n}\n\n/** The row of a call known only from its result — a reload, or a result whose call was never announced. */\nexport function toolFromStep(id: string, step: ResearchStep, turnId?: string): ToolBlock {\n return settledTool(pendingTool(id, { ...step, turnId }), step);\n}\n\n/**\n * What a plan marker says, read from the transcript entry `PlannerConversation`\n * writes for it (\"Plan generated with 2 tasks.\", \"Plan updated — now 1 task.\").\n * That entry is the only record of the marker a reload has, so the live view\n * reads the same text rather than counting tasks itself.\n */\nexport function planMarker(content: string): { status: Exclude<PlanMarkerStatus, 'building'>; taskCount?: number } {\n const count = /(\\d+) tasks?\\b/.exec(content);\n return { status: content.startsWith('Plan updated') ? 'updated' : 'generated', ...(count ? { taskCount: Number(count[1]) } : {}) };\n}\n","import type { ResearchStep } from '../models/Task';\nimport type { SessionMessage } from '../services/SessionMessage';\nimport { isMeasured } from '../models/Usage';\nimport type { DisplayBlock, MessageBlock, PlanBlock, SubagentBlock, SubagentChild, SubagentStatus, ToolBlock } from './blocks';\nimport { pendingTool, planMarker, settledMessage, settledTool, subagentBlock, toolFromStep, usageBlock } from './records';\n\n/**\n * A line a surface adds to the conversation itself rather than receiving from\n * the session: the user's prompt as it is sent, a notice, an error.\n */\nexport interface LocalEntry {\n type: 'local_entry';\n role: 'user' | 'system' | 'error';\n text: string;\n}\n\nexport type ConversationInput = SessionMessage | LocalEntry;\n\nexport interface ConversationView {\n readonly blocks: readonly DisplayBlock[];\n readonly nextId: number;\n /**\n * The newest transcript entry the view accounts for. Plan markers and system\n * notes reach a surface only inside the transcript a `plan_generated`\n * carries, so entries after this one are what is new in it.\n */\n readonly transcriptAt?: string;\n}\n\nexport const EMPTY_CONVERSATION: ConversationView = { blocks: [], nextId: 1 };\n\ntype Message<T extends SessionMessage['type']> = Extract<SessionMessage, { type: T }>;\n\n/** The top level of the conversation (`null`), or the index of the subagent whose children are meant. */\ntype Lane = number | null;\n\nfunction findLastIndex<T>(items: readonly T[], match: (item: T) => boolean): number {\n for (let i = items.length - 1; i >= 0; i--) if (match(items[i])) return i;\n return -1;\n}\n\nfunction replaceAt<T>(items: readonly T[], index: number, item: T): T[] {\n const next = items.slice();\n next[index] = item;\n return next;\n}\n\n// The usage line always sits last, so everything else lands above it.\nfunction insertTop(blocks: readonly DisplayBlock[], block: DisplayBlock): DisplayBlock[] {\n const last = blocks[blocks.length - 1];\n return last?.type === 'usage' ? [...blocks.slice(0, -1), block, last] : [...blocks, block];\n}\n\nfunction append(view: ConversationView, make: (id: string) => DisplayBlock): ConversationView {\n return { ...view, blocks: insertTop(view.blocks, make(`b${view.nextId}`)), nextId: view.nextId + 1 };\n}\n\nfunction subagentAt(view: ConversationView, lane: number): SubagentBlock {\n const block = view.blocks[lane];\n if (block.type !== 'subagent') throw new Error(`block ${lane} is not a subagent`);\n return block;\n}\n\nfunction laneBlocks(view: ConversationView, lane: Lane): readonly DisplayBlock[] {\n return lane === null ? view.blocks : subagentAt(view, lane).children;\n}\n\nfunction setChildren(view: ConversationView, lane: number, children: readonly SubagentChild[]): ConversationView {\n return { ...view, blocks: replaceAt(view.blocks, lane, { ...subagentAt(view, lane), children }) };\n}\n\nfunction laneReplace(view: ConversationView, lane: Lane, index: number, block: SubagentChild): ConversationView {\n if (lane === null) return { ...view, blocks: replaceAt(view.blocks, index, block) };\n return setChildren(view, lane, replaceAt(subagentAt(view, lane).children, index, block));\n}\n\nfunction laneAppend(view: ConversationView, lane: Lane, make: (id: string) => SubagentChild): ConversationView {\n if (lane === null) return append(view, make);\n const children = [...subagentAt(view, lane).children, make(`b${view.nextId}`)];\n return { ...setChildren(view, lane, children), nextId: view.nextId + 1 };\n}\n\nfunction isStreaming(block: DisplayBlock): boolean {\n return (block.type === 'message' || block.type === 'thinking') && block.streaming;\n}\n\nfunction sealed<B extends DisplayBlock>(block: B): B {\n return isStreaming(block) ? { ...block, streaming: false } : block;\n}\n\n/**\n * Planner output landed at `keep` in the lane, so nothing else there is still\n * streaming: one lane has one voice at a time, and a block left marked as\n * streaming would keep its cursor after its text had moved on.\n */\nfunction sealLane(view: ConversationView, lane: Lane, keep: number): ConversationView {\n const blocks = laneBlocks(view, lane);\n if (!blocks.some((b, i) => i !== keep && isStreaming(b))) return view;\n if (lane === null) return { ...view, blocks: view.blocks.map((b, i) => (i === keep ? b : sealed(b))) };\n return setChildren(view, lane, subagentAt(view, lane).children.map((b, i) => (i === keep ? b : sealed(b))));\n}\n\n/** Append planner output to the lane, which then speaks through it alone. */\nfunction appendOutput(view: ConversationView, lane: Lane, make: (id: string) => SubagentChild): ConversationView {\n const next = laneAppend(view, lane, make);\n return sealLane(next, lane, findLastIndex(laneBlocks(next, lane), (b) => b.type !== 'usage'));\n}\n\nfunction isUnsettledSegment(block: DisplayBlock, turnId: string): block is MessageBlock {\n return block.type === 'message' && block.turnId === turnId && block.segmentId !== undefined;\n}\n\nfunction isBuildingPlan(block: DisplayBlock, turnId: string | undefined): block is PlanBlock {\n return block.type === 'plan' && block.status === 'building' && block.turnId === turnId;\n}\n\n/**\n * Where the turn's final segment is, if its text streamed: the turn's latest\n * block, when that is streamed text. Anything the turn did after a segment —\n * a tool call, a plan envelope — means a later segment follows it, and a later\n * segment never rewrites an earlier one. `pastPlan` looks past the turn's plan\n * block, for a plan that settled after its own text.\n */\nfunction finalSegmentIndex(blocks: readonly DisplayBlock[], turnId: string, pastPlan = false): number {\n for (let i = blocks.length - 1; i >= 0; i--) {\n const block = blocks[i];\n if (block.type === 'usage' || block.type === 'thinking' || block.type === 'approval' || (pastPlan && block.type === 'plan')) continue;\n if (block.turnId !== turnId) continue;\n return isUnsettledSegment(block, turnId) ? i : -1;\n }\n return -1;\n}\n\nfunction openTurn(view: ConversationView, turnId: string, prompt: string): ConversationView {\n // A surface shows its user's prompt the moment it is sent; the turn that\n // answers it adopts that line rather than repeating it.\n const i = findLastIndex(view.blocks, (b) => b.type === 'message' && b.role === 'user');\n const sent = view.blocks[i];\n if (sent?.type === 'message' && sent.turnId === undefined && sent.text === prompt) {\n return { ...view, blocks: replaceAt(view.blocks, i, { ...sent, turnId }) };\n }\n return append(view, (id) => settledMessage(id, 'user', prompt, turnId));\n}\n\nfunction streamText(view: ConversationView, { turnId, segmentId, text }: Message<'planner_text_delta'>): ConversationView {\n const i = findLastIndex(view.blocks, (b) => isUnsettledSegment(b, turnId) && b.segmentId === segmentId);\n const segment = view.blocks[i];\n if (segment?.type === 'message') {\n return sealLane(laneReplace(view, null, i, { ...segment, text: segment.text + text, streaming: true }), null, i);\n }\n return appendOutput(view, null, (id) => ({ type: 'message', id, role: 'planner', text, streaming: true, turnId, segmentId }));\n}\n\n/**\n * Drop the turn's plan display while it is still building: whatever streamed\n * there did not become a plan — a task-ops or task-query envelope, or, from a\n * daemon older than turns, the reply's prose.\n */\nfunction dropBuildingPlan(view: ConversationView, turnId: string | undefined, segmentId?: string): ConversationView {\n const blocks = view.blocks.filter((b) => !(isBuildingPlan(b, turnId) && (segmentId === undefined || b.segmentId === segmentId)));\n return blocks.length === view.blocks.length ? view : { ...view, blocks };\n}\n\nfunction settleReply(view: ConversationView, { content, turnId }: Message<'planner_message'>): ConversationView {\n if (turnId !== undefined) {\n const i = finalSegmentIndex(view.blocks, turnId);\n if (i >= 0) {\n return dropBuildingPlan({ ...view, blocks: replaceAt(view.blocks, i, settledMessage(view.blocks[i].id, 'planner', content, turnId)) }, turnId);\n }\n }\n return append(dropBuildingPlan(view, turnId), (id) => settledMessage(id, 'planner', content, turnId));\n}\n\n// A retracted envelope takes the turn's plan display with it, or its retry\n// would build on the botched JSON and the partial-plan rows read both.\nfunction retractText(view: ConversationView, { turnId, segmentId }: Message<'planner_text_retracted'>): ConversationView {\n const blocks = view.blocks.filter((b) => !(isUnsettledSegment(b, turnId) && (segmentId === undefined || b.segmentId === segmentId)));\n return dropBuildingPlan(blocks.length === view.blocks.length ? view : { ...view, blocks }, turnId, segmentId);\n}\n\n/**\n * The subagent's block, created on first sighting: its activity can arrive\n * before (or without) its start. The planner call that spawns it becomes the\n * block, keeping the call's id and position — the call and the subagent are\n * one thing on screen.\n */\nfunction subagentLane(view: ConversationView, subagentId: string, turnId?: string): [ConversationView, number] {\n const existing = findLastIndex(view.blocks, (b) => b.type === 'subagent' && b.subagentId === subagentId);\n if (existing >= 0) return [view, existing];\n const spawn = findLastIndex(view.blocks, (b) => b.type === 'tool' && (b.spawns === subagentId || b.toolCallId === subagentId));\n const call = view.blocks[spawn];\n if (call?.type === 'tool') {\n const block = subagentBlock(call.id, {\n subagentId, brief: call.headline.keyArg, status: 'running', children: [], digest: '', toolCallId: call.toolCallId, turnId: call.turnId,\n });\n return [{ ...view, blocks: replaceAt(view.blocks, spawn, block) }, spawn];\n }\n const next = append(view, (id) => subagentBlock(id, { subagentId, brief: '', status: 'running', children: [], digest: '', turnId }));\n return [next, findLastIndex(next.blocks, (b) => b.type === 'subagent' && b.subagentId === subagentId)];\n}\n\nfunction updateSubagent(view: ConversationView, subagentId: string, turnId: string | undefined, update: (block: SubagentBlock) => SubagentBlock): ConversationView {\n const [next, lane] = subagentLane(view, subagentId, turnId);\n return { ...next, blocks: replaceAt(next.blocks, lane, update(subagentAt(next, lane))) };\n}\n\nfunction startSubagent(view: ConversationView, { subagentId, brief, model, turnId }: Message<'subagent_started'>): ConversationView {\n return updateSubagent(view, subagentId, turnId, (block) => ({ ...block, ...(brief ? { brief } : {}), ...(model ? { model } : {}) }));\n}\n\nfunction finishSubagent(view: ConversationView, { subagentId, outcome, digest, usage, turnId }: Message<'subagent_finished'>): ConversationView {\n return updateSubagent(view, subagentId, turnId, (block) => ({\n ...block, status: outcome, digest, children: block.children.map(sealed), ...(usage ? { usage } : {}),\n }));\n}\n\nfunction think(view: ConversationView, text: string, turnId?: string, segmentId?: string, subagentId?: string): ConversationView {\n const [placed, lane]: [ConversationView, Lane] = subagentId ? subagentLane(view, subagentId, turnId) : [view, null];\n const blocks = laneBlocks(placed, lane);\n const lastIndex = findLastIndex(blocks, (b) => b.type !== 'usage');\n const last = blocks[lastIndex];\n if (last?.type === 'thinking' && last.streaming && last.segmentId === segmentId) {\n return laneReplace(placed, lane, lastIndex, { ...last, text: last.text + text });\n }\n return appendOutput(placed, lane, (id) => ({\n type: 'thinking', id, text, streaming: true,\n ...(turnId ? { turnId } : {}),\n ...(segmentId ? { segmentId } : {}),\n ...(subagentId ? { subagentId } : {}),\n }));\n}\n\nfunction announceTool(view: ConversationView, call: Message<'research_step'>): ConversationView {\n // A spawn call names the subagent it starts; any other call tagged with a\n // subagent is that subagent's own.\n const spawns = call.tool === 'spawn_research_agent' ? call.subagentId : undefined;\n const [placed, lane]: [ConversationView, Lane] = call.subagentId && !spawns ? subagentLane(view, call.subagentId, call.turnId) : [view, null];\n return appendOutput(placed, lane, (id) => ({ ...pendingTool(id, call), ...(spawns ? { spawns } : {}) }));\n}\n\n// By the call's id: results of a parallel round return in any order, and a\n// name match would put one call's output on another's row (ADR-0008). The\n// name scan survives only for calls that announced no id.\nfunction isPendingCallOf(block: DisplayBlock, step: ResearchStep): block is ToolBlock {\n if (block.type !== 'tool' || block.status !== 'pending') return false;\n return step.toolCallId ? block.toolCallId === step.toolCallId : block.toolCallId === undefined && block.tool === step.tool;\n}\n\nfunction settleTool(view: ConversationView, { step, turnId }: Message<'research_step_done'>): ConversationView {\n const [placed, lane]: [ConversationView, Lane] = step.subagentId ? subagentLane(view, step.subagentId, turnId) : [view, null];\n const blocks = laneBlocks(placed, lane);\n const i = findLastIndex(blocks, (b) => isPendingCallOf(b, step));\n const pending = blocks[i];\n if (pending?.type === 'tool') return laneReplace(placed, lane, i, settledTool(pending, step));\n // The call that spawned a subagent is shown by the subagent's block, and\n // only the subagent's own finish settles it: a backgrounded agent's call\n // returns at launch, long before the work ends.\n if (lane === null && step.toolCallId && blocks.some((b) => b.type === 'subagent' && b.toolCallId === step.toolCallId)) return view;\n return laneAppend(placed, lane, (id) => toolFromStep(id, step, turnId));\n}\n\n/**\n * The building display shows one envelope: the segment streaming now. A turn's\n * later envelope (a plan after a read it asked for) starts it over, below\n * whatever the turn did in between, rather than running on from the last one.\n */\nfunction streamPlan(view: ConversationView, { token, turnId, segmentId }: Message<'plan_token'>): ConversationView {\n const i = findLastIndex(view.blocks, (b) => isBuildingPlan(b, turnId));\n const building = view.blocks[i];\n if (building?.type === 'plan' && building.segmentId === segmentId) {\n return { ...view, blocks: replaceAt(view.blocks, i, { ...building, text: building.text + token }) };\n }\n return append(dropBuildingPlan(view, turnId), (id) => ({\n type: 'plan', id, status: 'building', text: token, ...(turnId ? { turnId } : {}), ...(segmentId ? { segmentId } : {}),\n }));\n}\n\n/** A plan landed: the building display of the turn that committed it becomes its marker. */\nfunction markPlan(view: ConversationView, content: string, turnId: string | undefined): ConversationView {\n const marker = planMarker(content);\n const i = findLastIndex(view.blocks, (b) => isBuildingPlan(b, turnId));\n const building = view.blocks[i];\n if (building?.type === 'plan') {\n const settled: PlanBlock = { type: 'plan', id: building.id, ...marker, text: '', ...(building.turnId ? { turnId: building.turnId } : {}) };\n return { ...view, blocks: replaceAt(view.blocks, i, settled) };\n }\n return append(view, (id) => ({ type: 'plan', id, text: '', ...marker }));\n}\n\nfunction requestApproval(view: ConversationView, { id: approvalId, kind, subject, scope, detail, turnId }: Message<'approval_request'>): ConversationView {\n return append(view, (id) => ({\n type: 'approval', id, approvalId, kind, subject, scope, status: 'pending',\n ...(detail ? { detail } : {}),\n ...(turnId ? { turnId } : {}),\n }));\n}\n\nfunction settleApproval(view: ConversationView, { id, granted }: Message<'approval_settled'>): ConversationView {\n const i = findLastIndex(view.blocks, (b) => b.type === 'approval' && b.approvalId === id);\n const request = view.blocks[i];\n if (request?.type !== 'approval') return view;\n return { ...view, blocks: replaceAt(view.blocks, i, { ...request, status: granted ? 'granted' : 'denied', decidedBy: 'asked' }) };\n}\n\nfunction decideApproval(view: ConversationView, { kind, subject, scope, detail, granted, source }: Message<'approval_decided'>): ConversationView {\n return append(view, (id) => ({\n type: 'approval', id, kind, subject, scope, status: granted ? 'granted' : 'denied', decidedBy: source,\n ...(detail ? { detail } : {}),\n }));\n}\n\nfunction reportUsage(view: ConversationView, message: Message<'planner_usage'>): ConversationView {\n if (!isMeasured(message.totals)) return view;\n const last = view.blocks[view.blocks.length - 1];\n const line = usageBlock(last?.type === 'usage' ? last.id : `b${view.nextId}`, message);\n if (last?.type === 'usage') return { ...view, blocks: replaceAt(view.blocks, view.blocks.length - 1, line) };\n return { ...view, blocks: [...view.blocks, line], nextId: view.nextId + 1 };\n}\n\nfunction closeBlock<B extends DisplayBlock>(block: B, interrupted: boolean): B {\n return interrupted && block.type === 'tool' && block.status === 'pending' ? { ...block, status: 'interrupted' } : sealed(block);\n}\n\nfunction closeSubagent(block: SubagentBlock, cut: SubagentStatus | null): SubagentBlock {\n const children = block.children.map((c) => closeBlock(c, cut !== null));\n const status = cut && block.status === 'running' ? cut : block.status;\n return status === block.status && children.every((c, i) => c === block.children[i]) ? block : { ...block, status, children };\n}\n\n/**\n * Nothing carries the turn's id after this, so whatever it left open is closed:\n * streaming stops, and on a stop or failure its unanswered calls and running\n * subagents say they were cut short. A plan reply that streamed as text (a\n * prose preamble decides a segment's route) gives way to its plan block, as a\n * settled message would have replaced it.\n */\nfunction endTurn(view: ConversationView, { turnId, outcome }: Message<'planner_turn_ended'>): ConversationView {\n let next = dropBuildingPlan(view, turnId);\n const planText = outcome === 'plan' ? finalSegmentIndex(next.blocks, turnId, true) : -1;\n if (planText >= 0) next = { ...next, blocks: next.blocks.filter((_, i) => i !== planText) };\n const cut: SubagentStatus | null = outcome === 'stopped' ? 'stopped' : outcome === 'error' ? 'failed' : null;\n const blocks = next.blocks.map((b) => (b.type === 'subagent' ? closeSubagent(b, cut) : closeBlock(b, cut !== null)));\n return blocks.every((b, i) => b === next.blocks[i]) ? next : { ...next, blocks };\n}\n\n// The summary is announced as a planner message before the transcript it now\n// heads arrives; the transcript records it as a notice, and so does the view.\nfunction markCompaction(view: ConversationView, summary: string): ConversationView {\n const i = findLastIndex(view.blocks, (b) => b.type === 'message' && b.role === 'planner' && b.turnId === undefined && b.text === summary);\n const announced = view.blocks[i];\n if (announced?.type === 'message') return { ...view, blocks: replaceAt(view.blocks, i, { ...announced, role: 'system' }) };\n return append(view, (id) => settledMessage(id, 'system', summary));\n}\n\nfunction syncTranscript(view: ConversationView, { plan, turnId }: Message<'plan_generated'>): ConversationView {\n const history = plan.conversationHistory ?? [];\n // Only the newest marker can be the committing turn's; older ones a surface\n // is only now catching up on belong to turns long over.\n const committed = findLastIndex(history, (e) => e.kind === 'plan_generated');\n let next = view;\n let latest = view.transcriptAt;\n for (const [i, entry] of history.entries()) {\n const isNew = view.transcriptAt === undefined || entry.timestamp > view.transcriptAt;\n if (isNew && entry.kind === 'plan_generated') next = markPlan(next, entry.content, i === committed ? turnId : undefined);\n if (isNew && entry.kind === 'system') next = append(next, (id) => settledMessage(id, 'system', entry.content));\n if (isNew && entry.kind === 'compaction') next = markCompaction(next, entry.content);\n if (latest === undefined || entry.timestamp > latest) latest = entry.timestamp;\n }\n return next === view && latest === view.transcriptAt ? view : { ...next, transcriptAt: latest };\n}\n\n/**\n * Fold one input into the view. Pure and incremental: blocks the input does\n * not touch keep their identity, and an input that changes nothing returns\n * `view` itself — surfaces memoize their drawing on that, and text deltas\n * arrive quickly.\n */\nexport function reduceConversation(view: ConversationView, input: ConversationInput): ConversationView {\n switch (input.type) {\n case 'local_entry':\n return append(view, (id) => settledMessage(id, input.role, input.text));\n case 'planner_turn_started':\n return input.prompt === undefined ? view : openTurn(view, input.turnId, input.prompt);\n case 'planner_text_delta':\n return streamText(view, input);\n case 'planner_message':\n return settleReply(view, input);\n case 'planner_text_retracted':\n return retractText(view, input);\n case 'planner_thinking_delta':\n return think(view, input.text, input.turnId, input.segmentId, input.subagentId);\n case 'research_step':\n return announceTool(view, input);\n case 'research_step_done':\n return settleTool(view, input);\n case 'subagent_started':\n return startSubagent(view, input);\n case 'subagent_finished':\n return finishSubagent(view, input);\n case 'plan_token':\n return streamPlan(view, input);\n case 'plan_generated':\n return syncTranscript(view, input);\n case 'planner_turn_ended':\n return endTurn(view, input);\n case 'approval_request':\n return requestApproval(view, input);\n case 'approval_settled':\n return settleApproval(view, input);\n case 'approval_decided':\n return decideApproval(view, input);\n case 'planner_usage':\n return reportUsage(view, input);\n default:\n return view;\n }\n}\n","import type { ConversationMessage, ResearchLogEntry, ResearchStep, SubagentLogEntry } from '../models/Task';\nimport { isMeasured, usageLine, type PlannerUsage } from '../models/Usage';\nimport type { DisplayBlock } from './blocks';\nimport { planMarker, settledMessage, subagentBlock, toolFromStep, usageBlock } from './records';\nimport type { ConversationView } from './reduce';\n\n/** One top-level block before it has an id, and where it falls in time. */\ninterface Placed {\n at: string;\n /** Breaks a tie in `at`: a user's message, then the research it caused, then what the turn settled on. */\n rank: number;\n build: (nextId: () => string) => DisplayBlock;\n}\n\nconst USER = 0;\nconst RESEARCH = 1;\nconst SETTLED = 2;\n\nfunction fromEntry(entry: ConversationMessage): Placed['build'] {\n if (entry.kind === 'plan_generated') return (id) => ({ type: 'plan', id: id(), text: '', ...planMarker(entry.content) });\n const role = entry.kind === 'system' || entry.kind === 'compaction' ? 'system' : entry.role === 'user' ? 'user' : 'planner';\n return (id) => settledMessage(id(), role, entry.content);\n}\n\nfunction spawnBrief(step: ResearchStep): string | undefined {\n try {\n const args: unknown = JSON.parse(step.args);\n const prompt = args && typeof args === 'object' ? (args as Record<string, unknown>).prompt : undefined;\n return typeof prompt === 'string' ? prompt.trim() : undefined;\n } catch {\n return undefined;\n }\n}\n\n/**\n * Which subagent a planner step started, if it did. The log keeps no link: a\n * harness planner's subagent takes its call's id, and an ADR-0005 research\n * agent is briefed with its spawn call's prompt, trimmed.\n */\nfunction spawnedBy(step: ResearchStep, unclaimed: SubagentLogEntry[]): SubagentLogEntry | undefined {\n const byId = unclaimed.find((e) => e.subagentId === step.toolCallId);\n if (byId) return byId;\n if (step.tool !== 'spawn_research_agent') return undefined;\n const brief = spawnBrief(step);\n return brief === undefined ? undefined : unclaimed.find((e) => e.brief === brief);\n}\n\nfunction research(log: readonly ResearchLogEntry[]): Placed[] {\n const subagents = log.filter((e): e is SubagentLogEntry => 'type' in e && e.type === 'subagent');\n const ownSubagent = new Set(subagents.map((e) => e.subagentId));\n const unclaimed = [...subagents];\n const spawnCall = new Map<string, string | undefined>();\n const placed: Placed[] = [];\n\n for (const entry of log) {\n if ('type' in entry) {\n if (entry.type === 'system') placed.push({ at: entry.timestamp, rank: RESEARCH, build: (id) => settledMessage(id(), 'system', entry.content) });\n continue;\n }\n if (entry.subagentId && ownSubagent.has(entry.subagentId)) continue;\n const spawned = spawnedBy(entry, unclaimed);\n if (spawned) {\n unclaimed.splice(unclaimed.indexOf(spawned), 1);\n spawnCall.set(spawned.subagentId, entry.toolCallId);\n continue;\n }\n placed.push({ at: entry.timestamp, rank: RESEARCH, build: (id) => toolFromStep(id(), entry) });\n }\n\n // A subagent stands where it started, which is where its spawn call was\n // announced; the call's own record is written only once the subagent is done.\n for (const entry of subagents) {\n const toolCallId = spawnCall.get(entry.subagentId);\n const steps = log.filter((e): e is ResearchStep => !('type' in e) && e.subagentId === entry.subagentId);\n placed.push({\n at: entry.timestamp,\n rank: RESEARCH,\n build: (id) => subagentBlock(id(), {\n subagentId: entry.subagentId, brief: entry.brief, status: entry.outcome, digest: entry.digest,\n children: steps.map((step) => toolFromStep(id(), step)),\n toolCallId, model: entry.model, usage: entry.usage,\n }),\n });\n }\n return placed;\n}\n\n/**\n * The view a saved session reopens with: what its transcript and research log\n * kept, in the order it happened — messages, notices and the compaction\n * summary, plan markers, tool calls with each subagent's calls nested under\n * it, and the token line. Reasoning and streamed text were never saved, so a\n * reload has none.\n *\n * The research log interleaves with the transcript by time. Entries older than\n * a compaction's first kept message went with the turns it condensed.\n */\nexport function fromTranscript(\n conversationHistory: readonly ConversationMessage[] | undefined,\n researchLog: readonly ResearchLogEntry[] | undefined,\n plannerUsage?: PlannerUsage,\n): ConversationView {\n const history = conversationHistory ?? [];\n const compacted = history[0]?.kind === 'compaction' ? history[0] : undefined;\n const kept = compacted ? history.slice(1) : history;\n const cutoff = compacted ? (kept[0]?.timestamp ?? compacted.timestamp) : '';\n\n const placed: Placed[] = [\n ...kept.map((entry) => ({ at: entry.timestamp, rank: entry.role === 'user' && !entry.kind ? USER : SETTLED, build: fromEntry(entry) })),\n ...research(researchLog ?? []).filter((item) => item.at >= cutoff),\n ];\n placed.sort((a, b) => (a.at < b.at ? -1 : a.at > b.at ? 1 : a.rank - b.rank));\n // The summary stands where the condensed turns were, ahead of everything kept.\n if (compacted) placed.unshift({ at: '', rank: SETTLED, build: fromEntry(compacted) });\n\n let nextId = 1;\n const id = () => `b${nextId++}`;\n const blocks = placed.map((item) => item.build(id));\n\n if (plannerUsage && isMeasured(plannerUsage.totals)) blocks.push(usageBlock(id(), usageLine(plannerUsage)));\n\n const transcriptAt = history.reduce<string | undefined>((latest, e) => (latest === undefined || e.timestamp > latest ? e.timestamp : latest), undefined);\n return { blocks, nextId, ...(transcriptAt ? { transcriptAt } : {}) };\n}\n","/**\n * Prompts the user sent while a planner turn was in flight, waiting to be the\n * next turn's input. The hold is a plain immutable list, oldest first, so a\n * pure reducer can keep it in state and a host can keep it in a field — and\n * either can draw it as is.\n */\nexport type PromptHold = readonly string[];\n\nexport const EMPTY_HOLD: PromptHold = [];\n\n/** One prompt taken out of the hold, and what is left behind. */\nexport interface TakenPrompt {\n text: string;\n rest: PromptHold;\n}\n\nexport function holdPrompt(hold: PromptHold, text: string): PromptHold {\n return [...hold, text];\n}\n\n/** The prompt a settled turn sends next: the oldest, so they go in the order typed. */\nexport function drainNext(hold: PromptHold): TakenPrompt | undefined {\n const [text, ...rest] = hold;\n return text === undefined ? undefined : { text, rest };\n}\n\n/**\n * Takes back the newest prompt — the one the user most likely just regretted —\n * while the older ones stay queued behind the turn.\n */\nexport function unsendLatest(hold: PromptHold): TakenPrompt | undefined {\n if (hold.length === 0) return undefined;\n return { text: hold[hold.length - 1], rest: hold.slice(0, -1) };\n}\n\n/**\n * A stopped turn takes its queue with it: the prompts were written against a\n * turn that no longer exists, so they come back as one draft to edit and\n * resend rather than firing off after the stop.\n */\nexport function unsendAll(hold: PromptHold): TakenPrompt | undefined {\n if (hold.length === 0) return undefined;\n return { text: hold.join('\\n'), rest: EMPTY_HOLD };\n}\n\n/** Where an unsent prompt lands in the drafting input: above what is being typed, never replacing it. */\nexport function aheadOfDraft(text: string, draft: string): string {\n return draft ? `${text}\\n${draft}` : text;\n}\n","import type { SessionMessage } from '../services/SessionMessage';\nimport { reduceConversation, type ConversationInput, type ConversationView } from './reduce';\n\n/**\n * The planner turn a surface has open, and the one its user stopped. Kept\n * beside the view rather than in it: the view is what a turn drew, this is\n * whether the surface still wants to hear from it.\n */\nexport interface TurnGate {\n readonly open: string | null;\n readonly stopped: string | null;\n}\n\nexport const NO_TURN: TurnGate = { open: null, stopped: null };\n\nexport interface GatedConversation {\n view: ConversationView;\n gate: TurnGate;\n}\n\n/** What a turn streams — everything a stop cuts off. Usage, approvals and transcript markers are facts and still land. */\nconst TURN_STREAM = new Set<SessionMessage['type']>([\n 'planner_text_delta', 'planner_thinking_delta', 'planner_text_retracted', 'planner_message', 'plan_token',\n 'research_step', 'research_step_done', 'subagent_started', 'subagent_finished', 'planner_turn_ended',\n]);\n\n// A message with no turn id while a stop stands can only be the stopped\n// turn's: a new turn announces itself first, and that lifts the stop.\nfunction cutOff(gate: TurnGate, input: ConversationInput): boolean {\n if (gate.stopped === null || !TURN_STREAM.has(input.type as SessionMessage['type'])) return false;\n const turnId = 'turnId' in input ? input.turnId : undefined;\n return turnId === undefined || turnId === gate.stopped;\n}\n\nfunction withGate(live: GatedConversation, gate: TurnGate): GatedConversation {\n return gate.open === live.gate.open && gate.stopped === live.gate.stopped ? live : { ...live, gate };\n}\n\n/**\n * One input into the view, the stop rule applied: a turn the user stopped is\n * over on screen, and whatever it streams until the backend notices is\n * dropped. An input that changes nothing hands back the same view and gate.\n */\nexport function followTurn(view: ConversationView, gate: TurnGate, input: ConversationInput): GatedConversation {\n const live = { view, gate };\n if (input.type === 'planner_turn_started') {\n return withGate({ view: reduceConversation(view, input), gate }, { open: input.turnId, stopped: null });\n }\n if (cutOff(gate, input)) {\n return input.type === 'planner_turn_ended' ? withGate(live, { ...gate, stopped: null }) : live;\n }\n const next = { view: reduceConversation(view, input), gate };\n return input.type === 'planner_turn_ended' && input.turnId === gate.open ? withGate(next, { ...gate, open: null }) : next;\n}\n\n/**\n * The user stopped the planner. The turn ends on screen now rather than when\n * the backend notices the abort, so the surface is free at once and the\n * turn's late output never lands. Nothing changes when no turn is open.\n */\nexport function stopTurn(view: ConversationView, gate: TurnGate): GatedConversation {\n if (gate.open === null) return { view, gate };\n return {\n view: reduceConversation(view, { type: 'planner_turn_ended', turnId: gate.open, outcome: 'stopped' }),\n gate: { open: null, stopped: gate.open },\n };\n}\n","import type { DisplayBlock } from './blocks';\n\n/**\n * Whether the conversation holds anything the collapsed view hides — thinking,\n * a command row's arguments and output, a subagent's children and digest. The\n * detail toggle is the only expansion control (ADR-0017, X1), so a surface\n * offers it only when it would do something.\n */\nexport function hasHiddenDetail(blocks: readonly DisplayBlock[]): boolean {\n return blocks.some((b) => b.type === 'thinking' || b.type === 'tool' || b.type === 'subagent');\n}\n","/** The conversation line for a task starting, worded the same on every surface. */\nexport function taskStartedNotice(title: string, runner?: string): string {\n return runner ? `Started \"${title}\" · ${runner}` : `Started \"${title}\"`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAA6B;AAkM7B,SAAS,YAAY,OAA8B;AACjD,QAAM,OAAO,IAAI,IAAI,MAAM,IAAI,CAAC,MAAM,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;AAChD,QAAM,WAAW,oBAAI,IAAY;AACjC,QAAM,OAAO,oBAAI,IAAY;AAC7B,QAAM,QAAQ,CAAC,OAA8B;AAC3C,QAAI,KAAK,IAAI,EAAE,EAAG,QAAO;AACzB,QAAI,SAAS,IAAI,EAAE,EAAG,QAAO;AAC7B,aAAS,IAAI,EAAE;AACf,eAAW,OAAO,KAAK,IAAI,EAAE,GAAG,gBAAgB,CAAC,GAAG;AAClD,YAAM,MAAM,MAAM,GAAG;AACrB,UAAI,IAAK,QAAO;AAAA,IAClB;AACA,aAAS,OAAO,EAAE;AAClB,SAAK,IAAI,EAAE;AACX,WAAO;A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K;AAC5C,SAAO;AAAA,IACL,MAAM,mBAAmB,MAAM,EAAE,MAAM,sBAAsB,QAAQ,KAAK,MAAM,SAAS,UAAU,CAAC;AAAA,IACpG,MAAM,EAAE,MAAM,MAAM,SAAS,KAAK,KAAK;AAAA,EACzC;AACF;;;AC1DO,SAAS,gBAAgB,QAA0C;AACxE,SAAO,OAAO,KAAK,CAAC,MAAM,EAAE,SAAS,cAAc,EAAE,SAAS,UAAU,EAAE,SAAS,UAAU;AAC/F;;;ACTO,SAAS,kBAAkB,OAAe,QAAyB;AACxE,SAAO,SAAS,YAAY,KAAK,UAAO,MAAM,KAAK,YAAY,KAAK;AACtE;","names":["text","oneLine","text","text","text","text"]}
|
|
1
|
+
{"version":3,"sources":["../src/plan-utils.ts","../src/services/TaskOps.ts","../src/services/researchStepSummary.ts","../src/services/SessionMessage.ts","../src/services/conflictFiles.ts","../src/models/Usage.ts","../src/services/terminalRender.ts","../src/conversation/format.ts","../src/conversation/records.ts","../src/conversation/lanes.ts","../src/conversation/reduce.ts","../src/conversation/transcript.ts","../src/services/harness/agentTools.ts","../src/conversation/taskLog.ts","../src/conversation/promptHold.ts","../src/conversation/turnGate.ts","../src/conversation/detail.ts","../src/conversation/notices.ts"],"sourcesContent":["/**\n * Browser-safe entry point: pure plan-validation utilities with no Node or\n * AI-service dependencies. UI surfaces (the VS Code webview runs in a browser)\n * import runtime values from here instead of the main barrel, which bundles the\n * Gemini/OpenAI services and their `fs`/`child_process` imports.\n */\nexport { canMergeTasks, canSplitTask, canSetDependencies, dependencyCandidates, dependentsOf } from './services/TaskOps';\nexport type { TaskRef } from './services/TaskOps';\nexport { summarizeToolCall } from './services/researchStepSummary';\nexport { truncateCheckpointSummary, CHECKPOINT_TRUNCATE_LENGTH } from './services/SessionMessage';\nexport { capConflictFiles } from './services/conflictFiles';\n// The shared conversation view (#51) is pure too, so every surface — a webview\n// included — draws from the same reducer.\nexport * from './conversation';\n","import { v4 as uuidv4 } from 'uuid';\nimport {\n Task, RunnerId, flattenTasks,\n addTaskToPlan, removeTaskFromPlan, updateTaskInPlan, renumberTasks,\n createTask,\n} from '../models/Task';\nimport { extractObjectsWithKey, stripTrailingCommas, escapeControlCharsInStrings, PlanParseError, TASK_OPS_ENVELOPE_KEY } from './JsonExtractor';\nimport { validateTaskEdit, checkModelAndModeValidity, type EditCatalog } from './TaskEditValidator';\n\n/**\n * Targeted task edits emitted by the planner conversation (the 'task_ops'\n * ConversationTurn). Task references accept a task id, a \"#<order>\" ref, or a\n * bare order number — cheap models rarely echo UUIDs correctly.\n */\nexport type TaskOp =\n | { op: 'update'; taskId: string; changes: Partial<Task> }\n | { op: 'add'; task: Partial<Task>; handle?: string }\n | { op: 'remove'; taskId: string }\n | { op: 'reorder'; taskIds: string[] }\n | { op: 'merge'; taskIds: string[]; merged: Partial<Task>; handle?: string }\n | { op: 'split'; taskId: string; parts: Partial<Task>[]; handle?: string }\n | { op: 'rearm'; taskId: string; changes?: Partial<Task> };\n\n/** Fields the planner may change on an existing task. Everything else (id, status, verdict…) is system-owned. */\nexport const UPDATABLE_FIELDS: (keyof Task)[] = [\n 'title', 'description', 'prompt', 'dependencies', 'assignedRunner', 'assignedModel', 'taskMode', 'thinkingEffort', 'type', 'userSteps', 'autonomy', 'sliceType',\n];\n\n/**\n * The edit protocol as the planner reads it, kept beside the applier so the\n * prose can never undersell (or oversell) what {@link applyTaskOps} accepts.\n * `executionNote` rides on the ordering rule because that is the rule a\n * running execution changes.\n */\nexport function taskOpsProtocol(executionNote = ''): string[] {\n const updateChanges = UPDATABLE_FIELDS\n .map((f) => (f === 'dependencies' ? '\"dependencies\"?:[\"<id or #order>\"]' : `\"${f}\"?`))\n .join(',');\n return [\n '- To modify specific tasks, reply with ONLY this JSON object:',\n ' {\"taskOps\": [',\n ` {\"op\":\"update\",\"taskId\":\"<id or #order>\",\"changes\":{${updateChanges}}},`,\n ' {\"op\":\"add\",\"task\":{\"title\",\"description\",\"prompt\",\"dependencies\":[\"<id or #order>\"],\"assignedRunner\"?,\"assignedModel\"?},\"handle\"?:\"<name>\"},',\n ' {\"op\":\"remove\",\"taskId\":\"<id or #order>\"},',\n ' {\"op\":\"reorder\",\"taskIds\":[\"<id or #order>\", \"... every task exactly once\"]}, // only to re-prioritise INDEPENDENT tasks',\n ' {\"op\":\"merge\",\"taskIds\":[\"<id or #order>\", \"...\"],\"merged\":{\"title\",\"description\",\"prompt\"?,\"assignedRunner\"?,\"assignedModel\"?,...},\"handle\"?:\"<name>\"},',\n ' {\"op\":\"split\",\"taskId\":\"<id or #order>\",\"parts\":[{\"title\",\"description\",\"prompt\"?,\"assignedRunner\"?,\"assignedModel\"?,...}, ...],\"handle\"?:\"<name>\"},',\n ` {\"op\":\"rearm\",\"taskId\":\"<id or #order>\",\"changes\"?:{${updateChanges}}}`,\n ' ]}',\n '- For sweeping changes, you may instead emit a full {\"tasks\":[...]} plan JSON.',\n 'Every \"<id or #order>\" ref in a batch resolves against the plan shown above, before any op in the batch runs — an earlier remove/merge/split never shifts what a later \"#N\" means.',\n 'Give \"add\", \"merge\", or \"split\" a \"handle\" (any name you choose, unused elsewhere in this batch) to let a LATER op in the same batch reference the task it produces — for \"split\", the handle names its last part. A handle used before its defining op is rejected.',\n 'When creating or changing tasks, set \"assignedRunner\" and \"assignedModel\" ({\"modelId\",\"modelLabel\",\"thinkingEffort\"?}) using only the runners and models listed in <available_models>.',\n 'Keep dependencies consistent: no cycles, no references to removed tasks, and never touch running or completed tasks — \"rearm\" is the one exception, below.',\n 'Just declare the dependencies you want — display order is repaired for you afterwards, so a rewire or a newly added prerequisite never needs a \"reorder\" op. Only a task that is running or completed cannot be shifted, so an edit that would need one to move is rejected.' + executionNote,\n 'Flipping \"type\" between \"ai\" and \"user\" is a content change, not just a label: an update to \"user\" needs \"userSteps\" in the SAME op, and an update to \"ai\" needs \"prompt\" in the SAME op — the model/mode/effort/autonomy fields (flipping to \"user\") or the userSteps (flipping to \"ai\") are cleared automatically.',\n '\"rearm\" puts a failed OR completed task back to pending — verdict and output summary are cleared, any dependents it had blocked are released, and it may carry field changes (e.g. a corrected \"prompt\") applied in the same op. A running task cannot be re-armed. Never rearm a task just to relabel it — only when it should actually run again.',\n ];\n}\n\nexport function textHasTaskOps(text: string): boolean {\n return text.includes(`\"${TASK_OPS_ENVELOPE_KEY}\"`);\n}\n\n/** Parse a `{\"taskOps\":[...]}` reply. Throws PlanParseError when the JSON is unusable. */\nexport function parseTaskOpsJson(text: string): TaskOp[] {\n // Extract by the taskOps key specifically — a reply may also carry a\n // tasks-keyed object (schema echo, example), which must not shadow the ops.\n // Candidates are tried back to front: the last emission is the final answer.\n const { matches, fallback } = extractObjectsWithKey(text, TASK_OPS_ENVELOPE_KEY);\n const candidates = matches.length > 0 ? [...matches].reverse() : [fallback.json];\n\n let lastError: PlanParseError | null = null;\n for (const json of candidates) {\n try {\n return parseTaskOpsObject(json, text);\n } catch (err) {\n if (!(err instanceof PlanParseError)) throw err;\n if (!lastError) lastError = err;\n }\n }\n throw lastError ?? new PlanParseError('No JSON object found in task-ops reply', text);\n}\n\nfunction parseTaskOpsObject(json: string, text: string): TaskOp[] {\n let parsed: unknown;\n try {\n parsed = JSON.parse(escapeControlCharsInStrings(stripTrailingCommas(json)));\n } catch (err) {\n throw new PlanParseError(`Task-ops JSON is invalid: ${err instanceof Error ? err.message : String(err)}`, text);\n }\n const ops = (parsed as { taskOps?: unknown }).taskOps;\n if (!Array.isArray(ops) || ops.length === 0) {\n throw new PlanParseError('\"taskOps\" must be a non-empty array', text);\n }\n for (const op of ops) {\n if (typeof op !== 'object' || op === null || typeof (op as { op?: unknown }).op !== 'string') {\n throw new PlanParseError('Every task op needs an \"op\" field (update | add | remove | reorder | merge | split)', text);\n }\n }\n return ops as TaskOp[];\n}\n\ntype RefResolution = { id: string } | { error: string };\n\n/**\n * Resolve a task reference (id, \"#3\", \"3\", or a batch handle) to a task id.\n *\n * `#order` and title matches are pinned to `originalScope` — the plan as it\n * stood before the batch began — so an earlier op's renumbering can never\n * make a later op's \"#4\" land on a different task than the planner saw. A\n * literal id has no such drift (ids are stable across mutations), so id\n * matches check `liveScope` — the batch's current, mutated tasks — which is\n * also how a later op reaches a task created earlier in the same batch\n * without a handle (e.g. a split's own chained parts).\n */\nfunction resolveRef(\n ref: unknown,\n originalScope: readonly Task[],\n liveScope: Task[],\n handleOwner: Map<string, number>,\n handleId: Map<string, string>,\n opIndex: number,\n): RefResolution {\n if (typeof ref !== 'string' && typeof ref !== 'number') return { error: `invalid reference \"${String(ref)}\"` };\n const s = String(ref).trim();\n\n const ownerIdx = handleOwner.get(s);\n if (ownerIdx !== undefined) {\n if (ownerIdx >= opIndex) {\n return { error: `handle \"${s}\" is defined later in this batch (op ${ownerIdx + 1}) and cannot be referenced yet` };\n }\n const id = handleId.get(s);\n if (!id) return { error: `handle \"${s}\" was never resolved` };\n return { id };\n }\n\n const byLiveId = liveScope.find((t) => t.id === s);\n if (byLiveId) return { id: byLiveId.id };\n const orderStr = s.startsWith('#') ? s.slice(1) : s;\n if (/^\\d+$/.test(orderStr)) {\n const order = Number(orderStr);\n const byOrder = originalScope.filter((t) => t.order === order);\n if (byOrder.length === 1) return { id: byOrder[0].id };\n }\n const byTitle = originalScope.find((t) => t.title === s);\n if (byTitle) return { id: byTitle.id };\n return { error: `task \"${s}\" not found` };\n}\n\n/**\n * The task references an op carries, before a batch handle resolves; an `add`\n * names none. Used to tell whether a batch reaches a task a runner is running.\n */\nexport function taskOpRefs(op: TaskOp): string[] {\n switch (op.op) {\n case 'update':\n case 'remove':\n case 'split':\n case 'rearm':\n return [op.taskId];\n case 'merge':\n case 'reorder':\n return op.taskIds;\n case 'add':\n return [];\n }\n}\n\n/** Whether a written reference names `task` by id, `#order`, bare order, or title. */\nexport function refMatchesTask(ref: unknown, task: Pick<Task, 'id' | 'order' | 'title'>): boolean {\n if (typeof ref !== 'string' && typeof ref !== 'number') return false;\n const s = String(ref).trim();\n if (task.id === s) return true;\n const orderStr = s.startsWith('#') ? s.slice(1) : s;\n return (/^\\d+$/.test(orderStr) && task.order === Number(orderStr)) || task.title === s;\n}\n\n/** Whether `name` already denotes an existing task by id, `#order`, or title. */\nfunction refCollidesWithExisting(name: string, originalFlatAll: Task[]): boolean {\n if (originalFlatAll.some((t) => t.id === name)) return true;\n const orderStr = name.startsWith('#') ? name.slice(1) : name;\n if (/^\\d+$/.test(orderStr) && originalFlatAll.some((t) => t.order === Number(orderStr))) return true;\n return originalFlatAll.some((t) => t.title === name);\n}\n\nfunction findInTasks(tasks: Task[], id: string): Task | undefined {\n return flattenTasks(tasks).find((t) => t.id === id);\n}\n\nfunction findTopLevel(tasks: Task[], id: string): Task | undefined {\n return tasks.find((t) => t.id === id);\n}\n\nfunction detectCycle(tasks: Task[]): string | null {\n const byId = new Map(tasks.map((t) => [t.id, t]));\n const visiting = new Set<string>();\n const done = new Set<string>();\n const visit = (id: string): string | null => {\n if (done.has(id)) return null;\n if (visiting.has(id)) return id;\n visiting.add(id);\n for (const dep of byId.get(id)?.dependencies ?? []) {\n const hit = visit(dep);\n if (hit) return hit;\n }\n visiting.delete(id);\n done.add(id);\n return null;\n };\n for (const t of tasks) {\n const hit = visit(t.id);\n if (hit) return byId.get(hit)?.title ?? hit;\n }\n return null;\n}\n\n/** A task the repair may not reposition: execution has already reached it. */\nfunction isPinned(t: Task): boolean {\n return t.status === 'in_progress' || t.status === 'completed';\n}\n\n/**\n * Reorder top-level tasks so every dependency comes before its dependents,\n * moving as little as possible: pinned tasks keep the exact slot they are in,\n * and each free slot goes to the earliest-listed movable task whose\n * dependencies are already placed — so tasks the graph does not constrain keep\n * their relative order. Assumes an acyclic graph (the caller checks first).\n */\nfunction repairOrder(tasks: Task[]): { tasks: Task[] } | { error: string } {\n const byId = new Map(tasks.map((t) => [t.id, t]));\n const placed = new Set<string>();\n const movable = tasks.filter((t) => !isPinned(t));\n const result: Task[] = [];\n // Dependencies outside this list (a subtask id) constrain nothing here.\n const blockerOf = (t: Task): Task | undefined => {\n const id = t.dependencies.find((d) => byId.has(d) && !placed.has(d));\n return id ? byId.get(id) : undefined;\n };\n const place = (t: Task) => { result.push(t); placed.add(t.id); };\n\n for (const occupant of tasks) {\n if (isPinned(occupant)) {\n const blocker = blockerOf(occupant);\n if (blocker) return { error: pinnedRefusal(blocker, occupant, 'before') };\n place(occupant);\n continue;\n }\n const next = movable.findIndex((t) => !blockerOf(t));\n if (next < 0) {\n // Nothing is schedulable and the graph is acyclic, so every movable task\n // left waits — directly or through others — on a pinned task further down\n // that would have to come up to free this slot.\n const [stuck, pin] = trailToPin(movable[0], blockerOf);\n return { error: pinnedRefusal(stuck, pin, 'after') };\n }\n place(movable.splice(next, 1)[0]);\n }\n return { tasks: result };\n}\n\n/** Walk the waiting-on chain from `task` down to the pinned task holding it up. */\nfunction trailToPin(task: Task, blockerOf: (t: Task) => Task | undefined): [Task, Task] {\n let at = task;\n for (;;) {\n const blocker = blockerOf(at);\n if (!blocker || isPinned(blocker)) return [at, blocker ?? at];\n at = blocker;\n }\n}\n\n/** `\"B\" #2→#1, \"A\" #1→#3` for every task the repair actually moved, or null. */\nfunction describeMoves(before: Task[], after: Task[]): string | null {\n const wasAt = new Map(before.map((t, i) => [t.id, i + 1]));\n const moved = after\n .map((t, i) => ({ t, from: wasAt.get(t.id)!, to: i + 1 }))\n .filter((m) => m.from !== m.to);\n return moved.length ? moved.map((m) => `\"${m.t.title}\" #${m.from}→#${m.to}`).join(', ') : null;\n}\n\nfunction pinnedRefusal(other: Task, pin: Task, side: 'before' | 'after'): string {\n const word = pin.status === 'in_progress' ? 'running' : 'completed';\n return `\"${other.title}\" would have to run ${side} \"${pin.title}\", which is ${word} and cannot be moved`;\n}\n\nexport interface ApplyTaskOpsResult {\n ok: boolean;\n tasks: readonly Task[];\n errors: string[];\n /** Human-readable summary of what changed, for the chat transcript. */\n summary: string[];\n}\n\n/**\n * Pre-flight check for a merge: would collapsing the selected tasks into one\n * (rewiring every dependent of any selected task to the survivor) keep the\n * dependency graph valid? Rejects locked tasks and merges whose rewiring would\n * introduce a cycle. Display order is NOT checked here — neither\n * consecutiveness (a UI concern) nor a dependency the merge pulls out of order,\n * which {@link applyTaskOps} repairs; refusing on order would make this\n * pre-flight stricter than the applier it stands in for.\n */\nexport function canMergeTasks(tasks: readonly Task[], selectedIds: string[]): { ok: boolean; error?: string } {\n if (selectedIds.length < 2) return { ok: false, error: 'Select at least two tasks to merge' };\n const idSet = new Set(selectedIds);\n const toMerge = tasks.filter((t) => idSet.has(t.id));\n if (toMerge.length < 2) return { ok: false, error: 'At least two valid tasks are required to merge' };\n for (const t of toMerge) {\n if (t.status === 'in_progress') return { ok: false, error: `\"${t.title}\" is running and cannot be merged` };\n if (t.status === 'completed') return { ok: false, error: `\"${t.title}\" is completed and cannot be merged` };\n }\n const ordered = [...toMerge].sort((a, b) => a.order - b.order);\n const survivorId = '__merge_sim__';\n const sim: Task[] = tasks\n .filter((t) => !idSet.has(t.id))\n .map((t) => ({ ...t, dependencies: t.dependencies.map((d) => (idSet.has(d) ? survivorId : d)).filter((d) => d !== t.id) }));\n const unionDeps = [...new Set(toMerge.flatMap((t) => t.dependencies))].filter((d) => !idSet.has(d));\n sim.push({ ...ordered[0], id: survivorId, title: '__merge_sim__', order: ordered[0].order, dependencies: unionDeps });\n const cycle = detectCycle(sim);\n if (cycle) return { ok: false, error: `Merging would create a dependency cycle involving \"${cycle}\"` };\n return { ok: true };\n}\n\n/**\n * The shape the dependency helpers below read. Structural rather than `Task`,\n * because the TUI projects tasks into its own `TaskView`: a surface's\n * dependency picker and the API's validation must agree on which dependencies\n * are legal, and a signature only core can satisfy would have forced the TUI\n * to keep a second copy of the rule.\n */\nexport interface TaskRef {\n id: string;\n order: number;\n title: string;\n dependencies: string[];\n}\n\n/** The tasks listing `taskId` as a dependency — exactly what removing it detaches. */\nexport function dependentsOf<T extends Pick<TaskRef, 'id' | 'dependencies'>>(tasks: T[], taskId: string): T[] {\n return tasks.filter((t) => t.id !== taskId && t.dependencies.includes(taskId));\n}\n\n/**\n * The tasks that may become dependencies of `taskId`: those displayed before it.\n *\n * Offering only earlier tasks is what keeps a hand-edited graph valid without a\n * cycle check — dependencies then only ever point backwards in display order,\n * the same invariant `applyTaskOps` enforces. Omit `taskId` for a task that does\n * not exist yet: it lands last, so every current task is a candidate.\n */\nexport function dependencyCandidates<T extends Pick<TaskRef, 'id' | 'order'>>(tasks: readonly T[], taskId?: string): T[] {\n if (!taskId) return [...tasks];\n const target = tasks.find((t) => t.id === taskId);\n return target ? tasks.filter((t) => t.order < target.order) : [];\n}\n\n/** Pre-flight check for a hand-edited dependency list: every id exists and comes earlier. */\nexport function canSetDependencies<T extends TaskRef>(\n tasks: readonly T[],\n taskId: string,\n dependencies: string[],\n): { ok: boolean; error?: string } {\n const target = tasks.find((t) => t.id === taskId);\n if (!target) return { ok: false, error: 'Task not found' };\n const allowed = new Set(dependencyCandidates(tasks, taskId).map((t) => t.id));\n for (const dep of dependencies) {\n if (dep === taskId) return { ok: false, error: `\"${target.title}\" cannot depend on itself` };\n const found = tasks.find((t) => t.id === dep);\n if (!found) return { ok: false, error: `Unknown dependency \"${dep}\"` };\n if (!allowed.has(dep)) {\n return { ok: false, error: `\"${target.title}\" cannot depend on \"${found.title}\", which comes after it in the plan` };\n }\n }\n return { ok: true };\n}\n\n/** Pre-flight check for a split: the task exists and is not locked by execution. */\nexport function canSplitTask(tasks: readonly Task[], taskId: string): { ok: boolean; error?: string } {\n const target = tasks.find((t) => t.id === taskId);\n if (!target) return { ok: false, error: 'Task not found' };\n if (target.status === 'in_progress') return { ok: false, error: `\"${target.title}\" is running and cannot be split` };\n if (target.status === 'completed') return { ok: false, error: `\"${target.title}\" is completed and cannot be split` };\n return { ok: true };\n}\n\n/**\n * Apply task ops to a snapshot of the plan, atomically: either every op\n * applies and the result validates (deps resolve, no cycles, running and\n * completed tasks untouched), or nothing is returned and `errors` explains\n * why. The caller commits `tasks` on ok.\n */\nexport function applyTaskOps(currentTasks: readonly Task[], ops: TaskOp[], runners: RunnerId[], catalog?: EditCatalog): ApplyTaskOpsResult {\n let tasks: Task[] = JSON.parse(JSON.stringify(currentTasks));\n const errors: string[] = [];\n const summary: string[] = [];\n\n // Frozen snapshot every ref resolves `#order`/title matches against, so an\n // earlier op's renumbering can never shift what a later op's \"#N\" means.\n const originalFlatAll = flattenTasks(currentTasks);\n\n // Batch handles: planner-chosen names for a task an add/merge/split op is\n // about to create, so a later op in the same batch can reference it before\n // it has a real id. Pre-scanned up front — a bad handle refuses the whole\n // batch before anything mutates.\n const handleOwner = new Map<string, number>();\n const handleId = new Map<string, string>();\n for (const [i, op] of ops.entries()) {\n const handle = (op as { handle?: unknown }).handle;\n if (handle === undefined) continue;\n if (typeof handle !== 'string' || !handle.trim()) {\n errors.push(`op ${i + 1} (${op.op}): handle must be a non-empty string`);\n continue;\n }\n if (handleOwner.has(handle)) {\n errors.push(`op ${i + 1} (${op.op}): handle \"${handle}\" is already used earlier in this batch`);\n continue;\n }\n if (refCollidesWithExisting(handle, originalFlatAll)) {\n errors.push(`op ${i + 1} (${op.op}): handle \"${handle}\" collides with an existing task reference`);\n continue;\n }\n handleOwner.set(handle, i);\n }\n if (errors.length) return { ok: false, tasks: currentTasks, errors, summary: [] };\n\n const resolveDeps = (deps: unknown, opIndex: number): { ids: string[]; bad: string[] } => {\n const ids: string[] = [];\n const bad: string[] = [];\n const liveFlat = flattenTasks(tasks);\n for (const d of Array.isArray(deps) ? deps : []) {\n const r = resolveRef(d, originalFlatAll, liveFlat, handleOwner, handleId, opIndex);\n if ('error' in r) { bad.push(`${String(d)} (${r.error})`); continue; }\n if (!liveFlat.some((t) => t.id === r.id)) { bad.push(`${String(d)} (no longer exists in this batch)`); continue; }\n ids.push(r.id);\n }\n return { ids, bad };\n };\n\n for (const [i, op] of ops.entries()) {\n const label = `op ${i + 1} (${op.op})`;\n switch (op.op) {\n case 'update': {\n const ref = resolveRef(op.taskId, originalFlatAll, flattenTasks(tasks), handleOwner, handleId, i);\n if ('error' in ref) { errors.push(`${label}: ${ref.error}`); break; }\n const target = findInTasks(tasks, ref.id);\n if (!target) { errors.push(`${label}: task \"${op.taskId}\" no longer exists in this batch (removed, merged, or split by an earlier op)`); break; }\n const changes: Partial<Task> = {};\n for (const key of UPDATABLE_FIELDS) {\n if (op.changes && key in op.changes) (changes as Record<string, unknown>)[key] = (op.changes as Record<string, unknown>)[key];\n }\n if (changes.assignedRunner && !runners.includes(changes.assignedRunner)) {\n errors.push(`${label}: runner \"${changes.assignedRunner}\" is not in this plan's runner set [${runners.join(', ')}]`);\n break;\n }\n // Dependency refs are still unresolved (e.g. \"#3\") at this point, so\n // this check runs on everything but them; resolveDeps below handles\n // dependency well-formedness once they're real ids.\n const changesForCheck: Partial<Task> = { ...changes };\n delete changesForCheck.dependencies;\n const check = validateTaskEdit('planner', flattenTasks(tasks), target.id, changesForCheck, catalog);\n if (!check.ok) { errors.push(`${label}: ${check.error}`); break; }\n for (const field of check.clear ?? []) (changes as Record<string, unknown>)[field] = undefined;\n if ('dependencies' in changes) {\n const { ids, bad } = resolveDeps(changes.dependencies, i);\n if (bad.length) { errors.push(`${label}: unknown dependencies: ${bad.join(', ')}`); break; }\n changes.dependencies = ids.filter((id) => id !== target.id);\n }\n if (Object.keys(changes).length === 0) { errors.push(`${label}: no valid changes provided`); break; }\n tasks = updateTaskInPlan(tasks, target.id, changes);\n const clearedNote = check.clear?.length ? `; cleared ${check.clear.join(', ')}` : '';\n summary.push(`Updated \"${target.title}\" (${Object.keys(changes).join(', ')})${clearedNote}`);\n break;\n }\n case 'add': {\n const spec = op.task ?? {};\n if (!spec.title) { errors.push(`${label}: new task needs a title`); break; }\n const { ids, bad } = resolveDeps(spec.dependencies, i);\n if (bad.length) { errors.push(`${label}: unknown dependencies: ${bad.join(', ')}`); break; }\n const runner = spec.assignedRunner && runners.includes(spec.assignedRunner) ? spec.assignedRunner : runners[0];\n const validity = checkModelAndModeValidity(runner, spec.assignedModel, spec.taskMode, catalog);\n if (!validity.ok) { errors.push(`${label}: ${validity.error}`); break; }\n const newId = uuidv4();\n tasks = addTaskToPlan(tasks, {\n ...spec,\n id: newId,\n dependencies: ids,\n assignedRunner: runner,\n description: spec.description ?? spec.title,\n prompt: spec.prompt ?? spec.description ?? spec.title,\n });\n if (op.handle) handleId.set(op.handle, newId);\n summary.push(`Added \"${spec.title}\"`);\n break;\n }\n case 'remove': {\n const ref = resolveRef(op.taskId, originalFlatAll, flattenTasks(tasks), handleOwner, handleId, i);\n if ('error' in ref) { errors.push(`${label}: ${ref.error}`); break; }\n const target = findInTasks(tasks, ref.id);\n if (!target) { errors.push(`${label}: task \"${op.taskId}\" no longer exists in this batch (removed, merged, or split by an earlier op)`); break; }\n if (target.status === 'in_progress') { errors.push(`${label}: \"${target.title}\" is running and cannot be removed`); break; }\n if (target.status === 'completed') { errors.push(`${label}: \"${target.title}\" is completed and cannot be removed`); break; }\n tasks = removeTaskFromPlan(tasks, target.id);\n summary.push(`Removed \"${target.title}\"`);\n break;\n }\n case 'reorder': {\n const refs = (op.taskIds ?? []).map((r) => resolveRef(r, currentTasks, tasks, handleOwner, handleId, i));\n const badRef = refs.find((r): r is { error: string } => 'error' in r);\n if (badRef) { errors.push(`${label}: ${badRef.error}`); break; }\n const resolved = (refs as { id: string }[]).map((r) => findTopLevel(tasks, r.id));\n if (resolved.some((t) => !t)) { errors.push(`${label}: unknown task in reorder list`); break; }\n const orderIds = resolved.map((t) => t!.id);\n if (new Set(orderIds).size !== tasks.length) {\n errors.push(`${label}: reorder must list every top-level task exactly once (${tasks.length} tasks)`);\n break;\n }\n const byId = new Map(tasks.map((t) => [t.id, t]));\n tasks = renumberTasks(orderIds.map((id) => byId.get(id)!));\n summary.push('Reordered tasks');\n break;\n }\n case 'merge': {\n const refs = (op.taskIds ?? []).map((r) => resolveRef(r, currentTasks, tasks, handleOwner, handleId, i));\n const badRef = refs.find((r): r is { error: string } => 'error' in r);\n if (badRef) { errors.push(`${label}: ${badRef.error}`); break; }\n const resolved = (refs as { id: string }[]).map((r) => findTopLevel(tasks, r.id));\n if (resolved.some((t) => !t)) { errors.push(`${label}: unknown task in merge list (already removed, merged, or split by an earlier op)`); break; }\n const toMerge = resolved.map((t) => t!).sort((a, b) => a.order - b.order);\n if (toMerge.length < 2) { errors.push(`${label}: merge needs at least two tasks`); break; }\n const locked = toMerge.find((t) => t.status === 'in_progress' || t.status === 'completed');\n if (locked) {\n errors.push(`${label}: \"${locked.title}\" is ${locked.status === 'in_progress' ? 'running' : 'completed'} and cannot be merged`);\n break;\n }\n const idSet = new Set(toMerge.map((t) => t.id));\n const unionDepsRaw = [...new Set(toMerge.flatMap((t) => t.dependencies))].filter((d) => !idSet.has(d));\n const { ids, bad } = resolveDeps(unionDepsRaw, i);\n if (bad.length) { errors.push(`${label}: unknown dependencies: ${bad.join(', ')}`); break; }\n const spec = op.merged ?? {};\n if (!spec.title) { errors.push(`${label}: merged task needs a title`); break; }\n const runner = spec.assignedRunner && runners.includes(spec.assignedRunner) ? spec.assignedRunner : toMerge[0].assignedRunner;\n const mergedTask = createTask({\n title: spec.title,\n description: spec.description ?? toMerge.map((t) => t.description).filter(Boolean).join('\\n\\n'),\n prompt: spec.prompt ?? (toMerge.map((t) => t.prompt).filter(Boolean).join('\\n\\n---\\n\\n') || undefined),\n type: spec.type ?? (toMerge.some((t) => t.type === 'user') ? 'user' : 'ai'),\n dependencies: ids,\n assignedRunner: runner,\n assignedModel: spec.assignedModel ?? toMerge.find((t) => t.assignedModel)?.assignedModel,\n thinkingEffort: spec.thinkingEffort ?? toMerge.find((t) => t.thinkingEffort)?.thinkingEffort,\n taskMode: spec.taskMode ?? toMerge[0].taskMode,\n autonomy: spec.autonomy ?? toMerge.find((t) => t.autonomy)?.autonomy,\n sliceType: spec.sliceType ?? toMerge.find((t) => t.sliceType)?.sliceType,\n userStoriesCovered: spec.userStoriesCovered ?? (toMerge.flatMap((t) => t.userStoriesCovered ?? []).length\n ? [...new Set(toMerge.flatMap((t) => t.userStoriesCovered ?? []))]\n : undefined),\n });\n const mergedId = mergedTask.id;\n const firstId = toMerge[0].id;\n const result: Task[] = [];\n let inserted = false;\n for (const t of tasks) {\n if (idSet.has(t.id)) {\n if (!inserted && t.id === firstId) { result.push(mergedTask); inserted = true; }\n continue;\n }\n result.push({ ...t, dependencies: t.dependencies.map((d) => (idSet.has(d) ? mergedId : d)) });\n }\n if (!inserted) result.push(mergedTask);\n tasks = renumberTasks(result);\n if (op.handle) handleId.set(op.handle, mergedId);\n summary.push(`Merged ${toMerge.length} tasks into \"${spec.title}\"`);\n break;\n }\n case 'split': {\n const ref = resolveRef(op.taskId, currentTasks, tasks, handleOwner, handleId, i);\n if ('error' in ref) { errors.push(`${label}: ${ref.error}`); break; }\n const target = findTopLevel(tasks, ref.id);\n if (!target) { errors.push(`${label}: task \"${op.taskId}\" not found`); break; }\n if (target.status === 'in_progress') { errors.push(`${label}: \"${target.title}\" is running and cannot be split`); break; }\n if (target.status === 'completed') { errors.push(`${label}: \"${target.title}\" is completed and cannot be split`); break; }\n const parts = op.parts ?? [];\n if (parts.length < 2) { errors.push(`${label}: split needs at least two parts`); break; }\n const newTasks: Task[] = [];\n let prevId: string | undefined;\n let buildError = false;\n for (let p = 0; p < parts.length; p++) {\n const spec = parts[p];\n if (!spec || !spec.title) { errors.push(`${label}: split part ${p + 1} needs a title`); buildError = true; break; }\n // Part 0 inherits the original's (external) dependencies, resolved\n // against the current plan. Later parts chain onto the previous part\n // — a sibling id created in this same op, so it needs no resolution.\n let deps: string[];\n if (p === 0) {\n const { ids, bad } = resolveDeps(target.dependencies, i);\n if (bad.length) { errors.push(`${label}: split part 1 unknown dependencies: ${bad.join(', ')}`); buildError = true; break; }\n deps = ids;\n } else {\n deps = prevId ? [prevId] : [];\n }\n const runner = spec.assignedRunner && runners.includes(spec.assignedRunner) ? spec.assignedRunner : target.assignedRunner;\n const nt = createTask({\n title: spec.title,\n description: spec.description ?? spec.title,\n prompt: spec.prompt ?? spec.description ?? spec.title,\n type: spec.type ?? target.type,\n dependencies: deps,\n assignedRunner: runner,\n assignedModel: spec.assignedModel ?? target.assignedModel,\n taskMode: spec.taskMode ?? target.taskMode,\n autonomy: spec.autonomy ?? target.autonomy,\n sliceType: spec.sliceType ?? target.sliceType,\n userStoriesCovered: spec.userStoriesCovered ?? target.userStoriesCovered,\n });\n newTasks.push(nt);\n prevId = nt.id;\n }\n if (buildError) break;\n const tailId = newTasks[newTasks.length - 1].id;\n const result: Task[] = [];\n for (const t of tasks) {\n if (t.id === target.id) { result.push(...newTasks); continue; }\n result.push({ ...t, dependencies: t.dependencies.map((d) => (d === target.id ? tailId : d)) });\n }\n tasks = renumberTasks(result);\n if (op.handle) handleId.set(op.handle, tailId);\n summary.push(`Split \"${target.title}\" into ${newTasks.length} tasks`);\n break;\n }\n case 'rearm': {\n const ref = resolveRef(op.taskId, originalFlatAll, flattenTasks(tasks), handleOwner, handleId, i);\n if ('error' in ref) { errors.push(`${label}: ${ref.error}`); break; }\n const target = findInTasks(tasks, ref.id);\n if (!target) { errors.push(`${label}: task \"${op.taskId}\" no longer exists in this batch (removed, merged, or split by an earlier op)`); break; }\n if (target.status === 'in_progress') { errors.push(`${label}: \"${target.title}\" is running and cannot be re-armed`); break; }\n const changes: Partial<Task> = {};\n for (const key of UPDATABLE_FIELDS) {\n if (op.changes && key in op.changes) (changes as Record<string, unknown>)[key] = (op.changes as Record<string, unknown>)[key];\n }\n if (changes.assignedRunner && !runners.includes(changes.assignedRunner)) {\n errors.push(`${label}: runner \"${changes.assignedRunner}\" is not in this plan's runner set [${runners.join(', ')}]`);\n break;\n }\n // Validated as a direct edit (not 'planner'): re-arming is the sanctioned\n // exception to the planner lock — its whole point is to touch a task the\n // lock would otherwise refuse. Well-formedness (type coherence, model/mode\n // validity, dependency shape) still applies, same as every other edit.\n const changesForCheck: Partial<Task> = { ...changes };\n delete changesForCheck.dependencies;\n const check = validateTaskEdit('direct', flattenTasks(tasks), target.id, changesForCheck, catalog);\n if (!check.ok) { errors.push(`${label}: ${check.error}`); break; }\n for (const field of check.clear ?? []) (changes as Record<string, unknown>)[field] = undefined;\n if ('dependencies' in changes) {\n const { ids, bad } = resolveDeps(changes.dependencies, i);\n if (bad.length) { errors.push(`${label}: unknown dependencies: ${bad.join(', ')}`); break; }\n changes.dependencies = ids.filter((id) => id !== target.id);\n }\n tasks = updateTaskInPlan(tasks, target.id, { ...changes, status: 'pending', verdict: undefined, outputSummary: undefined, awaitingReason: undefined });\n // Dependents parked at 'blocked' by this task's earlier failure have\n // nothing else to release them — the scheduler reads `status` directly,\n // not a live recomputation of the dependency graph.\n for (const dep of flattenTasks(tasks)) {\n if (dep.status === 'blocked' && dep.dependencies.includes(target.id)) {\n tasks = updateTaskInPlan(tasks, dep.id, { status: 'pending' });\n }\n }\n const clearedNote = check.clear?.length ? `; cleared ${check.clear.join(', ')}` : '';\n const changedNote = Object.keys(changes).length ? ` (${Object.keys(changes).join(', ')})` : '';\n summary.push(`Re-armed \"${target.title}\"${changedNote}${clearedNote}`);\n break;\n }\n default:\n errors.push(`${label}: unknown op \"${(op as { op: string }).op}\"`);\n }\n if (errors.length) break;\n }\n\n if (!errors.length) {\n const flat = flattenTasks(tasks);\n const idSet = new Set(flat.map((t) => t.id));\n for (const t of flat) {\n for (const dep of t.dependencies) {\n if (!idSet.has(dep)) errors.push(`\"${t.title}\" depends on a task that no longer exists (${dep})`);\n }\n }\n const cycleAt = detectCycle(flat);\n if (cycleAt) errors.push(`Dependency cycle detected involving \"${cycleAt}\"`);\n // Display order is maintained, not demanded: whatever the batch produced,\n // the plan comes back with dependencies ahead of their dependents. The\n // planner never has to re-declare the whole order to move one task.\n if (!errors.length) {\n const repaired = repairOrder(tasks);\n if ('error' in repaired) {\n errors.push(repaired.error);\n } else {\n const moves = describeMoves(tasks, repaired.tasks);\n if (moves) summary.push(`Reordered to keep dependencies first: ${moves}`);\n tasks = renumberTasks(repaired.tasks);\n }\n }\n }\n\n return errors.length\n ? { ok: false, tasks: currentTasks, errors, summary: [] }\n : { ok: true, tasks, errors: [], summary };\n}\n","import type { ResearchStepOutcome } from '../models/Task';\n\n/**\n * One-line human summary of a research tool call, shared by every surface that\n * renders live planner progress (VS Code webview, CLI, TUI). Kept in core so\n * the arg-shape heuristics (file path vs. pattern vs. shell command) live in\n * one place instead of being re-derived per surface — which also means a new\n * tool becomes readable in all three UIs by being handled here once.\n */\n\nfunction ellipsize(text: string, max: number): string {\n const oneLine = text.replace(/\\s+/g, ' ').trim();\n return oneLine.length > max ? `${oneLine.slice(0, max)}…` : oneLine;\n}\n\n/**\n * @param toolLabel The agent's own name for the tool, when a harness planner\n * produced the call (ADR-0009). It replaces the member name in the summary, so\n * the timeline says `Edit` rather than the catch-all `agent_tool` — and says\n * `WebFetch` rather than claiming a shell command ran.\n */\nexport function summarizeToolCall(tool: string, argsJson: string, toolLabel?: string): string {\n const display = toolLabel?.trim() || tool;\n if (tool === 'agent_tool') {\n // No Ordewell equivalent, so no arg shape to rely on: show the agent's name\n // plus whichever conventional field the call happens to carry.\n try {\n const args = JSON.parse(argsJson);\n const hint = args.path ?? args.file_path ?? args.filePath ?? args.pattern ?? args.query ?? args.url ?? args.command;\n if (hint) return `${display} ${ellipsize(String(hint), 50)}`;\n } catch {\n // fall through to the bare name\n }\n return display;\n }\n\n if (tool === 'spawn_research_agent') {\n try {\n const args = JSON.parse(argsJson);\n const prompt = typeof args.prompt === 'string' ? args.prompt : '';\n const oneLine = prompt.replace(/\\s+/g, ' ').trim();\n return `spawn: \"${oneLine.slice(0, 60)}${oneLine.length > 60 ? '…' : ''}\"`;\n } catch {\n return 'spawn_research_agent';\n }\n }\n\n try {\n const args = JSON.parse(argsJson);\n\n // Ordered by specificity: a tool's defining argument wins over the generic\n // `path`, which several of the newer tools also accept as a search root.\n if (tool === 'find_symbol' && args.symbol) {\n return `find_symbol ${args.symbol}${args.language ? ` [${args.language}]` : ''}`;\n }\n if (tool === 'web_search' && args.query) {\n return `web_search \"${ellipsize(String(args.query), 50)}\"`;\n }\n if (tool === 'fetch' && args.url) {\n return `fetch ${ellipsize(String(args.url), 60)}`;\n }\n // The pattern is what a glob is about; the generic `path` below is only its\n // search root, and reporting that read as \"glob src\" for every call.\n if (tool === 'glob' && args.pattern) {\n return `glob ${ellipsize(String(args.pattern), 50)}`;\n }\n if (tool === 'grep' && args.pattern) {\n const mode = args.output_mode && args.output_mode !== 'content' ? ` (${args.output_mode})` : '';\n const scope = args.include ? ` in ${args.include}` : '';\n return `grep ${ellipsize(String(args.pattern), 40)}${scope}${mode}`;\n }\n\n const path = args.filePath || args.file_path || args.path;\n if (path) {\n const basename = String(path).split('/').pop() || String(path);\n return `${display} ${basename}`;\n }\n if (args.pattern) {\n return `${display} ${args.pattern}`;\n }\n if (args.command) {\n return `bash: ${String(args.command).slice(0, 40)}`;\n }\n } catch {\n // fall through to the bare tool name below\n }\n return display;\n}\n\n/**\n * Classify a tool outcome from its result text and success flag, so a surface\n * can render a refused `rm` or a denied `npm test` distinctly from a\n * successful run without re-deriving the refusal signatures per surface. The\n * refusal/denial strings come from `commandPolicy.ts`, `BaseFileSystem.ts`,\n * and the research-subagent wrapper — keeping the matching here means the\n * signatures stay in one place alongside the human summary.\n */\nexport function classifyOutcome(success: boolean, output: string): ResearchStepOutcome {\n if (!success) {\n if (/^Command refused:/.test(output) || /is not available\\. You are a read-only research subagent/.test(output)) return 'refused';\n if (/not approved/i.test(output) || /^Access denied:/.test(output) || /needs user approval/i.test(output)) return 'denied';\n return 'failure';\n }\n return 'success';\n}\n","import type { AwaitingReason, LegacyPlanState, QueuedMessage, ResearchStep, RunnerId, SubagentOutcome, Task, TaskTransport, Verdict } from '../models/Task';\nimport type { UsageTotals } from '../models/Usage';\nimport type { TaskLogEvent } from '../models/TaskLog';\nimport type { ApprovalKind } from '../interfaces/IApproval';\nimport type { ApprovalSource } from './ApprovalPolicy';\nimport type { IsolationHandoff, IsolationMergeResult, TaskIsolation } from '../interfaces/IWorktreeIsolation';\nimport type { QueuedTaskMessage } from '../interfaces/ITerminalRunner';\nimport { canContinue } from './continuation';\n\nexport type SerializedTaskStatus = {\n id: string;\n status: string;\n verdict: { outcome: 'pass' | 'fail'; reason: string; checks: Verdict['checks'] } | null;\n /** Advisory silence timestamp from VerdictEngine — not part of task status semantics. */\n idleSince?: string | null;\n /** Absent unless the plan has an isolation run, so a shared-root plan's updates are unchanged. */\n isolation?: TaskIsolation;\n /** Absent unless the task's plan asked for the structured transport (ADR-0018): what it ran on, or why it fell back. */\n transport?: Pick<TaskTransport, 'kind' | 'fallback'>;\n /** What an `awaiting_user` task waits on, when it was saved (ADR-0018, W1). */\n awaitingReason?: AwaitingReason;\n /** Messages waiting for a structured task's turn to end, oldest first; absent when there are none. */\n queued?: QueuedTaskMessage[];\n /** Set when the task can be continued in its saved runner session (ADR-0018, K1); the id itself stays in the daemon. */\n continuable?: true;\n /**\n * How many of a structured task's runner requests wait for an answer\n * (ADR-0018, A1) — \"waiting for approval\", which leaves `status` alone.\n * Absent when none do.\n */\n awaitingApproval?: number;\n};\n\nexport type SerializedTask = {\n id: string;\n order: number;\n title: string;\n type: string;\n description: string;\n dependencies: string[];\n assignedRunner: RunnerId;\n assignedModel: Task['assignedModel'] | null;\n taskMode: string;\n prompt: string | null;\n subtasks: SerializedTask[];\n userSteps: Task['userSteps'];\n thinkingEffort: Task['thinkingEffort'];\n autonomy: Task['autonomy'];\n sliceType: Task['sliceType'];\n userStoriesCovered: Task['userStoriesCovered'];\n};\n\nexport type SerializedPlan = {\n tasks: SerializedTask[];\n runners: RunnerId[];\n generatedAt: string;\n conversationHistory?: LegacyPlanState['conversationHistory'];\n prdMarkdown?: string;\n queuedMessages?: QueuedMessage[];\n};\n\n/** How a planner turn ended: the reply kind it settled on, a user stop, or a failure. */\nexport type PlannerTurnOutcome = 'message' | 'plan' | 'task_ops' | 'stopped' | 'error';\n\n/**\n * Everything a session tells its surfaces, over one broadcast seam.\n *\n * A planner turn (#47) streams between `planner_turn_started` and\n * `planner_turn_ended` with the same `turnId`; every turn-scoped message in\n * between carries it. The stream is provisional and the settled messages are\n * authoritative:\n * - `planner_message` replaces the streamed text of the turn's final segment —\n * a surface drops what it accumulated and shows the message instead.\n * - A reply that is a JSON envelope (plan, taskOps, taskQuery) never arrives\n * as `planner_text_delta`; it streams as `plan_token`, the \"building plan\"\n * display.\n * - A subagent's own text never appears in the reply; its activity arrives\n * tagged with its `subagentId`.\n */\nexport type SessionMessage =\n /**\n * The plan, whole. `turnId` names the planner turn whose commit this\n * broadcast carries, so the turn's building plan becomes the marker; it is\n * absent on every other broadcast of the plan.\n */\n | { type: 'plan_generated'; plan: SerializedPlan; goal: string; runners: RunnerId[]; turnId?: string }\n /**\n * The settled reply of a planner turn, emitted by the session once the turn\n * is classified. Authoritative over any `planner_text_delta` of its turn's\n * final segment. `turnId` is absent for replies sent outside a streamed turn.\n */\n | { type: 'planner_message'; content: string; timestamp: string; turnId?: string }\n /**\n * A planner turn began. Emitted once per turn by whoever runs the turn,\n * before any other message carrying its `turnId`. `prompt` is the user's\n * message when the turn answers one.\n */\n | { type: 'planner_turn_started'; turnId: string; prompt?: string }\n /**\n * A planner turn is over; nothing more carries its `turnId`. Emitted exactly\n * once per `planner_turn_started`, stop and failure included, after the\n * turn's `planner_message` when it has one.\n */\n | { type: 'planner_turn_ended'; turnId: string; outcome: PlannerTurnOutcome }\n /**\n * Reply prose as it streams, appended in order within its segment. A segment\n * is one continuous run of model text; text before a tool call is its own\n * segment, and a later segment never rewrites an earlier one. Never carries\n * a JSON envelope or a subagent's text (see the union's invariants).\n */\n | { type: 'planner_text_delta'; turnId: string; segmentId: string; text: string }\n /**\n * Exposed reasoning as it streams, from the planner or — tagged with\n * `subagentId` — from one of its subagents. Never part of the reply. The one\n * thinking message for every backend: `segmentId` is set only where the\n * backend streams thinking in segments (the API loops; harness planners do\n * not), and `turnId` is absent for thinking outside a turn (one-shot plans).\n */\n | { type: 'planner_thinking_delta'; turnId?: string; segmentId?: string; subagentId?: string; text: string }\n /**\n * Text streamed for an attempt the turn discarded (a corrective retry) is\n * taken back: a surface removes it. With `segmentId`, only that segment;\n * without, all of the turn's text not yet settled by a `planner_message`.\n * A segment that streamed to the plan display (`plan_token`) takes the\n * turn's building plan with it.\n */\n | { type: 'planner_text_retracted'; turnId: string; segmentId?: string }\n /**\n * The planner's running usage for the session, subagents included, emitted\n * after a model call reports usage. `totals` already contains every\n * `bySubagent` entry. `contextFill` is the last planner prompt against the\n * model's window, omitted when the window is unknown. Cost appears only as\n * reported by a provider or runner (see `UsageRecord`).\n */\n | {\n type: 'planner_usage';\n turnId?: string;\n totals: UsageTotals;\n bySubagent?: Record<string, UsageTotals>;\n contextFill?: { usedTokens: number; windowTokens: number };\n }\n /**\n * A subagent began work on `brief`. Emitted by the planner backend that\n * spawned it (ADR-0005 research agents, or a harness planner's own), before\n * any message tagged with its `subagentId`.\n */\n | { type: 'subagent_started'; turnId?: string; subagentId: string; brief: string; model?: string }\n /**\n * A subagent is done; nothing more is tagged with its `subagentId`. Emitted\n * once per `subagent_started`. `digest` is what it handed back to the\n * planner; `usage` is its own share, already counted in `planner_usage`.\n */\n | { type: 'subagent_finished'; turnId?: string; subagentId: string; outcome: SubagentOutcome; digest: string; usage?: UsageTotals }\n | { type: 'status_update'; tasks: SerializedTaskStatus[] }\n | { type: 'review_needed'; tasks: SerializedTask[] }\n | { type: 'review_approved' }\n | { type: 'checkpoint'; taskId: string; taskTitle: string; summary: string }\n | { type: 'execution_complete'; summary: { total: number; completed: number; failed: number } }\n | { type: 'execution_stopped' }\n | { type: 'queue_ready' }\n | { type: 'task_updated'; taskId: string; changes: Record<string, unknown> }\n | { type: 'task_started'; taskId: string; order: number; title: string; runner: RunnerId; modelId?: string }\n | { type: 'task_output'; taskId: string; text: string }\n /**\n * A structured task's log as it happens (ADR-0018, P1): the next events of\n * the task's attempt `attempt`, in order — the same ones appended to that\n * attempt's file, so a surface folding these and one replaying the file\n * draw the same blocks. Terminal-transport tasks send none.\n */\n | { type: 'task_log'; taskId: string; attempt: number; events: TaskLogEvent[] }\n // A run did not start because tracked files are modified. It waits for the\n // user to stash (`continueWithStash`) or to run without isolation this once\n // (`continueWithoutIsolation`); nothing is spawned until then. `repos` names\n // the dirty repos of a group; a group of one names none.\n | { type: 'isolation_blocked'; reason: 'dirty'; repos?: string[]; message: string }\n // An isolated run settled: for each repo, the branch its work landed on and\n // the commit that branch forked from; and what landed, in plan order. Sent\n // before `execution_complete`, which surfaces treat as the end of the stream.\n | { type: 'isolation_handoff'; repos: IsolationHandoff['repos']; landed: IsolationHandoff['landed'] }\n // What \"Merge all\" did, for every surface watching rather than only the one\n // that asked: merged, blocked with each repo and why, or stopped part-way\n // with the repos that stay merged.\n | { type: 'isolation_merge'; result: IsolationMergeResult }\n // Carries no content — see `ResearchProgress['liveness']`. Exists only so a\n // surface's idle watchdog sees the harness process working even during a\n // stretch that produces nothing visible.\n | { type: 'planner_liveness' }\n // `toolLabel` carries a harness planner's own name for the tool (ADR-0009) —\n // always set when `tool` is `agent_tool`, so no surface has to render the\n // catch-all member name at the user.\n | { type: 'research_step'; tool: string; toolLabel?: string; args: string; subagentId?: string; toolCallId?: string; turnId?: string }\n // The \"building plan\" display only: a JSON envelope as it streams, never\n // reply prose. `turnId` is absent for a one-shot plan, which has no turn.\n // `segmentId` names the segment the envelope streams in; a turn's next\n // envelope is a new segment, and the display starts over with it.\n | { type: 'plan_token'; token: string; turnId?: string; segmentId?: string }\n | { type: 'research_step_done'; step: ResearchStep; subagentId?: string; turnId?: string }\n // Planner research wants something outside its default envelope and is\n // blocked until a human answers. Broadcast rather than returned, because the\n // human may be on any surface (or several at once) and the request outlives\n // whichever HTTP call triggered it.\n | { type: 'approval_request'; id: string; kind: ApprovalKind; subject: string; scope: string; detail?: string; turnId?: string }\n | { type: 'approval_settled'; id: string; granted: boolean }\n // A decision reached with no round-trip prompt: pre-approved via config,\n // remembered from earlier in this session, or the operator's mode floor\n // (allow/deny skipping the human entirely). The interactive path above\n // already has full visibility via approval_request/approval_settled; this\n // is the one that previously had none — a model silently auto-running\n // `npm test` because the scope was already granted looked identical, from\n // every UI, to it never having been asked in the first place.\n | {\n type: 'approval_decided';\n kind: ApprovalKind;\n subject: string;\n scope: string;\n detail?: string;\n granted: boolean;\n source: Exclude<ApprovalSource, 'asked'>;\n };\n\n/**\n * A line a run wants the user to read — how it isolates, what it shares. Not a\n * {@link SessionMessage}: hosts that show notices as toasts already do, and\n * widening the union would break every exhaustive switch over it. A host with\n * no toast channel (the daemon) hands these to its clients by its own means.\n */\nexport type SessionNotice = { type: 'notice'; level: 'info' | 'warn' | 'error'; message: string };\n\nexport type SessionBroadcaster = (msg: SessionMessage) => void;\n\nexport function serializeTask(t: Task): SerializedTask {\n return {\n id: t.id,\n order: t.order,\n title: t.title,\n type: t.type,\n description: t.description,\n dependencies: t.dependencies || [],\n assignedRunner: t.assignedRunner,\n assignedModel: t.assignedModel || null,\n taskMode: t.taskMode || 'build',\n prompt: t.prompt || null,\n subtasks: (t.subtasks || []).map(serializeTask),\n userSteps: t.userSteps || undefined,\n thinkingEffort: t.thinkingEffort || undefined,\n autonomy: t.autonomy || undefined,\n sliceType: t.sliceType || undefined,\n userStoriesCovered: t.userStoriesCovered || undefined,\n };\n}\n\nexport function serializeTaskStatus(\n t: Task,\n idleSince: string | null = null,\n isolation: TaskIsolation | null = null,\n queued: readonly QueuedTaskMessage[] = [],\n awaitingApproval = 0,\n): SerializedTaskStatus {\n return {\n id: t.id,\n status: t.status,\n verdict: t.verdict\n ? { outcome: t.verdict.outcome, reason: t.verdict.reason, checks: t.verdict.checks || [] }\n : null,\n idleSince,\n ...(isolation ? { isolation } : {}),\n ...(t.transport ? { transport: t.transport.fallback ? { kind: t.transport.kind, fallback: t.transport.fallback } : { kind: t.transport.kind } } : {}),\n ...(t.status === 'awaiting_user' && t.awaitingReason ? { awaitingReason: t.awaitingReason } : {}),\n ...(queued.length > 0 ? { queued: queued.map((m) => ({ ...m })) } : {}),\n ...(canContinue(t) ? { continuable: true as const } : {}),\n ...(awaitingApproval > 0 ? { awaitingApproval } : {}),\n };\n}\n\nexport function serializePlan(plan: LegacyPlanState): SerializedPlan {\n return {\n tasks: plan.tasks.map(serializeTask),\n runners: plan.runners,\n generatedAt: plan.generatedAt,\n conversationHistory: plan.conversationHistory,\n prdMarkdown: plan.prdMarkdown,\n queuedMessages: plan.queuedMessages,\n };\n}\n\nexport function executionSummary(tasks: readonly Task[]): { total: number; completed: number; failed: number } {\n return {\n total: tasks.length,\n completed: tasks.filter((t) => t.status === 'completed').length,\n failed: tasks.filter((t) => t.status === 'failed').length,\n };\n}\n\nexport const CHECKPOINT_TRUNCATE_LENGTH = 120;\n\n/** Truncate a checkpoint summary to a single line of at most CHECKPOINT_TRUNCATE_LENGTH chars. */\nexport function truncateCheckpointSummary(summary: string): string {\n // Take the first line only (checkpoint summaries often contain reasoning across lines)\n const firstLine = summary.split('\\n')[0].trim();\n if (firstLine.length <= CHECKPOINT_TRUNCATE_LENGTH) return firstLine;\n return firstLine.slice(0, CHECKPOINT_TRUNCATE_LENGTH - 1) + '…';\n}\n","/**\n * Pure, no-imports helper so a browser surface can format a conflict's files\n * without pulling `isolationRecord` — and its `path` import — into a webview\n * bundle. Kept out of `isolationRecord.ts` for exactly that reason.\n */\nexport function capConflictFiles(files: string[], max = 5): string {\n return files.length <= max ? files.join(', ') : `${files.slice(0, max).join(', ')}, +${files.length - max} more`;\n}\n","/**\n * What one model call consumed, as its provider or runner reported it. Shared\n * by the planner's usage line (#49) and per-attempt task usage (#26): a record\n * says nothing about who asked for the call, so either can hold a list of them.\n *\n * Every measure is optional because backends report different subsets. An\n * absent field means \"not reported\" — never zero — so a total can tell the two\n * apart.\n */\nexport interface UsageRecord {\n /** The provider or runner id that reported the call — `openai`, `claude-code`, … */\n source: string;\n model?: string;\n inputTokens?: number;\n outputTokens?: number;\n /** The share of `inputTokens` served from the provider's prompt cache. */\n cachedInputTokens?: number;\n /**\n * Filled only from a provider's or runner's own report, never from a price\n * table or an estimate: prices go stale, and a subscription runner has no\n * per-token price at all. No report means no cost, not a guessed one.\n */\n reportedCost?: { amount: number; currency: string };\n /** The model's context window, when the runner itself reports it. */\n contextWindow?: number;\n /** Set when a subagent made the call; its usage still counts toward the total. */\n subagentId?: string;\n}\n\n/**\n * A running sum of {@link UsageRecord}s. A measure stays absent until some\n * record reports it. Cost is kept per currency: two runners may bill in\n * different ones, and there is no honest exchange rate to fold them together.\n */\nexport interface UsageTotals {\n inputTokens?: number;\n outputTokens?: number;\n cachedInputTokens?: number;\n reportedCost?: Record<string, number>;\n}\n\n/**\n * The input side of a call whose provider reports its prompt in parts — the\n * uncached tail, with cache reads and cache writes beside it rather than inside\n * it (Anthropic, and OpenCode after it). The prompt the model saw is all three;\n * only the reads were served from cache, since a write is billed as fresh\n * input. A part left unreported adds nothing, and with no part reported there\n * is no measure at all.\n */\nexport function partedPromptUsage(parts: { uncached?: number; cacheRead?: number; cacheWrite?: number }): Pick<UsageRecord, 'inputTokens' | 'cachedInputTokens'> {\n const reported = [parts.uncached, parts.cacheRead, parts.cacheWrite].filter((n): n is number => n !== undefined);\n return {\n ...(reported.length > 0 ? { inputTokens: reported.reduce((a, b) => a + b, 0) } : {}),\n ...(parts.cacheRead !== undefined ? { cachedInputTokens: parts.cacheRead } : {}),\n };\n}\n\nconst TOKEN_FIELDS = ['inputTokens', 'outputTokens', 'cachedInputTokens'] as const;\n\nexport function addUsage(totals: UsageTotals, record: UsageRecord): UsageTotals {\n const next: UsageTotals = { ...totals };\n for (const field of TOKEN_FIELDS) {\n const reported = record[field];\n if (reported !== undefined) next[field] = (totals[field] ?? 0) + reported;\n }\n if (record.reportedCost) {\n const { amount, currency } = record.reportedCost;\n next.reportedCost = { ...totals.reportedCost, [currency]: (totals.reportedCost?.[currency] ?? 0) + amount };\n }\n return next;\n}\n\n/**\n * What the planner has consumed over a session (#49). `totals` includes every\n * `bySubagent` entry. `lastPromptTokens` and `contextWindow` track the\n * planner's own calls only — a subagent runs its own model, whose window says\n * nothing about the planner's.\n */\nexport interface PlannerUsage {\n totals: UsageTotals;\n bySubagent?: Record<string, UsageTotals>;\n lastPromptTokens?: number;\n contextWindow?: number;\n}\n\nexport function addPlannerUsage(usage: PlannerUsage, record: UsageRecord): PlannerUsage {\n const next: PlannerUsage = { ...usage, totals: addUsage(usage.totals, record) };\n if (record.subagentId) {\n next.bySubagent = { ...usage.bySubagent, [record.subagentId]: addUsage(usage.bySubagent?.[record.subagentId] ?? {}, record) };\n return next;\n }\n if (record.inputTokens !== undefined) next.lastPromptTokens = record.inputTokens;\n // A reported window of 0 means \"not known\", not \"no room\": keep it out so the\n // fill is omitted rather than shown against a guessed zero.\n if (record.contextWindow !== undefined && record.contextWindow > 0) next.contextWindow = record.contextWindow;\n return next;\n}\n\n/** Whether any measure was reported: a token line of nothing but blanks says nothing. */\nexport function isMeasured(totals: UsageTotals): boolean {\n return totals.inputTokens !== undefined || totals.outputTokens !== undefined || totals.cachedInputTokens !== undefined\n || Object.keys(totals.reportedCost ?? {}).length > 0;\n}\n\n/** What the token line shows of a ledger — live from its broadcast, or reloaded from the saved one. */\nexport interface UsageLine {\n totals: UsageTotals;\n bySubagent?: Record<string, UsageTotals>;\n contextFill?: { usedTokens: number; windowTokens: number };\n}\n\nexport function usageLine(usage: PlannerUsage): UsageLine {\n const contextFill = plannerContextFill(usage);\n return {\n totals: usage.totals,\n ...(usage.bySubagent ? { bySubagent: usage.bySubagent } : {}),\n ...(contextFill ? { contextFill } : {}),\n };\n}\n\n/**\n * The last planner prompt against its window, or undefined while either is\n * unknown. `usedTokens` is the prompt total as reported, cached tokens\n * included: a cached token still occupies the window, so subtracting the\n * cached share would understate how full the context is. A window of 0 is\n * treated as unknown — never guessed.\n */\nexport function plannerContextFill(usage: PlannerUsage): { usedTokens: number; windowTokens: number } | undefined {\n if (usage.lastPromptTokens === undefined || usage.contextWindow === undefined || usage.contextWindow <= 0) return undefined;\n return { usedTokens: usage.lastPromptTokens, windowTokens: usage.contextWindow };\n}\n","/*\n * Pure terminal rendering: turns raw PTY bytes into the text a user would see.\n * No state and no I/O, so the verdict scan and every output reader share it.\n */\n\n// eslint-disable-next-line no-control-regex\nexport const ANSI_OR_CTRL_RE = /\\x1b\\[[0-9;?]*[A-Za-z]|\\x1b\\][^\\x07\\x1b]*(?:\\x07|\\x1b\\\\)?|\\x1b[()][AB012]|\\x1b[=>]|[\\x00-\\x08\\x0b-\\x1f\\x7f]/g;\n\n/**\n * Collapse terminal rendering out of raw PTY output: strip ANSI/OSC escape\n * sequences, box-drawing gutter characters, and ALL whitespace. Interactive\n * TUIs soft-wrap long lines, so a completion marker arrives split across\n * lines with escapes interleaved — a raw `includes()` never matches it.\n * Markers contain no whitespace, so this flattened view is safe to scan.\n */\nexport function flattenTerminalOutput(raw: string): string {\n return raw\n .replace(ANSI_OR_CTRL_RE, '')\n .replace(/[─-▟]/g, '') // box-drawing + block elements (TUI borders/gutters)\n .replace(/\\s+/g, '');\n}\n\n/**\n * Reconstruct the small terminal screen represented by a PTY output tail.\n *\n * Full-screen TUIs do not emit answer text as one chronological stream. They\n * paint fragments at absolute cursor positions and repaint unrelated widgets\n * between those writes. OpenCode, for example, may emit:\n *\n * row 9: \"<<<ORDEW\"\n * row 23: spinner repaint\n * row 9: \"ELL_DONE_...\"\n *\n * Flattening that byte stream inserts the spinner inside the marker. Rendering\n * the cursor-positioned writes first recovers the token the user actually sees.\n * This intentionally implements only the common cursor/erase subset used by\n * coding-agent TUIs; the chronological scanner remains the fallback for plain\n * output and soft-wrapped lines.\n */\nexport function renderTerminalOutput(raw: string): string {\n const rows = new Map<number, string[]>();\n let row = 1;\n let col = 1;\n let savedRow = 1;\n let savedCol = 1;\n\n const cells = (r: number): string[] => {\n let line = rows.get(r);\n if (!line) {\n line = [];\n rows.set(r, line);\n }\n return line;\n };\n const firstParam = (params: number[], fallback = 1): number => params[0] || fallback;\n const eraseLine = (mode: number): void => {\n const line = cells(row);\n if (mode === 2) {\n rows.set(row, []);\n } else if (mode === 1) {\n for (let c = 0; c < col; c++) line[c] = ' ';\n } else {\n line.length = Math.max(0, col - 1);\n }\n };\n\n for (let i = 0; i < raw.length;) {\n const ch = raw[i];\n if (ch === '\\x1b') {\n const kind = raw[i + 1];\n if (kind === '[') {\n let end = i + 2;\n while (end < raw.length) {\n const code = raw.charCodeAt(end);\n if (code >= 0x40 && code <= 0x7e) break;\n end++;\n }\n if (end >= raw.length) break;\n const final = raw[end];\n const body = raw.slice(i + 2, end).replace(/^[?<>=!]+/, '');\n const params = body.split(';').map((p) => Number.parseInt(p, 10) || 0);\n switch (final) {\n case 'H':\n case 'f':\n row = params[0] || 1;\n col = params[1] || 1;\n break;\n case 'G': col = firstParam(params); break;\n case 'd': row = firstParam(params); break;\n case 'A': row = Math.max(1, row - firstParam(params)); break;\n case 'B': row += firstParam(params); break;\n case 'C': col += firstParam(params); break;\n case 'D': col = Math.max(1, col - firstParam(params)); break;\n case 'E': row += firstParam(params); col = 1; break;\n case 'F': row = Math.max(1, row - firstParam(params)); col = 1; break;\n case 's': savedRow = row; savedCol = col; break;\n case 'u': row = savedRow; col = savedCol; break;\n case 'K': eraseLine(params[0] || 0); break;\n case 'J':\n if ((params[0] || 0) === 2 || (params[0] || 0) === 3) rows.clear();\n break;\n case 'X': {\n const line = cells(row);\n for (let c = 0; c < firstParam(params); c++) line[col - 1 + c] = ' ';\n break;\n }\n case 'P': {\n cells(row).splice(col - 1, firstParam(params));\n break;\n }\n case '@': {\n cells(row).splice(col - 1, 0, ...Array(firstParam(params)).fill(' '));\n break;\n }\n default:\n break;\n }\n i = end + 1;\n continue;\n }\n if (kind === ']' || kind === 'P' || kind === '^' || kind === '_') {\n let end = i + 2;\n while (end < raw.length && raw[end] !== '\\x07' && !(raw[end] === '\\x1b' && raw[end + 1] === '\\\\')) end++;\n if (end >= raw.length) break;\n i = raw[end] === '\\x07' ? end + 1 : end + 2;\n continue;\n }\n if (kind === '7') {\n savedRow = row;\n savedCol = col;\n } else if (kind === '8') {\n row = savedRow;\n col = savedCol;\n } else if (kind === 'c') {\n rows.clear();\n row = 1;\n col = 1;\n }\n // Character-set selection sequences carry one extra byte.\n i += kind === '(' || kind === ')' ? 3 : 2;\n continue;\n }\n if (ch === '\\r') {\n col = 1;\n i++;\n continue;\n }\n if (ch === '\\n') {\n row++;\n i++;\n continue;\n }\n if (ch === '\\b') {\n col = Math.max(1, col - 1);\n i++;\n continue;\n }\n if (ch === '\\t') {\n col += 8 - ((col - 1) % 8);\n i++;\n continue;\n }\n if (ch < ' ' || ch === '\\x7f') {\n i++;\n continue;\n }\n cells(row)[col - 1] = ch;\n col++;\n i++;\n }\n\n const populated = [...rows.keys()].sort((a, b) => a - b);\n return populated.map((r) => cells(r).join('')).join('\\n');\n}\n\n/**\n * Reconstruct what the user actually saw, then stop at the completion marker.\n * Used for anything captured as a task's durable output (outputSummary.logTail,\n * and any future planner read channel): the raw chronological PTY tail is\n * dominated by TUI paint — spinner lines, status bars, box-drawing borders,\n * cursor-positioned fragments — because screen-painting agents emit answer\n * text at absolute positions and repaint unrelated widgets around it.\n *\n * The marker row is the cut anchor: everything below it is the TUI's\n * persistent chrome, not task output. Falls back to the chronological\n * (stripAnsi'd) tail when neither render exposes the marker — the common case\n * for headless runners, whose raw stream IS meaningful.\n */\nexport function renderCleanCapture(raw: string, doneToken?: string): string {\n const rendered = renderTerminalOutput(raw).replace(/[\\s─-▟]+$/gm, '');\n if (!doneToken) return rendered.trim();\n const lines = rendered.split('\\n');\n const flat = (s: string) => flattenTerminalOutput(s);\n for (let i = 0; i < lines.length; i++) {\n // The marker may be split across two rendered rows (painted at a row\n // boundary): the first row alone won't match, so a two-row window is\n // scanned too. The cut is the last row that carries marker text — rows\n // above it are content, rows below are TUI chrome.\n const own = flat(lines[i]).includes(doneToken);\n if (own) return lines.slice(0, i).join('\\n').trim();\n const window = flat(lines[i]) + (i + 1 < lines.length ? flat(lines[i + 1]) : '');\n if (window.includes(doneToken)) return lines.slice(0, i + 1).join('\\n').trim();\n }\n return rendered.trim();\n}\n","import type { ToolHeadline } from './blocks';\nimport { ANSI_OR_CTRL_RE } from '../services/terminalRender';\n\n/**\n * How a command row names an Ordewell research tool. A harness planner's call\n * keeps the agent's own name instead (`toolLabel`, ADR-0009), so the row says\n * which tool actually ran.\n */\nconst DISPLAY_NAMES: Record<string, string> = {\n read_file: 'Read',\n read_files: 'Read',\n list_dir: 'List',\n glob: 'Glob',\n grep: 'Grep',\n find_symbol: 'Symbol',\n bash: 'Bash',\n fetch: 'Fetch',\n web_search: 'WebSearch',\n spawn_research_agent: 'Agent',\n};\n\n// Tried in order for a tool with no rule of its own: the field that names what\n// a call is about comes before the free text around it.\nconst HINT_FIELDS = ['file_path', 'filePath', 'path', 'url', 'pattern', 'query', 'command', 'description', 'prompt'] as const;\n\nfunction parseArgs(args: string): Record<string, unknown> | null {\n try {\n const parsed: unknown = JSON.parse(args);\n return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed as Record<string, unknown> : null;\n } catch {\n return null;\n }\n}\n\nfunction oneLine(text: string): string {\n return text.replace(/\\s+/g, ' ').trim();\n}\n\n// A command's later lines can change what it does (a heredoc body, a chained\n// step), so they are counted rather than folded into the first.\nfunction firstLineOf(command: string): string {\n const lines = command.trim().split(/\\r?\\n/);\n const more = lines.length - 1;\n return more > 0 ? `${lines[0]} … +${more} line${more === 1 ? '' : 's'}` : lines[0];\n}\n\nfunction text(value: unknown): string {\n return typeof value === 'string' ? value : '';\n}\n\nfunction searchArg(pattern: string, path: string, include = ''): string {\n return `${pattern}${path ? ` in ${path}` : ''}${include ? ` (${include})` : ''}`;\n}\n\nfunction hintArg(args: Record<string, unknown>): string {\n for (const field of HINT_FIELDS) {\n const value = text(args[field]);\n if (value) return field === 'command' ? firstLineOf(value) : oneLine(value);\n }\n const firstText = Object.values(args).find((v): v is string => typeof v === 'string' && v.trim() !== '');\n return firstText ? oneLine(firstText) : '';\n}\n\nfunction keyArgOf(tool: string, args: Record<string, unknown>): string {\n switch (tool) {\n case 'bash':\n return firstLineOf(text(args.command));\n case 'read_file':\n case 'list_dir':\n return text(args.path);\n case 'read_files':\n return Array.isArray(args.paths) ? args.paths.map(String).join(', ') : '';\n case 'glob':\n return searchArg(text(args.pattern), text(args.path));\n case 'grep':\n return searchArg(text(args.pattern), text(args.path), text(args.include));\n case 'find_symbol':\n return `${text(args.symbol)}${text(args.language) ? ` [${text(args.language)}]` : ''}`;\n case 'fetch':\n return text(args.url);\n case 'web_search':\n return oneLine(text(args.query));\n case 'spawn_research_agent':\n return oneLine(text(args.prompt));\n default:\n return hintArg(args);\n }\n}\n\n/**\n * The one-line head of a command row, shared by every surface: the tool's name\n * and the argument that says what the call is about.\n *\n * @param args The call's arguments as announced (JSON). Anything that is not a\n * JSON object is shown as it came, on one line.\n * @param toolLabel A harness planner's own name for the tool (ADR-0009).\n */\nexport function toolHeadline(tool: string, args: string, toolLabel?: string): ToolHeadline {\n const name = toolLabel?.trim() || DISPLAY_NAMES[tool] || tool;\n const parsed = parseArgs(args);\n return { name, keyArg: parsed ? keyArgOf(tool, parsed) : oneLine(args) };\n}\n\nexport interface OutputPreview {\n lines: string[];\n hiddenLineCount: number;\n}\n\n/** The lines of a tool's output as a reader sees them — what {@link outputPreview} shows and counts. */\nexport function outputLines(output: string): string[] {\n // Line endings first: CR is itself a control character the escape pass drops.\n const lines = output.replace(/\\r\\n?/g, '\\n').replace(ANSI_OR_CTRL_RE, '').split('\\n');\n while (lines.length > 0 && lines[lines.length - 1].trim() === '') lines.pop();\n return lines;\n}\n\n/** The head of a tool's output for a collapsed row, and how many lines it leaves out. */\nexport function outputPreview(output: string, maxLines: number): OutputPreview {\n const lines = outputLines(output);\n const shown = lines.slice(0, Math.max(0, maxLines));\n return { lines: shown, hiddenLineCount: lines.length - shown.length };\n}\n","import type { ResearchStep, ResearchStepOutcome } from '../models/Task';\nimport type { UsageLine } from '../models/Usage';\nimport type { MessageBlock, MessageRole, PlanMarkerStatus, SubagentBlock, ToolBlock, ToolStatus, UsageBlock } from './blocks';\nimport { outputLines, toolHeadline } from './format';\n\n/*\n * How the session's records — messages, research steps, subagents, transcript\n * markers, usage totals — read as blocks. Shared by the live view and the\n * reload, so the two cannot disagree about what a record shows.\n */\n\n/** A line that is done: nothing streams into it any more. */\nexport function settledMessage(id: string, role: MessageRole, text: string, turnId?: string): MessageBlock {\n return { type: 'message', id, role, text, streaming: false, ...(turnId ? { turnId } : {}) };\n}\n\ntype SubagentFields = Omit<SubagentBlock, 'type' | 'id' | 'toolCallId' | 'model' | 'usage' | 'turnId'>\n & { toolCallId?: string; model?: string; usage?: SubagentBlock['usage']; turnId?: string };\n\nexport function subagentBlock(id: string, { toolCallId, model, usage, turnId, ...fields }: SubagentFields): SubagentBlock {\n return {\n type: 'subagent', id, ...fields,\n ...(toolCallId ? { toolCallId } : {}),\n ...(model ? { model } : {}),\n ...(usage ? { usage } : {}),\n ...(turnId ? { turnId } : {}),\n };\n}\n\nexport function usageBlock(id: string, { totals, bySubagent, contextFill }: UsageLine): UsageBlock {\n return { type: 'usage', id, totals, ...(bySubagent ? { bySubagent } : {}), ...(contextFill ? { contextFill } : {}) };\n}\n\nconst STATUS_OF_OUTCOME: Record<ResearchStepOutcome, ToolStatus> = {\n success: 'ok',\n failure: 'error',\n refused: 'denied',\n denied: 'denied',\n not_executed: 'interrupted',\n};\n\nexport function finishedTool(call: ToolBlock, output: string, outcome: ResearchStepOutcome): ToolBlock {\n return { ...call, status: STATUS_OF_OUTCOME[outcome], outcome, output, outputLineCount: outputLines(output).length };\n}\n\nexport function settledTool(call: ToolBlock, step: ResearchStep): ToolBlock {\n return finishedTool(call, step.result, step.outcome);\n}\n\nexport interface AnnouncedCall {\n tool: string;\n toolLabel?: string;\n args: string;\n toolCallId?: string;\n turnId?: string;\n}\n\nexport function pendingTool(id: string, { tool, toolLabel, args, toolCallId, turnId }: AnnouncedCall): ToolBlock {\n return {\n type: 'tool', id, tool, headline: toolHeadline(tool, args, toolLabel), args, status: 'pending', output: '', outputLineCount: 0,\n ...(toolCallId ? { toolCallId } : {}),\n ...(toolLabel ? { toolLabel } : {}),\n ...(turnId ? { turnId } : {}),\n };\n}\n\n/** The row of a call known only from its result — a reload, or a result whose call was never announced. */\nexport function toolFromStep(id: string, step: ResearchStep, turnId?: string): ToolBlock {\n return settledTool(pendingTool(id, { ...step, turnId }), step);\n}\n\n/**\n * What a plan marker says, read from the transcript entry `PlannerConversation`\n * writes for it (\"Plan generated with 2 tasks.\", \"Plan updated — now 1 task.\").\n * That entry is the only record of the marker a reload has, so the live view\n * reads the same text rather than counting tasks itself.\n */\nexport function planMarker(content: string): { status: Exclude<PlanMarkerStatus, 'building'>; taskCount?: number } {\n const count = /(\\d+) tasks?\\b/.exec(content);\n return { status: content.startsWith('Plan updated') ? 'updated' : 'generated', ...(count ? { taskCount: Number(count[1]) } : {}) };\n}\n","import type { UsageLine } from '../models/Usage';\nimport type { DisplayBlock, SubagentBlock, SubagentChild, SubagentStatus } from './blocks';\nimport { subagentBlock, usageBlock } from './records';\n\n/*\n * The block-list operations both reducers are built from — the planner\n * conversation (`reduce.ts`) and a structured task's log (`taskLog.ts`) — so\n * a subagent, a streaming run of text or the usage line behaves the same in\n * either view.\n */\n\n/** What every view over display blocks holds; each view adds its own state beside it. */\nexport interface BlockList {\n readonly blocks: readonly DisplayBlock[];\n readonly nextId: number;\n}\n\n/** The top level of the view (`null`), or the index of the subagent whose children are meant. */\nexport type Lane = number | null;\n\nexport function findLastIndex<T>(items: readonly T[], match: (item: T) => boolean): number {\n for (let i = items.length - 1; i >= 0; i--) if (match(items[i])) return i;\n return -1;\n}\n\nexport function replaceAt<T>(items: readonly T[], index: number, item: T): T[] {\n const next = items.slice();\n next[index] = item;\n return next;\n}\n\n// The usage line always sits last, so everything else lands above it.\nfunction insertTop(blocks: readonly DisplayBlock[], block: DisplayBlock): DisplayBlock[] {\n const last = blocks[blocks.length - 1];\n return last?.type === 'usage' ? [...blocks.slice(0, -1), block, last] : [...blocks, block];\n}\n\nexport function append<V extends BlockList>(view: V, make: (id: string) => DisplayBlock): V {\n return { ...view, blocks: insertTop(view.blocks, make(`b${view.nextId}`)), nextId: view.nextId + 1 };\n}\n\nexport function subagentAt(view: BlockList, lane: number): SubagentBlock {\n const block = view.blocks[lane];\n if (block.type !== 'subagent') throw new Error(`block ${lane} is not a subagent`);\n return block;\n}\n\nexport function laneBlocks(view: BlockList, lane: Lane): readonly DisplayBlock[] {\n return lane === null ? view.blocks : subagentAt(view, lane).children;\n}\n\nfunction setChildren<V extends BlockList>(view: V, lane: number, children: readonly SubagentChild[]): V {\n return { ...view, blocks: replaceAt(view.blocks, lane, { ...subagentAt(view, lane), children }) };\n}\n\nexport function laneReplace<V extends BlockList>(view: V, lane: Lane, index: number, block: SubagentChild): V {\n if (lane === null) return { ...view, blocks: replaceAt(view.blocks, index, block) };\n return setChildren(view, lane, replaceAt(subagentAt(view, lane).children, index, block));\n}\n\nexport function laneAppend<V extends BlockList>(view: V, lane: Lane, make: (id: string) => SubagentChild): V {\n if (lane === null) return append(view, make);\n const children = [...subagentAt(view, lane).children, make(`b${view.nextId}`)];\n return { ...setChildren(view, lane, children), nextId: view.nextId + 1 };\n}\n\nfunction isStreaming(block: DisplayBlock): boolean {\n return (block.type === 'message' || block.type === 'thinking') && block.streaming;\n}\n\nexport function sealed<B extends DisplayBlock>(block: B): B {\n return isStreaming(block) ? { ...block, streaming: false } : block;\n}\n\n/**\n * Output landed at `keep` in the lane, so nothing else there is still\n * streaming: one lane has one voice at a time, and a block left marked as\n * streaming would keep its cursor after its text had moved on.\n */\nexport function sealLane<V extends BlockList>(view: V, lane: Lane, keep: number): V {\n const blocks = laneBlocks(view, lane);\n if (!blocks.some((b, i) => i !== keep && isStreaming(b))) return view;\n if (lane === null) return { ...view, blocks: view.blocks.map((b, i) => (i === keep ? b : sealed(b))) };\n return setChildren(view, lane, subagentAt(view, lane).children.map((b, i) => (i === keep ? b : sealed(b))));\n}\n\n/** Append output to the lane, which then speaks through it alone. */\nexport function appendOutput<V extends BlockList>(view: V, lane: Lane, make: (id: string) => SubagentChild): V {\n const next = laneAppend(view, lane, make);\n return sealLane(next, lane, findLastIndex(laneBlocks(next, lane), (b) => b.type !== 'usage'));\n}\n\n/**\n * The subagent's block, created on first sighting: its activity can arrive\n * before (or without) its start. The call that spawns it becomes the block,\n * keeping the call's id and position — the call and the subagent are one\n * thing on screen.\n */\nexport function subagentLane<V extends BlockList>(view: V, subagentId: string, turnId?: string): [V, number] {\n const existing = findLastIndex(view.blocks, (b) => b.type === 'subagent' && b.subagentId === subagentId);\n if (existing >= 0) return [view, existing];\n const spawn = findLastIndex(view.blocks, (b) => b.type === 'tool' && (b.spawns === subagentId || b.toolCallId === subagentId));\n const call = view.blocks[spawn];\n if (call?.type === 'tool') {\n const block = subagentBlock(call.id, {\n subagentId, brief: call.headline.keyArg, status: 'running', children: [], digest: '', toolCallId: call.toolCallId, turnId: call.turnId,\n });\n return [{ ...view, blocks: replaceAt(view.blocks, spawn, block) }, spawn];\n }\n const next = append(view, (id) => subagentBlock(id, { subagentId, brief: '', status: 'running', children: [], digest: '', turnId }));\n return [next, findLastIndex(next.blocks, (b) => b.type === 'subagent' && b.subagentId === subagentId)];\n}\n\nexport function updateSubagent<V extends BlockList>(view: V, subagentId: string, turnId: string | undefined, update: (block: SubagentBlock) => SubagentBlock): V {\n const [next, lane] = subagentLane(view, subagentId, turnId);\n return { ...next, blocks: replaceAt(next.blocks, lane, update(subagentAt(next, lane))) };\n}\n\n/** The lane a subagent's activity lands in: its own block's children, or the top level. */\nexport function laneOf<V extends BlockList>(view: V, subagentId: string | undefined, turnId?: string): [V, Lane] {\n return subagentId ? subagentLane(view, subagentId, turnId) : [view, null];\n}\n\nexport function think<V extends BlockList>(view: V, text: string, turnId?: string, segmentId?: string, subagentId?: string): V {\n const [placed, lane] = laneOf(view, subagentId, turnId);\n const blocks = laneBlocks(placed, lane);\n const lastIndex = findLastIndex(blocks, (b) => b.type !== 'usage');\n const last = blocks[lastIndex];\n if (last?.type === 'thinking' && last.streaming && last.segmentId === segmentId) {\n return laneReplace(placed, lane, lastIndex, { ...last, text: last.text + text });\n }\n return appendOutput(placed, lane, (id) => ({\n type: 'thinking', id, text, streaming: true,\n ...(turnId ? { turnId } : {}),\n ...(segmentId ? { segmentId } : {}),\n ...(subagentId ? { subagentId } : {}),\n }));\n}\n\n/** The one usage line, replaced in place or added as the last block. */\nexport function setUsageLine<V extends BlockList>(view: V, line: UsageLine): V {\n const last = view.blocks[view.blocks.length - 1];\n const block = usageBlock(last?.type === 'usage' ? last.id : `b${view.nextId}`, line);\n if (last?.type === 'usage') return { ...view, blocks: replaceAt(view.blocks, view.blocks.length - 1, block) };\n return { ...view, blocks: [...view.blocks, block], nextId: view.nextId + 1 };\n}\n\nfunction closeBlock<B extends DisplayBlock>(block: B, interrupted: boolean): B {\n return interrupted && block.type === 'tool' && block.status === 'pending' ? { ...block, status: 'interrupted' } : sealed(block);\n}\n\nfunction closeSubagent(block: SubagentBlock, cut: SubagentStatus | null): SubagentBlock {\n const children = block.children.map((c) => closeBlock(c, cut !== null));\n const status = cut && block.status === 'running' ? cut : block.status;\n return status === block.status && children.every((c, i) => c === block.children[i]) ? block : { ...block, status, children };\n}\n\n/**\n * A turn is over, so whatever it left open is closed: streaming stops, and\n * when the turn was cut short (`cut`), its unanswered calls and running\n * subagents say so.\n */\nexport function closeOpenBlocks<V extends BlockList>(view: V, cut: SubagentStatus | null): V {\n const blocks = view.blocks.map((b) => (b.type === 'subagent' ? closeSubagent(b, cut) : closeBlock(b, cut !== null)));\n return blocks.every((b, i) => b === view.blocks[i]) ? view : { ...view, blocks };\n}\n","import type { ResearchStep } from '../models/Task';\nimport type { SessionMessage } from '../services/SessionMessage';\nimport { isMeasured } from '../models/Usage';\nimport type { DisplayBlock, MessageBlock, PlanBlock, ToolBlock } from './blocks';\nimport { pendingTool, planMarker, settledMessage, settledTool, toolFromStep } from './records';\nimport {\n append, appendOutput, closeOpenBlocks, findLastIndex, laneBlocks, laneOf, laneAppend, laneReplace, replaceAt, sealLane, sealed, setUsageLine,\n think, updateSubagent, type BlockList,\n} from './lanes';\n\n/**\n * A line a surface adds to the conversation itself rather than receiving from\n * the session: the user's prompt as it is sent, a notice, an error.\n */\nexport interface LocalEntry {\n type: 'local_entry';\n role: 'user' | 'system' | 'error';\n text: string;\n}\n\nexport type ConversationInput = SessionMessage | LocalEntry;\n\nexport interface ConversationView extends BlockList {\n /**\n * The newest transcript entry the view accounts for. Plan markers and system\n * notes reach a surface only inside the transcript a `plan_generated`\n * carries, so entries after this one are what is new in it.\n */\n readonly transcriptAt?: string;\n}\n\nexport const EMPTY_CONVERSATION: ConversationView = { blocks: [], nextId: 1 };\n\ntype Message<T extends SessionMessage['type']> = Extract<SessionMessage, { type: T }>;\n\nfunction isUnsettledSegment(block: DisplayBlock, turnId: string): block is MessageBlock {\n return block.type === 'message' && block.turnId === turnId && block.segmentId !== undefined;\n}\n\nfunction isBuildingPlan(block: DisplayBlock, turnId: string | undefined): block is PlanBlock {\n return block.type === 'plan' && block.status === 'building' && block.turnId === turnId;\n}\n\n/**\n * Where the turn's final segment is, if its text streamed: the turn's latest\n * block, when that is streamed text. Anything the turn did after a segment —\n * a tool call, a plan envelope — means a later segment follows it, and a later\n * segment never rewrites an earlier one. `pastPlan` looks past the turn's plan\n * block, for a plan that settled after its own text.\n */\nfunction finalSegmentIndex(blocks: readonly DisplayBlock[], turnId: string, pastPlan = false): number {\n for (let i = blocks.length - 1; i >= 0; i--) {\n const block = blocks[i];\n if (block.type === 'usage' || block.type === 'thinking' || block.type === 'approval' || (pastPlan && block.type === 'plan')) continue;\n if (block.turnId !== turnId) continue;\n return isUnsettledSegment(block, turnId) ? i : -1;\n }\n return -1;\n}\n\nfunction openTurn(view: ConversationView, turnId: string, prompt: string): ConversationView {\n // A surface shows its user's prompt the moment it is sent; the turn that\n // answers it adopts that line rather than repeating it.\n const i = findLastIndex(view.blocks, (b) => b.type === 'message' && b.role === 'user');\n const sent = view.blocks[i];\n if (sent?.type === 'message' && sent.turnId === undefined && sent.text === prompt) {\n return { ...view, blocks: replaceAt(view.blocks, i, { ...sent, turnId }) };\n }\n return append(view, (id) => settledMessage(id, 'user', prompt, turnId));\n}\n\nfunction streamText(view: ConversationView, { turnId, segmentId, text }: Message<'planner_text_delta'>): ConversationView {\n const i = findLastIndex(view.blocks, (b) => isUnsettledSegment(b, turnId) && b.segmentId === segmentId);\n const segment = view.blocks[i];\n if (segment?.type === 'message') {\n return sealLane(laneReplace(view, null, i, { ...segment, text: segment.text + text, streaming: true }), null, i);\n }\n return appendOutput(view, null, (id) => ({ type: 'message', id, role: 'planner', text, streaming: true, turnId, segmentId }));\n}\n\n/**\n * Drop the turn's plan display while it is still building: whatever streamed\n * there did not become a plan — a task-ops or task-query envelope, or, from a\n * daemon older than turns, the reply's prose.\n */\nfunction dropBuildingPlan(view: ConversationView, turnId: string | undefined, segmentId?: string): ConversationView {\n const blocks = view.blocks.filter((b) => !(isBuildingPlan(b, turnId) && (segmentId === undefined || b.segmentId === segmentId)));\n return blocks.length === view.blocks.length ? view : { ...view, blocks };\n}\n\nfunction settleReply(view: ConversationView, { content, turnId }: Message<'planner_message'>): ConversationView {\n if (turnId !== undefined) {\n const i = finalSegmentIndex(view.blocks, turnId);\n if (i >= 0) {\n return dropBuildingPlan({ ...view, blocks: replaceAt(view.blocks, i, settledMessage(view.blocks[i].id, 'planner', content, turnId)) }, turnId);\n }\n }\n return append(dropBuildingPlan(view, turnId), (id) => settledMessage(id, 'planner', content, turnId));\n}\n\n// A retracted envelope takes the turn's plan display with it, or its retry\n// would build on the botched JSON and the partial-plan rows read both.\nfunction retractText(view: ConversationView, { turnId, segmentId }: Message<'planner_text_retracted'>): ConversationView {\n const blocks = view.blocks.filter((b) => !(isUnsettledSegment(b, turnId) && (segmentId === undefined || b.segmentId === segmentId)));\n return dropBuildingPlan(blocks.length === view.blocks.length ? view : { ...view, blocks }, turnId, segmentId);\n}\n\nfunction startSubagent(view: ConversationView, { subagentId, brief, model, turnId }: Message<'subagent_started'>): ConversationView {\n return updateSubagent(view, subagentId, turnId, (block) => ({ ...block, ...(brief ? { brief } : {}), ...(model ? { model } : {}) }));\n}\n\nfunction finishSubagent(view: ConversationView, { subagentId, outcome, digest, usage, turnId }: Message<'subagent_finished'>): ConversationView {\n return updateSubagent(view, subagentId, turnId, (block) => ({\n ...block, status: outcome, digest, children: block.children.map(sealed), ...(usage ? { usage } : {}),\n }));\n}\n\nfunction announceTool(view: ConversationView, call: Message<'research_step'>): ConversationView {\n // A spawn call names the subagent it starts; any other call tagged with a\n // subagent is that subagent's own.\n const spawns = call.tool === 'spawn_research_agent' ? call.subagentId : undefined;\n const [placed, lane] = laneOf(view, spawns ? undefined : call.subagentId, call.turnId);\n return appendOutput(placed, lane, (id) => ({ ...pendingTool(id, call), ...(spawns ? { spawns } : {}) }));\n}\n\n// By the call's id: results of a parallel round return in any order, and a\n// name match would put one call's output on another's row (ADR-0008). The\n// name scan survives only for calls that announced no id.\nfunction isPendingCallOf(block: DisplayBlock, step: ResearchStep): block is ToolBlock {\n if (block.type !== 'tool' || block.status !== 'pending') return false;\n return step.toolCallId ? block.toolCallId === step.toolCallId : block.toolCallId === undefined && block.tool === step.tool;\n}\n\nfunction settleTool(view: ConversationView, { step, turnId }: Message<'research_step_done'>): ConversationView {\n const [placed, lane] = laneOf(view, step.subagentId, turnId);\n const blocks = laneBlocks(placed, lane);\n const i = findLastIndex(blocks, (b) => isPendingCallOf(b, step));\n const pending = blocks[i];\n if (pending?.type === 'tool') return laneReplace(placed, lane, i, settledTool(pending, step));\n // The call that spawned a subagent is shown by the subagent's block, and\n // only the subagent's own finish settles it: a backgrounded agent's call\n // returns at launch, long before the work ends.\n if (lane === null && step.toolCallId && blocks.some((b) => b.type === 'subagent' && b.toolCallId === step.toolCallId)) return view;\n return laneAppend(placed, lane, (id) => toolFromStep(id, step, turnId));\n}\n\n/**\n * The building display shows one envelope: the segment streaming now. A turn's\n * later envelope (a plan after a read it asked for) starts it over, below\n * whatever the turn did in between, rather than running on from the last one.\n */\nfunction streamPlan(view: ConversationView, { token, turnId, segmentId }: Message<'plan_token'>): ConversationView {\n const i = findLastIndex(view.blocks, (b) => isBuildingPlan(b, turnId));\n const building = view.blocks[i];\n if (building?.type === 'plan' && building.segmentId === segmentId) {\n return { ...view, blocks: replaceAt(view.blocks, i, { ...building, text: building.text + token }) };\n }\n return append(dropBuildingPlan(view, turnId), (id) => ({\n type: 'plan', id, status: 'building', text: token, ...(turnId ? { turnId } : {}), ...(segmentId ? { segmentId } : {}),\n }));\n}\n\n/** A plan landed: the building display of the turn that committed it becomes its marker. */\nfunction markPlan(view: ConversationView, content: string, turnId: string | undefined): ConversationView {\n const marker = planMarker(content);\n const i = findLastIndex(view.blocks, (b) => isBuildingPlan(b, turnId));\n const building = view.blocks[i];\n if (building?.type === 'plan') {\n const settled: PlanBlock = { type: 'plan', id: building.id, ...marker, text: '', ...(building.turnId ? { turnId: building.turnId } : {}) };\n return { ...view, blocks: replaceAt(view.blocks, i, settled) };\n }\n return append(view, (id) => ({ type: 'plan', id, text: '', ...marker }));\n}\n\nfunction requestApproval(view: ConversationView, { id: approvalId, kind, subject, scope, detail, turnId }: Message<'approval_request'>): ConversationView {\n return append(view, (id) => ({\n type: 'approval', id, approvalId, kind, subject, scope, status: 'pending',\n ...(detail ? { detail } : {}),\n ...(turnId ? { turnId } : {}),\n }));\n}\n\nfunction settleApproval(view: ConversationView, { id, granted }: Message<'approval_settled'>): ConversationView {\n const i = findLastIndex(view.blocks, (b) => b.type === 'approval' && b.approvalId === id);\n const request = view.blocks[i];\n if (request?.type !== 'approval') return view;\n return { ...view, blocks: replaceAt(view.blocks, i, { ...request, status: granted ? 'granted' : 'denied', decidedBy: 'asked' }) };\n}\n\nfunction decideApproval(view: ConversationView, { kind, subject, scope, detail, granted, source }: Message<'approval_decided'>): ConversationView {\n return append(view, (id) => ({\n type: 'approval', id, kind, subject, scope, status: granted ? 'granted' : 'denied', decidedBy: source,\n ...(detail ? { detail } : {}),\n }));\n}\n\nfunction reportUsage(view: ConversationView, message: Message<'planner_usage'>): ConversationView {\n return isMeasured(message.totals) ? setUsageLine(view, message) : view;\n}\n\n/**\n * Nothing carries the turn's id after this, so whatever it left open is closed:\n * streaming stops, and on a stop or failure its unanswered calls and running\n * subagents say they were cut short. A plan reply that streamed as text (a\n * prose preamble decides a segment's route) gives way to its plan block, as a\n * settled message would have replaced it.\n */\nfunction endTurn(view: ConversationView, { turnId, outcome }: Message<'planner_turn_ended'>): ConversationView {\n let next = dropBuildingPlan(view, turnId);\n const planText = outcome === 'plan' ? finalSegmentIndex(next.blocks, turnId, true) : -1;\n if (planText >= 0) next = { ...next, blocks: next.blocks.filter((_, i) => i !== planText) };\n return closeOpenBlocks(next, outcome === 'stopped' ? 'stopped' : outcome === 'error' ? 'failed' : null);\n}\n\n// The summary is announced as a planner message before the transcript it now\n// heads arrives; the transcript records it as a notice, and so does the view.\nfunction markCompaction(view: ConversationView, summary: string): ConversationView {\n const i = findLastIndex(view.blocks, (b) => b.type === 'message' && b.role === 'planner' && b.turnId === undefined && b.text === summary);\n const announced = view.blocks[i];\n if (announced?.type === 'message') return { ...view, blocks: replaceAt(view.blocks, i, { ...announced, role: 'system' }) };\n return append(view, (id) => settledMessage(id, 'system', summary));\n}\n\nfunction syncTranscript(view: ConversationView, { plan, turnId }: Message<'plan_generated'>): ConversationView {\n const history = plan.conversationHistory ?? [];\n // Only the newest marker can be the committing turn's; older ones a surface\n // is only now catching up on belong to turns long over.\n const committed = findLastIndex(history, (e) => e.kind === 'plan_generated');\n let next = view;\n let latest = view.transcriptAt;\n for (const [i, entry] of history.entries()) {\n const isNew = view.transcriptAt === undefined || entry.timestamp > view.transcriptAt;\n if (isNew && entry.kind === 'plan_generated') next = markPlan(next, entry.content, i === committed ? turnId : undefined);\n if (isNew && entry.kind === 'system') next = append(next, (id) => settledMessage(id, 'system', entry.content));\n if (isNew && entry.kind === 'compaction') next = markCompaction(next, entry.content);\n if (latest === undefined || entry.timestamp > latest) latest = entry.timestamp;\n }\n return next === view && latest === view.transcriptAt ? view : { ...next, transcriptAt: latest };\n}\n\n/**\n * Fold one input into the view. Pure and incremental: blocks the input does\n * not touch keep their identity, and an input that changes nothing returns\n * `view` itself — surfaces memoize their drawing on that, and text deltas\n * arrive quickly.\n */\nexport function reduceConversation(view: ConversationView, input: ConversationInput): ConversationView {\n switch (input.type) {\n case 'local_entry':\n return append(view, (id) => settledMessage(id, input.role, input.text));\n case 'planner_turn_started':\n return input.prompt === undefined ? view : openTurn(view, input.turnId, input.prompt);\n case 'planner_text_delta':\n return streamText(view, input);\n case 'planner_message':\n return settleReply(view, input);\n case 'planner_text_retracted':\n return retractText(view, input);\n case 'planner_thinking_delta':\n return think(view, input.text, input.turnId, input.segmentId, input.subagentId);\n case 'research_step':\n return announceTool(view, input);\n case 'research_step_done':\n return settleTool(view, input);\n case 'subagent_started':\n return startSubagent(view, input);\n case 'subagent_finished':\n return finishSubagent(view, input);\n case 'plan_token':\n return streamPlan(view, input);\n case 'plan_generated':\n return syncTranscript(view, input);\n case 'planner_turn_ended':\n return endTurn(view, input);\n case 'approval_request':\n return requestApproval(view, input);\n case 'approval_settled':\n return settleApproval(view, input);\n case 'approval_decided':\n return decideApproval(view, input);\n case 'planner_usage':\n return reportUsage(view, input);\n default:\n return view;\n }\n}\n","import type { ConversationMessage, ResearchLogEntry, ResearchStep, SubagentLogEntry } from '../models/Task';\nimport { isMeasured, usageLine, type PlannerUsage } from '../models/Usage';\nimport type { DisplayBlock } from './blocks';\nimport { planMarker, settledMessage, subagentBlock, toolFromStep, usageBlock } from './records';\nimport type { ConversationView } from './reduce';\n\n/** One top-level block before it has an id, and where it falls in time. */\ninterface Placed {\n at: string;\n /** Breaks a tie in `at`: a user's message, then the research it caused, then what the turn settled on. */\n rank: number;\n build: (nextId: () => string) => DisplayBlock;\n}\n\nconst USER = 0;\nconst RESEARCH = 1;\nconst SETTLED = 2;\n\nfunction fromEntry(entry: ConversationMessage): Placed['build'] {\n if (entry.kind === 'plan_generated') return (id) => ({ type: 'plan', id: id(), text: '', ...planMarker(entry.content) });\n const role = entry.kind === 'system' || entry.kind === 'compaction' ? 'system' : entry.role === 'user' ? 'user' : 'planner';\n return (id) => settledMessage(id(), role, entry.content);\n}\n\nfunction spawnBrief(step: ResearchStep): string | undefined {\n try {\n const args: unknown = JSON.parse(step.args);\n const prompt = args && typeof args === 'object' ? (args as Record<string, unknown>).prompt : undefined;\n return typeof prompt === 'string' ? prompt.trim() : undefined;\n } catch {\n return undefined;\n }\n}\n\n/**\n * Which subagent a planner step started, if it did. The log keeps no link: a\n * harness planner's subagent takes its call's id, and an ADR-0005 research\n * agent is briefed with its spawn call's prompt, trimmed.\n */\nfunction spawnedBy(step: ResearchStep, unclaimed: SubagentLogEntry[]): SubagentLogEntry | undefined {\n const byId = unclaimed.find((e) => e.subagentId === step.toolCallId);\n if (byId) return byId;\n if (step.tool !== 'spawn_research_agent') return undefined;\n const brief = spawnBrief(step);\n return brief === undefined ? undefined : unclaimed.find((e) => e.brief === brief);\n}\n\nfunction research(log: readonly ResearchLogEntry[]): Placed[] {\n const subagents = log.filter((e): e is SubagentLogEntry => 'type' in e && e.type === 'subagent');\n const ownSubagent = new Set(subagents.map((e) => e.subagentId));\n const unclaimed = [...subagents];\n const spawnCall = new Map<string, string | undefined>();\n const placed: Placed[] = [];\n\n for (const entry of log) {\n if ('type' in entry) {\n if (entry.type === 'system') placed.push({ at: entry.timestamp, rank: RESEARCH, build: (id) => settledMessage(id(), 'system', entry.content) });\n continue;\n }\n if (entry.subagentId && ownSubagent.has(entry.subagentId)) continue;\n const spawned = spawnedBy(entry, unclaimed);\n if (spawned) {\n unclaimed.splice(unclaimed.indexOf(spawned), 1);\n spawnCall.set(spawned.subagentId, entry.toolCallId);\n continue;\n }\n placed.push({ at: entry.timestamp, rank: RESEARCH, build: (id) => toolFromStep(id(), entry) });\n }\n\n // A subagent stands where it started, which is where its spawn call was\n // announced; the call's own record is written only once the subagent is done.\n for (const entry of subagents) {\n const toolCallId = spawnCall.get(entry.subagentId);\n const steps = log.filter((e): e is ResearchStep => !('type' in e) && e.subagentId === entry.subagentId);\n placed.push({\n at: entry.timestamp,\n rank: RESEARCH,\n build: (id) => subagentBlock(id(), {\n subagentId: entry.subagentId, brief: entry.brief, status: entry.outcome, digest: entry.digest,\n children: steps.map((step) => toolFromStep(id(), step)),\n toolCallId, model: entry.model, usage: entry.usage,\n }),\n });\n }\n return placed;\n}\n\n/**\n * The view a saved session reopens with: what its transcript and research log\n * kept, in the order it happened — messages, notices and the compaction\n * summary, plan markers, tool calls with each subagent's calls nested under\n * it, and the token line. Reasoning and streamed text were never saved, so a\n * reload has none.\n *\n * The research log interleaves with the transcript by time. Entries older than\n * a compaction's first kept message went with the turns it condensed.\n */\nexport function fromTranscript(\n conversationHistory: readonly ConversationMessage[] | undefined,\n researchLog: readonly ResearchLogEntry[] | undefined,\n plannerUsage?: PlannerUsage,\n): ConversationView {\n const history = conversationHistory ?? [];\n const compacted = history[0]?.kind === 'compaction' ? history[0] : undefined;\n const kept = compacted ? history.slice(1) : history;\n const cutoff = compacted ? (kept[0]?.timestamp ?? compacted.timestamp) : '';\n\n const placed: Placed[] = [\n ...kept.map((entry) => ({ at: entry.timestamp, rank: entry.role === 'user' && !entry.kind ? USER : SETTLED, build: fromEntry(entry) })),\n ...research(researchLog ?? []).filter((item) => item.at >= cutoff),\n ];\n placed.sort((a, b) => (a.at < b.at ? -1 : a.at > b.at ? 1 : a.rank - b.rank));\n // The summary stands where the condensed turns were, ahead of everything kept.\n if (compacted) placed.unshift({ at: '', rank: SETTLED, build: fromEntry(compacted) });\n\n let nextId = 1;\n const id = () => `b${nextId++}`;\n const blocks = placed.map((item) => item.build(id));\n\n if (plannerUsage && isMeasured(plannerUsage.totals)) blocks.push(usageBlock(id(), usageLine(plannerUsage)));\n\n const transcriptAt = history.reduce<string | undefined>((latest, e) => (latest === undefined || e.timestamp > latest ? e.timestamp : latest), undefined);\n return { blocks, nextId, ...(transcriptAt ? { transcriptAt } : {}) };\n}\n","import type { ResearchToolType } from '../../models/Task';\n\n/**\n * Map a coding agent's own tool name onto Ordewell's research vocabulary\n * (ADR-0009, T5).\n *\n * A harness planner brings its own toolbox, and most of it overlaps ours: a\n * `Read` is a `read_file`, a `Grep` is a `grep`. Those map onto existing\n * members and render in every surface through the code path that is already\n * there. Everything else — `Edit`, `WebFetch`, `TodoWrite`, `Task`, whatever\n * ships next — becomes `agent_tool` carrying its real name.\n *\n * Nothing is relabelled as a tool it is not. Calling a web fetch a `bash` to\n * avoid adding a union member would put a lie in the timeline, which is the\n * point ADR-0008 spent its effort establishing.\n */\nconst KNOWN_TOOLS: Record<string, ResearchToolType> = {\n // Claude Code\n read: 'read_file',\n grep: 'grep',\n glob: 'glob',\n bash: 'bash',\n ls: 'list_dir',\n webfetch: 'fetch',\n websearch: 'web_search',\n // Codex — its sandboxed command tool is a shell by another name\n shell: 'bash',\n exec_command: 'bash',\n local_shell: 'bash',\n // OpenCode\n list: 'list_dir',\n fetch: 'fetch',\n};\n\nexport interface MappedTool {\n tool: ResearchToolType;\n /** The agent's own name, kept whenever it differs from the member it mapped to. */\n toolLabel?: string;\n}\n\n/**\n * Classify one agent tool name. Case- and separator-insensitive, because the\n * three agents disagree on `Read` vs `read` vs `read_file` for the same thing.\n */\nexport function mapAgentTool(name: string): MappedTool {\n const normalized = name.trim().toLowerCase().replace(/[_-]/g, '');\n const direct = KNOWN_TOOLS[normalized] ?? KNOWN_TOOLS[name.trim().toLowerCase()];\n if (direct) {\n // The label survives even on a mapped tool: `Read` and `read_file` render\n // the same way, but the transcript should still say which one ran.\n return { tool: direct, toolLabel: name };\n }\n return { tool: 'agent_tool', toolLabel: name };\n}\n\n/**\n * Normalize an agent's tool arguments into the shapes `summarizeToolCall`\n * already knows how to read, so a harness `Read` gets a one-line summary\n * instead of a bare tool name. Unknown keys are preserved — the raw args stay\n * visible under the surfaces' output chevron.\n */\nexport function normalizeAgentArgs(tool: ResearchToolType, args: Record<string, unknown>): Record<string, unknown> {\n const out = { ...args };\n const alias = (from: string, to: string) => {\n if (out[to] === undefined && out[from] !== undefined) out[to] = out[from];\n };\n if (tool === 'read_file' || tool === 'list_dir') {\n alias('filePath', 'path');\n alias('file_path', 'path');\n alias('target', 'path');\n }\n if (tool === 'bash') {\n alias('cmd', 'command');\n alias('script', 'command');\n // Codex hands the command over as an argv array; the summary wants a string.\n if (typeof out.command !== 'string' && Array.isArray(out.command)) {\n out.command = (out.command as unknown[]).join(' ');\n }\n }\n if (tool === 'fetch') alias('uri', 'url');\n if (tool === 'web_search') alias('q', 'query');\n return out;\n}\n","import type { QueuedTaskMessage, StructuredTurnEnd } from '../interfaces/ITerminalRunner';\nimport type { TaskLogEvent } from '../models/TaskLog';\nimport { addPlannerUsage, isMeasured, usageLine, type PlannerUsage } from '../models/Usage';\nimport { mapAgentTool, normalizeAgentArgs } from '../services/harness/agentTools';\nimport type { ApprovalBlock, MessageBlock, SubagentChild, SubagentStatus, ThinkingDisplayBlock } from './blocks';\nimport { toolHeadline } from './format';\nimport {\n append, appendOutput, replaceAt, closeOpenBlocks, findLastIndex, laneAppend, laneBlocks, laneOf, laneReplace, setUsageLine, updateSubagent, sealed,\n type BlockList, type Lane,\n} from './lanes';\nimport { finishedTool, pendingTool, settledMessage } from './records';\n\n/**\n * What a surface draws for one attempt of a structured task (ADR-0018, O1b):\n * the same display blocks as the planner conversation, built from the task's\n * {@link TaskLogEvent}s. No planner turn ids — a task's turns are its own,\n * one at a time.\n */\nexport interface TaskLogView extends BlockList {\n /** Messages waiting for the running turn to end, oldest first. */\n readonly queued: readonly QueuedTaskMessage[];\n /** A turn is running. */\n readonly working: boolean;\n /** How the last turn ended; absent before the first one does. */\n readonly lastTurnEnd?: StructuredTurnEnd;\n /** What the runner reported over the attempt, subagents included. */\n readonly usage?: PlannerUsage;\n /**\n * The block each run of deltas streams into, keyed by stream, so the run's\n * authoritative copy replaces it instead of repeating it below.\n */\n readonly streams: Readonly<Record<string, string>>;\n}\n\nexport const EMPTY_TASK_LOG: TaskLogView = { blocks: [], nextId: 1, queued: [], working: false, streams: {} };\n\ntype Event<T extends TaskLogEvent['type']> = Extract<TaskLogEvent, { type: T }>;\n\nconst TEXT_STREAM = 'text';\nconst thinkingStream = (subagentId: string | undefined) => `thinking:${subagentId ?? ''}`;\n\nfunction withoutStream(view: TaskLogView, key: string): TaskLogView {\n if (!(key in view.streams)) return view;\n return { ...view, streams: Object.fromEntries(Object.entries(view.streams).filter(([k]) => k !== key)) };\n}\n\n// A later text block of the same turn opens with a paragraph break (the\n// adapter joins a turn's messages that way); as a block of its own it needs none.\nfunction blockText(text: string): string {\n return text.replace(/^\\n+/, '');\n}\n\ntype Streamed = MessageBlock | ThinkingDisplayBlock;\n\n/** The lane's streaming block for `key`, while it is still the lane's latest output and still streaming. */\nfunction openStream(view: TaskLogView, lane: Lane, key: string): number {\n const id = view.streams[key];\n if (id === undefined) return -1;\n const blocks = laneBlocks(view, lane);\n const i = findLastIndex(blocks, (b) => b.type !== 'usage');\n const block = blocks[i];\n return block?.id === id && (block.type === 'message' || block.type === 'thinking') && block.streaming ? i : -1;\n}\n\nfunction streamDelta(view: TaskLogView, lane: Lane, key: string, text: string, make: (id: string, text: string) => Streamed): TaskLogView {\n const i = openStream(view, lane, key);\n const open = laneBlocks(view, lane)[i];\n if (open?.type === 'message' || open?.type === 'thinking') return laneReplace(view, lane, i, { ...open, text: open.text + text });\n const next = appendOutput(view, lane, (id) => make(id, blockText(text)));\n return { ...next, streams: { ...next.streams, [key]: `b${view.nextId}` } };\n}\n\n/**\n * A run's authoritative copy: it replaces the text its deltas built, wherever\n * that block now sits in the lane, and is added whole when nothing streamed.\n */\nfunction streamWhole(view: TaskLogView, lane: Lane, key: string, text: string, make: (id: string) => SubagentChild): TaskLogView {\n const id = view.streams[key];\n const blocks = laneBlocks(view, lane);\n const i = id === undefined ? -1 : findLastIndex(blocks, (b) => b.id === id);\n const streamed = blocks[i];\n const next = withoutStream(view, key);\n if (streamed?.type === 'message' || streamed?.type === 'thinking') {\n return laneReplace(next, lane, i, { ...streamed, text: blockText(text), streaming: false });\n }\n return appendOutput(next, lane, make);\n}\n\nfunction agentText(view: TaskLogView, text: string): TaskLogView {\n return streamWhole(view, null, TEXT_STREAM, text, (id) => settledMessage(id, 'agent', blockText(text)));\n}\n\nfunction agentTextDelta(view: TaskLogView, text: string): TaskLogView {\n return streamDelta(view, null, TEXT_STREAM, text, (id, t) => ({ type: 'message', id, role: 'agent', text: t, streaming: true }));\n}\n\nfunction thinkingBlock(id: string, text: string, streaming: boolean, subagentId: string | undefined): ThinkingDisplayBlock {\n return { type: 'thinking', id, text, streaming, ...(subagentId ? { subagentId } : {}) };\n}\n\nfunction thinkingDelta(view: TaskLogView, { text, subagentId }: Event<'thinking_delta'>): TaskLogView {\n const [placed, lane] = laneOf(view, subagentId);\n return streamDelta(placed, lane, thinkingStream(subagentId), text, (id, t) => thinkingBlock(id, t, true, subagentId));\n}\n\nfunction thinkingWhole(view: TaskLogView, { text, subagentId }: Event<'thinking'>): TaskLogView {\n const [placed, lane] = laneOf(view, subagentId);\n return streamWhole(placed, lane, thinkingStream(subagentId), text, (id) => thinkingBlock(id, blockText(text), false, subagentId));\n}\n\nfunction announceTool(view: TaskLogView, { id: toolCallId, name, args, subagentId }: Event<'tool_call'>): TaskLogView {\n const { tool, toolLabel } = mapAgentTool(name);\n const [placed, lane] = laneOf(view, subagentId);\n return appendOutput(placed, lane, (id) => {\n const call = pendingTool(id, { tool, toolLabel, args, toolCallId });\n const parsed = parseArgs(args);\n // Headline from the normalized arguments, as the harness planner's rows\n // are, so a `Read` or a Codex argv reads the same in both views.\n return parsed ? { ...call, headline: toolHeadline(tool, JSON.stringify(normalizeAgentArgs(tool, parsed)), toolLabel) } : call;\n });\n}\n\nfunction parseArgs(args: string): Record<string, unknown> | null {\n try {\n const parsed: unknown = JSON.parse(args);\n return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed as Record<string, unknown> : null;\n } catch {\n return null;\n }\n}\n\nfunction settleTool(view: TaskLogView, { id: toolCallId, output, success, subagentId }: Event<'tool_result'>): TaskLogView {\n const [placed, lane] = laneOf(view, subagentId);\n const blocks = laneBlocks(placed, lane);\n const i = findLastIndex(blocks, (b) => b.type === 'tool' && b.status === 'pending' && b.toolCallId === toolCallId);\n const pending = blocks[i];\n const outcome = success ? 'success' : 'failure';\n if (pending?.type === 'tool') return laneReplace(placed, lane, i, finishedTool(pending, output, outcome));\n // The call that spawned a subagent is shown by the subagent's block, and\n // only the subagent's own finish settles it: a backgrounded agent's call\n // returns at launch, long before the work ends.\n if (lane === null && blocks.some((b) => b.type === 'subagent' && b.toolCallId === toolCallId)) return view;\n return laneAppend(placed, lane, (id) => finishedTool(pendingTool(id, { tool: 'agent_tool', toolLabel: 'Tool', args: '', toolCallId }), output, outcome));\n}\n\nfunction startSubagent(view: TaskLogView, { subagentId, brief, model }: Event<'subagent_started'>): TaskLogView {\n return updateSubagent(view, subagentId, undefined, (block) => ({ ...block, ...(brief ? { brief } : {}), ...(model ? { model } : {}) }));\n}\n\nfunction finishSubagent(view: TaskLogView, { subagentId, outcome, digest }: Event<'subagent_finished'>): TaskLogView {\n const usage = view.usage?.bySubagent?.[subagentId];\n const next = updateSubagent(view, subagentId, undefined, (block) => ({\n ...block, status: outcome, digest, children: block.children.map(sealed), ...(usage ? { usage } : {}),\n }));\n return withoutStream(next, thinkingStream(subagentId));\n}\n\nfunction reportUsage(view: TaskLogView, { record }: Event<'usage'>): TaskLogView {\n const usage = addPlannerUsage(view.usage ?? { totals: {} }, record);\n const next = { ...view, usage };\n return isMeasured(usage.totals) ? setUsageLine(next, usageLine(usage)) : next;\n}\n\n/** A runner's tool request, read the way its call's row reads: `Bash(npm test)`. */\nexport function runnerToolSubject(tool: string, args: string): string {\n const { tool: mapped, toolLabel } = mapAgentTool(tool);\n const parsed = parseArgs(args);\n const headline = toolHeadline(mapped, parsed ? JSON.stringify(normalizeAgentArgs(mapped, parsed)) : args, toolLabel);\n return headline.keyArg ? `${headline.name}(${headline.keyArg})` : headline.name;\n}\n\nfunction requestApproval(view: TaskLogView, { approvalId, tool, args, allowForTask, toolCallId }: Event<'approval_requested'>): TaskLogView {\n if (view.blocks.some((b) => b.type === 'approval' && b.approvalId === approvalId)) return view;\n return append(view, (id) => ({\n type: 'approval', id, approvalId, kind: 'runner_tool',\n subject: runnerToolSubject(tool, args),\n scope: tool,\n status: 'pending',\n ...(allowForTask ? { allowForTask } : {}),\n ...(toolCallId ? { toolCallId } : {}),\n }));\n}\n\nfunction settleApproval(view: TaskLogView, approvalId: string, settle: (block: ApprovalBlock) => ApprovalBlock): TaskLogView {\n const i = findLastIndex(view.blocks, (b) => b.type === 'approval' && b.approvalId === approvalId);\n const block = view.blocks[i];\n if (block?.type !== 'approval' || block.status !== 'pending') return view;\n return { ...view, blocks: replaceAt(view.blocks, i, settle(block)) };\n}\n\nfunction decideApproval(view: TaskLogView, { approvalId, decision, note }: Event<'approval_decided'>): TaskLogView {\n return settleApproval(view, approvalId, (block) => ({\n ...block,\n status: decision === 'deny' ? 'denied' : 'granted',\n decidedBy: 'asked',\n ...(decision === 'allowForTask' ? { forTask: true } : {}),\n ...(note ? { note } : {}),\n }));\n}\n\nfunction startTurn(view: TaskLogView, { message, messageId }: Event<'turn_start'>): TaskLogView {\n const queued = messageId ? view.queued.filter((m) => m.id !== messageId) : view.queued;\n return append({ ...view, queued, working: true }, (id) => settledMessage(id, 'user', message));\n}\n\nconst CUT: Record<StructuredTurnEnd, SubagentStatus | null> = { completed: null, interrupted: 'stopped', failed: 'failed' };\n\nfunction endTurn(view: TaskLogView, { reason }: Event<'turn_end'>): TaskLogView {\n const closed = closeOpenBlocks({ ...view, working: false, lastTurnEnd: reason, streams: {} }, CUT[reason]);\n return reason === 'interrupted' ? append(closed, (id) => settledMessage(id, 'system', 'Interrupted.')) : closed;\n}\n\nfunction queueMessage(view: TaskLogView, { messageId, text }: Event<'message_queued'>): TaskLogView {\n if (view.queued.some((m) => m.id === messageId)) return view;\n return { ...view, queued: [...view.queued, { id: messageId, text }] };\n}\n\nfunction unqueueMessage(view: TaskLogView, { messageId }: Event<'message_removed'>): TaskLogView {\n const queued = view.queued.filter((m) => m.id !== messageId);\n return queued.length === view.queued.length ? view : { ...view, queued };\n}\n\n/**\n * Fold one task-log event into the view. Pure and incremental, like\n * `reduceConversation`: an event that changes nothing returns `view` itself,\n * and an event of a type this build does not know is skipped — a log written\n * by a newer Ordewell still reads.\n */\nexport function reduceTaskLog(view: TaskLogView, event: TaskLogEvent): TaskLogView {\n switch (event.type) {\n case 'turn_start': return startTurn(view, event);\n case 'turn_end': return endTurn(view, event);\n case 'text_delta': return agentTextDelta(view, event.text);\n case 'text': return agentText(view, event.text);\n case 'thinking_delta': return thinkingDelta(view, event);\n case 'thinking': return thinkingWhole(view, event);\n case 'tool_call': return announceTool(view, event);\n case 'tool_result': return settleTool(view, event);\n case 'subagent_started': return startSubagent(view, event);\n case 'subagent_finished': return finishSubagent(view, event);\n case 'usage': return reportUsage(view, event);\n case 'message_queued': return queueMessage(view, event);\n case 'message_removed': return unqueueMessage(view, event);\n case 'error': return appendOutput(view, null, (id) => settledMessage(id, 'error', event.message));\n case 'approval_requested': return requestApproval(view, event);\n case 'approval_decided': return decideApproval(view, event);\n case 'approval_withdrawn': return settleApproval(view, event.approvalId, (block) => ({ ...block, status: 'withdrawn' }));\n default: return view;\n }\n}\n\n/** A whole attempt's events, folded from the start — what a reload shows. */\nexport function replayTaskLog(events: readonly TaskLogEvent[], from: TaskLogView = EMPTY_TASK_LOG): TaskLogView {\n return events.reduce(reduceTaskLog, from);\n}\n","/**\n * Prompts the user sent while a planner turn was in flight, waiting to be the\n * next turn's input. The hold is a plain immutable list, oldest first, so a\n * pure reducer can keep it in state and a host can keep it in a field — and\n * either can draw it as is.\n */\nexport type PromptHold = readonly string[];\n\nexport const EMPTY_HOLD: PromptHold = [];\n\n/** One prompt taken out of the hold, and what is left behind. */\nexport interface TakenPrompt {\n text: string;\n rest: PromptHold;\n}\n\nexport function holdPrompt(hold: PromptHold, text: string): PromptHold {\n return [...hold, text];\n}\n\n/** The prompt a settled turn sends next: the oldest, so they go in the order typed. */\nexport function drainNext(hold: PromptHold): TakenPrompt | undefined {\n const [text, ...rest] = hold;\n return text === undefined ? undefined : { text, rest };\n}\n\n/**\n * Takes back the newest prompt — the one the user most likely just regretted —\n * while the older ones stay queued behind the turn.\n */\nexport function unsendLatest(hold: PromptHold): TakenPrompt | undefined {\n if (hold.length === 0) return undefined;\n return { text: hold[hold.length - 1], rest: hold.slice(0, -1) };\n}\n\n/**\n * A stopped turn takes its queue with it: the prompts were written against a\n * turn that no longer exists, so they come back as one draft to edit and\n * resend rather than firing off after the stop.\n */\nexport function unsendAll(hold: PromptHold): TakenPrompt | undefined {\n if (hold.length === 0) return undefined;\n return { text: hold.join('\\n'), rest: EMPTY_HOLD };\n}\n\n/** Where an unsent prompt lands in the drafting input: above what is being typed, never replacing it. */\nexport function aheadOfDraft(text: string, draft: string): string {\n return draft ? `${text}\\n${draft}` : text;\n}\n","import type { SessionMessage } from '../services/SessionMessage';\nimport { reduceConversation, type ConversationInput, type ConversationView } from './reduce';\n\n/**\n * The planner turn a surface has open, and the one its user stopped. Kept\n * beside the view rather than in it: the view is what a turn drew, this is\n * whether the surface still wants to hear from it.\n */\nexport interface TurnGate {\n readonly open: string | null;\n readonly stopped: string | null;\n}\n\nexport const NO_TURN: TurnGate = { open: null, stopped: null };\n\nexport interface GatedConversation {\n view: ConversationView;\n gate: TurnGate;\n}\n\n/** What a turn streams — everything a stop cuts off. Usage, approvals and transcript markers are facts and still land. */\nconst TURN_STREAM = new Set<SessionMessage['type']>([\n 'planner_text_delta', 'planner_thinking_delta', 'planner_text_retracted', 'planner_message', 'plan_token',\n 'research_step', 'research_step_done', 'subagent_started', 'subagent_finished', 'planner_turn_ended',\n]);\n\n// A message with no turn id while a stop stands can only be the stopped\n// turn's: a new turn announces itself first, and that lifts the stop.\nfunction cutOff(gate: TurnGate, input: ConversationInput): boolean {\n if (gate.stopped === null || !TURN_STREAM.has(input.type as SessionMessage['type'])) return false;\n const turnId = 'turnId' in input ? input.turnId : undefined;\n return turnId === undefined || turnId === gate.stopped;\n}\n\nfunction withGate(live: GatedConversation, gate: TurnGate): GatedConversation {\n return gate.open === live.gate.open && gate.stopped === live.gate.stopped ? live : { ...live, gate };\n}\n\n/**\n * One input into the view, the stop rule applied: a turn the user stopped is\n * over on screen, and whatever it streams until the backend notices is\n * dropped. An input that changes nothing hands back the same view and gate.\n */\nexport function followTurn(view: ConversationView, gate: TurnGate, input: ConversationInput): GatedConversation {\n const live = { view, gate };\n if (input.type === 'planner_turn_started') {\n return withGate({ view: reduceConversation(view, input), gate }, { open: input.turnId, stopped: null });\n }\n if (cutOff(gate, input)) {\n return input.type === 'planner_turn_ended' ? withGate(live, { ...gate, stopped: null }) : live;\n }\n const next = { view: reduceConversation(view, input), gate };\n return input.type === 'planner_turn_ended' && input.turnId === gate.open ? withGate(next, { ...gate, open: null }) : next;\n}\n\n/**\n * The user stopped the planner. The turn ends on screen now rather than when\n * the backend notices the abort, so the surface is free at once and the\n * turn's late output never lands. Nothing changes when no turn is open.\n */\nexport function stopTurn(view: ConversationView, gate: TurnGate): GatedConversation {\n if (gate.open === null) return { view, gate };\n return {\n view: reduceConversation(view, { type: 'planner_turn_ended', turnId: gate.open, outcome: 'stopped' }),\n gate: { open: null, stopped: gate.open },\n };\n}\n","import type { DisplayBlock } from './blocks';\n\n/**\n * Whether the conversation holds anything the collapsed view hides — thinking,\n * a command row's arguments and output, a subagent's children and digest. The\n * detail toggle is the only expansion control (ADR-0017, X1), so a surface\n * offers it only when it would do something.\n */\nexport function hasHiddenDetail(blocks: readonly DisplayBlock[]): boolean {\n return blocks.some((b) => b.type === 'thinking' || b.type === 'tool' || b.type === 'subagent');\n}\n","/** The conversation line for a task starting, worded the same on every surface. */\nexport function taskStartedNotice(title: string, runner?: string): string {\n return runner ? `Started \"${title}\" · ${runner}` : `Started \"${title}\"`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;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,MAAM,KAAK,GAAG,KAAK;AAC3D,SAAO,MAAM,SAAS,wBAAwB,MAAM,WAAW,KAAK,OAAO,SAAS,MAAM,EAAE,GAAG,MAAM,MAAM,KAAK,CAAC,IAAI;AACvH;AAOO,SAAS,SAAS,MAAwB,MAAmC;AAClF,MAAI,KAAK,SAAS,KAAM,QAAO,EAAE,MAAM,KAAK;AAC5C,SAAO;AAAA,IACL,MAAM,mBAAmB,MAAM,EAAE,MAAM,sBAAsB,QAAQ,KAAK,MAAM,SAAS,UAAU,CAAC;AAAA,IACpG,MAAM,EAAE,MAAM,MAAM,SAAS,KAAK,KAAK;AAAA,EACzC;AACF;;;AC1DO,SAAS,gBAAgB,QAA0C;AACxE,SAAO,OAAO,KAAK,CAAC,MAAM,EAAE,SAAS,cAAc,EAAE,SAAS,UAAU,EAAE,SAAS,UAAU;AAC/F;;;ACTO,SAAS,kBAAkB,OAAe,QAAyB;AACxE,SAAO,SAAS,YAAY,KAAK,UAAO,MAAM,KAAK,YAAY,KAAK;AACtE;","names":["text","oneLine","text","text","text","text","text","announceTool","parseArgs","settleTool","startSubagent","finishSubagent","reportUsage","requestApproval","settleApproval","decideApproval","endTurn","text"]}
|