Captain / Documentation / Part 7. Embedding Captain

Limits, run records and errors

Contents ↓

A pure call is bounded before it starts, ends in one of a closed set of outcomes, and leaves a record that can be checked by anyone holding the same artifact and arguments. This chapter covers each, then the error codes and the VM generations the SDK runs.

Limits

ExecutionLimits is part of every plan:

Field Bounds Default Ceiling
max_instructions VM instructions the run may use 1,000,000 100,000,000
max_result_bytes the result's canonical encoding 1 MiB 16 MiB
max_duration wall-clock time, or None for no deadline 30 seconds one hour
input structured arguments (below) see below see below

Every limit must be at least 1 and at most its ceiling, or planning fails with CAP-SDK-LIMITS. Override only what you need:

rust
let limits = ExecutionLimits {
    max_instructions: 1,
    ..ExecutionLimits::default()
};

That line is from the test cancellation_and_instruction_exhaustion_are_distinct_and_effect_free, which uses it to make a run stop at once.

Input limits

A plan whose arguments include a list, a record or an option is also bounded by ExecutionLimits::input, an InputLimits, over all its arguments together:

Limit Counts Default Ceiling
max_items values: each argument, list item, record field and Some payload once 1,048,576 16,777,216
max_depth nesting: an argument is depth 1; a list, record or Some adds one for what it holds 32 64
max_bytes bytes of text, octets, symbols and tagged text carried 16 MiB 256 MiB

A list of ten OrderLine records is 31 values: the list, and each line with its two fields. With max_items: 30 it is refused with CAP-SDK-INPUT-LIMIT; with 31 it plans. Nothing is truncated: a value over a limit is refused at planning, before anything runs.

A plan with structured arguments binds its input limits into its execution context, so the same call under different input limits is a different plan. A plan whose arguments are all scalars binds no input limits, and is planned exactly as it was before input limits existed. Records made for scalar calls by older versions of the SDK therefore still verify.

Outcomes

execute always returns an Execution. Its outcome is one of:

ExecutionOutcome Means In the record as
Completed { value, result_identity } the entry returned value completed
FailedBeforeEffects { code } the program failed at run time, with a CAP-RUN-… code such as CAP-RUN-ARITH failed:CODE
TimedOut max_duration passed timed-out
Cancelled the CancellationToken was cancelled cancelled
ResourceExhausted { resource, limit, observed } the run used up Instructions, or its result was over ResultBytes resource:instructions, resource:result-bytes
InvalidResult { code } the run produced something the boundary cannot return invalid-result:CODE

The name FailedBeforeEffects is exact: a pure run has no effects, so every failure is before any effect, and a retry can never repeat a change to the world.

A result over max_result_bytes is not truncated. The outcome says how big it was, and the record holds no result at all.

execution.diagnostics carries a code and a message for people. A message is never an outcome, and is not part of the record.

The run record

A run record, captain.sdk.run/1, is fifteen lines of UTF-8 text, in a fixed order, ending with a newline. The record of the order total from chapter 1 has this shape (identities shortened here; each is 64 hexadecimal digits):

captain.sdk.run/1
profile captain.pure/1
implementation 0da1a341…
artifact 94bc5a1d…
manifest 0d4d8eb0…
entry 0d02234f…
signature 77a32d5d…
arguments 757cd7e1…
context a151b28e…
plan 3ceef995…
instructions 59
effects 0
outcome completed
result-id 11a42fcd…
result 04f13502

It binds everything that decides the result:

  • implementation names the semantics the run was produced under, not the release that produced it (below).
  • artifact, manifest, entry, signature name exactly what ran.
  • arguments is the identity of the frozen arguments, in their canonical encoding. A record's fields are in declared order, whatever order the host gave them.
  • context binds the limits.
  • plan binds all of the above together.
  • instructions is the exact charge, and effects is always 0.
  • result is the result's canonical bytes, in hexadecimal, and result-id their identity. Both are - unless the run completed.

Each identity is a SHA-256 over a length-framed, domain-separated preimage, with its own Rust type: ArtifactId, ManifestId, EntryPointId, SignatureId, ArgumentsId, ExecutionContextId, PlanId, ResultId, RunRecordId and ImplementationId. A PlanId cannot be passed where a ResultId is wanted, even though both print as hexadecimal.

Verifying a record

verify_run_record(&plan, &bytes) parses a record strictly, checks that it binds this plan, and then:

  • for an outcome that can be reproduced (completed, failed, either resource outcome, invalid result), runs the plan again without a deadline and requires the same record, byte for byte. The result is VerificationDisposition::Reexecuted;
  • for cancelled and timed-out, which depend on events outside the program, checks only the record's structure and its binding to the plan. The result is VerificationDisposition::OperationalObservation: the record is well formed, but nothing proves the cancellation or the deadline happened.

Unknown, reordered or duplicate lines, non-canonical numbers or hexadecimal, a missing final newline, trailing data and mismatched identities are all refused. So is a record made for another plan: the test malformed_artifact_and_stale_plan_are_rejected verifies a record against a plan with a different argument and gets CAP-SDK-RUN-RECORD-BINDING. And a forged result, one changed hexadecimal digit, fails re-execution.

What a record proves is narrow, and the profile says so: this artifact, run on these arguments under these limits by this semantics, gives this result. It does not prove who asked, when, or that anyone was allowed to act on the answer.

Error codes

SdkError has a code and a message. Branch on the code; the message is for people.

Code When
CAP-SDK-LIMITS a load or execution limit is zero or over its ceiling
CAP-SDK-ARTIFACT-LIMIT the artifact is larger than LoadLimits allows
CAP-SDK-ARTIFACT the bytes do not decode as a .capb artifact
CAP-SDK-VERIFY the artifact fails verification, or needs a newer VM generation
CAP-SDK-SECRETS-UNSUPPORTED the artifact refers to a secret anywhere
CAP-SDK-EFFECTS-UNSUPPORTED the entry is not pure
CAP-SDK-ENTRY-NOT-FOUND no entry has exactly this full name
CAP-SDK-ENTRY-NOT-EXPORTED the entry exists but is not exported
CAP-SDK-ARGUMENT-CARDINALITY the wrong number of arguments
CAP-SDK-ARGUMENT-NAME an argument is missing, repeated or unknown
CAP-SDK-ARGUMENT-TYPE a value does not match its declared type
CAP-SDK-INPUT-LIMIT structured arguments are over their input limits
CAP-SDK-RUN-RECORD a record is malformed
CAP-SDK-RUN-RECORD-BINDING a record does not bind this plan
CAP-SDK-RUN-RECORD-LIMIT a record is over the plan's limits
CAP-SDK-RUN-RECORD-MISMATCH re-execution gave a different record

These codes appear inside an InvalidResult outcome rather than as errors:

Code When
CAP-SDK-RESULT-TYPE the result holds a value that cannot leave a run, such as a secret
CAP-SDK-PURE-EFFECT the run tried to dispatch an effect (verification makes this unreachable for a sound artifact; the SDK still checks)
CAP-SDK-PURE-UNKNOWN, CAP-SDK-PURE-DEFERRED the VM reported an effect outcome a pure run cannot have
CAP-SDK-FROZEN-ARGUMENTS the frozen arguments could not be rebuilt for the run

An effectful entry is listed by entry_points() with pure: false, and planning it fails with CAP-SDK-EFFECTS-UNSUPPORTED. For a program whose only export is look, which asks a machine for its memory and ports (the program in Mate), the error reads:

CAP-SDK-EFFECTS-UNSUPPORTED: entry point `look` is not admissible under captain.pure/1

VM generations

An artifact records the oldest VM generation that can run it, shown as vm by captain inspect, and the SDK runs a fixed range of generations. It refuses at load, with CAP-SDK-VERIFY, an artifact that needs a newer one. Which generations this release runs, and what each one added, are listed in Versions and VM generations.

Exact decimal division and rounding (Division and rounding) are part of that range, so an embedded calculation can divide and round in Captain. This entry splits a total into equal shares, rounded half-even to cents:

share.capt
module Shares.

export share total: Decimal parts: Decimal -> Decimal [
  ret total divide: parts scale: 2 rounding: #HalfEven.
].
Terminal
$ captain build share.capt -o share.capb
built share.capb ([..])
$ captain inspect share.capb
package  Shares 0.0.0
sha256   [..]
compiler 0.1.0   vm 7   stripped
...
$ captain run share.capb share total=100.00 parts=3
run log: share.runlog
33.33

Through the SDK, share total:parts: with 100.00 and 3 completes with OutputValue::Decimal { mantissa: 3333, scale: 2 }. A plain / is exact or fails: an entry returning a / b gives 5.005 for 10.01 and 2.00, and for 1.0 and 3.0 ends in FailedBeforeEffects { code: "CAP-RUN-INEXACT" } rather than a truncated quotient. Both records verify by re-execution like any other.

Which semantics a record names

The implementation line of a run record does not name the SDK release that wrote it. It names the semantics of the artifact's generation, each range of generations being named by the first release that ran it (DR-0017). So a later release that runs the same semantics writes the same records, byte for byte, and records retained under an earlier release still verify and replay. The test records_retained_under_0_5_0_verify_and_replay_byte_for_byte holds the SDK to that. Changing what a generation means needs a new generation.

implementation_info() reports the running SDK: its package and version, each profile's generation, the bytecode format, the newest VM generation it runs, and the identity of that semantics.