@abelxiaoxing/cadence 1.8.4 → 2.0.1
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 +324 -117
- package/THIRD_PARTY_NOTICES.md +15 -0
- package/agents/design-explorer.md +14 -18
- package/agents/diagnosis-worker.md +14 -14
- package/agents/implementation-worker.md +16 -17
- package/config/workflow-task.example.json +7 -0
- package/docs/remote-access.md +60 -0
- package/docs/reuse-and-distribution.md +44 -0
- package/docs/service-management.md +108 -0
- package/licenses/bundled-web.txt +6245 -0
- package/licenses/pi-web-ui-MIT.txt +21 -0
- package/licenses/pi-web-ui-reuse.md +22 -0
- package/package.json +92 -20
- package/src/index.ts +21 -1802
- package/src/native-build.ts +6 -0
- package/src/native-client.ts +103 -0
- package/src/native-export.ts +58 -0
- package/src/native-history.ts +359 -0
- package/src/operator-cli.mjs +453 -1937
- package/src/plan-proposal.ts +87 -0
- package/src/sdk-worker.mjs +549 -0
- package/src/service/contracts/active.ts +82 -0
- package/src/service/contracts/budget.ts +148 -0
- package/src/service/contracts/build.ts +67 -0
- package/src/service/contracts/capture.ts +48 -0
- package/src/service/contracts/core.ts +33 -0
- package/src/service/contracts/current.ts +261 -0
- package/src/service/contracts/domain.ts +68 -0
- package/src/service/contracts/history.ts +2245 -0
- package/src/service/contracts/models.ts +139 -0
- package/src/service/coordination.ts +862 -0
- package/src/service/private-files.ts +204 -0
- package/src/service-cli.mjs +5592 -0
- package/src/service-client.ts +747 -0
- package/src/service-contract.ts +245 -0
- package/src/service-host.mjs +22382 -0
- package/src/service-http-client.ts +425 -0
- package/src/service-wire.ts +372 -0
- package/src/task-shell.ts +1045 -0
- package/src/task-store.ts +479 -0
- package/src/task-types.ts +184 -0
- package/src/verification-diagnostics.ts +29 -72
- package/src/worker-capture.mjs +1197 -0
- package/src/worker-public.mjs +1753 -0
- package/src/worker-redaction.mjs +608 -0
- package/src/worker-tools.mjs +202 -0
- package/web/dist/assets/KaTeX_AMS-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_AMS-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_AMS-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Caligraphic-Bold.ttf +0 -0
- package/web/dist/assets/KaTeX_Caligraphic-Bold.woff +0 -0
- package/web/dist/assets/KaTeX_Caligraphic-Bold.woff2 +0 -0
- package/web/dist/assets/KaTeX_Caligraphic-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Caligraphic-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Caligraphic-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Fraktur-Bold.ttf +0 -0
- package/web/dist/assets/KaTeX_Fraktur-Bold.woff +0 -0
- package/web/dist/assets/KaTeX_Fraktur-Bold.woff2 +0 -0
- package/web/dist/assets/KaTeX_Fraktur-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Fraktur-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Fraktur-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Main-Bold.ttf +0 -0
- package/web/dist/assets/KaTeX_Main-Bold.woff +0 -0
- package/web/dist/assets/KaTeX_Main-Bold.woff2 +0 -0
- package/web/dist/assets/KaTeX_Main-BoldItalic.ttf +0 -0
- package/web/dist/assets/KaTeX_Main-BoldItalic.woff +0 -0
- package/web/dist/assets/KaTeX_Main-BoldItalic.woff2 +0 -0
- package/web/dist/assets/KaTeX_Main-Italic.ttf +0 -0
- package/web/dist/assets/KaTeX_Main-Italic.woff +0 -0
- package/web/dist/assets/KaTeX_Main-Italic.woff2 +0 -0
- package/web/dist/assets/KaTeX_Main-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Main-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Main-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Math-BoldItalic.ttf +0 -0
- package/web/dist/assets/KaTeX_Math-BoldItalic.woff +0 -0
- package/web/dist/assets/KaTeX_Math-BoldItalic.woff2 +0 -0
- package/web/dist/assets/KaTeX_Math-Italic.ttf +0 -0
- package/web/dist/assets/KaTeX_Math-Italic.woff +0 -0
- package/web/dist/assets/KaTeX_Math-Italic.woff2 +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Bold.ttf +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Bold.woff +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Bold.woff2 +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Italic.ttf +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Italic.woff +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Italic.woff2 +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_SansSerif-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Script-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Script-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Script-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Size1-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Size1-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Size1-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Size2-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Size2-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Size2-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Size3-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Size3-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Size3-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Size4-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Size4-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Size4-Regular.woff2 +0 -0
- package/web/dist/assets/KaTeX_Typewriter-Regular.ttf +0 -0
- package/web/dist/assets/KaTeX_Typewriter-Regular.woff +0 -0
- package/web/dist/assets/KaTeX_Typewriter-Regular.woff2 +0 -0
- package/web/dist/assets/cadence.css +2 -0
- package/web/dist/assets/cadence.js +7 -0
- package/web/dist/assets/markdown.js +296 -0
- package/web/dist/assets/react.js +9 -0
- package/web/dist/assets/rolldown-runtime.js +1 -0
- package/web/dist/assets/timeline-client.js +41 -0
- package/web/dist/assets/vis-timeline.css +1274 -0
- package/web/dist/assets/vis-timeline.js +32 -0
- package/web/dist/index.html +18 -0
- package/web/dist/manifest.json +505 -0
- package/config/plan-draft.example.json +0 -125
- package/config/plan-draft.multiple-tasks.example.json +0 -375
- package/config/plan-draft.quick.example.json +0 -78
- package/prompts/abel-design.md +0 -382
- package/prompts/abel-diagnose.md +0 -71
- package/prompts/abel-implement.md +0 -284
- package/prompts/abel-init.md +0 -52
- package/src/activation.ts +0 -44
- package/src/activity-contracts.ts +0 -82
- package/src/agent-registry.ts +0 -53
- package/src/apply-transaction.ts +0 -1437
- package/src/artifact-store.ts +0 -213
- package/src/build-windows-job.mjs +0 -95
- package/src/candidate-artifact.ts +0 -143
- package/src/candidate-context.ts +0 -289
- package/src/candidate-patch.ts +0 -271
- package/src/candidate-workspace.ts +0 -80
- package/src/canonical.ts +0 -16
- package/src/change-contract-schema.ts +0 -191
- package/src/change-contract.ts +0 -324
- package/src/change-verification.ts +0 -1617
- package/src/contracts.ts +0 -2466
- package/src/control-contracts.ts +0 -382
- package/src/delivery-compiler.ts +0 -1805
- package/src/delivery-revision.ts +0 -42
- package/src/design-control.ts +0 -1299
- package/src/design-diagnostics.ts +0 -181
- package/src/design-journal.ts +0 -1773
- package/src/directory-sync.ts +0 -36
- package/src/durable-contracts.ts +0 -227
- package/src/durable-workflow.ts +0 -822
- package/src/evidence-draft.ts +0 -122
- package/src/execution-profile.ts +0 -64
- package/src/execution-retention.ts +0 -77
- package/src/file-snapshot.ts +0 -163
- package/src/implement-continuation.ts +0 -559
- package/src/implement-graph.ts +0 -690
- package/src/implement-plan.ts +0 -72
- package/src/install-config.d.mts +0 -6
- package/src/isolation-backend.ts +0 -353
- package/src/model-source.ts +0 -22
- package/src/openspec-cli.ts +0 -494
- package/src/operator-cli-entry.mjs +0 -24
- package/src/operator-tools.ts +0 -309
- package/src/package-candidate-process.ts +0 -206
- package/src/package-candidate.ts +0 -24
- package/src/package-delivery.ts +0 -486
- package/src/package-state.ts +0 -320
- package/src/package-verification.ts +0 -635
- package/src/package-workflow.ts +0 -305
- package/src/packet-runtime.ts +0 -459
- package/src/phase-execution.ts +0 -2163
- package/src/pi-adapter.ts +0 -19
- package/src/plan-draft-summary.ts +0 -527
- package/src/plan-draft.ts +0 -502
- package/src/remove-path.ts +0 -32
- package/src/run-state.ts +0 -420
- package/src/run-store.ts +0 -1179
- package/src/safe-path.ts +0 -77
- package/src/scoped-grep-worker.mjs +0 -23
- package/src/scoped-tools.ts +0 -775
- package/src/single-task-draft.ts +0 -142
- package/src/sqlite-schema.ts +0 -171
- package/src/state-root.ts +0 -215
- package/src/storage-schema.ts +0 -360
- package/src/subagent-activity.ts +0 -1417
- package/src/subagent-process.ts +0 -476
- package/src/task-ledger.ts +0 -1338
- package/src/transport-budget.ts +0 -50
- package/src/verification-capability.ts +0 -1191
- package/src/verification-environment-io.ts +0 -389
- package/src/verification-environment.ts +0 -162
- package/src/verification-identity.ts +0 -77
- package/src/verification-scan.ts +0 -64
- package/src/windows-job-backend.ts +0 -419
- package/src/windows-job.c +0 -149
- package/src/workflow-engine.ts +0 -28
- package/src/workflow-policy.ts +0 -1670
- package/src/workflow-recovery-policy.ts +0 -97
- package/src/workflow-scheduling.ts +0 -105
- package/src/workflow-state-machine.ts +0 -4341
- package/src/workflow-status.ts +0 -576
- package/src/workspace-io-entry.mjs +0 -76
- package/src/workspace-io-worker.mjs +0 -1312
- package/src/workspace-io.ts +0 -89
- package/src/workspace-store.ts +0 -634
package/src/run-state.ts
DELETED
|
@@ -1,420 +0,0 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
|
-
import { canonicalJson, compareCanonicalStrings } from "./canonical.ts";
|
|
3
|
-
|
|
4
|
-
export const RUN_STAGES = ["abel-design", "abel-implement"] as const;
|
|
5
|
-
export type RunStage = (typeof RUN_STAGES)[number];
|
|
6
|
-
|
|
7
|
-
export const RUN_STATES = [
|
|
8
|
-
"created",
|
|
9
|
-
"validating-delivery",
|
|
10
|
-
"ready",
|
|
11
|
-
"queued",
|
|
12
|
-
"connecting",
|
|
13
|
-
"running",
|
|
14
|
-
"validating",
|
|
15
|
-
"verifying",
|
|
16
|
-
"retryable",
|
|
17
|
-
"paused",
|
|
18
|
-
"approval-needed",
|
|
19
|
-
"change-verifying",
|
|
20
|
-
"ready-to-apply",
|
|
21
|
-
"applying",
|
|
22
|
-
"recovering",
|
|
23
|
-
"completed",
|
|
24
|
-
"discarded",
|
|
25
|
-
"rejected",
|
|
26
|
-
] as const;
|
|
27
|
-
|
|
28
|
-
export const TASK_STATES = [
|
|
29
|
-
"pending",
|
|
30
|
-
"queued",
|
|
31
|
-
"phase-ready",
|
|
32
|
-
"phase-running",
|
|
33
|
-
"validating",
|
|
34
|
-
"phase-verified",
|
|
35
|
-
"repairable",
|
|
36
|
-
"retryable",
|
|
37
|
-
"paused",
|
|
38
|
-
"approval-needed",
|
|
39
|
-
"verified",
|
|
40
|
-
] as const;
|
|
41
|
-
|
|
42
|
-
export type RunState = (typeof RUN_STATES)[number];
|
|
43
|
-
export type TaskState = (typeof TASK_STATES)[number];
|
|
44
|
-
export type DeliveryGate = "gate-a" | "gate-b";
|
|
45
|
-
|
|
46
|
-
export const ZERO_EVENT_HASH = "0".repeat(64);
|
|
47
|
-
const SHA256 = /^[a-f0-9]{64}$/u;
|
|
48
|
-
|
|
49
|
-
export interface DeliveryBindingProjection {
|
|
50
|
-
gate: DeliveryGate;
|
|
51
|
-
revision: number;
|
|
52
|
-
receiptHash: string;
|
|
53
|
-
approvalProof?: {
|
|
54
|
-
revision: number;
|
|
55
|
-
contractHash: string;
|
|
56
|
-
recordHash: string;
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export interface RunProjection {
|
|
61
|
-
runId: string;
|
|
62
|
-
rootHash: string;
|
|
63
|
-
stage: RunStage;
|
|
64
|
-
change?: string;
|
|
65
|
-
provisionalKey?: string;
|
|
66
|
-
state: RunState;
|
|
67
|
-
sequence: number;
|
|
68
|
-
eventHash: string;
|
|
69
|
-
deliveryRevision?: number;
|
|
70
|
-
deliveryBindings: DeliveryBindingProjection[];
|
|
71
|
-
pauseCode?: string;
|
|
72
|
-
terminal?: "completed" | "discarded" | "rejected";
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export type RunEventType =
|
|
76
|
-
| "run-created"
|
|
77
|
-
| "change-bound"
|
|
78
|
-
| "delivery-bound"
|
|
79
|
-
| "state-transitioned"
|
|
80
|
-
| "operation-interrupted";
|
|
81
|
-
|
|
82
|
-
export interface RunEvent {
|
|
83
|
-
sequence: number;
|
|
84
|
-
type: RunEventType;
|
|
85
|
-
payload: Record<string, unknown>;
|
|
86
|
-
priorHash: string;
|
|
87
|
-
hash: string;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const TERMINAL_STATES = new Set<RunState>([
|
|
91
|
-
"completed",
|
|
92
|
-
"discarded",
|
|
93
|
-
"rejected",
|
|
94
|
-
]);
|
|
95
|
-
|
|
96
|
-
const LEGAL_TRANSITIONS: Readonly<Record<RunState, readonly RunState[]>> = {
|
|
97
|
-
created: ["validating-delivery", "paused", "discarded", "rejected"],
|
|
98
|
-
"validating-delivery": [
|
|
99
|
-
"ready",
|
|
100
|
-
"paused",
|
|
101
|
-
"approval-needed",
|
|
102
|
-
"discarded",
|
|
103
|
-
"rejected",
|
|
104
|
-
],
|
|
105
|
-
ready: ["queued", "running", "paused", "discarded", "rejected"],
|
|
106
|
-
queued: ["connecting", "running", "paused", "discarded"],
|
|
107
|
-
connecting: ["running", "retryable", "paused", "discarded"],
|
|
108
|
-
running: [
|
|
109
|
-
"validating",
|
|
110
|
-
"verifying",
|
|
111
|
-
"queued",
|
|
112
|
-
"retryable",
|
|
113
|
-
"paused",
|
|
114
|
-
"approval-needed",
|
|
115
|
-
"change-verifying",
|
|
116
|
-
"discarded",
|
|
117
|
-
],
|
|
118
|
-
validating: [
|
|
119
|
-
"running",
|
|
120
|
-
"verifying",
|
|
121
|
-
"retryable",
|
|
122
|
-
"paused",
|
|
123
|
-
"approval-needed",
|
|
124
|
-
"discarded",
|
|
125
|
-
],
|
|
126
|
-
verifying: [
|
|
127
|
-
"running",
|
|
128
|
-
"retryable",
|
|
129
|
-
"paused",
|
|
130
|
-
"approval-needed",
|
|
131
|
-
"change-verifying",
|
|
132
|
-
"discarded",
|
|
133
|
-
],
|
|
134
|
-
retryable: ["queued", "connecting", "running", "paused", "discarded"],
|
|
135
|
-
paused: [
|
|
136
|
-
"paused",
|
|
137
|
-
"validating-delivery",
|
|
138
|
-
"ready",
|
|
139
|
-
"queued",
|
|
140
|
-
"connecting",
|
|
141
|
-
"running",
|
|
142
|
-
"approval-needed",
|
|
143
|
-
"recovering",
|
|
144
|
-
"completed",
|
|
145
|
-
"discarded",
|
|
146
|
-
"rejected",
|
|
147
|
-
],
|
|
148
|
-
"approval-needed": ["validating-delivery", "paused", "discarded", "rejected"],
|
|
149
|
-
"change-verifying": [
|
|
150
|
-
"running",
|
|
151
|
-
"retryable",
|
|
152
|
-
"paused",
|
|
153
|
-
"approval-needed",
|
|
154
|
-
"ready-to-apply",
|
|
155
|
-
"discarded",
|
|
156
|
-
],
|
|
157
|
-
"ready-to-apply": ["applying", "paused", "discarded"],
|
|
158
|
-
applying: ["recovering", "completed"],
|
|
159
|
-
recovering: ["applying", "paused", "completed", "discarded", "rejected"],
|
|
160
|
-
completed: [],
|
|
161
|
-
discarded: [],
|
|
162
|
-
rejected: [],
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
export function hashRunEvent(event: Omit<RunEvent, "hash">): string {
|
|
166
|
-
return createHash("sha256").update(canonicalJson(event)).digest("hex");
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
export function createRunEvent(
|
|
170
|
-
sequence: number,
|
|
171
|
-
type: RunEventType,
|
|
172
|
-
payload: Record<string, unknown>,
|
|
173
|
-
priorHash: string,
|
|
174
|
-
): RunEvent {
|
|
175
|
-
if (
|
|
176
|
-
!Number.isSafeInteger(sequence) ||
|
|
177
|
-
sequence < 1 ||
|
|
178
|
-
!SHA256.test(priorHash)
|
|
179
|
-
) {
|
|
180
|
-
throw new Error("event-integrity-invalid-header");
|
|
181
|
-
}
|
|
182
|
-
const unsigned = {
|
|
183
|
-
sequence,
|
|
184
|
-
type,
|
|
185
|
-
payload: structuredClone(payload),
|
|
186
|
-
priorHash,
|
|
187
|
-
};
|
|
188
|
-
return { ...unsigned, hash: hashRunEvent(unsigned) };
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
export function canTransition(from: RunState, to: RunState): boolean {
|
|
192
|
-
return LEGAL_TRANSITIONS[from].includes(to);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
export function assertRunTransition(from: RunState, to: RunState): void {
|
|
196
|
-
if (!canTransition(from, to)) {
|
|
197
|
-
throw new Error(`invalid-run-transition:${from}:${to}`);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
function requireString(payload: Record<string, unknown>, key: string): string {
|
|
202
|
-
const value = payload[key];
|
|
203
|
-
if (typeof value !== "string" || value.length === 0) {
|
|
204
|
-
throw new Error(`event-integrity-invalid-${key}`);
|
|
205
|
-
}
|
|
206
|
-
return value;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
function applyVerifiedEvent(
|
|
210
|
-
projection: RunProjection | undefined,
|
|
211
|
-
event: RunEvent,
|
|
212
|
-
): RunProjection {
|
|
213
|
-
switch (event.type) {
|
|
214
|
-
case "run-created": {
|
|
215
|
-
if (projection || event.sequence !== 1) {
|
|
216
|
-
throw new Error("event-integrity-duplicate-create");
|
|
217
|
-
}
|
|
218
|
-
const stage = event.payload.stage;
|
|
219
|
-
if (stage !== "abel-design" && stage !== "abel-implement") {
|
|
220
|
-
throw new Error("event-integrity-invalid-stage");
|
|
221
|
-
}
|
|
222
|
-
const change = event.payload.change;
|
|
223
|
-
const provisionalKey = event.payload.provisionalKey;
|
|
224
|
-
if (
|
|
225
|
-
(typeof change !== "string" || change.length === 0) ===
|
|
226
|
-
(typeof provisionalKey !== "string" || provisionalKey.length === 0)
|
|
227
|
-
) {
|
|
228
|
-
throw new Error("event-integrity-invalid-run-key");
|
|
229
|
-
}
|
|
230
|
-
return {
|
|
231
|
-
runId: requireString(event.payload, "runId"),
|
|
232
|
-
rootHash: requireString(event.payload, "rootHash"),
|
|
233
|
-
stage,
|
|
234
|
-
...(typeof change === "string" ? { change } : {}),
|
|
235
|
-
...(typeof provisionalKey === "string" ? { provisionalKey } : {}),
|
|
236
|
-
state: "created",
|
|
237
|
-
sequence: event.sequence,
|
|
238
|
-
eventHash: event.hash,
|
|
239
|
-
deliveryBindings: [],
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
case "change-bound": {
|
|
243
|
-
if (projection?.stage !== "abel-design") {
|
|
244
|
-
throw new Error("event-integrity-change-binding");
|
|
245
|
-
}
|
|
246
|
-
const { provisionalKey: _provisionalKey, ...boundProjection } =
|
|
247
|
-
projection;
|
|
248
|
-
return {
|
|
249
|
-
...boundProjection,
|
|
250
|
-
change: requireString(event.payload, "change"),
|
|
251
|
-
sequence: event.sequence,
|
|
252
|
-
eventHash: event.hash,
|
|
253
|
-
};
|
|
254
|
-
}
|
|
255
|
-
case "delivery-bound": {
|
|
256
|
-
if (!projection) throw new Error("event-integrity-missing-run");
|
|
257
|
-
const gate = event.payload.gate;
|
|
258
|
-
const revision = event.payload.revision;
|
|
259
|
-
const receiptHash = event.payload.receiptHash;
|
|
260
|
-
const approvalProof = event.payload.approvalProof;
|
|
261
|
-
if (
|
|
262
|
-
(gate !== "gate-a" && gate !== "gate-b") ||
|
|
263
|
-
!Number.isSafeInteger(revision) ||
|
|
264
|
-
(revision as number) < 1 ||
|
|
265
|
-
typeof receiptHash !== "string" ||
|
|
266
|
-
!SHA256.test(receiptHash) ||
|
|
267
|
-
(approvalProof !== undefined &&
|
|
268
|
-
(!approvalProof ||
|
|
269
|
-
typeof approvalProof !== "object" ||
|
|
270
|
-
Array.isArray(approvalProof) ||
|
|
271
|
-
!Number.isSafeInteger(
|
|
272
|
-
(approvalProof as Record<string, unknown>).revision,
|
|
273
|
-
) ||
|
|
274
|
-
Number((approvalProof as Record<string, unknown>).revision) < 1 ||
|
|
275
|
-
typeof (approvalProof as Record<string, unknown>).contractHash !==
|
|
276
|
-
"string" ||
|
|
277
|
-
!SHA256.test(
|
|
278
|
-
String((approvalProof as Record<string, unknown>).contractHash),
|
|
279
|
-
) ||
|
|
280
|
-
typeof (approvalProof as Record<string, unknown>).recordHash !==
|
|
281
|
-
"string" ||
|
|
282
|
-
!SHA256.test(
|
|
283
|
-
String((approvalProof as Record<string, unknown>).recordHash),
|
|
284
|
-
)))
|
|
285
|
-
) {
|
|
286
|
-
throw new Error("event-integrity-delivery-binding");
|
|
287
|
-
}
|
|
288
|
-
const binding: DeliveryBindingProjection = {
|
|
289
|
-
gate: gate as DeliveryGate,
|
|
290
|
-
revision: revision as number,
|
|
291
|
-
receiptHash,
|
|
292
|
-
...(approvalProof
|
|
293
|
-
? {
|
|
294
|
-
approvalProof: structuredClone(
|
|
295
|
-
approvalProof as DeliveryBindingProjection["approvalProof"],
|
|
296
|
-
),
|
|
297
|
-
}
|
|
298
|
-
: {}),
|
|
299
|
-
};
|
|
300
|
-
const existing = projection.deliveryBindings.find(
|
|
301
|
-
(candidate) =>
|
|
302
|
-
candidate.gate === binding.gate &&
|
|
303
|
-
candidate.revision === binding.revision,
|
|
304
|
-
);
|
|
305
|
-
if (
|
|
306
|
-
existing &&
|
|
307
|
-
(existing.receiptHash !== binding.receiptHash ||
|
|
308
|
-
canonicalJson(existing.approvalProof) !==
|
|
309
|
-
canonicalJson(binding.approvalProof))
|
|
310
|
-
) {
|
|
311
|
-
throw new Error("event-integrity-delivery-conflict");
|
|
312
|
-
}
|
|
313
|
-
const deliveryBindings = existing
|
|
314
|
-
? projection.deliveryBindings
|
|
315
|
-
: [...projection.deliveryBindings, binding].sort(
|
|
316
|
-
(left, right) =>
|
|
317
|
-
left.revision - right.revision ||
|
|
318
|
-
compareCanonicalStrings(left.gate, right.gate),
|
|
319
|
-
);
|
|
320
|
-
return {
|
|
321
|
-
...projection,
|
|
322
|
-
deliveryBindings,
|
|
323
|
-
deliveryRevision: Math.max(
|
|
324
|
-
projection.deliveryRevision ?? 0,
|
|
325
|
-
binding.revision,
|
|
326
|
-
),
|
|
327
|
-
sequence: event.sequence,
|
|
328
|
-
eventHash: event.hash,
|
|
329
|
-
};
|
|
330
|
-
}
|
|
331
|
-
case "state-transitioned": {
|
|
332
|
-
if (!projection) throw new Error("event-integrity-missing-run");
|
|
333
|
-
const from = event.payload.from;
|
|
334
|
-
const to = event.payload.to;
|
|
335
|
-
if (
|
|
336
|
-
from !== projection.state ||
|
|
337
|
-
!(RUN_STATES as readonly unknown[]).includes(to)
|
|
338
|
-
) {
|
|
339
|
-
throw new Error("event-integrity-state-mismatch");
|
|
340
|
-
}
|
|
341
|
-
assertRunTransition(projection.state, to as RunState);
|
|
342
|
-
const nextState = to as RunState;
|
|
343
|
-
const {
|
|
344
|
-
pauseCode: _pauseCode,
|
|
345
|
-
terminal: _terminal,
|
|
346
|
-
...transitionedProjection
|
|
347
|
-
} = projection;
|
|
348
|
-
return {
|
|
349
|
-
...transitionedProjection,
|
|
350
|
-
state: nextState,
|
|
351
|
-
sequence: event.sequence,
|
|
352
|
-
eventHash: event.hash,
|
|
353
|
-
...(typeof event.payload.code === "string"
|
|
354
|
-
? { pauseCode: event.payload.code }
|
|
355
|
-
: {}),
|
|
356
|
-
...(TERMINAL_STATES.has(nextState)
|
|
357
|
-
? { terminal: nextState as "completed" | "discarded" | "rejected" }
|
|
358
|
-
: {}),
|
|
359
|
-
};
|
|
360
|
-
}
|
|
361
|
-
case "operation-interrupted": {
|
|
362
|
-
if (!projection || TERMINAL_STATES.has(projection.state)) {
|
|
363
|
-
throw new Error("event-integrity-interruption");
|
|
364
|
-
}
|
|
365
|
-
const nextState =
|
|
366
|
-
projection.state === "applying" ? "recovering" : "paused";
|
|
367
|
-
if (nextState !== projection.state) {
|
|
368
|
-
assertRunTransition(projection.state, nextState);
|
|
369
|
-
}
|
|
370
|
-
return {
|
|
371
|
-
...projection,
|
|
372
|
-
state: nextState,
|
|
373
|
-
pauseCode: "operation-interrupted",
|
|
374
|
-
sequence: event.sequence,
|
|
375
|
-
eventHash: event.hash,
|
|
376
|
-
};
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
export function reduceRunEvents(events: readonly RunEvent[]): RunProjection {
|
|
382
|
-
if (events.length === 0) throw new Error("event-integrity-empty");
|
|
383
|
-
let projection: RunProjection | undefined;
|
|
384
|
-
let priorHash = ZERO_EVENT_HASH;
|
|
385
|
-
for (let index = 0; index < events.length; index += 1) {
|
|
386
|
-
const event = events[index];
|
|
387
|
-
if (
|
|
388
|
-
event.sequence !== index + 1 ||
|
|
389
|
-
event.priorHash !== priorHash ||
|
|
390
|
-
!SHA256.test(event.hash) ||
|
|
391
|
-
hashRunEvent({
|
|
392
|
-
sequence: event.sequence,
|
|
393
|
-
type: event.type,
|
|
394
|
-
payload: event.payload,
|
|
395
|
-
priorHash: event.priorHash,
|
|
396
|
-
}) !== event.hash
|
|
397
|
-
) {
|
|
398
|
-
throw new Error("event-integrity-hash-chain");
|
|
399
|
-
}
|
|
400
|
-
projection = applyVerifiedEvent(projection, event);
|
|
401
|
-
priorHash = event.hash;
|
|
402
|
-
}
|
|
403
|
-
if (!projection) throw new Error("event-integrity-empty");
|
|
404
|
-
return projection;
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
export function legalControlCommands(state: RunState): string[] {
|
|
408
|
-
if (TERMINAL_STATES.has(state)) return ["status"];
|
|
409
|
-
if (
|
|
410
|
-
state === "paused" ||
|
|
411
|
-
state === "retryable" ||
|
|
412
|
-
state === "approval-needed"
|
|
413
|
-
) {
|
|
414
|
-
return ["status", "resume", "rebind", "discard"];
|
|
415
|
-
}
|
|
416
|
-
if (state === "recovering") return ["status", "resume", "discard"];
|
|
417
|
-
return ["status", "cancel", "discard"];
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
export { canonicalJson } from "./canonical.ts";
|