Skip to content

ExecutionTree

Defined in: core/execution-tree.ts:225

The Execution Tree - single source of truth for all execution state.

Features:

  • Stores all nodes (LLM calls, gadgets) in a hierarchical structure
  • Emits events on mutations
  • Provides query methods for aggregation (costs, media, descendants)
  • Supports single shared tree model for nested subagents
const tree = new ExecutionTree();
// Add root LLM call
const llmNode = tree.addLLMCall({ iteration: 1, model: "sonnet" });
// Add gadget under the LLM call
const gadgetNode = tree.addGadget({
invocationId: "gc_1",
name: "ReadFile",
parameters: { path: "/foo.txt" },
parentId: llmNode.id,
});
// Complete the gadget
tree.completeGadget(gadgetNode.id, { result: "file contents", executionTimeMs: 50 });
// Query total cost
console.log(tree.getTotalCost());

new ExecutionTree(options?): ExecutionTree

Defined in: core/execution-tree.ts:249

number

string | null

ExecutionTree

readonly baseDepth: number

Defined in: core/execution-tree.ts:241

Base depth for all nodes in this tree. Used when this tree is a subagent’s view into a parent tree.


readonly parentNodeId: string | null

Defined in: core/execution-tree.ts:247

Parent node ID for subagent trees. All root nodes in this tree will have this as their parentId.

addGadget(params): GadgetNode

Defined in: core/execution-tree.ts:445

Add a new gadget node to the tree.

AddGadgetParams

GadgetNode


addLLMCall(params): LLMCallNode

Defined in: core/execution-tree.ts:334

Add a new LLM call node to the tree.

AddLLMCallParams

LLMCallNode


appendLLMResponse(nodeId, chunk): void

Defined in: core/execution-tree.ts:381

Add text to an LLM call’s response (for streaming).

string

string

void


complete(): void

Defined in: core/execution-tree.ts:940

Mark the tree as complete (no more events will be emitted).

void


completeGadget(nodeId, params): void

Defined in: core/execution-tree.ts:514

Complete a gadget node successfully.

string

CompleteGadgetParams

void


completeLLMCall(nodeId, params): void

Defined in: core/execution-tree.ts:399

Complete an LLM call node.

string

CompleteLLMCallParams

void


emitText(content, llmCallNodeId): void

Defined in: core/execution-tree.ts:603

Emit a text event (notification only, not stored in tree).

string

string

void


events(): AsyncGenerator<ExecutionEvent>

Defined in: core/execution-tree.ts:909

Get async iterable of all events. Events are yielded as they occur.

AsyncGenerator<ExecutionEvent>


failLLMCall(nodeId, error, recovered): void

Defined in: core/execution-tree.ts:425

Mark an LLM call as failed.

string

Error

boolean

void


getAncestors(id): ExecutionNode[]

Defined in: core/execution-tree.ts:660

Get ancestors of a node (from root to parent).

string

ExecutionNode[]


getChildren(id): ExecutionNode[]

Defined in: core/execution-tree.ts:649

Get children of a node.

string

ExecutionNode[]


getCurrentLLMCallId(): string | undefined

Defined in: core/execution-tree.ts:705

Get the current (most recent incomplete) LLM call node.

string | undefined


getDescendants(id, type?): ExecutionNode[]

Defined in: core/execution-tree.ts:681

Get all descendants of a node.

string

ExecutionNodeType

ExecutionNode[]


getNode(id): ExecutionNode | undefined

Defined in: core/execution-tree.ts:623

Get a node by ID.

string

ExecutionNode | undefined


getNodeByInvocationId(invocationId): GadgetNode | undefined

Defined in: core/execution-tree.ts:630

Get a gadget node by invocation ID.

string

GadgetNode | undefined


getNodeCount(): object

Defined in: core/execution-tree.ts:853

Get node counts.

object

gadgets: number

llmCalls: number


getRoots(): ExecutionNode[]

Defined in: core/execution-tree.ts:640

Get all root nodes (depth 0 for this tree).

ExecutionNode[]


getSubtreeCost(nodeId): number

Defined in: core/execution-tree.ts:738

Get total cost for a subtree (node and all descendants).

string

number


getSubtreeMedia(nodeId): GadgetMediaOutput[]

Defined in: core/execution-tree.ts:815

Collect all media from a subtree.

string

GadgetMediaOutput[]


getSubtreeTokens(nodeId): object

Defined in: core/execution-tree.ts:788

Get token usage for a subtree.

string

object

cached: number

input: number

output: number


getTotalCost(): number

Defined in: core/execution-tree.ts:723

Get total cost for entire tree.

number


getTotalTokens(): object

Defined in: core/execution-tree.ts:766

Get token usage for entire tree.

object

cached: number

input: number

output: number


isComplete(): boolean

Defined in: core/execution-tree.ts:952

Check if the tree is complete.

boolean


isSubtreeComplete(nodeId): boolean

Defined in: core/execution-tree.ts:838

Check if a subtree is complete (all nodes finished).

string

boolean


on(type, listener): () => void

Defined in: core/execution-tree.ts:886

Subscribe to events of a specific type. Returns unsubscribe function.

ExecutionEventType

Event type to subscribe to (use ”*” for all events)

EventListener

Callback function that receives matching events

Unsubscribe function

(): void

void

const unsubscribe = tree.on("gadget_complete", (event) => {
if (event.type === "gadget_complete") {
console.log(`Gadget ${event.name} completed`);
}
});

onAll(listener): () => void

Defined in: core/execution-tree.ts:901

Subscribe to all events.

EventListener

(): void

void


skipGadget(nodeId, failedDependency, failedDependencyError, reason): void

Defined in: core/execution-tree.ts:561

Mark a gadget as skipped due to dependency failure.

string

string

string

"dependency_failed" | "controller_skip"

void


startGadget(nodeId): void

Defined in: core/execution-tree.ts:494

Mark a gadget as started (running).

string

void