reeval 0.15.1 → 0.17.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 (279) hide show
  1. package/README.md +21 -14
  2. package/dist/{adapter-CbqbMR7y.js → adapter-CqUt4A0w.js} +3 -3
  3. package/dist/{adapter-CbqbMR7y.js.map → adapter-CqUt4A0w.js.map} +1 -1
  4. package/dist/{agent-Zgn-kiqq.js → agent-BtAbRTFq.js} +6 -6
  5. package/dist/{agent-Zgn-kiqq.js.map → agent-BtAbRTFq.js.map} +1 -1
  6. package/dist/{app-files-ChbvRx3P.js → app-files-CDttHPOz.js} +3 -3
  7. package/dist/app-files-CDttHPOz.js.map +1 -0
  8. package/dist/assemble-Bb7zqpRw.js +1 -0
  9. package/dist/{assemble-SKQGW_QF.js → assemble-J9rkE29m.js} +114 -31
  10. package/dist/assemble-J9rkE29m.js.map +1 -0
  11. package/dist/{attempt-Ba2QerOK.js → attempt-CpbZ9arV.js} +2 -2
  12. package/dist/{attempt-Ba2QerOK.js.map → attempt-CpbZ9arV.js.map} +1 -1
  13. package/dist/{attempt-host-BOp8JklD.js → attempt-host-DDedP7Qv.js} +17 -8
  14. package/dist/attempt-host-DDedP7Qv.js.map +1 -0
  15. package/dist/capture-Bvc4FtWT.js +15 -0
  16. package/dist/capture-Bvc4FtWT.js.map +1 -0
  17. package/dist/capture-D68TiQQy.js +1 -0
  18. package/dist/{capture-BM_3100g.js → capture-DheNqeYS.js} +1495 -203
  19. package/dist/capture-DheNqeYS.js.map +1 -0
  20. package/dist/{cli-PdYrcoKM.js → cli-KTHjNFoo.js} +329 -74
  21. package/dist/cli-KTHjNFoo.js.map +1 -0
  22. package/dist/{cli-spec-D0qyrPZi.js → cli-spec-CPWT1mAG.js} +3 -2
  23. package/dist/cli-spec-CPWT1mAG.js.map +1 -0
  24. package/dist/cli.js +2 -2
  25. package/dist/{client-Ct6s6OnF.js → client-DMmOkbRA.js} +13 -6
  26. package/dist/client-DMmOkbRA.js.map +1 -0
  27. package/dist/{colors-QV9dFVhN.js → colors-Y6O3mtl_.js} +20 -2
  28. package/dist/colors-Y6O3mtl_.js.map +1 -0
  29. package/dist/{command-9xm5ImqI.js → command-1FBECMs-.js} +16 -16
  30. package/dist/{command-9xm5ImqI.js.map → command-1FBECMs-.js.map} +1 -1
  31. package/dist/{command-BqBxLgTc.js → command-Ce03LLtg.js} +14 -14
  32. package/dist/{command-BqBxLgTc.js.map → command-Ce03LLtg.js.map} +1 -1
  33. package/dist/{command-4pJYVCT4.js → command-CztRuwUM.js} +12 -12
  34. package/dist/{command-4pJYVCT4.js.map → command-CztRuwUM.js.map} +1 -1
  35. package/dist/{command-DbSMcxZL.js → command-nNN0mx65.js} +7 -7
  36. package/dist/{command-DbSMcxZL.js.map → command-nNN0mx65.js.map} +1 -1
  37. package/dist/{commands-BU6YJCz9.js → commands-DP13JX9i.js} +24 -12
  38. package/dist/commands-DP13JX9i.js.map +1 -0
  39. package/dist/{commands-Bi2SEQtr.js → commands-ZwQUezI1.js} +1 -1
  40. package/dist/{config-k6m7XbAz.js → config-BjjDryd5.js} +464 -220
  41. package/dist/config-BjjDryd5.js.map +1 -0
  42. package/dist/{contrasts-DLZF4dGF.js → contrasts-DSsRZpR8.js} +2 -2
  43. package/dist/{contrasts-DLZF4dGF.js.map → contrasts-DSsRZpR8.js.map} +1 -1
  44. package/dist/cursor-catalog-D7bFLEAD.js +143 -0
  45. package/dist/cursor-catalog-D7bFLEAD.js.map +1 -0
  46. package/dist/{db-CpsifVln.js → db-B9Ityuit.js} +8 -8
  47. package/dist/{db-CpsifVln.js.map → db-B9Ityuit.js.map} +1 -1
  48. package/dist/db-Bk6LWwza.js +2 -0
  49. package/dist/debug-CAx3qcaP.js +1 -0
  50. package/dist/{debug-BhizgAsU.js → debug-CgG3T071.js} +4 -2
  51. package/dist/debug-CgG3T071.js.map +1 -0
  52. package/dist/default-effort-DK8gospa.js +91 -0
  53. package/dist/default-effort-DK8gospa.js.map +1 -0
  54. package/dist/default-effort-DmXHpqo9.js +1 -0
  55. package/dist/{docker-YFj4H1Cr.js → docker-BLCNfUie.js} +1 -1
  56. package/dist/{docker-B4-rx4dA.js → docker-SKpYOlGi.js} +5 -5
  57. package/dist/{docker-B4-rx4dA.js.map → docker-SKpYOlGi.js.map} +1 -1
  58. package/dist/{docker-51WvhN_X.js → docker-hwp3EucJ.js} +9 -5
  59. package/dist/docker-hwp3EucJ.js.map +1 -0
  60. package/dist/{doctor-BuQnfz4e.js → doctor-CT0Sgjng.js} +16 -16
  61. package/dist/{doctor-BuQnfz4e.js.map → doctor-CT0Sgjng.js.map} +1 -1
  62. package/dist/{environment-BOFgzFDg.js → environment-CZdercoj.js} +3 -3
  63. package/dist/{environment-BOFgzFDg.js.map → environment-CZdercoj.js.map} +1 -1
  64. package/dist/{errors-UjcLWelF.js → errors-BZVKVKYd.js} +15 -4
  65. package/dist/errors-BZVKVKYd.js.map +1 -0
  66. package/dist/{eval-source-Dje3rERU.js → eval-source-nZD7-FSz.js} +9 -8
  67. package/dist/eval-source-nZD7-FSz.js.map +1 -0
  68. package/dist/{evaluate-dqXccn4k.d.ts → evaluate-CNi0lZ7n.d.ts} +7 -6
  69. package/dist/{evaluate-BPP9s3s6.js → evaluate-fhC1PCd_.js} +345 -47
  70. package/dist/evaluate-fhC1PCd_.js.map +1 -0
  71. package/dist/{files-Clipx4OW.js → files-BGy05Dlr.js} +4 -4
  72. package/dist/{files-Clipx4OW.js.map → files-BGy05Dlr.js.map} +1 -1
  73. package/dist/{files-Bi461Be2.js → files-BIGxpL08.js} +25 -16
  74. package/dist/{files-Bi461Be2.js.map → files-BIGxpL08.js.map} +1 -1
  75. package/dist/files-C2RC7K4m.js +1 -0
  76. package/dist/files-C3tQtD26.js +1 -0
  77. package/dist/{files-Ca0d6Uq3.js → files-D1y8Pg4Z.js} +35 -7
  78. package/dist/files-D1y8Pg4Z.js.map +1 -0
  79. package/dist/{files-H24pz0hL.js → files-I1i6Xyh1.js} +4 -4
  80. package/dist/{files-H24pz0hL.js.map → files-I1i6Xyh1.js.map} +1 -1
  81. package/dist/files-dJmrfmOM.js +1 -0
  82. package/dist/{pairs-puYi-zj2.js → gate-BC6SdQZs.js} +49 -2
  83. package/dist/gate-BC6SdQZs.js.map +1 -0
  84. package/dist/gateway.d.ts +1 -1
  85. package/dist/{git-CsVZkNNL.js → git-DR02tHtU.js} +2 -2
  86. package/dist/{git-CsVZkNNL.js.map → git-DR02tHtU.js.map} +1 -1
  87. package/dist/{harness-3NVyTSGK.js → harness-ylJLvrbp.js} +39 -5
  88. package/dist/{harness-3NVyTSGK.js.map → harness-ylJLvrbp.js.map} +1 -1
  89. package/dist/harness.d.ts +1 -1
  90. package/dist/harness.js +1 -1
  91. package/dist/{images-4mwhNkvh.js → images---X8Wu9d.js} +8 -8
  92. package/dist/{images-4mwhNkvh.js.map → images---X8Wu9d.js.map} +1 -1
  93. package/dist/index.d.ts +71 -16
  94. package/dist/index.js +5 -5
  95. package/dist/init-BNTXrtMO.js +1 -0
  96. package/dist/{init-cOk3D44b.js → init-CA95ZnkV.js} +4 -4
  97. package/dist/{init-cOk3D44b.js.map → init-CA95ZnkV.js.map} +1 -1
  98. package/dist/{invocations-DW-lTk2f.js → invocations-BdoROAfb.js} +3 -3
  99. package/dist/{invocations-DW-lTk2f.js.map → invocations-BdoROAfb.js.map} +1 -1
  100. package/dist/{judge-CKdgEXN7.js → judge-Dp7FPJ1P.js} +3 -3
  101. package/dist/{judge-CKdgEXN7.js.map → judge-Dp7FPJ1P.js.map} +1 -1
  102. package/dist/judge.d.ts +2 -2
  103. package/dist/judge.js +1 -1
  104. package/dist/{live-XJQ6nqK4.js → live-CBFkdK1K.js} +3 -3
  105. package/dist/{live-XJQ6nqK4.js.map → live-CBFkdK1K.js.map} +1 -1
  106. package/dist/{matchers-DT2TaGix.js → matchers-Cv74mJRJ.js} +43 -13
  107. package/dist/matchers-Cv74mJRJ.js.map +1 -0
  108. package/dist/{model-Bq3G0OG1.js → model-6dfKFkQd.js} +4 -4
  109. package/dist/{model-Bq3G0OG1.js.map → model-6dfKFkQd.js.map} +1 -1
  110. package/dist/{model-CTnyvh7H.js → model-Be7fPvgi.js} +4 -51
  111. package/dist/model-Be7fPvgi.js.map +1 -0
  112. package/dist/{model-g3XnzUv0.js → model-CN4ccjNz.js} +1 -1
  113. package/dist/model-C_5TspZS.js +1 -0
  114. package/dist/{model-vRiUycWh.js → model-DkJVmVd2.js} +4 -4
  115. package/dist/{model-vRiUycWh.js.map → model-DkJVmVd2.js.map} +1 -1
  116. package/dist/{node-BVbbnFtv.js → node-D_Zkwhq2.js} +2 -2
  117. package/dist/{node-BVbbnFtv.js.map → node-D_Zkwhq2.js.map} +1 -1
  118. package/dist/{object-CLf2-tNq.js → object-BkXGPKWY.js} +3 -2
  119. package/dist/object-BkXGPKWY.js.map +1 -0
  120. package/dist/{opencode-providers-Bz0l7OZI.js → opencode-providers-DPgdqRfg.js} +26 -2
  121. package/dist/{opencode-providers-Bz0l7OZI.js.map → opencode-providers-DPgdqRfg.js.map} +1 -1
  122. package/dist/{openrouter-models-CWRwLX-G.js → openrouter-models-CR00FWAa.js} +2 -2
  123. package/dist/{openrouter-models-CWRwLX-G.js.map → openrouter-models-CR00FWAa.js.map} +1 -1
  124. package/dist/{patch-DKJiC5Sq.js → patch-BJNdOZsf.js} +4 -4
  125. package/dist/patch-BJNdOZsf.js.map +1 -0
  126. package/dist/{plan-DdWkM6SW.js → plan-C1UpGZTk.js} +76 -12
  127. package/dist/plan-C1UpGZTk.js.map +1 -0
  128. package/dist/{plugins-D-CP6mMd.js → plugins-Cx6ReJWE.js} +24 -3
  129. package/dist/plugins-Cx6ReJWE.js.map +1 -0
  130. package/dist/{preflight-9ixuXzsx.js → preflight-BZ3L7P3a.js} +3 -3
  131. package/dist/{preflight-9ixuXzsx.js.map → preflight-BZ3L7P3a.js.map} +1 -1
  132. package/dist/{preflight-DTiVv0gY.js → preflight-CUooKgtT.js} +1 -1
  133. package/dist/{prepare-BfyikQit.js → prepare-BbBoTeDj.js} +7 -7
  134. package/dist/{prepare-BfyikQit.js.map → prepare-BbBoTeDj.js.map} +1 -1
  135. package/dist/{prepare-tmBBf4W8.js → prepare-CEZaEwaq.js} +2 -2
  136. package/dist/{provenance-BizwUn3h.js → provenance-BWvTIlQM.js} +2 -2
  137. package/dist/{provenance-BizwUn3h.js.map → provenance-BWvTIlQM.js.map} +1 -1
  138. package/dist/{providers-B6b9Tnyi.js → providers-B3yqakc7.js} +5 -5
  139. package/dist/{providers-B6b9Tnyi.js.map → providers-B3yqakc7.js.map} +1 -1
  140. package/dist/{providers-DkOupEBl.js → providers-DdyZW_ru.js} +1 -1
  141. package/dist/query-BkwQ0W-c.js +1 -0
  142. package/dist/{query-DyM-VExS.js → query-CZcqpOI9.js} +2 -2
  143. package/dist/{query-DyM-VExS.js.map → query-CZcqpOI9.js.map} +1 -1
  144. package/dist/{questions-BRhU8MzB.js → questions-CFU7-crW.js} +20 -19
  145. package/dist/questions-CFU7-crW.js.map +1 -0
  146. package/dist/{record-DZBCjuwm.js → record-Bu1Xi6MQ.js} +8 -4
  147. package/dist/record-Bu1Xi6MQ.js.map +1 -0
  148. package/dist/{remarks-C7i2LMRb.js → remarks-A3j-SkJ_.js} +42 -8
  149. package/dist/remarks-A3j-SkJ_.js.map +1 -0
  150. package/dist/remarks-Bp6AwPgb.js +1 -0
  151. package/dist/{remarks-D_3qhBWU.js → remarks-Dj_x9l5n.js} +4 -3
  152. package/dist/{remarks-D_3qhBWU.js.map → remarks-Dj_x9l5n.js.map} +1 -1
  153. package/dist/{remarks-dA8LyoPc.js → remarks-eBcrIZRu.js} +3 -2
  154. package/dist/remarks-eBcrIZRu.js.map +1 -0
  155. package/dist/{remote-DFIzcx_6.js → remote-EIFHdMd8.js} +5 -5
  156. package/dist/{remote-DFIzcx_6.js.map → remote-EIFHdMd8.js.map} +1 -1
  157. package/dist/{resolve-DWSJpKHX.js → resolve-8H5rz8mi.js} +5 -5
  158. package/dist/{resolve-DWSJpKHX.js.map → resolve-8H5rz8mi.js.map} +1 -1
  159. package/dist/resolve-Tm9pX7Hv.js +1 -0
  160. package/dist/{results-BC3pcG_Z.js → results-E7JdVjq2.js} +9 -9
  161. package/dist/{results-BC3pcG_Z.js.map → results-E7JdVjq2.js.map} +1 -1
  162. package/dist/{resume-j-rcIryi.js → resume-Blu4yIcq.js} +4 -4
  163. package/dist/{resume-j-rcIryi.js.map → resume-Blu4yIcq.js.map} +1 -1
  164. package/dist/{runtime-d56Gg3J5.js → runtime-CNbsMJQ7.js} +111 -37
  165. package/dist/runtime-CNbsMJQ7.js.map +1 -0
  166. package/dist/{sandbox-Cmln11gU.js → sandbox-RButGfIE.js} +2 -2
  167. package/dist/{sandbox-Cmln11gU.js.map → sandbox-RButGfIE.js.map} +1 -1
  168. package/dist/sandbox.d.ts +1 -1
  169. package/dist/sandbox.js +1 -1
  170. package/dist/server-Dqrq-Sia.js +1 -0
  171. package/dist/{server-BOoH10J8.js → server-XgCYs_MF.js} +35 -11
  172. package/dist/server-XgCYs_MF.js.map +1 -0
  173. package/dist/{source-wD04-rRG.js → source-BOkbJCD2.js} +17 -3
  174. package/dist/{source-wD04-rRG.js.map → source-BOkbJCD2.js.map} +1 -1
  175. package/dist/source-DooSTtyz.js +1 -0
  176. package/dist/src-1M0srjji.js +1 -0
  177. package/dist/{src-DgHuStEi.js → src-A8HLtO0a.js} +2 -2
  178. package/dist/{src-DgHuStEi.js.map → src-A8HLtO0a.js.map} +1 -1
  179. package/dist/{src-D91Hd6hT.js → src-BJCKWacl.js} +38 -18
  180. package/dist/src-BJCKWacl.js.map +1 -0
  181. package/dist/src-BZlhaDhE.js +2 -0
  182. package/dist/{src-BO8AYqJ5.js → src-BkhEPVJZ.js} +12 -3
  183. package/dist/{src-BO8AYqJ5.js.map → src-BkhEPVJZ.js.map} +1 -1
  184. package/dist/{src-C7upnZSd.js → src-Bp6X4193.js} +1 -1
  185. package/dist/{src-BYLM3hEz.js → src-C2Japg_I.js} +21 -10
  186. package/dist/{src-BYLM3hEz.js.map → src-C2Japg_I.js.map} +1 -1
  187. package/dist/src-C5q3_fRV.js +3 -0
  188. package/dist/src-C9EHSYyP.js +2 -0
  189. package/dist/src-CJdpsrXi.js +2 -0
  190. package/dist/{src-1lpTocbd.js → src-D1ET6oPL.js} +3 -27
  191. package/dist/{src-1lpTocbd.js.map → src-D1ET6oPL.js.map} +1 -1
  192. package/dist/{src-Bp4aN3rA.js → src-D4CZjMQ4.js} +1 -1
  193. package/dist/{src-BYPMM1x7.js → src-GkJ-1Rdy.js} +1 -1
  194. package/dist/{src-kYeaJJrl.js → src-OOKE-5GA.js} +1 -1
  195. package/dist/state-BCyNaz13.js +1 -0
  196. package/dist/{state-DrJ687ut.js → state-D6-tvqtp.js} +8 -8
  197. package/dist/{state-DrJ687ut.js.map → state-D6-tvqtp.js.map} +1 -1
  198. package/dist/suite-BiWajSlA.js +2 -0
  199. package/dist/{suite-Co8cwelb.js → suite-DYI1k2Ie.js} +22 -5
  200. package/dist/suite-DYI1k2Ie.js.map +1 -0
  201. package/dist/{summary-DLFHVoux.js → summary-CN2aqAUz.js} +59 -17
  202. package/dist/summary-CN2aqAUz.js.map +1 -0
  203. package/dist/tarball-CHcahyNK.js +258 -0
  204. package/dist/tarball-CHcahyNK.js.map +1 -0
  205. package/dist/testing.d.ts +96 -44
  206. package/dist/testing.js +4 -4
  207. package/dist/{tool-target-D2mcYlSQ.js → tool-target-CKWkd6QI.js} +2 -2
  208. package/dist/{tool-target-D2mcYlSQ.js.map → tool-target-CKWkd6QI.js.map} +1 -1
  209. package/dist/{transcript-DxPaWHTZ.js → transcript-BJrKfgRW.js} +2 -2
  210. package/dist/{transcript-DxPaWHTZ.js.map → transcript-BJrKfgRW.js.map} +1 -1
  211. package/dist/{types-coMWHaU2.d.ts → types-DDeCnNmp.d.ts} +437 -181
  212. package/dist/view/index.d.ts +34 -5
  213. package/dist/view/index.js +8 -7
  214. package/dist/views-7bNY17Z6.js +2 -0
  215. package/dist/{views-BFO1BLKu.js → views-Bd-H71_t.js} +5 -5
  216. package/dist/{views-BFO1BLKu.js.map → views-Bd-H71_t.js.map} +1 -1
  217. package/dist/{worker-entry-CJtdsKBP.js → worker-entry-BIHJqE_z.js} +34 -13
  218. package/dist/worker-entry-BIHJqE_z.js.map +1 -0
  219. package/dist/workspace-CxwHv8mG.js +1 -0
  220. package/dist/{workspace-BRxjfa8N.js → workspace-Sy5g8zW9.js} +4 -4
  221. package/dist/{workspace-BRxjfa8N.js.map → workspace-Sy5g8zW9.js.map} +1 -1
  222. package/package.json +1 -1
  223. package/skills/eval-rubric/SKILL.md +11 -0
  224. package/dist/app-files-ChbvRx3P.js.map +0 -1
  225. package/dist/assemble-SKQGW_QF.js.map +0 -1
  226. package/dist/assemble-XBWuK3Ed.js +0 -1
  227. package/dist/attempt-host-BOp8JklD.js.map +0 -1
  228. package/dist/capture-BM_3100g.js.map +0 -1
  229. package/dist/capture-DSK8O5bA.js +0 -1
  230. package/dist/cli-PdYrcoKM.js.map +0 -1
  231. package/dist/cli-spec-D0qyrPZi.js.map +0 -1
  232. package/dist/client-Ct6s6OnF.js.map +0 -1
  233. package/dist/colors-QV9dFVhN.js.map +0 -1
  234. package/dist/commands-BU6YJCz9.js.map +0 -1
  235. package/dist/config-k6m7XbAz.js.map +0 -1
  236. package/dist/db-DkD-F-X-.js +0 -2
  237. package/dist/debug-BhizgAsU.js.map +0 -1
  238. package/dist/debug-DeM5pswK.js +0 -1
  239. package/dist/docker-51WvhN_X.js.map +0 -1
  240. package/dist/errors-UjcLWelF.js.map +0 -1
  241. package/dist/eval-source-Dje3rERU.js.map +0 -1
  242. package/dist/evaluate-BPP9s3s6.js.map +0 -1
  243. package/dist/files-BAt0i00X.js +0 -1
  244. package/dist/files-C2h2Cw4F.js +0 -1
  245. package/dist/files-Ca0d6Uq3.js.map +0 -1
  246. package/dist/files-DmUrRmmL.js +0 -1
  247. package/dist/init-BDEOopK7.js +0 -1
  248. package/dist/matchers-DT2TaGix.js.map +0 -1
  249. package/dist/model-CTnyvh7H.js.map +0 -1
  250. package/dist/model-D-SWrLZG.js +0 -1
  251. package/dist/object-CLf2-tNq.js.map +0 -1
  252. package/dist/pairs-puYi-zj2.js.map +0 -1
  253. package/dist/patch-DKJiC5Sq.js.map +0 -1
  254. package/dist/plan-DdWkM6SW.js.map +0 -1
  255. package/dist/plugins-D-CP6mMd.js.map +0 -1
  256. package/dist/query-Clm2mNSm.js +0 -1
  257. package/dist/questions-BRhU8MzB.js.map +0 -1
  258. package/dist/record-DZBCjuwm.js.map +0 -1
  259. package/dist/remarks-BwJ8pcTQ.js +0 -1
  260. package/dist/remarks-C7i2LMRb.js.map +0 -1
  261. package/dist/remarks-dA8LyoPc.js.map +0 -1
  262. package/dist/resolve--rQZ8u6Z.js +0 -1
  263. package/dist/runtime-d56Gg3J5.js.map +0 -1
  264. package/dist/server-BOoH10J8.js.map +0 -1
  265. package/dist/server-DpuVAbrO.js +0 -1
  266. package/dist/source-BFd6f0-R.js +0 -1
  267. package/dist/src-6_zz7oJs.js +0 -2
  268. package/dist/src-D91Hd6hT.js.map +0 -1
  269. package/dist/src-DH-9IO7f.js +0 -1
  270. package/dist/src-HSxBazcg.js +0 -3
  271. package/dist/src-LD1BAJFa.js +0 -2
  272. package/dist/src-dlX-Lmxg.js +0 -2
  273. package/dist/state-B5PLm5qY.js +0 -1
  274. package/dist/suite-22Kt9Grx.js +0 -2
  275. package/dist/suite-Co8cwelb.js.map +0 -1
  276. package/dist/summary-DLFHVoux.js.map +0 -1
  277. package/dist/views-BOf8qEL8.js +0 -2
  278. package/dist/worker-entry-CJtdsKBP.js.map +0 -1
  279. package/dist/workspace-DDHzkMjm.js +0 -1
@@ -1,29 +1,32 @@
1
- import { n as isOneOf } from "./object-CLf2-tNq.js";
1
+ import { n as isOneOf } from "./object-BkXGPKWY.js";
2
2
  import { n as UsageError } from "./errors-B6BSMs03.js";
3
3
  import { c as jsonValueSchema, h as parseDuration, i as durationSchema, m as TIMER_LIMIT, n as dataProblem, p as MAX_TIMER_MS } from "./shared-B_lvGqWz.js";
4
- import { y as gatewayNameSchema } from "./harness-3NVyTSGK.js";
4
+ import { x as gatewayNameSchema } from "./harness-ylJLvrbp.js";
5
5
  import { l as sha256 } from "./ids-Bp6ut7ZD.js";
6
- import { Ct as captureWindowSchema, Et as judgeModelNameSchema, Ot as judgeStateSchema, Tt as judgeFields, b as resolveModelGateway, v as gatewayScope, wt as fakeJudgeDataSchema } from "./config-k6m7XbAz.js";
6
+ import { At as captureWindowSchema, Ft as judgeFields, It as judgeModelNameSchema, Nt as fakeJudgeDataSchema, Rt as sharedInputFields, b as resolveModelGateway, ct as parseProbe, lt as probeScript, st as awaitListening, v as gatewayScope, zt as turnsSchema } from "./config-BjjDryd5.js";
7
7
  import { y as vertexSettingsIn } from "./vertex-DypYjx71.js";
8
- import { m as redactSecrets } from "./redact-DVIaahLv.js";
9
- import { i as formatDuration, n as clip } from "./text-DTigcA64.js";
8
+ import { d as redactJson, m as redactSecrets } from "./redact-DVIaahLv.js";
9
+ import { a as plural, i as formatDuration, n as clip } from "./text-DTigcA64.js";
10
10
  import { s as cacheDir } from "./root-Dlff-zp8.js";
11
- import { ft as shellQuote } from "./src-D91Hd6hT.js";
11
+ import { pt as shellQuote } from "./src-BJCKWacl.js";
12
12
  import { t as InfraError } from "./types-C5vI3g1C.js";
13
- import { r as getRunContext, s as SUBJECT } from "./runtime-d56Gg3J5.js";
14
- import { i as debugTrace, r as debugLog } from "./attempt-Ba2QerOK.js";
15
- import { r as openRouterFallbacks } from "./openrouter-models-CWRwLX-G.js";
16
- import { i as hostModel } from "./model-Bq3G0OG1.js";
13
+ import { r as getRunContext, s as SUBJECT } from "./runtime-CNbsMJQ7.js";
14
+ import { i as debugTrace, r as debugLog } from "./attempt-CpbZ9arV.js";
15
+ import { r as openRouterFallbacks } from "./openrouter-models-CR00FWAa.js";
16
+ import { i as hostModel } from "./model-6dfKFkQd.js";
17
17
  import { t as addUsage } from "./usage-BNY1ABrU.js";
18
+ import { t as untraced } from "./docker-hwp3EucJ.js";
19
+ import { n as pageLines } from "./capture-Bvc4FtWT.js";
18
20
  import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
19
21
  import { homedir, tmpdir } from "node:os";
20
22
  import { join, posix } from "node:path";
21
23
  import { z } from "zod";
22
- import { createServer } from "node:net";
24
+ import net, { createServer } from "node:net";
23
25
  import { createHash, randomUUID } from "node:crypto";
24
26
  import { setTimeout as setTimeout$1 } from "node:timers/promises";
25
27
  import { spawn } from "node:child_process";
26
- import { crc32, deflateSync, inflateRawSync } from "node:zlib";
28
+ import http from "node:http";
29
+ import { crc32, deflateSync, inflateRawSync, inflateSync } from "node:zlib";
27
30
  import { mkdir, writeFile } from "node:fs/promises";
28
31
  import { generateText, stepCountIs, tool } from "ai";
29
32
  //#region src/contract/attempt.ts
@@ -170,10 +173,38 @@ const assertionRecordSchema = z.strictObject({
170
173
  "error"
171
174
  ]),
172
175
  soft: z.boolean(),
176
+ at: z.string().optional(),
173
177
  message: z.string().optional(),
174
178
  durationMs: z.number().optional()
175
179
  });
176
180
  const turnIndex = z.number().int("must be a turn index, from 0").min(0, "must be a turn index, from 0").describe("The turn's index, from 0.");
181
+ const captureConsoleMessageSchema = z.strictObject({
182
+ level: z.enum([
183
+ "error",
184
+ "warning",
185
+ "info",
186
+ "log",
187
+ "debug"
188
+ ]),
189
+ text: z.string(),
190
+ source: z.string(),
191
+ url: z.string().optional()
192
+ });
193
+ const captureFailedRequestSchema = z.strictObject({
194
+ method: z.string(),
195
+ url: z.string(),
196
+ status: z.number().optional(),
197
+ error: z.string().optional()
198
+ });
199
+ const capturePageFields = {
200
+ status: z.number().optional(),
201
+ finalUrl: z.string(),
202
+ console: z.array(captureConsoleMessageSchema),
203
+ errors: z.array(z.string()),
204
+ failedRequests: z.array(captureFailedRequestSchema),
205
+ dropped: z.number().optional()
206
+ };
207
+ z.strictObject(capturePageFields);
177
208
  const captureSchema = z.strictObject({
178
209
  name: z.string(),
179
210
  port: z.number(),
@@ -183,7 +214,8 @@ const captureSchema = z.strictObject({
183
214
  path: z.string(),
184
215
  sha256: z.string(),
185
216
  bytes: z.number()
186
- })
217
+ }),
218
+ ...capturePageFields
187
219
  });
188
220
  const judgeArtifactRefSchema = z.strictObject({
189
221
  name: z.string(),
@@ -269,9 +301,16 @@ const judgeOnSchema = z.union([z.strictObject({ turn: turnIndex }), z.strictObje
269
301
  const judgeCallDataSchema = z.strictObject({
270
302
  model: z.union([judgeModelNameSchema, fakeJudgeDataSchema]).optional().describe("`'provider/model'`, or a fake judge with a numeric score."),
271
303
  ...judgeFields,
272
- on: judgeOnSchema.optional().describe("What to grade instead of the latest turn: an earlier turn, or a value."),
304
+ ...sharedInputFields,
305
+ contents: z.array(nonEmpty).optional(),
306
+ turns: turnsSchema.optional(),
307
+ on: judgeOnSchema.optional().describe("The turn `task`, `output`, `transcript` and `diff` read instead of the latest: an earlier turn, or a value graded as the output."),
273
308
  artifacts: z.union([z.literal(true), z.array(z.union([nonEmpty, captureSchema]))]).optional().describe("Captures this call sends the judge as image file parts: a capture `c.capture` returned, or its name, or `true` for every capture so far. The provider that cannot take files fails the call.")
274
- }).describe("`c.judge()`'s options as data: the judge options, with the model by name or a fake judge's numeric score, plus the call-only `on` and `artifacts`.");
309
+ }).describe("`c.judge()`'s options as data: the judge options, with the model by name or a fake judge's numeric score, the inputs it shows the judge (at least one turned on) and `on`.");
310
+ const callSiteSchema = z.strictObject({
311
+ file: nonEmpty.describe("The eval file: absolute, or relative to the project root."),
312
+ line: z.number().int().min(1).describe("From 1.")
313
+ }).describe("Where the eval made the call, which the record names beside the check's outcome.");
275
314
  const attemptCallSchema = z.discriminatedUnion("op", [
276
315
  z.strictObject({
277
316
  op: z.literal("send"),
@@ -287,7 +326,7 @@ const attemptCallSchema = z.discriminatedUnion("op", [
287
326
  op: z.literal("serve"),
288
327
  command: nonEmpty.describe("A shell command; `PORT` is set to the port."),
289
328
  port: portSchema.optional().describe("The container port. Omit it and reeval allocates one.")
290
- }).describe("`c.serve(command, port?)`: a background process and its port."),
329
+ }).describe("`c.serve(command, port?)`: a background process, once it listens, and its port."),
291
330
  z.strictObject({
292
331
  op: z.literal("kill"),
293
332
  handle: handleId
@@ -313,8 +352,9 @@ const attemptCallSchema = z.discriminatedUnion("op", [
313
352
  matcher: z.string().regex(/^[a-z][A-Za-z]*$/, "must be a matcher name such as toHaveFile, without \"not.\" (set not: true instead)").describe("The matcher's name, e.g. `toHaveFile` or `toBe`, without `not.`."),
314
353
  args: z.array(matcherArgSchema).describe("The matcher's arguments."),
315
354
  not: z.boolean().describe("Called as `expect(…).not`."),
316
- soft: z.boolean().describe("Called as `expect.soft`: a failure fails the attempt at its end."),
355
+ soft: z.boolean().describe("Called as `expect`: a failure fails the attempt at its end and the body carries on. False for `expect.require`, whose failure the runner raises, ending the body."),
317
356
  label: z.string().optional().describe("`expect`'s second argument."),
357
+ at: callSiteSchema.optional(),
318
358
  poll: z.strictObject({
319
359
  interval: millisSchema.optional().describe("Milliseconds between tries. Default 50."),
320
360
  timeout: millisSchema.optional().describe("Milliseconds before it fails with the last error. Default 1000.")
@@ -343,12 +383,13 @@ const attemptCallSchema = z.discriminatedUnion("op", [
343
383
  label: z.string().optional().describe("The check's own name, the same whether it passes or fails."),
344
384
  status: z.enum(["pass", "fail"]),
345
385
  message: z.string().optional().describe("Why it failed."),
346
- soft: z.boolean().optional().describe("A failure fails the attempt at its end, as `expect.soft`. Default false.")
386
+ soft: z.boolean().optional().describe("The body carried on after a failure, as after an `expect`. Default false: the check raised, as Python's `assert` does."),
387
+ at: callSiteSchema.optional()
347
388
  }).describe("A check the eval's own language made, such as Python's `assert`: recorded as a matcher call is, so it's named, counted and compared the same. Replies with its record."),
348
389
  z.strictObject({
349
390
  op: z.literal("judge"),
350
- question: nonEmpty.describe("A yes/no question about the graded turn or value."),
351
- options: judgeCallDataSchema.optional()
391
+ question: nonEmpty.describe("A yes/no question about the inputs `options` turns on."),
392
+ options: judgeCallDataSchema
352
393
  }).describe("`c.judge(question, options)`. Replies with its verdict, or a judge failure when the provider failed.")
353
394
  ]).describe("One call an eval's body makes on its attempt. The sandbox and the agent session start on the first call that needs them; there's no call to start them.");
354
395
  const attemptErrorSchema = z.looseObject({
@@ -379,7 +420,7 @@ z.union([
379
420
  ok: z.literal(true),
380
421
  op: z.literal("serve"),
381
422
  handle: handleId,
382
- port: z.number().describe("The container port it was given.")
423
+ port: z.number().describe("The container port it listens on.")
383
424
  }),
384
425
  z.strictObject({
385
426
  id,
@@ -472,7 +513,7 @@ z.strictObject({
472
513
  }).describe("reeval to the runner: the attempt was aborted, as `c.signal` aborts. Calls in flight reply with errors; the runner should stop the body and send `done`.");
473
514
  z.strictObject({
474
515
  type: z.literal("done"),
475
- error: attemptErrorSchema.optional().describe("What the body threw, if it threw. A failed `expect` (not soft) is one.")
516
+ error: attemptErrorSchema.optional().describe("What the body threw, if it threw. A failed `expect.require` is one.")
476
517
  }).describe("The runner to reeval: the eval's body has finished. The attempt's checks and record follow.");
477
518
  const CALL_KEYS = Object.fromEntries(attemptCallSchema.options.map((o) => [o.shape.op.value, Object.keys(o.shape)]));
478
519
  const OTHER_KEYS = [
@@ -486,7 +527,6 @@ const OTHER_KEYS = [
486
527
  "matchedBy",
487
528
  "answers",
488
529
  ...Object.keys(judgeCallDataSchema.shape),
489
- ...Object.keys(judgeStateSchema.shape),
490
530
  ...Object.keys(captureSchema.shape),
491
531
  ...Object.keys(captureCallDataSchema.shape),
492
532
  "port",
@@ -529,6 +569,170 @@ function fakeInstructions(steps) {
529
569
  function fakeInstructionSteps(value) {
530
570
  if (typeof value === "object" && value !== null && FAKE_INSTRUCTIONS in value) return value[FAKE_INSTRUCTIONS];
531
571
  }
572
+ const TEXT_MAX = 2e3;
573
+ const params = (msg) => msg.params ?? msg;
574
+ function argText(arg) {
575
+ if (arg.type === "string") return String(arg.value);
576
+ if (arg.unserializableValue !== void 0) return arg.unserializableValue;
577
+ if (arg.description !== void 0) return arg.description;
578
+ if (arg.value !== void 0) return JSON.stringify(arg.value);
579
+ return String(arg.type ?? "");
580
+ }
581
+ /** `console.*` call types that print nothing of their own. */
582
+ const SILENT = /* @__PURE__ */ new Set([
583
+ "clear",
584
+ "endGroup",
585
+ "profile",
586
+ "profileEnd"
587
+ ]);
588
+ function consoleLevel(type) {
589
+ if (type === "error" || type === "assert") return "error";
590
+ if (type === "warning") return "warning";
591
+ if (type === "info") return "info";
592
+ if (type === "debug") return "debug";
593
+ return "log";
594
+ }
595
+ function logLevel(level) {
596
+ if (level === "error" || level === "warning" || level === "info") return level;
597
+ return "debug";
598
+ }
599
+ /**
600
+ * Collects what the tab reports from the moment it's called: its console, its page errors
601
+ * (uncaught exceptions and unhandled rejections), its failed requests and its main frame's
602
+ * document status. Needs `Runtime.enable`, `Log.enable` and `Network.enable` sent after it, and
603
+ * the main frame's id once it's known.
604
+ */
605
+ function watchPage(cdp) {
606
+ const consoleLines = [];
607
+ const errors = [];
608
+ const droppedErrors = /* @__PURE__ */ new Set();
609
+ const failed = [];
610
+ const requests = /* @__PURE__ */ new Map();
611
+ let frame;
612
+ let status;
613
+ let frameUrl = "";
614
+ let dropped = 0;
615
+ const keep = (list, item, count = list.length) => {
616
+ if (count < 100) list.push(item);
617
+ else dropped++;
618
+ return count < 100;
619
+ };
620
+ const held = (text) => clip(text, TEXT_MAX * 2);
621
+ const keepConsole = (line) => keep(consoleLines, line, consoleLines.filter((l) => l.level === line.level).length);
622
+ cdp.on("Runtime.consoleAPICalled", (msg) => {
623
+ const p = params(msg);
624
+ const type = p.type ?? "log";
625
+ if (SILENT.has(type)) return;
626
+ const url = p.stackTrace?.callFrames?.[0]?.url;
627
+ keepConsole({
628
+ level: consoleLevel(type),
629
+ text: held((p.args ?? []).map(argText).join(" ")),
630
+ source: "console-api",
631
+ ...url ? { url } : {}
632
+ });
633
+ });
634
+ cdp.on("Log.entryAdded", (msg) => {
635
+ const { entry } = params(msg);
636
+ if (!entry) return;
637
+ keepConsole({
638
+ level: logLevel(entry.level),
639
+ text: held(entry.text ?? ""),
640
+ source: entry.source ?? "other",
641
+ ...entry.url ? { url: entry.url } : {}
642
+ });
643
+ });
644
+ cdp.on("Runtime.exceptionThrown", (msg) => {
645
+ const { exceptionDetails: d } = params(msg);
646
+ if (!d) return;
647
+ const thrown = d.exception ? argText(d.exception) : "";
648
+ const text = held(thrown ? `${d.text ?? "Uncaught"} ${thrown}`.trim() : d.text ?? "Uncaught");
649
+ if (!keep(errors, {
650
+ id: d.exceptionId,
651
+ text
652
+ }) && d.exceptionId !== void 0) droppedErrors.add(d.exceptionId);
653
+ });
654
+ cdp.on("Runtime.exceptionRevoked", (msg) => {
655
+ const { exceptionId: id } = params(msg);
656
+ if (id === void 0) return;
657
+ if (droppedErrors.delete(id)) {
658
+ dropped--;
659
+ return;
660
+ }
661
+ const at = errors.findIndex((e) => e.id === id);
662
+ if (at >= 0) errors.splice(at, 1);
663
+ });
664
+ cdp.on("Page.frameNavigated", (msg) => {
665
+ const { frame: f } = params(msg);
666
+ if (f?.id && f.id === frame && f.url) frameUrl = f.url + (f.urlFragment ?? "");
667
+ });
668
+ cdp.on("Page.navigatedWithinDocument", (msg) => {
669
+ const p = params(msg);
670
+ if (p.frameId && p.frameId === frame && p.url) frameUrl = p.url;
671
+ });
672
+ cdp.on("Network.requestWillBeSent", (msg) => {
673
+ const p = params(msg);
674
+ if (p.requestId && p.request?.url) requests.set(p.requestId, {
675
+ url: p.request.url,
676
+ method: p.request.method ?? "GET"
677
+ });
678
+ });
679
+ cdp.on("Network.responseReceived", (msg) => {
680
+ const p = params(msg);
681
+ const code = p.response?.status ?? 0;
682
+ if (p.type === "Document" && p.frameId && p.frameId === frame) status = p.response?.status;
683
+ if (code < 400) return;
684
+ const req = p.requestId ? requests.get(p.requestId) : void 0;
685
+ keep(failed, {
686
+ method: req?.method ?? "GET",
687
+ url: p.response?.url ?? req?.url ?? "",
688
+ status: code
689
+ });
690
+ });
691
+ cdp.on("Network.loadingFinished", (msg) => {
692
+ const { requestId } = params(msg);
693
+ if (requestId) requests.delete(requestId);
694
+ });
695
+ cdp.on("Network.loadingFailed", (msg) => {
696
+ const p = params(msg);
697
+ const req = p.requestId ? requests.get(p.requestId) : void 0;
698
+ if (p.requestId) requests.delete(p.requestId);
699
+ if (p.canceled || !req) return;
700
+ const cors = p.corsErrorStatus?.corsError;
701
+ const error = `${p.errorText ?? "failed"}${cors ? ` (CORS ${cors})` : ""}`;
702
+ keep(failed, {
703
+ method: req.method,
704
+ url: req.url,
705
+ error
706
+ });
707
+ });
708
+ return {
709
+ mainFrame(id) {
710
+ frame = id;
711
+ },
712
+ facts(url, origin) {
713
+ url ||= frameUrl;
714
+ const base = origin.replace(/\/$/, "");
715
+ const at = base ? new RegExp(`${base.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?!\\d)`, "g") : void 0;
716
+ const local = (text) => clip(at ? text.replace(at, "") : text, TEXT_MAX);
717
+ const path = (u) => local(u) || "/";
718
+ return {
719
+ ...status ? { status } : {},
720
+ finalUrl: path(url),
721
+ console: consoleLines.map((l) => ({
722
+ ...l,
723
+ text: local(l.text),
724
+ ...l.url ? { url: path(l.url) } : {}
725
+ })),
726
+ errors: errors.map((e) => local(e.text)),
727
+ failedRequests: failed.map((r) => ({
728
+ ...r,
729
+ url: path(r.url)
730
+ })),
731
+ ...dropped ? { dropped } : {}
732
+ };
733
+ }
734
+ };
735
+ }
532
736
  //#endregion
533
737
  //#region src/capture/zip.ts
534
738
  const LOCAL = 67324752;
@@ -595,27 +799,88 @@ function mapPagePoint(x, y, window, page, scrollY) {
595
799
  dy: nextY - scrollY
596
800
  };
597
801
  }
802
+ const KEYS = {
803
+ Tab: {
804
+ key: "Tab",
805
+ code: "Tab",
806
+ windowsVirtualKeyCode: 9
807
+ },
808
+ Enter: {
809
+ key: "Enter",
810
+ code: "Enter",
811
+ windowsVirtualKeyCode: 13,
812
+ text: "\r"
813
+ },
814
+ Escape: {
815
+ key: "Escape",
816
+ code: "Escape",
817
+ windowsVirtualKeyCode: 27
818
+ },
819
+ Backspace: {
820
+ key: "Backspace",
821
+ code: "Backspace",
822
+ windowsVirtualKeyCode: 8
823
+ },
824
+ Space: {
825
+ key: " ",
826
+ code: "Space",
827
+ windowsVirtualKeyCode: 32,
828
+ text: " "
829
+ },
830
+ ArrowUp: {
831
+ key: "ArrowUp",
832
+ code: "ArrowUp",
833
+ windowsVirtualKeyCode: 38
834
+ },
835
+ ArrowDown: {
836
+ key: "ArrowDown",
837
+ code: "ArrowDown",
838
+ windowsVirtualKeyCode: 40
839
+ },
840
+ ArrowLeft: {
841
+ key: "ArrowLeft",
842
+ code: "ArrowLeft",
843
+ windowsVirtualKeyCode: 37
844
+ },
845
+ ArrowRight: {
846
+ key: "ArrowRight",
847
+ code: "ArrowRight",
848
+ windowsVirtualKeyCode: 39
849
+ }
850
+ };
851
+ /** `CaptureKey` names, for the driver's tool schema. */
852
+ const CAPTURE_KEYS = Object.keys(KEYS);
853
+ async function pressKey(cdp, event) {
854
+ await cdp.send("Input.dispatchKeyEvent", {
855
+ type: "keyDown",
856
+ ...event
857
+ });
858
+ const { text: _text, commands: _commands, ...up } = event;
859
+ await cdp.send("Input.dispatchKeyEvent", {
860
+ type: "keyUp",
861
+ ...up
862
+ });
863
+ }
598
864
  async function dispatchInput(cdp, action, window, page, scrollY = 0) {
599
865
  if (action.type === "done") return scrollY;
600
866
  if (action.type === "type") {
601
- await cdp.send("Input.insertText", { text: action.text });
867
+ if (action.replace) {
868
+ await pressKey(cdp, {
869
+ key: "a",
870
+ code: "KeyA",
871
+ windowsVirtualKeyCode: 65,
872
+ modifiers: 2,
873
+ commands: ["selectAll"]
874
+ });
875
+ if (!action.text) await pressKey(cdp, KEYS.Backspace);
876
+ }
877
+ if (action.text) await cdp.send("Input.insertText", { text: action.text });
602
878
  return scrollY;
603
879
  }
604
880
  if (action.type === "key") {
605
- const key = action.key;
606
- const vk = key === "Tab" ? 9 : 13;
607
- await cdp.send("Input.dispatchKeyEvent", {
608
- type: "keyDown",
609
- key,
610
- code: key,
611
- windowsVirtualKeyCode: vk
612
- });
613
- await cdp.send("Input.dispatchKeyEvent", {
614
- type: "keyUp",
615
- key,
616
- code: key,
617
- windowsVirtualKeyCode: vk
618
- });
881
+ const event = KEYS[action.key];
882
+ if (!event) throw new Error(`c.capture() can't press ${JSON.stringify(action.key)}: use one of ${CAPTURE_KEYS.join(", ")}`);
883
+ await pressKey(cdp, event);
619
884
  return scrollY;
620
885
  }
621
886
  if (action.type === "scroll") {
@@ -747,6 +1012,10 @@ function listenForCaptureWait(cdp, wait) {
747
1012
  };
748
1013
  }
749
1014
  const QUIET_IN_FLIGHT = 2;
1015
+ const FRESH_REQUEST_MS = 2e3;
1016
+ /** How long a screenshot waits, in tries, for a page that has no layout yet. */
1017
+ const SCREENSHOT_TRIES = 20;
1018
+ const SCREENSHOT_RETRY_MS = 100;
750
1019
  function requestId(msg) {
751
1020
  const body = msg;
752
1021
  return (body.params ?? body).requestId;
@@ -757,12 +1026,12 @@ function requestId(msg) {
757
1026
  * never comes again. As in Chromium, every request that starts or ends restarts the quiet window.
758
1027
  */
759
1028
  function trackNetwork(cdp, quietMs = 500) {
760
- const inFlight = /* @__PURE__ */ new Set();
1029
+ const inFlight = /* @__PURE__ */ new Map();
761
1030
  let changed;
762
1031
  cdp.on("Network.requestWillBeSent", (msg) => {
763
1032
  const id = requestId(msg);
764
1033
  if (id === void 0) return;
765
- inFlight.add(id);
1034
+ inFlight.set(id, Date.now());
766
1035
  changed?.();
767
1036
  });
768
1037
  const end = (msg) => {
@@ -772,29 +1041,37 @@ function trackNetwork(cdp, quietMs = 500) {
772
1041
  };
773
1042
  cdp.on("Network.loadingFinished", end);
774
1043
  cdp.on("Network.loadingFailed", end);
775
- return { quiet(timeoutMs) {
776
- return new Promise((resolve, reject) => {
777
- let settle;
778
- const stop = () => {
779
- clearTimeout(timer);
780
- clearTimeout(settle);
781
- changed = void 0;
782
- };
783
- const timer = setTimeout(() => {
784
- stop();
785
- reject(captureWaitTimeout("idle"));
786
- }, timeoutMs);
787
- changed = () => {
788
- clearTimeout(settle);
789
- if (inFlight.size > QUIET_IN_FLIGHT) return;
790
- settle = setTimeout(() => {
1044
+ return {
1045
+ /** A request started in the last `FRESH_REQUEST_MS` is still in flight; a long poll is not fresh. */
1046
+ fresh() {
1047
+ const since = Date.now() - FRESH_REQUEST_MS;
1048
+ for (const started of inFlight.values()) if (started >= since) return true;
1049
+ return false;
1050
+ },
1051
+ quiet(timeoutMs) {
1052
+ return new Promise((resolve, reject) => {
1053
+ let settle;
1054
+ const stop = () => {
1055
+ clearTimeout(timer);
1056
+ clearTimeout(settle);
1057
+ changed = void 0;
1058
+ };
1059
+ const timer = setTimeout(() => {
791
1060
  stop();
792
- resolve();
793
- }, quietMs);
794
- };
795
- changed();
796
- });
797
- } };
1061
+ reject(captureWaitTimeout("idle"));
1062
+ }, timeoutMs);
1063
+ changed = () => {
1064
+ clearTimeout(settle);
1065
+ if (inFlight.size > QUIET_IN_FLIGHT) return;
1066
+ settle = setTimeout(() => {
1067
+ stop();
1068
+ resolve();
1069
+ }, quietMs);
1070
+ };
1071
+ changed();
1072
+ });
1073
+ }
1074
+ };
798
1075
  }
799
1076
  /** Pinned Chrome for Testing `chrome-headless-shell` build. */
800
1077
  const CHROME_VERSION = "140.0.7339.185";
@@ -896,25 +1173,36 @@ async function ensureChrome(cacheRoot, options = { cacheLabel: cacheRoot }) {
896
1173
  }
897
1174
  return bin;
898
1175
  }
1176
+ /** The least time a CDP call gets, so the page facts still come after a screenshot near the deadline. */
1177
+ const CDP_CALL_MIN_MS = 1e3;
1178
+ /**
1179
+ * A CDP connection whose every call settles: with the reply, at `callMs()` (at least
1180
+ * `CDP_CALL_MIN_MS`), or when the socket closes. A blocked browser can't hold a capture past its
1181
+ * budget.
1182
+ */
899
1183
  var Cdp = class {
900
1184
  #ws;
901
1185
  #next = 0;
902
1186
  #pending = /* @__PURE__ */ new Map();
903
1187
  #events = /* @__PURE__ */ new Map();
1188
+ #closed;
1189
+ callMs = () => 3e4;
904
1190
  constructor(ws) {
905
1191
  this.#ws = ws;
906
1192
  ws.addEventListener("message", (ev) => {
907
1193
  const msg = JSON.parse(String(ev.data));
908
1194
  if (msg.id !== void 0) {
909
- const p = this.#pending.get(msg.id);
910
- if (!p) return;
911
- this.#pending.delete(msg.id);
912
- if (msg.error) p.reject(new Error(msg.error.message));
913
- else p.resolve(msg.result);
1195
+ this.#pending.get(msg.id)?.settle(msg);
914
1196
  return;
915
1197
  }
916
1198
  if (msg.method) for (const fn of this.#events.get(msg.method) ?? []) fn(msg);
917
1199
  });
1200
+ ws.addEventListener("close", () => this.#fail(/* @__PURE__ */ new Error("the browser closed its DevTools connection")));
1201
+ ws.addEventListener("error", () => this.#fail(/* @__PURE__ */ new Error("the browser closed its DevTools connection")));
1202
+ }
1203
+ #fail(error) {
1204
+ this.#closed ??= error;
1205
+ for (const p of [...this.#pending.values()]) p.reject(this.#closed);
918
1206
  }
919
1207
  on(method, fn) {
920
1208
  const list = this.#events.get(method) ?? [];
@@ -922,18 +1210,38 @@ var Cdp = class {
922
1210
  this.#events.set(method, list);
923
1211
  }
924
1212
  send(method, params) {
1213
+ if (this.#closed) return Promise.reject(this.#closed);
925
1214
  const id = ++this.#next;
926
- this.#ws.send(JSON.stringify({
927
- id,
928
- method,
929
- params
930
- }));
931
- return new Promise((resolve, reject) => this.#pending.set(id, {
932
- resolve,
933
- reject
934
- }));
1215
+ return new Promise((resolve, reject) => {
1216
+ const done = () => {
1217
+ clearTimeout(timer);
1218
+ this.#pending.delete(id);
1219
+ };
1220
+ const ms = Math.max(CDP_CALL_MIN_MS, this.callMs());
1221
+ const timer = setTimeout(() => {
1222
+ done();
1223
+ reject(/* @__PURE__ */ new Error(`c.capture() timed out after ${ms}ms waiting for the browser (${method})`));
1224
+ }, ms);
1225
+ this.#pending.set(id, {
1226
+ reject: (e) => {
1227
+ done();
1228
+ reject(e);
1229
+ },
1230
+ settle: (msg) => {
1231
+ done();
1232
+ if (msg.error) reject(new Error(msg.error.message));
1233
+ else resolve(msg.result);
1234
+ }
1235
+ });
1236
+ this.#ws.send(JSON.stringify({
1237
+ id,
1238
+ method,
1239
+ params
1240
+ }));
1241
+ });
935
1242
  }
936
1243
  close() {
1244
+ this.#fail(/* @__PURE__ */ new Error("the capture closed its DevTools connection"));
937
1245
  this.#ws.close();
938
1246
  }
939
1247
  };
@@ -1049,8 +1357,17 @@ async function disposeChrome(child, userData) {
1049
1357
  } catch {}
1050
1358
  removeChromeProfile(userData);
1051
1359
  }
1360
+ /**
1361
+ * Chrome's flags for `proxy`: every request goes to it, loopback ones included, which Chrome
1362
+ * otherwise never proxies.
1363
+ */
1364
+ function proxyArgs(proxy) {
1365
+ return proxy ? [`--proxy-server=http://${proxy}`, "--proxy-bypass-list=<-loopback>"] : [];
1366
+ }
1052
1367
  /** One Chrome process, one tab. `fn` must not keep the session after it returns. */
1053
- async function withChrome(bin, window, timeoutMs, fn) {
1368
+ async function withChrome(bin, window, timeoutMs, options, fn, signal) {
1369
+ signal?.throwIfAborted();
1370
+ const deadline = Date.now() + timeoutMs;
1054
1371
  const userData = mkdtempSync(join(tmpdir(), `reeval-chrome-${process.pid}-`));
1055
1372
  const child = spawn(bin, [
1056
1373
  "--headless",
@@ -1062,18 +1379,45 @@ async function withChrome(bin, window, timeoutMs, fn) {
1062
1379
  `--window-size=${window.width},${window.height}`,
1063
1380
  `--user-data-dir=${userData}`,
1064
1381
  "--remote-debugging-port=0",
1382
+ ...proxyArgs(options.proxy),
1065
1383
  "about:blank"
1066
- ], { stdio: [
1067
- "ignore",
1068
- "ignore",
1069
- "pipe"
1070
- ] });
1384
+ ], { stdio: "ignore" });
1385
+ const kill = () => {
1386
+ if (child.exitCode === null && child.signalCode === null) child.kill("SIGKILL");
1387
+ };
1388
+ signal?.addEventListener("abort", kill, { once: true });
1071
1389
  try {
1072
1390
  const cdp = await cdpConnect(await pageDebuggerUrl(await waitForPortFile(join(userData, "DevToolsActivePort"), timeoutMs), timeoutMs), timeoutMs);
1391
+ cdp.callMs = () => deadline - Date.now();
1392
+ const abort = () => cdp.close();
1393
+ signal?.addEventListener("abort", abort, { once: true });
1073
1394
  try {
1074
1395
  await cdp.send("Page.enable");
1075
1396
  await cdp.send("Page.setLifecycleEventsEnabled", { enabled: true });
1397
+ const watched = watchPage(cdp);
1398
+ const mainFrame = (await cdp.send("Page.getFrameTree"))?.frameTree?.frame?.id;
1399
+ if (mainFrame) watched.mainFrame(mainFrame);
1400
+ await cdp.send("Runtime.enable");
1401
+ await cdp.send("Log.enable");
1076
1402
  const network = trackNetwork(cdp);
1403
+ const loading = /* @__PURE__ */ new Set();
1404
+ const frame = (msg) => {
1405
+ const body = msg;
1406
+ return (body.params ?? body).frameId ?? "";
1407
+ };
1408
+ cdp.on("Page.frameStartedLoading", (msg) => loading.add(frame(msg)));
1409
+ cdp.on("Page.frameStoppedLoading", (msg) => loading.delete(frame(msg)));
1410
+ cdp.on("Page.frameDetached", (msg) => loading.delete(frame(msg)));
1411
+ const opened = [];
1412
+ cdp.on("Page.javascriptDialogOpening", (msg) => {
1413
+ const body = msg;
1414
+ const { type = "dialog", message = "", defaultPrompt = "" } = body.params ?? body;
1415
+ opened.push(`${type} ${JSON.stringify(message.slice(0, 60))}`);
1416
+ cdp.send("Page.handleJavaScriptDialog", {
1417
+ accept: true,
1418
+ promptText: defaultPrompt
1419
+ }).catch(() => {});
1420
+ });
1077
1421
  await cdp.send("Network.enable");
1078
1422
  await cdp.send("Emulation.setDeviceMetricsOverride", {
1079
1423
  width: window.width,
@@ -1082,7 +1426,7 @@ async function withChrome(bin, window, timeoutMs, fn) {
1082
1426
  mobile: false
1083
1427
  });
1084
1428
  let scrollY = 0;
1085
- return await fn({
1429
+ const run = fn({
1086
1430
  cdp,
1087
1431
  window,
1088
1432
  async navigate(url) {
@@ -1096,21 +1440,52 @@ async function withChrome(bin, window, timeoutMs, fn) {
1096
1440
  return waitForPage(cdp, wait, waitMs, nav);
1097
1441
  },
1098
1442
  async screenshot(fullPage) {
1099
- const shot = await cdp.send("Page.captureScreenshot", {
1100
- format: "png",
1101
- captureBeyondViewport: fullPage
1102
- });
1103
- if (!shot.data) throw new Error("Page.captureScreenshot returned no data");
1104
- return Buffer.from(shot.data, "base64");
1443
+ for (let tries = 1;; tries++) try {
1444
+ const shot = await cdp.send("Page.captureScreenshot", {
1445
+ format: "png",
1446
+ captureBeyondViewport: fullPage
1447
+ });
1448
+ if (!shot.data) throw new Error("Page.captureScreenshot returned no data");
1449
+ return Buffer.from(shot.data, "base64");
1450
+ } catch (error) {
1451
+ if (!/Cannot take screenshot with 0 (width|height)/.test(String(error)) || tries >= SCREENSHOT_TRIES) throw error;
1452
+ await new Promise((resolve) => setTimeout(resolve, SCREENSHOT_RETRY_MS));
1453
+ }
1105
1454
  },
1106
1455
  async dispatch(action, page) {
1107
1456
  scrollY = await dispatchInput(cdp, action, window, page, scrollY);
1457
+ },
1458
+ busy() {
1459
+ return loading.size > 0 || network.fresh();
1460
+ },
1461
+ dialogs() {
1462
+ return opened.splice(0);
1463
+ },
1464
+ async page(origin) {
1465
+ const history = await cdp.send("Page.getNavigationHistory").catch(() => void 0);
1466
+ const url = history?.entries?.[history.currentIndex ?? -1]?.url ?? "";
1467
+ return watched.facts(url, origin);
1108
1468
  }
1109
1469
  });
1470
+ if (!signal) return await run;
1471
+ run.catch(() => {});
1472
+ let stop = () => {};
1473
+ const canceled = new Promise((_, reject) => {
1474
+ stop = () => reject(signal.reason);
1475
+ if (signal.aborted) stop();
1476
+ else signal.addEventListener("abort", stop, { once: true });
1477
+ });
1478
+ try {
1479
+ return await Promise.race([run, canceled]);
1480
+ } finally {
1481
+ signal.removeEventListener("abort", stop);
1482
+ }
1110
1483
  } finally {
1484
+ signal?.removeEventListener("abort", abort);
1111
1485
  cdp.close();
1112
1486
  }
1113
1487
  } finally {
1488
+ signal?.removeEventListener("abort", kill);
1114
1489
  await disposeChrome(child, userData);
1115
1490
  }
1116
1491
  }
@@ -1128,22 +1503,35 @@ async function navigateAndWait(session, url, wait, timeoutMs) {
1128
1503
  }
1129
1504
  //#endregion
1130
1505
  //#region src/capture/poll.ts
1131
- /** Any HTTP response, including 404, means something accepted the connection. */
1506
+ /** A dev server still starting (Nuxt's loading page) or a proxy whose upstream isn't up yet. */
1507
+ const STARTING = /* @__PURE__ */ new Set([
1508
+ 502,
1509
+ 503,
1510
+ 504
1511
+ ]);
1512
+ /**
1513
+ * Any HTTP response, including 404, means something accepted the connection. A 502, 503 or 504
1514
+ * is polled past until the deadline, and then taken as it is.
1515
+ */
1132
1516
  async function pollHttp(url, timeoutMs, signal) {
1133
1517
  const deadline = Date.now() + timeoutMs;
1134
1518
  let last;
1519
+ let answered = false;
1135
1520
  while (Date.now() < deadline) {
1136
1521
  signal?.throwIfAborted();
1137
1522
  const left = Math.max(1, deadline - Date.now());
1138
1523
  try {
1139
- await fetch(url, { signal: AbortSignal.any([AbortSignal.timeout(Math.min(1e3, left)), ...signal ? [signal] : []]) });
1140
- return;
1524
+ const res = await fetch(url, { signal: AbortSignal.any([AbortSignal.timeout(Math.min(1e3, left)), ...signal ? [signal] : []]) });
1525
+ await res.body?.cancel();
1526
+ if (!STARTING.has(res.status)) return;
1527
+ answered = true;
1141
1528
  } catch (error) {
1142
1529
  last = error;
1143
1530
  if (signal?.aborted) throw error;
1144
- await new Promise((r) => setTimeout(r, 100));
1145
1531
  }
1532
+ await new Promise((r) => setTimeout(r, 100));
1146
1533
  }
1534
+ if (answered) return;
1147
1535
  throw last instanceof Error ? last : /* @__PURE__ */ new Error("poll timed out");
1148
1536
  }
1149
1537
  //#endregion
@@ -1330,15 +1718,467 @@ function pageUrl(origin, path) {
1330
1718
  const rel = path.startsWith("/") ? path.slice(1) : path;
1331
1719
  return new URL(rel, base).href;
1332
1720
  }
1333
- /** Internal settle between moves; not an authored wait. */
1334
- const SETTLE_MS = 80;
1335
- function remainingOrThrow(remaining, detail) {
1336
- const left = remaining();
1337
- if (left <= 0) throw new Error(`c.capture() timed out on ${detail}`);
1721
+ //#endregion
1722
+ //#region src/capture/loopback.ts
1723
+ /**
1724
+ * `localhost`, `*.localhost`, `127.0.0.0/8`, `::1`, their IPv4-mapped IPv6 form and the
1725
+ * unspecified addresses, with or without a trailing dot: the names a page means the sandbox by.
1726
+ */
1727
+ function isLoopbackHost(host) {
1728
+ const h = host.replace(/^\[|\]$/g, "").replace(/\.$/, "").toLowerCase();
1729
+ if (h === "localhost" || h.endsWith(".localhost")) return true;
1730
+ if (net.isIPv4(h)) return h.startsWith("127.") || h === "0.0.0.0";
1731
+ if (!net.isIPv6(h)) return false;
1732
+ if (h === "::1" || h === "::") return true;
1733
+ const mapped = /^::ffff:(?:(\d+\.\d+\.\d+\.\d+)|([0-9a-f]{1,4}):[0-9a-f]{1,4})$/.exec(h);
1734
+ if (!mapped) return false;
1735
+ return mapped[1] ? mapped[1].startsWith("127.") : Number.parseInt(mapped[2] ?? "", 16) >> 8 === 127;
1736
+ }
1737
+ function authority(value) {
1738
+ if (!URL.canParse(`http://${value}`)) return void 0;
1739
+ const url = new URL(`http://${value}`);
1740
+ return {
1741
+ host: url.hostname.replace(/^\[|\]$/g, ""),
1742
+ port: Number(url.port) || 80
1743
+ };
1744
+ }
1745
+ /** `rawHeaders` without the ones meant for the proxy, in the flat form `http.request` takes. */
1746
+ function forwarded(raw) {
1747
+ const out = [];
1748
+ for (let i = 0; i < raw.length; i += 2) {
1749
+ const name = raw[i] ?? "";
1750
+ if (!/^proxy-/i.test(name)) out.push(name, raw[i + 1] ?? "");
1751
+ }
1752
+ return out;
1753
+ }
1754
+ /**
1755
+ * The capture browser's HTTP proxy, on the host's loopback. Chrome sends it every request
1756
+ * (`--proxy-bypass-list=<-loopback>`): one for a loopback address goes into the sandbox, to that
1757
+ * port through its tunnel, opened on first use; everything else goes out as it would without a
1758
+ * proxy, so the page reaches the internet as before.
1759
+ *
1760
+ * A plain-HTTP request that gets no response (nothing listens on that sandbox port) drops the
1761
+ * page's connection rather than answering, so the page's fetch fails as a refused one does. A
1762
+ * CONNECT to such a port is answered and then closed: the tunnel accepts before the sandbox side
1763
+ * connects, and Chrome fails that request too.
1764
+ */
1765
+ async function startLoopbackProxy(options) {
1766
+ const direct = new Set(options.direct);
1767
+ const reached = [];
1768
+ const tunnels = /* @__PURE__ */ new Map();
1769
+ const live = /* @__PURE__ */ new Set();
1770
+ let closed = false;
1771
+ const track = (s) => {
1772
+ live.add(s);
1773
+ s.once("close", () => live.delete(s));
1774
+ return s;
1775
+ };
1776
+ const answered = (route) => {
1777
+ if (route.sandbox !== void 0 && !reached.includes(route.sandbox)) reached.push(route.sandbox);
1778
+ };
1779
+ const resolve = async (to) => {
1780
+ if (!isLoopbackHost(to.host) || direct.has(to.port)) return to;
1781
+ let open = tunnels.get(to.port);
1782
+ if (!open) {
1783
+ open = options.tunnel(to.port).then((url) => {
1784
+ const at = new URL(url);
1785
+ direct.add(Number(at.port));
1786
+ return {
1787
+ host: at.hostname,
1788
+ port: Number(at.port)
1789
+ };
1790
+ });
1791
+ open.catch(() => tunnels.delete(to.port));
1792
+ tunnels.set(to.port, open);
1793
+ }
1794
+ return {
1795
+ ...await open,
1796
+ sandbox: to.port
1797
+ };
1798
+ };
1799
+ const connect = async (to) => {
1800
+ const route = await resolve(to);
1801
+ if (closed) throw new Error("the capture proxy closed");
1802
+ return new Promise((ok, fail) => {
1803
+ const socket = track(net.connect({
1804
+ host: route.host,
1805
+ port: route.port,
1806
+ allowHalfOpen: true
1807
+ }));
1808
+ socket.once("connect", () => {
1809
+ socket.off("error", fail);
1810
+ ok({
1811
+ socket,
1812
+ route
1813
+ });
1814
+ });
1815
+ socket.once("error", fail);
1816
+ });
1817
+ };
1818
+ /** Joins the page's connection to the upstream one, unless the page left while it opened. */
1819
+ const join = (client, socket, route) => {
1820
+ if (closed || client.destroyed) {
1821
+ socket.destroy();
1822
+ return false;
1823
+ }
1824
+ socket.once("data", () => answered(route));
1825
+ socket.on("error", () => client.destroy());
1826
+ client.on("error", () => socket.destroy());
1827
+ client.once("close", () => socket.destroy());
1828
+ socket.once("close", () => client.destroy());
1829
+ client.pipe(socket);
1830
+ socket.pipe(client);
1831
+ return true;
1832
+ };
1833
+ const server = http.createServer((req, res) => {
1834
+ const url = URL.canParse(req.url ?? "") ? new URL(req.url ?? "") : void 0;
1835
+ const to = url && authority(url.host);
1836
+ if (!url || !to || url.protocol !== "http:") {
1837
+ res.writeHead(400).end();
1838
+ return;
1839
+ }
1840
+ const drop = () => req.socket.destroy();
1841
+ resolve(to).then((route) => {
1842
+ if (closed || res.destroyed) return drop();
1843
+ const upstream = http.request({
1844
+ host: route.host,
1845
+ port: route.port,
1846
+ method: req.method,
1847
+ path: `${url.pathname}${url.search}`,
1848
+ headers: forwarded(req.rawHeaders),
1849
+ agent: false
1850
+ }, (up) => {
1851
+ answered(route);
1852
+ res.writeHead(up.statusCode ?? 502, up.statusMessage, up.rawHeaders);
1853
+ up.pipe(res);
1854
+ });
1855
+ track(upstream);
1856
+ res.once("close", () => upstream.destroy());
1857
+ upstream.on("error", drop);
1858
+ req.pipe(upstream);
1859
+ }).catch(drop);
1860
+ });
1861
+ server.on("connection", track);
1862
+ server.on("connect", (req, client, early) => {
1863
+ client.on("error", () => client.destroy());
1864
+ const to = authority(req.url ?? "");
1865
+ if (!to) {
1866
+ client.end("HTTP/1.1 400 Bad Request\r\n\r\n");
1867
+ return;
1868
+ }
1869
+ connect(to).then(({ socket, route }) => {
1870
+ if (!join(client, socket, route)) return;
1871
+ client.write("HTTP/1.1 200 Connection Established\r\n\r\n");
1872
+ if (early.length) socket.write(early);
1873
+ }).catch(() => {
1874
+ if (!client.destroyed) client.end("HTTP/1.1 502 Bad Gateway\r\n\r\n");
1875
+ });
1876
+ });
1877
+ server.on("upgrade", (req, client, early) => {
1878
+ client.on("error", () => client.destroy());
1879
+ const url = URL.canParse(req.url ?? "") ? new URL(req.url ?? "") : void 0;
1880
+ const to = url && authority(url.host);
1881
+ if (!url || !to) {
1882
+ client.end("HTTP/1.1 400 Bad Request\r\n\r\n");
1883
+ return;
1884
+ }
1885
+ const headers = forwarded(req.rawHeaders);
1886
+ const lines = [`${req.method} ${url.pathname}${url.search} HTTP/${req.httpVersion}`];
1887
+ for (let i = 0; i < headers.length; i += 2) lines.push(`${headers[i]}: ${headers[i + 1]}`);
1888
+ connect(to).then(({ socket, route }) => {
1889
+ if (!join(client, socket, route)) return;
1890
+ socket.write(`${lines.join("\r\n")}\r\n\r\n`);
1891
+ if (early.length) socket.write(early);
1892
+ }).catch(() => {
1893
+ if (!client.destroyed) client.end("HTTP/1.1 502 Bad Gateway\r\n\r\n");
1894
+ });
1895
+ });
1896
+ await new Promise((ok, fail) => {
1897
+ server.once("error", fail);
1898
+ server.listen(0, "127.0.0.1", () => ok());
1899
+ });
1900
+ const { port } = server.address();
1901
+ return {
1902
+ address: `127.0.0.1:${port}`,
1903
+ reached,
1904
+ close: () => new Promise((ok) => {
1905
+ closed = true;
1906
+ for (const s of live) s.destroy();
1907
+ server.close(() => ok());
1908
+ })
1909
+ };
1910
+ }
1911
+ //#endregion
1912
+ //#region src/capture/png.ts
1913
+ const CHANNELS = {
1914
+ 0: 1,
1915
+ 2: 3,
1916
+ 4: 2,
1917
+ 6: 4
1918
+ };
1919
+ /** An 8-bit, non-interlaced PNG's unfiltered pixels, or undefined for any other. */
1920
+ function pixels(png) {
1921
+ if (png.length < 33 || png.readUInt32BE(12) !== 1229472850) return void 0;
1922
+ const width = png.readUInt32BE(16);
1923
+ const height = png.readUInt32BE(20);
1924
+ const channels = CHANNELS[png[25] ?? -1];
1925
+ if (png[24] !== 8 || png[28] !== 0 || !channels) return void 0;
1926
+ const idat = [];
1927
+ for (let at = 8; at + 8 <= png.length;) {
1928
+ const length = png.readUInt32BE(at);
1929
+ const type = png.toString("latin1", at + 4, at + 8);
1930
+ if (type === "IDAT") idat.push(png.subarray(at + 8, at + 8 + length));
1931
+ if (type === "IEND") break;
1932
+ at += 12 + length;
1933
+ }
1934
+ const raw = inflateSync(Buffer.concat(idat));
1935
+ const stride = width * channels;
1936
+ const data = Buffer.alloc(stride * height);
1937
+ for (let y = 0; y < height; y++) {
1938
+ const filter = raw[y * (stride + 1)];
1939
+ const line = raw.subarray(y * (stride + 1) + 1, (y + 1) * (stride + 1));
1940
+ const row = y * stride;
1941
+ for (let x = 0; x < stride; x++) {
1942
+ const a = x >= channels ? data[row + x - channels] ?? 0 : 0;
1943
+ const b = y > 0 ? data[row - stride + x] ?? 0 : 0;
1944
+ const c = x >= channels && y > 0 ? data[row - stride + x - channels] ?? 0 : 0;
1945
+ let value = line[x] ?? 0;
1946
+ if (filter === 1) value += a;
1947
+ else if (filter === 2) value += b;
1948
+ else if (filter === 3) value += a + b >> 1;
1949
+ else if (filter === 4) {
1950
+ const p = a + b - c;
1951
+ const pa = Math.abs(p - a);
1952
+ const pb = Math.abs(p - b);
1953
+ const pc = Math.abs(p - c);
1954
+ value += pa <= pb && pa <= pc ? a : pb <= pc ? b : c;
1955
+ }
1956
+ data[row + x] = value & 255;
1957
+ }
1958
+ }
1959
+ return {
1960
+ width,
1961
+ height,
1962
+ channels,
1963
+ data
1964
+ };
1965
+ }
1966
+ /** The diff samples every `SAMPLE`th pixel across and down. */
1967
+ const SAMPLE = 4;
1968
+ /** A sampled pixel differs when a channel moves more than this, so antialiasing doesn't count. */
1969
+ const CHANNEL_DELTA = 32;
1970
+ /** The share of sampled pixels that must differ: a caret at any font size is below it. */
1971
+ const VIEW_CHANGE = .002;
1972
+ /**
1973
+ * Whether `after` looks different from `before`, two viewport shots: more than `VIEW_CHANGE` of
1974
+ * the sampled pixels differ, or the size does. Bytes decide when either PNG is one this can't read.
1975
+ */
1976
+ function pageChanged(before, after) {
1977
+ if (before.equals(after)) return false;
1978
+ let a;
1979
+ let b;
1980
+ try {
1981
+ a = pixels(before);
1982
+ b = pixels(after);
1983
+ } catch {
1984
+ return true;
1985
+ }
1986
+ if (!a || !b) return true;
1987
+ if (a.width !== b.width || a.height !== b.height || a.channels !== b.channels) return true;
1988
+ const stride = a.width * a.channels;
1989
+ let sampled = 0;
1990
+ let differ = 0;
1991
+ for (let y = 0; y < a.height; y += SAMPLE) for (let x = 0; x < a.width; x += SAMPLE) {
1992
+ sampled++;
1993
+ const at = y * stride + x * a.channels;
1994
+ for (let c = 0; c < a.channels; c++) if (Math.abs((a.data[at + c] ?? 0) - (b.data[at + c] ?? 0)) > CHANNEL_DELTA) {
1995
+ differ++;
1996
+ break;
1997
+ }
1998
+ }
1999
+ return differ > sampled * VIEW_CHANGE;
2000
+ }
2001
+ //#endregion
2002
+ //#region src/capture/page-state.ts
2003
+ const FIELDS = [
2004
+ "url",
2005
+ "focus",
2006
+ "value",
2007
+ "form",
2008
+ "text",
2009
+ "nodes",
2010
+ "view"
2011
+ ];
2012
+ /** Page text in the prompt is capped: the page controls it. */
2013
+ const TEXT_CAP = 60;
2014
+ const PROBE = `(() => {
2015
+ const hash = (s) => { let h = 2166136261; for (let i = 0; i < s.length; i++) { h ^= s.charCodeAt(i); h = Math.imul(h, 16777619) } return (h >>> 0).toString(36) }
2016
+ const clip = (s) => String(s ?? '').replace(/\\s+/g, ' ').trim().slice(0, ${TEXT_CAP})
2017
+ let a = document.activeElement
2018
+ while (a && a.shadowRoot && a.shadowRoot.activeElement) a = a.shadowRoot.activeElement
2019
+ if (a === document.body || a === document.documentElement) a = null
2020
+ const tag = a ? a.tagName.toLowerCase() : ''
2021
+ const type = a && tag === 'input' ? (a.type || 'text') : ''
2022
+ const label = a ? clip(a.getAttribute('aria-label') || (a.labels && a.labels[0] && a.labels[0].innerText) || a.getAttribute('placeholder') || a.getAttribute('name') || a.id || (tag === 'button' || tag === 'a' ? a.innerText : '')) : ''
2023
+ const toggles = type === 'checkbox' || type === 'radio'
2024
+ const value = !a ? null
2025
+ : toggles ? (a.checked ? 'checked' : 'unchecked')
2026
+ : tag === 'select' ? clip(a.selectedOptions && a.selectedOptions[0] ? a.selectedOptions[0].text : '')
2027
+ : type === 'password' ? a.value.length + ' characters'
2028
+ : tag === 'input' || tag === 'textarea' ? clip(a.value)
2029
+ : a.isContentEditable ? clip(a.innerText)
2030
+ : null
2031
+ const buttons = ['button', 'submit', 'reset', 'image', 'file', 'range', 'color', 'hidden']
2032
+ const editable = !!a && ((tag === 'input' && !toggles && !buttons.includes(type)) || tag === 'textarea' || a.isContentEditable)
2033
+ const controls = Array.from(document.querySelectorAll('input,select,textarea'))
2034
+ .map((e) => (e.type === 'checkbox' || e.type === 'radio' ? (e.checked ? '1' : '0') : e.value))
2035
+ return {
2036
+ url: location.href,
2037
+ focus: a ? tag + (type ? '[' + type + ']' : '') + (label ? ' ' + JSON.stringify(label) : '') : null,
2038
+ value,
2039
+ editable,
2040
+ form: hash(controls.join('\\u0001')),
2041
+ text: hash(document.body ? document.body.innerText : ''),
2042
+ nodes: document.getElementsByTagName('*').length,
2043
+ }
2044
+ })()`;
2045
+ /** Reads the page. A field it can't read is left out. */
2046
+ async function probePage(session) {
2047
+ const read = await session.cdp.send("Runtime.evaluate", {
2048
+ expression: PROBE,
2049
+ returnByValue: true
2050
+ }).catch(() => void 0);
2051
+ const view = await session.screenshot(false).catch(() => void 0);
2052
+ return {
2053
+ ...read?.result?.value ?? {},
2054
+ ...view ? { view } : {}
2055
+ };
2056
+ }
2057
+ /** The fields that differ between two states, ignoring those `volatile` names. */
2058
+ function differing(before, after, volatile = /* @__PURE__ */ new Set()) {
2059
+ return FIELDS.filter((field) => {
2060
+ if (volatile.has(field)) return false;
2061
+ const a = before[field];
2062
+ const b = after[field];
2063
+ if (a === void 0 || b === void 0) return a !== b;
2064
+ if (field === "view") return pageChanged(a, b);
2065
+ return a !== b;
2066
+ });
2067
+ }
2068
+ /** The page's path, without its query or fragment, and capped. */
2069
+ function pathOf(url, origin) {
2070
+ if (!url) return void 0;
2071
+ const local = url.startsWith(`${origin}/`) ? url.slice(origin.length) : url;
2072
+ const cut = local.search(/[?#]/);
2073
+ const path = cut === -1 ? local : `${local.slice(0, cut)}${local[cut] === "?" ? "?…" : ""}`;
2074
+ return path.length > TEXT_CAP ? `${path.slice(0, TEXT_CAP)}…` : path;
2075
+ }
2076
+ /** What changed between two states, in the words the model reads. */
2077
+ function describeChange(after, fields, origin) {
2078
+ const notes = [];
2079
+ const has = (f) => fields.includes(f);
2080
+ if (has("url")) notes.push(`now at ${pathOf(after.url, origin) ?? "a page reeval could not read"}`);
2081
+ if (has("focus")) notes.push(after.focus ? `focus moved to ${after.focus}` : "nothing is focused now");
2082
+ if (after.focus && (has("value") || has("focus") && after.value)) notes.push(`${after.focus} holds ${JSON.stringify(after.value ?? "")}`);
2083
+ else if (has("form")) notes.push("a form control changed");
2084
+ if (has("text") || has("nodes")) notes.push("the page's content changed");
2085
+ else if (has("view") && !notes.length) notes.push("the page looks different");
2086
+ return notes;
2087
+ }
2088
+ /**
2089
+ * Waits for the page to stop changing after a move: two reads `SETTLE_POLL_MS` apart that agree,
2090
+ * with no navigation or fresh request in flight. At `SETTLE_CAP_MS` it gives up and adds the
2091
+ * fields still changing to `volatile`, so a clock or an animation doesn't hold up every move.
2092
+ */
2093
+ async function settledState(session, volatile, left) {
2094
+ const start = Date.now();
2095
+ const sleep = () => new Promise((resolve) => setTimeout(resolve, Math.max(0, Math.min(100, left()))));
2096
+ let prev = await probePage(session);
2097
+ for (;;) {
2098
+ if (left() <= 0) return prev;
2099
+ await sleep();
2100
+ const next = await probePage(session);
2101
+ const moving = differing(prev, next, volatile);
2102
+ if (!moving.length && !session.busy()) return next;
2103
+ if (Date.now() - start >= 3e3 || left() <= 0) {
2104
+ for (const field of moving) if (field !== "url" && field !== "focus") volatile.add(field);
2105
+ return next;
2106
+ }
2107
+ prev = next;
2108
+ }
2109
+ }
2110
+ /** How long a settled page is watched for a change still to come after a move that ends its instruction. */
2111
+ const LATE_CHANGE_MS = 1e3;
2112
+ /**
2113
+ * The page once a change that starts within `LATE_CHANGE_MS` of `settled` has settled too, or
2114
+ * `settled`: an auth callback's redirect or a save's message often comes a beat after the
2115
+ * request that made it, and the next instruction's first look must not be the page before it.
2116
+ */
2117
+ async function lateSettledState(session, settled, volatile, left) {
2118
+ const until = Date.now() + Math.min(LATE_CHANGE_MS, left());
2119
+ while (Date.now() < until) {
2120
+ await new Promise((resolve) => setTimeout(resolve, Math.min(100, left())));
2121
+ if (differing(settled, await probePage(session), volatile).length || session.busy()) return settledState(session, volatile, left);
2122
+ }
2123
+ return settled;
2124
+ }
2125
+ /** Points within this many CSS pixels of each other are the same move. */
2126
+ const SAME_POINT = 8;
2127
+ /** Model turns in a row thrown away because the page changed while the model chose. */
2128
+ const STALE_ASKS = 2;
2129
+ /** How long a stopped instruction's screenshot may take once the budget is spent. */
2130
+ const STOPPED_SHOT_MS = 5e3;
2131
+ function timedOut(clock) {
2132
+ const { step } = clock;
2133
+ const where = step ? ` (instruction ${step.index} of ${step.total}, after ${plural(step.moves, "move")})` : "";
2134
+ return /* @__PURE__ */ new Error(`c.capture() timed out on ${clock.detail}${where}: it used all of its ${formatDuration(clock.budgetMs)} timeout${stallReason(clock)}. Raise timeout on the call or capture.timeout.`);
2135
+ }
2136
+ /** Why the instruction's moves never finished it, as a clause; empty when nothing says. */
2137
+ function stallReason({ step, trail }) {
2138
+ if (!step || !trail) return "";
2139
+ const paths = [trail.start, ...trail.made.map((m) => m.after)].flatMap((s) => s ? [pathOf(s.url, trail.origin) ?? s.url] : []);
2140
+ for (let k = 2; k < paths.length; k++) {
2141
+ const path = paths[k];
2142
+ const first = paths.indexOf(path);
2143
+ if (first < k && paths.slice(first + 1, k).some((p) => p !== path)) return `; its moves left ${path} and came back to it, so the model may be undoing what it did`;
2144
+ }
2145
+ if (trail.choosing) return `; the model was still choosing move ${step.moves + 1}`;
2146
+ return "";
2147
+ }
2148
+ function check(clock) {
2149
+ const left = clock.remaining();
2150
+ if (left <= 0) throw timedOut(clock);
1338
2151
  return left;
1339
2152
  }
1340
- async function settle() {
1341
- await new Promise((resolve) => setTimeout(resolve, SETTLE_MS));
2153
+ /** `work()`, or the timeout once the budget runs out first: a page can hold any CDP call open. */
2154
+ function race(clock, work) {
2155
+ const left = clock.remaining();
2156
+ if (left <= 0) return Promise.reject(timedOut(clock));
2157
+ let timer;
2158
+ const out = new Promise((_, reject) => {
2159
+ timer = setTimeout(() => reject(timedOut(clock)), left);
2160
+ });
2161
+ return Promise.race([work(), out]).finally(() => clearTimeout(timer));
2162
+ }
2163
+ /** `session` with every call raced against the clock, and its own waits' timeouts named as the budget's. */
2164
+ function onClock(session, clock) {
2165
+ const own = (error) => {
2166
+ throw /c\.capture\(\) timed out/.test(String(error)) ? timedOut(clock) : error;
2167
+ };
2168
+ return {
2169
+ window: session.window,
2170
+ cdp: {
2171
+ on: (method, fn) => session.cdp.on(method, fn),
2172
+ send: (method, params) => race(clock, () => session.cdp.send(method, params))
2173
+ },
2174
+ navigate: (url) => race(clock, () => session.navigate(url)),
2175
+ wait: (wait, ms, nav) => race(clock, () => session.wait(wait, ms, nav).catch(own)),
2176
+ screenshot: (fullPage) => race(clock, () => session.screenshot(fullPage)),
2177
+ dispatch: (action, page) => race(clock, () => session.dispatch(action, page)),
2178
+ busy: () => session.busy(),
2179
+ dialogs: () => session.dialogs(),
2180
+ page: (origin) => session.page(origin)
2181
+ };
1342
2182
  }
1343
2183
  function asUsage(value) {
1344
2184
  return {
@@ -1349,38 +2189,82 @@ function asUsage(value) {
1349
2189
  reasoning: null
1350
2190
  };
1351
2191
  }
2192
+ const finite = (value) => {
2193
+ const n = typeof value === "number" ? value : typeof value === "string" && value.trim() ? Number(value) : NaN;
2194
+ return Number.isFinite(n) ? n : void 0;
2195
+ };
2196
+ /**
2197
+ * The point at `x`/`y`, or both packed into one as `"217,163"`, which models sometimes send. A
2198
+ * half missing from the packed form (`"217,"`) is read from its own field.
2199
+ */
2200
+ function point(input, x, y) {
2201
+ const packed = typeof input[x] === "string" ? input[x].split(",") : [];
2202
+ const px = packed.length === 2 ? finite(packed[0]) ?? finite(input[x]) : finite(input[x]);
2203
+ const py = packed.length === 2 ? finite(packed[1]) ?? finite(input[y]) : finite(input[y]);
2204
+ return px === void 0 || py === void 0 ? void 0 : [px, py];
2205
+ }
2206
+ /**
2207
+ * The action a tool call asks for, or why it can't be made. Only a click or a key press can
2208
+ * complete its goal: offered on `type`, models mark the text as final, not the goal.
2209
+ */
1352
2210
  function actionFromTool(name, input) {
1353
2211
  if (name === "done") return { type: "done" };
1354
- if (name === "type") return {
1355
- type: "type",
1356
- text: String(input.text ?? "")
1357
- };
1358
- if (name === "key") return {
1359
- type: "key",
1360
- key: input.key === "Tab" ? "Tab" : "Enter"
1361
- };
1362
- if (name === "scroll") return {
1363
- type: "scroll",
1364
- x: Number(input.x),
1365
- y: Number(input.y),
1366
- dy: Number(input.dy)
1367
- };
1368
- if (name === "drag") return {
1369
- type: "drag",
1370
- x: Number(input.x),
1371
- y: Number(input.y),
1372
- toX: Number(input.toX),
1373
- toY: Number(input.toY)
1374
- };
2212
+ const move = moveFromTool(name, input);
2213
+ if ("refused" in move || move.type !== "click" && move.type !== "key") return move;
2214
+ return input.completesGoal === true || input.completesGoal === "true" ? {
2215
+ ...move,
2216
+ final: true
2217
+ } : move;
2218
+ }
2219
+ function moveFromTool(name, input) {
2220
+ if (name === "type") {
2221
+ if (typeof input.text !== "string") return { refused: "it had no text" };
2222
+ return {
2223
+ type: "type",
2224
+ text: input.text,
2225
+ ...input.replace === true ? { replace: true } : {}
2226
+ };
2227
+ }
2228
+ if (name === "key") {
2229
+ const key = CAPTURE_KEYS.find((k) => k === input.key);
2230
+ if (!key) return { refused: `the key is not one of ${CAPTURE_KEYS.join(", ")}` };
2231
+ return {
2232
+ type: "key",
2233
+ key
2234
+ };
2235
+ }
2236
+ const at = point(input, "x", "y");
2237
+ if (!at) return { refused: "it had no numeric x and y" };
2238
+ const [x, y] = at;
2239
+ if (name === "scroll") {
2240
+ const dy = finite(input.dy);
2241
+ return dy === void 0 ? { refused: "it had no numeric dy" } : {
2242
+ type: "scroll",
2243
+ x,
2244
+ y,
2245
+ dy
2246
+ };
2247
+ }
2248
+ if (name === "drag") {
2249
+ const to = point(input, "toX", "toY");
2250
+ if (!to) return { refused: "it had no numeric toX and toY" };
2251
+ return {
2252
+ type: "drag",
2253
+ x,
2254
+ y,
2255
+ toX: to[0],
2256
+ toY: to[1]
2257
+ };
2258
+ }
1375
2259
  if (name === "hover") return {
1376
2260
  type: "hover",
1377
- x: Number(input.x),
1378
- y: Number(input.y)
2261
+ x,
2262
+ y
1379
2263
  };
1380
2264
  return {
1381
2265
  type: "click",
1382
- x: Number(input.x),
1383
- y: Number(input.y)
2266
+ x,
2267
+ y
1384
2268
  };
1385
2269
  }
1386
2270
  /**
@@ -1411,22 +2295,30 @@ function scalePoints(action, page) {
1411
2295
  default: return action;
1412
2296
  }
1413
2297
  }
1414
- const systemPrompt = (normalized) => `You drive a web page from screenshots. Call one tool. ${normalized ? `Coordinates are normalized to 0-${NORMALIZED} on the full-page image: x and toX across its width, y and toY down its height. A scroll's dy is in CSS pixels.` : "Coordinates are CSS pixels on the full-page image."} Call done when the goal is met. Do not mention CDP.`;
2298
+ const systemPrompt = (normalized) => `You drive a web page from screenshots. Call one tool. ${normalized ? `Coordinates are normalized to 0-${NORMALIZED} on the full-page image: x and toX across its width, y and toY down its height. A scroll's dy is in CSS pixels.` : "Coordinates are CSS pixels on the full-page image."} Each turn you get the goal, what has focus, your earlier moves with what each did, and a fresh screenshot. Click a field before typing into it, and do not repeat a move that already worked. A dropdown (select) never opens in the screenshot: click it once, press ArrowDown or ArrowUp until it shows the option you want, then press Enter to choose it. On a click or key press after which nothing of the goal is left, such as the click that submits a form you have filled in, set completesGoal to true: reeval makes it and goes on to the next goal without asking you again. Call done when the goal is met. Do not mention CDP.`;
2299
+ const completesGoal = z.boolean().optional().describe("True only on the last move the goal needs: nothing of the goal is left after it.");
1415
2300
  const useTools = {
1416
2301
  click: tool({
1417
2302
  description: "Click at (x, y) on the screenshot.",
1418
2303
  inputSchema: z.object({
1419
2304
  x: z.number(),
1420
- y: z.number()
2305
+ y: z.number(),
2306
+ completesGoal
1421
2307
  })
1422
2308
  }),
1423
2309
  type: tool({
1424
- description: "Insert text into the focused field. Click the field first.",
1425
- inputSchema: z.object({ text: z.string() })
2310
+ description: "Insert text into the focused field. Click the field first. Set replace to true to replace what the field already holds instead of adding to it.",
2311
+ inputSchema: z.object({
2312
+ text: z.string(),
2313
+ replace: z.boolean().optional()
2314
+ })
1426
2315
  }),
1427
2316
  key: tool({
1428
- description: "Press Tab or Enter.",
1429
- inputSchema: z.object({ key: z.enum(["Tab", "Enter"]) })
2317
+ description: "Press a key in the focused element. In a dropdown (select), click it, press ArrowDown or ArrowUp to the option, then Enter to choose it. Space toggles a focused checkbox.",
2318
+ inputSchema: z.object({
2319
+ key: z.enum(CAPTURE_KEYS),
2320
+ completesGoal
2321
+ })
1430
2322
  }),
1431
2323
  scroll: tool({
1432
2324
  description: "Wheel-scroll a constrained scroller at (x, y).",
@@ -1457,12 +2349,20 @@ const useTools = {
1457
2349
  inputSchema: z.object({})
1458
2350
  })
1459
2351
  };
1460
- async function runFakeScript(session, script, goal, remaining) {
2352
+ async function runFakeScript(session, script, step, clock, volatile) {
2353
+ const goal = step.goal;
1461
2354
  let moves = 0;
1462
2355
  for (const action of script) {
1463
- remainingOrThrow(remaining, JSON.stringify(goal));
1464
- if (action.type === "done") return moves;
1465
- if (moves >= 32) throw new Error(`c.capture() instruction ${JSON.stringify(goal)} did not finish in 32 moves`);
2356
+ clock.step = {
2357
+ ...step,
2358
+ moves
2359
+ };
2360
+ check(clock);
2361
+ if (action.type === "done") return {
2362
+ moves,
2363
+ ended: "done"
2364
+ };
2365
+ if (moves >= 32) throw unfinished(step, clock);
1466
2366
  const page = pngSize(await session.screenshot(true));
1467
2367
  debugTrace("check", "capture-use", {
1468
2368
  goal,
@@ -1471,97 +2371,345 @@ async function runFakeScript(session, script, goal, remaining) {
1471
2371
  }, `capture use ${action.type}`);
1472
2372
  await session.dispatch(action, page);
1473
2373
  moves++;
1474
- await settle();
2374
+ const settled = await settledState(session, volatile, clock.remaining);
2375
+ if (action.final) {
2376
+ await lateSettledState(session, settled, volatile, clock.remaining);
2377
+ return {
2378
+ moves,
2379
+ ended: "final"
2380
+ };
2381
+ }
2382
+ }
2383
+ clock.step = {
2384
+ ...step,
2385
+ moves
2386
+ };
2387
+ while (clock.remaining() > 0) await new Promise((resolve) => setTimeout(resolve, Math.min(100, clock.remaining())));
2388
+ throw timedOut(clock);
2389
+ }
2390
+ const unfinished = (step, clock) => /* @__PURE__ */ new Error(`c.capture() instruction ${step.index} of ${step.total} ${JSON.stringify(step.goal)} did not finish in 32 moves${stallReason(clock)}`);
2391
+ const grid = (n) => Math.round(n / SAME_POINT);
2392
+ function moveKey({ final: _, ...action }) {
2393
+ switch (action.type) {
2394
+ case "click":
2395
+ case "hover": return JSON.stringify([
2396
+ action.type,
2397
+ grid(action.x),
2398
+ grid(action.y)
2399
+ ]);
2400
+ case "scroll": return JSON.stringify([
2401
+ action.type,
2402
+ grid(action.x),
2403
+ grid(action.y),
2404
+ action.dy
2405
+ ]);
2406
+ case "drag": return JSON.stringify([
2407
+ action.type,
2408
+ grid(action.x),
2409
+ grid(action.y),
2410
+ grid(action.toX),
2411
+ grid(action.toY)
2412
+ ]);
2413
+ default: return JSON.stringify(action);
1475
2414
  }
1476
- while (remaining() > 0) await settle();
1477
- throw new Error(`c.capture() timed out on ${JSON.stringify(goal)}`);
1478
2415
  }
1479
- async function runModelGoal(session, goal, modelName, remaining, loadModel) {
2416
+ /** Sets what `move` did, from its page before and `after`. */
2417
+ function settleMove(move, after, origin, volatile) {
2418
+ move.after = after;
2419
+ const fields = move.before ? differing(move.before, after, volatile) : [];
2420
+ const notes = move.before ? describeChange(after, fields, origin) : [];
2421
+ for (const dialog of move.dialogs) notes.push(`the page opened ${dialog}, which reeval accepted`);
2422
+ move.changed = fields.length > 0 || move.dialogs.length > 0;
2423
+ move.effect = notes.length ? notes.join("; ") : "nothing changed";
2424
+ }
2425
+ /** The same move made, unchanged, at the end of `moves`: a refused move ends the run. */
2426
+ function stuckMoves(moves) {
2427
+ const last = moves.at(-1);
2428
+ let n = 0;
2429
+ for (let i = moves.length - 1; i >= 0; i--) {
2430
+ const move = moves[i];
2431
+ if (!move || move.refused || move.key !== last?.key || move.changed !== false) break;
2432
+ n++;
2433
+ }
2434
+ return n;
2435
+ }
2436
+ /** What the model reads about the page now: where it is and what has focus. */
2437
+ function stateLines(now, origin, volatile) {
2438
+ const lines = [];
2439
+ const path = pathOf(now.url, origin);
2440
+ if (path) lines.push(`The page is at ${path}.`);
2441
+ if (now.focus === null) lines.push("Nothing is focused.");
2442
+ else if (now.focus) {
2443
+ lines.push(`Focused: ${now.focus}${now.value ? `, holding ${JSON.stringify(now.value)}` : ""}.`);
2444
+ if (now.focus.startsWith("select")) lines.push("A dropdown never opens in the screenshot: press ArrowDown or ArrowUp to the option, then Enter; it holds the new option only after Enter.");
2445
+ }
2446
+ if (volatile.has("text") || volatile.has("nodes") || volatile.has("view")) lines.push("Parts of the page keep changing on their own, so the effects below leave them out.");
2447
+ return lines;
2448
+ }
2449
+ /** The goal, where the journey is, the page now, and the moves so far with what each did. */
2450
+ function goalPrompt(step, earlier, now, moves, origin, volatile = /* @__PURE__ */ new Set()) {
2451
+ const lines = [`Goal: ${step.goal}`];
2452
+ if (step.total > 1) lines.push(`This is instruction ${step.index} of ${step.total}.${earlier.length ? ` Already done: ${earlier.map((g) => JSON.stringify(g)).join(", ")}.` : ""}`);
2453
+ lines.push(...stateLines(now, origin, volatile));
2454
+ if (!moves.length) {
2455
+ lines.push("You have made no moves for this goal yet.");
2456
+ return lines.join("\n");
2457
+ }
2458
+ lines.push("Your moves for this goal so far, oldest first. The screenshot shows the page now.");
2459
+ moves.forEach((m, i) => {
2460
+ const effect = m.refused ? `not made: ${m.refused}` : m.effect ?? "";
2461
+ lines.push(`${i + 1}. ${m.said}${effect ? ` → ${effect}` : ""}`);
2462
+ });
2463
+ if (stuckMoves(moves) >= 2) lines.push("Your last moves repeated the same action and nothing changed. Do not repeat it: if the goal is met, call done; otherwise try a different action.");
2464
+ const from = pathOf(moves.find((m) => m.before)?.before?.url, origin);
2465
+ const at = pathOf(now.url, origin);
2466
+ if (from && at && from !== at) lines.push(`Your moves took the page from ${from} to ${at}. If they did what the goal asks, call done: a goal's "once", "if" or "when" says where it starts, so do not undo them to get back there.`);
2467
+ return lines.join("\n");
2468
+ }
2469
+ async function runModelGoal(session, { step, earlier, origin, modelName, clock, volatile, pristine, loadModel }) {
2470
+ const { goal } = step;
1480
2471
  const model = await loadModel(modelName);
1481
2472
  const normalized = normalizesPoints(modelName);
1482
- let moves = 0;
2473
+ const made = [];
2474
+ let dispatched = 0;
1483
2475
  let usage = null;
1484
- while (moves < 32) {
1485
- remainingOrThrow(remaining, JSON.stringify(goal));
2476
+ let now = await probePage(session);
2477
+ let stale = 0;
2478
+ const trail = {
2479
+ origin,
2480
+ start: now,
2481
+ made,
2482
+ choosing: false
2483
+ };
2484
+ clock.trail = trail;
2485
+ while (made.length < 32) {
2486
+ clock.step = {
2487
+ ...step,
2488
+ moves: dispatched
2489
+ };
2490
+ check(clock);
2491
+ const last = made.at(-1);
2492
+ if (last?.after) {
2493
+ const late = await probePage(session);
2494
+ last.dialogs.push(...session.dialogs());
2495
+ if (differing(last.after, late, volatile).length || last.dialogs.length) settleMove(last, late, origin, volatile);
2496
+ now = late;
2497
+ }
2498
+ if (stuckMoves(made) >= 5) throw new Error(`c.capture() instruction ${step.index} of ${step.total} ${JSON.stringify(goal)} repeated ${last?.said} 5 times and nothing changed`);
1486
2499
  const ephemeral = await session.screenshot(true);
1487
2500
  const page = pngSize(ephemeral);
1488
2501
  debugTrace("check", "capture-shot", {
1489
2502
  goal,
1490
2503
  bytes: ephemeral.length,
1491
- page
2504
+ page,
2505
+ url: now.url
1492
2506
  }, false);
1493
- let chosen;
1494
- const result = await generateText({
2507
+ const signal = AbortSignal.timeout(check(clock));
2508
+ const asked = Date.now();
2509
+ trail.choosing = true;
2510
+ const result = await race(clock, () => generateText({
1495
2511
  model,
1496
2512
  system: systemPrompt(normalized),
1497
2513
  messages: [{
1498
2514
  role: "user",
1499
2515
  content: [{
1500
2516
  type: "text",
1501
- text: goal
2517
+ text: goalPrompt(step, earlier, now, made, origin, volatile)
1502
2518
  }, {
1503
- type: "image",
1504
- image: ephemeral
2519
+ type: "file",
2520
+ mediaType: "image/png",
2521
+ data: ephemeral
1505
2522
  }]
1506
2523
  }],
1507
2524
  tools: useTools,
1508
2525
  toolChoice: "required",
1509
2526
  stopWhen: stepCountIs(1),
1510
- abortSignal: AbortSignal.timeout(remainingOrThrow(remaining, JSON.stringify(goal)))
2527
+ abortSignal: signal
2528
+ })).catch((error) => {
2529
+ throw signal.aborted ? timedOut(clock) : error;
1511
2530
  });
2531
+ trail.choosing = false;
1512
2532
  usage = addUsage(usage, asUsage(result.usage ?? {}));
1513
2533
  const call = result.toolCalls?.[0];
1514
- if (call) chosen = actionFromTool(call.toolName, call.input ?? {});
1515
- if (chosen && normalized) chosen = scalePoints(chosen, page);
1516
- if (!chosen) throw new Error(`c.capture() instruction ${JSON.stringify(goal)} returned no action`);
1517
- debugLog("check", `capture use ${chosen.type} on ${JSON.stringify(goal)}`);
2534
+ if (!call) throw new Error(`c.capture() instruction ${JSON.stringify(goal)} returned no action`);
2535
+ const input = call.input ?? {};
2536
+ const said = `${call.toolName} ${JSON.stringify(input)}`;
2537
+ const fresh = await probePage(session);
2538
+ const opened = session.dialogs();
2539
+ const moved = differing(now, fresh, volatile);
2540
+ if (!last && pristine) {
2541
+ for (const f of moved) if (f !== "url" && f !== "focus") volatile.add(f);
2542
+ } else if ((moved.length || opened.length) && stale < STALE_ASKS) {
2543
+ stale++;
2544
+ if (last && !last.refused) {
2545
+ last.dialogs.push(...opened);
2546
+ settleMove(last, fresh, origin, volatile);
2547
+ }
2548
+ now = fresh;
2549
+ debugLog("check", `capture use ${said} on ${JSON.stringify(goal)} not made: the page changed while the model chose`);
2550
+ continue;
2551
+ }
2552
+ stale = 0;
2553
+ let asks = actionFromTool(call.toolName, input);
2554
+ if (!("refused" in asks) && asks.type === "type" && now.editable === false) asks = { refused: "nothing that takes text is focused; click a field first" };
2555
+ const logged = !("refused" in asks) && asks.type === "type" && now.focus?.includes("[password]") ? `type ${JSON.stringify({
2556
+ ...input,
2557
+ text: "•".repeat(String(input.text ?? "").length)
2558
+ })}` : said;
2559
+ const latency = formatDuration(Date.now() - asked);
2560
+ if ("refused" in asks) {
2561
+ debugLog("check", `capture use ${logged} on ${JSON.stringify(goal)} not made: ${asks.refused} · ${latency}`);
2562
+ made.push({
2563
+ said,
2564
+ key: said,
2565
+ refused: asks.refused,
2566
+ dialogs: []
2567
+ });
2568
+ continue;
2569
+ }
2570
+ const chosen = normalized ? scalePoints(asks, page) : asks;
2571
+ debugLog("check", `capture use ${logged} on ${JSON.stringify(goal)} · ${latency}`);
1518
2572
  debugTrace("check", "capture-use", {
1519
2573
  goal,
1520
2574
  action: chosen,
1521
2575
  page
1522
2576
  }, `capture use ${chosen.type}`);
1523
- if (chosen.type === "done") return {
1524
- moves,
1525
- usage
2577
+ if (asks.type === "done") return {
2578
+ moves: dispatched,
2579
+ usage,
2580
+ ended: "done"
1526
2581
  };
2582
+ const move = {
2583
+ said,
2584
+ key: moveKey(asks),
2585
+ before: now,
2586
+ dialogs: []
2587
+ };
2588
+ made.push(move);
1527
2589
  await session.dispatch(chosen, page);
1528
- moves++;
1529
- await settle();
2590
+ dispatched++;
2591
+ now = await settledState(session, volatile, clock.remaining);
2592
+ move.dialogs.push(...session.dialogs());
2593
+ settleMove(move, now, origin, volatile);
2594
+ if (asks.final && move.changed) {
2595
+ await lateSettledState(session, now, volatile, clock.remaining);
2596
+ return {
2597
+ moves: dispatched,
2598
+ usage,
2599
+ ended: "final"
2600
+ };
2601
+ }
1530
2602
  }
1531
- throw new Error(`c.capture() instruction ${JSON.stringify(goal)} did not finish in 32 moves`);
2603
+ clock.step = {
2604
+ ...step,
2605
+ moves: dispatched
2606
+ };
2607
+ throw unfinished(step, clock);
2608
+ }
2609
+ /** The page once an instruction stopped the capture, unless the screenshot takes too long. */
2610
+ async function stoppedShot(raw, fullPage) {
2611
+ let timer;
2612
+ const late = new Promise((resolve) => {
2613
+ timer = setTimeout(() => resolve(void 0), STOPPED_SHOT_MS);
2614
+ });
2615
+ return Promise.race([raw.screenshot(fullPage).catch(() => void 0), late]).finally(() => clearTimeout(timer));
1532
2616
  }
1533
2617
  /**
1534
2618
  * Top-level wait, then the instruction loop, then the saved PNG.
1535
2619
  * Throws before `screenshot` when the budget or move cap is hit.
1536
2620
  */
1537
- async function runJourney(session, url, options) {
2621
+ async function runJourney(raw, url, options) {
1538
2622
  const { wait, timeoutMs, fullPage, instructions, use, remaining } = options;
1539
- remainingOrThrow(remaining, "navigate");
1540
- await navigateAndWait(session, url, wait, Math.min(timeoutMs, remaining()));
2623
+ const clock = {
2624
+ budgetMs: options.budgetMs ?? timeoutMs,
2625
+ remaining,
2626
+ detail: wait === "idle" ? "loading the page with wait: \"idle\"" : "loading the page"
2627
+ };
2628
+ const session = onClock(raw, clock);
2629
+ const origin = new URL(url).origin;
2630
+ const volatile = /* @__PURE__ */ new Set();
2631
+ check(clock);
2632
+ await navigateAndWait(session, url, wait, Math.min(timeoutMs, remaining())).catch((error) => {
2633
+ throw /c\.capture\(\) timed out/.test(String(error)) ? timedOut(clock) : error;
2634
+ });
2635
+ const goals = instructions.filter((s) => typeof s === "string");
1541
2636
  let moves = 0;
1542
2637
  let usage = null;
1543
2638
  let stringIndex = 0;
1544
- for (const step of instructions) {
1545
- if (typeof step !== "string") {
1546
- remainingOrThrow(remaining, "wait");
1547
- await session.wait(step.wait, remaining(), void 0);
2639
+ for (const instruction of instructions) {
2640
+ if (typeof instruction !== "string") {
2641
+ clock.detail = `{ wait: ${JSON.stringify(instruction.wait)} }`;
2642
+ clock.step = void 0;
2643
+ check(clock);
2644
+ await session.wait(instruction.wait, remaining(), void 0);
1548
2645
  continue;
1549
2646
  }
1550
- remainingOrThrow(remaining, JSON.stringify(step));
1551
- debugLog("check", `capture instruction ${JSON.stringify(step)}`);
1552
- if (use && "fake" in use) {
1553
- const script = use.fake[stringIndex] ?? [];
1554
- moves += await runFakeScript(session, script, step, remaining);
1555
- } else if (use && "model" in use) {
1556
- if (!options.loadModel) throw new Error("c.capture() instructions need a model loader");
1557
- const ran = await runModelGoal(session, step, use.model, remaining, options.loadModel);
1558
- moves += ran.moves;
1559
- usage = addUsage(usage, ran.usage);
2647
+ const step = {
2648
+ goal: instruction,
2649
+ index: stringIndex + 1,
2650
+ total: goals.length,
2651
+ moves: 0
2652
+ };
2653
+ clock.detail = JSON.stringify(instruction);
2654
+ clock.step = step;
2655
+ check(clock);
2656
+ debugLog("check", `capture instruction ${step.index} of ${step.total} ${JSON.stringify(instruction)}`);
2657
+ const began = Date.now();
2658
+ const shot = (png, ran) => {
2659
+ const ms = Date.now() - began;
2660
+ debugLog("check", `capture instruction ${step.index} of ${step.total} ${ran.ended} after ${plural(ran.moves, "move")} in ${formatDuration(ms)}`);
2661
+ return options.onStep?.({
2662
+ index: step.index,
2663
+ instruction,
2664
+ moves: ran.moves,
2665
+ ms,
2666
+ ended: ran.ended,
2667
+ png
2668
+ });
2669
+ };
2670
+ try {
2671
+ let ran;
2672
+ if (use && "fake" in use) ran = await runFakeScript(session, use.fake[stringIndex] ?? [], step, clock, volatile);
2673
+ else if (use && "model" in use) {
2674
+ if (!options.loadModel) throw new Error("c.capture() instructions need a model loader");
2675
+ const goal = await runModelGoal(session, {
2676
+ step,
2677
+ earlier: goals.slice(0, stringIndex),
2678
+ origin,
2679
+ modelName: use.model,
2680
+ clock,
2681
+ volatile,
2682
+ pristine: moves === 0,
2683
+ loadModel: options.loadModel
2684
+ });
2685
+ usage = addUsage(usage, goal.usage);
2686
+ ran = goal;
2687
+ }
2688
+ if (ran) {
2689
+ moves += ran.moves;
2690
+ clock.step = {
2691
+ ...step,
2692
+ moves: ran.moves
2693
+ };
2694
+ clock.trail = void 0;
2695
+ shot(await session.screenshot(fullPage), ran);
2696
+ }
2697
+ } catch (error) {
2698
+ clock.trail = void 0;
2699
+ const png = options.onStep && await stoppedShot(raw, fullPage);
2700
+ const saved = png && shot(png, {
2701
+ moves: clock.step?.moves ?? 0,
2702
+ ended: "stopped"
2703
+ });
2704
+ if (!saved || !(error instanceof Error)) throw error;
2705
+ const sentence = error.message.endsWith(".") ? error.message : `${error.message}.`;
2706
+ throw new Error(`${sentence} The page as it stopped: ${saved}.`);
1560
2707
  }
1561
2708
  stringIndex++;
1562
- await settle();
1563
2709
  }
1564
- remainingOrThrow(remaining, "screenshot");
2710
+ clock.detail = "the final screenshot";
2711
+ clock.step = void 0;
2712
+ check(clock);
1565
2713
  return {
1566
2714
  png: await session.screenshot(fullPage),
1567
2715
  moves,
@@ -1700,12 +2848,12 @@ function createChecks(a) {
1700
2848
  tries: retries + 1
1701
2849
  } : {}
1702
2850
  });
1703
- return r;
2851
+ return Object.defineProperty(r, SUBJECT, { value: command });
1704
2852
  };
1705
2853
  function exec(command, options) {
1706
2854
  const problem = options === void 0 ? void 0 : execOptionsProblem(options);
1707
2855
  if (problem) return Promise.reject(new UsageError(problem));
1708
- if (options?.background) return import("./capture-DSK8O5bA.js").then(({ startBackground }) => startBackground(a, command, "c.exec({ background: true })"));
2856
+ if (options?.background) return import("./capture-D68TiQQy.js").then(({ startBackground }) => startBackground(a, command, "c.exec({ background: true })"));
1709
2857
  const run = execForeground(command, options?.retries ?? 0, options?.interval ?? 1e3);
1710
2858
  return Object.defineProperty(run, SUBJECT, { value: command });
1711
2859
  }
@@ -1729,7 +2877,7 @@ function createChecks(a) {
1729
2877
  }
1730
2878
  //#endregion
1731
2879
  //#region src/attempt/capture.ts
1732
- const BG_LOG = "/tmp/reeval-bg.log";
2880
+ const BG_LOG = "/tmp/reeval-bg";
1733
2881
  const BG_TAG = "REEVAL_BACKGROUND";
1734
2882
  const isEvaluationModel = (model) => model.startsWith("typesafe-ai/");
1735
2883
  const languageModel = (model) => typeof model === "string" && !isEvaluationModel(model) ? model : void 0;
@@ -1779,10 +2927,14 @@ kill -9 -${group} 2>/dev/null`;
1779
2927
  }
1780
2928
  /** Starts `command` without waiting for exit. Teardown kills it if the eval didn't. */
1781
2929
  async function startBackground(a, command, who, extraEnv = {}) {
2930
+ return (await launch(a, command, who, extraEnv)).handle;
2931
+ }
2932
+ async function launch(a, command, who, extraEnv) {
1782
2933
  const box = await sandboxFor(a, who);
1783
2934
  const docker = a.spec.sandbox.kind === "docker";
1784
- const wrapped = `${docker ? "bash" : "sh"} -c ${shellQuote(command)} >${BG_LOG} 2>&1 & echo $$ $!`;
1785
2935
  const tag = randomUUID();
2936
+ const log = `${BG_LOG}-${tag.slice(0, 8)}.log`;
2937
+ const wrapped = `${docker ? "bash" : "sh"} -c ${shellQuote(command)} >${log} 2>&1 & echo $$ $!`;
1786
2938
  const r = await asCheck(a, () => box.exec(wrapped, {
1787
2939
  signal: a.signal,
1788
2940
  env: {
@@ -1798,16 +2950,53 @@ async function startBackground(a, command, who, extraEnv = {}) {
1798
2950
  const handle = { async kill() {
1799
2951
  if (killed) return;
1800
2952
  killed = true;
1801
- await box.exec(`${tree}; kill -9 ${pid} 2>/dev/null; true`).catch(() => {});
2953
+ await box.exec(`${tree}; kill -9 ${pid} 2>/dev/null; rm -f ${log}; true`).catch(() => {});
1802
2954
  } };
1803
2955
  a.backgrounds.push(handle);
1804
- return handle;
2956
+ return {
2957
+ handle,
2958
+ pid,
2959
+ log
2960
+ };
1805
2961
  }
2962
+ /**
2963
+ * Starts `command` with `PORT` set and returns once it listens: on `PORT` (or `port`), else on
2964
+ * the one port that wasn't listening before it started, which it records.
2965
+ */
1806
2966
  async function serveCommand(a, command, port) {
1807
- const bound = port ?? await allocatePort();
2967
+ const want = port ?? await allocatePort();
2968
+ const box = await sandboxFor(a, "c.serve()");
2969
+ const tables = a.spec.sandbox.kind !== "fake";
2970
+ const look = (pid) => untraced(() => box.exec(probeScript(pid, tables), { signal: a.signal })).then((r) => parseProbe(r.stdout));
2971
+ const first = await look();
2972
+ const before = new Set(first.proc ? first.listeners.map((l) => l.port) : []);
2973
+ const { handle, pid, log } = await launch(a, command, "c.serve()", { PORT: String(want) });
2974
+ const found = await awaitListening(command, {
2975
+ want,
2976
+ given: port !== void 0,
2977
+ before,
2978
+ signal: a.signal,
2979
+ probe: () => look(pid),
2980
+ output: async () => (await box.exec(`tail -c 65536 ${log} 2>/dev/null`)).stdout,
2981
+ ...tables ? { hostListening: async () => "" } : {}
2982
+ }).catch(async (error) => {
2983
+ await handle.kill();
2984
+ throw error;
2985
+ });
2986
+ const serve = {
2987
+ command,
2988
+ port: found.port,
2989
+ portEnv: want,
2990
+ detected: found.detected,
2991
+ address: found.address,
2992
+ readyMs: found.readyMs
2993
+ };
2994
+ a.record.serves = [...a.record.serves ?? [], serve];
2995
+ const at = found.address ? `${found.address} ` : "";
2996
+ debugLog("check", found.detected ? `c.serve ${command}: listening on ${at}port ${found.port}, not PORT ${want}; using ${found.port}` : `c.serve ${command}: listening on ${at}port ${found.port} after ${found.readyMs}ms`);
1808
2997
  return {
1809
- ...await startBackground(a, command, "c.serve()", { PORT: String(bound) }),
1810
- port: bound
2998
+ ...handle,
2999
+ port: found.port
1811
3000
  };
1812
3001
  }
1813
3002
  async function capturePage(a, target, call) {
@@ -1816,8 +3005,9 @@ async function capturePage(a, target, call) {
1816
3005
  const resolved = resolveCaptureCall(target, call, a.spec.capture);
1817
3006
  if (a.captureNames.has(resolved.name)) throw new Error(`c.capture() reused the name "${resolved.name}"`);
1818
3007
  const box = await sandboxFor(a, "c.capture()");
1819
- if (!box.getPortEndpoint) throw new Error("c.capture() needs a sandbox that can tunnel a port");
1820
- const { url: origin } = await box.getPortEndpoint({
3008
+ const tunnel = box.getPortEndpoint?.bind(box);
3009
+ if (!tunnel) throw new Error("c.capture() needs a sandbox that can tunnel a port");
3010
+ const { url: origin } = await tunnel({
1821
3011
  port: resolved.port,
1822
3012
  protocol: "http"
1823
3013
  });
@@ -1832,12 +3022,63 @@ async function capturePage(a, target, call) {
1832
3022
  const started = Date.now();
1833
3023
  const remaining = () => Math.max(0, resolved.timeoutMs - (Date.now() - started));
1834
3024
  let moves = 0;
1835
- const png = fake ? stubPng(resolved.name, resolved.port, resolved.path, resolved.viewport) : await shot(a, pageUrl(origin, resolved.path), {
3025
+ const camera = {
3026
+ port: resolved.port,
3027
+ path: resolved.path,
3028
+ viewport: resolved.window,
3029
+ ...resolved.fullPage ? { fullPage: true } : {}
3030
+ };
3031
+ const steps = [];
3032
+ const keepSteps = () => {
3033
+ for (const { name, png, step } of steps) {
3034
+ a.record.artifacts = a.record.artifacts ?? [];
3035
+ a.record.artifacts.push({
3036
+ name,
3037
+ mediaType: "image/png",
3038
+ sha256: sha256(png),
3039
+ bytes: png.length,
3040
+ capture: {
3041
+ ...camera,
3042
+ step
3043
+ }
3044
+ });
3045
+ a.stepBlobs.set(name, png);
3046
+ }
3047
+ };
3048
+ const url = pageUrl(origin, resolved.path);
3049
+ const { png, page } = fake ? {
3050
+ png: stubPng(resolved.name, resolved.port, resolved.path, resolved.viewport),
3051
+ page: await fetchedPage(url, origin, remaining(), a.signal)
3052
+ } : await shot(a, origin, url, {
1836
3053
  ...resolved,
1837
3054
  use
1838
3055
  }, remaining, (n) => {
1839
3056
  moves = n;
3057
+ }, (port) => tunnel({
3058
+ port,
3059
+ protocol: "http"
3060
+ }).then((t) => t.url), ({ png, ...step }) => {
3061
+ const name = `${resolved.name}.step-${step.index}`;
3062
+ steps.push({
3063
+ name,
3064
+ png,
3065
+ step: {
3066
+ of: resolved.name,
3067
+ ...step
3068
+ }
3069
+ });
3070
+ return `artifacts/${name}.png`;
3071
+ }).catch((error) => {
3072
+ keepSteps();
3073
+ throw error;
1840
3074
  });
3075
+ tracePage(resolved.name, page);
3076
+ const facts = {
3077
+ schemaVersion: 1,
3078
+ ...page
3079
+ };
3080
+ const pageJson = `${redactJson(facts, 2)}\n`;
3081
+ a.capturePages.set(resolved.name, pageJson);
1841
3082
  const digest = sha256(png);
1842
3083
  const file = {
1843
3084
  path: `artifacts/${resolved.name}.png`,
@@ -1846,15 +3087,19 @@ async function capturePage(a, target, call) {
1846
3087
  };
1847
3088
  const goals = instructionGoals(resolved.instructions);
1848
3089
  const capture = {
1849
- port: resolved.port,
1850
- path: resolved.path,
1851
- viewport: resolved.window,
1852
- ...resolved.fullPage ? { fullPage: true } : {},
3090
+ ...camera,
1853
3091
  ...goals.length ? { instructions: goals } : {},
1854
3092
  ...use ? { use: {
1855
3093
  model: "model" in use ? use.model : "fake",
1856
3094
  moves
1857
- } } : {}
3095
+ } } : {},
3096
+ ...page.status ? { status: page.status } : {},
3097
+ ...page.errors.length ? { pageErrors: page.errors.length } : {},
3098
+ page: {
3099
+ path: `artifacts/${resolved.name}.capture.json`,
3100
+ sha256: sha256(pageJson),
3101
+ bytes: Buffer.byteLength(pageJson)
3102
+ }
1858
3103
  };
1859
3104
  a.record.artifacts = a.record.artifacts ?? [];
1860
3105
  a.record.artifacts.push({
@@ -1865,31 +3110,67 @@ async function capturePage(a, target, call) {
1865
3110
  capture
1866
3111
  });
1867
3112
  a.artifactBlobs.set(resolved.name, png);
1868
- return {
3113
+ keepSteps();
3114
+ const taken = {
1869
3115
  name: resolved.name,
1870
3116
  port: resolved.port,
1871
3117
  path: resolved.path,
1872
3118
  viewport: resolved.viewport,
1873
- file
3119
+ file,
3120
+ ...structuredClone(page)
3121
+ };
3122
+ return Object.defineProperty(taken, SUBJECT, { value: resolved.name });
3123
+ }
3124
+ /** The fake sandbox has no browser: the page's status and final URL from a GET, and no more. */
3125
+ async function fetchedPage(url, origin, timeoutMs, signal) {
3126
+ const res = await fetch(url, { signal: AbortSignal.any([AbortSignal.timeout(Math.max(1, timeoutMs)), signal]) }).catch(() => void 0);
3127
+ await res?.body?.cancel();
3128
+ const base = origin.replace(/\/$/, "");
3129
+ const final = res?.url || url;
3130
+ return {
3131
+ ...res ? { status: res.status } : {},
3132
+ finalUrl: final.startsWith(base) ? final.slice(base.length) || "/" : final,
3133
+ console: [],
3134
+ errors: [],
3135
+ failedRequests: []
1874
3136
  };
1875
3137
  }
1876
- async function shot(a, url, resolved, remaining, onMoves) {
3138
+ /** The page's facts in the debug log, which `-v` prints: a summary, then a line each. */
3139
+ function tracePage(name, page) {
3140
+ const counts = [
3141
+ plural(page.errors.length, "page error"),
3142
+ plural(page.console.length, "console message"),
3143
+ plural(page.failedRequests.length, "failed request")
3144
+ ];
3145
+ const status = page.status ? ` (${page.status})` : "";
3146
+ debugLog("check", `capture "${name}" at ${page.finalUrl}${status}: ${counts.join(" · ")}`);
3147
+ for (const line of pageLines(page)) debugLog("check", `capture "${name}" ${line}`);
3148
+ }
3149
+ async function shot(a, origin, url, resolved, remaining, onMoves, tunnel, onStep) {
1877
3150
  const cache = cacheDir();
1878
- return withChrome(await ensureChrome(cache, {
3151
+ const bin = await ensureChrome(cache, {
1879
3152
  cacheLabel: cacheLabel(cache),
1880
3153
  signal: a.signal,
1881
3154
  onProgress: (p) => a.report({
1882
3155
  kind: "browser-download",
1883
3156
  ...p
1884
3157
  })
1885
- }), resolved.window, remaining(), async (session) => {
3158
+ });
3159
+ const proxy = await startLoopbackProxy({
3160
+ tunnel,
3161
+ direct: [Number(new URL(url).port)]
3162
+ });
3163
+ const browser = { proxy: proxy.address };
3164
+ return withChrome(bin, resolved.window, remaining(), browser, async (session) => {
1886
3165
  const result = await runJourney(session, url, {
1887
3166
  wait: resolved.wait,
1888
3167
  timeoutMs: remaining(),
3168
+ budgetMs: resolved.timeoutMs,
1889
3169
  fullPage: resolved.fullPage,
1890
3170
  instructions: resolved.instructions,
1891
3171
  use: resolved.use,
1892
3172
  remaining,
3173
+ onStep,
1893
3174
  loadModel: async (name) => {
1894
3175
  const { config, profile } = getRunContext();
1895
3176
  const scope = gatewayScope(config, profile);
@@ -1899,6 +3180,10 @@ async function shot(a, url, resolved, remaining, onMoves) {
1899
3180
  ...gateway === "vertex" ? { vertex: vertexSettingsIn(scope.gateways) } : {}
1900
3181
  });
1901
3182
  }
3183
+ }).catch(async (error) => {
3184
+ const page = await session.page(origin).catch(() => void 0);
3185
+ if (page) tracePage(resolved.name, page);
3186
+ throw error;
1902
3187
  });
1903
3188
  onMoves(result.moves);
1904
3189
  const spend = captureUseSpend(result);
@@ -1910,17 +3195,24 @@ async function shot(a, url, resolved, remaining, onMoves) {
1910
3195
  usage: addUsage(prev.usage, spend.usage)
1911
3196
  } : spend;
1912
3197
  }
1913
- return result.png;
3198
+ return {
3199
+ png: result.png,
3200
+ page: await session.page(origin)
3201
+ };
3202
+ }, a.signal).finally(async () => {
3203
+ await proxy.close();
3204
+ if (proxy.reached.length) debugLog("check", `c.capture ${resolved.name}: sandbox port ${proxy.reached.join(", ")} answered the page through its tunnel`);
1914
3205
  });
1915
3206
  }
1916
3207
  /** Writes PNG sidecars next to the staging eval JSON so redact never sees the bytes. */
1917
- async function writeCaptureSidecars(stagingDir, recordFile, blobs) {
3208
+ async function writeCaptureSidecars(stagingDir, recordFile, blobs, pages) {
1918
3209
  if (blobs.size === 0) return;
1919
3210
  const dir = join(stagingDir, "evals", recordFile.replace(/\.json$/, ".artifacts"));
1920
3211
  await mkdir(dir, { recursive: true });
1921
3212
  for (const [name, png] of blobs) await writeFile(join(dir, `${name}.png`), png);
3213
+ for (const [name, json] of pages) await writeFile(join(dir, `${name}.capture.json`), json);
1922
3214
  }
1923
3215
  //#endregion
1924
3216
  export { writeCaptureSidecars as a, sandboxFor as c, workspaceFiles as d, fakeInstructions as f, startBackground as i, diffSinceSnapshot as l, msysTreeKill as n, asCheck as o, attemptRequestProblem as p, serveCommand as r, createChecks as s, capturePage as t, snapshot as u };
1925
3217
 
1926
- //# sourceMappingURL=capture-BM_3100g.js.map
3218
+ //# sourceMappingURL=capture-DheNqeYS.js.map