Inside Pi

Appendix B

Reading path, projects and further reading

6 min read · checked against pi@4954d76 on 2026-10-16

The book explains the code; this appendix gets you reading the code yourself and then building your own. Work through B.1 alongside the chapters, attempt one project from B.2 per part, and use B.3 when a topic deserves a full course.

B.1 A reading path through the source

Read in this order. Each step is small enough for one sitting and builds on the one before. Paths are relative to packages/.

StepFilesWhat to look forChapter
1ai/src/types.tsThe message, content and event unions every other package speaks.1, 3
2ai/src/utils/event-stream.ts, ai/src/utils/json-parse.tsThe async event queue; parsing tool arguments while they stream.3
3ai/src/api/anthropic-messages.ts, ai/src/api/openai-completions.tsTwo adapters side by side: same output events, very different wire formats.4
4ai/src/utils/retry.ts, ai/src/utils/overflow.tsWhich errors are retryable, and how a full context window is recognised.4
5ai/src/providers/faux.ts, ai/src/api/constrained-sampling.tsA scripted provider for tests; schema and grammar constraints.5
6agent/src/agent-loop.ts, then agent/src/agent.tsThe pure loop, then the stateful wrapper around it.6
7coding-agent/src/core/agent-session.tsPrompt pipeline, retries, compaction triggers. Read it with Chapter 7 open.7
8coding-agent/src/modes/json-event.ts, coding-agent/src/modes/rpc/The machine-facing modes and their framing.7
9coding-agent/src/core/tools/edit.ts, edit-diff.ts, file-mutation-queue.ts, bash.tsExact and fuzzy edits, per-file locking, process trees.8
10coding-agent/src/core/session-manager.ts, core/compaction/The session tree, context reconstruction, compaction and branch summaries.9
11coding-agent/src/core/skills.ts, prompt-templates.ts, core/extensions/Resources and the extension runtime.10
12coding-agent/src/modes/interactive/interactive-mode.tsThe event switch that drives the app.11
13tui/src/tui.ts, stdin-buffer.ts, utils.ts, undo-stack.ts, kill-ring.tsDifferential rendering, input parsing, width measurement, editing.12
14durable/docs/spec.md, then durable/src/session/session.ts and transaction.tsThe rules first, then the commit path that enforces them.13, 14
15durable/src/storage/memory.ts, then storage/sqlite/ and storage/jsonl/The reference semantics, then two real backends.14
16durable/src/harness/scheduler.ts, tool.ts, generation.tsTasks, replay policies, model turns.15
17durable/src/env/index.ts, env/node-watch.ts, env/docs/protocol.mdThe environment contract, the watcher, the remote wire protocol.16
18chord/src/delta/README.md, chord/docs/spec.md, chord/src/context/index.tsOwnership rules, the object runtime, explicit context.17
19codemode/src/runtime/vm.ts, mcp/src/client.ts, telemetry/src/noop.ts, evals/src/plan.tsDeterminism, JSON-RPC, null objects, experimental design.18

A tip that applies everywhere in this repository: read the doc comment at the top of a file before its code. The authors put the reasoning there ("Problem: … Solution: …"), and the code then reads as an implementation of a stated design. Read the tests next: test/ folders show how each module is meant to be used, and the conformance suites state the contracts in executable form.

B.2 Projects

Each project rebuilds a piece of Pi from scratch, small enough to finish and large enough to teach. Compare your design with Pi's only after yours works.

#ProjectYou will practiseAfter
1A 200-line agent. One provider over raw fetch and SSE, a read and a bash tool, the ReAct loop, abort with Ctrl-C.Streaming parsing, tool calling, cooperative cancellation.Part III
2A provider-neutral event stream. Add a second provider behind the same event union, plus a faux provider for tests.Adapter pattern, discriminated unions, deterministic tests.Part II
3Branching sessions. Store your agent's history as JSONL with parent ids; add /fork, /tree and a compaction that summarises the oldest turns.Event sourcing, trees, LCA, crash-safe appends.Part IV
4A safe edit tool. Exact match first, then a normalised fuzzy match; refuse ambiguous matches; serialise edits per file.String algorithms, lost updates, locking.Ch. 8
5A differential terminal renderer. Components that render lines for a width; redraw only changed lines inside synchronised output.Terminal protocols, diffing, grapheme width.Part V
6A durable to-do agent. Every model turn and tool call is committed to SQLite before it is shown; kill the process at random and prove it resumes.WAL, idempotency, the effect sandwich, replay.Part VI
7A JSON delta tracker. Proxy drafts that emit set, delete, append and splice ops; a replica that replays them; property tests that both always agree.Persistent data, proxies, property-based testing.Ch. 17
8A tiny remote environment. A daemon that answers length-prefixed frames on stdin/stdout with read, write and exec, plus heartbeats and cancellation.Framing, multiplexing, liveness, process groups.Ch. 16
9An MCP client. JSON-RPC over stdio against a real MCP server: initialise, list tools, call one, cancel one, renew a timeout with progress.Protocol design, correlation, timeouts.Ch. 18
10An eval harness. Two prompt variants, paired cases, counterbalanced order, three repetitions; report lift with blocked pairs withheld.Experimental design, statistics, isolation.Ch. 18

For each project, write a one-page design note before coding: the invariants you will keep, the failure you fear most, and how a test will catch it. That habit is the most transferable thing this codebase teaches.

B.3 Further reading

Grouped by the parts of the book they deepen. Classic texts are named without links; standards and stable pages are linked.

Agents and LLM systems (Parts I–IV)

  • Yao et al., ReAct: Synergizing Reasoning and Acting in Language Models (2022) — the loop of Chapter 1. arXiv
  • The Model Context Protocol specification — Chapters 10 and 18. modelcontextprotocol.io
  • Robertson and Zaragoza, The Probabilistic Relevance Framework: BM25 and Beyond (2009) — the ranking of Chapter 16.

Protocols and security (Parts II, VI, VII)

Terminals (Part V)

Durability and distributed systems (Parts VI–VII)

Algorithms (Chapter 17)

  • Myers, An O(ND) Difference Algorithm and Its Variations (1986) and Hunt and Szymanski (1977) — diff.
  • Seidel and Aragon, Randomized Search Trees (1996) — treaps.
  • Marsaglia, Xorshift RNGs (2003).
  • Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms — dynamic programming, LIS, search trees.