driverkit 0.0.0-stage → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. package/CONTROLS.md +578 -0
  2. package/REACT.md +477 -0
  3. package/README.md +104 -2
  4. package/dist/agent/backend.d.ts +69 -0
  5. package/dist/agent/backend.d.ts.map +1 -0
  6. package/dist/agent/backend.js +130 -0
  7. package/dist/agent/backend.js.map +1 -0
  8. package/dist/agent/control.d.ts +148 -0
  9. package/dist/agent/control.d.ts.map +1 -0
  10. package/dist/agent/control.js +342 -0
  11. package/dist/agent/control.js.map +1 -0
  12. package/dist/agent/index.d.ts +9 -0
  13. package/dist/agent/index.d.ts.map +1 -0
  14. package/dist/agent/index.js +6 -0
  15. package/dist/agent/index.js.map +1 -0
  16. package/dist/agent/loop.d.ts +136 -0
  17. package/dist/agent/loop.d.ts.map +1 -0
  18. package/dist/agent/loop.js +824 -0
  19. package/dist/agent/loop.js.map +1 -0
  20. package/dist/agent/render.d.ts +173 -0
  21. package/dist/agent/render.d.ts.map +1 -0
  22. package/dist/agent/render.js +425 -0
  23. package/dist/agent/render.js.map +1 -0
  24. package/dist/agent/tools.d.ts +138 -0
  25. package/dist/agent/tools.d.ts.map +1 -0
  26. package/dist/agent/tools.js +622 -0
  27. package/dist/agent/tools.js.map +1 -0
  28. package/dist/agent/types.d.ts +566 -0
  29. package/dist/agent/types.d.ts.map +1 -0
  30. package/dist/agent/types.js +32 -0
  31. package/dist/agent/types.js.map +1 -0
  32. package/dist/browser/budget.d.ts +92 -0
  33. package/dist/browser/budget.d.ts.map +1 -0
  34. package/dist/browser/budget.js +151 -0
  35. package/dist/browser/budget.js.map +1 -0
  36. package/dist/browser/connect.d.ts +368 -0
  37. package/dist/browser/connect.d.ts.map +1 -0
  38. package/dist/browser/connect.js +1038 -0
  39. package/dist/browser/connect.js.map +1 -0
  40. package/dist/browser/elsewhere.d.ts +89 -0
  41. package/dist/browser/elsewhere.d.ts.map +1 -0
  42. package/dist/browser/elsewhere.js +91 -0
  43. package/dist/browser/elsewhere.js.map +1 -0
  44. package/dist/browser/frames.d.ts +85 -0
  45. package/dist/browser/frames.d.ts.map +1 -0
  46. package/dist/browser/frames.js +11 -0
  47. package/dist/browser/frames.js.map +1 -0
  48. package/dist/browser/index.d.ts +8 -0
  49. package/dist/browser/index.d.ts.map +1 -0
  50. package/dist/browser/index.js +9 -0
  51. package/dist/browser/index.js.map +1 -0
  52. package/dist/check/check.d.ts +171 -0
  53. package/dist/check/check.d.ts.map +1 -0
  54. package/dist/check/check.js +666 -0
  55. package/dist/check/check.js.map +1 -0
  56. package/dist/check/index.d.ts +3 -0
  57. package/dist/check/index.d.ts.map +1 -0
  58. package/dist/check/index.js +2 -0
  59. package/dist/check/index.js.map +1 -0
  60. package/dist/check/types.d.ts +149 -0
  61. package/dist/check/types.d.ts.map +1 -0
  62. package/dist/check/types.js +29 -0
  63. package/dist/check/types.js.map +1 -0
  64. package/dist/controls/controls.d.ts +198 -0
  65. package/dist/controls/controls.d.ts.map +1 -0
  66. package/dist/controls/controls.js +742 -0
  67. package/dist/controls/controls.js.map +1 -0
  68. package/dist/controls/dom.d.ts +108 -0
  69. package/dist/controls/dom.d.ts.map +1 -0
  70. package/dist/controls/dom.js +56 -0
  71. package/dist/controls/dom.js.map +1 -0
  72. package/dist/controls/index.d.ts +26 -0
  73. package/dist/controls/index.d.ts.map +1 -0
  74. package/dist/controls/index.js +23 -0
  75. package/dist/controls/index.js.map +1 -0
  76. package/dist/controls/rows.d.ts +66 -0
  77. package/dist/controls/rows.d.ts.map +1 -0
  78. package/dist/controls/rows.js +127 -0
  79. package/dist/controls/rows.js.map +1 -0
  80. package/dist/core/index.d.ts +3 -0
  81. package/dist/core/index.d.ts.map +1 -0
  82. package/dist/core/index.js +15 -0
  83. package/dist/core/index.js.map +1 -0
  84. package/dist/core/registry.d.ts +192 -0
  85. package/dist/core/registry.d.ts.map +1 -0
  86. package/dist/core/registry.js +2799 -0
  87. package/dist/core/registry.js.map +1 -0
  88. package/dist/core/types.d.ts +2468 -0
  89. package/dist/core/types.d.ts.map +1 -0
  90. package/dist/core/types.js +19 -0
  91. package/dist/core/types.js.map +1 -0
  92. package/dist/otel/index.d.ts +122 -0
  93. package/dist/otel/index.d.ts.map +1 -0
  94. package/dist/otel/index.js +157 -0
  95. package/dist/otel/index.js.map +1 -0
  96. package/dist/react/index.d.ts +191 -0
  97. package/dist/react/index.d.ts.map +1 -0
  98. package/dist/react/index.js +547 -0
  99. package/dist/react/index.js.map +1 -0
  100. package/dist/react/runtime.d.ts +95 -0
  101. package/dist/react/runtime.d.ts.map +1 -0
  102. package/dist/react/runtime.js +54 -0
  103. package/dist/react/runtime.js.map +1 -0
  104. package/dist/react/store.d.ts +333 -0
  105. package/dist/react/store.d.ts.map +1 -0
  106. package/dist/react/store.js +695 -0
  107. package/dist/react/store.js.map +1 -0
  108. package/dist/relay/auth.d.ts +244 -0
  109. package/dist/relay/auth.d.ts.map +1 -0
  110. package/dist/relay/auth.js +391 -0
  111. package/dist/relay/auth.js.map +1 -0
  112. package/dist/relay/frames.d.ts +141 -0
  113. package/dist/relay/frames.d.ts.map +1 -0
  114. package/dist/relay/frames.js +20 -0
  115. package/dist/relay/frames.js.map +1 -0
  116. package/dist/relay/index.d.ts +8 -0
  117. package/dist/relay/index.d.ts.map +1 -0
  118. package/dist/relay/index.js +4 -0
  119. package/dist/relay/index.js.map +1 -0
  120. package/dist/relay/relay.d.ts +277 -0
  121. package/dist/relay/relay.d.ts.map +1 -0
  122. package/dist/relay/relay.js +1026 -0
  123. package/dist/relay/relay.js.map +1 -0
  124. package/dist/relay/serve.d.ts +33 -0
  125. package/dist/relay/serve.d.ts.map +1 -0
  126. package/dist/relay/serve.js +86 -0
  127. package/dist/relay/serve.js.map +1 -0
  128. package/dist/widget/chat.d.ts +290 -0
  129. package/dist/widget/chat.d.ts.map +1 -0
  130. package/dist/widget/chat.js +486 -0
  131. package/dist/widget/chat.js.map +1 -0
  132. package/dist/widget/css.d.ts +49 -0
  133. package/dist/widget/css.d.ts.map +1 -0
  134. package/dist/widget/css.js +532 -0
  135. package/dist/widget/css.js.map +1 -0
  136. package/dist/widget/dom.d.ts +143 -0
  137. package/dist/widget/dom.d.ts.map +1 -0
  138. package/dist/widget/dom.js +27 -0
  139. package/dist/widget/dom.js.map +1 -0
  140. package/dist/widget/highlight.d.ts +64 -0
  141. package/dist/widget/highlight.d.ts.map +1 -0
  142. package/dist/widget/highlight.js +136 -0
  143. package/dist/widget/highlight.js.map +1 -0
  144. package/dist/widget/index.d.ts +9 -0
  145. package/dist/widget/index.d.ts.map +1 -0
  146. package/dist/widget/index.js +20 -0
  147. package/dist/widget/index.js.map +1 -0
  148. package/dist/widget/position.d.ts +166 -0
  149. package/dist/widget/position.d.ts.map +1 -0
  150. package/dist/widget/position.js +497 -0
  151. package/dist/widget/position.js.map +1 -0
  152. package/dist/widget/settings.d.ts +116 -0
  153. package/dist/widget/settings.d.ts.map +1 -0
  154. package/dist/widget/settings.js +117 -0
  155. package/dist/widget/settings.js.map +1 -0
  156. package/dist/widget/widget.d.ts +442 -0
  157. package/dist/widget/widget.d.ts.map +1 -0
  158. package/dist/widget/widget.js +2005 -0
  159. package/dist/widget/widget.js.map +1 -0
  160. package/package.json +144 -4
@@ -0,0 +1,2468 @@
1
+ /**
2
+ * driverkit — the types.
3
+ *
4
+ * The whole library rests on a split that is worth stating before any code:
5
+ *
6
+ * A VERB is static. Its name, its arguments and what it refuses are the same
7
+ * for every user on every day, so they can be sent to a model once, cached in
8
+ * the prompt prefix, and never sent again.
9
+ *
10
+ * A SNAPSHOT is dynamic. It is what a human could see at this moment, and
11
+ * nothing more, so it is small by construction and goes in the tail where it
12
+ * is never cached.
13
+ *
14
+ * Keeping anything per-user or per-screen out of the verb list is not tidiness,
15
+ * it is the entire cost argument. One user-specific string in the static half
16
+ * busts the cache for everybody and the library stops being cheap to drive.
17
+ */
18
+ /** A value an agent can pass to a verb. Deliberately not `any`: if an argument
19
+ * cannot survive JSON it cannot cross the relay to an agent in another
20
+ * process, and a verb that only works in-page is a verb that works once. */
21
+ export type Scalar = string | number | boolean | null;
22
+ /** What a verb accepts. A tiny subset of JSON Schema, hand-rolled so the core
23
+ * package keeps its promise of zero runtime dependencies. */
24
+ export interface ArgSpec {
25
+ type: "string" | "number" | "boolean";
26
+ /** Shown to the model. Write it for a reader who cannot see your app. */
27
+ describe: string;
28
+ /** If present, the only accepted values. Cheaper and safer than prose. */
29
+ oneOf?: Scalar[];
30
+ /** Absent means required. */
31
+ optional?: boolean;
32
+ /**
33
+ * `true` when this argument's VALUE must never appear in an audit record.
34
+ *
35
+ * The one half of redaction the library cannot work out for itself. An
36
+ * argument is whatever the app declared — a row id, a filter, a company
37
+ * name, and on some app somewhere an API key or a one-time code — and only
38
+ * the author who typed the `describe` knows which. A library that guessed
39
+ * would guess by name, which is how `token` gets redacted and
40
+ * `otp_material` does not.
41
+ *
42
+ * So the app declares it and the CORE does the replacing, before the hook
43
+ * is called: `AuditEvent.args` carries `"[redacted]"` in this field's place
44
+ * and `AuditEvent.redacted` names the field, so a reader cannot mistake the
45
+ * marker for a string the agent actually sent. The alternative — hand the
46
+ * hook everything and let the host strip it — puts the secret on the far
47
+ * side of the one boundary where a record is most likely to leave the box,
48
+ * and makes the DANGEROUS case the one you get by doing the obvious thing.
49
+ * A host that wires `onAudit` straight to its tracer should not thereby
50
+ * ship a key to a vendor.
51
+ *
52
+ * It changes nothing else. The value reaches `run` untouched — this is a
53
+ * statement about the record, not about the call — and nothing renders it
54
+ * to the model.
55
+ *
56
+ * It IS in the manifest, and so in `fingerprint()`, like the rest of an
57
+ * `ArgSpec`. That is consistent rather than a leak: it is static, it is the
58
+ * same bytes for every user forever, and it carries no value — only the
59
+ * claim that this field has one worth hiding. An app that declares none
60
+ * hashes exactly what it hashed before this field existed.
61
+ */
62
+ sensitive?: true;
63
+ }
64
+ /**
65
+ * What a verb does to the world, declared in the cached half so an agent knows
66
+ * before it calls.
67
+ *
68
+ * `read` — changes no data and reports no count. Free to call, free to
69
+ * get wrong. It may still move what the person is looking at:
70
+ * navigation is a read.
71
+ * `describe` — changes what is SELECTED, changes no data, and reports a
72
+ * `count`. Also safe: the worst outcome is the wrong rows
73
+ * highlighted.
74
+ * `commit` — changes data. May be irreversible; see `Verb.reversible`.
75
+ *
76
+ * `read` used to promise "changes nothing", and that was false of the commonest
77
+ * verb a multi-screen app has. `go`, `open-contact`, `back` and `sort-by` move
78
+ * the person's screen out from under them — the most visible thing an agent can
79
+ * do to somebody who is watching — and the cached half was telling it that was
80
+ * free. The wording was fixed rather than a fourth kind added, because these
81
+ * three are a ladder of WHAT A WRONG CALL COSTS YOU, and a wrong navigation
82
+ * costs exactly what a wrong read costs: the person has to look somewhere else.
83
+ * The two promises worth making are the ones that decide whether the next call
84
+ * is safe — no data moved, and no count is left pending — so those are the two
85
+ * this now makes, and they are true of every navigation verb.
86
+ *
87
+ * `describe` no longer says "or FILTERED", and that was not a trim. By that
88
+ * sentence a search box is a `describe`, which is how `search` gets declared
89
+ * one; but the rest of what `describe` means — that it reports a `count`, that
90
+ * the count is what a following `commit` acts on, that the pair is the safety
91
+ * mechanism — is true only of selection. A verb that narrows a list and leaves
92
+ * nothing pending is a `read`.
93
+ *
94
+ * This exists because of one failure mode. Asked to "select the top 1000 rows
95
+ * and bulk accept", an agent that cannot tell `select` from `accept` has to
96
+ * discover the difference by doing it, and the discovery costs you a thousand
97
+ * accepted rows. A screenshot cannot tell it apart either, and neither can an
98
+ * OpenAPI spec, which says what a call takes but not what it costs you.
99
+ *
100
+ * The pairing rule that makes bulk work safe: a `describe` verb takes the
101
+ * predicate and returns the count, a `commit` verb takes NO predicate and acts
102
+ * on the current selection. Two calls, so a count is always seen before data
103
+ * moves. A verb that both selects and accepts is how fifty thousand rows get
104
+ * accepted because a filter was wider than anybody thought.
105
+ *
106
+ * TWO CALLS IS NOT ENOUGH ON ITS OWN, and that is what `Verb.consumes` is for.
107
+ * "Acts on the current selection" re-reads the selection at call time, so
108
+ * between the `describe` and the `commit` a person can tick a row, page the
109
+ * list, or a websocket can land — and then the count the agent read and the
110
+ * human approved is not provably the set the commit touched. Django has shipped
111
+ * that exact bug class since 2010, where `select_across` was silently lost and
112
+ * a confirmed bulk action ran on page one only (tickets 15742, 33083, and 36706
113
+ * closed wontfix in 2025). Chrome's WebMCP best practices state the requirement
114
+ * in one line: confirmation must recompute the current state and reject stale
115
+ * work. So a `commit` may declare `consumes: "plan"`, the `describe` hands back
116
+ * a token the core minted over the selection it left behind, and the commit has
117
+ * to present it. See `Verb.consumes`, `Snapshot.selectionId` and
118
+ * `CallOptions.plan`.
119
+ *
120
+ * `ask` IS THE FOURTH KIND, AND THIS FILE HAS ALREADY REFUSED ONE ONCE, so the
121
+ * difference is worth stating before the type. Navigation was refused a kind of
122
+ * its own on the grounds that the three are a ladder of WHAT A WRONG CALL COSTS
123
+ * YOU and a wrong navigation costs exactly what a wrong read costs. A wrong
124
+ * question is not on that ladder at all: it costs a PERSON'S ATTENTION, which
125
+ * is the one currency the other three never spend. `read` says "free to call,
126
+ * free to get wrong", and that sentence is false of a question — asking is
127
+ * budgeted (`Budget.asks`), it blocks every other call while it is open, and
128
+ * the cost of getting it wrong is paid by somebody who then trusts the next
129
+ * question less. `describe` reports a count about the app and `commit` moves the
130
+ * app's data; a question reads and writes nothing in the app. So it is neither
131
+ * `read` in the ladder's own terms nor a lie dressed as one.
132
+ *
133
+ * **ONLY THE CORE MAY MINT IT.** An app verb declaring `kind: "ask"` is refused
134
+ * at `mount()`. A question is put through `MountOptions.ask` so that the core
135
+ * owns the lock, the deadline, the allowance and the audit row; an app verb
136
+ * wearing the kind would be a question none of those applied to, and the first
137
+ * thing somebody would build with it is a confirmation dialog that answers for
138
+ * the approval gate. See `AskHook`.
139
+ */
140
+ export type VerbKind = "read" | "describe" | "commit" | "ask";
141
+ /**
142
+ * Something the app can do.
143
+ *
144
+ * `refuses` is the field people skip and the one that earns its place. A
145
+ * screenshot cannot tell an agent what a button will do, and neither can a
146
+ * name: `delete` is reachable on screen whether or not this particular row is
147
+ * deletable. Listing the refusals up front, in the half that gets cached, lets
148
+ * an agent avoid the call instead of discovering the answer by making it.
149
+ *
150
+ * On bulk work, note what a verb must NOT be. If the only way to select a
151
+ * thousand rows is a thousand `select(id)` calls, an agent will make a
152
+ * thousand round trips and lose its way somewhere in the middle. So a verb
153
+ * that operates on many rows takes a PREDICATE and resolves it inside the app,
154
+ * where the rows already are — `select(where, limit)`, never `select(id)`
155
+ * repeated. The agent should never learn an id it did not need.
156
+ */
157
+ export interface Verb<A extends Record<string, Scalar> = Record<string, Scalar>> {
158
+ /** Stable, kebab-case, unique within a mount. This is the agent's handle on
159
+ * it, so renaming one is a breaking change to every stored prompt. */
160
+ name: string;
161
+ /** One line, imperative, in the user's own vocabulary: "accept the selected
162
+ * contacts". Not "calls patchContacts" — the agent cannot see your code,
163
+ * and a name drawn from your internals makes it guess at your intent. */
164
+ describe: string;
165
+ /** What this does to the world. Optional, and an absent one makes NO safety
166
+ * claim in either direction: treating it as `read` would tell an agent an
167
+ * irreversible verb is free to try, and treating it as `commit` would mark
168
+ * every verb of an app written before this field as dangerous, which is a
169
+ * warning on everything and therefore a warning on nothing. Declare it. */
170
+ kind?: VerbKind;
171
+ /**
172
+ * A CSS selector naming the element this verb affects. Optional, purely
173
+ * presentational, and **never used by driverkit to find anything.**
174
+ *
175
+ * It exists for the human watching. An agent driving the screen a person is
176
+ * looking at has to be legible, and "about to accept the selection" reads
177
+ * better with the table it means outlined on the page. `at` is how a verb
178
+ * says which part of the app it is about, so a UI may draw an outline there
179
+ * before the call and clear it after.
180
+ *
181
+ * The library never queries it, never resolves it, never shows it to the
182
+ * model, and nothing about a verb's behaviour depends on it. That restraint
183
+ * is the field's whole design rather than an omission waiting to be filled
184
+ * in: driverkit's claim is that an app is driven through its own verbs and
185
+ * not by poking at its DOM, and an `at` that driverkit itself clicked would
186
+ * be a back door straight back to selector automation — brittle, invisible
187
+ * to the app, and broken by the next CSS refactor. So a wrong or stale `at`
188
+ * must cost nothing but an outline drawn in the wrong place. If a verb stops
189
+ * working when `at` is edited, something has started using it, and that is
190
+ * the bug.
191
+ *
192
+ * It is in `fingerprint()` all the same. Re-pointing a verb at a different
193
+ * element changes what the cached surface claims, and a UI holding the old
194
+ * copy would go on outlining the thing the verb no longer touches.
195
+ */
196
+ at?: string;
197
+ /**
198
+ * The screen this verb belongs to, or the screens it belongs to, matched
199
+ * against `Snapshot.screen`.
200
+ *
201
+ * Absent means the verb applies anywhere, which is the right answer for most
202
+ * of them: `select-contact` and `clear-selection` work from any screen, and
203
+ * an app with one screen never fills this in.
204
+ *
205
+ * **An array is for a verb that genuinely lives on more than one screen**,
206
+ * and it was added because a single string made the library's own worked
207
+ * example lie. `next-page` and `prev-page` page both the contacts list and a
208
+ * company's contact list; declaring either screen would have the demo refuse
209
+ * a paging call on a screen that was still publishing a `page: {index, size,
210
+ * total}` block and whose rendered snapshot still said "more rows exist off
211
+ * screen; use the app's own paging verb". Faced with writing that
212
+ * contradiction into the example, the app invented an `no-pages-here`
213
+ * refusal of its own instead — which is the smell this field exists to
214
+ * remove: an app hand-rolling a check and naming it, so that the next app
215
+ * names it something else and no agent recognises both.
216
+ *
217
+ * A single string behaves exactly as it always did. An EMPTY ARRAY is
218
+ * refused at `mount()`, the way a duplicate verb name is: it says the verb
219
+ * works nowhere, which no author means, and a verb that can never be called
220
+ * is permanently dead weight in everybody's cached prefix. Omit the field to
221
+ * mean "anywhere".
222
+ *
223
+ * **The tool list does not change with the screen, and must not.** A verb
224
+ * list that varied per screen would bust the prompt prefix on every single
225
+ * navigation, which is the cost argument the whole library is built on — so
226
+ * `next-page` is offered on the dashboard too, and the core refuses it there
227
+ * with the screen named. Declaring the mapping statically is what makes it
228
+ * cacheable: the names of the screens and which verb lives on which are the
229
+ * same for every user forever, and only *which screen is live* is dynamic.
230
+ *
231
+ * What this buys over an app's own guard is that the refusal is structural
232
+ * and says both halves of the answer. Before it, the app wrote the check by
233
+ * hand and invented a slug for it, and an agent had no way to learn which
234
+ * screen a verb wanted except by being refused once — the manifest was a
235
+ * flat list with no screen on any entry. Now the mapping is in the cached
236
+ * half, where a well-briefed agent reads it before calling, which is the
237
+ * same argument `refuses` is in there for.
238
+ *
239
+ * In `fingerprint()`, for the reason `kind` is: moving a verb to another
240
+ * screen changes what the cached surface claims about where it works, and so
241
+ * does giving it a second one.
242
+ */
243
+ screen?: string | string[];
244
+ /**
245
+ * `true` on a `commit` whose effect the app can undo.
246
+ *
247
+ * Read only for `kind: "commit"`, and absent makes no claim — exactly like
248
+ * `kind` itself, because the two wrong defaults cost the same two things:
249
+ * assuming reversible tells an agent a bulk accept can be taken back, and
250
+ * assuming irreversible marks every commit of an app written before this
251
+ * field as the dangerous kind, which is a warning on everything and
252
+ * therefore a warning on nothing.
253
+ *
254
+ * It exists because `set-setting("dense-rows", "true")` and accepting 3,412
255
+ * contacts were both `commit`, so both got the same shouted banner and both
256
+ * stopped to ask a person. Putting a page-size change behind a confirmation
257
+ * dialog is how people learn to click Yes without reading, which is the exact
258
+ * failure the gate exists to prevent. The ladder's third rung was carrying
259
+ * two different costs and only had words for the worse one.
260
+ *
261
+ * Reversible is not harmless: it still changes data and the person still
262
+ * sees it. It means there is a way back, not that nobody minds.
263
+ */
264
+ reversible?: true;
265
+ /**
266
+ * `"plan"` on a `commit` that may only be called with a token from the
267
+ * `describe` that counted what it is about to touch.
268
+ *
269
+ * THE HOLE THIS CLOSES. The pairing rule — a `describe` takes the predicate
270
+ * and reports a `count`, a `commit` takes no predicate and acts on the
271
+ * current selection — was advertised as "two calls, so a count is always seen
272
+ * before data moves". It was not that. `commit` re-reads "the current
273
+ * selection" at call time, and between the two calls the person can tick a
274
+ * row, page the list, or a websocket can land. So the count that was read,
275
+ * and approved, was not provably the set that moved. Django shipped exactly
276
+ * this for fifteen years: `select_across` silently lost, so a confirmed bulk
277
+ * action ran on page one only. Chrome's WebMCP best-practices page states the
278
+ * fix in one line — confirmation must recompute the current state and reject
279
+ * stale work — and recommends binding the transition to state version and
280
+ * exact scope, which is what a token here is.
281
+ *
282
+ * The model is Terraform's saved plan: applying one performs exactly the
283
+ * steps in that file, and a saved plan is valid only while the state has not
284
+ * changed. So a `describe` that leaves a selection behind gets a `plan` token
285
+ * back beside its `count` (see `Result.plan`), and a `commit` declaring this
286
+ * must be called with that token in `CallOptions.plan`. The core refuses as
287
+ * `stale-plan` when the token is missing, was never minted here, was taken at
288
+ * an older `rev`, or named a selection that is no longer the one on screen —
289
+ * and names both counts in the hint, the way `wrong-screen` names both
290
+ * screens.
291
+ *
292
+ * **A READ-ONLY MOVE IS NOT AN OLDER `rev`.** Navigation bumps `rev` and
293
+ * changes no data, so a plan is carried across a `kind: "read"` verb whose
294
+ * call left `Snapshot.selectionId` unchanged. Without that, "go back and
295
+ * confirm the number before you move 3,412 rows" cost a refusal and two
296
+ * recovery calls, and committing blind cost nothing — pricing the
297
+ * double-check above the blind commit, which is the opposite of what this
298
+ * field is for. An app publishing no `selectionId` carries nothing and keeps
299
+ * the behaviour it had.
300
+ *
301
+ * **The app does not mint the token and cannot**, which is the point of the
302
+ * field being this small. An app that computed its own would have to get a
303
+ * hash right, and a token an app can forge or accidentally reuse is worse
304
+ * than no token because it still reads as a guarantee. All the app does is
305
+ * publish `Snapshot.selectionId`; the core does the binding.
306
+ *
307
+ * Static, and in `fingerprint()` for the same reason `kind` is: a verb that
308
+ * has started requiring a token has changed what a cached manifest claims
309
+ * about how it may be called, and an agent holding the old copy would go on
310
+ * calling it bare and being refused. An absent one makes NO claim and
311
+ * changes nothing — a commit written before this field behaves exactly as it
312
+ * did, which is why this is not the default. A `read` or a `describe` that
313
+ * declares it is an authoring mistake and is refused at `mount()`: a token
314
+ * is a claim about data that is about to move, and demanding one for a
315
+ * highlight teaches an agent to fetch tokens it does not need.
316
+ *
317
+ * **ALL OR NONE, AND THIS IS THE RULE THE FIELD IS USELESS WITHOUT.** If one
318
+ * `commit` that acts on the selection consumes a plan, every commit that acts
319
+ * on the same selection must, or the gate is advisory. This is not a style
320
+ * note. An eval asked the worked example for a job it cannot do — "archive
321
+ * everyone who has not replied in six months" — and the agent improvised:
322
+ * selected a company, then called the one bulk commit that asked for no
323
+ * receipt, and 403 rows changed with no count read and nothing gated. The
324
+ * verb an agent reaches for when it is off its intended path is the
325
+ * least-declared one, which is exactly when the receipt matters most, so a
326
+ * guarantee is only as strong as its weakest sibling. `mount()` warns when it
327
+ * can see this shape; the warning cannot see every case, and the rule is the
328
+ * thing to remember.
329
+ *
330
+ * What does NOT need one: a commit that names what it acts on. A verb that
331
+ * takes an id, or that changes the one record whose screen is open, is acting
332
+ * on something the person can see and count, and the count is 1. A plan is
333
+ * the receipt for a count LARGER THAN THE SCREEN, which is the only case
334
+ * where a number has to be carried between two calls to mean anything.
335
+ */
336
+ consumes?: "plan";
337
+ args?: Record<string, ArgSpec>;
338
+ /**
339
+ * The refusals this verb can return, as short stable slugs.
340
+ *
341
+ * Documentation for the model, and the list a `refuse()` is checked against:
342
+ * declaring it opts this verb into the check, and a refusal with a slug that
343
+ * is not on the list warns once (see `RESERVED_REFUSALS` and `mount`).
344
+ * Leaving it absent makes no claim and is not checked, the same way an absent
345
+ * `kind` makes no safety claim.
346
+ *
347
+ * None of these may be a slug from `RESERVED_REFUSALS`. An app that refuses
348
+ * with `timeout` has taken away an agent's only way to tell "the app said no"
349
+ * from "your call never landed".
350
+ */
351
+ refuses?: string[];
352
+ /**
353
+ * Do the thing.
354
+ *
355
+ * Return nothing for the ordinary case. Return `ctx.refuse(...)` for an
356
+ * expected "no". Return a `Report` when the verb has a number or a sentence
357
+ * the snapshot cannot carry — above all a `describe` verb's `count`, which is
358
+ * the number the person would have had to see before pressing a bulk button.
359
+ *
360
+ * Throwing is a bug, not a "no".
361
+ */
362
+ run: (args: A, ctx: VerbContext) => void | Refusal | Report | Promise<void | Refusal | Report>;
363
+ }
364
+ /**
365
+ * What a verb has to say about what it just did, beyond the screen.
366
+ *
367
+ * This exists for `count`. A `describe` verb resolves a predicate inside the
368
+ * app — "every contact at Acme" — and the only thing that makes the following
369
+ * `commit` safe is the person or the agent seeing how many rows that matched
370
+ * BEFORE anything irreversible runs. The screen cannot carry it: the whole
371
+ * point is that the match is larger than the window, so `rows` shows twenty of
372
+ * three thousand and `page.total` is about the table, not about the selection.
373
+ *
374
+ * Without this the pairing rule is advice. With it, it is a number.
375
+ */
376
+ export interface Report {
377
+ /** How many rows the verb matched or selected. `0` is a real answer and is
378
+ * distinct from omitting the field, which means "I do not count". */
379
+ count?: number;
380
+ /** One short line for the agent that the snapshot has nowhere to put. */
381
+ note?: string;
382
+ }
383
+ /**
384
+ * WHY THERE IS NO `plan` FIELD HERE, since this is the first place anybody
385
+ * looks for one.
386
+ *
387
+ * A plan token has to be checkable at the moment the `commit` runs, and the
388
+ * only thing the core can read then is `visible()`. A verb returns its `Report`
389
+ * once, at the end of the `describe`, and is never asked again — so an identity
390
+ * handed back here could be bound into a token and never re-derived, which
391
+ * makes the check "is this the token I minted" and not "is this still the
392
+ * selection I counted". That is the difference between a receipt and a
393
+ * guarantee, and a token that only proves its own provenance is the theatre
394
+ * this whole mechanism exists to avoid.
395
+ *
396
+ * So the identity lives in `Snapshot.selectionId`, which the core reads after
397
+ * the `describe` and again before the `commit`, and the app states it in one
398
+ * place instead of two. Two places is the real cost of the alternative: an app
399
+ * that computed the identity once for the report and once for the screen would
400
+ * eventually compute them differently, and the token would be minted over one
401
+ * and checked against the other.
402
+ */
403
+ export interface VerbContext {
404
+ /** Decline, with a reason the agent can act on. See `refuse`. */
405
+ refuse: (reason: string, hint?: string) => Refusal;
406
+ }
407
+ /** The marker returned by `ctx.refuse`. Not an Error: a refusal is an ordinary
408
+ * outcome the agent is expected to handle, and making it an exception pushes
409
+ * app authors toward try/catch around their own business rules. */
410
+ export interface Refusal {
411
+ readonly __driverkit: "refusal";
412
+ reason: string;
413
+ hint?: string;
414
+ }
415
+ /**
416
+ * One control the screen is offering right now.
417
+ *
418
+ * The dynamic half of the controls layer, and the split is the whole reason
419
+ * this type is so small. *That an app can press a control* is static and lives
420
+ * in the manifest as a verb. *Which controls are on this screen at this
421
+ * moment* is as per-screen as a row is, so it belongs here, in the half that is
422
+ * never cached. Listing the control names in a verb's `ArgSpec.oneOf` is the
423
+ * tempting mistake: it reads as helpful typing, and it moves a per-screen list
424
+ * into the cached prefix — busting it for every user on every navigation.
425
+ *
426
+ * Every field but `name` is omitted when it would state the common case, which
427
+ * is why `field` and `disabled` are `true` rather than `boolean`. A screen with
428
+ * eight controls should cost a line, not a paragraph.
429
+ *
430
+ * A per-row control is listed ONCE, with `per: "row"`, and addressed by row id.
431
+ * Twenty rows with six controls each is six entries here, not a hundred and
432
+ * twenty: a snapshot that grew with the row count would undo the cost argument
433
+ * the rest of this file is written to protect.
434
+ *
435
+ * Note what is absent. There is no selector, no path, no coordinates and no
436
+ * handle this library derived by looking at the page. `name` is a word the
437
+ * app's author typed next to their own button, which is the same claim `Verb`
438
+ * makes and the reason neither one rots when the CSS does. `Verb.at` is the
439
+ * one selector in the library and it is presentational — never used to find
440
+ * anything — so it is deliberately not mirrored here.
441
+ */
442
+ export interface ScreenControl {
443
+ /** The name the author marked on the element. What a control verb takes,
444
+ * matched exactly. */
445
+ name: string;
446
+ /** One short human label — the control's own text, usually. A model matches
447
+ * the user's words against this; `name` is only the handle. */
448
+ label?: string;
449
+ /** What using it does to the world, as the author declared it.
450
+ *
451
+ * **Absent means the author did not say**, which is not `read` and must
452
+ * never be rendered or treated as one. It is an unreviewed button, and the
453
+ * only honest thing to do with it is to assume the strongest claim. See
454
+ * `packages/adapters/CONTROLS.md`. */
455
+ kind?: VerbKind;
456
+ /** `"row"` when the control appears on each row in `rows` and is addressed by
457
+ * row id. Omitted for a control that belongs to the screen itself. */
458
+ per?: "row";
459
+ /** Present only when the control takes text. */
460
+ field?: true;
461
+ /** Present only when the control is on screen and not actionable. Listed
462
+ * rather than hidden: the person can see the greyed-out button, so an agent
463
+ * shown nothing would conclude the feature does not exist instead of looking
464
+ * for the precondition that enables it. */
465
+ disabled?: true;
466
+ }
467
+ /** One row of whatever the screen is showing. `id` is what verbs take. */
468
+ export interface Row {
469
+ id: string;
470
+ [column: string]: Scalar | undefined;
471
+ }
472
+ /**
473
+ * What a human could see right now.
474
+ *
475
+ * The rule this type exists to enforce: an agent is never shown more than the
476
+ * person at the keyboard. Same filters, same sort, same page. That is what
477
+ * keeps the cost flat as the data grows — a hundred rows on screen is a
478
+ * hundred rows in the prompt whether the table holds a hundred or a million —
479
+ * and it is also what makes "park that one" resolvable without a query.
480
+ */
481
+ export interface Snapshot {
482
+ /** Where the user is, in their words: "contacts, filtered to Acme". */
483
+ where: string;
484
+ /**
485
+ * Which screen that is, as a stable kebab-case id: `contacts`, `company`.
486
+ *
487
+ * `where` is prose for a human and carries everything else the person can
488
+ * see — the filter, the sort, and on a drill-down a sentence about the record
489
+ * they opened — so it changes constantly for reasons that are not navigation,
490
+ * and "did I navigate?" cannot be answered by diffing it. This can be
491
+ * compared with `===`, and that is its whole job.
492
+ *
493
+ * Dynamic, which is why it is here and not in the manifest: the *names* of
494
+ * an app's screens and which verb belongs to which are static and go in
495
+ * `Verb.screen`; which one is live right now is as per-turn as a row is.
496
+ *
497
+ * Absent means the app does not describe its screens, NOT that it has one.
498
+ * An app that leaves it out keeps every behaviour it had: the core cannot
499
+ * check a `Verb.screen` or a `CallOptions.screen` against nothing, and makes
500
+ * no claim rather than inventing one.
501
+ */
502
+ screen?: string;
503
+ /**
504
+ * Monotonic, bumped on every change. An agent echoes it back to prove it is
505
+ * acting on what it saw; see `stale` in PROTOCOL.md.
506
+ *
507
+ * It means "the snapshot changed", which is a wider thing than "the data
508
+ * changed": a read-only navigation genuinely changes the snapshot, because
509
+ * the person is looking somewhere else. That is right for `stale` -- the
510
+ * question there is "is what you were looking at still what is on screen?"
511
+ * -- and it is why a plan token does NOT turn on this number alone. A plan
512
+ * asks a narrower question, "is the selection you counted still the
513
+ * selection", and navigation does not touch it. See `selectionId`, and
514
+ * `plansGoodAt` in the registry for what one number answering two questions
515
+ * cost.
516
+ */
517
+ rev: number;
518
+ /** Only the rows actually on screen. */
519
+ rows?: Row[];
520
+ /**
521
+ * Present when `rows` is a window onto something larger.
522
+ *
523
+ * Three states, and the third exists because without it the type forces a
524
+ * lie. **Absence is a claim that the agent is seeing everything** — so an
525
+ * adapter reading a rendered table, which cannot tell a virtualised grid
526
+ * from a short list, had no honest option: omit `page` and assert
527
+ * completeness it cannot know, or invent a total. Either way an agent
528
+ * eventually concludes a record does not exist because it could not see it.
529
+ *
530
+ * page absent these are all the rows there are
531
+ * page, total given a window, and you know how big the whole is
532
+ * page, total absent a window, and you do NOT know how big the whole is
533
+ *
534
+ * The third is the weakest claim and the right default whenever you are
535
+ * reading the DOM rather than a store. An agent that needs certainty from it
536
+ * must widen the window or use a `describe` verb that counts server-side.
537
+ */
538
+ page?: {
539
+ index: number;
540
+ size: number;
541
+ total?: number;
542
+ };
543
+ /**
544
+ * Row ids the user has selected, in selection order. Resolves "this one".
545
+ *
546
+ * **Only the ids on screen belong here**, exactly like `rows`, and for the
547
+ * same reason. `select-where` exists to select thousands of rows in one call,
548
+ * so a driver that honestly fills this with every selected id puts tens of
549
+ * kilobytes of ids into the UNCACHED half of every result from then on — paid
550
+ * for on every turn, and on a big table straight through the relay's 1 MiB
551
+ * body limit. An author who fills it with ten thousand ids has silently
552
+ * broken their own token budget, which is the whole cost argument this type
553
+ * exists to protect.
554
+ *
555
+ * So this field answers "which of the things in front of me is 'this one'",
556
+ * and `selectedCount` answers "how big is the selection". Both are needed:
557
+ * an agent cannot resolve a pronoun from a number, and cannot decide whether
558
+ * a commit is safe from a truncated list.
559
+ */
560
+ selected?: string[];
561
+ /**
562
+ * How many rows are selected in all, including the ones off screen.
563
+ *
564
+ * The honest size of the selection, which `selected` deliberately does not
565
+ * carry. Omitting it means "I do not count" — distinct from `0`, which is a
566
+ * real answer — and when it is larger than `selected.length` the renderer
567
+ * says so in words, because a model handed three ids and a bulk commit will
568
+ * otherwise believe three rows are about to change.
569
+ *
570
+ * It is not a substitute for a `describe` verb's `Report.count`. That one is
571
+ * the number a commit decision turns on at the moment it is made; this one is
572
+ * a property of the screen, and is what makes a selection made several turns
573
+ * ago still legible.
574
+ */
575
+ selectedCount?: number;
576
+ /**
577
+ * An opaque fingerprint of the WHOLE current selection, off-screen rows
578
+ * included. The one input the plan token is built from.
579
+ *
580
+ * WHAT IT HAS TO BE. Equal when the selection has not changed, different when
581
+ * it has — including when one row has been swapped for another, which is the
582
+ * case a count cannot see. Nothing else is required of it: the core never
583
+ * parses it, never orders it and never shows it to a model, so an app may use
584
+ * any string that has that one property. `selectionId([...ids])` from this
585
+ * package is the one to use unless you have a reason; it is order-independent
586
+ * and cheap, and the reason it is exported is that an app computing its own
587
+ * is an app that can get it wrong.
588
+ *
589
+ * WHY NOT THE TWO FIELDS ABOVE, which look as though they would do. `selected`
590
+ * is by design only the ids on screen, so it cannot identify the 3,412-row
591
+ * selection that `select-where` exists to make — and filling it honestly with
592
+ * all of them is the token-budget mistake its own comment is about.
593
+ * `selectedCount` is a number, and a number cannot tell a selection from a
594
+ * different selection of the same size. Each would have produced a token that
595
+ * looked like a guarantee and was not.
596
+ *
597
+ * It is one short string whatever the selection's size, which is why it can
598
+ * be in the uncached half at all. An app with a large selection should keep a
599
+ * running value rather than re-hash a large set inside `visible()`, which
600
+ * runs on every call.
601
+ *
602
+ * IT IS ALSO WHAT MAKES LOOKING FREE. A plan token is refused when `rev` has
603
+ * moved, and `rev` moves on a read-only navigation — so an agent that went
604
+ * back to check its premise before an irreversible commit was refused, while
605
+ * one that committed blind was not. The core now carries a plan across a
606
+ * `kind: "read"` verb that moved `rev` **when this string is unchanged
607
+ * across the call**, because that is the app saying the selection is the same
608
+ * selection. This field is the whole of that permission.
609
+ *
610
+ * Absent means the app does not identify its selection — NOT that nothing is
611
+ * selected. A `commit` that declares `consumes: "plan"` in an app that omits
612
+ * this still gets a token, and the token still binds the `rev` and the count,
613
+ * which catches every change the app bumps its `rev` for. It cannot catch a
614
+ * change the app made without bumping `rev`, and the core says so once at the
615
+ * first such call rather than letting the weaker check pass for the stronger
616
+ * one. It also carries nothing across a navigation: an app that publishes no
617
+ * identity keeps exactly the expiry it has always had, which is the fail-safe
618
+ * direction, and the reason the permission above is hung on a field an app
619
+ * either publishes or does not rather than on a counter an author has to
620
+ * remember to bump.
621
+ */
622
+ selectionId?: string;
623
+ /**
624
+ * The controls the screen is offering right now.
625
+ *
626
+ * The second layer: `rows` is what the person can read, this is what they
627
+ * could press. It is here rather than in the manifest for the same reason
628
+ * `rows` is — which buttons exist changes with every navigation, and a verb
629
+ * list that changed with the screen would be a prompt prefix that is never
630
+ * cached twice. See `ScreenControl` and `packages/adapters/CONTROLS.md`.
631
+ *
632
+ * Absent means this screen does not describe its controls, NOT that it has
633
+ * none. An app driven entirely by hand-written verbs never fills this in and
634
+ * is no less drivable for it.
635
+ */
636
+ controls?: ScreenControl[];
637
+ /** Anything else the agent needs and the rows cannot carry. Keep it short;
638
+ * this is the uncached half. */
639
+ note?: string;
640
+ }
641
+ /**
642
+ * What the agent gets back. Carries the snapshot AFTER the call, so acting and
643
+ * observing are one round trip rather than two. With ten steps that is ten
644
+ * saved model turns, each of which would have re-read the whole prefix — the
645
+ * round trips, not the manifest, are what actually cost money.
646
+ *
647
+ * `count` is how a `describe` verb reports what it matched: "1,000 selected of
648
+ * 3,412 matching". It is the number a human would have to see before pressing
649
+ * a bulk button, and the reason `describe` and `commit` are separate verbs.
650
+ *
651
+ * A driver mounted in a page ALWAYS fills `snapshot`. That guarantee is worth
652
+ * keeping in the type, so in-page callers never check for it. See `WireResult`
653
+ * for the one case where it can be missing.
654
+ */
655
+ export type Result = {
656
+ ok: true;
657
+ note?: string;
658
+ count?: number;
659
+ /**
660
+ * The plan token for the selection this call left behind, minted by the
661
+ * core on a `describe` that reported a `count`. Present only there.
662
+ *
663
+ * It travels beside `count` because it means nothing without it: the
664
+ * count is the number a person approves and this is what binds that
665
+ * number to the set the next `commit` will touch. Pass it back as
666
+ * `CallOptions.plan`. Opaque — a client stores it and forwards it, and
667
+ * nothing outside the core that minted it reads anything out of it.
668
+ */
669
+ plan?: string;
670
+ snapshot: Snapshot;
671
+ } | {
672
+ ok: false;
673
+ refused: string;
674
+ hint?: string;
675
+ snapshot: Snapshot;
676
+ };
677
+ /**
678
+ * What an agent receives over the relay: a `Result`, or a refusal from the
679
+ * relay itself about a call that never reached a tab.
680
+ *
681
+ * The distinction is not pedantry. A relay can refuse because the tab id is
682
+ * unknown, because the tab closed, or because the tab accepted the call and
683
+ * went silent past the deadline. In none of those cases is there a screen to
684
+ * report, and the alternatives are both worse than a missing field: inventing
685
+ * an empty snapshot tells an agent the app has no rows, and returning a
686
+ * different shape makes every client parse two shapes and handle the rarer one
687
+ * badly.
688
+ *
689
+ * So the shape holds and `snapshot` goes missing, which is the truthful thing
690
+ * to say — nothing ran and nobody looked. A client must read its absence as
691
+ * "the call did not arrive", never as "the screen is empty".
692
+ */
693
+ export type WireResult = Result | {
694
+ ok: false;
695
+ refused: RelayRefusal;
696
+ hint?: string;
697
+ snapshot?: undefined;
698
+ };
699
+ /** Refusals that come from the relay rather than from an app's verb. Listed as
700
+ * a closed set so a client can tell "your app said no" (retry differently)
701
+ * from "your call never landed" (reconnect, or pick another tab). */
702
+ export type RelayRefusal = "no-such-tab" | "tab-went-away" | "timeout";
703
+ /**
704
+ * One screen the app has, declared once in the cached half.
705
+ *
706
+ * THE GAP THIS CLOSES. `Snapshot.screen` says which screen is live and
707
+ * `Verb.screen` says which screen a verb lives on, and between them an agent
708
+ * could learn where it is and be refused where it is not — but it could not
709
+ * learn that a screen EXISTS before standing on it. The library's own worked
710
+ * example papered over that by pasting a prose screen-map into
711
+ * `instructions` ("this is a CRM with six screens: a dashboard, a contacts
712
+ * list, …"), which is a map written twice: once in the route table the app
713
+ * already has, and once in English for the model, where nothing checks that
714
+ * the two agree. So this is the map, as data, in the half that is cached.
715
+ *
716
+ * It is static for the same reason `Verb.screen` is, and the argument is the
717
+ * one the whole library rests on: **the names of an app's screens are the same
718
+ * for every user forever.** A CRM has a contacts list whether or not you have
719
+ * any contacts, and whether or not you are allowed to see them. Only *which
720
+ * screen is live* is per-user and per-minute, and that is `Snapshot.screen`.
721
+ *
722
+ * **THE CONSTRAINT THAT MATTERS, and the one an author will be tempted to
723
+ * break: publish every screen the app has, not the screens THIS user can
724
+ * reach.** A list filtered by role, by plan, by feature flag or by permission
725
+ * is a per-user list, and a per-user list in the cached half busts the prompt
726
+ * prefix for everybody — exactly as a per-screen tool list would, which is the
727
+ * mistake `Verb.screen` exists to prevent one level down. Two users of the
728
+ * same app must receive the same bytes here. If a screen is one this person
729
+ * may not open, that is a REFUSAL from the navigate hook at the moment they
730
+ * try, with a reason an agent can relay ("admin only"), and not an absence
731
+ * from everybody's map: an absence is unfalsifiable, so an agent meeting it
732
+ * concludes the feature does not exist and stops asking — the same failure
733
+ * `ScreenControl.disabled` is listed rather than hidden to avoid.
734
+ */
735
+ export interface ScreenSpec {
736
+ /**
737
+ * Stable, kebab-case, unique within a mount. **The same string
738
+ * `Verb.screen` and `Snapshot.screen` compare**, which is the whole reason
739
+ * this type has an id rather than only a name: three fields that spell the
740
+ * screen three ways are three fields that disagree after the first rename.
741
+ */
742
+ id: string;
743
+ /** One line for the model, in the user's vocabulary: "the contacts list:
744
+ * every contact, searchable, twenty rows at a time". Write it for a reader
745
+ * who cannot see your app and has never been to this screen — which is
746
+ * precisely the reader this field exists for. At most
747
+ * `SIZE_LIMITS.describe` characters; see that constant for why there is a
748
+ * bound at all. */
749
+ describe: string;
750
+ /**
751
+ * An **RFC 6570** URI template for the screen, if it has an address:
752
+ * `"#/contacts"`, `"#/companies/{slug}"`.
753
+ *
754
+ * RFC 6570 rather than a placeholder syntax of our own, because a route
755
+ * table with holes in it is a solved problem with a registered media type
756
+ * and a dozen implementations, and the alternative is a third dialect of
757
+ * `:id` versus `{id}` versus `<id>` for every author to get wrong. Only
758
+ * simple string expansion — `{var}` — is used by the core, which is the
759
+ * level a route has; the syntax is the spec's so a template can be handed to
760
+ * `uri-templates` or any other RFC 6570 library unchanged, and the core's
761
+ * own expansion is tested against one.
762
+ *
763
+ * OPTIONAL, and absent is not a defect. A modal, a wizard step, a tab inside
764
+ * a screen and the whole of a single-page app with no router are all screens
765
+ * with no address, and a type that demanded one would push those authors
766
+ * into inventing fake URLs. `goto` reaches an address-less screen exactly as
767
+ * it reaches any other: the ID is the handle an agent uses, and the url is
768
+ * what the app can use to get there, plus the one thing a person can paste
769
+ * into a browser.
770
+ */
771
+ url?: string;
772
+ /**
773
+ * What the url template takes, by the template's own variable names.
774
+ *
775
+ * Typed, so the core can check `goto("company", { slug: 7 })` before any
776
+ * navigation happens and refuse it as `bad-argument` the way every other
777
+ * mistyped argument is refused — rather than expanding `7` into a URL and
778
+ * landing the person on a screen with no record on it. `ArgSpec` rather than
779
+ * a list of names for the reason verbs take `ArgSpec`: `oneOf` is cheaper
780
+ * and safer than prose, and the refusal lists the accepted set.
781
+ *
782
+ * A screen whose template has a variable with no entry here still expands —
783
+ * the value is taken as optional and an absent one expands to nothing, which
784
+ * is RFC 6570's own rule — but it is undeclared, so nothing checks it and
785
+ * the model is told nothing about it. Declare them.
786
+ */
787
+ args?: Record<string, ArgSpec>;
788
+ }
789
+ /**
790
+ * Where a `goto` is trying to get to, handed to `MountOptions.navigate`.
791
+ *
792
+ * WHY THERE IS A HOOK AT ALL. The core cannot navigate. It has no router, no
793
+ * `history`, no `location` and no DOM — it is the package that promises to
794
+ * import nothing — and a core that pushed a URL would be guessing between
795
+ * `pushState`, a hash, a framework's own router and a native app with no URLs
796
+ * at all. It would also be the one place in driverkit that reached around the
797
+ * app instead of through it, which is the same back door `Verb.at` is
798
+ * deliberately not allowed to be. So the app supplies the one function it
799
+ * alone can write, and the core supplies everything that is the same in every
800
+ * app: the declared list, the uniqueness check, the argument types, the
801
+ * expansion, the refusal when the screen is not one the app has, and the tool
802
+ * description the model reads.
803
+ */
804
+ export interface NavigateTarget {
805
+ /** The `ScreenSpec.id` that was asked for. Already checked against the
806
+ * declared list, so a hook never sees a screen the app did not declare. */
807
+ screen: string;
808
+ /** The arguments, already checked against the screen's `args`. A hook may
809
+ * read them directly rather than re-parsing the url. */
810
+ args: Record<string, Scalar>;
811
+ /** The screen's `url` with `args` expanded, or absent when the screen
812
+ * declared no template. A hook that has a router usually ignores this and
813
+ * routes on `screen`; a hook on an app whose only addressing is the address
814
+ * bar assigns it to `location.hash` and is done. */
815
+ url?: string;
816
+ }
817
+ /**
818
+ * How the app navigates, for the core's reserved `goto` verb.
819
+ *
820
+ * Returns nothing for the ordinary case, `ctx.refuse(...)` for a screen this
821
+ * person may not open or a record that does not exist, or a `Report` with a
822
+ * `note`. It is the same contract as `Verb.run` on purpose: `goto` is an
823
+ * ordinary verb whose body happens to be supplied through a different field,
824
+ * and an author who already knows how to write a verb has nothing new to
825
+ * learn.
826
+ *
827
+ * **A refusal here is how a per-user restriction is expressed**, and it is the
828
+ * other half of `ScreenSpec`'s rule against filtering the declared list. "You
829
+ * may not open the billing screen" is a fact about this person at this moment,
830
+ * so it belongs in the dynamic half — said at the moment of the call, with a
831
+ * reason — and never as a screen quietly missing from everybody's map.
832
+ */
833
+ export type NavigateHook = (target: NavigateTarget, ctx: VerbContext) => void | Refusal | Report | Promise<void | Refusal | Report>;
834
+ /**
835
+ * What the agent wants to know, handed to `MountOptions.ask`.
836
+ *
837
+ * One field today and a shape rather than a bare string all the same, because
838
+ * the hook is implemented by a UI and a UI that took a positional argument
839
+ * would have to be rewritten to be told anything else — `screen`, the verb that
840
+ * prompted it, a suggested set of answers. A `{ question }` is the version of
841
+ * this that can grow without every implementation changing.
842
+ */
843
+ export interface AskQuestion {
844
+ /**
845
+ * What the agent is asking, in its own words, as it typed it.
846
+ *
847
+ * Never built by the core and never rewritten by it: a question the library
848
+ * paraphrased is a question the person answers and the model did not ask.
849
+ * Bounded at `SIZE_LIMITS.describe` and refused as `bad-argument` above
850
+ * that — an agent that needs a paragraph to state its question has not
851
+ * worked out what it does not know, and a wall of text in front of somebody
852
+ * is a question they will dismiss.
853
+ */
854
+ question: string;
855
+ }
856
+ /**
857
+ * What a person said, or the news that nobody did.
858
+ *
859
+ * THREE ANSWERS, AND THE SPLIT IS THE WHOLE DISCIPLINE OF THIS TYPE. The
860
+ * library already keeps `approval-timeout` apart from `declined-by-user`
861
+ * because "nobody answered" and "the person said no" are different facts an
862
+ * auditor must never confuse. A question has the same hazard one layer over,
863
+ * with a different pair:
864
+ *
865
+ * a string the person's words. They reach the model as the
866
+ * RESULT of the ask, not as a side channel — see
867
+ * `AskHook` — so a model reads an answer where it reads
868
+ * every other outcome.
869
+ * `{ answered: false }` a question was put in front of somebody and no answer
870
+ * came back: they closed the panel, walked away, or the
871
+ * deadline lapsed. The agent is told plainly that the
872
+ * question was seen by nobody who replied, and that
873
+ * nothing in the app changed.
874
+ * `{ nobody: true }` there is no person on this path at all. A cron run, a
875
+ * test harness, a tab with no panel. The question was
876
+ * never put.
877
+ *
878
+ * `{ answered: false }` AND `{ nobody: true }` ARE NOT ONE ANSWER, and the
879
+ * repairs are why. Unanswered says the channel works and this particular
880
+ * question went unanswered, so asking once more later, or more briefly, is a
881
+ * move that can succeed. Nobody-to-ask says the channel does not exist on this
882
+ * run, so every further question is wasted and the only honest ending is to
883
+ * decide from what is already known or to stop and say what is missing. An
884
+ * agent handed one word for both will keep asking into an empty room.
885
+ *
886
+ * There is deliberately NO "the person refused to answer". Somebody who says "I
887
+ * would rather not say" has answered; their words are the result, and the model
888
+ * is better at reading that sentence than the core is at classifying it.
889
+ *
890
+ * A HOOK THAT THROWS IS `{ answered: false }`, never `{ nobody: true }`. The
891
+ * precedent is `ApprovalHook`, where a failed ask is a no — here a failed ask is
892
+ * an unanswered question, because a broken question UI is not evidence that
893
+ * nobody is sitting there, and claiming it is would tell an agent to stop asking
894
+ * for the rest of a run over one exception.
895
+ *
896
+ * An answer that is none of these three is treated as `{ answered: false }` and
897
+ * said out loud once. The same rule as `Approval`'s unknown shape being a
898
+ * denial: the core must not read a shape it does not recognise as somebody
899
+ * having spoken.
900
+ */
901
+ export type AskAnswer = string | {
902
+ answered: false;
903
+ } | {
904
+ nobody: true;
905
+ };
906
+ /**
907
+ * How the app puts a question to the person. The body of the reserved `ask`
908
+ * verb.
909
+ *
910
+ * WHY THERE IS A HOOK AT ALL, which is the same answer `NavigateHook` gives:
911
+ * the core cannot ask. It has no DOM, no panel, no notification channel and no
912
+ * idea whether anybody is looking. What it CAN own — and does — is everything
913
+ * that is the same in every app: the verb's name, its one argument, its
914
+ * description, the lock that stops the agent acting while a question is open,
915
+ * the deadline that keeps "nobody answered" a FACT rather than a transport
916
+ * mystery, the allowance that stops an agent asking instead of thinking, and
917
+ * the audit row.
918
+ *
919
+ * **DECLARED AT `mount()` AND NOWHERE ELSE, AND THIS IS NOT A CONVENIENCE.**
920
+ * The approval gate has `DriverApproval.adopt`, so a panel mounted after the
921
+ * driver can install one. A question cannot work that way, and the constraint
922
+ * is the one the whole library is priced on: `system` and `tools` are
923
+ * byte-identical on every turn so the prompt prefix stays cached, and a verb
924
+ * that appeared when a panel mounted would be a tool list that changed
925
+ * underneath a running conversation. So an app that may ever want to ask
926
+ * declares this at mount; an app that declares nothing gets no `ask` verb, the
927
+ * manifest it had before this field existed, and the same fingerprint.
928
+ *
929
+ * **THE ANSWER IS THE TOOL'S RESULT AND NOTHING ELSE KEEPS IT.** The person's
930
+ * words come back as the `note` of the ask's own `Result` — `ok — they said:
931
+ * "…"` — and the core stores not one byte of them. That is the structural half
932
+ * of "a question is not consent": there is no field a later `commit` could
933
+ * consult, no remembered yes, and `ApprovalRequest` has no way to reach an
934
+ * answer, so the approval gate is asked exactly as it would have been if the
935
+ * question had never happened. See `MountOptions.ask`.
936
+ */
937
+ export type AskHook = (question: AskQuestion) => AskAnswer | Promise<AskAnswer>;
938
+ /**
939
+ * One step of a `FlowSpec`: a verb, and a line about using it here.
940
+ *
941
+ * The shape is webmachinelearning/webmcp#340's `steps[].{section, tool, hint}`,
942
+ * with `tool` spelled `verb` because that is what this library calls the
943
+ * thing — and because a `tool` field whose value must be a verb name is a
944
+ * field somebody will eventually fill with an MCP tool name instead.
945
+ */
946
+ export interface FlowStep {
947
+ /** The verb to call. Checked at `mount()` against the verbs that exist; see
948
+ * `Manifest.flows`. */
949
+ verb: string;
950
+ /** What to do with it HERE, in this flow: "sort by pending so the companies
951
+ * with work at the top", "read the count it reports before going on". The
952
+ * verb's own `describe` already says what the verb does; this says why this
953
+ * step exists and what to watch for, which is what a recipe is. */
954
+ hint: string;
955
+ /** An optional grouping label for a flow with phases — "find the company",
956
+ * "check the numbers". From #340's shape. Several consecutive steps may
957
+ * share one. */
958
+ section?: string;
959
+ /**
960
+ * When this step may be skipped, in one line: "the person already named the
961
+ * company". Present means the step is NOT load-bearing; absent means take it.
962
+ *
963
+ * THE GAP THIS CLOSES. A flow was a flat list in which every step read as
964
+ * required, so a recipe with an optional first step had nowhere to say so and
965
+ * the condition went into the `hint` — the worked example's own step one said
966
+ * "If the person named the company, you can skip straight to `open-company`"
967
+ * in the middle of a sentence about what `goto` is for. A reader cannot tell
968
+ * from the structure which steps carry the job and which are there for the
969
+ * case where you do not already know the answer, and neither can a model
970
+ * skimming five of them.
971
+ *
972
+ * **IT IS A SENTENCE AND NOT AN EXPRESSION, AND THAT IS THE DESIGN.** The
973
+ * obvious thing to put here is a machine-readable predicate — Arazzo has
974
+ * `successCriteria`, VS Code has `when` clauses, and a `{ fact: "count", gt:
975
+ * 500 }` would look like the same move. It is not, because **nothing in this
976
+ * library evaluates a flow.** A flow is read and followed by the model (see
977
+ * `FlowSpec`), so the only evaluator a predicate could ever have is the
978
+ * reader it is written for, and a predicate read by a model is a sentence
979
+ * with brackets round it: all of the precision is in the notation and none of
980
+ * it is in the enforcement. That is worse than prose rather than better,
981
+ * because a condition that LOOKS checkable gets trusted like one. A string
982
+ * admits in its own type that this is a judgement call.
983
+ *
984
+ * WHERE THE CONDITIONS THAT MUST BE ENFORCED ACTUALLY LIVE, because "if the
985
+ * count is over 500, ask first" is the example everybody reaches for and it
986
+ * is already expressible in the one place where the core and not the model
987
+ * holds the gate:
988
+ *
989
+ * approve: (req) => (req.snapshot.selectedCount ?? 0) > 500
990
+ * ? askTheHuman(req)
991
+ * : true
992
+ *
993
+ * `ApprovalHook` runs at the moment of the commit, on the snapshot the core
994
+ * read itself, and a model cannot route around it. `Verb.consumes` is the
995
+ * same shape of answer for "do not commit a count you have not read". A
996
+ * threshold written HERE instead would be a second, weaker copy of a gate the
997
+ * library already enforces, and the copy is the one the model can misread.
998
+ * So: conditions that decide whether DATA MOVES go in `approve`; conditions
999
+ * that decide WHICH STEPS YOU TAKE go here, in words.
1000
+ *
1001
+ * One line, at most `SIZE_LIMITS.describe` characters, no newline, and it
1002
+ * counts against the flow's `SIZE_LIMITS.body` budget like a `hint` does —
1003
+ * it is rendered into the same cached prefix. An empty string is refused at
1004
+ * `mount()`: a step that is skippable for no stated reason is a step the
1005
+ * author has not decided about, and "optional" with no condition tells a
1006
+ * model it may drop a step whenever it likes.
1007
+ *
1008
+ * In `fingerprint()` with the rest of the flow, for the reason the steps are:
1009
+ * a step that has become optional is a different recipe, and an agent holding
1010
+ * the old cached copy is working from the version where it was required.
1011
+ */
1012
+ skipWhen?: string;
1013
+ }
1014
+ /**
1015
+ * WHY THERE IS NO ITERATION CONSTRUCT, since "do this for every company" is
1016
+ * the second thing anybody tries to write down here.
1017
+ *
1018
+ * It was considered and refused, and not on grounds of effort: a `forEach` over
1019
+ * a step list is small. It is refused because it is **against the grain of the
1020
+ * one safety mechanism this library actually enforces.** "For every company,
1021
+ * accept its pending contacts" is N commits. The whole `describe` → read the
1022
+ * `count` → `commit` apparatus, and the plan token that binds the second to the
1023
+ * first, exists to make ONE commit over many rows safe *instead of* many
1024
+ * commits over few — one count a person can check, one approval that names a
1025
+ * real number, one receipt. A declared loop would be the library's own manifest
1026
+ * teaching an agent to do the thing the plan token was built to stop: N
1027
+ * approvals that each look small, with nobody ever shown the total.
1028
+ *
1029
+ * And it is unnecessary in the case it is wanted for, because `Verb` already
1030
+ * answers it one level down: a verb that operates on many rows **takes a
1031
+ * predicate and resolves it inside the app**, where the rows already are. The
1032
+ * app that needs "every pending contact everywhere" declares
1033
+ * `select-where(status: "pending")` and gets one count and one commit; the
1034
+ * worked example has exactly that verb beside its per-company one. If an app
1035
+ * cannot express the job as a predicate, the loop is a judgement call about
1036
+ * data the agent has to look at between iterations — which is the argument in
1037
+ * `FlowSpec` for why nothing executes a flow, applied to the one construct that
1038
+ * would need to.
1039
+ *
1040
+ * So the honest form of "do this for every company" is a flow whose `describe`
1041
+ * says the job is per-company and whose steps are the one company's, plus the
1042
+ * agent repeating it and being gated each time. That repetition is not a gap
1043
+ * the notation should close. It is N approvals because it is N commits.
1044
+ *
1045
+ * WHY THERE IS NO `from: "step2.slug"` EITHER — no reference from one step to
1046
+ * an earlier step's output. It would require a named output to refer to:
1047
+ * `Verb.returns` or similar, since a verb's body returns `void | Refusal |
1048
+ * Report` and the only named things in a `Report` are a `count` and a `note`.
1049
+ * A half-wired reference — a `from` the model has to guess the meaning of
1050
+ * because no verb declares what it hands back — is the worst of the available
1051
+ * outcomes, so the choice was properly or not at all, and it is not at all:
1052
+ *
1053
+ * - Carrying a value between steps means the AGENT holds it, and `Verb` is
1054
+ * written against exactly that: "the agent should never learn an id it did
1055
+ * not need". The steps of the worked example's own flow pass nothing —
1056
+ * `open-company` moves the screen, `select-pending-here` needs no slug
1057
+ * because it means *this* company, `accept-selected` needs no ids because
1058
+ * it means *this* selection. The app holds the state between steps. That is
1059
+ * not a missing feature of the flow notation; it is where this library
1060
+ * decided state lives, and a `from` field would be the first thing to
1061
+ * argue the other way.
1062
+ *
1063
+ * - The ONE value that genuinely has to cross two steps is the plan token,
1064
+ * and it is already declared — per verb, in the cached half, as
1065
+ * `Verb.consumes` — and already *enforced* by the core rather than
1066
+ * interpreted by the reader. A second declaration of it on the step would
1067
+ * be a weaker copy of a check that works.
1068
+ *
1069
+ * What a flow now does about that one dependency is the little that can be done
1070
+ * honestly: `mount()` refuses a flow whose plan-consuming step has no earlier
1071
+ * `describe` step to mint the token (a recipe that cannot be followed, found at
1072
+ * startup instead of at the commit), and the renderer prints which step the
1073
+ * token comes from. Both are derived from what the verbs already declare. No
1074
+ * new field, nothing for an author to keep in step, and nothing claimed that
1075
+ * the core does not check.
1076
+ */
1077
+ /**
1078
+ * A declared, static, READABLE recipe: the level above a single verb.
1079
+ *
1080
+ * WHY THIS EXISTS, in one sentence: an app's verbs say what can be done and
1081
+ * nothing says what is usually done with them, so every agent re-derives the
1082
+ * same five-step job from scratch, and the four of five that get it slightly
1083
+ * wrong get it wrong in a way nobody sees until the commit. Five independent
1084
+ * efforts arrived at the same level — webmcp#255 (`registerCollection`),
1085
+ * webmcp#161 (`registerSkill`), webmcp#340, OpenAPI Arazzo's `workflows[]`,
1086
+ * and Apple's app skills — which is the usual sign that the gap is in the
1087
+ * problem and not in one design.
1088
+ *
1089
+ * **THE AGENT READS THIS AND FOLLOWS IT. IT DOES NOT CALL IT.** That is the
1090
+ * whole design and it is a deliberate refusal to ship a workflow engine. A
1091
+ * flow that executed internally would run step 3 on an app whose state had
1092
+ * moved — the client already exists, the selection is empty, the record was
1093
+ * accepted by somebody else a second ago — and the only answers available to
1094
+ * it are to carry on or to stop, both blind, with the agent that could have
1095
+ * adapted holding nothing but an `ok`. Adapting mid-job is the one thing a
1096
+ * model is better at than a state machine, and handing the recipe to the model
1097
+ * keeps the adapting where the judgement is. Apple's app skills make the same
1098
+ * admission out loud — *"Skills don't enforce deterministic behavior, and Siri
1099
+ * doesn't run the listed intents in sequence"* — and it reads as a caveat only
1100
+ * if you expected an engine.
1101
+ *
1102
+ * **The `describe` carries the weight.** Microsoft's finding on plugin
1103
+ * selection across groups is that the choice *"is based on the plugin's
1104
+ * description, not on any of the individual functions"*: a model picking
1105
+ * between several flows reads these sentences and not the steps. So `describe`
1106
+ * is the field to spend the effort on, which is also why it has a documented
1107
+ * size budget — see `SIZE_LIMITS`.
1108
+ *
1109
+ * Static, so it rides in the manifest that was already being cached and costs
1110
+ * nothing per turn. That is the cheapest possible version of this feature:
1111
+ * nothing is sent per turn, nothing is computed per call, and an app that
1112
+ * declares no flows produces the manifest it produced before the field
1113
+ * existed, byte for byte.
1114
+ */
1115
+ export interface FlowSpec {
1116
+ /** Stable, kebab-case, unique within a mount. An agent's handle on the flow
1117
+ * when a person says "do the usual thing", and the string a transcript
1118
+ * names. */
1119
+ id: string;
1120
+ /**
1121
+ * What this flow is for, in one line, in the user's own words: "accept a
1122
+ * company's pending contacts".
1123
+ *
1124
+ * The field a model chooses on. At most `SIZE_LIMITS.describe` characters
1125
+ * and **no newline**: it is a one-line label, and a paragraph here is a
1126
+ * paragraph that is read instead of the steps.
1127
+ */
1128
+ describe: string;
1129
+ /**
1130
+ * The steps, in order. At least one.
1131
+ *
1132
+ * Every `step.verb` must name a verb that exists, and `mount()` throws if
1133
+ * one does not — the way it throws on a duplicate verb name. A recipe citing
1134
+ * a verb nobody implemented is a dead end whose only symptom is an agent
1135
+ * halfway through a job getting `unknown-verb`, after the navigation and
1136
+ * before the commit, which is the worst place to find out.
1137
+ *
1138
+ * The same argument gets a second check: a step whose verb declares
1139
+ * `consumes: "plan"` must have an earlier step whose verb is a `describe`,
1140
+ * because the token can only come from one of those. A flow that commits
1141
+ * without counting first is a recipe whose only possible ending is
1142
+ * `stale-plan` — the same dead end, found at the same worst moment — and the
1143
+ * core can see it statically, from what the verbs already declare, so it says
1144
+ * so at `mount()` instead. Nothing new is written down for this: it is the
1145
+ * one piece of data flow between steps that exists, and it is declared on the
1146
+ * verb. See the note under `FlowStep` for why there is no other.
1147
+ */
1148
+ steps: FlowStep[];
1149
+ }
1150
+ /** The static half: hand this to a model once and cache it. Contains no data,
1151
+ * no ids, no user, nothing that varies between two people or two minutes. */
1152
+ export interface Manifest {
1153
+ /** Changes when the verb list changes, so an agent can tell its cached copy
1154
+ * went stale without diffing. */
1155
+ version: string;
1156
+ app: string;
1157
+ /**
1158
+ * The app's own briefing, in its own words: "this app has screens —
1159
+ * dashboard, contacts, companies, settings — and `go` reaches those".
1160
+ *
1161
+ * It is here because it is static by definition and belongs in the half that
1162
+ * gets cached once. Before it there was nowhere: a widget could pass
1163
+ * `instructions` to `drive()`, an agent arriving through the relay or MCP
1164
+ * never saw those, and an app that needed to say something about itself to
1165
+ * BOTH paths had to put it in `Snapshot.note` — in the uncached half, paid
1166
+ * for on every single turn, for a sentence that never changes. That was the
1167
+ * one thing in the library the in-page path and the outside path disagreed
1168
+ * about.
1169
+ *
1170
+ * Static means static. No ids, no counts, no user, nothing that differs
1171
+ * between two people or two minutes; one per-screen word in here makes the
1172
+ * prefix uncacheable across users, which is where most of the saving lives.
1173
+ * It is in `fingerprint()` for that reason — it is part of what a cached
1174
+ * copy claims.
1175
+ */
1176
+ instructions?: string;
1177
+ verbs: Array<{
1178
+ name: string;
1179
+ describe: string;
1180
+ kind?: VerbKind;
1181
+ /** A CSS selector for the element this verb affects, if the app named one.
1182
+ * A hint for a UI that wants to outline what is about to be touched, and
1183
+ * nothing else: driverkit never resolves it and never sends it to a
1184
+ * model. See `Verb.at`, which this is copied from. */
1185
+ at?: string;
1186
+ /** The screen, or screens, this verb belongs to; absent for "anywhere".
1187
+ * Copied from `Verb.screen`, which carries the reasoning — above all why
1188
+ * the tool list must NOT vary with the screen.
1189
+ *
1190
+ * Copied in the SHAPE the app declared it, a string staying a string, so
1191
+ * that a client written before verbs could name two screens goes on
1192
+ * reading every single-screen app exactly as it did. A reader that wants
1193
+ * one rule for both should normalise with an `Array.isArray` of its own
1194
+ * rather than hope. */
1195
+ screen?: string | string[];
1196
+ /** `true` on a `commit` the app can undo. Copied from `Verb.reversible`;
1197
+ * absent makes no claim. */
1198
+ reversible?: true;
1199
+ /** `"plan"` on a `commit` that may only be called with the token from the
1200
+ * `describe` that counted what it is about to touch. Copied from
1201
+ * `Verb.consumes`; absent means it may be called bare, which is what every
1202
+ * commit written before this field does.
1203
+ *
1204
+ * In the cached half because it has to be: a client renders a tool schema
1205
+ * and a description from this once and caches both, and "you must run the
1206
+ * `describe` first and pass what it gave you" is a fact an agent should
1207
+ * read there rather than discover by being refused. The same argument as
1208
+ * `refuses` and `screen`. */
1209
+ consumes?: "plan";
1210
+ args?: Record<string, ArgSpec>;
1211
+ refuses?: string[];
1212
+ }>;
1213
+ /**
1214
+ * The app's map: every screen it has, declared once.
1215
+ *
1216
+ * Copied from `MountOptions.screens` and in `fingerprint()`, because it is
1217
+ * part of what a cached copy claims about the app — a screen that gained a
1218
+ * url template, or whose `describe` was rewritten, is a screen a model was
1219
+ * briefed about in the app's old words.
1220
+ *
1221
+ * Absent means the app does not describe its screens, NOT that it has one.
1222
+ * Exactly `Snapshot.screen`'s rule, and for the same reason: an app mounted
1223
+ * before this field existed must read identically.
1224
+ *
1225
+ * See `ScreenSpec`, and above all its rule against publishing only the
1226
+ * screens the current user can reach.
1227
+ */
1228
+ screens?: ScreenSpec[];
1229
+ /**
1230
+ * The app's recipes: the multi-step jobs its verbs are usually combined
1231
+ * into. Read and followed by the agent, never called. See `FlowSpec`.
1232
+ *
1233
+ * In `fingerprint()` for the reason `kind` and `screen` are: a flow is
1234
+ * rendered straight into the cached system prompt, so an agent holding the
1235
+ * old manifest is working from the app's old recipe — one that may name a
1236
+ * verb this version has renamed.
1237
+ */
1238
+ flows?: FlowSpec[];
1239
+ }
1240
+ /**
1241
+ * Where a tab stands in the person's attention, as the page itself reports it.
1242
+ *
1243
+ * Three states and not a boolean, because the honest answer has three cases
1244
+ * and a boolean would have to lie about one of them:
1245
+ *
1246
+ * `"focused"` the active tab of the frontmost window. The person is looking
1247
+ * at THIS. `document.visibilityState === "visible"` AND
1248
+ * `document.hasFocus()`.
1249
+ * `"visible"` the active tab of its window, but the window is not frontmost
1250
+ * — the person is in another application. Visible, not looked
1251
+ * at. This is the state a two-state field would have had to
1252
+ * call either "focused" (and send an agent to a window nobody
1253
+ * is in front of) or "hidden" (and lose the only honest answer
1254
+ * to "which one was I last in?").
1255
+ * `"hidden"` a background tab, or a minimised window.
1256
+ *
1257
+ * It is reported by the page and never inferred by a relay: the only process
1258
+ * that can see `document.hasFocus()` is the document. A relay carries the
1259
+ * string and has no opinion about it, exactly as it carries `rev`.
1260
+ *
1261
+ * ABSENT IS UNKNOWN, NOT HIDDEN. A tab driven by a hand-written `fetch` loop,
1262
+ * or a page that passed `focus: false` to `connect()`, reports nothing — and a
1263
+ * client that read absence as "not the one" would quietly stop being able to
1264
+ * drive it at all.
1265
+ */
1266
+ export type TabFocus = "focused" | "visible" | "hidden";
1267
+ /**
1268
+ * One row of `GET /dk/tabs`.
1269
+ *
1270
+ * Declared here rather than left in the protocol document because this is the
1271
+ * listing every client hits first, and it carries a subtlety prose loses: the
1272
+ * id field is `tab`, not `id`, spelled the same as in the `hello` frame. A
1273
+ * client that re-derived this shape from the markdown is exactly how the first
1274
+ * version of it ended up mutually incompatible between two implementations.
1275
+ *
1276
+ * `manifestVersion` is lifted out of the manifest so a client can tell whether
1277
+ * its cached copy is current without fetching the whole thing — which is the
1278
+ * only reason the version exists.
1279
+ */
1280
+ export interface TabInfo {
1281
+ tab: string;
1282
+ app?: string;
1283
+ /** Only so a human can tell two tabs apart when asked "which one?". */
1284
+ title?: string;
1285
+ url?: string;
1286
+ manifestVersion?: string;
1287
+ rev?: number;
1288
+ /**
1289
+ * Where the person's attention is, as the page itself last reported it.
1290
+ *
1291
+ * The field that answers "the one I am looking at" — which is what a person
1292
+ * means when they say which tab, and the one thing `--tab <id>` could never
1293
+ * express. See `TabFocus` for the three states, and PROTOCOL.md for the rule
1294
+ * a client applies when more than one tab is a candidate.
1295
+ */
1296
+ focus?: TabFocus;
1297
+ /** ISO 8601. A string on purpose: nothing computes with it, and parsing it
1298
+ * would invent a failure mode for a field only ever shown to a person. */
1299
+ since?: string;
1300
+ }
1301
+ /**
1302
+ * What an approval is being asked about.
1303
+ *
1304
+ * Structurally identical to `driverkit/agent`'s `CommitRequest`, on purpose
1305
+ * and not by coincidence: the widget's dialog was written against that shape
1306
+ * and is now an implementation of THIS hook, so the two types have to be the
1307
+ * same four fields or the one working confirmation UI in the repository would
1308
+ * need a shim to be reusable. A function that satisfies one satisfies the
1309
+ * other.
1310
+ *
1311
+ * `verb` is the manifest entry rather than the `Verb`, so an approval UI reads
1312
+ * `kind`, `reversible` and `refuses` — the declared facts a person's question
1313
+ * should be worded from — and cannot reach `run`. A gate that could call the
1314
+ * body it is gating is a gate with a way round itself.
1315
+ */
1316
+ export interface ApprovalRequest {
1317
+ name: string;
1318
+ args: Record<string, Scalar>;
1319
+ verb: Manifest["verbs"][number];
1320
+ /** The screen as it is right now, so a question can name the selection the
1321
+ * commit is about to act on. Read before the verb runs. */
1322
+ snapshot: Snapshot;
1323
+ }
1324
+ /**
1325
+ * What an approval hook answers.
1326
+ *
1327
+ * Three outcomes, because there are three things that can happen when you ask
1328
+ * a person: they say yes, they say no, or they are not there.
1329
+ *
1330
+ * `true` / nothing allow. Nothing is a yes so a hook may fall through
1331
+ * its own cases without accidentally denying.
1332
+ * `false` deny, with the core's own wording.
1333
+ * a string deny, and the string becomes the hint the agent is
1334
+ * told. This is the useful one: "not until finance
1335
+ * signs off" is something a model can act on and
1336
+ * "declined" is not.
1337
+ * `{ timedOut: true }` nobody answered. Distinct from `false`, which is a
1338
+ * person deciding; see `approval-timeout` in
1339
+ * `RESERVED_REFUSALS`.
1340
+ * `{ asked: false }` allow, and say that nobody was asked. See below.
1341
+ *
1342
+ * Anything else — an object that is none of these — is treated as a DENIAL and
1343
+ * never as consent. A hook that returns a shape the core does not recognise
1344
+ * has not approved anything, and reading an unknown answer as yes is how a
1345
+ * refactor that changes a return type silently removes the gate.
1346
+ *
1347
+ * WHY THERE IS A FOURTH ANSWER, AND IT IS THE RECORD'S FAULT. A real gate does
1348
+ * not ask about every commit: the widget's dialog lets a `reversible` commit
1349
+ * under its size threshold through without a question, and lets every commit
1350
+ * through when the person has switched confirmations off. Such a hook used to
1351
+ * have to answer `true`, which is right about the call and a lie in the
1352
+ * record — the core writes `approval: "allowed"`, and that claims somebody
1353
+ * said yes. `{ asked: false }` allows the call and records `not-asked`, which
1354
+ * is what actually happened. A gate that asks every time never needs it, and
1355
+ * `true` still means "asked, and the answer was yes" for every hook written
1356
+ * before this existed.
1357
+ */
1358
+ export type Approval = boolean | string | void | {
1359
+ timedOut: true;
1360
+ hint?: string;
1361
+ } | {
1362
+ asked: false;
1363
+ };
1364
+ /** The hook itself. Async because a human is on the other end of it; a
1365
+ * synchronous gate can only ever consult a policy, never a person. */
1366
+ export type ApprovalHook = (req: ApprovalRequest) => Approval | Promise<Approval>;
1367
+ /**
1368
+ * What `MountOptions.approve` accepts: a hook, or the word `"unattended"`.
1369
+ *
1370
+ * `"unattended"` is not a hook that returns true. It is the app SAYING OUT
1371
+ * LOUD that commits on this driver run with no person in front of them —
1372
+ * which is a legitimate configuration (a cron job, a test harness, a backend
1373
+ * that has already done its own authorisation) and is also the dangerous one,
1374
+ * so it is written down rather than reached by leaving a field out. See
1375
+ * `MountOptions.approve` for the argument, and `connect()` for the one place
1376
+ * the absence of either becomes an error.
1377
+ */
1378
+ export type ApprovalPolicy = ApprovalHook | "unattended";
1379
+ /**
1380
+ * The approval state a mounted driver publishes.
1381
+ *
1382
+ * Three things live here and they are for three different readers. `declared`
1383
+ * is for whoever is about to publish this driver to the outside — `connect()`
1384
+ * reads it and refuses to publish a driver whose irreversible commits nobody
1385
+ * has said who approves. `policy` is for a reader of the RECORD: who is
1386
+ * actually asked right now, which is not always what the app declared. `hold`
1387
+ * is for an approval UI that is not an approver at all: a host's own modal that
1388
+ * gates outside `call()` and needs the core to stop taking calls while its
1389
+ * question is open.
1390
+ */
1391
+ export interface DriverApproval {
1392
+ /**
1393
+ * Who is asked about a commit on this driver RIGHT NOW.
1394
+ *
1395
+ * Four values, and the fourth is the one worth reading about. Three of them
1396
+ * are what the app wrote at `mount()` and are also `declared`: `none`,
1397
+ * `hook`, `unattended`. The fourth, `adopted`, means an approver was
1398
+ * installed after `mount()` by something in the page — see `adopt` — and it
1399
+ * is a weaker claim than `hook` on purpose:
1400
+ *
1401
+ * `hook` is a line in the app's own source. It is there before the first
1402
+ * call and it is there for every path, so `connect()` accepts it as the
1403
+ * declaration it requires.
1404
+ *
1405
+ * `adopted` is a gate that exists only while whatever installed it is
1406
+ * still mounted. A panel that is unmounted takes its dialog with it and
1407
+ * this goes back to `declared`. That is a real gate while it lasts, and
1408
+ * it is not a statement the app made, so it does not satisfy `connect()`.
1409
+ *
1410
+ * Folding the two into one word was the alternative and it is the one thing
1411
+ * this field must not do: an auditor reading `hook` on every record would
1412
+ * have no way to tell the app's own gate from one that happened to be on the
1413
+ * page that afternoon, and a field that cannot be wrong is a field that
1414
+ * cannot be checked.
1415
+ *
1416
+ * It is a live read, not a constant: the same driver answers `none` before
1417
+ * a panel mounts and `adopted` after, because that is the truth about which
1418
+ * commits had a person in front of them.
1419
+ */
1420
+ readonly policy: "none" | "hook" | "unattended" | "adopted";
1421
+ /**
1422
+ * Which of the three the app itself declared at `mount()`, for all time.
1423
+ *
1424
+ * Never moves, and that is what it is for. `policy` answers "is there a gate
1425
+ * on this driver now"; this answers "did the app say who approves", which is
1426
+ * the question `connect()` asks before publishing a driver to an outside
1427
+ * agent. The two were one field until the widget could install a gate of its
1428
+ * own, and merging them would have made mounting a panel quietly satisfy the
1429
+ * check that exists to make somebody write a sentence.
1430
+ *
1431
+ * Optional, so a hand-written `approval` from before this existed still
1432
+ * satisfies the interface; a reader falls back to `policy`, which is what it
1433
+ * meant then. See `Driver.approval`.
1434
+ */
1435
+ readonly declared?: "none" | "hook" | "unattended";
1436
+ /**
1437
+ * Install an approver now, for a dialog that lives in the page.
1438
+ *
1439
+ * WHAT IT IS FOR. The widget's confirmation is the one working approval UI
1440
+ * in this repository and it used to gate OUTSIDE `call()` — `hold` the lock,
1441
+ * ask the person, then call the verb. The core never saw the question, so an
1442
+ * app that mounted the panel and wired `onAudit` and nothing else recorded
1443
+ * `approval: "not-asked", policy: "none"` for a commit a person had just
1444
+ * clicked Approve on. That record reads as complete and says the opposite of
1445
+ * what happened, which is the one thing an audit trail cannot do.
1446
+ *
1447
+ * WHO WINS. The app does, always. This returns `null` and installs nothing
1448
+ * when the app declared `approve` of either kind:
1449
+ *
1450
+ * a declared `hook` is already asked on every path, so there is nothing to
1451
+ * fix and nothing to argue about — and replacing it would mean a commit
1452
+ * got two dialogs, or the app's own gate silently stopped running.
1453
+ *
1454
+ * `unattended` is the app saying out loud that commits here run with
1455
+ * nobody in front of them. An approver installed over the top of that
1456
+ * would make an unattended driver interactive, which is the configuration
1457
+ * somebody deliberately chose against.
1458
+ *
1459
+ * It also returns `null` when another adopter already holds it. First one
1460
+ * wins, because two gates for one commit is two dialogs, and the second
1461
+ * caller is told out loud rather than left believing it is the gate.
1462
+ *
1463
+ * RELEASING. The return value is a disposer, and calling it is not optional
1464
+ * for anything that can be torn down: an approver whose UI is gone is a
1465
+ * driver where every later commit waits forever on a dialog nobody can see.
1466
+ * After it runs, `policy` is `declared` again and the next adopter can win.
1467
+ * A disposer only ever releases its own hook, so a late one cannot unhook
1468
+ * whoever came after it.
1469
+ *
1470
+ * Optional on the interface, like the rest of `approval`: a hand-written
1471
+ * driver need not offer it, and a caller that finds it absent is on the
1472
+ * driver this mechanism did not exist on.
1473
+ */
1474
+ adopt?: (hook: ApprovalHook) => (() => void) | null;
1475
+ /**
1476
+ * Run `ask` with the driver's no-other-calls lock held.
1477
+ *
1478
+ * While it is held, every `call()` on this driver is refused
1479
+ * `approval-pending`. That is what stops an agent from pressing the Approve
1480
+ * button of the dialog that is asking it about something else — see
1481
+ * `RESERVED_REFUSALS` and webmachinelearning/webmcp#288 — and it is why the
1482
+ * widget's dialog goes through here even when the app declared no core hook.
1483
+ *
1484
+ * The verb's own call is not blocked by its own approval: the core clears
1485
+ * the lock before it runs `run`.
1486
+ */
1487
+ hold: <T>(verb: string, ask: () => Promise<T>) => Promise<T>;
1488
+ }
1489
+ /**
1490
+ * One thing an agent did to this app, as it happened.
1491
+ *
1492
+ * WHY THIS EXISTS. The library commits data on a person's behalf — the whole
1493
+ * design is about making a 3,412-row bulk accept safe — and until this it kept
1494
+ * no record that it happened. Who approved it, which plan token, what count,
1495
+ * at what `rev`, on which screen: "what did the agent do yesterday?" had no
1496
+ * answer. RPA tools have had an audit log for twenty years.
1497
+ *
1498
+ * WHY A HOOK AND NOT A SINK. The host already has somewhere for this to go:
1499
+ * one adopter runs OpenTelemetry, the next has Datadog, the next a table in
1500
+ * their own database. A library that picks your logging backend is a library
1501
+ * you fight, so this one emits a structured record and owns nothing else.
1502
+ * `driverkit/otel` is one such sink, kept behind its own entry point that
1503
+ * nothing in here imports.
1504
+ *
1505
+ * AND WHY THE SINK CANNOT BE THE RELAY OR THE PYTHON PACKAGE. The in-page
1506
+ * widget — the case most likely to commit data, because somebody is sitting
1507
+ * there — has no relay and no Python in it at all. An audit trail that only
1508
+ * existed for the relay case would be worse than none, because it would look
1509
+ * complete.
1510
+ *
1511
+ * WHERE IT IS EMITTED FROM, which is the property that makes it worth
1512
+ * trusting: the record is built from the `Result` that `call()` is about to
1513
+ * return, in the same place that `Result` is decided, and every field it
1514
+ * carries beyond the result was written down by the step that decided it — the
1515
+ * approval answer by the gate, the plan by the mint and the burn. Nothing here
1516
+ * is reconstructed by a second reading of the app, because a log that can
1517
+ * disagree with the `Result` is worse than no log.
1518
+ *
1519
+ * ONE EVENT PER `call()`, whichever path the call came in on — the in-page
1520
+ * loop, a relay frame, a test with no transport — including the calls that
1521
+ * never reached a verb at all. A refused call is the most interesting line in
1522
+ * an audit trail: `declined-by-user` is a person saying no, `stale-plan` is an
1523
+ * agent trying to spend a receipt for a count that moved, and a log that only
1524
+ * recorded successes would have nothing to say about either.
1525
+ *
1526
+ * ---
1527
+ *
1528
+ * **HOW A QUESTION APPEARS IN HERE, AND WHY IT NEEDED NO NEW FIELD.** An `ask`
1529
+ * is a human interaction and belongs in the record as much as an approval does,
1530
+ * and the fields already here say all of it — which is worth spelling out
1531
+ * because the instinct is to add an `asked:` enum beside `approval`, and a
1532
+ * second way to say one thing is a second thing to get wrong.
1533
+ *
1534
+ * `verb: "ask"`, `kind: "ask"` this row is a question.
1535
+ * `args.question` what was asked, as the agent typed it.
1536
+ * `waitedMs` how long it was open. **Present exactly when
1537
+ * a question reached somebody**, which is the
1538
+ * field that tells "asked" from "never put".
1539
+ * `ok: true` asked AND ANSWERED.
1540
+ * `refused: "ask-unanswered"` asked AND IGNORED. `waitedMs` says for how
1541
+ * long.
1542
+ * `refused: "nobody-to-ask"` never put: no person on this path.
1543
+ * `refused: "asks-spent"` never put: the allowance was gone.
1544
+ *
1545
+ * So "asked and answered" and "asked and ignored" are two values of a field
1546
+ * that already exists, and the two kinds of not-asking are told apart by slug
1547
+ * and by the absence of `waitedMs`. `approval` stays `not-asked` on every one
1548
+ * of these rows, because nobody approved anything: a question is not consent,
1549
+ * and a record in which a question could read as an approval would be the
1550
+ * bypass this feature was built not to be.
1551
+ *
1552
+ * **WHAT THE ANSWER'S TEXT IS DOING OUTSIDE THE RECORD, DELIBERATELY.** The
1553
+ * person's words are the `Result`'s `note` and the core keeps no copy, so they
1554
+ * are not here. That is the same argument `ArgSpec.sensitive` makes about this
1555
+ * very hook being "the one boundary where a record is most likely to leave the
1556
+ * box": an answer is free text a person typed, it may be anything, and a host
1557
+ * that wires `onAudit` straight to a vendor's tracer should not thereby ship it
1558
+ * there. What an auditor needs is that a question was asked, what it was, and
1559
+ * whether somebody answered — all of which is above. If you need the words,
1560
+ * they are in the transcript, which is the artefact that is supposed to hold
1561
+ * what was said.
1562
+ */
1563
+ export interface AuditEvent {
1564
+ /** `Date.now()` when the call settled. Milliseconds since the epoch, not an
1565
+ * ISO string: a host formats it its own way, and a number cannot be
1566
+ * mistaken for a timezone-bearing claim this library is not making. */
1567
+ at: number;
1568
+ /** How long the call took, in milliseconds, approval included. The gap
1569
+ * between a question being asked and a person answering it is the part of a
1570
+ * bulk commit a reader most often wants back. */
1571
+ ms: number;
1572
+ /**
1573
+ * How much of `ms` was spent waiting for an approval to be answered.
1574
+ *
1575
+ * Present only when somebody was actually asked — so its absence means no
1576
+ * question was open and `ms` is the app's own work, start to finish.
1577
+ *
1578
+ * **Why both numbers and not one.** `ms` is deliberately approval-inclusive:
1579
+ * "this bulk accept took 41 seconds from the agent's point of view" is the
1580
+ * fact an auditor wants, and a record that silently excluded the human would
1581
+ * make a slow decision look like a fast app. But the two are different
1582
+ * quantities and only one of them is about the software — `ms - waitedMs` is
1583
+ * how long the verb took, and `waitedMs` is how long a person took — and a
1584
+ * record that gave only the sum cannot be used to answer either question.
1585
+ * Subtracting is cheap; recovering a figure that was never written down is
1586
+ * not.
1587
+ *
1588
+ * The same separation reaches the MODEL by a different route and for a
1589
+ * sharper reason: see `DriveOptions.timing`, where the duration a model is
1590
+ * shown is measured so as to EXCLUDE this from the start. An auditor reading
1591
+ * "41 seconds, 38 of them waiting" draws the right conclusion; a model told
1592
+ * "this call took 40 seconds" draws the wrong one about the app's speed and
1593
+ * routes around a verb that is not slow.
1594
+ */
1595
+ waitedMs?: number;
1596
+ /** `MountOptions.app`. Two drivers can be mounted on one page — a widget and
1597
+ * an embedded view — and an unattributed record is one you cannot act on. */
1598
+ app: string;
1599
+ /** The verb. `goto` is the core's own and appears here like any other. */
1600
+ verb: string;
1601
+ /** As DECLARED. Absent means the app made no safety claim about this verb,
1602
+ * which is a fact worth keeping in the record rather than defaulting away:
1603
+ * an auditor asking "what commits ran" must be able to see that some verbs
1604
+ * never said. */
1605
+ kind?: VerbKind;
1606
+ /** As declared. Absent makes no claim; see `Verb.reversible`. */
1607
+ reversible?: true;
1608
+ /**
1609
+ * The arguments AS SENT, with one exception, named by `redacted`.
1610
+ *
1611
+ * As sent, not as validated: an argument of the wrong type, or one the verb
1612
+ * never declared, is exactly what an audit reader is looking for when a call
1613
+ * went wrong. The core does not strip undeclared arguments from the record
1614
+ * for the same reason it does not strip them from the call.
1615
+ */
1616
+ args: Record<string, Scalar>;
1617
+ /** The argument names whose values were replaced with `REDACTED`, because
1618
+ * the verb declared them `ArgSpec.sensitive`. Present only when something
1619
+ * was replaced, so the absence of this field means the `args` above are
1620
+ * the whole truth. */
1621
+ redacted?: string[];
1622
+ /** `Snapshot.screen` the call ran against, read once, before the verb. */
1623
+ screen?: string;
1624
+ /** `Snapshot.rev` the call ran against. With `screen`, this is what the
1625
+ * record needs to be re-readable later: "3,412 rows at rev 180 of the
1626
+ * contacts screen" is a claim somebody can go and check. */
1627
+ rev: number;
1628
+ /** What the agent CLAIMED it was acting on, when it claimed anything. Kept
1629
+ * beside the two above rather than folded into them, because the difference
1630
+ * between them is the whole of a `stale` or a `wrong-screen`, and a record
1631
+ * that kept only one side could not show why the call was refused. */
1632
+ claimed?: {
1633
+ rev?: number;
1634
+ screen?: string;
1635
+ };
1636
+ /** `Result.ok`. */
1637
+ ok: boolean;
1638
+ /** The refusal slug, present exactly when `ok` is false. The app's own slug
1639
+ * or a reserved one; see `RESERVED_REFUSALS`. */
1640
+ refused?: string;
1641
+ /** `Result.count` — what a `describe` reported it matched. The number a
1642
+ * person approves, and the reason this field is in the record. */
1643
+ count?: number;
1644
+ /** The plan token this call PRESENTED, when the verb consumes one. Recorded
1645
+ * whether or not it was accepted: a rejected token is how you find the
1646
+ * agent that tried to spend a stale receipt. */
1647
+ plan?: string;
1648
+ /** The plan token this call MINTED, on a `describe` that counted. The join
1649
+ * key for the two halves of a gated bulk commit: the event that carries
1650
+ * this one as `minted` reported the count, and the event that carries it as
1651
+ * `plan` is the commit that spent it. That pair is the answer to "who
1652
+ * approved 3,412 rows, and was 3,412 the number that moved". */
1653
+ minted?: string;
1654
+ /**
1655
+ * How the approval question was answered.
1656
+ *
1657
+ * `allowed` a hook said yes.
1658
+ * `declined` a hook said no — a person deciding.
1659
+ * `timed-out` nobody answered. The verb did not run.
1660
+ * `not-asked` nobody was asked, and `policy` says why.
1661
+ *
1662
+ * Four values, not three, because "declined" and "never asked" are the two
1663
+ * an auditor must never confuse: one is a person refusing and one is a gate
1664
+ * that was not there.
1665
+ *
1666
+ * `not-asked` has two shapes and `policy` tells them apart. With a `policy`
1667
+ * of `none` or `unattended` there was no gate: nothing could have asked.
1668
+ * With a `policy` of `hook` or `adopted` there WAS a gate and it let this
1669
+ * commit through without a question — a reversible verb under the panel's
1670
+ * size threshold, or confirmations switched off — which is a gate's decision
1671
+ * and not a person's, so it is not `allowed`. See `Approval`'s
1672
+ * `{ asked: false }`.
1673
+ */
1674
+ approval: "allowed" | "declined" | "timed-out" | "not-asked";
1675
+ /**
1676
+ * Who was in a position to be asked, at the moment of this call.
1677
+ *
1678
+ * It is what turns a `not-asked` from a silence into a sentence:
1679
+ * `unattended` is an app saying out loud that commits here run with nobody
1680
+ * in front of them, `none` is an app that never said, and `hook` or
1681
+ * `adopted` beside a `not-asked` is a gate that was there and decided this
1682
+ * particular commit did not need a question.
1683
+ *
1684
+ * Read per call rather than once at `mount()`, because a page can acquire a
1685
+ * gate after it starts: `adopted` is a panel's dialog installed by
1686
+ * `DriverApproval.adopt`, and the same driver's earlier records honestly say
1687
+ * `none`. See `DriverApproval.policy` for why that is a different word from
1688
+ * `hook` and not a synonym for it.
1689
+ */
1690
+ policy: DriverApproval["policy"];
1691
+ }
1692
+ /**
1693
+ * Where the records go. See `MountOptions.onAudit`.
1694
+ *
1695
+ * Returns nothing. A promise is tolerated and is NOT awaited; see the hook's
1696
+ * own documentation for why this one differs from `ApprovalHook`, which is.
1697
+ */
1698
+ export type AuditHook = (event: AuditEvent) => void | Promise<void>;
1699
+ /**
1700
+ * What one driver tells the other drivers of the same app when a commit lands.
1701
+ *
1702
+ * The whole payload, and it is deliberately two strings of the core's OWN
1703
+ * making. `verb` is a name out of this driver's verb table and `from` is the
1704
+ * driver's own identity; neither is app data, neither is a row, neither is a
1705
+ * count. A notice that carried the new rows would be a cross-tab replication
1706
+ * protocol, which is the app's problem and not a library's — and it would be a
1707
+ * leak besides, since the two tabs may not be showing the same person's data
1708
+ * if the app got that wrong.
1709
+ *
1710
+ * It says "something changed, and it was not your copy that changed it". That
1711
+ * is the only fact a sibling tab cannot work out for itself, and it is enough
1712
+ * to stop that tab claiming freshness it has not got.
1713
+ */
1714
+ export interface ElsewhereNotice {
1715
+ /** The `commit` verb that ran. Named so a refusal can say what happened
1716
+ * rather than "something changed somewhere", which an agent cannot act on. */
1717
+ verb: string;
1718
+ /** The driver that sent it. A driver ignores its own notices; without this
1719
+ * a `BroadcastChannel` that echoed to the sender would make every commit
1720
+ * poison the tab that made it. Opaque, and minted per `mount()`. */
1721
+ from: string;
1722
+ }
1723
+ /**
1724
+ * How the drivers of one app in different tabs hear about each other's commits.
1725
+ *
1726
+ * THE HOLE THIS EXISTS TO CLOSE. `Snapshot.rev` is the app's claim that its
1727
+ * data has not moved, and it is bumped by the app's own mutation path. Two
1728
+ * tabs of the same app over one backend: an agent commits in tab A, tab B's
1729
+ * data has changed, and B's `rev` has not moved, because B's store never heard.
1730
+ * So B's snapshot reports stale rows as current, a `stale` check against B's
1731
+ * `rev` PASSES, and a plan token minted in B still verifies — the whole
1732
+ * staleness machinery passing in exactly the situation it exists to catch.
1733
+ *
1734
+ * Nothing inside tab B can see that. Every value a check could be built from —
1735
+ * `rev`, `selectionId`, a count, a hash of the rows — comes out of `visible()`,
1736
+ * which reads B's own store, and B's own store is the thing that is wrong. The
1737
+ * fact has to arrive from outside, and the core's own commit is the only thing
1738
+ * that knows it.
1739
+ *
1740
+ * SO THIS IS A TRANSPORT AND NOT A POLICY. Two functions, no server, no
1741
+ * authority. `driverkit/browser` ships `broadcastElsewhere()`, a
1742
+ * `BroadcastChannel` implementation that works same-origin with no relay at all
1743
+ * — which matters, because an app driven only by its own in-page panel has no
1744
+ * relay to route anything through. An app that already has a websocket or a
1745
+ * service worker implements the same two functions over it and gets the same
1746
+ * check. The relay deliberately does NOT do this: it grants no authority and
1747
+ * knows nothing about verbs, and a relay that read `kind` out of a manifest to
1748
+ * decide what to announce would be a second place that decides what a commit
1749
+ * is.
1750
+ *
1751
+ * WHAT IT BUYS, STATED HONESTLY. It does not make tab B's data correct — only
1752
+ * the app refetching does that, and an app that listens on this channel itself
1753
+ * and refetches is the real fix. What it buys is that B stops lying in the
1754
+ * window before that happens: a `commit` in B is refused `changed-elsewhere`,
1755
+ * and every snapshot B hands back says so in words. See `MountOptions.elsewhere`.
1756
+ */
1757
+ export interface ElsewhereChannel {
1758
+ /**
1759
+ * Broadcast a notice. Called by the core after a `commit` verb's body runs.
1760
+ *
1761
+ * Must not throw usefully — the core catches and warns, because a failed
1762
+ * broadcast is not a reason to turn a commit that already happened into an
1763
+ * error.
1764
+ */
1765
+ post: (notice: ElsewhereNotice) => void;
1766
+ /**
1767
+ * Subscribe. Called once at `mount()`.
1768
+ *
1769
+ * The returned function, if there is one, is called at `unmount()`. Returning
1770
+ * nothing is allowed so that a three-line channel in a test need not invent
1771
+ * one.
1772
+ */
1773
+ listen: (heard: (notice: ElsewhereNotice) => void) => (() => void) | void;
1774
+ }
1775
+ /**
1776
+ * A hard ceiling on what one mounted driver will ever spend.
1777
+ *
1778
+ * `DriveOptions.maxSteps` bounds ONE run. Nothing bounded runs. An agent in a
1779
+ * loop re-enters `drive()` with a fresh `maxSteps` every time, and a widget
1780
+ * somebody leaves open in a tab over a long weekend has no bound at all — so
1781
+ * the app's API and somebody's token budget were both unprotected by
1782
+ * construction, not by oversight.
1783
+ *
1784
+ * WHY THIS IS IN `mount()` AND NOT IN THE WIDGET'S SETTINGS. The settings view
1785
+ * owns what a VIEWER is comfortable with: the pace, step mode, whether the
1786
+ * panel explains itself. A spend limit is not a comfort, it is somebody else's
1787
+ * money, and a viewer who can raise their own ceiling does not have one. So it
1788
+ * is declared where the app is declared, by the person who pays. The settings
1789
+ * view may SHOW it — see `Driver.budget`, which exists so it can — and must
1790
+ * not offer to change it.
1791
+ *
1792
+ * WHAT IS BOUNDED, AND WHAT WAS REJECTED.
1793
+ *
1794
+ * `calls` — every `call()` this driver ever accepts, refused ones
1795
+ * included. This is the bound that matters. It is a bound on the
1796
+ * app's own API, because every verb body is behind it, and it is
1797
+ * a bound on tokens, because an agent spends a model round trip
1798
+ * to produce each one. Refused calls count on purpose: an agent
1799
+ * spinning on `bad-argument` is precisely the runaway this
1800
+ * exists for, and a counter that only counted successes would
1801
+ * never fire on it.
1802
+ *
1803
+ * `commits` — `kind: "commit"` verbs that actually ran. A second counter
1804
+ * rather than a stricter first one, because the two bound
1805
+ * different costs: a thousand reads costs tokens, and a
1806
+ * thousand commits costs DATA. An app may happily allow the
1807
+ * first and not the second, and one number cannot say that.
1808
+ *
1809
+ * `asks` — questions actually put to somebody. A third counter by the
1810
+ * same argument the second one is made of: a thousand reads
1811
+ * costs tokens, a thousand commits costs data, and a thousand
1812
+ * questions costs A PERSON'S PATIENCE, which is the one of the
1813
+ * three that cannot be topped up. One number cannot bound all
1814
+ * three because an app may well want two hundred calls, ten
1815
+ * commits and three questions.
1816
+ *
1817
+ * It is the counter with the sharpest failure mode behind it: an
1818
+ * agent that is confused can ask instead of thinking, and each
1819
+ * question looks individually reasonable while the sequence is a
1820
+ * person being interrogated by something that has stopped
1821
+ * working. `drive`'s `refusalLimit` cannot see it, because every
1822
+ * one of those questions SUCCEEDS.
1823
+ *
1824
+ * It refuses `asks-spent` and never `budget-spent`, and that is
1825
+ * the one place this field departs from the two above. See the
1826
+ * slug in `RESERVED_REFUSALS`: those two end in no repair — stop,
1827
+ * and tell the person — and this one ends in a real one, which is
1828
+ * to say in the final answer what you would have asked.
1829
+ *
1830
+ * REJECTED: **runs per minute**, or calls per minute, or any rolling window.
1831
+ * It is the one that looks most like a cost control and is the least like one.
1832
+ * A rate limit converts an unbounded spend into a slower unbounded spend — the
1833
+ * runaway loop still runs forever, just politely — so it bounds instantaneous
1834
+ * load and not cost, and it is a ceiling nobody can ever hit and therefore a
1835
+ * ceiling that never says anything. An app that genuinely needs load shedding
1836
+ * has it at the server, where a client-side limiter is advisory anyway.
1837
+ *
1838
+ * REJECTED: **model round trips.** It is the thing an author actually wants to
1839
+ * cap, and the core cannot see one. `call()` is the only place every path
1840
+ * converges; a round-trip count would have to be reported by the loop, and the
1841
+ * loop is the consumer being limited. A number the limited thing reports about
1842
+ * itself is not a limit. `calls` is the honest proxy, and the relationship is
1843
+ * close: one tool call is one round trip in every tool-use API there is.
1844
+ *
1845
+ * REJECTED: **total steps per session** as a third field. That is `calls`
1846
+ * under another name — `drive()`'s steps are verb calls — and two fields for
1847
+ * one quantity is two fields that come to disagree.
1848
+ *
1849
+ * Absent, or a field absent, means no ceiling: an app mounted before this
1850
+ * existed behaves byte-identically, and the manifest and its fingerprint do
1851
+ * not move, because a spend limit is a runtime arrangement and not part of the
1852
+ * surface an agent caches.
1853
+ */
1854
+ export interface Budget {
1855
+ /** Most calls this driver will ever accept. Omitted means no bound. A
1856
+ * non-positive number is refused at `mount()`: it means the app is never
1857
+ * drivable, which nobody means, and it would make every call fail with no
1858
+ * hint that a budget was the cause. */
1859
+ calls?: number;
1860
+ /** Most `commit` verbs this driver will ever run. Omitted means no bound. */
1861
+ commits?: number;
1862
+ /**
1863
+ * Most questions this driver will ever put to a person. Omitted means no
1864
+ * bound, which is the same default the two above have and is deliberately
1865
+ * not a small number: a ceiling the library picked would be a ceiling that
1866
+ * fires in somebody's app for a reason they never wrote down.
1867
+ *
1868
+ * Counted only when a question actually reached the hook — a question
1869
+ * refused for a spent allowance, or one the app answered `{ nobody: true }`
1870
+ * to, charged nothing, because neither one took anybody's attention.
1871
+ */
1872
+ asks?: number;
1873
+ /**
1874
+ * Where the counters are kept, so a page reload does not refill them.
1875
+ *
1876
+ * **WHY THIS IS NOT OPTIONAL POLISH.** Navigation is one of the agent's own
1877
+ * verbs. `goto`, `go`, `open-contact` are `read` verbs an agent calls freely,
1878
+ * and a `goto` to a full page load tears the driver down and mounts a fresh
1879
+ * one — with `callsUsed` back at zero. So without this field an agent that
1880
+ * reaches a ceiling can refresh its way past it, and the `budget-spent` hint
1881
+ * says in so many words "it does not refill, and waiting or retrying will not
1882
+ * change it", which was then false. A ceiling that resets is not a ceiling,
1883
+ * and a hint that lies about it is worse than no hint.
1884
+ *
1885
+ * Absent means in-memory counters and the behaviour this had before the field
1886
+ * existed: the ceiling bounds one mounted driver and a reload starts over.
1887
+ * That is still the right default for an app with no storage to reach for.
1888
+ *
1889
+ * **FAIL CLOSED, UNLIKE EVERY OTHER PIECE OF PERSISTENCE IN HERE.** The
1890
+ * widget's panel position and its transcript swallow every storage failure,
1891
+ * because the worst case is a forgotten pane and a lost conversation. Here
1892
+ * the worst case is an uncapped agent, so the asymmetry is reversed on
1893
+ * purpose: when `load()` answers `{ ok: false }`, or a `save()` throws, every
1894
+ * subsequent call is refused `budget-spent` with a hint that names the cause.
1895
+ * An app whose storage is unavailable is still fully usable by the person at
1896
+ * the keyboard — it is only the agent that stops. See `BudgetStore`.
1897
+ *
1898
+ * **THIS IS A COST CONTROL AND NOT A SECURITY BOUNDARY, and the type should
1899
+ * say so rather than imply a guarantee the browser cannot give.** Whatever
1900
+ * holds the record is readable and writable by the person sitting there, in
1901
+ * two keystrokes of devtools, and clearing site data resets it. That is fine:
1902
+ * they own the machine and they are the one paying. What this stops is an
1903
+ * agent in a loop, which is the thing it was built for. A ceiling that has to
1904
+ * hold against the person is a ceiling that belongs on the server, behind the
1905
+ * API the verbs call.
1906
+ */
1907
+ store?: BudgetStore;
1908
+ /**
1909
+ * The name of the current allowance. Changing it starts a fresh one.
1910
+ *
1911
+ * A persisted ceiling cannot reset on nothing, because a budget that is
1912
+ * permanently spent makes the app permanently undrivable — which nobody will
1913
+ * read as a working cost control, they will read it as this library being
1914
+ * broken, and the next thing they do is clear site data, which resets it
1915
+ * anyway and teaches them to do that every time.
1916
+ *
1917
+ * So this is the deliberate, host-held reset: a string the app chooses. A
1918
+ * record written under a different `epoch` is discarded and the allowance
1919
+ * starts again. A deploy can bump it, a billing period can be its name, a
1920
+ * support tool can write a new one into the page it serves.
1921
+ *
1922
+ * **The agent cannot reach it.** It is an argument to `mount()`, so it lives
1923
+ * in the page's own source, which an agent driving through verbs has no verb
1924
+ * for. That is the test every reset mechanism here has to pass, and it is why
1925
+ * there is no reset VERB and never should be.
1926
+ */
1927
+ epoch?: string;
1928
+ /**
1929
+ * How long one allowance lasts, in milliseconds. Omitted means forever.
1930
+ *
1931
+ * `{ calls: 200, window: 86_400_000 }` is two hundred calls a day: the record
1932
+ * carries when its allowance started, and a record older than this is
1933
+ * discarded for a fresh one.
1934
+ *
1935
+ * **This is not the rolling window this type rejects further up, and the
1936
+ * difference is worth being exact about.** What was rejected was a RATE
1937
+ * limit — calls per minute as the bound — on the grounds that it "converts an
1938
+ * unbounded spend into a slower unbounded spend": the runaway still runs
1939
+ * forever, politely. This is a hard ceiling with a renewal period, which is a
1940
+ * quota. A runaway still stops dead at `calls` and stays stopped for the rest
1941
+ * of the window; what the window buys is that tomorrow the app works again
1942
+ * without anybody deploying anything. Tumbling and not sliding, because a
1943
+ * sliding window needs the timestamp of every call and this needs one number.
1944
+ *
1945
+ * Also agent-unreachable: it is declared at `mount()`, and the only thing
1946
+ * that advances it is the wall clock. An agent that could set the page's
1947
+ * clock forward has bigger powers than this field.
1948
+ *
1949
+ * A DESIGN NOTE, NOT BUILT, so the argument is not lost: the settings view
1950
+ * could let the VIEWER lower this app's ceiling but never raise it. A person
1951
+ * tightening their own leash is always safe, and the app's declared number
1952
+ * stays the hard stop, so there is no bypass in it. It is not here because a
1953
+ * viewer-held floor is a second persisted number with its own scope question,
1954
+ * and the ceiling that actually had to stop resetting is this one.
1955
+ */
1956
+ window?: number;
1957
+ }
1958
+ /**
1959
+ * Where a `Budget`'s counters live between page loads.
1960
+ *
1961
+ * An interface and not a `localStorage` call, for the reason every other
1962
+ * environment touch in the core is one: `mount()` runs in a browser, in a test
1963
+ * with a fake DOM, in `jsdom`, and in whatever somebody embeds this in next.
1964
+ * The core decides what the numbers MEAN — what counts, when an allowance has
1965
+ * expired, what a broken store does to the next call — and the store only
1966
+ * carries bytes. `localBudgetStore` in `driverkit/browser` is the one this
1967
+ * library ships; it is twenty lines over the same `loadJson`/`saveJson` the
1968
+ * widget's position and settings go through.
1969
+ *
1970
+ * **Scoped PER APP PER ORIGIN, which is what `localBudgetStore` does, and the
1971
+ * scope is a design decision rather than an implementation detail.** The
1972
+ * widget's transcript is scoped per TAB — it goes in `sessionStorage` — and a
1973
+ * budget deliberately is not, for one reason: if the ceiling were per tab then
1974
+ * "open a second tab" would be a budget-doubling move, which is the reload
1975
+ * bypass this whole mechanism exists to close, wearing a different hat. Two
1976
+ * tabs of one app therefore share one allowance, and yes, that means one tab
1977
+ * can exhaust the other's. That is the correct failure and the honest one: it
1978
+ * is ONE ceiling, spent by one person's agents, exactly as it would behave if
1979
+ * it were counted on the server — and the alternative fails in the direction
1980
+ * that costs money.
1981
+ */
1982
+ export interface BudgetStore {
1983
+ /**
1984
+ * The record this store holds, or the news that it cannot be consulted.
1985
+ *
1986
+ * Three answers and not two, because the third is the whole of fail-closed:
1987
+ * "nothing stored yet" and "I could not look" are indistinguishable in a
1988
+ * plain `getItem` and must never be treated alike. A private window, blocked
1989
+ * site data and a corrupt record all answer `{ ok: false }`, and a corrupt
1990
+ * record is included on purpose — it cannot be told apart from tampering, and
1991
+ * guessing in the agent's favour is the one guess with a bill attached.
1992
+ *
1993
+ * **Called at `mount()` and again before every charged call**, and the
1994
+ * re-read is what makes "one app, one ceiling" true rather than aspirational.
1995
+ * Two tabs open at once each hold their own copy of the counters, so a
1996
+ * mount-time read alone gave each of them the whole allowance — two tabs, two
1997
+ * ceilings, which is the reload bypass wearing a different hat. It must
1998
+ * therefore be cheap and synchronous; `localBudgetStore` is one `getItem` and
1999
+ * one `JSON.parse` of a four-field object.
2000
+ *
2001
+ * The core takes the HIGHER of what this answers and what it already holds,
2002
+ * so a store that reads back a smaller figure — a tab that was behind, a
2003
+ * record somebody edited — can never hand an allowance back.
2004
+ */
2005
+ load(): {
2006
+ ok: true;
2007
+ spent: BudgetRecord | null;
2008
+ } | {
2009
+ ok: false;
2010
+ };
2011
+ /**
2012
+ * Write the record back. Called after every charged call.
2013
+ *
2014
+ * **May throw, and a throw is load-bearing here.** Everywhere else in this
2015
+ * library a failed storage write is swallowed, because a forgotten panel
2016
+ * position is not worth an exception. A budget that cannot be written is a
2017
+ * budget that will not be there after the next reload, which is the uncapped
2018
+ * agent again — so the core catches this, refuses every later call
2019
+ * `budget-spent`, and names the reason in the hint. Throwing is how a store
2020
+ * says "this ceiling is no longer being kept".
2021
+ */
2022
+ save(record: BudgetRecord): void;
2023
+ }
2024
+ /**
2025
+ * One allowance, as it is stored.
2026
+ *
2027
+ * Four fields and no more. In particular there is no verb name, no argument, no
2028
+ * count and no screen: a spend record that carried any of those would be a
2029
+ * transcript in `localStorage` under a field nobody reads as one, and the
2030
+ * privacy class of "this app has made 41 calls" is nothing like the privacy
2031
+ * class of what those calls were about.
2032
+ */
2033
+ export interface BudgetRecord {
2034
+ /** Calls charged so far, matching `BudgetState.calls.used`. */
2035
+ calls: number;
2036
+ /** Commits run so far, matching `BudgetState.commits.used`. */
2037
+ commits: number;
2038
+ /**
2039
+ * Questions put so far, matching `BudgetState.asks.used`.
2040
+ *
2041
+ * Optional, and that is the migration: a record written before this field
2042
+ * existed reads back with no `asks`, which the core takes as 0 rather than
2043
+ * discarding the record — the two numbers beside it are still true, and
2044
+ * throwing an allowance away because a field was added would hand every app
2045
+ * with a store a fresh ceiling on upgrade.
2046
+ *
2047
+ * Still four fields' worth of privacy class and no more: a count of
2048
+ * questions, never a question. Nothing a person typed is written here, by
2049
+ * the rule the rest of this type is built on.
2050
+ */
2051
+ asks?: number;
2052
+ /** `Date.now()` when this allowance began. Only `Budget.window` reads it; an
2053
+ * app with no window never looks, and the field is still written so that
2054
+ * adding a window later does not have to treat every existing record as
2055
+ * having started at the epoch. */
2056
+ since: number;
2057
+ /** The `Budget.epoch` in force when this record was written, absent when the
2058
+ * app declared none. A mismatch discards the record; see `Budget.epoch`. */
2059
+ epoch?: string;
2060
+ }
2061
+ /**
2062
+ * What a mounted driver has spent, for a UI to show a person.
2063
+ *
2064
+ * READ-ONLY and that is the field's whole design. The widget's settings view
2065
+ * shows this and offers no control beside it; see `Budget` for why a viewer
2066
+ * must not be able to raise their own ceiling.
2067
+ *
2068
+ * `used` without `limit` is the normal case — an app with no budget still
2069
+ * reports what it has spent, which is the number somebody debugging a runaway
2070
+ * actually wants. `left` is deliberately absent: it is `limit - used`, and a
2071
+ * second way to say one thing is a second thing to get wrong.
2072
+ */
2073
+ export interface BudgetState {
2074
+ calls: {
2075
+ used: number;
2076
+ limit?: number;
2077
+ };
2078
+ commits: {
2079
+ used: number;
2080
+ limit?: number;
2081
+ };
2082
+ /** Questions put to a person, and the ceiling if the app set one. Reported
2083
+ * whatever the app declared, like the two above, so a settings view has one
2084
+ * shape to render — and so "it has asked me four things" is a number
2085
+ * somebody can see. */
2086
+ asks: {
2087
+ used: number;
2088
+ limit?: number;
2089
+ };
2090
+ }
2091
+ export interface MountOptions {
2092
+ /** Shown in the manifest. A name a human would recognise. */
2093
+ app: string;
2094
+ /** The app's own briefing, copied into `Manifest.instructions` and read by
2095
+ * every path that drives it — the in-page prompt and the MCP server alike.
2096
+ * Static: see `Manifest.instructions` for what may not go in it. */
2097
+ instructions?: string;
2098
+ verbs: Verb<never>[] | Verb<Record<string, Scalar>>[] | Verb<any>[];
2099
+ /** Called fresh on every read. Must be cheap: it runs on each verb call. */
2100
+ visible: () => Snapshot;
2101
+ /**
2102
+ * Every screen the app has, with an optional RFC 6570 url template each.
2103
+ *
2104
+ * Declaring this plus `navigate` is what makes the core offer the reserved
2105
+ * `goto` verb, which is the point of the field: an app stops hand-rolling a
2106
+ * navigation verb under a name of its own — `go`, `navigate`, `open-screen`,
2107
+ * `route-to`, one per app, none of which the next agent recognises — and
2108
+ * gets one whose name, arguments, refusals and tool description are the
2109
+ * same in every app that declares a map.
2110
+ *
2111
+ * Declared WITHOUT `navigate` the list is still published and still useful:
2112
+ * an agent learns what screens exist and what their addresses look like,
2113
+ * which is more than it had. But no `goto` appears, because a navigation
2114
+ * verb the core cannot carry out would be a dead verb sitting in everybody's
2115
+ * cached prefix. `mount()` says so once, out loud, since the likelier of the
2116
+ * two readings is that the hook was forgotten.
2117
+ *
2118
+ * See `ScreenSpec`. Static, and the one rule worth repeating here: publish
2119
+ * the screens the APP has, never the screens this user can reach.
2120
+ */
2121
+ screens?: ScreenSpec[];
2122
+ /**
2123
+ * How the app gets to a screen. The body of the reserved `goto` verb.
2124
+ *
2125
+ * Read only when `screens` is declared, and ignored without it: there is
2126
+ * nowhere for `goto` to go. See `NavigateHook` for why this is the app's
2127
+ * function and not the core's, and `NavigateTarget` for what it is handed.
2128
+ *
2129
+ * An app that would rather keep its own navigation verb simply declares one
2130
+ * named `goto` in `verbs`, and the core leaves it alone — the same rule as
2131
+ * the hoisted `rev` and `screen` arguments in `driverkit/agent`, where an
2132
+ * app that claims the name keeps it, because the app's meaning wins over
2133
+ * ours.
2134
+ */
2135
+ navigate?: NavigateHook;
2136
+ /**
2137
+ * How the app puts a question to the person. Declaring it is what makes the
2138
+ * core offer the reserved `ask` verb.
2139
+ *
2140
+ * THE GAP THIS CLOSES. `approve` is a GATE, not a channel. A person could
2141
+ * stop the agent and the agent could not ask the person anything, so an
2142
+ * agent meeting an ambiguous instruction — "select the contact called Ada
2143
+ * Duarte", and five people are called that — had two moves: guess, or give
2144
+ * up. The argument is the one `declined-by-user` already rests on, one level
2145
+ * up: a model that cannot tell "the person said no" from "your arguments
2146
+ * were wrong" will retry with different arguments, and **a model that cannot
2147
+ * ask will guess.** Two of this library's own eval cases were written for an
2148
+ * outcome it could not express.
2149
+ *
2150
+ * Absent means absent, which is the whole of the additive promise here: no
2151
+ * `ask` verb appears, the manifest is the document it was before this field
2152
+ * existed, and `fingerprint()` does not move. An app that cannot ask is an
2153
+ * app where the guidance in `systemPrompt` about asking is never rendered
2154
+ * either, so a model is not told about a tool it has not got.
2155
+ *
2156
+ * BOTH WAYS IN, which is the half that makes this a feature rather than a
2157
+ * widget. The verb lives in `call()`, like every other, so an agent arriving
2158
+ * over the relay or MCP asks the same question through the same hook and the
2159
+ * person answers it in the same panel. What the outside path needs that the
2160
+ * in-page one does not is `askTimeout`; see it for why the default is a
2161
+ * deadline here and "wait forever" for an approval.
2162
+ *
2163
+ * IT IS NOT AN APPROVAL AND MUST NOT BECOME ONE. Nothing the person types
2164
+ * here is stored, nothing reaches `ApprovalRequest`, and a `commit` after a
2165
+ * question is gated exactly as a commit before one would have been — so an
2166
+ * agent that asks "shall I accept these 3,412?" and is told yes still meets
2167
+ * `approve`, and a `false` from `approve` still refuses the commit. The
2168
+ * separation is structural because there is no state for it to be a policy
2169
+ * about. See `AskHook`.
2170
+ */
2171
+ ask?: AskHook;
2172
+ /**
2173
+ * How long to wait for an answer before giving up, in milliseconds.
2174
+ * **Defaults to `DEFAULT_ASK_TIMEOUT_MS`, which is 45 seconds.**
2175
+ *
2176
+ * **THE OPPOSITE DEFAULT FROM `approvalTimeout`, ON PURPOSE.** That one waits
2177
+ * forever, because the person may be reading the rows the question is about
2178
+ * and a deadline that DENIES while they are deciding teaches them the dialog
2179
+ * is a race. Both halves of that argument invert here.
2180
+ *
2181
+ * Nothing is denied by a lapsed question: no verb was going to run and no
2182
+ * data was going to move, so giving up costs the agent one refusal it can
2183
+ * read and act on, and costs the person nothing at all.
2184
+ *
2185
+ * And waiting past the transport's own deadline is actively dishonest. The
2186
+ * relay gives a call 60 seconds and then answers `timeout`, which PROTOCOL.md
2187
+ * defines as **outcome unknown** — a commit that came back that way may
2188
+ * already have run. A question that outlived it would convert "nobody
2189
+ * answered", which is a fact the agent can act on, into "nobody knows", which
2190
+ * is the most expensive answer in the system, over the one call where the
2191
+ * truth was plainly knowable. So the default sits comfortably under the
2192
+ * relay's: the page answers `ask-unanswered` first, every time, and the
2193
+ * outside agent learns the fact rather than the mystery.
2194
+ *
2195
+ * 45 seconds is also about as long as a question can sit in front of
2196
+ * somebody before it is stale: an agent that asked a minute ago is asking
2197
+ * about a screen that has moved. Raise it for a gate that is a webhook or a
2198
+ * phone, and raise `RelayOptions.timeoutMs` with it or the relay will answer
2199
+ * first — which is the one combination that gives back exactly the honesty
2200
+ * this default is protecting. A non-positive number means wait forever, which
2201
+ * is the right answer for an app that is only ever driven in-page.
2202
+ */
2203
+ askTimeout?: number;
2204
+ /**
2205
+ * The app's multi-step recipes, for an agent to read and follow.
2206
+ *
2207
+ * Validated at `mount()` — a flow naming a verb that does not exist throws,
2208
+ * like a duplicate verb name — and then copied into the manifest and
2209
+ * rendered into the cached system prompt. Nothing executes them. See
2210
+ * `FlowSpec`, which carries the argument for why not.
2211
+ */
2212
+ flows?: FlowSpec[];
2213
+ /**
2214
+ * Who approves a `commit`, as a hook or as the word `"unattended"`.
2215
+ *
2216
+ * WHY THIS IS IN THE CORE AND NOT IN A CONSUMER. The confirmation used to
2217
+ * live in `driverkit/agent`'s `onBeforeCommit`, which `mountWidget` turns
2218
+ * on by default — so a person watching the panel was asked, and an agent
2219
+ * arriving over the relay or MCP called `accept-selected` and it simply ran.
2220
+ * The reserved slug `declined-by-user` existed and nothing on the outside
2221
+ * path could ever emit it. That is the gate being in the wrong layer: a
2222
+ * safety property that holds on one of two paths is not a safety property,
2223
+ * it is a property of one consumer. `call()` is where every path converges —
2224
+ * the in-page loop, a relay frame, a test with no transport — so the gate
2225
+ * belongs here, and from here a denial is the same `declined-by-user`
2226
+ * refusal whichever way the call arrived.
2227
+ *
2228
+ * Called before `run`, for `kind: "commit"` verbs only. `read` and
2229
+ * `describe` are the two kinds that promise no data moves, which is the
2230
+ * promise this gate exists to check; asking about them is how people learn
2231
+ * to click Yes without reading. A commit that declares `reversible` is still
2232
+ * asked about here — the core does not know how large the selection is or
2233
+ * how good the app's undo really is, and the widget's dialog, which does,
2234
+ * answers that question inside its own implementation of this hook.
2235
+ *
2236
+ * NOBODY IS THERE TO ASK: the third answer, and the one this field is shaped
2237
+ * by. A headless agent over MCP has no human, and the three candidate
2238
+ * answers were refuse (safe, and it breaks every automated run), allow (what
2239
+ * happened before this existed, and it is the gap) or make the host DECLARE
2240
+ * which it is. The third is taken, and the precedent is `allow_remote=True`
2241
+ * with no way to identify callers, which raises at construction rather than
2242
+ * serving: three deliberate acts to publish a relay, not one. So here —
2243
+ * absent means absent, in-page behaviour is exactly what it was, and the
2244
+ * declaration is enforced at the moment the driver is published to the
2245
+ * outside. `connect()` refuses to open a stream for a driver that has
2246
+ * `commit` verbs which have not declared `reversible` and no `approve` of
2247
+ * either kind. Three deliberate acts to let an outside agent change data: a
2248
+ * commit verb, a relay connection, and a statement of who approves. The
2249
+ * unsafe case is still reachable — `approve: "unattended"` — but no longer
2250
+ * by accident, which is the whole of what this buys.
2251
+ *
2252
+ * Absent here makes NO claim and changes nothing about the core: an app
2253
+ * mounted yesterday with no `approve` behaves identically in-page, which is
2254
+ * why this is not a required field and why the default is not a gate.
2255
+ *
2256
+ * WHAT THE WIDGET DOES WITH AN ABSENT ONE. `mountWidget` installs its own
2257
+ * confirmation dialog here, through `DriverApproval.adopt`, so the question
2258
+ * a person answers in the panel is asked inside `call()` and lands in the
2259
+ * audit record as `approval: "allowed"` with `policy: "adopted"`. That is
2260
+ * additive — the dialog, its wording and its thresholds are what they were,
2261
+ * and a commit now gets asked about on paths the panel could not reach — and
2262
+ * it does not make the driver publishable: `connect()` reads
2263
+ * `DriverApproval.declared`, which this field alone writes.
2264
+ *
2265
+ * DECLARING ONE HERE OVERRULES THAT, always. A hook of your own is asked and
2266
+ * the panel installs nothing, so one commit gets one dialog; `"unattended"`
2267
+ * is not quietly made interactive by a panel being mounted. The documented
2268
+ * way to have both is to declare the panel's own dialog as this hook:
2269
+ * `approve: (req) => panel.approve(req)`. See `Widget.approve`.
2270
+ */
2271
+ approve?: ApprovalPolicy;
2272
+ /**
2273
+ * How long to wait for an approval before giving up, in milliseconds.
2274
+ *
2275
+ * Absent means wait, and that is the right default rather than a timid one:
2276
+ * the person may be reading the rows the question is about, and a deadline
2277
+ * that denies while they are deciding teaches them that the dialog is a race
2278
+ * instead of a decision. Nothing is lost by waiting — the agent's own
2279
+ * transport deadline fires on its side and tells it the outcome is unknown,
2280
+ * which is true.
2281
+ *
2282
+ * The cost of waiting is that an approval nobody EVER answers leaves the
2283
+ * lock held and every later call refused `approval-pending` until the page
2284
+ * is reloaded. That is safe and it is also a dead app, so an app whose
2285
+ * people are on a phone, or whose gate is a webhook, sets a number here. A
2286
+ * lapsed approval is `approval-timeout`, which says the verb DID NOT RUN —
2287
+ * unlike the relay's `timeout`, which means nobody knows.
2288
+ */
2289
+ approvalTimeout?: number;
2290
+ /**
2291
+ * Where this app's audit records go. One call per `call()`; see `AuditEvent`.
2292
+ *
2293
+ * WHAT IT COSTS TO LEAVE OUT. Nothing: an app that passes no hook behaves
2294
+ * byte-identically to one mounted before this field existed — no record is
2295
+ * built, no clock is read, and the manifest and its fingerprint do not move,
2296
+ * because an audit trail is a runtime arrangement and not part of the
2297
+ * surface an agent caches.
2298
+ *
2299
+ * A THROWING OR SLOW HOOK MUST NOT TAKE THE APP DOWN, and this hook is
2300
+ * shaped by that rather than by politeness. It is called and NOT awaited: a
2301
+ * returned promise has a `catch` attached and is otherwise left alone, and a
2302
+ * synchronous throw is caught. Either way the failure is reported once
2303
+ * through the same `console.warn` channel the rest of the core's authoring
2304
+ * mistakes use — because a record that vanished silently is the one failure
2305
+ * an audit trail cannot survive — and the call's `Result` is returned
2306
+ * unchanged.
2307
+ *
2308
+ * WHY THIS IS NOT `ApprovalHook`, which is awaited, deadlined and whose
2309
+ * failure is a refusal. An approval is asked BEFORE the decision and its
2310
+ * answer IS the decision, so there is nothing to do but wait for it, and a
2311
+ * failed ask has to be a no. An audit record is emitted AFTER the outcome is
2312
+ * decided and, for a commit, after the data has already moved: there is
2313
+ * nothing left to refuse, so waiting on the sink can only delay the agent,
2314
+ * and failing the call because the logger is down would turn a broken
2315
+ * logging backend into a broken application. Hence no deadline here, and
2316
+ * hence no timer — which is also why this hook cannot repeat the bug
2317
+ * `deadline()` carries a comment about.
2318
+ *
2319
+ * FAIL-OPEN IS A REAL LIMIT AND NOT A SHRUG. If you need a commit to be
2320
+ * impossible unless it has been recorded, the place to put that is the
2321
+ * `approve` hook, which runs before the verb and whose no means no. This one
2322
+ * cannot give you that guarantee and does not claim to.
2323
+ */
2324
+ onAudit?: AuditHook;
2325
+ /**
2326
+ * A hard ceiling on what this driver will ever spend. See `Budget`, which
2327
+ * carries the argument for what is bounded and what was rejected.
2328
+ *
2329
+ * Off by default and costs nothing to leave out: with no `budget` the
2330
+ * counters are not kept, nothing is compared, and the call path is the one
2331
+ * it was before this field existed.
2332
+ *
2333
+ * When a ceiling is reached, the call is refused `budget-spent` with the
2334
+ * numbers in the hint. That refusal is the point as much as the stopping is:
2335
+ * `approvalTimeout` waits forever by default because "a deadline that denies
2336
+ * while somebody is reading teaches them the dialog is a race", and a ceiling
2337
+ * has the same hazard pointing the other way — a job that stops halfway and
2338
+ * does not say why is read as a broken app, and the next thing somebody does
2339
+ * is run it again. So nothing here is silent: the agent is told which
2340
+ * ceiling and that it does not reset, `Driver.budget` lets a panel show the
2341
+ * person the same thing, and `drive()` ends the run with `over-budget`
2342
+ * rather than grinding through its refusal limit first.
2343
+ */
2344
+ budget?: Budget;
2345
+ /**
2346
+ * How this driver hears that another tab of the same app committed.
2347
+ *
2348
+ * Absent makes NO claim and changes nothing — the default, and deliberately
2349
+ * the default: a single-tab app, or an app that already invalidates across
2350
+ * tabs so that `rev` moves on its own, gains nothing from this and should pay
2351
+ * nothing for it. An app that passes a channel gets two things:
2352
+ *
2353
+ * - a `commit` is refused `changed-elsewhere` while a notice from another
2354
+ * driver is outstanding, which closes the hole described on
2355
+ * `ElsewhereChannel`: the one case where `stale` passed, the plan token
2356
+ * verified, and the data had moved anyway;
2357
+ * - every `Snapshot` this driver hands back says so, in `note`, so a read is
2358
+ * no longer silently wrong either.
2359
+ *
2360
+ * WHEN IT CLEARS: when this app's own `rev` moves. `rev` is the only
2361
+ * statement the core is entitled to believe about this app's data, and a
2362
+ * second staleness rule beside it would be a second set of rules — the thing
2363
+ * `call()` exists not to have. The cost is stated rather than hidden: a `rev`
2364
+ * that moves for an unrelated reason (the person typed in a filter) clears
2365
+ * the notice without the data having been refetched. That is the SAME
2366
+ * weakness `rev` already has — an app that bumps `rev` without refetching has
2367
+ * already broken `stale` — so this adds none. The fix for both is the same:
2368
+ * listen on the channel yourself and refetch. An app that does is an app
2369
+ * where none of this ever fires.
2370
+ *
2371
+ * ONE THING IS HELD BACK WHILE A NOTICE STANDS: a plan is not carried across
2372
+ * a read-only move. A plan surviving a navigation is a `selectionId` saying
2373
+ * the selection is the same selection, and while a notice stands the core has
2374
+ * been told that this tab's copy of the data is not. Those two would have
2375
+ * composed into a bulk commit going through with the notice silently cleared
2376
+ * and no fresh count read anywhere, so the carry declines and the agent has
2377
+ * to describe again. See `plansGoodAt` in the registry.
2378
+ *
2379
+ * See `ElsewhereChannel`, and `broadcastElsewhere()` in `driverkit/browser`
2380
+ * for the same-origin implementation that needs no server and no relay.
2381
+ */
2382
+ elsewhere?: ElsewhereChannel;
2383
+ }
2384
+ export interface Driver {
2385
+ /** The static half. Stable across calls unless the verbs change. */
2386
+ manifest: () => Manifest;
2387
+ /** The dynamic half, read on demand. */
2388
+ snapshot: () => Snapshot;
2389
+ /** Run a verb. Never throws for an expected refusal; see `Result`. */
2390
+ call: (name: string, args?: Record<string, Scalar>, opts?: CallOptions) => Promise<Result>;
2391
+ /**
2392
+ * The approval policy this driver was mounted with, and its lock.
2393
+ *
2394
+ * Optional only so that a hand-written driver — which PROTOCOL.md says must
2395
+ * remain possible — still satisfies this interface. A reader treats an
2396
+ * absent one as `policy: "none"`, and `connect()` does. A hand-written
2397
+ * driver that has its own gate says so with
2398
+ * `approval: { policy: "unattended", hold: (_, ask) => ask() }`, and such a
2399
+ * driver has no `declared` and no `adopt`: `connect()` reads `declared` and
2400
+ * falls back to `policy`, so that one goes on publishing exactly as it did,
2401
+ * and a panel mounted on it finds no `adopt` and keeps its own in-page
2402
+ * dialog.
2403
+ */
2404
+ approval?: DriverApproval;
2405
+ /**
2406
+ * What this driver has spent, and against what ceiling. See `BudgetState`.
2407
+ *
2408
+ * Offered whatever the app declared, including no budget at all, so a panel
2409
+ * has one shape to render instead of two — and so the numbers are there for
2410
+ * somebody debugging an app that is making more calls than they thought.
2411
+ *
2412
+ * Optional on the interface for the reason `approval` is: a hand-written
2413
+ * driver must remain possible, and a reader treats an absent one as "this
2414
+ * driver does not account for itself".
2415
+ */
2416
+ budget?: () => BudgetState;
2417
+ /** Stop serving. Idempotent. */
2418
+ unmount: () => void;
2419
+ }
2420
+ export interface CallOptions {
2421
+ /** The `rev` the agent believes it is acting on. If the screen has moved on,
2422
+ * the call is refused as `stale` rather than applied to a row the agent
2423
+ * never saw. Omitting it opts out of the check. */
2424
+ rev?: number;
2425
+ /**
2426
+ * The `Snapshot.screen` the agent believes it is on. Checked BEFORE `rev`,
2427
+ * and refused as `wrong-screen`.
2428
+ *
2429
+ * Before this, a `next-page` call sent with no `rev` landed on a screen the
2430
+ * agent had never seen, paged a different company's contact list, and
2431
+ * answered `ok`. "You are somewhere else" is a better answer than "the
2432
+ * number moved", and the two are different mistakes with different fixes —
2433
+ * read again, versus navigate back — which is why this is a separate check
2434
+ * and a separate slug rather than more prose on `stale`.
2435
+ *
2436
+ * Before `rev` for the same reason `rev` is before the argument check: if the
2437
+ * agent is not even on the screen it thinks it is, then the arguments and the
2438
+ * rev both describe something it never saw, and complaining about either
2439
+ * answers the wrong question. Omitting it opts out, like `rev`.
2440
+ */
2441
+ screen?: string;
2442
+ /**
2443
+ * The plan token from the `describe` whose `count` this call was approved
2444
+ * against. Required by a verb declaring `consumes: "plan"`, ignored by every
2445
+ * other verb, and refused as `stale-plan`.
2446
+ *
2447
+ * The third of the three claims, and the one that is NOT optional where it
2448
+ * applies. `rev` and `screen` may be omitted to opt out of their checks,
2449
+ * because an agent that makes no claim about what it was looking at has at
2450
+ * least not made a false one. A plan token cannot work that way: a commit
2451
+ * that may be called without one is a commit whose count guarantee is
2452
+ * advisory, which is the state this field was added to leave. So the verb
2453
+ * declares it statically, in the cached half, and the core refuses the bare
2454
+ * call — with the pairing spelled out in the hint, rather than a validator
2455
+ * at the client rejecting the call before the explanation can reach the
2456
+ * model.
2457
+ *
2458
+ * Checked after `screen` and BEFORE `rev`, which is the order where both
2459
+ * apply; `call()` carries the argument for it. They are not nested: a
2460
+ * read-only navigation is `stale` and is not `stale-plan`, since the screen
2461
+ * moved and the selection did not. Opaque: the core minted it and the core
2462
+ * reads it, and every hop
2463
+ * in between — both relays, `connect()`, the Python client — forwards the
2464
+ * string untouched, exactly as they do `rev` and `screen`.
2465
+ */
2466
+ plan?: string;
2467
+ }
2468
+ //# sourceMappingURL=types.d.ts.map