deepline 0.3.547 → 0.3.549

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 (166) hide show
  1. package/dist/bundling-sources/sdk/src/client-core.ts +93 -11
  2. package/dist/bundling-sources/sdk/src/index.ts +1 -0
  3. package/dist/bundling-sources/sdk/src/plays/check-local-play-graph.ts +3 -0
  4. package/dist/bundling-sources/sdk/src/release.ts +1 -1
  5. package/dist/bundling-sources/sdk/src/types.ts +11 -0
  6. package/dist/bundling-sources/shared_libs/monitors/action-actor.ts +203 -0
  7. package/dist/bundling-sources/shared_libs/monitors/fleets/limits.ts +20 -0
  8. package/dist/bundling-sources/shared_libs/monitors/package.json +2 -0
  9. package/dist/bundling-sources/shared_libs/monitors/provider-monitor-batch-operation.ts +33 -0
  10. package/dist/bundling-sources/shared_libs/monitors/signal-radars/inbound-dead-letter.ts +19 -3
  11. package/dist/bundling-sources/shared_libs/monitors/signal-radars/inbound-failure-codes.ts +21 -0
  12. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/absurd-runtime-worker.ts +83 -6
  13. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/postgres.ts +93 -0
  14. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/runtime-sandbox-capacity.ts +98 -1
  15. package/dist/bundling-sources/shared_libs/plays/play-check-compact.ts +636 -0
  16. package/dist/bundling-sources/shared_libs/security/email-blacklist.ts +3 -0
  17. package/dist/bundling-sources/shared_libs/security/email-domain.ts +24 -0
  18. package/dist/cli/auth-UVEZIP3H.js +43 -0
  19. package/dist/cli/{auth-WJEY4IDX.mjs → auth-YENF525S.mjs} +5 -5
  20. package/dist/cli/{billing-IJITDDRQ.js → billing-52GAHIDZ.js} +95 -95
  21. package/dist/cli/{billing-PHJUDYF3.mjs → billing-XPXB27G7.mjs} +9 -9
  22. package/dist/cli/{chunk-2HPTLBNB.js → chunk-3IUQNHIG.js} +3 -3
  23. package/dist/cli/{chunk-IKYN6PLO.mjs → chunk-4AUV5QJF.mjs} +5 -5
  24. package/dist/cli/{chunk-GZFZZQL4.js → chunk-4DJNXAPH.js} +75 -37
  25. package/dist/cli/{chunk-DZC7LMYM.js → chunk-4HSSPT73.js} +6 -6
  26. package/dist/cli/{chunk-TNJG4ZPN.mjs → chunk-4QUWXUUJ.mjs} +35 -6
  27. package/dist/cli/{chunk-SAJWYN4B.js → chunk-5622PYAP.js} +4 -4
  28. package/dist/cli/{chunk-PBMO36BP.mjs → chunk-5CXE22BI.mjs} +2 -2
  29. package/dist/cli/{chunk-ESCVHDNY.mjs → chunk-5T37KQNG.mjs} +5 -5
  30. package/dist/cli/{chunk-YURGAKI3.js → chunk-7FY7AHR6.js} +2 -2
  31. package/dist/cli/{chunk-PVQCX6KQ.js → chunk-ADWMIZCB.js} +67 -9
  32. package/dist/cli/{chunk-FMQXAOVE.mjs → chunk-ANOTB6V3.mjs} +4 -4
  33. package/dist/cli/{chunk-VPF54TXQ.mjs → chunk-AZSVDAXT.mjs} +5 -5
  34. package/dist/cli/{chunk-G6XTCG3J.mjs → chunk-BDOLPGHD.mjs} +2 -2
  35. package/dist/cli/{chunk-N63U5CRG.js → chunk-BDUZJX4V.js} +26 -26
  36. package/dist/cli/{chunk-HFIFKP3A.js → chunk-BPBPY42E.js} +5 -5
  37. package/dist/cli/{chunk-ECUD4QPD.mjs → chunk-CX4KGWCH.mjs} +3 -3
  38. package/dist/cli/{chunk-REAFIEV5.js → chunk-CXFOYRI4.js} +99 -99
  39. package/dist/cli/{chunk-RXRC2COC.mjs → chunk-DCTEH6N5.mjs} +2 -2
  40. package/dist/cli/{chunk-ABLTVG3J.js → chunk-DIRLEJJX.js} +41 -41
  41. package/dist/cli/{chunk-VNOQEDVC.js → chunk-ETLEPHLT.js} +7 -7
  42. package/dist/cli/{chunk-QU3K3IY6.js → chunk-HPSCRVTM.js} +12 -12
  43. package/dist/cli/{chunk-MBQELFL7.js → chunk-HWCIU3KY.js} +22 -22
  44. package/dist/cli/{chunk-XMBVHJPR.mjs → chunk-J6FINSAK.mjs} +49 -11
  45. package/dist/cli/{chunk-K3GMDSUX.js → chunk-KBMBNUUW.js} +5 -5
  46. package/dist/cli/{chunk-D6OIEH47.js → chunk-KTZ3VU6S.js} +5 -5
  47. package/dist/cli/{chunk-XNTDTAKC.mjs → chunk-L6UMWAAY.mjs} +1 -1
  48. package/dist/cli/{chunk-ITLZBDSR.mjs → chunk-LMFTDDAM.mjs} +2 -2
  49. package/dist/cli/{chunk-56MEAGWA.mjs → chunk-LP2VNGG3.mjs} +2 -2
  50. package/dist/cli/{chunk-RMCCB3XY.mjs → chunk-MAE3AKDR.mjs} +67 -9
  51. package/dist/cli/{chunk-L5J6UJ2X.js → chunk-NVALIJQS.js} +15 -15
  52. package/dist/cli/{chunk-MO7TGNXB.js → chunk-OWX4AAJ4.js} +13 -13
  53. package/dist/cli/{chunk-DIUNDLSX.mjs → chunk-PLAJ6HPD.mjs} +2 -2
  54. package/dist/cli/{chunk-G6KW764N.mjs → chunk-PLJCY35C.mjs} +1 -1
  55. package/dist/cli/{chunk-L6QE2XVM.mjs → chunk-PQX4WTBY.mjs} +1 -1
  56. package/dist/cli/{chunk-GJOGFWX3.js → chunk-QGKX2WBZ.js} +52 -23
  57. package/dist/cli/{chunk-AEQ5VWZ5.mjs → chunk-QIZBUY2N.mjs} +4 -4
  58. package/dist/cli/{chunk-E2OIYPB4.js → chunk-QUELC3B2.js} +10 -10
  59. package/dist/cli/{chunk-OVYXYTEP.js → chunk-RMKRZJOS.js} +16 -16
  60. package/dist/cli/{chunk-GIGAYLY6.js → chunk-SVNAP2QI.js} +9 -9
  61. package/dist/cli/{chunk-2AHY47SI.js → chunk-TYOAOSDL.js} +3 -3
  62. package/dist/cli/{chunk-GPAWLXW3.mjs → chunk-U6KHWIAO.mjs} +1 -1
  63. package/dist/cli/{chunk-MOZEAGSR.mjs → chunk-UGVXHT62.mjs} +1 -1
  64. package/dist/cli/{chunk-QLE3QKJ2.mjs → chunk-V5WPYOAE.mjs} +5 -5
  65. package/dist/cli/{chunk-FFBXLFKV.js → chunk-V7WMNX4R.js} +1 -1
  66. package/dist/cli/{chunk-NBYFQZ6O.js → chunk-VLAJGF27.js} +2 -2
  67. package/dist/cli/{chunk-5SFHPMBY.mjs → chunk-W77LBYF3.mjs} +11 -11
  68. package/dist/cli/{chunk-DJI5CLLW.mjs → chunk-X4RGDE4J.mjs} +1 -1
  69. package/dist/cli/{chunk-IVFFCAV2.js → chunk-XPYCNA43.js} +44 -44
  70. package/dist/cli/{chunk-HARZ4AAX.mjs → chunk-XS72HELO.mjs} +1 -1
  71. package/dist/cli/{chunk-LMLSOHTU.js → chunk-Y5MDCPWA.js} +53 -53
  72. package/dist/cli/{chunk-CKIRKYW7.mjs → chunk-Y5SKYU5X.mjs} +3 -3
  73. package/dist/cli/{chunk-Z2YGQZWV.mjs → chunk-YX7BX2FA.mjs} +2 -2
  74. package/dist/cli/{chunk-YVKLF7RW.js → chunk-YZTS3XA7.js} +2 -2
  75. package/dist/cli/{chunk-IGAK22XO.mjs → chunk-Z3DKJOUL.mjs} +1 -1
  76. package/dist/cli/command-docs.js +2 -2
  77. package/dist/cli/command-docs.mjs +1 -1
  78. package/dist/cli/concurrent-runs-help-74AFW3BD.js +21 -0
  79. package/dist/cli/{concurrent-runs-help-ZNGP563R.mjs → concurrent-runs-help-OYSLW55L.mjs} +6 -6
  80. package/dist/cli/{csv-X6D4ZELE.js → csv-FRO3UFMQ.js} +13 -13
  81. package/dist/cli/{csv-VAHQHYYV.mjs → csv-TFCTUYF2.mjs} +4 -4
  82. package/dist/cli/{db-4G4S2VOV.mjs → db-NHVZBT2H.mjs} +6 -6
  83. package/dist/cli/{db-YA5ZGVEX.js → db-SNFNRVZJ.js} +27 -27
  84. package/dist/cli/describe-2JCOC4AK.js +50 -0
  85. package/dist/cli/{describe-EVXEM2S6.mjs → describe-OVFPRJJL.mjs} +18 -18
  86. package/dist/cli/describe-core.d.mts +2 -2
  87. package/dist/cli/describe-core.d.ts +2 -2
  88. package/dist/cli/{executions-HR6CTN5M.mjs → executions-HWMH2LTC.mjs} +6 -6
  89. package/dist/cli/{executions-5YC7ABF6.js → executions-RZLKKPZV.js} +8 -8
  90. package/dist/cli/{feedback-MJHSYLFF.js → feedback-3D5OROCI.js} +14 -14
  91. package/dist/cli/{feedback-GEU5SPLR.mjs → feedback-WD4JVF77.mjs} +6 -6
  92. package/dist/cli/git-GCCPCUWJ.js +25 -0
  93. package/dist/cli/{git-NPFF6QUS.mjs → git-RYUKBCUZ.mjs} +5 -5
  94. package/dist/cli/index.js +146 -146
  95. package/dist/cli/index.mjs +45 -45
  96. package/dist/cli/{integrations-Z7EGVBHF.mjs → integrations-KJPFCEXL.mjs} +7 -7
  97. package/dist/cli/{integrations-YPHFI77G.js → integrations-Y7OK23DW.js} +15 -15
  98. package/dist/cli/{legacy-noops-5IYCFWWX.mjs → legacy-noops-2PU2ZVEP.mjs} +6 -6
  99. package/dist/cli/{legacy-noops-246E3KKG.js → legacy-noops-G7ZY7K3K.js} +9 -9
  100. package/dist/cli/{mcp-WUYGZXPI.mjs → mcp-D7F2RSOI.mjs} +4 -4
  101. package/dist/cli/{mcp-YTYTW67G.js → mcp-GILBYX72.js} +24 -24
  102. package/dist/cli/{monitors-73DQKCU7.mjs → monitors-IK5KXY2H.mjs} +32 -8
  103. package/dist/cli/{monitors-SA6ILD6Z.js → monitors-OXNHOT3C.js} +125 -101
  104. package/dist/cli/{org-WVQRB2KX.mjs → org-L5D7EISU.mjs} +6 -6
  105. package/dist/cli/{org-X7ELDV7B.js → org-OYM3UNTP.js} +88 -88
  106. package/dist/cli/{play-QI6V2EUQ.mjs → play-PK6QO3WX.mjs} +837 -290
  107. package/dist/cli/{play-QTYOAJPE.js → play-YK22QPYO.js} +1423 -876
  108. package/dist/cli/{providers-XXFLVREV.js → providers-5CYP7P4N.js} +9 -9
  109. package/dist/cli/{providers-DPVMVDS4.mjs → providers-Z6MNJDCG.mjs} +6 -6
  110. package/dist/cli/{quickstart-YWGNHULM.mjs → quickstart-2I73XQXB.mjs} +4 -4
  111. package/dist/cli/{quickstart-33ISXQJU.js → quickstart-2YKETYJY.js} +6 -6
  112. package/dist/cli/{run-J6FCAKQC.mjs → run-KGQ6TUBB.mjs} +23 -23
  113. package/dist/cli/{run-BWHZXLTD.js → run-YVSEAVE5.js} +43 -43
  114. package/dist/cli/{search-Z3UU3WBB.mjs → search-V2HPN6FU.mjs} +7 -7
  115. package/dist/cli/search-YG3UIZ6A.js +32 -0
  116. package/dist/cli/{search-output-YIPV74ME.js → search-output-BUSANFRN.js} +6 -6
  117. package/dist/cli/{search-output-OJYY4J7E.mjs → search-output-OCTOSDEO.mjs} +4 -4
  118. package/dist/cli/{secrets-UXRJCEN4.js → secrets-VDKTHCVB.js} +15 -15
  119. package/dist/cli/{secrets-YZZN2WST.mjs → secrets-WZGRBWI5.mjs} +6 -6
  120. package/dist/cli/{sessions-ITCGPD7Z.mjs → sessions-5DTN55DZ.mjs} +6 -6
  121. package/dist/cli/sessions-NSPT6LHT.js +22 -0
  122. package/dist/cli/{settings-LAFYMILP.js → settings-A7VSNOLI.js} +85 -85
  123. package/dist/cli/{settings-ZP2LSVNM.mjs → settings-BK5FPQ3X.mjs} +12 -12
  124. package/dist/cli/{setup-WEQAQSYC.mjs → setup-LLQVPZTP.mjs} +9 -9
  125. package/dist/cli/{setup-4AL3XYM5.js → setup-NV27CYQ5.js} +49 -49
  126. package/dist/cli/{skills-NQH4HFW2.mjs → skills-IV6HK4XA.mjs} +7 -7
  127. package/dist/cli/skills-UA5E4DA3.js +36 -0
  128. package/dist/cli/{tools-CXK2GSI4.mjs → tools-4FKPCRD7.mjs} +17 -17
  129. package/dist/cli/tools-NJVPAG37.js +57 -0
  130. package/dist/cli/update-DKX27XZB.js +34 -0
  131. package/dist/cli/{update-3ROF3Z75.mjs → update-MH5M746E.mjs} +9 -9
  132. package/dist/cli/{views-XJFO5CEH.js → views-JW44TCGL.js} +21 -21
  133. package/dist/cli/{views-AOUHVGV2.mjs → views-KXC4JDT7.mjs} +6 -6
  134. package/dist/cli/{workflow-GY3DBHXX.js → workflow-F5AHL2ET.js} +17 -17
  135. package/dist/cli/{workflow-KBS2QJK6.mjs → workflow-FI4DL7ZI.mjs} +7 -7
  136. package/dist/{client-D1uWA2Vc.d.mts → client-BEJqhQAj.d.mts} +1 -1
  137. package/dist/{client-Hz7SsBXs.d.ts → client-Ceo295L2.d.ts} +1 -1
  138. package/dist/{client-core-D3Ad7Flt.d.ts → client-core-BPYuWfBC.d.ts} +49 -1
  139. package/dist/{client-core-Db_e63Hb.d.mts → client-core-BqetRxx-.d.mts} +49 -1
  140. package/dist/index.d.mts +3 -3
  141. package/dist/index.d.ts +3 -3
  142. package/dist/index.js +9 -9
  143. package/dist/index.mjs +6 -6
  144. package/dist/install-integrity.json +118 -116
  145. package/dist/plays/play-description.d.mts +1 -1
  146. package/dist/plays/play-description.d.ts +1 -1
  147. package/dist/plays/run-request.d.mts +1 -1
  148. package/dist/plays/run-request.d.ts +1 -1
  149. package/dist/release.d.mts +1 -1
  150. package/dist/release.d.ts +1 -1
  151. package/dist/release.js +2 -2
  152. package/dist/release.mjs +1 -1
  153. package/dist/server.d.mts +2 -2
  154. package/dist/server.d.ts +2 -2
  155. package/dist/server.js +5 -5
  156. package/dist/server.mjs +3 -3
  157. package/package.json +1 -1
  158. package/dist/cli/auth-AC5277PI.js +0 -43
  159. package/dist/cli/concurrent-runs-help-ZYISYOQA.js +0 -21
  160. package/dist/cli/describe-GYYSLZLV.js +0 -50
  161. package/dist/cli/git-FZIQZNI4.js +0 -25
  162. package/dist/cli/search-JCWJX2XD.js +0 -32
  163. package/dist/cli/sessions-NA4SJH6Q.js +0 -22
  164. package/dist/cli/skills-XZ4Y6SZ7.js +0 -36
  165. package/dist/cli/tools-WJQ6Y7E4.js +0 -57
  166. package/dist/cli/update-ZLRLTBRD.js +0 -34
@@ -13846,6 +13846,99 @@ export type PostgresSchedulerComputeBillingJob = {
13846
13846
  bootstrapSessionPending: boolean;
13847
13847
  };
13848
13848
 
13849
+ /**
13850
+ * Whether the organization has a compute-billing obligation the scheduler has
13851
+ * not settled, quarantined ones included. This is the obligation's own truth:
13852
+ * its Convex session can already be finalized while a retry still owes a
13853
+ * late-resource adjustment, and every retry records into that session again.
13854
+ * `schema_absent` means the schema has no billing tables to read (dropped or
13855
+ * half rebuilt), which is not proof that nothing is owed. An unsettled answer
13856
+ * carries fixed counts so a caller can tell a quarantined or long-stuck
13857
+ * obligation, which only an operator recovers, from one still settling.
13858
+ * Read-only; uses the unsettled partial index and the runs primary key.
13859
+ */
13860
+ export type PostgresSchedulerComputeBillingObligation =
13861
+ | { state: 'settled' }
13862
+ | { state: 'schema_absent' }
13863
+ | {
13864
+ state: 'unsettled';
13865
+ unsettledCount: number;
13866
+ blockedCount: number;
13867
+ oldestUnsettledCreatedAtMs: number;
13868
+ };
13869
+
13870
+ export async function readPostgresSchedulerComputeBillingObligationForOrg(
13871
+ client: PostgresSchedulerQueryClient,
13872
+ input: { orgId: string },
13873
+ options?: Pick<PostgresSchedulerOptions, 'schema'>,
13874
+ ): Promise<PostgresSchedulerComputeBillingObligation> {
13875
+ const presence = await client.query<{ present: boolean }>(
13876
+ `
13877
+ SELECT (
13878
+ to_regclass($1) IS NOT NULL AND to_regclass($2) IS NOT NULL
13879
+ ) AS present
13880
+ `,
13881
+ [tableName('compute_billing_jobs_v2', options), tableName('runs', options)],
13882
+ );
13883
+ const present = presence.rows[0]?.present;
13884
+ if (typeof present !== 'boolean') {
13885
+ throw new Error('Compute billing schema presence read returned no answer.');
13886
+ }
13887
+ if (!present) return { state: 'schema_absent' };
13888
+ const result = await client.query<{
13889
+ unsettled_count: number | string;
13890
+ blocked_count: number | string;
13891
+ oldest_created_at_ms: number | string | null;
13892
+ }>(
13893
+ `
13894
+ SELECT
13895
+ count(*) AS unsettled_count,
13896
+ count(*) FILTER (WHERE job.blocked_at IS NOT NULL) AS blocked_count,
13897
+ floor(extract(epoch FROM min(job.created_at)) * 1000)::bigint
13898
+ AS oldest_created_at_ms
13899
+ FROM ${tableName('compute_billing_jobs_v2', options)} AS job
13900
+ JOIN ${tableName('runs', options)} AS run ON run.run_id = job.run_id
13901
+ WHERE job.status <> 'settled'
13902
+ AND run.org_id = $1
13903
+ `,
13904
+ [input.orgId],
13905
+ );
13906
+ const row = result.rows[0];
13907
+ if (!row) {
13908
+ throw new Error('Compute billing obligation read returned no answer.');
13909
+ }
13910
+ const unsettledCount = Number(row.unsettled_count);
13911
+ const blockedCount = Number(row.blocked_count);
13912
+ if (!Number.isSafeInteger(unsettledCount) || !Number.isSafeInteger(blockedCount)) {
13913
+ throw new Error('Compute billing obligation read returned invalid counts.');
13914
+ }
13915
+ if (unsettledCount === 0) return { state: 'settled' };
13916
+ const oldestUnsettledCreatedAtMs = Number(row.oldest_created_at_ms);
13917
+ if (!Number.isSafeInteger(oldestUnsettledCreatedAtMs)) {
13918
+ throw new Error('Compute billing obligation read returned no oldest job.');
13919
+ }
13920
+ return {
13921
+ state: 'unsettled',
13922
+ unsettledCount,
13923
+ blockedCount,
13924
+ oldestUnsettledCreatedAtMs,
13925
+ };
13926
+ }
13927
+
13928
+ export async function readComputeBillingObligationForOrgViaRuntimeScheduler(input: {
13929
+ poolOptions: RuntimeSchedulerPoolOptions;
13930
+ orgId: string;
13931
+ schedulerOptions: Pick<PostgresSchedulerOptions, 'schema'>;
13932
+ }): Promise<PostgresSchedulerComputeBillingObligation> {
13933
+ return await withRuntimeSchedulerClient(input.poolOptions, (client) =>
13934
+ readPostgresSchedulerComputeBillingObligationForOrg(
13935
+ client,
13936
+ { orgId: input.orgId },
13937
+ input.schedulerOptions,
13938
+ ),
13939
+ );
13940
+ }
13941
+
13849
13942
  /**
13850
13943
  * Rolling-deploy bridge for terminal rows written by a retained v1 runtime.
13851
13944
  * Current terminal writers create this obligation atomically; an observing
@@ -125,8 +125,15 @@ export class RuntimeSandboxCapacityLimitError extends RuntimeSchedulerCapacityEr
125
125
  readonly orgId: string;
126
126
  readonly limit: number;
127
127
  readonly activeLeases: number;
128
+ /** The attempt generation refused, so any catcher can back off by age. */
129
+ readonly attempt: number | null;
128
130
 
129
- constructor(input: { orgId: string; limit: number; activeLeases: number }) {
131
+ constructor(input: {
132
+ orgId: string;
133
+ limit: number;
134
+ activeLeases: number;
135
+ attempt?: number;
136
+ }) {
130
137
  super(
131
138
  `Runtime scheduler sandbox capacity is full for organization ${input.orgId} (${input.activeLeases}/${input.limit} active leases).`,
132
139
  15_000,
@@ -135,6 +142,7 @@ export class RuntimeSandboxCapacityLimitError extends RuntimeSchedulerCapacityEr
135
142
  this.orgId = input.orgId;
136
143
  this.limit = input.limit;
137
144
  this.activeLeases = input.activeLeases;
145
+ this.attempt = input.attempt ?? null;
138
146
  }
139
147
  }
140
148
 
@@ -286,6 +294,95 @@ type RuntimeSandboxReservationProof = {
286
294
  sandbox_id: string | null;
287
295
  };
288
296
 
297
+ /**
298
+ * Advisory, lock-free read of an organization's sandbox capacity, used before
299
+ * an attempt does any launch work (executor-token mint, the fleet-wide sandbox
300
+ * start limiter, provider resolution). It mirrors the counting in
301
+ * `runtime_sandbox_capacity_observation` without taking its advisory lock, so
302
+ * it never contends with real reservations. `reserveRuntimeSandboxCapacityLease`
303
+ * stays the only authority: this read can only defer an attempt that the
304
+ * reservation would also refuse, and a run that already holds an unreleased
305
+ * lease (a resume or a pending provider operation) is never reported full.
306
+ *
307
+ * On 2026-11-18 one capacity-capped organization's blocked cron runs retried
308
+ * every ~21 s through the whole launch path before the reservation refused
309
+ * them: about 27,000 attempts an hour, nearly all of the fleet's sandbox-start
310
+ * limiter traffic.
311
+ */
312
+ export async function precheckRuntimeSandboxCapacity(
313
+ client: PostgresSchedulerQueryClient,
314
+ input: {
315
+ orgId: string;
316
+ runId: string;
317
+ mode?: RuntimeSandboxCapacityAdmissionMode;
318
+ entitlementLimit?: number | null;
319
+ },
320
+ options?: Pick<PostgresSchedulerOptions, 'schema'>,
321
+ ): Promise<{ full: boolean; limit: number; activeLeases: number }> {
322
+ const orgId = assertId(input.orgId, 'orgId');
323
+ const runId = assertId(input.runId, 'runId');
324
+ const { cache, leases } = schemaTables(options);
325
+ const result = await client.query<{
326
+ limit: number;
327
+ active_leases: number;
328
+ run_holds_lease: boolean;
329
+ }>(
330
+ `
331
+ SELECT
332
+ -- Same order as the reservation: an unexpired time-limited override,
333
+ -- then the launch-carried entitlement, then the observation's
334
+ -- configured limit (an expired mirror falls back to the default).
335
+ coalesce(
336
+ (
337
+ SELECT override."limit" FROM ${cache} AS override
338
+ WHERE override.org_id = $1
339
+ AND override.expires_at IS NOT NULL
340
+ AND override.expires_at > clock_timestamp()
341
+ ),
342
+ $3::integer,
343
+ (
344
+ SELECT CASE
345
+ WHEN expires_at IS NOT NULL AND expires_at <= clock_timestamp()
346
+ THEN $2::integer
347
+ ELSE "limit"
348
+ END
349
+ FROM ${cache}
350
+ WHERE org_id = $1
351
+ ),
352
+ $2::integer
353
+ ) AS "limit",
354
+ (
355
+ SELECT count(*)::integer
356
+ FROM ${leases}
357
+ WHERE org_id = $1
358
+ AND released_at IS NULL
359
+ AND (sandbox_id IS NOT NULL OR expires_at > clock_timestamp())
360
+ ) AS active_leases,
361
+ EXISTS (
362
+ SELECT 1
363
+ FROM ${leases}
364
+ WHERE org_id = $1
365
+ AND run_id = $4
366
+ AND released_at IS NULL
367
+ ) AS run_holds_lease
368
+ `,
369
+ [
370
+ orgId,
371
+ DEFAULT_PLAY_SANDBOX_CONCURRENCY,
372
+ entitlementLimit(input.entitlementLimit),
373
+ runId,
374
+ ],
375
+ );
376
+ const row = result.rows[0];
377
+ const limit = Number(row?.limit ?? DEFAULT_PLAY_SANDBOX_CONCURRENCY);
378
+ const activeLeases = Number(row?.active_leases ?? 0);
379
+ const full =
380
+ (input.mode ?? 'enforce') === 'enforce' &&
381
+ row?.run_holds_lease !== true &&
382
+ activeLeases >= limit;
383
+ return { full, limit, activeLeases };
384
+ }
385
+
289
386
  /** A launch-carried limit inside the runtime's bounds, or null to ignore it. */
290
387
  function entitlementLimit(value: number | null | undefined): number | null {
291
388
  return typeof value === 'number' &&