deepline 0.3.536 → 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 +36 -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-KCTCUO7P.mjs → auth-KYH7N7RQ.mjs} +5 -5
  61. package/dist/cli/{billing-4VBGZ3CT.js → billing-4KTUVZ4S.js} +114 -90
  62. package/dist/cli/{billing-OOJHKLNS.mjs → billing-WK346KAI.mjs} +31 -7
  63. package/dist/cli/{chunk-4E5JXZ3W.js → chunk-25RGHJQL.js} +15 -15
  64. package/dist/cli/{chunk-MSP4DTSD.js → chunk-34PLG5OE.js} +13 -13
  65. package/dist/cli/{chunk-DQHUJMCL.mjs → chunk-35OCRH5C.mjs} +5 -5
  66. package/dist/cli/{chunk-2B4IOGFD.js → chunk-3I5TU5XE.js} +292 -279
  67. package/dist/cli/{chunk-Z7UIOGW4.mjs → chunk-4KZF6OYY.mjs} +3 -3
  68. package/dist/cli/{chunk-2YWJS76L.mjs → chunk-4NEOGPKD.mjs} +1 -1
  69. package/dist/cli/{chunk-LZXZQPA2.mjs → chunk-4WQR4DG4.mjs} +1 -1
  70. package/dist/cli/{chunk-KWYGWO4Y.js → chunk-5VSPRRTI.js} +7 -7
  71. package/dist/cli/{chunk-ANUKBZ6W.mjs → chunk-6JG3MRTM.mjs} +2 -2
  72. package/dist/cli/{chunk-VBDZIMWD.js → chunk-6SLQLWWL.js} +26 -26
  73. package/dist/cli/{chunk-E24UGPGG.mjs → chunk-7XDJKIM2.mjs} +4 -4
  74. package/dist/cli/{chunk-OUDTOMZC.mjs → chunk-A6E7TCK2.mjs} +5 -5
  75. package/dist/cli/{chunk-ZZ4MF2GI.js → chunk-ADLDU2QG.js} +6 -6
  76. package/dist/cli/{chunk-XVZRJJDS.mjs → chunk-ALARRSMZ.mjs} +3 -3
  77. package/dist/cli/{chunk-HWDKRN7L.js → chunk-B2BTBHX2.js} +3 -3
  78. package/dist/cli/{chunk-ZISEH7OL.js → chunk-BBS2BLPV.js} +16 -16
  79. package/dist/cli/{chunk-YRLAUGQY.mjs → chunk-CLO562KG.mjs} +3 -1
  80. package/dist/cli/{chunk-CXTW7SRW.mjs → chunk-E7D37PLM.mjs} +8 -0
  81. package/dist/cli/{chunk-SLPJ3LXI.js → chunk-ESX3H7Q7.js} +22 -22
  82. package/dist/cli/{chunk-AB2GBOD4.mjs → chunk-FJ5KPGCS.mjs} +10 -10
  83. package/dist/cli/{chunk-DUYAMLOC.mjs → chunk-FWE37N2P.mjs} +1 -1
  84. package/dist/cli/{chunk-3PCYV6ZJ.mjs → chunk-ICTA5ICE.mjs} +1 -1
  85. package/dist/cli/{chunk-ZILSRDIN.js → chunk-IMLFB66A.js} +3 -3
  86. package/dist/cli/{chunk-PNY3BWHP.mjs → chunk-IP3XJTWA.mjs} +2 -2
  87. package/dist/cli/{chunk-6VUGBQZQ.js → chunk-JCLRSMUP.js} +5 -5
  88. package/dist/cli/{chunk-YBMHOH6H.mjs → chunk-JPEBKN3K.mjs} +2 -2
  89. package/dist/cli/{chunk-GF32SBST.mjs → chunk-JTOV5RUA.mjs} +1 -1
  90. package/dist/cli/{chunk-Q5CXV3E3.js → chunk-KCUQNH4K.js} +2 -2
  91. package/dist/cli/{chunk-DA2I2M7D.mjs → chunk-KNIYEZVV.mjs} +2 -2
  92. package/dist/cli/{chunk-CWSLUNA6.js → chunk-LPTBHNGB.js} +7 -5
  93. package/dist/cli/{chunk-WC5M3JXE.mjs → chunk-M3NCXGNN.mjs} +2 -2
  94. package/dist/cli/{chunk-TX5TUHJN.js → chunk-MNBIGX6O.js} +10 -10
  95. package/dist/cli/{chunk-ZEV2Y56V.js → chunk-MYSPQUAO.js} +44 -44
  96. package/dist/cli/{chunk-KPQHUVYW.mjs → chunk-N2BCPZVR.mjs} +5 -5
  97. package/dist/cli/{chunk-XBAH3TKU.js → chunk-N5HSG4OV.js} +5 -5
  98. package/dist/cli/{chunk-GTVZKY3F.js → chunk-O765M4LT.js} +2 -2
  99. package/dist/cli/{chunk-2FCSUOYN.js → chunk-OOLBE6FR.js} +53 -53
  100. package/dist/cli/{chunk-W7AEIGZR.js → chunk-P3DP56HB.js} +41 -41
  101. package/dist/cli/{chunk-LN6RXDTB.js → chunk-PQKD5DMT.js} +19 -19
  102. package/dist/cli/{chunk-GHLDCLSC.mjs → chunk-T6GPTJ5F.mjs} +1 -1
  103. package/dist/cli/{chunk-XJD2GYUP.mjs → chunk-TWWYCJJW.mjs} +5 -5
  104. package/dist/cli/{chunk-BASLYGXK.mjs → chunk-UE7JHAZ2.mjs} +29 -16
  105. package/dist/cli/{chunk-Q4MNUOS4.js → chunk-UEZNIPFO.js} +107 -107
  106. package/dist/cli/{chunk-RVSSQPQO.mjs → chunk-VCHV4LS3.mjs} +1 -1
  107. package/dist/cli/{chunk-LDGZFDUB.js → chunk-VIFLWQXS.js} +2 -2
  108. package/dist/cli/{chunk-6BBGUQJJ.js → chunk-XTYHZEJ2.js} +8 -0
  109. package/dist/cli/{chunk-CIDN5K23.js → chunk-YQURGSRB.js} +1 -1
  110. package/dist/cli/{chunk-Y2NTPYGS.js → chunk-YZ2ZPHWR.js} +9 -9
  111. package/dist/cli/{chunk-HAJQGP5V.mjs → chunk-ZDLDCXY3.mjs} +2 -2
  112. package/dist/cli/{chunk-JUHDJNSI.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-P3HLK4GO.mjs → csv-2YKLUKDR.mjs} +4 -4
  116. package/dist/cli/{csv-2V2DIFNG.js → csv-LM7FBECI.js} +13 -13
  117. package/dist/cli/{db-ZYOY3QRQ.mjs → db-234UBWJH.mjs} +6 -6
  118. package/dist/cli/{db-MREAQVW6.js → db-5PMA4IVW.js} +27 -27
  119. package/dist/cli/describe-C7GOSGYB.js +49 -0
  120. package/dist/cli/{describe-IKZFOADR.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-H5IJ7QSQ.js → executions-3S736GXX.js} +8 -8
  124. package/dist/cli/{executions-KTSY5M34.mjs → executions-GD7GXTJM.mjs} +6 -6
  125. package/dist/cli/{feedback-724QK3Z5.js → feedback-BUWTDY4R.js} +14 -14
  126. package/dist/cli/{feedback-WWJRIXEU.mjs → feedback-GBV5HJJZ.mjs} +6 -6
  127. package/dist/cli/git-YG36MROC.js +25 -0
  128. package/dist/cli/{git-R5VRG7X6.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-PWZ5XLAO.mjs → legacy-noops-UJM6GBNP.mjs} +6 -6
  132. package/dist/cli/{legacy-noops-C6GDEGPU.js → legacy-noops-ZSF64V2U.js} +9 -9
  133. package/dist/cli/{monitors-WQSKMHAP.mjs → monitors-UERUQ6CT.mjs} +7 -7
  134. package/dist/cli/{monitors-VAVQ4WCE.js → monitors-XQ2GNH6D.js} +100 -100
  135. package/dist/cli/{org-MAL4QPVF.mjs → org-CP4VLH6M.mjs} +6 -6
  136. package/dist/cli/{org-CWWRWXZQ.js → org-IF6MPVNR.js} +86 -86
  137. package/dist/cli/{play-WJOWK5PQ.js → play-2NHQO5MO.js} +317 -317
  138. package/dist/cli/{play-ILW6MSNG.mjs → play-ZWDYYVPI.mjs} +17 -17
  139. package/dist/cli/{providers-4V2NMLZZ.mjs → providers-2CVIATU4.mjs} +6 -6
  140. package/dist/cli/{providers-W662ANCH.js → providers-LOPXQN56.js} +9 -9
  141. package/dist/cli/{quickstart-C22BR743.mjs → quickstart-KVCFXLID.mjs} +4 -4
  142. package/dist/cli/{quickstart-BW6EBUYX.js → quickstart-N4WDEQZY.js} +6 -6
  143. package/dist/cli/{run-KW7KW4FX.js → run-BDT5LK3I.js} +42 -42
  144. package/dist/cli/{run-SPAU5IMW.mjs → run-RUXHYWLF.mjs} +22 -22
  145. package/dist/cli/search-ATDDVADT.js +32 -0
  146. package/dist/cli/{search-XPC75HLM.mjs → search-BFUTNPA5.mjs} +7 -7
  147. package/dist/cli/{search-output-VJYE7USJ.js → search-output-CTXI2QZU.js} +6 -6
  148. package/dist/cli/{search-output-JKYJSEPM.mjs → search-output-WUGH4PZA.mjs} +4 -4
  149. package/dist/cli/{secrets-YB57KGSB.js → secrets-BZJZYFOU.js} +15 -15
  150. package/dist/cli/{secrets-OJG73GXQ.mjs → secrets-Z4XQVTC6.mjs} +6 -6
  151. package/dist/cli/sessions-HNU35AFM.js +22 -0
  152. package/dist/cli/{sessions-7ZL37BW4.mjs → sessions-VZXR3MQZ.mjs} +6 -6
  153. package/dist/cli/{settings-RKSQB7FA.mjs → settings-VYEZFR4L.mjs} +12 -12
  154. package/dist/cli/{settings-A7DD2ZYE.js → settings-WBYA4JMM.js} +85 -85
  155. package/dist/cli/{setup-S3K5YU62.js → setup-FS2RMSS2.js} +49 -49
  156. package/dist/cli/{setup-NXDUTITY.mjs → setup-H7KCI4SO.mjs} +9 -9
  157. package/dist/cli/skills-VPIURCAZ.js +36 -0
  158. package/dist/cli/{skills-VMFAIDO6.mjs → skills-YQNSJUUG.mjs} +7 -7
  159. package/dist/cli/tools-273GS5HU.js +56 -0
  160. package/dist/cli/{tools-52NTHBSJ.mjs → tools-ZF6SVEGW.mjs} +16 -16
  161. package/dist/cli/{update-SAKBEKMV.mjs → update-RKQNNJM4.mjs} +9 -9
  162. package/dist/cli/update-TOD3XY55.js +34 -0
  163. package/dist/cli/{views-ACRV7MD6.mjs → views-JHHOTWDU.mjs} +6 -6
  164. package/dist/cli/{views-C3GJOBDG.js → views-KN7FEDGT.js} +21 -21
  165. package/dist/cli/{workflow-ZNWCZEGN.js → workflow-2WHSKTTV.js} +17 -17
  166. package/dist/cli/{workflow-CS22E7TC.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.mts → compiler-manifest-Bes3OVA3.d.mts} +1 -1
  172. package/dist/{compiler-manifest-BHNC4RL-.d.ts → 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-6XQKFOLG.js +0 -43
  198. package/dist/cli/describe-WW3OQRLP.js +0 -49
  199. package/dist/cli/git-PHYRD56H.js +0 -25
  200. package/dist/cli/search-HLE54W5G.js +0 -32
  201. package/dist/cli/sessions-VEBYZIEU.js +0 -22
  202. package/dist/cli/skills-NVQ744CN.js +0 -36
  203. package/dist/cli/tools-SBDUL3WB.js +0 -56
  204. package/dist/cli/update-HHVFZPCV.js +0 -34
@@ -56,6 +56,13 @@ export const PREFERRED_RECEIPT_GATEWAY_MACHINE_ID_HEADER =
56
56
  * prod customer can never suppress their own failure alerts.
57
57
  */
58
58
  export const SYNTHETIC_RUN_HEADER = 'x-deepline-synthetic-run';
59
+ /**
60
+ * CLI→app request for per-run experiment-switch overrides (`B=1,H1=0`). The
61
+ * run route validates it and honors it only through the
62
+ * `play_run.experiment_switch_overrides` internal capability, refused on
63
+ * Production. See `experiment-switches.ts`.
64
+ */
65
+ export const EXPERIMENT_SWITCHES_HEADER = 'x-deepline-experiment-switches';
59
66
 
60
67
  let warnedAboutMissingInternalToken = false;
61
68
 
@@ -61,8 +61,29 @@ export function readDaytonaPayloadEnvelope(input: unknown): {
61
61
  * The sandbox has only Node built-ins. Generate its tiny entry from the same
62
62
  * contract-owned field list instead of maintaining a second hand-written list
63
63
  * in transport code. This is shipped source, not Function#toString output.
64
+ *
65
+ * An empty `runnerCode` means the runner bundle is already on disk at the path
66
+ * the launcher verified (prebaked image) or uploaded separately; it is never
67
+ * produced for a bundle that must be shipped in the envelope.
64
68
  */
65
69
  export function buildDaytonaPayloadMaterializerSource(): string {
66
70
  const fields = JSON.stringify(DAYTONA_PAYLOAD_FILE_FIELDS);
67
- return `const fs=require('node:fs');const zlib=require('node:zlib');const p=JSON.parse(zlib.gunzipSync(fs.readFileSync(process.argv[1])));if(!p||typeof p!=='object'||Array.isArray(p))throw new Error('Daytona payload: expected object');for(const key of ${fields})if(typeof p[key]!=='string')throw new Error('Daytona payload is missing required field '+key);if(!p.config||typeof p.config!=='object'||Array.isArray(p.config))throw new Error('Daytona payload is missing required field config');if(typeof p.schemaVersion==='number'&&p.schemaVersion!==1)console.warn('[daytona] payload schema marker differs; attempting structural materialization');fs.writeFileSync(process.argv[2],p.runnerCode);fs.writeFileSync(process.argv[3],JSON.stringify(p.config));fs.writeFileSync(process.argv[4],p.artifactBundledCode);fs.writeFileSync(process.argv[5],p.artifactSourceMap);fs.writeFileSync(process.argv[6],p.crashPusherCode);`;
71
+ return `const fs=require('node:fs');const zlib=require('node:zlib');const p=JSON.parse(zlib.gunzipSync(fs.readFileSync(process.argv[1])));if(!p||typeof p!=='object'||Array.isArray(p))throw new Error('Daytona payload: expected object');for(const key of ${fields})if(typeof p[key]!=='string')throw new Error('Daytona payload is missing required field '+key);if(!p.config||typeof p.config!=='object'||Array.isArray(p.config))throw new Error('Daytona payload is missing required field config');if(typeof p.schemaVersion==='number'&&p.schemaVersion!==1)console.warn('[daytona] payload schema marker differs; attempting structural materialization');if(p.runnerCode!=='')fs.writeFileSync(process.argv[2],p.runnerCode);fs.writeFileSync(process.argv[3],JSON.stringify(p.config));fs.writeFileSync(process.argv[4],p.artifactBundledCode);fs.writeFileSync(process.argv[5],p.artifactSourceMap);fs.writeFileSync(process.argv[6],p.crashPusherCode);`;
72
+ }
73
+
74
+ /**
75
+ * PB: the delivery-time materializer for a runner that was started before its
76
+ * envelope arrived and is polling for its config file (`--wait-config`).
77
+ * Arguments: 1 envelopePath, 2 configPath, 3 artifactCodePath,
78
+ * 4 artifactSourceMapPath, 5 crashPusherPath.
79
+ *
80
+ * The runner starts the run the moment the config path exists, so the config
81
+ * is written LAST and atomically: every other file first, then the config to a
82
+ * temporary sibling renamed onto the config path (one directory, so the rename
83
+ * is atomic). An envelope that carries runner bytes is refused: the running
84
+ * process is the verified baked runner, never a shipped one.
85
+ */
86
+ export function buildPrebootDeliveryMaterializerSource(): string {
87
+ const fields = JSON.stringify(DAYTONA_PAYLOAD_FILE_FIELDS);
88
+ return `const fs=require('node:fs');const zlib=require('node:zlib');const p=JSON.parse(zlib.gunzipSync(fs.readFileSync(process.argv[1])));if(!p||typeof p!=='object'||Array.isArray(p))throw new Error('Daytona payload: expected object');for(const key of ${fields})if(typeof p[key]!=='string')throw new Error('Daytona payload is missing required field '+key);if(!p.config||typeof p.config!=='object'||Array.isArray(p.config))throw new Error('Daytona payload is missing required field config');if(p.runnerCode!=='')throw new Error('Pre-booted runner payload must not carry runner code');fs.writeFileSync(process.argv[3],p.artifactBundledCode);fs.writeFileSync(process.argv[4],p.artifactSourceMap);fs.writeFileSync(process.argv[5],p.crashPusherCode);const tmp=process.argv[2]+'.tmp-'+process.pid;fs.writeFileSync(tmp,JSON.stringify(p.config));fs.renameSync(tmp,process.argv[2]);`;
68
89
  }
@@ -45,6 +45,47 @@ export const DURABLE_RUNS_ROLLOUT = {
45
45
  * `staleAfterSeconds`.
46
46
  */
47
47
  sharedReadCacheWrites: true,
48
+ /**
49
+ * Finish-in-execute: a direct tool call sends its planned finishing receipt
50
+ * keys on the execute request, and the execution relay commits them before
51
+ * returning a successful answer, so the runner skips its own completion
52
+ * request when the relay's acknowledgement covers exactly those writes.
53
+ *
54
+ * Skew-safe both ways: an older relay ignores the plan (the runner writes as
55
+ * before) and a newer relay without a plan relays untouched. Lands off and
56
+ * flips in a rollout PR after every relay serves the reader. Off stops new
57
+ * plans; rows already committed are ordinary immutable receipts.
58
+ */
59
+ finishInExecute: false,
60
+ /**
61
+ * A direct policy-0 tool call reads the result receipt it would claim in
62
+ * the same request as its operation slots (one fixed batch of direct keys),
63
+ * instead of a second read after the operation answer. Off: the call reads
64
+ * exactly as before. Reader-only, so safe in either direction; lands off so
65
+ * the flag-off build stays byte-identical to main.
66
+ */
67
+ toolResultCompanionRead: false,
68
+ /**
69
+ * Local-permit read-through: a warm policy-0 direct call whose dispatch
70
+ * capacity is already on hand sends its receipt keys (#10008's slots and its
71
+ * result receipt) on the relay's `execute-read-through` action instead of
72
+ * reading first. The relay executes only when every key reads empty; any
73
+ * record (a pre-binding one included) falls back to the ordinary #10027
74
+ * classification, and a fresh invocation never uses read-through. Never
75
+ * offered with a legacy operation key. Off keeps today's read on every call.
76
+ * Lands off.
77
+ */
78
+ localPermitReadThrough: false,
79
+ /**
80
+ * #10002: one Node process materializes the payload and then becomes the
81
+ * runtime watchdog (two Node starts instead of three). Off: the established
82
+ * `materialize && watchdog` chain, byte for byte. Lands off: a Preview
83
+ * comparison against main (1-call play, 3 runs per arm) measured the
84
+ * combined process holding about 13 MiB more for the whole run (watchdog
85
+ * RSS 92 vs 79 MiB; sandbox cgroup peak 140 vs 129 MiB), which counts
86
+ * against the sandbox memory abort.
87
+ */
88
+ singleNodeMaterializeWatchdog: false,
48
89
  /**
49
90
  * A provider's 5xx is retried inside the attempt with exponential backoff
50
91
  * and jitter, sending the same provider idempotency key. Owner rule
@@ -78,4 +119,86 @@ export const DURABLE_RUNS_ROLLOUT = {
78
119
  * compacted in between.
79
120
  */
80
121
  runsResume: true,
122
+ /**
123
+ * A Modal-placed run gets the `modal_sidecar` profile: the Play runner runs
124
+ * in the Modal main container with no outbound network, and a trusted
125
+ * supervisor sidecar in the same sandbox runs the unchanged runtime gateway
126
+ * (receipt role on one port, execution role on another) with the gateway's
127
+ * credentials. Receipts, permits, rate state, fences, the backpressure 429
128
+ * and the Vercel execute route are the gateway's own code; ctx.fetch goes
129
+ * through the gateway to the existing egress broker. Requires a published
130
+ * supervisor image (`DEEPLINE_MODAL_SUPERVISOR_HASH`). Off: Modal runs use
131
+ * the Fly gateway and relay as before. Lands off.
132
+ */
133
+ modalSupervisorSidecar: false,
134
+ /**
135
+ * A bounded rate-state transaction (provider permit validation, the
136
+ * pure-rate grant, and the opening of the concurrency grant) is sent as one
137
+ * simple-protocol request instead of separate BEGIN / timeout / statement /
138
+ * COMMIT round trips. Statement order, isolation, lock acquisition order and
139
+ * row-lock scope are unchanged: every statement stays its own READ COMMITTED
140
+ * statement inside the same transaction, the timeouts are the same
141
+ * transaction-local values (SET LOCAL), and a failure still rolls back and
142
+ * maps through the same lock-busy classification. Parameters are rendered
143
+ * as the same text node-postgres sends; any call with a parameter that is
144
+ * not a scalar, or a placeholder reused without an explicit cast, keeps the
145
+ * multi-trip path. Off: unchanged.
146
+ */
147
+ rateStateSingleTripTransactions: false,
148
+ /**
149
+ * Collapse a warm rate-state acquire to as few requests as its transactions
150
+ * allow (needs `rateStateSingleTripTransactions`). The concurrency grant
151
+ * sends BEGIN, the bounds, the scope lock, the grant and COMMIT as one
152
+ * request: the grant is still its own READ COMMITTED statement, so it takes
153
+ * a fresh snapshot after any wait for the lock. The observation fold sends
154
+ * BEGIN, the bounds, the observer upsert and the success count (one
155
+ * statement; the count is gated on the upsert's acceptance, as before) and,
156
+ * when no controller checkpoint is needed, COMMIT as one request; with a
157
+ * checkpoint the locked rule rows come back in that same request and the
158
+ * checkpoint updates go out with COMMIT as the second. Array parameters are
159
+ * rendered as the text node-postgres binds. Same transactions, lock order,
160
+ * row locks and errors; any statement that cannot be rendered keeps the
161
+ * previous path. Off: unchanged.
162
+ */
163
+ rateStateCollapsedAcquire: false,
164
+ /**
165
+ * While a detached runner waits for the park acknowledgement, it issues the
166
+ * read-only startup lookups its static plan names (tool metadata and
167
+ * auth-scope GETs, the same URLs and headers) and the first real lookup reuses
168
+ * a successful answer once. No write moves: governor, rate state, receipts
169
+ * and execute still start after the fence. Off: unchanged.
170
+ */
171
+ runnerStartupReadPrefetch: false,
172
+ /**
173
+ * The worker names the launch's static tool ids in the runner context
174
+ * (`startupReadToolIds`) so the park-fence startup prefetch can run without
175
+ * the full static pipeline in the runner config. Read-only lookups only; the
176
+ * runner still issues them with its own executor token. Off: unchanged.
177
+ */
178
+ workerStartupReadToolIds: false,
179
+ /**
180
+ * While fenced on the park acknowledgement, the runner asks the receipt
181
+ * gateway to hold its heartbeat answer (awaitSchedulerWaitingMs, at most
182
+ * 500 ms requested, 1 s honored) until the read-only attempt fence reports
183
+ * the durable `waiting` state or a terminal. The renewing write still commits
184
+ * first; nothing else moves. Off: unchanged.
185
+ */
186
+ runnerParkAckLongPoll: false,
187
+ /**
188
+ * Internal diagnostics on the runner's terminal push: relay connections
189
+ * opened (an undici connection-channel subscriber started with the run),
190
+ * the runner's phase timings for every run (not only origin-identity runs)
191
+ * and the runner process start time. Bounded scalars the gateway logs on
192
+ * its internal event; never written to customer run logs. Off: the
193
+ * terminal push and phase bookkeeping are main's, byte for byte.
194
+ */
195
+ runnerTerminalDiagnostics: false,
196
+ /**
197
+ * The execution relay's Postgres pool keeps idle clients for five minutes
198
+ * instead of the shared 30 s, so a run after an idle gap does not reopen a
199
+ * PgBouncer connection on its first relay call (Preview A/B: first-call
200
+ * launch read 90.5-93.5 -> 5 ms p50). Off: the relay pool uses the shared
201
+ * default, as on main.
202
+ */
203
+ executionRelayPoolIdleTimeout: false,
81
204
  } as const;
@@ -0,0 +1,480 @@
1
+ /**
2
+ * Runtime-speed experiment switches: one registry, and per-run overrides.
3
+ *
4
+ * Every experiment stays a module constant at its owner (`export const X =
5
+ * false;`). That constant is the switch's static default and the only source
6
+ * of truth for it; the measurement tooling edits those exact lines. This
7
+ * registry names each switch, records where its constant lives, and says
8
+ * whether a single run may override it.
9
+ *
10
+ * A per-run override flips an A/B arm for one run inside one deployment. It is
11
+ * requested with `x-deepline-experiment-switches: B=1,H1=0` on a Play run
12
+ * admission, authorized only by the `play_run.experiment_switch_overrides`
13
+ * internal capability (refused on Production), validated by
14
+ * `parseExperimentSwitchesHeader`, and carried on the run's launch envelope as
15
+ * the optional `experimentSwitchOverrides` field. Absence means "static
16
+ * defaults" everywhere, so an old worker or an old launch row is unaffected.
17
+ *
18
+ * Adding a per-run switch (for example a worker-side X1 in
19
+ * `launchModalRunnerCommand`) is one registry entry with `perRun: true`, plus
20
+ * `resolveExperimentSwitch('X1', config.context.experimentSwitchOverrides,
21
+ * X1_CONSTANT)` at its call site. A switch that cannot honour a per-run value
22
+ * is listed with `perRun: false` and the reason, so a request for it fails
23
+ * loudly instead of silently measuring the static arm.
24
+ *
25
+ * Every switch has a row in docs/runtime-speed-experiments.md. When a switch's
26
+ * experiment loses, delete its code and move its id to
27
+ * RETIRED_EXPERIMENT_SWITCHES.
28
+ */
29
+
30
+ import { EXPERIMENT_SWITCHES_HEADER } from './coordinator-headers';
31
+
32
+ export { EXPERIMENT_SWITCHES_HEADER };
33
+
34
+ type ExperimentSwitchConstant = {
35
+ /** Repo-relative file holding `export const <name> = false;`. */
36
+ readonly file: string;
37
+ readonly name: string;
38
+ };
39
+
40
+ type ExperimentSwitchDefinition =
41
+ | {
42
+ readonly perRun: true;
43
+ readonly summary: string;
44
+ /** Every constant read for this switch; each is the static default on its side. */
45
+ readonly constants: readonly ExperimentSwitchConstant[];
46
+ }
47
+ | {
48
+ readonly perRun: false;
49
+ readonly summary: string;
50
+ readonly constants: readonly ExperimentSwitchConstant[];
51
+ /** Why a run cannot override it. */
52
+ readonly reason: string;
53
+ };
54
+
55
+ const MODAL = 'packages/play-runtime/runner-backends/backends/modal.ts';
56
+
57
+ export const EXPERIMENT_SWITCHES = {
58
+ B: {
59
+ perRun: true,
60
+ summary:
61
+ 'Run route sends the execute prewarm at admission; the worker skips its own.',
62
+ constants: [
63
+ {
64
+ file: 'packages/play-runtime/modal-supervisor-sidecar.ts',
65
+ name: 'PREWARM_EXECUTE_AT_ADMISSION',
66
+ },
67
+ ],
68
+ },
69
+ H1: {
70
+ perRun: true,
71
+ summary:
72
+ 'Keep the 50 ms runner readiness cadence through the boot window (Modal launch; the Daytona launcher keeps the static default).',
73
+ constants: [
74
+ {
75
+ file: 'packages/play-runtime/runner-backends/backends/detached-runner-readiness-pacing.ts',
76
+ name: 'EXTENDED_FAST_READINESS_WINDOW',
77
+ },
78
+ ],
79
+ },
80
+ P1: {
81
+ perRun: true,
82
+ summary:
83
+ 'Overlap the pre-create traffic policy read with the executor-token mint (worker and executor-token route).',
84
+ constants: [
85
+ {
86
+ file: 'packages/play-runtime/runtime-traffic-policy.ts',
87
+ name: 'OVERLAP_RUNTIME_TRAFFIC_POLICY_READ',
88
+ },
89
+ ],
90
+ },
91
+ P2: {
92
+ perRun: true,
93
+ summary:
94
+ "Write a new source run's artifact without the R2 existence read (admission).",
95
+ constants: [
96
+ {
97
+ file: 'apps/deepline-api/src/lib/plays/source-run-admission.ts',
98
+ name: 'PUT_NEW_SOURCE_RUN_ARTIFACT_WITHOUT_HEAD',
99
+ },
100
+ ],
101
+ },
102
+ E2: {
103
+ perRun: true,
104
+ summary:
105
+ "Start the run's Modal sandbox at admission (app hint) and adopt it at launch (worker).",
106
+ constants: [
107
+ {
108
+ file: 'apps/deepline-api/src/lib/plays/early-sandbox-hint.ts',
109
+ name: 'PREVIEW_EARLY_SANDBOX_HINT',
110
+ },
111
+ { file: MODAL, name: 'EARLY_MODAL_SANDBOX_START' },
112
+ ],
113
+ },
114
+ S1: {
115
+ perRun: true,
116
+ summary:
117
+ 'With E2, start the supervisor sidecar on the early sandbox; the in-process launch reuses it instead of creating one (worker).',
118
+ constants: [{ file: MODAL, name: 'START_SUPERVISOR_WITH_EARLY_SANDBOX' }],
119
+ },
120
+ A1: {
121
+ perRun: true,
122
+ summary:
123
+ 'Adopting an in-process early sandbox checks the tags this process set instead of reading them back (worker).',
124
+ constants: [
125
+ { file: MODAL, name: 'ADOPT_IN_PROCESS_EARLY_SANDBOX_WITHOUT_TAG_READ' },
126
+ ],
127
+ },
128
+ PB: {
129
+ perRun: true,
130
+ summary:
131
+ 'With E2 and the baked image, start the baked runner (waiting for its config) on the early sandbox; the in-process launch delivers the envelope, config last (worker).',
132
+ constants: [{ file: MODAL, name: 'PREBOOT_RUNNER_IN_EARLY_SANDBOX' }],
133
+ },
134
+ PWM: {
135
+ perRun: true,
136
+ summary:
137
+ "With B, the admission prewarm also loads the run's first tool's provider custom-operations module (no reads).",
138
+ constants: [
139
+ {
140
+ file: 'apps/deepline-api/src/lib/integrations/runtime-execute-prewarm-sender.ts',
141
+ name: 'PREWARM_RUN_TOOL_MODULE',
142
+ },
143
+ ],
144
+ },
145
+ PWT: {
146
+ perRun: true,
147
+ summary:
148
+ 'With B, the admission prewarm resolves the run\'s first tool\'s catalog entry instead of the path id "prewarm" (same single read).',
149
+ constants: [
150
+ {
151
+ file: 'apps/deepline-api/src/lib/integrations/runtime-execute-prewarm-sender.ts',
152
+ name: 'PREWARM_RUN_TOOL_CATALOG',
153
+ },
154
+ ],
155
+ },
156
+ PWV: {
157
+ perRun: true,
158
+ summary:
159
+ "With B and PWT, the admission prewarm compiles the run's first tool's input validator from its cached entry (CPU only).",
160
+ constants: [
161
+ {
162
+ file: 'apps/deepline-api/src/lib/integrations/runtime-execute-prewarm-sender.ts',
163
+ name: 'PREWARM_RUN_TOOL_VALIDATOR',
164
+ },
165
+ ],
166
+ },
167
+ PI: {
168
+ perRun: true,
169
+ summary:
170
+ "Read a caller run's persisted start intent together with the admission scope reservation and replay read, not after them (run route).",
171
+ constants: [
172
+ {
173
+ file: 'apps/deepline-api/src/lib/plays/start-run.ts',
174
+ name: 'OVERLAP_PERSISTED_START_INTENT_READ',
175
+ },
176
+ ],
177
+ },
178
+ CA: {
179
+ perRun: true,
180
+ summary:
181
+ 'With E2, the hinted worker names itself in the hint reply, admission records it on the run task, and other workers yield that first claim for a bounded window (worker and admission; needs CAR).',
182
+ constants: [
183
+ {
184
+ file: 'packages/play-runtime/scheduler-backends/early-sandbox-claim-affinity.ts',
185
+ name: 'CLAIM_AFFINITY_FOR_EARLY_SANDBOX',
186
+ },
187
+ ],
188
+ },
189
+ V3: {
190
+ perRun: true,
191
+ summary:
192
+ 'Start tool-call plays before the run.created projection (admission launch plan).',
193
+ constants: [
194
+ {
195
+ file: 'apps/deepline-api/src/lib/plays/launch/plan.ts',
196
+ name: 'START_TOOL_PLAYS_BEFORE_RUN_PROJECTION',
197
+ },
198
+ ],
199
+ },
200
+ F3: {
201
+ perRun: true,
202
+ summary:
203
+ 'Run the Preview callback health probe detached after admission (admission).',
204
+ constants: [
205
+ {
206
+ file: 'apps/deepline-api/src/lib/plays/runtime-deployment.ts',
207
+ name: 'DETACH_PREVIEW_CALLBACK_HEALTH',
208
+ },
209
+ ],
210
+ },
211
+ RH: {
212
+ perRun: false,
213
+ summary:
214
+ "Run status route holds a watch snapshot with waitForTerminalMs until the run is terminal, bounded. E3 and the SDK's RUN_WATCH_SERVER_WAIT_MS need it: without RH the SDK setting would poll with no delay.",
215
+ constants: [
216
+ {
217
+ file: 'apps/deepline-api/src/app/api/v2/runs/[runId]/route.ts',
218
+ name: 'RUN_WATCH_SERVER_HOLD',
219
+ },
220
+ ],
221
+ reason:
222
+ 'read route: GET /api/v2/runs/{runId} does not read the launch, so it has no run-carried override.',
223
+ },
224
+ E3: {
225
+ perRun: false,
226
+ summary: 'Run watch reads the Postgres terminal first.',
227
+ constants: [
228
+ {
229
+ file: 'apps/deepline-api/src/app/api/v2/runs/[runId]/route.ts',
230
+ name: 'RUN_WATCH_POSTGRES_TERMINAL',
231
+ },
232
+ ],
233
+ reason:
234
+ 'read route: GET /api/v2/runs/{runId} does not read the launch, so it has no run-carried override.',
235
+ },
236
+ CAR: {
237
+ perRun: false,
238
+ summary:
239
+ "Worker claim gate that reads a task's recorded early-sandbox owner (CA) before each claim.",
240
+ constants: [
241
+ {
242
+ file: 'packages/play-runtime/scheduler-backends/early-sandbox-claim-affinity.ts',
243
+ name: 'EARLY_SANDBOX_CLAIM_AFFINITY_READER',
244
+ },
245
+ ],
246
+ reason:
247
+ 'worker-process: the gate runs before a claim, when no run (and so no override) is known yet.',
248
+ },
249
+ E5: {
250
+ perRun: false,
251
+ summary: 'Preview worker holds the direct LISTEN wake session.',
252
+ constants: [
253
+ {
254
+ file: 'packages/play-runtime/scheduler-backends/absurd-runtime-worker.ts',
255
+ name: 'PREVIEW_WORKER_DIRECT_NOTIFY_WAKE',
256
+ },
257
+ ],
258
+ reason:
259
+ "worker-process: decided at worker startup for the whole queue's claim loop, before any run.",
260
+ },
261
+ C1: {
262
+ perRun: false,
263
+ summary: 'Play typecheck worker globals.',
264
+ constants: [
265
+ {
266
+ file: 'packages/integrations/plays/cloud-tool-typecheck.ts',
267
+ name: 'PLAY_TYPECHECK_WORKER_GLOBALS',
268
+ },
269
+ ],
270
+ reason:
271
+ 'module-load time: shapes the checker program an instance builds once.',
272
+ },
273
+ C3: {
274
+ perRun: false,
275
+ summary: 'Durable typecheck verdicts.',
276
+ constants: [
277
+ {
278
+ file: 'apps/deepline-api/src/lib/plays/typecheck-verdict-store.ts',
279
+ name: 'DURABLE_TYPECHECK_VERDICTS',
280
+ },
281
+ ],
282
+ reason:
283
+ "shared cache: one run's verdict writes would warm the other arm, so a per-run A/B is confounded.",
284
+ },
285
+ C4: {
286
+ perRun: false,
287
+ summary: 'Launch credential check without the checker.',
288
+ constants: [
289
+ {
290
+ file: 'apps/deepline-api/src/lib/plays/launch-credential-check.ts',
291
+ name: 'LAUNCH_CREDENTIAL_CHECK_WITHOUT_CHECKER',
292
+ },
293
+ ],
294
+ reason:
295
+ 'module-load time: selects which credential-check module the instance loads and keeps.',
296
+ },
297
+ C5: {
298
+ perRun: false,
299
+ summary: 'Start the source-verdict read during route admission.',
300
+ constants: [
301
+ {
302
+ file: 'apps/deepline-api/src/lib/plays/play-source-verdicts.ts',
303
+ name: 'EARLY_SOURCE_VERDICT_READ',
304
+ },
305
+ ],
306
+ reason:
307
+ 'shared cache: reads the durable verdict cache that the other arm writes; also starts while auth runs, before the header is authorized.',
308
+ },
309
+ E4: {
310
+ perRun: false,
311
+ summary: 'Deferred run billing.',
312
+ constants: [
313
+ {
314
+ file: 'packages/integrations/billing/charge-lifecycle.ts',
315
+ name: 'DEFERRED_RUN_BILLING',
316
+ },
317
+ ],
318
+ reason: 'billing: E4/F1 are uncertified, never per-run.',
319
+ },
320
+ F1: {
321
+ perRun: false,
322
+ summary: 'Pre-admit a deferred run in the prewarm.',
323
+ constants: [
324
+ {
325
+ file: 'packages/integrations/billing/charge-lifecycle.ts',
326
+ name: 'DEFERRED_RUN_PREADMIT',
327
+ },
328
+ ],
329
+ reason: 'billing: E4/F1 are uncertified, never per-run.',
330
+ },
331
+ } as const satisfies Record<string, ExperimentSwitchDefinition>;
332
+
333
+ /**
334
+ * Switches whose experiments lost and whose code was deleted (see
335
+ * docs/runtime-speed-experiments.md). Each was per-run and off by default, so
336
+ * the static default is now the only behaviour. A caller that still sends one
337
+ * (an older CLI or measurement script) gets exactly what its run got before
338
+ * with `=0`, so the header parser accepts these ids and ignores them instead
339
+ * of turning a previously accepted header into a 400.
340
+ */
341
+ export const RETIRED_EXPERIMENT_SWITCHES: ReadonlySet<string> = new Set([
342
+ 'F2',
343
+ 'G65',
344
+ 'DH',
345
+ 'PS',
346
+ 'EB',
347
+ 'EC',
348
+ ]);
349
+
350
+ export type ExperimentSwitchId = keyof typeof EXPERIMENT_SWITCHES;
351
+
352
+ export type PerRunExperimentSwitchId = {
353
+ [Id in ExperimentSwitchId]: (typeof EXPERIMENT_SWITCHES)[Id]['perRun'] extends true
354
+ ? Id
355
+ : never;
356
+ }[ExperimentSwitchId];
357
+
358
+ /** A run's validated overrides. Absent ids keep their static default. */
359
+ export type ExperimentSwitchOverrides = Readonly<
360
+ Partial<Record<PerRunExperimentSwitchId, boolean>>
361
+ >;
362
+
363
+ function isExperimentSwitchId(value: string): value is ExperimentSwitchId {
364
+ return Object.prototype.hasOwnProperty.call(EXPERIMENT_SWITCHES, value);
365
+ }
366
+
367
+ function isPerRunExperimentSwitchId(
368
+ value: string,
369
+ ): value is PerRunExperimentSwitchId {
370
+ return isExperimentSwitchId(value) && EXPERIMENT_SWITCHES[value].perRun;
371
+ }
372
+
373
+ /**
374
+ * The value a switch has for this run: the run's override when the switch is
375
+ * per-run and the run carries one, otherwise the call site's static constant.
376
+ */
377
+ export function resolveExperimentSwitch(
378
+ id: PerRunExperimentSwitchId,
379
+ overrides: ExperimentSwitchOverrides | null | undefined,
380
+ staticDefault: boolean,
381
+ ): boolean {
382
+ if (!isPerRunExperimentSwitchId(id)) return staticDefault;
383
+ const override = overrides?.[id];
384
+ return typeof override === 'boolean' ? override : staticDefault;
385
+ }
386
+
387
+ export class ExperimentSwitchesHeaderError extends Error {
388
+ constructor(message: string) {
389
+ super(message);
390
+ this.name = 'ExperimentSwitchesHeaderError';
391
+ }
392
+ }
393
+
394
+ /**
395
+ * Strict parser for the request wire form `B=1,H1=0`. An empty value, an
396
+ * unknown id, a switch that is not per-run, a repeated id or a value other
397
+ * than 0/1 throws: an override is never silently dropped. The one exception is
398
+ * a retired id (RETIRED_EXPERIMENT_SWITCHES), which is validated the same way
399
+ * and then ignored, because its code no longer exists.
400
+ */
401
+ export function parseExperimentSwitchesHeader(
402
+ value: string,
403
+ ): ExperimentSwitchOverrides {
404
+ const trimmed = value.trim();
405
+ if (!trimmed) {
406
+ throw new ExperimentSwitchesHeaderError(
407
+ `${EXPERIMENT_SWITCHES_HEADER} is empty. Use <id>=0|1 pairs separated by commas, e.g. B=1,H1=0.`,
408
+ );
409
+ }
410
+ const overrides: Partial<Record<PerRunExperimentSwitchId, boolean>> = {};
411
+ const retired = new Set<string>();
412
+ for (const entry of trimmed.split(',')) {
413
+ const match = /^\s*([A-Za-z0-9_]+)\s*=\s*([01])\s*$/.exec(entry);
414
+ if (!match) {
415
+ throw new ExperimentSwitchesHeaderError(
416
+ `${EXPERIMENT_SWITCHES_HEADER} entry "${entry.trim()}" is malformed. Use <id>=0 or <id>=1.`,
417
+ );
418
+ }
419
+ const id = match[1]!;
420
+ if (RETIRED_EXPERIMENT_SWITCHES.has(id)) {
421
+ if (retired.has(id)) {
422
+ throw new ExperimentSwitchesHeaderError(
423
+ `Experiment switch "${id}" is repeated in ${EXPERIMENT_SWITCHES_HEADER}.`,
424
+ );
425
+ }
426
+ retired.add(id);
427
+ continue;
428
+ }
429
+ if (!isExperimentSwitchId(id)) {
430
+ throw new ExperimentSwitchesHeaderError(
431
+ `Unknown experiment switch "${id}". Known: ${Object.keys(EXPERIMENT_SWITCHES).join(', ')}.`,
432
+ );
433
+ }
434
+ const definition: ExperimentSwitchDefinition = EXPERIMENT_SWITCHES[id];
435
+ if (!definition.perRun) {
436
+ // `in` narrowing: some app tsconfigs (play-runner) lack strictNullChecks,
437
+ // where a boolean discriminant does not narrow the union.
438
+ const reason = 'reason' in definition ? definition.reason : 'not per-run';
439
+ throw new ExperimentSwitchesHeaderError(
440
+ `Experiment switch "${id}" cannot be overridden per run (${reason})`,
441
+ );
442
+ }
443
+ const perRunId = id as PerRunExperimentSwitchId;
444
+ if (perRunId in overrides) {
445
+ throw new ExperimentSwitchesHeaderError(
446
+ `Experiment switch "${id}" is repeated in ${EXPERIMENT_SWITCHES_HEADER}.`,
447
+ );
448
+ }
449
+ overrides[perRunId] = match[2] === '1';
450
+ }
451
+ return overrides;
452
+ }
453
+
454
+ /**
455
+ * Tolerant reader for overrides carried on a durable launch, a worker request
456
+ * or an early-sandbox hint. These were validated at admission; a newer writer
457
+ * may name a switch this build does not know, which keeps its static default
458
+ * here rather than failing the run. Returns null when nothing applies.
459
+ */
460
+ export function readCarriedExperimentSwitchOverrides(
461
+ value: unknown,
462
+ ): ExperimentSwitchOverrides | null {
463
+ if (!value || typeof value !== 'object' || Array.isArray(value)) return null;
464
+ const overrides: Partial<Record<PerRunExperimentSwitchId, boolean>> = {};
465
+ for (const [id, override] of Object.entries(value)) {
466
+ if (isPerRunExperimentSwitchId(id) && typeof override === 'boolean') {
467
+ overrides[id] = override;
468
+ }
469
+ }
470
+ return Object.keys(overrides).length > 0 ? overrides : null;
471
+ }
472
+
473
+ /** `B=1,H1=0`, for log lines that measurement joins on. */
474
+ export function formatExperimentSwitchOverrides(
475
+ overrides: ExperimentSwitchOverrides,
476
+ ): string {
477
+ return Object.entries(overrides)
478
+ .map(([id, value]) => `${id}=${value ? 1 : 0}`)
479
+ .join(',');
480
+ }