@open-qa/qa 0.1.0-alpha.3 → 0.1.0-alpha.5

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 (118) hide show
  1. package/THIRD_PARTY.md +5 -0
  2. package/drizzle/0017_provider_notifications.sql +13 -0
  3. package/drizzle/0018_classification_registry.sql +39 -0
  4. package/drizzle/0019_trial_audit.sql +44 -0
  5. package/drizzle/meta/0017_snapshot.json +2092 -0
  6. package/drizzle/meta/0018_snapshot.json +2102 -0
  7. package/drizzle/meta/0019_snapshot.json +2451 -0
  8. package/drizzle/meta/_journal.json +21 -0
  9. package/package.json +12 -7
  10. package/src/access-binding.d.ts +8 -0
  11. package/src/access-binding.js +26 -0
  12. package/src/adapters/classification-write.d.ts +87 -0
  13. package/src/adapters/classification-write.js +64 -0
  14. package/src/adapters/e2b-executor.js +4 -0
  15. package/src/adapters/file-run-store.d.ts +186 -41
  16. package/src/adapters/file-run-store.js +87 -19
  17. package/src/adapters/file-trial-audit.d.ts +280 -0
  18. package/src/adapters/file-trial-audit.js +105 -0
  19. package/src/adapters/pg-classification-store.d.ts +98 -20
  20. package/src/adapters/pg-classification-store.js +84 -44
  21. package/src/adapters/pg-run-store.d.ts +1 -0
  22. package/src/adapters/pg-run-store.js +70 -9
  23. package/src/adapters/run-metadata.d.ts +2 -0
  24. package/src/adapters/run-metadata.js +10 -0
  25. package/src/adapters/secret-manager-credential-resolver.d.ts +1 -1
  26. package/src/attempt-audit.d.ts +363 -0
  27. package/src/attempt-audit.js +253 -0
  28. package/src/audit-command.d.ts +13 -0
  29. package/src/audit-command.js +77 -0
  30. package/src/classification-command.d.ts +14 -9
  31. package/src/classification-command.js +119 -26
  32. package/src/classification-model.d.ts +10 -3
  33. package/src/classification-model.js +40 -24
  34. package/src/classification-notifier.d.ts +4 -3
  35. package/src/classification-notifier.js +26 -6
  36. package/src/classify-runs.d.ts +2 -1
  37. package/src/classify-runs.js +39 -3
  38. package/src/cli-program.js +54 -8
  39. package/src/commands.d.ts +1 -0
  40. package/src/commands.js +2 -0
  41. package/src/db/database.d.ts +7 -0
  42. package/src/db/database.js +15 -0
  43. package/src/db/event-chunks.d.ts +38 -29
  44. package/src/db/flow-status.d.ts +7 -3
  45. package/src/db/flow-status.js +240 -16
  46. package/src/db/queries.d.ts +24 -1
  47. package/src/db/queries.js +22 -0
  48. package/src/db/schema.d.ts +333 -4
  49. package/src/db/schema.js +83 -11
  50. package/src/db/trial-audit.d.ts +526 -0
  51. package/src/db/trial-audit.js +84 -0
  52. package/src/doctor-command.js +2 -1
  53. package/src/draft-api.d.ts +25 -421
  54. package/src/draft-api.js +10 -3
  55. package/src/engine.d.ts +47 -4
  56. package/src/engine.js +97 -8
  57. package/src/execution-snapshot.d.ts +1 -1
  58. package/src/execution-snapshot.js +24 -18
  59. package/src/export-command.d.ts +6 -0
  60. package/src/export-command.js +106 -0
  61. package/src/flow-check.js +1 -1
  62. package/src/flow-loader.d.ts +72 -6
  63. package/src/flow-loader.js +76 -12
  64. package/src/http-stage-client.d.ts +98 -1
  65. package/src/independent-model.js +53 -6
  66. package/src/judge-review-store.d.ts +13 -0
  67. package/src/judge-review-store.js +58 -0
  68. package/src/local-host-executor.d.ts +3 -1
  69. package/src/local-host-executor.js +2 -0
  70. package/src/quarantine-command.d.ts +5 -1
  71. package/src/quarantine-command.js +56 -13
  72. package/src/remote-command.js +2 -0
  73. package/src/report/browser.css +1 -1
  74. package/src/report/browser.js +41 -41
  75. package/src/report/ctrf.d.ts +983 -0
  76. package/src/report/ctrf.js +306 -0
  77. package/src/report/generate.d.ts +1 -1
  78. package/src/report/generate.js +29 -6
  79. package/src/report/load.d.ts +738 -42
  80. package/src/report/load.js +78 -6
  81. package/src/result-notifier.js +1 -1
  82. package/src/result-trace.d.ts +62 -0
  83. package/src/result-trace.js +81 -0
  84. package/src/run-command.d.ts +9 -0
  85. package/src/run-command.js +300 -63
  86. package/src/run-setup-error.d.ts +11 -0
  87. package/src/run-setup-error.js +18 -0
  88. package/src/server/app.js +5 -4
  89. package/src/server/index.d.ts +2 -0
  90. package/src/server/index.js +11 -2
  91. package/src/server/repository.d.ts +166 -29
  92. package/src/server/repository.js +14 -2
  93. package/src/studio-command.d.ts +1 -0
  94. package/src/studio-command.js +11 -0
  95. package/src/studio-control.d.ts +5 -0
  96. package/src/studio-control.js +207 -0
  97. package/src/studio-review.d.ts +24 -0
  98. package/src/studio-review.js +64 -0
  99. package/src/studio-worker.d.ts +1 -0
  100. package/src/studio-worker.js +48 -0
  101. package/src/system-config.d.ts +2 -2
  102. package/src/system-config.js +5 -4
  103. package/src/trial-audit.d.ts +630 -0
  104. package/src/trial-audit.js +272 -0
  105. package/src/visual-baseline.d.ts +4 -0
  106. package/src/visual-baseline.js +42 -0
  107. package/src/webhook-notification.d.ts +15 -0
  108. package/src/webhook-notification.js +61 -0
  109. package/src/worker/execution.d.ts +2 -1
  110. package/src/worker/execution.js +122 -3
  111. package/src/worker/index.d.ts +4 -0
  112. package/src/worker/index.js +10 -1
  113. package/src/worker/quarantine-notifications.js +2 -2
  114. package/src/worker/queue.d.ts +6 -2
  115. package/src/worker/queue.js +80 -19
  116. package/src/worker/result-notifications.d.ts +10 -2
  117. package/src/worker/result-notifications.js +120 -23
  118. package/src/worker/store.js +6 -3
@@ -0,0 +1,363 @@
1
+ import { z } from "zod";
2
+ import { type JsonObject, type RunEvent, type RunUsage, type Usage } from "@open-qa/qa-engine/protocol";
3
+ export declare const judgeReviewSchema: z.ZodObject<{
4
+ schemaVersion: z.ZodLiteral<1>;
5
+ eventSeq: z.ZodNumber;
6
+ actor: z.ZodString;
7
+ dbUser: z.ZodOptional<z.ZodString>;
8
+ host: z.ZodOptional<z.ZodString>;
9
+ at: z.ZodNumber;
10
+ reason: z.ZodString;
11
+ verdict: z.ZodEnum<{
12
+ passed: "passed";
13
+ failed: "failed";
14
+ inconclusive: "inconclusive";
15
+ }>;
16
+ }, z.core.$strict>;
17
+ export type JudgeReview = z.infer<typeof judgeReviewSchema>;
18
+ declare const usageEntrySchema: z.ZodObject<{
19
+ inputTokens: z.ZodNumber;
20
+ outputTokens: z.ZodNumber;
21
+ cacheReadTokens: z.ZodOptional<z.ZodNumber>;
22
+ cacheWriteTokens: z.ZodOptional<z.ZodNumber>;
23
+ costUsd: z.ZodOptional<z.ZodNumber>;
24
+ costUnknown: z.ZodOptional<z.ZodLiteral<true>>;
25
+ model: z.ZodOptional<z.ZodObject<{
26
+ provider: z.ZodNullable<z.ZodString>;
27
+ model: z.ZodNullable<z.ZodString>;
28
+ deployment: z.ZodOptional<z.ZodString>;
29
+ promptVersion: z.ZodOptional<z.ZodString>;
30
+ promptHash: z.ZodOptional<z.ZodString>;
31
+ parameters: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodJSONSchema>>;
32
+ seed: z.ZodUnion<readonly [z.ZodNumber, z.ZodEnum<{
33
+ unknown: "unknown";
34
+ unsupported: "unsupported";
35
+ }>]>;
36
+ billingVersion: z.ZodNullable<z.ZodString>;
37
+ }, z.core.$strict>>;
38
+ billingMode: z.ZodOptional<z.ZodEnum<{
39
+ subscription: "subscription";
40
+ metered: "metered";
41
+ }>>;
42
+ costUnknownReason: z.ZodOptional<z.ZodEnum<{
43
+ subscription: "subscription";
44
+ unpriced: "unpriced";
45
+ "usage-unavailable": "usage-unavailable";
46
+ "billing-unrecorded": "billing-unrecorded";
47
+ }>>;
48
+ role: z.ZodEnum<{
49
+ heal: "heal";
50
+ main: "main";
51
+ judge: "judge";
52
+ reviewer: "reviewer";
53
+ classifier: "classifier";
54
+ }>;
55
+ actor: z.ZodOptional<z.ZodString>;
56
+ step: z.ZodOptional<z.ZodString>;
57
+ billing: z.ZodNullable<z.ZodEnum<{
58
+ subscription: "subscription";
59
+ metered: "metered";
60
+ }>>;
61
+ }, z.core.$strict>;
62
+ type UsageEntry = z.infer<typeof usageEntrySchema>;
63
+ export declare const attemptAuditSchema: z.ZodObject<{
64
+ schemaVersion: z.ZodLiteral<1>;
65
+ environment: z.ZodOptional<z.ZodObject<{
66
+ image: z.ZodObject<{
67
+ template: z.ZodNullable<z.ZodString>;
68
+ digest: z.ZodNullable<z.ZodString>;
69
+ }, z.core.$strict>;
70
+ runtime: z.ZodNullable<z.ZodString>;
71
+ views: z.ZodArray<z.ZodObject<{
72
+ view: z.ZodString;
73
+ browser: z.ZodNullable<z.ZodString>;
74
+ viewport: z.ZodNullable<z.ZodObject<{
75
+ width: z.ZodNumber;
76
+ height: z.ZodNumber;
77
+ }, z.core.$strict>>;
78
+ device: z.ZodNullable<z.ZodString>;
79
+ }, z.core.$strict>>;
80
+ }, z.core.$strict>>;
81
+ usage: z.ZodArray<z.ZodObject<{
82
+ inputTokens: z.ZodNumber;
83
+ outputTokens: z.ZodNumber;
84
+ cacheReadTokens: z.ZodOptional<z.ZodNumber>;
85
+ cacheWriteTokens: z.ZodOptional<z.ZodNumber>;
86
+ costUsd: z.ZodOptional<z.ZodNumber>;
87
+ costUnknown: z.ZodOptional<z.ZodLiteral<true>>;
88
+ model: z.ZodOptional<z.ZodObject<{
89
+ provider: z.ZodNullable<z.ZodString>;
90
+ model: z.ZodNullable<z.ZodString>;
91
+ deployment: z.ZodOptional<z.ZodString>;
92
+ promptVersion: z.ZodOptional<z.ZodString>;
93
+ promptHash: z.ZodOptional<z.ZodString>;
94
+ parameters: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodJSONSchema>>;
95
+ seed: z.ZodUnion<readonly [z.ZodNumber, z.ZodEnum<{
96
+ unknown: "unknown";
97
+ unsupported: "unsupported";
98
+ }>]>;
99
+ billingVersion: z.ZodNullable<z.ZodString>;
100
+ }, z.core.$strict>>;
101
+ billingMode: z.ZodOptional<z.ZodEnum<{
102
+ subscription: "subscription";
103
+ metered: "metered";
104
+ }>>;
105
+ costUnknownReason: z.ZodOptional<z.ZodEnum<{
106
+ subscription: "subscription";
107
+ unpriced: "unpriced";
108
+ "usage-unavailable": "usage-unavailable";
109
+ "billing-unrecorded": "billing-unrecorded";
110
+ }>>;
111
+ role: z.ZodEnum<{
112
+ heal: "heal";
113
+ main: "main";
114
+ judge: "judge";
115
+ reviewer: "reviewer";
116
+ classifier: "classifier";
117
+ }>;
118
+ actor: z.ZodOptional<z.ZodString>;
119
+ step: z.ZodOptional<z.ZodString>;
120
+ billing: z.ZodNullable<z.ZodEnum<{
121
+ subscription: "subscription";
122
+ metered: "metered";
123
+ }>>;
124
+ }, z.core.$strict>>;
125
+ judges: z.ZodArray<z.ZodObject<{
126
+ eventSeq: z.ZodNumber;
127
+ actor: z.ZodNullable<z.ZodString>;
128
+ step: z.ZodNullable<z.ZodString>;
129
+ verdict: z.ZodEnum<{
130
+ passed: "passed";
131
+ failed: "failed";
132
+ inconclusive: "inconclusive";
133
+ }>;
134
+ confidence: z.ZodNullable<z.ZodNumber>;
135
+ reason: z.ZodString;
136
+ rubricVersion: z.ZodNullable<z.ZodString>;
137
+ rubricHash: z.ZodNullable<z.ZodString>;
138
+ outcome: z.ZodArray<z.ZodObject<{
139
+ eventSeq: z.ZodNumber;
140
+ type: z.ZodString;
141
+ passed: z.ZodBoolean;
142
+ }, z.core.$strict>>;
143
+ reviewStatus: z.ZodEnum<{
144
+ recorded: "recorded";
145
+ pending: "pending";
146
+ deterministic: "deterministic";
147
+ }>;
148
+ }, z.core.$strict>>;
149
+ reviewer: z.ZodArray<z.ZodObject<{
150
+ eventSeq: z.ZodNumber;
151
+ status: z.ZodEnum<{
152
+ completed: "completed";
153
+ incomplete: "incomplete";
154
+ skipped: "skipped";
155
+ }>;
156
+ reason: z.ZodString;
157
+ failureType: z.ZodNullable<z.ZodString>;
158
+ rubricVersion: z.ZodNullable<z.ZodString>;
159
+ rubricHash: z.ZodNullable<z.ZodString>;
160
+ }, z.core.$strict>>;
161
+ }, z.core.$strict>;
162
+ export type AttemptAudit = z.infer<typeof attemptAuditSchema>;
163
+ export declare function roleUsage(role: UsageEntry["role"], input: Usage): UsageEntry;
164
+ export declare function combineRoleUsage(entries: readonly UsageEntry[]): {
165
+ inputTokens: number;
166
+ outputTokens: number;
167
+ role: "heal" | "main" | "judge" | "reviewer" | "classifier";
168
+ billing: "subscription" | "metered" | null;
169
+ cacheReadTokens?: number | undefined;
170
+ cacheWriteTokens?: number | undefined;
171
+ costUsd?: number | undefined;
172
+ costUnknown?: true | undefined;
173
+ model?: {
174
+ provider: string | null;
175
+ model: string | null;
176
+ seed: number | "unknown" | "unsupported";
177
+ billingVersion: string | null;
178
+ deployment?: string | undefined;
179
+ promptVersion?: string | undefined;
180
+ promptHash?: string | undefined;
181
+ parameters?: Record<string, z.core.util.JSONType> | undefined;
182
+ } | undefined;
183
+ billingMode?: "subscription" | "metered" | undefined;
184
+ costUnknownReason?: "subscription" | "unpriced" | "usage-unavailable" | "billing-unrecorded" | undefined;
185
+ actor?: string | undefined;
186
+ step?: string | undefined;
187
+ }[];
188
+ export declare function addClassifierUsage(base: RunUsage | undefined, entries: readonly UsageEntry[]): RunUsage | undefined;
189
+ export declare class AttemptAuditCapture {
190
+ private readonly usage;
191
+ private readonly judges;
192
+ private readonly reviewer;
193
+ private readonly outcomes;
194
+ recordUsage(role: UsageEntry["role"], usage: Usage, context: Pick<UsageEntry, "actor" | "step">): void;
195
+ record(events: readonly RunEvent[]): void;
196
+ document(): AttemptAudit;
197
+ }
198
+ export declare function projectAttemptAudit(metadata: JsonObject | undefined, usage?: RunUsage, reviews?: readonly JudgeReview[]): {
199
+ knownCostUsd: number | null;
200
+ costUnknown: boolean;
201
+ roles: {
202
+ [k: string]: string;
203
+ };
204
+ judges: {
205
+ reviews: {
206
+ schemaVersion: 1;
207
+ eventSeq: number;
208
+ actor: string;
209
+ at: number;
210
+ reason: string;
211
+ verdict: "passed" | "failed" | "inconclusive";
212
+ dbUser?: string | undefined;
213
+ host?: string | undefined;
214
+ }[];
215
+ effectiveVerdict: "passed" | "failed" | "inconclusive";
216
+ reviewStatus: string;
217
+ eventSeq: number;
218
+ actor: string | null;
219
+ step: string | null;
220
+ verdict: "passed" | "failed" | "inconclusive";
221
+ confidence: number | null;
222
+ reason: string;
223
+ rubricVersion: string | null;
224
+ rubricHash: string | null;
225
+ outcome: {
226
+ eventSeq: number;
227
+ type: string;
228
+ passed: boolean;
229
+ }[];
230
+ }[];
231
+ schemaVersion: 1;
232
+ usage: {
233
+ inputTokens: number;
234
+ outputTokens: number;
235
+ role: "heal" | "main" | "judge" | "reviewer" | "classifier";
236
+ billing: "subscription" | "metered" | null;
237
+ cacheReadTokens?: number | undefined;
238
+ cacheWriteTokens?: number | undefined;
239
+ costUsd?: number | undefined;
240
+ costUnknown?: true | undefined;
241
+ model?: {
242
+ provider: string | null;
243
+ model: string | null;
244
+ seed: number | "unknown" | "unsupported";
245
+ billingVersion: string | null;
246
+ deployment?: string | undefined;
247
+ promptVersion?: string | undefined;
248
+ promptHash?: string | undefined;
249
+ parameters?: Record<string, z.core.util.JSONType> | undefined;
250
+ } | undefined;
251
+ billingMode?: "subscription" | "metered" | undefined;
252
+ costUnknownReason?: "subscription" | "unpriced" | "usage-unavailable" | "billing-unrecorded" | undefined;
253
+ actor?: string | undefined;
254
+ step?: string | undefined;
255
+ }[];
256
+ reviewer: {
257
+ eventSeq: number;
258
+ status: "completed" | "incomplete" | "skipped";
259
+ reason: string;
260
+ failureType: string | null;
261
+ rubricVersion: string | null;
262
+ rubricHash: string | null;
263
+ }[];
264
+ environment?: {
265
+ image: {
266
+ template: string | null;
267
+ digest: string | null;
268
+ };
269
+ runtime: string | null;
270
+ views: {
271
+ view: string;
272
+ browser: string | null;
273
+ viewport: {
274
+ width: number;
275
+ height: number;
276
+ } | null;
277
+ device: string | null;
278
+ }[];
279
+ } | undefined;
280
+ } | null;
281
+ export declare function classifierAuditUpdate(metadata: JsonObject | undefined, base: RunUsage | undefined, samples: readonly Usage[]): {
282
+ metadata: {
283
+ attemptAudit: {
284
+ usage: {
285
+ inputTokens: number;
286
+ outputTokens: number;
287
+ role: "heal" | "main" | "judge" | "reviewer" | "classifier";
288
+ billing: "subscription" | "metered" | null;
289
+ cacheReadTokens?: number | undefined;
290
+ cacheWriteTokens?: number | undefined;
291
+ costUsd?: number | undefined;
292
+ costUnknown?: true | undefined;
293
+ model?: {
294
+ provider: string | null;
295
+ model: string | null;
296
+ seed: number | "unknown" | "unsupported";
297
+ billingVersion: string | null;
298
+ deployment?: string | undefined;
299
+ promptVersion?: string | undefined;
300
+ promptHash?: string | undefined;
301
+ parameters?: Record<string, z.core.util.JSONType> | undefined;
302
+ } | undefined;
303
+ billingMode?: "subscription" | "metered" | undefined;
304
+ costUnknownReason?: "subscription" | "unpriced" | "usage-unavailable" | "billing-unrecorded" | undefined;
305
+ actor?: string | undefined;
306
+ step?: string | undefined;
307
+ }[];
308
+ schemaVersion: 1;
309
+ judges: {
310
+ eventSeq: number;
311
+ actor: string | null;
312
+ step: string | null;
313
+ verdict: "passed" | "failed" | "inconclusive";
314
+ confidence: number | null;
315
+ reason: string;
316
+ rubricVersion: string | null;
317
+ rubricHash: string | null;
318
+ outcome: {
319
+ eventSeq: number;
320
+ type: string;
321
+ passed: boolean;
322
+ }[];
323
+ reviewStatus: "recorded" | "pending" | "deterministic";
324
+ }[];
325
+ reviewer: {
326
+ eventSeq: number;
327
+ status: "completed" | "incomplete" | "skipped";
328
+ reason: string;
329
+ failureType: string | null;
330
+ rubricVersion: string | null;
331
+ rubricHash: string | null;
332
+ }[];
333
+ environment?: {
334
+ image: {
335
+ template: string | null;
336
+ digest: string | null;
337
+ };
338
+ runtime: string | null;
339
+ views: {
340
+ view: string;
341
+ browser: string | null;
342
+ viewport: {
343
+ width: number;
344
+ height: number;
345
+ } | null;
346
+ device: string | null;
347
+ }[];
348
+ } | undefined;
349
+ };
350
+ };
351
+ usage: {
352
+ inputTokens: number;
353
+ outputTokens: number;
354
+ actions: number;
355
+ minutes: number;
356
+ cacheReadTokens?: number | undefined;
357
+ cacheWriteTokens?: number | undefined;
358
+ costUsd?: number | undefined;
359
+ costUnknown?: true | undefined;
360
+ e2bMinutes?: number | undefined;
361
+ } | undefined;
362
+ } | undefined;
363
+ export {};
@@ -0,0 +1,253 @@
1
+ import { z } from "zod";
2
+ import { isDeepStrictEqual } from "node:util";
3
+ import { publicRunUsage } from "@open-qa/qa-engine";
4
+ import {
5
+ usageSchema
6
+ } from "@open-qa/qa-engine/protocol";
7
+ const judgeVerdict = z.enum(["passed", "failed", "inconclusive"]);
8
+ const outcomeReference = z.strictObject({
9
+ eventSeq: z.number().int().positive(),
10
+ type: z.string(),
11
+ passed: z.boolean()
12
+ });
13
+ const judgeEvidence = z.strictObject({
14
+ eventSeq: z.number().int().positive(),
15
+ actor: z.string().nullable(),
16
+ step: z.string().nullable(),
17
+ verdict: judgeVerdict,
18
+ confidence: z.number().nullable(),
19
+ reason: z.string(),
20
+ rubricVersion: z.string().nullable(),
21
+ rubricHash: z.string().nullable(),
22
+ outcome: z.array(outcomeReference),
23
+ reviewStatus: z.enum(["pending", "recorded", "deterministic"])
24
+ });
25
+ const judgeReviewSchema = z.strictObject({
26
+ schemaVersion: z.literal(1),
27
+ eventSeq: z.number().int().positive(),
28
+ actor: z.string().min(1).max(200),
29
+ dbUser: z.string().optional(),
30
+ host: z.string().optional(),
31
+ at: z.number().nonnegative(),
32
+ reason: z.string().trim().min(1).max(500),
33
+ verdict: judgeVerdict
34
+ });
35
+ const roleSchema = z.enum(["main", "heal", "judge", "reviewer", "classifier"]);
36
+ const usageEntrySchema = z.strictObject({
37
+ role: roleSchema,
38
+ actor: z.string().optional(),
39
+ step: z.string().optional(),
40
+ billing: z.enum(["subscription", "metered"]).nullable(),
41
+ ...usageSchema.shape
42
+ });
43
+ const attemptAuditSchema = z.strictObject({
44
+ schemaVersion: z.literal(1),
45
+ environment: z.strictObject({
46
+ image: z.strictObject({ template: z.string().nullable(), digest: z.string().nullable() }),
47
+ runtime: z.string().nullable(),
48
+ views: z.array(
49
+ z.strictObject({
50
+ view: z.string(),
51
+ browser: z.string().nullable(),
52
+ viewport: z.strictObject({ width: z.number(), height: z.number() }).nullable(),
53
+ device: z.string().nullable()
54
+ })
55
+ )
56
+ }).optional(),
57
+ usage: z.array(usageEntrySchema),
58
+ judges: z.array(judgeEvidence),
59
+ reviewer: z.array(
60
+ z.strictObject({
61
+ eventSeq: z.number().int().positive(),
62
+ status: z.enum(["completed", "incomplete", "skipped"]),
63
+ reason: z.string(),
64
+ failureType: z.string().nullable(),
65
+ rubricVersion: z.string().nullable(),
66
+ rubricHash: z.string().nullable()
67
+ })
68
+ )
69
+ });
70
+ function roleUsage(role, input) {
71
+ const { billingMode, ...usage } = input;
72
+ const { costUsd, costUnknown, ...rest } = usage;
73
+ if (billingMode === "subscription")
74
+ return {
75
+ role,
76
+ billing: billingMode,
77
+ ...rest,
78
+ costUnknownReason: rest.costUnknownReason ?? "subscription"
79
+ };
80
+ return {
81
+ role,
82
+ billing: billingMode ?? null,
83
+ ...rest,
84
+ ...costUsd !== void 0 && { costUsd },
85
+ ...costUnknown || costUsd === void 0 ? {
86
+ costUnknown: true,
87
+ costUnknownReason: usage.costUnknownReason ?? "unpriced"
88
+ } : {}
89
+ };
90
+ }
91
+ const numberFields = [
92
+ "inputTokens",
93
+ "outputTokens",
94
+ "cacheReadTokens",
95
+ "cacheWriteTokens",
96
+ "costUsd"
97
+ ];
98
+ function combineRoleUsage(entries) {
99
+ const groups = /* @__PURE__ */ new Map();
100
+ for (const entry of entries) {
101
+ const key = JSON.stringify([
102
+ entry.role,
103
+ entry.actor ?? null,
104
+ entry.step ?? null,
105
+ entry.billing,
106
+ entry.model ?? null,
107
+ entry.costUnknownReason ?? null
108
+ ]);
109
+ const prior = groups.get(key);
110
+ if (!prior) {
111
+ groups.set(key, { ...entry });
112
+ continue;
113
+ }
114
+ for (const field of numberFields)
115
+ if (entry[field] !== void 0) prior[field] = (prior[field] ?? 0) + entry[field];
116
+ if (entry.costUnknown) prior.costUnknown = true;
117
+ }
118
+ const totals = [...groups.values()].map((entry) => ({
119
+ ...entry,
120
+ ...entry.costUsd !== void 0 && { costUsd: Number(entry.costUsd.toFixed(6)) }
121
+ }));
122
+ return attemptAuditSchema.shape.usage.parse(JSON.parse(JSON.stringify(totals)));
123
+ }
124
+ function addClassifierUsage(base, entries) {
125
+ if (!entries.length) return base;
126
+ const usage = base ? { ...base } : { inputTokens: 0, outputTokens: 0, actions: 0, minutes: 0 };
127
+ for (const entry of entries) {
128
+ for (const field of numberFields)
129
+ if (entry[field] !== void 0) usage[field] = (usage[field] ?? 0) + entry[field];
130
+ if (entry.costUnknown || entry.costUsd === void 0) usage.costUnknown = true;
131
+ }
132
+ return usage;
133
+ }
134
+ function stringField(source, name) {
135
+ const value = source?.[name];
136
+ return typeof value === "string" ? value : null;
137
+ }
138
+ class AttemptAuditCapture {
139
+ usage = [];
140
+ judges = [];
141
+ reviewer = [];
142
+ outcomes = /* @__PURE__ */ new Map();
143
+ recordUsage(role, usage, context) {
144
+ this.usage.push({ ...roleUsage(role, usageSchema.parse(usage)), ...context });
145
+ }
146
+ record(events) {
147
+ for (const event of events) {
148
+ if (event.kind === "expect_result" && /^(http|db)\./.test(event.condition.type)) {
149
+ const key = JSON.stringify([event.actor, event.step]);
150
+ const existing = this.outcomes.get(key) ?? [];
151
+ existing.push({
152
+ condition: JSON.stringify(event.condition),
153
+ receipt: { eventSeq: event.seq, type: event.condition.type, passed: event.result.passed }
154
+ });
155
+ this.outcomes.set(key, existing);
156
+ }
157
+ if (event.kind === "judge_result" && event.by === "conductor") {
158
+ const { result } = event;
159
+ const outcome = this.outcomes.get(JSON.stringify([event.actor, event.step])) ?? [];
160
+ const deterministic = event.result.evidence.source === "deterministic";
161
+ const latest = new Map(outcome.map(({ condition, receipt }) => [condition, receipt]));
162
+ this.judges.push({
163
+ eventSeq: event.seq,
164
+ actor: event.actor ?? null,
165
+ step: event.step ?? null,
166
+ verdict: result.verdict,
167
+ confidence: result.confidence ?? null,
168
+ reason: result.reason,
169
+ rubricVersion: stringField(result.evidence, "rubricVersion"),
170
+ rubricHash: stringField(result.evidence, "rubricHash"),
171
+ outcome: outcome.map(({ receipt }) => receipt),
172
+ reviewStatus: deterministic ? "deterministic" : result.verdict === "inconclusive" || [...latest.values()].some(({ passed }) => passed !== (result.verdict === "passed")) ? "pending" : "recorded"
173
+ });
174
+ }
175
+ if (event.kind === "reviewer_result" && event.by === "conductor")
176
+ this.reviewer.push({
177
+ eventSeq: event.seq,
178
+ status: event.result.status,
179
+ reason: event.result.reason,
180
+ failureType: event.failureType ?? null,
181
+ rubricVersion: stringField(event.result.evidence, "rubricVersion"),
182
+ rubricHash: stringField(event.result.evidence, "rubricHash")
183
+ });
184
+ }
185
+ }
186
+ document() {
187
+ const pending = this.reviewer.some(
188
+ ({ status, failureType }) => status === "incomplete" || failureType === "agent"
189
+ );
190
+ return {
191
+ schemaVersion: 1,
192
+ usage: combineRoleUsage(this.usage),
193
+ reviewer: this.reviewer,
194
+ judges: this.judges.map(
195
+ (judge) => pending && judge.reviewStatus === "recorded" ? { ...judge, reviewStatus: "pending" } : judge
196
+ )
197
+ };
198
+ }
199
+ }
200
+ function projectAttemptAudit(metadata, usage, reviews = []) {
201
+ const audit = attemptAuditSchema.safeParse(metadata?.attemptAudit);
202
+ if (!audit.success) return null;
203
+ return {
204
+ ...audit.data,
205
+ knownCostUsd: (usage && publicRunUsage(usage).costUsd) ?? null,
206
+ costUnknown: !!usage?.costUnknown || usage?.costUsd === void 0 || audit.data.usage.some((entry) => entry.billing === "subscription" || entry.costUnknown),
207
+ roles: Object.fromEntries(
208
+ roleSchema.options.map((role) => [
209
+ role,
210
+ audit.data.usage.some((entry) => entry.role === role) ? "recorded" : "no-usage-recorded"
211
+ ])
212
+ ),
213
+ judges: audit.data.judges.map((judge) => {
214
+ const decisions = reviews.filter((review) => review.eventSeq === judge.eventSeq).sort((a, b) => a.at - b.at);
215
+ return {
216
+ ...judge,
217
+ reviews: decisions,
218
+ effectiveVerdict: decisions.at(-1)?.verdict ?? judge.verdict,
219
+ reviewStatus: decisions.length ? "reviewed" : judge.reviewStatus
220
+ };
221
+ })
222
+ };
223
+ }
224
+ function classifierAuditUpdate(metadata, base, samples) {
225
+ const parsed = attemptAuditSchema.safeParse(metadata?.attemptAudit);
226
+ const audit = parsed.success ? parsed.data : { schemaVersion: 1, usage: [], judges: [], reviewer: [] };
227
+ const classifier = combineRoleUsage(
228
+ samples.map((sample) => roleUsage("classifier", usageSchema.parse(sample)))
229
+ );
230
+ const existing = audit.usage.filter(({ role }) => role === "classifier");
231
+ if (existing.length) {
232
+ if (!isDeepStrictEqual(existing, classifier))
233
+ throw new Error("Conflicting immutable classifier usage");
234
+ return;
235
+ }
236
+ return {
237
+ metadata: { ...metadata, attemptAudit: { ...audit, usage: [...audit.usage, ...classifier] } },
238
+ usage: addClassifierUsage(
239
+ base,
240
+ samples.map((sample) => roleUsage("classifier", sample))
241
+ )
242
+ };
243
+ }
244
+ export {
245
+ AttemptAuditCapture,
246
+ addClassifierUsage,
247
+ attemptAuditSchema,
248
+ classifierAuditUpdate,
249
+ combineRoleUsage,
250
+ judgeReviewSchema,
251
+ projectAttemptAudit,
252
+ roleUsage
253
+ };
@@ -0,0 +1,13 @@
1
+ interface AuditOptions {
2
+ root?: string;
3
+ postgres?: boolean;
4
+ project?: string;
5
+ }
6
+ export declare function trialCommand(groupId: string | undefined, options: AuditOptions & {
7
+ flow?: string;
8
+ }): Promise<number>;
9
+ export declare function judgeReviewCommand(input: string, seq: string, options: AuditOptions & {
10
+ verdict?: string;
11
+ reason?: string;
12
+ }): Promise<number>;
13
+ export {};