deepline 0.3.520 → 0.3.522
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.
- package/dist/bundling-sources/sdk/src/plays/client-core.ts +16 -45
- package/dist/bundling-sources/sdk/src/release.ts +1 -1
- package/dist/bundling-sources/shared_libs/observability/scheduled-jobs.json +0 -18
- package/dist/bundling-sources/shared_libs/play-data-plane/r2.ts +0 -155
- package/dist/bundling-sources/shared_libs/play-runtime/durable-runs-rollout.ts +8 -1
- package/dist/bundling-sources/shared_libs/play-runtime/fixture-behavior.ts +24 -0
- package/dist/bundling-sources/shared_libs/play-runtime/internal-bootstrap-token.ts +1 -54
- package/dist/bundling-sources/shared_libs/play-runtime/launch-identity.ts +2 -7
- package/dist/bundling-sources/shared_libs/play-runtime/protocol.ts +0 -13
- package/dist/bundling-sources/shared_libs/play-runtime/run-identity.ts +0 -56
- package/dist/bundling-sources/shared_libs/play-runtime/runner-app/index.ts +10 -21
- package/dist/bundling-sources/shared_libs/play-runtime/runner-app/runtime-heartbeat.ts +1 -1
- package/dist/bundling-sources/shared_libs/play-runtime/runner-app/runtime-progress-push.ts +2 -1
- package/dist/bundling-sources/shared_libs/play-runtime/runner-app/runtime-terminal-push.ts +1 -1
- package/dist/bundling-sources/shared_libs/play-runtime/runner-backends/backends/daytona-lifecycle.ts +2 -37
- package/dist/bundling-sources/shared_libs/play-runtime/runner-backends/backends/daytona-payload-transport.ts +3 -74
- package/dist/bundling-sources/shared_libs/play-runtime/runner-backends/backends/daytona-runtime-watchdog.ts +2 -226
- package/dist/bundling-sources/shared_libs/play-runtime/runner-backends/backends/modal.ts +3 -36
- package/dist/bundling-sources/shared_libs/play-runtime/runtime-api-paths.ts +0 -26
- package/dist/bundling-sources/shared_libs/play-runtime/runtime-api.ts +1 -9
- package/dist/bundling-sources/shared_libs/play-runtime/runtime-authority.ts +0 -5
- package/dist/bundling-sources/shared_libs/play-runtime/runtime-environment.ts +9 -49
- package/dist/bundling-sources/shared_libs/play-runtime/runtime-gateway-oidc.ts +11 -447
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backend.ts +5 -4
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/absurd-runtime-worker.ts +12 -207
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/absurd-shared.ts +3 -17
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/gateway-progress-registry.ts +0 -9
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/postgres-progress.ts +2 -18
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/postgres.ts +15 -175
- package/dist/bundling-sources/shared_libs/play-runtime/workflow-executor-auth.ts +0 -4
- package/dist/cli/{artifacts-4DSPSIU5.js → artifacts-GIZZH5VA.js} +20 -20
- package/dist/cli/{artifacts-D6PU7SBC.mjs → artifacts-RY2MK74N.mjs} +8 -8
- package/dist/cli/auth-JSES2JOQ.js +43 -0
- package/dist/cli/{auth-MXKNPUXW.mjs → auth-YIBY5IKT.mjs} +5 -5
- package/dist/cli/{billing-SQ26NKGH.mjs → billing-G3IX3SNY.mjs} +8 -8
- package/dist/cli/{billing-WEYCNJIR.js → billing-UDKL53GI.js} +81 -81
- package/dist/cli/{chunk-6Y3DDRRN.js → chunk-2KHUFWTK.js} +3 -3
- package/dist/cli/{chunk-4VR5OTYP.js → chunk-37SXIFIU.js} +44 -44
- package/dist/cli/{chunk-QSWPDYOP.mjs → chunk-3KMGVOBM.mjs} +4 -4
- package/dist/cli/{chunk-7VJKHJY7.js → chunk-4DFWDA2Q.js} +14 -14
- package/dist/cli/{chunk-VSPNBBRT.js → chunk-4MF44THR.js} +66 -119
- package/dist/cli/{chunk-NLIO6QLP.mjs → chunk-4TUMKJZX.mjs} +2 -2
- package/dist/cli/{chunk-KKSMHTQO.mjs → chunk-52MHE7B4.mjs} +1 -1
- package/dist/cli/{chunk-AVW2LKRF.js → chunk-534YYHIF.js} +6 -6
- package/dist/cli/{chunk-QQGRQRCS.mjs → chunk-5477PEHL.mjs} +1 -1
- package/dist/cli/{chunk-YHHLLONU.js → chunk-5BQPEHEP.js} +14 -14
- package/dist/cli/{chunk-GVTHFDKN.js → chunk-5H22VJPE.js} +6 -6
- package/dist/cli/{chunk-LUIPBI6P.js → chunk-6SJPZV5E.js} +2 -2
- package/dist/cli/{chunk-NNUJQ5O2.mjs → chunk-7J5PLHII.mjs} +1 -1
- package/dist/cli/{chunk-I4GBCKBL.js → chunk-7JEN3FX4.js} +5 -5
- package/dist/cli/{chunk-VSLU4JZV.mjs → chunk-BONXEWYF.mjs} +3 -3
- package/dist/cli/{chunk-AM7HMWNA.js → chunk-BZY335JP.js} +5 -5
- package/dist/cli/{chunk-YP3G2CP6.mjs → chunk-C6WGMODR.mjs} +2 -2
- package/dist/cli/{chunk-V6BUWQBI.mjs → chunk-CFXERH7F.mjs} +1 -1
- package/dist/cli/{chunk-ORAWUIMN.js → chunk-DARD7TJ7.js} +53 -53
- package/dist/cli/{chunk-BHAP6FIZ.js → chunk-DLDWAPA5.js} +4 -4
- package/dist/cli/{chunk-FWGTBR3W.js → chunk-DNJJ3ULJ.js} +2 -2
- package/dist/cli/{chunk-Y5QYWFRI.js → chunk-DRDZBRXV.js} +7 -7
- package/dist/cli/{chunk-JO2MQ6XR.js → chunk-EJBW7Y6Z.js} +98 -98
- package/dist/cli/{chunk-2EDGA7OE.mjs → chunk-ENWJFUWK.mjs} +2 -2
- package/dist/cli/{chunk-6XJA23MZ.js → chunk-F5D623KO.js} +26 -26
- package/dist/cli/{chunk-HT675XNR.mjs → chunk-FKD3KL5Q.mjs} +1 -1
- package/dist/cli/{chunk-2BJILUE3.js → chunk-FNHUDCJE.js} +10 -10
- package/dist/cli/{chunk-XU57CH42.mjs → chunk-FQQAAR3Q.mjs} +1 -1
- package/dist/cli/{chunk-6T2LB43P.mjs → chunk-G65FJ5JX.mjs} +2 -2
- package/dist/cli/{chunk-UA4MXESL.js → chunk-GMYDVW7Z.js} +3 -3
- package/dist/cli/{chunk-74MOIEDC.js → chunk-GVKW3LCA.js} +11 -11
- package/dist/cli/{chunk-D5VTWJ7K.js → chunk-ISUUSSS4.js} +16 -16
- package/dist/cli/{chunk-KTAOVRXT.js → chunk-KMBF3N2Z.js} +16 -16
- package/dist/cli/{chunk-5TEXJM6F.mjs → chunk-LFSOBW7E.mjs} +11 -11
- package/dist/cli/{chunk-A5VR463F.mjs → chunk-MD524MRB.mjs} +1 -1
- package/dist/cli/{chunk-NPRSGUHB.mjs → chunk-NDNG3MAT.mjs} +5 -5
- package/dist/cli/{chunk-NWSKBWQM.mjs → chunk-OAQIVJFY.mjs} +2 -2
- package/dist/cli/{chunk-6VY2SJ6B.mjs → chunk-PDLRUZ42.mjs} +5 -5
- package/dist/cli/{chunk-M6YHS5V7.mjs → chunk-RDJTCW3I.mjs} +3 -3
- package/dist/cli/{chunk-3YHS4BLB.js → chunk-RI672VNY.js} +2 -2
- package/dist/cli/{chunk-TQTQ3YYA.mjs → chunk-RKE5YTHY.mjs} +5 -5
- package/dist/cli/{chunk-SHB2BNI6.mjs → chunk-SFMCKB57.mjs} +37 -90
- package/dist/cli/{chunk-BMCRTI6Z.mjs → chunk-UBKA33KY.mjs} +1 -22
- package/dist/cli/{chunk-6GI7BYSL.mjs → chunk-UV6XCHOZ.mjs} +1 -1
- package/dist/cli/{chunk-U5646OVV.mjs → chunk-VFCVH2LV.mjs} +5 -5
- package/dist/cli/{chunk-7QAX5VBT.js → chunk-VIM6I7RC.js} +41 -41
- package/dist/cli/{chunk-WSGXBROE.js → chunk-XDVT3GOM.js} +1 -22
- package/dist/cli/{chunk-KCISDDNK.mjs → chunk-XR6TN5DT.mjs} +6 -6
- package/dist/cli/{chunk-XXDY7K7F.js → chunk-XT25FX6T.js} +3 -3
- package/dist/cli/{chunk-ASGGZ6BI.mjs → chunk-XXY4E57R.mjs} +2 -2
- package/dist/cli/{chunk-YVQLXMJU.mjs → chunk-Y3LDRCMB.mjs} +3 -3
- package/dist/cli/{chunk-5ECMRQCL.js → chunk-ZUAKRBZB.js} +1 -1
- package/dist/cli/{chunk-DQQ6FDE6.mjs → chunk-ZZ7U4YJS.mjs} +1 -1
- package/dist/cli/{chunk-LYIJ3YUP.js → chunk-ZZLZCO6Y.js} +22 -22
- package/dist/cli/{csv-VJNR6FOZ.js → csv-3HIE4VCO.js} +13 -13
- package/dist/cli/{csv-T7HIELZ2.mjs → csv-PE35WGRO.mjs} +4 -4
- package/dist/cli/{db-OE2EXZXL.mjs → db-L7ZMMQKW.mjs} +8 -8
- package/dist/cli/{db-JF2I54V7.js → db-O2YRTTDO.js} +29 -29
- package/dist/cli/describe-EB336DAO.js +50 -0
- package/dist/cli/{describe-HXZZC3LC.mjs → describe-NI324Y2M.mjs} +20 -20
- package/dist/cli/describe-core.d.mts +2 -2
- package/dist/cli/describe-core.d.ts +2 -2
- package/dist/cli/{executions-4BXRIJFF.mjs → executions-T6KU2OKK.mjs} +8 -8
- package/dist/cli/{executions-UQ2H6MJZ.js → executions-VH4DKLFI.js} +10 -10
- package/dist/cli/{feedback-TXF5CQKA.mjs → feedback-SCSJC3FX.mjs} +5 -5
- package/dist/cli/{feedback-MKFN7DYY.js → feedback-ZLC6G5OK.js} +10 -10
- package/dist/cli/git-23T7TXAH.js +25 -0
- package/dist/cli/{git-MG3NAV65.mjs → git-I4LVQCID.mjs} +5 -5
- package/dist/cli/index.js +141 -141
- package/dist/cli/index.mjs +43 -43
- package/dist/cli/{legacy-noops-XSDY46XD.mjs → legacy-noops-44FQ2256.mjs} +6 -6
- package/dist/cli/{legacy-noops-R76I2OWW.js → legacy-noops-UPFWEHPG.js} +9 -9
- package/dist/cli/{monitors-FS3DIDM7.js → monitors-JN6IQGK6.js} +102 -102
- package/dist/cli/{monitors-NVT5YQKV.mjs → monitors-LMSASWXE.mjs} +9 -9
- package/dist/cli/{org-SBI6FC6D.js → org-F7O3ZU5O.js} +74 -74
- package/dist/cli/{org-XU7JONWS.mjs → org-QYLVEE7A.mjs} +8 -8
- package/dist/cli/{pages-AFB5DVBR.mjs → pages-5XE4LBZN.mjs} +8 -8
- package/dist/cli/{pages-2CZFJJV6.js → pages-KEL3Y4L4.js} +20 -20
- package/dist/cli/{play-EQEIT5S4.mjs → play-MWQG3G32.mjs} +21 -21
- package/dist/cli/{play-WZH72UGC.js → play-OURHH6KV.js} +338 -338
- package/dist/cli/{providers-3VXTATHF.mjs → providers-IQY7LUBB.mjs} +8 -8
- package/dist/cli/{providers-SHSVUQA3.js → providers-ZCBWPE3C.js} +11 -11
- package/dist/cli/{quickstart-45YS4SOD.mjs → quickstart-L45NGRMI.mjs} +4 -4
- package/dist/cli/{quickstart-KPVPK3VL.js → quickstart-O53OXTN6.js} +6 -6
- package/dist/cli/{run-XEHRZT57.mjs → run-VCSIWLEU.mjs} +25 -25
- package/dist/cli/{run-U5RWBX5J.js → run-W745Z3Z5.js} +43 -43
- package/dist/cli/run-identity-G257GQED.js +25 -0
- package/dist/cli/{run-identity-MNRZS2N3.mjs → run-identity-MRNCEGV2.mjs} +3 -9
- package/dist/cli/search-R6K7ZAMU.js +33 -0
- package/dist/cli/{search-V7XIWBQ2.mjs → search-THELKULN.mjs} +10 -10
- package/dist/cli/{search-output-3KAJUV2A.mjs → search-output-N7MJLYYD.mjs} +4 -4
- package/dist/cli/{search-output-ZNIICV5Z.js → search-output-SJW7LEOM.js} +6 -6
- package/dist/cli/{secrets-SCSG3QXJ.mjs → secrets-44ST3FJH.mjs} +8 -8
- package/dist/cli/{secrets-53TUV6JK.js → secrets-GDXPX3KB.js} +17 -17
- package/dist/cli/sessions-2V5ZFOWL.js +18 -0
- package/dist/cli/{sessions-MCTHLLK3.mjs → sessions-LYCBI77S.mjs} +6 -6
- package/dist/cli/{settings-ZNOXOIX2.js → settings-3WOFF642.js} +88 -88
- package/dist/cli/{settings-Q2HKACSE.mjs → settings-GXPDMJFG.mjs} +15 -15
- package/dist/cli/{setup-GO4EULLY.js → setup-YHLMBJP4.js} +50 -50
- package/dist/cli/{setup-WKM35SZJ.mjs → setup-YM6JEB64.mjs} +10 -10
- package/dist/cli/{skills-DZZLW6OV.mjs → skills-WX4XWEAT.mjs} +8 -8
- package/dist/cli/skills-XNGADDO6.js +37 -0
- package/dist/cli/{tools-EKVSK6X4.mjs → tools-K24VSQ7S.mjs} +19 -19
- package/dist/cli/tools-ROUMCB7B.js +57 -0
- package/dist/cli/{update-FPHYVHSC.mjs → update-PSSCF5AT.mjs} +10 -10
- package/dist/cli/update-RXURGIRW.js +35 -0
- package/dist/cli/{workflow-5YAZK5EI.mjs → workflow-DGOPYATO.mjs} +9 -9
- package/dist/cli/{workflow-27H3NCNY.js → workflow-FPVI3NIA.js} +19 -19
- package/dist/{client-DhEOE19l.d.ts → client-B-2Am2y6.d.ts} +1 -1
- package/dist/{client-lJmz2QwF.d.mts → client-DA71-2_u.d.mts} +1 -1
- package/dist/{client-core-BGiSMrBD.d.mts → client-core-BXbds1cb.d.mts} +0 -19
- package/dist/{client-core-DYCGaRBZ.d.ts → client-core-C0IzDKMJ.d.ts} +0 -19
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +19 -19
- package/dist/index.mjs +8 -8
- package/dist/install-integrity.json +114 -122
- package/dist/plays/play-description.d.mts +1 -1
- package/dist/plays/play-description.d.ts +1 -1
- package/dist/plays/run-request.d.mts +1 -1
- package/dist/plays/run-request.d.ts +1 -1
- package/dist/plays/run-request.js +3 -3
- package/dist/plays/run-request.mjs +2 -2
- package/dist/release.d.mts +1 -1
- package/dist/release.d.ts +1 -1
- package/dist/release.js +2 -2
- package/dist/release.mjs +1 -1
- package/dist/server.d.mts +2 -2
- package/dist/server.d.ts +2 -2
- package/dist/server.js +7 -7
- package/dist/server.mjs +5 -5
- package/package.json +1 -1
- package/dist/bundling-sources/shared_libs/play-runtime/runtime-app-transport.ts +0 -374
- package/dist/bundling-sources/shared_libs/play-runtime/runtime-gateway-egress.ts +0 -55
- package/dist/bundling-sources/shared_libs/play-runtime/runtime-gateway-profile.ts +0 -329
- package/dist/bundling-sources/shared_libs/play-runtime/runtime-gateway-sandbox-credential.ts +0 -338
- package/dist/bundling-sources/shared_libs/play-runtime/runtime-gateway-staging.ts +0 -417
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/runtime-gateway-attempt.ts +0 -81
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/runtime-gateway-staging-journal.ts +0 -708
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/runtime-gateway-staging-teardown.ts +0 -160
- package/dist/cli/auth-TH4JV6BH.js +0 -43
- package/dist/cli/describe-4FLGKRCA.js +0 -50
- package/dist/cli/git-B2QO3SLK.js +0 -25
- package/dist/cli/run-identity-HFYABUJS.js +0 -31
- package/dist/cli/search-Z2V43OPM.js +0 -33
- package/dist/cli/sessions-BOJCVLR7.js +0 -18
- package/dist/cli/skills-B4BAH2B2.js +0 -37
- package/dist/cli/tools-Z5NCKIAB.js +0 -57
- package/dist/cli/update-XCL7F5T6.js +0 -35
|
@@ -1,708 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from 'node:crypto';
|
|
2
|
-
import {
|
|
3
|
-
assertRuntimeGatewayStagingScope,
|
|
4
|
-
isRuntimeGatewayStagingSha256,
|
|
5
|
-
isRuntimeGatewayStagingUploadId,
|
|
6
|
-
RUNTIME_GATEWAY_STAGING_ERROR_CODES,
|
|
7
|
-
RUNTIME_GATEWAY_STAGING_GRANT_MAX_TTL_SECONDS,
|
|
8
|
-
RUNTIME_GATEWAY_STAGING_RETIRE_GRACE_SECONDS,
|
|
9
|
-
runtimeGatewayStagingObjectKey,
|
|
10
|
-
type RuntimeGatewayStagingDirection,
|
|
11
|
-
type RuntimeGatewayStagingScope,
|
|
12
|
-
} from '../runtime-gateway-staging';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Custody journal for app-hosted Runtime gateway staged bodies.
|
|
16
|
-
*
|
|
17
|
-
* Schema contract: one new table, two partial indexes, and one additive
|
|
18
|
-
* nullable column, created with `IF NOT EXISTS` by the deployment migration
|
|
19
|
-
* on every run (the common
|
|
20
|
-
* migration path, the shared online expansion, and the cold bootstrap). It is
|
|
21
|
-
* not a scheduler-shape requirement and not behind the schema marker:
|
|
22
|
-
* readiness, admission, and every other runtime path never read it. When it
|
|
23
|
-
* is absent, only a staging operation of a pinned run fails, with
|
|
24
|
-
* `runtime_gateway_staging_journal_missing`.
|
|
25
|
-
*
|
|
26
|
-
* Row lifecycle: `reserved` (custody persisted before any upload authority is
|
|
27
|
-
* minted) -> `accepting` -> `accepted` | `rejected` -> `deleted`. Every row,
|
|
28
|
-
* whatever its state, is retired only after `retire_after` (grant expiry plus
|
|
29
|
-
* the gateway invocation ceiling), and is marked deleted only after the
|
|
30
|
-
* object store confirms the key absent.
|
|
31
|
-
*
|
|
32
|
-
* `bucket_fingerprint` records which bucket the reserving process addressed
|
|
33
|
-
* (`getR2BucketFingerprint`: SHA-256 of bucket name and endpoint host).
|
|
34
|
-
* Teardown refuses to delete or mark deleted a row whose fingerprint differs
|
|
35
|
-
* from its own store's, because an absence readback from another bucket
|
|
36
|
-
* proves nothing. The column is part of the table's creation: the table has
|
|
37
|
-
* never shipped without it, so no migration ever alters a current table.
|
|
38
|
-
*/
|
|
39
|
-
|
|
40
|
-
type QueryClient = {
|
|
41
|
-
query<Row extends Record<string, unknown> = Record<string, unknown>>(
|
|
42
|
-
sql: string,
|
|
43
|
-
params?: unknown[],
|
|
44
|
-
): Promise<{ rows: Row[] }>;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
export const RUNTIME_GATEWAY_STAGING_TABLE = 'runtime_gateway_staged_bodies';
|
|
48
|
-
|
|
49
|
-
const quote = (value: string) => `"${value.replace(/"/g, '""')}"`;
|
|
50
|
-
const relation = (schema: string) =>
|
|
51
|
-
`${quote(schema)}.${quote(RUNTIME_GATEWAY_STAGING_TABLE)}`;
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Index names are schema-local, so they carry no schema prefix: a prefixed
|
|
55
|
-
* name longer than Postgres' 63-byte identifier limit is truncated, and for a
|
|
56
|
-
* long schema name both indexes truncated to the same name, so `IF NOT
|
|
57
|
-
* EXISTS` silently skipped the second one.
|
|
58
|
-
*/
|
|
59
|
-
export const RUNTIME_GATEWAY_STAGING_RETIRE_INDEX =
|
|
60
|
-
'runtime_gateway_staged_bodies_retire_idx';
|
|
61
|
-
export const RUNTIME_GATEWAY_STAGING_DELETED_INDEX =
|
|
62
|
-
'runtime_gateway_staged_bodies_deleted_idx';
|
|
63
|
-
|
|
64
|
-
/** Deployment-owned additive DDL. Request paths never run it. */
|
|
65
|
-
export function runtimeGatewayStagingSchemaStatements(
|
|
66
|
-
schema: string,
|
|
67
|
-
): string[] {
|
|
68
|
-
return [
|
|
69
|
-
`CREATE TABLE IF NOT EXISTS ${relation(schema)} (
|
|
70
|
-
upload_id text PRIMARY KEY,
|
|
71
|
-
org_id text NOT NULL,
|
|
72
|
-
run_id text NOT NULL,
|
|
73
|
-
attempt integer NOT NULL CHECK (attempt >= 0),
|
|
74
|
-
direction text NOT NULL CHECK (direction IN ('request', 'response')),
|
|
75
|
-
object_key text NOT NULL UNIQUE,
|
|
76
|
-
sha256 text NOT NULL CHECK (sha256 ~ '^[0-9a-f]{64}$'),
|
|
77
|
-
byte_length integer NOT NULL CHECK (byte_length > 0),
|
|
78
|
-
state text NOT NULL CHECK (state IN ('reserved', 'accepting', 'accepted', 'rejected', 'deleted')),
|
|
79
|
-
bucket_fingerprint text,
|
|
80
|
-
grant_expires_at timestamptz NOT NULL,
|
|
81
|
-
retire_after timestamptz NOT NULL,
|
|
82
|
-
retire_claim_token text,
|
|
83
|
-
retire_claimed_at timestamptz,
|
|
84
|
-
consumed_at timestamptz,
|
|
85
|
-
deleted_at timestamptz,
|
|
86
|
-
created_at timestamptz NOT NULL DEFAULT now(),
|
|
87
|
-
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
88
|
-
CHECK (retire_after > grant_expires_at),
|
|
89
|
-
CHECK ((retire_claim_token IS NULL) = (retire_claimed_at IS NULL)),
|
|
90
|
-
CHECK ((state = 'deleted') = (deleted_at IS NOT NULL))
|
|
91
|
-
)`,
|
|
92
|
-
`CREATE INDEX IF NOT EXISTS ${quote(RUNTIME_GATEWAY_STAGING_RETIRE_INDEX)}
|
|
93
|
-
ON ${relation(schema)} (retire_after) WHERE state <> 'deleted'`,
|
|
94
|
-
`CREATE INDEX IF NOT EXISTS ${quote(RUNTIME_GATEWAY_STAGING_DELETED_INDEX)}
|
|
95
|
-
ON ${relation(schema)} (deleted_at) WHERE state = 'deleted'`,
|
|
96
|
-
];
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/** Install the journal. Idempotent; never touches an existing table. */
|
|
100
|
-
export async function ensureRuntimeGatewayStagingSchema(
|
|
101
|
-
client: QueryClient,
|
|
102
|
-
options: { schema: string },
|
|
103
|
-
): Promise<void> {
|
|
104
|
-
for (const statement of runtimeGatewayStagingSchemaStatements(
|
|
105
|
-
options.schema,
|
|
106
|
-
)) {
|
|
107
|
-
await client.query(statement);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/** Upper bound on schemas probed by one discovery statement. */
|
|
112
|
-
export const RUNTIME_GATEWAY_STAGING_DISCOVERY_MAX_PAGE_SIZE = 512;
|
|
113
|
-
|
|
114
|
-
export type RuntimeGatewayStagingActionableSchema = Readonly<{
|
|
115
|
-
schema: string;
|
|
116
|
-
/** When this schema's oldest actionable row became due. */
|
|
117
|
-
dueAt: Date;
|
|
118
|
-
}>;
|
|
119
|
-
|
|
120
|
-
export type RuntimeGatewayStagingDiscoveryPage = Readonly<{
|
|
121
|
-
/** Schemas in this page with a due retirement or a prunable deleted row. */
|
|
122
|
-
actionable: readonly RuntimeGatewayStagingActionableSchema[];
|
|
123
|
-
/** Keyset cursor for the next page; null after the last page. */
|
|
124
|
-
nextAfterSchema: string | null;
|
|
125
|
-
}>;
|
|
126
|
-
|
|
127
|
-
/** Due-time probe for one schema: one ordered seek per partial index. */
|
|
128
|
-
function stagingDueProbeSql(
|
|
129
|
-
table: string,
|
|
130
|
-
schemaParam: string,
|
|
131
|
-
retentionParam: string,
|
|
132
|
-
) {
|
|
133
|
-
return `(SELECT ${schemaParam}::text AS schema, LEAST(
|
|
134
|
-
(SELECT retire_after FROM ${table}
|
|
135
|
-
WHERE state <> 'deleted' AND retire_after < now()
|
|
136
|
-
ORDER BY retire_after LIMIT 1),
|
|
137
|
-
(SELECT deleted_at + make_interval(secs => ${retentionParam}::double precision)
|
|
138
|
-
FROM ${table}
|
|
139
|
-
WHERE state = 'deleted'
|
|
140
|
-
AND deleted_at < now() - make_interval(secs => ${retentionParam}::double precision)
|
|
141
|
-
ORDER BY deleted_at LIMIT 1)
|
|
142
|
-
) AS due_at)`;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
function decodeDueRows(
|
|
146
|
-
rows: Array<{ schema: string; due_at: Date | string | null }>,
|
|
147
|
-
): RuntimeGatewayStagingActionableSchema[] {
|
|
148
|
-
return rows
|
|
149
|
-
.filter((row) => row.due_at != null)
|
|
150
|
-
.map((row) =>
|
|
151
|
-
Object.freeze({ schema: row.schema, dueAt: new Date(row.due_at!) }),
|
|
152
|
-
);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
async function probeStagingSchemas(
|
|
156
|
-
client: QueryClient,
|
|
157
|
-
schemas: readonly string[],
|
|
158
|
-
prunableOlderThanSeconds: number,
|
|
159
|
-
): Promise<RuntimeGatewayStagingActionableSchema[]> {
|
|
160
|
-
const probes = schemas.map((schema, index) =>
|
|
161
|
-
stagingDueProbeSql(relation(schema), `$${index + 2}`, '$1'),
|
|
162
|
-
);
|
|
163
|
-
const result = await client.query<{
|
|
164
|
-
schema: string;
|
|
165
|
-
due_at: Date | string | null;
|
|
166
|
-
}>(probes.join('\n UNION ALL \n'), [prunableOlderThanSeconds, ...schemas]);
|
|
167
|
-
return decodeDueRows(result.rows);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* One keyset page of the scheduler schemas on this database that carry the
|
|
172
|
-
* journal (byte order, strictly after `afterSchema`), narrowed to those with
|
|
173
|
-
* actionable work: a non-deleted row past `retire_after`, or a deleted row
|
|
174
|
-
* past its retention. Every schema in the page costs one ordered seek on each
|
|
175
|
-
* partial index, all in one statement, so a caller pages the whole catalog
|
|
176
|
-
* without ever planning a claim on an idle schema. A schema dropped while the
|
|
177
|
-
* page was probed is skipped, not fatal.
|
|
178
|
-
*/
|
|
179
|
-
export async function listActionableRuntimeGatewayStagingSchemas(
|
|
180
|
-
client: QueryClient,
|
|
181
|
-
input: {
|
|
182
|
-
afterSchema: string | null;
|
|
183
|
-
pageSize: number;
|
|
184
|
-
prunableOlderThanSeconds: number;
|
|
185
|
-
},
|
|
186
|
-
): Promise<RuntimeGatewayStagingDiscoveryPage> {
|
|
187
|
-
if (
|
|
188
|
-
!Number.isSafeInteger(input.pageSize) ||
|
|
189
|
-
input.pageSize < 1 ||
|
|
190
|
-
input.pageSize > RUNTIME_GATEWAY_STAGING_DISCOVERY_MAX_PAGE_SIZE
|
|
191
|
-
) {
|
|
192
|
-
throw new Error('Runtime gateway staging discovery page size is invalid.');
|
|
193
|
-
}
|
|
194
|
-
if (
|
|
195
|
-
!Number.isFinite(input.prunableOlderThanSeconds) ||
|
|
196
|
-
input.prunableOlderThanSeconds < 0
|
|
197
|
-
) {
|
|
198
|
-
throw new Error('Runtime gateway staging retention is invalid.');
|
|
199
|
-
}
|
|
200
|
-
const catalog = await client.query<{ schema: string }>(
|
|
201
|
-
`SELECT namespace.nspname::text AS schema
|
|
202
|
-
FROM pg_catalog.pg_class AS relation
|
|
203
|
-
JOIN pg_catalog.pg_namespace AS namespace
|
|
204
|
-
ON namespace.oid = relation.relnamespace
|
|
205
|
-
WHERE relation.relname = $1 AND relation.relkind IN ('r', 'p')
|
|
206
|
-
AND ($2::text IS NULL
|
|
207
|
-
OR namespace.nspname::text COLLATE "C" > $2::text COLLATE "C")
|
|
208
|
-
ORDER BY namespace.nspname::text COLLATE "C"
|
|
209
|
-
LIMIT $3`,
|
|
210
|
-
[RUNTIME_GATEWAY_STAGING_TABLE, input.afterSchema, input.pageSize],
|
|
211
|
-
);
|
|
212
|
-
const schemas = catalog.rows.map((row) => row.schema);
|
|
213
|
-
const nextAfterSchema =
|
|
214
|
-
schemas.length === input.pageSize ? schemas[schemas.length - 1]! : null;
|
|
215
|
-
if (schemas.length === 0) return { actionable: [], nextAfterSchema };
|
|
216
|
-
let actionable: RuntimeGatewayStagingActionableSchema[];
|
|
217
|
-
try {
|
|
218
|
-
actionable = await probeStagingSchemas(
|
|
219
|
-
client,
|
|
220
|
-
schemas,
|
|
221
|
-
input.prunableOlderThanSeconds,
|
|
222
|
-
);
|
|
223
|
-
} catch (error) {
|
|
224
|
-
if (!isUndefinedTable(error)) throw error;
|
|
225
|
-
// A Preview estate was dropped between the catalog read and the probe.
|
|
226
|
-
// Probe the page one schema at a time and skip the ones that are gone.
|
|
227
|
-
actionable = [];
|
|
228
|
-
for (const schema of schemas) {
|
|
229
|
-
try {
|
|
230
|
-
actionable.push(
|
|
231
|
-
...(await probeStagingSchemas(
|
|
232
|
-
client,
|
|
233
|
-
[schema],
|
|
234
|
-
input.prunableOlderThanSeconds,
|
|
235
|
-
)),
|
|
236
|
-
);
|
|
237
|
-
} catch (probeError) {
|
|
238
|
-
if (!isUndefinedTable(probeError)) throw probeError;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
return { actionable, nextAfterSchema };
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/** The pinned run cannot stage: the deployment has not installed the journal. */
|
|
246
|
-
export class RuntimeGatewayStagingJournalMissingError extends Error {
|
|
247
|
-
readonly code = RUNTIME_GATEWAY_STAGING_ERROR_CODES.journalMissing;
|
|
248
|
-
constructor(schema: string) {
|
|
249
|
-
super(
|
|
250
|
-
`${RUNTIME_GATEWAY_STAGING_ERROR_CODES.journalMissing}: scheduler schema ${schema} has no ${RUNTIME_GATEWAY_STAGING_TABLE} table; run the runtime scheduler migration.`,
|
|
251
|
-
);
|
|
252
|
-
this.name = 'RuntimeGatewayStagingJournalMissingError';
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
function isUndefinedTable(error: unknown): boolean {
|
|
257
|
-
return (
|
|
258
|
-
!!error &&
|
|
259
|
-
typeof error === 'object' &&
|
|
260
|
-
(error as { code?: unknown }).code === '42P01'
|
|
261
|
-
);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
export type RuntimeGatewayStagedBody = Readonly<{
|
|
265
|
-
uploadId: string;
|
|
266
|
-
scope: RuntimeGatewayStagingScope;
|
|
267
|
-
direction: RuntimeGatewayStagingDirection;
|
|
268
|
-
objectKey: string;
|
|
269
|
-
sha256: string;
|
|
270
|
-
byteLength: number;
|
|
271
|
-
state: 'reserved' | 'accepting' | 'accepted' | 'rejected' | 'deleted';
|
|
272
|
-
grantExpiresAt: Date;
|
|
273
|
-
retireAfter: Date;
|
|
274
|
-
retireClaimToken: string | null;
|
|
275
|
-
/** Null only for rows reserved before the column existed. */
|
|
276
|
-
bucketFingerprint: string | null;
|
|
277
|
-
}>;
|
|
278
|
-
|
|
279
|
-
type Row = {
|
|
280
|
-
upload_id: string;
|
|
281
|
-
org_id: string;
|
|
282
|
-
run_id: string;
|
|
283
|
-
attempt: number;
|
|
284
|
-
direction: RuntimeGatewayStagingDirection;
|
|
285
|
-
object_key: string;
|
|
286
|
-
sha256: string;
|
|
287
|
-
byte_length: number | string;
|
|
288
|
-
state: RuntimeGatewayStagedBody['state'];
|
|
289
|
-
grant_expires_at: Date | string;
|
|
290
|
-
retire_after: Date | string;
|
|
291
|
-
retire_claim_token: string | null;
|
|
292
|
-
bucket_fingerprint?: string | null;
|
|
293
|
-
};
|
|
294
|
-
|
|
295
|
-
function decode(row: Row): RuntimeGatewayStagedBody {
|
|
296
|
-
return Object.freeze({
|
|
297
|
-
uploadId: row.upload_id,
|
|
298
|
-
scope: Object.freeze({
|
|
299
|
-
orgId: row.org_id,
|
|
300
|
-
runId: row.run_id,
|
|
301
|
-
attempt: Number(row.attempt),
|
|
302
|
-
}),
|
|
303
|
-
direction: row.direction,
|
|
304
|
-
objectKey: row.object_key,
|
|
305
|
-
sha256: row.sha256,
|
|
306
|
-
byteLength: Number(row.byte_length),
|
|
307
|
-
state: row.state,
|
|
308
|
-
grantExpiresAt: new Date(row.grant_expires_at),
|
|
309
|
-
retireAfter: new Date(row.retire_after),
|
|
310
|
-
retireClaimToken: row.retire_claim_token,
|
|
311
|
-
bucketFingerprint: row.bucket_fingerprint ?? null,
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
const SCOPE_WHERE = `upload_id = $1 AND org_id = $2 AND run_id = $3 AND attempt = $4`;
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
* Row operations only: no method performs object-storage I/O. Callers
|
|
319
|
-
* authenticate the scope (executor token claims) before any call.
|
|
320
|
-
*/
|
|
321
|
-
export class RuntimeGatewayStagingJournal {
|
|
322
|
-
private readonly table: string;
|
|
323
|
-
|
|
324
|
-
constructor(
|
|
325
|
-
private readonly client: QueryClient,
|
|
326
|
-
private readonly schema: string,
|
|
327
|
-
) {
|
|
328
|
-
this.table = relation(schema);
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
/**
|
|
332
|
-
* True when this schema's journal is current (table and every column this
|
|
333
|
-
* build writes). One catalog read; request paths call it before handing a
|
|
334
|
-
* staging-capable request to the gateway, never DDL.
|
|
335
|
-
*/
|
|
336
|
-
async isInstalled(): Promise<boolean> {
|
|
337
|
-
const result = await this.client.query<{ installed: boolean }>(
|
|
338
|
-
`SELECT EXISTS (
|
|
339
|
-
SELECT 1 FROM pg_catalog.pg_attribute AS attribute
|
|
340
|
-
JOIN pg_catalog.pg_class AS relation ON relation.oid = attribute.attrelid
|
|
341
|
-
JOIN pg_catalog.pg_namespace AS namespace
|
|
342
|
-
ON namespace.oid = relation.relnamespace
|
|
343
|
-
WHERE namespace.nspname = $1 AND relation.relname = $2
|
|
344
|
-
AND attribute.attname = 'bucket_fingerprint'
|
|
345
|
-
AND NOT attribute.attisdropped
|
|
346
|
-
) AS installed`,
|
|
347
|
-
[this.schema, RUNTIME_GATEWAY_STAGING_TABLE],
|
|
348
|
-
);
|
|
349
|
-
return result.rows[0]?.installed === true;
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
private async run<T extends Record<string, unknown>>(
|
|
353
|
-
sql: string,
|
|
354
|
-
params: unknown[],
|
|
355
|
-
): Promise<{ rows: T[] }> {
|
|
356
|
-
try {
|
|
357
|
-
return await this.client.query<T>(sql, params);
|
|
358
|
-
} catch (error) {
|
|
359
|
-
if (isUndefinedTable(error)) {
|
|
360
|
-
throw new RuntimeGatewayStagingJournalMissingError(this.schema);
|
|
361
|
-
}
|
|
362
|
-
throw error;
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
/**
|
|
367
|
-
* Persist custody, including the grant's exact expiry, before any upload
|
|
368
|
-
* authority exists. A caller mints its presigned URL only after this
|
|
369
|
-
* returns, with a lifetime no later than `grantExpiresAt`.
|
|
370
|
-
*/
|
|
371
|
-
async reserve(input: {
|
|
372
|
-
scope: RuntimeGatewayStagingScope;
|
|
373
|
-
direction: RuntimeGatewayStagingDirection;
|
|
374
|
-
sha256: string;
|
|
375
|
-
byteLength: number;
|
|
376
|
-
maxBytes: number;
|
|
377
|
-
grantTtlSeconds: number;
|
|
378
|
-
/** The store's `getR2BucketFingerprint`, persisted with custody. */
|
|
379
|
-
bucketFingerprint: string;
|
|
380
|
-
}): Promise<RuntimeGatewayStagedBody> {
|
|
381
|
-
assertRuntimeGatewayStagingScope(input.scope);
|
|
382
|
-
if (!isRuntimeGatewayStagingSha256(input.sha256)) {
|
|
383
|
-
throw new Error('Runtime gateway staging digest is invalid.');
|
|
384
|
-
}
|
|
385
|
-
if (
|
|
386
|
-
!Number.isSafeInteger(input.byteLength) ||
|
|
387
|
-
input.byteLength < 1 ||
|
|
388
|
-
input.byteLength > input.maxBytes
|
|
389
|
-
) {
|
|
390
|
-
throw new Error('Runtime gateway staging size is outside its bound.');
|
|
391
|
-
}
|
|
392
|
-
if (
|
|
393
|
-
!Number.isSafeInteger(input.grantTtlSeconds) ||
|
|
394
|
-
input.grantTtlSeconds < 1 ||
|
|
395
|
-
input.grantTtlSeconds > RUNTIME_GATEWAY_STAGING_GRANT_MAX_TTL_SECONDS
|
|
396
|
-
) {
|
|
397
|
-
throw new Error('Runtime gateway staging grant lifetime is invalid.');
|
|
398
|
-
}
|
|
399
|
-
if (!/^[0-9a-f]{64}$/.test(input.bucketFingerprint)) {
|
|
400
|
-
throw new Error('Runtime gateway staging bucket fingerprint is invalid.');
|
|
401
|
-
}
|
|
402
|
-
const uploadId = randomUUID();
|
|
403
|
-
const objectKey = runtimeGatewayStagingObjectKey(
|
|
404
|
-
input.scope,
|
|
405
|
-
input.direction,
|
|
406
|
-
uploadId,
|
|
407
|
-
);
|
|
408
|
-
const result = await this.run<Row>(
|
|
409
|
-
`INSERT INTO ${this.table}
|
|
410
|
-
(upload_id, org_id, run_id, attempt, direction, object_key, sha256,
|
|
411
|
-
byte_length, state, grant_expires_at, retire_after,
|
|
412
|
-
bucket_fingerprint)
|
|
413
|
-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 'reserved',
|
|
414
|
-
now() + make_interval(secs => $9::double precision),
|
|
415
|
-
now() + make_interval(secs => $10::double precision), $11)
|
|
416
|
-
RETURNING *`,
|
|
417
|
-
[
|
|
418
|
-
uploadId,
|
|
419
|
-
input.scope.orgId,
|
|
420
|
-
input.scope.runId,
|
|
421
|
-
input.scope.attempt,
|
|
422
|
-
input.direction,
|
|
423
|
-
objectKey,
|
|
424
|
-
input.sha256,
|
|
425
|
-
input.byteLength,
|
|
426
|
-
input.grantTtlSeconds,
|
|
427
|
-
input.grantTtlSeconds + RUNTIME_GATEWAY_STAGING_RETIRE_GRACE_SECONDS,
|
|
428
|
-
input.bucketFingerprint,
|
|
429
|
-
],
|
|
430
|
-
);
|
|
431
|
-
return decode(result.rows[0]!);
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
/**
|
|
435
|
-
* Take the one consumption of a reserved request upload in this exact run
|
|
436
|
-
* attempt while its grant is live. Null when unknown, out of scope,
|
|
437
|
-
* already consumed, or expired (retirement may already be deleting it).
|
|
438
|
-
*/
|
|
439
|
-
async claimAccept(input: {
|
|
440
|
-
scope: RuntimeGatewayStagingScope;
|
|
441
|
-
uploadId: string;
|
|
442
|
-
}): Promise<RuntimeGatewayStagedBody | null> {
|
|
443
|
-
if (!isRuntimeGatewayStagingUploadId(input.uploadId)) return null;
|
|
444
|
-
const result = await this.run<Row>(
|
|
445
|
-
`UPDATE ${this.table} SET state = 'accepting', updated_at = now()
|
|
446
|
-
WHERE ${SCOPE_WHERE} AND direction = 'request' AND state = 'reserved'
|
|
447
|
-
AND grant_expires_at > now()
|
|
448
|
-
RETURNING *`,
|
|
449
|
-
[
|
|
450
|
-
input.uploadId,
|
|
451
|
-
input.scope.orgId,
|
|
452
|
-
input.scope.runId,
|
|
453
|
-
input.scope.attempt,
|
|
454
|
-
],
|
|
455
|
-
);
|
|
456
|
-
return result.rows[0] ? decode(result.rows[0]) : null;
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
/** Verified bytes were handed to the gateway, or a response was stored. */
|
|
460
|
-
async markAccepted(body: RuntimeGatewayStagedBody): Promise<boolean> {
|
|
461
|
-
const result = await this.run<Row>(
|
|
462
|
-
`UPDATE ${this.table}
|
|
463
|
-
SET state = 'accepted', consumed_at = now(), updated_at = now()
|
|
464
|
-
WHERE ${SCOPE_WHERE} AND state IN ('reserved', 'accepting')
|
|
465
|
-
RETURNING upload_id`,
|
|
466
|
-
[body.uploadId, body.scope.orgId, body.scope.runId, body.scope.attempt],
|
|
467
|
-
);
|
|
468
|
-
return result.rows.length === 1;
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
/** Stored bytes failed their integrity check; never consumable again. */
|
|
472
|
-
async markRejected(body: RuntimeGatewayStagedBody): Promise<boolean> {
|
|
473
|
-
const result = await this.run<Row>(
|
|
474
|
-
`UPDATE ${this.table} SET state = 'rejected', updated_at = now()
|
|
475
|
-
WHERE ${SCOPE_WHERE} AND state = 'accepting'
|
|
476
|
-
RETURNING upload_id`,
|
|
477
|
-
[body.uploadId, body.scope.orgId, body.scope.runId, body.scope.attempt],
|
|
478
|
-
);
|
|
479
|
-
return result.rows.length === 1;
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
/**
|
|
483
|
-
* An ambiguous read: hand the upload back so nothing is lost. The client
|
|
484
|
-
* retries with a fresh upload; retirement covers this row either way.
|
|
485
|
-
*/
|
|
486
|
-
async releaseAccept(body: RuntimeGatewayStagedBody): Promise<boolean> {
|
|
487
|
-
const result = await this.run<Row>(
|
|
488
|
-
`UPDATE ${this.table} SET state = 'reserved', updated_at = now()
|
|
489
|
-
WHERE ${SCOPE_WHERE} AND state = 'accepting'
|
|
490
|
-
RETURNING upload_id`,
|
|
491
|
-
[body.uploadId, body.scope.orgId, body.scope.runId, body.scope.attempt],
|
|
492
|
-
);
|
|
493
|
-
return result.rows.length === 1;
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
/**
|
|
497
|
-
* Idle probe for `claimRetirements`: true when a non-deleted row is past
|
|
498
|
-
* `retire_after`. A superset of the claim's predicates, served by the same
|
|
499
|
-
* partial index as an ordered seek (a scalar subquery, never `EXISTS`), so
|
|
500
|
-
* an idle pass never plans the claim statement.
|
|
501
|
-
*/
|
|
502
|
-
async hasDueRetirement(): Promise<boolean> {
|
|
503
|
-
const result = await this.run<{ due: Date | string | null }>(
|
|
504
|
-
`SELECT (
|
|
505
|
-
SELECT retire_after FROM ${this.table}
|
|
506
|
-
WHERE state <> 'deleted' AND retire_after < now()
|
|
507
|
-
ORDER BY retire_after
|
|
508
|
-
LIMIT 1
|
|
509
|
-
) AS due`,
|
|
510
|
-
[],
|
|
511
|
-
);
|
|
512
|
-
return result.rows[0]?.due != null;
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
/** Idle probe for `pruneDeleted`, on the deleted-rows partial index. */
|
|
516
|
-
async hasPrunableDeleted(olderThanSeconds: number): Promise<boolean> {
|
|
517
|
-
const result = await this.run<{ due: Date | string | null }>(
|
|
518
|
-
`SELECT (
|
|
519
|
-
SELECT deleted_at FROM ${this.table}
|
|
520
|
-
WHERE state = 'deleted'
|
|
521
|
-
AND deleted_at < now() - make_interval(secs => $1::double precision)
|
|
522
|
-
ORDER BY deleted_at
|
|
523
|
-
LIMIT 1
|
|
524
|
-
) AS due`,
|
|
525
|
-
[olderThanSeconds],
|
|
526
|
-
);
|
|
527
|
-
return result.rows[0]?.due != null;
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
/**
|
|
531
|
-
* Claim actionable retirements only: rows past `retire_after` that are not
|
|
532
|
-
* deleted and not held by a live claim. Bounded and skip-locked, so
|
|
533
|
-
* concurrent sweepers split the work.
|
|
534
|
-
*/
|
|
535
|
-
async claimRetirements(limit: number): Promise<RuntimeGatewayStagedBody[]> {
|
|
536
|
-
if (!Number.isSafeInteger(limit) || limit < 1 || limit > 64) {
|
|
537
|
-
throw new Error('Runtime gateway staging retirement limit is invalid.');
|
|
538
|
-
}
|
|
539
|
-
const result = await this.run<Row>(
|
|
540
|
-
`WITH due AS (
|
|
541
|
-
SELECT upload_id FROM ${this.table}
|
|
542
|
-
WHERE state <> 'deleted' AND retire_after < now()
|
|
543
|
-
AND (retire_claim_token IS NULL
|
|
544
|
-
OR retire_claimed_at < now() - interval '5 minutes')
|
|
545
|
-
ORDER BY retire_after
|
|
546
|
-
LIMIT $1
|
|
547
|
-
FOR UPDATE SKIP LOCKED
|
|
548
|
-
)
|
|
549
|
-
UPDATE ${this.table} AS staged
|
|
550
|
-
SET retire_claim_token = $2, retire_claimed_at = now(), updated_at = now()
|
|
551
|
-
FROM due WHERE staged.upload_id = due.upload_id
|
|
552
|
-
RETURNING staged.*`,
|
|
553
|
-
[limit, randomUUID()],
|
|
554
|
-
);
|
|
555
|
-
return result.rows.map(decode);
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
/** Call only after the object store confirmed this exact key absent. */
|
|
559
|
-
async confirmDeleted(body: RuntimeGatewayStagedBody): Promise<boolean> {
|
|
560
|
-
if (!body.retireClaimToken) return false;
|
|
561
|
-
const result = await this.run<Row>(
|
|
562
|
-
`UPDATE ${this.table}
|
|
563
|
-
SET state = 'deleted', deleted_at = now(), retire_claim_token = NULL,
|
|
564
|
-
retire_claimed_at = NULL, updated_at = now()
|
|
565
|
-
WHERE upload_id = $1 AND retire_claim_token = $2 AND state <> 'deleted'
|
|
566
|
-
AND retire_after < now()
|
|
567
|
-
RETURNING upload_id`,
|
|
568
|
-
[body.uploadId, body.retireClaimToken],
|
|
569
|
-
);
|
|
570
|
-
return result.rows.length === 1;
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
/** A failed or ambiguous delete keeps the row for the next sweep. */
|
|
574
|
-
async releaseRetirement(body: RuntimeGatewayStagedBody): Promise<boolean> {
|
|
575
|
-
if (!body.retireClaimToken) return false;
|
|
576
|
-
const result = await this.run<Row>(
|
|
577
|
-
`UPDATE ${this.table}
|
|
578
|
-
SET retire_claim_token = NULL, retire_claimed_at = NULL, updated_at = now()
|
|
579
|
-
WHERE upload_id = $1 AND retire_claim_token = $2
|
|
580
|
-
RETURNING upload_id`,
|
|
581
|
-
[body.uploadId, body.retireClaimToken],
|
|
582
|
-
);
|
|
583
|
-
return result.rows.length === 1;
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
/**
|
|
587
|
-
* Hand back claims a pass will not process (its deadline arrived), in one
|
|
588
|
-
* statement, so they are claimable by the next pass at once rather than
|
|
589
|
-
* after the stale-claim window.
|
|
590
|
-
*/
|
|
591
|
-
async releaseRetirements(
|
|
592
|
-
bodies: readonly RuntimeGatewayStagedBody[],
|
|
593
|
-
): Promise<number> {
|
|
594
|
-
const held = bodies.filter((body) => body.retireClaimToken);
|
|
595
|
-
if (held.length === 0) return 0;
|
|
596
|
-
const result = await this.run<Row>(
|
|
597
|
-
`UPDATE ${this.table} AS staged
|
|
598
|
-
SET retire_claim_token = NULL, retire_claimed_at = NULL, updated_at = now()
|
|
599
|
-
FROM unnest($1::text[], $2::text[]) AS released(upload_id, token)
|
|
600
|
-
WHERE staged.upload_id = released.upload_id
|
|
601
|
-
AND staged.retire_claim_token = released.token
|
|
602
|
-
RETURNING staged.upload_id`,
|
|
603
|
-
[
|
|
604
|
-
held.map((body) => body.uploadId),
|
|
605
|
-
held.map((body) => body.retireClaimToken),
|
|
606
|
-
],
|
|
607
|
-
);
|
|
608
|
-
return result.rows.length;
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
/** Drop bounded batches of long-deleted custody rows. */
|
|
612
|
-
async pruneDeleted(input: {
|
|
613
|
-
olderThanSeconds: number;
|
|
614
|
-
limit: number;
|
|
615
|
-
}): Promise<number> {
|
|
616
|
-
const result = await this.run<Row>(
|
|
617
|
-
`DELETE FROM ${this.table} WHERE upload_id IN (
|
|
618
|
-
SELECT upload_id FROM ${this.table}
|
|
619
|
-
WHERE state = 'deleted'
|
|
620
|
-
AND deleted_at < now() - make_interval(secs => $1::double precision)
|
|
621
|
-
ORDER BY deleted_at LIMIT $2
|
|
622
|
-
) RETURNING upload_id`,
|
|
623
|
-
[input.olderThanSeconds, input.limit],
|
|
624
|
-
);
|
|
625
|
-
return result.rows.length;
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
/**
|
|
629
|
-
* Teardown only: the number of rows whose object is not yet confirmed
|
|
630
|
-
* absent, or null when the journal table does not exist.
|
|
631
|
-
*/
|
|
632
|
-
async countUnconfirmedCustody(): Promise<number | null> {
|
|
633
|
-
try {
|
|
634
|
-
const result = await this.client.query<{ count: string | number }>(
|
|
635
|
-
`SELECT count(*) AS count FROM ${this.table} WHERE state <> 'deleted'`,
|
|
636
|
-
[],
|
|
637
|
-
);
|
|
638
|
-
return Number(result.rows[0]?.count ?? 0);
|
|
639
|
-
} catch (error) {
|
|
640
|
-
if (isUndefinedTable(error)) return null;
|
|
641
|
-
throw error;
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
/**
|
|
646
|
-
* Teardown only: claim undeleted rows whose upload grant has expired,
|
|
647
|
-
* regardless of `retire_after`. The caller has quiesced every writer of
|
|
648
|
-
* this schema and is about to drop it. A row whose grant is still live is
|
|
649
|
-
* never claimed: its presigned PUT could recreate the key after a delete,
|
|
650
|
-
* so it stays unconfirmed and the teardown refuses the drop until it
|
|
651
|
-
* expires.
|
|
652
|
-
*/
|
|
653
|
-
async claimForcedRetirements(
|
|
654
|
-
limit: number,
|
|
655
|
-
excludeUploadIds: readonly string[] = [],
|
|
656
|
-
): Promise<RuntimeGatewayStagedBody[]> {
|
|
657
|
-
if (!Number.isSafeInteger(limit) || limit < 1 || limit > 64) {
|
|
658
|
-
throw new Error('Runtime gateway staging retirement limit is invalid.');
|
|
659
|
-
}
|
|
660
|
-
const result = await this.run<Row>(
|
|
661
|
-
`WITH held AS (
|
|
662
|
-
SELECT upload_id FROM ${this.table}
|
|
663
|
-
WHERE state <> 'deleted' AND grant_expires_at <= now()
|
|
664
|
-
AND NOT (upload_id = ANY($3::text[]))
|
|
665
|
-
ORDER BY created_at
|
|
666
|
-
LIMIT $1
|
|
667
|
-
FOR UPDATE SKIP LOCKED
|
|
668
|
-
)
|
|
669
|
-
UPDATE ${this.table} AS staged
|
|
670
|
-
SET retire_claim_token = $2, retire_claimed_at = now(), updated_at = now()
|
|
671
|
-
FROM held WHERE staged.upload_id = held.upload_id
|
|
672
|
-
RETURNING staged.*`,
|
|
673
|
-
[limit, randomUUID(), [...excludeUploadIds]],
|
|
674
|
-
);
|
|
675
|
-
return result.rows.map(decode);
|
|
676
|
-
}
|
|
677
|
-
|
|
678
|
-
/**
|
|
679
|
-
* Teardown only: after confirmed absence in the bucket `bucketFingerprint`
|
|
680
|
-
* names, whatever `retire_after` says. A row reserved against another
|
|
681
|
-
* bucket is never marked deleted.
|
|
682
|
-
*/
|
|
683
|
-
async confirmForcedDeleted(
|
|
684
|
-
body: RuntimeGatewayStagedBody,
|
|
685
|
-
bucketFingerprint: string,
|
|
686
|
-
): Promise<boolean> {
|
|
687
|
-
if (!body.retireClaimToken) return false;
|
|
688
|
-
const result = await this.run<Row>(
|
|
689
|
-
`UPDATE ${this.table}
|
|
690
|
-
SET state = 'deleted', deleted_at = now(), retire_claim_token = NULL,
|
|
691
|
-
retire_claimed_at = NULL, updated_at = now()
|
|
692
|
-
WHERE upload_id = $1 AND retire_claim_token = $2 AND state <> 'deleted'
|
|
693
|
-
AND grant_expires_at <= now()
|
|
694
|
-
AND (bucket_fingerprint IS NULL OR bucket_fingerprint = $3)
|
|
695
|
-
RETURNING upload_id`,
|
|
696
|
-
[body.uploadId, body.retireClaimToken, bucketFingerprint],
|
|
697
|
-
);
|
|
698
|
-
return result.rows.length === 1;
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
async get(uploadId: string): Promise<RuntimeGatewayStagedBody | null> {
|
|
702
|
-
const result = await this.run<Row>(
|
|
703
|
-
`SELECT * FROM ${this.table} WHERE upload_id = $1`,
|
|
704
|
-
[uploadId],
|
|
705
|
-
);
|
|
706
|
-
return result.rows[0] ? decode(result.rows[0]) : null;
|
|
707
|
-
}
|
|
708
|
-
}
|