coderClaw

Agent Loop (CoderClaw)

An agentic loop is the full “real” run of an agent: intake → context assembly → model inference → tool execution → streaming replies → persistence. It’s the authoritative path that turns a message into actions and a final reply, while keeping session state consistent.

In CoderClaw, a loop is a single, serialized run per session that emits lifecycle and stream events as the model thinks, calls tools, and streams output. This doc explains how that authentic loop is wired end-to-end.

Entry points

How it works (high-level)

  1. agent RPC validates params, resolves session (sessionKey/sessionId), persists session metadata, returns { runId, acceptedAt } immediately.
  2. agentCommand runs the agent:
    • resolves model + thinking/verbose defaults
    • loads skills snapshot
    • calls runEmbeddedPiAgent (pi-agent-core runtime)
    • emits lifecycle end/error if the embedded loop does not emit one
  3. runEmbeddedPiAgent:
    • serializes runs via per-session + global queues
    • resolves model + auth profile and builds the pi session
    • subscribes to pi events and streams assistant/tool deltas
    • enforces timeout -> aborts run if exceeded
    • returns payloads + usage metadata
  4. subscribeEmbeddedPiSession bridges pi-agent-core events to CoderClaw agent stream:
    • tool events => stream: "tool"
    • assistant deltas => stream: "assistant"
    • lifecycle events => stream: "lifecycle" (phase: "start" | "end" | "error")
  5. agent.wait uses waitForAgentJob:
    • waits for lifecycle end/error for runId
    • returns { status: ok|error|timeout, startedAt, endedAt, error? }

Queueing + concurrency

Session + workspace preparation

Prompt assembly + system prompt

Hook points (where you can intercept)

CoderClaw has two hook systems:

Internal hooks (Gateway hooks)

See Hooks for setup and examples.

Plugin hooks (agent + gateway lifecycle)

These run inside the agent loop or gateway pipeline:

See Plugins for the hook API and registration details.

Streaming + partial replies

Tool execution + messaging tools

Reply shaping + suppression

Compaction + retries

Event streams (today)

Chat channel handling

Timeouts

Where things can end early