@narumitw/pi-subagents 2.1.4 → 3.0.0
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/README.md +187 -1228
- package/dist/child-communication-bridge.ts +136 -0
- package/dist/child-communication-bridge.ts.map +7 -0
- package/dist/chunks/chunk-UKTWUQRM.js +667 -0
- package/dist/chunks/chunk-UKTWUQRM.js.map +7 -0
- package/dist/index.ts +1448 -1614
- package/dist/index.ts.map +4 -4
- package/docs/design-principles.md +31 -0
- package/docs/ipc-transport-idea.md +103 -0
- package/docs/tools.md +109 -0
- package/package.json +11 -19
- package/src/broker-credentials.ts +71 -0
- package/src/child-communication-bridge.ts +153 -0
- package/src/child-communication-tools.ts +147 -0
- package/src/completion-renderer.ts +60 -0
- package/src/index.ts +1 -1
- package/src/message-broker.ts +628 -0
- package/src/model-output.ts +42 -0
- package/src/process.ts +688 -0
- package/src/runtime.ts +538 -0
- package/src/subagents.ts +33 -34
- package/src/tools.ts +416 -0
- package/src/types.ts +85 -0
- package/src/widget.ts +130 -0
- package/dist/chunks/auto-transport-FUUKFDIG.ts +0 -109
- package/dist/chunks/auto-transport-FUUKFDIG.ts.map +0 -7
- package/dist/chunks/capability-grant-PR72SWWS.ts +0 -19
- package/dist/chunks/capability-grant-PR72SWWS.ts.map +0 -7
- package/dist/chunks/child-peer-bridge.ts +0 -108
- package/dist/chunks/child-peer-bridge.ts.map +0 -7
- package/dist/chunks/chunk-2LMJU25E.ts +0 -1641
- package/dist/chunks/chunk-2LMJU25E.ts.map +0 -7
- package/dist/chunks/chunk-47SK2QYC.ts +0 -56
- package/dist/chunks/chunk-47SK2QYC.ts.map +0 -7
- package/dist/chunks/chunk-4AQSF7AS.ts +0 -29
- package/dist/chunks/chunk-4AQSF7AS.ts.map +0 -7
- package/dist/chunks/chunk-5DGRMKNV.ts +0 -126
- package/dist/chunks/chunk-5DGRMKNV.ts.map +0 -7
- package/dist/chunks/chunk-6H6TBBED.ts +0 -108
- package/dist/chunks/chunk-6H6TBBED.ts.map +0 -7
- package/dist/chunks/chunk-6KJ34M6S.ts +0 -159
- package/dist/chunks/chunk-6KJ34M6S.ts.map +0 -7
- package/dist/chunks/chunk-6NSJVPXX.ts +0 -480
- package/dist/chunks/chunk-6NSJVPXX.ts.map +0 -7
- package/dist/chunks/chunk-7AAJEUSL.ts +0 -238
- package/dist/chunks/chunk-7AAJEUSL.ts.map +0 -7
- package/dist/chunks/chunk-BFK2Z4ZF.ts +0 -189
- package/dist/chunks/chunk-BFK2Z4ZF.ts.map +0 -7
- package/dist/chunks/chunk-BOAXY55Y.ts +0 -153
- package/dist/chunks/chunk-BOAXY55Y.ts.map +0 -7
- package/dist/chunks/chunk-D4CR7T73.ts +0 -353
- package/dist/chunks/chunk-D4CR7T73.ts.map +0 -7
- package/dist/chunks/chunk-FEVPWRMU.ts +0 -300
- package/dist/chunks/chunk-FEVPWRMU.ts.map +0 -7
- package/dist/chunks/chunk-G6VZTCFH.ts +0 -70
- package/dist/chunks/chunk-G6VZTCFH.ts.map +0 -7
- package/dist/chunks/chunk-H3FP6DLR.ts +0 -160
- package/dist/chunks/chunk-H3FP6DLR.ts.map +0 -7
- package/dist/chunks/chunk-HA36LPCO.ts +0 -130
- package/dist/chunks/chunk-HA36LPCO.ts.map +0 -7
- package/dist/chunks/chunk-HOP5FXDT.ts +0 -27
- package/dist/chunks/chunk-HOP5FXDT.ts.map +0 -7
- package/dist/chunks/chunk-IGAMVWFN.ts +0 -139
- package/dist/chunks/chunk-IGAMVWFN.ts.map +0 -7
- package/dist/chunks/chunk-ILEQ27AL.ts +0 -45
- package/dist/chunks/chunk-ILEQ27AL.ts.map +0 -7
- package/dist/chunks/chunk-ITVWPNU4.ts +0 -819
- package/dist/chunks/chunk-ITVWPNU4.ts.map +0 -7
- package/dist/chunks/chunk-IWC32VPY.ts +0 -172
- package/dist/chunks/chunk-IWC32VPY.ts.map +0 -7
- package/dist/chunks/chunk-JSZIP73U.ts +0 -362
- package/dist/chunks/chunk-JSZIP73U.ts.map +0 -7
- package/dist/chunks/chunk-JU6LUNLP.ts +0 -380
- package/dist/chunks/chunk-JU6LUNLP.ts.map +0 -7
- package/dist/chunks/chunk-KMGKCEO4.ts +0 -38
- package/dist/chunks/chunk-KMGKCEO4.ts.map +0 -7
- package/dist/chunks/chunk-LASD73CM.ts +0 -339
- package/dist/chunks/chunk-LASD73CM.ts.map +0 -7
- package/dist/chunks/chunk-LEOYDZI3.ts +0 -22
- package/dist/chunks/chunk-LEOYDZI3.ts.map +0 -7
- package/dist/chunks/chunk-LL4LP2T7.ts +0 -737
- package/dist/chunks/chunk-LL4LP2T7.ts.map +0 -7
- package/dist/chunks/chunk-N2T5IN4X.ts +0 -18
- package/dist/chunks/chunk-N2T5IN4X.ts.map +0 -7
- package/dist/chunks/chunk-N7BLVXKK.ts +0 -73
- package/dist/chunks/chunk-N7BLVXKK.ts.map +0 -7
- package/dist/chunks/chunk-NLT67IZS.ts +0 -322
- package/dist/chunks/chunk-NLT67IZS.ts.map +0 -7
- package/dist/chunks/chunk-NTRPLF46.ts +0 -63
- package/dist/chunks/chunk-NTRPLF46.ts.map +0 -7
- package/dist/chunks/chunk-ONDTY4EL.ts +0 -91
- package/dist/chunks/chunk-ONDTY4EL.ts.map +0 -7
- package/dist/chunks/chunk-OVPHFGKO.ts +0 -1769
- package/dist/chunks/chunk-OVPHFGKO.ts.map +0 -7
- package/dist/chunks/chunk-P7OH4XMF.ts +0 -60
- package/dist/chunks/chunk-P7OH4XMF.ts.map +0 -7
- package/dist/chunks/chunk-PBZMBTNJ.ts +0 -422
- package/dist/chunks/chunk-PBZMBTNJ.ts.map +0 -7
- package/dist/chunks/chunk-PGLSFLYW.ts +0 -52
- package/dist/chunks/chunk-PGLSFLYW.ts.map +0 -7
- package/dist/chunks/chunk-RRR66UWR.ts +0 -28
- package/dist/chunks/chunk-RRR66UWR.ts.map +0 -7
- package/dist/chunks/chunk-RSUXZD6S.ts +0 -275
- package/dist/chunks/chunk-RSUXZD6S.ts.map +0 -7
- package/dist/chunks/chunk-RTCVYIZA.ts +0 -221
- package/dist/chunks/chunk-RTCVYIZA.ts.map +0 -7
- package/dist/chunks/chunk-RY2AEGMZ.ts +0 -34
- package/dist/chunks/chunk-RY2AEGMZ.ts.map +0 -7
- package/dist/chunks/chunk-SWGQLFSD.ts +0 -60
- package/dist/chunks/chunk-SWGQLFSD.ts.map +0 -7
- package/dist/chunks/chunk-TM2R67J3.ts +0 -52
- package/dist/chunks/chunk-TM2R67J3.ts.map +0 -7
- package/dist/chunks/chunk-TMZRHIIK.ts +0 -497
- package/dist/chunks/chunk-TMZRHIIK.ts.map +0 -7
- package/dist/chunks/chunk-TZ34IQ3M.ts +0 -59
- package/dist/chunks/chunk-TZ34IQ3M.ts.map +0 -7
- package/dist/chunks/chunk-UMBPMVJW.ts +0 -22
- package/dist/chunks/chunk-UMBPMVJW.ts.map +0 -7
- package/dist/chunks/chunk-VDG7LTYE.ts +0 -80
- package/dist/chunks/chunk-VDG7LTYE.ts.map +0 -7
- package/dist/chunks/chunk-X4NMONPE.ts +0 -73
- package/dist/chunks/chunk-X4NMONPE.ts.map +0 -7
- package/dist/chunks/chunk-YPJEN6NU.ts +0 -65
- package/dist/chunks/chunk-YPJEN6NU.ts.map +0 -7
- package/dist/chunks/chunk-YU53SHA7.ts +0 -328
- package/dist/chunks/chunk-YU53SHA7.ts.map +0 -7
- package/dist/chunks/completion-delivery-RSJU6BXL.ts +0 -17
- package/dist/chunks/completion-delivery-RSJU6BXL.ts.map +0 -7
- package/dist/chunks/config-status-FKGDECZ3.ts +0 -35
- package/dist/chunks/config-status-FKGDECZ3.ts.map +0 -7
- package/dist/chunks/config-ui-ABHYNGQ7.ts +0 -1212
- package/dist/chunks/config-ui-ABHYNGQ7.ts.map +0 -7
- package/dist/chunks/consult-LJU3IQY5.ts +0 -519
- package/dist/chunks/consult-LJU3IQY5.ts.map +0 -7
- package/dist/chunks/context-QDKLVQXE.ts +0 -11
- package/dist/chunks/context-QDKLVQXE.ts.map +0 -7
- package/dist/chunks/create-stateful-transport-JWC2EFYL.ts +0 -130
- package/dist/chunks/create-stateful-transport-JWC2EFYL.ts.map +0 -7
- package/dist/chunks/cwd-policy-NB6XZ5GF.ts +0 -17
- package/dist/chunks/cwd-policy-NB6XZ5GF.ts.map +0 -7
- package/dist/chunks/delegation-contract-LA56I5DT.ts +0 -24
- package/dist/chunks/delegation-contract-LA56I5DT.ts.map +0 -7
- package/dist/chunks/discovery-MIFB2U4Y.ts +0 -12
- package/dist/chunks/discovery-MIFB2U4Y.ts.map +0 -7
- package/dist/chunks/execution-Q2JZLKJA.ts +0 -4037
- package/dist/chunks/execution-Q2JZLKJA.ts.map +0 -7
- package/dist/chunks/in-process-transport-HJ6TZXC3.ts +0 -39
- package/dist/chunks/in-process-transport-HJ6TZXC3.ts.map +0 -7
- package/dist/chunks/inspect-UH2TKH6E.ts +0 -634
- package/dist/chunks/inspect-UH2TKH6E.ts.map +0 -7
- package/dist/chunks/peer-communication-KL36Z6CO.ts +0 -294
- package/dist/chunks/peer-communication-KL36Z6CO.ts.map +0 -7
- package/dist/chunks/persistence-UY3PY6E5.ts +0 -348
- package/dist/chunks/persistence-UY3PY6E5.ts.map +0 -7
- package/dist/chunks/registry-BT54L6CY.ts +0 -1445
- package/dist/chunks/registry-BT54L6CY.ts.map +0 -7
- package/dist/chunks/retained-semantic-state-GM75FZGE.ts +0 -81
- package/dist/chunks/retained-semantic-state-GM75FZGE.ts.map +0 -7
- package/dist/chunks/rpc-transport-7R7DVCEB.ts +0 -1222
- package/dist/chunks/rpc-transport-7R7DVCEB.ts.map +0 -7
- package/dist/chunks/runtime-policy-5YELCOVC.ts +0 -15
- package/dist/chunks/runtime-policy-5YELCOVC.ts.map +0 -7
- package/dist/chunks/semantic-snapshot-WPJ3UAFW.ts +0 -19
- package/dist/chunks/semantic-snapshot-WPJ3UAFW.ts.map +0 -7
- package/dist/chunks/spawn-idempotency-BNHOSMZW.ts +0 -13
- package/dist/chunks/spawn-idempotency-BNHOSMZW.ts.map +0 -7
- package/dist/chunks/stateful-lifecycle-JAR6K55H.ts +0 -15
- package/dist/chunks/stateful-lifecycle-JAR6K55H.ts.map +0 -7
- package/dist/chunks/subprocess-transport-VHZJRBTW.ts +0 -160
- package/dist/chunks/subprocess-transport-VHZJRBTW.ts.map +0 -7
- package/dist/chunks/usage-recording-store-CW3EH2SZ.ts +0 -164
- package/dist/chunks/usage-recording-store-CW3EH2SZ.ts.map +0 -7
- package/dist/chunks/workspace-WDKLN72J.ts +0 -11
- package/dist/chunks/workspace-WDKLN72J.ts.map +0 -7
- package/docs/async-runtime-protocol.md +0 -84
- package/docs/implementation-notes/pi-subagents-capability-matrix.md +0 -62
- package/docs/implementation-notes/pi-subagents-current-direction.md +0 -99
- package/docs/implementation-notes/pi-subagents-rpc-v1.md +0 -139
- package/docs/pi-subagents-diagrams.md +0 -183
- package/src/adaptive-scheduler.ts +0 -224
- package/src/admission-policy.ts +0 -78
- package/src/agent-projection.ts +0 -53
- package/src/agents/built-ins.ts +0 -71
- package/src/agents/catalog.ts +0 -241
- package/src/agents/discovery.ts +0 -265
- package/src/agents/types.ts +0 -100
- package/src/agents.ts +0 -51
- package/src/auto-transport.ts +0 -121
- package/src/blocking-status.ts +0 -63
- package/src/cached-module-loader.ts +0 -18
- package/src/capabilities.ts +0 -145
- package/src/capability-grant.ts +0 -115
- package/src/capability-router.ts +0 -107
- package/src/child-peer-bridge.ts +0 -124
- package/src/child-peer-tools.ts +0 -132
- package/src/completion-delivery.ts +0 -409
- package/src/completion-render.ts +0 -190
- package/src/completion-requirement.ts +0 -479
- package/src/completion-routing.ts +0 -24
- package/src/config-registration.ts +0 -114
- package/src/config-status.ts +0 -217
- package/src/config-ui.ts +0 -983
- package/src/consult-policy.ts +0 -15
- package/src/consult-registration.ts +0 -125
- package/src/consult-render.ts +0 -194
- package/src/consult-resources.ts +0 -51
- package/src/consult-tool.ts +0 -95
- package/src/consult.ts +0 -566
- package/src/context.ts +0 -126
- package/src/create-stateful-transport.ts +0 -157
- package/src/cwd-policy.ts +0 -183
- package/src/delegation-contract.ts +0 -463
- package/src/execution/budget.ts +0 -56
- package/src/execution/runtime-policy.ts +0 -19
- package/src/execution-plan.ts +0 -322
- package/src/execution-ui.ts +0 -259
- package/src/execution.ts +0 -1679
- package/src/in-process-transport.ts +0 -941
- package/src/inspect-registration.ts +0 -64
- package/src/inspect-render.ts +0 -334
- package/src/inspect-tool.ts +0 -45
- package/src/inspect.ts +0 -769
- package/src/integration-controller.ts +0 -98
- package/src/limits.ts +0 -75
- package/src/orchestration-metrics.ts +0 -116
- package/src/outcome.ts +0 -61
- package/src/panel-child-group.ts +0 -35
- package/src/panel-contract.ts +0 -343
- package/src/panel-evidence.ts +0 -59
- package/src/panel-execution.ts +0 -769
- package/src/panel-failure.ts +0 -56
- package/src/panel-planning.ts +0 -175
- package/src/panel-presets.ts +0 -3
- package/src/panel-prompts.ts +0 -132
- package/src/panel-reconciliation.ts +0 -57
- package/src/panel-render.ts +0 -103
- package/src/parallel-limit-ui.ts +0 -113
- package/src/params.ts +0 -278
- package/src/peer-communication.ts +0 -352
- package/src/peer-transport.ts +0 -49
- package/src/persistence.ts +0 -522
- package/src/pi-args.ts +0 -43
- package/src/pi-invocation.ts +0 -168
- package/src/process-control.ts +0 -43
- package/src/prompt-resources.ts +0 -38
- package/src/prompt-source-safety.ts +0 -48
- package/src/protocol.ts +0 -76
- package/src/registry-types.ts +0 -204
- package/src/registry.ts +0 -1698
- package/src/render-common.ts +0 -252
- package/src/render.ts +0 -704
- package/src/result-contract.ts +0 -431
- package/src/retained-semantic-state.ts +0 -100
- package/src/rpc-timeout-finalization.ts +0 -207
- package/src/rpc-transport-metadata.ts +0 -65
- package/src/rpc-transport.ts +0 -1035
- package/src/rpc-turn-capture.ts +0 -217
- package/src/runner-outcome.ts +0 -31
- package/src/runner-result.ts +0 -55
- package/src/runner-types.ts +0 -102
- package/src/runner-usage.ts +0 -48
- package/src/runner.ts +0 -866
- package/src/safe-text.ts +0 -67
- package/src/semantic-snapshot.ts +0 -214
- package/src/session-guidance-contract.ts +0 -399
- package/src/settings/inspection.ts +0 -302
- package/src/settings/schema.ts +0 -195
- package/src/settings-reader.ts +0 -215
- package/src/settings.ts +0 -452
- package/src/spawn-idempotency.ts +0 -68
- package/src/stateful-agent-view.ts +0 -85
- package/src/stateful-config.ts +0 -13
- package/src/stateful-guidance.ts +0 -29
- package/src/stateful-lifecycle.ts +0 -74
- package/src/stateful-limit-ui.ts +0 -249
- package/src/stateful-limits.ts +0 -96
- package/src/stateful-prompt.ts +0 -49
- package/src/stateful-registration.ts +0 -1320
- package/src/stateful-render.ts +0 -318
- package/src/stateful-safety.ts +0 -47
- package/src/stateful-tool-params.ts +0 -215
- package/src/stateful.ts +0 -12
- package/src/subagent-details.ts +0 -43
- package/src/subagents-extension.ts +0 -372
- package/src/subprocess-transport.ts +0 -145
- package/src/supervision.ts +0 -104
- package/src/task-path.ts +0 -65
- package/src/timeout-checkpoint.ts +0 -305
- package/src/timeout-finalization.ts +0 -75
- package/src/tool-schema-compatibility.ts +0 -73
- package/src/transport-types.ts +0 -74
- package/src/transport-ui.ts +0 -135
- package/src/transport.ts +0 -43
- package/src/turn-budget.ts +0 -109
- package/src/usage-format.ts +0 -42
- package/src/usage-recording-config.ts +0 -13
- package/src/usage-recording-store.ts +0 -183
- package/src/usage-recording.ts +0 -478
- package/src/verification-harness.ts +0 -516
- package/src/verification-policy.ts +0 -67
- package/src/verification-receipt.ts +0 -275
- package/src/verified-execution-benchmark.ts +0 -86
- package/src/verified-execution-contract.ts +0 -191
- package/src/verified-execution-schema.ts +0 -32
- package/src/work-item-ledger.ts +0 -1404
- package/src/work-item-persistence.ts +0 -254
- package/src/workflow-completion-controller.ts +0 -397
- package/src/workflow-planning.ts +0 -172
- package/src/workflow-tree-identity.ts +0 -289
- package/src/workflow-ui.ts +0 -69
- package/src/workflow-verification.ts +0 -296
- package/src/workspace.ts +0 -174
package/src/work-item-ledger.ts
DELETED
|
@@ -1,1404 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Ledger transitions and restore validators stay together so every persisted acceptance invariant
|
|
3
|
-
* is audited against the same atomic state machine rather than duplicated across modules.
|
|
4
|
-
*/
|
|
5
|
-
import {
|
|
6
|
-
type ManagedIntegrationCandidate,
|
|
7
|
-
type ManagedIntegrationExpectation,
|
|
8
|
-
verifyManagedIntegration,
|
|
9
|
-
} from "./integration-controller.js";
|
|
10
|
-
import type { VerificationSubmission } from "./verification-harness.js";
|
|
11
|
-
import { isVerificationReceipt, type VerificationReceipt } from "./verification-receipt.js";
|
|
12
|
-
import {
|
|
13
|
-
isWorkflowTreeIdentity,
|
|
14
|
-
sameWorkflowTreeIdentity,
|
|
15
|
-
type WorkflowTreeIdentity,
|
|
16
|
-
} from "./workflow-tree-identity.js";
|
|
17
|
-
import {
|
|
18
|
-
isWorkflowVerificationReceipt,
|
|
19
|
-
type WorkflowVerificationReceipt,
|
|
20
|
-
} from "./workflow-verification.js";
|
|
21
|
-
|
|
22
|
-
export const WORK_ITEM_LEDGER_VERSION = "pi-subagents:work-ledger:v3" as const;
|
|
23
|
-
const LEGACY_WORK_ITEM_LEDGER_VERSIONS = new Set([
|
|
24
|
-
"pi-subagents:work-ledger:v1",
|
|
25
|
-
"pi-subagents:work-ledger:v2",
|
|
26
|
-
]);
|
|
27
|
-
export const WORK_ITEM_ACCEPTANCE_VERSION = "pi-subagents:work-acceptance:v1" as const;
|
|
28
|
-
export type WorkItemAcceptanceState =
|
|
29
|
-
| "not-required"
|
|
30
|
-
| "pending"
|
|
31
|
-
| "accepted"
|
|
32
|
-
| "rework-requested"
|
|
33
|
-
| "rejected";
|
|
34
|
-
|
|
35
|
-
export type WorkItemState =
|
|
36
|
-
| "pending"
|
|
37
|
-
| "ready"
|
|
38
|
-
| "running"
|
|
39
|
-
| "awaiting-verification"
|
|
40
|
-
| "blocked"
|
|
41
|
-
| "needs-input"
|
|
42
|
-
| "completed"
|
|
43
|
-
| "failed"
|
|
44
|
-
| "interrupted"
|
|
45
|
-
| "stale"
|
|
46
|
-
| "invalidated";
|
|
47
|
-
|
|
48
|
-
export interface WorkArtifactReference {
|
|
49
|
-
id: string;
|
|
50
|
-
kind: string;
|
|
51
|
-
version: string;
|
|
52
|
-
digest?: string;
|
|
53
|
-
producerTaskId?: string;
|
|
54
|
-
generation?: number;
|
|
55
|
-
verified?: boolean;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export interface WorkItemDefinition {
|
|
59
|
-
id: string;
|
|
60
|
-
objective: string;
|
|
61
|
-
dependencies: string[];
|
|
62
|
-
inputArtifacts?: string[];
|
|
63
|
-
inputArtifactVersions?: Record<string, string>;
|
|
64
|
-
requiredCapabilities?: string[];
|
|
65
|
-
requiredTools?: string[];
|
|
66
|
-
selectedAgentName?: string;
|
|
67
|
-
sideEffectPolicy?: "read-only" | "idempotent" | "mutating";
|
|
68
|
-
readPaths?: string[];
|
|
69
|
-
writePaths?: string[];
|
|
70
|
-
ownershipKeys?: string[];
|
|
71
|
-
acceptanceCriteria?: string[];
|
|
72
|
-
requiredEvidence?: string[];
|
|
73
|
-
integrationOwner?: boolean;
|
|
74
|
-
verifierFor?: string;
|
|
75
|
-
dependencyPolicy?: "completed" | "settled";
|
|
76
|
-
acceptanceRequired?: boolean;
|
|
77
|
-
maxReworkCycles?: 0 | 1;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export interface WorkItemRecord {
|
|
81
|
-
id: string;
|
|
82
|
-
objective: string;
|
|
83
|
-
dependencies: string[];
|
|
84
|
-
dependents: string[];
|
|
85
|
-
state: WorkItemState;
|
|
86
|
-
generation: number;
|
|
87
|
-
taskGeneration: number;
|
|
88
|
-
assignedAgentId?: string;
|
|
89
|
-
acceptedExecutionPlanId?: string;
|
|
90
|
-
inputArtifacts: string[];
|
|
91
|
-
inputArtifactVersions: Record<string, string>;
|
|
92
|
-
requiredArtifactVersions: Record<string, string>;
|
|
93
|
-
requiredCapabilities: string[];
|
|
94
|
-
requiredTools: string[];
|
|
95
|
-
selectedAgentName?: string;
|
|
96
|
-
sideEffectPolicy: "read-only" | "idempotent" | "mutating";
|
|
97
|
-
artifacts: WorkArtifactReference[];
|
|
98
|
-
artifactHistory: WorkArtifactReference[];
|
|
99
|
-
readPaths: string[];
|
|
100
|
-
writePaths: string[];
|
|
101
|
-
ownershipKeys: string[];
|
|
102
|
-
acceptanceCriteria: string[];
|
|
103
|
-
requiredEvidence: string[];
|
|
104
|
-
integrationOwner: boolean;
|
|
105
|
-
verifierFor?: string;
|
|
106
|
-
dependencyPolicy: "completed" | "settled";
|
|
107
|
-
acceptanceStateVersion: typeof WORK_ITEM_ACCEPTANCE_VERSION;
|
|
108
|
-
acceptanceRequired: boolean;
|
|
109
|
-
acceptanceState: WorkItemAcceptanceState;
|
|
110
|
-
reworkCount: number;
|
|
111
|
-
maxReworkCycles: 0 | 1;
|
|
112
|
-
verificationAccepted: boolean;
|
|
113
|
-
stagedTreeIdentity?: WorkflowTreeIdentity;
|
|
114
|
-
verificationReceipt?: WorkflowVerificationReceipt;
|
|
115
|
-
acceptanceReceipt?: VerificationReceipt;
|
|
116
|
-
acceptanceReceiptHistory: VerificationReceipt[];
|
|
117
|
-
submission?: VerificationSubmission;
|
|
118
|
-
invalidationReasons: string[];
|
|
119
|
-
outcomeReason?: string;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export interface WorkItemLedgerSnapshot {
|
|
123
|
-
version: typeof WORK_ITEM_LEDGER_VERSION;
|
|
124
|
-
workflowId: string;
|
|
125
|
-
generation: number;
|
|
126
|
-
items: WorkItemRecord[];
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
export interface CreateWorkItemLedgerInput {
|
|
130
|
-
workflowId: string;
|
|
131
|
-
items: WorkItemDefinition[];
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export interface CompleteWorkItemInput {
|
|
135
|
-
taskGeneration: number;
|
|
136
|
-
executionPlanId?: string;
|
|
137
|
-
artifacts?: Array<Omit<WorkArtifactReference, "producerTaskId" | "generation">>;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
export interface StageWorkItemVerificationInput extends CompleteWorkItemInput {
|
|
141
|
-
executionPlanId: string;
|
|
142
|
-
treeIdentity: WorkflowTreeIdentity;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export interface StageVerifiedAcceptanceInput
|
|
146
|
-
extends CompleteWorkItemInput,
|
|
147
|
-
VerificationSubmission {
|
|
148
|
-
executionPlanId: string;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export interface CompleteWorkItemVerificationInput {
|
|
152
|
-
taskGeneration: number;
|
|
153
|
-
executionPlanId: string;
|
|
154
|
-
receipt: WorkflowVerificationReceipt;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
export interface RecordVerificationDecisionInput {
|
|
158
|
-
taskGeneration: number;
|
|
159
|
-
executionPlanId: string;
|
|
160
|
-
receipt: VerificationReceipt;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export interface VerifiedIntegrationInput {
|
|
164
|
-
verifierId: string;
|
|
165
|
-
verifierTaskGeneration: number;
|
|
166
|
-
verifierExecutionPlanId: string;
|
|
167
|
-
receipt: VerificationReceipt;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const MAX_ITEMS = 64;
|
|
171
|
-
const MAX_IDENTIFIER_LENGTH = 256;
|
|
172
|
-
const MAX_TEXT_LENGTH = 16 * 1024;
|
|
173
|
-
const MAX_LIST_ITEMS = 50;
|
|
174
|
-
|
|
175
|
-
const TERMINAL_STATES = new Set<WorkItemState>([
|
|
176
|
-
"completed",
|
|
177
|
-
"failed",
|
|
178
|
-
"interrupted",
|
|
179
|
-
"stale",
|
|
180
|
-
"invalidated",
|
|
181
|
-
]);
|
|
182
|
-
|
|
183
|
-
export class WorkItemLedger {
|
|
184
|
-
private readonly items = new Map<string, WorkItemRecord>();
|
|
185
|
-
private generation = 0;
|
|
186
|
-
|
|
187
|
-
private constructor(readonly workflowId: string) {}
|
|
188
|
-
|
|
189
|
-
static create(input: CreateWorkItemLedgerInput): WorkItemLedger {
|
|
190
|
-
validateIdentifier(input.workflowId, "workflowId");
|
|
191
|
-
if (!Array.isArray(input.items) || input.items.length < 1 || input.items.length > MAX_ITEMS) {
|
|
192
|
-
throw new Error(`WorkItem workflow must contain 1-${MAX_ITEMS} items`);
|
|
193
|
-
}
|
|
194
|
-
const ledger = new WorkItemLedger(input.workflowId);
|
|
195
|
-
for (const definition of input.items) ledger.addDefinition(definition);
|
|
196
|
-
ledger.linkAndValidate();
|
|
197
|
-
ledger.refreshReadyState();
|
|
198
|
-
return ledger;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
get(id: string): WorkItemRecord | undefined {
|
|
202
|
-
const item = this.items.get(id);
|
|
203
|
-
return item ? structuredClone(item) : undefined;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
readyItems(): WorkItemRecord[] {
|
|
207
|
-
this.refreshReadyState();
|
|
208
|
-
return [...this.items.values()]
|
|
209
|
-
.filter((item) => item.state === "ready")
|
|
210
|
-
.sort((left, right) => left.id.localeCompare(right.id))
|
|
211
|
-
.map((item) => structuredClone(item));
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
start(id: string, agentId: string): WorkItemRecord {
|
|
215
|
-
const item = this.require(id);
|
|
216
|
-
if (item.state !== "ready") {
|
|
217
|
-
throw new Error(`WorkItem ${id} cannot start while ${item.state}`);
|
|
218
|
-
}
|
|
219
|
-
validateIdentifier(agentId, "agentId");
|
|
220
|
-
item.state = "running";
|
|
221
|
-
item.assignedAgentId = agentId;
|
|
222
|
-
item.generation = ++this.generation;
|
|
223
|
-
return structuredClone(item);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
complete(id: string, input: CompleteWorkItemInput): WorkItemRecord {
|
|
227
|
-
const item = this.require(id);
|
|
228
|
-
this.assertMutable(item);
|
|
229
|
-
if (item.acceptanceRequired) {
|
|
230
|
-
throw new Error(`WorkItem ${id} requires executor-owned acceptance staging`);
|
|
231
|
-
}
|
|
232
|
-
if (item.state !== "running") {
|
|
233
|
-
throw new Error(`WorkItem ${id} cannot complete while ${item.state}`);
|
|
234
|
-
}
|
|
235
|
-
if (input.taskGeneration !== item.taskGeneration) {
|
|
236
|
-
throw new Error(`WorkItem ${id} rejected a stale task generation`);
|
|
237
|
-
}
|
|
238
|
-
item.acceptedExecutionPlanId = input.executionPlanId?.slice(0, 256);
|
|
239
|
-
item.artifactHistory.push(...item.artifacts.map((artifact) => structuredClone(artifact)));
|
|
240
|
-
item.artifacts = normalizeArtifacts(input.artifacts ?? [], id, this.generation + 1);
|
|
241
|
-
item.verificationAccepted = false;
|
|
242
|
-
item.stagedTreeIdentity = undefined;
|
|
243
|
-
item.verificationReceipt = undefined;
|
|
244
|
-
item.acceptanceState = "not-required";
|
|
245
|
-
item.state = "completed";
|
|
246
|
-
item.generation = ++this.generation;
|
|
247
|
-
this.refreshReadyState();
|
|
248
|
-
return structuredClone(item);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
stageForVerifiedAcceptance(id: string, input: StageVerifiedAcceptanceInput): WorkItemRecord {
|
|
252
|
-
const item = this.require(id);
|
|
253
|
-
this.assertMutable(item);
|
|
254
|
-
if (!item.acceptanceRequired || !item.integrationOwner) {
|
|
255
|
-
throw new Error(`WorkItem ${id} is not a verified integration target`);
|
|
256
|
-
}
|
|
257
|
-
if (item.state !== "running") {
|
|
258
|
-
throw new Error(`WorkItem ${id} cannot stage acceptance while ${item.state}`);
|
|
259
|
-
}
|
|
260
|
-
if (input.taskGeneration !== item.taskGeneration) {
|
|
261
|
-
throw new Error(`WorkItem ${id} rejected a stale task generation`);
|
|
262
|
-
}
|
|
263
|
-
validatePlanId(input.executionPlanId, "staged execution plan");
|
|
264
|
-
validateSubmission(input);
|
|
265
|
-
item.acceptedExecutionPlanId = input.executionPlanId;
|
|
266
|
-
item.artifactHistory.push(...item.artifacts.map((artifact) => structuredClone(artifact)));
|
|
267
|
-
item.artifacts = normalizeArtifacts(input.artifacts ?? [], id, this.generation + 1);
|
|
268
|
-
item.verificationAccepted = false;
|
|
269
|
-
item.acceptanceState = "pending";
|
|
270
|
-
item.stagedTreeIdentity = structuredClone(input.treeIdentity);
|
|
271
|
-
item.submission = {
|
|
272
|
-
treeIdentity: structuredClone(input.treeIdentity),
|
|
273
|
-
baseRepositoryGeneration: input.baseRepositoryGeneration,
|
|
274
|
-
patchDigest: input.patchDigest,
|
|
275
|
-
changedPaths: [...input.changedPaths],
|
|
276
|
-
fileVersions: { ...input.fileVersions },
|
|
277
|
-
};
|
|
278
|
-
item.acceptanceReceipt = undefined;
|
|
279
|
-
item.state = "completed";
|
|
280
|
-
item.generation = ++this.generation;
|
|
281
|
-
this.refreshReadyState();
|
|
282
|
-
return structuredClone(item);
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
stageForVerification(id: string, input: StageWorkItemVerificationInput): WorkItemRecord {
|
|
286
|
-
const item = this.require(id);
|
|
287
|
-
this.assertMutable(item);
|
|
288
|
-
if (item.state !== "running") {
|
|
289
|
-
throw new Error(`WorkItem ${id} cannot stage verification while ${item.state}`);
|
|
290
|
-
}
|
|
291
|
-
if (input.taskGeneration !== item.taskGeneration) {
|
|
292
|
-
throw new Error(`WorkItem ${id} rejected a stale task generation`);
|
|
293
|
-
}
|
|
294
|
-
validatePlanId(input.executionPlanId, "staged execution plan");
|
|
295
|
-
if (!isWorkflowTreeIdentity(input.treeIdentity)) {
|
|
296
|
-
throw new Error(`WorkItem ${id} received an invalid staged tree identity`);
|
|
297
|
-
}
|
|
298
|
-
item.acceptedExecutionPlanId = input.executionPlanId;
|
|
299
|
-
item.artifactHistory.push(...item.artifacts.map((artifact) => structuredClone(artifact)));
|
|
300
|
-
item.artifacts = normalizeArtifacts(input.artifacts ?? [], id, this.generation + 1);
|
|
301
|
-
item.verificationAccepted = false;
|
|
302
|
-
item.stagedTreeIdentity = structuredClone(input.treeIdentity);
|
|
303
|
-
item.verificationReceipt = undefined;
|
|
304
|
-
item.state = "awaiting-verification";
|
|
305
|
-
item.generation = ++this.generation;
|
|
306
|
-
this.refreshReadyState();
|
|
307
|
-
return structuredClone(item);
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
completeVerification(
|
|
311
|
-
verifierId: string,
|
|
312
|
-
input: CompleteWorkItemVerificationInput,
|
|
313
|
-
): { target: WorkItemRecord; verifier: WorkItemRecord } {
|
|
314
|
-
const verifier = this.require(verifierId);
|
|
315
|
-
this.assertMutable(verifier);
|
|
316
|
-
if (verifier.state !== "running" || !verifier.verifierFor) {
|
|
317
|
-
throw new Error(`WorkItem ${verifierId} is not a running verifier`);
|
|
318
|
-
}
|
|
319
|
-
if (input.taskGeneration !== verifier.taskGeneration) {
|
|
320
|
-
throw new Error(`WorkItem ${verifierId} rejected a stale verifier generation`);
|
|
321
|
-
}
|
|
322
|
-
validatePlanId(input.executionPlanId, "verifier execution plan");
|
|
323
|
-
if (!isWorkflowVerificationReceipt(input.receipt)) {
|
|
324
|
-
throw new Error(`WorkItem ${verifierId} received an invalid verification receipt`);
|
|
325
|
-
}
|
|
326
|
-
const target = this.require(verifier.verifierFor);
|
|
327
|
-
if (target.state !== "awaiting-verification") {
|
|
328
|
-
throw new Error(`WorkItem ${target.id} is not awaiting verification`);
|
|
329
|
-
}
|
|
330
|
-
assertReceiptMatches(target, verifier, input.executionPlanId, input.receipt);
|
|
331
|
-
verifier.acceptedExecutionPlanId = input.executionPlanId;
|
|
332
|
-
verifier.verificationReceipt = undefined;
|
|
333
|
-
verifier.verificationAccepted = false;
|
|
334
|
-
verifier.state = "completed";
|
|
335
|
-
verifier.generation = ++this.generation;
|
|
336
|
-
target.verificationReceipt = structuredClone(input.receipt);
|
|
337
|
-
target.verificationAccepted = input.receipt.decision === "accept";
|
|
338
|
-
target.outcomeReason =
|
|
339
|
-
input.receipt.decision === "accept"
|
|
340
|
-
? undefined
|
|
341
|
-
: input.receipt.decision === "rework"
|
|
342
|
-
? "verification-rework"
|
|
343
|
-
: "verification-rejected";
|
|
344
|
-
if (input.receipt.decision === "accept") {
|
|
345
|
-
target.artifacts = target.artifacts.map((artifact) => ({ ...artifact, verified: true }));
|
|
346
|
-
target.state = "completed";
|
|
347
|
-
} else {
|
|
348
|
-
target.state = input.receipt.decision === "rework" ? "blocked" : "failed";
|
|
349
|
-
this.invalidateDependents(target.id, verifier.id, target.outcomeReason);
|
|
350
|
-
}
|
|
351
|
-
target.generation = ++this.generation;
|
|
352
|
-
this.refreshReadyState();
|
|
353
|
-
return { target: structuredClone(target), verifier: structuredClone(verifier) };
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
recordVerificationDecision(
|
|
357
|
-
verifierId: string,
|
|
358
|
-
input: RecordVerificationDecisionInput,
|
|
359
|
-
): { target: WorkItemRecord; verifier: WorkItemRecord } {
|
|
360
|
-
const verifier = this.require(verifierId);
|
|
361
|
-
if (verifier.state !== "running" || !verifier.verifierFor) {
|
|
362
|
-
throw new Error(`WorkItem ${verifierId} is not a running verifier`);
|
|
363
|
-
}
|
|
364
|
-
if (input.taskGeneration !== verifier.taskGeneration) {
|
|
365
|
-
throw new Error(`WorkItem ${verifierId} rejected a stale verifier generation`);
|
|
366
|
-
}
|
|
367
|
-
validatePlanId(input.executionPlanId, "verifier execution plan");
|
|
368
|
-
if (!isVerificationReceipt(input.receipt) || input.receipt.decision === "accept") {
|
|
369
|
-
throw new Error(`WorkItem ${verifierId} received an invalid non-acceptance receipt`);
|
|
370
|
-
}
|
|
371
|
-
const target = this.require(verifier.verifierFor);
|
|
372
|
-
assertVerifiedReceiptMatches(target, verifier, input.executionPlanId, input.receipt);
|
|
373
|
-
verifier.acceptedExecutionPlanId = input.executionPlanId;
|
|
374
|
-
verifier.state = "completed";
|
|
375
|
-
verifier.generation = ++this.generation;
|
|
376
|
-
target.acceptanceReceipt = structuredClone(input.receipt);
|
|
377
|
-
target.acceptanceReceiptHistory.push(structuredClone(input.receipt));
|
|
378
|
-
target.verificationAccepted = false;
|
|
379
|
-
if (input.receipt.decision === "rework" && target.reworkCount < target.maxReworkCycles) {
|
|
380
|
-
target.reworkCount++;
|
|
381
|
-
target.acceptanceState = "rework-requested";
|
|
382
|
-
target.state = "blocked";
|
|
383
|
-
target.outcomeReason = "verification-rework";
|
|
384
|
-
} else {
|
|
385
|
-
target.acceptanceState = "rejected";
|
|
386
|
-
target.state = "failed";
|
|
387
|
-
target.outcomeReason =
|
|
388
|
-
input.receipt.decision === "rework"
|
|
389
|
-
? "verification-rework-exhausted"
|
|
390
|
-
: "verification-rejected";
|
|
391
|
-
}
|
|
392
|
-
target.generation = ++this.generation;
|
|
393
|
-
this.invalidateDependents(target.id, verifier.id, target.outcomeReason);
|
|
394
|
-
this.refreshReadyState();
|
|
395
|
-
return { target: structuredClone(target), verifier: structuredClone(verifier) };
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
beginVerificationRework(id: string): WorkItemRecord {
|
|
399
|
-
const target = this.require(id);
|
|
400
|
-
if (target.acceptanceState !== "rework-requested" || target.state !== "blocked") {
|
|
401
|
-
throw new Error(`WorkItem ${id} has no accepted rework request`);
|
|
402
|
-
}
|
|
403
|
-
const verifier = [...this.items.values()].find((item) => item.verifierFor === id);
|
|
404
|
-
if (verifier?.state !== "completed") {
|
|
405
|
-
throw new Error(`WorkItem ${id} has no completed verifier for rework`);
|
|
406
|
-
}
|
|
407
|
-
target.artifactHistory.push(...target.artifacts.map((artifact) => structuredClone(artifact)));
|
|
408
|
-
target.artifacts = [];
|
|
409
|
-
target.state = "pending";
|
|
410
|
-
target.acceptanceState = "pending";
|
|
411
|
-
target.taskGeneration++;
|
|
412
|
-
target.assignedAgentId = undefined;
|
|
413
|
-
target.acceptedExecutionPlanId = undefined;
|
|
414
|
-
target.stagedTreeIdentity = undefined;
|
|
415
|
-
target.submission = undefined;
|
|
416
|
-
target.verificationAccepted = false;
|
|
417
|
-
target.outcomeReason = undefined;
|
|
418
|
-
target.generation = ++this.generation;
|
|
419
|
-
verifier.state = "pending";
|
|
420
|
-
verifier.taskGeneration++;
|
|
421
|
-
verifier.assignedAgentId = undefined;
|
|
422
|
-
verifier.acceptedExecutionPlanId = undefined;
|
|
423
|
-
verifier.outcomeReason = undefined;
|
|
424
|
-
verifier.generation = ++this.generation;
|
|
425
|
-
this.resetReworkDependents(target.id, verifier.id);
|
|
426
|
-
this.refreshReadyState();
|
|
427
|
-
return structuredClone(target);
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
failVerification(verifierId: string, reason: string): WorkItemRecord[] {
|
|
431
|
-
const verifier = this.require(verifierId);
|
|
432
|
-
if (!verifier.verifierFor) throw new Error(`WorkItem ${verifierId} is not a verifier`);
|
|
433
|
-
const target = this.require(verifier.verifierFor);
|
|
434
|
-
const boundedReason = bounded(reason, MAX_TEXT_LENGTH);
|
|
435
|
-
if (!boundedReason) throw new Error("Verification failure requires a reason");
|
|
436
|
-
if (!TERMINAL_STATES.has(verifier.state)) {
|
|
437
|
-
verifier.state = "failed";
|
|
438
|
-
verifier.outcomeReason = boundedReason;
|
|
439
|
-
verifier.generation = ++this.generation;
|
|
440
|
-
}
|
|
441
|
-
if (
|
|
442
|
-
target.state === "awaiting-verification" ||
|
|
443
|
-
(target.state === "completed" && target.acceptanceState === "pending")
|
|
444
|
-
) {
|
|
445
|
-
target.state = "failed";
|
|
446
|
-
if (target.acceptanceRequired) target.acceptanceState = "rejected";
|
|
447
|
-
target.verificationAccepted = false;
|
|
448
|
-
target.outcomeReason = boundedReason;
|
|
449
|
-
target.generation = ++this.generation;
|
|
450
|
-
}
|
|
451
|
-
const invalidated = this.invalidateDependents(target.id, verifier.id, boundedReason);
|
|
452
|
-
return [structuredClone(target), structuredClone(verifier), ...invalidated];
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
settle(
|
|
456
|
-
id: string,
|
|
457
|
-
state: "blocked" | "needs-input" | "failed" | "interrupted",
|
|
458
|
-
reason?: string,
|
|
459
|
-
): WorkItemRecord {
|
|
460
|
-
const item = this.require(id);
|
|
461
|
-
this.assertMutable(item);
|
|
462
|
-
if (
|
|
463
|
-
item.state !== "running" &&
|
|
464
|
-
item.state !== "awaiting-verification" &&
|
|
465
|
-
!(item.state === "completed" && item.acceptanceState === "pending") &&
|
|
466
|
-
item.state !== "ready" &&
|
|
467
|
-
item.state !== "pending"
|
|
468
|
-
) {
|
|
469
|
-
throw new Error(`WorkItem ${id} cannot settle while ${item.state}`);
|
|
470
|
-
}
|
|
471
|
-
item.state = state;
|
|
472
|
-
if (item.acceptanceRequired) item.acceptanceState = "rejected";
|
|
473
|
-
item.outcomeReason = reason ? bounded(reason, MAX_TEXT_LENGTH) : undefined;
|
|
474
|
-
item.generation = ++this.generation;
|
|
475
|
-
this.refreshReadyState();
|
|
476
|
-
return structuredClone(item);
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
acceptIntegration(
|
|
480
|
-
id: string,
|
|
481
|
-
expected: ManagedIntegrationExpectation,
|
|
482
|
-
candidate: ManagedIntegrationCandidate,
|
|
483
|
-
verified?: VerifiedIntegrationInput,
|
|
484
|
-
): WorkItemRecord {
|
|
485
|
-
const item = this.require(id);
|
|
486
|
-
if (!item.integrationOwner) {
|
|
487
|
-
throw new Error(`WorkItem ${id} is not the integration owner`);
|
|
488
|
-
}
|
|
489
|
-
if (expected.taskId !== id || expected.taskGeneration !== item.taskGeneration) {
|
|
490
|
-
throw new Error(`WorkItem ${id} integration expectation has a stale generation`);
|
|
491
|
-
}
|
|
492
|
-
if (!verified) {
|
|
493
|
-
if (item.state !== "running" || item.acceptanceRequired) {
|
|
494
|
-
throw new Error(`WorkItem ${id} cannot integrate while ${item.state}`);
|
|
495
|
-
}
|
|
496
|
-
verifyManagedIntegration(expected, candidate);
|
|
497
|
-
item.verificationAccepted = true;
|
|
498
|
-
item.generation = ++this.generation;
|
|
499
|
-
return structuredClone(item);
|
|
500
|
-
}
|
|
501
|
-
if (item.state !== "completed" || item.acceptanceState !== "pending") {
|
|
502
|
-
throw new Error(`WorkItem ${id} is not pending verified acceptance`);
|
|
503
|
-
}
|
|
504
|
-
const verifier = this.require(verified.verifierId);
|
|
505
|
-
if (
|
|
506
|
-
verifier.state !== "running" ||
|
|
507
|
-
verifier.verifierFor !== id ||
|
|
508
|
-
verified.verifierTaskGeneration !== verifier.taskGeneration
|
|
509
|
-
) {
|
|
510
|
-
throw new Error(`WorkItem ${verified.verifierId} is not the current verifier`);
|
|
511
|
-
}
|
|
512
|
-
if (!isVerificationReceipt(verified.receipt) || verified.receipt.decision !== "accept") {
|
|
513
|
-
throw new Error("Managed integration requires an accepting verification receipt");
|
|
514
|
-
}
|
|
515
|
-
assertVerifiedReceiptMatches(
|
|
516
|
-
item,
|
|
517
|
-
verifier,
|
|
518
|
-
verified.verifierExecutionPlanId,
|
|
519
|
-
verified.receipt,
|
|
520
|
-
);
|
|
521
|
-
verifyManagedIntegration(expected, candidate);
|
|
522
|
-
item.verificationAccepted = true;
|
|
523
|
-
item.acceptanceState = "accepted";
|
|
524
|
-
item.acceptanceReceipt = structuredClone(verified.receipt);
|
|
525
|
-
item.acceptanceReceiptHistory.push(structuredClone(verified.receipt));
|
|
526
|
-
item.artifacts = item.artifacts.map((artifact) => ({ ...artifact, verified: true }));
|
|
527
|
-
item.outcomeReason = undefined;
|
|
528
|
-
item.generation = ++this.generation;
|
|
529
|
-
verifier.acceptedExecutionPlanId = verified.verifierExecutionPlanId;
|
|
530
|
-
verifier.state = "completed";
|
|
531
|
-
verifier.generation = ++this.generation;
|
|
532
|
-
this.refreshReadyState();
|
|
533
|
-
return structuredClone(item);
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
rerun(id: string): WorkItemRecord {
|
|
537
|
-
const item = this.require(id);
|
|
538
|
-
if (item.acceptanceState === "accepted") {
|
|
539
|
-
throw new Error(`WorkItem ${id} has terminal accepted evidence`);
|
|
540
|
-
}
|
|
541
|
-
if (
|
|
542
|
-
!["blocked", "needs-input", "failed", "interrupted", "stale", "invalidated"].includes(
|
|
543
|
-
item.state,
|
|
544
|
-
)
|
|
545
|
-
) {
|
|
546
|
-
throw new Error(`WorkItem ${id} cannot rerun while ${item.state}`);
|
|
547
|
-
}
|
|
548
|
-
item.state = "pending";
|
|
549
|
-
item.taskGeneration++;
|
|
550
|
-
item.assignedAgentId = undefined;
|
|
551
|
-
item.acceptedExecutionPlanId = undefined;
|
|
552
|
-
item.outcomeReason = undefined;
|
|
553
|
-
item.verificationAccepted = false;
|
|
554
|
-
item.acceptanceState = item.acceptanceRequired ? "pending" : "not-required";
|
|
555
|
-
item.stagedTreeIdentity = undefined;
|
|
556
|
-
item.submission = undefined;
|
|
557
|
-
item.verificationReceipt = undefined;
|
|
558
|
-
item.acceptanceReceipt = undefined;
|
|
559
|
-
item.generation = ++this.generation;
|
|
560
|
-
this.refreshReadyState();
|
|
561
|
-
return structuredClone(item);
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
invalidate(id: string, reason: string): WorkItemRecord[] {
|
|
565
|
-
const root = this.require(id);
|
|
566
|
-
const normalizedReason = bounded(reason, MAX_TEXT_LENGTH);
|
|
567
|
-
if (!normalizedReason) throw new Error("WorkItem invalidation requires a reason");
|
|
568
|
-
const ordered: WorkItemRecord[] = [];
|
|
569
|
-
const queue = [root.id];
|
|
570
|
-
const seen = new Set<string>();
|
|
571
|
-
while (queue.length > 0) {
|
|
572
|
-
const currentId = queue.shift();
|
|
573
|
-
if (!currentId || seen.has(currentId)) continue;
|
|
574
|
-
seen.add(currentId);
|
|
575
|
-
const item = this.require(currentId);
|
|
576
|
-
if (item.acceptanceState === "accepted") {
|
|
577
|
-
throw new Error(`WorkItem ${item.id} has terminal accepted evidence`);
|
|
578
|
-
}
|
|
579
|
-
ordered.push(item);
|
|
580
|
-
queue.push(...item.dependents);
|
|
581
|
-
}
|
|
582
|
-
return ordered.map((item) => {
|
|
583
|
-
item.state = item.id === root.id ? "stale" : "invalidated";
|
|
584
|
-
if (item.acceptanceRequired) item.acceptanceState = "rejected";
|
|
585
|
-
item.taskGeneration++;
|
|
586
|
-
item.invalidationReasons.push(`${root.id}:${normalizedReason}`);
|
|
587
|
-
item.generation = ++this.generation;
|
|
588
|
-
return structuredClone(item);
|
|
589
|
-
});
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
snapshot(): WorkItemLedgerSnapshot {
|
|
593
|
-
return {
|
|
594
|
-
version: WORK_ITEM_LEDGER_VERSION,
|
|
595
|
-
workflowId: this.workflowId,
|
|
596
|
-
generation: this.generation,
|
|
597
|
-
items: [...this.items.values()]
|
|
598
|
-
.sort((left, right) => left.id.localeCompare(right.id))
|
|
599
|
-
.map((item) => structuredClone(item)),
|
|
600
|
-
};
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
static restore(snapshot: WorkItemLedgerSnapshot): WorkItemLedger {
|
|
604
|
-
const storedVersion = (snapshot as { version?: unknown } | undefined)?.version;
|
|
605
|
-
if (
|
|
606
|
-
!snapshot ||
|
|
607
|
-
(storedVersion !== WORK_ITEM_LEDGER_VERSION &&
|
|
608
|
-
!LEGACY_WORK_ITEM_LEDGER_VERSIONS.has(String(storedVersion))) ||
|
|
609
|
-
!Number.isSafeInteger(snapshot.generation) ||
|
|
610
|
-
snapshot.generation < 0
|
|
611
|
-
) {
|
|
612
|
-
throw new Error("Unsupported or malformed WorkItem ledger snapshot");
|
|
613
|
-
}
|
|
614
|
-
const isLegacySnapshot = storedVersion !== WORK_ITEM_LEDGER_VERSION;
|
|
615
|
-
const isV1Snapshot = storedVersion === "pi-subagents:work-ledger:v1";
|
|
616
|
-
if (
|
|
617
|
-
!Array.isArray(snapshot.items) ||
|
|
618
|
-
snapshot.items.length < 1 ||
|
|
619
|
-
snapshot.items.length > MAX_ITEMS
|
|
620
|
-
) {
|
|
621
|
-
throw new Error("Malformed WorkItem ledger items");
|
|
622
|
-
}
|
|
623
|
-
const storedItems = snapshot.items.map((item) =>
|
|
624
|
-
isLegacySnapshot ? normalizeLegacyAcceptance(item, isV1Snapshot) : item,
|
|
625
|
-
);
|
|
626
|
-
for (const item of storedItems) validateStoredRecord(item, snapshot.generation);
|
|
627
|
-
const ledger = WorkItemLedger.create({
|
|
628
|
-
workflowId: snapshot.workflowId,
|
|
629
|
-
items: storedItems.map((item) => ({
|
|
630
|
-
id: item.id,
|
|
631
|
-
objective: item.objective,
|
|
632
|
-
dependencies: item.dependencies,
|
|
633
|
-
inputArtifacts: item.inputArtifacts,
|
|
634
|
-
inputArtifactVersions: item.requiredArtifactVersions,
|
|
635
|
-
requiredCapabilities: item.requiredCapabilities,
|
|
636
|
-
requiredTools: item.requiredTools,
|
|
637
|
-
selectedAgentName: item.selectedAgentName,
|
|
638
|
-
sideEffectPolicy: item.sideEffectPolicy,
|
|
639
|
-
readPaths: item.readPaths,
|
|
640
|
-
writePaths: item.writePaths,
|
|
641
|
-
ownershipKeys: item.ownershipKeys,
|
|
642
|
-
acceptanceCriteria: item.acceptanceCriteria,
|
|
643
|
-
requiredEvidence: item.requiredEvidence,
|
|
644
|
-
integrationOwner: item.integrationOwner,
|
|
645
|
-
verifierFor: item.verifierFor,
|
|
646
|
-
dependencyPolicy: item.dependencyPolicy,
|
|
647
|
-
acceptanceRequired: item.acceptanceRequired,
|
|
648
|
-
maxReworkCycles: item.maxReworkCycles,
|
|
649
|
-
})),
|
|
650
|
-
});
|
|
651
|
-
ledger.generation = snapshot.generation;
|
|
652
|
-
for (const stored of storedItems) {
|
|
653
|
-
const item = ledger.require(stored.id);
|
|
654
|
-
const interruptedAcceptance =
|
|
655
|
-
stored.acceptanceRequired &&
|
|
656
|
-
stored.state === "completed" &&
|
|
657
|
-
stored.acceptanceState === "pending";
|
|
658
|
-
item.state =
|
|
659
|
-
stored.state === "running" ||
|
|
660
|
-
stored.state === "awaiting-verification" ||
|
|
661
|
-
interruptedAcceptance
|
|
662
|
-
? "interrupted"
|
|
663
|
-
: stored.state;
|
|
664
|
-
item.generation = stored.generation;
|
|
665
|
-
item.taskGeneration = stored.taskGeneration ?? 1;
|
|
666
|
-
item.assignedAgentId = stored.assignedAgentId;
|
|
667
|
-
item.acceptedExecutionPlanId = stored.acceptedExecutionPlanId;
|
|
668
|
-
item.inputArtifactVersions = { ...stored.inputArtifactVersions };
|
|
669
|
-
item.artifacts = normalizeStoredArtifacts(
|
|
670
|
-
stored.artifacts,
|
|
671
|
-
stored.id,
|
|
672
|
-
stored.generation,
|
|
673
|
-
!isV1Snapshot,
|
|
674
|
-
);
|
|
675
|
-
item.artifactHistory = normalizeStoredArtifacts(
|
|
676
|
-
stored.artifactHistory ?? [],
|
|
677
|
-
stored.id,
|
|
678
|
-
stored.generation,
|
|
679
|
-
!isV1Snapshot,
|
|
680
|
-
);
|
|
681
|
-
item.acceptanceStateVersion = WORK_ITEM_ACCEPTANCE_VERSION;
|
|
682
|
-
item.acceptanceRequired = stored.acceptanceRequired;
|
|
683
|
-
item.acceptanceState = interruptedAcceptance ? "rejected" : stored.acceptanceState;
|
|
684
|
-
item.reworkCount = stored.reworkCount;
|
|
685
|
-
item.maxReworkCycles = stored.maxReworkCycles;
|
|
686
|
-
item.verificationAccepted = isV1Snapshot ? false : stored.verificationAccepted;
|
|
687
|
-
item.stagedTreeIdentity = stored.stagedTreeIdentity
|
|
688
|
-
? structuredClone(stored.stagedTreeIdentity)
|
|
689
|
-
: undefined;
|
|
690
|
-
item.verificationReceipt =
|
|
691
|
-
!isV1Snapshot && stored.verificationReceipt
|
|
692
|
-
? structuredClone(stored.verificationReceipt)
|
|
693
|
-
: undefined;
|
|
694
|
-
item.acceptanceReceipt = stored.acceptanceReceipt
|
|
695
|
-
? structuredClone(stored.acceptanceReceipt)
|
|
696
|
-
: undefined;
|
|
697
|
-
item.acceptanceReceiptHistory = stored.acceptanceReceiptHistory.map((receipt) =>
|
|
698
|
-
structuredClone(receipt),
|
|
699
|
-
);
|
|
700
|
-
item.submission = stored.submission ? structuredClone(stored.submission) : undefined;
|
|
701
|
-
item.invalidationReasons = [...stored.invalidationReasons];
|
|
702
|
-
item.outcomeReason = interruptedAcceptance
|
|
703
|
-
? "verification-interrupted"
|
|
704
|
-
: stored.outcomeReason;
|
|
705
|
-
}
|
|
706
|
-
ledger.validateRestoredVerificationLinks();
|
|
707
|
-
return ledger;
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
private validateRestoredVerificationLinks(): void {
|
|
711
|
-
for (const target of this.items.values()) {
|
|
712
|
-
const receipt = target.verificationReceipt;
|
|
713
|
-
if (receipt) {
|
|
714
|
-
const verifier = this.items.get(receipt.verifierTaskId);
|
|
715
|
-
if (
|
|
716
|
-
!verifier ||
|
|
717
|
-
verifier.verifierFor !== target.id ||
|
|
718
|
-
verifier.state !== "completed" ||
|
|
719
|
-
verifier.taskGeneration !== receipt.verifierTaskGeneration ||
|
|
720
|
-
verifier.acceptedExecutionPlanId !== receipt.verifierExecutionPlanId
|
|
721
|
-
) {
|
|
722
|
-
throw new Error(`Malformed stored WorkItem verification link for ${target.id}`);
|
|
723
|
-
}
|
|
724
|
-
}
|
|
725
|
-
const acceptanceReceipt = target.acceptanceReceipt;
|
|
726
|
-
if (!acceptanceReceipt || target.acceptanceState === "pending") continue;
|
|
727
|
-
const verifier = this.items.get(acceptanceReceipt.verifierTaskId);
|
|
728
|
-
if (
|
|
729
|
-
!verifier ||
|
|
730
|
-
verifier.verifierFor !== target.id ||
|
|
731
|
-
verifier.state !== "completed" ||
|
|
732
|
-
verifier.taskGeneration !== acceptanceReceipt.verifierTaskGeneration ||
|
|
733
|
-
verifier.acceptedExecutionPlanId !== acceptanceReceipt.verifierExecutionPlanId ||
|
|
734
|
-
acceptanceReceipt.targetTaskId !== target.id ||
|
|
735
|
-
acceptanceReceipt.targetTaskGeneration !== target.taskGeneration ||
|
|
736
|
-
acceptanceReceipt.targetExecutionPlanId !== target.acceptedExecutionPlanId
|
|
737
|
-
) {
|
|
738
|
-
throw new Error(`Malformed stored WorkItem acceptance link for ${target.id}`);
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
}
|
|
742
|
-
|
|
743
|
-
private addDefinition(definition: WorkItemDefinition): void {
|
|
744
|
-
validateIdentifier(definition.id, "WorkItem id");
|
|
745
|
-
if (this.items.has(definition.id)) throw new Error(`Duplicate WorkItem id ${definition.id}`);
|
|
746
|
-
const objective = bounded(definition.objective, MAX_TEXT_LENGTH);
|
|
747
|
-
if (!objective) throw new Error(`WorkItem ${definition.id} requires an objective`);
|
|
748
|
-
this.items.set(definition.id, {
|
|
749
|
-
id: definition.id,
|
|
750
|
-
objective,
|
|
751
|
-
dependencies: uniqueBounded(definition.dependencies, "dependency"),
|
|
752
|
-
dependents: [],
|
|
753
|
-
state: "pending",
|
|
754
|
-
generation: 0,
|
|
755
|
-
taskGeneration: 1,
|
|
756
|
-
acceptedExecutionPlanId: undefined,
|
|
757
|
-
inputArtifacts: uniqueBounded(definition.inputArtifacts ?? [], "input artifact"),
|
|
758
|
-
inputArtifactVersions: {},
|
|
759
|
-
requiredArtifactVersions: normalizeVersionMap(definition.inputArtifactVersions),
|
|
760
|
-
requiredCapabilities: uniqueBounded(
|
|
761
|
-
definition.requiredCapabilities ?? [],
|
|
762
|
-
"required capability",
|
|
763
|
-
),
|
|
764
|
-
requiredTools: uniqueBounded(definition.requiredTools ?? [], "required tool"),
|
|
765
|
-
selectedAgentName: definition.selectedAgentName,
|
|
766
|
-
sideEffectPolicy: definition.sideEffectPolicy ?? "mutating",
|
|
767
|
-
artifacts: [],
|
|
768
|
-
artifactHistory: [],
|
|
769
|
-
readPaths: uniqueBounded(definition.readPaths ?? [], "read path"),
|
|
770
|
-
writePaths: uniqueBounded(definition.writePaths ?? [], "write path"),
|
|
771
|
-
ownershipKeys: uniqueBounded(definition.ownershipKeys ?? [], "ownership key"),
|
|
772
|
-
acceptanceCriteria: uniqueBounded(
|
|
773
|
-
definition.acceptanceCriteria ?? [],
|
|
774
|
-
"acceptance criterion",
|
|
775
|
-
),
|
|
776
|
-
requiredEvidence: uniqueBounded(definition.requiredEvidence ?? [], "required evidence"),
|
|
777
|
-
integrationOwner: definition.integrationOwner === true,
|
|
778
|
-
verifierFor: definition.verifierFor,
|
|
779
|
-
dependencyPolicy: definition.dependencyPolicy ?? "completed",
|
|
780
|
-
acceptanceStateVersion: WORK_ITEM_ACCEPTANCE_VERSION,
|
|
781
|
-
acceptanceRequired: definition.acceptanceRequired === true,
|
|
782
|
-
acceptanceState: definition.acceptanceRequired === true ? "pending" : "not-required",
|
|
783
|
-
reworkCount: 0,
|
|
784
|
-
maxReworkCycles: definition.maxReworkCycles === 0 ? 0 : 1,
|
|
785
|
-
verificationAccepted: false,
|
|
786
|
-
stagedTreeIdentity: undefined,
|
|
787
|
-
verificationReceipt: undefined,
|
|
788
|
-
acceptanceReceipt: undefined,
|
|
789
|
-
acceptanceReceiptHistory: [],
|
|
790
|
-
submission: undefined,
|
|
791
|
-
invalidationReasons: [],
|
|
792
|
-
outcomeReason: undefined,
|
|
793
|
-
});
|
|
794
|
-
}
|
|
795
|
-
|
|
796
|
-
private linkAndValidate(): void {
|
|
797
|
-
const integrationOwners = [...this.items.values()].filter((item) => item.integrationOwner);
|
|
798
|
-
if (integrationOwners.length > 1)
|
|
799
|
-
throw new Error("Workflow can have only one integration owner");
|
|
800
|
-
for (const item of this.items.values()) {
|
|
801
|
-
if (item.acceptanceRequired && !item.integrationOwner) {
|
|
802
|
-
throw new Error(`WorkItem ${item.id} requires acceptance but is not integration owner`);
|
|
803
|
-
}
|
|
804
|
-
for (const dependency of item.dependencies) {
|
|
805
|
-
if (dependency === item.id) throw new Error(`WorkItem ${item.id} has a self cycle`);
|
|
806
|
-
const parent = this.items.get(dependency);
|
|
807
|
-
if (!parent) throw new Error(`WorkItem ${item.id} has missing dependency ${dependency}`);
|
|
808
|
-
parent.dependents.push(item.id);
|
|
809
|
-
}
|
|
810
|
-
if (item.verifierFor && !this.items.has(item.verifierFor)) {
|
|
811
|
-
throw new Error(`WorkItem ${item.id} verifies missing WorkItem ${item.verifierFor}`);
|
|
812
|
-
}
|
|
813
|
-
if (item.verifierFor && !item.dependencies.includes(item.verifierFor)) {
|
|
814
|
-
throw new Error(
|
|
815
|
-
`WorkItem ${item.id} must depend on the WorkItem it verifies (${item.verifierFor})`,
|
|
816
|
-
);
|
|
817
|
-
}
|
|
818
|
-
}
|
|
819
|
-
const visiting = new Set<string>();
|
|
820
|
-
const visited = new Set<string>();
|
|
821
|
-
const visit = (id: string) => {
|
|
822
|
-
if (visiting.has(id)) throw new Error(`WorkItem dependency cycle includes ${id}`);
|
|
823
|
-
if (visited.has(id)) return;
|
|
824
|
-
visiting.add(id);
|
|
825
|
-
for (const dependency of this.require(id).dependencies) visit(dependency);
|
|
826
|
-
visiting.delete(id);
|
|
827
|
-
visited.add(id);
|
|
828
|
-
};
|
|
829
|
-
for (const id of this.items.keys()) visit(id);
|
|
830
|
-
}
|
|
831
|
-
|
|
832
|
-
private refreshReadyState(): void {
|
|
833
|
-
for (const item of this.items.values()) {
|
|
834
|
-
if (item.state !== "pending") continue;
|
|
835
|
-
const dependencies = item.dependencies.map((id) => this.require(id));
|
|
836
|
-
const acceptedDependency = (dependency: WorkItemRecord) =>
|
|
837
|
-
dependency.state === "completed" &&
|
|
838
|
-
(!dependency.acceptanceRequired || dependency.acceptanceState === "accepted");
|
|
839
|
-
const dependenciesReady = item.verifierFor
|
|
840
|
-
? dependencies.every((dependency) =>
|
|
841
|
-
dependency.id === item.verifierFor
|
|
842
|
-
? dependency.acceptanceRequired
|
|
843
|
-
? dependency.state === "completed" && dependency.acceptanceState === "pending"
|
|
844
|
-
: dependency.state === "awaiting-verification"
|
|
845
|
-
: acceptedDependency(dependency),
|
|
846
|
-
)
|
|
847
|
-
: item.dependencyPolicy === "settled"
|
|
848
|
-
? dependencies.every(
|
|
849
|
-
(dependency) =>
|
|
850
|
-
(!dependency.acceptanceRequired || dependency.acceptanceState === "accepted") &&
|
|
851
|
-
!["pending", "ready", "running", "awaiting-verification"].includes(
|
|
852
|
-
dependency.state,
|
|
853
|
-
),
|
|
854
|
-
)
|
|
855
|
-
: dependencies.every(acceptedDependency);
|
|
856
|
-
if (!dependenciesReady) continue;
|
|
857
|
-
const available = new Map<string, WorkArtifactReference>();
|
|
858
|
-
for (const dependency of dependencies) {
|
|
859
|
-
for (const artifact of dependency.artifacts) available.set(artifact.id, artifact);
|
|
860
|
-
}
|
|
861
|
-
if (!item.inputArtifacts.every((artifact) => available.has(artifact))) continue;
|
|
862
|
-
if (
|
|
863
|
-
!Object.entries(item.requiredArtifactVersions).every(
|
|
864
|
-
([id, version]) => available.get(id)?.version === version,
|
|
865
|
-
)
|
|
866
|
-
) {
|
|
867
|
-
continue;
|
|
868
|
-
}
|
|
869
|
-
item.inputArtifactVersions = Object.fromEntries(
|
|
870
|
-
item.inputArtifacts.map((id) => [id, available.get(id)?.version ?? "unknown"]),
|
|
871
|
-
);
|
|
872
|
-
item.state = "ready";
|
|
873
|
-
item.generation = this.generation;
|
|
874
|
-
}
|
|
875
|
-
}
|
|
876
|
-
|
|
877
|
-
private resetReworkDependents(targetId: string, excludedId: string): void {
|
|
878
|
-
const target = this.require(targetId);
|
|
879
|
-
const reworkReason = `${targetId}:verification-rework`;
|
|
880
|
-
const queue = target.dependents.filter((id) => id !== excludedId);
|
|
881
|
-
const seen = new Set<string>();
|
|
882
|
-
while (queue.length > 0) {
|
|
883
|
-
const currentId = queue.shift();
|
|
884
|
-
if (!currentId || seen.has(currentId)) continue;
|
|
885
|
-
seen.add(currentId);
|
|
886
|
-
const current = this.require(currentId);
|
|
887
|
-
queue.push(...current.dependents.filter((id) => id !== excludedId));
|
|
888
|
-
if (current.state !== "invalidated" || current.invalidationReasons.at(-1) !== reworkReason) {
|
|
889
|
-
continue;
|
|
890
|
-
}
|
|
891
|
-
current.artifactHistory.push(
|
|
892
|
-
...current.artifacts.map((artifact) => structuredClone(artifact)),
|
|
893
|
-
);
|
|
894
|
-
current.artifacts = [];
|
|
895
|
-
current.inputArtifactVersions = {};
|
|
896
|
-
current.state = "pending";
|
|
897
|
-
current.assignedAgentId = undefined;
|
|
898
|
-
current.acceptedExecutionPlanId = undefined;
|
|
899
|
-
current.outcomeReason = undefined;
|
|
900
|
-
current.verificationAccepted = false;
|
|
901
|
-
current.acceptanceState = current.acceptanceRequired ? "pending" : "not-required";
|
|
902
|
-
current.stagedTreeIdentity = undefined;
|
|
903
|
-
current.submission = undefined;
|
|
904
|
-
current.verificationReceipt = undefined;
|
|
905
|
-
current.acceptanceReceipt = undefined;
|
|
906
|
-
current.generation = ++this.generation;
|
|
907
|
-
}
|
|
908
|
-
}
|
|
909
|
-
|
|
910
|
-
private invalidateDependents(
|
|
911
|
-
targetId: string,
|
|
912
|
-
excludedId: string,
|
|
913
|
-
reason: string | undefined,
|
|
914
|
-
): WorkItemRecord[] {
|
|
915
|
-
const target = this.require(targetId);
|
|
916
|
-
const normalizedReason = bounded(reason ?? "verification-not-accepted", MAX_TEXT_LENGTH);
|
|
917
|
-
const queue = target.dependents.filter((id) => id !== excludedId);
|
|
918
|
-
const seen = new Set<string>();
|
|
919
|
-
const affected: WorkItemRecord[] = [];
|
|
920
|
-
while (queue.length > 0) {
|
|
921
|
-
const currentId = queue.shift();
|
|
922
|
-
if (!currentId || seen.has(currentId)) continue;
|
|
923
|
-
seen.add(currentId);
|
|
924
|
-
const current = this.require(currentId);
|
|
925
|
-
current.state = "invalidated";
|
|
926
|
-
current.taskGeneration++;
|
|
927
|
-
current.invalidationReasons.push(`${targetId}:${normalizedReason}`);
|
|
928
|
-
current.generation = ++this.generation;
|
|
929
|
-
affected.push(structuredClone(current));
|
|
930
|
-
queue.push(...current.dependents.filter((id) => id !== excludedId));
|
|
931
|
-
}
|
|
932
|
-
return affected;
|
|
933
|
-
}
|
|
934
|
-
|
|
935
|
-
private require(id: string): WorkItemRecord {
|
|
936
|
-
const item = this.items.get(id);
|
|
937
|
-
if (!item) throw new Error(`Unknown WorkItem ${id}`);
|
|
938
|
-
return item;
|
|
939
|
-
}
|
|
940
|
-
|
|
941
|
-
private assertMutable(item: WorkItemRecord): void {
|
|
942
|
-
if (
|
|
943
|
-
TERMINAL_STATES.has(item.state) &&
|
|
944
|
-
!(item.state === "completed" && item.acceptanceState === "pending")
|
|
945
|
-
) {
|
|
946
|
-
throw new Error(`WorkItem ${item.id} is terminal (${item.state})`);
|
|
947
|
-
}
|
|
948
|
-
}
|
|
949
|
-
}
|
|
950
|
-
|
|
951
|
-
function assertReceiptMatches(
|
|
952
|
-
target: WorkItemRecord,
|
|
953
|
-
verifier: WorkItemRecord,
|
|
954
|
-
verifierExecutionPlanId: string,
|
|
955
|
-
receipt: WorkflowVerificationReceipt,
|
|
956
|
-
): void {
|
|
957
|
-
if (
|
|
958
|
-
receipt.targetTaskId !== target.id ||
|
|
959
|
-
receipt.targetTaskGeneration !== target.taskGeneration ||
|
|
960
|
-
receipt.targetExecutionPlanId !== target.acceptedExecutionPlanId ||
|
|
961
|
-
receipt.verifierTaskId !== verifier.id ||
|
|
962
|
-
receipt.verifierTaskGeneration !== verifier.taskGeneration ||
|
|
963
|
-
receipt.verifierExecutionPlanId !== verifierExecutionPlanId ||
|
|
964
|
-
!target.stagedTreeIdentity ||
|
|
965
|
-
!sameWorkflowTreeIdentity(receipt.treeIdentity, target.stagedTreeIdentity)
|
|
966
|
-
) {
|
|
967
|
-
throw new Error("WorkItem verification receipt has stale or mismatched executor identity");
|
|
968
|
-
}
|
|
969
|
-
}
|
|
970
|
-
|
|
971
|
-
function assertVerifiedReceiptMatches(
|
|
972
|
-
target: WorkItemRecord,
|
|
973
|
-
verifier: WorkItemRecord,
|
|
974
|
-
verifierExecutionPlanId: string,
|
|
975
|
-
receipt: VerificationReceipt,
|
|
976
|
-
): void {
|
|
977
|
-
if (
|
|
978
|
-
!target.acceptanceRequired ||
|
|
979
|
-
target.acceptanceState !== "pending" ||
|
|
980
|
-
target.state !== "completed" ||
|
|
981
|
-
!target.submission ||
|
|
982
|
-
receipt.targetTaskId !== target.id ||
|
|
983
|
-
receipt.targetTaskGeneration !== target.taskGeneration ||
|
|
984
|
-
receipt.targetExecutionPlanId !== target.acceptedExecutionPlanId ||
|
|
985
|
-
receipt.verifierTaskId !== verifier.id ||
|
|
986
|
-
receipt.verifierTaskGeneration !== verifier.taskGeneration ||
|
|
987
|
-
receipt.verifierExecutionPlanId !== verifierExecutionPlanId ||
|
|
988
|
-
receipt.verifierAgent !== verifier.assignedAgentId?.replace(/^agent:/u, "") ||
|
|
989
|
-
!sameWorkflowTreeIdentity(receipt.beforeTreeIdentity, target.submission.treeIdentity) ||
|
|
990
|
-
!sameWorkflowTreeIdentity(receipt.afterTreeIdentity, target.submission.treeIdentity) ||
|
|
991
|
-
receipt.baseRepositoryGeneration !== target.submission.baseRepositoryGeneration ||
|
|
992
|
-
receipt.patchDigest !== target.submission.patchDigest ||
|
|
993
|
-
JSON.stringify(receipt.changedPaths) !== JSON.stringify(target.submission.changedPaths) ||
|
|
994
|
-
JSON.stringify(receipt.allowedScopes) !== JSON.stringify(target.writePaths) ||
|
|
995
|
-
JSON.stringify(receipt.dependencyVersions) !== JSON.stringify(target.inputArtifactVersions) ||
|
|
996
|
-
JSON.stringify(receipt.readSetVersions) !== JSON.stringify(target.submission.fileVersions) ||
|
|
997
|
-
JSON.stringify(receipt.acceptanceCriteria) !== JSON.stringify(target.acceptanceCriteria) ||
|
|
998
|
-
JSON.stringify(receipt.requiredEvidenceIds) !== JSON.stringify(target.requiredEvidence)
|
|
999
|
-
) {
|
|
1000
|
-
throw new Error("WorkItem verification receipt has stale or mismatched managed identity");
|
|
1001
|
-
}
|
|
1002
|
-
}
|
|
1003
|
-
|
|
1004
|
-
function validatePlanId(value: string, label: string): void {
|
|
1005
|
-
if (!/^[a-f0-9]{64}$/u.test(value)) throw new Error(`Invalid ${label}`);
|
|
1006
|
-
}
|
|
1007
|
-
|
|
1008
|
-
function validateSubmission(value: VerificationSubmission): void {
|
|
1009
|
-
if (
|
|
1010
|
-
!isWorkflowTreeIdentity(value.treeIdentity) ||
|
|
1011
|
-
!/^[a-f0-9]{40,64}$/u.test(value.baseRepositoryGeneration) ||
|
|
1012
|
-
!/^[a-f0-9]{64}$/u.test(value.patchDigest) ||
|
|
1013
|
-
!Array.isArray(value.changedPaths) ||
|
|
1014
|
-
value.changedPaths.length > MAX_LIST_ITEMS ||
|
|
1015
|
-
value.changedPaths.some(
|
|
1016
|
-
(item) =>
|
|
1017
|
-
typeof item !== "string" ||
|
|
1018
|
-
!item ||
|
|
1019
|
-
item.length > 4096 ||
|
|
1020
|
-
item.startsWith("/") ||
|
|
1021
|
-
item.startsWith("\\") ||
|
|
1022
|
-
item.split(/[\\/]/u).includes(".."),
|
|
1023
|
-
) ||
|
|
1024
|
-
new Set(value.changedPaths).size !== value.changedPaths.length ||
|
|
1025
|
-
value.changedPaths.reduce((total, item) => total + Buffer.byteLength(item, "utf8"), 0) >
|
|
1026
|
-
8 * 1024 ||
|
|
1027
|
-
JSON.stringify([...value.changedPaths].sort((left, right) => left.localeCompare(right))) !==
|
|
1028
|
-
JSON.stringify(value.changedPaths) ||
|
|
1029
|
-
!value.fileVersions ||
|
|
1030
|
-
typeof value.fileVersions !== "object" ||
|
|
1031
|
-
Array.isArray(value.fileVersions) ||
|
|
1032
|
-
Object.entries(value.fileVersions).length > MAX_LIST_ITEMS ||
|
|
1033
|
-
JSON.stringify(
|
|
1034
|
-
Object.keys(value.fileVersions).sort((left, right) => left.localeCompare(right)),
|
|
1035
|
-
) !== JSON.stringify(value.changedPaths) ||
|
|
1036
|
-
Object.entries(value.fileVersions).some(
|
|
1037
|
-
([key, digest]) =>
|
|
1038
|
-
!key ||
|
|
1039
|
-
key.length > 4096 ||
|
|
1040
|
-
typeof digest !== "string" ||
|
|
1041
|
-
(digest !== "deleted" && !/^[a-f0-9]{64}$/u.test(digest)),
|
|
1042
|
-
)
|
|
1043
|
-
) {
|
|
1044
|
-
throw new Error("WorkItem received malformed verification submission metadata");
|
|
1045
|
-
}
|
|
1046
|
-
}
|
|
1047
|
-
|
|
1048
|
-
function validSubmission(value: VerificationSubmission): boolean {
|
|
1049
|
-
try {
|
|
1050
|
-
validateSubmission(value);
|
|
1051
|
-
return true;
|
|
1052
|
-
} catch {
|
|
1053
|
-
return false;
|
|
1054
|
-
}
|
|
1055
|
-
}
|
|
1056
|
-
|
|
1057
|
-
function normalizeLegacyAcceptance(item: WorkItemRecord, isV1: boolean): WorkItemRecord {
|
|
1058
|
-
return {
|
|
1059
|
-
...structuredClone(item),
|
|
1060
|
-
requiredEvidence: [],
|
|
1061
|
-
acceptanceStateVersion: WORK_ITEM_ACCEPTANCE_VERSION,
|
|
1062
|
-
acceptanceRequired: false,
|
|
1063
|
-
acceptanceState: "not-required",
|
|
1064
|
-
reworkCount: 0,
|
|
1065
|
-
maxReworkCycles: 1,
|
|
1066
|
-
verificationAccepted: isV1 ? false : item.verificationAccepted,
|
|
1067
|
-
acceptanceReceipt: undefined,
|
|
1068
|
-
acceptanceReceiptHistory: [],
|
|
1069
|
-
submission: undefined,
|
|
1070
|
-
};
|
|
1071
|
-
}
|
|
1072
|
-
|
|
1073
|
-
function validateStoredRecord(item: WorkItemRecord, ledgerGeneration: number): void {
|
|
1074
|
-
const states: WorkItemState[] = [
|
|
1075
|
-
"pending",
|
|
1076
|
-
"ready",
|
|
1077
|
-
"running",
|
|
1078
|
-
"awaiting-verification",
|
|
1079
|
-
"blocked",
|
|
1080
|
-
"needs-input",
|
|
1081
|
-
"completed",
|
|
1082
|
-
"failed",
|
|
1083
|
-
"interrupted",
|
|
1084
|
-
"stale",
|
|
1085
|
-
"invalidated",
|
|
1086
|
-
];
|
|
1087
|
-
if (
|
|
1088
|
-
!item ||
|
|
1089
|
-
typeof item !== "object" ||
|
|
1090
|
-
typeof item.objective !== "string" ||
|
|
1091
|
-
item.objective.length === 0 ||
|
|
1092
|
-
item.objective.length > MAX_TEXT_LENGTH ||
|
|
1093
|
-
item.objective.trim() !== item.objective ||
|
|
1094
|
-
!states.includes(item.state) ||
|
|
1095
|
-
!Number.isSafeInteger(item.generation) ||
|
|
1096
|
-
item.generation < 0 ||
|
|
1097
|
-
item.generation > ledgerGeneration ||
|
|
1098
|
-
!Number.isSafeInteger(item.taskGeneration) ||
|
|
1099
|
-
item.taskGeneration < 1 ||
|
|
1100
|
-
!Array.isArray(item.artifacts) ||
|
|
1101
|
-
!validStoredArtifacts(item.artifacts, item.id, item.generation) ||
|
|
1102
|
-
(item.artifactHistory !== undefined && !Array.isArray(item.artifactHistory)) ||
|
|
1103
|
-
(item.artifactHistory !== undefined &&
|
|
1104
|
-
!validStoredArtifacts(item.artifactHistory, item.id, item.generation)) ||
|
|
1105
|
-
!item.inputArtifactVersions ||
|
|
1106
|
-
typeof item.inputArtifactVersions !== "object" ||
|
|
1107
|
-
Array.isArray(item.inputArtifactVersions) ||
|
|
1108
|
-
(item.assignedAgentId !== undefined && !isValidIdentifier(item.assignedAgentId)) ||
|
|
1109
|
-
(item.selectedAgentName !== undefined &&
|
|
1110
|
-
(typeof item.selectedAgentName !== "string" ||
|
|
1111
|
-
item.selectedAgentName.length === 0 ||
|
|
1112
|
-
item.selectedAgentName.length > MAX_IDENTIFIER_LENGTH ||
|
|
1113
|
-
item.selectedAgentName.trim() !== item.selectedAgentName)) ||
|
|
1114
|
-
item.acceptanceStateVersion !== WORK_ITEM_ACCEPTANCE_VERSION ||
|
|
1115
|
-
typeof item.acceptanceRequired !== "boolean" ||
|
|
1116
|
-
!["not-required", "pending", "accepted", "rework-requested", "rejected"].includes(
|
|
1117
|
-
item.acceptanceState,
|
|
1118
|
-
) ||
|
|
1119
|
-
!Number.isSafeInteger(item.reworkCount) ||
|
|
1120
|
-
item.reworkCount < 0 ||
|
|
1121
|
-
item.reworkCount > 1 ||
|
|
1122
|
-
(item.maxReworkCycles !== 0 && item.maxReworkCycles !== 1) ||
|
|
1123
|
-
(item.acceptanceRequired && !item.integrationOwner) ||
|
|
1124
|
-
(!item.acceptanceRequired && item.acceptanceState !== "not-required") ||
|
|
1125
|
-
typeof item.integrationOwner !== "boolean" ||
|
|
1126
|
-
!(["completed", "settled"] as const).includes(item.dependencyPolicy) ||
|
|
1127
|
-
!(["read-only", "idempotent", "mutating"] as const).includes(item.sideEffectPolicy) ||
|
|
1128
|
-
(item.verifierFor !== undefined && !isValidIdentifier(item.verifierFor)) ||
|
|
1129
|
-
![
|
|
1130
|
-
item.dependencies,
|
|
1131
|
-
item.dependents,
|
|
1132
|
-
item.inputArtifacts,
|
|
1133
|
-
item.requiredCapabilities,
|
|
1134
|
-
item.requiredTools,
|
|
1135
|
-
item.readPaths,
|
|
1136
|
-
item.writePaths,
|
|
1137
|
-
item.ownershipKeys,
|
|
1138
|
-
item.acceptanceCriteria,
|
|
1139
|
-
item.requiredEvidence,
|
|
1140
|
-
].every(
|
|
1141
|
-
(values) =>
|
|
1142
|
-
Array.isArray(values) &&
|
|
1143
|
-
values.every(
|
|
1144
|
-
(value) =>
|
|
1145
|
-
typeof value === "string" &&
|
|
1146
|
-
value.length > 0 &&
|
|
1147
|
-
value.length <= MAX_TEXT_LENGTH &&
|
|
1148
|
-
value.trim() === value,
|
|
1149
|
-
),
|
|
1150
|
-
) ||
|
|
1151
|
-
!item.requiredArtifactVersions ||
|
|
1152
|
-
typeof item.requiredArtifactVersions !== "object" ||
|
|
1153
|
-
Array.isArray(item.requiredArtifactVersions) ||
|
|
1154
|
-
!Object.entries(item.requiredArtifactVersions).every(
|
|
1155
|
-
([id, version]) =>
|
|
1156
|
-
isValidIdentifier(id) &&
|
|
1157
|
-
typeof version === "string" &&
|
|
1158
|
-
version.length > 0 &&
|
|
1159
|
-
version.length <= MAX_IDENTIFIER_LENGTH &&
|
|
1160
|
-
version.trim() === version,
|
|
1161
|
-
) ||
|
|
1162
|
-
(item.acceptedExecutionPlanId !== undefined &&
|
|
1163
|
-
(typeof item.acceptedExecutionPlanId !== "string" ||
|
|
1164
|
-
!/^[a-f0-9]{64}$/u.test(item.acceptedExecutionPlanId))) ||
|
|
1165
|
-
!Object.entries(item.inputArtifactVersions).every(
|
|
1166
|
-
([id, value]) =>
|
|
1167
|
-
isValidIdentifier(id) &&
|
|
1168
|
-
typeof value === "string" &&
|
|
1169
|
-
value.length > 0 &&
|
|
1170
|
-
value.length <= MAX_IDENTIFIER_LENGTH &&
|
|
1171
|
-
value.trim() === value,
|
|
1172
|
-
) ||
|
|
1173
|
-
!Array.isArray(item.invalidationReasons) ||
|
|
1174
|
-
!item.invalidationReasons.every(
|
|
1175
|
-
(reason) =>
|
|
1176
|
-
typeof reason === "string" &&
|
|
1177
|
-
reason.length > 0 &&
|
|
1178
|
-
reason.length <= MAX_TEXT_LENGTH &&
|
|
1179
|
-
reason.trim() === reason,
|
|
1180
|
-
) ||
|
|
1181
|
-
(item.outcomeReason !== undefined &&
|
|
1182
|
-
(typeof item.outcomeReason !== "string" ||
|
|
1183
|
-
item.outcomeReason.length > MAX_TEXT_LENGTH ||
|
|
1184
|
-
item.outcomeReason.trim() !== item.outcomeReason)) ||
|
|
1185
|
-
typeof item.verificationAccepted !== "boolean" ||
|
|
1186
|
-
(item.stagedTreeIdentity !== undefined && !isWorkflowTreeIdentity(item.stagedTreeIdentity)) ||
|
|
1187
|
-
(item.verificationReceipt !== undefined &&
|
|
1188
|
-
(!isWorkflowVerificationReceipt(item.verificationReceipt) ||
|
|
1189
|
-
!storedVerificationMatchesItem(item, item.verificationReceipt))) ||
|
|
1190
|
-
(item.acceptanceReceipt !== undefined &&
|
|
1191
|
-
(!isVerificationReceipt(item.acceptanceReceipt) ||
|
|
1192
|
-
(item.acceptanceState !== "pending" &&
|
|
1193
|
-
!storedAcceptanceMatchesItem(item, item.acceptanceReceipt)))) ||
|
|
1194
|
-
!Array.isArray(item.acceptanceReceiptHistory) ||
|
|
1195
|
-
item.acceptanceReceiptHistory.length > 2 ||
|
|
1196
|
-
!item.acceptanceReceiptHistory.every(isVerificationReceipt) ||
|
|
1197
|
-
(item.acceptanceReceipt !== undefined &&
|
|
1198
|
-
JSON.stringify(item.acceptanceReceiptHistory.at(-1)) !==
|
|
1199
|
-
JSON.stringify(item.acceptanceReceipt)) ||
|
|
1200
|
-
item.reworkCount !==
|
|
1201
|
-
Math.min(
|
|
1202
|
-
item.maxReworkCycles,
|
|
1203
|
-
item.acceptanceReceiptHistory.filter((receipt) => receipt.decision === "rework").length,
|
|
1204
|
-
) ||
|
|
1205
|
-
(item.submission !== undefined && !validSubmission(item.submission)) ||
|
|
1206
|
-
(item.acceptanceRequired &&
|
|
1207
|
-
item.state === "completed" &&
|
|
1208
|
-
item.acceptanceState === "pending" &&
|
|
1209
|
-
(!item.submission || !item.acceptedExecutionPlanId)) ||
|
|
1210
|
-
(item.acceptanceState === "accepted" &&
|
|
1211
|
-
(item.acceptanceReceipt?.decision !== "accept" || !item.verificationAccepted)) ||
|
|
1212
|
-
(item.acceptanceState === "rejected" &&
|
|
1213
|
-
(!["failed", "interrupted", "stale", "invalidated", "blocked", "needs-input"].includes(
|
|
1214
|
-
item.state,
|
|
1215
|
-
) ||
|
|
1216
|
-
(item.acceptanceReceipt !== undefined &&
|
|
1217
|
-
item.acceptanceReceipt.decision !== "reject" &&
|
|
1218
|
-
item.acceptanceReceipt.decision !== "rework"))) ||
|
|
1219
|
-
(item.acceptanceState === "rework-requested" &&
|
|
1220
|
-
(item.state !== "blocked" || item.acceptanceReceipt?.decision !== "rework")) ||
|
|
1221
|
-
(item.state === "awaiting-verification" &&
|
|
1222
|
-
(!item.stagedTreeIdentity || !item.acceptedExecutionPlanId)) ||
|
|
1223
|
-
(item.stagedTreeIdentity !== undefined &&
|
|
1224
|
-
item.state === "completed" &&
|
|
1225
|
-
!item.acceptanceRequired &&
|
|
1226
|
-
item.verificationReceipt === undefined)
|
|
1227
|
-
) {
|
|
1228
|
-
throw new Error(`Malformed stored WorkItem ${String(item?.id ?? "unknown")}`);
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
|
-
|
|
1232
|
-
function storedAcceptanceMatchesItem(item: WorkItemRecord, receipt: VerificationReceipt): boolean {
|
|
1233
|
-
return (
|
|
1234
|
-
item.acceptanceRequired &&
|
|
1235
|
-
item.submission !== undefined &&
|
|
1236
|
-
receipt.targetTaskId === item.id &&
|
|
1237
|
-
receipt.targetTaskGeneration === item.taskGeneration &&
|
|
1238
|
-
receipt.targetExecutionPlanId === item.acceptedExecutionPlanId &&
|
|
1239
|
-
sameWorkflowTreeIdentity(receipt.beforeTreeIdentity, item.submission.treeIdentity) &&
|
|
1240
|
-
sameWorkflowTreeIdentity(receipt.afterTreeIdentity, item.submission.treeIdentity) &&
|
|
1241
|
-
receipt.baseRepositoryGeneration === item.submission.baseRepositoryGeneration &&
|
|
1242
|
-
receipt.patchDigest === item.submission.patchDigest &&
|
|
1243
|
-
JSON.stringify(receipt.changedPaths) === JSON.stringify(item.submission.changedPaths) &&
|
|
1244
|
-
JSON.stringify(receipt.allowedScopes) === JSON.stringify(item.writePaths) &&
|
|
1245
|
-
JSON.stringify(receipt.dependencyVersions) === JSON.stringify(item.inputArtifactVersions) &&
|
|
1246
|
-
JSON.stringify(receipt.readSetVersions) === JSON.stringify(item.submission.fileVersions) &&
|
|
1247
|
-
JSON.stringify(receipt.acceptanceCriteria) === JSON.stringify(item.acceptanceCriteria) &&
|
|
1248
|
-
JSON.stringify(receipt.requiredEvidenceIds) === JSON.stringify(item.requiredEvidence)
|
|
1249
|
-
);
|
|
1250
|
-
}
|
|
1251
|
-
|
|
1252
|
-
function storedVerificationMatchesItem(
|
|
1253
|
-
item: WorkItemRecord,
|
|
1254
|
-
receipt: WorkflowVerificationReceipt,
|
|
1255
|
-
): boolean {
|
|
1256
|
-
return (
|
|
1257
|
-
!item.verifierFor &&
|
|
1258
|
-
receipt.targetTaskId === item.id &&
|
|
1259
|
-
receipt.targetTaskGeneration === item.taskGeneration &&
|
|
1260
|
-
receipt.targetExecutionPlanId === item.acceptedExecutionPlanId &&
|
|
1261
|
-
item.stagedTreeIdentity !== undefined &&
|
|
1262
|
-
sameWorkflowTreeIdentity(receipt.treeIdentity, item.stagedTreeIdentity) &&
|
|
1263
|
-
item.verificationAccepted === (receipt.decision === "accept") &&
|
|
1264
|
-
(receipt.decision === "accept"
|
|
1265
|
-
? item.state === "completed"
|
|
1266
|
-
: receipt.decision === "rework"
|
|
1267
|
-
? item.state === "blocked"
|
|
1268
|
-
: item.state === "failed")
|
|
1269
|
-
);
|
|
1270
|
-
}
|
|
1271
|
-
|
|
1272
|
-
function validStoredArtifacts(
|
|
1273
|
-
values: WorkArtifactReference[],
|
|
1274
|
-
producerTaskId: string,
|
|
1275
|
-
itemGeneration: number,
|
|
1276
|
-
): boolean {
|
|
1277
|
-
const seen = new Set<string>();
|
|
1278
|
-
return values.every(
|
|
1279
|
-
(artifact) =>
|
|
1280
|
-
artifact !== null &&
|
|
1281
|
-
typeof artifact === "object" &&
|
|
1282
|
-
Object.keys(artifact).every((key) =>
|
|
1283
|
-
["id", "kind", "version", "digest", "producerTaskId", "generation", "verified"].includes(
|
|
1284
|
-
key,
|
|
1285
|
-
),
|
|
1286
|
-
) &&
|
|
1287
|
-
isValidIdentifier(artifact.id) &&
|
|
1288
|
-
claimUnique(artifact.id, seen) &&
|
|
1289
|
-
typeof artifact.kind === "string" &&
|
|
1290
|
-
artifact.kind.length > 0 &&
|
|
1291
|
-
artifact.kind.length <= MAX_IDENTIFIER_LENGTH &&
|
|
1292
|
-
artifact.kind.trim() === artifact.kind &&
|
|
1293
|
-
typeof artifact.version === "string" &&
|
|
1294
|
-
artifact.version.length > 0 &&
|
|
1295
|
-
artifact.version.length <= MAX_IDENTIFIER_LENGTH &&
|
|
1296
|
-
artifact.version.trim() === artifact.version &&
|
|
1297
|
-
(artifact.digest === undefined ||
|
|
1298
|
-
(typeof artifact.digest === "string" &&
|
|
1299
|
-
artifact.digest.length > 0 &&
|
|
1300
|
-
artifact.digest.length <= MAX_TEXT_LENGTH &&
|
|
1301
|
-
artifact.digest.trim() === artifact.digest)) &&
|
|
1302
|
-
artifact.producerTaskId === producerTaskId &&
|
|
1303
|
-
Number.isSafeInteger(artifact.generation) &&
|
|
1304
|
-
Number(artifact.generation) >= 0 &&
|
|
1305
|
-
Number(artifact.generation) <= itemGeneration &&
|
|
1306
|
-
typeof artifact.verified === "boolean",
|
|
1307
|
-
);
|
|
1308
|
-
}
|
|
1309
|
-
|
|
1310
|
-
function claimUnique(value: string, seen: Set<string>): boolean {
|
|
1311
|
-
if (seen.has(value)) return false;
|
|
1312
|
-
seen.add(value);
|
|
1313
|
-
return true;
|
|
1314
|
-
}
|
|
1315
|
-
|
|
1316
|
-
function normalizeStoredArtifacts(
|
|
1317
|
-
values: WorkArtifactReference[],
|
|
1318
|
-
defaultProducerTaskId: string,
|
|
1319
|
-
defaultGeneration: number,
|
|
1320
|
-
preserveVerification: boolean,
|
|
1321
|
-
): WorkArtifactReference[] {
|
|
1322
|
-
if (!validStoredArtifacts(values, defaultProducerTaskId, defaultGeneration)) {
|
|
1323
|
-
throw new Error(`Malformed stored artifacts for WorkItem ${defaultProducerTaskId}`);
|
|
1324
|
-
}
|
|
1325
|
-
return values.map((value) => ({
|
|
1326
|
-
...structuredClone(value),
|
|
1327
|
-
verified: preserveVerification && value.verified,
|
|
1328
|
-
}));
|
|
1329
|
-
}
|
|
1330
|
-
|
|
1331
|
-
function normalizeArtifacts(
|
|
1332
|
-
values: Array<Omit<WorkArtifactReference, "producerTaskId" | "generation">>,
|
|
1333
|
-
producerTaskId: string,
|
|
1334
|
-
generation: number,
|
|
1335
|
-
): WorkArtifactReference[] {
|
|
1336
|
-
if (values.length > MAX_LIST_ITEMS) throw new Error("Too many WorkItem artifacts");
|
|
1337
|
-
const seen = new Set<string>();
|
|
1338
|
-
return values.map((value) => {
|
|
1339
|
-
validateIdentifier(value.id, "artifact id");
|
|
1340
|
-
if (seen.has(value.id)) throw new Error(`Duplicate artifact id ${value.id}`);
|
|
1341
|
-
seen.add(value.id);
|
|
1342
|
-
const kind = bounded(value.kind, MAX_IDENTIFIER_LENGTH);
|
|
1343
|
-
const version = bounded(value.version, MAX_IDENTIFIER_LENGTH);
|
|
1344
|
-
if (!kind || !version) throw new Error(`Artifact ${value.id} requires kind and version`);
|
|
1345
|
-
return {
|
|
1346
|
-
id: value.id,
|
|
1347
|
-
kind,
|
|
1348
|
-
version,
|
|
1349
|
-
...(value.digest ? { digest: bounded(value.digest, MAX_TEXT_LENGTH) } : {}),
|
|
1350
|
-
producerTaskId,
|
|
1351
|
-
generation,
|
|
1352
|
-
verified: false,
|
|
1353
|
-
};
|
|
1354
|
-
});
|
|
1355
|
-
}
|
|
1356
|
-
|
|
1357
|
-
function normalizeVersionMap(value: Record<string, string> | undefined): Record<string, string> {
|
|
1358
|
-
if (value === undefined) return {};
|
|
1359
|
-
const entries = Object.entries(value);
|
|
1360
|
-
if (entries.length > MAX_LIST_ITEMS) throw new Error("Too many artifact version requirements");
|
|
1361
|
-
return Object.fromEntries(
|
|
1362
|
-
entries.map(([id, version]) => {
|
|
1363
|
-
validateIdentifier(id, "artifact version id");
|
|
1364
|
-
const normalized = bounded(version, MAX_IDENTIFIER_LENGTH);
|
|
1365
|
-
if (!normalized) throw new Error(`Artifact ${id} requires a version`);
|
|
1366
|
-
return [id, normalized];
|
|
1367
|
-
}),
|
|
1368
|
-
);
|
|
1369
|
-
}
|
|
1370
|
-
|
|
1371
|
-
function uniqueBounded(values: readonly string[], label: string): string[] {
|
|
1372
|
-
if (!Array.isArray(values) || values.length > MAX_LIST_ITEMS) {
|
|
1373
|
-
throw new Error(`Too many WorkItem ${label} values`);
|
|
1374
|
-
}
|
|
1375
|
-
const result: string[] = [];
|
|
1376
|
-
const seen = new Set<string>();
|
|
1377
|
-
for (const value of values) {
|
|
1378
|
-
if (typeof value !== "string") throw new Error(`Invalid WorkItem ${label}`);
|
|
1379
|
-
const normalized = bounded(value, MAX_TEXT_LENGTH);
|
|
1380
|
-
if (!normalized) throw new Error(`Empty WorkItem ${label}`);
|
|
1381
|
-
if (!seen.has(normalized)) result.push(normalized);
|
|
1382
|
-
seen.add(normalized);
|
|
1383
|
-
}
|
|
1384
|
-
return result;
|
|
1385
|
-
}
|
|
1386
|
-
|
|
1387
|
-
function isValidIdentifier(value: unknown): value is string {
|
|
1388
|
-
return (
|
|
1389
|
-
typeof value === "string" &&
|
|
1390
|
-
value.length >= 1 &&
|
|
1391
|
-
value.length <= MAX_IDENTIFIER_LENGTH &&
|
|
1392
|
-
/^[A-Za-z0-9][A-Za-z0-9._:-]*$/u.test(value)
|
|
1393
|
-
);
|
|
1394
|
-
}
|
|
1395
|
-
|
|
1396
|
-
function validateIdentifier(value: string, label: string): void {
|
|
1397
|
-
if (!isValidIdentifier(value)) {
|
|
1398
|
-
throw new Error(`Invalid ${label}`);
|
|
1399
|
-
}
|
|
1400
|
-
}
|
|
1401
|
-
|
|
1402
|
-
function bounded(value: string, maxLength: number): string {
|
|
1403
|
-
return value.trim().slice(0, maxLength);
|
|
1404
|
-
}
|