deepline 0.3.491 → 0.3.493

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 (184) hide show
  1. package/dist/bundling-sources/sdk/src/client-core.ts +11 -5
  2. package/dist/bundling-sources/sdk/src/plays/run-request.ts +1 -0
  3. package/dist/bundling-sources/sdk/src/release.ts +1 -1
  4. package/dist/bundling-sources/sdk/src/types.ts +6 -0
  5. package/dist/bundling-sources/shared_libs/monitors/fleets/verdict.ts +25 -9
  6. package/dist/bundling-sources/shared_libs/observability/queue-health.ts +9 -0
  7. package/dist/bundling-sources/shared_libs/observability/queue-item-connectors.ts +1 -0
  8. package/dist/bundling-sources/shared_libs/observability/scheduled-jobs.json +54 -0
  9. package/dist/bundling-sources/shared_libs/observability/scheduled-work-manual.ts +5 -4
  10. package/dist/bundling-sources/shared_libs/play-runtime/crustdata-company-identify-batching.ts +162 -0
  11. package/dist/bundling-sources/shared_libs/play-runtime/daytona-sandbox-reaper.ts +19 -3
  12. package/dist/bundling-sources/shared_libs/play-runtime/output-size-limits.ts +5 -3
  13. package/dist/bundling-sources/shared_libs/play-runtime/play-runtime-batching-registry.ts +2 -0
  14. package/dist/bundling-sources/shared_libs/play-runtime/receipt-sql.ts +17 -23
  15. package/dist/bundling-sources/shared_libs/play-runtime/runner-backends/backends/daytona-modal-fallback.ts +28 -1
  16. package/dist/bundling-sources/shared_libs/play-runtime/runtime-api.ts +293 -110
  17. package/dist/bundling-sources/shared_libs/play-runtime/runtime-sandbox-placement-policy.ts +75 -0
  18. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/absurd-runtime-worker.ts +20 -0
  19. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/postgres.ts +412 -59
  20. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/runtime-sandbox-capacity.ts +38 -10
  21. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/runtime-sandbox-ledger-reconciliation.ts +19 -11
  22. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/runtime-sandbox-operation.ts +165 -12
  23. package/dist/bundling-sources/shared_libs/play-runtime/work-receipt-state-machine.ts +358 -0
  24. package/dist/bundling-sources/shared_libs/product-notifications/catalog.ts +104 -2
  25. package/dist/bundling-sources/shared_libs/product-notifications/configuration-draft.ts +7 -2
  26. package/dist/bundling-sources/shared_libs/product-notifications/configuration.ts +4 -0
  27. package/dist/bundling-sources/shared_libs/product-notifications/content.ts +13 -0
  28. package/dist/bundling-sources/shared_libs/product-notifications/email.ts +49 -10
  29. package/dist/bundling-sources/shared_libs/product-notifications/events.ts +22 -0
  30. package/dist/bundling-sources/shared_libs/product-notifications/slack-delivery.ts +6 -1
  31. package/dist/cli/{auth-FXAC472Y.mjs → auth-3NAY3AST.mjs} +5 -5
  32. package/dist/cli/auth-KYQLDI6V.js +43 -0
  33. package/dist/cli/{billing-PDOA3KZ3.js → billing-FSB4BBBO.js} +80 -80
  34. package/dist/cli/{billing-UM4LE33K.mjs → billing-SBWQCC7B.mjs} +7 -7
  35. package/dist/cli/{chunk-HSJK6EMU.mjs → chunk-2BRTYIAH.mjs} +23 -9
  36. package/dist/cli/{chunk-B2TXKEED.js → chunk-2H4OFVR5.js} +5 -5
  37. package/dist/cli/{chunk-BCKIBVEJ.js → chunk-2I7VN65H.js} +10 -10
  38. package/dist/cli/{chunk-LMG7O6XJ.mjs → chunk-2IHGVAGT.mjs} +2 -2
  39. package/dist/cli/{chunk-5KMGKG4B.js → chunk-2NGIBJM2.js} +8 -8
  40. package/dist/cli/{chunk-2MW6TGNZ.mjs → chunk-3QGUWB6E.mjs} +4 -4
  41. package/dist/cli/{chunk-ORKLMANE.mjs → chunk-4IJFQV3O.mjs} +1 -1
  42. package/dist/cli/{chunk-PIPEPCRL.mjs → chunk-4NIGNT7X.mjs} +1 -1
  43. package/dist/cli/{chunk-H6UIS36S.mjs → chunk-5DIZ2G74.mjs} +2 -2
  44. package/dist/cli/{chunk-TKLDKIJU.mjs → chunk-5JPKRDFC.mjs} +7 -5
  45. package/dist/cli/{chunk-FTXIJTV2.mjs → chunk-5WOPBX3H.mjs} +1 -1
  46. package/dist/cli/{chunk-KGJVDR4Q.mjs → chunk-67ZG7XZV.mjs} +3 -3
  47. package/dist/cli/{chunk-DUFRH43S.js → chunk-7HABWOMB.js} +261 -261
  48. package/dist/cli/{chunk-MN6DCV63.js → chunk-AGTXL6LZ.js} +5 -5
  49. package/dist/cli/{chunk-Q7G7CU4W.js → chunk-AWMCZAUR.js} +3 -3
  50. package/dist/cli/{chunk-HHTVRICE.js → chunk-C2MRDPA5.js} +17 -17
  51. package/dist/cli/{chunk-ONONSV2Q.mjs → chunk-DVQHG6AM.mjs} +1 -0
  52. package/dist/cli/{chunk-O3K22ZC7.js → chunk-DWJ4RXSA.js} +34 -11
  53. package/dist/cli/{chunk-MAM6ZOVS.mjs → chunk-DYP27IWC.mjs} +99 -24
  54. package/dist/cli/{chunk-NUOKAKZW.mjs → chunk-E5ILCMKQ.mjs} +1 -1
  55. package/dist/cli/{chunk-OLHWZM4A.mjs → chunk-EBAS3UT2.mjs} +6 -6
  56. package/dist/cli/{chunk-74H5OTRM.js → chunk-EFO5GVYS.js} +1 -1
  57. package/dist/cli/{chunk-X7YOVJBC.js → chunk-EYG336YV.js} +132 -57
  58. package/dist/cli/{chunk-YV4DP6KQ.mjs → chunk-GSC35TPZ.mjs} +2 -2
  59. package/dist/cli/{chunk-EPD7NSDJ.mjs → chunk-HLM2UFQ5.mjs} +13 -5
  60. package/dist/cli/{chunk-IG3GSBBE.js → chunk-HTUSTPP4.js} +32 -32
  61. package/dist/cli/{chunk-CFPFTS3A.mjs → chunk-IJJ6D5WL.mjs} +12 -12
  62. package/dist/cli/{chunk-TH7JLR5D.js → chunk-IJOHWGPF.js} +6 -6
  63. package/dist/cli/{chunk-RFSXXDH7.js → chunk-IOBPR2VO.js} +17 -17
  64. package/dist/cli/{chunk-HNG2U3IG.js → chunk-IOMEJKGK.js} +2 -2
  65. package/dist/cli/{chunk-A7ZMG2QZ.mjs → chunk-IRDYBMA7.mjs} +34 -11
  66. package/dist/cli/{chunk-ZEZ3CZ3X.js → chunk-JO2JZUBR.js} +3 -3
  67. package/dist/cli/{chunk-4IP5MLWU.js → chunk-JO5FFSR7.js} +6 -1
  68. package/dist/cli/{chunk-SJOHQ2WW.js → chunk-KCDZWS2B.js} +56 -56
  69. package/dist/cli/{chunk-CGDUFVNE.mjs → chunk-KGDDQMI7.mjs} +2 -2
  70. package/dist/cli/{chunk-OYJTLEB4.mjs → chunk-KKSKYZO4.mjs} +1 -1
  71. package/dist/cli/{chunk-TMAS3WH3.js → chunk-LGTZYI5B.js} +21 -21
  72. package/dist/cli/{chunk-SUMKWA53.js → chunk-MLEUOTEQ.js} +73 -59
  73. package/dist/cli/{chunk-3WRWPNHE.mjs → chunk-NALJJTPS.mjs} +5 -5
  74. package/dist/cli/{chunk-27MRSV6O.js → chunk-NPNAEPY4.js} +26 -26
  75. package/dist/cli/{chunk-AEKJZJ4R.js → chunk-NWIDPT7H.js} +4 -4
  76. package/dist/cli/{chunk-AUQQVDAB.js → chunk-PKV5CD3W.js} +1 -0
  77. package/dist/cli/{chunk-3FMU6F6E.js → chunk-QTPS5RLG.js} +15 -15
  78. package/dist/cli/{chunk-4OLXMJ2L.js → chunk-RLX5ZFNX.js} +8 -6
  79. package/dist/cli/{chunk-DEJIN4DF.mjs → chunk-SAW3FBCA.mjs} +5 -0
  80. package/dist/cli/{chunk-S4CYM4EC.mjs → chunk-SXD2EYAH.mjs} +6 -6
  81. package/dist/cli/{chunk-YNWET57Z.js → chunk-TIFRRLZT.js} +7 -7
  82. package/dist/cli/{chunk-6PMM7VJN.mjs → chunk-TNQOR6XQ.mjs} +1 -1
  83. package/dist/cli/{chunk-6UARNRXT.js → chunk-UCMY2SO2.js} +23 -15
  84. package/dist/cli/{chunk-X75ONMDJ.mjs → chunk-VFHIYWMC.mjs} +5 -5
  85. package/dist/cli/{chunk-ZX2FXZJN.js → chunk-WCZFCOMZ.js} +7 -7
  86. package/dist/cli/{chunk-IIO7UOVW.mjs → chunk-WTFI7BVH.mjs} +3 -3
  87. package/dist/cli/{chunk-I64PYCTB.js → chunk-XLE7KXE7.js} +2 -2
  88. package/dist/cli/{chunk-CRBBGKRH.js → chunk-XQX476QK.js} +14 -14
  89. package/dist/cli/{chunk-WCWVXN3R.mjs → chunk-Y3NN2UAF.mjs} +2 -2
  90. package/dist/cli/{chunk-3K4IAV7I.mjs → chunk-ZQH4TXM2.mjs} +2 -2
  91. package/dist/cli/{chunk-VC3SH4QW.mjs → chunk-ZVW7EWTC.mjs} +7 -7
  92. package/dist/cli/{chunk-QBOE5LEH.mjs → chunk-ZVXB7OU7.mjs} +1 -1
  93. package/dist/cli/command-docs.js +2 -2
  94. package/dist/cli/command-docs.mjs +1 -1
  95. package/dist/cli/{csv-IGPAPAES.js → csv-DKYZE2KM.js} +13 -13
  96. package/dist/cli/{csv-RNMQHECI.mjs → csv-XR22YQXT.mjs} +4 -4
  97. package/dist/cli/{db-CKMVVGXJ.mjs → db-EOKG36YN.mjs} +7 -7
  98. package/dist/cli/{db-32OXM3TO.js → db-RNYRTCMV.js} +28 -28
  99. package/dist/cli/describe-ICFXOQBW.mjs +50 -0
  100. package/dist/cli/describe-RS75I33X.js +50 -0
  101. package/dist/cli/describe-core.d.mts +2 -2
  102. package/dist/cli/describe-core.d.ts +2 -2
  103. package/dist/cli/describe-core.js +4 -4
  104. package/dist/cli/describe-core.mjs +3 -3
  105. package/dist/cli/{executions-S4M4JMCU.mjs → executions-3RAOACNS.mjs} +7 -7
  106. package/dist/cli/{executions-PCZS7B2M.js → executions-OBWPSWH7.js} +9 -9
  107. package/dist/cli/{feedback-FGUJVCBS.mjs → feedback-6EQRVBKF.mjs} +4 -4
  108. package/dist/cli/{feedback-6EGKPZ3U.js → feedback-6GPEUBLF.js} +8 -8
  109. package/dist/cli/getting-started.d.mts +12 -10
  110. package/dist/cli/getting-started.d.ts +12 -10
  111. package/dist/cli/getting-started.js +4 -2
  112. package/dist/cli/getting-started.mjs +3 -1
  113. package/dist/cli/{git-LR4ARG5H.mjs → git-CVQ24DTR.mjs} +5 -5
  114. package/dist/cli/git-DEFE4SSB.js +25 -0
  115. package/dist/cli/index.js +143 -143
  116. package/dist/cli/index.mjs +43 -43
  117. package/dist/cli/{legacy-noops-S37PJ7OJ.mjs → legacy-noops-QULJZYNH.mjs} +5 -5
  118. package/dist/cli/{legacy-noops-VX4JEQR6.js → legacy-noops-XPOATG5V.js} +8 -8
  119. package/dist/cli/{monitors-H3AX6CZG.mjs → monitors-AFT5YJPZ.mjs} +49 -9
  120. package/dist/cli/{monitors-ADASJI72.js → monitors-OJ4CMREE.js} +169 -129
  121. package/dist/cli/{org-ZRJ3C56J.js → org-RKT52CSL.js} +73 -73
  122. package/dist/cli/{org-D3F5LUDW.mjs → org-WHKNXANG.mjs} +7 -7
  123. package/dist/cli/{pages-PSHU366M.mjs → pages-EEAQFGVD.mjs} +7 -7
  124. package/dist/cli/{pages-5TKO3644.js → pages-SOE3R667.js} +18 -18
  125. package/dist/cli/{play-AUDDK7XB.mjs → play-LXFWHUBP.mjs} +147 -52
  126. package/dist/cli/{play-VDAETQDI.js → play-OTJJ4E4T.js} +661 -566
  127. package/dist/cli/{providers-IURPWRWD.js → providers-RU4HIAT3.js} +10 -10
  128. package/dist/cli/{providers-X7YMZSKQ.mjs → providers-U3CPKTMM.mjs} +7 -7
  129. package/dist/cli/{quickstart-6AGWVFNX.mjs → quickstart-75AO2AJC.mjs} +5 -5
  130. package/dist/cli/{quickstart-Y6GFXLDV.js → quickstart-N72IVA2Y.js} +13 -13
  131. package/dist/cli/{run-TG7J45NS.mjs → run-IGWFL6WH.mjs} +29 -28
  132. package/dist/cli/{run-TQFGOXNT.js → run-MMYES3IW.js} +44 -43
  133. package/dist/cli/search-EQURI7SZ.js +32 -0
  134. package/dist/cli/{search-3G4J2GC6.mjs → search-FIYESBLZ.mjs} +8 -8
  135. package/dist/cli/{search-output-NPZICK5C.js → search-output-2FXN4ZWD.js} +9 -6
  136. package/dist/cli/{search-output-ZGNHDCLL.mjs → search-output-3I5KSW36.mjs} +7 -4
  137. package/dist/cli/{secrets-KTTHT4ZG.js → secrets-HCZQ6JNP.js} +16 -16
  138. package/dist/cli/{secrets-EAWJBRUD.mjs → secrets-O7IUQ3VT.mjs} +7 -7
  139. package/dist/cli/{sessions-S4YVBPAW.js → sessions-5AYTT4IW.js} +6 -6
  140. package/dist/cli/{sessions-D4Q3LCEQ.mjs → sessions-KY7A3EZO.mjs} +5 -5
  141. package/dist/cli/{settings-BAK3VOWW.js → settings-P4VFYWJK.js} +80 -80
  142. package/dist/cli/{settings-JUORW6NL.mjs → settings-WSMRR2GS.mjs} +13 -13
  143. package/dist/cli/{setup-NJPHTW32.js → setup-J7CA3EJL.js} +57 -57
  144. package/dist/cli/{setup-AXFGCXJE.mjs → setup-YTVKVQC7.mjs} +13 -13
  145. package/dist/cli/skills-KKPEGLAR.js +36 -0
  146. package/dist/cli/{skills-SKJVDJEF.mjs → skills-W5WY764N.mjs} +10 -10
  147. package/dist/cli/tools-65Y4QTW6.js +57 -0
  148. package/dist/cli/{tools-63THJVJZ.mjs → tools-YECI6N3N.mjs} +20 -20
  149. package/dist/cli/{update-KSID5F74.mjs → update-BIFBIEII.mjs} +10 -10
  150. package/dist/cli/update-WDG7VFOW.js +34 -0
  151. package/dist/cli/{workflow-BLSMWBKW.js → workflow-D2TUFFH6.js} +18 -18
  152. package/dist/cli/{workflow-C4B54X2Z.mjs → workflow-XZVDNARC.mjs} +8 -8
  153. package/dist/{client-CQgGFxAH.d.ts → client-BDB_VbE4.d.ts} +1 -1
  154. package/dist/{client-B4R1scm2.d.mts → client-CNzJ1LTF.d.mts} +1 -1
  155. package/dist/{client-core-DMvHWjub.d.mts → client-core-CSv3DlPD.d.mts} +76 -5
  156. package/dist/{client-core-BUNjKBKq.d.ts → client-core-xEa9x3pl.d.ts} +76 -5
  157. package/dist/index.d.mts +3 -3
  158. package/dist/index.d.ts +3 -3
  159. package/dist/index.js +10 -10
  160. package/dist/index.mjs +7 -7
  161. package/dist/install-integrity.json +115 -114
  162. package/dist/plays/play-description.d.mts +1 -1
  163. package/dist/plays/play-description.d.ts +1 -1
  164. package/dist/plays/run-request.d.mts +1 -1
  165. package/dist/plays/run-request.d.ts +1 -1
  166. package/dist/plays/run-request.js +2 -2
  167. package/dist/plays/run-request.mjs +1 -1
  168. package/dist/release.d.mts +1 -1
  169. package/dist/release.d.ts +1 -1
  170. package/dist/release.js +2 -2
  171. package/dist/release.mjs +1 -1
  172. package/dist/server.d.mts +2 -2
  173. package/dist/server.d.ts +2 -2
  174. package/dist/server.js +6 -6
  175. package/dist/server.mjs +4 -4
  176. package/package.json +1 -1
  177. package/dist/cli/auth-QIDL3YCW.js +0 -43
  178. package/dist/cli/describe-67GMIEIO.js +0 -50
  179. package/dist/cli/describe-QR7I3VOF.mjs +0 -50
  180. package/dist/cli/git-PIH5Z5LN.js +0 -25
  181. package/dist/cli/search-VC43NUY7.js +0 -32
  182. package/dist/cli/skills-4NQZISNK.js +0 -36
  183. package/dist/cli/tools-BAHK33FQ.js +0 -57
  184. package/dist/cli/update-EMIGRGJ7.js +0 -34
@@ -12,6 +12,10 @@ import { RUNTIME_SANDBOX_CAPACITY_LIMIT_ERROR_NAME } from '../runner-backends/ty
12
12
  import {
13
13
  RuntimeSandboxOperationState,
14
14
  RuntimeSandboxProvider,
15
+ runtimeSandboxOperationFromStates,
16
+ runtimeSandboxOperationFromStatesSqlCast,
17
+ runtimeSandboxOperationStateSqlType,
18
+ runtimeSandboxOperationTargetState,
15
19
  } from './runtime-sandbox-operation';
16
20
  import type { ComputeBillingJobStatus } from './compute-billing-job-state';
17
21
  import { readComputeBillingJobStatus } from './compute-billing-job-state';
@@ -84,6 +88,17 @@ function schemaTables(options?: Pick<PostgresSchedulerOptions, 'schema'>) {
84
88
  };
85
89
  }
86
90
 
91
+ /** Every state write below takes its set and target from the owner's table. */
92
+ function operationStateSqlTypes(
93
+ options?: Pick<PostgresSchedulerOptions, 'schema'>,
94
+ ): { operationState: string; operationStates: string } {
95
+ const schema = quoteIdent(options?.schema ?? 'runtime_scheduler');
96
+ return {
97
+ operationState: runtimeSandboxOperationStateSqlType(schema),
98
+ operationStates: runtimeSandboxOperationFromStatesSqlCast(schema),
99
+ };
100
+ }
101
+
87
102
  function assertId(value: string, name: string): string {
88
103
  const normalized = value.trim();
89
104
  if (!normalized || normalized.length > 256) {
@@ -315,6 +330,7 @@ export async function reserveRuntimeSandboxCapacityLease(
315
330
  cleanup,
316
331
  } = schemaTables(options);
317
332
  const leaseId = input.leaseId?.trim() || randomUUID();
333
+ const { operationState } = operationStateSqlTypes(options);
318
334
  const unsentProviderOperationToken = unsentReservationInputs.get(input);
319
335
  const providerOperationToken =
320
336
  input.attempt === undefined
@@ -464,7 +480,7 @@ export async function reserveRuntimeSandboxCapacityLease(
464
480
  INSERT INTO ${operations} (
465
481
  operation_key, provider_operation_token, provider, run_id, attempt, org_id, environment, state
466
482
  )
467
- SELECT lease_id, $12, $4, $3, $8, $1, $9, $7
483
+ SELECT lease_id, $12, $4, $3, $8, $1, $9, $13::${operationState}
468
484
  FROM reserved_lease
469
485
  ON CONFLICT (operation_key) DO NOTHING
470
486
  RETURNING operation_key, provider_operation_token, provider,
@@ -500,6 +516,7 @@ export async function reserveRuntimeSandboxCapacityLease(
500
516
  input.mode ?? 'enforce',
501
517
  RUNTIME_SANDBOX_CAPACITY_LEASE_MS,
502
518
  providerOperationToken,
519
+ runtimeSandboxOperationTargetState('reserve'),
503
520
  ],
504
521
  );
505
522
  const limit = Number(
@@ -939,6 +956,7 @@ export async function abandonRuntimeSandboxOperationReservation(
939
956
  options?: Pick<PostgresSchedulerOptions, 'schema'>,
940
957
  ): Promise<boolean> {
941
958
  const { leases, operations, runs } = schemaTables(options);
959
+ const { operationStates } = operationStateSqlTypes(options);
942
960
  const result = await client.query<{ operation_key: string }>(
943
961
  `
944
962
  WITH custody_run AS MATERIALIZED (
@@ -959,7 +977,7 @@ export async function abandonRuntimeSandboxOperationReservation(
959
977
  SET state = $2,
960
978
  updated_at = clock_timestamp()
961
979
  WHERE operation.operation_key = $1
962
- AND operation.state = $3
980
+ AND operation.state = ANY($3::${operationStates})
963
981
  AND ($4::integer IS NULL OR operation.attempt <= $4)
964
982
  AND EXISTS (SELECT 1 FROM eligible_lease)
965
983
  AND (
@@ -981,8 +999,8 @@ export async function abandonRuntimeSandboxOperationReservation(
981
999
  `,
982
1000
  [
983
1001
  assertId(input.operationKey, 'operationKey'),
984
- RuntimeSandboxOperationState.Abandoned,
985
- RuntimeSandboxOperationState.Reserved,
1002
+ runtimeSandboxOperationTargetState('abandon_reservation'),
1003
+ runtimeSandboxOperationFromStates('abandon_reservation'),
986
1004
  input.attempt ?? null,
987
1005
  input.onlyWhileRunActive ?? false,
988
1006
  ],
@@ -1060,6 +1078,7 @@ export async function recordRecoveredDaytonaCancellationSandbox(
1060
1078
  ): Promise<boolean> {
1061
1079
  const { operations, leases, cleanup } = schemaTables(options);
1062
1080
  const schema = quoteIdent(options?.schema ?? 'runtime_scheduler');
1081
+ const { operationStates } = operationStateSqlTypes(options);
1063
1082
  const recorded = await client.query<{ operation_key: string }>(
1064
1083
  `WITH custody_run AS MATERIALIZED (
1065
1084
  SELECT run.run_id FROM ${schema}."runs" AS run
@@ -1079,7 +1098,7 @@ export async function recordRecoveredDaytonaCancellationSandbox(
1079
1098
  WHERE operation.operation_key = $1 FOR UPDATE OF operation
1080
1099
  ), promoted AS (
1081
1100
  UPDATE ${operations} AS operation
1082
- SET state = 'deleting',
1101
+ SET state = $4,
1083
1102
  sandbox_id = $2,
1084
1103
  routing_domain = $3,
1085
1104
  provider_created_at = clock_timestamp(),
@@ -1087,7 +1106,7 @@ export async function recordRecoveredDaytonaCancellationSandbox(
1087
1106
  FROM custody_operation
1088
1107
  WHERE operation.operation_key = custody_operation.operation_key
1089
1108
  AND operation.provider = 'daytona'
1090
- AND operation.state = 'reserved'
1109
+ AND operation.state = ANY($5::${operationStates})
1091
1110
  RETURNING operation.operation_key, operation.run_id, operation.attempt,
1092
1111
  operation.environment, operation.created_at
1093
1112
  ), resource AS (
@@ -1134,7 +1153,13 @@ export async function recordRecoveredDaytonaCancellationSandbox(
1134
1153
  SELECT promoted.operation_key FROM promoted
1135
1154
  JOIN recorded_attempt ON recorded_attempt.run_id = promoted.run_id
1136
1155
  JOIN bound ON bound.lease_id = promoted.operation_key`,
1137
- [input.operationKey, input.sandboxId, input.routingDomain],
1156
+ [
1157
+ input.operationKey,
1158
+ input.sandboxId,
1159
+ input.routingDomain,
1160
+ runtimeSandboxOperationTargetState('recover_daytona_cancellation'),
1161
+ runtimeSandboxOperationFromStates('recover_daytona_cancellation'),
1162
+ ],
1138
1163
  );
1139
1164
  return recorded.rows.length === 1;
1140
1165
  }
@@ -1153,6 +1178,7 @@ export async function recordDaytonaSandboxCleanupIntent(
1153
1178
  ): Promise<boolean> {
1154
1179
  const { operations, leases, cleanup } = schemaTables(options);
1155
1180
  const schema = quoteIdent(options?.schema ?? 'runtime_scheduler');
1181
+ const { operationStates } = operationStateSqlTypes(options);
1156
1182
  const recorded = await client.query<{ operation_key: string }>(
1157
1183
  `WITH custody_run AS MATERIALIZED (
1158
1184
  SELECT run.run_id FROM ${schema}."runs" AS run
@@ -1186,7 +1212,7 @@ export async function recordDaytonaSandboxCleanupIntent(
1186
1212
  AND operation.org_id = $3
1187
1213
  AND operation.provider_operation_token = $4
1188
1214
  AND operation.provider = 'daytona'
1189
- AND operation.state = 'reserved'
1215
+ AND operation.state = ANY($8::${operationStates})
1190
1216
  AND lease.released_at IS NULL
1191
1217
  AND lease.sandbox_id IS NULL
1192
1218
  ), queued AS (
@@ -1207,7 +1233,7 @@ export async function recordDaytonaSandboxCleanupIntent(
1207
1233
  job.environment, job.routing_domain
1208
1234
  ), promoted AS (
1209
1235
  UPDATE ${operations} AS operation
1210
- SET state = 'deleting',
1236
+ SET state = $7,
1211
1237
  sandbox_id = $5,
1212
1238
  routing_domain = $6,
1213
1239
  provider_created_at = clock_timestamp(),
@@ -1217,7 +1243,7 @@ export async function recordDaytonaSandboxCleanupIntent(
1217
1243
  AND queued.attempt = eligible.attempt
1218
1244
  AND queued.environment = eligible.environment
1219
1245
  WHERE operation.operation_key = eligible.operation_key
1220
- AND operation.state = 'reserved'
1246
+ AND operation.state = ANY($8::${operationStates})
1221
1247
  RETURNING operation.operation_key, operation.run_id, operation.attempt,
1222
1248
  operation.environment, operation.created_at
1223
1249
  ), resource AS (
@@ -1293,6 +1319,8 @@ export async function recordDaytonaSandboxCleanupIntent(
1293
1319
  input.providerOperationToken,
1294
1320
  input.sandboxId,
1295
1321
  input.routingDomain,
1322
+ runtimeSandboxOperationTargetState('record_daytona_cleanup_intent'),
1323
+ runtimeSandboxOperationFromStates('record_daytona_cleanup_intent'),
1296
1324
  ],
1297
1325
  );
1298
1326
  return recorded.rows.length === 1;
@@ -5,6 +5,10 @@ import type {
5
5
  import {
6
6
  RuntimeSandboxOperationState,
7
7
  RuntimeSandboxProvider,
8
+ runtimeSandboxOperationFromStates,
9
+ runtimeSandboxOperationFromStatesSqlCast,
10
+ runtimeSandboxOperationStateSqlType,
11
+ runtimeSandboxOperationTargetState,
8
12
  } from './runtime-sandbox-operation';
9
13
 
10
14
  const DAYTONA_PROVIDER = RuntimeSandboxProvider.Daytona;
@@ -473,6 +477,9 @@ export async function backfillRuntimeSandboxOperationsFromAttemptResources(
473
477
  throw new Error('Invalid runtime sandbox operation backfill cursor.');
474
478
  }
475
479
  const { attempts, runs, operations } = tables(options);
480
+ const operationState = runtimeSandboxOperationStateSqlType(
481
+ quoteIdent(options?.schema ?? 'runtime_scheduler'),
482
+ );
476
483
  const result = await client.query<{
477
484
  run_id: string;
478
485
  attempt: number;
@@ -532,7 +539,7 @@ export async function backfillRuntimeSandboxOperationsFromAttemptResources(
532
539
  environment,
533
540
  (resource::jsonb)->>'sandboxId',
534
541
  routing_domain,
535
- $6,
542
+ $6::${operationState},
536
543
  updated_at
537
544
  FROM ranked_resources
538
545
  WHERE conflict_rank = 1
@@ -563,7 +570,7 @@ export async function backfillRuntimeSandboxOperationsFromAttemptResources(
563
570
  cursor?.attempt ?? 0,
564
571
  cursor?.resourceOrdinal ?? 0,
565
572
  limit,
566
- RuntimeSandboxOperationState.Live,
573
+ runtimeSandboxOperationTargetState('backfill_live'),
567
574
  ],
568
575
  );
569
576
  const last = result.rows[0] ?? null;
@@ -673,9 +680,10 @@ export async function reconcileRuntimeDaytonaSandboxLedger(
673
680
  throw new Error('minimumAgeMs must be a non-negative safe integer.');
674
681
  }
675
682
  const { operations, cleanup, cleanupEvents, leases } = tables(options);
676
- const operationStateType = `${quoteIdent(
677
- options?.schema ?? 'runtime_scheduler',
678
- )}.${quoteIdent('runtime_sandbox_operation_state')}`;
683
+ const quotedSchema = quoteIdent(options?.schema ?? 'runtime_scheduler');
684
+ const operationStateType = runtimeSandboxOperationStateSqlType(quotedSchema);
685
+ const operationStates =
686
+ runtimeSandboxOperationFromStatesSqlCast(quotedSchema);
679
687
  const diagnostics: RuntimeSandboxLedgerReconciliationDiagnostic[] = [];
680
688
  async function measuredQuery<Row extends Record<string, unknown>>(
681
689
  phase: RuntimeSandboxLedgerReconciliationPhase,
@@ -801,7 +809,7 @@ export async function reconcileRuntimeDaytonaSandboxLedger(
801
809
  WHERE operation.provider_operation_token = inventory.operation_key
802
810
  AND operation.provider = $2
803
811
  AND operation.environment = 'production'
804
- AND operation.state = $4
812
+ AND operation.state = ANY($4::${operationStates})
805
813
  AND lease.lease_id = operation.operation_key
806
814
  AND lease.released_at IS NULL
807
815
  RETURNING operation.operation_key, operation.run_id, operation.attempt,
@@ -863,8 +871,8 @@ export async function reconcileRuntimeDaytonaSandboxLedger(
863
871
  [
864
872
  inventoryJson,
865
873
  DAYTONA_PROVIDER,
866
- RuntimeSandboxOperationState.Deleting,
867
- RuntimeSandboxOperationState.Reserved,
874
+ runtimeSandboxOperationTargetState('reconcile_recover_reserved'),
875
+ runtimeSandboxOperationFromStates('reconcile_recover_reserved'),
868
876
  ],
869
877
  );
870
878
  recoveredReservedOperations = recovered.rows.length;
@@ -1257,7 +1265,7 @@ export async function reconcileRuntimeDaytonaSandboxLedger(
1257
1265
  WHERE operation.provider = '${DAYTONA_PROVIDER}'
1258
1266
  AND operation.sandbox_id = changed.sandbox_id
1259
1267
  AND operation.routing_domain IS NOT DISTINCT FROM changed.routing_domain
1260
- AND operation.state = $3
1268
+ AND operation.state = ANY($3::${operationStates})
1261
1269
  RETURNING operation.operation_key
1262
1270
  ),
1263
1271
  released_leases AS (
@@ -1282,8 +1290,8 @@ export async function reconcileRuntimeDaytonaSandboxLedger(
1282
1290
  routing_domain: row.routingDomain,
1283
1291
  })),
1284
1292
  ),
1285
- RuntimeSandboxOperationState.Deleted,
1286
- RuntimeSandboxOperationState.Live,
1293
+ runtimeSandboxOperationTargetState('reconcile_confirmed_absent'),
1294
+ runtimeSandboxOperationFromStates('reconcile_confirmed_absent'),
1287
1295
  ],
1288
1296
  )
1289
1297
  : { rows: [] };
@@ -86,21 +86,174 @@ export type RuntimeSandboxOperation =
86
86
  | RuntimeSandboxOperationDeleted
87
87
  | RuntimeSandboxOperationAbandoned;
88
88
 
89
- /** Every legal transition is explicit and closed over the enum. */
89
+ export const RUNTIME_SANDBOX_OPERATION_STATES = [
90
+ RuntimeSandboxOperationState.Reserved,
91
+ RuntimeSandboxOperationState.Live,
92
+ RuntimeSandboxOperationState.Deleting,
93
+ RuntimeSandboxOperationState.Deleted,
94
+ RuntimeSandboxOperationState.Abandoned,
95
+ ] as const;
96
+
97
+ /** No writer moves an operation out of these; see the exits list below. */
98
+ export const RUNTIME_SANDBOX_OPERATION_TERMINAL_STATES = [
99
+ RuntimeSandboxOperationState.Deleted,
100
+ RuntimeSandboxOperationState.Abandoned,
101
+ ] as const;
102
+
103
+ /** The source of an INSERT: no operation row exists yet. */
104
+ export const RUNTIME_SANDBOX_OPERATION_ABSENT = 'absent';
105
+ export type RuntimeSandboxOperationTransitionSource =
106
+ | RuntimeSandboxOperationState
107
+ | typeof RUNTIME_SANDBOX_OPERATION_ABSENT;
108
+
109
+ export type RuntimeSandboxOperationTransitionSpec = {
110
+ /** States (or `absent`, for an INSERT) the write may start from. */
111
+ readonly from: readonly RuntimeSandboxOperationTransitionSource[];
112
+ readonly to: RuntimeSandboxOperationState;
113
+ };
114
+
115
+ const {
116
+ Reserved: RESERVED,
117
+ Live: LIVE,
118
+ Deleting: DELETING,
119
+ Deleted: DELETED,
120
+ Abandoned: ABANDONED,
121
+ } = RuntimeSandboxOperationState;
122
+ const ABSENT = RUNTIME_SANDBOX_OPERATION_ABSENT;
123
+
124
+ /**
125
+ * `runtime_sandbox_operations.state`, one entry per write. Every writer takes
126
+ * its allowed-from set from here (`state = ANY($n)`) and its target from `to`;
127
+ * the row fences (run, attempt, lease, sandbox identity, cleanup job) stay in
128
+ * each statement and only narrow the set. An INSERT starts from `absent` and
129
+ * never overwrites: its conflict clause is `DO NOTHING`.
130
+ */
131
+ export const RUNTIME_SANDBOX_OPERATION_STATE_TRANSITIONS = {
132
+ /** reserveRuntimeSandboxCapacityLease: the create intent precedes provider I/O. */
133
+ reserve: { from: [ABSENT], to: RESERVED },
134
+ /** backfillRuntimeSandboxOperationsFromAttemptResources: a pre-ledger attempt resource. */
135
+ backfill_live: { from: [ABSENT], to: LIVE },
136
+ /** recordPostgresSchedulerReconciledModalResourcesForCleanup: a late Modal sandbox with no reservation. */
137
+ record_reconciled_modal: { from: [ABSENT], to: DELETING },
138
+ /** recordPostgresSchedulerManagedSandboxOperationLive: the provider create returned. */
139
+ bind_live: { from: [RESERVED], to: LIVE },
140
+ /**
141
+ * recordPostgresSchedulerManagedSandboxOperationLive with `billingEndedAt`:
142
+ * Daytona retired the sandbox before retrying acquisition. A repeat on a
143
+ * deleting row only refreshes `updated_at`.
144
+ */
145
+ retire_live: { from: [LIVE, DELETING], to: DELETING },
146
+ /** recordPostgresSchedulerLateManagedSandboxOperationForCleanup: the create returned after the attempt lost its fence. */
147
+ record_late: { from: [RESERVED], to: DELETING },
148
+ /** recordPostgresSchedulerReconciledModalResourcesForCleanup: the reserved Modal create the provider later listed. */
149
+ promote_reconciled_modal: { from: [RESERVED], to: DELETING },
150
+ /** resolvePostgresSchedulerAmbiguousModalCreateIntentFromProvider: the provider confirmed no sandbox. */
151
+ abandon_unresolved_modal_intent: { from: [RESERVED], to: ABANDONED },
152
+ /** abandonUnstartedModalCancellationReservations: an old Modal reservation with no intent or resource. */
153
+ abandon_unstarted_modal: { from: [RESERVED], to: ABANDONED },
154
+ /** abandonRuntimeSandboxOperationReservation: a confirmed provider rejection. */
155
+ abandon_reservation: { from: [RESERVED], to: ABANDONED },
156
+ /** claimPostgresSchedulerSandboxCleanupJobs: the cleanup worker takes a live sandbox. */
157
+ cleanup_claim_live: { from: [LIVE], to: DELETING },
158
+ /** claimPostgresSchedulerSandboxCleanupJobs: a reservation whose lease is already bound to the sandbox. */
159
+ cleanup_claim_reserved: { from: [RESERVED], to: DELETING },
160
+ /** completePostgresSchedulerSandboxCleanupJob: the provider confirmed the delete. */
161
+ cleanup_complete: { from: [DELETING], to: DELETED },
162
+ /** recordRecoveredDaytonaCancellationSandbox: a cancelled run's create was found by its label. */
163
+ recover_daytona_cancellation: { from: [RESERVED], to: DELETING },
164
+ /** recordDaytonaSandboxCleanupIntent: queue cleanup for a labelled create before binding it. */
165
+ record_daytona_cleanup_intent: { from: [RESERVED], to: DELETING },
166
+ /** reconcileRuntimeDaytonaSandboxLedger: the inventory lists a reserved create by its label. */
167
+ reconcile_recover_reserved: { from: [RESERVED], to: DELETING },
168
+ /**
169
+ * reconcileRuntimeDaytonaSandboxLedger: an exact read confirmed a live
170
+ * sandbox is gone. It skips `deleting` because no cleanup worker holds it.
171
+ */
172
+ reconcile_confirmed_absent: { from: [LIVE], to: DELETED },
173
+ /** createPostgresDaytonaJanitorOwnershipPort.authorizeCleanup: the janitor queued cleanup for a live sandbox. */
174
+ janitor_authorize_cleanup: { from: [LIVE], to: DELETING },
175
+ } as const satisfies Record<string, RuntimeSandboxOperationTransitionSpec>;
176
+
177
+ export type RuntimeSandboxOperationTransition =
178
+ keyof typeof RUNTIME_SANDBOX_OPERATION_STATE_TRANSITIONS;
179
+
180
+ /**
181
+ * Every move out of a terminal state, by name. None exists today: a table
182
+ * entry that leaves `deleted` or `abandoned` fails the table test until it is
183
+ * listed here with its reason.
184
+ */
185
+ export const RUNTIME_SANDBOX_OPERATION_TERMINAL_EXITS: readonly {
186
+ transition: RuntimeSandboxOperationTransition;
187
+ from: RuntimeSandboxOperationState;
188
+ why: string;
189
+ }[] = [];
190
+
191
+ /** The allowed-from states of an UPDATE, for `state = ANY($n)`. */
192
+ export function runtimeSandboxOperationFromStates(
193
+ transition: RuntimeSandboxOperationTransition,
194
+ ): RuntimeSandboxOperationState[] {
195
+ const states = (
196
+ RUNTIME_SANDBOX_OPERATION_STATE_TRANSITIONS[transition]
197
+ .from as readonly RuntimeSandboxOperationTransitionSource[]
198
+ ).filter(
199
+ (state): state is RuntimeSandboxOperationState => state !== ABSENT,
200
+ );
201
+ if (states.length === 0) {
202
+ throw new Error(
203
+ `Sandbox operation transition ${transition} only inserts; it has no from-state.`,
204
+ );
205
+ }
206
+ return states;
207
+ }
208
+
209
+ /** The state an INSERT or UPDATE writes for a transition. */
210
+ export function runtimeSandboxOperationTargetState(
211
+ transition: RuntimeSandboxOperationTransition,
212
+ ): RuntimeSandboxOperationState {
213
+ return RUNTIME_SANDBOX_OPERATION_STATE_TRANSITIONS[transition].to;
214
+ }
215
+
216
+ /** The column's enum type, given the writer's quoted schema. */
217
+ export function runtimeSandboxOperationStateSqlType(
218
+ quotedSchema: string,
219
+ ): string {
220
+ return `${quotedSchema}."runtime_sandbox_operation_state"`;
221
+ }
222
+
223
+ /**
224
+ * The cast for an allowed-from parameter: `state = ANY($n::<this>)`. The
225
+ * parameter travels as `text[]` (drivers cannot encode a custom enum array)
226
+ * and becomes the enum array, so the comparison stays enum to enum.
227
+ */
228
+ export function runtimeSandboxOperationFromStatesSqlCast(
229
+ quotedSchema: string,
230
+ ): string {
231
+ return `text[]::${runtimeSandboxOperationStateSqlType(quotedSchema)}[]`;
232
+ }
233
+
234
+ /** The targets the writers' table reaches from one state. */
235
+ function runtimeSandboxOperationTargetsFrom(
236
+ state: RuntimeSandboxOperationState,
237
+ ): readonly RuntimeSandboxOperationState[] {
238
+ const specs: readonly RuntimeSandboxOperationTransitionSpec[] = Object.values(
239
+ RUNTIME_SANDBOX_OPERATION_STATE_TRANSITIONS,
240
+ );
241
+ return [
242
+ ...new Set(
243
+ specs.filter((spec) => spec.from.includes(state)).map((spec) => spec.to),
244
+ ),
245
+ ];
246
+ }
247
+
248
+ /** Every legal state change, derived from the writers' table above. */
90
249
  export const RUNTIME_SANDBOX_OPERATION_TRANSITIONS: Readonly<
91
250
  Record<RuntimeSandboxOperationState, readonly RuntimeSandboxOperationState[]>
92
251
  > = {
93
- [RuntimeSandboxOperationState.Reserved]: [
94
- RuntimeSandboxOperationState.Live,
95
- RuntimeSandboxOperationState.Deleting,
96
- RuntimeSandboxOperationState.Abandoned,
97
- ],
98
- [RuntimeSandboxOperationState.Live]: [RuntimeSandboxOperationState.Deleting],
99
- [RuntimeSandboxOperationState.Deleting]: [
100
- RuntimeSandboxOperationState.Deleted,
101
- ],
102
- [RuntimeSandboxOperationState.Deleted]: [],
103
- [RuntimeSandboxOperationState.Abandoned]: [],
252
+ [RESERVED]: runtimeSandboxOperationTargetsFrom(RESERVED),
253
+ [LIVE]: runtimeSandboxOperationTargetsFrom(LIVE),
254
+ [DELETING]: runtimeSandboxOperationTargetsFrom(DELETING),
255
+ [DELETED]: runtimeSandboxOperationTargetsFrom(DELETED),
256
+ [ABANDONED]: runtimeSandboxOperationTargetsFrom(ABANDONED),
104
257
  };
105
258
 
106
259
  export function canTransitionRuntimeSandboxOperation(