@nexart/ai-execution 0.6.0 → 0.8.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/dist/index.mjs CHANGED
@@ -12,7 +12,13 @@ var CerVerifyCode = {
12
12
  ATTESTATION_MISSING: "ATTESTATION_MISSING",
13
13
  ATTESTATION_KEY_NOT_FOUND: "ATTESTATION_KEY_NOT_FOUND",
14
14
  ATTESTATION_INVALID_SIGNATURE: "ATTESTATION_INVALID_SIGNATURE",
15
- ATTESTATION_KEY_FORMAT_UNSUPPORTED: "ATTESTATION_KEY_FORMAT_UNSUPPORTED"
15
+ ATTESTATION_KEY_FORMAT_UNSUPPORTED: "ATTESTATION_KEY_FORMAT_UNSUPPORTED",
16
+ // v0.7.0 — Level 4 + AIEF interop codes
17
+ CHAIN_BREAK_DETECTED: "CHAIN_BREAK_DETECTED",
18
+ INCOMPLETE_ARTIFACT: "INCOMPLETE_ARTIFACT",
19
+ VERIFICATION_MATERIAL_UNAVAILABLE: "VERIFICATION_MATERIAL_UNAVAILABLE",
20
+ TOOL_EVIDENCE_MISSING: "TOOL_EVIDENCE_MISSING",
21
+ TOOL_OUTPUT_HASH_MISMATCH: "TOOL_OUTPUT_HASH_MISMATCH"
16
22
  };
17
23
 
18
24
  // src/errors.ts
@@ -108,7 +114,7 @@ function computeOutputHash(output) {
108
114
  }
109
115
 
110
116
  // src/snapshot.ts
111
- var PACKAGE_VERSION = "0.6.0";
117
+ var PACKAGE_VERSION = "0.8.0";
112
118
  function validateParameters(params) {
113
119
  const errors = [];
114
120
  if (typeof params.temperature !== "number" || !Number.isFinite(params.temperature)) {
@@ -161,6 +167,9 @@ function createSnapshot(params) {
161
167
  if (params.workflowId !== void 0) snapshot.workflowId = params.workflowId ?? null;
162
168
  if (params.conversationId !== void 0) snapshot.conversationId = params.conversationId ?? null;
163
169
  if (params.prevStepHash !== void 0) snapshot.prevStepHash = params.prevStepHash ?? null;
170
+ if (params.toolCalls !== void 0 && params.toolCalls.length > 0) {
171
+ snapshot.toolCalls = params.toolCalls;
172
+ }
164
173
  return snapshot;
165
174
  }
166
175
  function verifySnapshot(snapshot) {
@@ -268,6 +277,9 @@ function sealCer(snapshot, options) {
268
277
  if (options?.meta) {
269
278
  bundle.meta = options.meta;
270
279
  }
280
+ if (options?.declaration) {
281
+ bundle.declaration = options.declaration;
282
+ }
271
283
  return bundle;
272
284
  }
273
285
  function verifyCer(bundle) {
@@ -2082,6 +2094,375 @@ function createClient(defaults = {}) {
2082
2094
  }
2083
2095
  };
2084
2096
  }
2097
+
2098
+ // src/aief.ts
2099
+ var AIEF_REASON_MAP = {
2100
+ [CerVerifyCode.OK]: "ok",
2101
+ [CerVerifyCode.CERTIFICATE_HASH_MISMATCH]: "integrityProofMismatch",
2102
+ [CerVerifyCode.SNAPSHOT_HASH_MISMATCH]: "integrityProofMismatch",
2103
+ [CerVerifyCode.INPUT_HASH_MISMATCH]: "integrityProofMismatch",
2104
+ [CerVerifyCode.OUTPUT_HASH_MISMATCH]: "integrityProofMismatch",
2105
+ [CerVerifyCode.TOOL_OUTPUT_HASH_MISMATCH]: "integrityProofMismatch",
2106
+ [CerVerifyCode.SCHEMA_ERROR]: "unsupportedSchema",
2107
+ [CerVerifyCode.CANONICALIZATION_ERROR]: "malformedArtifact",
2108
+ [CerVerifyCode.INVALID_SHA256_FORMAT]: "malformedArtifact",
2109
+ [CerVerifyCode.UNKNOWN_ERROR]: "malformedArtifact",
2110
+ [CerVerifyCode.INCOMPLETE_ARTIFACT]: "incompleteArtifact",
2111
+ [CerVerifyCode.CHAIN_BREAK_DETECTED]: "chainBreakDetected",
2112
+ [CerVerifyCode.VERIFICATION_MATERIAL_UNAVAILABLE]: "verificationMaterialUnavailable",
2113
+ [CerVerifyCode.TOOL_EVIDENCE_MISSING]: "incompleteArtifact",
2114
+ [CerVerifyCode.ATTESTATION_INVALID_SIGNATURE]: "signatureInvalid",
2115
+ [CerVerifyCode.ATTESTATION_MISSING]: "verificationMaterialUnavailable",
2116
+ [CerVerifyCode.ATTESTATION_KEY_NOT_FOUND]: "verificationMaterialUnavailable",
2117
+ [CerVerifyCode.ATTESTATION_KEY_FORMAT_UNSUPPORTED]: "verificationMaterialUnavailable"
2118
+ };
2119
+ var INTEGRITY_FAILURE_CODES = /* @__PURE__ */ new Set([
2120
+ CerVerifyCode.CERTIFICATE_HASH_MISMATCH,
2121
+ CerVerifyCode.SNAPSHOT_HASH_MISMATCH,
2122
+ CerVerifyCode.INPUT_HASH_MISMATCH,
2123
+ CerVerifyCode.OUTPUT_HASH_MISMATCH,
2124
+ CerVerifyCode.TOOL_OUTPUT_HASH_MISMATCH
2125
+ ]);
2126
+ var SCHEMA_FAILURE_CODES = /* @__PURE__ */ new Set([
2127
+ CerVerifyCode.SCHEMA_ERROR,
2128
+ CerVerifyCode.CANONICALIZATION_ERROR,
2129
+ CerVerifyCode.INVALID_SHA256_FORMAT,
2130
+ CerVerifyCode.UNKNOWN_ERROR,
2131
+ CerVerifyCode.INCOMPLETE_ARTIFACT
2132
+ ]);
2133
+ function mapToAiefReason(code) {
2134
+ return AIEF_REASON_MAP[code] ?? "malformedArtifact";
2135
+ }
2136
+ function verifyAief(bundle) {
2137
+ const inner = verifyCer(bundle);
2138
+ if (inner.ok) {
2139
+ return {
2140
+ result: "PASS",
2141
+ reason: null,
2142
+ checks: {
2143
+ schemaSupported: true,
2144
+ integrityValid: true,
2145
+ protectedSetValid: true,
2146
+ chainValid: true
2147
+ }
2148
+ };
2149
+ }
2150
+ const aiefReason = mapToAiefReason(inner.code);
2151
+ const isIntegrityFailure = INTEGRITY_FAILURE_CODES.has(inner.code);
2152
+ const isSchemaFailure = SCHEMA_FAILURE_CODES.has(inner.code);
2153
+ const isChainFailure = inner.code === CerVerifyCode.CHAIN_BREAK_DETECTED;
2154
+ const result = {
2155
+ result: "FAIL",
2156
+ reason: aiefReason,
2157
+ checks: {
2158
+ schemaSupported: !isSchemaFailure,
2159
+ integrityValid: !isIntegrityFailure,
2160
+ protectedSetValid: !isIntegrityFailure,
2161
+ chainValid: !isChainFailure
2162
+ }
2163
+ };
2164
+ if (inner.details && inner.details.length > 0) {
2165
+ result.notes = inner.details;
2166
+ } else if (inner.errors.length > 0) {
2167
+ result.notes = inner.errors;
2168
+ }
2169
+ return result;
2170
+ }
2171
+
2172
+ // src/tools.ts
2173
+ function hashToolOutput(value) {
2174
+ if (typeof value === "string") {
2175
+ return hashUtf8(value);
2176
+ }
2177
+ return hashCanonicalJson(value);
2178
+ }
2179
+ function makeToolEvent(params) {
2180
+ const at = params.at ?? (/* @__PURE__ */ new Date()).toISOString();
2181
+ const outputHash = hashToolOutput(params.output);
2182
+ const event = {
2183
+ toolId: params.toolId,
2184
+ at,
2185
+ outputHash
2186
+ };
2187
+ if (params.input !== void 0) {
2188
+ event.inputHash = hashToolOutput(params.input);
2189
+ }
2190
+ if (params.evidenceRef !== void 0) {
2191
+ event.evidenceRef = params.evidenceRef;
2192
+ }
2193
+ if (params.error !== void 0) {
2194
+ event.error = params.error;
2195
+ }
2196
+ return event;
2197
+ }
2198
+
2199
+ // src/chain.ts
2200
+ function verifyRunSummary(summary, bundles, opts) {
2201
+ if (bundles.length !== summary.stepCount) {
2202
+ return {
2203
+ ok: false,
2204
+ code: CerVerifyCode.INCOMPLETE_ARTIFACT,
2205
+ errors: [
2206
+ `Step count mismatch: summary declares ${summary.stepCount} step(s), received ${bundles.length}`
2207
+ ],
2208
+ details: ["Possible step deletion or insertion"]
2209
+ };
2210
+ }
2211
+ if (bundles.length === 0) {
2212
+ return { ok: true, code: CerVerifyCode.OK, errors: [] };
2213
+ }
2214
+ let prevHash = null;
2215
+ for (let i = 0; i < bundles.length; i++) {
2216
+ const bundle = bundles[i];
2217
+ const summaryStep = summary.steps[i];
2218
+ if (!summaryStep) {
2219
+ return {
2220
+ ok: false,
2221
+ code: CerVerifyCode.INCOMPLETE_ARTIFACT,
2222
+ errors: [`Missing summary entry for step index ${i}`],
2223
+ breakAt: i
2224
+ };
2225
+ }
2226
+ if (!opts?.skipBundleVerification) {
2227
+ const bundleResult = verifyCer(bundle);
2228
+ if (!bundleResult.ok) {
2229
+ return {
2230
+ ok: false,
2231
+ code: bundleResult.code,
2232
+ errors: [`Step ${i}: individual bundle verification failed: ${bundleResult.errors.join("; ")}`],
2233
+ details: bundleResult.details,
2234
+ breakAt: i
2235
+ };
2236
+ }
2237
+ }
2238
+ const stepIndex = bundle.snapshot?.stepIndex;
2239
+ if (stepIndex !== i) {
2240
+ return {
2241
+ ok: false,
2242
+ code: CerVerifyCode.CHAIN_BREAK_DETECTED,
2243
+ errors: [`Step ${i}: expected stepIndex ${i}, got ${String(stepIndex)}`],
2244
+ details: ["stepIndex mismatch indicates insertion or reordering"],
2245
+ breakAt: i
2246
+ };
2247
+ }
2248
+ const snapshotPrevHash = bundle.snapshot?.prevStepHash ?? null;
2249
+ if (i === 0) {
2250
+ if (snapshotPrevHash !== null) {
2251
+ return {
2252
+ ok: false,
2253
+ code: CerVerifyCode.CHAIN_BREAK_DETECTED,
2254
+ errors: [`Step 0: expected prevStepHash null, got "${snapshotPrevHash}"`],
2255
+ details: ["First step must have prevStepHash null"],
2256
+ breakAt: 0,
2257
+ expectedPrev: "null",
2258
+ observedPrev: snapshotPrevHash
2259
+ };
2260
+ }
2261
+ } else {
2262
+ if (snapshotPrevHash !== prevHash) {
2263
+ return {
2264
+ ok: false,
2265
+ code: CerVerifyCode.CHAIN_BREAK_DETECTED,
2266
+ errors: [`Step ${i}: prevStepHash mismatch \u2014 chain break detected`],
2267
+ details: [
2268
+ `Expected prevStepHash "${prevHash ?? "null"}", got "${snapshotPrevHash ?? "null"}"`
2269
+ ],
2270
+ breakAt: i,
2271
+ expectedPrev: prevHash ?? void 0,
2272
+ observedPrev: snapshotPrevHash ?? void 0
2273
+ };
2274
+ }
2275
+ }
2276
+ if (bundle.certificateHash !== summaryStep.certificateHash) {
2277
+ return {
2278
+ ok: false,
2279
+ code: CerVerifyCode.CHAIN_BREAK_DETECTED,
2280
+ errors: [`Step ${i}: certificateHash does not match summary record`],
2281
+ details: [
2282
+ `Summary has "${summaryStep.certificateHash}", bundle has "${bundle.certificateHash}"`
2283
+ ],
2284
+ breakAt: i
2285
+ };
2286
+ }
2287
+ prevHash = bundle.certificateHash;
2288
+ }
2289
+ const lastBundle = bundles[bundles.length - 1];
2290
+ if (summary.finalStepHash !== lastBundle.certificateHash) {
2291
+ return {
2292
+ ok: false,
2293
+ code: CerVerifyCode.CHAIN_BREAK_DETECTED,
2294
+ errors: ["summary.finalStepHash does not match last step certificateHash"],
2295
+ details: [
2296
+ `Expected "${summary.finalStepHash ?? "null"}", got "${lastBundle.certificateHash}"`
2297
+ ],
2298
+ breakAt: bundles.length - 1,
2299
+ expectedPrev: summary.finalStepHash ?? void 0,
2300
+ observedPrev: lastBundle.certificateHash
2301
+ };
2302
+ }
2303
+ return { ok: true, code: CerVerifyCode.OK, errors: [] };
2304
+ }
2305
+
2306
+ // src/redact.ts
2307
+ function hashValue(value) {
2308
+ if (typeof value === "string") return hashUtf8(value);
2309
+ return hashCanonicalJson(value);
2310
+ }
2311
+ function getNestedValue(obj, path) {
2312
+ const parts = path.split(".");
2313
+ let cursor = obj;
2314
+ for (const part of parts) {
2315
+ if (typeof cursor !== "object" || cursor === null) return void 0;
2316
+ cursor = cursor[part];
2317
+ }
2318
+ return cursor;
2319
+ }
2320
+ function setNestedPath(obj, path, value) {
2321
+ const parts = path.split(".");
2322
+ const clone = { ...obj };
2323
+ let cursor = clone;
2324
+ for (let i = 0; i < parts.length - 1; i++) {
2325
+ const key = parts[i];
2326
+ const child = cursor[key];
2327
+ if (typeof child !== "object" || child === null) return clone;
2328
+ cursor[key] = { ...child };
2329
+ cursor = cursor[key];
2330
+ }
2331
+ const last = parts[parts.length - 1];
2332
+ if (last in cursor) {
2333
+ cursor[last] = value;
2334
+ }
2335
+ return clone;
2336
+ }
2337
+ function redactBeforeSeal(snapshot, policy) {
2338
+ let result = { ...snapshot };
2339
+ for (const path of policy.paths) {
2340
+ const original = getNestedValue(result, path);
2341
+ if (original === void 0) continue;
2342
+ const envelope = {
2343
+ _redacted: true,
2344
+ hash: hashValue(original)
2345
+ };
2346
+ result = setNestedPath(result, path, envelope);
2347
+ if (path === "input") {
2348
+ result["inputHash"] = computeInputHash(envelope);
2349
+ } else if (path === "output") {
2350
+ result["outputHash"] = computeOutputHash(envelope);
2351
+ }
2352
+ }
2353
+ return result;
2354
+ }
2355
+
2356
+ // src/profile.ts
2357
+ var SHA256_PATTERN2 = /^sha256:[0-9a-f]{64}$/;
2358
+ function validateToolEvent(event, index) {
2359
+ const errs = [];
2360
+ if (!event.toolId || typeof event.toolId !== "string") {
2361
+ errs.push(`toolCalls[${index}].toolId must be a non-empty string`);
2362
+ }
2363
+ if (!event.at || typeof event.at !== "string") {
2364
+ errs.push(`toolCalls[${index}].at must be a non-empty ISO 8601 string`);
2365
+ }
2366
+ if (!event.outputHash || !SHA256_PATTERN2.test(event.outputHash)) {
2367
+ errs.push(`toolCalls[${index}].outputHash must be in sha256:<64hex> format, got "${event.outputHash ?? ""}"`);
2368
+ }
2369
+ if (event.inputHash !== void 0 && !SHA256_PATTERN2.test(event.inputHash)) {
2370
+ errs.push(`toolCalls[${index}].inputHash must be in sha256:<64hex> format when present`);
2371
+ }
2372
+ return errs;
2373
+ }
2374
+ function validateL2(snapshot, label) {
2375
+ const errs = [];
2376
+ if (!snapshot.executionId) errs.push(`executionId is required for ${label}`);
2377
+ if (!snapshot.timestamp) errs.push(`timestamp is required for ${label}`);
2378
+ if (!snapshot.provider) errs.push(`provider is required for ${label}`);
2379
+ if (!snapshot.model) errs.push(`model is required for ${label}`);
2380
+ if (snapshot.input === void 0 || snapshot.input === null) errs.push(`input is required for ${label}`);
2381
+ if (snapshot.output === void 0 || snapshot.output === null) errs.push(`output is required for ${label}`);
2382
+ if (!snapshot.inputHash) errs.push(`inputHash is required for ${label}`);
2383
+ if (!snapshot.outputHash) errs.push(`outputHash is required for ${label}`);
2384
+ return errs;
2385
+ }
2386
+ function resolveSnapshot(target) {
2387
+ if ("snapshot" in target && target.snapshot) {
2388
+ return target.snapshot;
2389
+ }
2390
+ return target;
2391
+ }
2392
+ function validateProfile(target, profile) {
2393
+ if (profile === "flexible") {
2394
+ return { ok: true, errors: [] };
2395
+ }
2396
+ const snapshot = resolveSnapshot(target);
2397
+ const errors = [];
2398
+ if (profile === "AIEF_L2" || profile === "AIEF_L3" || profile === "AIEF_L4") {
2399
+ errors.push(...validateL2(snapshot, profile));
2400
+ }
2401
+ if (profile === "AIEF_L4") {
2402
+ if (snapshot.toolCalls && snapshot.toolCalls.length > 0) {
2403
+ for (let i = 0; i < snapshot.toolCalls.length; i++) {
2404
+ errors.push(...validateToolEvent(snapshot.toolCalls[i], i));
2405
+ }
2406
+ }
2407
+ if (snapshot.stepIndex !== null && snapshot.stepIndex !== void 0 && snapshot.stepIndex > 0 && !snapshot.prevStepHash) {
2408
+ errors.push("prevStepHash is required when stepIndex > 0 in AIEF_L4 profile");
2409
+ }
2410
+ }
2411
+ return { ok: errors.length === 0, errors };
2412
+ }
2413
+
2414
+ // src/exportRedacted.ts
2415
+ function exportVerifiableRedacted(bundle, policy, options) {
2416
+ const createdAt = options?.createdAt ?? (/* @__PURE__ */ new Date()).toISOString();
2417
+ const redactedSnapshot = redactBeforeSeal(bundle.snapshot, policy);
2418
+ const provenance = {
2419
+ originalCertificateHash: bundle.certificateHash,
2420
+ redactionPolicy: { paths: [...policy.paths] },
2421
+ redactedAt: createdAt
2422
+ };
2423
+ const mergedMeta = {
2424
+ ...bundle.meta,
2425
+ provenance
2426
+ };
2427
+ const newBundle = sealCer(redactedSnapshot, {
2428
+ createdAt,
2429
+ meta: mergedMeta,
2430
+ declaration: bundle.declaration
2431
+ });
2432
+ return {
2433
+ bundle: newBundle,
2434
+ originalCertificateHash: bundle.certificateHash
2435
+ };
2436
+ }
2437
+
2438
+ // src/runHelper.ts
2439
+ async function certifyAndAttestRun(steps, options) {
2440
+ const run = new RunBuilder({
2441
+ runId: options?.runId,
2442
+ workflowId: options?.workflowId,
2443
+ conversationId: options?.conversationId,
2444
+ appId: options?.appId
2445
+ });
2446
+ const stepBundles = [];
2447
+ const receipts = [];
2448
+ for (const stepParams of steps) {
2449
+ const bundle = run.step(stepParams);
2450
+ stepBundles.push(bundle);
2451
+ if (options?.attestStep) {
2452
+ const receipt = await options.attestStep(bundle);
2453
+ receipts.push(receipt);
2454
+ } else {
2455
+ receipts.push(null);
2456
+ }
2457
+ }
2458
+ const runSummary = run.finalize();
2459
+ return {
2460
+ runSummary,
2461
+ stepBundles,
2462
+ receipts,
2463
+ finalStepHash: runSummary.finalStepHash
2464
+ };
2465
+ }
2085
2466
  export {
2086
2467
  CerAttestationError,
2087
2468
  CerVerificationError,
@@ -2090,6 +2471,7 @@ export {
2090
2471
  attest,
2091
2472
  attestIfNeeded,
2092
2473
  certifyAndAttestDecision,
2474
+ certifyAndAttestRun,
2093
2475
  certifyDecision,
2094
2476
  certifyDecisionFromProviderCall,
2095
2477
  computeInputHash,
@@ -2097,12 +2479,17 @@ export {
2097
2479
  createClient,
2098
2480
  createSnapshot,
2099
2481
  exportCer,
2482
+ exportVerifiableRedacted,
2100
2483
  fetchNodeKeys,
2101
2484
  getAttestationReceipt,
2102
2485
  hasAttestation,
2103
2486
  hashCanonicalJson,
2487
+ hashToolOutput,
2104
2488
  hashUtf8,
2105
2489
  importCer,
2490
+ makeToolEvent,
2491
+ mapToAiefReason,
2492
+ redactBeforeSeal,
2106
2493
  sanitizeForAttestation,
2107
2494
  sanitizeForStamp,
2108
2495
  sanitizeForStorage,
@@ -2110,10 +2497,13 @@ export {
2110
2497
  selectNodeKey,
2111
2498
  sha256Hex,
2112
2499
  toCanonicalJson,
2500
+ validateProfile,
2113
2501
  verifyCer as verify,
2502
+ verifyAief,
2114
2503
  verifyBundleAttestation,
2115
2504
  verifyCer,
2116
2505
  verifyNodeReceiptSignature,
2506
+ verifyRunSummary,
2117
2507
  verifySnapshot,
2118
2508
  wrapProvider
2119
2509
  };