@tangle-network/agent-bench 0.3.6 → 0.3.8

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 (265) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/HARNESS.md +43 -0
  3. package/dist/adapters.js +24 -24
  4. package/dist/benchmarks/_harness.d.ts +1 -1
  5. package/dist/benchmarks/_harness.js +1 -1
  6. package/dist/benchmarks/aec-bench.js +2 -2
  7. package/dist/benchmarks/agentbench.js +2 -2
  8. package/dist/benchmarks/appworld.js +2 -2
  9. package/dist/benchmarks/bfcl.js +2 -2
  10. package/dist/benchmarks/commit0.js +2 -2
  11. package/dist/benchmarks/crag.js +2 -2
  12. package/dist/benchmarks/dabstep.js +2 -2
  13. package/dist/benchmarks/enterpriseops-gym.js +2 -2
  14. package/dist/benchmarks/finresearchbench.js +2 -2
  15. package/dist/benchmarks/humaneval.d.ts +10 -1
  16. package/dist/benchmarks/humaneval.js +5 -3
  17. package/dist/benchmarks/nomiracl.js +2 -2
  18. package/dist/benchmarks/open-rag-bench.js +2 -2
  19. package/dist/benchmarks/programbench.js +2 -2
  20. package/dist/benchmarks/ragbench.js +2 -2
  21. package/dist/benchmarks/swe-bench.js +2 -2
  22. package/dist/benchmarks/t2-ragbench.js +2 -2
  23. package/dist/benchmarks/tau-bench-shared.js +2 -2
  24. package/dist/benchmarks/tau2-bench.js +3 -3
  25. package/dist/benchmarks/tau3-banking.js +3 -3
  26. package/dist/benchmarks/terminal-bench.js +2 -2
  27. package/dist/benchmarks/toollm.js +2 -2
  28. package/dist/benchmarks/webarena-verified.js +2 -2
  29. package/dist/{chunk-CKUVRZ2T.js → chunk-3U5TXJZS.js} +2 -2
  30. package/dist/{chunk-PPYSEKFM.js → chunk-5H5XV76F.js} +73 -15
  31. package/dist/chunk-5H5XV76F.js.map +1 -0
  32. package/dist/{chunk-YCGY7UIZ.js → chunk-7GRVHU22.js} +2 -2
  33. package/dist/{chunk-Z7ML6L77.js → chunk-HWST3SED.js} +2 -2
  34. package/dist/{chunk-SYDW647C.js → chunk-IA2FBTWC.js} +2 -2
  35. package/dist/{chunk-R67DFVLO.js → chunk-IFVINJ4B.js} +2 -2
  36. package/dist/{chunk-R36V2VP7.js → chunk-IZ5M6OAC.js} +2 -2
  37. package/dist/{chunk-ODT47UAY.js → chunk-K3BQGZCT.js} +2 -2
  38. package/dist/{chunk-IFAV6KEM.js → chunk-KP5KD6EN.js} +2 -2
  39. package/dist/{chunk-ZEWMTR5M.js → chunk-MQMRLGOG.js} +2 -2
  40. package/dist/{chunk-TSWPNOYM.js → chunk-NQG5XDSB.js} +2 -2
  41. package/dist/{chunk-7WSD27QQ.js → chunk-PB64GYIG.js} +2 -2
  42. package/dist/{chunk-HBSWHQNJ.js → chunk-RCYQEFNX.js} +3 -3
  43. package/dist/{chunk-J3KDJNX2.js → chunk-RH5F53JT.js} +2 -2
  44. package/dist/{chunk-UAIOHCUK.js → chunk-SFLA7OH3.js} +3 -3
  45. package/dist/{chunk-Y6O2OCUO.js → chunk-SHM6MRRF.js} +2 -2
  46. package/dist/{chunk-KDIKRJGB.js → chunk-SHYIRB7I.js} +2 -2
  47. package/dist/{chunk-HHXFIHXC.js → chunk-SVR2LKYI.js} +2 -2
  48. package/dist/{chunk-5SBJCB6W.js → chunk-V7AEBY6U.js} +22 -22
  49. package/dist/{chunk-LRRD7NAG.js → chunk-WSKWVEQB.js} +18 -2
  50. package/dist/chunk-WSKWVEQB.js.map +1 -0
  51. package/dist/{chunk-2PVVP7GN.js → chunk-XKEFIFIC.js} +2 -2
  52. package/dist/{chunk-JRWWGMK7.js → chunk-XYA4XSNU.js} +2 -2
  53. package/dist/{chunk-X5YKXC6V.js → chunk-YSMEKBTD.js} +2 -2
  54. package/dist/{chunk-2XU6OGEN.js → chunk-Z4TZ76N7.js} +2 -2
  55. package/dist/index.js +24 -24
  56. package/package.json +6 -5
  57. package/scripts/run-package-tests.mjs +30 -8
  58. package/scripts/verify-packed-consumer.mjs +1 -1
  59. package/scripts/verify-pier-agent.mts +1 -0
  60. package/src/benchmarks/_harness.ts +20 -2
  61. package/src/benchmarks/humaneval.test.mts +122 -0
  62. package/src/benchmarks/humaneval.ts +100 -27
  63. package/src/david-attribution.mts +78 -0
  64. package/src/david-goliath.mts +149 -0
  65. package/src/hev-improve.mts +25 -6
  66. package/src/humaneval-object-ablation.mts +201 -0
  67. package/src/live-improve-campaign-mbpp.mts +641 -0
  68. package/src/live-improve-campaign.mts +500 -0
  69. package/src/mbpp-structural.mts +12 -7
  70. package/src/quant-arena/README.md +144 -0
  71. package/src/quant-arena/backtest.test.mts +135 -0
  72. package/src/quant-arena/backtest.ts +218 -0
  73. package/src/quant-arena/data.test.mts +44 -0
  74. package/src/quant-arena/data.ts +141 -0
  75. package/src/quant-arena/driver.test.mts +253 -0
  76. package/src/quant-arena/driver.ts +219 -0
  77. package/src/quant-arena/fixtures/data/PROVENANCE.md +26 -0
  78. package/src/quant-arena/fixtures/data/holdout/IDX.csv +523 -0
  79. package/src/quant-arena/fixtures/data/holdout/S01.csv +523 -0
  80. package/src/quant-arena/fixtures/data/holdout/S02.csv +523 -0
  81. package/src/quant-arena/fixtures/data/holdout/S03.csv +523 -0
  82. package/src/quant-arena/fixtures/data/holdout/S04.csv +523 -0
  83. package/src/quant-arena/fixtures/data/holdout/S05.csv +523 -0
  84. package/src/quant-arena/fixtures/data/holdout/S06.csv +523 -0
  85. package/src/quant-arena/fixtures/data/holdout/S07.csv +523 -0
  86. package/src/quant-arena/fixtures/data/holdout/S08.csv +523 -0
  87. package/src/quant-arena/fixtures/data/holdout/S09.csv +523 -0
  88. package/src/quant-arena/fixtures/data/holdout/S10.csv +523 -0
  89. package/src/quant-arena/fixtures/data/insample/IDX.csv +2087 -0
  90. package/src/quant-arena/fixtures/data/insample/S01.csv +2087 -0
  91. package/src/quant-arena/fixtures/data/insample/S02.csv +2087 -0
  92. package/src/quant-arena/fixtures/data/insample/S03.csv +2087 -0
  93. package/src/quant-arena/fixtures/data/insample/S04.csv +2087 -0
  94. package/src/quant-arena/fixtures/data/insample/S05.csv +2087 -0
  95. package/src/quant-arena/fixtures/data/insample/S06.csv +2087 -0
  96. package/src/quant-arena/fixtures/data/insample/S07.csv +2087 -0
  97. package/src/quant-arena/fixtures/data/insample/S08.csv +2087 -0
  98. package/src/quant-arena/fixtures/data/insample/S09.csv +2087 -0
  99. package/src/quant-arena/fixtures/data/insample/S10.csv +2087 -0
  100. package/src/quant-arena/fixtures/demo-campaign/cost-ledger.jsonl +16 -0
  101. package/src/quant-arena/fixtures/demo-campaign/notebook.jsonl +5 -0
  102. package/src/quant-arena/fixtures/demo-campaign/rollout-manifest.json +171 -0
  103. package/src/quant-arena/fixtures/demo-campaign/strategies/cand-001-default-author/strategy.ts +119 -0
  104. package/src/quant-arena/fixtures/demo-campaign/strategies/cand-002-default-author/strategy.ts +119 -0
  105. package/src/quant-arena/fixtures/demo-campaign/strategies/cand-003-quant-researcher/strategy.ts +105 -0
  106. package/src/quant-arena/fixtures/demo-campaign/strategies/cand-004-quant-researcher/strategy.ts +102 -0
  107. package/src/quant-arena/fixtures/demo-campaign-v2/cost-ledger.jsonl +4 -0
  108. package/src/quant-arena/fixtures/demo-campaign-v2/notebook.jsonl +2 -0
  109. package/src/quant-arena/fixtures/demo-campaign-v2/rollout-manifest.json +84 -0
  110. package/src/quant-arena/fixtures/demo-campaign-v2/strategies/cand-001-quant-researcher/strategy.ts +117 -0
  111. package/src/quant-arena/holdout-certify.mts +206 -0
  112. package/src/quant-arena/holdout-certify.test.mts +82 -0
  113. package/src/quant-arena/leak-audit.test.mts +79 -0
  114. package/src/quant-arena/leak-audit.ts +95 -0
  115. package/src/quant-arena/make-fixtures.mts +161 -0
  116. package/src/quant-arena/multiplicity.test.mts +68 -0
  117. package/src/quant-arena/multiplicity.ts +87 -0
  118. package/src/quant-arena/nautilus-certify.ts +31 -0
  119. package/src/quant-arena/oms.ts +90 -0
  120. package/src/quant-arena/profiles/quant-researcher.profile.json +7 -0
  121. package/src/quant-arena/python/pyproject.toml +8 -0
  122. package/src/quant-arena/python/uv.lock +1297 -0
  123. package/src/quant-arena/python/vbt-worker.py +192 -0
  124. package/src/quant-arena/quant-loop.mts +813 -0
  125. package/src/quant-arena/quant-loop.test.mts +75 -0
  126. package/src/quant-arena/strategies/buy-hold-index/strategy.ts +11 -0
  127. package/src/quant-arena/strategies/equal-weight/strategy.ts +20 -0
  128. package/src/quant-arena/strategies/sma-crossover/strategy.ts +42 -0
  129. package/src/quant-arena/types.ts +133 -0
  130. package/src/quant-arena/vbt-client.ts +321 -0
  131. package/src/quant-arena/vbt-parity.test.mts +183 -0
  132. package/src/quant-arena/windows.test.mts +45 -0
  133. package/src/quant-arena/windows.ts +54 -0
  134. package/src/rollout-ledger/backfill-swe-arena.mts +606 -0
  135. package/src/rollout-ledger/backfill-swe-arena.test.mts +338 -0
  136. package/src/rollout-ledger/settle-capture.mts +442 -0
  137. package/src/rollout-ledger/settle-capture.test.mts +270 -0
  138. package/src/stream-observe.py +45 -0
  139. package/src/stream-observe.tpl.html +247 -0
  140. package/src/supervisor-arena.mts +816 -0
  141. package/src/swe-arena/activation.mts +228 -0
  142. package/src/swe-arena/activation.test.mts +303 -0
  143. package/src/swe-arena/analyze.ts +211 -0
  144. package/src/swe-arena/arms.ts +804 -0
  145. package/src/swe-arena/bootstrap-meta.mts +188 -0
  146. package/src/swe-arena/bootstrap-meta.test.mts +51 -0
  147. package/src/swe-arena/briefing.mts +217 -0
  148. package/src/swe-arena/briefing.test.mts +178 -0
  149. package/src/swe-arena/calibrate.ts +217 -0
  150. package/src/swe-arena/capabilities.mts +76 -0
  151. package/src/swe-arena/capabilities.test.mts +57 -0
  152. package/src/swe-arena/capacity.ts +194 -0
  153. package/src/swe-arena/cell-evidence.mts +437 -0
  154. package/src/swe-arena/cell-evidence.test.mts +248 -0
  155. package/src/swe-arena/diagnosis-ensemble.test.mts +210 -0
  156. package/src/swe-arena/diagnosis-ensemble.ts +520 -0
  157. package/src/swe-arena/execution.test.mts +1170 -0
  158. package/src/swe-arena/factory-command-container.ts +284 -0
  159. package/src/swe-arena/factory-judge-child.mts +228 -0
  160. package/src/swe-arena/factory.test.mts +643 -0
  161. package/src/swe-arena/fixtures/analyze.py +80 -0
  162. package/src/swe-arena/fixtures/excludes.txt +8 -0
  163. package/src/swe-arena/fixtures/factory/agent-eval-309/calibration.md +51 -0
  164. package/src/swe-arena/fixtures/factory/agent-eval-309/manifest.json +29 -0
  165. package/src/swe-arena/fixtures/factory/agent-eval-309/spec.md +64 -0
  166. package/src/swe-arena/fixtures/factory/agent-runtime-232/calibration.md +48 -0
  167. package/src/swe-arena/fixtures/factory/agent-runtime-232/manifest.json +29 -0
  168. package/src/swe-arena/fixtures/factory/agent-runtime-232/spec.md +48 -0
  169. package/src/swe-arena/fixtures/factory/loops-28/calibration.md +47 -0
  170. package/src/swe-arena/fixtures/factory/loops-28/manifest.json +30 -0
  171. package/src/swe-arena/fixtures/factory/loops-28/spec.md +50 -0
  172. package/src/swe-arena/fixtures/gen1-salvage/README.md +45 -0
  173. package/src/swe-arena/fixtures/gen1-salvage/cand0-e6d7361.diff +116 -0
  174. package/src/swe-arena/fixtures/gen1-salvage/cand1-76a8590.diff +293 -0
  175. package/src/swe-arena/fixtures/holdout-preregister.log +12 -0
  176. package/src/swe-arena/fixtures/holdout.json +44 -0
  177. package/src/swe-arena/fixtures/instances.json +146 -0
  178. package/src/swe-arena/fixtures/ledger.jsonl +12 -0
  179. package/src/swe-arena/fixtures/patches/pallets__flask-5014.solo.patch +36 -0
  180. package/src/swe-arena/fixtures/patches/pydata__xarray-4687.sup.patch +33 -0
  181. package/src/swe-arena/fixtures/rejudge.jsonl +15 -0
  182. package/src/swe-arena/fixtures/rematch.jsonl +3 -0
  183. package/src/swe-arena/fixtures/rematch2.jsonl +3 -0
  184. package/src/swe-arena/fixtures/rematch3.jsonl +3 -0
  185. package/src/swe-arena/fixtures/run-report/README.md +43 -0
  186. package/src/swe-arena/fixtures/run-report/factory-agent-eval-309-FSUP0.json +173 -0
  187. package/src/swe-arena/fixtures/run-report/factory-agent-eval-309-FSUP0.md +100 -0
  188. package/src/swe-arena/fixtures/run-report/gen3-rollup.json +551 -0
  189. package/src/swe-arena/fixtures/run-report/gen3-rollup.md +64 -0
  190. package/src/swe-arena/fixtures/sup-journal-true.json +19 -0
  191. package/src/swe-arena/fixtures/verify/astropy__astropy-13033.sh +48 -0
  192. package/src/swe-arena/fixtures/verify/django__django-11532.sh +50 -0
  193. package/src/swe-arena/fixtures/verify/matplotlib__matplotlib-20826.sh +76 -0
  194. package/src/swe-arena/fixtures/verify/pydata__xarray-4687.sh +44 -0
  195. package/src/swe-arena/fixtures/verify/pytest-dev__pytest-6197.sh +32 -0
  196. package/src/swe-arena/fixtures/verify/sphinx-doc__sphinx-9658.sh +51 -0
  197. package/src/swe-arena/fixtures/worker-tokens.json +42 -0
  198. package/src/swe-arena/fixtures.ts +237 -0
  199. package/src/swe-arena/gepa-seat.mts +583 -0
  200. package/src/swe-arena/gepa-seat.test.mts +635 -0
  201. package/src/swe-arena/holdout-certify.mts +408 -0
  202. package/src/swe-arena/holdout-certify.test.mts +160 -0
  203. package/src/swe-arena/judge-child.mts +37 -0
  204. package/src/swe-arena/ledger-orphans.mts +77 -0
  205. package/src/swe-arena/ledger-orphans.test.mts +147 -0
  206. package/src/swe-arena/lineage-record.mts +164 -0
  207. package/src/swe-arena/lineage-record.test.mts +115 -0
  208. package/src/swe-arena/manifest.mts +293 -0
  209. package/src/swe-arena/manifest.test.mts +169 -0
  210. package/src/swe-arena/materialize.ts +142 -0
  211. package/src/swe-arena/outer-loop.mts +2795 -0
  212. package/src/swe-arena/outer-loop.test.mts +696 -0
  213. package/src/swe-arena/parity.test.mts +87 -0
  214. package/src/swe-arena/premeasured-from-cells.mts +281 -0
  215. package/src/swe-arena/premeasured-from-cells.test.mts +180 -0
  216. package/src/swe-arena/proc.test.mts +174 -0
  217. package/src/swe-arena/proc.ts +260 -0
  218. package/src/swe-arena/profiles/default-author.profile.json +4 -0
  219. package/src/swe-arena/proposer-fanout.mts +770 -0
  220. package/src/swe-arena/proposer-fanout.test.mts +619 -0
  221. package/src/swe-arena/proposer-provenance.mts +177 -0
  222. package/src/swe-arena/proposer-provenance.test.mts +106 -0
  223. package/src/swe-arena/reconcile.ts +0 -0
  224. package/src/swe-arena/replay.mts +183 -0
  225. package/src/swe-arena/replay.test.mts +300 -0
  226. package/src/swe-arena/run-experiment.mts +727 -0
  227. package/src/swe-arena/run-report.mts +75 -0
  228. package/src/swe-arena/run-supervisor.mjs +297 -0
  229. package/src/swe-arena/run-supervisor.test.mts +500 -0
  230. package/src/swe-arena/score-split.mts +140 -0
  231. package/src/swe-arena/score-split.test.mts +123 -0
  232. package/src/swe-arena/serialized-judge.ts +414 -0
  233. package/src/swe-arena/types.ts +218 -0
  234. package/src/swe-code-improve.mts +328 -0
  235. package/src/swe-emit-patch.mts +104 -0
  236. package/src/swe-improve.mts +232 -0
  237. package/src/swe-jail.ts +2 -2
  238. package/src/swe-local-proof.mts +169 -0
  239. package/src/swe-repro-calibrate.mts +446 -0
  240. package/src/swe-stream.mts +1497 -0
  241. package/src/swe-structural.mts +245 -837
  242. package/dist/chunk-LRRD7NAG.js.map +0 -1
  243. package/dist/chunk-PPYSEKFM.js.map +0 -1
  244. /package/dist/{chunk-CKUVRZ2T.js.map → chunk-3U5TXJZS.js.map} +0 -0
  245. /package/dist/{chunk-YCGY7UIZ.js.map → chunk-7GRVHU22.js.map} +0 -0
  246. /package/dist/{chunk-Z7ML6L77.js.map → chunk-HWST3SED.js.map} +0 -0
  247. /package/dist/{chunk-SYDW647C.js.map → chunk-IA2FBTWC.js.map} +0 -0
  248. /package/dist/{chunk-R67DFVLO.js.map → chunk-IFVINJ4B.js.map} +0 -0
  249. /package/dist/{chunk-R36V2VP7.js.map → chunk-IZ5M6OAC.js.map} +0 -0
  250. /package/dist/{chunk-ODT47UAY.js.map → chunk-K3BQGZCT.js.map} +0 -0
  251. /package/dist/{chunk-IFAV6KEM.js.map → chunk-KP5KD6EN.js.map} +0 -0
  252. /package/dist/{chunk-ZEWMTR5M.js.map → chunk-MQMRLGOG.js.map} +0 -0
  253. /package/dist/{chunk-TSWPNOYM.js.map → chunk-NQG5XDSB.js.map} +0 -0
  254. /package/dist/{chunk-7WSD27QQ.js.map → chunk-PB64GYIG.js.map} +0 -0
  255. /package/dist/{chunk-HBSWHQNJ.js.map → chunk-RCYQEFNX.js.map} +0 -0
  256. /package/dist/{chunk-J3KDJNX2.js.map → chunk-RH5F53JT.js.map} +0 -0
  257. /package/dist/{chunk-UAIOHCUK.js.map → chunk-SFLA7OH3.js.map} +0 -0
  258. /package/dist/{chunk-Y6O2OCUO.js.map → chunk-SHM6MRRF.js.map} +0 -0
  259. /package/dist/{chunk-KDIKRJGB.js.map → chunk-SHYIRB7I.js.map} +0 -0
  260. /package/dist/{chunk-HHXFIHXC.js.map → chunk-SVR2LKYI.js.map} +0 -0
  261. /package/dist/{chunk-5SBJCB6W.js.map → chunk-V7AEBY6U.js.map} +0 -0
  262. /package/dist/{chunk-2PVVP7GN.js.map → chunk-XKEFIFIC.js.map} +0 -0
  263. /package/dist/{chunk-JRWWGMK7.js.map → chunk-XYA4XSNU.js.map} +0 -0
  264. /package/dist/{chunk-X5YKXC6V.js.map → chunk-YSMEKBTD.js.map} +0 -0
  265. /package/dist/{chunk-2XU6OGEN.js.map → chunk-Z4TZ76N7.js.map} +0 -0
@@ -0,0 +1,174 @@
1
+ import { access, mkdtemp, readFile, rm } from 'node:fs/promises'
2
+ import { tmpdir } from 'node:os'
3
+ import { join } from 'node:path'
4
+ import { afterAll, describe, expect, it } from 'vitest'
5
+ import { waitForCapacity, type EndpointCapacityGate } from './capacity.ts'
6
+ import { installProcessSignalAbort, run, TIMEOUT_RC } from './proc.ts'
7
+
8
+ const dirs: string[] = []
9
+ const cleanupPids: number[] = []
10
+
11
+ afterAll(async () => {
12
+ for (const pid of cleanupPids) {
13
+ try {
14
+ process.kill(pid, 'SIGKILL')
15
+ } catch {
16
+ // The expected path already removed it.
17
+ }
18
+ }
19
+ await Promise.all(dirs.map((dir) => rm(dir, { recursive: true, force: true })))
20
+ })
21
+
22
+ describe('run process-group timeout', () => {
23
+ it('does not spawn when the caller signal is already aborted', () => {
24
+ const controller = new AbortController()
25
+ controller.abort(new Error('stop before spawn'))
26
+ expect(() => run(process.execPath, ['-e', 'process.exit(99)'], { signal: controller.signal })).toThrow(
27
+ 'stop before spawn',
28
+ )
29
+ })
30
+
31
+ it('allows a SIGTERM handler to finish cleanup before returning timeout', async () => {
32
+ const dir = await mkdtemp(join(tmpdir(), 'swe-proc-term-'))
33
+ dirs.push(dir)
34
+ const marker = join(dir, 'cleanup.txt')
35
+ const result = await run(process.execPath, [
36
+ '-e',
37
+ `const fs=require('node:fs'); const marker=process.argv[1]; process.on('SIGTERM',()=>{fs.writeFileSync(marker,'settled'); setTimeout(()=>process.exit(0),25)}); setInterval(()=>{},1000)`,
38
+ marker,
39
+ ], { timeoutMs: 60, killGraceMs: 500 })
40
+
41
+ expect(result.code).toBe(TIMEOUT_RC)
42
+ expect(result.timedOut).toBe(true)
43
+ expect(await readFile(marker, 'utf8')).toBe('settled')
44
+ })
45
+
46
+ it('force-kills a process group that ignores SIGTERM after the grace period', async () => {
47
+ const dir = await mkdtemp(join(tmpdir(), 'swe-proc-kill-'))
48
+ dirs.push(dir)
49
+ const marker = join(dir, 'should-not-exist.txt')
50
+ const started = Date.now()
51
+ const result = await run(process.execPath, [
52
+ '-e',
53
+ `const fs=require('node:fs'); const marker=process.argv[1]; process.on('SIGTERM',()=>{}); setTimeout(()=>fs.writeFileSync(marker,'escaped'),5000); setInterval(()=>{},1000)`,
54
+ marker,
55
+ ], { timeoutMs: 60, killGraceMs: 80 })
56
+
57
+ expect(result.code).toBe(TIMEOUT_RC)
58
+ expect(result.timedOut).toBe(true)
59
+ expect(Date.now() - started).toBeLessThan(2_000)
60
+ await expect(access(marker)).rejects.toMatchObject({ code: 'ENOENT' })
61
+ })
62
+
63
+ it('kills a TERM-ignoring grandchild when its direct parent exits on TERM', async () => {
64
+ if (process.platform === 'win32') return
65
+ const dir = await mkdtemp(join(tmpdir(), 'swe-proc-tree-'))
66
+ dirs.push(dir)
67
+ const pidFile = join(dir, 'grandchild.pid')
68
+ const grandchild = `process.on('SIGTERM',()=>{}); setInterval(()=>{},1000)`
69
+ const parent = [
70
+ `const {spawn}=require('node:child_process')`,
71
+ `const fs=require('node:fs')`,
72
+ `const child=spawn(process.execPath,['-e',${JSON.stringify(grandchild)}],{stdio:'ignore'})`,
73
+ `fs.writeFileSync(process.argv[1],String(child.pid))`,
74
+ `process.on('SIGTERM',()=>process.exit(0))`,
75
+ `setInterval(()=>{},1000)`,
76
+ ].join(';')
77
+
78
+ const result = await run(process.execPath, ['-e', parent, pidFile], {
79
+ timeoutMs: 100,
80
+ killGraceMs: 50,
81
+ })
82
+ const grandchildPid = Number(await readFile(pidFile, 'utf8'))
83
+ cleanupPids.push(grandchildPid)
84
+
85
+ expect(result.code).toBe(TIMEOUT_RC)
86
+ expect(result.timedOut).toBe(true)
87
+ expect(() => process.kill(grandchildPid, 0)).toThrow(expect.objectContaining({ code: 'ESRCH' }))
88
+ })
89
+
90
+ it('kills a TERM-ignoring grandchild when its direct parent exits normally', async () => {
91
+ if (process.platform === 'win32') return
92
+ const dir = await mkdtemp(join(tmpdir(), 'swe-proc-normal-exit-tree-'))
93
+ dirs.push(dir)
94
+ const pidFile = join(dir, 'grandchild.pid')
95
+ const grandchild = [
96
+ `const fs=require('node:fs')`,
97
+ `process.on('SIGTERM',()=>{})`,
98
+ `fs.writeFileSync(process.argv[1],String(process.pid))`,
99
+ `setInterval(()=>{},1000)`,
100
+ ].join(';')
101
+ const parent = [
102
+ `const {spawn}=require('node:child_process')`,
103
+ `const fs=require('node:fs')`,
104
+ `spawn(process.execPath,['-e',${JSON.stringify(grandchild)},process.argv[1]],{stdio:'ignore'})`,
105
+ `const ready=setInterval(()=>{if(fs.existsSync(process.argv[1])){clearInterval(ready);process.exit(0)}},5)`,
106
+ ].join(';')
107
+
108
+ const result = await run(process.execPath, ['-e', parent, pidFile], { killGraceMs: 50 })
109
+ const grandchildPid = Number(await readFile(pidFile, 'utf8'))
110
+ cleanupPids.push(grandchildPid)
111
+
112
+ expect(result.code).toBe(0)
113
+ expect(result.timedOut).toBe(false)
114
+ expect(result.aborted).toBe(false)
115
+ expect(() => process.kill(grandchildPid, 0)).toThrow(expect.objectContaining({ code: 'ESRCH' }))
116
+ })
117
+ })
118
+
119
+ describe('cooperative process cancellation', () => {
120
+ it('maps the first terminal signal to cancellation and removes its handlers', () => {
121
+ const oldExitCode = process.exitCode
122
+ const beforeInt = new Set(process.rawListeners('SIGINT'))
123
+ const beforeTerm = process.listenerCount('SIGTERM')
124
+ const interrupt = installProcessSignalAbort('test-run')
125
+ const listener = process.rawListeners('SIGINT').find((candidate) => !beforeInt.has(candidate))
126
+ expect(listener).toBeDefined()
127
+ listener?.call(process, 'SIGINT')
128
+ expect(interrupt.signal.aborted).toBe(true)
129
+ expect((interrupt.signal.reason as Error).message).toBe('test-run interrupted by SIGINT')
130
+ expect(interrupt.receivedSignal).toBe('SIGINT')
131
+ expect(process.exitCode).toBe(130)
132
+ interrupt.dispose()
133
+ expect(process.rawListeners('SIGINT').filter((candidate) => !beforeInt.has(candidate))).toHaveLength(0)
134
+ expect(process.listenerCount('SIGTERM')).toBe(beforeTerm)
135
+ process.exitCode = oldExitCode
136
+ })
137
+
138
+ it('cancels during a capacity retry sleep and never starts another probe', async () => {
139
+ const controller = new AbortController()
140
+ let probes = 0
141
+ const gate: EndpointCapacityGate = {
142
+ name: 'cancel-test',
143
+ probe: async () => {
144
+ probes += 1
145
+ return false
146
+ },
147
+ kOfN: { k: 1, n: 1 },
148
+ waitCeilingMs: 10_000,
149
+ retryDelayMs: 10_000,
150
+ }
151
+ const waiting = waitForCapacity(gate, controller.signal)
152
+ await new Promise((resolve) => setTimeout(resolve, 20))
153
+ controller.abort(new Error('capacity cancelled'))
154
+ await expect(waiting).rejects.toThrow('capacity cancelled')
155
+ expect(probes).toBe(1)
156
+ })
157
+
158
+ it('runs zero capacity probes when already aborted', async () => {
159
+ const controller = new AbortController()
160
+ controller.abort(new Error('cancelled before probing'))
161
+ let probes = 0
162
+ const gate: EndpointCapacityGate = {
163
+ name: 'pre-abort',
164
+ probe: async () => {
165
+ probes += 1
166
+ return true
167
+ },
168
+ kOfN: { k: 1, n: 1 },
169
+ waitCeilingMs: 1_000,
170
+ }
171
+ await expect(waitForCapacity(gate, controller.signal)).rejects.toThrow('cancelled before probing')
172
+ expect(probes).toBe(0)
173
+ })
174
+ })
@@ -0,0 +1,260 @@
1
+ /**
2
+ * Process helpers shared by the swe-arena execution path (materialize /
3
+ * calibrate / arms / serialized-judge). One place owns spawn semantics so
4
+ * every port of a bash script gets identical rc/timeout behavior:
5
+ *
6
+ * - `run` never throws on a nonzero exit — the bash scripts branched on `$?`,
7
+ * so callers get `{ code, stdout, stderr, timedOut }` and decide.
8
+ * - timeouts signal the whole process GROUP (detached spawn), because the
9
+ * arm commands are `dotenvx → bash -c → opencode/node` chains; killing only
10
+ * the leader would orphan the model call exactly like a dropped `timeout`.
11
+ * SIGTERM gets a short cleanup window before SIGKILL so children can cancel
12
+ * their own detached workers and flush terminal evidence.
13
+ * - `code` is 124 on timeout, mirroring coreutils `timeout` so ledger rc
14
+ * fields keep the bash experiment's encoding.
15
+ */
16
+
17
+ import { spawn } from 'node:child_process'
18
+
19
+ export interface RunResult {
20
+ code: number
21
+ stdout: string
22
+ stderr: string
23
+ timedOut: boolean
24
+ aborted: boolean
25
+ }
26
+
27
+ export interface RunOptions {
28
+ cwd?: string
29
+ env?: NodeJS.ProcessEnv
30
+ /** 0 / undefined = no timeout. On expiry the process group gets TERM then KILL. */
31
+ timeoutMs?: number
32
+ /** Grace between timeout SIGTERM and fallback SIGKILL. Default 1 second. */
33
+ killGraceMs?: number
34
+ /** Cancels the complete process group and waits until it is gone. */
35
+ signal?: AbortSignal
36
+ /** Cap captured stdout/stderr (bytes); beyond it output is dropped, not buffered. */
37
+ maxBuffer?: number
38
+ /** Piped to the child's stdin, then stdin is closed. */
39
+ stdin?: string
40
+ }
41
+
42
+ const DEFAULT_MAX_BUFFER = 64 * 1024 * 1024
43
+ const PROCESS_GROUP_EXIT_CONFIRM_MS = 1_000
44
+ const PROCESS_GROUP_EXIT_POLL_MS = 10
45
+
46
+ const exitKillGroups = new Set<number>()
47
+ const killRegisteredGroupsOnExit = (): void => {
48
+ for (const processGroupId of exitKillGroups) {
49
+ try {
50
+ process.kill(process.platform === 'win32' ? processGroupId : -processGroupId, 'SIGKILL')
51
+ } catch {
52
+ // The group may have settled between its final async check and process exit.
53
+ }
54
+ }
55
+ }
56
+
57
+ function registerExitKill(processGroupId: number | undefined): () => void {
58
+ if (processGroupId === undefined) return () => {}
59
+ if (exitKillGroups.size === 0) process.once('exit', killRegisteredGroupsOnExit)
60
+ exitKillGroups.add(processGroupId)
61
+ return () => {
62
+ exitKillGroups.delete(processGroupId)
63
+ if (exitKillGroups.size === 0) process.removeListener('exit', killRegisteredGroupsOnExit)
64
+ }
65
+ }
66
+
67
+ /** coreutils `timeout` exit code — kept so ledger rc columns stay comparable. */
68
+ export const TIMEOUT_RC = 124
69
+ export const ABORT_RC = 130
70
+
71
+ export interface ProcessSignalAbort {
72
+ signal: AbortSignal
73
+ /** First signal received; null until interrupted. */
74
+ readonly receivedSignal: NodeJS.Signals | null
75
+ /** Removes handlers after the caller has awaited cleanup. */
76
+ dispose: () => void
77
+ }
78
+
79
+ /**
80
+ * Translate terminal interrupts into cooperative cancellation. The handler
81
+ * never exits immediately: callers await their normal cleanup in `finally`,
82
+ * then dispose it. Repeated interrupts remain cooperative rather than
83
+ * bypassing cleanup.
84
+ */
85
+ export function installProcessSignalAbort(label: string): ProcessSignalAbort {
86
+ const controller = new AbortController()
87
+ let receivedSignal: NodeJS.Signals | null = null
88
+ const onSignal = (signal: NodeJS.Signals): void => {
89
+ if (receivedSignal !== null) return
90
+ receivedSignal = signal
91
+ process.exitCode = signal === 'SIGINT' ? 130 : 143
92
+ controller.abort(new Error(`${label} interrupted by ${signal}`))
93
+ }
94
+ const onSigint = (): void => onSignal('SIGINT')
95
+ const onSigterm = (): void => onSignal('SIGTERM')
96
+ process.on('SIGINT', onSigint)
97
+ process.on('SIGTERM', onSigterm)
98
+ return {
99
+ signal: controller.signal,
100
+ get receivedSignal() {
101
+ return receivedSignal
102
+ },
103
+ dispose: () => {
104
+ process.removeListener('SIGINT', onSigint)
105
+ process.removeListener('SIGTERM', onSigterm)
106
+ },
107
+ }
108
+ }
109
+
110
+ function processGroupExists(processGroupId: number): boolean {
111
+ try {
112
+ process.kill(-processGroupId, 0)
113
+ return true
114
+ } catch (err) {
115
+ return (err as NodeJS.ErrnoException).code !== 'ESRCH'
116
+ }
117
+ }
118
+
119
+ async function waitUntil(deadline: number, predicate: () => boolean): Promise<boolean> {
120
+ while (predicate()) {
121
+ if (Date.now() >= deadline) return false
122
+ await new Promise((resolve) => setTimeout(resolve, PROCESS_GROUP_EXIT_POLL_MS))
123
+ }
124
+ return true
125
+ }
126
+
127
+ export function run(bin: string, argv: string[], opts: RunOptions = {}): Promise<RunResult> {
128
+ opts.signal?.throwIfAborted()
129
+ return new Promise((resolve, reject) => {
130
+ const child = spawn(bin, argv, {
131
+ cwd: opts.cwd,
132
+ env: opts.env ?? process.env,
133
+ detached: true,
134
+ stdio: ['pipe', 'pipe', 'pipe'],
135
+ })
136
+ const unregisterExitKill = registerExitKill(child.pid)
137
+ const cap = opts.maxBuffer ?? DEFAULT_MAX_BUFFER
138
+ let stdout = ''
139
+ let stderr = ''
140
+ let outBytes = 0
141
+ let errBytes = 0
142
+ let timedOut = false
143
+ let aborted = false
144
+ let settled = false
145
+ let timer: NodeJS.Timeout | undefined
146
+ const signalGroup = (signal: NodeJS.Signals) => {
147
+ try {
148
+ process.kill(-child.pid!, signal)
149
+ } catch {
150
+ try {
151
+ child.kill(signal)
152
+ } catch {
153
+ /* already dead */
154
+ }
155
+ }
156
+ }
157
+ let termination: Promise<void> | undefined
158
+ const terminate = (): Promise<void> => {
159
+ termination ??= (async () => {
160
+ signalGroup('SIGTERM')
161
+ if (process.platform === 'win32' || typeof child.pid !== 'number') return
162
+ const processGroupId = child.pid
163
+ const exitedOnTerm = await waitUntil(
164
+ Date.now() + Math.max(0, opts.killGraceMs ?? 1_000),
165
+ () => processGroupExists(processGroupId),
166
+ )
167
+ if (exitedOnTerm) return
168
+ signalGroup('SIGKILL')
169
+ const exitedOnKill = await waitUntil(
170
+ Date.now() + PROCESS_GROUP_EXIT_CONFIRM_MS,
171
+ () => processGroupExists(processGroupId),
172
+ )
173
+ if (!exitedOnKill) throw new Error(`process group ${processGroupId} survived SIGKILL`)
174
+ })()
175
+ // The close handler owns the rejection. Attaching this handler prevents
176
+ // an early unhandled rejection while the direct child is still closing.
177
+ void termination.catch(() => undefined)
178
+ return termination
179
+ }
180
+ const onAbort = () => {
181
+ aborted = true
182
+ void terminate()
183
+ }
184
+ if (opts.timeoutMs && opts.timeoutMs > 0) {
185
+ timer = setTimeout(() => {
186
+ timedOut = true
187
+ void terminate()
188
+ }, opts.timeoutMs)
189
+ }
190
+ opts.signal?.addEventListener('abort', onAbort, { once: true })
191
+ if (opts.signal?.aborted) onAbort()
192
+ child.stdout.on('data', (c: Buffer) => {
193
+ outBytes += c.length
194
+ if (outBytes <= cap) stdout += c.toString('utf8')
195
+ })
196
+ child.stderr.on('data', (c: Buffer) => {
197
+ errBytes += c.length
198
+ if (errBytes <= cap) stderr += c.toString('utf8')
199
+ })
200
+ child.on('error', (err) => {
201
+ if (settled) return
202
+ settled = true
203
+ if (timer) clearTimeout(timer)
204
+ opts.signal?.removeEventListener('abort', onAbort)
205
+ unregisterExitKill()
206
+ reject(err)
207
+ })
208
+ child.on('close', (code, signal) => {
209
+ if (settled) return
210
+ if (timer) clearTimeout(timer)
211
+ opts.signal?.removeEventListener('abort', onAbort)
212
+ void (async () => {
213
+ try {
214
+ if (
215
+ !termination &&
216
+ process.platform !== 'win32' &&
217
+ typeof child.pid === 'number' &&
218
+ processGroupExists(child.pid)
219
+ ) {
220
+ await terminate()
221
+ }
222
+ if (termination) await termination
223
+ if (settled) return
224
+ settled = true
225
+ unregisterExitKill()
226
+ resolve({
227
+ code: timedOut ? TIMEOUT_RC : aborted ? ABORT_RC : (code ?? (signal ? 1 : 0)),
228
+ stdout,
229
+ stderr,
230
+ timedOut,
231
+ aborted,
232
+ })
233
+ } catch (err) {
234
+ if (settled) return
235
+ settled = true
236
+ unregisterExitKill()
237
+ reject(err)
238
+ }
239
+ })()
240
+ })
241
+ if (opts.stdin !== undefined) child.stdin.end(opts.stdin)
242
+ else child.stdin.end()
243
+ })
244
+ }
245
+
246
+ /** `run` that throws (with captured stderr) on nonzero exit — for steps the bash aborted on. */
247
+ export async function runOk(bin: string, argv: string[], opts: RunOptions = {}): Promise<RunResult> {
248
+ const res = await run(bin, argv, opts)
249
+ opts.signal?.throwIfAborted()
250
+ if (res.code !== 0) {
251
+ const detail = (res.stderr || res.stdout).slice(0, 1500)
252
+ throw new Error(`${bin} ${argv.join(' ')} exited ${res.code}${res.timedOut ? ' (timeout)' : ''}: ${detail}`)
253
+ }
254
+ return res
255
+ }
256
+
257
+ /** Single-quote shell escaping — identical to the quoting the bash scripts relied on. */
258
+ export function shq(value: string): string {
259
+ return `'${value.replace(/'/g, `'\\''`)}'`
260
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "name": "swe-arena-default-author",
3
+ "description": "The gen-2 winning author seat, codified: bare claude CLI invocation. No systemPrompt, no instructions, no model override — harnessInvocation() with this profile is byte-identical to the legacy prompt-only buildArgs path, so provenance pins the author identity without changing gen-2 behavior."
4
+ }