Modules and Capability Boundaries(v1 · en)

Concept

Modules put bindings inside named fences: module defines bindings in a fresh scope and :export declares which names cross the wall; require imports public names, and an alias is a map from keywords to values. Mutable bindings inside a module still obey lexical rules — exported names are live, and re-registration is immediately visible.

(module :demo.wallet :export [balance deposit]
  (def balance 0)
  (defun deposit [x] (set! balance (+ balance x))))
(require :demo.wallet :as wallet)
(println (demo.wallet/deposit 5))
(println demo.wallet/balance)
(println ((get wallet :deposit) 10))
(println demo.wallet/balance)

Expected stdout:

5
5
15
15

deposit updates the module-scoped balance via set!; code outside the module can only touch it through the export surface — shared state inside a fence. Capabilities are the other boundary: what a program may touch (files, network, processes) is granted by the launcher and cannot grow at runtime. (language-capabilities) reports the current runtime's capability declaration surface; map printing order is not a language guarantee, so the example extracts fixed fields:

(def caps (language-capabilities))
(println (get caps :schema-version))
(println (get caps :remote-restart))
(println (get (get caps :engines) :js))

Expected stdout:

2
false
true

The engines table reports registration truth (it varies with host assembly — the same CLI inside a restored-image context reports :js false), not authorization state — presence ≠ authorization.

Design notes

Module boundaries and capability boundaries complement each other: modules govern "may this name cross the wall", capabilities govern "may this effect happen". Three anti-confusion principles run through the latter: presence ≠ authorization (an engine registered in root only means its symbols resolve); installation ≠ authorization (a package manifest is a declaration, not a grant); authorization cannot flow upward (libraries and macros cannot obtain authorization on the caller's behalf — it can only shrink along the call chain). File access is the most direct way to run this boundary: a restored image inherits no authority from the process that saved it, and every file access during evaluation is checked against the grants in force at that moment — see the experiment Capability denial.

Going further

The object-capability principle "the reference is the capability" operates at two levels: module exports (you can change the ledger only if you hold deposit) and host grants (you can read a file only if you hold file-read). EROS/KeyKOS built it into a kernel, WASI into a syscall boundary, Zio into a language effect boundary — the three layers compose. The lesson for language designers: a security model is exactly as strong as "every capability acquisition path is explicit"; one implicit path (a global variable, environment probing) collapses the whole wall. For content organization across modules, see the map nodes Module system and Capabilities and authorization.

前序概念

后续概念