namzu.aidocs

Runtime Pipeline

Detailed walkthrough of how @namzu/sdk turns agent input into provider calls, tool execution, checkpoints, and final results.

The core runtime path of the SDK lives in runtime/, but it relies on several adjacent modules. The easiest way to understand it is to follow a run from ReactiveAgent.run() into drainQuery() and then through the iteration phases.

1. Entry Point

For the common path, execution starts in agents/ReactiveAgent.ts:

  1. ReactiveAgent.run() validates that sessionId, projectId, and tenantId are present.
  2. It forwards the request into drainQuery() from runtime/query/index.ts.
  3. drainQuery() consumes the async query() generator and assembles a final AgentRun.

This is why most public runtime behavior eventually converges on the same query pipeline even when the surface API starts at an agent class.

2. Query Bootstrap

runtime/query/index.ts owns the high-level bootstrap sequence:

query(params)
  -> ensureMigrated(.namzu root)
  -> RunContextFactory.build(...)
  -> wire event translators and optional stores
  -> register dynamic tool surfaces
  -> create prompt, tooling, executor, guards, checkpoints
  -> hand control to IterationOrchestrator.runLoop()

Key responsibilities in this stage:

ModuleResponsibility
context.tsBuild run context and initialize the run manager
prompt.tsAssemble prompt segments
tooling.tsPrepare tool availability and model-facing tool schemas
executor.tsExecute tools during a run
guard.tsDecide whether iteration should continue or stop
checkpoint.tsCreate and summarize checkpoints
events.tsTranslate runtime activity into RunEvents

3. Iteration Orchestrator

runtime/query/iteration/index.ts is the center of the loop. It coordinates phases rather than implementing every concern inline.

The practical iteration sequence is:

plan gate
  -> limit and cancellation guard
  -> pending task notifications
  -> compaction check
  -> provider.chat(...)
  -> advisory phase
  -> tool review or direct tool execution
  -> checkpoint
  -> loop or stop

4. Iteration Phases

The phase modules live in runtime/query/iteration/phases/:

Phase fileResponsibility
plan.tsStops at a plan gate if a plan is awaiting approval
compaction.tsReplaces old context with structured compacted state when token pressure rises
advisory.tsTriggers advisor consultation based on runtime state
tool-review.tsRuns verification and HITL review before tool execution
checkpoint.tsWrites an iteration checkpoint and asks the resume handler what to do next
context.tsShared iteration context plus HITL decision handling

5. Provider Call Boundary

The LLM boundary is intentionally narrow:

  • The runtime prepares normalized messages and tool schemas.
  • The provider receives chat({ model, messages, tools, ... }).
  • The provider returns a normalized ChatCompletionResponse.

Because providers implement the shared LLMProvider contract, the runtime does not need vendor-specific branching at this point.

6. Tool Review and Execution

Tool execution is a two-stage boundary:

  1. tool-review.ts inspects requested tool calls.
  2. If a VerificationGate exists, it evaluates allow, deny, or review decisions first.
  3. If human review is required, the runtime asks the resume handler for a decision.
  4. Approved tool calls execute through the tool executor and append tool messages back into the run.

This split is important because the runtime treats tool approval as a first-class phase, not as an incidental check buried inside tool execution.

7. Compaction and Advisory Are Side Paths, Not Separate Loops

Two subsystems often look separate from the main runtime, but they are actually embedded into the same iteration flow:

  • compaction/ reduces context pressure before the next model call.
  • advisory/ injects structured guidance after evaluating trigger conditions.

Neither subsystem creates an alternative run architecture. Both are additions around the same iteration loop.

8. Stop Conditions

The loop exits through explicit stop paths:

Stop pathTrigger
Guard stopToken, cost, timeout, or iteration limit
CancellationAbort signal or explicit cancellation
PauseHITL decision at plan gate, tool review, or checkpoint
Final responseRuntime forces a final answer because limits are near

When the runtime stops, RunPersistence and the surrounding query pipeline finalize the AgentRun result that the agent surface returns.

On this page