Captain / Documentation / Part 2. A tour

An operation, planned, approved and run

Contents ↓

This chapter follows one small operation from its source to a finished, recorded run. It builds on Your first program and adds the steps between writing a program and letting it change something. Those steps are the point.

The operation is a release. A website serves whichever version is named in a marker file, site/current.txt. Releasing version 2.4.1 means replacing 2.4.0 in that file with 2.4.1. It is one write, but it changes what every visitor gets, so a person has to approve it first.

In practice the program might well have been written by an agent, and the person approving it may never read its source. They read a plan: a short, fixed-format account of exactly what the program will do at these arguments. Everything below is arranged so that the plan is enough.

The program

release.capt
module Site::Release.

;; What a release receipt attests to, and what approval grants.
export struct Released {}.

;; What the site serves now: the version in the marker file, and the
;; digest of the file's bytes.
export struct Served { version: Text, digest: Digest }.

;; The marker file holds the version and a line break.
markerText version: Version -> Text [
  ret (Text from: version) append: "\n".
].

;; Look at the marker, changing nothing.
export served marker: Path -> Served [
  file := readFileText path: marker maxBytes: 4KiB.
  ret Served { version: ((file content) trimmed), digest: (file digest) }.
].

;; Switching the version the site serves is the step that needs approval:
;; the effect cannot be called without a Grant[Released]. The marker is
;; replaced only if its bytes still have the digest the reviewer saw.
effect promote on: LocalMachine marker: Path to: Version replacing: Digest
  authorizedBy: Grant[Released] -> Receipt[Released] [
  writeFileText path: marker content: (markerText version: to) replacing: replacing.
].

;; Release a version: the entry point a person approves.
export release version: Version marker: Path replacing: Digest
  authorizedBy: Grant[Released] -> Text [
  promote marker: marker to: version replacing: replacing authorizedBy: authorizedBy.
  ret "now serving " append: (Text from: version).
].

The module has four kinds of thing in it:

  • Two structs. Served is ordinary data. Released has no fields; it exists to name what a receipt attests to and what a grant authorizes.
  • A pure procedure, markerText, which turns a Version into the file's text. It has no effects and is not exported.
  • An exported procedure that reads, served. It reads the marker and returns the version and the file's SHA-256 digest.
  • An effect, promote, and the entry point release that calls it. An effect is a procedure whose body may perform effects, and whose signature says what it needs. promote needs a Grant[Released], and the program has no way to make one: a grant comes only from an approved plan. Its write is writeFileText path:content:replacing:, which replaces the file only if the file's bytes still have the digest it is given.

Types such as Version, Path and Digest are tagged values: text that has been checked to be a version, a path or a SHA-256 (Symbols, quantities and tagged values). A version that is not MAJOR.MINOR.PATCH cannot reach the marker file.

Check, build, inspect

Terminal
$ captain check release.capt
ok    release.capt (module Site::Release, 6 declarations)
checked 1 file as 1 module, no errors
$ captain build release.capt -o release.capb
built release.capb ([..])
$ captain inspect release.capb
package  Site::Release 0.0.0
sha256   [..]
compiler 0.1.0   vm [..]   stripped

exports
  release version: Version marker: Path replacing: Digest authorizedBy: Grant[Released] -> Text  (destructive)  [promote marker:to:replacing:authorizedBy:]
  served marker: Path -> Served  (mutating)  [readFileText path:maxBytes:]

effects
  promote marker:to:replacing:authorizedBy:  @ LocalMachine  → Receipt[Released]  (defined)
  writeFileText path:content:replacing:  @ LocalMachine  → FileWritten  (declared)
  readFileText path:maxBytes:  @ LocalMachine  → FileText  (declared)

frontiers
  LocalMachine  (local)

entry points
  promote marker:to:replacing:authorizedBy:  marker=<Path> to=<Version> replacing=<Digest> authorizedBy=<Grant[Released]>
  release version:marker:replacing:authorizedBy:  version=<Version> marker=<Path> replacing=<Digest> authorizedBy=<Grant[Released]>
  served marker:  marker=<Path>

pools: [..]

captain check finds mistakes before anything is built. captain build compiles the module into an artifact, release.capb, whose SHA-256 is its identity: the same source always builds to the same bytes.

captain inspect reads the artifact, not the source, and says what it needs and offers. This is the first thing a reviewer can look at:

  • release is destructive: it takes a grant, so it cannot run without an approved plan. served is mutating, which only means it has effects; reading a file is an effect. The class cannot tell a read from a write, which is why the host decides that (Effects).
  • effects lists every effect the program can reach. Two are built into the runtime (declared), and one is written in Captain (defined). Every one runs on LocalMachine; this program reaches no other machine, no network, and runs no other program.
  • entry points are what captain run can start, with the arguments each one takes.

Look before you change

The site's marker says 2.4.0 today:

Terminal
$ mkdir site
$ printf '2.4.0\n' > site/current.txt
$ captain run release.capb served marker=site/current.txt --read-only
run log: served.runlog
Served { version: "2.4.0", digest: "2ad7b0c3f0ede58c3165b7a8a5479e890a758d39bfe8683eaef50ce6afad7232"#sha256 }

--read-only tells the host to admit observations and refuse anything that could change the machine, whatever the program asks for (Host policies). The digest is what the release will be bound to: it identifies exactly the file that was looked at.

Plan

The release cannot simply be run:

Terminal
$ captain run release.capb release version=2.4.1 marker=site/current.txt \
    replacing=2ad7b0c3f0ede58c3165b7a8a5479e890a758d39bfe8683eaef50ce6afad7232
error: `release version:marker:replacing:authorizedBy:` demands Grant[Released]: plan it, approve the plan, then run with --plan <approval file>

So plan it. captain plan runs the same program on the same virtual machine, against a host that performs nothing, and writes down every effect the run would request:

Terminal
$ captain plan release.capb release version=2.4.1 marker=site/current.txt \
    replacing=2ad7b0c3f0ede58c3165b7a8a5479e890a758d39bfe8683eaef50ce6afad7232
captain plan v1
program sha256 [..]
entry `release version:marker:replacing:authorizedBy:`
  version: "2.4.1"#semver
  marker: "site/current.txt"#path
  replacing: "2ad7b0c3f0ede58c3165b7a8a5479e890a758d39bfe8683eaef50ce6afad7232"#sha256
  authorizedBy: «grant, minted on approval»
args sha256 [..]

node 1: `writeFileText path:content:replacing:` on LocalMachine
  path: "site/current.txt"#path
  content: "2.4.1\n"
  replacing: "2ad7b0c3f0ede58c3165b7a8a5479e890a758d39bfe8683eaef50ce6afad7232"#sha256
  bounds: no timeout declared

authorities required (minted on approval, bound to this plan's hash):
  Grant[Released]

defaults in force: sequential execution, no automatic retries (v0.1)
result: "now serving 2.4.1"
plan sha256 [..]
plan file: release.capp

This is what the reviewer reads, and it answers the questions a reviewer has:

  • Which program? program sha256, the digest of the artifact.
  • With what? The entry and its arguments, with their digest. The grant is not an argument anyone supplies: it is minted on approval.
  • Doing what? One node: a write of "2.4.1\n" to site/current.txt, replacing the file whose digest is 2ad7…7232, the one served showed. The content is computed, not described: pure code such as markerText runs for real during planning. The effect promote is not a node of its own; the root effect its body requests is.
  • Needing what authority? Grant[Released].

The plan is saved as release.capp, and plan sha256 is the SHA-256 of that file. The same program, entry and arguments always give the same plan, byte for byte. Plans and approval describes every line.

Approve

Terminal
$ captain approve release.capp
approved plan sha256 [..]
minted Grant[Released] (bound to this plan)
approval file: release.capp.approved

The approval names the plan by its hash, and holds one grant for each grant the plan requires. Approving the plan is approving these exact effects with these exact arguments, not the program in general.

Run

With --plan, captain run plans the invocation again, in memory, and starts only if it comes to the approved hash. Then it fills the entry's Grant[Released] parameter. Approval does not decide where the program may write, though; the host does, and captain run writes nothing unless told where it may:

Terminal
$ captain run release.capb release version=2.4.1 marker=site/current.txt \
    replacing=2ad7b0c3f0ede58c3165b7a8a5479e890a758d39bfe8683eaef50ce6afad7232 \
    --plan release.capp.approved
run log: release.runlog
error[CAP-RUN-EFFECT]: effect `writeFileText path:content:replacing:` failed: writeFileText: this host admits no local writes; it must name the directories it allows
$ captain run release.capb release version=2.4.1 marker=site/current.txt \
    replacing=2ad7b0c3f0ede58c3165b7a8a5479e890a758d39bfe8683eaef50ce6afad7232 \
    --plan release.capp.approved --allow-write site
run log: release.runlog
"now serving 2.4.1"
$ cat site/current.txt
2.4.1

The approval says what may happen; --allow-write site says where this host lets it happen. Both have to agree. A plan that tried to write outside site/ would be refused by the host even though someone approved it.

The record

Every run writes a run log, and says where on its first line:

Terminal
$ cat release.runlog
capb [..]
vm [..]
entry release version:marker:replacing:authorizedBy:
args [..]
e 0 [..] ok [..] LocalMachine writeFileText path:content:replacing:
status completed "now serving 2.4.1"

It names the artifact, the entry and a digest of the arguments; then one line per effect, with a digest of its request and its outcome in full; then how the run ended. Logs are the evidence of what happened (Run logs).

captain replay runs a program again against its log, answering every effect from the log and performing none. Replaying the look taken before the release shows what the reviewer was shown, although the file has changed since:

Terminal
$ captain replay release.capb served.runlog marker=site/current.txt
replay ok: Served { version: "2.4.0", digest: "2ad7b0c3f0ede58c3165b7a8a5479e890a758d39bfe8683eaef50ce6afad7232"#sha256 }
$ captain replay release.capb release.runlog version=2.4.1 marker=site/current.txt \
    replacing=2ad7b0c3f0ede58c3165b7a8a5479e890a758d39bfe8683eaef50ce6afad7232
error: `release version:marker:replacing:authorizedBy:` demands Grant[Released]: plan it, approve the plan, then run with --plan <approval file>

The second command shows a limit of the command line today: captain replay asks for an approval for an entry that takes a grant, and has no option to accept one, so the release's own log cannot be replayed with it (Replay).

When something changes

An approval covers one plan. Change anything the plan was made from, and the run refuses to start. Here the arguments change, then the program:

Terminal
$ captain run release.capb release version=2.5.0 marker=site/current.txt \
    replacing=2ad7b0c3f0ede58c3165b7a8a5479e890a758d39bfe8683eaef50ce6afad7232 \
    --plan release.capp.approved --allow-write site
error[CAP-PLAN-STALE]: this invocation plans to sha256 [..]
but the approval is for sha256 [..]
(program, entry, or arguments changed since approval — re-plan and re-approve)
$ sed 's/append: "\\n"/append: " (beta)\\n"/' release.capt > edited.capt
$ captain build edited.capt -o edited.capb
built edited.capb ([..])
$ captain run edited.capb release version=2.4.1 marker=site/current.txt \
    replacing=2ad7b0c3f0ede58c3165b7a8a5479e890a758d39bfe8683eaef50ce6afad7232 \
    --plan release.capp.approved --allow-write site
error[CAP-PLAN-STALE]: this invocation plans to sha256 [..]
but the approval is for sha256 [..]
(program, entry, or arguments changed since approval — re-plan and re-approve)

Editing the plan file would not help either: the run compares against a plan it makes itself.

The world can change too, and no plan can bind the world. That is what the digest is for. Presenting the same approval again, now that the marker says 2.4.1, plans to the same hash, so the run starts, but the write finds a file that is not the one the reviewer saw, and refuses:

Terminal
$ captain run release.capb release version=2.4.1 marker=site/current.txt \
    replacing=2ad7b0c3f0ede58c3165b7a8a5479e890a758d39bfe8683eaef50ce6afad7232 \
    --plan release.capp.approved --allow-write site
run log: release.runlog
error[CAP-RUN-EFFECT]: effect `writeFileText path:content:replacing:` failed: writeFileText: `site/current.txt` has changed since it was read (it is now [..], not 2ad7b0c3f0ede58c3165b7a8a5479e890a758d39bfe8683eaef50ce6afad7232); nothing was written

The same refusal protects a change someone else made between approval and run: it is never silently overwritten.

Why each step exists

A program that changes things has two authors and a reader. One author writes the program, perhaps an agent. The other, the host, decides what it may touch. The reader, a person, approves what it will do. Each step gives one of them something they can check without trusting the others:

  • captain check refuses whole classes of mistake before anything runs. Among them, a program cannot make its own grant, so an effect that demands one cannot be reached without approval:
Refused by the checker: CAP-RESOLVE-005
module Site::Shortcut.

use Site::Release.

;; Release without asking anyone.
export hurry version: Version marker: Path replacing: Digest -> Text [
  ret Release::release version: version marker: marker replacing: replacing
    authorizedBy: Grant[Release::Released] {}.
].

Which effects demand a grant is decided by their signatures. A program that called writeFileText itself would need no grant, and that is visible too: captain inspect would list the write in its effect set (Grants). - captain inspect shows what the artifact can reach at all: here, two file effects on this machine and nothing else. - captain plan shows what it will do at these arguments: one write, with its content and the digest of the file it replaces. - captain approve binds a person's decision to that plan's hash. The reviewer approves the plan, not the program text, and the machine that wrote the program cannot widen what was approved: any change to the program, the entry or an argument changes the hash. - --allow-write is the host's own limit, which no program or plan can move. - The run log and replay keep a record that can be re-examined later, anywhere, without the machine and without performing anything again.

Part 4 takes each of these further: Effects, Plans and approval, Secrets, receipts and grants, Determinism, run records and replay and Host policies. The file effects are listed in Files.