@tangle-network/agent-bench 0.3.7 → 0.4.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 (211) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/HARNESS.md +43 -0
  3. package/README.md +7 -0
  4. package/dist/adapters.js +23 -23
  5. package/dist/benchmarks/_harness.d.ts +1 -1
  6. package/dist/benchmarks/_harness.js +1 -1
  7. package/dist/benchmarks/aec-bench.js +2 -2
  8. package/dist/benchmarks/agentbench.js +2 -2
  9. package/dist/benchmarks/appworld.js +2 -2
  10. package/dist/benchmarks/bfcl.js +2 -2
  11. package/dist/benchmarks/commit0.js +2 -2
  12. package/dist/benchmarks/crag.js +2 -2
  13. package/dist/benchmarks/dabstep.js +2 -2
  14. package/dist/benchmarks/enterpriseops-gym.js +2 -2
  15. package/dist/benchmarks/finresearchbench.js +2 -2
  16. package/dist/benchmarks/nomiracl.js +2 -2
  17. package/dist/benchmarks/open-rag-bench.js +2 -2
  18. package/dist/benchmarks/programbench.js +2 -2
  19. package/dist/benchmarks/ragbench.js +2 -2
  20. package/dist/benchmarks/swe-bench.js +2 -2
  21. package/dist/benchmarks/t2-ragbench.js +2 -2
  22. package/dist/benchmarks/tau-bench-shared.js +2 -2
  23. package/dist/benchmarks/tau2-bench.js +3 -3
  24. package/dist/benchmarks/tau3-banking.js +3 -3
  25. package/dist/benchmarks/terminal-bench.js +2 -2
  26. package/dist/benchmarks/toollm.js +2 -2
  27. package/dist/benchmarks/webarena-verified.js +2 -2
  28. package/dist/{chunk-CKUVRZ2T.js → chunk-3U5TXJZS.js} +2 -2
  29. package/dist/{chunk-YCGY7UIZ.js → chunk-7GRVHU22.js} +2 -2
  30. package/dist/{chunk-Z7ML6L77.js → chunk-HWST3SED.js} +2 -2
  31. package/dist/{chunk-SYDW647C.js → chunk-IA2FBTWC.js} +2 -2
  32. package/dist/{chunk-R67DFVLO.js → chunk-IFVINJ4B.js} +2 -2
  33. package/dist/{chunk-R36V2VP7.js → chunk-IZ5M6OAC.js} +2 -2
  34. package/dist/{chunk-ODT47UAY.js → chunk-K3BQGZCT.js} +2 -2
  35. package/dist/{chunk-IFAV6KEM.js → chunk-KP5KD6EN.js} +2 -2
  36. package/dist/{chunk-ZEWMTR5M.js → chunk-MQMRLGOG.js} +2 -2
  37. package/dist/{chunk-TSWPNOYM.js → chunk-NQG5XDSB.js} +2 -2
  38. package/dist/{chunk-7WSD27QQ.js → chunk-PB64GYIG.js} +2 -2
  39. package/dist/{chunk-HBSWHQNJ.js → chunk-RCYQEFNX.js} +3 -3
  40. package/dist/{chunk-J3KDJNX2.js → chunk-RH5F53JT.js} +2 -2
  41. package/dist/{chunk-UAIOHCUK.js → chunk-SFLA7OH3.js} +3 -3
  42. package/dist/{chunk-Y6O2OCUO.js → chunk-SHM6MRRF.js} +2 -2
  43. package/dist/{chunk-KDIKRJGB.js → chunk-SHYIRB7I.js} +2 -2
  44. package/dist/{chunk-HHXFIHXC.js → chunk-SVR2LKYI.js} +2 -2
  45. package/dist/{chunk-PWQVGAJB.js → chunk-V7AEBY6U.js} +21 -21
  46. package/dist/{chunk-LRRD7NAG.js → chunk-WSKWVEQB.js} +18 -2
  47. package/dist/chunk-WSKWVEQB.js.map +1 -0
  48. package/dist/{chunk-2PVVP7GN.js → chunk-XKEFIFIC.js} +2 -2
  49. package/dist/{chunk-JRWWGMK7.js → chunk-XYA4XSNU.js} +2 -2
  50. package/dist/{chunk-X5YKXC6V.js → chunk-YSMEKBTD.js} +2 -2
  51. package/dist/{chunk-2XU6OGEN.js → chunk-Z4TZ76N7.js} +2 -2
  52. package/dist/index.js +23 -23
  53. package/package.json +5 -5
  54. package/scripts/trata-hedge/README.md +6 -5
  55. package/scripts/verify-packed-consumer.mjs +1 -1
  56. package/src/benchmarks/_harness.ts +20 -2
  57. package/src/benchmarks/humaneval.test.mts +2 -2
  58. package/src/gate.ts +1 -1
  59. package/src/hev-eval.mts +5 -2
  60. package/src/hev-improve.mts +118 -73
  61. package/src/official-optimizer-config.mts +89 -0
  62. package/src/official-optimizer-config.test.mts +88 -0
  63. package/src/profiles.ts +2 -2
  64. package/src/quant-arena/README.md +144 -0
  65. package/src/quant-arena/backtest.test.mts +135 -0
  66. package/src/quant-arena/backtest.ts +218 -0
  67. package/src/quant-arena/data.test.mts +44 -0
  68. package/src/quant-arena/data.ts +141 -0
  69. package/src/quant-arena/driver.test.mts +253 -0
  70. package/src/quant-arena/driver.ts +219 -0
  71. package/src/quant-arena/fixtures/data/PROVENANCE.md +26 -0
  72. package/src/quant-arena/fixtures/data/holdout/IDX.csv +523 -0
  73. package/src/quant-arena/fixtures/data/holdout/S01.csv +523 -0
  74. package/src/quant-arena/fixtures/data/holdout/S02.csv +523 -0
  75. package/src/quant-arena/fixtures/data/holdout/S03.csv +523 -0
  76. package/src/quant-arena/fixtures/data/holdout/S04.csv +523 -0
  77. package/src/quant-arena/fixtures/data/holdout/S05.csv +523 -0
  78. package/src/quant-arena/fixtures/data/holdout/S06.csv +523 -0
  79. package/src/quant-arena/fixtures/data/holdout/S07.csv +523 -0
  80. package/src/quant-arena/fixtures/data/holdout/S08.csv +523 -0
  81. package/src/quant-arena/fixtures/data/holdout/S09.csv +523 -0
  82. package/src/quant-arena/fixtures/data/holdout/S10.csv +523 -0
  83. package/src/quant-arena/fixtures/data/insample/IDX.csv +2087 -0
  84. package/src/quant-arena/fixtures/data/insample/S01.csv +2087 -0
  85. package/src/quant-arena/fixtures/data/insample/S02.csv +2087 -0
  86. package/src/quant-arena/fixtures/data/insample/S03.csv +2087 -0
  87. package/src/quant-arena/fixtures/data/insample/S04.csv +2087 -0
  88. package/src/quant-arena/fixtures/data/insample/S05.csv +2087 -0
  89. package/src/quant-arena/fixtures/data/insample/S06.csv +2087 -0
  90. package/src/quant-arena/fixtures/data/insample/S07.csv +2087 -0
  91. package/src/quant-arena/fixtures/data/insample/S08.csv +2087 -0
  92. package/src/quant-arena/fixtures/data/insample/S09.csv +2087 -0
  93. package/src/quant-arena/fixtures/data/insample/S10.csv +2087 -0
  94. package/src/quant-arena/fixtures/demo-campaign/cost-ledger.jsonl +16 -0
  95. package/src/quant-arena/fixtures/demo-campaign/notebook.jsonl +5 -0
  96. package/src/quant-arena/fixtures/demo-campaign/rollout-manifest.json +171 -0
  97. package/src/quant-arena/fixtures/demo-campaign/strategies/cand-001-default-author/strategy.ts +119 -0
  98. package/src/quant-arena/fixtures/demo-campaign/strategies/cand-002-default-author/strategy.ts +119 -0
  99. package/src/quant-arena/fixtures/demo-campaign/strategies/cand-003-quant-researcher/strategy.ts +105 -0
  100. package/src/quant-arena/fixtures/demo-campaign/strategies/cand-004-quant-researcher/strategy.ts +102 -0
  101. package/src/quant-arena/fixtures/demo-campaign-v2/cost-ledger.jsonl +4 -0
  102. package/src/quant-arena/fixtures/demo-campaign-v2/notebook.jsonl +2 -0
  103. package/src/quant-arena/fixtures/demo-campaign-v2/rollout-manifest.json +84 -0
  104. package/src/quant-arena/fixtures/demo-campaign-v2/strategies/cand-001-quant-researcher/strategy.ts +117 -0
  105. package/src/quant-arena/holdout-certify.mts +206 -0
  106. package/src/quant-arena/holdout-certify.test.mts +82 -0
  107. package/src/quant-arena/leak-audit.test.mts +79 -0
  108. package/src/quant-arena/leak-audit.ts +95 -0
  109. package/src/quant-arena/make-fixtures.mts +161 -0
  110. package/src/quant-arena/multiplicity.test.mts +68 -0
  111. package/src/quant-arena/multiplicity.ts +87 -0
  112. package/src/quant-arena/nautilus-certify.ts +31 -0
  113. package/src/quant-arena/oms.ts +90 -0
  114. package/src/quant-arena/profiles/quant-researcher.profile.json +7 -0
  115. package/src/quant-arena/python/pyproject.toml +8 -0
  116. package/src/quant-arena/python/uv.lock +1297 -0
  117. package/src/quant-arena/python/vbt-worker.py +192 -0
  118. package/src/quant-arena/quant-loop.mts +813 -0
  119. package/src/quant-arena/quant-loop.test.mts +75 -0
  120. package/src/quant-arena/strategies/buy-hold-index/strategy.ts +11 -0
  121. package/src/quant-arena/strategies/equal-weight/strategy.ts +20 -0
  122. package/src/quant-arena/strategies/sma-crossover/strategy.ts +42 -0
  123. package/src/quant-arena/types.ts +133 -0
  124. package/src/quant-arena/vbt-client.ts +321 -0
  125. package/src/quant-arena/vbt-parity.test.mts +183 -0
  126. package/src/quant-arena/windows.test.mts +45 -0
  127. package/src/quant-arena/windows.ts +54 -0
  128. package/src/rollout-ledger/backfill-swe-arena.mts +606 -0
  129. package/src/rollout-ledger/backfill-swe-arena.test.mts +342 -0
  130. package/src/rollout-ledger/settle-capture.mts +442 -0
  131. package/src/rollout-ledger/settle-capture.test.mts +270 -0
  132. package/src/smoke-structural-rollout.mts +15 -9
  133. package/src/swe-arena/activation.mts +225 -0
  134. package/src/swe-arena/activation.test.mts +300 -0
  135. package/src/swe-arena/arms.ts +30 -14
  136. package/src/swe-arena/briefing.mts +217 -0
  137. package/src/swe-arena/briefing.test.mts +178 -0
  138. package/src/swe-arena/calibrate.ts +102 -1
  139. package/src/swe-arena/cell-evidence.mts +41 -9
  140. package/src/swe-arena/factory-command-container.ts +284 -0
  141. package/src/swe-arena/factory-judge-child.mts +228 -0
  142. package/src/swe-arena/factory.test.mts +643 -0
  143. package/src/swe-arena/fixtures/factory/agent-eval-309/calibration.md +51 -0
  144. package/src/swe-arena/fixtures/factory/agent-eval-309/manifest.json +29 -0
  145. package/src/swe-arena/fixtures/factory/agent-eval-309/spec.md +64 -0
  146. package/src/swe-arena/fixtures/factory/agent-runtime-232/calibration.md +48 -0
  147. package/src/swe-arena/fixtures/factory/agent-runtime-232/manifest.json +29 -0
  148. package/src/swe-arena/fixtures/factory/agent-runtime-232/spec.md +48 -0
  149. package/src/swe-arena/fixtures/factory/loops-28/calibration.md +47 -0
  150. package/src/swe-arena/fixtures/factory/loops-28/manifest.json +30 -0
  151. package/src/swe-arena/fixtures/factory/loops-28/spec.md +50 -0
  152. package/src/swe-arena/fixtures/run-report/README.md +43 -0
  153. package/src/swe-arena/fixtures/run-report/factory-agent-eval-309-FSUP0.json +173 -0
  154. package/src/swe-arena/fixtures/run-report/factory-agent-eval-309-FSUP0.md +100 -0
  155. package/src/swe-arena/fixtures/run-report/gen3-rollup.json +551 -0
  156. package/src/swe-arena/fixtures/run-report/gen3-rollup.md +64 -0
  157. package/src/swe-arena/fixtures.ts +135 -2
  158. package/src/swe-arena/gepa-seat.mts +877 -0
  159. package/src/swe-arena/gepa-seat.test.mts +1059 -0
  160. package/src/swe-arena/implementation-ref.test.mts +64 -0
  161. package/src/swe-arena/implementation-ref.ts +62 -0
  162. package/src/swe-arena/ledger-orphans.mts +77 -0
  163. package/src/swe-arena/ledger-orphans.test.mts +147 -0
  164. package/src/swe-arena/outer-loop.mts +710 -33
  165. package/src/swe-arena/premeasured-from-cells.mts +281 -0
  166. package/src/swe-arena/premeasured-from-cells.test.mts +180 -0
  167. package/src/swe-arena/proposer-fanout.mts +327 -31
  168. package/src/swe-arena/proposer-fanout.test.mts +246 -0
  169. package/src/swe-arena/proposer-provenance.mts +172 -0
  170. package/src/swe-arena/proposer-provenance.test.mts +106 -0
  171. package/src/swe-arena/run-experiment.mts +367 -1
  172. package/src/swe-arena/run-report.mts +75 -0
  173. package/src/swe-arena/run-supervisor.test.mts +5 -3
  174. package/src/swe-arena/score-split.mts +140 -0
  175. package/src/swe-arena/score-split.test.mts +123 -0
  176. package/src/swe-arena/scratch-worktree.test.mts +55 -0
  177. package/src/swe-arena/scratch-worktree.ts +34 -0
  178. package/src/swe-arena/types.ts +52 -0
  179. package/src/swe-code-improve.mts +24 -25
  180. package/src/swe-improve.mts +129 -96
  181. package/src/swe-local-proof.mts +6 -1
  182. package/src/swe-stream.mts +4 -2
  183. package/src/swe-structural.mts +245 -837
  184. package/src/tb-container-executor.test.mts +30 -6
  185. package/src/tb-supervisor-sidecar.mts +2 -1
  186. package/src/trata-gepa.mts +182 -245
  187. package/dist/chunk-LRRD7NAG.js.map +0 -1
  188. package/src/live-improve-campaign-mbpp.mts +0 -641
  189. package/src/live-improve-campaign.mts +0 -500
  190. /package/dist/{chunk-CKUVRZ2T.js.map → chunk-3U5TXJZS.js.map} +0 -0
  191. /package/dist/{chunk-YCGY7UIZ.js.map → chunk-7GRVHU22.js.map} +0 -0
  192. /package/dist/{chunk-Z7ML6L77.js.map → chunk-HWST3SED.js.map} +0 -0
  193. /package/dist/{chunk-SYDW647C.js.map → chunk-IA2FBTWC.js.map} +0 -0
  194. /package/dist/{chunk-R67DFVLO.js.map → chunk-IFVINJ4B.js.map} +0 -0
  195. /package/dist/{chunk-R36V2VP7.js.map → chunk-IZ5M6OAC.js.map} +0 -0
  196. /package/dist/{chunk-ODT47UAY.js.map → chunk-K3BQGZCT.js.map} +0 -0
  197. /package/dist/{chunk-IFAV6KEM.js.map → chunk-KP5KD6EN.js.map} +0 -0
  198. /package/dist/{chunk-ZEWMTR5M.js.map → chunk-MQMRLGOG.js.map} +0 -0
  199. /package/dist/{chunk-TSWPNOYM.js.map → chunk-NQG5XDSB.js.map} +0 -0
  200. /package/dist/{chunk-7WSD27QQ.js.map → chunk-PB64GYIG.js.map} +0 -0
  201. /package/dist/{chunk-HBSWHQNJ.js.map → chunk-RCYQEFNX.js.map} +0 -0
  202. /package/dist/{chunk-J3KDJNX2.js.map → chunk-RH5F53JT.js.map} +0 -0
  203. /package/dist/{chunk-UAIOHCUK.js.map → chunk-SFLA7OH3.js.map} +0 -0
  204. /package/dist/{chunk-Y6O2OCUO.js.map → chunk-SHM6MRRF.js.map} +0 -0
  205. /package/dist/{chunk-KDIKRJGB.js.map → chunk-SHYIRB7I.js.map} +0 -0
  206. /package/dist/{chunk-HHXFIHXC.js.map → chunk-SVR2LKYI.js.map} +0 -0
  207. /package/dist/{chunk-PWQVGAJB.js.map → chunk-V7AEBY6U.js.map} +0 -0
  208. /package/dist/{chunk-2PVVP7GN.js.map → chunk-XKEFIFIC.js.map} +0 -0
  209. /package/dist/{chunk-JRWWGMK7.js.map → chunk-XYA4XSNU.js.map} +0 -0
  210. /package/dist/{chunk-X5YKXC6V.js.map → chunk-YSMEKBTD.js.map} +0 -0
  211. /package/dist/{chunk-2XU6OGEN.js.map → chunk-Z4TZ76N7.js.map} +0 -0
@@ -0,0 +1,228 @@
1
+ /**
2
+ * Factory judge child — the factory-bench equivalent of `judge-child.mts`.
3
+ * One candidate patch, one hidden-test verdict, printed as a single
4
+ * `JUDGE_RESULT {...}` line so serialized-judge.ts (queue, retry, SIGKILL
5
+ * ceiling) carries over unchanged. The judge workspace is a `git archive`
6
+ * export of the instance's base commit from the local mirror. Setup and judge
7
+ * commands run in separate containers, and the PR's added test files are
8
+ * overlaid from the merge commit only after setup completes.
9
+ *
10
+ * Steps: export base tree → apply candidate patch → run `setup_cmds` in a
11
+ * fresh container → overlay judge tests from `judge_ref` → run `judge_cmds`
12
+ * in a second network-disabled container →
13
+ * parse vitest's pass/fail counts → JUDGE_RESULT with partial credit.
14
+ *
15
+ * Verdict semantics:
16
+ * - `resolved` = every calibrated judge test passes (passed === total, 0 failed).
17
+ * - `score` = passed / judgeTestTotal, where the denominator is the CALIBRATED
18
+ * total from the manifest's resolved_criterion — never vitest's reported
19
+ * total, which shrinks when a judge file fails to collect.
20
+ * - An unappliable patch is the candidate's failure → resolved:false, score 0.
21
+ * - A setup/parse failure is an INFRA failure → exit 1 with no JUDGE_RESULT
22
+ * line, so serialized-judge retries and then records `resolved: null`
23
+ * (inconclusive), never a fabricated verdict.
24
+ *
25
+ * usage: node --import tsx src/swe-arena/factory-judge-child.mts <instanceDir> <patchPath>
26
+ */
27
+
28
+ import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
29
+ import { tmpdir } from 'node:os'
30
+ import { dirname, join } from 'node:path'
31
+ import { pathToFileURL } from 'node:url'
32
+ import { applyPatchWithFallback } from './calibrate.ts'
33
+ import { runFactoryCommand } from './factory-command-container.ts'
34
+ import { loadFactoryInstance, type LoadedFactoryInstance } from './fixtures.ts'
35
+ import { runOk, shq } from './proc.ts'
36
+
37
+ export interface VitestSummary {
38
+ passed: number
39
+ failed: number
40
+ /** vitest's own reported total (collected tests only — informational). */
41
+ reportedTotal: number
42
+ }
43
+
44
+ const stripAnsi = (s: string): string => s.replace(/\u001b\[[0-9;]*m/g, '')
45
+
46
+ /**
47
+ * Parse the `Tests …` summary line of a vitest run (v3 and v4 formats):
48
+ * " Tests 30 passed (30)"
49
+ * " Tests 17 failed | 13 passed (30)"
50
+ * " Tests no tests" (collection failure — 0 passed is a verdict)
51
+ * Returns undefined when no summary line exists (infra failure, not a verdict).
52
+ */
53
+ export function parseVitestSummary(output: string): VitestSummary | undefined {
54
+ for (const rawLine of stripAnsi(output).split('\n')) {
55
+ const m = /^\s*Tests\s{2,}(.+)$/.exec(rawLine)
56
+ if (!m) continue
57
+ const body = m[1].trim()
58
+ if (body.startsWith('no tests')) return { passed: 0, failed: 0, reportedTotal: 0 }
59
+ const passed = /(\d+) passed/.exec(body)
60
+ const failed = /(\d+) failed/.exec(body)
61
+ const total = /\((\d+)\)/.exec(body)
62
+ if (!passed && !failed) continue
63
+ return {
64
+ passed: passed ? Number(passed[1]) : 0,
65
+ failed: failed ? Number(failed[1]) : 0,
66
+ reportedTotal: total ? Number(total[1]) : 0,
67
+ }
68
+ }
69
+ return undefined
70
+ }
71
+
72
+ /** The JUDGE_RESULT row this child prints (superset of the SWE child's fields). */
73
+ export interface FactoryJudgeResult {
74
+ iid: string
75
+ resolved: boolean
76
+ /** passed / judgeTestTotal — partial credit for the staircase. */
77
+ score: number
78
+ passed: number
79
+ total: number
80
+ secs: number
81
+ patch_bytes: number
82
+ error?: string
83
+ }
84
+
85
+ /** Parse one JUDGE_RESULT line back out of child stdout (calibrate + tests). */
86
+ export function parseFactoryJudgeResult(stdout: string): FactoryJudgeResult | undefined {
87
+ const line = stdout.split('\n').find((l) => l.includes('JUDGE_RESULT'))
88
+ if (!line) return undefined
89
+ try {
90
+ const parsed = JSON.parse(line.slice(line.indexOf('JUDGE_RESULT') + 'JUDGE_RESULT'.length).trim())
91
+ if (typeof parsed !== 'object' || parsed === null || typeof parsed.resolved !== 'boolean') return undefined
92
+ return parsed as FactoryJudgeResult
93
+ } catch {
94
+ return undefined
95
+ }
96
+ }
97
+
98
+ /** `git archive <ref> | tar -x` — a bare tree, no .git, no refs, no future history. */
99
+ export async function exportBaseTree(mirror: string, ref: string, dest: string): Promise<void> {
100
+ await mkdir(dest, { recursive: true })
101
+ await runOk('bash', ['-c', `git -C ${shq(mirror)} archive ${shq(ref)} | tar -x -C ${shq(dest)}`])
102
+ }
103
+
104
+ /** Overlay each judge test from the merge commit (`git show <judge_ref>:<path>`). */
105
+ export async function overlayJudgeTests(inst: LoadedFactoryInstance, ws: string): Promise<void> {
106
+ for (const testPath of inst.judge_tests) {
107
+ const show = await runOk('git', ['-C', inst.repo_local_mirror, 'show', `${inst.judge_ref}:${testPath}`])
108
+ await mkdir(dirname(join(ws, testPath)), { recursive: true })
109
+ await writeFile(join(ws, testPath), show.stdout)
110
+ }
111
+ }
112
+
113
+ export interface FactoryJudgeOutcome {
114
+ result: FactoryJudgeResult
115
+ /** Raw judge_cmds output, for post-mortem. */
116
+ output: string
117
+ }
118
+
119
+ /**
120
+ * Build the judge workspace and run the judge over one patch. Throws on infra
121
+ * failure (export/overlay/setup/parse); returns a verdict otherwise.
122
+ */
123
+ export async function judgeFactoryPatch(
124
+ inst: LoadedFactoryInstance,
125
+ patch: string,
126
+ opts: { workDir?: string; keepWorkspace?: boolean } = {},
127
+ ): Promise<FactoryJudgeOutcome> {
128
+ const t0 = Date.now()
129
+ const ws = opts.workDir ?? (await mkdtemp(join(tmpdir(), `factory-judge-${inst.id.replace(/[^a-zA-Z0-9.-]/g, '-')}-`)))
130
+ const cmdTimeoutMs = inst.timeout_s * 1000
131
+ try {
132
+ await exportBaseTree(inst.repo_local_mirror, inst.base_commit, ws)
133
+ // A git repo (fresh, historyless) so `git apply` and repo tooling behave;
134
+ // judge-side only, so its refs are irrelevant to leak prevention.
135
+ await runOk('git', ['-C', ws, 'init', '-q'])
136
+
137
+ if (patch.trim().length > 0) {
138
+ const patchFile = join(ws, '.factory-candidate.patch')
139
+ await writeFile(patchFile, patch)
140
+ const applyRc = await applyPatchWithFallback(ws, patchFile)
141
+ await rm(patchFile, { force: true })
142
+ if (applyRc !== 0) {
143
+ return {
144
+ result: {
145
+ iid: inst.id,
146
+ resolved: false,
147
+ score: 0,
148
+ passed: 0,
149
+ total: inst.judgeTestTotal,
150
+ secs: Math.round((Date.now() - t0) / 1000),
151
+ patch_bytes: patch.length,
152
+ error: `apply-failed rc=${applyRc}`,
153
+ },
154
+ output: '',
155
+ }
156
+ }
157
+ }
158
+
159
+ for (const cmd of inst.setup_cmds) {
160
+ const res = await runFactoryCommand(ws, cmd, {
161
+ image: inst.command_image,
162
+ network: 'enabled',
163
+ timeoutMs: cmdTimeoutMs,
164
+ })
165
+ if (res.code !== 0) {
166
+ throw new Error(
167
+ `setup_cmd failed (rc=${res.code}${res.timedOut ? ', timeout' : ''}): ${cmd}\n${(res.stderr || res.stdout).slice(-2000)}`,
168
+ )
169
+ }
170
+ }
171
+
172
+ await overlayJudgeTests(inst, ws)
173
+
174
+ let passed = 0
175
+ let failed = 0
176
+ let output = ''
177
+ for (const cmd of inst.judge_cmds) {
178
+ const res = await runFactoryCommand(ws, cmd, {
179
+ image: inst.command_image,
180
+ network: 'none',
181
+ timeoutMs: cmdTimeoutMs,
182
+ })
183
+ const combined = res.stdout + res.stderr
184
+ output += combined
185
+ if (res.timedOut) throw new Error(`judge_cmd timed out after ${cmdTimeoutMs}ms: ${cmd}`)
186
+ const summary = parseVitestSummary(combined)
187
+ if (!summary) {
188
+ throw new Error(`judge_cmd produced no parseable test summary (rc=${res.code}): ${cmd}\n${combined.slice(-2000)}`)
189
+ }
190
+ passed += summary.passed
191
+ failed += summary.failed
192
+ }
193
+
194
+ const total = inst.judgeTestTotal
195
+ return {
196
+ result: {
197
+ iid: inst.id,
198
+ resolved: failed === 0 && passed === total,
199
+ score: total > 0 ? Number((passed / total).toFixed(4)) : 0,
200
+ passed,
201
+ total,
202
+ secs: Math.round((Date.now() - t0) / 1000),
203
+ patch_bytes: patch.length,
204
+ },
205
+ output,
206
+ }
207
+ } finally {
208
+ if (!opts.keepWorkspace) await rm(ws, { recursive: true, force: true })
209
+ }
210
+ }
211
+
212
+ // ---------------------------------------------------------------------------
213
+ // CLI (guarded so calibrate/tests can import the helpers above).
214
+ // ---------------------------------------------------------------------------
215
+
216
+ const isMain = process.argv[1] !== undefined && import.meta.url === pathToFileURL(process.argv[1]).href
217
+
218
+ if (isMain) {
219
+ const [, , instanceDir, patchPath] = process.argv
220
+ if (!instanceDir || !patchPath) {
221
+ console.error('usage: node --import tsx factory-judge-child.mts <instanceDir> <patchPath>')
222
+ process.exit(2)
223
+ }
224
+ const inst = loadFactoryInstance(instanceDir)
225
+ const patch = await readFile(patchPath, 'utf8')
226
+ const { result } = await judgeFactoryPatch(inst, patch)
227
+ console.log('JUDGE_RESULT ' + JSON.stringify(result))
228
+ }