A bare Remote is Remote<any>: name what a reference is, declare buildRootObject with @satisfies so its root type can be exported, and put the one cast where the kernel loses the type. Fixed twice in vattr97 code and back again, because toybox's CONTRIBUTING called bare Remote the one place any was fine; so the skill also says to fix the guidance and add a lint rule. Generated-by: Claude Opus 5.5
3.4 KiB
name, description
| name | description |
|---|---|
| typed-remotes | Typed references in Endo / toybox / Agoric JSDoc — use when writing or reviewing a vat's `Params` typedef, a `buildRootObject`, a store that holds references, or any `@param`/`@property`/`@type` that names another vat's object or a device; when about to write a bare `Remote`, `Remote[]`, `{any}`, or `@type {BuildRootObject<...>}`; when someone says "Params has a bunch of any", "it won't stay fixed", or "type the remotes". Not for whether a reference should be handed over at all (that's pola). |
Typed remotes
A bare Remote is Remote<any>. Every call through it, and everything
it answers, goes unchecked, so a wrong method name or a misread answer
gets through tsc --checkJs and fails at runtime, in another vat.
The rule
-
Name what the reference is:
Remote<X>, neverRemoteorRemote[].Xis:- for another vat's root, that vat's own type, exported beside its
code:
/** @typedef {ReturnType<typeof buildRootObject>} Cache */ - for a facet it hands out, the method's answer:
ReturnType<Cache['viewer']> - for a device or a platform vat with no exported type, a small
interface naming the calls this code makes, in one
types.ts
- for another vat's root, that vat's own type, exported beside its
code:
-
Declare
buildRootObjectwith@satisfies, not@type:/** @satisfies {BuildRootObject<Params>} */.@typechecks the params but answers onlyobject, soReturnType<typeof buildRootObject>is useless;@satisfieschecks the same and keeps what it infers. -
One cast, where the type is truly lost, and it says why. The kernel answers only "some vat's root" for
createVat. Look roots up through one accessor typed by a role table:/** @typedef {{ cache: Cache, gateway: Gateway }} Roles */ /** * it's the vat Roles names because ensure made it from that role's code * @template {keyof Roles} R @param {R} role @returns {Remote<Roles[R]>} */ const rootOf = role => /** @type {Remote<Roles[R]>} */ (vats.get(role).root); -
unknown, notany, for what isn't checked yet, such as parsed JSON:{{ key?: unknown, at?: unknown }}. Then narrow with a check. -
Type the store's values too:
MapStore<keyof State, State[keyof State]>withput/gotgeneric over the key, notMapStore<string, any>.
Make it stay fixed
A fix to the code alone drifts back: the next agent copies the nearest
example, or a doc that blesses Remote as the place any is fine.
- Fix the guidance that licensed it (CONTRIBUTING, AGENTS.md), not just the instances.
- Add a lint rule that rejects a bare
Remoteoranyin JSDoc types, on the files that are clean now, and widen itsfilesas more get typed. vattr97'seslint-typed-refs.mjsis one, with no dependencies: it finds each type tag's balanced{...}in JSDoc comments and flags\bany\band\bRemote\b(?!\s*<). - After typing, remove the test's
anys: they hid the same gaps in the code under test (one, here, hid a store typed so thatgot('user')was alwaysundefined).
Sources
- @agoric/internal types.ts — the
Remotethat toybox's follows,anyby default. - TypeScript 4.9: the satisfies operator — checks a type without widening what's inferred; JSDoc
@satisfiessince 5.0.