Captain / Documentation / Part 4. Effects and safety

Plans and approval

Contents ↓

Before an operation runs, you can ask Captain what it will do. captain plan answers with a plan: every effect the run will request, in order, with its arguments, where it runs, what it depends on and how far it can be trusted. You read the plan, approve it, and then run exactly what you approved. If anything changed in between, the run refuses to start.

This is the review step the language is built around. When a program was written by an agent, the plan is what the person responsible reads: a short, fixed-format account of the program's effects at these arguments, rather than the program itself.

How a plan is made

There is no separate planner. captain plan runs the same compiled program, on the same virtual machine, against a host that never performs anything. Each effect the program requests becomes a node of the plan, and its result becomes a placeholder, «result of node N», that flows on through the program:

  • pure code with known values runs for real, so a plan shows computed arguments, not expressions;
  • a value computed from a placeholder is a placeholder too;
  • a decision that depends on a placeholder cannot be made, so planning stops there and says so (see Deferred plans).

Planning reads nothing and writes nothing, so it is safe to plan any program on any machine, including one written for servers you cannot reach.

Reading a plan

This program posts a notice by writing a file. Posting needs approval: the effect demands a Grant[Posted], which only an approved plan can provide (Secrets, receipts and grants).

notice.capt
module Example::Notice.

;; What a posting receipt attests to.
export struct Posted {}.

;; Posting a notice is the step that needs approval: the effect cannot be
;; called without a Grant[Posted], and only an approved plan mints one.
effect post on: LocalMachine text: Text at: Path authorizedBy: Grant[Posted]
  -> Receipt[Posted] [
  createFileText path: at content: text.
].

export publish text: Text at: Path authorizedBy: Grant[Posted] -> Text [
  post text: text at: at authorizedBy: authorizedBy.
  ret "posted".
].
Terminal
$ captain build notice.capt -o notice.capb
built notice.capb ([..])
$ mkdir board
$ captain plan notice.capb publish text="Lunch is at noon." at=board/today.txt
captain plan v1
program sha256 [..]
entry `publish text:at:authorizedBy:`
  text: "Lunch is at noon."
  at: "board/today.txt"#path
  authorizedBy: «grant, minted on approval»
args sha256 [..]

node 1: `createFileText path:content:` on LocalMachine
  path: "board/today.txt"#path
  content: "Lunch is at noon."
  bounds: no timeout declared

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

defaults in force: sequential execution, no automatic retries (v0.1)
result: "posted"
plan sha256 [..]
plan file: publish.capp

From the top:

  • program sha256 is the digest of the compiled program. A rebuilt program with any change has a different one.
  • entry and its arguments, then args sha256, their digest. Grant parameters are shown as minted on approval: they are outputs of approval, not inputs to the plan, so they are not in the digest.
  • One block per node: the effect, the frontier it runs on, and the host for a node inside an on host: block. Then its arguments, and:
  • after: the earlier nodes whose results it uses;
  • secrets: the names of the secrets it uses, never their values;
  • weakened guarantees: for exec and shell (Effects);
  • bounds: its timeout, or no timeout declared.

Text longer than 160 bytes is shown as its first 80 characters, its size and its SHA-256, so a file's content does not bury the plan and the plan still binds to the exact text. - staged blocks lists every block that crosses to another frontier or fans out, and what it captures (Frontiers). - authorities required lists the grants the entry demands. - dormant declarations lists declarations that check but do not run (Frontiers). - defaults in force states what the runtime will do that the program did not say: run the nodes one at a time, in order, and retry nothing. - result is the entry's result, as far as it is known, or a deferred: line.

An effect defined in Captain, such as post, is not a node of its own. Its body runs during planning, and the root effects it requests, here createFileText, are the nodes.

The plan file and its hash

captain plan writes the plan to <entry>.capp (publish.capp here), or to the file named by -o, and prints plan sha256, the SHA-256 of that file's bytes. The plan text is a deterministic function of the program, the entry and the arguments, so the same invocation always gives the same plan and the same hash.

captain plan --json prints the same plan as one captain.plan/1 JSON document, with the hash as planSha256, for tools that show plans; it writes no plan file.

Approving

captain approve records an approval of a plan file:

Terminal
$ captain approve publish.capp
approved plan sha256 [..]
minted Grant[Posted] (bound to this plan)
approval file: publish.capp.approved
$ cat publish.capp.approved
captain approval v1
plan sha256 [..]
grant Grant[Posted]

The approval file names the plan by its hash and lists one grant for each grant the plan requires. It is a record, not a signature: approving and running happen on the same machine, by the same operator, and nothing cryptographic is minted. What a grant binds to, the plan hash, is what a signed approval would sign.

Running what you approved

An entry that demands a grant cannot run without an approval. The grant parameter cannot be given on the command line either; it can only come from an approval file:

Terminal
$ captain run notice.capb publish text="Lunch is at noon." at=board/today.txt
error: `publish text:at:authorizedBy:` demands Grant[Posted]: plan it, approve the plan, then run with --plan <approval file>
$ captain run notice.capb publish text="Lunch is at noon." at=board/today.txt \
    --plan publish.capp.approved --allow-write board
run log: publish.runlog
"posted"
$ cat board/today.txt
Lunch is at noon.

With --plan, captain run plans the invocation again, in memory, and compares the hash with the approved one. Only if they match does it start, with each Grant[...] parameter of the entry filled by a grant bound to that hash. If the program, the entry or any argument has changed, the hash differs and nothing runs:

Terminal
$ captain run notice.capb publish text="Lunch is at one." at=board/today.txt \
    --plan publish.capp.approved --allow-write board
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 does not help: the run compares against a plan it makes itself, not the file. The way forward is to plan and approve again.

--plan works for any entry, not only those that demand grants. With it, a run refuses to start unless it would do what was approved.

What an approval does not cover

  • The host's policy. --allow-write board is not in the plan. A host can still refuse an approved effect: run the approved plan above without --allow-write and createFileText fails with this host admits no local writes (Host policies). The same goes for --allow-exec: approving a plan that runs a command does not let the run start it.
  • The state of the world. The plan binds the program, the entry and the arguments, not what the effects will find. A file the plan creates may exist by the time the run starts, and the effect then fails.
  • Single use. An approval is not consumed. The same approval file can be presented again, and the run will plan to the same hash.

Deferred plans

A plan lists what is known before anything runs. When the program decides what to do next from an effect's result, that is not known yet:

rotate.capt
module Example::Rotate.

;; Copy a log aside once it has grown past a limit.
export rotate log: Path into: Path -> Text [
  file := readFileText path: log maxBytes: 1MiB.
  ((file size) > 1000) ifTrue: [
    createFileText path: into content: (file content).
    ret "copied".
  ].
  ret "still small".
].
Terminal
$ captain plan rotate.capt rotate log=app.log into=app.log.1
...
node 1: `readFileText path:maxBytes:` on LocalMachine
  path: "app.log"#path
  maxBytes: 1MiB
  bounds: no timeout declared

defaults in force: sequential execution, no automatic retries (v0.1)
deferred: control flow depends on «computed from «result of node 1 size»»
  the graph beyond this point depends on effect results and is not
  enumerated in v0.1 (DR-0008); rerun `plan` as results become known
plan sha256 [..]
plan file: rotate.capp

The plan stops at the first such decision and says what it depends on. It does not list the branches after it. Approving a deferred plan approves what it shows and the fact that the rest is not known; a reviewer who needs more should ask for a program whose effects do not depend on what it reads, or plan again once the result is known.

A loop over a list that an effect gives is such a decision: forEach: tests the list's length before every round. A calculation over that list is not. collect:, select:, reject:, detect:, inject:into: and sortedBy: have pure blocks and run as a pure unit of their own, and a pure unit given a placeholder gives one placeholder, so the plan goes on (DR-0037):

inventory.capt
module Example::Inventory.

;; Write the names in a directory, one a line, to a file.
export inventory dir: Path into: Path -> Text [
  names := (listDirectory path: dir maxEntries: 1000) collect: [ :e | (e name) ].
  createFileText path: into content: (names join: "\n").
  ret "listed".
].
Terminal
$ captain plan inventory.capt inventory dir=logs into=names.txt
...
node 1: `listDirectory path:maxEntries:` on LocalMachine
  path: "logs"#path
  maxEntries: 1000
  bounds: no timeout declared
node 2: `createFileText path:content:` on LocalMachine
  path: "names.txt"#path
  content: «result of `list_join`»
  after: node 1
  bounds: no timeout declared

defaults in force: sequential execution, no automatic retries (v0.1)
result: "listed"
plan sha256 [..]
plan file: inventory.capp

Written with forEach: and push:, the same program's plan stops after node 1.

The specification goes further than the runtime here. It describes plans that enumerate every branch of a deferred decision, with the worst case of each, and a binding that also covers the versions of provider interfaces and a snapshot of what was observed. Neither is built yet.

The honesty rule

A plan never claims more than it knows. What cannot be known before the run is shown as unknown, never left out: a result is a placeholder, a decision on a result is deferred, an exec says what it cannot prove, and a declaration that does not run is listed as dormant. A plan you can approve by its digest is only worth approving if it is honest about its gaps.