deepline 0.3.499 → 0.3.501

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 (178) hide show
  1. package/dist/bundling-sources/sdk/src/plays/client-core.ts +45 -7
  2. package/dist/bundling-sources/sdk/src/release.ts +1 -1
  3. package/dist/bundling-sources/shared_libs/observability/scheduled-jobs.json +36 -0
  4. package/dist/bundling-sources/shared_libs/play-data-plane/r2.ts +155 -0
  5. package/dist/bundling-sources/shared_libs/play-runtime/app-runtime-api.ts +7 -1
  6. package/dist/bundling-sources/shared_libs/play-runtime/context.ts +2 -1
  7. package/dist/bundling-sources/shared_libs/play-runtime/durable-invocation-fence.ts +3 -1
  8. package/dist/bundling-sources/shared_libs/play-runtime/internal-bootstrap-token.ts +54 -1
  9. package/dist/bundling-sources/shared_libs/play-runtime/launch-identity.ts +12 -0
  10. package/dist/bundling-sources/shared_libs/play-runtime/protocol.ts +13 -0
  11. package/dist/bundling-sources/shared_libs/play-runtime/run-identity.ts +56 -0
  12. package/dist/bundling-sources/shared_libs/play-runtime/runner-app/index.ts +21 -10
  13. package/dist/bundling-sources/shared_libs/play-runtime/runner-app/runtime-heartbeat.ts +1 -1
  14. package/dist/bundling-sources/shared_libs/play-runtime/runner-app/runtime-progress-push.ts +1 -2
  15. package/dist/bundling-sources/shared_libs/play-runtime/runner-app/runtime-terminal-push.ts +1 -1
  16. package/dist/bundling-sources/shared_libs/play-runtime/runner-backends/backends/daytona-launch-transport.ts +9 -2
  17. package/dist/bundling-sources/shared_libs/play-runtime/runner-backends/backends/daytona-lifecycle.ts +31 -1
  18. package/dist/bundling-sources/shared_libs/play-runtime/runner-backends/backends/daytona-payload-transport.ts +74 -3
  19. package/dist/bundling-sources/shared_libs/play-runtime/runner-backends/backends/daytona-runtime-watchdog.ts +226 -2
  20. package/dist/bundling-sources/shared_libs/play-runtime/runtime-api-paths.ts +41 -0
  21. package/dist/bundling-sources/shared_libs/play-runtime/runtime-api.ts +5 -1
  22. package/dist/bundling-sources/shared_libs/play-runtime/runtime-app-transport.ts +374 -0
  23. package/dist/bundling-sources/shared_libs/play-runtime/runtime-authority.ts +5 -0
  24. package/dist/bundling-sources/shared_libs/play-runtime/runtime-environment.ts +49 -9
  25. package/dist/bundling-sources/shared_libs/play-runtime/runtime-gateway-oidc.ts +25 -3
  26. package/dist/bundling-sources/shared_libs/play-runtime/runtime-gateway-profile.ts +12 -1
  27. package/dist/bundling-sources/shared_libs/play-runtime/runtime-gateway-sandbox-credential.ts +338 -0
  28. package/dist/bundling-sources/shared_libs/play-runtime/runtime-gateway-staging.ts +417 -0
  29. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backend.ts +15 -0
  30. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/absurd-runtime-worker.ts +184 -11
  31. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/absurd-shared.ts +17 -3
  32. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/gateway-progress-registry.ts +9 -0
  33. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/postgres-progress.ts +18 -2
  34. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/postgres.ts +176 -16
  35. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/runtime-gateway-attempt.ts +81 -0
  36. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/runtime-gateway-staging-journal.ts +708 -0
  37. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/runtime-gateway-staging-teardown.ts +160 -0
  38. package/dist/bundling-sources/shared_libs/play-runtime/workflow-executor-auth.ts +4 -0
  39. package/dist/cli/{auth-TECJ27G3.mjs → auth-6ECHXOI4.mjs} +5 -5
  40. package/dist/cli/auth-VP7PMXKQ.js +43 -0
  41. package/dist/cli/{billing-PMKO73VG.mjs → billing-75NZYRXA.mjs} +8 -8
  42. package/dist/cli/{billing-ULZGAED6.js → billing-QRR7O6BD.js} +81 -81
  43. package/dist/cli/{chunk-K54BUQSK.js → chunk-463ZOO2L.js} +7 -7
  44. package/dist/cli/{chunk-L6C7C7IJ.js → chunk-4OXHLTCP.js} +1 -1
  45. package/dist/cli/{chunk-3GP52OHV.mjs → chunk-5S7MLAI7.mjs} +11 -11
  46. package/dist/cli/{chunk-7OQZZHZ4.js → chunk-5X4AUFYW.js} +7 -7
  47. package/dist/cli/{chunk-56JE475N.mjs → chunk-66XDYH5T.mjs} +2 -2
  48. package/dist/cli/{chunk-A2VCFBZN.mjs → chunk-7L6QNHUL.mjs} +1 -1
  49. package/dist/cli/{chunk-UOQUCFAK.js → chunk-APAHMCGR.js} +53 -53
  50. package/dist/cli/{chunk-UBKA33KY.mjs → chunk-BMCRTI6Z.mjs} +22 -1
  51. package/dist/cli/{chunk-WSOM3TY6.js → chunk-BOFCF4L7.js} +3 -3
  52. package/dist/cli/{chunk-SLWCXGKP.js → chunk-CJJ7JBM2.js} +16 -16
  53. package/dist/cli/{chunk-JM52ATUS.mjs → chunk-EHVFBD36.mjs} +1 -1
  54. package/dist/cli/{chunk-7WUMIRBV.js → chunk-EKF4DKUA.js} +5 -5
  55. package/dist/cli/{chunk-AGZRRHSB.js → chunk-EKZQ5LQO.js} +3 -3
  56. package/dist/cli/{chunk-5IDORX4E.mjs → chunk-EOO7OSS4.mjs} +4 -4
  57. package/dist/cli/{chunk-2PKZ3RUU.js → chunk-FS7XYDOF.js} +16 -16
  58. package/dist/cli/{chunk-Z2MO7FZG.js → chunk-HDKXSHGA.js} +53 -53
  59. package/dist/cli/{chunk-YRQWGWRJ.js → chunk-J6SVNKRP.js} +2 -2
  60. package/dist/cli/{chunk-QXI2W7YY.mjs → chunk-J74N375S.mjs} +4 -4
  61. package/dist/cli/{chunk-KLQ5CR77.js → chunk-KTOAJP4N.js} +6 -6
  62. package/dist/cli/{chunk-ZVAD65CF.mjs → chunk-MADTHS56.mjs} +1 -1
  63. package/dist/cli/{chunk-UMPCQ4EE.mjs → chunk-MSLXAVVK.mjs} +2 -2
  64. package/dist/cli/{chunk-JIZHGOZ5.js → chunk-N2TJJNCW.js} +96 -96
  65. package/dist/cli/{chunk-CS2AEAD6.mjs → chunk-N4TR6QRI.mjs} +1 -1
  66. package/dist/cli/{chunk-43ZERPGR.mjs → chunk-NA3J3F2Z.mjs} +1 -1
  67. package/dist/cli/{chunk-JQHJKARU.mjs → chunk-O3DDVP7Z.mjs} +1 -1
  68. package/dist/cli/{chunk-JUQESEJX.mjs → chunk-OKOXB3LY.mjs} +2 -2
  69. package/dist/cli/{chunk-FG5OZ5FB.js → chunk-PNF2ZU4R.js} +8 -8
  70. package/dist/cli/{chunk-KZJKRU24.mjs → chunk-PXIF74UJ.mjs} +4 -4
  71. package/dist/cli/{chunk-TUDYBF4Y.js → chunk-QQ3JJYE2.js} +14 -14
  72. package/dist/cli/{chunk-IGCSKXGK.mjs → chunk-RFV7PPLS.mjs} +3 -3
  73. package/dist/cli/{chunk-EQR5O2D2.js → chunk-RNSHCJME.js} +3 -3
  74. package/dist/cli/{chunk-PE3UHMMZ.js → chunk-RU5ROUFO.js} +5 -5
  75. package/dist/cli/{chunk-2RZHMCDL.js → chunk-SV2TEZ2M.js} +4 -4
  76. package/dist/cli/{chunk-P3MGNLZF.mjs → chunk-T4U245E4.mjs} +2 -2
  77. package/dist/cli/{chunk-7UC2UZYW.mjs → chunk-T7E3XEPG.mjs} +6 -6
  78. package/dist/cli/{chunk-LUNXREUI.js → chunk-TOE7LHEA.js} +6 -6
  79. package/dist/cli/{chunk-OWHQ4OG5.mjs → chunk-UJRN6YP6.mjs} +3 -3
  80. package/dist/cli/{chunk-XSUQAUG4.js → chunk-UR7JOSLT.js} +360 -300
  81. package/dist/cli/{chunk-KOD45UP5.mjs → chunk-V5NHEYU7.mjs} +5 -5
  82. package/dist/cli/{chunk-PSYGSXDS.mjs → chunk-VD2374K7.mjs} +1 -1
  83. package/dist/cli/{chunk-57LW4TGY.js → chunk-VL72UKNW.js} +26 -26
  84. package/dist/cli/{chunk-YAW7ET4P.mjs → chunk-VTDAJZIJ.mjs} +90 -30
  85. package/dist/cli/{chunk-AKLI4XSG.js → chunk-VVEMPC2I.js} +2 -2
  86. package/dist/cli/{chunk-L7O7AFSF.js → chunk-W6JTHVPF.js} +21 -21
  87. package/dist/cli/{chunk-MQ5YPD7K.mjs → chunk-WJUDAHIQ.mjs} +1 -1
  88. package/dist/cli/{chunk-XDVT3GOM.js → chunk-WSGXBROE.js} +22 -1
  89. package/dist/cli/{chunk-G7BB2VOC.js → chunk-XEBFJ2PH.js} +41 -41
  90. package/dist/cli/{chunk-WEVIUQGZ.mjs → chunk-Z6AUOV7D.mjs} +2 -2
  91. package/dist/cli/{chunk-BBFWIYAF.mjs → chunk-ZUOKIBLJ.mjs} +2 -2
  92. package/dist/cli/{chunk-C2V5U6HO.mjs → chunk-ZUQHQGC5.mjs} +5 -5
  93. package/dist/cli/{csv-VGSQ3JYE.js → csv-JXX2LXK5.js} +13 -13
  94. package/dist/cli/{csv-ACO5YC46.mjs → csv-RZJ4SOIL.mjs} +4 -4
  95. package/dist/cli/{db-CXNZOXGM.mjs → db-MUTT2NWI.mjs} +8 -8
  96. package/dist/cli/{db-QOUPHFFN.js → db-T2KJNONM.js} +29 -29
  97. package/dist/cli/{describe-J4O5GCJS.mjs → describe-K3UMAAVO.mjs} +19 -19
  98. package/dist/cli/describe-SEXUJLE4.js +50 -0
  99. package/dist/cli/describe-core.d.mts +2 -2
  100. package/dist/cli/describe-core.d.ts +2 -2
  101. package/dist/cli/{executions-YIECH2CL.js → executions-7HSLDRHR.js} +10 -10
  102. package/dist/cli/{executions-SJETRLXW.mjs → executions-PHS4R2OX.mjs} +8 -8
  103. package/dist/cli/{feedback-5BRCNERV.js → feedback-BJZRM637.js} +8 -8
  104. package/dist/cli/{feedback-GSAFFMF6.mjs → feedback-RWMDCIDF.mjs} +4 -4
  105. package/dist/cli/git-VNS7UQWN.js +25 -0
  106. package/dist/cli/{git-SF5XIGLU.mjs → git-YW6VHYZG.mjs} +5 -5
  107. package/dist/cli/index.js +137 -137
  108. package/dist/cli/index.mjs +40 -40
  109. package/dist/cli/{legacy-noops-5TBT4XC6.js → legacy-noops-2WUV2XD7.js} +8 -8
  110. package/dist/cli/{legacy-noops-TZA3CALE.mjs → legacy-noops-5UBSUPGG.mjs} +5 -5
  111. package/dist/cli/{monitors-XAYWJNSV.mjs → monitors-HWOQO3OK.mjs} +9 -9
  112. package/dist/cli/{monitors-XNB5YNC3.js → monitors-YZZRKSTG.js} +97 -97
  113. package/dist/cli/{org-2UYWJXG7.js → org-JY7HLHBA.js} +74 -74
  114. package/dist/cli/{org-S5VZL5KZ.mjs → org-NCXT6RB4.mjs} +8 -8
  115. package/dist/cli/{pages-ZQRRTZIT.js → pages-6ZOHXL6X.js} +20 -20
  116. package/dist/cli/{pages-NGVJO7PA.mjs → pages-QJO53QAT.mjs} +8 -8
  117. package/dist/cli/{play-3JNERHSC.mjs → play-SQRS7JN3.mjs} +20 -20
  118. package/dist/cli/{play-P3OYQ7JV.js → play-TAYGSR35.js} +336 -336
  119. package/dist/cli/{providers-27O6BOAP.mjs → providers-3IR3N5DT.mjs} +8 -8
  120. package/dist/cli/{providers-2FFGAPUR.js → providers-4Q5LOIQ5.js} +11 -11
  121. package/dist/cli/{quickstart-5G2UPKJV.js → quickstart-ITNUZENY.js} +6 -6
  122. package/dist/cli/{quickstart-B2EXIAU6.mjs → quickstart-KCZCYO6B.mjs} +4 -4
  123. package/dist/cli/{run-3QFXYV3Z.mjs → run-JCWRWDSK.mjs} +24 -24
  124. package/dist/cli/{run-2TGD7FAL.js → run-YQ57LMKN.js} +42 -42
  125. package/dist/cli/run-identity-HFYABUJS.js +31 -0
  126. package/dist/cli/{run-identity-MRNCEGV2.mjs → run-identity-MNRZS2N3.mjs} +9 -3
  127. package/dist/cli/{search-GXEEOMVD.mjs → search-FUM27HQP.mjs} +9 -9
  128. package/dist/cli/search-I425FRTK.js +32 -0
  129. package/dist/cli/{search-output-DMIJQTAA.mjs → search-output-EWEYT34M.mjs} +4 -4
  130. package/dist/cli/{search-output-I4RZPHU6.js → search-output-XAMWKRJH.js} +6 -6
  131. package/dist/cli/{secrets-MJMNRXZX.js → secrets-2IRRRZBL.js} +17 -17
  132. package/dist/cli/{secrets-DQI5YCS6.mjs → secrets-YTI4IAKR.mjs} +8 -8
  133. package/dist/cli/{sessions-HAUN3VYE.mjs → sessions-EFS4KUPF.mjs} +5 -5
  134. package/dist/cli/{sessions-QS74ZTRH.js → sessions-HWTRDXBQ.js} +6 -6
  135. package/dist/cli/{settings-VQBOS6VU.js → settings-FRVEIOKR.js} +80 -80
  136. package/dist/cli/{settings-E64HIUXW.mjs → settings-NTOO42DY.mjs} +13 -13
  137. package/dist/cli/{setup-KRW3JCJE.js → setup-6ZIOAE2V.js} +49 -49
  138. package/dist/cli/{setup-NRUG42F2.mjs → setup-PAVQPALX.mjs} +9 -9
  139. package/dist/cli/{skills-AHLZRTKZ.mjs → skills-3O3XTPUM.mjs} +7 -7
  140. package/dist/cli/skills-6ZNMO2PR.js +36 -0
  141. package/dist/cli/{tools-INRRIWOU.mjs → tools-7QSB65UL.mjs} +18 -18
  142. package/dist/cli/tools-DPV2JES6.js +57 -0
  143. package/dist/cli/update-Q5GUDEAW.js +34 -0
  144. package/dist/cli/{update-ETOWEJ7D.mjs → update-T6CYS5ZT.mjs} +9 -9
  145. package/dist/cli/{workflow-RUAYQK4D.js → workflow-4KMJBMGX.js} +19 -19
  146. package/dist/cli/{workflow-JT2DOSCJ.mjs → workflow-OMSXZ73T.mjs} +9 -9
  147. package/dist/{client-Cj3UqrCZ.d.ts → client-Bn8FwKGO.d.ts} +1 -1
  148. package/dist/{client-DSCTVVRj.d.mts → client-CfNRmjLK.d.mts} +1 -1
  149. package/dist/{client-core-dI4KTuWS.d.ts → client-core-B91EV0WS.d.ts} +19 -0
  150. package/dist/{client-core-B-8DTIN1.d.mts → client-core-BG8Cgrmk.d.mts} +19 -0
  151. package/dist/index.d.mts +3 -3
  152. package/dist/index.d.ts +3 -3
  153. package/dist/index.js +19 -19
  154. package/dist/index.mjs +8 -8
  155. package/dist/install-integrity.json +114 -108
  156. package/dist/plays/play-description.d.mts +1 -1
  157. package/dist/plays/play-description.d.ts +1 -1
  158. package/dist/plays/run-request.d.mts +1 -1
  159. package/dist/plays/run-request.d.ts +1 -1
  160. package/dist/plays/run-request.js +3 -3
  161. package/dist/plays/run-request.mjs +2 -2
  162. package/dist/release.d.mts +1 -1
  163. package/dist/release.d.ts +1 -1
  164. package/dist/release.js +2 -2
  165. package/dist/release.mjs +1 -1
  166. package/dist/server.d.mts +2 -2
  167. package/dist/server.d.ts +2 -2
  168. package/dist/server.js +7 -7
  169. package/dist/server.mjs +5 -5
  170. package/package.json +1 -1
  171. package/dist/cli/auth-32FRISRT.js +0 -43
  172. package/dist/cli/describe-DEXYQMBO.js +0 -50
  173. package/dist/cli/git-RDKLITUC.js +0 -25
  174. package/dist/cli/run-identity-G257GQED.js +0 -25
  175. package/dist/cli/search-YKTYDBCN.js +0 -32
  176. package/dist/cli/skills-PGVIJK6Q.js +0 -36
  177. package/dist/cli/tools-IXXN2RE4.js +0 -57
  178. package/dist/cli/update-QW4RV2UX.js +0 -34
@@ -21,3 +21,44 @@ export const PLAY_RUNTIME_HARNESS_ALLOWED_PATH_PREFIXES = [
21
21
  '/api/v2/plays/',
22
22
  '/api/v2/integrations/',
23
23
  ] as const;
24
+
25
+ /**
26
+ * Paths the runner, watchdog, and gateways send. Senders and the gateway
27
+ * route table (`@deepline/runtime-gateway/routes`) share these builders, so a
28
+ * sent path and its route cannot drift apart.
29
+ */
30
+ export const APP_RUNTIME_COMPAT_PATH = '/api/v2/plays/runtime';
31
+ /** Pre-split runners posted runtime actions to `/runtime` on the gateway. */
32
+ export const LEGACY_GATEWAY_RUNTIME_PATH = '/runtime';
33
+ export const PLAY_RUN_START_PATH = '/api/v2/plays/run';
34
+ export const RUNTIME_EXECUTOR_JWKS_PATH =
35
+ '/.well-known/deepline-runtime-jwks.json';
36
+
37
+ export type RuntimeToolPathKind = 'metadata' | 'auth_scope' | 'event_wait';
38
+
39
+ export function runtimeToolPath(
40
+ toolId: string,
41
+ kind: RuntimeToolPathKind,
42
+ ): string {
43
+ const base = `/api/v2/plays/runtime-tools/${encodeURIComponent(toolId)}`;
44
+ return kind === 'metadata'
45
+ ? base
46
+ : kind === 'auth_scope'
47
+ ? `${base}/auth-scope`
48
+ : `${base}/event-wait`;
49
+ }
50
+
51
+ export type IntegrationExecuteSuffix =
52
+ | 'execute'
53
+ | 'stream'
54
+ | 'execute-fenced-v1'
55
+ | 'execute-fenced-v2'
56
+ | 'execute-with-provider-permit'
57
+ | 'execute-fenced-v2-with-provider-permit';
58
+
59
+ export function integrationExecutePath(
60
+ toolId: string,
61
+ suffix: IntegrationExecuteSuffix,
62
+ ): string {
63
+ return `/api/v2/integrations/${encodeURIComponent(toolId)}/${suffix}`;
64
+ }
@@ -213,6 +213,10 @@ type RuntimeApiContext = {
213
213
  dbSessionStrategy?: 'preloaded' | 'gateway_only' | 'trusted_dynamic' | null;
214
214
  preloadedDbSessions?: PreloadedRuntimeDbSession[] | null;
215
215
  vercelProtectionBypassToken?: string | null;
216
+ /** App transport; an app-hosted gateway passes its in-process dispatch so
217
+ * calls carrying a deployment-pinned token stay inside that deployment.
218
+ * Absent uses fetch. */
219
+ fetch?: typeof fetch;
216
220
  runtimeTestFaultHeader?: string | null;
217
221
  disablePostgresPoolCache?: boolean | null;
218
222
  postgresSessionUnwrapKey?: string | null;
@@ -1037,7 +1041,7 @@ async function postRuntimeApi<TResponse>(
1037
1041
  Math.min(runtimeApiRequestTimeoutMs(body.action), operationRemainingMs),
1038
1042
  );
1039
1043
  try {
1040
- response = await fetch(url, {
1044
+ response = await (context.fetch ?? fetch)(url, {
1041
1045
  method: 'POST',
1042
1046
  headers: resolveRuntimeApiHeaders(context, vercelHeaders),
1043
1047
  body:
@@ -0,0 +1,374 @@
1
+ /**
2
+ * The one owner of "how does this caller reach the app".
3
+ *
4
+ * Every code-owned call from a worker, a gateway, or a sandbox to the app's
5
+ * runtime API resolves its transport here. The decision comes from the
6
+ * executor token's `runtime_gateway_deployment_id` claim and the launch pin,
7
+ * never from caller convention:
8
+ *
9
+ * - `direct`: the launch's runtime API origin over the network (Fly, every
10
+ * unpinned launch, and the primary/dispatch/internal tokens). A token that
11
+ * carries a gateway pin claim is refused here
12
+ * (`runtime_gateway_transport_unpinned`): the app accepts it only inside its
13
+ * pinned deployment's gateway, so a direct call would be a 403.
14
+ * - `pinned_gateway`: a worker holding a pinned attempt token reaches the app
15
+ * through the pinned receipt gateway, with an OIDC credential it mints and
16
+ * staged bodies above the inline limit (the sandbox's contract).
17
+ * - `in_process`: inside an app-hosted gateway, the gateway's own deployment
18
+ * is called in process and never receives the protection-bypass credential.
19
+ * - `sandbox`: the sandbox runner's credentialed fetch, installed once, and an
20
+ * explicit wrapper for callers that do not use the global fetch
21
+ * (`safeOutboundFetch`).
22
+ *
23
+ * Unpinned callers get exactly what they had before this module existed.
24
+ * Generated sandbox scripts (watchdog, crash terminal pusher) cannot import;
25
+ * they embed `runtimeGatewayOidcRequestHeaders` and are allowlisted in
26
+ * `scripts/check-runtime-app-transport-boundary.ts`.
27
+ */
28
+ import type { WorkflowRunExecutionTokenClaims } from './internal-bootstrap-token';
29
+ import {
30
+ readRuntimeGatewayLaunchPin,
31
+ RuntimeGatewayLaunchError,
32
+ type RuntimeGatewayLaunchFields,
33
+ type RuntimeGatewayLaunchPin,
34
+ } from './runtime-gateway-profile';
35
+ import {
36
+ mintRuntimeGatewayOidcToken,
37
+ RUNTIME_GATEWAY_OIDC_FILE_ENV,
38
+ RUNTIME_GATEWAY_OIDC_PRIVATE_KEY_ENV,
39
+ RUNTIME_GATEWAY_OIDC_RENEW_AFTER_SECONDS,
40
+ RuntimeGatewayOidcError,
41
+ type RuntimeGatewayOidcSigningKey,
42
+ } from './runtime-gateway-oidc';
43
+ import {
44
+ createRuntimeGatewayCredentialFetch,
45
+ currentRuntimeGatewayOidcToken,
46
+ runtimeGatewayCredentialScopeFromPin,
47
+ type RuntimeGatewaySandboxContext,
48
+ } from './runtime-gateway-sandbox-credential';
49
+ import { runtimeApiBaseUrlFromLaunch } from './scheduler-backends/runtime-api-origin';
50
+
51
+ export type RuntimeAppTransportKind =
52
+ | 'direct'
53
+ | 'pinned_gateway'
54
+ | 'in_process'
55
+ | 'sandbox';
56
+
57
+ export type RuntimeAppTransport = Readonly<{
58
+ kind: RuntimeAppTransportKind;
59
+ baseUrl: string;
60
+ /** Absent: the global fetch (the direct transport's historical default). */
61
+ fetch?: typeof fetch;
62
+ vercelProtectionBypassToken: string | null;
63
+ }>;
64
+
65
+ type TransportLaunch = RuntimeGatewayLaunchFields & {
66
+ runId: string;
67
+ executorToken: string;
68
+ baseUrl: string;
69
+ runtimeApiBaseUrl?: string | null;
70
+ vercelProtectionBypassToken?: string | null;
71
+ };
72
+
73
+ /**
74
+ * Read an executor token's claims without verifying its signature. Only for
75
+ * a holder deciding how to use its own token; the receiver always verifies.
76
+ */
77
+ export function readExecutorTokenClaimsUnverified(
78
+ token: string,
79
+ ): Partial<WorkflowRunExecutionTokenClaims> | null {
80
+ try {
81
+ const payload = token.split('.')[1];
82
+ const decoded: unknown = payload
83
+ ? JSON.parse(Buffer.from(payload, 'base64url').toString('utf8'))
84
+ : null;
85
+ return decoded && typeof decoded === 'object' && !Array.isArray(decoded)
86
+ ? (decoded as Partial<WorkflowRunExecutionTokenClaims>)
87
+ : null;
88
+ } catch {
89
+ return null;
90
+ }
91
+ }
92
+
93
+ function tokenGatewayDeploymentId(token: string): string | undefined {
94
+ return readExecutorTokenClaimsUnverified(token)
95
+ ?.runtime_gateway_deployment_id;
96
+ }
97
+
98
+ /** Mint the OIDC credential for one attempt of a pinned run, loudly. */
99
+ export function mintRuntimeGatewayAttemptOidcToken(input: {
100
+ pin: RuntimeGatewayLaunchPin;
101
+ runId: string;
102
+ attempt: number;
103
+ nowMs: number;
104
+ signingKey: RuntimeGatewayOidcSigningKey | null;
105
+ }): string {
106
+ if (!input.signingKey) {
107
+ throw new RuntimeGatewayLaunchError(
108
+ 'runtime_gateway_oidc_unavailable',
109
+ `This worker has no ${RUNTIME_GATEWAY_OIDC_PRIVATE_KEY_ENV}; it cannot start a run pinned to ${input.pin.deploymentId}.`,
110
+ );
111
+ }
112
+ try {
113
+ return mintRuntimeGatewayOidcToken({
114
+ signingKey: input.signingKey,
115
+ deploymentId: input.pin.deploymentId,
116
+ runId: input.runId,
117
+ attempt: input.attempt,
118
+ nowMs: input.nowMs,
119
+ });
120
+ } catch (error) {
121
+ // A secret that is not this environment's published key (or not a key
122
+ // at all) can never mint a credential the pinned gateway accepts.
123
+ if (error instanceof RuntimeGatewayOidcError) {
124
+ throw new RuntimeGatewayLaunchError(
125
+ 'runtime_gateway_oidc_unavailable',
126
+ `This worker cannot sign a gateway credential for a run pinned to ${input.pin.deploymentId}: ${error.message}`,
127
+ );
128
+ }
129
+ throw error;
130
+ }
131
+ }
132
+
133
+ export class RuntimeAppTransportError extends Error {
134
+ readonly code = 'runtime_gateway_transport_unpinned';
135
+ constructor(message: string) {
136
+ super(`runtime_gateway_transport_unpinned: ${message}`);
137
+ this.name = 'RuntimeAppTransportError';
138
+ }
139
+ }
140
+
141
+ function directTransport(
142
+ launch: Pick<
143
+ TransportLaunch,
144
+ 'baseUrl' | 'runtimeApiBaseUrl' | 'vercelProtectionBypassToken'
145
+ >,
146
+ executorToken: string,
147
+ ): RuntimeAppTransport {
148
+ if (tokenGatewayDeploymentId(executorToken) !== undefined) {
149
+ throw new RuntimeAppTransportError(
150
+ 'a token pinned to an app-hosted gateway deployment cannot call the app directly; resolve it as a worker or in-gateway caller.',
151
+ );
152
+ }
153
+ return {
154
+ kind: 'direct',
155
+ baseUrl: runtimeApiBaseUrlFromLaunch(launch),
156
+ vercelProtectionBypassToken: launch.vercelProtectionBypassToken ?? null,
157
+ };
158
+ }
159
+
160
+ /**
161
+ * A worker's transport for one launch. The primary launch token carries no
162
+ * pin claim and stays direct; a pinned attempt token goes through the pinned
163
+ * receipt gateway with a credential minted here and staged large bodies.
164
+ */
165
+ function workerTransport(input: {
166
+ launch: TransportLaunch;
167
+ signingKey: () => RuntimeGatewayOidcSigningKey | null;
168
+ nowMs?: () => number;
169
+ fetch?: typeof fetch;
170
+ }): RuntimeAppTransport {
171
+ const { launch } = input;
172
+ const pin = readRuntimeGatewayLaunchPin(launch);
173
+ const claims = readExecutorTokenClaimsUnverified(launch.executorToken);
174
+ if (!pin || claims?.runtime_gateway_deployment_id === undefined) {
175
+ return directTransport(launch, launch.executorToken);
176
+ }
177
+ const attempt = claims.run_attempt;
178
+ if (
179
+ claims.runtime_gateway_deployment_id !== pin.deploymentId ||
180
+ typeof attempt !== 'number' ||
181
+ !Number.isSafeInteger(attempt)
182
+ ) {
183
+ throw new RuntimeGatewayLaunchError(
184
+ 'runtime_gateway_executor_token_unpinned',
185
+ `The worker's executor token for run ${launch.runId} does not name pinned deployment ${pin.deploymentId} with an attempt.`,
186
+ );
187
+ }
188
+ const now = input.nowMs ?? Date.now;
189
+ let credential: { token: string; mintedAtMs: number } | null = null;
190
+ const currentToken = (): string => {
191
+ const nowMs = now();
192
+ if (
193
+ !credential ||
194
+ nowMs - credential.mintedAtMs >=
195
+ RUNTIME_GATEWAY_OIDC_RENEW_AFTER_SECONDS * 1_000
196
+ ) {
197
+ credential = {
198
+ token: mintRuntimeGatewayAttemptOidcToken({
199
+ pin,
200
+ runId: launch.runId,
201
+ attempt,
202
+ nowMs,
203
+ signingKey: input.signingKey(),
204
+ }),
205
+ mintedAtMs: nowMs,
206
+ };
207
+ }
208
+ return credential.token;
209
+ };
210
+ return {
211
+ kind: 'pinned_gateway',
212
+ baseUrl: pin.receiptGatewayBaseUrl,
213
+ fetch: createRuntimeGatewayCredentialFetch({
214
+ scope: runtimeGatewayCredentialScopeFromPin(pin),
215
+ // Late-bound so a stubbed global fetch is honored per call.
216
+ send: input.fetch ?? ((url, init) => fetch(url, init)),
217
+ currentToken,
218
+ }),
219
+ // The gateway is the pinned immutable deployment, reached with OIDC.
220
+ vercelProtectionBypassToken: null,
221
+ };
222
+ }
223
+
224
+ /**
225
+ * Inside a gateway. App-hosted (an upstream fetch is present): the gateway's
226
+ * own deployment, in process, without the bypass credential. Fly: the
227
+ * launch's network transport, unchanged.
228
+ *
229
+ * `baseUrl: 'launch_callback'` keeps the data-plane context on the launch's
230
+ * callback URL, as the Fly gateway always has (the in-process dispatcher
231
+ * selects a handler by path only, so it does not matter there).
232
+ */
233
+ function inGatewayTransport(input: {
234
+ upstreamFetch: typeof fetch | undefined;
235
+ launch?: Pick<
236
+ TransportLaunch,
237
+ 'baseUrl' | 'runtimeApiBaseUrl' | 'vercelProtectionBypassToken'
238
+ >;
239
+ baseUrl?: 'runtime_api' | 'launch_callback';
240
+ }): RuntimeAppTransport {
241
+ const baseUrl = !input.launch
242
+ ? ''
243
+ : input.baseUrl === 'launch_callback'
244
+ ? input.launch.baseUrl
245
+ : runtimeApiBaseUrlFromLaunch(input.launch);
246
+ return input.upstreamFetch
247
+ ? {
248
+ kind: 'in_process',
249
+ baseUrl,
250
+ fetch: input.upstreamFetch,
251
+ vercelProtectionBypassToken: null,
252
+ }
253
+ : {
254
+ kind: 'direct',
255
+ baseUrl,
256
+ vercelProtectionBypassToken:
257
+ input.launch?.vercelProtectionBypassToken ?? null,
258
+ };
259
+ }
260
+
261
+ export type RuntimeAppTransportCaller =
262
+ | {
263
+ caller: 'direct';
264
+ launch: Pick<
265
+ TransportLaunch,
266
+ 'baseUrl' | 'runtimeApiBaseUrl' | 'vercelProtectionBypassToken'
267
+ >;
268
+ /** The token the call carries (primary, dispatch, or internal). */
269
+ executorToken: string;
270
+ }
271
+ | {
272
+ caller: 'worker';
273
+ launch: TransportLaunch;
274
+ signingKey: () => RuntimeGatewayOidcSigningKey | null;
275
+ nowMs?: () => number;
276
+ fetch?: typeof fetch;
277
+ }
278
+ | {
279
+ caller: 'in_gateway';
280
+ upstreamFetch: typeof fetch | undefined;
281
+ launch?: Pick<
282
+ TransportLaunch,
283
+ 'baseUrl' | 'runtimeApiBaseUrl' | 'vercelProtectionBypassToken'
284
+ >;
285
+ baseUrl?: 'runtime_api' | 'launch_callback';
286
+ };
287
+
288
+ export function resolveRuntimeAppTransport(
289
+ caller: RuntimeAppTransportCaller,
290
+ ): RuntimeAppTransport {
291
+ switch (caller.caller) {
292
+ case 'direct':
293
+ return directTransport(caller.launch, caller.executorToken);
294
+ case 'worker':
295
+ return workerTransport(caller);
296
+ case 'in_gateway':
297
+ return inGatewayTransport(caller);
298
+ }
299
+ }
300
+
301
+ /**
302
+ * Fields a runtime API context spreads in. Direct: nothing (the context keeps
303
+ * its own network defaults byte for byte). Otherwise the transport's fetch
304
+ * and its bypass decision.
305
+ */
306
+ export function runtimeAppTransportContextFields(
307
+ transport: RuntimeAppTransport,
308
+ ): { fetch?: typeof fetch; vercelProtectionBypassToken?: string | null } {
309
+ return transport.kind === 'direct'
310
+ ? {}
311
+ : {
312
+ ...(transport.fetch ? { fetch: transport.fetch } : {}),
313
+ vercelProtectionBypassToken: transport.vercelProtectionBypassToken,
314
+ };
315
+ }
316
+
317
+ // ---------------------------------------------------------------------------
318
+ // Sandbox
319
+
320
+ let sandboxCredential: {
321
+ context: RuntimeGatewaySandboxContext;
322
+ currentToken: () => string | null;
323
+ } | null = null;
324
+
325
+ /**
326
+ * Sandbox runner entrypoint only: route this process's gateway traffic,
327
+ * global fetch and every `sandboxRuntimeAppFetch`-wrapped sender, through
328
+ * the credential. A no-op for unpinned runs. Returns a restore function.
329
+ */
330
+ export function installSandboxRuntimeAppTransport(
331
+ context: RuntimeGatewaySandboxContext,
332
+ env: Readonly<Record<string, string | undefined>> = process.env,
333
+ ): () => void {
334
+ const scope = context.runtimeGatewayCredentialScope;
335
+ if (!scope || !context.runtimeGatewayOidcToken) {
336
+ return () => undefined;
337
+ }
338
+ const filePath = env[RUNTIME_GATEWAY_OIDC_FILE_ENV] ?? null;
339
+ const installed = {
340
+ context,
341
+ currentToken: () =>
342
+ currentRuntimeGatewayOidcToken(context.runtimeGatewayOidcToken, filePath),
343
+ };
344
+ sandboxCredential = installed;
345
+ const original = globalThis.fetch;
346
+ globalThis.fetch = createRuntimeGatewayCredentialFetch({
347
+ scope,
348
+ send: original,
349
+ currentToken: installed.currentToken,
350
+ });
351
+ return () => {
352
+ globalThis.fetch = original;
353
+ if (sandboxCredential === installed) sandboxCredential = null;
354
+ };
355
+ }
356
+
357
+ /**
358
+ * Wrap a sender that does not go through the global fetch (the runner's
359
+ * `safeOutboundFetch`) so gateway requests carry the installed credential.
360
+ * Requests outside the pinned gateway, and every request on an unpinned run,
361
+ * pass through untouched.
362
+ */
363
+ export function sandboxRuntimeAppFetch(send: typeof fetch): typeof fetch {
364
+ return ((input, init) => {
365
+ const installed = sandboxCredential;
366
+ const scope = installed?.context.runtimeGatewayCredentialScope;
367
+ if (!installed || !scope) return send(input, init);
368
+ return createRuntimeGatewayCredentialFetch({
369
+ scope,
370
+ send,
371
+ currentToken: installed.currentToken,
372
+ })(input, init);
373
+ }) as typeof fetch;
374
+ }
@@ -190,10 +190,15 @@ export async function mintPlayRunAttemptRuntimeToken(input: {
190
190
  terminalContract?: DirectCompletedTerminalContract | null;
191
191
  capabilities?: readonly RuntimeExecutionCapability[];
192
192
  runtimeOrigin?: string | null;
193
+ /** Runtime gateway pin claims; both or neither. Absent on unpinned runs. */
194
+ runtimeGatewayDeploymentId?: string | null;
195
+ runtimeGatewayProfileId?: string | null;
193
196
  runLedgerIngressPrefixOrigin?: string | null;
194
197
  }): Promise<string> {
195
198
  return await mintWorkflowExecutorToken({
196
199
  ...runtimeAuthorityMintInput(input.descriptor, input),
200
+ runtimeGatewayDeploymentId: input.runtimeGatewayDeploymentId,
201
+ runtimeGatewayProfileId: input.runtimeGatewayProfileId,
197
202
  runLedgerIngressPrefixOrigin: input.runLedgerIngressPrefixOrigin,
198
203
  });
199
204
  }
@@ -1,4 +1,8 @@
1
1
  import { PLAY_RUNTIME_BACKENDS, type PlayRuntimeBackendId } from './backend';
2
+ import {
3
+ normalizeRuntimeGatewayProfileId,
4
+ type RuntimeGatewayProfileId,
5
+ } from './runtime-gateway-profile';
2
6
 
3
7
  export const PLAY_RUNTIME_ENVIRONMENTS = ['preview'] as const;
4
8
 
@@ -12,8 +16,21 @@ export type PlayRuntimeSelection = {
12
16
  backend?: Extract<PlayRuntimeBackendId, 'daytona' | 'modal'>;
13
17
  /** Durable placement contract for the selected Preview worker lane. */
14
18
  sandboxPlacementPolicy?: string;
19
+ /** Where this run's receipt/execution gateways are hosted. Absent: Fly. */
20
+ gatewayProfile?: RuntimeGatewayProfileId;
15
21
  };
16
22
 
23
+ /**
24
+ * Absent stays absent (undefined); a present value must name a known profile
25
+ * or the whole selector is invalid (null).
26
+ */
27
+ function selectedGatewayProfile(
28
+ value: unknown,
29
+ ): RuntimeGatewayProfileId | undefined | null {
30
+ if (value === undefined) return undefined;
31
+ return normalizeRuntimeGatewayProfileId(value) ?? null;
32
+ }
33
+
17
34
  const PLAY_RUNTIME_NAMESPACE_PATTERN = /^[a-z][a-z0-9-]{0,30}$/;
18
35
  const PLAY_RUNTIME_SANDBOX_PLACEMENT_POLICY_PATTERN =
19
36
  /^[a-z][a-z0-9_]*@[1-9][0-9]*$/;
@@ -51,12 +68,15 @@ export function normalizePlayRuntimeSelection(
51
68
  key !== 'environment' &&
52
69
  key !== 'namespace' &&
53
70
  key !== 'backend' &&
54
- key !== 'sandboxPlacementPolicy',
71
+ key !== 'sandboxPlacementPolicy' &&
72
+ key !== 'gatewayProfile',
55
73
  ) ||
56
74
  record.environment !== 'preview'
57
75
  ) {
58
76
  return null;
59
77
  }
78
+ const gatewayProfile = selectedGatewayProfile(record.gatewayProfile);
79
+ if (gatewayProfile === null) return null;
60
80
  const namespace = normalizePlayRuntimeNamespace(record.namespace);
61
81
  if (!namespace) return null;
62
82
  const sandboxPlacementPolicy =
@@ -83,6 +103,7 @@ export function normalizePlayRuntimeSelection(
83
103
  namespace,
84
104
  ...(record.backend === undefined ? {} : { backend: record.backend }),
85
105
  ...(sandboxPlacementPolicy ? { sandboxPlacementPolicy } : {}),
106
+ ...(gatewayProfile ? { gatewayProfile } : {}),
86
107
  };
87
108
  }
88
109
 
@@ -91,10 +112,18 @@ export function normalizePlayRuntimeSelection(
91
112
  * the `play_run.sandbox_placement` capability in
92
113
  * `@deepline/auth/internal-capabilities` decides who may send it.
93
114
  */
94
- export type PlayNativeRuntimeSelection = {
95
- environment: 'native';
96
- sandboxPlacementPolicy: string;
97
- };
115
+ export type PlayNativeRuntimeSelection =
116
+ | {
117
+ environment: 'native';
118
+ sandboxPlacementPolicy: string;
119
+ gatewayProfile?: RuntimeGatewayProfileId;
120
+ }
121
+ | {
122
+ environment: 'native';
123
+ sandboxPlacementPolicy?: undefined;
124
+ /** A native selection names at least one of the two. */
125
+ gatewayProfile: RuntimeGatewayProfileId;
126
+ };
98
127
 
99
128
  /** Every `runtime` value a Play run accepts. */
100
129
  export type PlayRunRuntimeSelection =
@@ -111,17 +140,28 @@ export function normalizePlayRunRuntimeSelection(
111
140
  }
112
141
  if (
113
142
  Object.keys(record).some(
114
- (key) => key !== 'environment' && key !== 'sandboxPlacementPolicy',
143
+ (key) =>
144
+ key !== 'environment' &&
145
+ key !== 'sandboxPlacementPolicy' &&
146
+ key !== 'gatewayProfile',
115
147
  )
116
148
  ) {
117
149
  return null;
118
150
  }
151
+ const gatewayProfile = selectedGatewayProfile(record.gatewayProfile);
152
+ if (gatewayProfile === null) return null;
153
+ if (record.sandboxPlacementPolicy === undefined) {
154
+ return gatewayProfile ? { environment: 'native', gatewayProfile } : null;
155
+ }
119
156
  const sandboxPlacementPolicy = normalizePlayRuntimeSandboxPlacementPolicy(
120
157
  record.sandboxPlacementPolicy,
121
158
  );
122
- return sandboxPlacementPolicy
123
- ? { environment: 'native', sandboxPlacementPolicy }
124
- : null;
159
+ if (!sandboxPlacementPolicy) return null;
160
+ return {
161
+ environment: 'native',
162
+ sandboxPlacementPolicy,
163
+ ...(gatewayProfile ? { gatewayProfile } : {}),
164
+ };
125
165
  }
126
166
 
127
167
  export function normalizePlayRuntimeEnvironment(
@@ -7,6 +7,7 @@ import {
7
7
  type KeyObject,
8
8
  } from 'node:crypto';
9
9
  import { isRuntimeGatewayDeploymentId } from './runtime-gateway-profile';
10
+ import { isRuntimeScopeRunId } from './run-identity';
10
11
  import { isTestOnlyRuntime } from './runtime-env';
11
12
 
12
13
  /**
@@ -206,6 +207,27 @@ function rsaPrivateKey(signingKey: RuntimeGatewayOidcSigningKey): KeyObject {
206
207
  return key;
207
208
  }
208
209
 
210
+ /**
211
+ * Parsed verification keys, by key material. Every app-gateway request
212
+ * verifies one credential; parsing the JWK into a KeyObject each time was
213
+ * per-request work. Bounded by the published (and, in tests, test) keys.
214
+ */
215
+ const publicKeyObjects = new Map<string, KeyObject>();
216
+ const PUBLIC_KEY_OBJECT_CACHE_MAX = 32;
217
+
218
+ function publicKeyObject(jwk: RuntimeGatewayOidcPublicJwk): KeyObject {
219
+ const cacheKey = `${jwk.kid}\u0000${jwk.n}\u0000${jwk.e}`;
220
+ let key = publicKeyObjects.get(cacheKey);
221
+ if (!key) {
222
+ key = createPublicKey({ key: jwk as JsonWebKey, format: 'jwk' });
223
+ if (publicKeyObjects.size >= PUBLIC_KEY_OBJECT_CACHE_MAX) {
224
+ publicKeyObjects.delete(publicKeyObjects.keys().next().value!);
225
+ }
226
+ publicKeyObjects.set(cacheKey, key);
227
+ }
228
+ return key;
229
+ }
230
+
209
231
  function encodeSegment(value: object): string {
210
232
  return Buffer.from(JSON.stringify(value)).toString('base64url');
211
233
  }
@@ -251,7 +273,7 @@ export function mintRuntimeGatewayOidcToken(input: {
251
273
  }): string {
252
274
  if (
253
275
  !isRuntimeGatewayDeploymentId(input.deploymentId) ||
254
- !input.runId.trim() ||
276
+ !isRuntimeScopeRunId(input.runId) ||
255
277
  !Number.isSafeInteger(input.attempt) ||
256
278
  input.attempt < 0 ||
257
279
  !Number.isFinite(input.nowMs)
@@ -327,7 +349,7 @@ function isClaimsShape(
327
349
  typeof claims.aud === 'string' &&
328
350
  typeof claims.sub === 'string' &&
329
351
  typeof claims.deployment_id === 'string' &&
330
- typeof claims.run_id === 'string' &&
352
+ isRuntimeScopeRunId(claims.run_id) &&
331
353
  Number.isSafeInteger(claims.run_attempt) &&
332
354
  (claims.deepline_env === 'preview' ||
333
355
  claims.deepline_env === 'production') &&
@@ -399,7 +421,7 @@ export function verifyRuntimeGatewayOidcToken(
399
421
  signatureValid = verify(
400
422
  'RSA-SHA256',
401
423
  Buffer.from(`${headerPart}.${claimsPart}`),
402
- createPublicKey({ key: jwk as JsonWebKey, format: 'jwk' }),
424
+ publicKeyObject(jwk),
403
425
  Buffer.from(signaturePart, 'base64url'),
404
426
  );
405
427
  } catch {
@@ -19,7 +19,18 @@ export type RuntimeGatewayProfile = Readonly<{
19
19
  hosting: 'admitting_app_deployment';
20
20
  receiptPath: '/api/receipt';
21
21
  executionPath: '/api/execution';
22
- /** Oldest pin a new attempt or resumed sandbox may still use. */
22
+ /**
23
+ * Oldest pin a new attempt or resumed sandbox may still use.
24
+ *
25
+ * Contract (`vercel_app@1`: 6 hours): the pin is checked only when an
26
+ * attempt starts or a sandbox resumes, never while one runs, so it must
27
+ * outlive the longest single attempt. A pinned run resumed or retried more
28
+ * than 6 hours after it was pinned (a long durable wait, a late retry)
29
+ * fails loudly with `runtime_gateway_pin_expired` instead of running on a
30
+ * deployment that may be gone; the Play must be re-run. The profile is
31
+ * Preview-only today. Enabling it in Production must revisit this bound
32
+ * against Production wait and retry horizons.
33
+ */
23
34
  maxPinAgeMs: number;
24
35
  /** Scheduler Postgres clients one gateway function instance may hold. */
25
36
  poolMaxClients: number;