Runtime Model

Turn execution is a process running. The configuration is the full state of that process at any point — its environment, managed context, memory, mailbox, tool registry, turn state, and remaining program.


Configuration (Process State)

ComponentTypeLifetime
envMap: identifier → valueLexical: push/pop on blocks
contextPriority Stack (System, Plan, Scratchpad, History)Per process. Token-bounded
memoryKey-value map + semantic vectorsPer process. Persisted across runs
mailboxMessage queue (send/receive)Per process
tool_registryMap: tool name → handlerSet at startup
turn_stateTurn ID + pending suspensionUpdated on turn entry / suspension
programRemaining AST/bytecodeCurrent instruction pointer
notation
Config = (env, context, memory, mailbox, tool_registry, turn_state, program)

Serializable state = (env, context, memory, mailbox, turn_state)
Tool registry and program are provided separately on resume.

Invariants

InvariantToken Budget (Gas)

context.append(expr) is gas-metered. If appending exceeds the budget, the VM executes the Entropic Expansion Policy — semantic summarization or Priority Stack eviction. The VM does not allow context to silently overflow.

InvariantPriority Stack Preservation

P0 (System/Mission) is never evicted. P1 (Scratchpad) is summarized. P2 (History) is dropped first.

InvariantConfiguration Well-Formedness

All components are finite, valid structures at all times. No component is undefined or malformed.

InvariantSerializable State

The tuple (env, context, memory, mailbox, turn_state) is sufficient to restore execution given the program and tool registry.


Transition: One Step

Small-Step Transition

Config → Config' or Config → Suspension(effect, arg, continuation). One step takes the configuration to a new configuration or to a suspension point.

Key transition rules:

  • let id = expr → evaluate expr, extend env with binding
  • context.append(expr) → evaluate, append (with token budget check)
  • remember(k, v) → update memory
  • call(tool, arg)Suspension (runtime executes tool, resumes with result)
  • infer Type { prompt }Suspension (LLM call with schema validation)
  • suspendSuspension (durable checkpoint to disk)
  • return expr → turn completes with value

Deterministic Semantics

IMPORTANT

Turn's core language is deterministic: given a configuration and input sequence, execution is reproducible. Non-determinism is quarantined at effect boundaries (call, infer). Same inputs → same state transitions.

This enables:

  • Debugging: Replay input sequence → reproduce the bug
  • Audit: Log inputs → reconstruct what happened
  • Testing: Provide deterministic inputs → test behavior
  • Formal model: S(t+1) = F(S(t), e(t)) — well-defined and reproducible

The Universal Loop

implementation pattern
1. Load:   Initialize VM with Program + State (or fresh)
2. Run:    Execute until Complete or Suspended
3. Handle: If Suspended(tool, arg, continuation):
 a. Persist continuation to durable storage
 b. Execute the tool (async, human-in-the-loop, etc.)
 c. Resume: load continuation, inject result, goto 2
4. Complete: Return final value

THE ENGINEERING SOLUTION

This loop ensures the agent is never "blocked" on a thread — it is "suspended" in state. This is the key to scalable, long-running agentic software.