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.

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.

EXPLORE
Inspect directory structures, glob matching, git status, and grep searches to map the codebase without hallucinations or unintended mutations.
PLAN
Decompose user requirements into discrete step-by-step milestones. Loads specialized SKILL.md playbooks just-in-time (JIT) to preserve token budget.
EDIT
Surgical line edits and atomic file writes. Enforces TypeScript linting, exact match boundary verification, and unified diff formatting.
VERIFY
Runs test suites, compilers, typecheckers, and inspects git diffs. Catches regressions before completing the task.
RECOVER
Automatic demotion loop: on compiler or test failure, reverts bad diffs, diagnoses stderr, reflects on errors, and pivots to alternate solutions.
COMPLETE
Final validation, clean git status verification, session memory persistence, and streaming conversational summary to the client UI.
Built-in Safety Guardrails & Compaction
Halts execution automatically if total steps exceed MAX_STEPS (default: 1000) or identical error occurs 3 times.
Past 90% token window capacity, summarizes older turns while preserving recent history and active skills verbatim.
Gracefully handles provider rate-limits (HTTP 429) without crashing the agent loop.
Safely suspends for tool permissions, user clarifications, and external MCP approvals before resuming.