# Architecture (/docs/architecture)



Every LLM call in `@pleach/core` routes through one of four seams,
resolves against a family-locked model matrix, and lands as one
append-only `AuditableCall` row. Six pieces compose that path:
`SessionRuntime`, `CompiledGraph`, `Channels`, `Seams`, the
`AuditableCall` ledger, and the event log — the four-stage lattice
(`anchor-plan` → `tool-loop` ⇄ `synthesize` → `post-turn`)
constrains where calls live, and the `AuditableCall` ledger
carries what they did.

These six are the structural members of the hedge — the rigid
frame the agent's branches grow against. The four-stage lattice
says where a call can go; the channels say what a call can read
and write; the seams say which model a call routes to; the
ledger says where its outcome lands. No piece is optional; no
piece is replaced silently.

This page is the substrate-wide map. Each section names a piece,
the role it plays, and the page that owns the deep-dive. The
[boundary rules](#10-boundary-rules) at the bottom are unique to
this page — they're the architectural invariants the rest of the
design depends on.

This page mirrors the [`@pleach/core` architecture deep-dive](https://www.getpleach.com/docs/core)
in the package repo. If the two disagree, the repo wins — file
an issue against [pleachhq/core](https://www.getpleach.com/docs/core).

## TL;DR — six pieces, one substrate [#tldr--six-pieces-one-substrate]

**Every arrow points away from `SessionRuntime`; nothing points back.**

<Mermaid
  chart="flowchart LR
    SR[SessionRuntime] -->|owns| G[CompiledGraph]
    G -->|reads/writes| Ch[Channels]
    G -->|invokes| S[Seams]
    S -->|memoizes via| C[Cache]
    C -->|resolves via| M[modelfamily matrix]
    S -->|records| A[AuditableCall ledger]
    G -->|emits| E[EventLog]
    Ch -.->|snapshot| Cp[Checkpointer]"
/>

1. **`SessionRuntime`** owns per-session state, the compiled graph,
   and the lifecycle.
2. **`CompiledGraph`** is the declarative topology — nodes wired to
   channels, edges constrained to a four-stage lattice.
3. **Channels** are typed reactive state slots. A node fires when one
   of its subscribed channels advances; concurrent writes have well-
   defined reducer semantics.
4. **Seams** are per-call-class provider entry points. Every LLM call
   goes through exactly one seam; the `callClass` literal is lint-
   restricted to the seam factories.
5. **Modelfamily matrix** resolves `(family × callClass)` → model id +
   transport. Family is locked at session start; the matrix never
   silently widens.
6. **Audit ledger** + **event log** carry the two write streams: every
   addressable decision lands in the ledger; every observable event
   lands in the log.

## The execution-graph cluster [#the-execution-graph-cluster]

Three of the six pieces — `CompiledGraph`, `Channels`, and the
nodes the graph compiles — form a tighter cluster: the per-turn
execution substrate that lives *inside* the lattice. The full
triplet framing lives at
[Concept clusters → Execution-graph](/docs/concept-clusters#execution-graph-cluster);
the deep-dive pages are [Graph](/docs/graph), [Nodes](/docs/nodes),
and [Channels](/docs/channels).

The piece that *runs* that cluster is the **engine** — the
`engine/` scheduler (`SuperstepRunner` and its primitives). Where
`CompiledGraph` is the declarative topology, the engine is the
deterministic superstep executor that drives it: each superstep
fires the set of triggered nodes in parallel
(`shouldTriggerNode` resolves channel-version advances into node
activations), accumulates their writes, and commits the resulting
channel updates atomically at the step boundary. That Pregel-style
discipline — run, accumulate, commit — is what makes graph
scheduling deterministic and race-free between concurrent writers,
and it underwrites the byte-replay property in §9. The engine also
owns the cross-cutting execution plumbing the graph depends on:
retry-with-backoff (`RetryPolicy`), abort-signal composition
(`AbortComposer`), and graph↔session state mirroring
(`GraphStateSynchronizer`). It ships in the main `@pleach/core`
entry today, not a separate subpath.

## 1. Stage lattice [#1-stage-lattice]

**Nine legal transitions. CI rejects every other pair.**

<Mermaid
  chart="flowchart LR
    AP[anchor-plan] --> TL[tool-loop]
    AP -->|intra-stage chain| AP
    TL -->|decide / dispatch| TL
    TL --> SY[synthesize]
    TL -->|recovery dispatch| PT[post-turn]
    SY -->|bounded re-entry, same messageId| TL
    SY --> PT
    PT -->|intra-stage chain| PT
    PT -.->|next turn| AP"
/>

Every node in the compiled graph belongs to exactly one of four
stages — `anchor-plan` → `tool-loop` → `synthesize` → `post-turn`.
The runtime enumerates nine legal
`(from-stage, to-stage)` pairs: six cross-stage transitions (the
three forward edges, the `tool-loop → post-turn` recovery-dispatch
edge, the bounded `synthesize → tool-loop` re-entry — at most once
per turn, same `messageId` — and the `post-turn → anchor-plan`
next-turn rollover) and three intra-stage chains — `anchor-plan`,
`tool-loop`, `post-turn`. `synthesize` is the only stage with no
intra-stage edge: the `synthesize → synthesize` retry self-loop is
retired and forbidden. Every other pair is forbidden.
The lattice is structural, not advisory: CI rejects any
out-of-lattice edge in the default graph.

Why structural matters: once stages are structural, *cost
allocation*, *observability*, and *time-travel* become structural
too. A group-by on stage in the audit ledger returns per-stage
spend for any turn against a row shape that exists by construction
— every row carries a non-null stage id because a node that
doesn't declare a stage fails CI before it ever fires.

See [Graph](/docs/graph) for the substrate API, [Nodes](/docs/nodes)
for the per-node metadata, and [Turn lifecycle](/docs/turn-lifecycle)
for the per-stage stream-event and ledger-payload tables.

## 2. Call classes [#2-call-classes]

Every LLM call declares one of four classes — `utility`,
`reasoning`, `converse`, `synthesize` — which the seam factory
carries through to the matrix. The class drives three things: the
seam that carries the invocation, the per-turn allotment, and the
audit-row slice the call lands in. `synthesize` goes through one
seam per runtime, and a turn lands at most one terminal synthesis
row, so the rendered string and the audited string are the same
string. A turn can still make more than one synthesize call (a
recovery or replacement retry); each one is billed and gets its own
audit row.

See [Call classes](/docs/call-classes) for the four-class taxonomy,
the per-class roles, and the CI check that restricts the literal.

## 3. Seams [#3-seams]

A seam is the per-call-class provider entry point. Four factories
live in `graph/seams/`, one per call class. The seam carries the
locked `callClass`, threads it as a type parameter into matrix
resolution, and dispatches a sync per-chunk observer ladder on the
inbound stream — observers return `continue`, `amend`, `emit`, or
`halt`. `onChunk` is sync-only because async would race on replay.

See [Seams](/docs/seams) for the four factories, the singleton
synthesize seam, the observer verdict ladder, and how a node
consumes a seam.

## 4. Family-locked routing [#4-family-locked-routing]

**The cascade walks one family's column, then stops. No arrow leaves it.**

<Mermaid
  chart="flowchart TD
    Start[pickNextInFamily] --> R1[anthropic rung 1]
    R1 -.->|fail| R2[anthropic rung 2]
    R2 -.->|fail| R3[anthropic rung 3]
    R3 -.->|fail| FE[family-exhausted]
    FE --> Stop[runtime surfaces state to host]
    Other[other family column]
    FE -.-x Other"
/>

`(ProviderFamily × CallClass)` keys the model resolution matrix.
Family and transport lock at session start. When a provider call
fails, the cascade walks the next rung in the same family
(`pickNextInFamily`); when the column is exhausted, the runtime
records a family-exhausted delivery outcome and surfaces the
state to the host. No silent cross-family widening. A model no
family derives from (a BYOK slug, a gateway alias, an
unrecognized id) keeps its served model; the legacy cross-family
fallback for such models is a deprecated opt-in
(`legacyNonMatrixFallback`), removed in 1.0.0.

See [Family-locked routing](/docs/family-lock) for the lock
contract, the four properties it freezes (tokenizer, prompt-cache
key, tool-call dialect, refusal pattern), and the family-strict
cascade. The per-family per-callclass lookup lives at
[Model resolution matrix](/docs/model-resolution-matrix).

## 5. Audit ledger [#5-audit-ledger]

**Four seams, one table. Every call lands one row.**

<Mermaid
  chart="flowchart LR
    US[utilitySeam] --> L[(AuditableCall ledger)]
    RS[reasoningSeam] --> L
    CS[converseSeam] --> L
    SS[synthesizeSeam] --> L
    L -.-> ID[identity tuple: sessionId, turnId, stageId, seqWithinTurn]"
/>

Every seam writes one row per call. The four-field identity tuple
is non-null by construction. The row is append-only by interface
contract — no `update`, no `delete`; a row that needs to mutate is
a wire-format break that bumps `auditRecordVersion` instead. The
ledger is also what makes per-axis attribution work *inside* one
Anthropic Workspace or OpenAI Project: every row carries the
opaque `tenantId` the host wires to its billing axis, and the
rollup is one group-by on tenant.

Three compliance plug-points (`TamperEvidence`, `PIIRedactor`,
`GDPRSoftDelete`) ride on top of the ledger. `TamperEvidence` and
`GDPRSoftDelete` ship as no-op defaults in `@pleach/core`, and the
durable ledgers default `PIIRedactor` to the built-in scrubber
chain; production implementations land
in `@pleach/compliance@0.1.0` (scrubber cohort, redaction, attestation). Hash-chain
verification lives in `@pleach/core/eventLog`
(`verifyChainForChat`, `generateProof`); `@pleach/replay@0.1.0`
composes them.

`@pleach/core/attestation` is the cryptographic signing layer on
top of the canonical row hash — ed25519 over RFC 8785-canonicalized
payload bytes, with a pluggable `AttestationKeyStore` (AWS KMS and
Vault Transit stubs at v1 contract; a file-backed test adapter
under the `/testing` subpath). A signed row makes any post-hoc
mutation detectable per-row; the hash chain makes deletion or
reordering detectable across rows. See [Attestation](/docs/attestation).

See [Audit ledger](/docs/audit-ledger) for the write interface and
the plug-points, [AuditableCall row](/docs/auditable-call-row) for
the row shape, and [Hash chain](/docs/hash-chain) for the
tamper-evidence layer.

## 6. Event log [#6-event-log]

Distinct from the ledger. The event log is the broader stream of
*observable events* — messages, tool dispatches, interrupts,
subagent spawns, exports, plugin domain events. Three layers:
`EventLogWriter` (fire-and-forget enqueue), `durableFlush`
(`waitUntil`-routed flush with status-code-aware retries,
idempotent on client-generated `id`), and `hydrateFromEvents` (a
walk that rebuilds session state from a slice of events).

Every write passes through the `Scrubber` allowlist before
persistence. CI checks that every event type
has a redaction decision (see [Scrubbers](/docs/scrubbers)).

See [Event log](/docs/event-log) for the full per-layer contract.

## 7. Sessions, storage, sync [#7-sessions-storage-sync]

`SessionRuntime` is constructed with three swappable backends —
storage adapter, checkpointer, and (optional) sync. Memory /
IndexedDB / Supabase implementations of each share one interface;
swapping is a one-line change.

The runtime also ships `memoryCacheBackend` as the default
`cacheBackend` — runtime-side memoization of prepared LLM inputs,
default-constructed in the `SessionRuntime` constructor and threaded through the four seam factories. On by
day zero, no opt-in required. This is distinct from provider-side
prompt caching (see [Cache](/docs/cache) and
[Prompt caching](/docs/prompt-caching)).

`SessionRuntime` exposes its capabilities through named accessor
groups (`runtime.sessions`, `runtime.events`, `runtime.spans`,
`runtime.tenant`, `runtime.graph.{recovery, heuristics, config}`),
which is what makes the runtime LLM-agent friendly — an agent
reading `runtime.<TAB>` sees a grouped surface rather than a flat
40-method dump. `runtime.spans` in particular is the in-process
OTel facet, exposing `inFlightCount`, `isShutdown`, and `snapshot`
introspection over the four read-side span types — useful for
graceful-shutdown gates and live dashboards that need to know
whether the in-process exporter has drained (see
[OTel observability](/docs/otel-observability)). The cross-session
dependency graph — typed edges between sessions, artifact
provenance — is read-side observability sitting alongside the
span surface; see [Lineage](/docs/lineage). See [Facets](/docs/facets)
for the full inventory.

Sync uses version vectors per session — `compareVectors` detects
concurrent writes at push time and the coordinator surfaces them
through `sync.conflict` rather than overwriting. See
[Storage](/docs/storage), [Checkpointing](/docs/checkpointing),
and [Sync](/docs/sync) for the per-axis deep-dives.

## 8. Plugins [#8-plugins]

`HarnessPlugin` is the consumer extension contract. A plugin can
register tier nodes into the lattice's enrichment slots, stream
observers, prompt contributors, safety contributions, and domain
event handlers — and crucially, cannot add an out-of-lattice edge,
bypass the singleton synthesize seam, reach across the `seams/`
boundary, or register an async observer dispatch.

Sibling SKUs (`@pleach/compliance`, `@pleach/eval`,
`@pleach/gateway`, `@pleach/replay`, `@pleach/mcp`,
`@pleach/coding-agent`, `@pleach/sandbox`, `@pleach/langchain`,
`@pleach/base-tools`, `@pleach/observe`, `@pleach/recipes`) land
as plugins on top of this contract. Every sibling is
licensed FSL-1.1-Apache-2.0; `@pleach/trust-pack` is the one
exception, a reserved npm name. See
[Packages](/docs/packages) for the canonical per-SKU status table.
The plugin contract is stable so a host implementing the same
plug-points manually swaps into the sibling SKUs without
graph-shape changes.

See [Plugin contract](/docs/plugin-contract) for the full
`definePleachPlugin` surface, every slot, and the breadcrumbs that
fire when a capability isn't contributed.

## 9. Determinism [#9-determinism]

Several pieces of the substrate are deliberately deterministic —
the fingerprint module, channel reducers, stream observers,
prompt composition, and the `engine/` superstep scheduler
(`SuperstepRunner`). Together they buy the
replay property: a recorded turn replayed against the same
package version + the same input re-serves its recorded
fingerprint stream, and any drift raises `ReplayDivergenceError`. This is the property `@pleach/eval@0.1.0`
and `@pleach/replay@0.1.0` are built around (both have real
bodies for their entry points; neither is on npm yet).

See [Determinism](/docs/determinism) for the five contracts and
[Fingerprint](/docs/fingerprint) for the pure-function fingerprint
that platform-uniformly hashes prepared inputs.

## 10. Boundary rules [#10-boundary-rules]

CI enforces the architectural boundaries:

| Boundary                          | What's forbidden                                                                               |
| --------------------------------- | ---------------------------------------------------------------------------------------------- |
| Seams and the model family matrix | Importing the matrix from a seam, or app-layer code from core                                  |
| Call classes                      | A `callClass: "..."` literal outside the four seam factories and the matrix module             |
| Fallback                          | Reassigning the model in a fallback path                                                       |
| Stream observers                  | Binding a detector directly to a seam, rather than through a plugin                            |
| Graph                             | An out-of-lattice edge                                                                         |
| Audit row                         | A row shape change without an `AuditRecordVersion` bump                                        |
| Domain purity                     | Host vocabulary, vendor backend names, tool prefixes, and domain phrasing in core's own source |

Each row is a structural guarantee the rest of the design depends on.
If `callClass` could appear anywhere, the
seam model leaks — a node could call `synthesize` outside the
singleton seam and the rendered string would diverge from the
audited string. If family could silently widen, the modelfamily lock
is advisory — a tool that worked on `anthropic`'s tool-call dialect
would start failing mid-session when the cascade reached an
`openai`-shaped call. If observers could be async, replay determinism
is gone — `@pleach/replay` strict mode would throw
`ReplayDivergenceError` on the first race between two observer
promises resolving in a different order on replay than on record.
And if a host name, a vendor backend, or a sandbox tool prefix
could land in core's own source, the language-agnostic claim
leaks — the Go runtime would have to mirror string literals that
only mean something to one TS consumer.
That check makes plugins the only legitimate channel for
consumer-specific content.

What's checked on every change — package shape, plugin contracts,
event-log integrity — is listed at [Release checks](/docs/audit-gates).

## Every page in this group [#every-page-in-this-group]

Thirty-three pages sit under Architecture. They group into eight
themes, and this table is the index —
[Concept clusters](/docs/concept-clusters) is the *concept* map, which
is a different thing: it names the primitives, not the pages.

| Theme                         | Pages                                                                                                                                                                                                 |
| ----------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Orientation**               | [Concept clusters](/docs/concept-clusters) · [Language-agnostic contract](/docs/language-agnostic-contract) · [Defensive patterns](/docs/defensive-patterns)                                          |
| **The execution graph**       | [Graph](/docs/graph) · [Nodes](/docs/nodes) · [Channels](/docs/channels) · [Node catalog](/docs/graph-node-catalog) · [Edge catalog](/docs/graph-edge-catalog) · [Seams](/docs/seams)                 |
| **Routing a call**            | [Call classes](/docs/call-classes) · [Family-locked routing](/docs/family-lock) · [Model resolution matrix](/docs/model-resolution-matrix)                                                            |
| **Lifecycle**                 | [Session lifecycle](/docs/session-lifecycle) · [Turn lifecycle](/docs/turn-lifecycle) · [Config manifest](/docs/config-manifest)                                                                      |
| **The record**                | [Event log](/docs/event-log) · [Event-log projections](/docs/event-log-projections) · [Schema](/docs/schema)                                                                                          |
| **Streaming and interaction** | [Stream events](/docs/stream-events) · [`streamSingleTurn`](/docs/stream-single-turn) · [Interrupts](/docs/interrupts) · [Async tasks](/docs/async-tasks)                                             |
| **Persistence**               | [Storage](/docs/storage) · [Checkpointing](/docs/checkpointing) · [Sync](/docs/sync) · [Time travel](/docs/time-travel) · [Cache & memoization](/docs/cache) · [Prompt caching](/docs/prompt-caching) |
| **Determinism**               | [Determinism](/docs/determinism) · [Fingerprint](/docs/fingerprint)                                                                                                                                   |
| **Plugins**                   | [Plugin contract](/docs/plugin-contract) · [Plugin authoring standards](/docs/plugin-authoring-standards) · [Plugin bundles](/docs/plugin-bundles)                                                    |

## Where to go next [#where-to-go-next]

These four are the usual next reads. The first two sit in other nav
groups, because the runtime surface and the ledger schema are build
concerns rather than architecture ones.

<Cards>
  <Card title="SessionRuntime" href="/docs/session-runtime" description="Construct the runtime, create sessions, execute messages, abort turns." />

  <Card title="Audit ledger" href="/docs/audit-ledger" description="The call ledger schema and the three compliance plug-points." />

  <Card title="The AuditableCall row" href="/docs/auditable-call-row" description="Per-call row shape — turnId, toolName, subagentDepth, tokenCost." />

  <Card title="Plugin contract" href="/docs/plugin-contract" description="The HarnessPlugin extension surface — what plugins can do and what they can't." />
</Cards>
