Replaces the self/puppet split: `alice.tradeWith(bob)` rather than `alice.pay(to, amount)`, per the actor-sim skill. Generated-by: Claude Opus 5.5
60 lines
5.0 KiB
Markdown
60 lines
5.0 KiB
Markdown
---
|
|
name: pola
|
|
description: >-
|
|
Principle of Least Authority as the way to organize all code: trace the flow of authority, give each component only what its task needs, design APIs around attenuation. Use when writing or reviewing any code, tests included: makers and their parameters, module imports and exports, which objects share a scope, what a facet exposes; when someone says "POLA", "least authority", "who can reach that?", "Bob shouldn't see Alice's purse", or "makeFoo-style actors". The table stakes (no clocks, fetch, or env reached for inside logic) are no-ambient-authority; this is the organizing principle on top.
|
|
---
|
|
|
|
# POLA: organize code by the flow of authority
|
|
|
|
Mark Miller, [SOSP 50th anniversary panel](https://www.youtube.com/watch?v=br9DwtjqmVI) (3 min):
|
|
|
|
> we design abstraction boundaries so that a component's interface only requires arguments that are somehow relevant to its task. If such argument passing were the only source of authority, we would have taken a huge step towards least authority.
|
|
|
|
Every reference to anything but immutable data is authority: a purse is the right to spend some assets; a mint, the right to create them; a seat, the right to take part in one deal; a DB handle, every row. Ask of every reference: who holds it, who gave it to them, and does their task need all of it?
|
|
|
|
## Recursively, at every level
|
|
|
|
[Navigating the Attack Surface](https://www.youtube.com/watch?v=wW9-KuezPp8) (15 min): the same move repeats at each level of composition (OS accounts, apps, modules, objects), and the benefit multiplies, like a Menger sponge. CapMail's main module "receives all of that authority … wiring them together, subdividing the authority … and handing it out piecemeal … written knowing that the address book does not need access to the PGP keyring, so the main module simply doesn't give it that access."
|
|
|
|
So at each level, code has two roles:
|
|
|
|
- **A wiring layer** (entry point, `main`, test setup, a maker) holds a lot of authority, and its job is to split it and hand out pieces. Keep it small and obvious: it is the TCB at its level.
|
|
- **Everything else** gets only the pieces its task needs, as arguments, and can't reach more.
|
|
|
|
## In practice
|
|
|
|
- **Makers take only what their task needs.** `makeAlice({ usdcPurse, stockDepositFacet })`, not `makeAlice({ ledger })`.
|
|
- **Attenuate at API boundaries.** Hand out the narrowest facet that works: a deposit facet rather than the purse; a seat rather than the escrow's admin; a read-only view rather than the DB. When designing an API, decide which facets it has and who should get each.
|
|
- **Separate parties get separate scopes.** If two parties' authority shares a scope, a bug or a careless line lets one act as the other. A scope holding both Alice's and Bob's purses is a design error.
|
|
- **Tests too.** Tests are where the code's intended authority flow is first written down. Write each party as a `makeFoo`-style actor holding its own authority privately; the test's top level is the wiring layer that creates the parties and introduces them ([sequence-diagram-actor-sim](https://github.com/Agoric/agoric-sdk/blob/master/.agents/skills/sequence-diagram-actor-sim/SKILL.md)).
|
|
- **The test takes the spontaneous step; actors carry out the consequences.** Give an actor what it knows at construction and let it decide how to act: `alice.run()`, or `alice.tradeWith(bob)` to introduce a counterparty. Don't drive it with arguments that decide for it, like `alice.pay(to, amount)`: that is the test acting with Alice's authority, as if it held her purse. What an actor hands others (a deposit facet, a shop) carries no authority to spend for it.
|
|
- **Modules decide what authority enters through imports and leaves through exports.** Export makers, not instances that close over power ([no-ambient-authority](../no-ambient-authority/SKILL.md)).
|
|
|
|
```ts
|
|
// wrong: one scope holds everyone's purses; Bob's code could spend Alice's
|
|
const alice = { usdc: await E(usdc.issuer).makeEmptyPurse(), ... };
|
|
const bob = { usdc: await E(usdc.issuer).makeEmptyPurse(), ... };
|
|
await E(seatB).offer(await E(bob.stock).withdraw(shares(3n)), ...);
|
|
|
|
// right: each party holds its own; the test wires, drives, and observes
|
|
const makeAlice = async ({ usdcIssuer, stockIssuer, dollars }) => {
|
|
const usdc = await E(usdcIssuer).makeEmptyPurse(); // Alice's alone
|
|
...
|
|
return harden({
|
|
// Alice decides what to pay; Bob shows only his shop
|
|
tradeWith: async bob => E(bob.shop).sell(await E(usdc).withdraw(price), ...),
|
|
getBalances,
|
|
});
|
|
};
|
|
await alice.tradeWith(bob); // the one spontaneous step
|
|
```
|
|
|
|
## Review checklist
|
|
|
|
Before committing, for each new maker, scope, and export:
|
|
|
|
1. Draw (in your head or as a [wiring-diagram](../wiring-diagram/SKILL.md)) who holds what after this change.
|
|
2. Does any non-wiring scope hold two parties' authority, or more than its task needs?
|
|
3. Could a narrower facet replace what's passed? Does any facet let a caller spend for its owner, or does the test decide what an actor does?
|
|
4. Name the wiring layer at this level. Is it small?
|