DocsThe 6-Phase Agent Loop
Runtime Architecture & 3 Pillars

Phase Loop Machine & 3-Pillar Engine

How Smoke Monkey decouples autonomous agent execution across three modular libraries: the React Chat UI, the Harness Core Engine, and the Model Context Protocol (MCP) Hub.

Smoke Monkey 3-Pillar Architecture Diagram: React Chat UI, Backend Harness Core Engine, and Model Context Protocol Server Hub
Figure 1: Architectural data plane showing real-time streaming between @smoke-monkey/ui, the 6-phase state machine in @smoke-monkey/harness, and external tool connectors in @smoke-monkey/mcp.

The 3 Pillars of Smoke Monkey

1. Frontend UI (@smoke-monkey/ui)

Drop-in React chat interface featuring streaming markdown, collapsible tool-call execution cards, charts, Mermaid diagrams, and 14 built-in theme presets.

2. Backend Harness (@smoke-monkey/harness)

Embeddable, zero-dependency TypeScript agent loop with a 6-phase state machine, automated loop guards, just-in-time skills, and human-in-the-loop pauses.

3. MCP Protocol Hub (@smoke-monkey/mcp)

Dual-mode Model Context Protocol integration: connects external tools via stdio/HTTP, while exposing 22 tools to external agents (Claude Code, Cursor, Codex).

The 6-Phase State Machine

Unconstrained agents easily fall into infinite loops or goal drift. Smoke Monkey binds execution to a deterministic state machine with self-healing demotion loops.

Interactive Engine Flow
Drag nodes · Scroll to pan & zoom
Deterministic loop: verification failure triggers an automated recovery demotion instead of halting.
Live Diagram
Smoke Monkey 6-Phase State Machine: Explore, Plan, Edit, Verify, Recover, and Complete with loop protection shields
Figure 2: Deterministic 6-phase cycle with automated failure demotion (verify ➔ recover) and real-time loop guardrails.
01

EXPLORE

Read-Only

Inspect directory structures, glob matching, git status, and grep searches to map the codebase without hallucinations or unintended mutations.

02

PLAN

Task Planning

Decompose user requirements into discrete step-by-step milestones. Loads specialized SKILL.md playbooks just-in-time (JIT) to preserve token budget.

03

EDIT

Code Modification

Surgical line edits and atomic file writes. Enforces TypeScript linting, exact match boundary verification, and unified diff formatting.

04

VERIFY

Validation

Runs test suites, compilers, typecheckers, and inspects git diffs. Catches regressions before completing the task.

05

RECOVER

Self-Healing

Automatic demotion loop: on compiler or test failure, reverts bad diffs, diagnoses stderr, reflects on errors, and pivots to alternate solutions.

06

COMPLETE

Synthesis

Final validation, clean git status verification, session memory persistence, and streaming conversational summary to the client UI.

Built-in Safety Guardrails & Compaction

Runaway Loop Protection

Halts execution automatically if total steps exceed MAX_STEPS (default: 1000) or identical error occurs 3 times.

Context Compaction (90%)

Past 90% token window capacity, summarizes older turns while preserving recent history and active skills verbatim.

Empty Response Exponential Backoff

Gracefully handles provider rate-limits (HTTP 429) without crashing the agent loop.

The Three Interactive Pauses

Safely suspends for tool permissions, user clarifications, and external MCP approvals before resuming.

Ready to build your first agent?

Follow our 60-second quickstart guide with production code examples.