pi-web-ui 0.115.0 → 0.117.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/CHANGELOG.md +65 -2
- package/bin/device-hub-cli.mjs +26 -3
- package/bin/pi-web-ui.mjs +159 -71
- package/dist/server/agent-service-daemon.js +136 -8
- package/dist/server/agent-service.js +594 -159
- package/dist/server/canvas/canvas-workspace.js +18 -86
- package/dist/server/client-state.js +52 -8
- package/dist/server/compaction-markers.js +43 -24
- package/dist/server/dangling-tools.js +172 -16
- package/dist/server/device-center/bot-execution.js +77 -2
- package/dist/server/device-center/collab-service.js +61 -12
- package/dist/server/device-center/collab-tool.js +482 -21
- package/dist/server/device-center/connector.js +107 -37
- package/dist/server/device-center/delivery-authority.js +98 -0
- package/dist/server/device-center/delivery-subject.js +30 -0
- package/dist/server/device-center/device-integration.js +453 -183
- package/dist/server/device-center/execution-guides.js +17 -7
- package/dist/server/device-center/execution-instructions.js +2 -1
- package/dist/server/device-center/execution-settings.js +138 -25
- package/dist/server/device-center/executor.js +51 -6
- package/dist/server/device-center/file-sync.js +14 -13
- package/dist/server/device-center/group-context-message.js +3 -1
- package/dist/server/device-center/group-context.js +73 -3
- package/dist/server/device-center/hub-conversations.js +71 -6
- package/dist/server/device-center/hub-deliveries.js +120 -15
- package/dist/server/device-center/hub-integration.js +655 -334
- package/dist/server/device-center/hub-locks.js +320 -0
- package/dist/server/device-center/hub-page.js +16 -35
- package/dist/server/device-center/hub-setup.js +23 -1
- package/dist/server/device-center/hub-transaction-log.js +197 -0
- package/dist/server/device-center/hub-webhooks.js +165 -0
- package/dist/server/device-center/hub.js +1531 -305
- package/dist/server/device-center/integration-verification.js +113 -0
- package/dist/server/device-center/local-hub.js +8 -3
- package/dist/server/device-center/local-publication.js +423 -0
- package/dist/server/device-center/local-verification-job.js +97 -0
- package/dist/server/device-center/local-verification.js +274 -0
- package/dist/server/device-center/local.js +181 -13
- package/dist/server/device-center/main.js +4 -1
- package/dist/server/device-center/partition-log.js +330 -0
- package/dist/server/device-center/publication-journal.js +312 -0
- package/dist/server/device-center/publish-intent.js +379 -0
- package/dist/server/device-center/relay.js +134 -24
- package/dist/server/device-center/runtime.js +475 -42
- package/dist/server/device-center/store.js +366 -89
- package/dist/server/device-center/task-outcome.js +81 -0
- package/dist/server/device-center/task-recovery.js +603 -178
- package/dist/server/device-center/task-scheduler.js +48 -3
- package/dist/server/device-center/task-settlement.js +353 -0
- package/dist/server/device-center/task-workspace.js +443 -0
- package/dist/server/device-center/transfers.js +41 -15
- package/dist/server/device-center/verification-limits.js +10 -0
- package/dist/server/device-center/work-contract.js +995 -0
- package/dist/server/dispatch-domain-handlers.js +29 -3
- package/dist/server/dsh/dsh-agent-service.js +9 -2
- package/dist/server/engine-probe.js +112 -1
- package/dist/server/global-loader.js +119 -19
- package/dist/server/index.js +159 -17
- package/dist/server/managed.js +2 -0
- package/dist/server/model-admin.js +48 -6
- package/dist/server/model-enrich.js +10 -1
- package/dist/server/patch-node-pty.js +3 -2
- package/dist/server/patch-remote-catalog.js +3 -2
- package/dist/server/plugins.js +64 -7
- package/dist/server/preset-fields.js +17 -1
- package/dist/server/preset-share.js +1 -0
- package/dist/server/prompt-composer.js +39 -0
- package/dist/server/protocol-version.js +1 -1
- package/dist/server/quick-ask-settings.js +28 -0
- package/dist/server/quick-ask.js +164 -0
- package/dist/server/reasoning-registry.js +207 -0
- package/dist/server/resolve-global-sdk.js +61 -11
- package/dist/server/scheduler-tasks.js +35 -8
- package/dist/server/session-list-scan.js +223 -0
- package/dist/server/session-open.js +93 -0
- package/dist/server/session-parser.js +4 -0
- package/dist/server/session-scan.js +90 -38
- package/dist/server/settings-service.js +9 -1
- package/dist/server/soft-cap.js +5 -0
- package/dist/server/startup-banner.js +35 -0
- package/dist/server/subagents.js +2 -1
- package/dist/server/tool-manager.js +17 -9
- package/dist/server/transcript-prepare.js +73 -0
- package/dist/server/unauthorized-page.js +319 -0
- package/dist/server/universal/drivers/claude-code.js +98 -34
- package/dist/server/universal/universal-session.js +4 -0
- package/dist/server/workspace-handle.js +428 -0
- package/docs/architecture-plugins.md +959 -0
- package/docs/device-center.md +34 -20
- package/package.json +4 -4
- package/plugin-sdk/README.md +16 -8
- package/plugin-sdk/client.d.ts +27 -0
- package/plugin-sdk/client.mjs +82 -0
- package/plugin-sdk/index.d.ts +10 -0
- package/plugin-sdk/index.mjs +4 -683
- package/plugin-sdk/package.json +15 -4
- package/plugin-sdk/testing.d.ts +10 -0
- package/plugin-sdk/testing.mjs +566 -0
- package/themes/flow-cristallo.css +168 -0
- package/themes/flow-ink.css +169 -0
- package/themes/flow-obsidian.css +169 -0
- package/themes/flow-zurich.css +169 -0
- package/web/dist/.build-assets.json +1 -1
- package/web/dist/assets/{BgTasksModal-BYzPgmoq.js → BgTasksModal-BjNRIUIc.js} +1 -1
- package/web/dist/assets/{BgTasksModal-ni0VTmAQ.js → BgTasksModal-C0zuU8FE.js} +1 -1
- package/web/dist/assets/{CanvasStudio-DNmDNBod.js → CanvasStudio-BWvdmJ0M.js} +3 -3
- package/web/dist/assets/{CanvasStudio-BCUeEf25.js → CanvasStudio-VQFjmTGq.js} +3 -3
- package/web/dist/assets/DeviceCenter-DI9_0aBB.js +3 -0
- package/web/dist/assets/DeviceCenter-DsNtXqzY.js +3 -0
- package/web/dist/assets/DeviceExecutionSettings-BLP1Su5G.js +1 -0
- package/web/dist/assets/DeviceGroups-2Pvk0Cds.js +1 -0
- package/web/dist/assets/DeviceGroups-CP7CcyPD.js +1 -0
- package/web/dist/assets/{Dropdown-CGXD0DfT.js → Dropdown-CQtstPGv.js} +1 -1
- package/web/dist/assets/FilePreview-BMB2tNyT.js +2 -0
- package/web/dist/assets/FilePreview-RGFQUbJo.js +2 -0
- package/web/dist/assets/{GlobalSearchModal-mevF-ySi.js → GlobalSearchModal-BM5wxiqU.js} +1 -1
- package/web/dist/assets/{GlobalSearchModal-qL99FN_F.js → GlobalSearchModal-DGv_WINq.js} +1 -1
- package/web/dist/assets/GroupConversation-B7SnuXj1.js +61 -0
- package/web/dist/assets/GroupConversation-Dda69q-2.js +61 -0
- package/web/dist/assets/{HintTip-LHwkS2jV.js → HintTip-ChTW8o1m.js} +1 -1
- package/web/dist/assets/ModelConfigModal-C-0Z4hUZ.js +2 -0
- package/web/dist/assets/ModelConfigModal-yqeadyab.js +2 -0
- package/web/dist/assets/{PiSetupModal-DE5-gEtI.js → PiSetupModal-BX5rUuoI.js} +1 -1
- package/web/dist/assets/{ProviderOAuthControls-6UMARucA.js → ProviderOAuthControls-DY4SCfXg.js} +1 -1
- package/web/dist/assets/SCMPanel-C4KbQvvK.js +2 -0
- package/web/dist/assets/SCMPanel-DPAyY2sW.js +2 -0
- package/web/dist/assets/SettingsModal-DxZp6D_g.js +32 -0
- package/web/dist/assets/SettingsModal-WhQzaOPt.js +32 -0
- package/web/dist/assets/{TerminalPanel-CTp_BZ4S.js → TerminalPanel-D4By_RVc.js} +2 -2
- package/web/dist/assets/{TerminalPanel-CkYxC-Gh.js → TerminalPanel-lZw8-i3j.js} +2 -2
- package/web/dist/assets/{TuiOverlayModal-1cRLP8IF.js → TuiOverlayModal-C3pAVuC8.js} +1 -1
- package/web/dist/assets/{TuiOverlayModal-2xlptqWX.js → TuiOverlayModal-D7_rEXcJ.js} +1 -1
- package/web/dist/assets/app-globals-KqOVtmp1.js +123 -0
- package/web/dist/assets/{canvas-client-Bye9nSFO.js → canvas-client-BY6ojmle.js} +1 -1
- package/web/dist/assets/{desktop-kjBoE3Nu.js → desktop-CV6KKp9R.js} +1 -1
- package/web/dist/assets/index-BawF7ZP5.js +397 -0
- package/web/dist/assets/index-BoGCoq5X.js +397 -0
- package/web/dist/assets/index-CVW9FoY2.css +1 -0
- package/web/dist/assets/{plugin-host-C8mNgghx.js → plugin-host-Dn8Dd6SC.js} +1 -1
- package/web/dist/assets/{remote-ssh-client-CeP3CLUW.js → remote-ssh-client-Dzi-eEyE.js} +1 -1
- package/web/dist/assets/{theme-BXGpyTMh.js → theme-D_bihpdN.js} +1 -1
- package/web/dist/index.html +11 -11
- package/web/dist/sw.js +1 -1
- package/dist/server/patch-turn-end-boundary.js +0 -77
- package/web/dist/assets/DeviceCenter-BcBhRgL6.js +0 -3
- package/web/dist/assets/DeviceCenter-CmL5FZmt.js +0 -3
- package/web/dist/assets/DeviceGroups-CLdpdnI7.js +0 -1
- package/web/dist/assets/DeviceGroups-CgBaDPrD.js +0 -1
- package/web/dist/assets/FilePreview-B8iqnBjy.js +0 -2
- package/web/dist/assets/FilePreview-BqviZKCN.js +0 -2
- package/web/dist/assets/GroupConversation-B88Yy-lt.js +0 -3
- package/web/dist/assets/GroupConversation-BwP5NVOE.js +0 -3
- package/web/dist/assets/ModelConfigModal-Do3Fe3jH.js +0 -2
- package/web/dist/assets/SCMPanel-Hi6QqC1Q.js +0 -2
- package/web/dist/assets/SCMPanel-ezY_ZtCu.js +0 -2
- package/web/dist/assets/SettingsModal-6HgeEsJ8.js +0 -20
- package/web/dist/assets/SettingsModal-CF-7MwqS.js +0 -20
- package/web/dist/assets/app-globals-DZNa9v8u.js +0 -325
- package/web/dist/assets/index-CsFs_HMb.css +0 -1
- package/web/dist/assets/index-DHeeHS_l.js +0 -85
- package/web/dist/assets/index-DTZch8uz.js +0 -85
|
@@ -0,0 +1,995 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
const COMMIT_SHA_REGEX = /^[0-9a-fA-F]{40}$/;
|
|
3
|
+
const HEX_64_REGEX = /^[0-9a-fA-F]{64}$/;
|
|
4
|
+
const TREE_HEX_REGEX = /^[0-9a-fA-F]{40}$/;
|
|
5
|
+
const STRICT_BASE64_REGEX = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
|
|
6
|
+
const LOCAL_PATH_REGEX = /^[a-zA-Z]:[\\/]|^\/(\/|[a-zA-Z0-9_-]+[\\/])|[\\/]/;
|
|
7
|
+
export const MAX_GOAL_CHARS = 16_384;
|
|
8
|
+
export const MAX_ID_CHARS = 128;
|
|
9
|
+
export const MAX_ARRAY_ITEMS = 64;
|
|
10
|
+
export const MAX_ITEM_CHARS = 2_048;
|
|
11
|
+
export const MAX_REPO_ID_CHARS = 256;
|
|
12
|
+
export const MAX_POLICY_REF_CHARS = 256;
|
|
13
|
+
export const MAX_TRUNCATION_BUDGET = 10_000_000;
|
|
14
|
+
export const MAX_PATCH_BYTES = 128 * 1024; // 128 KiB
|
|
15
|
+
export const MAX_INPUT_ARTIFACTS = 8;
|
|
16
|
+
export const MAX_WORK_CONTRACT_BYTES = 256 * 1024;
|
|
17
|
+
export const MAX_CONTEXT_MANIFEST_BYTES = 96 * 1024;
|
|
18
|
+
export const MAX_CONTRACT_REQUEST_BYTES = 384 * 1024;
|
|
19
|
+
export function contractInputReferences(contract) {
|
|
20
|
+
return contract.inputArtifacts?.length ? contract.inputArtifacts.map(({ id, digest }) => ({ id, digest })) : null;
|
|
21
|
+
}
|
|
22
|
+
const CONTRACT_ALLOWED_KEYS = new Set([
|
|
23
|
+
"id",
|
|
24
|
+
"revision",
|
|
25
|
+
"digest",
|
|
26
|
+
"executionMode",
|
|
27
|
+
"goal",
|
|
28
|
+
"constraints",
|
|
29
|
+
"acceptanceCriteria",
|
|
30
|
+
"baseline",
|
|
31
|
+
"inputArtifacts",
|
|
32
|
+
"createdAt",
|
|
33
|
+
]);
|
|
34
|
+
const CONTRACT_INPUT_ALLOWED_KEYS = new Set([
|
|
35
|
+
"id",
|
|
36
|
+
"revision",
|
|
37
|
+
"executionMode",
|
|
38
|
+
"goal",
|
|
39
|
+
"constraints",
|
|
40
|
+
"acceptanceCriteria",
|
|
41
|
+
"baseline",
|
|
42
|
+
"inputArtifacts",
|
|
43
|
+
]);
|
|
44
|
+
const BASELINE_ALLOWED_KEYS = new Set(["repositoryId", "commit"]);
|
|
45
|
+
const PRODUCER_ALLOWED_KEYS = new Set(["deviceId", "botId", "taskId", "attemptId", "contractDigest", "manifestDigest"]);
|
|
46
|
+
const ARTIFACT_ALLOWED_KEYS = new Set([
|
|
47
|
+
"id",
|
|
48
|
+
"digest",
|
|
49
|
+
"producer",
|
|
50
|
+
"baseline",
|
|
51
|
+
"inputTree",
|
|
52
|
+
"outputTree",
|
|
53
|
+
"patchBase64",
|
|
54
|
+
"patchSha256",
|
|
55
|
+
"createdAt",
|
|
56
|
+
]);
|
|
57
|
+
const MANIFEST_ALLOWED_KEYS = new Set([
|
|
58
|
+
"version",
|
|
59
|
+
"digest",
|
|
60
|
+
"contractId",
|
|
61
|
+
"contractRevision",
|
|
62
|
+
"contractDigest",
|
|
63
|
+
"executionPolicy",
|
|
64
|
+
"context",
|
|
65
|
+
"taskState",
|
|
66
|
+
"inputs",
|
|
67
|
+
"policyRef",
|
|
68
|
+
"truncationBudget",
|
|
69
|
+
"createdAt",
|
|
70
|
+
]);
|
|
71
|
+
function checkAllowedKeys(obj, allowed, context) {
|
|
72
|
+
for (const key of Object.keys(obj)) {
|
|
73
|
+
if (!allowed.has(key)) {
|
|
74
|
+
return `Unknown field '${key}' in ${context}`;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Deeply freeze an object and all nested properties recursively.
|
|
81
|
+
*/
|
|
82
|
+
export function deepFreeze(obj) {
|
|
83
|
+
if (obj === null || typeof obj !== "object") {
|
|
84
|
+
return obj;
|
|
85
|
+
}
|
|
86
|
+
const seen = new Set();
|
|
87
|
+
function freezeInternal(val) {
|
|
88
|
+
if (val === null || typeof val !== "object" || seen.has(val)) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
seen.add(val);
|
|
92
|
+
Object.freeze(val);
|
|
93
|
+
for (const key of Object.getOwnPropertyNames(val)) {
|
|
94
|
+
freezeInternal(val[key]);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
freezeInternal(obj);
|
|
98
|
+
return obj;
|
|
99
|
+
}
|
|
100
|
+
// Manifests cross JSON transport and storage. Reject values whose executable meaning
|
|
101
|
+
// would change during serialization, rather than silently dropping or coercing them.
|
|
102
|
+
function safeClone(val) {
|
|
103
|
+
const ancestors = new Set();
|
|
104
|
+
let bytes = 0;
|
|
105
|
+
function visit(value, depth) {
|
|
106
|
+
if (depth > 64)
|
|
107
|
+
throw new Error("JSON input exceeds maximum depth of 64");
|
|
108
|
+
if (value === null || typeof value === "boolean")
|
|
109
|
+
return;
|
|
110
|
+
if (typeof value === "string")
|
|
111
|
+
bytes += Buffer.byteLength(value);
|
|
112
|
+
else if (typeof value === "number" && Number.isFinite(value))
|
|
113
|
+
bytes += 8;
|
|
114
|
+
else if (typeof value === "object") {
|
|
115
|
+
if (ancestors.has(value))
|
|
116
|
+
throw new Error("JSON input contains a cycle");
|
|
117
|
+
if (!Array.isArray(value) &&
|
|
118
|
+
Object.getPrototypeOf(value) !== Object.prototype &&
|
|
119
|
+
Object.getPrototypeOf(value) !== null)
|
|
120
|
+
throw new Error("JSON input must contain plain objects");
|
|
121
|
+
if (Object.getOwnPropertySymbols(value).length)
|
|
122
|
+
throw new Error("JSON input contains symbol keys");
|
|
123
|
+
ancestors.add(value);
|
|
124
|
+
for (const key of Object.keys(value)) {
|
|
125
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
126
|
+
if (!("value" in descriptor))
|
|
127
|
+
throw new Error("JSON input contains an accessor");
|
|
128
|
+
bytes += Buffer.byteLength(key) + 4;
|
|
129
|
+
if (descriptor.value !== undefined || Array.isArray(value))
|
|
130
|
+
visit(descriptor.value, depth + 1);
|
|
131
|
+
}
|
|
132
|
+
if (Array.isArray(value) && Object.keys(value).length !== value.length)
|
|
133
|
+
throw new Error("JSON input contains a sparse array");
|
|
134
|
+
ancestors.delete(value);
|
|
135
|
+
}
|
|
136
|
+
else
|
|
137
|
+
throw new Error("JSON input contains a non-JSON value");
|
|
138
|
+
if (bytes > 16 * 1024 * 1024)
|
|
139
|
+
throw new Error("JSON input exceeds maximum size of 16 MiB");
|
|
140
|
+
}
|
|
141
|
+
visit(val, 0);
|
|
142
|
+
return JSON.parse(JSON.stringify(val));
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Deterministically serialize a JS value into canonical JSON.
|
|
146
|
+
* - Recursively sorts object keys alphabetically.
|
|
147
|
+
* - Ignores undefined object values.
|
|
148
|
+
* - Formats primitives and arrays consistently.
|
|
149
|
+
*/
|
|
150
|
+
export function canonicalizeJson(value) {
|
|
151
|
+
if (value === null || typeof value !== "object") {
|
|
152
|
+
return JSON.stringify(value);
|
|
153
|
+
}
|
|
154
|
+
if (Array.isArray(value)) {
|
|
155
|
+
return "[" + value.map((item) => canonicalizeJson(item)).join(",") + "]";
|
|
156
|
+
}
|
|
157
|
+
const keys = Object.keys(value).sort();
|
|
158
|
+
const entries = [];
|
|
159
|
+
for (const key of keys) {
|
|
160
|
+
const val = value[key];
|
|
161
|
+
if (val !== undefined) {
|
|
162
|
+
entries.push(JSON.stringify(key) + ":" + canonicalizeJson(val));
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return "{" + entries.join(",") + "}";
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Deterministically compute the SHA-256 digest of a DeviceWorkContract.
|
|
169
|
+
* Excludes the digest field itself from the hash payload.
|
|
170
|
+
*/
|
|
171
|
+
export function computeContractDigest(contract) {
|
|
172
|
+
const canonicalPayload = {
|
|
173
|
+
id: contract.id,
|
|
174
|
+
revision: contract.revision,
|
|
175
|
+
executionMode: contract.executionMode,
|
|
176
|
+
goal: contract.goal,
|
|
177
|
+
constraints: contract.constraints ?? [],
|
|
178
|
+
acceptanceCriteria: contract.acceptanceCriteria ?? [],
|
|
179
|
+
baseline: contract.baseline
|
|
180
|
+
? {
|
|
181
|
+
repositoryId: contract.baseline.repositoryId,
|
|
182
|
+
commit: contract.baseline.commit,
|
|
183
|
+
}
|
|
184
|
+
: null,
|
|
185
|
+
inputArtifacts: (contract.inputArtifacts ?? []).map((a) => ({
|
|
186
|
+
id: a.id,
|
|
187
|
+
digest: a.digest,
|
|
188
|
+
producer: {
|
|
189
|
+
deviceId: a.producer.deviceId,
|
|
190
|
+
botId: a.producer.botId,
|
|
191
|
+
taskId: a.producer.taskId,
|
|
192
|
+
attemptId: a.producer.attemptId,
|
|
193
|
+
contractDigest: a.producer.contractDigest ?? null,
|
|
194
|
+
manifestDigest: a.producer.manifestDigest ?? null,
|
|
195
|
+
},
|
|
196
|
+
baseline: a.baseline
|
|
197
|
+
? {
|
|
198
|
+
repositoryId: a.baseline.repositoryId,
|
|
199
|
+
commit: a.baseline.commit,
|
|
200
|
+
}
|
|
201
|
+
: null,
|
|
202
|
+
inputTree: a.inputTree ?? null,
|
|
203
|
+
outputTree: a.outputTree ?? null,
|
|
204
|
+
patchBase64: a.patchBase64 ?? null,
|
|
205
|
+
patchSha256: a.patchSha256 ?? null,
|
|
206
|
+
createdAt: a.createdAt,
|
|
207
|
+
})),
|
|
208
|
+
};
|
|
209
|
+
return createHash("sha256").update(canonicalizeJson(canonicalPayload)).digest("hex");
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Deterministically compute the SHA-256 digest of a ContextManifest.
|
|
213
|
+
* Excludes the digest field itself from the hash payload.
|
|
214
|
+
*/
|
|
215
|
+
export function computeManifestDigest(manifest) {
|
|
216
|
+
const canonicalPayload = {
|
|
217
|
+
version: manifest.version,
|
|
218
|
+
contractId: manifest.contractId,
|
|
219
|
+
contractRevision: manifest.contractRevision,
|
|
220
|
+
contractDigest: manifest.contractDigest,
|
|
221
|
+
executionPolicy: manifest.executionPolicy ?? null,
|
|
222
|
+
context: manifest.context ?? null,
|
|
223
|
+
taskState: manifest.taskState ?? null,
|
|
224
|
+
inputs: manifest.inputs ?? null,
|
|
225
|
+
policyRef: manifest.policyRef ?? null,
|
|
226
|
+
truncationBudget: manifest.truncationBudget ?? null,
|
|
227
|
+
};
|
|
228
|
+
return createHash("sha256").update(canonicalizeJson(canonicalPayload)).digest("hex");
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Deterministically compute the SHA-256 digest of a DeviceArtifact.
|
|
232
|
+
* Excludes the digest field itself from the hash payload.
|
|
233
|
+
*/
|
|
234
|
+
export function computeArtifactDigest(artifact) {
|
|
235
|
+
const canonicalPayload = {
|
|
236
|
+
id: artifact.id,
|
|
237
|
+
producer: {
|
|
238
|
+
deviceId: artifact.producer.deviceId,
|
|
239
|
+
botId: artifact.producer.botId,
|
|
240
|
+
taskId: artifact.producer.taskId,
|
|
241
|
+
attemptId: artifact.producer.attemptId,
|
|
242
|
+
contractDigest: artifact.producer.contractDigest ?? null,
|
|
243
|
+
manifestDigest: artifact.producer.manifestDigest ?? null,
|
|
244
|
+
},
|
|
245
|
+
baseline: artifact.baseline
|
|
246
|
+
? {
|
|
247
|
+
repositoryId: artifact.baseline.repositoryId,
|
|
248
|
+
commit: artifact.baseline.commit,
|
|
249
|
+
}
|
|
250
|
+
: null,
|
|
251
|
+
inputTree: artifact.inputTree ?? null,
|
|
252
|
+
outputTree: artifact.outputTree ?? null,
|
|
253
|
+
patchBase64: artifact.patchBase64 ?? null,
|
|
254
|
+
patchSha256: artifact.patchSha256 ?? null,
|
|
255
|
+
createdAt: artifact.createdAt,
|
|
256
|
+
};
|
|
257
|
+
return createHash("sha256").update(canonicalizeJson(canonicalPayload)).digest("hex");
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Validate a DeviceArtifact:
|
|
261
|
+
* - Strict allowlist (no extra fields like producer.cwd).
|
|
262
|
+
* - Must not contain machine-local file paths.
|
|
263
|
+
* - Must have valid SHA-256 digest.
|
|
264
|
+
* - If baseline is present, commit must be 40-char hex SHA.
|
|
265
|
+
* - If patchBase64 is present, must be strict canonical base64, bounded to 128 KiB decoded,
|
|
266
|
+
* and patchSha256 must match its content hash.
|
|
267
|
+
* - Never throws on malformed input.
|
|
268
|
+
*/
|
|
269
|
+
export function validateDeviceArtifact(artifact) {
|
|
270
|
+
try {
|
|
271
|
+
if (!artifact || typeof artifact !== "object" || Array.isArray(artifact)) {
|
|
272
|
+
return { valid: false, reason: "Artifact must be a non-null object" };
|
|
273
|
+
}
|
|
274
|
+
const a = safeClone(artifact);
|
|
275
|
+
const rootUnknown = checkAllowedKeys(a, ARTIFACT_ALLOWED_KEYS, "artifact");
|
|
276
|
+
if (rootUnknown) {
|
|
277
|
+
return { valid: false, reason: rootUnknown };
|
|
278
|
+
}
|
|
279
|
+
if (typeof a.id !== "string" || !a.id.trim() || a.id.length > MAX_ID_CHARS) {
|
|
280
|
+
return { valid: false, reason: `Artifact id must be a non-empty string up to ${MAX_ID_CHARS} chars` };
|
|
281
|
+
}
|
|
282
|
+
if (typeof a.digest !== "string" || !HEX_64_REGEX.test(a.digest)) {
|
|
283
|
+
return { valid: false, reason: "Artifact digest must be a 64-character hex SHA-256 digest" };
|
|
284
|
+
}
|
|
285
|
+
if (typeof a.createdAt !== "number" || !Number.isFinite(a.createdAt) || a.createdAt < 0) {
|
|
286
|
+
return { valid: false, reason: "Artifact createdAt must be a valid positive timestamp" };
|
|
287
|
+
}
|
|
288
|
+
if (!a.producer || typeof a.producer !== "object" || Array.isArray(a.producer)) {
|
|
289
|
+
return { valid: false, reason: "Artifact producer must be an object" };
|
|
290
|
+
}
|
|
291
|
+
const p = a.producer;
|
|
292
|
+
const prodUnknown = checkAllowedKeys(p, PRODUCER_ALLOWED_KEYS, "artifact.producer");
|
|
293
|
+
if (prodUnknown) {
|
|
294
|
+
return { valid: false, reason: prodUnknown };
|
|
295
|
+
}
|
|
296
|
+
if (typeof p.deviceId !== "string" || !p.deviceId.trim() || p.deviceId.length > MAX_ID_CHARS) {
|
|
297
|
+
return { valid: false, reason: "Artifact producer.deviceId is required" };
|
|
298
|
+
}
|
|
299
|
+
if (typeof p.botId !== "string" || !p.botId.trim() || p.botId.length > MAX_ID_CHARS) {
|
|
300
|
+
return { valid: false, reason: "Artifact producer.botId is required" };
|
|
301
|
+
}
|
|
302
|
+
if (typeof p.taskId !== "string" || !p.taskId.trim() || p.taskId.length > MAX_ID_CHARS) {
|
|
303
|
+
return { valid: false, reason: "Artifact producer.taskId is required" };
|
|
304
|
+
}
|
|
305
|
+
if (typeof p.attemptId !== "string" || !p.attemptId.trim() || p.attemptId.length > MAX_ID_CHARS) {
|
|
306
|
+
return { valid: false, reason: "Artifact producer.attemptId is required" };
|
|
307
|
+
}
|
|
308
|
+
if (p.contractDigest !== undefined &&
|
|
309
|
+
(typeof p.contractDigest !== "string" || !HEX_64_REGEX.test(p.contractDigest))) {
|
|
310
|
+
return { valid: false, reason: "Artifact producer.contractDigest must be a 64-character hex SHA-256 string" };
|
|
311
|
+
}
|
|
312
|
+
if (p.manifestDigest !== undefined &&
|
|
313
|
+
(typeof p.manifestDigest !== "string" || !HEX_64_REGEX.test(p.manifestDigest))) {
|
|
314
|
+
return { valid: false, reason: "Artifact producer.manifestDigest must be a 64-character hex SHA-256 string" };
|
|
315
|
+
}
|
|
316
|
+
if (a.baseline !== undefined && a.baseline !== null) {
|
|
317
|
+
if (typeof a.baseline !== "object" || Array.isArray(a.baseline)) {
|
|
318
|
+
return { valid: false, reason: "Artifact baseline must be an object" };
|
|
319
|
+
}
|
|
320
|
+
const b = a.baseline;
|
|
321
|
+
const baseUnknown = checkAllowedKeys(b, BASELINE_ALLOWED_KEYS, "artifact.baseline");
|
|
322
|
+
if (baseUnknown) {
|
|
323
|
+
return { valid: false, reason: baseUnknown };
|
|
324
|
+
}
|
|
325
|
+
if (typeof b.repositoryId !== "string" || !b.repositoryId.trim() || b.repositoryId.length > MAX_REPO_ID_CHARS) {
|
|
326
|
+
return { valid: false, reason: "Artifact baseline.repositoryId must be a non-empty string" };
|
|
327
|
+
}
|
|
328
|
+
if (typeof b.commit !== "string" || !COMMIT_SHA_REGEX.test(b.commit)) {
|
|
329
|
+
return { valid: false, reason: "Artifact baseline.commit must be a 40-character hex commit SHA" };
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
// Trees must not be local paths and if provided must be 40-character hex tree SHAs
|
|
333
|
+
if (a.inputTree !== undefined && a.inputTree !== null) {
|
|
334
|
+
if (typeof a.inputTree !== "string" || !TREE_HEX_REGEX.test(a.inputTree)) {
|
|
335
|
+
return { valid: false, reason: "Artifact inputTree must be a 40-character hex tree SHA" };
|
|
336
|
+
}
|
|
337
|
+
if (LOCAL_PATH_REGEX.test(a.inputTree)) {
|
|
338
|
+
return { valid: false, reason: "Artifact inputTree must not be a machine-local file path" };
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
if (a.outputTree !== undefined && a.outputTree !== null) {
|
|
342
|
+
if (typeof a.outputTree !== "string" || !TREE_HEX_REGEX.test(a.outputTree)) {
|
|
343
|
+
return { valid: false, reason: "Artifact outputTree must be a 40-character hex tree SHA" };
|
|
344
|
+
}
|
|
345
|
+
if (LOCAL_PATH_REGEX.test(a.outputTree)) {
|
|
346
|
+
return { valid: false, reason: "Artifact outputTree must not be a machine-local file path" };
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
if ((a.patchBase64 === undefined) !== (a.patchSha256 === undefined)) {
|
|
350
|
+
return { valid: false, reason: "Artifact patchBase64 and patchSha256 must be provided together" };
|
|
351
|
+
}
|
|
352
|
+
if (a.patchBase64 !== undefined) {
|
|
353
|
+
if (typeof a.patchBase64 !== "string") {
|
|
354
|
+
return { valid: false, reason: "Artifact patchBase64 must be a string" };
|
|
355
|
+
}
|
|
356
|
+
if (a.patchBase64.length > 4 * Math.ceil(MAX_PATCH_BYTES / 3)) {
|
|
357
|
+
return { valid: false, reason: `Artifact patch exceeds maximum bounded size of ${MAX_PATCH_BYTES} bytes` };
|
|
358
|
+
}
|
|
359
|
+
if (!STRICT_BASE64_REGEX.test(a.patchBase64)) {
|
|
360
|
+
return { valid: false, reason: "Artifact patchBase64 contains invalid base64 characters or padding" };
|
|
361
|
+
}
|
|
362
|
+
if (typeof a.patchSha256 !== "string" || !HEX_64_REGEX.test(a.patchSha256)) {
|
|
363
|
+
return { valid: false, reason: "Artifact patchSha256 must be a 64-character hex SHA-256 string" };
|
|
364
|
+
}
|
|
365
|
+
const decoded = Buffer.from(a.patchBase64, "base64");
|
|
366
|
+
if (decoded.byteLength > MAX_PATCH_BYTES) {
|
|
367
|
+
return { valid: false, reason: `Artifact patch exceeds maximum bounded size of ${MAX_PATCH_BYTES} bytes` };
|
|
368
|
+
}
|
|
369
|
+
if (decoded.toString("base64") !== a.patchBase64) {
|
|
370
|
+
return { valid: false, reason: "Artifact patchBase64 is not strictly canonical" };
|
|
371
|
+
}
|
|
372
|
+
const computedPatchSha256 = createHash("sha256").update(decoded).digest("hex");
|
|
373
|
+
if (computedPatchSha256.toLowerCase() !== a.patchSha256.toLowerCase()) {
|
|
374
|
+
return { valid: false, reason: "Artifact patchSha256 does not match patchBase64 content" };
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
const typedArtifact = a;
|
|
378
|
+
const computedDigest = computeArtifactDigest(typedArtifact);
|
|
379
|
+
if (computedDigest.toLowerCase() !== typedArtifact.digest.toLowerCase()) {
|
|
380
|
+
return {
|
|
381
|
+
valid: false,
|
|
382
|
+
reason: `Artifact digest mismatch: expected ${computedDigest}, got ${typedArtifact.digest}`,
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
return { valid: true, artifact: deepFreeze(typedArtifact) };
|
|
386
|
+
}
|
|
387
|
+
catch (err) {
|
|
388
|
+
return { valid: false, reason: `Malformed artifact: ${err?.message ?? "unknown error"}` };
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
function codeArtifactProofError(artifact) {
|
|
392
|
+
if (!artifact.baseline)
|
|
393
|
+
return "Code artifact requires complete baseline";
|
|
394
|
+
if (!artifact.inputTree || !artifact.outputTree)
|
|
395
|
+
return "Code artifact requires 40-character hex inputTree and outputTree";
|
|
396
|
+
if (typeof artifact.patchBase64 !== "string" || !artifact.patchSha256)
|
|
397
|
+
return "Code artifact requires patchBase64 and patchSha256 (empty patch is allowed)";
|
|
398
|
+
if (!artifact.producer.contractDigest || !artifact.producer.manifestDigest)
|
|
399
|
+
return "Code artifact requires producer contractDigest and manifestDigest";
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Validate input artifacts and ensure they can be safely synthesized for the contract.
|
|
403
|
+
* Rejects if artifacts conflict, lack matching baseline, or cannot be verified.
|
|
404
|
+
* Never throws on malformed input.
|
|
405
|
+
*/
|
|
406
|
+
export function validateInputArtifacts(artifacts, baseline, executionMode) {
|
|
407
|
+
try {
|
|
408
|
+
if (artifacts === undefined) {
|
|
409
|
+
return { valid: true, artifacts: [] };
|
|
410
|
+
}
|
|
411
|
+
if (!Array.isArray(artifacts)) {
|
|
412
|
+
return { valid: false, reason: "inputArtifacts must be an array" };
|
|
413
|
+
}
|
|
414
|
+
if (artifacts.length === 0) {
|
|
415
|
+
return { valid: true, artifacts: [] };
|
|
416
|
+
}
|
|
417
|
+
if (executionMode === "chat") {
|
|
418
|
+
return { valid: false, reason: "Chat execution mode does not support inputArtifacts synthesis" };
|
|
419
|
+
}
|
|
420
|
+
if (artifacts.length > MAX_INPUT_ARTIFACTS) {
|
|
421
|
+
return { valid: false, reason: `inputArtifacts exceeds maximum limit of ${MAX_INPUT_ARTIFACTS} items` };
|
|
422
|
+
}
|
|
423
|
+
const validated = [];
|
|
424
|
+
const seenIds = new Set();
|
|
425
|
+
for (let i = 0; i < artifacts.length; i++) {
|
|
426
|
+
const raw = artifacts[i];
|
|
427
|
+
const res = validateDeviceArtifact(raw);
|
|
428
|
+
if (!res.valid) {
|
|
429
|
+
return { valid: false, reason: `inputArtifacts[${i}] is invalid: ${res.reason}` };
|
|
430
|
+
}
|
|
431
|
+
const art = res.artifact;
|
|
432
|
+
if (seenIds.has(art.id)) {
|
|
433
|
+
return { valid: false, reason: `Duplicate artifact id in inputArtifacts: ${art.id}` };
|
|
434
|
+
}
|
|
435
|
+
seenIds.add(art.id);
|
|
436
|
+
if (baseline && art.baseline) {
|
|
437
|
+
if (art.baseline.repositoryId !== baseline.repositoryId) {
|
|
438
|
+
return {
|
|
439
|
+
valid: false,
|
|
440
|
+
reason: `Artifact baseline repository mismatch: artifact has '${art.baseline.repositoryId}', contract requires '${baseline.repositoryId}'`,
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
if (art.baseline.commit.toLowerCase() !== baseline.commit.toLowerCase()) {
|
|
444
|
+
return {
|
|
445
|
+
valid: false,
|
|
446
|
+
reason: `Artifact baseline commit mismatch: artifact has '${art.baseline.commit}', contract requires '${baseline.commit}'`,
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
const proofError = codeArtifactProofError(art);
|
|
451
|
+
if (proofError)
|
|
452
|
+
return { valid: false, reason: `inputArtifacts[${i}]: ${proofError}` };
|
|
453
|
+
if (!baseline)
|
|
454
|
+
return { valid: false, reason: "inputArtifacts require a contract baseline" };
|
|
455
|
+
validated.push(art);
|
|
456
|
+
}
|
|
457
|
+
return { valid: true, artifacts: validated };
|
|
458
|
+
}
|
|
459
|
+
catch (err) {
|
|
460
|
+
return { valid: false, reason: `Malformed inputArtifacts: ${err?.message ?? "unknown error"}` };
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* Create an immutable DeviceWorkContract with computed digest.
|
|
465
|
+
* Recursively deep-freezes and clones inputs so callers cannot mutate the contract after creation.
|
|
466
|
+
* executionMode defaults to "chat".
|
|
467
|
+
* If executionMode === "code", baseline with stable repositoryId and full 40-char commit SHA is strictly required.
|
|
468
|
+
*/
|
|
469
|
+
export function createWorkContract(input, fallbackGoal) {
|
|
470
|
+
if (!input || typeof input !== "object" || Array.isArray(input)) {
|
|
471
|
+
throw new Error("Contract input must be a non-null object");
|
|
472
|
+
}
|
|
473
|
+
input = safeClone(input);
|
|
474
|
+
const unknownKey = checkAllowedKeys(input, CONTRACT_INPUT_ALLOWED_KEYS, "contract input");
|
|
475
|
+
if (unknownKey) {
|
|
476
|
+
throw new Error(unknownKey);
|
|
477
|
+
}
|
|
478
|
+
const executionMode = input.executionMode === undefined ? "chat" : input.executionMode;
|
|
479
|
+
if (executionMode !== "chat" && executionMode !== "code") {
|
|
480
|
+
throw new Error(`Invalid executionMode: expected 'chat' or 'code', got '${executionMode}'`);
|
|
481
|
+
}
|
|
482
|
+
const rawGoal = input.goal === undefined ? (fallbackGoal ?? "") : input.goal;
|
|
483
|
+
if (typeof rawGoal !== "string" || !rawGoal.trim()) {
|
|
484
|
+
throw new Error("Contract goal must be a non-empty string");
|
|
485
|
+
}
|
|
486
|
+
const goal = rawGoal.trim();
|
|
487
|
+
if (goal.length > MAX_GOAL_CHARS) {
|
|
488
|
+
throw new Error(`Contract goal exceeds maximum length of ${MAX_GOAL_CHARS} characters`);
|
|
489
|
+
}
|
|
490
|
+
if (input.id !== undefined && (typeof input.id !== "string" || !input.id.trim()))
|
|
491
|
+
throw new Error("Contract id must be a non-empty string");
|
|
492
|
+
const id = input.id?.trim() ?? `contract-${randomUUID()}`;
|
|
493
|
+
if (id.length > MAX_ID_CHARS) {
|
|
494
|
+
throw new Error(`Contract id exceeds maximum length of ${MAX_ID_CHARS} characters`);
|
|
495
|
+
}
|
|
496
|
+
const revision = input.revision === undefined ? 1 : input.revision;
|
|
497
|
+
if (!Number.isSafeInteger(revision) || revision < 1 || revision > 1_000_000) {
|
|
498
|
+
throw new Error(`Contract revision must be a positive integer up to 1,000,000, got ${revision}`);
|
|
499
|
+
}
|
|
500
|
+
let constraints;
|
|
501
|
+
if (input.constraints !== undefined) {
|
|
502
|
+
if (!Array.isArray(input.constraints)) {
|
|
503
|
+
throw new Error("Contract constraints must be an array of strings");
|
|
504
|
+
}
|
|
505
|
+
if (input.constraints.length > MAX_ARRAY_ITEMS) {
|
|
506
|
+
throw new Error(`Contract constraints exceeds limit of ${MAX_ARRAY_ITEMS} items`);
|
|
507
|
+
}
|
|
508
|
+
for (let i = 0; i < input.constraints.length; i++) {
|
|
509
|
+
const item = input.constraints[i];
|
|
510
|
+
if (typeof item !== "string" || item.length > MAX_ITEM_CHARS) {
|
|
511
|
+
throw new Error(`Contract constraints[${i}] must be a string up to ${MAX_ITEM_CHARS} characters`);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
constraints = [...input.constraints];
|
|
515
|
+
}
|
|
516
|
+
let acceptanceCriteria;
|
|
517
|
+
if (input.acceptanceCriteria !== undefined) {
|
|
518
|
+
if (!Array.isArray(input.acceptanceCriteria)) {
|
|
519
|
+
throw new Error("Contract acceptanceCriteria must be an array of strings");
|
|
520
|
+
}
|
|
521
|
+
if (input.acceptanceCriteria.length > MAX_ARRAY_ITEMS) {
|
|
522
|
+
throw new Error(`Contract acceptanceCriteria exceeds limit of ${MAX_ARRAY_ITEMS} items`);
|
|
523
|
+
}
|
|
524
|
+
for (let i = 0; i < input.acceptanceCriteria.length; i++) {
|
|
525
|
+
const item = input.acceptanceCriteria[i];
|
|
526
|
+
if (typeof item !== "string" || item.length > MAX_ITEM_CHARS) {
|
|
527
|
+
throw new Error(`Contract acceptanceCriteria[${i}] must be a string up to ${MAX_ITEM_CHARS} characters`);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
acceptanceCriteria = [...input.acceptanceCriteria];
|
|
531
|
+
}
|
|
532
|
+
let baseline;
|
|
533
|
+
if (executionMode === "code") {
|
|
534
|
+
if (!input.baseline || typeof input.baseline !== "object" || Array.isArray(input.baseline)) {
|
|
535
|
+
throw new Error("Code executionMode requires a baseline with repositoryId and commit SHA");
|
|
536
|
+
}
|
|
537
|
+
const baseUnknown = checkAllowedKeys(input.baseline, BASELINE_ALLOWED_KEYS, "baseline");
|
|
538
|
+
if (baseUnknown) {
|
|
539
|
+
throw new Error(baseUnknown);
|
|
540
|
+
}
|
|
541
|
+
if (typeof input.baseline.repositoryId !== "string" || !input.baseline.repositoryId.trim()) {
|
|
542
|
+
throw new Error("Code baseline.repositoryId must be a non-empty string");
|
|
543
|
+
}
|
|
544
|
+
if (input.baseline.repositoryId.trim().length > MAX_REPO_ID_CHARS) {
|
|
545
|
+
throw new Error(`Code baseline.repositoryId exceeds maximum length of ${MAX_REPO_ID_CHARS}`);
|
|
546
|
+
}
|
|
547
|
+
if (typeof input.baseline.commit !== "string" || !COMMIT_SHA_REGEX.test(input.baseline.commit)) {
|
|
548
|
+
throw new Error("Code baseline.commit must be a 40-character hex commit SHA");
|
|
549
|
+
}
|
|
550
|
+
baseline = {
|
|
551
|
+
repositoryId: input.baseline.repositoryId.trim(),
|
|
552
|
+
commit: input.baseline.commit.trim().toLowerCase(),
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
else if (input.baseline !== undefined) {
|
|
556
|
+
if (!input.baseline || typeof input.baseline !== "object" || Array.isArray(input.baseline)) {
|
|
557
|
+
throw new Error("Contract baseline must be an object");
|
|
558
|
+
}
|
|
559
|
+
const baseUnknown = checkAllowedKeys(input.baseline, BASELINE_ALLOWED_KEYS, "baseline");
|
|
560
|
+
if (baseUnknown) {
|
|
561
|
+
throw new Error(baseUnknown);
|
|
562
|
+
}
|
|
563
|
+
if (typeof input.baseline.repositoryId !== "string" || !input.baseline.repositoryId.trim()) {
|
|
564
|
+
throw new Error("baseline.repositoryId must be a non-empty string");
|
|
565
|
+
}
|
|
566
|
+
if (input.baseline.repositoryId.trim().length > MAX_REPO_ID_CHARS) {
|
|
567
|
+
throw new Error(`baseline.repositoryId exceeds maximum length of ${MAX_REPO_ID_CHARS}`);
|
|
568
|
+
}
|
|
569
|
+
if (typeof input.baseline.commit !== "string" || !COMMIT_SHA_REGEX.test(input.baseline.commit)) {
|
|
570
|
+
throw new Error("baseline.commit must be a 40-character hex commit SHA");
|
|
571
|
+
}
|
|
572
|
+
baseline = {
|
|
573
|
+
repositoryId: input.baseline.repositoryId.trim(),
|
|
574
|
+
commit: input.baseline.commit.trim().toLowerCase(),
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
const clonedArtifacts = input.inputArtifacts === undefined ? undefined : safeClone(input.inputArtifacts);
|
|
578
|
+
const synthCheck = validateInputArtifacts(clonedArtifacts, baseline, executionMode);
|
|
579
|
+
if (!synthCheck.valid) {
|
|
580
|
+
throw new Error(`Cannot safely synthesize input artifacts: ${synthCheck.reason}`);
|
|
581
|
+
}
|
|
582
|
+
const contractWithoutDigest = {
|
|
583
|
+
id,
|
|
584
|
+
revision,
|
|
585
|
+
executionMode,
|
|
586
|
+
goal,
|
|
587
|
+
constraints,
|
|
588
|
+
acceptanceCriteria,
|
|
589
|
+
baseline,
|
|
590
|
+
inputArtifacts: synthCheck.artifacts.length > 0 ? synthCheck.artifacts : undefined,
|
|
591
|
+
createdAt: Date.now(),
|
|
592
|
+
};
|
|
593
|
+
const digest = computeContractDigest(contractWithoutDigest);
|
|
594
|
+
const contract = {
|
|
595
|
+
...contractWithoutDigest,
|
|
596
|
+
digest,
|
|
597
|
+
};
|
|
598
|
+
if (Buffer.byteLength(JSON.stringify(contract)) > MAX_WORK_CONTRACT_BYTES)
|
|
599
|
+
throw new Error(`Contract exceeds maximum size of ${MAX_WORK_CONTRACT_BYTES} bytes`);
|
|
600
|
+
return deepFreeze(contract);
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Validate a DeviceWorkContract:
|
|
604
|
+
* - Strict allowlist (no extra/unknown fields).
|
|
605
|
+
* - Checks executionMode ("chat" | "code").
|
|
606
|
+
* - If "code", verifies baseline repositoryId and 40-char commit SHA.
|
|
607
|
+
* - Verifies inputArtifacts safe synthesis.
|
|
608
|
+
* - Verifies immutable SHA-256 digest.
|
|
609
|
+
* - Never throws on malformed input.
|
|
610
|
+
*/
|
|
611
|
+
export function validateWorkContract(contract) {
|
|
612
|
+
try {
|
|
613
|
+
if (!contract || typeof contract !== "object" || Array.isArray(contract)) {
|
|
614
|
+
return { valid: false, reason: "Contract must be a non-null object" };
|
|
615
|
+
}
|
|
616
|
+
const c = safeClone(contract);
|
|
617
|
+
if (Buffer.byteLength(JSON.stringify(c)) > MAX_WORK_CONTRACT_BYTES)
|
|
618
|
+
return { valid: false, reason: `Contract exceeds maximum size of ${MAX_WORK_CONTRACT_BYTES} bytes` };
|
|
619
|
+
const rootUnknown = checkAllowedKeys(c, CONTRACT_ALLOWED_KEYS, "contract");
|
|
620
|
+
if (rootUnknown) {
|
|
621
|
+
return { valid: false, reason: rootUnknown };
|
|
622
|
+
}
|
|
623
|
+
if (typeof c.id !== "string" || !c.id.trim() || c.id.length > MAX_ID_CHARS) {
|
|
624
|
+
return { valid: false, reason: `Contract id must be a string up to ${MAX_ID_CHARS} chars` };
|
|
625
|
+
}
|
|
626
|
+
if (!Number.isSafeInteger(c.revision) || c.revision < 1 || c.revision > 1_000_000) {
|
|
627
|
+
return { valid: false, reason: "Contract revision must be a positive integer up to 1,000,000" };
|
|
628
|
+
}
|
|
629
|
+
if (c.createdAt !== undefined &&
|
|
630
|
+
(typeof c.createdAt !== "number" || !Number.isFinite(c.createdAt) || c.createdAt < 0))
|
|
631
|
+
return { valid: false, reason: "Contract createdAt must be a valid timestamp" };
|
|
632
|
+
if (c.executionMode !== "chat" && c.executionMode !== "code") {
|
|
633
|
+
return { valid: false, reason: "Contract executionMode must be 'chat' or 'code'" };
|
|
634
|
+
}
|
|
635
|
+
if (typeof c.goal !== "string" || !c.goal.trim() || c.goal.length > MAX_GOAL_CHARS) {
|
|
636
|
+
return { valid: false, reason: `Contract goal must be a non-empty string up to ${MAX_GOAL_CHARS} chars` };
|
|
637
|
+
}
|
|
638
|
+
if (c.constraints !== undefined) {
|
|
639
|
+
if (!Array.isArray(c.constraints)) {
|
|
640
|
+
return { valid: false, reason: "Contract constraints must be an array" };
|
|
641
|
+
}
|
|
642
|
+
if (c.constraints.length > MAX_ARRAY_ITEMS) {
|
|
643
|
+
return { valid: false, reason: `Contract constraints exceeds ${MAX_ARRAY_ITEMS} items` };
|
|
644
|
+
}
|
|
645
|
+
for (let i = 0; i < c.constraints.length; i++) {
|
|
646
|
+
const item = c.constraints[i];
|
|
647
|
+
if (typeof item !== "string" || item.length > MAX_ITEM_CHARS) {
|
|
648
|
+
return { valid: false, reason: `Contract constraints[${i}] must be string up to ${MAX_ITEM_CHARS} chars` };
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
if (c.acceptanceCriteria !== undefined) {
|
|
653
|
+
if (!Array.isArray(c.acceptanceCriteria)) {
|
|
654
|
+
return { valid: false, reason: "Contract acceptanceCriteria must be an array" };
|
|
655
|
+
}
|
|
656
|
+
if (c.acceptanceCriteria.length > MAX_ARRAY_ITEMS) {
|
|
657
|
+
return { valid: false, reason: `Contract acceptanceCriteria exceeds ${MAX_ARRAY_ITEMS} items` };
|
|
658
|
+
}
|
|
659
|
+
for (let i = 0; i < c.acceptanceCriteria.length; i++) {
|
|
660
|
+
const item = c.acceptanceCriteria[i];
|
|
661
|
+
if (typeof item !== "string" || item.length > MAX_ITEM_CHARS) {
|
|
662
|
+
return {
|
|
663
|
+
valid: false,
|
|
664
|
+
reason: `Contract acceptanceCriteria[${i}] must be string up to ${MAX_ITEM_CHARS} chars`,
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
if (c.executionMode === "code") {
|
|
670
|
+
if (!c.baseline || typeof c.baseline !== "object" || Array.isArray(c.baseline)) {
|
|
671
|
+
return { valid: false, reason: "Code contract requires baseline object" };
|
|
672
|
+
}
|
|
673
|
+
const b = c.baseline;
|
|
674
|
+
const baseUnknown = checkAllowedKeys(b, BASELINE_ALLOWED_KEYS, "contract.baseline");
|
|
675
|
+
if (baseUnknown) {
|
|
676
|
+
return { valid: false, reason: baseUnknown };
|
|
677
|
+
}
|
|
678
|
+
if (typeof b.repositoryId !== "string" || !b.repositoryId.trim() || b.repositoryId.length > MAX_REPO_ID_CHARS) {
|
|
679
|
+
return { valid: false, reason: "Code contract baseline.repositoryId must be non-empty string" };
|
|
680
|
+
}
|
|
681
|
+
if (typeof b.commit !== "string" || !COMMIT_SHA_REGEX.test(b.commit)) {
|
|
682
|
+
return { valid: false, reason: "Code contract baseline.commit must be 40-character hex commit SHA" };
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
else if (c.baseline !== undefined) {
|
|
686
|
+
if (!c.baseline || typeof c.baseline !== "object" || Array.isArray(c.baseline)) {
|
|
687
|
+
return { valid: false, reason: "Contract baseline must be an object" };
|
|
688
|
+
}
|
|
689
|
+
const b = c.baseline;
|
|
690
|
+
const baseUnknown = checkAllowedKeys(b, BASELINE_ALLOWED_KEYS, "contract.baseline");
|
|
691
|
+
if (baseUnknown) {
|
|
692
|
+
return { valid: false, reason: baseUnknown };
|
|
693
|
+
}
|
|
694
|
+
if (typeof b.repositoryId !== "string" || !b.repositoryId.trim() || b.repositoryId.length > MAX_REPO_ID_CHARS) {
|
|
695
|
+
return { valid: false, reason: "Contract baseline.repositoryId must be non-empty string" };
|
|
696
|
+
}
|
|
697
|
+
if (typeof b.commit !== "string" || !COMMIT_SHA_REGEX.test(b.commit)) {
|
|
698
|
+
return { valid: false, reason: "Contract baseline.commit must be 40-character hex commit SHA" };
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
const baseline = c.baseline;
|
|
702
|
+
const synth = validateInputArtifacts(c.inputArtifacts, baseline, c.executionMode);
|
|
703
|
+
if (!synth.valid) {
|
|
704
|
+
return { valid: false, reason: `inputArtifacts invalid: ${synth.reason}` };
|
|
705
|
+
}
|
|
706
|
+
if (typeof c.digest !== "string" || !HEX_64_REGEX.test(c.digest)) {
|
|
707
|
+
return { valid: false, reason: "Contract digest must be a 64-character hex SHA-256 digest" };
|
|
708
|
+
}
|
|
709
|
+
const typedContract = c;
|
|
710
|
+
const computedDigest = computeContractDigest(typedContract);
|
|
711
|
+
if (computedDigest.toLowerCase() !== typedContract.digest.toLowerCase()) {
|
|
712
|
+
return {
|
|
713
|
+
valid: false,
|
|
714
|
+
reason: `Contract digest mismatch: expected ${computedDigest}, got ${typedContract.digest}`,
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
return { valid: true, contract: deepFreeze(typedContract) };
|
|
718
|
+
}
|
|
719
|
+
catch (err) {
|
|
720
|
+
return { valid: false, reason: `Malformed contract: ${err?.message ?? "unknown error"}` };
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
export function assertValidWorkContract(contract) {
|
|
724
|
+
const res = validateWorkContract(contract);
|
|
725
|
+
if (!res.valid) {
|
|
726
|
+
throw new Error(`Invalid work contract: ${res.reason}`);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
* Freeze a ContextManifest before task delivery.
|
|
731
|
+
* Clones inputs, context, and taskState to prevent later external mutation, and deeply freezes the result.
|
|
732
|
+
*/
|
|
733
|
+
export function createContextManifest(args) {
|
|
734
|
+
assertValidWorkContract(args.contract);
|
|
735
|
+
if (args.policyRef !== undefined &&
|
|
736
|
+
(typeof args.policyRef !== "string" || !args.policyRef.trim() || args.policyRef.length > MAX_POLICY_REF_CHARS)) {
|
|
737
|
+
throw new Error(`policyRef must be a string up to ${MAX_POLICY_REF_CHARS} characters`);
|
|
738
|
+
}
|
|
739
|
+
if (args.truncationBudget !== undefined &&
|
|
740
|
+
(!Number.isSafeInteger(args.truncationBudget) ||
|
|
741
|
+
args.truncationBudget < 1 ||
|
|
742
|
+
args.truncationBudget > MAX_TRUNCATION_BUDGET)) {
|
|
743
|
+
throw new Error(`truncationBudget must be a positive integer up to ${MAX_TRUNCATION_BUDGET}`);
|
|
744
|
+
}
|
|
745
|
+
const clonedContext = args.context !== undefined ? safeClone(args.context) : null;
|
|
746
|
+
const clonedTaskState = args.taskState !== undefined ? safeClone(args.taskState) : null;
|
|
747
|
+
const clonedInputs = args.inputs !== undefined ? safeClone(args.inputs) : contractInputReferences(args.contract);
|
|
748
|
+
const manifestWithoutDigest = {
|
|
749
|
+
version: 1,
|
|
750
|
+
contractId: args.contract.id,
|
|
751
|
+
contractRevision: args.contract.revision,
|
|
752
|
+
contractDigest: args.contract.digest,
|
|
753
|
+
executionPolicy: args.executionPolicy === undefined ? undefined : safeClone(args.executionPolicy),
|
|
754
|
+
context: clonedContext,
|
|
755
|
+
taskState: clonedTaskState,
|
|
756
|
+
inputs: clonedInputs,
|
|
757
|
+
policyRef: args.policyRef,
|
|
758
|
+
truncationBudget: args.truncationBudget,
|
|
759
|
+
createdAt: args.createdAt ?? Date.now(),
|
|
760
|
+
};
|
|
761
|
+
const digest = computeManifestDigest(manifestWithoutDigest);
|
|
762
|
+
const manifest = {
|
|
763
|
+
...manifestWithoutDigest,
|
|
764
|
+
digest,
|
|
765
|
+
};
|
|
766
|
+
const validated = validateContextManifest(manifest, args.contract);
|
|
767
|
+
if (!validated.valid)
|
|
768
|
+
throw new Error(validated.reason);
|
|
769
|
+
return validated.manifest;
|
|
770
|
+
}
|
|
771
|
+
/**
|
|
772
|
+
* Validate a ContextManifest and ensure its references match the expected contract.
|
|
773
|
+
* Never throws on malformed input.
|
|
774
|
+
*/
|
|
775
|
+
export function validateContextManifest(manifest, expectedContract) {
|
|
776
|
+
try {
|
|
777
|
+
if (!manifest || typeof manifest !== "object" || Array.isArray(manifest)) {
|
|
778
|
+
return { valid: false, reason: "ContextManifest must be a non-null object" };
|
|
779
|
+
}
|
|
780
|
+
const m = safeClone(manifest);
|
|
781
|
+
if (Buffer.byteLength(JSON.stringify(m)) > MAX_CONTEXT_MANIFEST_BYTES)
|
|
782
|
+
return { valid: false, reason: `ContextManifest exceeds maximum size of ${MAX_CONTEXT_MANIFEST_BYTES} bytes` };
|
|
783
|
+
const rootUnknown = checkAllowedKeys(m, MANIFEST_ALLOWED_KEYS, "contextManifest");
|
|
784
|
+
if (rootUnknown) {
|
|
785
|
+
return { valid: false, reason: rootUnknown };
|
|
786
|
+
}
|
|
787
|
+
if (m.executionPolicy !== undefined) {
|
|
788
|
+
const p = m.executionPolicy;
|
|
789
|
+
if (!p ||
|
|
790
|
+
typeof p !== "object" ||
|
|
791
|
+
Array.isArray(p) ||
|
|
792
|
+
checkAllowedKeys(p, new Set(["deviceId", "botId", "botRevision"]), "executionPolicy") ||
|
|
793
|
+
!Number.isSafeInteger(p.botRevision) ||
|
|
794
|
+
p.botRevision < 1 ||
|
|
795
|
+
["deviceId", "botId"].some((key) => typeof p[key] !== "string" || !p[key].trim() || p[key].length > MAX_ID_CHARS))
|
|
796
|
+
return {
|
|
797
|
+
valid: false,
|
|
798
|
+
reason: "ContextManifest executionPolicy must reference deviceId, botId and a positive botRevision",
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
if (typeof m.createdAt !== "number" || !Number.isFinite(m.createdAt) || m.createdAt < 0)
|
|
802
|
+
return { valid: false, reason: "ContextManifest createdAt must be a valid timestamp" };
|
|
803
|
+
if (m.context === undefined)
|
|
804
|
+
return { valid: false, reason: "ContextManifest context is required (null is allowed)" };
|
|
805
|
+
if (m.version !== 1) {
|
|
806
|
+
return { valid: false, reason: `ContextManifest version must be 1, got ${m.version}` };
|
|
807
|
+
}
|
|
808
|
+
if (typeof m.contractId !== "string" || !m.contractId || m.contractId.length > MAX_ID_CHARS) {
|
|
809
|
+
return { valid: false, reason: "ContextManifest contractId is required" };
|
|
810
|
+
}
|
|
811
|
+
if (!Number.isSafeInteger(m.contractRevision) || m.contractRevision < 1) {
|
|
812
|
+
return { valid: false, reason: "ContextManifest contractRevision must be positive integer" };
|
|
813
|
+
}
|
|
814
|
+
if (typeof m.contractDigest !== "string" || !HEX_64_REGEX.test(m.contractDigest)) {
|
|
815
|
+
return { valid: false, reason: "ContextManifest contractDigest must be 64-char hex SHA-256" };
|
|
816
|
+
}
|
|
817
|
+
if (typeof m.digest !== "string" || !HEX_64_REGEX.test(m.digest)) {
|
|
818
|
+
return { valid: false, reason: "ContextManifest digest must be 64-char hex SHA-256" };
|
|
819
|
+
}
|
|
820
|
+
if (m.policyRef !== undefined &&
|
|
821
|
+
(typeof m.policyRef !== "string" || !m.policyRef.trim() || m.policyRef.length > MAX_POLICY_REF_CHARS)) {
|
|
822
|
+
return { valid: false, reason: `policyRef must be a string up to ${MAX_POLICY_REF_CHARS} chars` };
|
|
823
|
+
}
|
|
824
|
+
if (m.truncationBudget !== undefined &&
|
|
825
|
+
(!Number.isSafeInteger(m.truncationBudget) ||
|
|
826
|
+
m.truncationBudget < 1 ||
|
|
827
|
+
m.truncationBudget > MAX_TRUNCATION_BUDGET)) {
|
|
828
|
+
return { valid: false, reason: `truncationBudget must be integer up to ${MAX_TRUNCATION_BUDGET}` };
|
|
829
|
+
}
|
|
830
|
+
if (typeof m.truncationBudget === "number" && Buffer.byteLength(JSON.stringify(m.context)) > m.truncationBudget)
|
|
831
|
+
return { valid: false, reason: "ContextManifest context exceeds truncationBudget" };
|
|
832
|
+
const typedManifest = m;
|
|
833
|
+
const computedDigest = computeManifestDigest(typedManifest);
|
|
834
|
+
if (computedDigest.toLowerCase() !== typedManifest.digest.toLowerCase()) {
|
|
835
|
+
return {
|
|
836
|
+
valid: false,
|
|
837
|
+
reason: `ContextManifest digest mismatch: expected ${computedDigest}, got ${typedManifest.digest}`,
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
if (expectedContract) {
|
|
841
|
+
const contractCheck = validateWorkContract(expectedContract);
|
|
842
|
+
if (!contractCheck.valid)
|
|
843
|
+
return { valid: false, reason: `Invalid expected contract: ${contractCheck.reason}` };
|
|
844
|
+
if (canonicalizeJson(typedManifest.inputs ?? null) !== canonicalizeJson(contractInputReferences(expectedContract)))
|
|
845
|
+
return { valid: false, reason: "ContextManifest inputs do not match contract inputArtifacts" };
|
|
846
|
+
if (typedManifest.contractId !== expectedContract.id) {
|
|
847
|
+
return {
|
|
848
|
+
valid: false,
|
|
849
|
+
reason: `ContextManifest contractId '${typedManifest.contractId}' does not match expected contract id '${expectedContract.id}'`,
|
|
850
|
+
};
|
|
851
|
+
}
|
|
852
|
+
if (typedManifest.contractRevision !== expectedContract.revision) {
|
|
853
|
+
return {
|
|
854
|
+
valid: false,
|
|
855
|
+
reason: `ContextManifest contractRevision '${typedManifest.contractRevision}' does not match expected revision '${expectedContract.revision}'`,
|
|
856
|
+
};
|
|
857
|
+
}
|
|
858
|
+
if (typedManifest.contractDigest.toLowerCase() !== expectedContract.digest.toLowerCase()) {
|
|
859
|
+
return {
|
|
860
|
+
valid: false,
|
|
861
|
+
reason: `ContextManifest contractDigest '${typedManifest.contractDigest}' does not match expected digest '${expectedContract.digest}'`,
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
return { valid: true, manifest: deepFreeze(typedManifest) };
|
|
866
|
+
}
|
|
867
|
+
catch (err) {
|
|
868
|
+
return { valid: false, reason: `Malformed ContextManifest: ${err?.message ?? "unknown error"}` };
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
/**
|
|
872
|
+
* Validate a code artifact outcome when Hub receives terminal event or completion.
|
|
873
|
+
* Checks proof fields (executionCompleted, artifactProduced) and validates artifact identity against contract/manifest.
|
|
874
|
+
* Code artifact must have:
|
|
875
|
+
* - 40hex inputTree and outputTree
|
|
876
|
+
* - Complete baseline matching contract
|
|
877
|
+
* - Canonical base64 patch <= 128 KiB decoded matching patchSha256
|
|
878
|
+
* - Never throws on malformed input.
|
|
879
|
+
*/
|
|
880
|
+
export function validateCodeArtifactOutcome(args) {
|
|
881
|
+
try {
|
|
882
|
+
const isCode = args.task.executionMode === "code" || args.task.contract?.executionMode === "code";
|
|
883
|
+
if ((args.executionCompleted !== undefined && typeof args.executionCompleted !== "boolean") ||
|
|
884
|
+
(args.artifactProduced !== undefined && typeof args.artifactProduced !== "boolean"))
|
|
885
|
+
return { valid: false, reason: "executionCompleted and artifactProduced proofs must be boolean" };
|
|
886
|
+
if (args.artifactProduced === true || args.artifact !== undefined) {
|
|
887
|
+
if (args.executionCompleted !== true) {
|
|
888
|
+
return {
|
|
889
|
+
valid: false,
|
|
890
|
+
reason: "Execution proof 'executionCompleted' must be true when artifact is produced",
|
|
891
|
+
};
|
|
892
|
+
}
|
|
893
|
+
if (args.artifactProduced !== true) {
|
|
894
|
+
return {
|
|
895
|
+
valid: false,
|
|
896
|
+
reason: "Artifact proof 'artifactProduced' must be true when artifact is provided",
|
|
897
|
+
};
|
|
898
|
+
}
|
|
899
|
+
if (!args.artifact) {
|
|
900
|
+
return {
|
|
901
|
+
valid: false,
|
|
902
|
+
reason: "Artifact proof 'artifactProduced' is true but no artifact was provided",
|
|
903
|
+
};
|
|
904
|
+
}
|
|
905
|
+
const artifactRes = validateDeviceArtifact(args.artifact);
|
|
906
|
+
if (!artifactRes.valid) {
|
|
907
|
+
return { valid: false, reason: `Artifact validation failed: ${artifactRes.reason}` };
|
|
908
|
+
}
|
|
909
|
+
const art = artifactRes.artifact;
|
|
910
|
+
// Code artifact specific checks
|
|
911
|
+
if (isCode) {
|
|
912
|
+
const proofError = codeArtifactProofError(art);
|
|
913
|
+
if (proofError)
|
|
914
|
+
return { valid: false, reason: proofError };
|
|
915
|
+
if (!args.task.contract || !args.task.contextManifest)
|
|
916
|
+
return { valid: false, reason: "Code artifact requires sealed task contract and manifest" };
|
|
917
|
+
if (!art.inputTree || !TREE_HEX_REGEX.test(art.inputTree)) {
|
|
918
|
+
return { valid: false, reason: "Code artifact requires 40-character hex inputTree" };
|
|
919
|
+
}
|
|
920
|
+
if (!art.outputTree || !TREE_HEX_REGEX.test(art.outputTree)) {
|
|
921
|
+
return { valid: false, reason: "Code artifact requires 40-character hex outputTree" };
|
|
922
|
+
}
|
|
923
|
+
if (!art.baseline) {
|
|
924
|
+
return { valid: false, reason: "Code artifact requires complete baseline" };
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
if (art.producer.taskId !== args.task.id) {
|
|
928
|
+
return {
|
|
929
|
+
valid: false,
|
|
930
|
+
reason: `Artifact producer taskId '${art.producer.taskId}' mismatch with task '${args.task.id}'`,
|
|
931
|
+
};
|
|
932
|
+
}
|
|
933
|
+
if (art.producer.attemptId !== args.task.attemptId) {
|
|
934
|
+
return {
|
|
935
|
+
valid: false,
|
|
936
|
+
reason: `Artifact producer attemptId '${art.producer.attemptId}' mismatch with task attempt '${args.task.attemptId}'`,
|
|
937
|
+
};
|
|
938
|
+
}
|
|
939
|
+
if (art.producer.deviceId !== args.task.deviceId) {
|
|
940
|
+
return {
|
|
941
|
+
valid: false,
|
|
942
|
+
reason: `Artifact producer deviceId '${art.producer.deviceId}' mismatch with task device '${args.task.deviceId}'`,
|
|
943
|
+
};
|
|
944
|
+
}
|
|
945
|
+
if (art.producer.botId !== args.task.botId) {
|
|
946
|
+
return {
|
|
947
|
+
valid: false,
|
|
948
|
+
reason: `Artifact producer botId '${art.producer.botId}' mismatch with task bot '${args.task.botId}'`,
|
|
949
|
+
};
|
|
950
|
+
}
|
|
951
|
+
if (args.task.contract) {
|
|
952
|
+
if (art.producer.contractDigest &&
|
|
953
|
+
art.producer.contractDigest.toLowerCase() !== args.task.contract.digest.toLowerCase()) {
|
|
954
|
+
return {
|
|
955
|
+
valid: false,
|
|
956
|
+
reason: `Artifact producer contractDigest '${art.producer.contractDigest}' mismatch with contract '${args.task.contract.digest}'`,
|
|
957
|
+
};
|
|
958
|
+
}
|
|
959
|
+
if (args.task.contract.baseline) {
|
|
960
|
+
if (!art.baseline) {
|
|
961
|
+
return { valid: false, reason: "Artifact baseline missing on code task with baseline contract" };
|
|
962
|
+
}
|
|
963
|
+
if (art.baseline.repositoryId !== args.task.contract.baseline.repositoryId) {
|
|
964
|
+
return {
|
|
965
|
+
valid: false,
|
|
966
|
+
reason: `Artifact baseline repositoryId '${art.baseline.repositoryId}' mismatch with contract '${args.task.contract.baseline.repositoryId}'`,
|
|
967
|
+
};
|
|
968
|
+
}
|
|
969
|
+
if (art.baseline.commit.toLowerCase() !== args.task.contract.baseline.commit.toLowerCase()) {
|
|
970
|
+
return {
|
|
971
|
+
valid: false,
|
|
972
|
+
reason: `Artifact baseline commit '${art.baseline.commit}' mismatch with contract '${args.task.contract.baseline.commit}'`,
|
|
973
|
+
};
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
if (args.task.contextManifest && art.producer.manifestDigest) {
|
|
978
|
+
if (art.producer.manifestDigest.toLowerCase() !== args.task.contextManifest.digest.toLowerCase()) {
|
|
979
|
+
return {
|
|
980
|
+
valid: false,
|
|
981
|
+
reason: `Artifact producer manifestDigest '${art.producer.manifestDigest}' mismatch with manifest '${args.task.contextManifest.digest}'`,
|
|
982
|
+
};
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
return { valid: true, artifact: art };
|
|
986
|
+
}
|
|
987
|
+
if (isCode && args.artifactProduced === false && args.artifact !== undefined) {
|
|
988
|
+
return { valid: false, reason: "Artifact provided while artifactProduced is false" };
|
|
989
|
+
}
|
|
990
|
+
return { valid: true };
|
|
991
|
+
}
|
|
992
|
+
catch (err) {
|
|
993
|
+
return { valid: false, reason: `Malformed code artifact outcome: ${err?.message ?? "unknown error"}` };
|
|
994
|
+
}
|
|
995
|
+
}
|