Commit 8989cee
feat(kernel-utils): add described*() combinators for guard+schema authoring (#958)
## Explanation
Adds a `./described` export whose combinators author an `@endo/patterns`
interface guard and a matching `MethodSchema` from a single source, so a
discoverable exo's enforced shape and its `__getDescription__` hint
cannot
drift.
The combinator namespace is exported as `S` (mirroring
`@endo/patterns`'s `M`
and `@endo/eventual-send`'s `E`). `D` was avoided because it reads as
liveslots'
`D()` device operator to readers familiar with that codebase. `S` is the
single
authoring surface — there are no bare combinator function exports:
- Leaves:
`S.string`/`S.number`/`S.boolean`/`S.arrayOf`/`S.record`/`S.object`/`S.nothing`
each yield a `{ pattern, schema }` pair.
- `S.arg` names a positional parameter.
- `S.method` and `S.interface` assemble them into `{ guard, schema }` /
`{ interfaceGuard, schemas }`, ready to splat into
`makeDiscoverableExo`.
Because the enforced pattern and the descriptive schema are projected
from the
same authored leaves, their conformance is a construction invariant.
Method
guards use `M.callWhen(...).returns(...)` (exo methods are invoked
across an
eventual-send boundary) and the interface guard sets `defaultGuards:
'passable'`
so the `__getDescription__` method injected by `makeDiscoverableExo` is
allowed.
Optional arguments must be trailing (enforced by `S.method`).
### Before this PR
```ts
// guard and schema written by hand, independently — nothing keeps them in sync.
const interfaceGuard = M.interface('Math', {
add: M.callWhen(M.arrayOf(M.number())).returns(M.number()),
});
const schemas = {
add: {
description: 'Add a list of numbers.',
args: { summands: { type: 'array', items: { type: 'number' } } },
returns: { type: 'number' },
},
};
// Relax the guard to accept M.string() and the schema still advertises number[]:
// the membrane and the prompt now describe different methods, and nothing catches it.
```
### After this PR
```ts
// one source — the guard and the MethodSchema are projected from the
// same authored leaves, so they cannot drift.
const { interfaceGuard, schemas } = S.interface('Math', {
add: S.method(
'Add a list of numbers.',
[S.arg('summands', S.arrayOf(S.number()))],
S.number('The sum of the numbers.'),
),
});
```
## Test plan
- [x] `yarn workspace @metamask/kernel-utils test:dev:quiet` (304 pass,
incl. 13 new `described` tests)
- [x] `yarn workspace @metamask/kernel-utils build`
- [x] `yarn workspace @metamask/kernel-utils lint`
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Low Risk**
> Additive API and schema metadata; behavior change is limited to
optional-arg validation/conversion where `required` is set.
>
> **Overview**
> Adds **`@metamask/kernel-utils`** `./described` (and root **`S`**) so
discoverable exos can author **`@endo/patterns`** guards and
**`MethodSchema`** from one definition, avoiding drift between membrane
enforcement and **`__getDescription__`**.
>
> **`S`** exposes leaves (`string`, `number`, `boolean`, `arrayOf`,
`record`, `object`, `nothing`), **`S.arg`**, **`S.method`** (async
**`M.callWhen`**, trailing optional args), and **`S.interface`** (
**`defaultGuards: 'passable'`** for injected description).
**`MethodSchema`** gains optional **`required`**;
**`methodArgsToStruct`** honors it for Superstruct validation.
**`service-discovery-types`** **`methodSchemaToMethodSpec`** now marks
parameters not in **`required`** as **`optional`**.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
af20bba. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>1 parent f07e97d commit 8989cee
12 files changed
Lines changed: 597 additions & 6 deletions
File tree
- packages
- kernel-utils
- src
- service-discovery-types
- src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
12 | 14 | | |
13 | 15 | | |
14 | 16 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
62 | 72 | | |
63 | 73 | | |
64 | 74 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
0 commit comments