Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233/** Task-list release for forced continuation. A session whose tracked tasks are all finished has nothing for another turn to advance, so blocking its stop buys a restatement rather than work. State is replayed from the transcript because the `Stop` payload carries background tasks but not the task list. `TaskCreate` results announce an id, and `TaskUpdate` calls carry `taskId` and `status`, so the latest status per id reconstructs the list without a sidecar. An absent task list is deliberately not treated as finished. Most sessions never create a task, and releasing on an empty list would disable forced continuation for all of them. @module */
import type { TranscriptRecord, } from '@monochromatic-dev/claude-code-plugin-hook-type/ts';
import { parseRecord, UNPARSABLE,} from './continuation-depth.ts';
/** Statuses meaning a task needs no further work. Observed vocabulary across this repository's transcripts is `pending`, `in_progress`, `completed`, and `deleted`. A deleted task is finished in the sense that matters here: nothing remains to do for it. */const FINISHED_STATUSES: ReadonlySet<string> = new Set([ 'completed', 'deleted',],);
/** Status a task carries between creation and its first update. */const INITIAL_STATUS = 'pending' as const;
/** Outcome of reading task state from a transcript. `no-task-list` is distinct from `all-finished` because an absent list must not release, while a list whose every entry is done must. */type TaskListState = 'no-task-list' | 'all-finished' | 'work-remains';
/** Replays task state from transcript records. Scans oldest to newest so later updates win. Only successful calls matter, but a failed `TaskUpdate` merely records a status that a later call corrects, so result inspection is not needed for the release decision. @param transcriptLines - transcript JSONL lines, oldest first @returns whether the list is absent, entirely finished, or still has work @example ```ts taskListState(lines); // 'all-finished' ``` */function taskListState(transcriptLines: readonly string[],): TaskListState { /** Latest known status per task id. */ const statusById = new Map<string, string>();
for (const line of transcriptLines) { if (line === '') { continue; }
/** Parsed record, or the sentinel for a truncated line. */ const record = parseRecord(line,);
if (record === UNPARSABLE) { continue; } recordTaskCreation({ record, statusById, },); recordTaskUpdate({ record, statusById, },); }
if (statusById.size === 0) { return 'no-task-list'; } return [...statusById.values(),].every(isFinishedStatus,) ? 'all-finished' : 'work-remains';}
/** Reports whether a status means no further work is needed. @param status - task status from a `TaskUpdate` call @returns whether this status is terminal @example ```ts isFinishedStatus('completed'); // true ``` */function isFinishedStatus(status: string,): boolean { return FINISHED_STATUSES.has(status,);}
/** Registers a task announced by a `TaskCreate` result. @param record - parsed transcript record @param statusById - accumulator mutated with newly created task ids @mutates statusById - inserts newly created task ids at their initial status @example ```ts recordTaskCreation(record, new Map()); ``` */function recordTaskCreation( { record, statusById, }: { readonly record: TranscriptRecord; readonly statusById: Map<string, string>; },): void { /** Task envelope present on a successful `TaskCreate` result. */ const task = record .toolUseResult ?.task; /** Identifier Claude Code assigned, absent on every other record. */ const id = task?.id;
if (((typeof id) !== 'string') || statusById.has(id,)) { return; } statusById.set( id, INITIAL_STATUS, );}
/** Applies a `TaskUpdate` call to the replayed state. @param record - parsed transcript record @param statusById - accumulator mutated with the updated status @mutates statusById - overwrites the status for the referenced task id @example ```ts recordTaskUpdate(record, new Map()); ``` */function recordTaskUpdate( { record, statusById, }: { readonly record: TranscriptRecord; readonly statusById: Map<string, string>; },): void { /** Assistant content blocks, an array only for structured messages. */ const content = record .message ?.content;
if ((content === undefined) || ((typeof content) === 'string')) { return; } for (const call of content) { /** Task identifier the call addresses, absent on every other tool. */ const taskId = call .input ?.taskId; /** Status the call applies. */ const status = call .input ?.status;
if ((call.type !== 'tool_use') || (call.name !== 'TaskUpdate') || ((typeof taskId) !== 'string') || ((typeof status) !== 'string')) { continue; } statusById.set( taskId, status, ); }}
export type { TaskListState, };
export { FINISHED_STATUSES, INITIAL_STATUS, isFinishedStatus, taskListState,};