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,666 @@
1
+ /**
2
+ * driverkit/check — the snapshot honesty checker.
3
+ *
4
+ * WHY A WRAPPER AND NOT A `mount({ strict: true })`, AND NOT AN `eval`
5
+ * SUBCOMMAND. Three shapes were available and the choice decides which checks
6
+ * are even possible, so the argument is here rather than in a changelog.
7
+ *
8
+ * A STRICT MODE inside `mount()` can only ever run the weak half of this list.
9
+ * The checks that matter are DIFFERENTIAL — call a verb and see whether `rev`
10
+ * moved, page a list and see whether `page` appeared, call `visible()` twice
11
+ * with nothing in between and see whether it changed. `call()` cannot drive
12
+ * the app to find out: a strict mode that called a `commit` to test whether
13
+ * `rev` bumps would be the library mutating an app's data to grade it, and one
14
+ * that called a paging verb would move the screen under the person watching.
15
+ * So the only thing a strict mode could check is what one resting snapshot
16
+ * says about itself — `rows` against `page.size`, `selected` against `rows` —
17
+ * which is the cheap third of what is here and the third least likely to be
18
+ * wrong. It would also have to decide what to DO on a failure. Refusing the
19
+ * call makes a dev flag change behaviour, which means dev and prod no longer
20
+ * run the same code; warning is what `warn()` in the core already does, and
21
+ * duplicating that mechanism behind a flag buys nothing.
22
+ *
23
+ * AN `eval` SUBCOMMAND puts a deterministic, free, offline check behind the
24
+ * one tool in the repository that needs a model and an API key. Honesty is not
25
+ * a model question — every check here is mechanical — and a check that costs
26
+ * money per run is a check somebody runs once before a demo instead of on
27
+ * every commit. `packages/eval` is also a development tool nobody installs
28
+ * (see `tsconfig.build.json`), so the check would not ship to the adopter at
29
+ * all.
30
+ *
31
+ * THE WRAPPER wins on the one thing that decides whether a checker is worth
32
+ * writing: whether an author actually runs it. It is a function they call from
33
+ * their own test file, against their own mounted driver, with no key and no
34
+ * network —
35
+ *
36
+ * import { assertHonest } from "driverkit/check";
37
+ * test("the snapshot tells the truth", () => assertHonest(mountMyApp()));
38
+ *
39
+ * — so it fails in the place an author is already looking when they break it.
40
+ * It is also the only one of the three that is itself testable against
41
+ * deliberately dishonest apps, which is how the checks in here are known to
42
+ * fire at all.
43
+ *
44
+ * WHAT IT COSTS A PRODUCTION APP: nothing, structurally rather than by
45
+ * promise. This file is a separate entry in the package's `exports` map, it is
46
+ * imported by nothing in `src/` outside this folder, and the core does not
47
+ * know it exists. An app that never imports `driverkit/check` does not bundle
48
+ * it, and `sideEffects: false` is what lets a bundler prove that. There is no
49
+ * flag to leave on in production because there is no flag.
50
+ *
51
+ * WHAT A DISHONEST `visible()` CAN STILL GET AWAY WITH is in `CHECKS` at the
52
+ * bottom of this file, and it is a long list. A checker that claimed
53
+ * completeness would be the thing not to trust.
54
+ */
55
+ /** Default budget for one `visible()`. See `CheckOptions.visibleBudgetMs`. */
56
+ const VISIBLE_BUDGET_MS = 1;
57
+ const VISIBLE_SAMPLES = 200;
58
+ /**
59
+ * Drive a mounted driver and report where its `visible()` is not telling the
60
+ * truth.
61
+ *
62
+ * Mutates the app, which is worth saying first: it calls the app's own `read`
63
+ * and `describe` verbs, so the screen moves and the selection changes. Point
64
+ * it at a driver you built for the test, not at one a person is looking at.
65
+ * `commit` verbs are left alone unless `CheckOptions.commits` says otherwise.
66
+ *
67
+ * Never throws for a finding — findings are the return value. It still throws
68
+ * for the things that are not findings: a driver that is unmounted, or an app
69
+ * whose `visible()` itself throws, because neither is a statement about
70
+ * honesty and reporting them as one would bury them.
71
+ */
72
+ export async function checkDriver(driver, options = {}) {
73
+ const findings = [];
74
+ const skipped = [];
75
+ let calls = 0;
76
+ /**
77
+ * Record a finding, ONCE.
78
+ *
79
+ * Deduped by check and message, and that is not tidiness. The static checks
80
+ * run against every snapshot the run produces — the resting one and the one
81
+ * after each call — because a defect that only appears on the third screen
82
+ * is exactly the one a single read would miss. The cost of that is that a
83
+ * standing defect, which is most of them, is rediscovered on every call and
84
+ * would be reported once per verb. An author handed eleven copies of one
85
+ * sentence reads it as eleven problems, starts counting instead of reading,
86
+ * and the one finding further down that was different gets lost.
87
+ *
88
+ * By MESSAGE and not by slug, because the messages carry the particulars:
89
+ * two verbs that each leave `rev` behind are two bugs and say so with two
90
+ * verb names, while one `rows`/`page.size` contradiction seen eleven times
91
+ * is one sentence eleven times.
92
+ */
93
+ const said = new Set();
94
+ const add = (f) => {
95
+ const key = `${f.check}\u0000${f.message}`;
96
+ if (said.has(key))
97
+ return;
98
+ said.add(key);
99
+ findings.push(f);
100
+ };
101
+ const manifest = driver.manifest();
102
+ // ---- visible() on its own -----------------------------------------------
103
+ //
104
+ // First, and before a single verb runs, because both of these are properties
105
+ // of the function rather than of any call — and because if `visible()`
106
+ // mutates, every differential check below is comparing against a snapshot
107
+ // the act of reading already changed, and the rest of this report would be
108
+ // noise. Said in that order in `impure`'s message.
109
+ const first = driver.snapshot();
110
+ const second = driver.snapshot();
111
+ const impure = stable(first) !== stable(second);
112
+ if (impure) {
113
+ add({
114
+ check: "visible-mutates",
115
+ severity: "lie",
116
+ message: "two `visible()` calls with nothing in between returned different snapshots. " +
117
+ "`call()` reads it twice per verb — once before the verb and once after — so " +
118
+ "whatever is changing is being attributed to the verb. If it is a timestamp or a " +
119
+ "`Math.random` in `where`, take it out: `rev` is how an agent is told something " +
120
+ "changed, and a snapshot that differs between the two reads makes `stale` fire on " +
121
+ "calls that were never stale. If it is a counter being incremented or a cache being " +
122
+ "filled, move it out of `visible()` — it is documented as a read.",
123
+ });
124
+ }
125
+ const visibleUs = timeVisible(driver, options.visibleSamples ?? VISIBLE_SAMPLES);
126
+ const budgetUs = (options.visibleBudgetMs ?? VISIBLE_BUDGET_MS) * 1000;
127
+ if (visibleUs > budgetUs) {
128
+ add({
129
+ check: "visible-expensive",
130
+ severity: "warn",
131
+ message: `\`visible()\` averaged ${Math.round(visibleUs)}µs, over the ` +
132
+ `${Math.round(budgetUs)}µs budget. It is documented as \`must be cheap: it runs on ` +
133
+ "each verb call`, and `call()` runs it twice, so this is paid twice per verb. The " +
134
+ "two usual causes are re-filtering or re-sorting the whole table in here instead of " +
135
+ "reading a window the app already has, and re-hashing a large selection for " +
136
+ "`selectionId` — keep a running value for that one.",
137
+ });
138
+ }
139
+ // ---- what one resting snapshot says about itself ------------------------
140
+ staticChecks(first, undefined, add);
141
+ manifestChecks(manifest, first, add);
142
+ // ---- then drive -----------------------------------------------------------
143
+ //
144
+ // `read` before `describe` before `commit`, which is `VerbKind`'s own ladder
145
+ // of what a wrong call costs you, cheapest first. Not the manifest's order:
146
+ // an app whose first verb is `accept-selected` would otherwise have its
147
+ // whole check run against a table that had already been emptied.
148
+ // `ask` is in here only so the sort has a number for it; the loop below
149
+ // skips every ask verb before it could be called. It sits at the cheap end
150
+ // because that is where an uncallable verb does least harm if the skip ever
151
+ // goes missing -- which would be a checker that stopped to ask somebody a
152
+ // question, and a checker is run by a test or a CLI with nobody in front of
153
+ // it.
154
+ const order = { ask: 0, read: 1, describe: 2, commit: 3, undeclared: 4 };
155
+ const verbs = [...manifest.verbs].sort((a, b) => order[a.kind ?? "undeclared"] - order[b.kind ?? "undeclared"]);
156
+ /** Row-id signatures seen on a screen while `page` was absent. See
157
+ * `window-without-page`: one screen showing two different row sets while
158
+ * claiming completeness for both is the detection. */
159
+ const completeClaims = new Map();
160
+ const noteClaim = (snapshot) => {
161
+ if (snapshot.page !== undefined)
162
+ return;
163
+ const rows = snapshot.rows;
164
+ if (rows === undefined || rows.length === 0)
165
+ return;
166
+ // Keyed by screen, falling back to `where`. A `read` verb that navigates
167
+ // legitimately shows a different set of rows with no `page`, because on the
168
+ // new screen those really are all the rows there are — so comparing across
169
+ // screens would report every multi-screen app. `where` is the weaker key
170
+ // and changes with the filter as well, which only makes this miss things;
171
+ // see `CHECKS`.
172
+ const key = snapshot.screen ?? snapshot.where;
173
+ const sig = rows.map((r) => r.id).join("\u0000");
174
+ const was = completeClaims.get(key);
175
+ if (was === undefined) {
176
+ completeClaims.set(key, sig);
177
+ return;
178
+ }
179
+ if (was === sig)
180
+ return;
181
+ add({
182
+ check: "window-without-page",
183
+ severity: "lie",
184
+ message: `the \`${key}\` screen has now shown two different sets of rows with no \`page\` ` +
185
+ "block. An absent `page` is a claim that these are all the rows there are — the " +
186
+ 'renderer says "rows N (all of them)" on the strength of it — and a screen that ' +
187
+ "can show a different set is a window. Publish `page: { index, size }` and leave " +
188
+ "`total` off if you do not know it; that third state exists exactly so a window " +
189
+ "does not have to be reported as a whole.",
190
+ });
191
+ };
192
+ noteClaim(first);
193
+ for (const verb of verbs) {
194
+ if (options.verbs && !options.verbs.includes(verb.name)) {
195
+ skipped.push({ verb: verb.name, why: "not in `options.verbs`" });
196
+ continue;
197
+ }
198
+ if (verb.kind === "commit" && !options.commits) {
199
+ skipped.push({
200
+ verb: verb.name,
201
+ why: "a commit, and `options.commits` is off. `rev-did-not-move` is the check this costs you",
202
+ });
203
+ continue;
204
+ }
205
+ if (verb.kind === "ask") {
206
+ // NEVER CALLED, and this is the one skip in here that is about the person
207
+ // rather than about the app. `ask` puts a question in front of somebody
208
+ // and holds the driver's lock until they answer; a checker is run by a
209
+ // CLI, a test or CI, where the honest answer is that nobody is there, so
210
+ // calling it would either interrupt a person who did not ask to be
211
+ // interrupted or park the whole check on the ask deadline. There is also
212
+ // nothing here to check: the verb's body is the core's, not the app's.
213
+ skipped.push({
214
+ verb: verb.name,
215
+ why: "a question for the person, and nobody is watching a check run",
216
+ });
217
+ continue;
218
+ }
219
+ if (verb.consumes === "plan") {
220
+ // Not callable without a token, and a token comes from the `describe`
221
+ // that minted it over the selection it left behind — which this checker
222
+ // cannot pair up without knowing which describe goes with which commit.
223
+ // Refusing to guess: a `stale-plan` refusal reported as a finding would
224
+ // be the checker's bug dressed as the app's.
225
+ skipped.push({ verb: verb.name, why: "consumes a plan, which this checker does not mint" });
226
+ continue;
227
+ }
228
+ const live = driver.snapshot();
229
+ if (!onScreen(verb.screen, live.screen)) {
230
+ skipped.push({
231
+ verb: verb.name,
232
+ why: `scoped to ${JSON.stringify(verb.screen)} and the app is on \`${live.screen}\``,
233
+ });
234
+ continue;
235
+ }
236
+ const args = argsFor(verb, options.args?.[verb.name]);
237
+ if (args === null) {
238
+ skipped.push({
239
+ verb: verb.name,
240
+ why: "has a required argument with no `oneOf`; pass one in `options.args` to check it",
241
+ });
242
+ continue;
243
+ }
244
+ const before = live;
245
+ calls++;
246
+ const result = await driver.call(verb.name, args);
247
+ const after = result.snapshot;
248
+ if (!result.ok) {
249
+ // A refusal is not a finding. The checker called the verb with made-up
250
+ // arguments on a screen it did not set up, and the app saying no is the
251
+ // app working. Recorded in `skipped` so the coverage stays honest: this
252
+ // verb told us nothing.
253
+ skipped.push({ verb: verb.name, why: `refused \`${result.refused}\`` });
254
+ continue;
255
+ }
256
+ staticChecks(after, verb.name, add);
257
+ noteClaim(after);
258
+ if (after.rev < before.rev) {
259
+ add({
260
+ check: "rev-went-backwards",
261
+ severity: "lie",
262
+ verb: verb.name,
263
+ message: `\`rev\` went from ${before.rev} to ${after.rev} across \`${verb.name}\`. It is ` +
264
+ "documented as monotonic, and every agent holding the older number will now pass " +
265
+ "the `stale` check with a snapshot it has never seen. If this is a hash or a " +
266
+ "version string, use a counter.",
267
+ });
268
+ }
269
+ else if (after.rev === before.rev && stable(bodyOf(before)) !== stable(bodyOf(after))) {
270
+ // The strong form of the check, and the reason it is this and not "a
271
+ // describe must bump rev". A `describe` that matched nothing changes
272
+ // nothing and must NOT bump — reporting that would be a false positive
273
+ // on a correct app. What is never correct is the snapshot CHANGING while
274
+ // `rev` stands still, and that is what is compared: everything but `rev`,
275
+ // before against after.
276
+ add({
277
+ check: "rev-did-not-move",
278
+ severity: "lie",
279
+ verb: verb.name,
280
+ message: `\`${verb.name}\` changed the snapshot and left \`rev\` at ${before.rev}. This is ` +
281
+ "the one that defeats everything quietly: `stale` cannot refuse a call made " +
282
+ "against the screen before this one, and a plan token minted here binds a `rev` " +
283
+ "that no longer identifies anything, so a `commit` acts on a selection nobody " +
284
+ "counted. Bump `rev` wherever the app's state changes — once, in the setter — not " +
285
+ "in each verb, which is how one verb comes to forget.",
286
+ });
287
+ }
288
+ if (verb.kind === "describe" && result.count === undefined) {
289
+ add({
290
+ check: "describe-without-count",
291
+ severity: "lie",
292
+ verb: verb.name,
293
+ message: `\`${verb.name}\` declares \`kind: "describe"\` and returned no \`count\`. The ` +
294
+ "count is the whole reason `describe` is a separate kind from `read`: it is the " +
295
+ "number a person approves before a `commit` moves anything, and without it the " +
296
+ "describe/commit pairing is advisory. Return `{ count }` — including `0`, which " +
297
+ 'is a real answer and not the same as "I do not count" — or declare this `read`.',
298
+ });
299
+ }
300
+ }
301
+ return {
302
+ findings,
303
+ ok: !findings.some((f) => f.severity === "lie"),
304
+ skipped,
305
+ calls,
306
+ visibleUs,
307
+ };
308
+ }
309
+ /**
310
+ * `checkDriver`, but it throws on a lie. The one-liner for a test file.
311
+ *
312
+ * Throws on `"lie"` only, and prints the warnings into the message of the
313
+ * error it throws when there is one. A warning on its own is not a failure:
314
+ * `visible-expensive` is a judgement about an app's own performance budget and
315
+ * a suite that failed on it would be a suite somebody turns off.
316
+ */
317
+ export async function assertHonest(driver, options = {}) {
318
+ const report = await checkDriver(driver, options);
319
+ if (report.ok)
320
+ return report;
321
+ const lines = report.findings.map((f) => ` [${f.severity}] ${f.check}${f.verb ? ` (${f.verb})` : ""}: ${f.message}`);
322
+ // The coverage goes in the failure too. An author reading this is about to
323
+ // fix one line in `visible()`, and the next thing they should know is how
324
+ // much of their app was never looked at.
325
+ const cover = report.skipped.length
326
+ ? `\nnot checked: ${report.skipped.map((s) => `${s.verb} (${s.why})`).join("; ")}`
327
+ : "";
328
+ throw new Error(`driverkit/check: this app's \`visible()\` is not telling the truth.\n${lines.join("\n")}${cover}`);
329
+ }
330
+ // ---------------------------------------------------------------------------
331
+ /** Everything a single snapshot can contradict about itself. */
332
+ function staticChecks(s, verb, add) {
333
+ const at = verb === undefined ? {} : { verb };
334
+ const rows = s.rows;
335
+ const page = s.page;
336
+ if (rows !== undefined) {
337
+ const ids = rows.map((r) => r.id);
338
+ const seen = new Set();
339
+ for (const id of ids) {
340
+ if (id === undefined || id === "") {
341
+ add({
342
+ check: "row-without-id",
343
+ severity: "lie",
344
+ ...at,
345
+ message: "a row in `rows` has no `id`. `id` is what verbs take, so a row without one is " +
346
+ "a row an agent can read about and cannot act on, and `selected` cannot refer " +
347
+ "to it either.",
348
+ });
349
+ break;
350
+ }
351
+ if (seen.has(id)) {
352
+ add({
353
+ check: "duplicate-row-id",
354
+ severity: "lie",
355
+ ...at,
356
+ message: `two rows on screen share the id \`${id}\`. An agent resolving "this one" to an ` +
357
+ "id has no way to say which, and a per-row control addressed by id reaches " +
358
+ "whichever the app happens to find first.",
359
+ });
360
+ break;
361
+ }
362
+ seen.add(id);
363
+ }
364
+ if (page !== undefined) {
365
+ if (!(page.size > 0)) {
366
+ add({
367
+ check: "page-size-not-positive",
368
+ severity: "lie",
369
+ ...at,
370
+ message: `\`page.size\` is ${page.size}. The renderer computes the row numbers a model ` +
371
+ "reads from `index * size`, so a zero or negative size prints a window that " +
372
+ "does not exist.",
373
+ });
374
+ }
375
+ else if (rows.length > page.size) {
376
+ add({
377
+ check: "rows-exceed-page-size",
378
+ severity: "lie",
379
+ ...at,
380
+ message: `\`rows\` has ${rows.length} entries and \`page.size\` is ${page.size}. One of ` +
381
+ "the two is wrong, and either way the agent is being shown rows the person at " +
382
+ "the keyboard is not — which is the one rule `Snapshot` exists to enforce.",
383
+ });
384
+ }
385
+ if (page.index < 0 || !Number.isInteger(page.index)) {
386
+ add({
387
+ check: "page-index-not-a-page",
388
+ severity: "lie",
389
+ ...at,
390
+ message: `\`page.index\` is ${page.index}; it is a zero-based page number.`,
391
+ });
392
+ }
393
+ const seenThrough = page.index * page.size + rows.length;
394
+ if (page.total !== undefined && page.total < seenThrough) {
395
+ add({
396
+ check: "page-total-below-window",
397
+ severity: "lie",
398
+ ...at,
399
+ message: `\`page.total\` is ${page.total} but this window already reaches row ` +
400
+ `${seenThrough}. The renderer prints "rows N-M of ${page.total}" from these, so ` +
401
+ "a model is being told the table ends before the rows it can already see. If " +
402
+ "the total is not known, leave `total` off — that third state exists for this.",
403
+ });
404
+ }
405
+ }
406
+ }
407
+ const selected = s.selected;
408
+ if (selected !== undefined && selected.length > 0) {
409
+ if (rows !== undefined) {
410
+ const onScreenIds = new Set(rows.map((r) => r.id));
411
+ const strays = selected.filter((id) => !onScreenIds.has(id));
412
+ if (strays.length > 0) {
413
+ add({
414
+ check: "selected-not-on-screen",
415
+ severity: "lie",
416
+ ...at,
417
+ message: `\`selected\` names ${strays.length} id(s) that are not in \`rows\` ` +
418
+ `(${strays.slice(0, 3).join(", ")}${strays.length > 3 ? ", …" : ""}). It is ` +
419
+ 'documented as on-screen ids only — it answers "which of the things in front of ' +
420
+ 'me is \'this one\'" — and the size of the selection belongs in `selectedCount`. ' +
421
+ "Filling it with off-screen ids is also the token-budget mistake its own comment " +
422
+ "is about: `select-where` selects thousands, and thousands of ids ride in the " +
423
+ "uncached half of every result from then on.",
424
+ });
425
+ }
426
+ }
427
+ if (s.selectedCount === undefined) {
428
+ add({
429
+ check: "selected-count-missing",
430
+ severity: "warn",
431
+ ...at,
432
+ message: `\`selected\` carries ${selected.length} id(s) and \`selectedCount\` is absent. ` +
433
+ 'Absent means "I do not count", so a model handed these ids and a bulk commit ' +
434
+ "will believe that is the whole selection. If the number is knowable, publish it; " +
435
+ "if the selection really is only what is on screen, publish it anyway — " +
436
+ "`selectedCount: n` and an absent one are different claims and only one of them " +
437
+ "is yours.",
438
+ });
439
+ }
440
+ else if (s.selectedCount < selected.length) {
441
+ add({
442
+ check: "selected-count-below-selected",
443
+ severity: "lie",
444
+ ...at,
445
+ message: `\`selectedCount\` is ${s.selectedCount} and \`selected\` lists ` +
446
+ `${selected.length} ids. \`selected\` is a subset of the selection — the part on ` +
447
+ "screen — so the count can never be smaller than it.",
448
+ });
449
+ }
450
+ }
451
+ }
452
+ /** What the manifest promises, against what the snapshot provides. Two checks
453
+ * the core also warns about once at call time; they are repeated here because
454
+ * a console warning in a browser tab is not a thing a test suite fails on. */
455
+ function manifestChecks(manifest, s, add) {
456
+ if (s.screen === undefined) {
457
+ const scoped = manifest.verbs.filter((v) => v.screen !== undefined).map((v) => v.name);
458
+ if (scoped.length > 0) {
459
+ add({
460
+ check: "screen-scoped-verbs-without-screen",
461
+ severity: "lie",
462
+ message: `${scoped.length} verb(s) are scoped to a screen (${scoped.slice(0, 3).join(", ")}` +
463
+ `${scoped.length > 3 ? ", …" : ""}) and \`visible()\` returns no \`screen\`, so the ` +
464
+ "check they asked for never runs. Every one of them is callable from every screen, " +
465
+ "which is the `next-page` landing on another company's list that `Verb.screen` " +
466
+ "exists to stop. Fill in `Snapshot.screen`, or drop `screen` from the verbs.",
467
+ });
468
+ }
469
+ }
470
+ if (s.selectionId === undefined) {
471
+ const consuming = manifest.verbs.filter((v) => v.consumes === "plan").map((v) => v.name);
472
+ if (consuming.length > 0) {
473
+ add({
474
+ check: "plan-without-selection-id",
475
+ severity: "warn",
476
+ message: `${consuming.join(", ")} consume a plan and \`visible()\` publishes no ` +
477
+ "`selectionId`, so the token binds the `rev` and the count and nothing about the " +
478
+ "selection itself. It still catches every change the app bumps `rev` for — which " +
479
+ "is why this is a warning — and it cannot catch one row being swapped for another " +
480
+ "at the same `rev`. `selectionId([...ids])` is exported for this.",
481
+ });
482
+ }
483
+ }
484
+ }
485
+ /** The snapshot minus `rev`, for the did-it-change comparison. Built by
486
+ * rebuilding rather than by deleting a key, so the field order is this
487
+ * function's and not the app's — two snapshots from the same `visible()` have
488
+ * the same order anyway, but a `delete` on a caller's object would be this
489
+ * checker mutating the app it is grading. */
490
+ function bodyOf(s) {
491
+ const { rev: _rev, ...body } = s;
492
+ return body;
493
+ }
494
+ /**
495
+ * A stable string for a snapshot.
496
+ *
497
+ * `JSON.stringify` with sorted keys at every level, because two snapshots from
498
+ * the same `visible()` agree on key order and two from different code paths in
499
+ * the same `visible()` do not — an app that spreads `...(selected.length ? {
500
+ * selected } : {})` emits the key in one branch and not the other, and a
501
+ * plain stringify would call that a change.
502
+ */
503
+ function stable(value) {
504
+ return JSON.stringify(value, (_key, v) => {
505
+ if (v === null || typeof v !== "object" || Array.isArray(v))
506
+ return v;
507
+ const out = {};
508
+ for (const k of Object.keys(v).sort()) {
509
+ out[k] = v[k];
510
+ }
511
+ return out;
512
+ });
513
+ }
514
+ /** Mean microseconds per `visible()`. `performance.now()` where there is one —
515
+ * `Date.now()` has a millisecond floor, and the budget being checked is one
516
+ * millisecond, so a correct app would measure as exactly 0 or exactly over. */
517
+ function timeVisible(driver, samples) {
518
+ const now = typeof globalThis.performance?.now === "function"
519
+ ? () => globalThis.performance.now()
520
+ : () => Date.now();
521
+ const n = Math.max(1, samples);
522
+ // Warm once outside the measurement. A first call that fills a lazy cache is
523
+ // a real cost an author should know about, but charging it to the mean over
524
+ // 200 samples reports it as 1/200th of itself and as nothing at all.
525
+ driver.snapshot();
526
+ const start = now();
527
+ for (let i = 0; i < n; i++)
528
+ driver.snapshot();
529
+ return ((now() - start) * 1000) / n;
530
+ }
531
+ /** Whether a verb's declared screen covers the live one. Absent on either side
532
+ * means there is nothing to check, which is the core's own rule. */
533
+ function onScreen(wants, live) {
534
+ if (wants === undefined || live === undefined)
535
+ return true;
536
+ return Array.isArray(wants) ? wants.includes(live) : wants === live;
537
+ }
538
+ /** Arguments to call a verb with, or `null` for "cannot, without guessing". */
539
+ function argsFor(verb, given) {
540
+ const spec = verb.args;
541
+ if (!spec)
542
+ return given ?? {};
543
+ const out = { ...given };
544
+ for (const [name, arg] of Object.entries(spec)) {
545
+ if (out[name] !== undefined)
546
+ continue;
547
+ if (arg.optional)
548
+ continue;
549
+ const choice = arg.oneOf?.[0];
550
+ if (choice !== undefined) {
551
+ out[name] = choice;
552
+ continue;
553
+ }
554
+ return null;
555
+ }
556
+ return out;
557
+ }
558
+ /**
559
+ * Every check, and — the part worth reading — what each one cannot see.
560
+ *
561
+ * A checker that claims completeness is the one not to trust, so this list is
562
+ * written from the other end: here is what a dishonest `visible()` still gets
563
+ * away with after a clean run.
564
+ *
565
+ * THE CHECKS
566
+ *
567
+ * visible-mutates two reads, nothing between, different answers.
568
+ * visible-expensive mean over the budget; it runs twice per call.
569
+ * row-without-id a row an agent cannot name.
570
+ * duplicate-row-id two rows one agent cannot tell apart.
571
+ * page-size-not-positive a window with no width.
572
+ * rows-exceed-page-size more rows than the app says fit.
573
+ * page-index-not-a-page a negative or fractional page number.
574
+ * page-total-below-window a total smaller than what is on screen.
575
+ * selected-not-on-screen `selected` naming rows nobody can see.
576
+ * selected-count-missing ids with no count: reads as the whole selection.
577
+ * selected-count-below-selected a total smaller than its own on-screen part.
578
+ * screen-scoped-verbs-without-screen a check the app asked for that cannot run.
579
+ * plan-without-selection-id a token that binds `rev` and nothing else.
580
+ * window-without-page one screen, two row sets, completeness claimed twice.
581
+ * rev-went-backwards not monotonic; old revs start passing `stale`.
582
+ * rev-did-not-move the screen changed and `rev` did not.
583
+ * describe-without-count the pairing rule's number, missing.
584
+ *
585
+ * WHAT STILL GETS THROUGH
586
+ *
587
+ * 1. **Rows that are simply false.** Nothing here reads the app's data, so a
588
+ * `visible()` that reports a status of "new" for a row the screen shows as
589
+ * "accepted", or that hides the three rows a filter should have matched, is
590
+ * invisible to every check above. This is the largest hole and it is not
591
+ * closable from here: the only oracle for "is this what the person can see"
592
+ * is the rendered page, which is what `driverkit/controls` reads and what
593
+ * an end-to-end test asserts. The checker verifies the snapshot's INTERNAL
594
+ * consistency and its consistency WITH THE APP'S OWN VERBS. It cannot
595
+ * verify it against the screen.
596
+ *
597
+ * 2. **A `rev` that moves when nothing changed.** The inverse of
598
+ * `rev-did-not-move`, and deliberately not checked: an app that bumps on
599
+ * every tick is noisy and causes spurious `stale` refusals, but `rev` is
600
+ * documented as monotonic and bumped on change, not as bumped ONLY on
601
+ * change, and an app that bumps for a change the snapshot does not expose
602
+ * is correct. Flagging it would report correct apps.
603
+ *
604
+ * 3. **A window whose rows never change.** `window-without-page` needs two
605
+ * different row sets on one screen to fire. An app with a single screen, no
606
+ * paging verb and a virtualised grid showing the first twenty of a million
607
+ * rows with no `page` block is exactly the case the three-state rule was
608
+ * added for, and it is undetectable here: every call shows the same twenty
609
+ * rows and the lie never contradicts itself. A paging verb with a required
610
+ * argument and nothing in `options.args` lands in the same place.
611
+ *
612
+ * 4. **`selectedCount` that is merely wrong.** A count of 5 on a selection of
613
+ * 3,412 passes everything above as long as it is not smaller than
614
+ * `selected.length`. The checker has no independent count; the app is the
615
+ * only thing that knows. Same for `page.total`, which is checked only
616
+ * against the window it must not undercut.
617
+ *
618
+ * 5. **`selectionId` that is constant.** It is checked for presence, never for
619
+ * the one property it has to have. A `selectionId: "sel"` returned forever
620
+ * makes every plan token pass, and provoking it would need the checker to
621
+ * change the selection two ways and compare — which needs predicates it
622
+ * cannot invent. Pass two `describe` calls through `options.args` and a
623
+ * future check could; today it cannot.
624
+ *
625
+ * 6. **Anything behind a commit, when `commits` is off** — which is the
626
+ * default. `rev-did-not-move` is most likely to fire on a `commit`, because
627
+ * a commit is where an author writes to a store they forgot to wrap. A run
628
+ * with the default reports every commit in `skipped` for exactly this
629
+ * reason, and an author with a disposable fixture should turn it on.
630
+ *
631
+ * 7. **Anything behind a required argument.** `argsFor` refuses to guess, so
632
+ * `select-where` is never called unless the author says what to select. The
633
+ * verbs most worth driving are the ones that take a predicate.
634
+ *
635
+ * 8. **Dishonesty that only appears under load or over time.** Every check
636
+ * here runs in one process in a few milliseconds. A `visible()` that is
637
+ * cheap on twenty rows and quadratic on twenty thousand measures as cheap,
638
+ * and a `rev` that is bumped by a websocket but not by a verb looks correct
639
+ * because no websocket landed.
640
+ *
641
+ * 9. **`where`, `note` and every other sentence.** They are prose for a model
642
+ * and nothing mechanical can tell a true sentence about a screen from a
643
+ * false one. `where` claiming "filtered to Acme" over unfiltered rows is
644
+ * the kind of thing `packages/eval` catches by accident, through a model
645
+ * doing the wrong thing with it, and this never will.
646
+ */
647
+ export const CHECKS = [
648
+ "visible-mutates",
649
+ "visible-expensive",
650
+ "row-without-id",
651
+ "duplicate-row-id",
652
+ "page-size-not-positive",
653
+ "rows-exceed-page-size",
654
+ "page-index-not-a-page",
655
+ "page-total-below-window",
656
+ "selected-not-on-screen",
657
+ "selected-count-missing",
658
+ "selected-count-below-selected",
659
+ "screen-scoped-verbs-without-screen",
660
+ "plan-without-selection-id",
661
+ "window-without-page",
662
+ "rev-went-backwards",
663
+ "rev-did-not-move",
664
+ "describe-without-count",
665
+ ];
666
+ //# sourceMappingURL=check.js.map