Captain / Documentation / Part 4. Effects and safety

Frontiers

Contents ↓

A frontier is a place where effects run, together with the effects that are available there. The local machine is one frontier; a server reached over SSH is another. Every line of a Captain program has a frontier that the checker knows before the program runs, so a program cannot quietly do on one machine what it was written to do on another.

The frontiers that run

Frontier What it is Transport
LocalMachine the machine running captain run direct: subprocesses, system calls, the HTTP client
SshHost a server entered with on host: ... using: #Ssh do: [ ... ] the system OpenSSH client

A module may also declare frontiers of its own, such as an account at a cloud provider, and home effects on them; below says how far that goes today.

Where each line runs

The checker gives every statement a place, and the place decides which effects may be called there:

Place Effects allowed
a procedure body any; the procedure takes on the place of whoever calls it, and a call is refused where the effects it performs outside its own frontier blocks are not available
an effect body with on: F F's effects and effects with no home
an effect body without on: effects with no home only: exec, shell, http, observe serverAt:
an on host:using:do: block the entered frontier's effects and effects with no home
a check, build or evidence body of a declaration, a test none

An effect with no home runs on the frontier of the place that calls it: an exec inside an SSH block runs on the server, the same exec outside it runs locally. An effect with a home always runs at its home.

Calling an effect where its frontier is not available is refused (CAP-FRONTIER-020). Calling any effect in a body that may have none is refused too (CAP-EFFECT-032, and CAP-EFFECT-036 for a procedure that has effects).

Entering another machine

on host: server using: #Ssh do: [ ... ] runs a block on another machine:

remote.capt
module Example::Remote.

;; How long a server has been up, as its `uptime` command says.
export up at: Address -> Text [
  server := observe serverAt: at.
  seen := on host: server using: #Ssh do: [
    r := exec program: "uptime"#path args: #() timeout: 10s.
    (r stdout).
  ].
  ret seen.
].
  • observe serverAt: makes a Server value from an address. It is a binding, not a probe: it performs no I/O, but it is an effect, so it appears in plans and run logs.
  • host: takes that Server. #Ssh names the way in; a symbol that no frontier is entered by, or a host of any other type, such as an address given as Text, is refused (CAP-FRONTIER-022). A block meant for another machine never runs on this one instead: the runtime, too, refuses an effect bound to a host it has no transport for.
  • Inside the block the frontier is SshHost.
  • The form's value is the value of the block's last expression statement, here the command's output. ret inside the block is not supported: the block is not a procedure, so there is nothing for it to return from (CAP-BUILD-001).

on host:using:do: is part of the language, like ifTrue:, not a library call. Its block is staged: the compiler builds it as a separate unit meant to run on the other side of the boundary, and it never exists as a value the program could store or pass around.

Captures

A staged block may read the bindings around it. Each one it reads is a capture: a copy taken across the boundary. Captures are read-only inside the block, because assigning to a copy would change nothing outside it (CAP-FRONTIER-021). To bring something back, make it the block's value.

What crosses is data: numbers, text, quantities, symbols, tagged values, records of those, Server values, receipts, grants and secret handles (the handle crosses; the bytes never do). Blocks and frontiers are not values at all, so they cannot be captured.

The compiler records each staged block's captures, with their types, in the artifact. Every plan lists them under staged blocks (capture manifests), so what crosses a boundary is never implicit.

Planning a remote program

Planning runs nothing, so a program for another machine can be planned anywhere:

Terminal
$ captain plan remote.capt up at=203.0.113.10
captain plan v1
program sha256 [..]
entry `up at:`
  at: "203.0.113.10"#address
args sha256 [..]

node 1: `observe serverAt:` on LocalMachine
  serverAt: "203.0.113.10"#address
  bounds: no timeout declared
node 2: `exec program:args:` on SshHost host «result of node 1»
  program: "uptime"#path
  args: #(  )
  timeout: 10s
  after: node 1
  weakened guarantees: exec — idempotency not proven, observation untyped, compensation unknown
  bounds: timeout 10s

staged blocks (capture manifests):
  `up at:#block0` captures nothing

defaults in force: sequential execution, no automatic retries (v0.1)
result: «result of node 2 stdout»
plan sha256 [..]
starts processes: exec program:args: (captain run needs --allow-exec)
plan file: up.capp

Node 2 runs on SshHost, on the host that node 1 produced, and after: records that dependency. The last line says the entry starts a process: uptime, on the server. A command on another machine is still a command the runtime cannot see into, so captain run needs --allow-exec for it (Host policies).

The SSH transport

Captain does not install anything on the servers it manages. There is one orchestrator per run, the captain run process, and the block's control flow, bindings and parsing all run there. Only the effects cross: each one inside the block is carried to the server by the transport. For SshHost that is the system ssh client, run in batch mode (it never prompts) with strict host-key checking. The server needs sshd and the programs the wrappers name, and nothing of Captain.

The SSH transport carries exec, shell and installFileText. exec's arguments are quoted for the remote shell one by one, and its unlessFileExists: and creates: witnesses are tested on the server. http is not carried: a block that makes an HTTP request checks, but the request fails at run time.

Other effects homed on SshHost, such as those of Host::Debian, check and plan, but have no implementation, so the run refuses them before connecting to anything:

service.capt
module Example::Service.

use Host::Debian.

export restart at: Address -> Text [
  server := observe serverAt: at.
  on host: server using: #Ssh do: [
    service name: "app" ensure: #Running.
  ].
  ret "running".
].
Terminal
$ captain build service.capt -o service.capb
built service.capb ([..])
$ captain run service.capb restart at=203.0.113.10
run log: restart.runlog
error[CAP-RUN-EFFECT]: effect `service name:ensure:` failed: effect `service name:ensure:` is a declaration without a definition; the ssh transport carries exec/shell only[..]

The options that choose the user, key, port and known-hosts file, and the commands that pin a server's host key, are in Host policies.

Fanning out

parallelDo: runs a staged block once for each element of a list:

fan.capt
module Example::Fan.

;; Read two files, each in its own staged block.
export check a: Path b: Path -> Text [
  limit := 1KiB.
  paths := List empty: Path.
  paths := paths push: a.
  paths := paths push: b.
  paths parallelDo: [ :p |
    readFileText path: p maxBytes: limit.
  ].
  ret "read both".
].

The block takes the element as its parameter and may capture outer bindings, read-only, like any staged block. parallelDo: is a statement: it has no value. The usual use is one on host: block per server, as in the repository's corpus/tools/deploy.capt.

Despite the name, the runtime runs the blocks one after another, in list order. The meaning is fixed either way: results and failures are taken in input order, never in the order work happens to finish, so a later runtime that runs them at the same time cannot change what a program does.

Terminal
$ printf 'one\n' > one.txt
$ printf 'two\n' > two.txt
$ captain plan fan.capt check a=one.txt b=two.txt
...
node 1: `readFileText path:maxBytes:` on LocalMachine
  path: "one.txt"#path
  maxBytes: 1KiB
  bounds: no timeout declared
node 2: `readFileText path:maxBytes:` on LocalMachine
  path: "two.txt"#path
  maxBytes: 1KiB
  bounds: no timeout declared

staged blocks (capture manifests):
  `check a:b:#block0` captures limit: Bytes
...
$ captain build fan.capt -o fan.capb
built fan.capb ([..])
$ captain run fan.capb check a=one.txt b=two.txt --read-only
run log: check.runlog
"read both"

Declared frontiers

A module may declare a frontier and home effects on it:

Fragment
frontier DigitalOceanAccount [
  provides effects: #( #createDroplet, #assignFloatingIp, #destroyDroplet ).
  secrets scope: #Account.
].

export effect createDroplet on: DigitalOceanAccount
  name: Text region: Symbol size: Text image: Text
  sshKeys: List[Text] tags: List[Text]
  -> Droplet [ ... ].

This is from corpus/tools/digitalocean.capt, a wrapper over a cloud provider's HTTP API. Today the declaration does three things: it names a type that effects can use as their home, its entry using: #Sym onto: Type clauses say how on host:using:do: may enter it, and captain inspect lists it under frontiers with the transport api. Its provides effects: and secrets scope: clauses are checked for form only. An effect homed on it runs its body in the captain run process, and the HTTP requests go out from the local machine. A plan lists the declaration under dormant declarations. A block entered onto a declared frontier with on host:using:do: checks and plans, but captain run has a transport for SshHost only, so an effect inside such a block fails rather than run on the local machine.

Designed, not built

The language specification describes more than the runtime does. These parts check, so programs that use them can be written and reviewed, but they do not run:

  • cluster, fleet, volume, lowering and lifecycle declarations. The checker holds their clauses to schemas, and gives their phase bodies (bootstrap, steps, compensation, checks) the right places and effect rules. The compiled artifact carries them as dormant, plans list them, and captain run never activates them. The same goes for test declarations, whose simulator does not exist yet.
  • Adapter tables. The specification lets a frontier map a protocol to the instance it uses there (Package to Apt on a Debian host), with the plan naming the choice. There is no syntax for this yet. Protocols and their instances exist (Types as arguments, and generics), and a program that needs a particular backend calls it directly.
  • Remote executors. A later runtime may run a staged block inside a cluster rather than drive it from one local process. Nothing in a program would change; only the transport would.