deepline 0.3.536 → 0.3.538
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 +36 -0
- package/dist/bundling-sources/shared_libs/play-runtime/app-runtime-api.ts +1 -0
- package/dist/bundling-sources/shared_libs/play-runtime/baked-runner-image.ts +330 -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 +2 -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 -3
- 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 +2390 -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-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 +15 -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-4WEVLYK4.js +43 -0
- package/dist/cli/{auth-KCTCUO7P.mjs → auth-KYH7N7RQ.mjs} +5 -5
- package/dist/cli/{billing-4VBGZ3CT.js → billing-4KTUVZ4S.js} +114 -90
- package/dist/cli/{billing-OOJHKLNS.mjs → billing-WK346KAI.mjs} +31 -7
- package/dist/cli/{chunk-4E5JXZ3W.js → chunk-25RGHJQL.js} +15 -15
- package/dist/cli/{chunk-MSP4DTSD.js → chunk-34PLG5OE.js} +13 -13
- package/dist/cli/{chunk-DQHUJMCL.mjs → chunk-35OCRH5C.mjs} +5 -5
- package/dist/cli/{chunk-2B4IOGFD.js → chunk-3I5TU5XE.js} +292 -279
- package/dist/cli/{chunk-Z7UIOGW4.mjs → chunk-4KZF6OYY.mjs} +3 -3
- package/dist/cli/{chunk-2YWJS76L.mjs → chunk-4NEOGPKD.mjs} +1 -1
- package/dist/cli/{chunk-LZXZQPA2.mjs → chunk-4WQR4DG4.mjs} +1 -1
- package/dist/cli/{chunk-KWYGWO4Y.js → chunk-5VSPRRTI.js} +7 -7
- package/dist/cli/{chunk-ANUKBZ6W.mjs → chunk-6JG3MRTM.mjs} +2 -2
- package/dist/cli/{chunk-VBDZIMWD.js → chunk-6SLQLWWL.js} +26 -26
- package/dist/cli/{chunk-E24UGPGG.mjs → chunk-7XDJKIM2.mjs} +4 -4
- package/dist/cli/{chunk-OUDTOMZC.mjs → chunk-A6E7TCK2.mjs} +5 -5
- package/dist/cli/{chunk-ZZ4MF2GI.js → chunk-ADLDU2QG.js} +6 -6
- package/dist/cli/{chunk-XVZRJJDS.mjs → chunk-ALARRSMZ.mjs} +3 -3
- package/dist/cli/{chunk-HWDKRN7L.js → chunk-B2BTBHX2.js} +3 -3
- package/dist/cli/{chunk-ZISEH7OL.js → chunk-BBS2BLPV.js} +16 -16
- package/dist/cli/{chunk-YRLAUGQY.mjs → chunk-CLO562KG.mjs} +3 -1
- package/dist/cli/{chunk-CXTW7SRW.mjs → chunk-E7D37PLM.mjs} +8 -0
- package/dist/cli/{chunk-SLPJ3LXI.js → chunk-ESX3H7Q7.js} +22 -22
- package/dist/cli/{chunk-AB2GBOD4.mjs → chunk-FJ5KPGCS.mjs} +10 -10
- package/dist/cli/{chunk-DUYAMLOC.mjs → chunk-FWE37N2P.mjs} +1 -1
- package/dist/cli/{chunk-3PCYV6ZJ.mjs → chunk-ICTA5ICE.mjs} +1 -1
- package/dist/cli/{chunk-ZILSRDIN.js → chunk-IMLFB66A.js} +3 -3
- package/dist/cli/{chunk-PNY3BWHP.mjs → chunk-IP3XJTWA.mjs} +2 -2
- package/dist/cli/{chunk-6VUGBQZQ.js → chunk-JCLRSMUP.js} +5 -5
- package/dist/cli/{chunk-YBMHOH6H.mjs → chunk-JPEBKN3K.mjs} +2 -2
- package/dist/cli/{chunk-GF32SBST.mjs → chunk-JTOV5RUA.mjs} +1 -1
- package/dist/cli/{chunk-Q5CXV3E3.js → chunk-KCUQNH4K.js} +2 -2
- package/dist/cli/{chunk-DA2I2M7D.mjs → chunk-KNIYEZVV.mjs} +2 -2
- package/dist/cli/{chunk-CWSLUNA6.js → chunk-LPTBHNGB.js} +7 -5
- package/dist/cli/{chunk-WC5M3JXE.mjs → chunk-M3NCXGNN.mjs} +2 -2
- package/dist/cli/{chunk-TX5TUHJN.js → chunk-MNBIGX6O.js} +10 -10
- package/dist/cli/{chunk-ZEV2Y56V.js → chunk-MYSPQUAO.js} +44 -44
- package/dist/cli/{chunk-KPQHUVYW.mjs → chunk-N2BCPZVR.mjs} +5 -5
- package/dist/cli/{chunk-XBAH3TKU.js → chunk-N5HSG4OV.js} +5 -5
- package/dist/cli/{chunk-GTVZKY3F.js → chunk-O765M4LT.js} +2 -2
- package/dist/cli/{chunk-2FCSUOYN.js → chunk-OOLBE6FR.js} +53 -53
- package/dist/cli/{chunk-W7AEIGZR.js → chunk-P3DP56HB.js} +41 -41
- package/dist/cli/{chunk-LN6RXDTB.js → chunk-PQKD5DMT.js} +19 -19
- package/dist/cli/{chunk-GHLDCLSC.mjs → chunk-T6GPTJ5F.mjs} +1 -1
- package/dist/cli/{chunk-XJD2GYUP.mjs → chunk-TWWYCJJW.mjs} +5 -5
- package/dist/cli/{chunk-BASLYGXK.mjs → chunk-UE7JHAZ2.mjs} +29 -16
- package/dist/cli/{chunk-Q4MNUOS4.js → chunk-UEZNIPFO.js} +107 -107
- package/dist/cli/{chunk-RVSSQPQO.mjs → chunk-VCHV4LS3.mjs} +1 -1
- package/dist/cli/{chunk-LDGZFDUB.js → chunk-VIFLWQXS.js} +2 -2
- package/dist/cli/{chunk-6BBGUQJJ.js → chunk-XTYHZEJ2.js} +8 -0
- package/dist/cli/{chunk-CIDN5K23.js → chunk-YQURGSRB.js} +1 -1
- package/dist/cli/{chunk-Y2NTPYGS.js → chunk-YZ2ZPHWR.js} +9 -9
- package/dist/cli/{chunk-HAJQGP5V.mjs → chunk-ZDLDCXY3.mjs} +2 -2
- package/dist/cli/{chunk-JUHDJNSI.mjs → chunk-ZU6JBRV5.mjs} +4 -4
- package/dist/cli/command-docs.js +2 -2
- package/dist/cli/command-docs.mjs +1 -1
- package/dist/cli/{csv-P3HLK4GO.mjs → csv-2YKLUKDR.mjs} +4 -4
- package/dist/cli/{csv-2V2DIFNG.js → csv-LM7FBECI.js} +13 -13
- package/dist/cli/{db-ZYOY3QRQ.mjs → db-234UBWJH.mjs} +6 -6
- package/dist/cli/{db-MREAQVW6.js → db-5PMA4IVW.js} +27 -27
- package/dist/cli/describe-C7GOSGYB.js +49 -0
- package/dist/cli/{describe-IKZFOADR.mjs → describe-W26LW5O7.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-H5IJ7QSQ.js → executions-3S736GXX.js} +8 -8
- package/dist/cli/{executions-KTSY5M34.mjs → executions-GD7GXTJM.mjs} +6 -6
- package/dist/cli/{feedback-724QK3Z5.js → feedback-BUWTDY4R.js} +14 -14
- package/dist/cli/{feedback-WWJRIXEU.mjs → feedback-GBV5HJJZ.mjs} +6 -6
- package/dist/cli/git-YG36MROC.js +25 -0
- package/dist/cli/{git-R5VRG7X6.mjs → git-YXJGTDQL.mjs} +5 -5
- package/dist/cli/index.js +143 -143
- package/dist/cli/index.mjs +42 -42
- package/dist/cli/{legacy-noops-PWZ5XLAO.mjs → legacy-noops-UJM6GBNP.mjs} +6 -6
- package/dist/cli/{legacy-noops-C6GDEGPU.js → legacy-noops-ZSF64V2U.js} +9 -9
- package/dist/cli/{monitors-WQSKMHAP.mjs → monitors-UERUQ6CT.mjs} +7 -7
- package/dist/cli/{monitors-VAVQ4WCE.js → monitors-XQ2GNH6D.js} +100 -100
- package/dist/cli/{org-MAL4QPVF.mjs → org-CP4VLH6M.mjs} +6 -6
- package/dist/cli/{org-CWWRWXZQ.js → org-IF6MPVNR.js} +86 -86
- package/dist/cli/{play-WJOWK5PQ.js → play-2NHQO5MO.js} +317 -317
- package/dist/cli/{play-ILW6MSNG.mjs → play-ZWDYYVPI.mjs} +17 -17
- package/dist/cli/{providers-4V2NMLZZ.mjs → providers-2CVIATU4.mjs} +6 -6
- package/dist/cli/{providers-W662ANCH.js → providers-LOPXQN56.js} +9 -9
- package/dist/cli/{quickstart-C22BR743.mjs → quickstart-KVCFXLID.mjs} +4 -4
- package/dist/cli/{quickstart-BW6EBUYX.js → quickstart-N4WDEQZY.js} +6 -6
- package/dist/cli/{run-KW7KW4FX.js → run-BDT5LK3I.js} +42 -42
- package/dist/cli/{run-SPAU5IMW.mjs → run-RUXHYWLF.mjs} +22 -22
- package/dist/cli/search-ATDDVADT.js +32 -0
- package/dist/cli/{search-XPC75HLM.mjs → search-BFUTNPA5.mjs} +7 -7
- package/dist/cli/{search-output-VJYE7USJ.js → search-output-CTXI2QZU.js} +6 -6
- package/dist/cli/{search-output-JKYJSEPM.mjs → search-output-WUGH4PZA.mjs} +4 -4
- package/dist/cli/{secrets-YB57KGSB.js → secrets-BZJZYFOU.js} +15 -15
- package/dist/cli/{secrets-OJG73GXQ.mjs → secrets-Z4XQVTC6.mjs} +6 -6
- package/dist/cli/sessions-HNU35AFM.js +22 -0
- package/dist/cli/{sessions-7ZL37BW4.mjs → sessions-VZXR3MQZ.mjs} +6 -6
- package/dist/cli/{settings-RKSQB7FA.mjs → settings-VYEZFR4L.mjs} +12 -12
- package/dist/cli/{settings-A7DD2ZYE.js → settings-WBYA4JMM.js} +85 -85
- package/dist/cli/{setup-S3K5YU62.js → setup-FS2RMSS2.js} +49 -49
- package/dist/cli/{setup-NXDUTITY.mjs → setup-H7KCI4SO.mjs} +9 -9
- package/dist/cli/skills-VPIURCAZ.js +36 -0
- package/dist/cli/{skills-VMFAIDO6.mjs → skills-YQNSJUUG.mjs} +7 -7
- package/dist/cli/tools-273GS5HU.js +56 -0
- package/dist/cli/{tools-52NTHBSJ.mjs → tools-ZF6SVEGW.mjs} +16 -16
- package/dist/cli/{update-SAKBEKMV.mjs → update-RKQNNJM4.mjs} +9 -9
- package/dist/cli/update-TOD3XY55.js +34 -0
- package/dist/cli/{views-ACRV7MD6.mjs → views-JHHOTWDU.mjs} +6 -6
- package/dist/cli/{views-C3GJOBDG.js → views-KN7FEDGT.js} +21 -21
- package/dist/cli/{workflow-ZNWCZEGN.js → workflow-2WHSKTTV.js} +17 -17
- package/dist/cli/{workflow-CS22E7TC.mjs → workflow-AAYLCLWA.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.mts → compiler-manifest-Bes3OVA3.d.mts} +1 -1
- package/dist/{compiler-manifest-BHNC4RL-.d.ts → 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 +120 -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-6XQKFOLG.js +0 -43
- package/dist/cli/describe-WW3OQRLP.js +0 -49
- package/dist/cli/git-PHYRD56H.js +0 -25
- package/dist/cli/search-HLE54W5G.js +0 -32
- package/dist/cli/sessions-VEBYZIEU.js +0 -22
- package/dist/cli/skills-NVQ744CN.js +0 -36
- package/dist/cli/tools-SBDUL3WB.js +0 -56
- package/dist/cli/update-HHVFZPCV.js +0 -34
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { stableStringify } from '../plays/row-identity';
|
|
3
|
+
import {
|
|
4
|
+
legacyRawFromToolResponseRawV2,
|
|
5
|
+
providerMetaFromToolResponseRawV2,
|
|
6
|
+
type ToolResponseView,
|
|
7
|
+
} from '../plays/tool-response-contract';
|
|
8
|
+
import type {
|
|
9
|
+
ToolResultBilling,
|
|
10
|
+
ToolResultExecutionMetadata,
|
|
11
|
+
ToolResultMetadataInput,
|
|
12
|
+
} from '../plays/tool-result-types';
|
|
13
|
+
import { isCompletedSuccessfulAnswer } from './completed-answer';
|
|
14
|
+
import { completedCallForResult } from './freshness-receipt-execution';
|
|
15
|
+
import { assertRuntimeReceiptOutputWithinLimit } from './output-size-limits';
|
|
16
|
+
import { stampToolResultReceiptKey } from './tool-call/runtime-step-receipts-adapter';
|
|
17
|
+
import {
|
|
18
|
+
createToolExecuteResult,
|
|
19
|
+
isToolExecuteResult,
|
|
20
|
+
parseToolExecuteResponse,
|
|
21
|
+
serializeToolExecuteResult,
|
|
22
|
+
type ToolExecuteResult,
|
|
23
|
+
} from './tool-result';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Finish-in-execute: the finishing receipt writes of one direct tool call,
|
|
27
|
+
* committed by the trusted execution owner inside the execute request.
|
|
28
|
+
*
|
|
29
|
+
* The runner already knows every key it will write before it dispatches the
|
|
30
|
+
* provider call. It sends them as an optional plan on the execute request.
|
|
31
|
+
* After a successful provider answer the execution gateway builds the exact
|
|
32
|
+
* rows the runner would have built, with the functions in this module, and
|
|
33
|
+
* commits them through the receipt gateway's own completion dispatcher. Its
|
|
34
|
+
* response carries an acknowledgement naming each key and the digest of the
|
|
35
|
+
* value it committed. The runner skips its own completion request only when
|
|
36
|
+
* that acknowledgement names exactly the keys and values it would have
|
|
37
|
+
* written; anything else (old gateway, failure, partial commit, a value that
|
|
38
|
+
* differs) keeps today's runner-owned write, which is insert-if-absent.
|
|
39
|
+
*
|
|
40
|
+
* Both sides call these functions, so the stored rows are the same rows by
|
|
41
|
+
* construction. The digest is the proof, not the mechanism.
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Request header carrying the plan. A header, not body metadata: the execute
|
|
46
|
+
* body (and so every relay's durable-invocation fingerprint, old or new) stays
|
|
47
|
+
* byte-identical, so a redelivery that crosses relay versions cannot conflict.
|
|
48
|
+
* The supporting relay removes it before the upstream hop.
|
|
49
|
+
*/
|
|
50
|
+
export const RUNTIME_RECEIPT_FINISH_PLAN_HEADER =
|
|
51
|
+
'x-deepline-runtime-receipt-finish-plan';
|
|
52
|
+
/**
|
|
53
|
+
* Encoded plan ceiling. A larger plan (in practice a large redacted request
|
|
54
|
+
* input) is not sent: the runner keeps its own write, and no request grows
|
|
55
|
+
* toward the body or header limits.
|
|
56
|
+
*/
|
|
57
|
+
export const RUNTIME_RECEIPT_FINISH_PLAN_MAX_HEADER_BYTES = 6 * 1024;
|
|
58
|
+
/** Response header minted only by the execution gateway after a commit. */
|
|
59
|
+
export const EXECUTION_GATEWAY_RECEIPT_FINISH_HEADER =
|
|
60
|
+
'x-deepline-runtime-receipt-finish';
|
|
61
|
+
|
|
62
|
+
const MAX_PLAN_KEY_BYTES = 1024;
|
|
63
|
+
const MAX_ACK_HEADER_LENGTH = 16 * 1024;
|
|
64
|
+
|
|
65
|
+
/** The wire plan, exactly as the runner sends it. */
|
|
66
|
+
export type RuntimeReceiptFinishPlan = {
|
|
67
|
+
tool_id: string;
|
|
68
|
+
/** The attempt the runner writes its receipts under (`runAttempt`). */
|
|
69
|
+
run_attempt?: number;
|
|
70
|
+
/**
|
|
71
|
+
* The credential identity the runner prepared this call under. The relay
|
|
72
|
+
* finishes only when it equals the request's `execution_auth_scope_digest`
|
|
73
|
+
* beside a `durable_call_receipt_key`: the execute route then refuses
|
|
74
|
+
* (409 AUTH_SCOPE_CHANGED) unless that identity is still current, so a
|
|
75
|
+
* successful answer is proof it ran under exactly this credential.
|
|
76
|
+
*/
|
|
77
|
+
execution_auth_scope_digest: string;
|
|
78
|
+
result: {
|
|
79
|
+
/** Result receipt key: the invocation-scoped key the runner would write. */
|
|
80
|
+
key: string;
|
|
81
|
+
execution: ToolResultExecutionMetadata;
|
|
82
|
+
/** Runner-resolved tool metadata, used when the response carries none. */
|
|
83
|
+
metadata: ToolResultMetadataInput;
|
|
84
|
+
/** Already-redacted request input for the fallback call evidence. */
|
|
85
|
+
call_evidence_input?: unknown;
|
|
86
|
+
};
|
|
87
|
+
/** Immutable `--from-run` replay pointer; best effort, as today. */
|
|
88
|
+
source_run_manifest?: { key: string; output: unknown };
|
|
89
|
+
/**
|
|
90
|
+
* #10008's operation slots and binding. `exact_request_key` is the
|
|
91
|
+
* exact-request slot when the call has one; both slots hold the same bound
|
|
92
|
+
* record `{ ...completed, ...fingerprints }`, built exactly as the runner
|
|
93
|
+
* builds it. A plan without a binding is not finishable (no unbound record
|
|
94
|
+
* is ever written by the owner).
|
|
95
|
+
*/
|
|
96
|
+
operation: {
|
|
97
|
+
key: string;
|
|
98
|
+
exact_request_key?: string;
|
|
99
|
+
fingerprints: { requestFingerprint: string; inputFingerprint?: string };
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* The freshness policy bucket prefix. The bucket key depends on the
|
|
103
|
+
* completion time, so the owner derives it with `reusableBucketKey`.
|
|
104
|
+
*/
|
|
105
|
+
reusable?: { prefix: string; ttl_seconds: number | null };
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export type RuntimeReceiptFinishWrite = {
|
|
109
|
+
key: string;
|
|
110
|
+
output: unknown;
|
|
111
|
+
bestEffort?: true;
|
|
112
|
+
sourceRunManifest?: true;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export type RuntimeReceiptFinishAck = {
|
|
116
|
+
/** The completion time the owner used for the operation receipt. */
|
|
117
|
+
completedAtMs: number;
|
|
118
|
+
receipts: Array<{ key: string; digest: string }>;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/** Value digest shared by owner and runner. JSON-normalized, key-sorted. */
|
|
122
|
+
export function runtimeReceiptFinishDigest(output: unknown): string {
|
|
123
|
+
return createHash('sha256')
|
|
124
|
+
.update(stableStringify(JSON.parse(JSON.stringify(output ?? null))))
|
|
125
|
+
.digest('hex');
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Same bucket keys `executeWithFreshnessReceipt` publishes. */
|
|
129
|
+
export function reusableBucketKey(input: {
|
|
130
|
+
prefix: string;
|
|
131
|
+
ttlSeconds: number | null;
|
|
132
|
+
completedAtMs: number;
|
|
133
|
+
}): string {
|
|
134
|
+
return input.ttlSeconds === null
|
|
135
|
+
? `${input.prefix}:indefinite`
|
|
136
|
+
: `${input.prefix}:${input.ttlSeconds}:${Math.floor(
|
|
137
|
+
input.completedAtMs / (input.ttlSeconds * 1000),
|
|
138
|
+
)}`;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The completion time a tool result reports for itself: the provider
|
|
143
|
+
* operation's own completion, else the execution metadata. Undefined means
|
|
144
|
+
* the caller's clock decides.
|
|
145
|
+
*/
|
|
146
|
+
export function toolResultCompletedAtMs(value: unknown): number | undefined {
|
|
147
|
+
if (!isToolExecuteResult(value)) return undefined;
|
|
148
|
+
const raw = value.toolResponse.raw as {
|
|
149
|
+
operation?: { completed_at_ms?: unknown };
|
|
150
|
+
} | null;
|
|
151
|
+
const completedAt = raw?.operation?.completed_at_ms;
|
|
152
|
+
return typeof completedAt === 'number' && Number.isSafeInteger(completedAt)
|
|
153
|
+
? completedAt
|
|
154
|
+
: value._metadata.execution.completedAt;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** A public `{status, toolResponse}` envelope nested in a parsed result. */
|
|
158
|
+
export function publicToolResponseEnvelope(value: unknown): {
|
|
159
|
+
status: string;
|
|
160
|
+
raw: unknown;
|
|
161
|
+
rawV2?: unknown;
|
|
162
|
+
view?: ToolResponseView;
|
|
163
|
+
meta?: Record<string, unknown>;
|
|
164
|
+
} | null {
|
|
165
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
const record = value as Record<string, unknown>;
|
|
169
|
+
if (typeof record.status !== 'string') return null;
|
|
170
|
+
const toolResponse = record.toolResponse;
|
|
171
|
+
if (
|
|
172
|
+
!toolResponse ||
|
|
173
|
+
typeof toolResponse !== 'object' ||
|
|
174
|
+
Array.isArray(toolResponse)
|
|
175
|
+
) {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
const response = toolResponse as Record<string, unknown>;
|
|
179
|
+
const rawV2 = Object.prototype.hasOwnProperty.call(response, 'rawV2')
|
|
180
|
+
? response.rawV2
|
|
181
|
+
: undefined;
|
|
182
|
+
const view =
|
|
183
|
+
response.view === 'data' || response.view === 'rawV2'
|
|
184
|
+
? response.view
|
|
185
|
+
: undefined;
|
|
186
|
+
if (
|
|
187
|
+
!Object.prototype.hasOwnProperty.call(response, 'raw') &&
|
|
188
|
+
rawV2 === undefined
|
|
189
|
+
) {
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
const providerMeta = providerMetaFromToolResponseRawV2(
|
|
193
|
+
rawV2,
|
|
194
|
+
view ?? 'rawV2',
|
|
195
|
+
);
|
|
196
|
+
const toolResponseMeta =
|
|
197
|
+
response.meta &&
|
|
198
|
+
typeof response.meta === 'object' &&
|
|
199
|
+
!Array.isArray(response.meta)
|
|
200
|
+
? (response.meta as Record<string, unknown>)
|
|
201
|
+
: {};
|
|
202
|
+
const responseMeta =
|
|
203
|
+
response.responseMeta &&
|
|
204
|
+
typeof response.responseMeta === 'object' &&
|
|
205
|
+
!Array.isArray(response.responseMeta)
|
|
206
|
+
? (response.responseMeta as Record<string, unknown>)
|
|
207
|
+
: {};
|
|
208
|
+
return {
|
|
209
|
+
status: record.status,
|
|
210
|
+
raw: Object.prototype.hasOwnProperty.call(response, 'raw')
|
|
211
|
+
? response.raw
|
|
212
|
+
: legacyRawFromToolResponseRawV2(rawV2, view ?? 'rawV2', responseMeta),
|
|
213
|
+
...(rawV2 !== undefined ? { rawV2 } : {}),
|
|
214
|
+
...(view ? { view } : {}),
|
|
215
|
+
...(Object.keys({ ...toolResponseMeta, ...providerMeta, ...responseMeta })
|
|
216
|
+
.length > 0
|
|
217
|
+
? { meta: { ...toolResponseMeta, ...providerMeta, ...responseMeta } }
|
|
218
|
+
: {}),
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* The live ToolExecuteResult for a parsed execute response that is not
|
|
224
|
+
* itself a ToolExecuteResult. The runner's result wrapper and the execution
|
|
225
|
+
* owner both build it here.
|
|
226
|
+
*/
|
|
227
|
+
export function buildLiveToolExecuteResult(input: {
|
|
228
|
+
status: string;
|
|
229
|
+
jobId?: string;
|
|
230
|
+
result: unknown;
|
|
231
|
+
toolResponse?: {
|
|
232
|
+
raw?: unknown;
|
|
233
|
+
rawV2?: unknown;
|
|
234
|
+
view?: 'data' | 'rawV2';
|
|
235
|
+
meta?: Record<string, unknown>;
|
|
236
|
+
};
|
|
237
|
+
metadata: ToolResultMetadataInput;
|
|
238
|
+
execution: ToolResultExecutionMetadata;
|
|
239
|
+
meta?: Record<string, unknown>;
|
|
240
|
+
billing?: ToolResultBilling;
|
|
241
|
+
/** Redacted request input, read only when call evidence is missing. */
|
|
242
|
+
callEvidenceInput?: () => unknown;
|
|
243
|
+
}): ToolExecuteResult {
|
|
244
|
+
const publicToolResult = publicToolResponseEnvelope(input.result);
|
|
245
|
+
return createToolExecuteResult({
|
|
246
|
+
status: publicToolResult?.status ?? input.status,
|
|
247
|
+
jobId: input.jobId,
|
|
248
|
+
result: publicToolResult
|
|
249
|
+
? {
|
|
250
|
+
data: publicToolResult.raw,
|
|
251
|
+
...(publicToolResult.meta ? { meta: publicToolResult.meta } : {}),
|
|
252
|
+
}
|
|
253
|
+
: input.result,
|
|
254
|
+
response: publicToolResult
|
|
255
|
+
? {
|
|
256
|
+
...(Object.prototype.hasOwnProperty.call(publicToolResult, 'rawV2')
|
|
257
|
+
? { rawV2: publicToolResult.rawV2 }
|
|
258
|
+
: {}),
|
|
259
|
+
...(publicToolResult.view ? { view: publicToolResult.view } : {}),
|
|
260
|
+
...(publicToolResult.meta ? { meta: publicToolResult.meta } : {}),
|
|
261
|
+
}
|
|
262
|
+
: input.toolResponse,
|
|
263
|
+
metadata: input.metadata,
|
|
264
|
+
execution: input.execution,
|
|
265
|
+
meta: {
|
|
266
|
+
...input.meta,
|
|
267
|
+
...(input.execution.source === 'live' &&
|
|
268
|
+
input.callEvidenceInput &&
|
|
269
|
+
!input.meta?.callEvidence
|
|
270
|
+
? {
|
|
271
|
+
callEvidence: {
|
|
272
|
+
input: input.callEvidenceInput(),
|
|
273
|
+
providerRequestsUnavailable:
|
|
274
|
+
'This response did not retain provider HTTP evidence.',
|
|
275
|
+
},
|
|
276
|
+
}
|
|
277
|
+
: {}),
|
|
278
|
+
},
|
|
279
|
+
billing: input.billing,
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
284
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function boundedKey(value: unknown): string | null {
|
|
288
|
+
if (typeof value !== 'string') return null;
|
|
289
|
+
const key = value.trim();
|
|
290
|
+
if (!key || key !== value || Buffer.byteLength(key) > MAX_PLAN_KEY_BYTES) {
|
|
291
|
+
return null;
|
|
292
|
+
}
|
|
293
|
+
return key;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Validate the plan's shape. Authorization (key scope, capability, run and
|
|
298
|
+
* attempt) is the receiver's job and is not decided here.
|
|
299
|
+
*/
|
|
300
|
+
export function parseRuntimeReceiptFinishPlan(
|
|
301
|
+
value: unknown,
|
|
302
|
+
): RuntimeReceiptFinishPlan | null {
|
|
303
|
+
if (!isRecord(value)) return null;
|
|
304
|
+
const toolId = boundedKey(value.tool_id);
|
|
305
|
+
if (!toolId || !isRecord(value.result) || !isRecord(value.operation)) {
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
const resultKey = boundedKey(value.result.key);
|
|
309
|
+
const operationKey = boundedKey(value.operation.key);
|
|
310
|
+
const exactRequestKey =
|
|
311
|
+
value.operation.exact_request_key === undefined
|
|
312
|
+
? null
|
|
313
|
+
: boundedKey(value.operation.exact_request_key);
|
|
314
|
+
const fingerprints = parseFingerprints(value.operation.fingerprints);
|
|
315
|
+
const execution = value.result.execution;
|
|
316
|
+
const metadata = value.result.metadata;
|
|
317
|
+
if (
|
|
318
|
+
!resultKey ||
|
|
319
|
+
!operationKey ||
|
|
320
|
+
resultKey === operationKey ||
|
|
321
|
+
!fingerprints ||
|
|
322
|
+
(value.operation.exact_request_key !== undefined &&
|
|
323
|
+
(!exactRequestKey ||
|
|
324
|
+
exactRequestKey === operationKey ||
|
|
325
|
+
exactRequestKey === resultKey)) ||
|
|
326
|
+
!isRecord(execution) ||
|
|
327
|
+
execution.source !== 'live' ||
|
|
328
|
+
execution.cached !== false ||
|
|
329
|
+
!isRecord(metadata) ||
|
|
330
|
+
metadata.toolId !== toolId
|
|
331
|
+
) {
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
334
|
+
let sourceRunManifest: RuntimeReceiptFinishPlan['source_run_manifest'];
|
|
335
|
+
if (value.source_run_manifest !== undefined) {
|
|
336
|
+
if (!isRecord(value.source_run_manifest)) return null;
|
|
337
|
+
const key = boundedKey(value.source_run_manifest.key);
|
|
338
|
+
if (!key || !isRecord(value.source_run_manifest.output)) return null;
|
|
339
|
+
sourceRunManifest = { key, output: value.source_run_manifest.output };
|
|
340
|
+
}
|
|
341
|
+
const authScopeDigest = boundedKey(value.execution_auth_scope_digest);
|
|
342
|
+
if (!authScopeDigest) return null;
|
|
343
|
+
const runAttempt = value.run_attempt;
|
|
344
|
+
if (
|
|
345
|
+
runAttempt !== undefined &&
|
|
346
|
+
!(Number.isSafeInteger(runAttempt) && (runAttempt as number) >= 0)
|
|
347
|
+
) {
|
|
348
|
+
return null;
|
|
349
|
+
}
|
|
350
|
+
let reusable: RuntimeReceiptFinishPlan['reusable'];
|
|
351
|
+
if (value.reusable !== undefined) {
|
|
352
|
+
if (!isRecord(value.reusable)) return null;
|
|
353
|
+
const prefix = boundedKey(value.reusable.prefix);
|
|
354
|
+
const ttl = value.reusable.ttl_seconds;
|
|
355
|
+
if (
|
|
356
|
+
!prefix ||
|
|
357
|
+
!(ttl === null || (Number.isSafeInteger(ttl) && (ttl as number) > 0))
|
|
358
|
+
) {
|
|
359
|
+
return null;
|
|
360
|
+
}
|
|
361
|
+
reusable = { prefix, ttl_seconds: ttl as number | null };
|
|
362
|
+
}
|
|
363
|
+
return {
|
|
364
|
+
tool_id: toolId,
|
|
365
|
+
...(runAttempt !== undefined ? { run_attempt: runAttempt as number } : {}),
|
|
366
|
+
execution_auth_scope_digest: authScopeDigest,
|
|
367
|
+
result: {
|
|
368
|
+
key: resultKey,
|
|
369
|
+
execution: execution as ToolResultExecutionMetadata,
|
|
370
|
+
metadata: metadata as ToolResultMetadataInput,
|
|
371
|
+
...(Object.prototype.hasOwnProperty.call(
|
|
372
|
+
value.result,
|
|
373
|
+
'call_evidence_input',
|
|
374
|
+
)
|
|
375
|
+
? { call_evidence_input: value.result.call_evidence_input }
|
|
376
|
+
: {}),
|
|
377
|
+
},
|
|
378
|
+
...(sourceRunManifest ? { source_run_manifest: sourceRunManifest } : {}),
|
|
379
|
+
operation: {
|
|
380
|
+
key: operationKey,
|
|
381
|
+
...(exactRequestKey ? { exact_request_key: exactRequestKey } : {}),
|
|
382
|
+
fingerprints: fingerprints!,
|
|
383
|
+
},
|
|
384
|
+
...(reusable ? { reusable } : {}),
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* #10008's binding as the runner records it: `requestFingerprint` always,
|
|
390
|
+
* `inputFingerprint` only when the runner has one. Anything else: null.
|
|
391
|
+
*/
|
|
392
|
+
function parseFingerprints(
|
|
393
|
+
value: unknown,
|
|
394
|
+
): RuntimeReceiptFinishPlan['operation']['fingerprints'] | null {
|
|
395
|
+
if (!isRecord(value)) return null;
|
|
396
|
+
const keys = Object.keys(value);
|
|
397
|
+
const request = boundedKey(value.requestFingerprint);
|
|
398
|
+
if (!request) return null;
|
|
399
|
+
if (
|
|
400
|
+
keys.some(
|
|
401
|
+
(key) => key !== 'requestFingerprint' && key !== 'inputFingerprint',
|
|
402
|
+
)
|
|
403
|
+
) {
|
|
404
|
+
return null;
|
|
405
|
+
}
|
|
406
|
+
if (value.inputFingerprint === undefined) {
|
|
407
|
+
return { requestFingerprint: request };
|
|
408
|
+
}
|
|
409
|
+
const inputFingerprint = boundedKey(value.inputFingerprint);
|
|
410
|
+
if (!inputFingerprint) return null;
|
|
411
|
+
return { requestFingerprint: request, inputFingerprint };
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/** Every key the plan may write, for the receiver's scope check. */
|
|
415
|
+
export function runtimeReceiptFinishPlanKeyPrefixes(
|
|
416
|
+
plan: RuntimeReceiptFinishPlan,
|
|
417
|
+
): string[] {
|
|
418
|
+
return [
|
|
419
|
+
plan.result.key,
|
|
420
|
+
plan.operation.key,
|
|
421
|
+
...(plan.operation.exact_request_key
|
|
422
|
+
? [plan.operation.exact_request_key]
|
|
423
|
+
: []),
|
|
424
|
+
...(plan.source_run_manifest ? [plan.source_run_manifest.key] : []),
|
|
425
|
+
...(plan.reusable ? [`${plan.reusable.prefix}:`] : []),
|
|
426
|
+
];
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* The rows the runner would write for this successful response, built with
|
|
431
|
+
* the runner's own functions. Null when the response is not a final answer
|
|
432
|
+
* the owner may finish; the runner then writes as it does today.
|
|
433
|
+
*/
|
|
434
|
+
export function buildRuntimeReceiptFinishWrites(input: {
|
|
435
|
+
plan: RuntimeReceiptFinishPlan;
|
|
436
|
+
responseBody: unknown;
|
|
437
|
+
now: () => number;
|
|
438
|
+
}): { writes: RuntimeReceiptFinishWrite[]; completedAtMs: number } | null {
|
|
439
|
+
if (!isRecord(input.responseBody)) return null;
|
|
440
|
+
const { plan } = input;
|
|
441
|
+
const parsed = parseToolExecuteResponse(plan.tool_id, input.responseBody);
|
|
442
|
+
// Only a final provider answer is finished here. Anything else (pending,
|
|
443
|
+
// failed, running) stays with the runner, which may still poll or retry.
|
|
444
|
+
if (parsed.status !== 'completed' && parsed.status !== 'no_result') {
|
|
445
|
+
return null;
|
|
446
|
+
}
|
|
447
|
+
// A result already shaped as a ToolExecuteResult takes the runner's
|
|
448
|
+
// separate merge branch; it is never finished here.
|
|
449
|
+
if (isToolExecuteResult(parsed.result)) return null;
|
|
450
|
+
const callEvidenceInput = Object.prototype.hasOwnProperty.call(
|
|
451
|
+
plan.result,
|
|
452
|
+
'call_evidence_input',
|
|
453
|
+
)
|
|
454
|
+
? () => plan.result.call_evidence_input
|
|
455
|
+
: undefined;
|
|
456
|
+
const live = buildLiveToolExecuteResult({
|
|
457
|
+
status: parsed.status,
|
|
458
|
+
jobId: parsed.jobId,
|
|
459
|
+
result: parsed.result,
|
|
460
|
+
toolResponse: parsed.toolResponse,
|
|
461
|
+
metadata: parsed.metadata ?? plan.result.metadata,
|
|
462
|
+
execution: plan.result.execution,
|
|
463
|
+
meta: parsed.meta,
|
|
464
|
+
billing: parsed.billing,
|
|
465
|
+
...(callEvidenceInput ? { callEvidenceInput } : {}),
|
|
466
|
+
});
|
|
467
|
+
const stored = stampToolResultReceiptKey(live, plan.result.key);
|
|
468
|
+
const completedAtMs = toolResultCompletedAtMs(stored) ?? input.now();
|
|
469
|
+
if (!Number.isSafeInteger(completedAtMs) || completedAtMs < 0) return null;
|
|
470
|
+
const resultOutput = serializeToolExecuteResult(stored);
|
|
471
|
+
// Exactly the runner's `recorded`: `{ ...completed, ...fingerprints }`.
|
|
472
|
+
const operationOutput = {
|
|
473
|
+
...completedCallForResult(stored, completedAtMs),
|
|
474
|
+
...plan.operation.fingerprints,
|
|
475
|
+
};
|
|
476
|
+
const writes: RuntimeReceiptFinishWrite[] = [
|
|
477
|
+
{ key: plan.result.key, output: resultOutput },
|
|
478
|
+
...(plan.source_run_manifest
|
|
479
|
+
? [
|
|
480
|
+
{
|
|
481
|
+
key: plan.source_run_manifest.key,
|
|
482
|
+
output: plan.source_run_manifest.output,
|
|
483
|
+
bestEffort: true as const,
|
|
484
|
+
sourceRunManifest: true as const,
|
|
485
|
+
},
|
|
486
|
+
]
|
|
487
|
+
: []),
|
|
488
|
+
...(plan.operation.exact_request_key
|
|
489
|
+
? [{ key: plan.operation.exact_request_key, output: operationOutput }]
|
|
490
|
+
: []),
|
|
491
|
+
{ key: plan.operation.key, output: operationOutput },
|
|
492
|
+
...(plan.reusable && isCompletedSuccessfulAnswer(stored)
|
|
493
|
+
? [
|
|
494
|
+
{
|
|
495
|
+
key: reusableBucketKey({
|
|
496
|
+
prefix: plan.reusable.prefix,
|
|
497
|
+
ttlSeconds: plan.reusable.ttl_seconds,
|
|
498
|
+
completedAtMs,
|
|
499
|
+
}),
|
|
500
|
+
output: operationOutput,
|
|
501
|
+
},
|
|
502
|
+
]
|
|
503
|
+
: []),
|
|
504
|
+
];
|
|
505
|
+
// The runner refuses an oversized receipt before it writes; so does this.
|
|
506
|
+
for (const write of writes) {
|
|
507
|
+
assertRuntimeReceiptOutputWithinLimit({
|
|
508
|
+
output: write.output,
|
|
509
|
+
path: `finish-in-execute receipt "${write.key}"`,
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
return { writes, completedAtMs };
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/** The plan header value, or null when it would exceed the ceiling. */
|
|
516
|
+
export function encodeRuntimeReceiptFinishPlan(
|
|
517
|
+
plan: RuntimeReceiptFinishPlan,
|
|
518
|
+
): string | null {
|
|
519
|
+
const encoded = Buffer.from(JSON.stringify(plan), 'utf8').toString(
|
|
520
|
+
'base64url',
|
|
521
|
+
);
|
|
522
|
+
return encoded.length > RUNTIME_RECEIPT_FINISH_PLAN_MAX_HEADER_BYTES
|
|
523
|
+
? null
|
|
524
|
+
: encoded;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/** Decode the header; shape validation is `parseRuntimeReceiptFinishPlan`. */
|
|
528
|
+
export function decodeRuntimeReceiptFinishPlanHeader(
|
|
529
|
+
value: string | null | undefined,
|
|
530
|
+
): unknown {
|
|
531
|
+
const encoded = value?.trim();
|
|
532
|
+
if (!encoded) return undefined;
|
|
533
|
+
if (encoded.length > RUNTIME_RECEIPT_FINISH_PLAN_MAX_HEADER_BYTES) {
|
|
534
|
+
return null;
|
|
535
|
+
}
|
|
536
|
+
try {
|
|
537
|
+
return JSON.parse(Buffer.from(encoded, 'base64url').toString('utf8'));
|
|
538
|
+
} catch {
|
|
539
|
+
return null;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
export function encodeRuntimeReceiptFinishAck(
|
|
544
|
+
ack: RuntimeReceiptFinishAck,
|
|
545
|
+
): string {
|
|
546
|
+
return Buffer.from(JSON.stringify(ack), 'utf8').toString('base64url');
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
export function parseRuntimeReceiptFinishAck(
|
|
550
|
+
value: string | null | undefined,
|
|
551
|
+
): RuntimeReceiptFinishAck | null {
|
|
552
|
+
const encoded = value?.trim();
|
|
553
|
+
if (!encoded || encoded.length > MAX_ACK_HEADER_LENGTH) return null;
|
|
554
|
+
try {
|
|
555
|
+
const parsed = JSON.parse(
|
|
556
|
+
Buffer.from(encoded, 'base64url').toString('utf8'),
|
|
557
|
+
) as unknown;
|
|
558
|
+
if (
|
|
559
|
+
!isRecord(parsed) ||
|
|
560
|
+
!Number.isSafeInteger(parsed.completedAtMs) ||
|
|
561
|
+
(parsed.completedAtMs as number) < 0 ||
|
|
562
|
+
!Array.isArray(parsed.receipts) ||
|
|
563
|
+
parsed.receipts.length === 0
|
|
564
|
+
) {
|
|
565
|
+
return null;
|
|
566
|
+
}
|
|
567
|
+
const receipts: RuntimeReceiptFinishAck['receipts'] = [];
|
|
568
|
+
for (const entry of parsed.receipts) {
|
|
569
|
+
if (
|
|
570
|
+
!isRecord(entry) ||
|
|
571
|
+
typeof entry.key !== 'string' ||
|
|
572
|
+
!entry.key ||
|
|
573
|
+
typeof entry.digest !== 'string' ||
|
|
574
|
+
!/^[0-9a-f]{64}$/.test(entry.digest)
|
|
575
|
+
) {
|
|
576
|
+
return null;
|
|
577
|
+
}
|
|
578
|
+
receipts.push({ key: entry.key, digest: entry.digest });
|
|
579
|
+
}
|
|
580
|
+
return {
|
|
581
|
+
completedAtMs: parsed.completedAtMs as number,
|
|
582
|
+
receipts,
|
|
583
|
+
};
|
|
584
|
+
} catch {
|
|
585
|
+
return null;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* True only when the acknowledgement names exactly these writes, each with
|
|
591
|
+
* the digest of the value the runner would itself write.
|
|
592
|
+
*/
|
|
593
|
+
export function runtimeReceiptFinishAckCovers(
|
|
594
|
+
ack: RuntimeReceiptFinishAck,
|
|
595
|
+
writes: ReadonlyArray<{ key: string; output: unknown }>,
|
|
596
|
+
): boolean {
|
|
597
|
+
if (ack.receipts.length !== writes.length) return false;
|
|
598
|
+
const acknowledged = new Map(
|
|
599
|
+
ack.receipts.map((entry) => [entry.key, entry.digest]),
|
|
600
|
+
);
|
|
601
|
+
if (acknowledged.size !== writes.length) return false;
|
|
602
|
+
return writes.every(
|
|
603
|
+
(write) =>
|
|
604
|
+
acknowledged.get(write.key) === runtimeReceiptFinishDigest(write.output),
|
|
605
|
+
);
|
|
606
|
+
}
|