/** * WorkTree — Hierarchical task decomposition tree with rollup stats * * Maintains a rooted tree of work items where each node can spawn children. * Status, tokens, and progress roll up automatically from leaves to root. * Integrates with {@link TopologyTracker} to add `subtask` edges and sync * agent status. * * @module WorkTree * @version 1.0.0 */ import { EventEmitter } from 'events'; /** Status of a work node */ export type WorkNodeStatus = 'pending' | 'running' | 'completed' | 'failed' | 'skipped' | 'blocked'; /** A single node in the work tree */ export interface WorkNode { /** Unique work-item identifier */ id: string; /** Parent work-item id (undefined for root) */ parentId?: string; /** Human-readable label */ label: string; /** Agent assigned to this work item (if any) */ agent?: string; /** Current status */ status: WorkNodeStatus; /** Depth in the tree (root = 0) */ depth: number; /** Direct children ids (ordered) */ children: string[]; /** Tokens consumed by this node alone (not including children) */ ownTokens: number; /** Rollup: total tokens including all descendants */ totalTokens: number; /** Rollup: progress 0–1 based on completed descendants */ progress: number; /** ISO 8601 created timestamp */ createdAt: string; /** ISO 8601 last status change */ updatedAt: string; /** Arbitrary metadata */ metadata: Record; } /** Summary statistics for the entire tree */ export interface WorkTreeStats { /** Total node count */ total: number; /** By status */ pending: number; running: number; completed: number; failed: number; skipped: number; blocked: number; /** Overall progress 0–1 */ progress: number; /** Aggregate tokens across all nodes */ totalTokens: number; /** Max tree depth */ maxDepth: number; } /** Flat representation of the tree for serialisation */ export interface WorkTreeSnapshot { /** Root node id */ rootId: string; /** All nodes keyed by id */ nodes: Record; /** Tree-wide stats */ stats: WorkTreeStats; /** ISO 8601 snapshot timestamp */ timestamp: string; } /** Events emitted by WorkTree */ export interface WorkTreeEvents { 'node:added': (node: WorkNode) => void; 'node:removed': (id: string) => void; 'node:status': (id: string, status: WorkNodeStatus, prev: WorkNodeStatus) => void; 'node:tokens': (id: string, ownTokens: number, totalTokens: number) => void; 'node:progress': (id: string, progress: number) => void; 'tree:complete': (stats: WorkTreeStats) => void; } /** Options for auto-status rollup behaviour */ export interface WorkTreeOptions { /** Auto-complete a parent when all children complete (default: true) */ autoCompleteParent?: boolean; /** Auto-fail a parent when any child fails (default: false) */ autoFailParent?: boolean; /** Auto-block children when parent is not running (default: true) */ autoBlockChildren?: boolean; } /** * Hierarchical task decomposition tree. * * ```typescript * const tree = new WorkTree('root', 'Build feature'); * tree.addChild('root', { id: 'design', label: 'Design API' }); * tree.addChild('root', { id: 'impl', label: 'Implement' }); * tree.addChild('impl', { id: 'impl-auth', label: 'Auth module', agent: 'worker-1' }); * tree.addChild('impl', { id: 'impl-db', label: 'DB schema', agent: 'worker-2' }); * * tree.setStatus('impl-auth', 'running'); * tree.setStatus('impl-auth', 'completed'); * // impl.progress → 0.5 (1 of 2 children done) * ``` */ export declare class WorkTree extends EventEmitter { private nodes; private readonly rootId; private readonly opts; constructor(rootId: string, rootLabel: string, options?: WorkTreeOptions); /** * Add a child work item under a parent. * @throws if parentId does not exist or id is already taken. */ addChild(parentId: string, child: { id: string; label: string; agent?: string; metadata?: Record; }): WorkNode; /** * Remove a node and all its descendants. * Cannot remove the root. * @returns number of nodes removed */ removeSubtree(id: string): number; /** * Set the status of a work node. Triggers rollup. */ setStatus(id: string, status: WorkNodeStatus): void; /** * Add tokens to a work node. Rolls up totalTokens to ancestors. */ addTokens(id: string, tokens: number): void; /** Get a single node (copy). */ getNode(id: string): WorkNode | undefined; /** Get the root node. */ getRoot(): WorkNode; /** Get direct children of a node. */ getChildren(id: string): WorkNode[]; /** Get all ancestors from node to root (excluding the node itself). */ getAncestors(id: string): WorkNode[]; /** Get all descendants (depth-first). */ getDescendants(id: string): WorkNode[]; /** Get all leaf nodes (no children). */ getLeaves(): WorkNode[]; /** Total node count. */ size(): number; /** The root node id. */ getRootId(): string; /** Compute tree-wide statistics. */ stats(): WorkTreeStats; /** Full snapshot for serialisation. */ snapshot(): WorkTreeSnapshot; /** * Flatten the tree into a depth-first ordered array (for rendering). */ flatten(): WorkNode[]; /** * Build the tree from a flat list of tasks with dependencies. * Creates a virtual root if multiple top-level tasks exist. * Compatible with TaskDAG nodes from GoalDecomposer. */ static fromTaskList(tasks: Array<{ id: string; description: string; agent?: string; dependencies: string[]; metadata?: Record; }>, rootLabel?: string, options?: WorkTreeOptions): WorkTree; /** Collect all node ids in a subtree (including the root of the subtree). */ private collectSubtree; /** Re-compute progress and auto-status from a node up to root. */ private rollupFrom; /** Recompute totalTokens for a single node from its children. */ private recomputeTokens; /** Rollup token totals from a node up to root. */ private rollupTokensFrom; /** Block all pending descendants of a node. */ private blockDescendants; /** Check if the entire tree is done and emit tree:complete. */ private checkTreeComplete; } //# sourceMappingURL=work-tree.d.ts.map