xytara 3.1.7 → 3.1.9

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.
Files changed (47) hide show
  1. package/README.md +1230 -1228
  2. package/RELEASE_NOTES.md +6 -8
  3. package/examples/generated.carried-handoff.json +3 -3
  4. package/lib/a2a_rpc.js +280 -0
  5. package/lib/account_auth.js +585 -518
  6. package/lib/agent_identity.js +162 -0
  7. package/lib/bolt_adapter.js +187 -0
  8. package/lib/bolt_payment_front.js +224 -172
  9. package/lib/checkout_processor_adapter.js +217 -0
  10. package/lib/command_flow.js +561 -411
  11. package/lib/commerce_artifacts.js +530 -503
  12. package/lib/commerce_bridge.js +274 -0
  13. package/lib/commerce_cases.js +399 -317
  14. package/lib/commerce_client.js +7694 -7687
  15. package/lib/commerce_runtime.js +322 -315
  16. package/lib/execution_dedupe.js +86 -0
  17. package/lib/external_credit_bridge.js +142 -142
  18. package/lib/external_execution_runtime.js +82 -34
  19. package/lib/http_transport.js +81 -73
  20. package/lib/l402_adapter.js +241 -0
  21. package/lib/l402_payment_front.js +265 -192
  22. package/lib/mpp_adapter.js +237 -0
  23. package/lib/payment_verification.js +52 -9
  24. package/lib/provider_network_contract.js +143 -0
  25. package/lib/provider_payout_execution.js +137 -0
  26. package/lib/provider_registry.js +666 -0
  27. package/lib/rate_limiter.js +81 -0
  28. package/lib/runtime_state_store.js +369 -354
  29. package/lib/stripe_mpp_payment_front.js +258 -158
  30. package/package.json +18 -3
  31. package/scripts/verify_a2a_rpc.js +105 -0
  32. package/scripts/verify_agent_identity.js +153 -0
  33. package/scripts/verify_bolt_adapter.js +128 -0
  34. package/scripts/verify_checkout_processor.js +109 -0
  35. package/scripts/verify_commerce_bridge.js +177 -0
  36. package/scripts/verify_execution_dedupe.js +189 -0
  37. package/scripts/verify_execution_surfacing.js +76 -0
  38. package/scripts/verify_external_execution_retry.js +113 -0
  39. package/scripts/verify_l402_adapter.js +86 -0
  40. package/scripts/verify_mpp_adapter.js +128 -0
  41. package/scripts/verify_provider_network.js +567 -0
  42. package/scripts/verify_rate_limiter.js +52 -0
  43. package/scripts/verify_refund_credit.js +106 -0
  44. package/scripts/verify_service.js +15421 -15410
  45. package/scripts/verify_settlement.js +116 -0
  46. package/scripts/verify_verify_execute_settle.js +208 -0
  47. package/server.js +684 -16
@@ -1,503 +1,530 @@
1
- "use strict";
2
-
3
- const crypto = require("crypto");
4
- const { findTask } = require("../fixtures/catalog");
5
- const { buildDefaultEconomicPath, buildTreasuryDestination } = require("./commerce_quotes");
6
- const {
7
- buildBsvTeranodeSettlementHints,
8
- extractBsvTeranodePaymentReference,
9
- isBsvTeranodeSettlementMode
10
- } = require("./settlement_bsv_teranode");
11
-
12
- function classifyObservedLatency(durationMs) {
13
- if (durationMs <= 1000) return "instant";
14
- if (durationMs <= 15000) return "near_real_time";
15
- return "governed_async";
16
- }
17
-
18
- function assessLatency(declaredLatencyClass, observedLatencyClass) {
19
- const order = {
20
- instant: 1,
21
- near_real_time: 2,
22
- governed_async: 3
23
- };
24
- const declared = order[declaredLatencyClass] || 99;
25
- const observed = order[observedLatencyClass] || 99;
26
- return observed <= declared ? "within_posture" : "outside_posture";
27
- }
28
-
29
- function createCommandArtifacts(payload, quote, options) {
30
- const opts = options || {};
31
- const transactionId = `txn_${crypto.randomBytes(6).toString("hex")}`;
32
- const receiptId = `rcpt_${crypto.randomBytes(6).toString("hex")}`;
33
- const startedAt = new Date();
34
- const durationMs = 250;
35
- const completedAt = new Date(startedAt.getTime() + durationMs);
36
- const requestedTasks = Array.isArray(payload.tasks) && payload.tasks.length > 0
37
- ? payload.tasks.map((task, index) => ({
38
- task_id: task && task.task_id ? String(task.task_id).trim() : `task_${index + 1}`,
39
- task_ref: task && task.task_ref ? String(task.task_ref).trim() : ""
40
- }))
41
- : [{
42
- task_id: "task_1",
43
- task_ref: String(payload.task_ref || "").trim()
44
- }];
45
- const resolvedTasks = requestedTasks
46
- .map((task) => {
47
- const catalogTask = findTask(task.task_ref);
48
- return catalogTask ? { ...task, catalogTask } : null;
49
- })
50
- .filter(Boolean);
51
- const primaryTask = resolvedTasks[0] && resolvedTasks[0].catalogTask ? resolvedTasks[0].catalogTask : null;
52
- const declaredLatencyClass = (primaryTask && primaryTask.latency_class) || "near_real_time";
53
- const observedLatencyClass = classifyObservedLatency(durationMs);
54
- const deferredCompletion = opts.deferredCompletion === true;
55
- const timing = {
56
- declared_latency_class: declaredLatencyClass,
57
- started_at_iso: startedAt.toISOString(),
58
- completed_at_iso: deferredCompletion ? null : completedAt.toISOString(),
59
- duration_ms: deferredCompletion ? null : durationMs,
60
- observed_latency_class: deferredCompletion ? null : observedLatencyClass,
61
- assessment: deferredCompletion ? "pending" : assessLatency(declaredLatencyClass, observedLatencyClass)
62
- };
63
- const resultTasks = resolvedTasks.length > 0 ? resolvedTasks : requestedTasks.map((task) => ({
64
- ...task,
65
- catalogTask: null
66
- }));
67
- const normalizedBody = payload && payload.body && typeof payload.body === "object" && !Array.isArray(payload.body)
68
- ? payload.body
69
- : {};
70
- const externalExecution = opts.externalExecution && typeof opts.externalExecution === "object"
71
- ? opts.externalExecution
72
- : null;
73
- const externalResponse = externalExecution && externalExecution.target_response && typeof externalExecution.target_response === "object"
74
- ? externalExecution.target_response
75
- : null;
76
- const resultsByTaskId = Object.fromEntries(resultTasks.map((task, index) => [
77
- task.task_id,
78
- (() => {
79
- const base = {
80
- task_ref: task.task_ref,
81
- status: "completed",
82
- output_ref: `output_${transactionId}_${index + 1}`
83
- };
84
- if (task.task_ref === "admission.preview") {
85
- base.preview_kind = "admission";
86
- base.admission_ref = `admission_${transactionId}_${index + 1}`;
87
- }
88
- if (task.task_ref === "a2a.negotiate") {
89
- base.negotiation_ref = `a2a_negotiation_${transactionId}_${index + 1}`;
90
- base.interaction_front = "a2a";
91
- }
92
- if (task.task_ref === "a2a.commit") {
93
- base.negotiation_ref = `a2a_negotiation_${transactionId}_${index + 1}`;
94
- base.commitment_ref = `a2a_commitment_${transactionId}_${index + 1}`;
95
- base.interaction_front = "a2a";
96
- }
97
- if (task.task_ref === "a2c.session.open") {
98
- base.session_ref = `a2c_session_${transactionId}_${index + 1}`;
99
- base.interaction_front = "a2c";
100
- }
101
- if (task.task_ref === "a2c.pack.preview") {
102
- base.preview_kind = "a2c_pack";
103
- base.pack_preview_ref = `a2c_pack_preview_${transactionId}_${index + 1}`;
104
- base.interaction_front = "a2c";
105
- }
106
- if (task.task_ref === "trust.handoff.emit") {
107
- base.handoff_kind = "trust";
108
- base.handoff_action = "emit";
109
- base.handoff_ref = `trust_handoff_${transactionId}_${index + 1}`;
110
- }
111
- if (task.task_ref === "trust.handoff.validate") {
112
- base.handoff_kind = "trust";
113
- base.handoff_action = "validate";
114
- base.handoff_ref = `trust_handoff_${transactionId}_${index + 1}`;
115
- }
116
- if (task.task_ref === "execution_receipt.handoff.emit") {
117
- base.handoff_kind = "execution_receipt";
118
- base.handoff_action = "emit";
119
- base.handoff_ref = `execution_receipt_handoff_${transactionId}_${index + 1}`;
120
- }
121
- if (task.task_ref === "execution_receipt.handoff.validate") {
122
- base.handoff_kind = "execution_receipt";
123
- base.handoff_action = "validate";
124
- base.handoff_ref = `execution_receipt_handoff_${transactionId}_${index + 1}`;
125
- }
126
- if (task.task_ref === "receipt_ledger.handoff.emit") {
127
- base.handoff_kind = "receipt_ledger";
128
- base.handoff_action = "emit";
129
- base.handoff_ref = `receipt_ledger_handoff_${transactionId}_${index + 1}`;
130
- }
131
- if (task.task_ref === "receipt_ledger.handoff.validate") {
132
- base.handoff_kind = "receipt_ledger";
133
- base.handoff_action = "validate";
134
- base.handoff_ref = `receipt_ledger_handoff_${transactionId}_${index + 1}`;
135
- }
136
- if (task.task_ref === "runtime.emit") {
137
- base.runtime_ref = `runtime_event_${transactionId}_${index + 1}`;
138
- base.runtime_action = "emit";
139
- }
140
- if (task.task_ref === "runtime.anchor") {
141
- base.anchor_ref = `anchor_${transactionId}_${index + 1}`;
142
- base.runtime_action = "anchor";
143
- }
144
- if (task.task_ref === "runtime.emit_anchor") {
145
- base.runtime_ref = `runtime_event_${transactionId}_${index + 1}`;
146
- base.anchor_ref = `anchor_${transactionId}_${index + 1}`;
147
- base.runtime_action = "emit_anchor";
148
- }
149
- if (task.task_ref === "identity.attest.preview") {
150
- base.preview_kind = "identity_attestation";
151
- base.identity_attestation_ref = `identity_attestation_${transactionId}_${index + 1}`;
152
- }
153
- if (task.task_ref === "identity.registry.link") {
154
- base.identity_link_ref = `identity_link_${transactionId}_${index + 1}`;
155
- base.registry_record_ref = `registry_${transactionId}_${index + 1}`;
156
- }
157
- if (task.task_ref === "registry.register") {
158
- base.registry_record_ref = `registry_${transactionId}_${index + 1}`;
159
- }
160
- if (task.task_ref === "registry.pack.bundle") {
161
- base.registry_pack_ref = `registry_pack_${transactionId}_${index + 1}`;
162
- }
163
- if (task.task_ref === "anchoring.submit") {
164
- base.anchor_ref = `anchor_${transactionId}_${index + 1}`;
165
- }
166
- if (task.task_ref === "anchoring.pack.preview") {
167
- base.preview_kind = "anchoring_pack";
168
- base.anchor_preview_ref = `anchor_preview_${transactionId}_${index + 1}`;
169
- }
170
- if (task.task_ref === "adapter.mcp.invoke") {
171
- base.adapter_lane = "mcp";
172
- base.tool_name = normalizedBody.tool_name || null;
173
- base.invocation_status = externalExecution ? "provider_completed" : "simulated";
174
- }
175
- if (externalExecution) {
176
- base.execution_provider = externalExecution.provider || null;
177
- base.execution_target_id = externalExecution.target_id || null;
178
- if (externalResponse && externalResponse.provider_job_id) base.provider_job_id = externalResponse.provider_job_id;
179
- if (externalResponse && externalResponse.output_payload) base.output_payload = externalResponse.output_payload;
180
- if (externalResponse && externalResponse.output_ref) base.output_ref = externalResponse.output_ref;
181
- base.provider_status = externalResponse && externalResponse.provider_status
182
- ? externalResponse.provider_status
183
- : "completed";
184
- }
185
- return base;
186
- })()
187
- ]));
188
- const capabilityScopes = Array.from(new Set(resultTasks.flatMap((task) =>
189
- Array.isArray(task.catalogTask && task.catalogTask.capabilities) ? task.catalogTask.capabilities : []
190
- )));
191
- const adaptersUsed = Array.from(new Set(resultTasks.flatMap((task) =>
192
- Array.isArray(task.catalogTask && task.catalogTask.adapters) ? task.catalogTask.adapters : []
193
- )));
194
- const remediation = {
195
- status: "none",
196
- reason_code: null,
197
- active_case_type: null,
198
- active_case_id: null,
199
- compensating_action_status: null,
200
- updated_at_iso: completedAt.toISOString()
201
- };
202
- const payment = {
203
- payment_id: `pay_${quote.quote_id}`,
204
- scheme: quote.payment_scheme || "native",
205
- protocol: quote.payment_protocol || null,
206
- status: deferredCompletion ? "authorized" : "recorded",
207
- amount_minor: quote.amount_minor,
208
- currency: quote.currency,
209
- quote_id: quote.quote_id,
210
- wallet_id: opts.paymentVerification && opts.paymentVerification.wallet_id
211
- ? opts.paymentVerification.wallet_id
212
- : null,
213
- verification_mode: opts.paymentVerification && opts.paymentVerification.verification_mode
214
- ? opts.paymentVerification.verification_mode
215
- : null,
216
- recorded_at_iso: deferredCompletion ? null : completedAt.toISOString()
217
- };
218
- const bsvHints = buildBsvTeranodeSettlementHints({
219
- settlementMode: quote.settlement_mode,
220
- amountMinor: quote.amount_minor,
221
- currency: quote.currency,
222
- settlementBindingHash: quote.settlement_binding_hash,
223
- treasuryDestination: quote.treasury
224
- });
225
- const bsvPaymentReference = extractBsvTeranodePaymentReference(
226
- opts.paymentVerification && opts.paymentVerification.payment_payload
227
- ? opts.paymentVerification.payment_payload
228
- : null
229
- );
230
- const settlement = {
231
- settlement_id: `settlement_${transactionId}`,
232
- mode: quote.settlement_mode || null,
233
- preference: quote.settlement_preference || null,
234
- adapter: quote.settlement_adapter || null,
235
- integration_id: quote.settlement_integration_id || null,
236
- binding_hash: quote.settlement_binding_hash || null,
237
- rail: isBsvTeranodeSettlementMode(quote.settlement_mode) ? "bsv_teranode" : null,
238
- network: bsvHints ? bsvHints.network : null,
239
- hints: bsvHints,
240
- txid: bsvPaymentReference && bsvPaymentReference.txid ? bsvPaymentReference.txid : null,
241
- raw_tx: bsvPaymentReference && (bsvPaymentReference.raw_tx || bsvPaymentReference.rawTx)
242
- ? (bsvPaymentReference.raw_tx || bsvPaymentReference.rawTx)
243
- : null,
244
- status: deferredCompletion ? "pending_completion" : "recorded",
245
- recorded_at_iso: deferredCompletion ? null : completedAt.toISOString()
246
- };
247
- const treasuryDestination = buildTreasuryDestination(quote.account_id, quote.settlement_mode);
248
- const treasury = {
249
- ...treasuryDestination,
250
- treasury_receipt_id: `treasury_${receiptId}`,
251
- status: deferredCompletion ? "pending_settlement" : "recorded",
252
- amount_minor: quote.amount_minor,
253
- currency: quote.currency,
254
- payment_id: payment.payment_id,
255
- settlement_id: settlement.settlement_id,
256
- custody_external_ref: treasuryDestination.external_ref || null,
257
- rail: treasuryDestination.rail || settlement.rail || null,
258
- recorded_at_iso: deferredCompletion ? null : completedAt.toISOString()
259
- };
260
- const defaultPath = quote.default_path || buildDefaultEconomicPath(
261
- Array.isArray(quote.integration_ids)
262
- ? quote.integration_ids.find((integrationId) => typeof integrationId === "string" && integrationId.startsWith("builtin.payment."))
263
- : null,
264
- quote.settlement_integration_id || null,
265
- treasury,
266
- {
267
- paymentProtocol: quote.payment_protocol || null,
268
- settlementMode: quote.settlement_mode || null,
269
- status: deferredCompletion ? "pending_completion" : "recorded"
270
- }
271
- );
272
- const transaction = {
273
- transaction_id: transactionId,
274
- account_id: quote.account_id,
275
- status: deferredCompletion ? "accepted" : "completed",
276
- intent: payload.command || "command.execute",
277
- amount_minor: quote.amount_minor,
278
- currency: quote.currency,
279
- quote_id: quote.quote_id,
280
- integration_ids: Array.isArray(quote.integration_ids) ? quote.integration_ids : [],
281
- protocols: Array.isArray(quote.protocols) ? quote.protocols : [],
282
- output_ref: externalResponse && externalResponse.output_ref ? externalResponse.output_ref : `output_${transactionId}`,
283
- callback_url: payload.callback_url || null,
284
- capabilities: quote.capabilities,
285
- adapters: quote.adapters,
286
- payment,
287
- settlement,
288
- treasury,
289
- default_path: {
290
- ...defaultPath,
291
- status: deferredCompletion ? "pending_completion" : "recorded"
292
- },
293
- execution: {
294
- status: deferredCompletion ? "accepted" : "completed",
295
- task_count: resultTasks.length,
296
- capability_scopes: capabilityScopes,
297
- adapters_used: adaptersUsed,
298
- integration_ids: Array.isArray(quote.execution_integration_ids) ? quote.execution_integration_ids : [],
299
- results_by_task_id: resultsByTaskId,
300
- provider: externalExecution ? externalExecution.provider || null : null,
301
- provider_target_id: externalExecution ? externalExecution.target_id || null : null,
302
- provider_job_id: externalResponse && externalResponse.provider_job_id ? externalResponse.provider_job_id : null
303
- },
304
- created_at_iso: startedAt.toISOString(),
305
- completed_at_iso: deferredCompletion ? null : completedAt.toISOString(),
306
- timing,
307
- remediation
308
- };
309
- const receipt = {
310
- receipt_id: receiptId,
311
- transaction_id: transactionId,
312
- account_id: quote.account_id,
313
- status: deferredCompletion ? "pending" : "completed",
314
- amount_minor: quote.amount_minor,
315
- currency: quote.currency,
316
- quote_id: quote.quote_id,
317
- integration_ids: Array.isArray(quote.integration_ids) ? quote.integration_ids : [],
318
- settlement_integration_id: quote.settlement_integration_id || null,
319
- payment,
320
- settlement,
321
- treasury,
322
- default_path: {
323
- ...defaultPath,
324
- status: deferredCompletion ? "pending_completion" : "recorded"
325
- },
326
- proof_ref: externalResponse && externalResponse.proof_ref ? externalResponse.proof_ref : `proof_${receiptId}`,
327
- execution_summary: {
328
- command: payload.command || "command.execute",
329
- task_count: resultTasks.length,
330
- tasks: resultTasks.map((task) => ({
331
- task_id: task.task_id,
332
- task_ref: task.task_ref
333
- }))
334
- },
335
- created_at_iso: deferredCompletion ? startedAt.toISOString() : completedAt.toISOString(),
336
- timing,
337
- remediation
338
- };
339
- const command = {
340
- command: payload.command || "command.execute",
341
- integration_ids: Array.isArray(quote.integration_ids) ? quote.integration_ids : [],
342
- payment,
343
- settlement,
344
- treasury,
345
- default_path: {
346
- ...defaultPath,
347
- status: deferredCompletion ? "pending_completion" : "recorded"
348
- },
349
- deliverables: resultTasks.map((task, index) => ({
350
- kind: task.task_ref === "admission.preview" ? "preview" : "result",
351
- ...(task.task_ref === "a2c.pack.preview"
352
- ? {
353
- kind: "preview"
354
- }
355
- : {}),
356
- task_id: task.task_id,
357
- task_ref: task.task_ref,
358
- ref: `deliverable_${transactionId}_${index + 1}`,
359
- ...(task.task_ref === "admission.preview"
360
- ? {
361
- admission_ref: `admission_${transactionId}_${index + 1}`
362
- }
363
- : {}),
364
- ...(task.task_ref === "identity.attest.preview"
365
- ? {
366
- kind: "preview",
367
- identity_attestation_ref: `identity_attestation_${transactionId}_${index + 1}`
368
- }
369
- : {}),
370
- ...(task.task_ref === "identity.registry.link"
371
- ? {
372
- identity_link_ref: `identity_link_${transactionId}_${index + 1}`,
373
- registry_record_ref: `registry_${transactionId}_${index + 1}`
374
- }
375
- : {}),
376
- ...(task.task_ref === "a2a.negotiate"
377
- ? {
378
- negotiation_ref: `a2a_negotiation_${transactionId}_${index + 1}`,
379
- interaction_front: "a2a"
380
- }
381
- : {}),
382
- ...(task.task_ref === "a2a.commit"
383
- ? {
384
- negotiation_ref: `a2a_negotiation_${transactionId}_${index + 1}`,
385
- commitment_ref: `a2a_commitment_${transactionId}_${index + 1}`,
386
- interaction_front: "a2a"
387
- }
388
- : {}),
389
- ...(task.task_ref === "a2c.session.open"
390
- ? {
391
- session_ref: `a2c_session_${transactionId}_${index + 1}`,
392
- interaction_front: "a2c"
393
- }
394
- : {}),
395
- ...(task.task_ref === "a2c.pack.preview"
396
- ? {
397
- pack_preview_ref: `a2c_pack_preview_${transactionId}_${index + 1}`,
398
- interaction_front: "a2c"
399
- }
400
- : {}),
401
- ...(task.task_ref === "trust.handoff.emit"
402
- ? {
403
- handoff_kind: "trust",
404
- handoff_action: "emit",
405
- handoff_ref: `trust_handoff_${transactionId}_${index + 1}`
406
- }
407
- : {}),
408
- ...(task.task_ref === "trust.handoff.validate"
409
- ? {
410
- handoff_kind: "trust",
411
- handoff_action: "validate",
412
- handoff_ref: `trust_handoff_${transactionId}_${index + 1}`
413
- }
414
- : {}),
415
- ...(task.task_ref === "execution_receipt.handoff.emit"
416
- ? {
417
- handoff_kind: "execution_receipt",
418
- handoff_action: "emit",
419
- handoff_ref: `execution_receipt_handoff_${transactionId}_${index + 1}`
420
- }
421
- : {}),
422
- ...(task.task_ref === "execution_receipt.handoff.validate"
423
- ? {
424
- handoff_kind: "execution_receipt",
425
- handoff_action: "validate",
426
- handoff_ref: `execution_receipt_handoff_${transactionId}_${index + 1}`
427
- }
428
- : {}),
429
- ...(task.task_ref === "receipt_ledger.handoff.emit"
430
- ? {
431
- handoff_kind: "receipt_ledger",
432
- handoff_action: "emit",
433
- handoff_ref: `receipt_ledger_handoff_${transactionId}_${index + 1}`
434
- }
435
- : {}),
436
- ...(task.task_ref === "receipt_ledger.handoff.validate"
437
- ? {
438
- handoff_kind: "receipt_ledger",
439
- handoff_action: "validate",
440
- handoff_ref: `receipt_ledger_handoff_${transactionId}_${index + 1}`
441
- }
442
- : {}),
443
- ...(task.task_ref === "runtime.emit"
444
- ? {
445
- runtime_ref: `runtime_event_${transactionId}_${index + 1}`,
446
- runtime_action: "emit"
447
- }
448
- : {}),
449
- ...(task.task_ref === "runtime.anchor"
450
- ? {
451
- anchor_ref: `anchor_${transactionId}_${index + 1}`,
452
- runtime_action: "anchor"
453
- }
454
- : {}),
455
- ...(task.task_ref === "runtime.emit_anchor"
456
- ? {
457
- runtime_ref: `runtime_event_${transactionId}_${index + 1}`,
458
- anchor_ref: `anchor_${transactionId}_${index + 1}`,
459
- runtime_action: "emit_anchor"
460
- }
461
- : {}),
462
- ...(task.task_ref === "registry.register"
463
- ? {
464
- registry_record_ref: `registry_${transactionId}_${index + 1}`
465
- }
466
- : {}),
467
- ...(task.task_ref === "registry.pack.bundle"
468
- ? {
469
- registry_pack_ref: `registry_pack_${transactionId}_${index + 1}`
470
- }
471
- : {}),
472
- ...(task.task_ref === "anchoring.submit"
473
- ? {
474
- anchor_ref: `anchor_${transactionId}_${index + 1}`
475
- }
476
- : {}),
477
- ...(task.task_ref === "anchoring.pack.preview"
478
- ? {
479
- kind: "preview",
480
- anchor_preview_ref: `anchor_preview_${transactionId}_${index + 1}`
481
- }
482
- : {}),
483
- ...(task.task_ref === "adapter.mcp.invoke"
484
- ? {
485
- adapter_lane: "mcp",
486
- tool_name: normalizedBody.tool_name || null
487
- }
488
- : {})
489
- })),
490
- monitor: {
491
- transaction_id: transactionId,
492
- status_url: `/v1/governed-transactions/${transactionId}`,
493
- receipt_url: `/v1/receipts/${receiptId}`,
494
- delivery_url: `/v1/deliveries/delivery_${transactionId}`,
495
- payment_url: `/v1/payment-ledger/${payment.payment_id}`
496
- }
497
- };
498
- return { transaction, receipt, command };
499
- }
500
-
501
- module.exports = {
502
- createCommandArtifacts
503
- };
1
+ "use strict";
2
+
3
+ const crypto = require("crypto");
4
+ const { findTask } = require("../fixtures/catalog");
5
+ const { buildDefaultEconomicPath, buildTreasuryDestination } = require("./commerce_quotes");
6
+ const {
7
+ buildBsvTeranodeSettlementHints,
8
+ extractBsvTeranodePaymentReference,
9
+ isBsvTeranodeSettlementMode
10
+ } = require("./settlement_bsv_teranode");
11
+
12
+ function classifyObservedLatency(durationMs) {
13
+ if (durationMs <= 1000) return "instant";
14
+ if (durationMs <= 15000) return "near_real_time";
15
+ return "governed_async";
16
+ }
17
+
18
+ function assessLatency(declaredLatencyClass, observedLatencyClass) {
19
+ const order = {
20
+ instant: 1,
21
+ near_real_time: 2,
22
+ governed_async: 3
23
+ };
24
+ const declared = order[declaredLatencyClass] || 99;
25
+ const observed = order[observedLatencyClass] || 99;
26
+ return observed <= declared ? "within_posture" : "outside_posture";
27
+ }
28
+
29
+ function createCommandArtifacts(payload, quote, options) {
30
+ const opts = options || {};
31
+ const transactionId = `txn_${crypto.randomBytes(6).toString("hex")}`;
32
+ const receiptId = `rcpt_${crypto.randomBytes(6).toString("hex")}`;
33
+ const startedAt = new Date();
34
+ const durationMs = 250;
35
+ const completedAt = new Date(startedAt.getTime() + durationMs);
36
+ const requestedTasks = Array.isArray(payload.tasks) && payload.tasks.length > 0
37
+ ? payload.tasks.map((task, index) => ({
38
+ task_id: task && task.task_id ? String(task.task_id).trim() : `task_${index + 1}`,
39
+ task_ref: task && task.task_ref ? String(task.task_ref).trim() : ""
40
+ }))
41
+ : [{
42
+ task_id: "task_1",
43
+ task_ref: String(payload.task_ref || "").trim()
44
+ }];
45
+ const resolvedTasks = requestedTasks
46
+ .map((task) => {
47
+ const catalogTask = findTask(task.task_ref);
48
+ return catalogTask ? { ...task, catalogTask } : null;
49
+ })
50
+ .filter(Boolean);
51
+ const primaryTask = resolvedTasks[0] && resolvedTasks[0].catalogTask ? resolvedTasks[0].catalogTask : null;
52
+ const declaredLatencyClass = (primaryTask && primaryTask.latency_class) || "near_real_time";
53
+ const observedLatencyClass = classifyObservedLatency(durationMs);
54
+ const deferredCompletion = opts.deferredCompletion === true;
55
+ const timing = {
56
+ declared_latency_class: declaredLatencyClass,
57
+ started_at_iso: startedAt.toISOString(),
58
+ completed_at_iso: deferredCompletion ? null : completedAt.toISOString(),
59
+ duration_ms: deferredCompletion ? null : durationMs,
60
+ observed_latency_class: deferredCompletion ? null : observedLatencyClass,
61
+ assessment: deferredCompletion ? "pending" : assessLatency(declaredLatencyClass, observedLatencyClass)
62
+ };
63
+ const resultTasks = resolvedTasks.length > 0 ? resolvedTasks : requestedTasks.map((task) => ({
64
+ ...task,
65
+ catalogTask: null
66
+ }));
67
+ const normalizedBody = payload && payload.body && typeof payload.body === "object" && !Array.isArray(payload.body)
68
+ ? payload.body
69
+ : {};
70
+ const externalExecution = opts.externalExecution && typeof opts.externalExecution === "object"
71
+ ? opts.externalExecution
72
+ : null;
73
+ const externalResponse = externalExecution && externalExecution.target_response && typeof externalExecution.target_response === "object"
74
+ ? externalExecution.target_response
75
+ : null;
76
+ // Provider-agnostic extraction of the real deliverable payload an execution
77
+ // target returned. Targets may conform to the contract (`output_payload`), or
78
+ // nest their result under `result.output` (the bundled MCP execution target),
79
+ // or return a bare `output`. Falling back across these shapes ensures a wired
80
+ // real call actually surfaces its output to the buyer instead of dropping it.
81
+ const externalOutputPayload = !externalResponse
82
+ ? null
83
+ : externalResponse.output_payload !== undefined && externalResponse.output_payload !== null
84
+ ? externalResponse.output_payload
85
+ : externalResponse.result && typeof externalResponse.result === "object" && externalResponse.result.output !== undefined
86
+ ? externalResponse.result.output
87
+ : externalResponse.output !== undefined
88
+ ? externalResponse.output
89
+ : null;
90
+ const resultsByTaskId = Object.fromEntries(resultTasks.map((task, index) => [
91
+ task.task_id,
92
+ (() => {
93
+ const base = {
94
+ task_ref: task.task_ref,
95
+ status: "completed",
96
+ output_ref: `output_${transactionId}_${index + 1}`
97
+ };
98
+ if (task.task_ref === "admission.preview") {
99
+ base.preview_kind = "admission";
100
+ base.admission_ref = `admission_${transactionId}_${index + 1}`;
101
+ }
102
+ if (task.task_ref === "a2a.negotiate") {
103
+ base.negotiation_ref = `a2a_negotiation_${transactionId}_${index + 1}`;
104
+ base.interaction_front = "a2a";
105
+ }
106
+ if (task.task_ref === "a2a.commit") {
107
+ base.negotiation_ref = `a2a_negotiation_${transactionId}_${index + 1}`;
108
+ base.commitment_ref = `a2a_commitment_${transactionId}_${index + 1}`;
109
+ base.interaction_front = "a2a";
110
+ }
111
+ if (task.task_ref === "a2c.session.open") {
112
+ base.session_ref = `a2c_session_${transactionId}_${index + 1}`;
113
+ base.interaction_front = "a2c";
114
+ }
115
+ if (task.task_ref === "a2c.pack.preview") {
116
+ base.preview_kind = "a2c_pack";
117
+ base.pack_preview_ref = `a2c_pack_preview_${transactionId}_${index + 1}`;
118
+ base.interaction_front = "a2c";
119
+ }
120
+ if (task.task_ref === "trust.handoff.emit") {
121
+ base.handoff_kind = "trust";
122
+ base.handoff_action = "emit";
123
+ base.handoff_ref = `trust_handoff_${transactionId}_${index + 1}`;
124
+ }
125
+ if (task.task_ref === "trust.handoff.validate") {
126
+ base.handoff_kind = "trust";
127
+ base.handoff_action = "validate";
128
+ base.handoff_ref = `trust_handoff_${transactionId}_${index + 1}`;
129
+ }
130
+ if (task.task_ref === "execution_receipt.handoff.emit") {
131
+ base.handoff_kind = "execution_receipt";
132
+ base.handoff_action = "emit";
133
+ base.handoff_ref = `execution_receipt_handoff_${transactionId}_${index + 1}`;
134
+ }
135
+ if (task.task_ref === "execution_receipt.handoff.validate") {
136
+ base.handoff_kind = "execution_receipt";
137
+ base.handoff_action = "validate";
138
+ base.handoff_ref = `execution_receipt_handoff_${transactionId}_${index + 1}`;
139
+ }
140
+ if (task.task_ref === "receipt_ledger.handoff.emit") {
141
+ base.handoff_kind = "receipt_ledger";
142
+ base.handoff_action = "emit";
143
+ base.handoff_ref = `receipt_ledger_handoff_${transactionId}_${index + 1}`;
144
+ }
145
+ if (task.task_ref === "receipt_ledger.handoff.validate") {
146
+ base.handoff_kind = "receipt_ledger";
147
+ base.handoff_action = "validate";
148
+ base.handoff_ref = `receipt_ledger_handoff_${transactionId}_${index + 1}`;
149
+ }
150
+ if (task.task_ref === "runtime.emit") {
151
+ base.runtime_ref = `runtime_event_${transactionId}_${index + 1}`;
152
+ base.runtime_action = "emit";
153
+ }
154
+ if (task.task_ref === "runtime.anchor") {
155
+ base.anchor_ref = `anchor_${transactionId}_${index + 1}`;
156
+ base.runtime_action = "anchor";
157
+ }
158
+ if (task.task_ref === "runtime.emit_anchor") {
159
+ base.runtime_ref = `runtime_event_${transactionId}_${index + 1}`;
160
+ base.anchor_ref = `anchor_${transactionId}_${index + 1}`;
161
+ base.runtime_action = "emit_anchor";
162
+ }
163
+ if (task.task_ref === "identity.attest.preview") {
164
+ base.preview_kind = "identity_attestation";
165
+ base.identity_attestation_ref = `identity_attestation_${transactionId}_${index + 1}`;
166
+ }
167
+ if (task.task_ref === "identity.registry.link") {
168
+ base.identity_link_ref = `identity_link_${transactionId}_${index + 1}`;
169
+ base.registry_record_ref = `registry_${transactionId}_${index + 1}`;
170
+ }
171
+ if (task.task_ref === "registry.register") {
172
+ base.registry_record_ref = `registry_${transactionId}_${index + 1}`;
173
+ }
174
+ if (task.task_ref === "registry.pack.bundle") {
175
+ base.registry_pack_ref = `registry_pack_${transactionId}_${index + 1}`;
176
+ }
177
+ if (task.task_ref === "anchoring.submit") {
178
+ base.anchor_ref = `anchor_${transactionId}_${index + 1}`;
179
+ }
180
+ if (task.task_ref === "anchoring.pack.preview") {
181
+ base.preview_kind = "anchoring_pack";
182
+ base.anchor_preview_ref = `anchor_preview_${transactionId}_${index + 1}`;
183
+ }
184
+ if (task.task_ref === "adapter.mcp.invoke") {
185
+ base.adapter_lane = "mcp";
186
+ base.tool_name = normalizedBody.tool_name || null;
187
+ base.invocation_status = externalExecution ? "provider_completed" : "simulated";
188
+ }
189
+ if (externalExecution) {
190
+ base.execution_provider = externalExecution.provider || null;
191
+ base.execution_target_id = externalExecution.target_id || null;
192
+ if (externalResponse && externalResponse.provider_job_id) base.provider_job_id = externalResponse.provider_job_id;
193
+ if (externalOutputPayload !== null) base.output_payload = externalOutputPayload;
194
+ if (externalResponse && externalResponse.output_ref) base.output_ref = externalResponse.output_ref;
195
+ base.provider_status = externalResponse && externalResponse.provider_status
196
+ ? externalResponse.provider_status
197
+ : "completed";
198
+ }
199
+ return base;
200
+ })()
201
+ ]));
202
+ const capabilityScopes = Array.from(new Set(resultTasks.flatMap((task) =>
203
+ Array.isArray(task.catalogTask && task.catalogTask.capabilities) ? task.catalogTask.capabilities : []
204
+ )));
205
+ const adaptersUsed = Array.from(new Set(resultTasks.flatMap((task) =>
206
+ Array.isArray(task.catalogTask && task.catalogTask.adapters) ? task.catalogTask.adapters : []
207
+ )));
208
+ const remediation = {
209
+ status: "none",
210
+ reason_code: null,
211
+ active_case_type: null,
212
+ active_case_id: null,
213
+ compensating_action_status: null,
214
+ updated_at_iso: completedAt.toISOString()
215
+ };
216
+ const payment = {
217
+ payment_id: `pay_${quote.quote_id}`,
218
+ scheme: quote.payment_scheme || "native",
219
+ protocol: quote.payment_protocol || null,
220
+ status: deferredCompletion ? "authorized" : "recorded",
221
+ amount_minor: quote.amount_minor,
222
+ currency: quote.currency,
223
+ quote_id: quote.quote_id,
224
+ wallet_id: opts.paymentVerification && opts.paymentVerification.wallet_id
225
+ ? opts.paymentVerification.wallet_id
226
+ : null,
227
+ verification_mode: opts.paymentVerification && opts.paymentVerification.verification_mode
228
+ ? opts.paymentVerification.verification_mode
229
+ : null,
230
+ recorded_at_iso: deferredCompletion ? null : completedAt.toISOString()
231
+ };
232
+ const bsvHints = buildBsvTeranodeSettlementHints({
233
+ settlementMode: quote.settlement_mode,
234
+ amountMinor: quote.amount_minor,
235
+ currency: quote.currency,
236
+ settlementBindingHash: quote.settlement_binding_hash,
237
+ treasuryDestination: quote.treasury
238
+ });
239
+ const bsvPaymentReference = extractBsvTeranodePaymentReference(
240
+ opts.paymentVerification && opts.paymentVerification.payment_payload
241
+ ? opts.paymentVerification.payment_payload
242
+ : null
243
+ );
244
+ const settlement = {
245
+ settlement_id: `settlement_${transactionId}`,
246
+ mode: quote.settlement_mode || null,
247
+ preference: quote.settlement_preference || null,
248
+ adapter: quote.settlement_adapter || null,
249
+ integration_id: quote.settlement_integration_id || null,
250
+ binding_hash: quote.settlement_binding_hash || null,
251
+ rail: isBsvTeranodeSettlementMode(quote.settlement_mode) ? "bsv_teranode" : null,
252
+ network: bsvHints ? bsvHints.network : null,
253
+ hints: bsvHints,
254
+ txid: bsvPaymentReference && bsvPaymentReference.txid ? bsvPaymentReference.txid : null,
255
+ raw_tx: bsvPaymentReference && (bsvPaymentReference.raw_tx || bsvPaymentReference.rawTx)
256
+ ? (bsvPaymentReference.raw_tx || bsvPaymentReference.rawTx)
257
+ : null,
258
+ status: deferredCompletion ? "pending_completion" : "recorded",
259
+ recorded_at_iso: deferredCompletion ? null : completedAt.toISOString()
260
+ };
261
+ const treasuryDestination = buildTreasuryDestination(quote.account_id, quote.settlement_mode);
262
+ const treasury = {
263
+ ...treasuryDestination,
264
+ treasury_receipt_id: `treasury_${receiptId}`,
265
+ status: deferredCompletion ? "pending_settlement" : "recorded",
266
+ amount_minor: quote.amount_minor,
267
+ currency: quote.currency,
268
+ payment_id: payment.payment_id,
269
+ settlement_id: settlement.settlement_id,
270
+ custody_external_ref: treasuryDestination.external_ref || null,
271
+ rail: treasuryDestination.rail || settlement.rail || null,
272
+ recorded_at_iso: deferredCompletion ? null : completedAt.toISOString()
273
+ };
274
+ const defaultPath = quote.default_path || buildDefaultEconomicPath(
275
+ Array.isArray(quote.integration_ids)
276
+ ? quote.integration_ids.find((integrationId) => typeof integrationId === "string" && integrationId.startsWith("builtin.payment."))
277
+ : null,
278
+ quote.settlement_integration_id || null,
279
+ treasury,
280
+ {
281
+ paymentProtocol: quote.payment_protocol || null,
282
+ settlementMode: quote.settlement_mode || null,
283
+ status: deferredCompletion ? "pending_completion" : "recorded"
284
+ }
285
+ );
286
+ const transaction = {
287
+ transaction_id: transactionId,
288
+ account_id: quote.account_id,
289
+ status: deferredCompletion ? "accepted" : "completed",
290
+ intent: payload.command || "command.execute",
291
+ amount_minor: quote.amount_minor,
292
+ currency: quote.currency,
293
+ quote_id: quote.quote_id,
294
+ integration_ids: Array.isArray(quote.integration_ids) ? quote.integration_ids : [],
295
+ protocols: Array.isArray(quote.protocols) ? quote.protocols : [],
296
+ output_ref: externalResponse && externalResponse.output_ref ? externalResponse.output_ref : `output_${transactionId}`,
297
+ callback_url: payload.callback_url || null,
298
+ capabilities: quote.capabilities,
299
+ adapters: quote.adapters,
300
+ payment,
301
+ settlement,
302
+ treasury,
303
+ default_path: {
304
+ ...defaultPath,
305
+ status: deferredCompletion ? "pending_completion" : "recorded"
306
+ },
307
+ execution: {
308
+ status: deferredCompletion ? "accepted" : "completed",
309
+ task_count: resultTasks.length,
310
+ capability_scopes: capabilityScopes,
311
+ adapters_used: adaptersUsed,
312
+ integration_ids: Array.isArray(quote.execution_integration_ids) ? quote.execution_integration_ids : [],
313
+ results_by_task_id: resultsByTaskId,
314
+ provider: externalExecution ? externalExecution.provider || null : null,
315
+ provider_target_id: externalExecution ? externalExecution.target_id || null : null,
316
+ provider_job_id: externalResponse && externalResponse.provider_job_id ? externalResponse.provider_job_id : null
317
+ },
318
+ created_at_iso: startedAt.toISOString(),
319
+ completed_at_iso: deferredCompletion ? null : completedAt.toISOString(),
320
+ timing,
321
+ remediation
322
+ };
323
+ const receipt = {
324
+ receipt_id: receiptId,
325
+ transaction_id: transactionId,
326
+ account_id: quote.account_id,
327
+ status: deferredCompletion ? "pending" : "completed",
328
+ amount_minor: quote.amount_minor,
329
+ currency: quote.currency,
330
+ quote_id: quote.quote_id,
331
+ integration_ids: Array.isArray(quote.integration_ids) ? quote.integration_ids : [],
332
+ settlement_integration_id: quote.settlement_integration_id || null,
333
+ payment,
334
+ settlement,
335
+ treasury,
336
+ default_path: {
337
+ ...defaultPath,
338
+ status: deferredCompletion ? "pending_completion" : "recorded"
339
+ },
340
+ proof_ref: externalResponse && externalResponse.proof_ref ? externalResponse.proof_ref : `proof_${receiptId}`,
341
+ execution_summary: {
342
+ command: payload.command || "command.execute",
343
+ task_count: resultTasks.length,
344
+ tasks: resultTasks.map((task) => ({
345
+ task_id: task.task_id,
346
+ task_ref: task.task_ref
347
+ }))
348
+ },
349
+ created_at_iso: deferredCompletion ? startedAt.toISOString() : completedAt.toISOString(),
350
+ timing,
351
+ remediation
352
+ };
353
+ const command = {
354
+ command: payload.command || "command.execute",
355
+ integration_ids: Array.isArray(quote.integration_ids) ? quote.integration_ids : [],
356
+ payment,
357
+ settlement,
358
+ treasury,
359
+ default_path: {
360
+ ...defaultPath,
361
+ status: deferredCompletion ? "pending_completion" : "recorded"
362
+ },
363
+ deliverables: resultTasks.map((task, index) => ({
364
+ kind: task.task_ref === "admission.preview" ? "preview" : "result",
365
+ ...(task.task_ref === "a2c.pack.preview"
366
+ ? {
367
+ kind: "preview"
368
+ }
369
+ : {}),
370
+ task_id: task.task_id,
371
+ task_ref: task.task_ref,
372
+ ref: `deliverable_${transactionId}_${index + 1}`,
373
+ // When a real execution target ran, carry its actual output into the
374
+ // buyer-facing deliverable (not just a placeholder ref), so the paid caller
375
+ // receives the real result in one response.
376
+ ...(externalExecution
377
+ ? {
378
+ invocation_status: "provider_completed",
379
+ execution_provider: externalExecution.provider || null,
380
+ execution_target_id: externalExecution.target_id || null,
381
+ provider_status: (externalResponse && externalResponse.provider_status) || "completed",
382
+ ...(externalResponse && externalResponse.output_ref ? { output_ref: externalResponse.output_ref } : {}),
383
+ ...(externalOutputPayload !== null ? { output_payload: externalOutputPayload } : {})
384
+ }
385
+ : {}),
386
+ ...(task.task_ref === "admission.preview"
387
+ ? {
388
+ admission_ref: `admission_${transactionId}_${index + 1}`
389
+ }
390
+ : {}),
391
+ ...(task.task_ref === "identity.attest.preview"
392
+ ? {
393
+ kind: "preview",
394
+ identity_attestation_ref: `identity_attestation_${transactionId}_${index + 1}`
395
+ }
396
+ : {}),
397
+ ...(task.task_ref === "identity.registry.link"
398
+ ? {
399
+ identity_link_ref: `identity_link_${transactionId}_${index + 1}`,
400
+ registry_record_ref: `registry_${transactionId}_${index + 1}`
401
+ }
402
+ : {}),
403
+ ...(task.task_ref === "a2a.negotiate"
404
+ ? {
405
+ negotiation_ref: `a2a_negotiation_${transactionId}_${index + 1}`,
406
+ interaction_front: "a2a"
407
+ }
408
+ : {}),
409
+ ...(task.task_ref === "a2a.commit"
410
+ ? {
411
+ negotiation_ref: `a2a_negotiation_${transactionId}_${index + 1}`,
412
+ commitment_ref: `a2a_commitment_${transactionId}_${index + 1}`,
413
+ interaction_front: "a2a"
414
+ }
415
+ : {}),
416
+ ...(task.task_ref === "a2c.session.open"
417
+ ? {
418
+ session_ref: `a2c_session_${transactionId}_${index + 1}`,
419
+ interaction_front: "a2c"
420
+ }
421
+ : {}),
422
+ ...(task.task_ref === "a2c.pack.preview"
423
+ ? {
424
+ pack_preview_ref: `a2c_pack_preview_${transactionId}_${index + 1}`,
425
+ interaction_front: "a2c"
426
+ }
427
+ : {}),
428
+ ...(task.task_ref === "trust.handoff.emit"
429
+ ? {
430
+ handoff_kind: "trust",
431
+ handoff_action: "emit",
432
+ handoff_ref: `trust_handoff_${transactionId}_${index + 1}`
433
+ }
434
+ : {}),
435
+ ...(task.task_ref === "trust.handoff.validate"
436
+ ? {
437
+ handoff_kind: "trust",
438
+ handoff_action: "validate",
439
+ handoff_ref: `trust_handoff_${transactionId}_${index + 1}`
440
+ }
441
+ : {}),
442
+ ...(task.task_ref === "execution_receipt.handoff.emit"
443
+ ? {
444
+ handoff_kind: "execution_receipt",
445
+ handoff_action: "emit",
446
+ handoff_ref: `execution_receipt_handoff_${transactionId}_${index + 1}`
447
+ }
448
+ : {}),
449
+ ...(task.task_ref === "execution_receipt.handoff.validate"
450
+ ? {
451
+ handoff_kind: "execution_receipt",
452
+ handoff_action: "validate",
453
+ handoff_ref: `execution_receipt_handoff_${transactionId}_${index + 1}`
454
+ }
455
+ : {}),
456
+ ...(task.task_ref === "receipt_ledger.handoff.emit"
457
+ ? {
458
+ handoff_kind: "receipt_ledger",
459
+ handoff_action: "emit",
460
+ handoff_ref: `receipt_ledger_handoff_${transactionId}_${index + 1}`
461
+ }
462
+ : {}),
463
+ ...(task.task_ref === "receipt_ledger.handoff.validate"
464
+ ? {
465
+ handoff_kind: "receipt_ledger",
466
+ handoff_action: "validate",
467
+ handoff_ref: `receipt_ledger_handoff_${transactionId}_${index + 1}`
468
+ }
469
+ : {}),
470
+ ...(task.task_ref === "runtime.emit"
471
+ ? {
472
+ runtime_ref: `runtime_event_${transactionId}_${index + 1}`,
473
+ runtime_action: "emit"
474
+ }
475
+ : {}),
476
+ ...(task.task_ref === "runtime.anchor"
477
+ ? {
478
+ anchor_ref: `anchor_${transactionId}_${index + 1}`,
479
+ runtime_action: "anchor"
480
+ }
481
+ : {}),
482
+ ...(task.task_ref === "runtime.emit_anchor"
483
+ ? {
484
+ runtime_ref: `runtime_event_${transactionId}_${index + 1}`,
485
+ anchor_ref: `anchor_${transactionId}_${index + 1}`,
486
+ runtime_action: "emit_anchor"
487
+ }
488
+ : {}),
489
+ ...(task.task_ref === "registry.register"
490
+ ? {
491
+ registry_record_ref: `registry_${transactionId}_${index + 1}`
492
+ }
493
+ : {}),
494
+ ...(task.task_ref === "registry.pack.bundle"
495
+ ? {
496
+ registry_pack_ref: `registry_pack_${transactionId}_${index + 1}`
497
+ }
498
+ : {}),
499
+ ...(task.task_ref === "anchoring.submit"
500
+ ? {
501
+ anchor_ref: `anchor_${transactionId}_${index + 1}`
502
+ }
503
+ : {}),
504
+ ...(task.task_ref === "anchoring.pack.preview"
505
+ ? {
506
+ kind: "preview",
507
+ anchor_preview_ref: `anchor_preview_${transactionId}_${index + 1}`
508
+ }
509
+ : {}),
510
+ ...(task.task_ref === "adapter.mcp.invoke"
511
+ ? {
512
+ adapter_lane: "mcp",
513
+ tool_name: normalizedBody.tool_name || null
514
+ }
515
+ : {})
516
+ })),
517
+ monitor: {
518
+ transaction_id: transactionId,
519
+ status_url: `/v1/governed-transactions/${transactionId}`,
520
+ receipt_url: `/v1/receipts/${receiptId}`,
521
+ delivery_url: `/v1/deliveries/delivery_${transactionId}`,
522
+ payment_url: `/v1/payment-ledger/${payment.payment_id}`
523
+ }
524
+ };
525
+ return { transaction, receipt, command };
526
+ }
527
+
528
+ module.exports = {
529
+ createCommandArtifacts
530
+ };