# The Coax Programming Language

*A draft book, begun October 2026.*

This is a book about Coax, the language OpenAgents uses for code that has
to be fast and has to be right: the Metaverse Engine and the world it runs, the
iPhone app's home screen and Grid, and the money rules in its bitcoin
wallet. Coax descends from Jonathan Blow's Jai. It compiles to plain C you
can read, it checks arithmetic and array bounds while you develop, and it
lets you write checks that the compiler runs for you.

The [Introduction](introduction.md) explains why Coax exists. If you'd
rather start typing, go straight to [Chapter 1](ch01-getting-started.md).

Every other Coax-implementation OpenAgents book (eight of the nine books; the
ninth, [Reverse Engineering Claude Code](https://github.com/OpenAgentsInc/openagents/blob/main/claude-code/book/README.md),
is a companion TypeScript codebase study) is written in Coax and assumes Parts I–III of
this one. See [the OpenAgents books](https://github.com/OpenAgentsInc/openagents/blob/main/docs/books/README.md) for the whole library and the order to read it in.

## Who this book is for

We assume you've programmed before, in any language. You don't need to
know C, though it helps in a few places, and when it does we'll say so.
If you know C, C++, Rust or Jai, you'll find notes along the way that
compare Coax with what you already know, and two appendices written just
for you ([Jai](appendix-c-jai.md), [C++](appendix-d-cpp.md)).

The book is also written to be read by agents. Most Coax in OpenAgents
is written by models, and they read this book the way a new teammate
would. That's one reason every error code has an entry in
[Appendix B](appendix-b-error-codes.md), and every rule comes with the
reason for it.

## How to use this book

Read it in order the first time. Each chapter builds on the ones before,
and the early chapters introduce ideas lightly that later chapters cover
in depth.

Part I gets you running. Chapter 1 builds the compiler and a first
program. Chapter 2 is a small project, an ASCII Mandelbrot renderer, that
touches most of the everyday language at once. Chapters 3 and 4 then go
back over the basics properly and show how to read the compiler's errors.

Part II covers the language itself: structs and enums, memory, errors and
cleanup, generics, compile-time execution, organizing a program into source
units and modules, writing checks, and debugging and measuring cost. Part III
is about data layout, jobs, asynchronous lifetimes, determinism and the GPU.
Part IV is about how Coax fits into OpenAgents: the protocol boundary, builds
and hot reload, interop with C, C++ and Swift, targets, tooling for agents,
and what verification does and doesn't establish. Part V turns those pieces
into larger programs: a runnable protocol-to-native capstone, a voice system
built from small checked policies and native boundaries, and small consumers
of shared libraries (caller-owned buffers, protocol fixtures and a tiny
Psionic CPU operation). Part VI opens up the compiler and ends with the road
ahead.

Parts VII–IX describe roadmap work, one chapter per roadmap phase. Most of
it doesn't run in `coax0` yet: each planned chapter is an outline that says
what the phase adds, which program uses it first and what "done" means.
Chapter 29 is partly implemented: its affine points and vectors in declared
frames run today, in a checked tutorial section. Part VII covers the
physical world: units and frames, clocks, bounded control, strict math and
integrators, replay, robot models, sensors and actuators, fault scenarios and
parameter provenance, then a simulated robot and a freighter scenario built
from them. Part VIII covers simulation compute: logical tensors, compile and
startup budgets, device graphs and kernel derivatives. Part IX covers the
rest of the toolchain: self-hosting, procedure-body replacement, resident
compilation and new backends.

The [selected library index](https://github.com/OpenAgentsInc/openagents/blob/main/coax/LIBRARIES.md) names reusable entry points, native link inputs and their lifetime/error limits. Chapter 26 supplies small offline consumers.

The best way to learn is to type the examples in and run them. Change
them, break them on purpose, and read what the compiler says.

## What runs today and what doesn't

Coax is young. Its first compiler, `coax0`, compiles a real but limited
subset of the language: procedures, integers and floats, strings,
structs, enums, the initial native LP64 profile of tagged unions, optionals and
handled direct Results, exhaustive selection, fixed arrays, writable and read-only views, `defer`,
compile-time `#run` and
`#assert`, checks with `#test` and `#forall`, logical modules and explicit
exports, generic procedures and fixed-storage struct templates over type
and length parameters with built-in constraints, nominal scalar quantities,
affine points and vectors in declared frames, and a C boundary.
User-defined constraints, owning containers and other later features remain
planned. The generated
[current status](appendix-a-keywords.md) records the bounded implemented
subset separately from the full design.

Runnable sections show current source and output. Planned sections are
marked separately; when a chapter describes something `coax0` cannot
compile yet, it uses a note like this:

> Note: This feature is planned and not available in `coax0`. The syntax
> shown follows [the Coax specification](https://github.com/OpenAgentsInc/openagents/tree/main/coax/spec.md) and may change
> before it's built.

Code in planned sections has not been compiled. The runnable examples are
checked against the current compiler; elided and illustrative fragments
need the surrounding program described in their chapter.

When the book and the compiler disagree, reproduce the case and check the
implemented subset and its conformance tests. Either the book or the compiler
can have a bug. The specification, `coax/spec.md`, is the design of record;
planned features are not claims about the current compiler.

## Chapter status

The [chapter registry](chapters.json) records every chapter's number, title,
status, profile, dependencies and three separate facts:

- **Implemented source** (`source`): the code the chapter shows or runs.
- **Qualified targets** (`qualified_targets`): the targets, such as `cpu`,
  `cuda`, `metal` or `wasm`, with recorded evidence for the chapter's
  profile. Source that might compile for a target doesn't qualify it.
- **Adoption** (`adopted_by`): code outside the book that uses the chapter's
  code today in an application or reusable library, including direct build
  and binding code. Each recorded consumer uses an identified source or
  generated interface described below. Tests, examples and fixtures do not
  count as adoption. An empty list means no adoption is recorded.

A chapter's status is one of *implemented*, *partly implemented*, *design
draft* or *planned*. A planned chapter starts with the planned note shown above, and a partly implemented chapter with `**Status: partly implemented.**`; the contents and the list below label both. `python3 coax/book/check-structure.py` fails when a status marker, a
contents label, the list below or the registry disagree.

Status describes the chapter's named teaching profile. An *implemented*
chapter can also explain a planned extension: chapters 5 and 7 teach the initial
native LP64 sums, direct Results, optionals and cleanup while naming their
remaining containment, generic, effect and target limits. Chapter
counts therefore measure recorded teaching coverage. They do not measure
completion of the language specification, a whole product or every target.

### Recorded direct consumers

These are selected source-to-consumer relationships, not an exhaustive usage
inventory. Adding one records use; it does not extend `qualified_targets` or
turn a library's bounded profile into the full chapter goal.

| Chapter | Source and direct use outside this book |
| --- | --- |
| [8. Generics](ch08-generics.md) | [`coax/lib/bounded.coax`](https://github.com/OpenAgentsInc/openagents/blob/main/coax/lib/bounded.coax) supplies fixed-storage `Bounded` values to [`psionic/lib/shadow.coax`](https://github.com/OpenAgentsInc/openagents/blob/main/psionic/lib/shadow.coax)'s model registry and [`metaverse/fairy-forest/forest.coax`](https://github.com/OpenAgentsInc/openagents/blob/main/metaverse/fairy-forest/forest.coax)'s blooms. It does not supply owning containers. |
| [17. GPU programs](ch17-gpu.md) | The iOS app's [`generate.sh`](https://github.com/OpenAgentsInc/openagents/blob/main/apps/openagents-ios/generate.sh) emits its component and composition shaders as Metal with `coax metal`. This is the existing graphics shader path; it does not qualify every GPU profile in the chapter. |
| [19. Builds](ch19-builds-hot-reload.md) | [`coax/build.sh`](https://github.com/OpenAgentsInc/openagents/blob/main/coax/build.sh), [`coax/web/tool.sh`](https://github.com/OpenAgentsInc/openagents/blob/main/coax/web/tool.sh), [`coax/web/wasm.sh`](https://github.com/OpenAgentsInc/openagents/blob/main/coax/web/wasm.sh) and [`coax/web/serve.sh`](https://github.com/OpenAgentsInc/openagents/blob/main/coax/web/serve.sh) are invoked directly by [`apps/openagents-ios/generate.sh`](https://github.com/OpenAgentsInc/openagents/blob/main/apps/openagents-ios/generate.sh), [`apps/openagents-desktop/build.sh`](https://github.com/OpenAgentsInc/openagents/blob/main/apps/openagents-desktop/build.sh), [`apps/openagents-desktop/install.sh`](https://github.com/OpenAgentsInc/openagents/blob/main/apps/openagents-desktop/install.sh), [`apps/openagents-web/build.sh`](https://github.com/OpenAgentsInc/openagents/blob/main/apps/openagents-web/build.sh) and [`gamebuilder/web/build.sh`](https://github.com/OpenAgentsInc/openagents/blob/main/gamebuilder/web/build.sh). `build.coax` and live hot reload remain planned. |
| [25. Voice](ch25-case-study-voice.md) | [`metaverse/platform/voice.c`](https://github.com/OpenAgentsInc/openagents/blob/main/metaverse/platform/voice.c) directly calls the generated C interfaces of `metaverse/voice.coax`, `voice-wire.coax`, `voice-floor.coax` and `voice-meter.coax`. The chapter's smaller `ch25_*` examples remain teaching programs. |
| [26. Libraries](ch26-library-recipes.md) | [`coax/lib/sequence.coax`](https://github.com/OpenAgentsInc/openagents/blob/main/coax/lib/sequence.coax) is loaded by [`metaverse/gpu/everglade-pass.coax`](https://github.com/OpenAgentsInc/openagents/blob/main/metaverse/gpu/everglade-pass.coax) to reverse diagnostic digits and by [`psionic/lib/serve-engine.coax`](https://github.com/OpenAgentsInc/openagents/blob/main/psionic/lib/serve-engine.coax) to swap bytes during HTTP buffer rotation. These are synchronous caller-owned operations. |
| [29. Frames](ch29-units-and-frames.md) | [`metaverse-engine/cam/spacecam.coax`](https://github.com/OpenAgentsInc/openagents/blob/main/metaverse-engine/cam/spacecam.coax) uses typed world points and a floating origin; [`panda-fk-types.coax`](https://github.com/OpenAgentsInc/openagents/blob/main/psionic/lib/robotics/panda-fk-types.coax) and [`panda-fk-math.coax`](https://github.com/OpenAgentsInc/openagents/blob/main/psionic/lib/robotics/panda-fk-math.coax) use typed world positions and affine offsets. The chapter identifies the remaining untyped transforms, joints and spacecraft states. |
| [30. Clocks](ch30-clocks-and-ticks.md) | [`coax/lib/clock.coax`](https://github.com/OpenAgentsInc/openagents/blob/main/coax/lib/clock.coax) is loaded directly by [`metaverse-engine/space/ephemeris.coax`](https://github.com/OpenAgentsInc/openagents/blob/main/metaverse-engine/space/ephemeris.coax) for typed UTC/TAI/TT/TDB instants. Compiler-enforced clock effects and `task @rate` remain planned. |
| [41. Budgets](ch41-compile-budgets.md) | [`coax/web/wasm.sh`](https://github.com/OpenAgentsInc/openagents/blob/main/coax/web/wasm.sh)'s per-step cache and report driver are invoked by [`apps/openagents-web/build.sh`](https://github.com/OpenAgentsInc/openagents/blob/main/apps/openagents-web/build.sh), [`gamebuilder/web/build.sh`](https://github.com/OpenAgentsInc/openagents/blob/main/gamebuilder/web/build.sh), [`coax/native/generate.sh`](https://github.com/OpenAgentsInc/openagents/blob/main/coax/native/generate.sh) and [`metaverse/gpu/everglade-pass.coax`](https://github.com/OpenAgentsInc/openagents/blob/main/metaverse/gpu/everglade-pass.coax). R22's general startup and kernel budgets remain planned. |

## Promoting a draft to a teaching chapter

A design draft or planned chapter becomes a teaching chapter only when it has
all four of the following:

1. **A worked calculation or trace.** A small input whose result the chapter
   works out by hand, step by step, before the program runs.
2. **Runnable source.** The code and an example in [`examples/`](https://github.com/OpenAgentsInc/openagents/tree/main/coax/book/examples/), with an exact
   transcript that the book's checks compare in debug and release.
3. **A deliberately broken case and its diagnostic.** An input or an edit
   that the code refuses, with the exact error or refusal shown and its exit
   status checked.
4. **An independent check.** A result computed another way, such as a
   separate oracle, a reference implementation or a hand-derived value, that
   the checks compare with the code's result.

When all four exist, update the chapter's status in the same commit in four
places: the chapter's status marker, its entry in [chapters.json](chapters.json),
its label in the [contents](SUMMARY.md) and its label in the list below. The registry
check fails when they disagree. [Chapter 29](ch29-units-and-frames.md)'s
affine section is an example: a hand calculation, `examples/ch29_frames.coax`,
a refused program with `E0834`, and a `#test` that compares the result with
plain arithmetic.

## Completion route

The current route through chapters 1–28 and chapter 29's affine section is
usable now. Completing the rest proceeds through the existing roadmap and
chapter gates. The order below is a priority order; a slice can proceed when
its own prerequisites and consumer are ready.

1. **Keep the current guide complete for its bounded profile.** Reconcile
   chapters 5–10, 17, 22 and 27 with delivered language and compiler changes;
   retain exact examples, diagnostics and the limits in
   [chapter 23](ch23-verification-boundaries.md). Keep source, target evidence
   and direct adoption separate in the registry and the
   [paths through all nine books](https://github.com/OpenAgentsInc/openagents/blob/main/docs/books/paths.md).
2. **Finish shared application contracts.** Close the roadmap's remaining
   R0 device, frame-tail, development-loop and independent-review gates.
   Chapters [6](ch06-memory.md), [7](ch07-errors.md),
   [15](ch15-asynchronous-work.md), [20](ch20-interop.md) and
   [24](ch24-a-complete-boundary.md) now join handled Results, the real file
   adapter and System One/Lab request ownership. Their bounded native profile
   and component adoption are distinct from whole-desktop/device readiness;
   see the [request closure qualification](https://github.com/OpenAgentsInc/openagents/blob/main/docs/verification/coax-12625-closure-2026-11-02/README.md).
   Its cache/origin and final query scopes have zero survivors; the v6 native,
   fixedpoint, authored and actual-cost gates pass. Whole-desktop
   E0381 and normal release/cache gates retain their separate scope. Extend chapter
   17 only with named GPU resource, layout and artifact evidence. The shipped
   bounded generics, modules and borrow diagnostics remain the starting point.
3. **Build physical-world facilities through the shared consumers.** Complete
   chapters 29–37 in their dependency order, using the Metaverse realm, Psionic's
   Panda loop and the spacecraft scenario. Frames and clocks lead to bounded
   control, strict integrators and replay, then model import, stamps,
   supervision, faults and provenance. Chapters
   [38](ch38-a-simulated-robot.md) and [39](ch39-a-freighter-scenario.md)
   become worked capstones when those consumers meet their gates; the same
   facilities support the OpenAgents Metaverse Engine's physics and space chapters.
4. **Give one solver the compute forms.** Complete chapters
   [40](ch40-logical-tensors.md)–[43](ch43-differentiating-kernels.md) through
   the Metaverse Engine's solver state and Psionic's existing compute consumers: two layouts
   and batch sizes first, then measured compilation/cache budgets, graph and
   buffer ownership, and independently checked derivatives.
5. **Finish the supported toolchain on measured evidence.** Chapter
   [44](ch44-self-hosting.md) needs a clean bootstrap of real applications and
   an explicit trust boundary. Chapters [45](ch45-body-replacement.md) and
   [46](ch46-resident-compilation.md) need a useful unchanged-layout experiment
   before transactional migration. Chapter [47](ch47-backends.md) remains
   conditional on a measured backend bottleneck. These tracks can overlap
   earlier work under the roadmap's dependency rules.

## Where the examples live

Every program the book runs is a file in
[`examples/`](https://github.com/OpenAgentsInc/openagents/tree/main/coax/book/examples/), next to its exact output in a `.out` file.
The script `check-examples.sh` in this folder compiles and runs each one
and compares the output in debug and release modes. It also runs authored
checks, library/host examples and local link checks. Selected source-linked
listings are compared with their example files; excerpts and illustrative
fragments are not all standalone programs. Programs shown only being
compiled have no `.out` file. Deliberate traps have explicit `.status`
files; mode-specific output or
status uses `.release.out` and `.release.status`. A matching error message
with the wrong exit status is a failure. See Chapter 23 for the limits.

Commands assume you're at the root of the openagents repository, and
`coax` means `coax/bin/coax`. Chapter 1 shows how to build it and put it
on your path.

## Contents at a glance

To write current Coax, build a playable realm, run an original model, train a
small model or sign and verify a Nostr event, see
[Paths through all nine books](https://github.com/OpenAgentsInc/openagents/blob/main/docs/books/paths.md).


- [Foreword](foreword.md) and [Introduction](introduction.md)
- Part I. Getting started:
  [1. Getting started](ch01-getting-started.md),
  [2. A first project](ch02-a-first-project.md),
  [3. Common concepts](ch03-common-concepts.md),
  [4. Reading errors](ch04-reading-errors.md)
- Part II. The language:
  [5. Structs, enums and unions](ch05-structs-enums-unions.md),
  [6. Arrays, strings, memory and the context](ch06-memory.md) (partly implemented),
  [7. Errors and cleanup](ch07-errors.md),
  [8. Generics and constraints](ch08-generics.md) (partly implemented),
  [9. Compile time](ch09-compile-time.md),
  [10. Organizing a larger program](ch10-organizing-programs.md),
  [11. Writing checks](ch11-writing-checks.md),
  [12. Debugging and measuring cost](ch12-debugging-and-cost.md)
- Part III. Data and speed:
  [13. Data-oriented layout](ch13-data-layout.md) (partly implemented),
  [14. Jobs: captures as effects](ch14-jobs.md) (partly implemented),
  [15. Asynchronous work and lifetimes](ch15-asynchronous-work.md),
  [16. Determinism](ch16-determinism.md) (partly implemented),
  [17. GPU programs](ch17-gpu.md) (partly implemented)
- Part IV. Coax in OpenAgents:
  [18. The protocol boundary](ch18-protocol.md),
  [19. Builds and hot reload](ch19-builds-hot-reload.md) (partly implemented),
  [20. Living beside C, C++ and Swift](ch20-interop.md),
  [21. Targets: Apple first, then the browser](ch21-targets.md),
  [22. Built for agents](ch22-agents.md),
  [23. What verification establishes](ch23-verification-boundaries.md)
- Part V. Larger programs:
  [24. A complete boundary](ch24-a-complete-boundary.md),
  [25. Building a voice system in pieces](ch25-case-study-voice.md),
  [26. Reusing bounded libraries](ch26-library-recipes.md)
- Part VI. Building Coax:
  [27. Inside coax0](ch27-inside-coax0.md),
  [28. The road ahead](ch28-road-ahead.md)
- Part VII. The physical world:
  [29. Units and coordinate frames](ch29-units-and-frames.md) (partly implemented),
  [30. Typed clocks and multirate ticks](ch30-clocks-and-ticks.md) (partly implemented),
  [31. Bounded control](ch31-bounded-control.md) (planned),
  [32. Strict math, integrators and deterministic jobs](ch32-strict-math-and-integrators.md) (partly implemented),
  [33. Replay and snapshots](ch33-replay-and-snapshots.md) (planned),
  [34. Importing robot models](ch34-robot-models.md) (planned),
  [35. Stamped sensors and supervised actuators](ch35-sensors-and-actuators.md) (planned),
  [36. Fault scenarios you can replay](ch36-fault-scenarios.md) (planned),
  [37. Provenance, fidelity profiles and rule modules](ch37-provenance-and-rule-modules.md) (planned),
  [38. A simulated robot, end to end](ch38-a-simulated-robot.md) (planned),
  [39. A freighter scenario, end to end](ch39-a-freighter-scenario.md) (planned)
- Part VIII. Simulation compute:
  [40. Logical tensors over chosen layouts](ch40-logical-tensors.md) (planned),
  [41. Compile and startup budgets](ch41-compile-budgets.md) (partly implemented),
  [42. Device graphs and zero-copy buffers](ch42-device-graphs.md) (planned),
  [43. Differentiating kernels](ch43-differentiating-kernels.md) (planned)
- Part IX. Finishing the toolchain:
  [44. A self-hosted compiler](ch44-self-hosting.md) (planned),
  [45. Replacing a procedure body while it runs](ch45-body-replacement.md) (planned),
  [46. Resident compilation and state migration](ch46-resident-compilation.md) (partly implemented),
  [47. Backends chosen by profiling](ch47-backends.md) (planned)
- Appendices: [A. Keywords, directives and types](appendix-a-keywords.md),
  [B. Error codes](appendix-b-error-codes.md),
  [C. Coax for Jai programmers](appendix-c-jai.md),
  [D. Coax for C++ programmers](appendix-d-cpp.md),
  [E. Glossary](appendix-e-glossary.md),
  [F. Sources](appendix-f-sources.md)
