--- name: facet description: >- Facet: hand other code a fresh, frozen object with just the functions it needs, never your SDK objects. Use when passing an argument to, or returning a value from, code you don't control: a vendor SDK's callback, an AI model's tools, a plugin, a webhook handler, a contractor's or user's script. Not for choosing which methods a service offers (that's capability-based-design). --- # Facet: a new, narrow object for each party Whatever you hand to another party, plus everything reachable from it (properties, prototype, and the return value of every function), is what they get. Build that object on purpose. **Bad:** hand over the SDK object. An email SDK's Email reaches `.mailbox.account.password`. ```js summarize({ email }); ``` **Good:** a frozen object of closures and plain data. ```js const { freeze } = Object; summarize(freeze({ subject: email.subject, thread: async () => (await email.thread()).map(e => freeze({ from: e.from.address, body: e.body })), reply: async text => { await email.reply(String(text)); }, })); ``` Rules: 1. **Return data, not what the underlying call returned.** `text => email.reply(text)` hands out the Email that `reply` returns. Chaining APIs (`db.set()` returning the db, any method returning `this`) leak the same way. 2. **Copy records** into frozen plain objects; SDK records carry back-references. 3. **Keep authority in closures,** not properties: a `this.client` or `this.headers` field is readable and writable by the holder. Freeze what you hand out. 4. **Bind scope when you make the facet.** Don't take a folder name, path, or user from the holder and act on it with your own authority. Before finishing, walk everything reachable from each facet and add a test that does it: no client, token, raw store, or other party's data. ## Sources - [E in a Walnut: Capability Patterns](https://www.skyhunter.com/marcs/ewalnut.html#SEC45): facets. - [awesome-ocap wiki](https://github.com/dckc/awesome-ocap/wiki): a pattern language for ocaps.