Captain / Documentation / Part 4. Effects and safety

Determinism, run records and replay

Contents ↓

Captain's first law is determinism. Approving a plan by its digest, trusting a compiled artifact, auditing a calculation and replaying a run all depend on it: if the same inputs could give different results, none of them would mean anything.

The law has three parts:

  1. Builds are deterministic. The same source, built by the same compiler, gives a byte-identical .capb on any machine. The artifact's SHA-256 is its identity.
  2. Runs are deterministic. Given the same artifact, the same arguments and the same effect outcomes, the virtual machine performs the same steps and gives the same values and the same errors.
  3. Replays are exact. A run's log, fed back to the virtual machine, reproduces the run.

One door for the outside world

All nondeterminism enters a run through one place: the outcomes of effects. Everything else is closed off.

  • There is no clock. "What time is it" would have to be an effect, and the runtime has none for it: the time is an argument (below). A program could run date with exec, but then the answer is an effect outcome, recorded and replayed like any other.
  • There is no random number generator and no access to environment variables, the locale or the time zone.
  • There is no floating point. Decimals are exact, and integer overflow and division by zero stop the run with a defined error instead of wrapping (Exact decimals).
  • Text is compared and ordered by its code points, the same everywhere.
  • There are no unordered collections, and parallelDo: takes results and failures in list order, never in the order work finishes.

A pure entry has no effects, so its result depends on its arguments alone.

The same source, the same bytes

motd.capt
module Example::Motd.

;; A greeting built from whatever the file says today.
export greeting path: Path -> Text [
  file := readFileText path: path maxBytes: 4KiB.
  ret "today: " append: ((file content) trimmed).
].
Terminal
$ captain build motd.capt -o first.capb
built first.capb ([..])
$ captain build motd.capt -o second.capb
built second.capb ([..])
$ cmp first.capb second.capb && echo identical
identical

The artifact holds no timestamps, paths or other traces of where or when it was built. Two builders can each compile a program and compare digests, and an artifact can be checked against its source at any time by building again.

Run logs

Every captain run writes a run log, <entry>.runlog unless --log FILE names another, and says where on its first line. Once a run has started, the log is written whether it succeeds or fails.

Terminal
$ printf 'sunny\n' > weather.txt
$ captain build motd.capt -o motd.capb
built motd.capb ([..])
$ captain run motd.capb greeting path=weather.txt --read-only
run log: greeting.runlog
"today: sunny"
$ cat greeting.runlog
capb [..]
vm [..]
entry greeting path:
args [..]
e 0 [..] ok [..] LocalMachine readFileText path:maxBytes:
status completed "today: sunny"

A log is plain text, one record per line:

Line Holds
capb the SHA-256 of the artifact that ran
vm the virtual-machine generation it ran under
entry the entry point
args a SHA-256 of the arguments, not the arguments themselves
rewrite one line for each --rewrite-host A=B the run was given, as rewrite A B: its http requests for A went to a mock at B (Host policies). A run without any has no such line
allow exec present when the run was given --allow-exec, and so could start processes (Starting processes). A run without it has no such line
e one line per effect, in order: its sequence number, a SHA-256 of its request (the effect's arguments, and the host for a remote effect), the outcome (ok, failed or unknown), the outcome's encoded value or message in hexadecimal, the frontier and the effect
status how the run ended, and its result or error

The arguments are recorded only as a digest, so to replay a run you supply them again. Each outcome is recorded in full, because replay must give the program exactly what it got: a readFileText outcome holds the file's content, an http outcome the response body. Treat a run log with the same care as the data the program read. Secret bytes are never in it (Secrets).

Replay

captain replay runs a program again against its log. Every effect is answered from the log, in order, and nothing is performed: a replayed read reads nothing, and a replayed write writes nothing. A replayed exec starts nothing either, so replay needs no --allow-exec, nor any other run option.

Terminal
$ printf 'rain\n' > weather.txt
$ captain replay motd.capb greeting.runlog path=weather.txt
replay ok: "today: sunny"

The file now says rain, but the replay shows what the run saw. This is what replay is for: an operation that went wrong can be re-executed later, anywhere, step for step, without the machine it ran on and without its secrets.

Replay refuses, with CAP-REPLAY-DIVERGENCE, whenever the run and the log disagree:

Terminal
$ captain replay motd.capb greeting.runlog path=other.txt
CAP-REPLAY-DIVERGENCE: run arguments differ from the recorded run
$ sed 's/today: /now: /' motd.capt > changed.capt
$ captain build changed.capt -o changed.capb
built changed.capb ([..])
$ captain replay changed.capb greeting.runlog path=weather.txt
CAP-REPLAY-DIVERGENCE: log was recorded against artifact [..], not this one

The other divergences are an effect the log does not have, an effect the program requests in a different order, with different arguments or on a different frontier, and recorded effects the replay never requests. CAP-REPLAY-DIVERGENCE lists the messages. Under the determinism law, a divergence always means that something changed: the program, its arguments or the log. It is never noise to be retried.

Replay also checks the recorded outcomes by what they produce: the replayed run must end exactly as the log's status line says it ended. A log whose outcomes were edited replays to a different result, and a log whose status was edited no longer matches its outcomes, so either edit is refused:

Terminal
$ sed 's/status completed "today: sunny"/status completed "today: snow"/' greeting.runlog > edited.runlog
$ captain replay motd.capb edited.runlog path=weather.txt
CAP-REPLAY-DIVERGENCE: the replay ended `completed "today: sunny"`, but the log records `completed "today: snow"`; the log's outcomes or status were changed after the run

A run that failed replays to the same failure (replay ok (run failed as recorded): ...), and so does a run whose outcome was unknown.

Two limits of replay as it stands:

  • The log is not signed. Replay finds any edit that leaves the log inconsistent with itself, but a log rewritten throughout, outcomes and status together, replays to whatever it now says. Keep logs where they cannot be changed if you rely on them as evidence.
  • captain replay cannot replay an entry that takes a grant: it asks for an approval, as captain run does, and has no option to take one.

Time is an argument

A program that needs the current time takes it as an argument. The caller observed it, and the run log's args digest covers it:

expiry.capt
module Example::Expiry.

use Std::Utc.

;; Whether a certificate has expired, and when it does. The time is an
;; argument: the caller observed it, the program does not.
export check expires: i64 now: i64 -> Text [
  (Utc::isoSeconds milliseconds: expires)
    ifOk: [ :when |
      (now >= expires) ifTrue: [ ret "expired at " append: when ].
      ret "valid until " append: when ]
    ifFail: [ :e | ret (e message) ].
].
Terminal
$ captain build expiry.capt -I lib/std -o expiry.capb
built expiry.capb ([..])
$ captain run expiry.capb check expires=1798761600000 now=1795000000000
run log: check.runlog
"valid until 2027-01-01T00:00:00Z"
$ captain run expiry.capb check expires=1798761600000 now=1800000000000
run log: check.runlog
"expired at 2027-01-01T00:00:00Z"

check is pure, so the same two numbers always give the same answer, and a test or an auditor can ask about any moment, past or future. Times here are integer milliseconds since 1970 in UTC, as Std::Utc uses them.

Run records

captain run writes run logs. A program that embeds Captain through the Rust SDK gets something stronger for pure entries: a run record, a short fixed-format text that binds the artifact, the entry, the arguments, the limits, the exact instruction count and the result, and names the semantics the run was produced under. Anyone holding the artifact can verify a record by running it again and comparing byte for byte.

A record names the semantics, not the release that produced it, and a VM generation never changes meaning once released. So a record made by one release still verifies under every later release. Limits, run records and errors describes the record, and VM generations the generations.

Designed, not built

The specification describes tests that run a program against a simulator: the same virtual machine, fed synthetic effect outcomes instead of real ones, exploring what happens when each step fails. Because the machine is deterministic, the simulator would be the executor itself, not a second implementation. test declarations check today, but the simulator does not exist yet, and they do not run.