feat(no-ambient-authority): designation is not authority; tests' helpers
From reworking toybox's tests: a port or a path, joined to a fetch or fs the code holds anyway, reaches more than the one resource, so hand out the object bound to it (a toybox's sockets; a tree rooted at a directory, as openat reaches it). Swiss numbers belong at the boundary that can't carry references. A test's I/O is set up once, as a promise each test awaits, and each helper gets only the powers it uses. Generated-by: Claude Opus 5.5
This commit is contained in:
1 parent
2b8704bdd0
commit
2851fe872a
1 file changed
+41
-1
@@ -1,7 +1,7 @@
|
||||
---
|
||||
name: no-ambient-authority
|
||||
description: >-
|
||||
Use when writing or reviewing code whose functions touch I/O, clocks, random number generators, process.env, fetch, or filesystem; before writing or refactoring any Python script (DisciplinedPython style guide and checker); when someone says "ambient authority", "wash your mouth out", or "that function shouldn't reach for that"; when debating dependency injection or default-arg seams; when tempted to inject a logger or assert on log output; when asked what OCap discipline is. Source: the Agoric wiki's OCap Discipline page.
|
||||
Use when writing or reviewing code whose functions touch I/O, clocks, random number generators, process.env, fetch, or filesystem; before writing or refactoring any Python script (DisciplinedPython style guide and checker); when someone says "ambient authority", "wash your mouth out", or "that function shouldn't reach for that"; when debating dependency injection or default-arg seams; when tempted to inject a logger or assert on log output; when a helper takes a port, a socket path, or a file path beside the fetch or fs that reaches it ("designation vs authority"); when setting up a test's I/O; when asked what OCap discipline is. Source: the Agoric wiki's OCap Discipline page.
|
||||
---
|
||||
|
||||
# OCap Discipline
|
||||
@@ -42,6 +42,43 @@ capability-based languages, systems, papers, and talks.
|
||||
authority they need, then *delegate* it downward.
|
||||
3. **Test scripts** are named `test-*.js`; tests export nothing.
|
||||
|
||||
## designation is not authority
|
||||
|
||||
A string that names a resource is a designator: a port, a socket path, a file path, a package directory computed from `import.meta.url`. Joined to a broad power the code holds anyway (`fetch`, `WebSocket`, `fs`), it becomes access to that resource, and to everything else the same power reaches. That's the integer-to-pointer cast again, done by the caller. Hand out the object bound to the resource, not its address:
|
||||
|
||||
- A test harness's `startToybox()` answered `{ server, port, appPort, sock }`, and each test joined those to its own `fetch` and `WebSocket`. Now it answers `{ http(path, init), app(path, init), admin(kernel), stop() }`, each bound to its own socket, so a helper can be handed just the app socket.
|
||||
- A module-level `here` (the package's path) let any line read or write anywhere by joining onto it. Instead, hand a directory and the tree under it, as `openat(dirfd, path)` reaches it: `pkg.readText('test/boot/boot.js')`, relative paths only, no `..`. endo's `EndoMount` and `ReadableTree`, and workerd's disk service, have the same shape.
|
||||
- Where something truly needs an address (workerd's `-d dir=<path>`), the bound object can answer one (`tree.path(rel)`), at the edge.
|
||||
|
||||
Bearer secrets (swiss numbers, webkeys) are designation and authority in one string, for crossing a boundary that can't carry a reference: a link, a page's JSON. Mint and deref them at that boundary, and let inner modules deal in references. A module that mints them holds a registry and entropy its task doesn't need.
|
||||
|
||||
## tests are the edge; their helpers aren't
|
||||
|
||||
A test may import Node's I/O, but set it up once, as a promise each test awaits, not at module level (ava's `test.before(t => { t.context = ... })` is the same idea):
|
||||
|
||||
```js
|
||||
const worldP = (async () => {
|
||||
const { startToybox } = makeToyboxes({ startWorkerd, freePort, mkdir, fetch, WebSocket });
|
||||
const fs = { cp, mkdir, readFile, rm, writeFile };
|
||||
return {
|
||||
startToybox,
|
||||
net: { createHttpServer, listenLocal },
|
||||
pkg: makeTree(fs, fileURLToPath(new URL('..', import.meta.url))),
|
||||
makeDir: async prefix => makeTree(fs, await mkdtemp(join(tmpdir(), prefix))),
|
||||
};
|
||||
})();
|
||||
|
||||
test('...', async () => {
|
||||
const { startToybox, pkg, makeDir } = await worldP;
|
||||
// ...
|
||||
});
|
||||
```
|
||||
|
||||
- Each helper takes its powers as its first parameter, and only those it uses: `copyExamples({ pkg }, dir)`, not `copyExamples(world, dir)`.
|
||||
- Group powers that are equivalent in authority anyway, and pass the group whole: with a server and a local listener, `net` reaches the network as `fetch` would.
|
||||
- Destructure the world on one line. When a whole group fits better than its members, take the group and pick at the call: `makeClient({ fetch: net.fetch }, url)`.
|
||||
- Spawning a shell script to copy a few files (`execFile('wallet-boot.sh')`) is the biggest hammer for the smallest job. If the copy is configuration, make it data, or point a read-only root at the files where they are.
|
||||
|
||||
## Python: DisciplinedPython
|
||||
|
||||
For Python, the rules above are spelled out in the DisciplinedPython style
|
||||
@@ -84,6 +121,9 @@ before writing or refactoring a Python script; don't invent conventions.
|
||||
helpers become untestable without monkey-patching; inject a
|
||||
narrower interface (the caller passes the one URL's worth of fetch,
|
||||
not the whole world).
|
||||
- A helper taking `(port, fetch)` or `(path, fs)` → designation split
|
||||
from authority: the helper can reach any port, any path. Hand it the
|
||||
bound object (see above).
|
||||
- A module's default export closing over bindings/config → a
|
||||
powerful object export. Split: pure logic (argument-only) + a thin
|
||||
program-script-ish edge that wires authority in.
|
||||
|
||||
Reference in new issue
Block a user