deepline 0.3.537 → 0.3.539
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/client-core.ts +22 -5
- package/dist/bundling-sources/sdk/src/index.ts +2 -0
- package/dist/bundling-sources/sdk/src/plays/client-core.ts +18 -1
- package/dist/bundling-sources/sdk/src/release.ts +1 -1
- package/dist/bundling-sources/sdk/src/runs/watch-polling.ts +33 -4
- package/dist/bundling-sources/shared_libs/integration-contracts/billing-entitlements.ts +1 -1
- package/dist/bundling-sources/shared_libs/observability/scheduled-jobs.json +18 -0
- package/dist/bundling-sources/shared_libs/play-runtime/app-runtime-api.ts +9 -1
- package/dist/bundling-sources/shared_libs/play-runtime/baked-runner-image.ts +392 -0
- package/dist/bundling-sources/shared_libs/play-runtime/context.ts +717 -133
- package/dist/bundling-sources/shared_libs/play-runtime/coordinator-headers.ts +7 -0
- package/dist/bundling-sources/shared_libs/play-runtime/daytona-payload-contract.ts +22 -1
- package/dist/bundling-sources/shared_libs/play-runtime/durable-runs-rollout.ts +123 -0
- package/dist/bundling-sources/shared_libs/play-runtime/experiment-switches.ts +480 -0
- package/dist/bundling-sources/shared_libs/play-runtime/fixture-egress-fetch.ts +24 -16
- package/dist/bundling-sources/shared_libs/play-runtime/freshness-receipt-execution.ts +293 -53
- package/dist/bundling-sources/shared_libs/play-runtime/governor/block-reserving-budget-state-backend.ts +57 -2
- package/dist/bundling-sources/shared_libs/play-runtime/governor/budget-state-backend.ts +43 -0
- package/dist/bundling-sources/shared_libs/play-runtime/governor/governor.ts +141 -9
- package/dist/bundling-sources/shared_libs/play-runtime/launch-identity.ts +4 -0
- package/dist/bundling-sources/shared_libs/play-runtime/modal-runtime-config.ts +6 -0
- package/dist/bundling-sources/shared_libs/play-runtime/modal-supervisor-sidecar.ts +556 -0
- package/dist/bundling-sources/shared_libs/play-runtime/protocol.ts +27 -0
- package/dist/bundling-sources/shared_libs/play-runtime/resource-governor.ts +19 -0
- package/dist/bundling-sources/shared_libs/play-runtime/run-ledger.ts +54 -0
- package/dist/bundling-sources/shared_libs/play-runtime/run-snapshot-stream.ts +5 -0
- package/dist/bundling-sources/shared_libs/play-runtime/runner-app/index.ts +155 -7
- package/dist/bundling-sources/shared_libs/play-runtime/runner-app/relay-connection-counter.ts +103 -0
- package/dist/bundling-sources/shared_libs/play-runtime/runner-app/runtime-heartbeat.ts +75 -6
- package/dist/bundling-sources/shared_libs/play-runtime/runner-app/runtime-terminal-push.ts +59 -0
- package/dist/bundling-sources/shared_libs/play-runtime/runner-backends/backends/daytona-payload-transport.ts +544 -73
- package/dist/bundling-sources/shared_libs/play-runtime/runner-backends/backends/daytona-runtime-watchdog.ts +8 -1
- package/dist/bundling-sources/shared_libs/play-runtime/runner-backends/backends/detached-runner-readiness-pacing.ts +19 -1
- package/dist/bundling-sources/shared_libs/play-runtime/runner-backends/backends/modal.ts +2399 -110
- package/dist/bundling-sources/shared_libs/play-runtime/runtime-flow.ts +42 -8
- package/dist/bundling-sources/shared_libs/play-runtime/runtime-read-through.ts +287 -0
- package/dist/bundling-sources/shared_libs/play-runtime/runtime-receipt-finish.ts +606 -0
- package/dist/bundling-sources/shared_libs/play-runtime/runtime-receipt-writer.ts +274 -119
- package/dist/bundling-sources/shared_libs/play-runtime/runtime-speed-profile.ts +136 -0
- package/dist/bundling-sources/shared_libs/play-runtime/runtime-traffic-policy.ts +54 -0
- package/dist/bundling-sources/shared_libs/play-runtime/runtime-work-counters.ts +10 -1
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backend.ts +22 -0
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/absurd-play-run-task.ts +18 -1
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/absurd-pool.ts +18 -4
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/absurd-runtime-worker.ts +219 -7
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/absurd-shared.ts +3 -0
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/early-sandbox-claim-affinity.ts +340 -0
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/play-run-recovery.ts +1 -0
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/postgres-progress.ts +10 -1
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/postgres-rate-state.ts +623 -7
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/postgres.ts +340 -5
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/runner-push.ts +61 -0
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/runtime-event-search.ts +2 -1
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/runtime-sandbox-ledger-reconciliation.ts +269 -2
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/runtime-worker-wake.ts +238 -5
- package/dist/bundling-sources/shared_libs/play-runtime/startup-read-tool-ids.ts +47 -0
- package/dist/bundling-sources/shared_libs/play-runtime/tool-call/runtime-step-receipts-adapter.ts +5 -4
- package/dist/bundling-sources/shared_libs/play-runtime/tool-result.ts +6 -0
- package/dist/bundling-sources/shared_libs/play-runtime/transient-service-error.ts +11 -0
- package/dist/cli/auth-O5Y2VYXK.js +43 -0
- package/dist/cli/{auth-4SUPGDIM.mjs → auth-QD7QGD3B.mjs} +5 -5
- package/dist/cli/{billing-5JUA3ZM3.mjs → billing-FIZXMHBW.mjs} +31 -7
- package/dist/cli/{billing-SQU726P7.js → billing-KOQAF6WS.js} +114 -90
- package/dist/cli/{chunk-R7JYG6XO.mjs → chunk-2XMFT44N.mjs} +1 -1
- package/dist/cli/{chunk-XDS26J7Z.js → chunk-3YDI2PTN.js} +10 -10
- package/dist/cli/{chunk-KH27B2HJ.mjs → chunk-55ITVU2V.mjs} +2 -2
- package/dist/cli/{chunk-2Q7VXTMS.js → chunk-5QL4GJB2.js} +2 -2
- package/dist/cli/{chunk-D77W6Q67.js → chunk-5RUB53KG.js} +26 -26
- package/dist/cli/{chunk-JS2SBCWD.js → chunk-5YF35HAV.js} +6 -6
- package/dist/cli/{chunk-C37VELBR.mjs → chunk-6KG4KVTH.mjs} +5 -5
- package/dist/cli/{chunk-AX2OAXDL.mjs → chunk-6NZBLFL4.mjs} +1 -1
- package/dist/cli/{chunk-C3XD2WHM.mjs → chunk-6V3LNYK5.mjs} +2 -2
- package/dist/cli/{chunk-GPMS6OPX.mjs → chunk-ADV2IB2C.mjs} +5 -5
- package/dist/cli/{chunk-ZUAMXZXC.mjs → chunk-APKZGUTU.mjs} +4 -4
- package/dist/cli/{chunk-IQOIT6E3.js → chunk-CQJCLHBE.js} +41 -41
- package/dist/cli/{chunk-W3J3LT22.mjs → chunk-DZFVVP3S.mjs} +5 -5
- package/dist/cli/{chunk-V5IIBG2E.mjs → chunk-E4QHTDPF.mjs} +1 -1
- package/dist/cli/{chunk-CXTW7SRW.mjs → chunk-E7D37PLM.mjs} +8 -0
- package/dist/cli/{chunk-KULT6O5K.js → chunk-EAZS52DL.js} +15 -15
- package/dist/cli/{chunk-L3JVGNAT.mjs → chunk-F43MXIN3.mjs} +2 -2
- package/dist/cli/{chunk-WBTYWJLN.js → chunk-FH3P3E4S.js} +2 -2
- package/dist/cli/{chunk-K4WJ4BYO.mjs → chunk-FIRV7BXJ.mjs} +1 -1
- package/dist/cli/{chunk-NXGLJGPB.js → chunk-FSQCB5SJ.js} +2 -2
- package/dist/cli/{chunk-AVQET3NR.js → chunk-FTBWFDF3.js} +7 -7
- package/dist/cli/{chunk-QDBGPRR2.mjs → chunk-G3UFDUYU.mjs} +3 -1
- package/dist/cli/{chunk-ZHPSJ3LJ.mjs → chunk-GEO2EMM7.mjs} +29 -16
- package/dist/cli/{chunk-CGDW2CRR.mjs → chunk-GLEGINSP.mjs} +2 -2
- package/dist/cli/{chunk-EEFSUCYP.mjs → chunk-GLKUC5PO.mjs} +2 -2
- package/dist/cli/{chunk-BFHR7IC2.mjs → chunk-GNCIMHO2.mjs} +10 -10
- package/dist/cli/{chunk-PU42VOBI.js → chunk-ISXX5DUD.js} +3 -3
- package/dist/cli/{chunk-Y6AMMXWS.mjs → chunk-JBCQFG7G.mjs} +3 -3
- package/dist/cli/{chunk-Z2PPB3IO.mjs → chunk-JHEZQMOM.mjs} +1 -1
- package/dist/cli/{chunk-WBFOV5HT.js → chunk-K22EKW6L.js} +5 -5
- package/dist/cli/{chunk-CHIGEDAE.js → chunk-K5MZHTK7.js} +3 -3
- package/dist/cli/{chunk-L6QAIZNZ.js → chunk-KAEVL62Q.js} +44 -44
- package/dist/cli/{chunk-PJ4ZGZAN.js → chunk-KQTX545E.js} +13 -13
- package/dist/cli/{chunk-VP2K5B3R.mjs → chunk-LE5KN7CZ.mjs} +3 -3
- package/dist/cli/{chunk-BGXEMTIG.js → chunk-LEREPTAE.js} +19 -19
- package/dist/cli/{chunk-QPDLG7DQ.js → chunk-MI53W4DN.js} +53 -53
- package/dist/cli/{chunk-RMVZTBI5.mjs → chunk-MKESK3SW.mjs} +4 -4
- package/dist/cli/{chunk-NU7B5X6J.mjs → chunk-N3LPGGWD.mjs} +1 -1
- package/dist/cli/{chunk-VRYX2BR4.js → chunk-NR5PLMNG.js} +107 -107
- package/dist/cli/{chunk-JMHYDDBQ.js → chunk-Q4HNK7JX.js} +292 -279
- package/dist/cli/{chunk-37AZHCTP.mjs → chunk-RULRGMMM.mjs} +5 -5
- package/dist/cli/{chunk-GI3V4D7K.mjs → chunk-SDAP5MCO.mjs} +2 -2
- package/dist/cli/{chunk-5EOEJKSV.js → chunk-TZBCJHV6.js} +1 -1
- package/dist/cli/{chunk-3PMCQ3LK.js → chunk-V7DEGIYO.js} +7 -5
- package/dist/cli/{chunk-ALHKB3AE.js → chunk-VK5UTS5B.js} +9 -9
- package/dist/cli/{chunk-ZFSB7GXF.js → chunk-W32KT6LM.js} +5 -5
- package/dist/cli/{chunk-6BBGUQJJ.js → chunk-XTYHZEJ2.js} +8 -0
- package/dist/cli/{chunk-UGA5KI4A.js → chunk-XZBXQCRJ.js} +22 -22
- package/dist/cli/{chunk-BRRCWUOW.mjs → chunk-Y66ZMOJI.mjs} +1 -1
- package/dist/cli/{chunk-CFND2ACL.js → chunk-YMQFQO3K.js} +16 -16
- package/dist/cli/command-docs.js +2 -2
- package/dist/cli/command-docs.mjs +1 -1
- package/dist/cli/{csv-JITYN27L.js → csv-BWWJYV2C.js} +13 -13
- package/dist/cli/{csv-HZP6QMG7.mjs → csv-G5RQQ74W.mjs} +4 -4
- package/dist/cli/{db-FYSRB7O4.js → db-5KNDLADE.js} +27 -27
- package/dist/cli/{db-QRIRTOMM.mjs → db-AJZHWB3V.mjs} +6 -6
- package/dist/cli/describe-B6CN6CFJ.js +49 -0
- package/dist/cli/{describe-REXJVJC3.mjs → describe-QY5TKWVD.mjs} +17 -17
- package/dist/cli/describe-core.d.mts +3 -3
- package/dist/cli/describe-core.d.ts +3 -3
- package/dist/cli/{executions-BSTF4DDS.js → executions-BCCW76BI.js} +8 -8
- package/dist/cli/{executions-53KJAFOW.mjs → executions-SJPH3MZT.mjs} +6 -6
- package/dist/cli/{feedback-6MW3QOXX.js → feedback-GF65JKK6.js} +14 -14
- package/dist/cli/{feedback-CWUJJF3J.mjs → feedback-S6SZOFGK.mjs} +6 -6
- package/dist/cli/git-DX6NB7VF.js +25 -0
- package/dist/cli/{git-CKVZTJFS.mjs → git-F5FT5YVB.mjs} +5 -5
- package/dist/cli/index.js +143 -143
- package/dist/cli/index.mjs +42 -42
- package/dist/cli/{legacy-noops-QBL4PNGB.js → legacy-noops-ECR6FS6U.js} +9 -9
- package/dist/cli/{legacy-noops-USTBRFRX.mjs → legacy-noops-NOA4P5NE.mjs} +6 -6
- package/dist/cli/{monitors-6UCG5D7W.mjs → monitors-CTYSAGP4.mjs} +7 -7
- package/dist/cli/{monitors-VCR6SUZD.js → monitors-DEWBSURN.js} +100 -100
- package/dist/cli/{org-GSRBOJVO.mjs → org-EKSKKT7H.mjs} +6 -6
- package/dist/cli/{org-HPILTEG4.js → org-ZM2UUMZZ.js} +86 -86
- package/dist/cli/{play-TK7WOP5H.js → play-6JQOM6DE.js} +317 -317
- package/dist/cli/{play-QRTYQNG4.mjs → play-R2BH374J.mjs} +17 -17
- package/dist/cli/{providers-HEXNJ5QC.js → providers-CMIYHEMN.js} +9 -9
- package/dist/cli/{providers-OAQJWVBS.mjs → providers-X7WMIWO4.mjs} +6 -6
- package/dist/cli/{quickstart-PYZTFXZX.js → quickstart-6HVPAMSW.js} +6 -6
- package/dist/cli/{quickstart-FFVHTPSP.mjs → quickstart-ZZ7L6VZJ.mjs} +4 -4
- package/dist/cli/{run-HLN6YYZH.mjs → run-FP32RKBU.mjs} +22 -22
- package/dist/cli/{run-4DWJXJRO.js → run-OOHYH3GI.js} +42 -42
- package/dist/cli/search-34C6LFOJ.js +32 -0
- package/dist/cli/{search-ZHRCXGEL.mjs → search-BDDNC23Z.mjs} +7 -7
- package/dist/cli/{search-output-2KY6PFDH.js → search-output-5VZIQQAM.js} +6 -6
- package/dist/cli/{search-output-N7QXACVK.mjs → search-output-QXJ6CV5G.mjs} +4 -4
- package/dist/cli/{secrets-WXQ7XO5C.js → secrets-EN2HQCO3.js} +15 -15
- package/dist/cli/{secrets-FKMWBLHU.mjs → secrets-Z7CQSQGC.mjs} +6 -6
- package/dist/cli/{sessions-3YDTEDA5.mjs → sessions-I2H37OTJ.mjs} +6 -6
- package/dist/cli/sessions-RYQX3NDD.js +22 -0
- package/dist/cli/{settings-67TWLQ6E.js → settings-CTRAPFNZ.js} +85 -85
- package/dist/cli/{settings-JTPASUZN.mjs → settings-IYSC5PPQ.mjs} +12 -12
- package/dist/cli/{setup-VYNGXUJ6.mjs → setup-PU3HOVI3.mjs} +9 -9
- package/dist/cli/{setup-KUERQJUX.js → setup-XIN53GEW.js} +49 -49
- package/dist/cli/skills-576CX7DK.js +36 -0
- package/dist/cli/{skills-KI4KYHH6.mjs → skills-TYBML2ZO.mjs} +7 -7
- package/dist/cli/tools-EXMUWA3Z.js +56 -0
- package/dist/cli/{tools-UQDJS2RS.mjs → tools-ZVNE6M6J.mjs} +16 -16
- package/dist/cli/{update-7FQQUBOD.mjs → update-54WENBNT.mjs} +9 -9
- package/dist/cli/update-RAHETNL2.js +34 -0
- package/dist/cli/{views-MJK7JGZG.js → views-7L75EZOJ.js} +21 -21
- package/dist/cli/{views-JAM4MNNV.mjs → views-EUBB4ONT.mjs} +6 -6
- package/dist/cli/{workflow-AQPRIWQX.js → workflow-EZVYCR6Y.js} +17 -17
- package/dist/cli/{workflow-TQRV5FGO.mjs → workflow-GUKGFJ72.mjs} +7 -7
- package/dist/{client-BlLYQDNo.d.mts → client-D0uDBybG.d.mts} +1 -1
- package/dist/{client-DSQlkIDO.d.ts → client-DZTnAv4B.d.ts} +1 -1
- package/dist/{client-core-rX7n2H8U.d.mts → client-core-BhWf8Y1b.d.mts} +15 -2
- package/dist/{client-core-CDwapXy5.d.ts → client-core-og3AP1LL.d.ts} +15 -2
- package/dist/{compiler-manifest-BHNC4RL-.d.ts → compiler-manifest-Bes3OVA3.d.mts} +1 -1
- package/dist/{compiler-manifest-BHNC4RL-.d.mts → compiler-manifest-Bes3OVA3.d.ts} +1 -1
- package/dist/{errors-lhtIWG5Z.d.ts → errors-BJm4lLWc.d.ts} +1 -1
- package/dist/{errors-D7sPXrvB.d.mts → errors-Dc5r42ks.d.mts} +1 -1
- package/dist/{index-CQTBHXHr.d.ts → index-BfV7mRPF.d.ts} +1 -1
- package/dist/{index-NaDZq2xf.d.mts → index-DTkiz_Ur.d.mts} +1 -1
- package/dist/index.d.mts +7 -7
- package/dist/index.d.ts +7 -7
- package/dist/index.js +9 -9
- package/dist/index.mjs +6 -6
- package/dist/install-integrity.json +121 -112
- package/dist/plays/bundle-play-file.d.mts +3 -3
- package/dist/plays/bundle-play-file.d.ts +3 -3
- package/dist/plays/play-description.d.mts +2 -2
- package/dist/plays/play-description.d.ts +2 -2
- package/dist/plays/run-request.d.mts +2 -2
- package/dist/plays/run-request.d.ts +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 +4 -4
- package/dist/server.d.ts +4 -4
- package/dist/server.js +5 -5
- package/dist/server.mjs +3 -3
- package/package.json +1 -1
- package/dist/cli/auth-Y2K2ZYYA.js +0 -43
- package/dist/cli/describe-LSOHOLFI.js +0 -49
- package/dist/cli/git-S7SWUZ3T.js +0 -25
- package/dist/cli/search-WERQBC6V.js +0 -32
- package/dist/cli/sessions-LXQ4Q6BS.js +0 -22
- package/dist/cli/skills-EFRLHY3F.js +0 -36
- package/dist/cli/tools-C3EIGFFP.js +0 -56
- package/dist/cli/update-KCY72RXH.js +0 -34
|
@@ -16,6 +16,12 @@ export function createFixtureEgressFetch(input: {
|
|
|
16
16
|
baseUrl: string;
|
|
17
17
|
executorToken: string;
|
|
18
18
|
extraHeaders?: () => Promise<Record<string, string>>;
|
|
19
|
+
/**
|
|
20
|
+
* `broker` sends the plain egress request the broker executes for a live
|
|
21
|
+
* run (`modal_sidecar`, whose runner has no outbound network). Default
|
|
22
|
+
* `fixture` keeps the fixture envelope byte for byte.
|
|
23
|
+
*/
|
|
24
|
+
envelope?: 'fixture' | 'broker';
|
|
19
25
|
}): (url: string | URL, init?: RequestInit) => Promise<Response> {
|
|
20
26
|
const endpoint = `${input.baseUrl.replace(/\/$/, '')}${PLAY_RUNTIME_EGRESS_FETCH_CURRENT_PATH}`;
|
|
21
27
|
return async (url, init) => {
|
|
@@ -31,6 +37,19 @@ export function createFixtureEgressFetch(input: {
|
|
|
31
37
|
'Fixture ctx.fetch supports only string or URLSearchParams bodies.',
|
|
32
38
|
);
|
|
33
39
|
}
|
|
40
|
+
const egressRequest = {
|
|
41
|
+
url: String(url),
|
|
42
|
+
method: init?.method ?? 'GET',
|
|
43
|
+
headers,
|
|
44
|
+
body:
|
|
45
|
+
body === undefined || body === null
|
|
46
|
+
? { kind: 'none' }
|
|
47
|
+
: typeof body === 'string'
|
|
48
|
+
? { kind: 'string', value: body }
|
|
49
|
+
: { kind: 'urlSearchParams', value: body.toString() },
|
|
50
|
+
// ctx.fetch redacts its secret header values from the response itself.
|
|
51
|
+
sensitiveHeaders: [],
|
|
52
|
+
};
|
|
34
53
|
const response = await fetch(endpoint, {
|
|
35
54
|
method: 'POST',
|
|
36
55
|
redirect: 'manual',
|
|
@@ -40,22 +59,11 @@ export function createFixtureEgressFetch(input: {
|
|
|
40
59
|
'Content-Type': 'application/json',
|
|
41
60
|
...(await input.extraHeaders?.()),
|
|
42
61
|
},
|
|
43
|
-
// Older brokers reject
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
headers,
|
|
49
|
-
body:
|
|
50
|
-
body === undefined || body === null
|
|
51
|
-
? { kind: 'none' }
|
|
52
|
-
: typeof body === 'string'
|
|
53
|
-
? { kind: 'string', value: body }
|
|
54
|
-
: { kind: 'urlSearchParams', value: body.toString() },
|
|
55
|
-
// ctx.fetch redacts its secret header values from the response itself.
|
|
56
|
-
sensitiveHeaders: [],
|
|
57
|
-
},
|
|
58
|
-
}),
|
|
62
|
+
// Older brokers reject the fixture envelope before opening an outbound
|
|
63
|
+
// socket; the broker envelope is the live egress request itself.
|
|
64
|
+
body: JSON.stringify(
|
|
65
|
+
input.envelope === 'broker' ? egressRequest : { fixtureRequest: egressRequest },
|
|
66
|
+
),
|
|
59
67
|
});
|
|
60
68
|
const envelope = (await response.json().catch(() => null)) as {
|
|
61
69
|
error?: unknown;
|
|
@@ -9,6 +9,11 @@ import {
|
|
|
9
9
|
type DurableReceiptExecutionStore,
|
|
10
10
|
} from './durable-receipt-execution';
|
|
11
11
|
import { DURABLE_RUNS_ROLLOUT } from './durable-runs-rollout';
|
|
12
|
+
import {
|
|
13
|
+
isRuntimeReadThroughFallback,
|
|
14
|
+
type RuntimeReadThroughFallback,
|
|
15
|
+
type RuntimeReadThroughGrant,
|
|
16
|
+
} from './runtime-read-through';
|
|
12
17
|
import { RuntimeReceiptWriterBufferLimitError } from './runtime-receipt-writer';
|
|
13
18
|
import { assertNoSecretTaint } from './secret-capability';
|
|
14
19
|
import {
|
|
@@ -143,6 +148,54 @@ export type DeferredReceiptWrite = {
|
|
|
143
148
|
|
|
144
149
|
export type DeferredReceiptPublication = {
|
|
145
150
|
defer(write: DeferredReceiptWrite): void;
|
|
151
|
+
/**
|
|
152
|
+
* A receipt named in `companionReceiptKeys`, as the operation receipt's
|
|
153
|
+
* request read it: `{ receipt: null }` when absent, undefined when it was
|
|
154
|
+
* not read. Each is handed out once, to the read it replaces.
|
|
155
|
+
*/
|
|
156
|
+
takeCompanionReceipt?(
|
|
157
|
+
key: string,
|
|
158
|
+
): { receipt: RuntimeStepReceipt | null } | undefined;
|
|
159
|
+
/**
|
|
160
|
+
* The call's own finishing keys, for a trusted execution owner that can
|
|
161
|
+
* commit them inside the execute request. Absent for callers that never
|
|
162
|
+
* finish remotely.
|
|
163
|
+
*/
|
|
164
|
+
finishTargets?: {
|
|
165
|
+
operationKey: string;
|
|
166
|
+
/**
|
|
167
|
+
* #10008's exact-request slot, when this call has one. The owner writes
|
|
168
|
+
* it beside the call-order slot, with the same bound record.
|
|
169
|
+
*/
|
|
170
|
+
exactRequestOperationKey: string | null;
|
|
171
|
+
/**
|
|
172
|
+
* #10008's binding, exactly as this call records it in every operation
|
|
173
|
+
* slot (`{ ...completed, ...fingerprints }`). Never re-derived.
|
|
174
|
+
*/
|
|
175
|
+
fingerprints: { requestFingerprint: string; inputFingerprint?: string };
|
|
176
|
+
/** Policy bucket prefix and duration; null when the policy is 0. */
|
|
177
|
+
reusable: { prefix: string; ttlSeconds: number | null } | null;
|
|
178
|
+
};
|
|
179
|
+
/**
|
|
180
|
+
* The execution owner reports what it committed. The call still decides
|
|
181
|
+
* for itself: it skips its own finishing request only when `covers` proves
|
|
182
|
+
* the owner committed exactly the writes it would have made.
|
|
183
|
+
*/
|
|
184
|
+
acknowledgeRemoteFinish?(ack: RemoteFinishAcknowledgement): void;
|
|
185
|
+
/**
|
|
186
|
+
* Present only on a read-through attempt: the operation slots and companion
|
|
187
|
+
* receipts were not read here. The call must hand this grant to a dispatch
|
|
188
|
+
* that reads them at the execution relay before executing, or throw
|
|
189
|
+
* `RuntimeReadThroughFallback` before any dispatch.
|
|
190
|
+
*/
|
|
191
|
+
readThrough?: RuntimeReadThroughGrant;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
export type RemoteFinishAcknowledgement = {
|
|
195
|
+
/** The completion time the owner recorded in the operation receipt. */
|
|
196
|
+
completedAtMs: number;
|
|
197
|
+
/** Exactly these keys, each with exactly this value, are durable. */
|
|
198
|
+
covers(writes: ReadonlyArray<{ key: string; output: unknown }>): boolean;
|
|
146
199
|
};
|
|
147
200
|
|
|
148
201
|
type FreshnessStore = Pick<
|
|
@@ -470,6 +523,23 @@ export async function executeWithFreshnessReceipt<T>(input: {
|
|
|
470
523
|
publication: DeferredReceiptPublication,
|
|
471
524
|
replay?: FreshExecutionReplay,
|
|
472
525
|
) => Promise<T>;
|
|
526
|
+
/**
|
|
527
|
+
* Keys `execute` reads first if the call executes (a tool call's own result
|
|
528
|
+
* receipt). They travel in the operation receipt's read request, so a miss
|
|
529
|
+
* costs one read instead of two; `execute` takes each through
|
|
530
|
+
* `publication.takeCompanionReceipt`. Bodies are hydrated: the read happens
|
|
531
|
+
* before the operation answer is known.
|
|
532
|
+
*/
|
|
533
|
+
companionReceiptKeys?: readonly string[];
|
|
534
|
+
/**
|
|
535
|
+
* Local-permit read-through. For a policy-0 call with companion keys,
|
|
536
|
+
* `begin` takes dispatch capacity without waiting; a grant replaces this
|
|
537
|
+
* layer's own read with the relay's read of exactly the same keys inside
|
|
538
|
+
* the execute request. Null keeps today's read. Absent: today's read.
|
|
539
|
+
*/
|
|
540
|
+
readThrough?: {
|
|
541
|
+
begin(keys: readonly string[]): Promise<RuntimeReadThroughGrant | null>;
|
|
542
|
+
};
|
|
473
543
|
completedAt?: (value: T) => number | undefined;
|
|
474
544
|
/**
|
|
475
545
|
* False keeps an empty or unsuccessful result out of the shared latest
|
|
@@ -516,6 +586,11 @@ export async function executeWithFreshnessReceipt<T>(input: {
|
|
|
516
586
|
reason: 'too_large' | 'error';
|
|
517
587
|
error: unknown;
|
|
518
588
|
}) => void;
|
|
589
|
+
/**
|
|
590
|
+
* Reports whether a remote finish replaced this call's finishing request.
|
|
591
|
+
* Observation only; never changes the outcome.
|
|
592
|
+
*/
|
|
593
|
+
onRemoteFinish?: (outcome: 'acknowledged' | 'not_covered') => void;
|
|
519
594
|
onOutcome: (value: T, metadata: CallFreshnessMetadata) => T;
|
|
520
595
|
withHydration: <Result>(
|
|
521
596
|
read: (
|
|
@@ -818,27 +893,196 @@ export async function executeWithFreshnessReceipt<T>(input: {
|
|
|
818
893
|
// One stage: this run's own operation (with its body, so recovery needs no
|
|
819
894
|
// second read) beside the candidates' manifests. Own-run recovery still
|
|
820
895
|
// wins whenever it exists.
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
896
|
+
// The call's own result receipt rides in the same batch (#9962): a fixed,
|
|
897
|
+
// directly keyed set, never a scan. Classification of the operation slots
|
|
898
|
+
// is unchanged; a companion is only handed to the read it replaces.
|
|
899
|
+
const ownSlotKeys = exactRequestOperationKey
|
|
900
|
+
? [exactRequestOperationKey, operationKey]
|
|
901
|
+
: [operationKey];
|
|
902
|
+
const companionKeys = input.store.getMany
|
|
903
|
+
? [
|
|
904
|
+
...new Set(
|
|
905
|
+
(input.companionReceiptKeys ?? []).filter(
|
|
906
|
+
(key) => key.trim().length > 0 && !ownSlotKeys.includes(key),
|
|
907
|
+
),
|
|
908
|
+
),
|
|
909
|
+
]
|
|
910
|
+
: [];
|
|
911
|
+
const companions = new Map<string, RuntimeStepReceipt | null>();
|
|
912
|
+
const readOperationSlots = async (
|
|
913
|
+
relayRead: ReadonlyArray<RuntimeStepReceipt | null> | null = null,
|
|
914
|
+
): Promise<{
|
|
915
|
+
exact: CompletedCall | null;
|
|
916
|
+
callOrder: CompletedCall | null;
|
|
917
|
+
}> => {
|
|
918
|
+
if (companionKeys.length === 0 && relayRead === null) {
|
|
919
|
+
return exactRequestOperationKey
|
|
920
|
+
? // One batch: the exact-request slot and the call-order slot.
|
|
921
|
+
readMany([exactRequestOperationKey, operationKey]).then(
|
|
922
|
+
([exact, callOrder]) => ({
|
|
923
|
+
exact: completedCall(exact ?? null),
|
|
924
|
+
callOrder: completedCall(callOrder ?? null),
|
|
925
|
+
}),
|
|
926
|
+
)
|
|
927
|
+
: read(operationKey).then((callOrder) => ({
|
|
928
|
+
exact: null,
|
|
929
|
+
callOrder,
|
|
930
|
+
}));
|
|
931
|
+
}
|
|
932
|
+
// The relay read exactly these keys, in this order, before it declined
|
|
933
|
+
// or answered hit; its records are classified as this read's would be.
|
|
934
|
+
const receipts =
|
|
935
|
+
relayRead ?? (await readMany([...ownSlotKeys, ...companionKeys]));
|
|
936
|
+
companionKeys.forEach((key, index) => {
|
|
937
|
+
companions.set(key, receipts[ownSlotKeys.length + index] ?? null);
|
|
938
|
+
});
|
|
939
|
+
return exactRequestOperationKey
|
|
940
|
+
? {
|
|
941
|
+
exact: completedCall(receipts[0] ?? null),
|
|
942
|
+
callOrder: completedCall(receipts[1] ?? null),
|
|
943
|
+
}
|
|
944
|
+
: { exact: null, callOrder: completedCall(receipts[0] ?? null) };
|
|
945
|
+
};
|
|
946
|
+
const deferred: DeferredReceiptWrite[] = [];
|
|
947
|
+
let remoteFinish: RemoteFinishAcknowledgement | null = null;
|
|
948
|
+
let exact: CompletedCall | null = null;
|
|
949
|
+
let callOrder: CompletedCall | null = null;
|
|
950
|
+
/**
|
|
951
|
+
* Finishing inside the execute request is offered only to a call whose
|
|
952
|
+
* slots were both empty and that has no legacy operation key: the owner
|
|
953
|
+
* then writes exactly the bound records this call would. Any other shape
|
|
954
|
+
* (a recorded slot, a legacy operation key, a fresh invocation) declines
|
|
955
|
+
* and the call publishes its own writes, as on main.
|
|
956
|
+
*/
|
|
957
|
+
const executeCall = async (
|
|
958
|
+
readThrough: RuntimeReadThroughGrant | null,
|
|
959
|
+
replay?: FreshExecutionReplay,
|
|
960
|
+
): Promise<T> => {
|
|
961
|
+
// A fresh invocation (#10027) executes exactly as with every switch off:
|
|
962
|
+
// no companion receipt (the pre-binding result receipts it must not
|
|
963
|
+
// answer from), no remote finish and no read-through. It still runs
|
|
964
|
+
// inside the catch below, which saves a result it already received.
|
|
965
|
+
if (replay) companions.clear();
|
|
966
|
+
try {
|
|
967
|
+
if (replay) {
|
|
968
|
+
return await input.execute(
|
|
969
|
+
{
|
|
970
|
+
defer: (write) => {
|
|
971
|
+
deferred.push(write);
|
|
972
|
+
},
|
|
973
|
+
},
|
|
974
|
+
replay,
|
|
975
|
+
);
|
|
976
|
+
}
|
|
977
|
+
return await input.execute({
|
|
978
|
+
defer: (write) => {
|
|
979
|
+
deferred.push(write);
|
|
980
|
+
},
|
|
981
|
+
takeCompanionReceipt: (key) => {
|
|
982
|
+
if (!companions.has(key)) return undefined;
|
|
983
|
+
const receipt = companions.get(key) ?? null;
|
|
984
|
+
companions.delete(key);
|
|
985
|
+
return { receipt };
|
|
986
|
+
},
|
|
987
|
+
...(!exact && !callOrder && !legacyOperationKey
|
|
988
|
+
? {
|
|
989
|
+
finishTargets: {
|
|
990
|
+
operationKey,
|
|
991
|
+
exactRequestOperationKey,
|
|
992
|
+
fingerprints,
|
|
993
|
+
reusable:
|
|
994
|
+
ttl === 0
|
|
995
|
+
? null
|
|
996
|
+
: { prefix: `${cachePrefix}:reusable`, ttlSeconds: ttl },
|
|
997
|
+
},
|
|
998
|
+
}
|
|
999
|
+
: {}),
|
|
1000
|
+
acknowledgeRemoteFinish: (ack) => {
|
|
1001
|
+
remoteFinish = ack;
|
|
1002
|
+
},
|
|
1003
|
+
...(readThrough ? { readThrough } : {}),
|
|
1004
|
+
});
|
|
1005
|
+
} catch (error) {
|
|
1006
|
+
if (isRuntimeReadThroughFallback(error)) {
|
|
1007
|
+
// Nothing executed, so nothing may have been deferred.
|
|
1008
|
+
if (
|
|
1009
|
+
deferred.length > 0 ||
|
|
1010
|
+
(remoteFinish as RemoteFinishAcknowledgement | null) !== null
|
|
1011
|
+
) {
|
|
1012
|
+
throw new Error(
|
|
1013
|
+
'Read-through fallback after the call produced a result.',
|
|
1014
|
+
);
|
|
1015
|
+
}
|
|
1016
|
+
throw error;
|
|
1017
|
+
}
|
|
1018
|
+
// A deferred write is a result the call already received (the provider
|
|
1019
|
+
// answered and was paid) before a later step threw, for example a durable
|
|
1020
|
+
// suspension or a cancelled signal. Before deferral that result receipt
|
|
1021
|
+
// was written at once, so a resumed call reused it instead of paying the
|
|
1022
|
+
// provider again. Keep that: publish what was deferred, then rethrow the
|
|
1023
|
+
// original error unchanged, because it may be suspension control flow.
|
|
1024
|
+
// The operation and reusable pointers are not published, exactly as
|
|
1025
|
+
// before. If this publication itself fails, the call keeps the documented
|
|
1026
|
+
// at-least-once behavior and re-executes on resume.
|
|
1027
|
+
if (deferred.length > 0) {
|
|
1028
|
+
await publishFinishingWrites(deferred).catch(() => undefined);
|
|
1029
|
+
}
|
|
1030
|
+
throw error;
|
|
1031
|
+
}
|
|
1032
|
+
};
|
|
1033
|
+
|
|
1034
|
+
// Local-permit read-through: a policy-0 call whose own result receipt is
|
|
1035
|
+
// planned and keyed by the executing run (no legacy operation key). The
|
|
1036
|
+
// relay reads exactly the keys this layer would have read and executes only
|
|
1037
|
+
// when every one is empty. Any record, including a pre-binding record in
|
|
1038
|
+
// this call's own slot, answers `hit` and executes nothing; the call then
|
|
1039
|
+
// classifies those records below exactly as its own read would, and
|
|
1040
|
+
// read-through is never offered again for it (a fresh invocation included).
|
|
1041
|
+
const readThroughGrant =
|
|
1042
|
+
input.readThrough &&
|
|
1043
|
+
ttl === 0 &&
|
|
1044
|
+
candidateKeys.length === 0 &&
|
|
1045
|
+
legacyOperationKey === null &&
|
|
1046
|
+
companionKeys.length > 0
|
|
1047
|
+
? await input.readThrough.begin([...ownSlotKeys, ...companionKeys])
|
|
1048
|
+
: null;
|
|
1049
|
+
let speculative: { value: T } | null = null;
|
|
1050
|
+
let readThroughFallback: RuntimeReadThroughFallback | null = null;
|
|
1051
|
+
if (readThroughGrant) {
|
|
1052
|
+
// The relay reads the companion receipts before it executes.
|
|
1053
|
+
for (const key of companionKeys) companions.set(key, null);
|
|
1054
|
+
try {
|
|
1055
|
+
speculative = { value: await executeCall(readThroughGrant) };
|
|
1056
|
+
} catch (error) {
|
|
1057
|
+
if (!isRuntimeReadThroughFallback(error)) {
|
|
1058
|
+
await readThroughGrant.settle('done');
|
|
1059
|
+
throw error;
|
|
1060
|
+
}
|
|
1061
|
+
readThroughFallback = error;
|
|
1062
|
+
}
|
|
1063
|
+
await readThroughGrant.settle(
|
|
1064
|
+
speculative || readThroughFallback?.earlierAttemptMayHaveRun
|
|
1065
|
+
? 'done'
|
|
1066
|
+
: 'fallback',
|
|
1067
|
+
);
|
|
1068
|
+
if (!speculative) companions.clear();
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
let candidateManifests: PromiseSettledResult<ReadonlyArray<RuntimeStepReceipt | null> | null> =
|
|
1072
|
+
{ status: 'fulfilled', value: null };
|
|
1073
|
+
if (!speculative) {
|
|
1074
|
+
const [operation, manifests] = await Promise.allSettled([
|
|
1075
|
+
readOperationSlots(readThroughFallback?.receipts ?? null),
|
|
1076
|
+
candidateKeys.length > 0 && input.store.getMany
|
|
1077
|
+
? input.store
|
|
1078
|
+
.getMany([...candidateKeys], { hydrateOutput: false })
|
|
1079
|
+
.then((byKey) => candidateKeys.map((key) => byKey.get(key) ?? null))
|
|
1080
|
+
: Promise.resolve(null),
|
|
1081
|
+
]);
|
|
1082
|
+
if (operation.status === 'rejected') throw operation.reason;
|
|
1083
|
+
({ exact, callOrder } = operation.value);
|
|
1084
|
+
candidateManifests = manifests;
|
|
1085
|
+
}
|
|
842
1086
|
/** Set when a pre-binding record in this call's own slot may not be reused. */
|
|
843
1087
|
let freshExecution = false;
|
|
844
1088
|
// Same-run recovery returns only this call's own recorded result.
|
|
@@ -1069,35 +1313,18 @@ export async function executeWithFreshnessReceipt<T>(input: {
|
|
|
1069
1313
|
return outcome(hit, 'reusable_result', served);
|
|
1070
1314
|
}
|
|
1071
1315
|
}
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
defer: (write) => {
|
|
1079
|
-
deferred.push(write);
|
|
1080
|
-
},
|
|
1081
|
-
},
|
|
1082
|
-
freshExecution ? { freshInvocation: true } : undefined,
|
|
1083
|
-
);
|
|
1084
|
-
} catch (error) {
|
|
1085
|
-
// A deferred write is a result the call already received (the provider
|
|
1086
|
-
// answered and was paid) before a later step threw, for example a durable
|
|
1087
|
-
// suspension or a cancelled signal. Before deferral that result receipt
|
|
1088
|
-
// was written at once, so a resumed call reused it instead of paying the
|
|
1089
|
-
// provider again. Keep that: publish what was deferred, then rethrow the
|
|
1090
|
-
// original error unchanged, because it may be suspension control flow.
|
|
1091
|
-
// The operation and reusable pointers are not published, exactly as
|
|
1092
|
-
// before. If this publication itself fails, the call keeps the documented
|
|
1093
|
-
// at-least-once behavior and re-executes on resume.
|
|
1094
|
-
if (deferred.length > 0) {
|
|
1095
|
-
await publishFinishingWrites(deferred).catch(() => undefined);
|
|
1096
|
-
}
|
|
1097
|
-
throw error;
|
|
1098
|
-
}
|
|
1316
|
+
const value = speculative
|
|
1317
|
+
? speculative.value
|
|
1318
|
+
: await executeCall(
|
|
1319
|
+
null,
|
|
1320
|
+
freshExecution ? { freshInvocation: true } : undefined,
|
|
1321
|
+
);
|
|
1099
1322
|
assertNoSecretTaint(value, 'Freshness call result');
|
|
1100
|
-
|
|
1323
|
+
// The owner that finished remotely stamped the operation with its clock;
|
|
1324
|
+
// reuse that instant so both sides describe the same completion.
|
|
1325
|
+
const remote = remoteFinish as RemoteFinishAcknowledgement | null;
|
|
1326
|
+
const completedAt =
|
|
1327
|
+
input.completedAt?.(value) ?? remote?.completedAtMs ?? now();
|
|
1101
1328
|
if (!Number.isSafeInteger(completedAt) || completedAt < 0) {
|
|
1102
1329
|
throw new Error('Freshness call returned an invalid completion time.');
|
|
1103
1330
|
}
|
|
@@ -1142,10 +1369,23 @@ export async function executeWithFreshnessReceipt<T>(input: {
|
|
|
1142
1369
|
const pointersNeedReferent =
|
|
1143
1370
|
referent !== undefined &&
|
|
1144
1371
|
deferred.some((write) => write.key === referent && !write.bestEffort);
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1372
|
+
// The execution owner may already have committed every one of these
|
|
1373
|
+
// writes inside the execute request. Skip this request only when its
|
|
1374
|
+
// acknowledgement names exactly these keys with exactly these values;
|
|
1375
|
+
// otherwise publish as before (insert-if-absent, so repeating is safe).
|
|
1376
|
+
const finishedRemotely =
|
|
1377
|
+
remote !== null &&
|
|
1378
|
+
!pointersNeedReferent &&
|
|
1379
|
+
remote.covers([...deferred, ...pointers]);
|
|
1380
|
+
if (remote !== null) {
|
|
1381
|
+
input.onRemoteFinish?.(finishedRemotely ? 'acknowledged' : 'not_covered');
|
|
1382
|
+
}
|
|
1383
|
+
if (!finishedRemotely) {
|
|
1384
|
+
await publishFinishingWrites(
|
|
1385
|
+
pointersNeedReferent ? deferred : [...deferred, ...pointers],
|
|
1386
|
+
);
|
|
1387
|
+
if (pointersNeedReferent) await publishFinishingWrites(pointers);
|
|
1388
|
+
}
|
|
1149
1389
|
// A refresh publishes only the newest shared result, not a policy bucket.
|
|
1150
1390
|
if (
|
|
1151
1391
|
(ttl !== 0 || input.publishLatestOnRefresh === true) &&
|
|
@@ -43,6 +43,8 @@ export interface BlockReservingBudgetStateOptions {
|
|
|
43
43
|
export class BlockReservingBudgetStateBackend implements BudgetStateBackend {
|
|
44
44
|
private readonly remaining = new Map<string, number>();
|
|
45
45
|
private chargeTail: Promise<void> = Promise.resolve();
|
|
46
|
+
/** Charges queued or reserving remotely; `tryCharge` never races them. */
|
|
47
|
+
private chargesSettling = 0;
|
|
46
48
|
private readonly now: () => number;
|
|
47
49
|
private readonly sleep: (ms: number) => Promise<void>;
|
|
48
50
|
private readonly jitter: () => number;
|
|
@@ -63,16 +65,69 @@ export class BlockReservingBudgetStateBackend implements BudgetStateBackend {
|
|
|
63
65
|
charges: readonly PlayRunnerBudgetCharge[];
|
|
64
66
|
}): Promise<PlayRunnerBudgetChargeResult> {
|
|
65
67
|
const requestedAtMs = this.now();
|
|
68
|
+
this.chargesSettling += 1;
|
|
66
69
|
const result = this.chargeTail.then(() =>
|
|
67
70
|
this.chargeSerial(input, requestedAtMs),
|
|
68
71
|
);
|
|
69
72
|
this.chargeTail = result.then(
|
|
70
|
-
() =>
|
|
71
|
-
|
|
73
|
+
() => {
|
|
74
|
+
this.chargesSettling -= 1;
|
|
75
|
+
},
|
|
76
|
+
() => {
|
|
77
|
+
this.chargesSettling -= 1;
|
|
78
|
+
},
|
|
72
79
|
);
|
|
73
80
|
return result;
|
|
74
81
|
}
|
|
75
82
|
|
|
83
|
+
/**
|
|
84
|
+
* Draw from the block this process already reserved, or refuse. A refusal
|
|
85
|
+
* leaves every counter untouched; the caller then takes the ordinary
|
|
86
|
+
* `charge` path. Refuses while a charge is settling, because a failed
|
|
87
|
+
* reservation restores the remaining count it saw before this draw.
|
|
88
|
+
*/
|
|
89
|
+
tryCharge(input: {
|
|
90
|
+
rootRunId: string;
|
|
91
|
+
charges: readonly PlayRunnerBudgetCharge[];
|
|
92
|
+
}): boolean {
|
|
93
|
+
if (this.chargesSettling > 0) return false;
|
|
94
|
+
for (const charge of input.charges) {
|
|
95
|
+
if ((this.remaining.get(charge.key) ?? 0) < charge.amount) return false;
|
|
96
|
+
}
|
|
97
|
+
for (const charge of input.charges) {
|
|
98
|
+
this.remaining.set(
|
|
99
|
+
charge.key,
|
|
100
|
+
(this.remaining.get(charge.key) ?? 0) - charge.amount,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Put an undispatched draw back in the reserved block. The durable
|
|
108
|
+
* reservation never changed, so this is exactly the state before the draw.
|
|
109
|
+
* Behind a settling charge it waits for that charge, which may restore an
|
|
110
|
+
* earlier count, and applies after it.
|
|
111
|
+
*/
|
|
112
|
+
refundUndispatched(input: {
|
|
113
|
+
rootRunId: string;
|
|
114
|
+
charges: readonly PlayRunnerBudgetCharge[];
|
|
115
|
+
}): void {
|
|
116
|
+
const apply = () => {
|
|
117
|
+
for (const charge of input.charges) {
|
|
118
|
+
this.remaining.set(
|
|
119
|
+
charge.key,
|
|
120
|
+
(this.remaining.get(charge.key) ?? 0) + charge.amount,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
if (this.chargesSettling === 0) {
|
|
125
|
+
apply();
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
this.chargeTail = this.chargeTail.then(apply, apply);
|
|
129
|
+
}
|
|
130
|
+
|
|
76
131
|
private async chargeSerial(
|
|
77
132
|
input: {
|
|
78
133
|
rootRunId: string;
|
|
@@ -8,6 +8,23 @@ export interface BudgetStateBackend {
|
|
|
8
8
|
rootRunId: string;
|
|
9
9
|
charges: readonly PlayRunnerBudgetCharge[];
|
|
10
10
|
}): Promise<PlayRunnerBudgetChargeResult>;
|
|
11
|
+
/**
|
|
12
|
+
* Charge only from budget this process already reserved. Synchronous and
|
|
13
|
+
* never remote: false when the charge would need a reservation, would
|
|
14
|
+
* breach a limit, or another charge is still settling.
|
|
15
|
+
*/
|
|
16
|
+
tryCharge?(input: {
|
|
17
|
+
rootRunId: string;
|
|
18
|
+
charges: readonly PlayRunnerBudgetCharge[];
|
|
19
|
+
}): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Return a `tryCharge` whose work never dispatched, so local and durable
|
|
22
|
+
* budget state end exactly as if it had not been taken.
|
|
23
|
+
*/
|
|
24
|
+
refundUndispatched?(input: {
|
|
25
|
+
rootRunId: string;
|
|
26
|
+
charges: readonly PlayRunnerBudgetCharge[];
|
|
27
|
+
}): void;
|
|
11
28
|
}
|
|
12
29
|
|
|
13
30
|
export class BudgetStateLimitError extends Error {
|
|
@@ -40,4 +57,30 @@ export class InMemoryBudgetStateBackend implements BudgetStateBackend {
|
|
|
40
57
|
this.counters.set(input.rootRunId, next);
|
|
41
58
|
return { counters: { ...next } };
|
|
42
59
|
}
|
|
60
|
+
|
|
61
|
+
tryCharge(input: {
|
|
62
|
+
rootRunId: string;
|
|
63
|
+
charges: readonly PlayRunnerBudgetCharge[];
|
|
64
|
+
}): boolean {
|
|
65
|
+
const current = this.counters.get(input.rootRunId) ?? {};
|
|
66
|
+
const next = { ...current };
|
|
67
|
+
for (const charge of input.charges) {
|
|
68
|
+
const observed = (next[charge.key] ?? 0) + charge.amount;
|
|
69
|
+
if (observed > charge.limit) return false;
|
|
70
|
+
next[charge.key] = observed;
|
|
71
|
+
}
|
|
72
|
+
this.counters.set(input.rootRunId, next);
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
refundUndispatched(input: {
|
|
77
|
+
rootRunId: string;
|
|
78
|
+
charges: readonly PlayRunnerBudgetCharge[];
|
|
79
|
+
}): void {
|
|
80
|
+
const next = { ...(this.counters.get(input.rootRunId) ?? {}) };
|
|
81
|
+
for (const charge of input.charges) {
|
|
82
|
+
next[charge.key] = Math.max(0, (next[charge.key] ?? 0) - charge.amount);
|
|
83
|
+
}
|
|
84
|
+
this.counters.set(input.rootRunId, next);
|
|
85
|
+
}
|
|
43
86
|
}
|