Zig
A Result Target, into name, retains the answer of an ask, a tell-block Operation line, a Command Call (including a Host Command), a call statement or a waiting send (ADR 0114). It writes the bare Name with put semantics and keeps it; and wait remains last. Fire-and-forget calls reject targets, and a Message Path Command Call or explicit send needs explicit and wait. A Join records each reached member’s destination and assigns them in start order only after every answer converts and all completion charges are reserved, so duplicate targets retain the last-started answer and failure writes no member targets. Saves retain the member instruction and its owning frame, including early answers and repeated destinations.
The Zig Core is a third NorthTalk Core for embedding outside Go and TS: natively
through the C API in include/talk.h, and as a wasm32-wasi
module for Hosts with a WASM runtime
(ADR 0078).
It is written from the Spec, its Data Files and the Conformance Corpus, never
translated from the Go or TS Core
(ADR 0071,
ADR 0009). The
design decisions that bind it are recorded on
#625.
Built step by step through
Appendix B, it conforms to
language 1.0-rc.4 and Cost Model 0
(#636): it passes every
Corpus case, every Trace Case in both replays and the pinned Unicode tests, its
REPL reproduces every Session Transcript, and talk.h declares every talk.go
name. Blessing needs it to agree with the Go and TS Cores, and the nightly
differential fuzzer runs it. Its Message Layer carries request-charge alongside the other chapter 9 messages.
Use the Zig task map for implementation files, Spec links, tests and check commands.
A local Handler may share a Script Variable’s or Constant’s name in either
declaration order. Value references read the variable or Constant; Command
Calls and Deliveries reach the Handler. Extensions preserve both inherited
lookups and Handler suspension checking, including after Save/Restore. An
extension that redeclares an existing name is still refused with name reused;
Library Import clashes retain chapter 7’s rules.
it is a body local. In a Constant or Script Variable initialiser or a
parameter default, each unbound it reports unknown name, alongside
not constant at only the first disallowed token. Handler, function and
Lambda bodies retain their own it, including Lambdas created at load.
Statically resolved Built-in calls check the required and total parameter
counts from chapter 7 at load, including trailing defaults. Invalid counts
report wrong argument count at the Built-in name; calls through shadowing
variables remain dynamic.
Whole Host Object properties can be assigned with set through me, it
or the target, including intermediate keys/chunks that locate the Object.
These implicit bindings cannot be replaced or used as roots for ordinary
Container mutations. set does not modify chunks inside a property’s value.
Direct known-owner read-only properties are checked at load; it and Target
properties are checked at runtime (ADR 0085).
a otherwise b and the optional k of x follow chapter 4 of language 1.0-rc.3. otherwise parses as the lowest binary operator and lowers to branch-not-nothing and pop at its own token, so the left side runs once and the fallback only on Nothing. Optional reads parse into their own optional_key node, so they are never Containers; the optional of m stays the ordinary key read by the optional-key decision. They lower to get-optional-key instructions at their the, which read maps and Nothing only and reject every Host Object, even for id, with no Host crossing. The checker checks both forms in every operand in Guards, Constants and other restricted expressions, and an optional read of a known Host Object is no Host property access in a Guard. An unknown name in an initialiser or parameter default is both unknown name and not constant. The Zig Core passes the shared disassembly/nothing-defaults case and the nothing-defaults/, load-diagnostics/nothing-defaults and load-diagnostics/constant-unknown-name Trace Cases.
Layout
Section titled “Layout”-
build.zig,build.zig.zon: one Zig package,northtalk, pinned to Zig 0.17.1 byminimum_zig_version. It uses Zig’s standard library only: no third-party packages and no linked C libraries. -
include/talk.h: the C API. It mirrorsspec/embedding/talk.go’s names in C style, astools/zig/api.tsspells them, and declares only what the Core implements. -
src/root.zig: the package root.src/talk.zigholdstalk.h’s exports. -
src/reactor/: the WASI reactor’s image root, the Core beside the Session Host it serves assession-v1(ADR 0180). The Session Host is an ordinary Host overtalk.h, so the Core can’t import it;message.zigfinds the image’smessage_extensionby name instead. -
src/unicode/: UTF-8, NFC, Characters (extended grapheme clusters), simple case folding, and thewordandword breakrules, built only on the generated tables. -
src/generated/: tables written from the Data Files and the pinned Unicode data. Never edit them by hand. -
src/decimal/: chapter 3 decimals and exact rational arithmetic. Addition, subtraction, multiplication, terminating division and integerdiv/moduse checkedu128intermediates when the exact result fits. Terminating division selects the representable exponent nearest its ideal without rounding. Decimal comparisons handle signs, zeros and equal exponents directly, and align unequal exponents with checkedu128scaling when it fits. Wider comparisons retain bounded padded-digit comparison without allocating. Rounding and wider arithmetic intermediates use managed big integers. Quantity arithmetic shares these paths and uses compile-time catalogue factors, preserving separate conversion steps and their rounding. The Value arithmetic API allocates an immutable result; the run loop can reuse a uniquely held numeric local when storing that result. -
src/value.zig,src/units.zig,src/dates.zig: immutable values, copy-on-write containers, catalogue Units and Gregorian dates. -
src/display.zig,src/encoding.zig,src/pattern.zig: the display reader and writer, lossless Value Encoding and canonical Pattern source reader. -
src/syntax/: the modal lexer of chapter 1 and the predictive parser of chapter 2, which builds a lossless syntax tree. -
src/check/: the checker, which raises chapter 2’s load-time diagnostics and resolves the names the lowering reads. -
src/lower/: the normative lowering of chapter 8 tomachine.toml’s instructions, and the canonical disassembly. -
src/corpus/: case discovery, metadata, encoding replay, Trace parsing, case setup and Disassembly Cases. -
src/machine/: the Abstract Machine: Groups, Scripts, Runs and frames, the run loop, Cost Model 0 charging, chunks and properties, the Built-ins it has so far, Trace writing, the Group Fingerprint, and saving and restoring. -
tests/: C programs that usetalk.has a Host would. -
src/session/: the Session Host of chapter 12, an ordinary Host overtalk.h, and Session Transcripts. -
src/notify/: the Notification routerstorewatches use.
The REPL joins src/ in step 6.
Syntax
Section titled “Syntax”The parser drives the lexer: it names the mode of every token it asks for,
reads at most two tokens ahead for the decisions grammar.toml lists, and
stops at the first syntax error with its code and position. A lexical error
comes back as an invalid token, so a parser that has peeked past the token
it fails at still reports that one. Fenced text is scanned whole when its
opener is lexed, holes and nested literals included, and each literal is
remembered by its offset, so nested interpolation stays linear. Margins,
escapes and line breaks are decoded then; NFC is left to the value.
Every token, line breaks and the end of the source included, is kept in
source order, and a backtick literal with holes is split into pieces around
its holes’ tokens. Tree.reconstruct rebuilds the source byte for byte from
them. The tree’s shape is the Zig Core’s own.
Source nesting is bounded at 128 recursive parser regions, and an expression
tree at 128 levels, so hostile source ends in source nesting too deep, never
a stack overflow. Like the Go Core’s limit, that code is an implementation
limit outside the Spec’s catalogue.
Loading retains the source, executable code and required metadata. The syntax tree, checker tables and lowering buffers use temporary storage counted against the Group’s physical cap, then released. Extensions, Reload and restored code use the same policy; compiled Libraries also release their compiler storage. The WASI check loads 1 MiB of newlines and 100,000 empty Handlers within a 256 MiB instance cap, bounds their retained memory and load time, and runs Handlers after compilation. The load measurements record the causes and before/after figures for #790.
Text-to-Text concatenation trusts the immutable operands’ validated UTF-8. ASCII Values keep an exact Character count, correcting for CRLF at joins, and their byte length supplies scalar charges without rescanning the result. The metadata uses existing Value header space, adds no separate allocation, and is reconstructed from Text bytes on Restore; saved formats and logical charges are unchanged. Unicode joins still normalise the complete result and use the pinned grapheme rules rather than adding Character counts. Host Text construction still validates strict UTF-8. Results remain contiguous owned buffers, so repeated growth still copies the whole prefix.
ASCII concatenation writes directly into the result’s owned storage. The WASI check doubles text under the default Allocation Budget with a 128 MiB instance cap, and the conformance minimum with a 1 GiB cap. Three successive Runs must end in the Allocation Limit Fault, with the instance still answering and running a fresh Handler after each fault.
Checking
Section titled “Checking”check.check takes a parsed unit and a Context: whether it is a Library,
the well-known objects, the Script’s Grants and the Group’s Libraries. It
returns every load-time diagnostic, ordered by position and then by
diagnostics.toml‘s order, with what each Name refers to, each body’s locals
and captures, and which Handlers may suspend. check.exports gives what a
checked Library exports to its importers. The stdlib Libraries’ exports and
the Built-ins come from stdlib.toml.
A Handler may suspend when a clause holds a written Suspension Point, or a
Command Call to a Handler that may, found over the call graph to a fixpoint.
That decides missing and wait, needless and wait and can't suspend here.
A , deciding Handler’s veto and pass are checked over every path through
its body for after a suspension. Local calls, command or function-style, into
a Handler containing veto report veto outside a decision. Handler exports
retain veto positions across code units, so Extend applies the same check to
inherited Handlers without retaining their syntax trees. Refused extensions
leave code identity, Script Variables and existing Runs intact.
Capability calls, say included, are checked only when the Context gives
the Grants, wrong argument among them for a literal against an Operation’s
Shape. The checker also records each call as a Library’s needs, which a
Script’s load checks against its Grants for missing Operations and incompatible
modes, argument counts, labels and literal Shapes. Each use checks original
sites once per defining Library identity, including shared dependencies in a
diamond; separate use lines repeat the check. Direct calls precede imported
Libraries in import order, and missing Operations report once per Operation
per use. Message Layer diagnostic messages use catalogue wording and do not
yet name original Library call sites. Loading raises two
diagnostics after the checker: pattern too large for a Text Pattern literal
whose compiled program, every copy of a counted repetition counted, runs past
the default patternSize, and initialiser failed at the instruction where a
unit’s initialiser raises, which leaves no Script in the Group, or no compiled
Library. import cycle is never raised: talk_compile_library takes a
Library’s imports already compiled, so a cycle can’t be built.
Host Commands (ADR 0094):
an Operation declared command (talk_operation.command, the Message Layer’s
"command": true, case.toml’s command = true) may be called by its name
alone. The checker resolves an unlabelled Command Call that names no Handler of
the unit, local, imported or from an earlier Extend, to the one Operation the
Context’s Grants offer as command, and records it in Result.commands by its
name token: a fire-and-forget one without and wait as tell, any other as
ask. It is then checked as a tell block’s line is, with wrong mode and
wrong argument count at its name and wrong argument at the argument, and
joins the needs, so GrantsAsUsed and an importer’s missing grant see an
ordinary Operation call. Two Grants offering the called command are name clash at the call; an Import named like any offered command is name clash
at its name in the use line, called or not. and wait on one stays not in a join and not in a timeout. A Library’s resolve against its compilation
declarations (talk_operation_check.command). DefineCapability refuses
command on a Reserved Word with invalid value, and the Message Layer
refuses a non-Boolean. The flag is in the Operation Data: the Host Manifest,
the Group Fingerprint and restore matching write "command":true after
segmentBound, only when true. The Zig Core keeps no compile cache, so a
resolution never outlives the Grants it was made against; the code identity is
unchanged.
Argument Labels (ADR 0115):
an Operation Declaration’s labels (talk_operation.labels/labels_lens/
labels_len and the same on talk_operation_check, the Message Layer’s
"labels": [...] on define-capability and compile-library, case.toml’s
labels = [...]) give one word per argument after the first. The parser reads
ask, tell and tell block line arguments as a Command Call’s phrase does,
into ast.Operation.labels. The checker reports wrong argument count first,
then wrong labels at the first label that isn’t the declared one at its
position (check.Operation.wrongLabel), alongside any wrong argument; the
labels join each Need, so an importer’s use line rechecks them. A command
Operation answers its plain name and, for each argument count from 2 it takes,
its name and that prefix of its labels as a Selector (check.Operation.spells);
a labelled Command Call no Operation spells stays a message. Lowering, op
frames and the Trace never see labels. validScopes refuses a wrong length or
a word that isn’t an Argument Label word; the Fingerprint, saves, restore
matching and the Host Manifest write "labels" after args, only when not
empty.
Lowering
Section titled “Lowering”lower.lower turns a unit that checks clean into a code unit: its constant
pool, definitions, variables, objects, body table, code, Unwind Table, offer
records and event table, each instruction with its source position.
lower.disassemble prints its canonical disassembly. The in-memory
instructions are the Zig Core’s own (ADR 0009), and nothing outside src/
depends on them.
Verified static Unit conversion targets carry a dimension signature and an ordered factor plan in counted code storage. Invalid Unit targets retain their runtime conversion failure; preparing metadata adds no Load diagnostic. Plans preserve each Decimal operation and rounding step, storing exponent repetitions as counts. Reload and Restore rebuild them with the code unit; they are absent from canonical disassembly, fingerprints and snapshots. Unit Values keep compact catalogue indices and a derived active-Kind mask, so arithmetic and comparison traverse only present Kinds. No runtime Unit combination or dimension cache is retained.
Each body is lowered into its own buffer, with labels local to it, and the
buffers are laid out in body order at the end. A Lambda or an event test is
numbered and lowered where the lowering reaches it, so its constants and
nested bodies are numbered there too. Temps take the lowest free slot; a catch
handler’s dispatch slots start above every slot the protected body used.
Every inlined copy of a finally block, and the exit after it, is cut out of
the spans of the trys it leaves, so an entry split by copies becomes one
entry per span. The Operation modes that pick ask, ask-wait, join-ask
or tell come from the Context’s Grants. A Host Command lowers to exactly the
instructions of the tell or ask the checker chose: tell, or ask or
ask-wait and a store 0, with no opcode of its own.
Building and testing
Section titled “Building and testing”Run these from impl/zig/, after bun install --frozen-lockfile and
bun run unicode:check at the repository root. The Unicode check downloads and
verifies the pinned UCD files the tests read.
zig build test # the Zig tests and the talk.h C testszig build # zig-out/lib/libtalk.a and zig-out/include/talk.hzig build -Dtarget=wasm32-wasi # zig-out/bin/talk.wasmzig build corpus # the whole Corpus, as CI runs itzig build corpus -- --listzig build fuzzworker # zig-out/bin/talk-fuzzworker, for the fuzzerzig build sidecar # zig-out/bin/messagelayer, native framed stdiozig build corpus -- bytes/value-encodingzig fmt --check .The build has no -Dtest-filter option: run zig build test for unit and C
tests, or select a Corpus case with zig build corpus -- <case>. Output from
std.debug.print in a passing test appears only on the run that executes it;
an unchanged rerun is cached and prints nothing, so save the first
zig build test 2>&1 to a file.
Link a C Host against libtalk.a with zig cc, as the
benchmark Host does in the bench smoke job.
The library isn’t position-independent and calls Zig’s compiler runtime
(__zig_probe_stack), so the system cc fails to link it on Linux, and
-no-pie alone doesn’t help. From the root, after zig build:
zig cc -std=c11 -O2 -Iimpl/zig/include host.c impl/zig/zig-out/lib/libtalk.a -o hostEvery build is -Doptimize=safe (ReleaseSafe) unless told otherwise, so bounds
and integer overflow stay checked while the Core reads hostile input.
-Doptimize=fast is refused.
Instruction scratch uses the standard arena. At each instruction boundary,
an empty arena or one empty buffer without a free list skips the redundant
retain_capacity reset. Dirty buffers, chains and free lists use the standard
reset. This fast path reads the pinned Zig arena state; an upgrade must
recheck that empty-state invariant and the tests in
scratch.zig. Charging, limit admission, landed
control, preemption and operand protection still run at every instruction.
Zig 0.17 renamed standard library APIs that older examples and documentation
still use. Check names against the installed source, under lib_dir from
zig env, rather than online docs:
std.builtinis an alias forstd.lang, defined instd/lang.zig.- Optimize modes are
debug,safe,fastandsmall, so the flags are-Doptimize=safeand so on. std.testing.refAllDeclsRecursiveis gone; userefAllDeclsper container.@typeInfoof an enum givesfield_namesandfield_values, notfields.
The Go adapter, now the Go default, also
uses Zig’s saves, in the shared save format zig/2 (ADR 0144). Its restore-bindings-v1 Message Layer feature
permits Capability/Grant registration while answering resolution, keeps public
Capability names separate from transport handles, and returns the restored
Script/Object identities and pending Call metadata. Saves retain each
pending Call’s Segment id, including preempted open Joins and suspended waits. Saves retain each pending Call’s original Clock for adoption,
even after later Pumps; Reissue uses the first restored Pump’s Clock. A save
taken before a restored Group’s first Pump keeps its queued settlements and how
each settles, and restore requires their calls to still pend (#1055). zig/2
also reads zig/1 saves, which hold no Path (ADR 0184), and
rejects earlier save layouts, Zig’s own zig/17 among them, with invalid save, including under VariablesOnly;
Hosts must drain existing Groups on their old runtime or start fresh Groups.
Rejected mismatches retain their mandatory Trace mode and error-reply records.
The wasm32-wasi target is the Message Layer reactor for Hosts with a WASM runtime, replacing the retired Go wasip1 entry point (ADR 0089). A browser Host can run it through @northtalk/ts/wasm, the TS embedding API over the Message Layer (ADR 0097). The native sidecar also uses Zig (ADR 0090). Go-Core saves cannot be restored here: saves are per Core (ADR 0008). Start fresh Groups from source and Host-owned data, or retain the old Go runtime/artifact until existing Groups are drained or rebuilt; no save converter is supplied.
The wasm32-wasi target builds a WASI reactor: a module with no _start that
exports talk.h’s functions. A Host instantiates it once per instance and calls
the exports directly, passing structs and text through talk_alloc and
talk_free. It also speaks the
Message Layer through
talk_buffer/talk_send (src/message.zig,
#635): one Core per
instance, its hello, Capability definition and Grants, new-group, load,
deliver/request, pump with interim op and prop needs, answer/fail,
save and fingerprint, Object Kinds with object, set-parent and
dispose, {object} Delivery targets and load owner/objects,
broadcast and decide, reload and extend, compile-library,
add-library and replace-library, inspect and grants, restore
with its interim resolve need, settle, $function values and call, export-manifest,
standard-capability for all but sqlite,
counters, and the Host Inputs stop, cancel-run, rewind-run, revoke
and cancel-delivery. Capabilities may declare scopes and Segment-bound Operations, with
segmentLifecycle and release. Their hooks reach the Host during a Pump,
reload or replace-library as effect and release needs and automatic
op needs, and a grant may name a
shared coordinator; store’s hooks are one Coordinator’s.
function-metadata-v1 adds the Function’s declared name (Nothing for a Lambda)
and required/total parameter counts and defining-unit identity to Core-to-Host $function values,
including stale values after Save/Restore. Hosts without the opt-in keep the
original three-item form. Tokens and Function identity stay the same in both
forms; returned metadata never overrides the token’s defining code. Its
script-units query reads the current Script, extension and transitive Library
sources with their identities, without execution or Trace, for a Session Host
to archive the defining documentation across code changes and Restore.
queued-inputs-v1 opens queued-inputs between worker exchanges. It returns
whether the addressed Group has Host Inputs waiting for its next Pump, without
execution, a vars snapshot or Trace. The Go Session uses it for callback
Deliveries, Store Notifications and restored settlements. Runnable Runs and
messages already in Script mailboxes do not count as queued Host Inputs.
analysis-v1 opens analyze-entry, analyze-source, analysis-catalogue
and message-name, using the same language analysis
as the Zig Session Host. These exchanges need no Group, load no source and
record no Trace. They return Entry kind, completeness and documentation state,
positioned syntax errors, declarations and implicit Script Variables, the
Built-in and Standard Library catalogue, the :inspect reader and computed
message-name validity. Load-time diagnostics still come from loading or
extending source. The C API has no separate analysis calls; a C Host can use
the Message Layer, as chapter 9
specifies.
standard-refinements-v1 additionally lets adapters attach a fixed Standard
family to an independently named declaration, retaining raw arguments for their
own factory wrappers. It includes sqlite, with its row cap carried separately
from coordinator identity; the standalone standard-capability sqlite factory
remains unavailable. Locale defaults are length-delimited, including invalid
tags which must reach the guest’s uncharged checks intact. A pump,
reload or replace-library reply’s abandoned lists the started calls the
Core has abandoned since the last notification, once each. To observe cancellation
before subsequent Host callbacks, request features: ["abandoned-before-needs-v1"]
in hello and check the advertised feature. Opt-in lasts for the Session:
interim op, prop, effect and release replies then carry outer abandoned
before their callback, including a release which precedes a held need or final
reply. Final successes retain unreported notices in ok.abandoned; final worker
errors carry them at the outer level. Hosts without opt-in retain final-success
notification timing. See the contract. request-charge acknowledges
enqueue with a Group-wide q<N> id; its later Pump returns a charge result
report with accepted, limit or inactive. Post-start work needs this
authorization; the starting need’s local fuelLeft cannot fund it. A crossing pause, where a
Host function answers deferred, is the transport’s: it ends no Stretch and
writes no record. Segment begin, commit and rollback hooks and automatic
abandonment may defer at a Segment’s end, while draining Stop, cancellation,
owner disposal and Rewind inputs, and while Reload or Library replacement
terminates old Runs. Library replacement holds every proposed Library and
Script until all terminations finish; fatal cleanup prevents publication and
returns effect state unknown. Load, Reload, Extend and Library replacement
cannot revive a Group stopped by fatal cleanup. Pump, Save, Fingerprint, Inspect
and code-change worker calls from lifecycle callbacks return reentrant call,
during a Pump and during code changes. Refused worker inputs retain their Trace
records after the callback record, including across a Message Layer pause.
Paused needs admit the same Group’s queued Host Inputs under distinct refs;
hello.features advertises paused-inputs-v1. Admission preserves synchronous
errors and ids, while ordinary inputs still wait for the next Pump. Stop,
CancelRun and RewindRun land after the callback record and scope acknowledgement,
before result conversion. A suspending Call retains its identity and metering
eligibility while Start is paused, including a restored Call reissued at the
first Pump. Reissued Start preserves its settlement until acknowledged and lands
controls before a sibling Start. Answer/Fail and RequestCharge can enqueue
before started. Worker reentry remains refused throughout a transport pause.
Grant releases pause the continuation until release-result, before later
callbacks or Script execution; inputs queued by release use the original worker’s
Pump/code-change boundary. Deferred cleanup of another Run retains the original
Host return without repeating its callback or record. Native callbacks retain
their synchronous contract.
Hosts may opt in to crossing-trace-v1 to receive available Trace before
Operation needs and a trace need after each ordinary Operation’s call record,
before controls land or result conversion. Its trace-result acknowledgement
continues the same Pump without repeating the callback or accounting.
Trace observation preserves Join admission, including exactly MaxJoin members,
with the same Fuel and Trace as without the feature. Refusal
and Load-error replies carry outer Trace with this feature; legacy Hosts retain
final-reply timing. This is an Operation crossing observation contract, not
per-record suspension during arbitrary execution or non-yielding WASM admission.
The optional WASI inline-inputs-v1 transport adds those boundaries: every
complete record is observed synchronously, and input admission checkpoints keep
the active guest stack without export reentry. It also invokes release hooks
before their following records, with pending Calls already cancelled. See the
inline contract and the
Go adapter guide for acceptance and
limitations. The ordinary sidecar retains its existing framing and timing.
Hosts may opt in to group-exchanges-v1 to host many Groups in one Session:
the Exchange a Pump, Reload or library replacement pauses in is parked on its
Group’s entry while frames for another Group run, and drop-group frees an idle
Group. See the contract.
In both exchange modes, callback results and Trace/release acknowledgements
must name their exchange’s Group. A missing, unknown or mismatched Group is a
protocol error that leaves the need open for a corrected result, in both JSON
and binary fields layouts.
The contract
describes the supported boundary. Message Layer answer.fuel now reaches the
settlement-cost API. A queued settlement remains tracked until consumed, so a
Run discarded before resumption still notifies cancellation before later needs.
Typed CallFailed reports name the Capability definition, while Trace and
ScriptError fields continue to name the Grant.
zig build corpus -- --defer-crossings defers each eligible crossing once and
holds every Trace unchanged. The WASI reactor ships stripped of debug sections
and has a 1 MiB stack, since the
stack sits in linear memory: source recursion stops at the depth guards, and
Values are walked from heap work lists, never by native recursion.
For guest function profiling, build a separate diagnostic artifact with
zig build -Dtarget=wasm32-wasi -Dstrip=false -p /tmp/northtalk-profile.
This keeps safety checks and optimization unchanged and retains function names
for wazero listeners. From the repository root, run
NORTHTALK_PROFILE_GUEST=/tmp/northtalk-profile/bin/talk.wasm go -C impl/go test ./internal/zigwasm -run '^TestGuestRequestProfile$' -v.
The opt-in harness times a request and Pump in separate runs for parsing,
execution, Report writing and transport functions; nested timings overlap and
listener overhead must be checked against its uninstrumented run. Ordinary
acceptance skips this diagnostic. Shipped artifacts use the default strip setting.
For the Message Layer, supply WASI imports and call _initialize once before
serial export calls. Opting in to
chunked-wasi-v1 adds
talk_send_chunk/talk_reply_next and inline continuation headers: transfers
stay within 64 MiB, while logical messages may reach 2 GiB minus one byte.
Complete messages are still assembled in memory before decoding.
Legacy talk_buffer(n) -> i32 reserves the input frame’s bytes;
write JSON without a length prefix. Zero or more than 64 MiB returns 0, which
must not be written through. talk_send(n) -> i64 returns the reply as unsigned
ptr << 32 | len; copy it before the next talk_send. The input buffer lasts
until the next talk_buffer. A length beyond the reserved buffer is a protocol
error. Either export may grow memory, so refresh memory views
after calls. Host Operations and properties use interim needs and result
messages, without custom Host imports or reentrant exports.
Pump, Reload and Library replacement errors send queued Grant releases before
their final reply, including binary Pump errors and refused Reload sources.
The final reply clears the exchange’s release owner; a Trace pause retains it
until the Host answers trace-result and the worker finishes.
load-fields-v1 independently enables binary Load inputs with raw UTF-8
source and optional newGroup fields, as the
binary-exchange contract
describes. JSON Load and new-group remain supported. Both paths use the
same Load validation, worker exclusion and Trace; replies remain JSON.
queue-fields-v1, negotiated with request-pump-fields-v1, enables binary
deliver, broadcast and decide inputs with the binary admission reply, as
the binary-exchange contract
describes. JSON queued inputs remain supported and admit through the same path.
Message Layer frames have no JSON nesting-depth cap. Parsed Value Encodings,
including Function captures, go directly to the Core’s heap-stack Value builder
without recursive JSON re-encoding. Replies use the iterative Value writer.
Native tests and the WASI Message Layer check exercise arrays and maps nested
100,000 levels, including deep error data in replies. The frame-size and
available-memory limits still apply; malformed deep JSON is a protocol error
and leaves the Session and any pending Host exchange usable. Hosts must decode
deep replies within their own resources, as
chapter 9 specifies
(ADR 0088).
tools/wasi/zig.py runs it in wasmtime and holds its
memory per instance and payload to #532’s thresholds, in CI; the first
figures are 2 MiB per instance and
0.40 MiB of Brotli payload, against the Go reactor’s 15.5 and 2.04.
After building the WASI target, run its pinned Python dependencies through uv
from the repository root:
uv run tools/wasi/zig.py impl/zig/zig-out/bin/talk.wasmThe script declares its Python version and dependencies inline; uv run creates
the environment. This checks the C API reactor; Message Layer verification uses
the shared reactor harness:
(cd impl/zig && zig build -Dtarget=wasm32-wasi --prefix ../../.cache/zig-wasi)uv run tools/wasi/check.pyuv run tools/wasi/list_memory.py --profile minimumThe shared scripts default to .cache/zig-wasi/bin/talk.wasm; an explicit path
(check.py path/to/talk.wasm, or list_memory.py --wasm path/to/talk.wasm)
selects another build. CI runs both checks against Zig, with each List workload
in its own job. The C and
Elixir harnesses also build and default to
this module, so remeasurement needs no TALK_WASM override.
Native Message Layer sidecar
Section titled “Native Message Layer sidecar”zig build sidecar installs zig-out/bin/messagelayer (also included in a
native zig build). It shares src/message.zig with the WASI reactor and
needs neither Go nor a WASI runtime. macOS uses the system libSystem for
process I/O; the Linux artifacts are statically linked. The safe optimization policy applies
to every target. Linux builds use static *-linux-musl targets:
# From impl/zig:zig build sidecar -Dtarget=x86_64-linux-musl --prefix ../../.cache/zig-linux-amd64zig build sidecar -Dtarget=aarch64-linux-musl --prefix ../../.cache/zig-linux-arm64zig build sidecar -Dtarget=x86_64-macos --prefix ../../.cache/zig-macos-amd64zig build sidecar -Dtarget=aarch64-macos --prefix ../../.cache/zig-macos-arm64# From the root, using a binary executable on this machine:python3 tools/sidecar/check.py impl/zig/zig-out/bin/messagelayeruv run tools/wasi/list_memory.py --sidecar impl/zig/zig-out/bin/messagelayerCI cross-builds and executes the sidecar on Linux amd64/arm64 and macOS arm64.
Each request and reply is a four-byte big-endian length followed by UTF-8 JSON.
Every request receives one flushed reply, including interim needs. Stdout is
reserved for frames. Clean EOF between frames exits with status zero; a
truncated header or body exits unsuccessfully with a messagelayer: stderr
diagnostic. The maximum incoming frame is 64 MiB. Larger frames are drained
through a 64 KiB buffer, answered with a protocol error under ref: -1, and
leave the Session usable. Truncation while draining is a transport failure.
JSON and Value nesting have no fixed depth limit. Callback inputs use distinct
refs while the worker ref remains reserved for its result, just as in WASI.
The executable checks exercise framing, malformed/deep JSON, Value Encoding, immediate and suspended Operations, paused Host Inputs, properties, effects, scope cleanup, Grant releases, cancellation/Stop and Zig save/restore in a fresh process. The shared Message Layer tests include Reload/replacement and native callback parity. CI separates cross-builds from execution on macOS and Linux, amd64 and arm64, and runs native List-memory and Linux cgroup checks. The Elixir harness builds both Zig transports and uses the sidecar through an Erlang Port. Its Linux containment checks need Docker; an unavailable Docker check is unverified. Local platform and memory evidence is recorded in the cutover verification.
Go-sidecar saves require fresh Groups from source and Host-owned data; there is no cross-Core converter. Keep the previous Go executable to drain old Groups if necessary. Zig saves remain supported on the replacement. The Go public API and its facilities stay available; #884 retains the separate full-Core retirement and performance gates.
From the repository root, bun tools/zig/api.ts compares talk.h with
talk.go. It fails on a talk.h name that talk.go doesn’t declare, unless
the declaration is marked C only, and on a talk.go name talk.h doesn’t
declare.
The Session Host
Section titled “The Session Host”src/session/session.zig is chapter 12’s Session
Host. It is an ordinary Host: it drives a Group named session, holding one
Script, through talk.h alone, using shared analysis to
read Entries and declaration metadata. It
follows its Runs through their reports, placed by the Group’s Trace records,
and never inserts Inspect() to do so. A Driver gives it everything from
outside: Clock readings, typed lines, prompt answers, and where its output and
Trace go.
-
Entries are decided on their first token. A declaration with new names extends the Script, and a redefinition reloads it from the session source, carrying Script Variables over. A statement or an expression becomes
entry<n>, with the names it binds as Container roots or Captures as new Script Variables. The checker gives these as each body’sroots. -
Positions of an Entry’s diagnostics and errors are shown in its own lines: the extension’s lines before the Entry’s first, and
returnbefore an expression’s first line, are taken off. -
Output of a Pump is placed by its Trace: a Host function’s lines at its
callrecord, a Run’s end at itsrunrecord, andunhandledat its record. A Run other than the current Entry’s has[<run>]before its lines. -
The corpus runner replays
session.transcriptthrough it, and compares the Transcript it writes, its@,<and~lines included where it read them, and the Group’s Trace withcase.trace. -
The Session Store (
src/session/store.zig) keeps each Store in memory, every value by its Value Encoding, so Stores outlive any Group a:restorereplaces. Writes wait in their Segment until it commits, and a commit with changed keys counts a revision and goes to the router. After each Pump the router retries, and while it admitted a Notification the session pumps again at the same reading.:saveand:restoretake and restore router checkpoints. -
Observation (
:describe,:inspect,:apropos,:trace,:untrace,:fuel) follows Session observation. The session keeps every code unit’s source by its code identity, whichtalk_script_unitgives, sotalk_function_info_offinds a Function Value’s documentation even once a Reload has made it stale. Trace rows come from the Run lifecycle reports, and Fuel Measurements from Run accounting, each after a Host call’s ordinary output. -
Object crossings (
%envelopes) follow Session observation. A replay defines each recorded Object Kind and registers each object with a dummy native identity. Its getters and setters answer from the recordedproperty-begin/property-endpairs, making the Host actions recorded between them, and each replayed action’s reply must be the recorded one. A live embedder hands an action’s request alone (Session.act); the session performs it and records it with its reply.answerandfailreach the calls the session’s Group issued to it, waiting or not; any other call id isunknown call. Each Function Value leaf a report or a setter exposes binds the nextfunctionhandle, and a Restore’s resolver answers from theresolveenvelope. Crossing Values go directly from parsed JSON to the Core’s iterative Value decoder, which resolves$sessionFunctionhandles as owned Function references, including inside lists and maps nested 100,000 deep on a 256 KiB native stack. Unknown or malformed handles are refused, with partial Values released even after allocation failure; ordinary text stays text. This Transcript-only resolver is internal to Zig and does not changetalk.hor durable Value Encoding.:inspectof an object adds itsobjectandpropertyrows. It then reads each property in a second Run, the normative reader, extended under the first freeentry<n>.
The REPL and Transcript replay
Section titled “The REPL and Transcript replay”src/repl/main.zig is northtalk, the native command.
Given a Script file it runs it (The Script Runner);
otherwise it is the REPL, the Session Host at a terminal under a real Clock. It reads Entries and Session Commands from
standard input and prints what the session prints. An Entry may span lines:
the REPL prompts | and waits while its syntax ends early or a fenced literal
or hole is open
(chapter 12). It answers
the foreground’s read with the next line (< ), and a user prompt (~ )
with a value in the display form, or fail and an error map. A line that
isn’t in the display form answers as text. While it waits for input, it pumps at each deadline the session
reaches. :library add|replace <name> <path>, :fix <run> <path> and
:store load [<store>] <path> read the file, and the Transcript records its
lines in place of the path. End the session with end of input (Ctrl-D). Ctrl-C
isn’t caught yet, so it ends the REPL rather than cancelling the foreground
Run.
From a clean checkout, with Zig 0.17 (nothing else is needed to build):
cd impl/zigzig build repl # start a sessionzig build repl -- --transcript s.transcript --trace s.tracezig build && ./zig-out/bin/northtalk --help # the installed binary--transcript writes the session’s Transcript as it goes, and --trace the
Group’s Trace.
The Script Runner
Section titled “The Script Runner”northtalk app.talk [args…] runs a Script file
(chapter 12,
ADR 0126),
as the TS and Go northtalk commands do. A first argument that ends in .talk,
or names an existing file and isn’t a flag, is the file; --port <n>,
--listen and --peer may come before it.
cd impl/zigzig build && ./zig-out/bin/northtalk ../../examples/http/hello.talkzig build repl -- --port 0 ../../examples/http/hello.talksrc/runner/ loads the file as one Script, named for the file,
in one Group, granted under GrantsAsUsed console, clock, timer, an
in-memory store (the Session Store’s), http, peer and, when standard
input is a terminal, user. peer carries Remote Sends to other Runners
(Runner peers) over
mutual TLS with pinned keys, or over tcp:// on loopback. Zig’s standard
library has no TLS server, so src/runner/tls/ holds a
minimal TLS 1.3 of its own (ADR
0164):
one cipher suite, X25519 and ECDSA P-256, pinned keys, and no resumption. It has no time-zone or Locale data, so it offers no
calendar or locale. It Delivers start with the list of the arguments
after the file, and exits 0 once nothing can happen again, or 1 when a Run
ended with an uncaught error, a limit fault or a failed effect, which it
writes to standard error as file:line:col: …. A load error is written as
file:line:col: <code>: talk.h gives a Script’s diagnostics only through the
Trace, without their wording. The first SIGINT or SIGTERM stops accepting
connections, Delivers stop, waits up to 5 seconds for open HTTP Exchanges,
answers the rest 503 and exits 130 or 143; a second exits at once.
One thread does everything: it polls the HTTP sockets, standard input (while a
read or user prompt waits) and a signal pipe, and pumps at each Host Input,
timer and deadline. The HTTP router (router.zig,
request.zig) is the TS and Go routers’ over
talk.h: one listener per address shared by its Serve Registrations, port 0
for one the system picks, the longest prefix, the request map with its query
keys and header names in code-point order, a 1 MiB body limit answered 413,
and 404, 500 and 503 settled from the Pump’s reports. A suspending call the
Host can answer at once, such as serve, is answered once the Pump returns,
since a Host function’s own answer to one isn’t taken.
http.fetch (fetch.zig) allows any origin, as
chapter 12 says. Each call runs as a concurrent task on the Runner’s
std.Io, with std.http.Client: one HTTP/1.1 request on its own connection,
accept-encoding: identity unless the Script sets one, no redirects, no
decompression and no cookies, an 8 MiB body and 64 KiB header bound, and
HTTPS verified against the system’s certificate authorities and the host
name. The task wakes the loop through a pipe, and the loop answers the call;
a call the Core abandons cancels its task. A failure the Script sees as
network error is written to standard error with its cause.
zig build runner-test # northtalk on examples/http/hello.talk and fetch-smoke.talk, over socketszig build http-kit # corpus/http-kit/ through the native routerzig build test runs both.
Transcript replay is the corpus runner’s. It gives a case’s
session.transcript to a fresh Session Host, with the recorded Clock
readings, lines, answers and object crossings, and compares what it writes
with the Transcript and its Trace with case.trace:
cd impl/zigzig build corpus -- sessions # every Session Transcript casezig build corpus -- sessions/real-clock # oneTo replay a Transcript the REPL wrote, put it in a case directory under
corpus/ as session.transcript, with the Trace as case.trace, and a
case.toml holding kind = "transcript" and the [versions] table, as in
corpus/sessions/mocks.
The Notification router
Section titled “The Notification router”src/notify/router.zig is the router chapter 7
describes for store watches, declared in talk.h as talk_router_*. It is
Host code over talk.h, as talk.go’s notify package is: the Core takes no
part in it. A Host’s store implementation forwards watch, unwatch and
its Grant release hook to it, calls talk_router_commit with each commit’s
changed keys under the Store’s synchronization, and calls
talk_router_pumped after each Pump. talk_router_save and
talk_router_restore take and restore checkpoints, which hold the Group’s
save and the routing state tied to it by its digest. C has no way to read a
Store’s name out of an opaque binding, so the router’s options include
store_of, which does that.
The machine
Section titled “The machine”Metering support: Operation Declarations carry metering: "read-only" or
"segment-bound", validated against mode, scope and coordinator metadata.
It participates in the Host Manifest, Group Fingerprint and restore matching.
talk_call_charge permits repeated callback-time increments, retains the
accepted prefix and latches the first budget refusal even if the Host suppresses
it. Invalid Fuel amounts are invalid value and do not latch a Limit Fault.
A suspending read-only Operation may call talk_call_request_charge, including
while starting, and receive a Group-owned talk_charge_request. The by-id
talk_request_charge has the same admission rules for the Message Layer and
restored issued ids. Admission is safe from Host threads: one unresolved request
per call, Group-wide q<N> ids, and FIFO order with other queued inputs. Install
talk_group_on_ready before starting workers to receive enqueue notifications;
its hook runs outside the admission lock. talk_charge_request_done and
talk_charge_request_result use the processing Pump’s completed outcome. A
starting callback must return before awaiting it. Check talk_call_context
after acceptance and before work, since a later cancellation or timer in that
Pump may already have abandoned the call.
Accepted charges immediately contribute to Run accounting, Script counters,
Pump Fuel and slice debt, including Pumps with only waiting Runs. Refusal faults
at the originating call, including a Join Member, without a synthetic Segment.
A preempted Join’s open Segment rolls back; a suspended Run keeps its committed
variables. talk_save refuses with effects pending for pending metered calls
or unresolved requests, even after Reload ends their call. Successful saves
preserve the request counter and issued-call eligibility in the save format;
older Zig snapshot layouts are refused. Issued ids are kept as consecutive
per-Run ranges with the same eligibility. Repeated polling through one Operation
therefore keeps a fixed number of history entries in memory and in saves.
Eligibility changes and gaps for non-Operation calls require separate ranges;
historical eligibility remains available for the Group’s lifetime.
The shared corpus carries the metering Trace and setup extensions, which the
Zig replay consumers understand, including accepted-prefix-plus-refusal replay.
SQLite fixtures set perStep, and the replay Host pays a step per Stub row up
to max, plus the completion check or the probe past it, through the metered
step factory below; a fixture’s perRow is refused.
talk_new, talk_new_group, talk_load, talk_script_deliver,
talk_script_request, talk_pending_result, talk_pump, talk_inspect and
talk_script_counters drive a Group. A Group’s Trace reaches the Host’s talk_trace_sink one line at
a time. talk_pump, talk_script_reload and talk_replace_library
also return the Host call’s reports (chapter 9),
in the order they happened, as a talk_reports the caller releases before
its Group: each run end with its result, error or limit, stop,
unhandled, call failed, effect failure, decided, and a queued input
the Pump refused. A Pump’s result also gives its next deadline. The Group
collects reports whether or not it writes a Trace, and charges nothing for
them. Direct C Pending handles currently report done only for successful returns;
failed Runs are observable through their typed Reports
(#1195). Each Host call that returns reports ends with Run accounting’s tail:
run started and run discarded join the other reports as they happen, then
come a run accounting row for each Run started, charged, ended or discarded,
in the order the Runs were made, and causal work for each root whose work
changed, in admission order. Every message carries its root Delivery, and the
Run and call that sent it, so a save keeps ancestry, the roots’ discard counts
and the roots not yet reported. talk_restore_result_reports gives a
restore’s baseline. The shared fixtures
run against the exports. Loading a Script parses, checks and lowers it, runs its initialiser
uncharged and writes its load record with its code identity, or its diag
records and TALK_LOAD_REFUSED. C Hosts set limits through Load and message
options, and Fuel Slices through Pump options.
C Requests and Host Function Value calls settle their talk_pending when the
Delivery finishes, including errors, Limit Faults, cancellation, dropped or
unhandled messages, stale Function calls and Stop/Reload/library-replacement
discards. talk_pending_done distinguishes an open Request from a settled one;
talk_pending_result gives an owned successful result, or Nothing while open
or after an unsuccessful outcome. Run failure details remain in the typed Reports.
Climbing the Message Path and Rewind keep the Request open, as does Reload for
messages it keeps in the mailbox. Settlement survives releasing the Reports and later Pumps.
talk_script_request_pump, talk_request_pump (Object target), and
talk_call_pump admit one Request or Function call and perform exactly one Pump
under the worker guard, through the final Trace callback. Earlier inputs retain
their order, and the Pending may remain open. An admission refusal clears all
outputs and does not Pump; a Pump refusal retains the owned delivery id and
Pending, with an empty Pump result, leaving the input queued. Release them with
talk_owned_bytes_release, talk_pending_release and talk_reports_release
as usual. Initial OnReady is omitted; Pump refusal notifies readiness after
worker release. Function staleness is checked at drain and arity at execution.
talk_inspect is the Host Input vars: it writes each Script’s vars
record and, when asked, returns the talk_inspection a REPL renders :vars,
:runs and :mailbox from. That is each Script’s disabled Grants, its Script
Variables, its Runs that haven’t ended, in the order they started, and its
mailbox. A Run is preempted mid-Segment, ready with what it resumes with,
parked by its clause’s Queueing Policy, or suspended. A suspended Run shows
the end reason and the deadline its seg record wrote, and the calls,
replies or Join Members it waits for.
Each Group allocates everything it holds through its own allocator
(Group allocator), which wraps the
talk_allocator the Host gives in talk_group_options, or the Core’s, and
counts physical bytes against memory_cap (256 MiB by default). Freed Value
nodes are kept for reuse in a cache of at most 128 nodes, counted against the
cap like everything else, flushed under cap pressure and returned when the
Group is released. Released frame locals and operand buffers have a separate
cache of at most 32 buffers, each at most 1 KiB (at most 32 KiB retained).
Only empty buffers with room for an intrusive header are kept; Values and
internal stack objects are released first. Reuse requires the same byte size
and alignment, including for frames rebuilt by Restore, and every local is
initialised again. Unbound locals share one immutable Nothing per call, with
one owned reference per slot. Both caches count in physical memory, are
flushed before a cap refusal, and return all storage on Group release.
Cost Model 0
still charges logical allocation; the cap is for defects. A refused
allocation becomes TALK_OUT_OF_MEMORY: the Run in flight is dropped whole,
messages and waits that couldn’t be queued are released, and the Group takes
no more work, while talk_group_release still gives back every byte.
talk_group_memory reports the bytes held, the peak, the cap and whether a
refusal happened. A test refuses at every allocation of a run of Scripts with
sends, Joins, wait for, errors and finally, and checks that nothing is
left.
The Core’s own allocator is smp_allocator natively. On wasm32 it is
a heap for large blocks over wasm_allocator,
which keeps the latter’s size classes for blocks up to half a 64 KiB page
(#1447). A larger block takes exactly the pages it needs. Freed runs merge
with free neighbours and serve any later block that fits, and a block grows
in place into a free run after it or at the top of memory. wasm_allocator
alone rounds large blocks up to a power of two of pages and reuses a freed
run only at the same size. The lowering also lays out each body’s code as
soon as the bodies before it are laid out, so it holds one body’s working
code at a time. Together these took a Load of 100,000 empty Handlers from
104 times its source in linear memory to 51.
A Pump writes and drains the queued Deliveries and Requests, then runs each
Script’s mailbox: each Run tries its Handler’s clauses in order, charging the
clause rate at each clause’s first instruction. A Command Call to a Handler
of the Script, with or without and wait, tries its clauses the same way in a
new frame, and raises no match at the call when none takes it. A message no
named clause takes goes to the Fallback’s clauses with its {name, args} map,
built without charge, unless it is an error message; its Run’s records add
fallback=yes. A clause’s Queueing Policy applies once its patterns and Guard
have passed, while a Run of the clause hasn’t ended. , queued parks the new Run after its clause charge, and the
first parked Run becomes ready when the clause’s Run ends, behind any error
message. , dropping ends the new Run dropped. , replacing cancels the
earlier Runs: what each waits on is abandoned at once, and in its own turn it
runs its finally blocks, innermost first, with nothing unwound, and ends
cancelled. Cancelling a parked queued Run also runs its dispatch-local
finally blocks, without rolling back committed Script Variables or releasing
the active Run’s clause slot. A waiting sender gets send failed with that reason.
A Decision (talk_decide, talk_script_decide) is a Delivery that carries
an open Verdict. A wait for that takes it allows it, and so does a selected
clause without , deciding; a selected , deciding clause holds it. The
Verdict is sealed at the end of that Run’s first Segment, with a decided
record after its seg: veto vetoes it, and suspending or ending without one
allows it. pass carries it up the Message Path, and the end of the path
allows it. A Run that errors, faults or ends effect failed first, or a
, dropping clause that drops it, leaves it undecided, after the run
record. A Delivery, Request or Decision whose name has a : but isn’t a
Selector with one argument per part, or whose override no Delivery can hold,
such as a negative one, is refused at the call, with its > line, no id, and
refused code="invalid value". cancel-delivery comes
with cancellation.
A Broadcast (talk_broadcast) or Broadcast Decision (talk_decide_broadcast)
finds its recipients when the queue is drained: the Scripts with a Handler
for the message or a wait for pending on it, in load order, each with its
own delivery id, which the > line lists. A wait whose Run a cancel-run or
cancel-delivery queued in the same drain will cancel doesn’t count. A limit
override may not loosen the default limits; each recipient keeps the tighter of
its own limits and the override. A refused Broadcast’s > line keeps the
override’s fields as given, and consumes no id. A recipient’s Delivery carries the
broadcast id, never climbs, never reaches a Fallback and is never
unhandled; neither does a Decision’s. A Broadcast Decision gathers each
recipient’s part, its veto, its undecided outcome or its allowing, and writes
one decided record once the last has one: vetoed if any vetoed, undecided
if none did but one was undecided, and allowed otherwise, at once when there
are no recipients. Frames are on the heap, so
the run loop never recurses on Script depth. Every instruction is charged by
its machine.toml cost key, or builtin.<name>, from the generated Cost Model
0 terms, with scanned reported by the operation itself, and Persistent State
is measured over the Script Variables and the mailbox at a Segment’s end.
Opcode rates are resolved at compile time. The Cost Model generator also emits shared evaluators for static Fuel/allocation formula pairs, selecting measures and folding constants ahead of execution while retaining each term’s own rounding and checked arithmetic. Dynamic Built-ins, keyed charges and formulas with unsupported measures or subjects use the term interpreter, which remains the differential-test oracle. Extra Host Fuel and allocation are still added at each charge; the machine owns refusal ordering and instruction boundaries.
fastSteps runs const, load, store, move, jump, the two branches,
Number comparisons, Number/Quantity arithmetic (including Number div and
mod), and static Unit conversions of Numbers/Quantities without the general
loop’s per-instruction checks, while those checks would find nothing:
no Host poll due, no crossing, landing, selection or preemption pending, and
no finally scope in the unit. Each instruction is charged and ticked
separately, and the fast path leaves any instruction that would raise, fault
or be refused to the general loop untouched. A comparison followed by a branch
skips building the Boolean, and an arithmetic result stored into a local
reuses that local’s Number or Quantity node when nothing else holds it. Unit
conversions and rounding follow the same arithmetic implementation as the
general loop; a static Unit conversion can also reuse its uniquely held
numeric operand. Identity Unit conversions skip the factor phases for nonzero
numbers, while zero and positive exponents retain Decimal clamping. Physical
allocations therefore differ from the general loop’s; Fuel, logical
allocation and the Trace do not. fast_steps_test.zig runs Scripts with the
path on and off across every Fuel and allocation limit and Fuel Slice.
talk_script_counters reads a Script’s lifetime Fuel, allocation, Runs and
Limit Faults, live Runs included, with its current Persistent State and
mailbox, and writes the counters record without draining Host Inputs.
Instructions within a finally scope retain their popped operands until the
instruction ends, so Error-mode cleanup can keep the failed continuation’s
stack. The eight-slot inline buffer is reused within each execute call;
release clears its live-slot metadata after releasing those owners and any
overflow storage. Each retained slot is written before its length is published.
This storage remains local to execution; it adds no Run or snapshot state.
Lists keep immutable visible slices over shared backing storage. Appending or
prepending to the newest slice uses spare capacity, with geometric growth when
it runs out, so repeated spine growth takes amortised linear time when the
inserted values have lower ownership ranks. Ranks decrease along physical
ownership edges, including hidden items, so cycle prevention needs no graph
walk. Growing an older slice or writing inside a shared slice copies its
spine; inserting a value with an equal or higher rank also copies into new,
higher-ranked storage. Growing a Host List copies its spine into the Group
allocator. collecting and List literal spreads use the same growth path,
without a temporary copy of the preceding List. Scalar collecting therefore
has amortised linear spine work; nested values with equal or higher ownership
ranks can still force copies.
In-range single-item writes use the copy-on-write point setter directly, without first building a temporary spine. A unique same-allocator List can reuse its storage for a lower-ranked child. Chunk writes retain the borrowed input, including lowering temporaries and Segment rollback bindings, so they still copy the visible spine once per replacement; repeated writes on a long List remain linear in its length per write. Padded writes and range replacement rebuild the result. Host-owned input spines copy into the Group allocator. Snapshots, captures and rollback bindings keep their previous contents. Spare capacity and retained items outside an older slice count against the Group’s physical cap; Cost Model 0 still charges only the specified logical allocation. The List measurements record size scaling, allocations and the remaining point-write cost. Lowering temp release makes a slot available to the compiler; it does not clear the runtime local. Such locals still contribute to retained frame size and Save/Restore. The local-write ownership investigation explains why last-use loads alone cannot enable unique machine point writes.
List point replacements initialize the active List payload directly. Local
load, store and move instructions avoid the general instruction handler;
stack pushes reuse available capacity directly, and shared-reference releases
inline their decrement. Each instruction still reaches the ordinary charging,
preemption and retained-state boundaries. The
item-write measurements
compare replacement with append and record accounting and ownership checks.
Forward Character reads walk only through the requested end and keep byte offsets instead of segmentation arrays. Missing and reversed reads still charge every Character as scanned input. Negative indexes retain full segmentation; there is no retained Text index or cache.
So far the machine runs straight-line code, branches, loops, functions, chunks
and properties, keys, lists and maps, match with its destructuring tests,
Bytes builds and Binary Patterns, as conversions, number, Quantity and date
arithmetic, ^, and the Built-ins. floor,
ceiling, truncate and round round the coefficient exactly by the General
Decimal Arithmetic’s modes; upper and lower use the full case mappings, with
Final_Sigma, then NFC; the date fields, ISO weeks, toCivil and toInstant
work on proleptic Gregorian days, toCivil and toInstant also under Zone
Rules (ADR 0179); and the float Built-ins read and write IEEE
754 bytes through the shortest round-trip decimal. sqrt and power are the
decimal power, exact where the value is. exp, ln, log10 and the
trigonometric functions (transcendental)
bound the exact value in a fixed-point interval, with pi by Machin’s formula
and each series’ tail bounded, and round both ends; by Ziv’s method, a
disagreement retries with twice the guard bits. A Built-in that raises still
pays its rate. A
raised error unwinds in two phases: the catch search walks the Unwind Table
outward, charging unwind per frame it leaves and running each catch test in
a dispatch activation over the owner’s locals, then the exited finally
blocks run, innermost first. A Run keeps its error control as a stack of
dispatches and cleanups, so a catch’s tests and a Recovery policy run in the
ordinary run loop and may call functions; the failed frames above the owner
are held aside meanwhile. An error inside a policy, or inside a finally
block a cleanup is running, first searches only inside it, then escapes as
chapter 6 says: an escaping policy runs its own and the failure’s finally
blocks and goes on outward from the catching try, past its other catches,
from where the policy failed. choose-offer looks the name up in the failure
chain, charging 4 * frames for the frames it scans, and transfers after the
policy’s finally blocks and then the failure’s down to the offering try,
with offer-chosen and offer-entered records. offerAvailable searches the
innermost dispatch’s chain, or a pending transfer’s until its action is
entered. An uncaught error ends the Run and queues an
error message to the Script’s own mailbox, for its on error Handler. Core
messages are left out of the Trace, since their wording is outside parity.
An error Run’s , during message map stays available in locally called error
clauses, including later clauses after a pattern or Guard fails. Helper calls,
captured Lambdas and Fuel slicing retain it. A Host-delivered error message
has no such metadata and binds Nothing. Message failures bind {name, args};
Function Value call failures bind {fn, args}, retaining the original Function
Value and its captures. Save/Restore preserves this metadata in failed Runs,
queued errors and locally called error Handlers.
Text Patterns run on chapter 8’s Pike VM (matcher).
A pattern value keeps its canonical source, so the matcher reads it back,
compiles it and runs the program, counting steps by the Running rules: the
threads in the list at each position. make-pattern splices values into a
template and writes the result’s canonical source. A text needle, or a Bytes
needle in Bytes, runs as a literal program. A pattern’s logical size compiles
it to count its program. Patterns made at run time can nest without bound, and
a program can be a long chain of splits, so reading, splicing, writing,
compiling and matching keep their pending work on explicit stacks and never
recurse on a pattern or its subject.
Object element references: element name of receiver and
the number of elements of receiver run here as get-element and
count-elements (the contract).
An Object Kind declares its Object Collection natively (objects.Elements),
through C’s talk_object_elements, which registration checks and copies, or
through the Message Layer’s define-object-kind elements after the
object-elements-v1 hello feature; each callback is a prop need with an
op and a selector. Corpus setup, both replay modes and Session
Transcript replay answer collection calls from their records.
A Host defines Capabilities and Grants through talk.h
(capabilities), and binds Grants at Load, by
the name the Script uses. ask and tell call immediate and fire-and-forget
Operations: each argument is checked against its Shape first, with nothing
charged for a mismatch, then the declared cost and any Charge are charged,
the Host function runs, and its result is checked against the result Shape.
Function Values that break scalar, container or closed-map Shapes raise
wrong kind with got: "function". Data scans under any and open maps’
undeclared fields raise not encodable. A mismatching Quantity reports
got: "quantity" unless the failing Shape constrains its Unit or Unit Kind,
in which case it reports the Unit; an Optional preserves that diagnostic.
A Fail raises its code with its Data as fields, unless it misuses a
catalogue code, a reserved key or an undeclared code, which the Script sees
as host error, with a call-failed record. The loader checks each literal
argument’s kind against its Shape. The corpus runner answers calls from
stub lines.
An Operation may open or close a Capability Scope
(the lifecycle contract). A Run keeps
its occupied slots, by named Grant and scope, in opening order. After the
argument checks, a call that would open an occupied slot, close an empty one
or open one inside a Join is refused, uncharged, and so is a suspending
Operation, wait, wait for, a waiting send, a Join or another Script’s
Function Value while a scope is open. A Host return acknowledges the opening
or closing with a scope record before its result is checked. When the Run
ends, the Core calls each slot’s abandonment Operation, newest first, with
automatic set and nothing charged; a failure disables the Grant, so later
calls raise capability disabled.
talk_script_revoke queues a revoke input. When it is drained, the first
revocation of a name the Script keeps marks that Grant revoked, and later
calls through it raise capability revoked, uncharged, after the
capability disabled check. A call in flight is left to the Host, and its
answer still resumes the Run. A repeat, or a name the Script doesn’t keep, is
recorded and does nothing. A Capability’s release hook, a
talk_with_release_hook option given with its definition or store’s
release, is called with the reason revoke then, and with disable right
after the effect-failure record of a failed abandonment. Stop releases with stop, and Reload and Library
replacement with reload, only once the new code is accepted. An accepted
Script Reload releases its kept Grants even when the Script was already stopped;
resuming after a release crossing does not repeat the callbacks.
Segment-bound Operations come from talk_define_segment_capability, which
makes each Grant its own Segment Coordinator, or
talk_define_coordinated_capability, which maps each Grant’s binding to one
when the Grant is made; talk_define_capability refuses them. A Run’s first
Segment-bound call in a Segment enrolls its Grant and calls the coordinator’s
begin, after the call’s checks and charges. Later calls through Grants
mapped to the same coordinator enroll them with no hook, and any other is
segment participant conflict, uncharged. When the Segment ends, after
abandonment, the participant commits, unless the Segment faulted or was
cancelled, or abandonment failed on a participating Grant, when it rolls back.
A failed commit rolls back too, restores the Script Variables to the
Segment’s base, which a Script with Segment-bound Grants keeps at each
Segment’s start, and ends the Run effect failed. An unknown begin or
commit, after a rollback attempt, or a rollback that doesn’t succeed, stops the
Group: the running Run’s Stretch ends stop, keeping the call’s charge, and
every Script is stopped with reason effect state unknown.
clock, calendar, locale and console come from factories with chapter
7’s fixed declarations (standard). clock.now
answers from the Pump’s Clock. The Core makes the chapter’s own checks:
toInstant’s date-time and disambiguation and a Locale’s option words and
BCP 47 tag before the call, charged nothing, and each result’s rules after it,
whose failure is host error. A Host function receives the resolved
arguments, with a Locale’s options map complete.
store (talk_store_capability) declares its eight Operations, with set,
delete, increment and swap Segment-bound, and maps every binding to the
one Segment Coordinator its implementation gives. The Core refuses an empty
key with invalid key, and a watch or unwatch with an empty name or a
message that isn’t a plain Name with invalid watch, before the call is
charged. The implementation hears get’s and increment‘s omitted
arguments as Nothing, keys’ as empty text, and watch’s effective message.
A Host failure must name a declared code with the catalogue’s fields:
store full’s and watch limit’s limit must be one of the chapter’s
words, and the codes the Core raises itself are host error from the Host.
Notifications, and so the router, aren’t implemented yet.
sqlite (talk_sqlite_capability) is optional
(values at the boundary). Each Grant’s binding is a
talk_sqlite_binding, and maps to its database’s coordinator, which the
implementation gives; change, begin, commit and rollback are
Segment-bound, and begin opens the scope transaction that rollback
abandons. Before the call, uncharged, the Core checks max (out of range),
then that params is a list or a map, then each value (wrong kind at
argument 2, with its path). max defaults to the binding’s cap. The factory
converts params to SQL values and supplies before_step and its opaque
step_context to query and change. The driver calls it once immediately
before every statement step and propagates refusal without stepping, including
completion, SQL errors and the overflow probe. per_step is the factory’s rate;
N returned rows normally cost N+1 steps, an empty result or a non-returning
change costs one. max=0 still permits a completion step. Queries are metered
read-only; changes are metered Segment-bound and roll back on refusal. The Core
converts the rows back, raising sql for a column named twice and
unrepresentable for a double it can’t hold or text that isn’t UTF-8. A
malformed result is host error. talk_store_coordinator gives the Core’s
copy of a store implementation’s coordinator, the same one for the same
hooks and context, which the implementation’s coordinator may give for the
database that keeps the Stores, so both share a Segment’s participant.
timer (talk_timer_capability) forwards schedule’s name, Instant, message
and arguments and cancel’s name to the Host, both fire-and-forget. The Host
stores timers and delivers a due one as an ordinary Delivery; the Core keeps
no timer of its own. user (talk_user_capability) is optional and has no
binding. The Core refuses an empty choose with out of domain before the
call, and the implementation hears each omitted option resolved: an empty
prompt, default or title, and multiple false. confirm, choose and
enter suspend for up to 2,147,483,647 ms, and the Core checks the answer:
a boolean, text or Nothing, and for choose Nothing, one of the items, or
with multiple: true a subsequence of them. Any other answer ends the call as
host error. A suspending call whose Operation checks its answer keeps its
arguments until it is answered or the Group is released.
http (talk_http_capability) is optional, and the Core never reads its
binding. Before charging the call, the Core raises invalid serve for an empty
name, a prefix without a leading / or an effective message that isn’t a
plain Name, and invalid response for a status that isn’t a whole number
from 100 to 599, a header value that isn’t text or a list of texts, or body
with json, and for a json the json Library can’t write, both with
field json. serve hears its effective message, httpRequest when
omitted or Nothing, and an empty options map when omitted; it suspends, and an
answer that isn’t a map of text host and whole-number port is host error. respond hears json as its compact JSON text.
Wait deadlines retain their mathematical microsecond counts, including those
outside the Clock’s years 0001–9999. A valid wait suspends and pays its charge;
Inspect and the Trace omit an unrepresentable until. Unreachable future
deadlines have no next deadline, while one before the Clock range requests
another Pump at the current reading. Timer ordering, earlier Timeout Blocks,
cancellation and save/restore retain the full count
(#1064). The save format
writes them as signed LEB128 integers; earlier layouts are refused.
Runs suspend at ask … and wait and wait d. A suspended Run lives on the
heap, its Script goes on to its next work, and each Script’s work is one
queue of messages and ready Runs, in the order each became ready. A
suspending call’s talk_call outlives the call, and talk_call_answer,
talk_call_answer_with_cost and talk_call_fail queue Host Inputs that the
next Pump drains; an answer for a call that no longer waits becomes a
late-answer note. talk_call_context turns true, readable from any thread,
once a call’s wait ends without its answer, so the Host may give up its work. Each Pump then fires due deadlines, maxPending or the
default MaxWait for a call, in deadline order, and a timed-out call raises
timeout and is abandoned. A resumed Run is charged its answer’s terms over
the result and any cost the answer brought. Persistent State counts each
suspended or ready Run’s frames, its pending call and what it will resume
with.
Scripts send to each other: send, send … and wait and the computed-name
and spread forms put the message at the back of the receiver’s work, with a
send record, and a waiting sender resumes with the reply or send failed.
The complete instruction’s Fuel and allocation must fit before it delivers,
issues a call id or registers a reply wait. A budget fault leaves the receiver
untouched and spends none of the failed instruction’s charge.
A Script named as a receiver is its name, and me with no owner is the
Script itself. A Join starts its members where they stand and suspends once at
its end: it resumes with every answer in start order, or with the first
failure, index added, and an error leaving the Join abandons its pending
members. A catch inside the body preserves the Join, including while inner
catch tests reject the error and exited finally blocks run.
A Timeout Block’s deadline sits on the frame’s stack, bounds every
Suspension Point inside it, and raises timeout with deadline: true, at once
when it has already passed.
A wait for holds the values its branches popped and suspends. Before a
Script dispatches a message, every pending wait for in it runs its test
bodies on the waiting Run, which pays for them, in the order the waits began.
A match binds the names its test gives and leaves the message in it, and
the message still goes to the Handler Clauses. A timeout or an after branch
resumes the Run with Nothing, and a Timeout Block bounds them all.
Host Objects (objects) are the Group’s handles,
each of an Object Kind defined once on the Core with talk_define_object_kind,
and their object value’s identity is the handle. The Core holds each object’s
parent, set by a queued talk_set_parent that is checked at the call and
again when drained, each refusal a refused record, and talk_dispose marks
one disposed. A Script may own an object, which is me, and binds well-known
objects by name, which the checker uses for can't write. the k of o reads
the Kind’s Get, with a prop record, charged get-key, the property’s
getCost and the value’s conversion; id is the Core’s, and a Guard reads
nothing else. A well-formed Get or Set failure raises its code, as an
Operation’s does, with capability the Kind and operation the property,
and its Data’s conversion is charged. Any other failure is host error, with
a call failed Report identifying the Kind/property and an empty Call id.
Properties emit no call-failed Trace record. A failed setter’s prop record
retains both its assigned value and error; Stop/cancel landing still happens
before failure classification and suppresses interrupted Host-failure Reports.
set checks the Shape, uncharged when it fails, then runs the
Set. A Delivery or send to an object goes to its nearest Owning Script,
whose path is walked again when the message reaches the head of its mailbox,
so a message follows its object if it moves. A Run that ends unhandled or
reaches pass sends the message on from the parent of the object whose
Owning Script took it, and a Command Call with no Handler climbs from the
parent of me. A message with no Owning Script left is reported unhandled,
with its unhandled report even for a Script’s send. A climb into a full
mailbox ends there, with a climb-full note on the Run that passed it and
unhandled. A Host override never loosens the limits of the Script that
receives the message after a move or climb. Property Get and Set pay their
declared cost before the Host is called, faulting first if the Run can’t.
A Script’s send to a stopped Script is dropped once the sender’s Segment
ends, with another stopped record and stop report, and a waiting sender
gets send failed, reason stopped. talk_load refuses an owner from
another Group or one disposed or already owning a Script, then a bound Object
from another Group, with the load and refused records. Over the Message
Layer, climb-v1 marks the run end of a Run whose message climbed with
climbed: true.
A Host call of a Function Value (talk_group_call, the Message Layer’s
call) is admitted as a Request to its Home Script: an override that loosens
that Script’s limits, or one no Delivery can hold, and a full Home mailbox are
refused with the call-value and refused records. A Script’s … and wait
call to another Home raises mailbox full when that mailbox is full and
function gone when the Home is stopped. A Function Value call’s run end
names the value’s display form as its Handler, and a limit fault in it the
function’s body. With function-gone-v1, a Pump reply’s gone lists the Host
calls found stale at the drain, which end with no report.
A use naming an unknown Library binds nothing, so each use of its names is
also unknown name. talk_compile_library with NULL declarations declares no
Grants, so a Library’s direct Capability call, say included, is unknown operation. A Join in a Library is not in a library. A Library replacement
whose importers hold Grants with release hooks waits for each Script’s
releases, then reloads it against the new Library. Over the Message Layer, a
refused reload or extend publishes its records to a crossing-trace-v1
Host, as a refused Load does.
Libraries are compiled once per Core with talk_compile_library, against the
Libraries they import and the Grant declarations their Capability calls are
checked and lowered against: each Operation’s mode and argument Shapes, as
talk_grant_decls, which talk_capability_def_checks gives for a definition
the Host holds. Their Constants are evaluated at compile time, since an
initialiser may use only literals, Constants and Built-ins. A code
identity covers each imported Library’s identity, in the order the use lines
first name them. talk_add_library writes the add-library record and refuses
a stdlib name, a name the Group holds, or an import the Group doesn’t hold.
The seven stdlib Libraries, generated from spec/stdlib/ into
src/generated/stdlib_sources.zig, are compiled the first time a unit imports
one, and every Group holds them. A Library’s code runs in the caller’s Run:
each frame names the Library whose code it runs, so its constants, Unwind
Table and positions are that unit’s. call-import calls a Library function
with its defaults, and a Command Call finds an imported Handler. An error raised
in stdlib code looks like a Built-in’s: its at names the call into the stdlib
and the Core adds its message. Loading a Script checks the needs of every
Library it imports against its Grants, at the use line, and grantsAsUsed
keeps the Operations those needs call.
Function Values nested in Library Constants and parameter defaults bind to each importing or calling Script’s Home, including through transitive imports. The compiled Library holds unbound code templates, reusable across Groups; binding preserves Library code positions and captures without changing the shared Constant. Each Home Script caches a Library’s bound definitions on first access, so later Constant reads and defaulted calls retain the cached values without walking their contents. Reload and Library replacement discard the old generation’s cache; extensions keep their own initialised definitions. Bound values survive full Group Save/Restore, and the derived caches rebuild on first access after Restore without changing the Save format.
The trusted stdlib loader alone lowers private map:restrictedCall to
call-restricted. Its inherited frame restriction rejects live Script state,
Host effects and recovery-context access before acting, and survives catches,
cleanup, preemption and restore. Host Library compilation cannot enable it.
Snapshot format 6 rejects earlier formats. The map.transform Library export uses this boundary for callbacks; no
public pure-call API is available. See the normative contract
and the cross-Core kit.
map.transform implements ordered, presence-aware Mappings from the shared
normative Library source, including wildcard/default/skip selection, complete
Shape checks, definition validation before callbacks and complete output or
ordered diagnostics. Limit Faults escape. The contract
fixes definition precedence and concrete source locations; the stdlib/mappings*
Corpus cases cover behavior, sliced restore and resource exhaustion.
Mapping introduces no depth cap; its depth case verifies 96-step paths under a
12-frame call-depth limit.
A Lambda makes a Function Value with its captures, by name in slot order, and
a function’s bare name makes one with none. Each Group keeps one record of
the code a Function Value runs, its Home Script, unit and body, which the
value’s identity names. It displays as <function home:where {captures}>,
where where puts a Library’s name before a function’s name or a Lambda’s
position. Its size is 32 and its captures, since items is 0 for it.
call-value runs one whose Home Script is this one, and that can’t suspend,
in the caller’s Run with its captures and defaults, after the wrong kind,
would suspend and wrong arity checks. call-value-wait may run one that
suspends, and sends another Script’s to its Home Script, whose Run has one
frame and no Handler Clause and writes fn= in place of a Handler.
functionArity and functionName read what the record kept when the value
was made. The record also holds its Home Script’s code generation, which a
Reload replaces, so a value made before it is stale: calling it raises
function gone, while functionArity and functionName still answer and its
display is unchanged. An extension’s Lambda displays with the extension’s unit
name, <function s+1:3:9>, and its named functions, like the Script’s own,
with the Script’s name.
talk_script_extend adds an Entry as a new code unit of the Script, an
extension named <script>+<n>, counted from its last Load or Reload. Its
checker sees the Script’s Script Variables first, then its Constants,
functions, Handlers and imports as imports from the unit that declares them,
@<unit>, or from their Library, and only the Script’s unrevoked Grants with
the Operations it keeps. An Entry that declares a name the Script has is
refused name reused before it’s checked. The extension’s definitions start
with the Script’s Constants, and its variables table with the Script’s
variables, which grow by its own; its initialiser runs, then the Script’s
Persistent State must fit its cap, or state too large puts everything back.
An extension’s diagnostics and errors are placed in its own source, under its
unit name. Dispatch, wait for tables and Command Calls look across all the
Script’s units, since each Handler is in one of them. The extended Script’s
identity hashes the previous identity, extend and the extension’s own.
Inherited Constant links split @<unit>:<name> at the final colon, preserving
Host-chosen Script names containing colons, dots or slashes across successive
extensions. The same behavior reaches Elixir through the WASI reactor.
talk_script_reload builds the new code in a detached Script against the
Script’s kept, unrevoked Grants, objects and owner, and runs its initialiser
between Pumps, with no Fuel Slice or Fuel cap. With TALK_CARRY_VARIABLES,
each Script Variable both codes declare takes its old value, a preempted Run’s
Segment base standing in for what its Segment wrote. A LoadError or a carry
over the cap changes nothing. Then the Script is stopped with reason
reload, keeping its mailbox if asked, its revoked Grants are removed, and
the new code replaces the old and every extension. Reload isn’t sticky, and of a stopped Script it stops nothing. A stop whose
rollback leaves an effect’s state unknown stops the Group, with reason
effect state unknown, and prevents the replacement.
talk_replace_library recompiles each held Library that imports the
replaced one, directly or through another, with the Grant declarations it was
compiled with, then rebuilds every Script that imports any of them from its
source and extensions, in load order; any refusal changes nothing, and the
Core owns the recompiled Libraries. A use line naming something its Library
doesn’t export binds nothing, so a later use is unknown name.
talk_save saves the Group between Pumps in the Zig Core’s own format
(snapshot), which no other Core reads: a header
with the format version, the language and Cost Model versions and the Group
Fingerprint; the Group’s state as plain data; and the SHA-256 of all of it.
It holds every Library by identity, every Host Object by kind and id, every
Function Value’s code by its Home Script, unit and body, each Script’s
sources, Grants with their declarations, Script Variables, Constants and
counters, every Run with its frames, error control, wait and Join, the
Scripts’ work, the waits in the order they were set, the pending calls with
their arguments, the queued Host Inputs and the open Broadcast Decisions.
Code is never saved, and pointers become indices. An attempt takes the next
save id, s<n>; a live Capability Scope, a Segment participant or a stopped
Group refuses it with effects pending and no snapshot.
talk_restore builds a new Group in chapter 10’s order. Bytes it can’t read,
or whose digest is wrong, are invalid save. It matches the Libraries the
Host passes by name and identity, asks the Host’s grants function to
re-bind each Grant, whose kept Operations’ declarations must match the
saved ones, and compiles each Script’s sources again as they were first
compiled, against every Grant, without running an initialiser. A Grant the
Host doesn’t re-bind restores revoked, with a placeholder Capability built
from its saved declarations. If the Group Fingerprint of what it rebuilt
differs from the saved one, it is save mismatch, unless the Host’s policy
is TALK_VARIABLES_ONLY. A variables-only restore rebuilds each Script from
its saved source and extensions, like a Reload, against the Library given
under each saved name and its Grants, revoked ones included, so a Grant the
Host didn’t re-bind still binds its name. It reads the Runs only to discard
them, trusting none of their code positions: each Script Variable the new
code declares takes its saved value, as a preempted Run’s rollback would
leave it, and each discarded Run’s Fuel stays in its counters. The
mailboxes, the input queue, whose lines it writes before its own, the Fuel
Slice debt and every pending call go. AbandonedCalls includes pending
Capability and Script calls, including Join members, and excludes Timers.
Each open Decision settles undecided, cancelled, at the first Pump. A
Script that no longer loads is TALK_LOAD_REFUSED, and carried variables over a cap
TALK_STATE_TOO_LARGE. Then every Run is read and
checked against its unit’s code, Host Objects the Host’s resolve refuses
restore disposed, and the restore record lists the pending calls. Until
the first Pump, talk_settle queues each settlement: Answer and Fail as
the Host’s would, Reissue runs the Operation’s Start again, charging only
its Charge, which a Run that can’t cover it faults on at the call, and
Adopt hands back the call. The first Pump must read a Clock at or after the
saved one, or it is clock backwards, and after draining the settlements
it fails every call still pending with call lost, in call id order.
A restore checks every index, length, count and code position a save
gives, so a corrupt or hostile save is refused, never read past or run into
a crash. Integers above 2⁵⁶, and limits above 2⁶², are refused, so the
machine’s arithmetic on them can’t overflow, and every deadline must be an
Instant the Core can show. The stack verifier
works out, from each unit’s code and machine.toml‘s stack effects, the
operand stack every instruction expects: its depth, and which slots hold an
iterator, a reader, a replacement or a Timeout Block’s deadline, through
jumps, Unwind Table targets and offer action entries. Each saved frame’s
stack must be that one: a calling frame’s less its call’s result, a
suspended Run’s top frame’s less what its Suspension Point pushes on
resuming. Inside a catch dispatch or cleanup, where frames are held aside
and stacks cut back, each stack must be a prefix of its code’s, and its
search, cleanups and transfer must point at frames and Unwind Table entries
that exist, counting the frames a dispatch holds aside. A Run’s own wait is no Join Member’s, a Run queued in its
Script’s work is ready or preempted, and a saved iterator stays within what
it iterates. A test holds every corpus unit to one stack shape at each
instruction, and the decoder is fuzzed: every byte of two busy Groups’
saves changed, and thousands of random changes, each with its digest made
good again, must be refused or restore and run without a crash or leak,
under both mismatch policies.
The store notification router, a Host companion, waits for checkpoints.
A Script’s limits come from talk_load_options’ talk_limits, a zero field
taking its default from limits.toml. On both native and WASI targets, each
positive field is supported up to its conformance minimum in that Data File;
a larger or negative field is refused at load as TALK_INVALID_VALUE, before
the Script is compiled or registered. The Message Layer’s load follows the
same profile and returns invalid value. Refusing an unsupported profile
leaves the Group able to load and run Scripts. A Delivery’s talk_limit_override
tightens its Run’s Fuel, allocation, MaxWait and MaxJoin; one that loosens is
refused invalid value, with the override on its > line. A Limit Fault
comes before the faulting instruction’s charge, so none of it is made:
- Its own limit first: a call past the call depth, a
make-patternpast the Text Pattern size and a Join Member past MaxJoin fault before anything else, througherror.LimitFault. - Fuel and allocation: every other instruction faults when its whole
charge, dispatch’s
clauserate included, won’t fit. A search’sunwindis part of the charge of the instruction driving it. An error’s raise record precedes that charge; its Join is abandoned only when the error leaves the Join’s body. A Capability call checks its declared cost before the Host is called; one whoseChargeis refused spends its own charge, and one whose result’s conversion faults on allocation keeps its Fuel before the conversion.store-varchecks its charge before it writes. - Persistent State: a Run-ending
return,passorvetomeasures the Script without the Run before its charge, and a suspension measures it with the Run after its charge, abandoning the call it waits on.
The fault’s record names the Script Variables its rollback changes back, and
every Script’s Segment keeps a base for it. MaxWait bounds a Run’s waiting
calls, and a Script’s send to a mailbox at its depth raises mailbox full.
A Host Delivery to a Script whose mailbox and queued Deliveries reach its
depth is refused TALK_ERR_MAILBOX_FULL.
talk_pump_options gives a Pump a Fuel Slice per Script and a Fuel cap for
the Group. Each Script’s slice is what its debt leaves of it, and an overrun
is carried to its next Pump as debt. Before each instruction, a Run whose
Script has spent its slice, or whose Group has spent its cap, is preempted:
its Stretch ends with a preempt record, it stays at the head of its
Script’s work, and a later Pump continues it with seg … continue, in the
same Segment. A Run preempted in a clause’s patterns or Guard goes on with
its dispatch from that clause. A Script that has spent its slice takes no
more turns, a spent cap ends the turns, and either leaves the Pump sliced.
A preempted Run’s Persistent State counts its catch dispatches: each context
and Error, its activation, the owner’s own stack and the frames it holds
aside. Its pending cleanup has no size of its own, as chapter 8’s Pending
cleanup state gives it: an Error-mode cleanup counts its Error’s context and
the failed continuation’s operand stack, which keeps the failed
instruction’s operands; an acceptance its context and activation; a transfer
its pending arguments. A cancellation’s cleanup drops the dispatch contexts
it interrupted and keeps only the frames and activations its remaining
finally blocks need, their stacks cut to the iterator prefixes those
blocks use. Dispatch’s clause rate is paid at the clause’s first instruction,
with that instruction’s fixed rate for a clause with no patterns or Guard, or
alone for one that parks or is dropped; parking and dropping measure
Persistent State.
talk_script_cancel_run cancels a Run by its id, and the C-only
talk_cancel_delivery a Request or Decision by its delivery id, or a Broadcast
Decision by its broadcast id, both as Host Inputs. A Broadcast cancellation
takes each recipient that hasn’t sealed its part, Script by Script in load
order, its queued Delivery before its Run. A Run whose Decision already sealed
carries on, and a cancellation drained after the whole Decision sealed does
nothing and writes no record, since a Message Layer Host learns of the seal
only from the Pump’s reports; , replacing cancels through the same path. Where the cancel lands, a
preempted Run, in the middle of its Segment, abandons the scopes on its
participating Grants and rolls back that participant and its Script Variables.
Its cleanup becomes a Segment of its own; unrelated scopes remain until that
cleanup ends. Every cancelled Run’s pending calls and Join Members are abandoned, and
named in abandon records after its first cleanup Stretch. This includes a
suspending Call cancelled at its starting callback, before any suspension
Segment commits. Its finally blocks run once, innermost first, in its Script’s turn, a queued Run keeping
its place, on the Cleanup Budget in place of Fuel per Run. An error escaping
cleanup ends it with cleanup-failed … code=, keeping its writes; a limit
ends it with cleanup-failed … limit=, rolling them back. Either way the Run
ends cancelled, and its cleanup Segment’s participant commits unless a limit
ended it. A Delivery still in a mailbox is removed and reported as a run
with no Run, and its Decision settles undecided.
talk_script_stop, talk_script_cancel_run and talk_script_rewind_run
made between Pumps wait for the next one’s drain. Made during a Pump, from
a Host function, they land at that Host crossing (chapter 9, Threads and the
input queue): right after the crossing’s call or prop records, and its
scope record, and before its result is converted, which isn’t charged.
What a Host function writes while it runs, such as a refused set-parent,
is held and written there too, in the order made, and a Stop, cancellation
or Rewind of the running Run ends its Stretch there: a Stop with
end=stop, a cancellation rolling the Segment back before its cleanup, and
a Rewind with end=rewind. Anything else made during a Pump lands between
turns, or at its end.
talk_script_rewind_run is RewindRun (ADR 0068): a Run still in its first
Segment, or parked at dispatch, ends as if it had never started. Its Segment
rolls back as at a Limit Fault, its Script Variables too unless it was
parked or had been unparked but had not resumed its clause body. Changes
committed by other Runs since dispatch stay committed. Its scopes are
abandoned and its participant rolled back, and its
message goes back to the head of its Script’s mailbox as it left it, its ids,
its place on its Message Path and an open Decision kept. Its Fuel stays in
its Script’s counters. The Pump it lands in returns rewound, so the Host can Reload before the
message is dispatched again. When a callback rewinds another Run, the active
callback and its Segment finalization finish before that Pump returns, including
any deferred lifecycle cleanup. One that lands on a
Run that has passed a Suspension Point, or has ended, writes note … kind= not-rewindable and does nothing.
talk_group_call calls a Function Value from the Host, the Host Input
call-value: a Request to its Home Script, whose Run writes fn= in place
of a Handler. A value whose Home Script is stopped, or that is stale, writes
note d<n> kind=function-gone when the queue is drained and runs nothing,
and one called with the wrong number of arguments raises wrong arity at
its function’s first instruction, charging nothing.
talk_script_stop queues Stop Script, which is sticky; disposing a Script’s
owner stops it within that input, with reason owner disposed. Each of the
Script’s Runs, in start order, has its scopes abandoned and its participant
rolled back, and one in the middle of its Segment its Script Variables too;
then they are discarded with no finally and the mailbox is dropped. Senders
waiting on them get send failed, reason stopped. The Script’s named Grants
are released with reason stop, in code-point order, then its stopped
record lists what was discarded, dropped and abandoned, the pending calls in
call id order, and its open Decisions settle undecided, cancelled. A
Delivery that reaches a stopped Script later is
dropped with another stopped record, a Script’s send to it is dropped, and
a Broadcast leaves it out, as it does a Script a Stop or disposal queued ahead
of it will stop. A Group whose Scripts are all stopped pumps as stopped.
Stop marks every discarded Run’s suspended Calls and pending Join members abandoned before invoking cleanup or Grant release hooks. Already-settled Calls are cancelled only if their answers have not been consumed; they add no second abandonment record. Call handles and ids remain valid through release.
Values and ownership
Section titled “Values and ownership”talk.h implements Nothing, Bool, Text, Int, Uint, FromFloat, Dec, Quantity,
CivilDate, ParseCivilDate, Instant, Bytes, List, Map and Range, and their value
and Decimal accessors. Number text is checked before arithmetic, including
linear rejection of oversized inputs. Arithmetic uses native Zig integers
and arbitrary-precision rational intermediates, with one half-even rounding
and the operator’s ideal exponent. Addition, subtraction, multiplication,
division, div, mod and ^ are available internally, and ^ is also
power(x, y). An integer exponent of any size gives the exact power, rounded
once; any other exponent gives the correctly rounded result with trailing
zeros dropped. Noninteger powers bound e^(y ln x) in fixed point and add
precision until both bounds round alike, after exact results and ties are
found directly. Results that overflow or round to zero are settled from their
magnitude, so huge exponents stay cheap.
Each returned value owns one reference. Constructors copy byte input and retain child values; text and map keys become NFC. Map keys preserve insertion order and refuse NFC duplicates. Container writes rebind the root, preserving aliases and preventing cycles, including insertion of the previous root into its replacement. Map writes validate and normalise only the incoming key. Maps of at most 64 entries use flat storage; larger Maps share immutable blocks of 32 ordered entries. A write copies the directory and its changed block, including that block’s key bytes, while retaining the other blocks. Indexed entry access is constant-time; key lookup remains linear. Deletion rebuilds ordered storage inside the value module without repeating key validation. Storage sharing does not change logical sizes, Fuel or the Save format. Final block release queues children through the allocation-free Value teardown. The Map storage measurements record scaling, retained-version memory and the small-Map regression checks. Every node remembers its allocator, so Zig tests use the leak-detecting test allocator. C values use the process allocator; a Group’s own values use its Group allocator. Handles require Host synchronization when shared across threads.
Number, Boolean, Nothing and Quantity construction specializes the active tag before
allocating a node. Each node initializes every header and its active payload
through its owning allocator, without Allocator.create’s whole-node poison
fill. Number, Boolean and Quantity construction also avoid copying the full
Data union;
Zig still zero-fills Nothing’s inactive union storage. The Value layout,
inline Quantity and Group cache are unchanged. The
constructor measurements
record safe native assembly, paired timings and allocation probes.
Text and Decimal accessor pointers borrow their value. Byte accessors copy
out, and display, encoding and canonical Unit strings return an owned
talk_owned_bytes, released with talk_owned_bytes_release. Child accessors
return owned references; the header documents each lifetime. Encoding refuses
Function Values at any depth and resolves Host Objects through the decoder’s
callback. Decoding accepts only the forms the encoder writes: a decoded value
must re-encode to the number, tag and payload the input spelled, so {"$dec": "3"}, 1.0 and a non-UTC or non-shortest $instant are refused. JSON white
space and string escapes are syntax, and text is normalised to NFC. Pattern
decoding reparses canonical source; compilation and matching belong to the
front end.
Values nest without a Core cap, as in the Go and TS Cores: only the Allocation
Budget, the Persistent State cap and the Group’s physical cap bound them, each
with its specified fault (#871). Every container and Function Value keeps its
contents’ logical size as it is built, so measuring one never walks it.
Equality and ordering honor ignoring case at every text value, including
texts inside lists, map values and Function captures. Map keys and Text Pattern
sources remain exact. Comparison Fuel counts Characters of the simple foldings,
and each comparison folds a shared text node once while charging every logical
occurrence. Membership and Destructuring literal tests use the same comparison
mode.
Equality, ordering, display, both JSON encodings, decoding, snapshots, Shape
scans and teardown keep their pending work on heap stacks, so a value nested
100,000 deep runs on a 256 KiB native stack. The json Library’s source
recurses on nesting, so a deep value there meets the call-depth Limit Fault;
limits/deep-values shows both.
Corpus runner
Section titled “Corpus runner”zig build corpus -- [--list] [case directory ...] discovers
all case kinds and runs encoding cases through talk.h, and Disassembly
Cases through the front end, since talk.h exposes no compiled unit, with the
Go runner’s messages. It holds each case.toml to the language and Cost Model versions
and to the tables and keys that corpus.toml
allows for its kind. Selectors may name a category or a case, with or without
a corpus/ prefix. --list prints each case’s kind and whether it is
supported or deferred, and a failure prints the first divergent line and the
command that reproduces it. The case setup reader
builds each Script’s Grants, with their modes and arities, from the case’s
Operation Declarations and Standard Capabilities. Trace Cases replay through
talk.h alone (talk_replay.zig) when they need
only loads, Deliveries and Requests, Pumps, vars, counters, Host Object
inputs, the Grants it makes and its Libraries, compiled with its Operations as
their Grant declarations, and reload, extend and replace-library, whose
source it reads with its scalars as written, compiling a replacement against
the version of each Library it compiled last. It keeps each save by its id,
restores with the Libraries it compiled last but those withheld, the
Grants it loaded each Script with but those unbound, and every Host Object
but those disposed, and goes on with the restored Group’s handles.
A Stop, CancelRun, RewindRun or set-parent written inside a Pump’s output
is made from the Host function of the crossing whose call or prop record
it follows, counting from the Pump, so that it lands there. A call-value
calls the Function Value a Request earlier gave the Host.
Every Trace Case runs twice: as written, and with a save and restore between
each pair of Pumps (chapter 11, Save and restore replays), which must give
the same Trace. After each Pump that another follows, the runner saves; a
save refused with effects pending leaves the original Group going on.
Otherwise it restores with every Library, Grant and Host Object given back,
adopts each pending call, and goes on in the restored Group. The lines it
causes are left out of the Trace, the settle lines its adoptions make when
the next Pump drains them included, and the case’s own save ids keep the
numbers they would have had. It skips the round trip where the case saves,
restores or settles next itself, before any later call-value, whose
Function Value the original Group made, and where a later answer or fail
is for a call the save doesn’t hold, whose handle belongs to the original
Group. A
failure in this replay says so. Encoding cases that register Host Objects are
not supported.
With no selection the runner runs the whole Corpus, as CI does, and every case must pass: a failing or unsupported case fails the run. There is no passing list. Expected corpus bytes are never rewritten. The runner’s replay backend says which cases it supports and replays each from its directory; Trace parsing uses generated record/key metadata, including filled input keys and optional ids, for the comparison.
Fuzz worker
Section titled “Fuzz worker”zig build fuzzworker builds talk-fuzzworker, which the differential
fuzzer runs with --zig-runner (the fuzzer).
It reads a Fuzz Case’s setup as the Corpus runner reads case.toml, with each
inline source under its source name, and takes the case’s Host Inputs one
at a time through the same replay Host, resolving each symbolic reference
against its own Trace as the fuzzer’s runner.ts does. A bare > save is
kept under the id the Core gives it. It answers with the Trace, the action and
record counts and the Group Fingerprints around each input.
Regenerating tables
Section titled “Regenerating tables”tools/zig/generate.ts writes the catalogues
and compiled Cost Model terms from spec/data/*.toml, and
tools/unicode/generate.ts writes
unicode.zig and its UNICODE-LICENSE.txt from the pinned UCD. Neither reads
another Core.
bun run zig:generate # the cataloguesbun run unicode:generate # the Unicode table, for every CoreThe catalogues include grammar.toml’s word lists, which the parser reads,
and stdlib.toml’s Built-ins and stdlib Library exports, which the checker
reads.
bun run check holds both to their sources and fails on a stale table. The
emitters write zig fmt’s canonical form themselves, so the check needs no Zig
toolchain; CI’s zig fmt --check holds them to it.
Every Pump enforces a nondecreasing Clock before draining queued inputs or
changing accounting. Equal readings are allowed, and a new Group may begin
before the Unix epoch. A restored Group starts with its saved Clock as the
lower bound. The C API and Message Layer return clock backwards for a
decrease. Without crossing-trace-v1, the Message Layer retains refusal Trace
for its next successful reply; opted-in Hosts receive available records on the
error reply itself. Early Load refusals record the attempted input and refusal.
Binary Request/Pump exchange
Section titled “Binary Request/Pump exchange”src/binary_value.zig implements the Value items for the
Request/Pump binary exchange.
It preserves decimal scale, uses iterative traversal, and resolves Objects and
Functions through the owning Group’s hooks. src/request_pump.zig holds the
fields-envelope reader and writer. Tags, feature names, Report names and
positional fields come from src/generated/envelope.zig, written from
spec/data/envelope.toml; field-name projections are computed at compile time.
The Session negotiates the fields features at hello, serves Request/Pump,
callback, settlement and Load frames, and accepts cancel-correlated by token
until the final reply. The retired map layout is refused. Shared
envelope-frames.json vectors check decoded
field meanings and exact re-encoded bytes, including specialized Reports and
nested Values.
src/need.zig describes each op, prop, effect and release Need once for both
layouts and reads their results into a layout-neutral result before mapping it
to the worker’s Answer. Function resolution returns error.WrongGroup directly,
including from nested Values. Invalid successful Values refuse the frame;
malformed failure Data makes the callback a Host error. Callback parity tests
at the module’s interface consume the shared vectors and exercise both paths.
Frame dispatch and the exchange gate stay in src/message.zig and
src/exchange.zig (ADR 0131).
The experiment record records the
transport implementation and the performance evidence.