Captain / Documentation / Part 1. Getting started

The toolchain

Contents ↓

Everything happens through one program, captain. This chapter goes through its commands in the order a program meets them. Later parts explain the ideas behind each command; this is the map.

Command Reads Does Runs the program?
captain check .capt parse and check no
captain build .capt compile to an artifact no
captain inspect .capb or .capt print the manifest no
captain plan .capb or .capt render every effect an entry would perform against a host that never acts
captain approve a plan file record approval, bound to the plan's digest no
captain run .capb execute an entry and write a run log yes
captain replay .capb and a run log re-execute against the log alone yes, with no effects
captain lsp the language server, over standard input and output no

The examples below use one small program that imports a library module. Save it as stamp.capt:

stamp.capt
module Stamp.
use Std::Utc.

;; An ISO 8601 time for a moment given in milliseconds since 1970.
;; Captain programs have no clock: the moment is always an argument.
export stamp ms: i64 -> Text [
  (Utc::isoSeconds milliseconds: ms)
    ifOk: [ :text | ret text ]
    ifFail: [ :e | ret "invalid: " append: (e message) ].
  ret "".
].

It uses Std::Utc from the standard library, which lives in the lib/std/ directory of the Captain distribution's library sources. An imported module's procedures are called with the module's last name in front: Utc::isoSeconds milliseconds: (Modules and names). In the sessions below, lib/ is that library directory.

captain check

Terminal
$ captain check stamp.capt lib/std/utc.capt
ok    lib/std/utc.capt (module Std::Utc, 8 declarations)
ok    stamp.capt (module Stamp, 1 declaration)
checked 2 files as 2 modules, no errors

check takes any number of files and checks them together, as one session: a module may import any other module among them. It reports each file and then a summary, and exits with status 1 if any file has an error.

Without the library file, the import has nothing to resolve to:

Terminal
$ captain check stamp.capt
error[CAP-RESOLVE-003]: unknown module `Std::Utc`
  --> stamp.capt:2:5
2 | use Std::Utc.
  |     ^^^^^^^^
...

--json prints one captain.check/1 document instead, for editors and other tools. Each diagnostic carries its code, message, help line, path and range:

Terminal
$ captain check --json stamp.capt lib/std/utc.capt
{"captain":"[..]","diagnostics":[],"files":[[..]],"format":"captain.check/1","modules":2,"release":"[..]","verdict":"ok"}

captain is the version of the captain command, and release the version of the Captain release it belongs to (Versions and VM generations).

captain build

Terminal
$ captain build stamp.capt -I lib/std -o stamp.capb
built stamp.capb ([..] bytes, sha256 [..])

build compiles one root module and every module it imports, found beneath the -I directories, into one artifact. -o names the artifact; without it, the artifact is named after the source file. The same sources always give the same bytes, so the printed SHA-256 identifies the program.

captain inspect

Terminal
$ captain inspect stamp.capb
package  Stamp 0.0.0
sha256   [..]
compiler [..]   vm [..]   stripped

exports
  stamp ms: i64 -> Text  (pure)

entry points
  stamp ms:  ms=<i64>

pools: [..]

inspect prints what the artifact offers and what it needs: its exports with their signatures and effect class, the entry points captain run accepts with the type of each argument, and, for a program with effects, the effects, tools and secrets it uses. It runs nothing, so it is safe on an artifact you have not reviewed. inspect also accepts a .capt file (with -I), and --json prints one captain.inspect/1 document.

captain plan and captain approve

Terminal
$ captain plan stamp.capb stamp ms=1700000000000
captain plan v1
program sha256 [..]
entry `stamp ms:`
  ms: 1700000000000
args sha256 [..]

no effects: the entry is pure at these arguments

defaults in force: sequential execution, no automatic retries (v0.1)
result: "2023-11-14T22:13:20Z"
plan sha256 [..]
plan file: stamp.capp

plan runs the entry against a host that never acts. Every effect the entry would perform becomes a node of the plan, with its arguments and its place in the order; values that only an effect could produce stay symbolic. This entry is pure, so the plan has no nodes and can show the result. The plan is written to a plan file (stamp.capp), and its SHA-256 is the plan's identity. For an entry that can start a process, a line after the hash says so: starts processes: ..., which captain run admits only with --allow-exec.

captain approve stamp.capp records approval of exactly that plan, in stamp.capp.approved, and mints the grants the plan's destructive steps demand. captain run --plan stamp.capp.approved then re-plans in memory and refuses to run if anything differs from what was approved. Plans and approval explains this with an operation that needs it.

captain run

Terminal
$ captain run stamp.capb stamp ms=1700000000000
run log: stamp.runlog
"2023-11-14T22:13:20Z"

run executes one entry point of an artifact. Arguments are selector=value, one per selector, and must match the entry's types: ms is an i64 here. The result is printed as a Captain value, and the run log is written to <entry>.runlog, or to the file named by --log.

run takes only an artifact, never source: what runs is always a built, verified .capb.

The host decides what a program may do. These options widen or narrow it:

Option Effect
--read-only admit observation effects only
--allow-exec let the program start processes (exec, shell, observeProcess); without it, an entry that can start one is refused before anything runs
--allow-write DIR admit the runtime's file writes beneath DIR, and only there
--plan FILE.approved run under an approved plan; required for entries that demand a Grant
--secret NAME=VALUE, --secret-file NAME=PATH, --secret-env NAME=ENV, --secret-fd-env NAME=ENV supply a secret
--arg-file NAME=PATH take a Text argument from a file
--rewrite-host A=B send requests for host A to B, for mock endpoints
--ssh-user, --ssh-known-hosts, --ssh-identity, --ssh-port configure the SSH transport
--log FILE write the run log to FILE

Host policies describes each in detail.

captain replay

Terminal
$ captain replay stamp.capb stamp.runlog ms=1700000000000
replay ok: "2023-11-14T22:13:20Z"
$ captain replay stamp.capb stamp.runlog ms=0
CAP-REPLAY-DIVERGENCE: run arguments differ from the recorded run

replay runs the program again, with the same arguments, against the run log alone: every effect's outcome comes from the log, and nothing touches the world. It reports CAP-REPLAY-DIVERGENCE at the first difference. Determinism, run records and replay covers what it proves.

The rest

  • captain lsp serves .capt and .forge files to an editor (Installing Captain).
  • captain ssh-inspect HOST shows the SSH host-key fingerprints a machine offers, and captain ssh-accept HOST --fingerprint SHA256:... --known-hosts FILE pins one you have checked, for the SSH transport's strict host-key checking (Host policies).
  • captain --version, --copyright and --license print what they say.