@tangle-network/agent-eval 0.23.1 → 0.25.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 (148) hide show
  1. package/CHANGELOG.md +145 -0
  2. package/README.md +212 -79
  3. package/dist/baseline-4R5deP0N.d.ts +108 -0
  4. package/dist/benchmarks/index.d.ts +3 -2
  5. package/dist/benchmarks/index.js +1 -1
  6. package/dist/builder-eval/index.d.ts +249 -0
  7. package/dist/builder-eval/index.js +391 -0
  8. package/dist/builder-eval/index.js.map +1 -0
  9. package/dist/{chunk-IOXMGMHQ.js → chunk-2A5XJB43.js} +142 -318
  10. package/dist/chunk-2A5XJB43.js.map +1 -0
  11. package/dist/chunk-47X6LRCE.js +76 -0
  12. package/dist/chunk-47X6LRCE.js.map +1 -0
  13. package/dist/{chunk-6M774GY6.js → chunk-4F5DQN55.js} +1 -1
  14. package/dist/chunk-4F5DQN55.js.map +1 -0
  15. package/dist/{chunk-KAO3Q65R.js → chunk-4S4BM3QQ.js} +15 -13
  16. package/dist/chunk-4S4BM3QQ.js.map +1 -0
  17. package/dist/chunk-5BKGXME7.js +65 -0
  18. package/dist/chunk-5BKGXME7.js.map +1 -0
  19. package/dist/{chunk-6KQG5HAH.js → chunk-5LBB5B3Z.js} +376 -72
  20. package/dist/chunk-5LBB5B3Z.js.map +1 -0
  21. package/dist/{chunk-42I2QC2L.js → chunk-6QDKWHLS.js} +18 -14
  22. package/dist/chunk-6QDKWHLS.js.map +1 -0
  23. package/dist/{chunk-VQQSPGSM.js → chunk-EDUKQ5AM.js} +247 -189
  24. package/dist/chunk-EDUKQ5AM.js.map +1 -0
  25. package/dist/chunk-I4MBDTY5.js +272 -0
  26. package/dist/chunk-I4MBDTY5.js.map +1 -0
  27. package/dist/chunk-JLZQWFV3.js +618 -0
  28. package/dist/chunk-JLZQWFV3.js.map +1 -0
  29. package/dist/chunk-K2TPS5LB.js +569 -0
  30. package/dist/chunk-K2TPS5LB.js.map +1 -0
  31. package/dist/chunk-KKHDIONI.js +414 -0
  32. package/dist/chunk-KKHDIONI.js.map +1 -0
  33. package/dist/chunk-KMPRBJK4.js +74 -0
  34. package/dist/chunk-KMPRBJK4.js.map +1 -0
  35. package/dist/{chunk-QUKKGHTZ.js → chunk-KTGTIOFD.js} +6 -3
  36. package/dist/chunk-KTGTIOFD.js.map +1 -0
  37. package/dist/chunk-LSH4MMOZ.js +838 -0
  38. package/dist/chunk-LSH4MMOZ.js.map +1 -0
  39. package/dist/chunk-NG236HPC.js +57 -0
  40. package/dist/chunk-NG236HPC.js.map +1 -0
  41. package/dist/{chunk-QBW3YBTR.js → chunk-NLMNWKVM.js} +14 -6
  42. package/dist/chunk-NLMNWKVM.js.map +1 -0
  43. package/dist/chunk-NU65VQ7M.js +99 -0
  44. package/dist/chunk-NU65VQ7M.js.map +1 -0
  45. package/dist/chunk-OWLAAMME.js +250 -0
  46. package/dist/chunk-OWLAAMME.js.map +1 -0
  47. package/dist/{chunk-SQQLHODJ.js → chunk-PC4UYEBM.js} +7 -4
  48. package/dist/chunk-PC4UYEBM.js.map +1 -0
  49. package/dist/{chunk-7EAUOUQS.js → chunk-RAF443UI.js} +213 -115
  50. package/dist/chunk-RAF443UI.js.map +1 -0
  51. package/dist/chunk-RZTMDUO7.js +49 -0
  52. package/dist/chunk-RZTMDUO7.js.map +1 -0
  53. package/dist/{chunk-EXGR4XEM.js → chunk-SESZDQPX.js} +23 -19
  54. package/dist/chunk-SESZDQPX.js.map +1 -0
  55. package/dist/{chunk-5IIQKMD5.js → chunk-TVVP3ZZQ.js} +14 -4
  56. package/dist/chunk-TVVP3ZZQ.js.map +1 -0
  57. package/dist/chunk-WWYCWKUM.js +196 -0
  58. package/dist/chunk-WWYCWKUM.js.map +1 -0
  59. package/dist/{chunk-AXHNWLIX.js → chunk-YRZ4M5GS.js} +2 -90
  60. package/dist/chunk-YRZ4M5GS.js.map +1 -0
  61. package/dist/chunk-ZN274SWR.js +613 -0
  62. package/dist/chunk-ZN274SWR.js.map +1 -0
  63. package/dist/cli.js +10 -6
  64. package/dist/cli.js.map +1 -1
  65. package/dist/{control-DvkH87qJ.d.ts → control-CBShYYA6.d.ts} +32 -33
  66. package/dist/control-runtime-BuJHoLg0.d.ts +180 -0
  67. package/dist/control.d.ts +8 -6
  68. package/dist/control.js +10 -7
  69. package/dist/{dataset-B9qvlm_o.d.ts → dataset-CiK_3LDr.d.ts} +5 -2
  70. package/dist/{emitter-B2XqDKFU.d.ts → emitter-DP_cSSiw.d.ts} +1 -1
  71. package/dist/errors-BZ9sTdz7.d.ts +70 -0
  72. package/dist/failure-cluster-C2EGSDiT.d.ts +76 -0
  73. package/dist/feedback-trajectory-DfFdrraJ.d.ts +169 -0
  74. package/dist/governance/index.d.ts +5 -0
  75. package/dist/governance/index.js +18 -0
  76. package/dist/governance/index.js.map +1 -0
  77. package/dist/{index-DDTlbHEK.d.ts → index--fVrWDiR.d.ts} +1 -1
  78. package/dist/index-Oj9fAPPN.d.ts +270 -0
  79. package/dist/index.d.ts +2018 -3003
  80. package/dist/index.js +7443 -9102
  81. package/dist/index.js.map +1 -1
  82. package/dist/{integrity-Cr5YodSY.d.ts → integrity-DK2EBVZC.d.ts} +4 -3
  83. package/dist/knowledge/index.d.ts +102 -0
  84. package/dist/knowledge/index.js +18 -0
  85. package/dist/knowledge/index.js.map +1 -0
  86. package/dist/meta-eval/index.d.ts +99 -0
  87. package/dist/meta-eval/index.js +324 -0
  88. package/dist/meta-eval/index.js.map +1 -0
  89. package/dist/multi-layer-verifier-LkP3LVKj.d.ts +141 -0
  90. package/dist/openapi.json +491 -1
  91. package/dist/optimization.d.ts +11 -8
  92. package/dist/optimization.js +11 -9
  93. package/dist/outcome-store-D6KWmYvj.d.ts +63 -0
  94. package/dist/pipelines/index.d.ts +172 -0
  95. package/dist/pipelines/index.js +345 -0
  96. package/dist/pipelines/index.js.map +1 -0
  97. package/dist/prm/index.d.ts +99 -0
  98. package/dist/prm/index.js +222 -0
  99. package/dist/prm/index.js.map +1 -0
  100. package/dist/query-DODUYdPg.d.ts +30 -0
  101. package/dist/release-report-BNgMdqPF.d.ts +292 -0
  102. package/dist/replay-BL96gCEP.d.ts +226 -0
  103. package/dist/reporting.d.ts +10 -295
  104. package/dist/reporting.js +10 -6
  105. package/dist/{eval-campaign-Ds5QljIh.d.ts → researcher-BPT8x_NT.d.ts} +148 -146
  106. package/dist/rl.d.ts +1762 -8
  107. package/dist/rl.js +2035 -58
  108. package/dist/rl.js.map +1 -1
  109. package/dist/rubric-D5tjHNJQ.d.ts +72 -0
  110. package/dist/rubric-predictive-validity-C0uDYwG6.d.ts +105 -0
  111. package/dist/{run-record-DNiOMBrZ.d.ts → run-record-CqzahIbx.d.ts} +4 -1
  112. package/dist/sequential-Dgz1n51-.d.ts +139 -0
  113. package/dist/{store-u47QaJ9G.d.ts → store-Db2Bv8Cf.d.ts} +1 -1
  114. package/dist/{summary-report-Ce1r4EYo.d.ts → summary-report-C7VPYEj2.d.ts} +3 -76
  115. package/dist/telemetry/file.js +4 -1
  116. package/dist/telemetry/file.js.map +1 -1
  117. package/dist/telemetry/index.js +57 -57
  118. package/dist/telemetry/index.js.map +1 -1
  119. package/dist/test-graded-scenario-B2kWEdh9.d.ts +146 -0
  120. package/dist/traces.d.ts +142 -387
  121. package/dist/traces.js +1302 -40
  122. package/dist/traces.js.map +1 -1
  123. package/dist/trajectory-CnoBo-JY.d.ts +32 -0
  124. package/dist/wire/index.d.ts +369 -25
  125. package/dist/wire/index.js +22 -3
  126. package/package.json +44 -18
  127. package/dist/chunk-42I2QC2L.js.map +0 -1
  128. package/dist/chunk-5IIQKMD5.js.map +0 -1
  129. package/dist/chunk-6KQG5HAH.js.map +0 -1
  130. package/dist/chunk-6M774GY6.js.map +0 -1
  131. package/dist/chunk-7EAUOUQS.js.map +0 -1
  132. package/dist/chunk-AXHNWLIX.js.map +0 -1
  133. package/dist/chunk-EXGR4XEM.js.map +0 -1
  134. package/dist/chunk-IOXMGMHQ.js.map +0 -1
  135. package/dist/chunk-KAO3Q65R.js.map +0 -1
  136. package/dist/chunk-LZKIOBG2.js +0 -2026
  137. package/dist/chunk-LZKIOBG2.js.map +0 -1
  138. package/dist/chunk-QBW3YBTR.js.map +0 -1
  139. package/dist/chunk-QUKKGHTZ.js.map +0 -1
  140. package/dist/chunk-SQQLHODJ.js.map +0 -1
  141. package/dist/chunk-V5QSWN7L.js +0 -1310
  142. package/dist/chunk-V5QSWN7L.js.map +0 -1
  143. package/dist/chunk-VQQSPGSM.js.map +0 -1
  144. package/dist/chunk-XPHOZPOM.js +0 -1947
  145. package/dist/chunk-XPHOZPOM.js.map +0 -1
  146. package/dist/feedback-trajectory-c43WGtTX.d.ts +0 -346
  147. package/dist/index-ekBXweiQ.d.ts +0 -1894
  148. package/dist/sequential-DgU2mFsE.d.ts +0 -304
@@ -0,0 +1,613 @@
1
+ import {
2
+ objectiveEval,
3
+ runAgentControlLoop
4
+ } from "./chunk-LSH4MMOZ.js";
5
+ import {
6
+ validateRunRecord
7
+ } from "./chunk-NLMNWKVM.js";
8
+ import {
9
+ TraceEmitter
10
+ } from "./chunk-TVVP3ZZQ.js";
11
+
12
+ // src/action-policy.ts
13
+ function evaluateActionPolicy(action, policy = {}, options = {}) {
14
+ const reasons = [];
15
+ let blocked = false;
16
+ let requiresApproval = Boolean(action.requiresApproval);
17
+ if (policy.allowedTypes?.length && !policy.allowedTypes.includes(action.type)) {
18
+ blocked = true;
19
+ reasons.push(`action type "${action.type}" is not allowed`);
20
+ }
21
+ if (policy.blockedTypes?.includes(action.type)) {
22
+ blocked = true;
23
+ reasons.push(`action type "${action.type}" is blocked`);
24
+ }
25
+ if (policy.alwaysRequireApprovalTypes?.includes(action.type)) {
26
+ requiresApproval = true;
27
+ reasons.push(`action type "${action.type}" requires approval`);
28
+ }
29
+ if (policy.requireApprovalForExternalSideEffects && action.externalSideEffect) {
30
+ requiresApproval = true;
31
+ reasons.push("external side effect requires approval");
32
+ }
33
+ if (policy.requireApprovalAboveCostUsd !== void 0 && (action.costUsd ?? 0) > policy.requireApprovalAboveCostUsd) {
34
+ requiresApproval = true;
35
+ reasons.push(
36
+ `cost ${action.costUsd} exceeds approval threshold ${policy.requireApprovalAboveCostUsd}`
37
+ );
38
+ }
39
+ if (policy.maxActionCostUsd !== void 0 && (action.costUsd ?? 0) > policy.maxActionCostUsd) {
40
+ blocked = true;
41
+ reasons.push(`cost ${action.costUsd} exceeds max action cost ${policy.maxActionCostUsd}`);
42
+ }
43
+ if (policy.remainingBudgetUsd !== void 0 && (action.costUsd ?? 0) > policy.remainingBudgetUsd) {
44
+ blocked = true;
45
+ reasons.push(`cost ${action.costUsd} exceeds remaining budget ${policy.remainingBudgetUsd}`);
46
+ }
47
+ if (policy.expectedOutcomeRequired && !action.metadata?.expectedOutcome) {
48
+ blocked = true;
49
+ reasons.push("expected outcome is required");
50
+ }
51
+ if (policy.killCriteriaRequired && !action.metadata?.killCriteria) {
52
+ blocked = true;
53
+ reasons.push("kill criteria are required");
54
+ }
55
+ if (policy.autoApproveTypes?.includes(action.type) && requiresApproval) {
56
+ reasons.push(
57
+ `action type "${action.type}" is auto-approved only when no approval policy applies`
58
+ );
59
+ }
60
+ if (!reasons.length) reasons.push(requiresApproval ? "approval required" : "action allowed");
61
+ const label = blocked || requiresApproval ? {
62
+ source: "policy",
63
+ kind: blocked ? "policy_block" : "comment",
64
+ value: { actionType: action.type, blocked, requiresApproval },
65
+ reason: reasons.join("; "),
66
+ severity: blocked ? "critical" : "warning",
67
+ createdAt: options.createdAt ?? (/* @__PURE__ */ new Date()).toISOString(),
68
+ metadata: { action, policy }
69
+ } : void 0;
70
+ return {
71
+ allowed: !blocked,
72
+ blocked,
73
+ requiresApproval: !blocked && requiresApproval,
74
+ reasons,
75
+ label
76
+ };
77
+ }
78
+
79
+ // src/run-evidence.ts
80
+ function controlRunToRunRecord(run, options) {
81
+ const score = clampScore(
82
+ options.score ?? run.score ?? scoreFromEvals(run.finalEvals) ?? (run.pass ? 1 : 0)
83
+ );
84
+ const outcome = options.splitTag === "holdout" ? { holdoutScore: score, raw: normalizeRawMetrics(options.raw, run, score) } : { searchScore: score, raw: normalizeRawMetrics(options.raw, run, score) };
85
+ return validateRunRecord({
86
+ runId: options.runId ?? run.runId ?? `control:${options.experimentId}:${options.candidateId}:${options.seed}:${options.splitTag}`,
87
+ experimentId: options.experimentId,
88
+ candidateId: options.candidateId,
89
+ seed: options.seed,
90
+ model: options.model,
91
+ promptHash: options.promptHash,
92
+ configHash: options.configHash,
93
+ commitSha: options.commitSha,
94
+ wallMs: run.wallMs,
95
+ ...options.queueMs !== void 0 ? { queueMs: options.queueMs } : {},
96
+ costUsd: run.spentCostUsd,
97
+ tokenUsage: options.tokenUsage,
98
+ ...options.judgeMetadata ? { judgeMetadata: options.judgeMetadata } : {},
99
+ outcome,
100
+ failureMode: options.failureMode ?? failureModeFromRun(run),
101
+ splitTag: options.splitTag
102
+ });
103
+ }
104
+ function scoreFromEvals(evals) {
105
+ const scores = evals.map((e) => e.score).filter((score) => typeof score === "number" && Number.isFinite(score));
106
+ if (scores.length === 0) return void 0;
107
+ return clampScore(scores.reduce((sum, score) => sum + score, 0) / scores.length);
108
+ }
109
+ function normalizeRawMetrics(raw, run, score) {
110
+ return {
111
+ ...finiteOnly(raw ?? {}),
112
+ score,
113
+ pass: run.pass ? 1 : 0,
114
+ completed: run.completed ? 1 : 0,
115
+ steps: run.steps.length,
116
+ runtimeErrors: run.runtimeErrors.length
117
+ };
118
+ }
119
+ function finiteOnly(values) {
120
+ const out = {};
121
+ for (const [key, value] of Object.entries(values)) {
122
+ if (Number.isFinite(value)) out[key] = value;
123
+ }
124
+ return out;
125
+ }
126
+ function failureModeFromRun(run) {
127
+ if (run.pass) return void 0;
128
+ return run.failureClass ?? "unknown";
129
+ }
130
+ function clampScore(value) {
131
+ if (!Number.isFinite(value)) return 0;
132
+ return Math.max(0, Math.min(1, value));
133
+ }
134
+
135
+ // src/propose-review.ts
136
+ import { appendFileSync, existsSync, mkdirSync, readFileSync } from "fs";
137
+ import { dirname } from "path";
138
+ function inMemoryReviewStore(initial = []) {
139
+ const entries = [...initial];
140
+ return {
141
+ async load() {
142
+ return [...entries];
143
+ },
144
+ async append(entry) {
145
+ entries.push(entry);
146
+ }
147
+ };
148
+ }
149
+ function jsonlReviewStore(path) {
150
+ return {
151
+ async load() {
152
+ if (!existsSync(path)) return [];
153
+ const raw = readFileSync(path, "utf8");
154
+ const out = [];
155
+ for (const line of raw.split("\n")) {
156
+ const trimmed = line.trim();
157
+ if (!trimmed) continue;
158
+ try {
159
+ out.push(JSON.parse(trimmed));
160
+ } catch {
161
+ }
162
+ }
163
+ return out;
164
+ },
165
+ async append(entry) {
166
+ mkdirSync(dirname(path), { recursive: true });
167
+ appendFileSync(path, `${JSON.stringify(entry)}
168
+ `);
169
+ }
170
+ };
171
+ }
172
+ var DEFAULT_FALLBACK_INSTRUCTION = "Inspect the verification failures above. Fix the critical issues first, then the major ones. Do not restate the failures \u2014 act on them.";
173
+ async function runProposeReview(config) {
174
+ const maxShots = config.maxShots ?? 10;
175
+ const maxWallMs = config.maxWallMs ?? 10 * 60 * 1e3;
176
+ const confidenceFloor = config.confidenceFloor ?? 0.3;
177
+ const confidenceFloorWindow = config.confidenceFloorWindow ?? 2;
178
+ const memory = config.memory ?? inMemoryReviewStore();
179
+ const fallbackInstruction = config.fallbackInstruction ?? DEFAULT_FALLBACK_INSTRUCTION;
180
+ const emitter = config.store ? new TraceEmitter(config.store) : null;
181
+ if (emitter) {
182
+ await emitter.startRun({
183
+ scenarioId: config.scenarioId ?? "propose-review",
184
+ projectId: config.projectId,
185
+ variantId: config.variantId,
186
+ layer: "meta",
187
+ tags: {
188
+ goal: config.goal.slice(0, 120),
189
+ maxShots: String(maxShots)
190
+ }
191
+ });
192
+ }
193
+ const abort = new AbortController();
194
+ const wallStart = Date.now();
195
+ const wallTimer = setTimeout(
196
+ () => abort.abort(new Error("propose-review wall timeout")),
197
+ maxWallMs
198
+ );
199
+ const shots = [];
200
+ let state = config.initialState;
201
+ let priorReview = null;
202
+ let lastVerification = { pass: false };
203
+ let failureClass;
204
+ let completed = false;
205
+ let lowConfidenceStreak = 0;
206
+ try {
207
+ for (let shot = 1; shot <= maxShots; shot++) {
208
+ if (abort.signal.aborted) {
209
+ failureClass = "timeout";
210
+ break;
211
+ }
212
+ const shotStart = Date.now();
213
+ const shotHandle = emitter ? await emitter.span({ kind: "tool", name: `shot-${shot}` }) : null;
214
+ let proposeOut;
215
+ try {
216
+ proposeOut = await config.propose({
217
+ shot,
218
+ goal: config.goal,
219
+ state,
220
+ priorReview,
221
+ abortSignal: abort.signal,
222
+ emitter: emitter ?? void 0
223
+ });
224
+ } catch (err) {
225
+ await shotHandle?.fail(err instanceof Error ? err : String(err));
226
+ failureClass = "unknown";
227
+ throw err;
228
+ }
229
+ state = proposeOut.state;
230
+ const traceSummary = proposeOut.traceSummary;
231
+ let verification;
232
+ try {
233
+ verification = await config.verify(state);
234
+ } catch (err) {
235
+ await shotHandle?.fail(err instanceof Error ? err : String(err));
236
+ failureClass = "unknown";
237
+ throw err;
238
+ }
239
+ lastVerification = verification;
240
+ const memorySnapshot = await memory.load();
241
+ const verificationDigest = {
242
+ pass: verification.pass,
243
+ score: verification.score,
244
+ failingLayers: verification.failingLayers ?? []
245
+ };
246
+ let review;
247
+ let reviewAvailable = true;
248
+ let reviewError;
249
+ if (verification.pass) {
250
+ review = {
251
+ observations: "verification passed \u2014 skipping reviewer LLM call",
252
+ diagnosis: "no failures to diagnose",
253
+ nextShotInstruction: "(done)",
254
+ shouldContinue: false,
255
+ confidence: 1
256
+ };
257
+ } else {
258
+ try {
259
+ review = await config.review({
260
+ shot,
261
+ goal: config.goal,
262
+ state,
263
+ verification,
264
+ traceSummary,
265
+ memory: memorySnapshot
266
+ });
267
+ review = coerceReview(review);
268
+ } catch (err) {
269
+ reviewAvailable = false;
270
+ reviewError = err instanceof Error ? err.message : String(err);
271
+ const lastInstruction = memorySnapshot.length > 0 ? memorySnapshot[memorySnapshot.length - 1].nextShotInstruction : fallbackInstruction;
272
+ review = {
273
+ observations: "(reviewer unavailable \u2014 using last-known instruction)",
274
+ diagnosis: reviewError,
275
+ nextShotInstruction: lastInstruction,
276
+ shouldContinue: true,
277
+ confidence: 0.3
278
+ };
279
+ }
280
+ }
281
+ const entry = {
282
+ shot,
283
+ timestamp: Date.now(),
284
+ ...review,
285
+ verification: verificationDigest
286
+ };
287
+ await memory.append(entry);
288
+ const shotRecord = {
289
+ shot,
290
+ state,
291
+ verification,
292
+ traceSummary,
293
+ review,
294
+ reviewAvailable,
295
+ reviewError,
296
+ durationMs: Date.now() - shotStart
297
+ };
298
+ shots.push(shotRecord);
299
+ await shotHandle?.end({
300
+ attributes: {
301
+ verificationPass: verification.pass,
302
+ verificationScore: verification.score ?? null,
303
+ reviewShouldContinue: review.shouldContinue,
304
+ reviewConfidence: review.confidence,
305
+ reviewAvailable
306
+ }
307
+ });
308
+ if (verification.pass) {
309
+ completed = true;
310
+ break;
311
+ }
312
+ if (!review.shouldContinue) {
313
+ break;
314
+ }
315
+ if (confidenceFloorWindow > 0 && review.confidence <= confidenceFloor) {
316
+ lowConfidenceStreak += 1;
317
+ if (lowConfidenceStreak >= confidenceFloorWindow) break;
318
+ } else {
319
+ lowConfidenceStreak = 0;
320
+ }
321
+ priorReview = review;
322
+ }
323
+ if (!completed && !failureClass) {
324
+ failureClass = shots.length >= maxShots ? "budget_exceeded" : "unknown";
325
+ }
326
+ } finally {
327
+ clearTimeout(wallTimer);
328
+ }
329
+ const score = lastVerification.pass ? 1 : typeof lastVerification.score === "number" ? lastVerification.score : 0;
330
+ if (emitter) {
331
+ await emitter.endRun({
332
+ pass: completed,
333
+ score,
334
+ failureClass,
335
+ notes: `${shots.length} shot(s); final pass=${lastVerification.pass}`
336
+ });
337
+ }
338
+ return {
339
+ runId: emitter?.runId ?? null,
340
+ completed,
341
+ shots,
342
+ finalState: state,
343
+ finalVerification: lastVerification,
344
+ failureClass,
345
+ wallMs: Date.now() - wallStart,
346
+ score
347
+ };
348
+ }
349
+ var REVIEWER_SYSTEM_PROMPT = `You are a senior reviewer directing a multi-shot build loop.
350
+ You do NOT grade \u2014 the verifier already did. Your job is to direct the worker's next shot.
351
+ You are blind to the worker's inner monologue. You see what it DID, not what it thought.
352
+ Return STRICT JSON matching the schema. No prose outside the JSON.`;
353
+ function createLlmReviewer(cfg) {
354
+ const renderState = cfg.renderState ?? ((s) => safeJson(s));
355
+ const renderTraceSummary = cfg.renderTraceSummary ?? ((s) => s === void 0 ? "(none)" : safeJson(s));
356
+ const system = cfg.systemPromptAddendum ? `${REVIEWER_SYSTEM_PROMPT}
357
+
358
+ ${cfg.systemPromptAddendum}` : REVIEWER_SYSTEM_PROMPT;
359
+ return async (input) => {
360
+ const memoryBlock = input.memory.length === 0 ? "(no prior shots \u2014 this is shot 1)" : input.memory.map(
361
+ (m) => [
362
+ `shot ${m.shot} \u2014 verification.pass=${m.verification.pass}` + (typeof m.verification.score === "number" ? ` score=${m.verification.score.toFixed(2)}` : "") + ` confidence=${m.confidence.toFixed(2)} failing=[${(m.verification.failingLayers ?? []).join(",")}]`,
363
+ ` observations: ${m.observations.slice(0, 400)}`,
364
+ ` diagnosis: ${m.diagnosis.slice(0, 400)}`,
365
+ ` instruction given: ${m.nextShotInstruction.slice(0, 400)}`
366
+ ].join("\n")
367
+ ).join("\n\n");
368
+ const user = [
369
+ `=== GOAL ===`,
370
+ input.goal,
371
+ ``,
372
+ `=== SHOT NUMBER ===`,
373
+ String(input.shot),
374
+ ``,
375
+ `=== CURRENT STATE ===`,
376
+ renderState(input.state),
377
+ ``,
378
+ `=== TRACE SUMMARY ===`,
379
+ renderTraceSummary(input.traceSummary),
380
+ ``,
381
+ `=== VERIFICATION ===`,
382
+ summarizeVerification(input.verification),
383
+ ``,
384
+ `=== REVIEWER MEMORY (prior shots) ===`,
385
+ memoryBlock,
386
+ ``,
387
+ `=== YOUR TASK ===`,
388
+ `Return STRICT JSON:`,
389
+ `{`,
390
+ ` "observations": string (20..2000 chars, first-person worker behavior \u2014 quote counts, errors, loops)`,
391
+ ` "diagnosis": string (20..1500 chars, root cause, NOT a restatement of verification)`,
392
+ ` "nextShotInstruction": string (40..3000 chars, concrete directive to the worker)`,
393
+ ` "shouldContinue": boolean (false if verification.pass, or if thrashing, or unachievable)`,
394
+ ` "confidence": number in [0,1]`,
395
+ `}`
396
+ ].join("\n");
397
+ const raw = await cfg.callJson({ system, user });
398
+ return coerceReview(raw);
399
+ };
400
+ }
401
+ function coerceReview(raw) {
402
+ if (!raw || typeof raw !== "object") {
403
+ throw new Error("reviewer returned non-object");
404
+ }
405
+ const observations = typeof raw.observations === "string" ? raw.observations : "";
406
+ const diagnosis = typeof raw.diagnosis === "string" ? raw.diagnosis : "";
407
+ const nextShotInstruction = typeof raw.nextShotInstruction === "string" ? raw.nextShotInstruction : "";
408
+ if (!observations || !diagnosis || !nextShotInstruction) {
409
+ throw new Error("reviewer missing required string fields");
410
+ }
411
+ if (typeof raw.shouldContinue !== "boolean") {
412
+ throw new Error("reviewer missing shouldContinue boolean");
413
+ }
414
+ const confidenceRaw = Number(raw.confidence);
415
+ if (!Number.isFinite(confidenceRaw)) {
416
+ throw new Error("reviewer confidence not finite");
417
+ }
418
+ return {
419
+ observations,
420
+ diagnosis,
421
+ nextShotInstruction,
422
+ shouldContinue: raw.shouldContinue,
423
+ confidence: Math.max(0, Math.min(1, confidenceRaw))
424
+ };
425
+ }
426
+ function summarizeVerification(v) {
427
+ const header = `pass=${v.pass}` + (typeof v.score === "number" ? ` score=${v.score.toFixed(3)}` : "") + (v.failingLayers && v.failingLayers.length > 0 ? ` failing=[${v.failingLayers.join(", ")}]` : "");
428
+ const details = v.details === void 0 ? "" : `
429
+ ${safeJson(v.details).slice(0, 1500)}`;
430
+ return header + details;
431
+ }
432
+ function safeJson(x) {
433
+ try {
434
+ return JSON.stringify(x, null, 2);
435
+ } catch {
436
+ return String(x);
437
+ }
438
+ }
439
+
440
+ // src/propose-review-control.ts
441
+ var DEFAULT_FALLBACK_INSTRUCTION2 = "Inspect the verification failures above. Fix the critical issues first, then the major ones. Do not restate the failures \u2014 act on them.";
442
+ async function runProposeReviewAsControlLoop(config) {
443
+ const maxShots = config.maxShots ?? 10;
444
+ const confidenceFloor = config.confidenceFloor ?? 0.3;
445
+ const confidenceFloorWindow = config.confidenceFloorWindow ?? 2;
446
+ const memory = config.memory ?? inMemoryReviewStore();
447
+ const fallbackInstruction = config.fallbackInstruction ?? DEFAULT_FALLBACK_INSTRUCTION2;
448
+ const failureClassFromVerification = config.failureClassFromVerification ?? controlFailureClassFromVerification;
449
+ let lowConfidenceStreak = 0;
450
+ let current = {
451
+ shot: 0,
452
+ state: config.initialState,
453
+ priorReview: null,
454
+ verification: { pass: false },
455
+ memory: await memory.load(),
456
+ completed: false,
457
+ reviewAvailable: false
458
+ };
459
+ return runAgentControlLoop({
460
+ intent: config.goal,
461
+ budget: { maxSteps: maxShots, maxWallMs: config.maxWallMs },
462
+ store: config.store,
463
+ scenarioId: config.scenarioId ?? "propose-review-control",
464
+ projectId: config.projectId,
465
+ variantId: config.variantId,
466
+ actionFailure: config.actionFailure ?? "stop",
467
+ observe: () => current,
468
+ validate: ({ state }) => [
469
+ objectiveEval({
470
+ id: "verification",
471
+ passed: state.verification.pass,
472
+ score: state.verification.score,
473
+ severity: "critical",
474
+ detail: state.verification.pass ? "verification passed" : `verification failed${state.verification.failingLayers?.length ? `: ${state.verification.failingLayers.join(", ")}` : ""}`
475
+ })
476
+ ],
477
+ shouldStop: ({ state }) => {
478
+ if (state.verification.pass) {
479
+ return {
480
+ stop: true,
481
+ pass: true,
482
+ reason: "verification passed",
483
+ score: state.verification.score
484
+ };
485
+ }
486
+ if (state.completed) {
487
+ return {
488
+ stop: true,
489
+ pass: false,
490
+ reason: "reviewer stopped continuation",
491
+ score: state.verification.score,
492
+ failureClass: failureClassFromVerification(state.verification)
493
+ };
494
+ }
495
+ return {
496
+ stop: false,
497
+ pass: false,
498
+ reason: "verification still failing",
499
+ score: state.verification.score
500
+ };
501
+ },
502
+ decide: ({ state }) => ({
503
+ type: "continue",
504
+ action: { type: "propose-review-shot", shot: state.shot + 1 },
505
+ reason: state.priorReview?.nextShotInstruction ?? fallbackInstruction
506
+ }),
507
+ act: async (action, ctx) => {
508
+ const shot = action.shot;
509
+ const proposeOut = await config.propose({
510
+ shot,
511
+ goal: config.goal,
512
+ state: current.state,
513
+ priorReview: current.priorReview,
514
+ abortSignal: ctx.abortSignal,
515
+ emitter: ctx.emitter
516
+ });
517
+ const nextState = proposeOut.state;
518
+ const verification = await config.verify(nextState);
519
+ let review = null;
520
+ let reviewAvailable = false;
521
+ let reviewError;
522
+ let shouldContinue = !verification.pass;
523
+ if (!verification.pass) {
524
+ try {
525
+ review = await config.review({
526
+ shot,
527
+ goal: config.goal,
528
+ state: nextState,
529
+ verification,
530
+ traceSummary: proposeOut.traceSummary,
531
+ memory: await memory.load()
532
+ });
533
+ reviewAvailable = true;
534
+ shouldContinue = review.shouldContinue;
535
+ lowConfidenceStreak = review.confidence <= confidenceFloor ? lowConfidenceStreak + 1 : 0;
536
+ if (confidenceFloorWindow > 0 && lowConfidenceStreak >= confidenceFloorWindow)
537
+ shouldContinue = false;
538
+ } catch (err) {
539
+ reviewError = err instanceof Error ? err.message : String(err);
540
+ review = current.priorReview ?? {
541
+ observations: "Reviewer unavailable.",
542
+ diagnosis: reviewError,
543
+ nextShotInstruction: fallbackInstruction,
544
+ shouldContinue: true,
545
+ confidence: 0
546
+ };
547
+ shouldContinue = true;
548
+ }
549
+ } else {
550
+ review = {
551
+ observations: "Verification passed.",
552
+ diagnosis: "No further revision needed.",
553
+ nextShotInstruction: "",
554
+ shouldContinue: false,
555
+ confidence: 1
556
+ };
557
+ }
558
+ const entry = {
559
+ ...review ?? {
560
+ observations: "No review.",
561
+ diagnosis: "",
562
+ nextShotInstruction: fallbackInstruction,
563
+ shouldContinue,
564
+ confidence: 0
565
+ },
566
+ shot,
567
+ timestamp: Date.now(),
568
+ verification: {
569
+ pass: verification.pass,
570
+ score: verification.score,
571
+ failingLayers: verification.failingLayers
572
+ }
573
+ };
574
+ await memory.append(entry);
575
+ current = {
576
+ shot,
577
+ state: nextState,
578
+ priorReview: review,
579
+ verification,
580
+ traceSummary: proposeOut.traceSummary,
581
+ memory: await memory.load(),
582
+ completed: verification.pass || !shouldContinue,
583
+ reviewAvailable,
584
+ reviewError
585
+ };
586
+ return {
587
+ state: nextState,
588
+ verification,
589
+ traceSummary: proposeOut.traceSummary,
590
+ review,
591
+ reviewAvailable,
592
+ reviewError
593
+ };
594
+ }
595
+ });
596
+ }
597
+ function controlFailureClassFromVerification(verification) {
598
+ if (verification.pass) return void 0;
599
+ return verification.failingLayers?.length ? "instruction_following" : "unknown";
600
+ }
601
+
602
+ export {
603
+ evaluateActionPolicy,
604
+ controlRunToRunRecord,
605
+ scoreFromEvals,
606
+ inMemoryReviewStore,
607
+ jsonlReviewStore,
608
+ runProposeReview,
609
+ createLlmReviewer,
610
+ runProposeReviewAsControlLoop,
611
+ controlFailureClassFromVerification
612
+ };
613
+ //# sourceMappingURL=chunk-ZN274SWR.js.map