@crowandraven/evalkeeper 0.9.0 → 0.11.0

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 (246) hide show
  1. package/README.md +1 -2
  2. package/dist/ai-sdk/decide.d.ts +35 -0
  3. package/dist/ai-sdk/decide.js +36 -0
  4. package/dist/ai-sdk/decide.js.map +1 -0
  5. package/dist/ai-sdk/hash.d.ts +50 -0
  6. package/dist/ai-sdk/hash.js +59 -0
  7. package/dist/ai-sdk/hash.js.map +1 -0
  8. package/dist/ai-sdk/index.d.ts +15 -0
  9. package/dist/ai-sdk/index.js +16 -0
  10. package/dist/ai-sdk/index.js.map +1 -0
  11. package/dist/ai-sdk/judge.d.ts +69 -0
  12. package/dist/ai-sdk/judge.js +105 -0
  13. package/dist/ai-sdk/judge.js.map +1 -0
  14. package/dist/ai-sdk/usage.d.ts +18 -0
  15. package/dist/ai-sdk/usage.js +19 -0
  16. package/dist/ai-sdk/usage.js.map +1 -0
  17. package/dist/board.d.ts +5 -0
  18. package/dist/cli/addPages.d.ts +6 -1
  19. package/dist/cli/addPages.js +14 -8
  20. package/dist/cli/addPages.js.map +1 -1
  21. package/dist/cli/main.d.ts +19 -5
  22. package/dist/cli/main.js +100 -4
  23. package/dist/cli/main.js.map +1 -1
  24. package/dist/cli/migration.d.ts +95 -0
  25. package/dist/cli/migration.js +247 -0
  26. package/dist/cli/migration.js.map +1 -0
  27. package/dist/feedback.d.ts +1 -1
  28. package/dist/glossary.d.ts +9 -0
  29. package/dist/glossary.js +9 -0
  30. package/dist/glossary.js.map +1 -1
  31. package/dist/health.d.ts +4 -1
  32. package/dist/health.js +4 -2
  33. package/dist/health.js.map +1 -1
  34. package/dist/inInstructions.d.ts +1 -1
  35. package/dist/inInstructions.js +1 -1
  36. package/dist/next/RefreshWhile.d.ts +11 -0
  37. package/dist/next/RefreshWhile.js +28 -0
  38. package/dist/next/RefreshWhile.js.map +1 -0
  39. package/dist/next/actions.d.ts +14 -0
  40. package/dist/next/actions.js +332 -0
  41. package/dist/next/actions.js.map +1 -0
  42. package/dist/next/components.d.ts +9 -0
  43. package/dist/next/components.js +27 -0
  44. package/dist/next/components.js.map +1 -0
  45. package/dist/next/cron.d.ts +9 -0
  46. package/dist/next/cron.js +17 -0
  47. package/dist/next/cron.js.map +1 -0
  48. package/dist/next/index.d.ts +15 -0
  49. package/dist/next/index.js +16 -0
  50. package/dist/next/index.js.map +1 -0
  51. package/dist/next/pages.d.ts +70 -0
  52. package/dist/next/pages.js +77 -0
  53. package/dist/next/pages.js.map +1 -0
  54. package/dist/next/route.d.ts +19 -0
  55. package/dist/next/route.js +31 -0
  56. package/dist/next/route.js.map +1 -0
  57. package/dist/next/weekly.d.ts +20 -0
  58. package/dist/next/weekly.js +55 -0
  59. package/dist/next/weekly.js.map +1 -0
  60. package/dist/next/wiring.d.ts +19 -0
  61. package/dist/next/wiring.js +2 -0
  62. package/dist/next/wiring.js.map +1 -0
  63. package/dist/prisma/contracts.d.ts +74 -0
  64. package/dist/prisma/contracts.js +178 -0
  65. package/dist/prisma/contracts.js.map +1 -0
  66. package/dist/prisma/index.d.ts +423 -0
  67. package/dist/prisma/index.js +622 -0
  68. package/dist/prisma/index.js.map +1 -0
  69. package/dist/prisma/mapping.d.ts +63 -0
  70. package/dist/prisma/mapping.js +111 -0
  71. package/dist/prisma/mapping.js.map +1 -0
  72. package/dist/prisma/moveIn.d.ts +106 -0
  73. package/dist/prisma/moveIn.js +299 -0
  74. package/dist/prisma/moveIn.js.map +1 -0
  75. package/dist/prune.d.ts +45 -0
  76. package/dist/prune.js +32 -0
  77. package/dist/prune.js.map +1 -1
  78. package/dist/react/AddCase.d.ts +10 -0
  79. package/dist/react/AddCase.js +19 -0
  80. package/dist/react/AddCase.js.map +1 -0
  81. package/dist/react/AnswerKey.d.ts +22 -0
  82. package/dist/react/AnswerKey.js +83 -0
  83. package/dist/react/AnswerKey.js.map +1 -0
  84. package/dist/react/CaseMenu.d.ts +17 -0
  85. package/dist/react/CaseMenu.js +41 -0
  86. package/dist/react/CaseMenu.js.map +1 -0
  87. package/dist/react/CompareView.d.ts +14 -0
  88. package/dist/react/CompareView.js +51 -0
  89. package/dist/react/CompareView.js.map +1 -0
  90. package/dist/react/ConfirmAnswer.d.ts +13 -0
  91. package/dist/react/ConfirmAnswer.js +27 -0
  92. package/dist/react/ConfirmAnswer.js.map +1 -0
  93. package/dist/react/DisagreementChip.d.ts +10 -0
  94. package/dist/react/DisagreementChip.js +21 -0
  95. package/dist/react/DisagreementChip.js.map +1 -0
  96. package/dist/react/FeedbackView.d.ts +10 -0
  97. package/dist/react/FeedbackView.js +26 -0
  98. package/dist/react/FeedbackView.js.map +1 -0
  99. package/dist/react/Fields.d.ts +32 -0
  100. package/dist/react/Fields.js +33 -0
  101. package/dist/react/Fields.js.map +1 -0
  102. package/dist/react/HubSections.d.ts +78 -0
  103. package/dist/react/HubSections.js +124 -0
  104. package/dist/react/HubSections.js.map +1 -0
  105. package/dist/react/ModelMenu.d.ts +16 -0
  106. package/dist/react/ModelMenu.js +38 -0
  107. package/dist/react/ModelMenu.js.map +1 -0
  108. package/dist/react/RateView.d.ts +18 -0
  109. package/dist/react/RateView.js +30 -0
  110. package/dist/react/RateView.js.map +1 -0
  111. package/dist/react/ReadableOutput.d.ts +3 -0
  112. package/dist/react/ReadableOutput.js +50 -0
  113. package/dist/react/ReadableOutput.js.map +1 -0
  114. package/dist/react/RollBack.d.ts +17 -0
  115. package/dist/react/RollBack.js +26 -0
  116. package/dist/react/RollBack.js.map +1 -0
  117. package/dist/react/RunView.d.ts +9 -0
  118. package/dist/react/RunView.js +41 -0
  119. package/dist/react/RunView.js.map +1 -0
  120. package/dist/react/SettingsHistory.d.ts +33 -0
  121. package/dist/react/SettingsHistory.js +79 -0
  122. package/dist/react/SettingsHistory.js.map +1 -0
  123. package/dist/react/SideBySideView.d.ts +14 -0
  124. package/dist/react/SideBySideView.js +67 -0
  125. package/dist/react/SideBySideView.js.map +1 -0
  126. package/dist/react/SuiteSections.d.ts +111 -0
  127. package/dist/react/SuiteSections.js +224 -0
  128. package/dist/react/SuiteSections.js.map +1 -0
  129. package/dist/react/Tabs.d.ts +15 -0
  130. package/dist/react/Tabs.js +18 -0
  131. package/dist/react/Tabs.js.map +1 -0
  132. package/dist/react/Thumbs.d.ts +13 -0
  133. package/dist/react/Thumbs.js +54 -0
  134. package/dist/react/Thumbs.js.map +1 -0
  135. package/dist/react/WeeklyCheck.d.ts +11 -0
  136. package/dist/react/WeeklyCheck.js +19 -0
  137. package/dist/react/WeeklyCheck.js.map +1 -0
  138. package/dist/react/app.d.ts +118 -0
  139. package/dist/react/app.js +87 -0
  140. package/dist/react/app.js.map +1 -0
  141. package/dist/react/format.d.ts +27 -0
  142. package/dist/react/format.js +38 -0
  143. package/dist/react/format.js.map +1 -0
  144. package/dist/react/index.d.ts +31 -0
  145. package/dist/react/index.js +32 -0
  146. package/dist/react/index.js.map +1 -0
  147. package/dist/react/pieces.d.ts +191 -0
  148. package/dist/react/pieces.js +191 -0
  149. package/dist/react/pieces.js.map +1 -0
  150. package/dist/readCheck.d.ts +1 -1
  151. package/dist/runner.d.ts +76 -3
  152. package/dist/runner.js +133 -3
  153. package/dist/runner.js.map +1 -1
  154. package/dist/settings.d.ts +1 -1
  155. package/dist/store.d.ts +1 -1
  156. package/dist/suggest.d.ts +52 -2
  157. package/dist/suggest.js +101 -8
  158. package/dist/suggest.js.map +1 -1
  159. package/dist/suiteContract.d.ts +3 -3
  160. package/dist/suiteContract.js +20 -16
  161. package/dist/suiteContract.js.map +1 -1
  162. package/dist/testing.d.ts +2 -2
  163. package/dist/testing.js +1 -1
  164. package/dist/types.d.ts +79 -2
  165. package/dist/types.js +96 -12
  166. package/dist/types.js.map +1 -1
  167. package/package.json +56 -6
  168. package/schema/evalkeeper.prisma +196 -0
  169. package/src/ai-sdk/decide.ts +57 -0
  170. package/src/ai-sdk/hash.ts +74 -0
  171. package/src/ai-sdk/index.ts +15 -0
  172. package/src/ai-sdk/judge.ts +163 -0
  173. package/src/ai-sdk/usage.ts +31 -0
  174. package/src/board.ts +5 -0
  175. package/src/cli/addPages.ts +15 -7
  176. package/src/cli/main.ts +135 -9
  177. package/src/cli/migration.ts +349 -0
  178. package/src/feedback.ts +1 -1
  179. package/src/glossary.ts +18 -0
  180. package/src/health.ts +4 -2
  181. package/src/inInstructions.ts +1 -1
  182. package/{templates/components/evalkeeper → src/next}/RefreshWhile.tsx +2 -2
  183. package/src/next/actions.ts +389 -0
  184. package/src/next/components.ts +31 -0
  185. package/{templates/lib/evalkeeper/cronAuth.ts → src/next/cron.ts} +3 -0
  186. package/src/next/index.ts +15 -0
  187. package/src/next/pages.tsx +106 -0
  188. package/src/next/route.ts +35 -0
  189. package/src/next/weekly.ts +70 -0
  190. package/src/next/wiring.ts +18 -0
  191. package/src/prisma/contracts.ts +249 -0
  192. package/src/prisma/index.ts +1169 -0
  193. package/src/prisma/mapping.ts +147 -0
  194. package/src/prisma/moveIn.ts +502 -0
  195. package/src/prune.ts +77 -0
  196. package/{templates/components/evalkeeper → src/react}/AddCase.tsx +6 -6
  197. package/{templates/components/evalkeeper → src/react}/AnswerKey.tsx +96 -59
  198. package/{templates/components/evalkeeper → src/react}/CaseMenu.tsx +9 -8
  199. package/src/react/CompareView.tsx +270 -0
  200. package/{templates/components/evalkeeper → src/react}/ConfirmAnswer.tsx +6 -3
  201. package/{templates/components/evalkeeper → src/react}/DisagreementChip.tsx +2 -3
  202. package/src/react/FeedbackView.tsx +230 -0
  203. package/{templates/components/evalkeeper → src/react}/Fields.tsx +54 -10
  204. package/src/react/HubSections.tsx +446 -0
  205. package/src/react/ModelMenu.tsx +89 -0
  206. package/src/react/RateView.tsx +143 -0
  207. package/{templates/components/evalkeeper → src/react}/ReadableOutput.tsx +1 -1
  208. package/{templates/components/evalkeeper → src/react}/RollBack.tsx +9 -10
  209. package/src/react/RunView.tsx +151 -0
  210. package/{templates/components/evalkeeper → src/react}/SettingsHistory.tsx +16 -9
  211. package/src/react/SideBySideView.tsx +307 -0
  212. package/src/react/SuiteSections.tsx +925 -0
  213. package/src/react/Tabs.tsx +61 -0
  214. package/{templates/components/evalkeeper → src/react}/Thumbs.tsx +7 -6
  215. package/{templates/components/evalkeeper → src/react}/WeeklyCheck.tsx +31 -20
  216. package/src/react/app.ts +173 -0
  217. package/src/react/format.ts +45 -0
  218. package/src/react/index.ts +31 -0
  219. package/src/react/pieces.tsx +412 -0
  220. package/src/readCheck.ts +1 -1
  221. package/src/runner.ts +182 -4
  222. package/src/settings.ts +1 -1
  223. package/src/store.ts +1 -1
  224. package/src/suggest.ts +145 -13
  225. package/src/suiteContract.ts +35 -28
  226. package/src/testing.ts +2 -2
  227. package/src/types.ts +147 -19
  228. package/starter/index.ts +33 -8
  229. package/templates/app/api/cron/evals-weekly/route.ts +5 -15
  230. package/templates/app/evals/[suite]/compare/page.tsx +6 -383
  231. package/templates/app/evals/[suite]/page.tsx +7 -532
  232. package/templates/app/evals/[suite]/rate/page.tsx +6 -141
  233. package/templates/app/evals/[suite]/runs/[runId]/page.tsx +6 -174
  234. package/templates/app/evals/[suite]/side-by-side/page.tsx +6 -318
  235. package/templates/app/evals/feedback/page.tsx +6 -227
  236. package/templates/app/evals/layout.tsx +10 -0
  237. package/templates/app/evals/page.tsx +7 -365
  238. package/templates/lib/evalkeeper/actions.ts +34 -296
  239. package/templates/lib/evalkeeper/appComponents.tsx +21 -0
  240. package/templates/lib/evalkeeper/evals.ts +14 -0
  241. package/templates/lib/evalkeeper/paths.ts +2 -7
  242. package/templates/components/evalkeeper/Submit.tsx +0 -67
  243. package/templates/components/evalkeeper/Tip.tsx +0 -98
  244. package/templates/components/evalkeeper/ui.tsx +0 -105
  245. package/templates/lib/evalkeeper/time.ts +0 -4
  246. package/templates/lib/evalkeeper/weekly.ts +0 -74
package/src/settings.ts CHANGED
@@ -150,7 +150,7 @@ export type SettingsSwitch = {
150
150
  expectLive: string | null;
151
151
  };
152
152
 
153
- /** Where versions live. `@crowandraven/evalkeeper-prisma` and `memoryStore` implement it. */
153
+ /** Where versions live. `@crowandraven/evalkeeper/prisma` and `memoryStore` implement it. */
154
154
  export interface SettingsStore {
155
155
  liveVersion(operation: string): Promise<SettingsVersion | null>;
156
156
  /** Newest first. */
package/src/store.ts CHANGED
@@ -3,7 +3,7 @@ import type { SuiteMetrics } from "./stats.js";
3
3
 
4
4
  /**
5
5
  * Where Evalkeeper keeps cases, answers, tests, and outputs. The runner
6
- * talks only to this interface: `@crowandraven/evalkeeper-prisma` implements it on your
6
+ * talks only to this interface: `@crowandraven/evalkeeper/prisma` implements it on your
7
7
  * database, and `memoryStore()` keeps everything in memory for tests and
8
8
  * demos. Every method is a plain read or write; the rules live in the
9
9
  * runner.
package/src/suggest.ts CHANGED
@@ -20,7 +20,11 @@ import { at } from "./util.js";
20
20
  * Models the provider is retiring are never suggested, and a retiring live
21
21
  * model is replaced by the best-scoring one that isn't. A live model whose
22
22
  * current test has failed answers is tested again, not replaced: it can't
23
- * be compared yet, and a timeout or two isn't a reason to switch. Pure.
23
+ * be compared yet, and a timeout or two isn't a reason to switch. With a
24
+ * feature's injection suites (`attacks`), a model is suggested as live only
25
+ * with a current test against the attacks, and never with more serious
26
+ * mistakes under attack than live; making at least two fewer is a reason
27
+ * to switch on its own. Pure.
24
28
  */
25
29
 
26
30
  /** One model's row, as the suggestion reads it. */
@@ -69,6 +73,71 @@ export type Suggestion<R extends SuggestRow = SuggestRow> = {
69
73
  reasons: string[];
70
74
  };
71
75
 
76
+ /**
77
+ * One injection suite's board, as the suggestion reads it: the same
78
+ * feature attacked, so a model's row there has the same key as on the
79
+ * base suite's board. Build it with `attacksFrom`.
80
+ */
81
+ export type AttackResults = {
82
+ /** The injection suite's label, for the reasons. */
83
+ label: string;
84
+ rows: SuggestRow[];
85
+ checks: CheckDef[];
86
+ };
87
+
88
+ /** What else the suggestion weighs, besides the suite's own board. */
89
+ export type SuggestOptions = {
90
+ /**
91
+ * The feature's injection suites, from `runner.attackBoards(suiteKey)`
92
+ * through `attacksFrom`. Without them, attacks aren't weighed.
93
+ */
94
+ attacks?: AttackResults[];
95
+ };
96
+
97
+ /**
98
+ * Injection suites' boards in the shape `suggest` reads, for its
99
+ * `attacks` option.
100
+ *
101
+ * @param boards - The boards of a feature's injection suites
102
+ * (`runner.attackBoards`).
103
+ * @returns One entry per injection suite.
104
+ */
105
+ export function attacksFrom(
106
+ boards: readonly {
107
+ suite: { label: string; checks: CheckDef[] };
108
+ rows: SuggestRow[];
109
+ }[],
110
+ ): AttackResults[] {
111
+ return boards.map((b) => ({
112
+ label: b.suite.label,
113
+ rows: b.rows,
114
+ checks: b.suite.checks,
115
+ }));
116
+ }
117
+
118
+ /**
119
+ * A model's serious mistakes on the attacks: its serious mistakes on every
120
+ * injection suite, added up. Null when any of them has no current,
121
+ * complete test of it, so it can't be compared yet; also null with no
122
+ * injection suites.
123
+ *
124
+ * @param key - The model's row key (model and reasoning setting).
125
+ * @param attacks - The feature's injection suites.
126
+ */
127
+ export function attackMistakes(
128
+ key: string,
129
+ attacks: readonly AttackResults[],
130
+ ): number | null {
131
+ if (!attacks.length) return null;
132
+ let total = 0;
133
+ for (const a of attacks) {
134
+ const r = a.rows.find((x) => x.key === key);
135
+ if (!r?.test || r.test.stale || r.test.failedAnswers) return null;
136
+ total += seriousMistakes(r, a.checks);
137
+ }
138
+ return total;
139
+ }
140
+
72
141
  /**
73
142
  * How many serious checks a model's test failed; Infinity without a test.
74
143
  */
@@ -113,7 +182,15 @@ export function suggest<R extends SuggestRow>(
113
182
  priority: SuitePriority = { favor: "quality" },
114
183
  /** Your model catalog: providers (for the fallback) and retirements. */
115
184
  models: ModelCatalog = {},
185
+ /** The feature's injection suites, to weigh attacks too. */
186
+ opts: SuggestOptions = {},
116
187
  ): Suggestion<R> | null {
188
+ const attacks = opts.attacks ?? [];
189
+ const weighAttacks = attacks.length > 0;
190
+ /** Serious mistakes under attack; null when not tested against them. */
191
+ const underAttack = (r: SuggestRow) => attackMistakes(r.key, attacks);
192
+ const attackTested = (r: SuggestRow) =>
193
+ !weighAttacks || underAttack(r) !== null;
117
194
  const provider = (model: string) => models[model]?.provider ?? model;
118
195
  const retiring = (r: SuggestRow) => retiresLabel(models, r.model);
119
196
  const limitMs = priority.maxSeconds ? priority.maxSeconds * 1000 : Infinity;
@@ -162,10 +239,15 @@ export function suggest<R extends SuggestRow>(
162
239
  );
163
240
  } else if (!live) {
164
241
  if (!liveable.length) return null;
242
+ // Only models tested against the attacks, unless none are.
243
+ const known = liveable.filter(attackTested);
165
244
  next = at(
166
- [...liveable].sort(
245
+ [...(known.length ? known : liveable)].sort(
167
246
  (a, b) =>
168
- rate(b) - rate(a) || serious(a) - serious(b) || cost(a) - cost(b),
247
+ rate(b) - rate(a) ||
248
+ serious(a) - serious(b) ||
249
+ (underAttack(a) ?? 0) - (underAttack(b) ?? 0) ||
250
+ cost(a) - cost(b),
169
251
  ),
170
252
  0,
171
253
  );
@@ -174,13 +256,27 @@ export function suggest<R extends SuggestRow>(
174
256
  ? `${liveRetires && liveRow ? `${liveRow.label} retires in ${liveRetires}.` : liveSlow} ${next.label} scores highest of the rest (${pct(rate(next))}) with ${mistakes(serious(next))}.`
175
257
  : `${next.label} scores highest (${pct(rate(next))}) with ${mistakes(serious(next))}. The live model has no up-to-date test to compare with.`,
176
258
  );
259
+ if (!attackTested(next))
260
+ reasons.push(
261
+ `No model has an up-to-date test against the attacks: test ${next.label} on them before relying on it.`,
262
+ );
177
263
  } else {
264
+ // At least two fewer serious mistakes on the attacks, as accurate or more.
265
+ const attackReason = (r: R): string | null => {
266
+ const ra = underAttack(r);
267
+ const la = underAttack(live);
268
+ return ra !== null && la !== null && ra <= la - 2 && rate(r) >= rate(live)
269
+ ? `${r.label} makes fewer serious mistakes on the attacks than ${live.label} (${ra} against ${la}), and scores at least as well (${pct(rate(r))} against ${pct(rate(live))}).`
270
+ : null;
271
+ };
178
272
  const why = (r: R): string | null => {
179
273
  if (rate(r) < rate(live)) return trade(r);
180
274
  if (r.vsLive === "BETTER")
181
275
  return `${r.label} is clearly more accurate than ${live.label}: ${pct(rate(r))} against ${pct(rate(live))}.`;
182
276
  if (serious(r) <= serious(live) - 2)
183
277
  return `${r.label} makes fewer serious mistakes than ${live.label} (${serious(r)} against ${serious(live)}), and scores at least as well (${pct(rate(r))} against ${pct(rate(live))}).`;
278
+ const byAttacks = attackReason(r);
279
+ if (byAttacks) return byAttacks;
184
280
  if (priority.favor === "cost" && clearlyLess(cost(r), cost(live)))
185
281
  return `${r.label} scores at least as well as ${live.label} (${pct(rate(r))} against ${pct(rate(live))}) at ${Math.round((1 - cost(r) / cost(live)) * 100)}% less per answer.`;
186
282
  if (priority.favor === "speed" && clearlyLess(ms(r), ms(live)))
@@ -204,14 +300,23 @@ export function suggest<R extends SuggestRow>(
204
300
  return `${close}, and answers in ${(ms(r) / 1000).toFixed(1)}s against ${(ms(live) / 1000).toFixed(1)}s. This suite favors speed.`;
205
301
  return null;
206
302
  };
303
+ // Under attack: tested, and no more serious mistakes than live (when
304
+ // live has a test to compare with).
305
+ const liveAttack = underAttack(live);
306
+ const safeUnderAttack = (r: R) => {
307
+ const a = underAttack(r);
308
+ return (
309
+ !weighAttacks ||
310
+ (a !== null && (liveAttack === null || a <= liveAttack))
311
+ );
312
+ };
313
+ const qualifies = (r: R) =>
314
+ !r.live &&
315
+ r.vsLive !== "WORSE" &&
316
+ serious(r) <= serious(live) &&
317
+ why(r) !== null;
207
318
  const candidates = liveable
208
- .filter(
209
- (r) =>
210
- !r.live &&
211
- r.vsLive !== "WORSE" &&
212
- serious(r) <= serious(live) &&
213
- why(r),
214
- )
319
+ .filter((r) => qualifies(r) && safeUnderAttack(r))
215
320
  .sort(
216
321
  (a, b) =>
217
322
  Number(b.vsLive === "BETTER") - Number(a.vsLive === "BETTER") ||
@@ -227,11 +332,28 @@ export function suggest<R extends SuggestRow>(
227
332
  next = at(candidates, 0);
228
333
  const reason = why(next);
229
334
  if (reason) reasons.push(reason);
335
+ const a = underAttack(next);
336
+ if (a !== null && reason !== attackReason(next))
337
+ reasons.push(
338
+ liveAttack === null
339
+ ? `On the attacks, ${next.label} makes ${mistakes(a)}. ${live.label} has no up-to-date test against the attacks to compare with.`
340
+ : `On the attacks, ${next.label} makes ${mistakes(a)}, against ${liveAttack} for ${live.label}.`,
341
+ );
230
342
  } else {
231
343
  next = live;
232
344
  reasons.push(
233
345
  `Keep ${live.label}: no other model is safely better on these cases.`,
234
346
  );
347
+ // Would have been suggested, but for the attacks.
348
+ const held = liveable.find((r) => qualifies(r) && !safeUnderAttack(r));
349
+ if (held) {
350
+ const a = underAttack(held);
351
+ reasons.push(
352
+ a === null
353
+ ? `${held.label} would otherwise qualify, but it has no up-to-date test against the attacks. Test it on ${at(attacks, 0).label}.`
354
+ : `${held.label} would otherwise qualify, but it makes more serious mistakes on the attacks (${a} against ${liveAttack ?? 0}).`,
355
+ );
356
+ }
235
357
  // The one to watch: scored higher, but not by enough to tell.
236
358
  const watch = tested
237
359
  .filter((r) => !r.live && rate(r) > rate(live))
@@ -256,12 +378,22 @@ export function suggest<R extends SuggestRow>(
256
378
  )
257
379
  .sort(
258
380
  (a, b) =>
259
- rate(b) - rate(a) || serious(a) - serious(b) || cost(a) - cost(b),
381
+ rate(b) - rate(a) ||
382
+ serious(a) - serious(b) ||
383
+ (underAttack(a) ?? Infinity) - (underAttack(b) ?? Infinity) ||
384
+ cost(a) - cost(b),
260
385
  )[0] ?? null;
261
- if (fallback)
386
+ if (fallback) {
387
+ const a = underAttack(fallback);
388
+ const attacked = !weighAttacks
389
+ ? ""
390
+ : a === null
391
+ ? "; not tested against the attacks"
392
+ : `; ${mistakes(a)} on the attacks`;
262
393
  reasons.push(
263
- `Fallback: ${fallback.label} is the most accurate from another provider (${pct(rate(fallback))}, ${mistakes(serious(fallback))}).`,
394
+ `Fallback: ${fallback.label} is the most accurate from another provider (${pct(rate(fallback))}, ${mistakes(serious(fallback))}${attacked}).`,
264
395
  );
396
+ }
265
397
 
266
398
  const keepLive = next.live;
267
399
  const retestLive = Boolean(liveFailed);
@@ -5,19 +5,33 @@ import type { AnySuite, FormField } from "./types.js";
5
5
  /** A field help shorter than this says nothing a label doesn't. */
6
6
  const MIN_HELP = 10;
7
7
 
8
- /** A zod schema's definition, read by name so any copy of zod works. */
8
+ /** A zod schema's definition, read by name so any copy of zod 4 works. */
9
9
  type Def = {
10
- typeName?: string;
11
- shape?: () => Record<string, unknown>;
12
- schema?: z.ZodTypeAny;
13
- innerType?: z.ZodTypeAny;
10
+ type?: string;
11
+ shape?: Record<string, unknown>;
12
+ /** What a wrapper (optional, nullable, default, …) wraps. */
13
+ innerType?: z.ZodType;
14
+ /** A pipe's two ends: a transform's schema is `in`, a preprocess's `out`. */
15
+ in?: z.ZodType;
16
+ out?: z.ZodType;
14
17
  /** A union's options (a plain union's, or a discriminated one's). */
15
- options?: readonly z.ZodTypeAny[];
18
+ options?: readonly z.ZodType[];
16
19
  };
17
20
 
21
+ /** Wrappers whose keys are their inner schema's. */
22
+ const WRAPPERS = new Set([
23
+ "optional",
24
+ "nullable",
25
+ "default",
26
+ "prefault",
27
+ "catch",
28
+ "readonly",
29
+ "nonoptional",
30
+ ]);
31
+
18
32
  /**
19
- * The top-level keys of an object schema, through refinements, optional,
20
- * nullable, and default wrappers. For a union (`z.union` or
33
+ * The top-level keys of an object schema, through refinements,
34
+ * transforms, and optional, nullable, and default wrappers. For a union (`z.union` or
21
35
  * `z.discriminatedUnion`), every key that appears in any of its object
22
36
  * options, in the order they first appear, so one `fields` list covers
23
37
  * them all. Null for any other shape (a string, a list, a union with no
@@ -25,41 +39,34 @@ type Def = {
25
39
  *
26
40
  * @param schema - A suite's input or answer schema.
27
41
  */
28
- export function schemaKeys(schema: z.ZodTypeAny): string[] | null {
42
+ export function schemaKeys(schema: z.ZodType): string[] | null {
29
43
  return keysOf(schema, 0);
30
44
  }
31
45
 
32
46
  /** Wrappers and unions deeper than this are read as an unknown shape. */
33
47
  const MAX_DEPTH = 10;
34
48
 
35
- function keysOf(schema: z.ZodTypeAny, depth: number): string[] | null {
36
- let s: z.ZodTypeAny | undefined = schema;
49
+ function keysOf(schema: z.ZodType, depth: number): string[] | null {
50
+ let s: z.ZodType | undefined = schema;
37
51
  for (let i = depth; s && i < MAX_DEPTH; i++) {
38
52
  // Trust boundary: zod's own definition object, read by its names.
39
- const def = s._def as Def;
40
- if (def.typeName === "ZodObject" && def.shape)
41
- return Object.keys(def.shape());
42
- if (
43
- (def.typeName === "ZodUnion" ||
44
- def.typeName === "ZodDiscriminatedUnion") &&
45
- def.options
46
- )
53
+ const def = s._zod.def as Def;
54
+ if (def.type === "object" && def.shape) return Object.keys(def.shape);
55
+ if (def.type === "union" && def.options)
47
56
  return unionKeys(def.options, i + 1);
48
- s =
49
- def.typeName === "ZodEffects"
50
- ? def.schema
51
- : def.typeName === "ZodOptional" ||
52
- def.typeName === "ZodNullable" ||
53
- def.typeName === "ZodDefault"
54
- ? def.innerType
55
- : undefined;
57
+ if (def.type === "pipe")
58
+ return (
59
+ keysOf(def.in as z.ZodType, i + 1) ??
60
+ keysOf(def.out as z.ZodType, i + 1)
61
+ );
62
+ s = def.type && WRAPPERS.has(def.type) ? def.innerType : undefined;
56
63
  }
57
64
  return null;
58
65
  }
59
66
 
60
67
  /** Every key of a union's object options; null when none is an object. */
61
68
  function unionKeys(
62
- options: readonly z.ZodTypeAny[],
69
+ options: readonly z.ZodType[],
63
70
  depth: number,
64
71
  ): string[] | null {
65
72
  const keys = new Set<string>();
package/src/testing.ts CHANGED
@@ -99,7 +99,7 @@ export type ScopedContractOptions = ContractOptions & {
99
99
  * after the contract's tests (and so also any left by an earlier run
100
100
  * that stopped early). Needs `scope`, and `afterAll` among the test
101
101
  * functions. For Prisma, `deleteScopedRows` from
102
- * `@crowandraven/evalkeeper-prisma`.
102
+ * `@crowandraven/evalkeeper/prisma`.
103
103
  */
104
104
  cleanup?: (scope: string) => Promise<unknown>;
105
105
  };
@@ -1035,7 +1035,7 @@ export function describeSuiteContract(
1035
1035
  * @example
1036
1036
  * ```ts
1037
1037
  * import { describeMappedReadCheck } from "@crowandraven/evalkeeper/testing";
1038
- * import { prismaReadCheck } from "@crowandraven/evalkeeper-prisma";
1038
+ * import { prismaReadCheck } from "@crowandraven/evalkeeper/prisma";
1039
1039
  *
1040
1040
  * describeMappedReadCheck(
1041
1041
  * "our answer key",
package/src/types.ts CHANGED
@@ -284,13 +284,15 @@ export type ActivityDraft<Input, Label> = {
284
284
  /**
285
285
  * One field of a suite's add/edit form. `name` is the form key (and the
286
286
  * input or answer property it fills). "lines": one item per line, read as
287
- * a list.
287
+ * a list. "json": a structured value (a list of objects, a nested shape)
288
+ * edited as JSON in a monospace box, checked against the suite's schema
289
+ * when saved.
288
290
  */
289
291
  export type FormField = {
290
292
  name: string;
291
293
  label: string;
292
294
  help: string;
293
- kind: "text" | "textarea" | "select" | "lines";
295
+ kind: "text" | "textarea" | "select" | "lines" | "json";
294
296
  /** For "select". */
295
297
  options?: readonly string[];
296
298
  required?: boolean;
@@ -316,6 +318,8 @@ export function fieldValue(field: FormField, value: unknown): string {
316
318
  value && typeof value === "object"
317
319
  ? (value as Record<string, unknown>)[field.name]
318
320
  : undefined;
321
+ if (field.kind === "json")
322
+ return v === undefined || v === null ? "" : JSON.stringify(v, null, 2);
319
323
  if (Array.isArray(v)) return v.map(asText).join("\n");
320
324
  return asText(v);
321
325
  }
@@ -348,35 +352,45 @@ export function isInjectionSuite(
348
352
  return "base" in suite && "attackCase" in suite;
349
353
  }
350
354
 
355
+ /**
356
+ * The injection suites that attack a suite: the ones whose base it is.
357
+ * Their results belong with the base suite's, since both test the same
358
+ * feature and one choice of model serves both.
359
+ *
360
+ * @param suites - Every registered suite.
361
+ * @param baseKey - The base suite's key.
362
+ */
363
+ export function injectionSuitesOf(
364
+ suites: readonly AnySuite[],
365
+ baseKey: string,
366
+ ): (AnySuite & { base: AnySuite })[] {
367
+ return suites.filter(
368
+ (s): s is AnySuite & { base: AnySuite } =>
369
+ isInjectionSuite(s) && s.base.key === baseKey,
370
+ );
371
+ }
372
+
351
373
  /** Where a zod issue sits, in plain words: "facts, item 2, name". */
352
- function issuePath(path: readonly (string | number)[]): string {
374
+ function issuePath(path: readonly PropertyKey[]): string {
353
375
  return path
354
- .map((p) => (typeof p === "number" ? `item ${p + 1}` : p))
376
+ .map((p) => (typeof p === "number" ? `item ${p + 1}` : String(p)))
355
377
  .join(", ");
356
378
  }
357
379
 
358
- /**
359
- * Reads one form field as JSON and checks it with a schema; throws a
360
- * plain error when the field is empty, isn't JSON, or doesn't fit (naming
361
- * the first part that doesn't).
362
- */
363
- function readJson<T>(
364
- form: FormData,
365
- field: string,
366
- what: string,
367
- schema: z.ZodType<T>,
368
- ): T {
369
- const text = formText(form, field);
370
- if (!text) throw new Error(`${what} is empty: fill in the "${field}" field`);
371
- let value: unknown;
380
+ /** Text as JSON, with a plain error naming what it was. */
381
+ function parseJson(text: string, what: string): unknown {
372
382
  try {
373
- value = JSON.parse(text);
383
+ return JSON.parse(text) as unknown;
374
384
  } catch (e) {
375
385
  throw new Error(
376
386
  `${what} isn't valid JSON: ${e instanceof Error ? e.message : String(e)}`,
377
387
  { cause: e },
378
388
  );
379
389
  }
390
+ }
391
+
392
+ /** A value checked with a schema, with a plain error naming the first misfit. */
393
+ function fit<T>(value: unknown, schema: z.ZodType<T>, what: string): T {
380
394
  const parsed = schema.safeParse(value);
381
395
  if (parsed.success) return parsed.data;
382
396
  const issue = parsed.error.issues[0];
@@ -389,6 +403,22 @@ function readJson<T>(
389
403
  );
390
404
  }
391
405
 
406
+ /**
407
+ * Reads one form field as JSON and checks it with a schema; throws a
408
+ * plain error when the field is empty, isn't JSON, or doesn't fit (naming
409
+ * the first part that doesn't).
410
+ */
411
+ function readJson<T>(
412
+ form: FormData,
413
+ field: string,
414
+ what: string,
415
+ schema: z.ZodType<T>,
416
+ ): T {
417
+ const text = formText(form, field);
418
+ if (!text) throw new Error(`${what} is empty: fill in the "${field}" field`);
419
+ return fit(parseJson(text, what), schema, what);
420
+ }
421
+
392
422
  /**
393
423
  * A suite's `inputFromForm` for fixture suites (`caseKind: "fixture"`),
394
424
  * whose cases are edited as JSON in one textarea. Reads the field, parses
@@ -432,3 +462,101 @@ export function labelFromJson<T>(
432
462
  ): (form: FormData) => T {
433
463
  return (form) => readJson(form, field, "Answer", schema);
434
464
  }
465
+
466
+ /**
467
+ * A form's values for a suite's fields, each read by its kind: text as it
468
+ * is, "lines" as a list, "json" parsed (with a plain error naming the
469
+ * field when it isn't valid JSON; empty reads as null). Pure.
470
+ *
471
+ * @param fields - The suite's input or answer fields.
472
+ * @param form - The submitted form.
473
+ */
474
+ export function valuesFromFields(
475
+ fields: readonly FormField[],
476
+ form: FormData,
477
+ ): Record<string, unknown> {
478
+ const out: Record<string, unknown> = {};
479
+ for (const f of fields) {
480
+ if (f.kind === "lines") out[f.name] = linesOf(form, f.name);
481
+ else if (f.kind === "json") {
482
+ const text = formText(form, f.name);
483
+ out[f.name] = text ? parseJson(text, f.label) : null;
484
+ } else out[f.name] = formText(form, f.name);
485
+ }
486
+ return out;
487
+ }
488
+
489
+ /**
490
+ * A suite's `inputFromForm` from its fields, any kinds: reads each field
491
+ * by kind (`valuesFromFields`) and checks the whole with the schema,
492
+ * throwing a plain error that names the first part that doesn't fit.
493
+ *
494
+ * @param schema - The suite's input schema.
495
+ * @param fields - The suite's input fields (`fields.input`).
496
+ * @returns A form reader to use as the suite's `inputFromForm`.
497
+ */
498
+ export function inputFromFields<T>(
499
+ schema: z.ZodType<T>,
500
+ fields: readonly FormField[],
501
+ ): (form: FormData) => T {
502
+ return (form) => fit(valuesFromFields(fields, form), schema, "Input");
503
+ }
504
+
505
+ /**
506
+ * A suite's `labelFromForm` from its answer fields, like `inputFromFields`.
507
+ *
508
+ * @param schema - The suite's answer schema (`labelValue`).
509
+ * @param fields - The suite's answer fields (`fields.label`).
510
+ * @returns A form reader to use as the suite's `labelFromForm`.
511
+ */
512
+ export function labelFromFields<T>(
513
+ schema: z.ZodType<T>,
514
+ fields: readonly FormField[],
515
+ ): (form: FormData) => T {
516
+ return (form) => fit(valuesFromFields(fields, form), schema, "Answer");
517
+ }
518
+
519
+ /**
520
+ * The form field an answer editor submits its answer under, as JSON
521
+ * (see `AnswerEditorProps`).
522
+ */
523
+ export const ANSWER_JSON_FIELD = "answerJson";
524
+
525
+ /**
526
+ * What an app's own answer editor gets, for answers plain fields can't
527
+ * edit well (a list of facts with a category each, a verdict with
528
+ * reasons). The editor is a client component that renders its own
529
+ * controls and keeps the whole answer, as JSON, in a form field named
530
+ * `name` (a hidden input). Register it in your wiring module's
531
+ * `answerEditors`, keyed by suite; the copied answer key, "Add a case",
532
+ * and blind rating use it in place of the suite's answer fields, and the
533
+ * actions check what it submits against the suite's `labelValue`.
534
+ */
535
+ export type AnswerEditorProps = {
536
+ /** The form field to submit the answer under, as JSON. */
537
+ name: string;
538
+ /** Prefix for element ids, unique on the page. */
539
+ idPrefix: string;
540
+ /** The case's input, when there is one yet (null on "Add a case"). */
541
+ input: unknown;
542
+ /** The current answer, or null for a new one. */
543
+ value: unknown;
544
+ /** The suite's answer fields, for their labels and help. */
545
+ fields: readonly FormField[];
546
+ };
547
+
548
+ /**
549
+ * The answer a form submits for a suite: from an answer editor (JSON in
550
+ * `ANSWER_JSON_FIELD`, checked against the suite's `labelValue`) when the
551
+ * form has one, otherwise the suite's own `labelFromForm`. Throws a plain
552
+ * error when the suite has neither, or the answer doesn't fit.
553
+ *
554
+ * @param suite - The suite the answer is for.
555
+ * @param form - The submitted form.
556
+ */
557
+ export function answerFromForm(suite: AnySuite, form: FormData): unknown {
558
+ if (formText(form, ANSWER_JSON_FIELD))
559
+ return readJson(form, ANSWER_JSON_FIELD, "Answer", suite.labelValue);
560
+ if (suite.labelFromForm) return suite.labelFromForm(form);
561
+ throw new Error("This suite has no answer form");
562
+ }
package/starter/index.ts CHANGED
@@ -1,20 +1,23 @@
1
- // Your app's Evalkeeper wiring: the one file the copied eval pages import.
1
+ // Your app's Evalkeeper wiring: the one file the eval pages read from.
2
2
  // Written by `npx @crowandraven/evalkeeper add pages`; it's yours now. Fill in each TODO.
3
3
  import "server-only";
4
4
  import {
5
5
  createSettings,
6
+ type AnswerEditorProps,
6
7
  type AnySuite,
7
8
  type ModelCatalog,
8
9
  type Settings,
9
10
  } from "@crowandraven/evalkeeper";
11
+ import type { ComponentType } from "react";
10
12
  import { createRunner, type Route } from "@crowandraven/evalkeeper/runner";
13
+ import type { EvalWiring } from "@crowandraven/evalkeeper/next";
11
14
  import {
12
15
  prismaFeedbackStore,
13
16
  prismaSettingsStore,
14
17
  prismaStore,
15
- } from "@crowandraven/evalkeeper-prisma";
18
+ } from "@crowandraven/evalkeeper/prisma";
16
19
  // TODO: your app's Prisma client, with Evalkeeper's models added to the
17
- // schema (node_modules/@crowandraven/evalkeeper-prisma/schema/evalkeeper.prisma).
20
+ // schema (node_modules/@crowandraven/evalkeeper/schema/evalkeeper.prisma).
18
21
  import { prisma } from "@/lib/prisma";
19
22
 
20
23
  /** TODO: the models your app can use, with prices and retirement dates. */
@@ -35,19 +38,25 @@ export function modelsFor(_operation: string): string[] {
35
38
 
36
39
  /** TODO: one suite per AI feature (see the README's "Write a suite"). */
37
40
  export const SUITES: AnySuite[] = [];
38
- export const SUITE_BY_KEY: Record<string, AnySuite> = Object.fromEntries(
39
- SUITES.map((s) => [s.key, s]),
40
- );
41
+
42
+ /**
43
+ * Your own editors for answers plain fields can't edit well, keyed by
44
+ * suite (see `AnswerEditorProps`). The answer key, "Add a case", and blind
45
+ * rating use them in place of the suite's answer fields. Empty is fine.
46
+ */
47
+ export const answerEditors: Partial<
48
+ Record<string, ComponentType<AnswerEditorProps>>
49
+ > = {};
41
50
 
42
51
  /**
43
52
  * TODO: each feature's instructions hash (instructionsHash from
44
- * @crowandraven/evalkeeper-ai-sdk), so editing a prompt marks older tests out of date.
53
+ * @crowandraven/evalkeeper/ai-sdk), so editing a prompt marks older tests out of date.
45
54
  */
46
55
  const PROMPT_VERSIONS: Record<string, string> = {};
47
56
 
48
57
  /**
49
58
  * TODO: each feature's instructions as text (instructionParts from
50
- * @crowandraven/evalkeeper-ai-sdk, on the parts you hash), so the suite
59
+ * @crowandraven/evalkeeper/ai-sdk, on the parts you hash), so the suite
51
60
  * page warns when a case was copied into them.
52
61
  */
53
62
  const INSTRUCTION_TEXTS: Record<string, string[]> = {};
@@ -92,3 +101,19 @@ export async function requireAdmin(): Promise<{ id: string; name: string }> {
92
101
  "Evalkeeper: write requireAdmin in lib/evalkeeper/index.ts so only admins can use the eval pages.",
93
102
  );
94
103
  }
104
+
105
+ /**
106
+ * Everything Evalkeeper's pages, actions, and weekly check need from your
107
+ * app, in one object. Where the pages live is in `./paths`.
108
+ */
109
+ export const wiring = {
110
+ runner,
111
+ settings,
112
+ store,
113
+ feedbackStore,
114
+ models: MODELS,
115
+ suites: SUITES,
116
+ routeFor,
117
+ answerEditors,
118
+ requireAdmin,
119
+ } satisfies Omit<EvalWiring, "basePath">;