/** * Shared Context Tree — the file-based blackboard + supervision-tree store. * * This module is the IPC substrate that lets isolated subagent processes share * context with each other (a horizontal blackboard) and report results upward * so a parent can react (a vertical supervision tree). It deliberately reuses * the SAME atomic-write + file-lock primitives as the run store (`store.ts`), * so it inherits the project's "all file ops are atomic" invariant for free. * * On-disk layout, rooted at PI_TASKFLOW_CTX_DIR (one directory per run): * * / * ├── tree.json the node tree (who spawned whom + status) * ├── tree.json.lock lock guarding tree.json RMW cycles * ├── findings/ * │ ├── .json findings written by one node (last-write-wins per key) * │ └── .json.lock * ├── reports/ * │ └── .json a node's upward report ({summary, structured?}) * └── pending/ * └── -.json a ctx_spawn intent the runtime will pick up * * Why per-node findings files (not one shared findings.json): sibling subagents * run concurrently. Giving each node its OWN file means concurrent writers never * contend on the same lock — a node only locks its own file. A reader unions the * relevant nodes' files (its ancestors + completed siblings). This is the same * "shard by writer" trick the run index uses to avoid a global write bottleneck. */ /** Max bytes for a single findings value (after JSON.stringify). */ export declare const MAX_VALUE_BYTES: number; /** Max bytes for a single report summary string. */ export declare const MAX_REPORT_BYTES: number; /** Max bytes for a report's structured payload (after JSON.stringify). */ export declare const MAX_STRUCTURED_BYTES: number; /** Max bytes for a single ctx_spawn task prompt. */ export declare const MAX_TASK_BYTES: number; /** Max number of keys one node may write. */ export declare const MAX_KEYS_PER_NODE = 256; /** Max assignments a single ctx_spawn call may queue. */ export declare const MAX_SPAWN_ASSIGNMENTS = 16; /** Max bytes for a single ctx_spawn `subflow` payload (after JSON.stringify). */ export declare const MAX_SUBFLOW_BYTES: number; export declare function isValidKey(key: string): boolean; export type NodeStatus = "running" | "done" | "failed"; /** One node in the supervision tree (one subagent task). */ export interface TreeNode { nodeId: string; phaseId: string; parentNodeId?: string; status: NodeStatus; createdAt: number; updatedAt: number; } export interface ContextTree { nodes: TreeNode[]; } /** A node's findings — a flat string→JSON map. Last-write-wins per key. */ export type FindingsMap = Record; export interface NodeReport { nodeId: string; summary: string; structured?: unknown; at: number; } /** * A queued ctx_spawn intent, picked up by the runtime after the node finishes. * Each assignment is EITHER a flat task OR an inline sub-flow (DAG) — never both. * * - `task` : a single prompt string (the agent named by `agent` runs it). * - `subflow` : an inline Taskflow ({phases:[...]} or a bare phases array) * the runtime validates + runs as a nested sub-flow. Inner * phases without their own `agent` fall back to `defaultAgent`. * * `agent` (flat) means "who executes this task"; `defaultAgent` (subflow) means * "fallback agent for inner phases" — different semantics, hence different fields. */ export interface SpawnAssignment { task?: string; agent?: string; subflow?: unknown; defaultAgent?: string; } export interface PendingSpawn { parentNodeId: string; assignments: SpawnAssignment[]; at: number; } /** Build the per-run ctx directory path under a runs root. */ export declare function ctxDirFor(runsRoot: string, runId: string): string; /** * Ensure the ctx directory tree exists. Idempotent; safe to call repeatedly. * Returns the same ctxDir for chaining. */ export declare function initCtxDir(ctxDir: string): string; export declare function readTree(ctxDir: string): ContextTree; /** * Register (or update) a node in the tree. IDEMPOTENT — upserts by nodeId so a * resume that re-runs a phase does not duplicate tree entries (which would * double-count ancestor findings). This is the C3 resume-safety fix. */ export declare function registerNode(ctxDir: string, nodeId: string, phaseId: string, parentNodeId: string | undefined, status?: NodeStatus): void; export declare function setNodeStatus(ctxDir: string, nodeId: string, status: NodeStatus): void; /** Compute a node's depth (root = 0) by walking the parent chain. */ export declare function nodeDepth(tree: ContextTree, nodeId: string): number; /** Return the ancestor chain nodeIds for a node (excluding itself), in nearest-first order (parent, grandparent, …). */ export declare function ancestorIds(tree: ContextTree, nodeId: string): string[]; export declare function readNodeFindings(ctxDir: string, nodeId: string): FindingsMap; /** * Write one finding (last-write-wins per key) into THIS node's findings file. * Only locks the node's own file → concurrent siblings never contend. * Throws on bad key / oversized value / too many keys (caller surfaces as tool error). */ export declare function writeFinding(ctxDir: string, nodeId: string, key: string, value: unknown): void; /** * Read the findings visible to a node: its OWN findings unioned with its * ancestors' and all sibling/other nodes' findings that are already `done`. * "done" visibility prevents reading a half-written blackboard from a sibling * that is still running (eventual consistency: you see a sibling's findings * once it has reported completion). The node's own findings are always visible. * * On key conflicts: nearer scope wins (own > ancestors > completed others), * matching intuition that a node trusts its own/closer notes most. * * @param key optional — return only that key's value (or undefined). */ export declare function readVisibleFindings(ctxDir: string, nodeId: string, key?: string): FindingsMap | unknown; export declare function writeReport(ctxDir: string, nodeId: string, summary: string, structured?: unknown): void; export declare function readReport(ctxDir: string, nodeId: string): NodeReport | undefined; /** Queue a ctx_spawn intent. Each call writes a unique file the runtime picks up. */ export declare function queueSpawn(ctxDir: string, parentNodeId: string, assignments: SpawnAssignment[]): number; /** * Drain (read + delete) all pending spawn intents queued by a parent node. * Returns the flattened assignment list. Used by the runtime supervision loop * after a node's subagent finishes. * * INVARIANT: only call this AFTER the parent subagent process has exited. The * read-then-unlink is not directory-locked, so a concurrent queueSpawn from a * still-running parent could be missed. The runtime drains post-exit, so no * concurrent writer exists. */ export declare function drainPendingSpawns(ctxDir: string, parentNodeId: string): SpawnAssignment[]; //# sourceMappingURL=context-store.d.ts.map