Part 10: Agent Loop Engineering
Part 9 established why an Agent needs a harness. Part 10 examines the runtime mechanisms that keep that harness useful under long contexts, large tool surfaces, restarts, mid-run user input, hung providers, repeated actions, and parallel calls.
Public-source snapshot: implementation details are examples from public Kocoro
origin/mainat commit4ec6772, reviewed 2026-07-27. Treat the invariant as the lesson and re-check constants against current source.
Chapter map
| Chapter | Topic | Question answered |
|---|---|---|
| 34 | From DAG to Agent Loop | Explains when a deterministic DAG should give way to an adaptive Agent loop, and what state a long-lived daemon must rebuild for every turn. |
| 35 | Context Compaction | Designs a staged context-compaction ladder that acts before overflow, verifies summaries, and retains an emergency recovery path. |
| 36 | Tool Result Budget and Spill | Shows how context offloading stores large tool results outside the prompt while preserving a useful preview and durable pointer. |
| 37 | Tiered Compression | Explains first-touch tiering, content-bearing floors, and byte-stable rewrites for old tool evidence. |
| 38 | Deferred Tool Loading and Tool Search | Reduces MCP tool-schema bloat with token-budget and category-based deferral, a warm set, and model-visible tool search. |
| 39 | Prompt Cache Stability | Shows how stable prompt prefixes, byte-equal forked requests, and execution-time permission checks improve Agent prompt-cache reuse. |
| 40 | Durable Agent Loops | Designs resumable Agent turns with phase-boundary checkpoints, interrupted markers, bounded unattended recovery, and idempotent persistence. |
| 41 | Steering a Running Agent | Explains how running Agents absorb follow-ups, preserve message addressing, distinguish delivery acknowledgement from replay, and interrupt safely. |
| 42 | Agent Timeouts and Watchdogs | Separates soft-idle warnings, hard cancellation, stream-gap detection, and phase-aware watchdog policy for hung Agents. |
| 43 | Stuck Loop Detection | Builds stuck-loop detection from complementary progress signals, bounded nudges, force-stop behavior, and a public zero-byte write incident. |
| 44 | Parallel Tool Execution | Explains safe parallel tool calls using per-invocation concurrency traits, ordered result pairing, and conservative Bash classification. |
| 45 | Computer-Use Context Management | Separates text observations, browser screenshots, and global image budgets so GUI loops retain actionable state without unbounded prompt growth. |
Reading paths
- Context and cost: 35 → 36 → 37 → 38 → 39
- Durability and control: 40 → 41 → 42
- Reliability, speed, and GUI context: 43 → 44 → 45
- Coming from DAG workflows: 34 → 40 → 43 → 44
Source boundary
Only tracked files on Kocoro public origin/main are used. Local-only architecture notes, unpushed branches, private postmortems, hosted-service topology, internal tokens, and private repository paths are excluded.
Chapter 45 is included because its observation-window and image-budget implementation is now tracked on public origin/main.