Skip to content

PI Agent ​

Build a PI-Style Coding Agent from Scratch in 100 Days ​

Project Status: D100 / 100 · v1.0.0 · MIT
Technology Stack: TypeScript Strict · Node.js 24+ · JSONL / SQLite · TUI · CBOR RPC
Engineering Highlights: An independently implemented educational edition with zero runtime dependencies and a cross-reference map to the real PI source code.

PI Agent uses the real PI architecture as a learning reference without simply copying its interfaces. Starting with messages and the model layer, the tutorial incrementally implements the Agent Loop, Harness lifecycle, tools, session trees, context compaction, terminal UI, RPC, and extension system.

← Back to Series Overview · View Source and Curriculum

Core Modules ​

Multi-Provider Model Layer ​

  • Types for messages, content blocks, and StopReason.
  • Offline Faux Provider, scripted responses, and invocation snapshots.
  • ModelRegistry, model metadata, and provider/id references.
  • OpenAI-compatible APIs, error normalization, AbortSignal, and timeouts.
  • SSE parsing across arbitrary chunk boundaries, pull-based EventStream, and streaming text.
  • Incremental parsing of Partial JSON tool arguments, plus usage and cost tracking.

Agent Loop and Tools ​

  • Separation of AgentMessage and LLM Message, with unified lifecycle events.
  • Single-turn and multi-turn tool loops, continuation turns, and turn guardrails.
  • Tool Schema validation, structured error feedback, and parallel/sequential execution.
  • Result<T, E> error model and ExecutionEnv injection.
  • Read, Write, Edit, and Bash tools.
  • Path protection, change queues, output Spill, and process-group cleanup.

Agent and Harness ​

  • Stateful Agent facade, Barrier subscriptions, Abort, and WaitForIdle.
  • Four Harness state categories: Config, Snapshot, Session, and Pending.
  • Phase state machine, Save Points, and writes pending persistence.
  • Before/After Tool Call hooks.
  • Steering/Follow-up queues and Active Tools allowlists.

Sessions and Context ​

  • Append-only JSONL session logs and session trees with ID/ParentID/Leaf.
  • Branch navigation, search, crash recovery, and a SQLite backend.
  • Data version migrations, UTF-8-safe truncation, and Spill.
  • Auto-compaction, Branch Summarization, Skills, and Prompt Templates.

Terminal, Protocols, and Extensions ​

  • Raw Mode input, Bracketed Paste, and dual renderers.
  • Line-level diff rendering, CSI 2026, flicker prevention, components, and Editor.
  • Strict CBOR subset, incremental length framing, handwritten Schemas, and RPC Server/Client.
  • Three CLI modes: Interactive, Print, and JSON-event.
  • Project Trust, extension Loader, extension API, Evals, and release audits.

100-Day Roadmap ​

StageDaysCore TopicsRelease
G1D1–D25Model layer, event-driven Agent Loop, tool contracts, and the first acceptance milestonev0.1.0
G2D26–D53Error model, file and Bash tools, and Agent/Harness lifecyclev0.2.0
G3D54–D74JSONL/SQLite sessions, costs, compaction, Skills, and templatesv0.3.0
G4D75–D88TUI, diff rendering, CBOR, and RPC Server/Clientv0.4.0
G5D89–D100Three CLI modes, Project Trust, extensions, Evals, and releasev1.0.0

Learning Against the Real Source Code ​

The project maintains docs/pi-source-map.md, mapping tutorial concepts to locations in the real PI source code. Readers can first complete the minimal implementation of the educational edition, then return to the production project to understand more complex Provider, sandbox, supply-chain, and reliability concerns.

Evidence Boundaries ​

The project README documents the complete roadmap, offline milestone acceptance, and test baselines. Faux Provider and deterministic Demos demonstrate component contracts and failure paths; they do not represent real model performance or production SLOs. A clear capability gap remains between the educational edition and production PI.

Keep building. Stay curious.