deepline 0.3.537 → 0.3.538

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 (204) hide show
  1. package/dist/bundling-sources/sdk/src/client-core.ts +22 -5
  2. package/dist/bundling-sources/sdk/src/index.ts +2 -0
  3. package/dist/bundling-sources/sdk/src/plays/client-core.ts +18 -1
  4. package/dist/bundling-sources/sdk/src/release.ts +1 -1
  5. package/dist/bundling-sources/sdk/src/runs/watch-polling.ts +33 -4
  6. package/dist/bundling-sources/shared_libs/integration-contracts/billing-entitlements.ts +1 -1
  7. package/dist/bundling-sources/shared_libs/observability/scheduled-jobs.json +18 -0
  8. package/dist/bundling-sources/shared_libs/play-runtime/app-runtime-api.ts +1 -0
  9. package/dist/bundling-sources/shared_libs/play-runtime/baked-runner-image.ts +330 -0
  10. package/dist/bundling-sources/shared_libs/play-runtime/context.ts +717 -133
  11. package/dist/bundling-sources/shared_libs/play-runtime/coordinator-headers.ts +7 -0
  12. package/dist/bundling-sources/shared_libs/play-runtime/daytona-payload-contract.ts +22 -1
  13. package/dist/bundling-sources/shared_libs/play-runtime/durable-runs-rollout.ts +123 -0
  14. package/dist/bundling-sources/shared_libs/play-runtime/experiment-switches.ts +480 -0
  15. package/dist/bundling-sources/shared_libs/play-runtime/fixture-egress-fetch.ts +24 -16
  16. package/dist/bundling-sources/shared_libs/play-runtime/freshness-receipt-execution.ts +293 -53
  17. package/dist/bundling-sources/shared_libs/play-runtime/governor/block-reserving-budget-state-backend.ts +57 -2
  18. package/dist/bundling-sources/shared_libs/play-runtime/governor/budget-state-backend.ts +43 -0
  19. package/dist/bundling-sources/shared_libs/play-runtime/governor/governor.ts +141 -9
  20. package/dist/bundling-sources/shared_libs/play-runtime/launch-identity.ts +2 -0
  21. package/dist/bundling-sources/shared_libs/play-runtime/modal-runtime-config.ts +6 -0
  22. package/dist/bundling-sources/shared_libs/play-runtime/modal-supervisor-sidecar.ts +556 -0
  23. package/dist/bundling-sources/shared_libs/play-runtime/protocol.ts +27 -0
  24. package/dist/bundling-sources/shared_libs/play-runtime/resource-governor.ts +19 -0
  25. package/dist/bundling-sources/shared_libs/play-runtime/run-ledger.ts +54 -0
  26. package/dist/bundling-sources/shared_libs/play-runtime/run-snapshot-stream.ts +5 -0
  27. package/dist/bundling-sources/shared_libs/play-runtime/runner-app/index.ts +155 -3
  28. package/dist/bundling-sources/shared_libs/play-runtime/runner-app/relay-connection-counter.ts +103 -0
  29. package/dist/bundling-sources/shared_libs/play-runtime/runner-app/runtime-heartbeat.ts +75 -6
  30. package/dist/bundling-sources/shared_libs/play-runtime/runner-app/runtime-terminal-push.ts +59 -0
  31. package/dist/bundling-sources/shared_libs/play-runtime/runner-backends/backends/daytona-payload-transport.ts +544 -73
  32. package/dist/bundling-sources/shared_libs/play-runtime/runner-backends/backends/daytona-runtime-watchdog.ts +8 -1
  33. package/dist/bundling-sources/shared_libs/play-runtime/runner-backends/backends/detached-runner-readiness-pacing.ts +19 -1
  34. package/dist/bundling-sources/shared_libs/play-runtime/runner-backends/backends/modal.ts +2390 -110
  35. package/dist/bundling-sources/shared_libs/play-runtime/runtime-flow.ts +42 -8
  36. package/dist/bundling-sources/shared_libs/play-runtime/runtime-read-through.ts +287 -0
  37. package/dist/bundling-sources/shared_libs/play-runtime/runtime-receipt-finish.ts +606 -0
  38. package/dist/bundling-sources/shared_libs/play-runtime/runtime-receipt-writer.ts +274 -119
  39. package/dist/bundling-sources/shared_libs/play-runtime/runtime-traffic-policy.ts +54 -0
  40. package/dist/bundling-sources/shared_libs/play-runtime/runtime-work-counters.ts +10 -1
  41. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backend.ts +15 -0
  42. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/absurd-play-run-task.ts +18 -1
  43. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/absurd-pool.ts +18 -4
  44. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/absurd-runtime-worker.ts +219 -7
  45. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/absurd-shared.ts +3 -0
  46. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/early-sandbox-claim-affinity.ts +340 -0
  47. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/play-run-recovery.ts +1 -0
  48. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/postgres-progress.ts +10 -1
  49. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/postgres-rate-state.ts +623 -7
  50. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/postgres.ts +340 -5
  51. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/runner-push.ts +61 -0
  52. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/runtime-event-search.ts +2 -1
  53. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/runtime-sandbox-ledger-reconciliation.ts +269 -2
  54. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/runtime-worker-wake.ts +238 -5
  55. package/dist/bundling-sources/shared_libs/play-runtime/startup-read-tool-ids.ts +47 -0
  56. package/dist/bundling-sources/shared_libs/play-runtime/tool-call/runtime-step-receipts-adapter.ts +5 -4
  57. package/dist/bundling-sources/shared_libs/play-runtime/tool-result.ts +6 -0
  58. package/dist/bundling-sources/shared_libs/play-runtime/transient-service-error.ts +11 -0
  59. package/dist/cli/auth-4WEVLYK4.js +43 -0
  60. package/dist/cli/{auth-4SUPGDIM.mjs → auth-KYH7N7RQ.mjs} +5 -5
  61. package/dist/cli/{billing-SQU726P7.js → billing-4KTUVZ4S.js} +114 -90
  62. package/dist/cli/{billing-5JUA3ZM3.mjs → billing-WK346KAI.mjs} +31 -7
  63. package/dist/cli/{chunk-KULT6O5K.js → chunk-25RGHJQL.js} +15 -15
  64. package/dist/cli/{chunk-PJ4ZGZAN.js → chunk-34PLG5OE.js} +13 -13
  65. package/dist/cli/{chunk-37AZHCTP.mjs → chunk-35OCRH5C.mjs} +5 -5
  66. package/dist/cli/{chunk-JMHYDDBQ.js → chunk-3I5TU5XE.js} +292 -279
  67. package/dist/cli/{chunk-Y6AMMXWS.mjs → chunk-4KZF6OYY.mjs} +3 -3
  68. package/dist/cli/{chunk-BRRCWUOW.mjs → chunk-4NEOGPKD.mjs} +1 -1
  69. package/dist/cli/{chunk-NU7B5X6J.mjs → chunk-4WQR4DG4.mjs} +1 -1
  70. package/dist/cli/{chunk-AVQET3NR.js → chunk-5VSPRRTI.js} +7 -7
  71. package/dist/cli/{chunk-EEFSUCYP.mjs → chunk-6JG3MRTM.mjs} +2 -2
  72. package/dist/cli/{chunk-D77W6Q67.js → chunk-6SLQLWWL.js} +26 -26
  73. package/dist/cli/{chunk-ZUAMXZXC.mjs → chunk-7XDJKIM2.mjs} +4 -4
  74. package/dist/cli/{chunk-C37VELBR.mjs → chunk-A6E7TCK2.mjs} +5 -5
  75. package/dist/cli/{chunk-JS2SBCWD.js → chunk-ADLDU2QG.js} +6 -6
  76. package/dist/cli/{chunk-VP2K5B3R.mjs → chunk-ALARRSMZ.mjs} +3 -3
  77. package/dist/cli/{chunk-CHIGEDAE.js → chunk-B2BTBHX2.js} +3 -3
  78. package/dist/cli/{chunk-CFND2ACL.js → chunk-BBS2BLPV.js} +16 -16
  79. package/dist/cli/{chunk-QDBGPRR2.mjs → chunk-CLO562KG.mjs} +3 -1
  80. package/dist/cli/{chunk-CXTW7SRW.mjs → chunk-E7D37PLM.mjs} +8 -0
  81. package/dist/cli/{chunk-UGA5KI4A.js → chunk-ESX3H7Q7.js} +22 -22
  82. package/dist/cli/{chunk-BFHR7IC2.mjs → chunk-FJ5KPGCS.mjs} +10 -10
  83. package/dist/cli/{chunk-R7JYG6XO.mjs → chunk-FWE37N2P.mjs} +1 -1
  84. package/dist/cli/{chunk-K4WJ4BYO.mjs → chunk-ICTA5ICE.mjs} +1 -1
  85. package/dist/cli/{chunk-PU42VOBI.js → chunk-IMLFB66A.js} +3 -3
  86. package/dist/cli/{chunk-GI3V4D7K.mjs → chunk-IP3XJTWA.mjs} +2 -2
  87. package/dist/cli/{chunk-WBFOV5HT.js → chunk-JCLRSMUP.js} +5 -5
  88. package/dist/cli/{chunk-L3JVGNAT.mjs → chunk-JPEBKN3K.mjs} +2 -2
  89. package/dist/cli/{chunk-Z2PPB3IO.mjs → chunk-JTOV5RUA.mjs} +1 -1
  90. package/dist/cli/{chunk-WBTYWJLN.js → chunk-KCUQNH4K.js} +2 -2
  91. package/dist/cli/{chunk-C3XD2WHM.mjs → chunk-KNIYEZVV.mjs} +2 -2
  92. package/dist/cli/{chunk-3PMCQ3LK.js → chunk-LPTBHNGB.js} +7 -5
  93. package/dist/cli/{chunk-KH27B2HJ.mjs → chunk-M3NCXGNN.mjs} +2 -2
  94. package/dist/cli/{chunk-XDS26J7Z.js → chunk-MNBIGX6O.js} +10 -10
  95. package/dist/cli/{chunk-L6QAIZNZ.js → chunk-MYSPQUAO.js} +44 -44
  96. package/dist/cli/{chunk-W3J3LT22.mjs → chunk-N2BCPZVR.mjs} +5 -5
  97. package/dist/cli/{chunk-ZFSB7GXF.js → chunk-N5HSG4OV.js} +5 -5
  98. package/dist/cli/{chunk-2Q7VXTMS.js → chunk-O765M4LT.js} +2 -2
  99. package/dist/cli/{chunk-QPDLG7DQ.js → chunk-OOLBE6FR.js} +53 -53
  100. package/dist/cli/{chunk-IQOIT6E3.js → chunk-P3DP56HB.js} +41 -41
  101. package/dist/cli/{chunk-BGXEMTIG.js → chunk-PQKD5DMT.js} +19 -19
  102. package/dist/cli/{chunk-V5IIBG2E.mjs → chunk-T6GPTJ5F.mjs} +1 -1
  103. package/dist/cli/{chunk-GPMS6OPX.mjs → chunk-TWWYCJJW.mjs} +5 -5
  104. package/dist/cli/{chunk-ZHPSJ3LJ.mjs → chunk-UE7JHAZ2.mjs} +29 -16
  105. package/dist/cli/{chunk-VRYX2BR4.js → chunk-UEZNIPFO.js} +107 -107
  106. package/dist/cli/{chunk-AX2OAXDL.mjs → chunk-VCHV4LS3.mjs} +1 -1
  107. package/dist/cli/{chunk-NXGLJGPB.js → chunk-VIFLWQXS.js} +2 -2
  108. package/dist/cli/{chunk-6BBGUQJJ.js → chunk-XTYHZEJ2.js} +8 -0
  109. package/dist/cli/{chunk-5EOEJKSV.js → chunk-YQURGSRB.js} +1 -1
  110. package/dist/cli/{chunk-ALHKB3AE.js → chunk-YZ2ZPHWR.js} +9 -9
  111. package/dist/cli/{chunk-CGDW2CRR.mjs → chunk-ZDLDCXY3.mjs} +2 -2
  112. package/dist/cli/{chunk-RMVZTBI5.mjs → chunk-ZU6JBRV5.mjs} +4 -4
  113. package/dist/cli/command-docs.js +2 -2
  114. package/dist/cli/command-docs.mjs +1 -1
  115. package/dist/cli/{csv-HZP6QMG7.mjs → csv-2YKLUKDR.mjs} +4 -4
  116. package/dist/cli/{csv-JITYN27L.js → csv-LM7FBECI.js} +13 -13
  117. package/dist/cli/{db-QRIRTOMM.mjs → db-234UBWJH.mjs} +6 -6
  118. package/dist/cli/{db-FYSRB7O4.js → db-5PMA4IVW.js} +27 -27
  119. package/dist/cli/describe-C7GOSGYB.js +49 -0
  120. package/dist/cli/{describe-REXJVJC3.mjs → describe-W26LW5O7.mjs} +17 -17
  121. package/dist/cli/describe-core.d.mts +3 -3
  122. package/dist/cli/describe-core.d.ts +3 -3
  123. package/dist/cli/{executions-BSTF4DDS.js → executions-3S736GXX.js} +8 -8
  124. package/dist/cli/{executions-53KJAFOW.mjs → executions-GD7GXTJM.mjs} +6 -6
  125. package/dist/cli/{feedback-6MW3QOXX.js → feedback-BUWTDY4R.js} +14 -14
  126. package/dist/cli/{feedback-CWUJJF3J.mjs → feedback-GBV5HJJZ.mjs} +6 -6
  127. package/dist/cli/git-YG36MROC.js +25 -0
  128. package/dist/cli/{git-CKVZTJFS.mjs → git-YXJGTDQL.mjs} +5 -5
  129. package/dist/cli/index.js +143 -143
  130. package/dist/cli/index.mjs +42 -42
  131. package/dist/cli/{legacy-noops-USTBRFRX.mjs → legacy-noops-UJM6GBNP.mjs} +6 -6
  132. package/dist/cli/{legacy-noops-QBL4PNGB.js → legacy-noops-ZSF64V2U.js} +9 -9
  133. package/dist/cli/{monitors-6UCG5D7W.mjs → monitors-UERUQ6CT.mjs} +7 -7
  134. package/dist/cli/{monitors-VCR6SUZD.js → monitors-XQ2GNH6D.js} +100 -100
  135. package/dist/cli/{org-GSRBOJVO.mjs → org-CP4VLH6M.mjs} +6 -6
  136. package/dist/cli/{org-HPILTEG4.js → org-IF6MPVNR.js} +86 -86
  137. package/dist/cli/{play-TK7WOP5H.js → play-2NHQO5MO.js} +317 -317
  138. package/dist/cli/{play-QRTYQNG4.mjs → play-ZWDYYVPI.mjs} +17 -17
  139. package/dist/cli/{providers-OAQJWVBS.mjs → providers-2CVIATU4.mjs} +6 -6
  140. package/dist/cli/{providers-HEXNJ5QC.js → providers-LOPXQN56.js} +9 -9
  141. package/dist/cli/{quickstart-FFVHTPSP.mjs → quickstart-KVCFXLID.mjs} +4 -4
  142. package/dist/cli/{quickstart-PYZTFXZX.js → quickstart-N4WDEQZY.js} +6 -6
  143. package/dist/cli/{run-4DWJXJRO.js → run-BDT5LK3I.js} +42 -42
  144. package/dist/cli/{run-HLN6YYZH.mjs → run-RUXHYWLF.mjs} +22 -22
  145. package/dist/cli/search-ATDDVADT.js +32 -0
  146. package/dist/cli/{search-ZHRCXGEL.mjs → search-BFUTNPA5.mjs} +7 -7
  147. package/dist/cli/{search-output-2KY6PFDH.js → search-output-CTXI2QZU.js} +6 -6
  148. package/dist/cli/{search-output-N7QXACVK.mjs → search-output-WUGH4PZA.mjs} +4 -4
  149. package/dist/cli/{secrets-WXQ7XO5C.js → secrets-BZJZYFOU.js} +15 -15
  150. package/dist/cli/{secrets-FKMWBLHU.mjs → secrets-Z4XQVTC6.mjs} +6 -6
  151. package/dist/cli/sessions-HNU35AFM.js +22 -0
  152. package/dist/cli/{sessions-3YDTEDA5.mjs → sessions-VZXR3MQZ.mjs} +6 -6
  153. package/dist/cli/{settings-JTPASUZN.mjs → settings-VYEZFR4L.mjs} +12 -12
  154. package/dist/cli/{settings-67TWLQ6E.js → settings-WBYA4JMM.js} +85 -85
  155. package/dist/cli/{setup-KUERQJUX.js → setup-FS2RMSS2.js} +49 -49
  156. package/dist/cli/{setup-VYNGXUJ6.mjs → setup-H7KCI4SO.mjs} +9 -9
  157. package/dist/cli/skills-VPIURCAZ.js +36 -0
  158. package/dist/cli/{skills-KI4KYHH6.mjs → skills-YQNSJUUG.mjs} +7 -7
  159. package/dist/cli/tools-273GS5HU.js +56 -0
  160. package/dist/cli/{tools-UQDJS2RS.mjs → tools-ZF6SVEGW.mjs} +16 -16
  161. package/dist/cli/{update-7FQQUBOD.mjs → update-RKQNNJM4.mjs} +9 -9
  162. package/dist/cli/update-TOD3XY55.js +34 -0
  163. package/dist/cli/{views-JAM4MNNV.mjs → views-JHHOTWDU.mjs} +6 -6
  164. package/dist/cli/{views-MJK7JGZG.js → views-KN7FEDGT.js} +21 -21
  165. package/dist/cli/{workflow-AQPRIWQX.js → workflow-2WHSKTTV.js} +17 -17
  166. package/dist/cli/{workflow-TQRV5FGO.mjs → workflow-AAYLCLWA.mjs} +7 -7
  167. package/dist/{client-BlLYQDNo.d.mts → client-D0uDBybG.d.mts} +1 -1
  168. package/dist/{client-DSQlkIDO.d.ts → client-DZTnAv4B.d.ts} +1 -1
  169. package/dist/{client-core-rX7n2H8U.d.mts → client-core-BhWf8Y1b.d.mts} +15 -2
  170. package/dist/{client-core-CDwapXy5.d.ts → client-core-og3AP1LL.d.ts} +15 -2
  171. package/dist/{compiler-manifest-BHNC4RL-.d.ts → compiler-manifest-Bes3OVA3.d.mts} +1 -1
  172. package/dist/{compiler-manifest-BHNC4RL-.d.mts → compiler-manifest-Bes3OVA3.d.ts} +1 -1
  173. package/dist/{errors-lhtIWG5Z.d.ts → errors-BJm4lLWc.d.ts} +1 -1
  174. package/dist/{errors-D7sPXrvB.d.mts → errors-Dc5r42ks.d.mts} +1 -1
  175. package/dist/{index-CQTBHXHr.d.ts → index-BfV7mRPF.d.ts} +1 -1
  176. package/dist/{index-NaDZq2xf.d.mts → index-DTkiz_Ur.d.mts} +1 -1
  177. package/dist/index.d.mts +7 -7
  178. package/dist/index.d.ts +7 -7
  179. package/dist/index.js +9 -9
  180. package/dist/index.mjs +6 -6
  181. package/dist/install-integrity.json +120 -112
  182. package/dist/plays/bundle-play-file.d.mts +3 -3
  183. package/dist/plays/bundle-play-file.d.ts +3 -3
  184. package/dist/plays/play-description.d.mts +2 -2
  185. package/dist/plays/play-description.d.ts +2 -2
  186. package/dist/plays/run-request.d.mts +2 -2
  187. package/dist/plays/run-request.d.ts +2 -2
  188. package/dist/release.d.mts +1 -1
  189. package/dist/release.d.ts +1 -1
  190. package/dist/release.js +2 -2
  191. package/dist/release.mjs +1 -1
  192. package/dist/server.d.mts +4 -4
  193. package/dist/server.d.ts +4 -4
  194. package/dist/server.js +5 -5
  195. package/dist/server.mjs +3 -3
  196. package/package.json +1 -1
  197. package/dist/cli/auth-Y2K2ZYYA.js +0 -43
  198. package/dist/cli/describe-LSOHOLFI.js +0 -49
  199. package/dist/cli/git-S7SWUZ3T.js +0 -25
  200. package/dist/cli/search-WERQBC6V.js +0 -32
  201. package/dist/cli/sessions-LXQ4Q6BS.js +0 -22
  202. package/dist/cli/skills-EFRLHY3F.js +0 -36
  203. package/dist/cli/tools-C3EIGFFP.js +0 -56
  204. package/dist/cli/update-KCY72RXH.js +0 -34
@@ -0,0 +1,340 @@
1
+ import type { RuntimeSchedulerQueryClient } from './postgres-pool';
2
+
3
+ /**
4
+ * Preview experiment CA: claim affinity for the early (E2/PB) sandbox.
5
+ *
6
+ * The early-sandbox hint reaches whichever worker Machine the private
7
+ * `.internal` name resolves to, while the run is claimed by the queue's elected
8
+ * LISTEN worker. When they differ, the claimer refuses the other process's
9
+ * pre-booted sandbox (`early_supervisor_foreign`, which stays strict) and boots
10
+ * its own. With CA, the hinted worker names itself in the hint reply, admission
11
+ * records that name on the run's durable task (`earlySandboxClaimOwner`, an
12
+ * optional task param), and every other worker yields that task's first claim
13
+ * for a short window after enqueue. The owner meanwhile probes for the task
14
+ * and claims it. After the window any worker claims as today, so a lost owner
15
+ * costs at most the window. A task without the param (no hint, CA off, older
16
+ * admission) is claimed exactly as before.
17
+ *
18
+ * Two switches, both off:
19
+ * - `CLAIM_AFFINITY_FOR_EARLY_SANDBOX` (CA, per run): the hinted worker
20
+ * replies with its name and probes for the run; admission records it.
21
+ * - `EARLY_SANDBOX_CLAIM_AFFINITY_READER` (CAR, worker process): the claim
22
+ * gate that reads the param before each claim. It runs before any run is
23
+ * known, so it cannot be a per-run switch.
24
+ */
25
+ export const CLAIM_AFFINITY_FOR_EARLY_SANDBOX = false;
26
+ export const EARLY_SANDBOX_CLAIM_AFFINITY_READER = false;
27
+
28
+ /** Task param naming the worker that started the run's early sandbox. */
29
+ export const EARLY_SANDBOX_CLAIM_OWNER_PARAM = 'earlySandboxClaimOwner';
30
+
31
+ /**
32
+ * How long other workers yield a recorded owner's first claim, from the task's
33
+ * enqueue time. Derived from the measured path: the hint reply returns in p50
34
+ * 38 ms (max 163) and the run is enqueued ~360 ms after the hint, so the owner
35
+ * is already probing when the task appears; with a 20 ms probe it claims
36
+ * within ~30 ms of enqueue. 300 ms leaves room for a slow probe query while
37
+ * bounding the cost of a lost owner.
38
+ */
39
+ export const EARLY_SANDBOX_CLAIM_AFFINITY_WINDOW_MS = 300;
40
+ /** The owner's probe cadence while it waits for its run's task. */
41
+ export const EARLY_SANDBOX_OWNER_PROBE_INTERVAL_MS = 20;
42
+ /**
43
+ * How long after the hint the owner keeps probing. Admission enqueues ~0.4 s
44
+ * after the hint (slow admissions up to ~2 s); past this the window still
45
+ * hands the run to any worker.
46
+ */
47
+ export const EARLY_SANDBOX_OWNER_PROBE_DEADLINE_MS = 2_500;
48
+
49
+ const OWNER_PATTERN = /^[A-Za-z0-9._:-]{1,128}$/;
50
+
51
+ /** A worker name carried in a hint reply or task param, or null. */
52
+ export function parseEarlySandboxClaimOwner(value: unknown): string | null {
53
+ if (typeof value !== 'string') return null;
54
+ const owner = value.trim();
55
+ return OWNER_PATTERN.test(owner) ? owner : null;
56
+ }
57
+
58
+ function quoteIdentifier(identifier: string): string {
59
+ return `"${identifier.replaceAll('"', '""')}"`;
60
+ }
61
+
62
+ /** One due pending run as the claim gate sees it. */
63
+ export type ClaimAffinityDueRun = {
64
+ owner: string | null;
65
+ attempt: number;
66
+ /** Milliseconds left in the owner's window (0 when it has passed). */
67
+ remainingMs: number;
68
+ };
69
+
70
+ /**
71
+ * Whether this worker should skip its next claim, and for how long. Yields
72
+ * only when every due run is a first attempt recorded for ANOTHER owner whose
73
+ * window is still open, and nothing else is due (expired claims, due sleeps
74
+ * and due cancellations are claim_task work too). Anything else claims now.
75
+ */
76
+ export function decideEarlySandboxClaimYield(input: {
77
+ self: string;
78
+ dueRuns: readonly ClaimAffinityDueRun[];
79
+ otherDueWork: boolean;
80
+ }): number | null {
81
+ if (input.otherDueWork || input.dueRuns.length === 0) return null;
82
+ let yieldMs = Number.POSITIVE_INFINITY;
83
+ for (const run of input.dueRuns) {
84
+ const owner = parseEarlySandboxClaimOwner(run.owner);
85
+ if (
86
+ !owner ||
87
+ owner === input.self ||
88
+ run.attempt !== 1 ||
89
+ !(run.remainingMs > 0)
90
+ ) {
91
+ return null;
92
+ }
93
+ yieldMs = Math.min(yieldMs, run.remainingMs);
94
+ }
95
+ return Number.isFinite(yieldMs) ? Math.ceil(yieldMs) : null;
96
+ }
97
+
98
+ /**
99
+ * Read the claim gate's inputs: the first `limit` due pending runs (the same
100
+ * ordered index seek `claim_task` uses) with their recorded owner and window,
101
+ * and whether any other claim_task work is due.
102
+ */
103
+ export async function readEarlySandboxClaimAffinity(input: {
104
+ client: RuntimeSchedulerQueryClient;
105
+ queue: string;
106
+ limit: number;
107
+ windowMs: number;
108
+ }): Promise<{ dueRuns: ClaimAffinityDueRun[]; otherDueWork: boolean }> {
109
+ const runTable = quoteIdentifier(`r_${input.queue}`);
110
+ const taskTable = quoteIdentifier(`t_${input.queue}`);
111
+ const result = await input.client.query<{
112
+ owner: string | null;
113
+ attempt: number | null;
114
+ remaining_ms: number | null;
115
+ other_due: boolean;
116
+ }>(
117
+ `WITH clock AS MATERIALIZED (SELECT absurd.current_time() AS now),
118
+ due AS MATERIALIZED (
119
+ SELECT run_id, task_id, attempt FROM absurd.${runTable}
120
+ WHERE state = 'pending'
121
+ AND available_at <= (SELECT now FROM clock)
122
+ ORDER BY available_at LIMIT $1
123
+ ),
124
+ other AS MATERIALIZED (
125
+ SELECT (
126
+ (SELECT run_id FROM absurd.${runTable}
127
+ WHERE state = 'sleeping'
128
+ AND available_at <= (SELECT now FROM clock)
129
+ ORDER BY available_at LIMIT 1) IS NOT NULL
130
+ OR (SELECT run_id FROM absurd.${runTable}
131
+ WHERE state = 'running'
132
+ AND claim_expires_at IS NOT NULL
133
+ AND claim_expires_at <= (SELECT now FROM clock)
134
+ ORDER BY claim_expires_at LIMIT 1) IS NOT NULL
135
+ OR (SELECT task_id FROM absurd.${taskTable}
136
+ WHERE state IN ('pending', 'sleeping', 'running')
137
+ AND cancel_at IS NOT NULL
138
+ AND cancel_at <= (SELECT now FROM clock)
139
+ ORDER BY cancel_at LIMIT 1) IS NOT NULL
140
+ ) AS other_due
141
+ )
142
+ SELECT task.params ->> '${EARLY_SANDBOX_CLAIM_OWNER_PARAM}' AS owner,
143
+ due.attempt,
144
+ GREATEST(0, CEIL(EXTRACT(EPOCH FROM (
145
+ task.enqueue_at + make_interval(secs => $2::double precision / 1000)
146
+ - (SELECT now FROM clock))) * 1000))::integer AS remaining_ms,
147
+ (SELECT other_due FROM other) AS other_due
148
+ FROM other
149
+ LEFT JOIN due ON true
150
+ LEFT JOIN absurd.${taskTable} AS task ON task.task_id = due.task_id`,
151
+ [Math.max(1, Math.floor(input.limit)), input.windowMs],
152
+ );
153
+ const otherDueWork = result.rows[0]?.other_due === true;
154
+ const dueRuns = result.rows.flatMap((row) =>
155
+ row.attempt === null || row.attempt === undefined
156
+ ? []
157
+ : [
158
+ {
159
+ owner: row.owner ?? null,
160
+ attempt: Number(row.attempt),
161
+ remainingMs: Number(row.remaining_ms ?? 0),
162
+ },
163
+ ],
164
+ );
165
+ return { dueRuns, otherDueWork };
166
+ }
167
+
168
+ /** The vendored engine's claim statement (`Absurd.claimTasks`). */
169
+ const ABSURD_CLAIM_TASK_SQL = 'absurd.claim_task(';
170
+
171
+ export type EarlySandboxClaimGate = (
172
+ client: RuntimeSchedulerQueryClient,
173
+ claimParams: readonly unknown[],
174
+ ) => Promise<boolean>;
175
+
176
+ /**
177
+ * The claim gate the worker's scheduler queryable runs on the claim's own
178
+ * pooled client, before `claim_task`. It returns false (skip this claim) only
179
+ * while another owner's window is open, and then wakes the engine when that
180
+ * window closes, so the run is claimed by someone within the window plus one
181
+ * wake. A failed read claims as today.
182
+ */
183
+ export function createEarlySandboxClaimGate(input: {
184
+ self: string;
185
+ queue: string;
186
+ windowMs?: number;
187
+ wake: () => boolean;
188
+ setTimer?: (callback: () => void, ms: number) => unknown;
189
+ log?: (line: string, fields: Record<string, unknown>) => void;
190
+ }): EarlySandboxClaimGate {
191
+ const windowMs = input.windowMs ?? EARLY_SANDBOX_CLAIM_AFFINITY_WINDOW_MS;
192
+ const setTimer =
193
+ input.setTimer ??
194
+ ((callback: () => void, ms: number) => {
195
+ const timer = setTimeout(callback, ms);
196
+ timer.unref?.();
197
+ return timer;
198
+ });
199
+ const log = input.log ?? ((line, fields) => console.info(line, fields));
200
+ let wakeAt: number | null = null;
201
+ return async (client, claimParams) => {
202
+ const startedAt = performance.now();
203
+ const limit = Number(claimParams[3] ?? 1);
204
+ let decision: number | null;
205
+ try {
206
+ const read = await readEarlySandboxClaimAffinity({
207
+ client,
208
+ queue: input.queue,
209
+ limit: Number.isFinite(limit) && limit > 0 ? limit : 1,
210
+ windowMs,
211
+ });
212
+ decision = decideEarlySandboxClaimYield({
213
+ self: input.self,
214
+ dueRuns: read.dueRuns,
215
+ otherDueWork: read.otherDueWork,
216
+ });
217
+ } catch (error) {
218
+ log('[runtime.claim_affinity]', {
219
+ event: 'gate_failed',
220
+ error: error instanceof Error ? error.message : String(error),
221
+ });
222
+ return true;
223
+ }
224
+ const gateMs = Math.round(performance.now() - startedAt);
225
+ if (decision === null) {
226
+ log('[runtime.claim_affinity]', { event: 'gate_claim', gateMs });
227
+ return true;
228
+ }
229
+ // One timer per open window: a later yield inside it reuses the wake.
230
+ const due = Date.now() + decision + 1;
231
+ if (wakeAt === null || wakeAt <= Date.now() || due < wakeAt) {
232
+ wakeAt = due;
233
+ setTimer(() => {
234
+ wakeAt = null;
235
+ input.wake();
236
+ }, decision + 1);
237
+ }
238
+ log('[runtime.claim_affinity]', {
239
+ event: 'gate_yield',
240
+ yieldMs: decision,
241
+ gateMs,
242
+ });
243
+ return false;
244
+ };
245
+ }
246
+
247
+ /**
248
+ * Run `claim` unless `gate` says to yield; a yielded claim returns no rows,
249
+ * exactly like a claim that found nothing. Non-claim statements pass through.
250
+ */
251
+ export async function runGatedAbsurdClaimQuery<Row>(input: {
252
+ sql: string;
253
+ params: readonly unknown[] | undefined;
254
+ client: RuntimeSchedulerQueryClient;
255
+ gate: EarlySandboxClaimGate | undefined;
256
+ run: () => Promise<{ rows: Row[] }>;
257
+ }): Promise<{ rows: Row[] }> {
258
+ if (
259
+ input.gate &&
260
+ input.sql.includes(ABSURD_CLAIM_TASK_SQL) &&
261
+ !(await input.gate(input.client, input.params ?? []))
262
+ ) {
263
+ return { rows: [] };
264
+ }
265
+ return input.run();
266
+ }
267
+
268
+ /**
269
+ * Probe whether a pending task recorded for `owner` exists (due or not).
270
+ * One ordered seek over the pending partial index plus the task row.
271
+ */
272
+ export async function hasPendingEarlySandboxClaim(input: {
273
+ client: RuntimeSchedulerQueryClient;
274
+ queue: string;
275
+ owner: string;
276
+ }): Promise<boolean> {
277
+ const runTable = quoteIdentifier(`r_${input.queue}`);
278
+ const taskTable = quoteIdentifier(`t_${input.queue}`);
279
+ const result = await input.client.query<{ found: boolean }>(
280
+ `SELECT EXISTS (
281
+ SELECT 1
282
+ FROM (SELECT task_id, attempt FROM absurd.${runTable}
283
+ WHERE state = 'pending'
284
+ ORDER BY available_at LIMIT 32) AS pending
285
+ JOIN absurd.${taskTable} AS task ON task.task_id = pending.task_id
286
+ WHERE pending.attempt = 1
287
+ AND task.params ->> '${EARLY_SANDBOX_CLAIM_OWNER_PARAM}' = $1
288
+ ) AS found`,
289
+ [input.owner],
290
+ );
291
+ return result.rows[0]?.found === true;
292
+ }
293
+
294
+ /**
295
+ * The owner's side: after accepting a CA hint, probe every `intervalMs` for a
296
+ * pending task recorded for this worker and wake the claim loop when one
297
+ * appears. Stops once that task has been seen and is gone (claimed), or at the
298
+ * deadline. Errors are reported and the probe continues; the window bounds the
299
+ * cost of any miss.
300
+ */
301
+ export async function runEarlySandboxOwnerClaimProbe(input: {
302
+ probe: () => Promise<boolean>;
303
+ wake: () => boolean;
304
+ intervalMs?: number;
305
+ deadlineMs?: number;
306
+ now?: () => number;
307
+ sleep?: (ms: number) => Promise<void>;
308
+ onError?: (error: unknown) => void;
309
+ }): Promise<{ outcome: 'claimed' | 'deadline'; probes: number; wakes: number }> {
310
+ const intervalMs = Math.max(
311
+ 1,
312
+ input.intervalMs ?? EARLY_SANDBOX_OWNER_PROBE_INTERVAL_MS,
313
+ );
314
+ const now = input.now ?? Date.now;
315
+ const sleep =
316
+ input.sleep ??
317
+ ((ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms)));
318
+ const deadline =
319
+ now() + (input.deadlineMs ?? EARLY_SANDBOX_OWNER_PROBE_DEADLINE_MS);
320
+ let seen = false;
321
+ let probes = 0;
322
+ let wakes = 0;
323
+ while (now() < deadline) {
324
+ let pending = false;
325
+ try {
326
+ probes += 1;
327
+ pending = await input.probe();
328
+ } catch (error) {
329
+ input.onError?.(error);
330
+ }
331
+ if (pending) {
332
+ seen = true;
333
+ if (input.wake()) wakes += 1;
334
+ } else if (seen) {
335
+ return { outcome: 'claimed', probes, wakes };
336
+ }
337
+ await sleep(intervalMs);
338
+ }
339
+ return { outcome: 'deadline', probes, wakes };
340
+ }
@@ -131,6 +131,7 @@ export async function transitionPreCodePlayRunRecovery(input: {
131
131
  logicalCalls: 0,
132
132
  providerRequests: 0,
133
133
  provider429s: 0,
134
+ reusableResultCalls: 0,
134
135
  },
135
136
  runtimeWorkCounterSegment: {
136
137
  invocationSeq: 1,
@@ -1440,6 +1440,7 @@ export function createPostgresSchedulerProgressReporter(input: {
1440
1440
  logicalCalls?: unknown;
1441
1441
  providerRequests?: unknown;
1442
1442
  provider429s?: unknown;
1443
+ reusableResultCalls?: unknown;
1443
1444
  };
1444
1445
  if (
1445
1446
  typeof observed.attempt !== 'number' ||
@@ -1471,6 +1472,10 @@ export function createPostgresSchedulerProgressReporter(input: {
1471
1472
  logicalCalls: observed.logicalCalls as number,
1472
1473
  providerRequests: observed.providerRequests as number,
1473
1474
  provider429s: observed.provider429s as number,
1475
+ ...(Number.isSafeInteger(observed.reusableResultCalls) &&
1476
+ (observed.reusableResultCalls as number) >= 0
1477
+ ? { reusableResultCalls: observed.reusableResultCalls as number }
1478
+ : {}),
1474
1479
  final: false,
1475
1480
  };
1476
1481
  if (
@@ -1480,7 +1485,8 @@ export function createPostgresSchedulerProgressReporter(input: {
1480
1485
  workCounters.segmentId === next.segmentId &&
1481
1486
  workCounters.logicalCalls >= next.logicalCalls &&
1482
1487
  workCounters.providerRequests >= next.providerRequests &&
1483
- workCounters.provider429s >= next.provider429s
1488
+ workCounters.provider429s >= next.provider429s &&
1489
+ workCounters.reusableResultCalls === next.reusableResultCalls
1484
1490
  )
1485
1491
  return;
1486
1492
  workCounters = next;
@@ -1766,6 +1772,9 @@ export function createPostgresSchedulerProgressReporter(input: {
1766
1772
  logicalCalls: terminalCounters.logicalCalls,
1767
1773
  providerRequests: terminalCounters.providerRequests,
1768
1774
  provider429s: terminalCounters.provider429s,
1775
+ ...(terminalCounters.reusableResultCalls !== undefined
1776
+ ? { reusableResultCalls: terminalCounters.reusableResultCalls }
1777
+ : {}),
1769
1778
  final: true,
1770
1779
  };
1771
1780
  workCountersDirty = true;