@dvmkit/dvm-cli 0.4.2 → 0.6.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.
Files changed (32) hide show
  1. package/README.md +18 -0
  2. package/dist/{chunk-JVQFBKDX.js → chunk-3YE43JJR.js} +3 -3
  3. package/dist/{chunk-2TKOJBBB.js → chunk-4KINCBHL.js} +53 -51
  4. package/dist/{chunk-CSRGRJB6.js → chunk-EE2MREOM.js} +68 -4
  5. package/dist/chunk-ERJFRCJO.js +150 -0
  6. package/dist/chunk-FL2ICZW5.js +383 -0
  7. package/dist/chunk-FSYEZERJ.js +1667 -0
  8. package/dist/{chunk-6KEZKHN7.js → chunk-ID5G54HJ.js} +211 -35
  9. package/dist/{chunk-4NWEJTAW.js → chunk-MJ4X4BEW.js} +7953 -8036
  10. package/dist/{chunk-KBFK5MM5.js → chunk-QICJRKB7.js} +6972 -1321
  11. package/dist/{chunk-ECEM3ZAS.js → chunk-R55XNXAR.js} +53 -185
  12. package/dist/{chunk-OOA6X5MI.js → chunk-SHZMNEXE.js} +58 -17
  13. package/dist/{chunk-PRN4FHDY.js → chunk-TZWZTS37.js} +38 -23
  14. package/dist/{chunk-DRO3JP42.js → chunk-VOO5IAK7.js} +582 -83
  15. package/dist/{chunk-2VVVWBQA.js → chunk-WTM2EICO.js} +2 -352
  16. package/dist/chunk-XHOFQ3OS.js +717 -0
  17. package/dist/{chunk-KUBOPQ3N.js → chunk-Z4CZUMUT.js} +937 -1599
  18. package/dist/{client-OWIOFBKS.js → client-RYDA62DU.js} +11 -8
  19. package/dist/{dist-2G3QBGID.js → dist-GKCEIY2A.js} +7 -4
  20. package/dist/dvm.js +866 -121
  21. package/dist/{internal-DS42OMXR.js → internal-INUTGTXQ.js} +52 -38
  22. package/dist/{payout-reporter-TQ3YIFLZ-64RGUGTA.js → payout-reporter-3Y2U55ZX-CMKACKA3.js} +2 -2
  23. package/dist/{pocket-refill-3WMWHMOT.js → pocket-refill-K5T4LZF6.js} +18 -14
  24. package/dist/{revenue-reporter-VN4WYN74-T7HYTIDM.js → revenue-reporter-MAERPYGK-CCPZXGFA.js} +2 -2
  25. package/dist/tempo-AXTE4ZEA.js +34 -0
  26. package/dist/{tempo-lifecycle-TJ62A7UX-OLLWSS3I.js → tempo-lifecycle-ZBBLFBMH-HWI4PLYP.js} +11 -7
  27. package/dist/{tempo-wallet-RCGBBAJH-TSTQAZKT.js → tempo-wallet-B5XXV3YH-SRQBMYFV.js} +2 -2
  28. package/dist/{x402-S47Q77UE-33J7AIH3.js → x402-R6HPBY5D-VMSW7Z32.js} +3 -3
  29. package/package.json +2 -2
  30. package/skills/dvm/calling.md +2 -0
  31. package/skills/dvm/credit.md +4 -0
  32. package/skills/dvm/wallets.md +4 -0
@@ -4,9 +4,9 @@ const require = __dvmCreateRequire(import.meta.url);
4
4
  import {
5
5
  diagnosticConsole,
6
6
  withSdkInitLock
7
- } from "./chunk-PRN4FHDY.js";
7
+ } from "./chunk-TZWZTS37.js";
8
8
 
9
- // node_modules/@dvmkit/sdk/dist/chunk-Y7JZLO5J.js
9
+ // node_modules/@dvmkit/sdk/dist/chunk-GAOAYHZA.js
10
10
  import { createHash, randomUUID } from "crypto";
11
11
  var MemoryX402BatchStore = class {
12
12
  batches = /* @__PURE__ */ new Map();
@@ -53,9 +53,13 @@ var PostgresX402BatchStore = class {
53
53
  first_failed_at BIGINT,
54
54
  last_error TEXT,
55
55
  next_retry_at BIGINT,
56
+ obligation_ids JSONB NOT NULL DEFAULT '[]'::jsonb,
56
57
  updated_at BIGINT NOT NULL
57
58
  )
58
59
  `);
60
+ await this.db.query(
61
+ "ALTER TABLE payout_x402_batches ADD COLUMN IF NOT EXISTS obligation_ids JSONB NOT NULL DEFAULT '[]'::jsonb"
62
+ );
59
63
  });
60
64
  }
61
65
  async get(scope) {
@@ -72,15 +76,16 @@ var PostgresX402BatchStore = class {
72
76
  attempts: row.attempts,
73
77
  firstFailedAt: row.first_failed_at === null ? null : Number(row.first_failed_at),
74
78
  lastError: row.last_error,
75
- nextRetryAt: row.next_retry_at === null ? null : Number(row.next_retry_at)
79
+ nextRetryAt: row.next_retry_at === null ? null : Number(row.next_retry_at),
80
+ ...row.obligation_ids.length ? { obligationIds: row.obligation_ids } : {}
76
81
  };
77
82
  }
78
83
  async put(batch) {
79
84
  await this.db.query(
80
85
  `INSERT INTO payout_x402_batches (
81
86
  scope, batch_id, claimed_native, voucher_count, claims, opened_at,
82
- attempts, first_failed_at, last_error, next_retry_at, updated_at
83
- ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
87
+ attempts, first_failed_at, last_error, next_retry_at, obligation_ids, updated_at
88
+ ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
84
89
  ON CONFLICT (scope) DO UPDATE SET
85
90
  batch_id = EXCLUDED.batch_id,
86
91
  claimed_native = EXCLUDED.claimed_native,
@@ -91,6 +96,7 @@ var PostgresX402BatchStore = class {
91
96
  first_failed_at = EXCLUDED.first_failed_at,
92
97
  last_error = EXCLUDED.last_error,
93
98
  next_retry_at = EXCLUDED.next_retry_at,
99
+ obligation_ids = EXCLUDED.obligation_ids,
94
100
  updated_at = EXCLUDED.updated_at`,
95
101
  [
96
102
  batch.scope,
@@ -103,6 +109,7 @@ var PostgresX402BatchStore = class {
103
109
  batch.firstFailedAt,
104
110
  batch.lastError,
105
111
  batch.nextRetryAt,
112
+ JSON.stringify(batch.obligationIds ?? []),
106
113
  Date.now()
107
114
  ]
108
115
  );
@@ -143,6 +150,7 @@ var PayoutReporter = class {
143
150
  ownsBatchStore;
144
151
  intervalMs;
145
152
  now;
153
+ hostedObligations;
146
154
  lastX402SnapshotAt = 0;
147
155
  x402;
148
156
  tempo;
@@ -157,6 +165,7 @@ var PayoutReporter = class {
157
165
  this.batches = opts.batchStore ?? (opts.db ? new PostgresX402BatchStore(opts.db) : new MemoryX402BatchStore());
158
166
  this.intervalMs = opts.snapshotIntervalMs ?? PAYOUT_SNAPSHOT_INTERVAL_MS;
159
167
  this.now = opts.now ?? Date.now;
168
+ this.hostedObligations = opts.hostedObligations;
160
169
  }
161
170
  async init() {
162
171
  if (this.ownsBatchStore && this.batches instanceof PostgresX402BatchStore) {
@@ -172,10 +181,12 @@ var PayoutReporter = class {
172
181
  if (this.timer) return;
173
182
  this.warmup = setTimeout(() => {
174
183
  void this.snapshot();
184
+ void this.reconcileHosted();
175
185
  }, WARMUP_MS);
176
186
  this.warmup.unref();
177
187
  this.timer = setInterval(() => {
178
188
  void this.snapshot();
189
+ void this.reconcileHosted();
179
190
  }, this.intervalMs);
180
191
  this.timer.unref();
181
192
  }
@@ -185,6 +196,45 @@ var PayoutReporter = class {
185
196
  if (this.timer) clearInterval(this.timer);
186
197
  this.timer = null;
187
198
  }
199
+ async reconcileHosted() {
200
+ if (!this.hostedObligations) return;
201
+ try {
202
+ for (const failure of await this.hostedObligations.recoverOriginalX402Refunds()) {
203
+ warn("hosted_refund_recovery_pending", { id: failure.id, error: message(failure.error) });
204
+ }
205
+ for (const candidate of await this.hostedObligations.recoveryCandidates()) {
206
+ if (candidate.blockHash) continue;
207
+ try {
208
+ await this.hostedObligations.observeCanonical(candidate.id);
209
+ } catch (error) {
210
+ warn("hosted_effect_observation_pending", { id: candidate.id, error: message(error) });
211
+ }
212
+ }
213
+ for (const failure of await this.hostedObligations.reconcileX402Batches()) {
214
+ warn("hosted_batch_recovery_pending", { id: failure.id, error: message(failure.error) });
215
+ }
216
+ for (const candidate of await this.hostedObligations.recoveryCandidates()) {
217
+ if (!candidate.blockHash || candidate.reportId) continue;
218
+ try {
219
+ if (candidate.rail === "tempo" && (candidate.operation === "settle" || candidate.operation === "close") && candidate.deltaNative !== null && BigInt(candidate.deltaNative) > 0n) {
220
+ await this.tempoSettled({
221
+ txHash: candidate.txHash,
222
+ channelId: candidate.channelId,
223
+ trigger: candidate.operation,
224
+ amount: BigInt(candidate.deltaNative),
225
+ delta: BigInt(candidate.deltaNative)
226
+ });
227
+ } else if (candidate.rail === "x402" && candidate.operation === "settle" && this.x402) {
228
+ await this.recordX402Settle(this.x402, candidate.txHash);
229
+ }
230
+ } catch (error) {
231
+ warn("hosted_effect_report_pending", { id: candidate.id, error: message(error) });
232
+ }
233
+ }
234
+ } catch (error) {
235
+ warn("hosted_effect_recovery_pending", { error: message(error) });
236
+ }
237
+ }
188
238
  enqueuePayout(payload, tx) {
189
239
  return this.transport.enqueuePayout(payload, tx);
190
240
  }
@@ -264,8 +314,24 @@ var PayoutReporter = class {
264
314
  */
265
315
  async tempoSettled(event) {
266
316
  try {
317
+ let hostedIds;
318
+ if (this.hostedObligations) {
319
+ hostedIds = await this.hostedObligations.submittedIds(
320
+ "tempo",
321
+ event.trigger === "close" ? "close" : "settle",
322
+ event.txHash
323
+ );
324
+ if (hostedIds.length === 1) {
325
+ const observed = await this.hostedObligations.observeCanonical(hostedIds[0]);
326
+ if (BigInt(observed.deltaNative) !== event.delta) {
327
+ throw new Error("Hosted Tempo callback differs from finalized settlement amount");
328
+ }
329
+ } else if (await this.hostedObligations.hasCompleteHistory()) {
330
+ throw new Error("Hosted Tempo payout has no exact original settlement obligation");
331
+ }
332
+ }
267
333
  if (event.delta <= 0n) return;
268
- await this.reportPayout({
334
+ const payload = {
269
335
  dvmId: this.dvmId,
270
336
  payoutId: `tempo:${event.txHash}`,
271
337
  rail: "tempo",
@@ -280,9 +346,23 @@ var PayoutReporter = class {
280
346
  ...this.tempo?.network ? { network: this.tempo.network } : {}
281
347
  },
282
348
  landedAt: this.now()
283
- });
349
+ };
350
+ if (this.hostedObligations) {
351
+ if (hostedIds?.length !== 1 || !this.transport.enqueueHostedPayout) {
352
+ if (await this.hostedObligations.hasCompleteHistory()) {
353
+ throw new Error("Hosted Tempo payout has no exact original settlement obligation");
354
+ }
355
+ await this.reportPayout(payload);
356
+ } else {
357
+ await this.transport.enqueueHostedPayout(payload, hostedIds);
358
+ void this.flush();
359
+ }
360
+ } else {
361
+ await this.reportPayout(payload);
362
+ }
284
363
  } catch (err) {
285
364
  warn("payout_report_failed", { rail: "tempo", kind: event.trigger, error: message(err) });
365
+ if (this.hostedObligations) throw err;
286
366
  }
287
367
  void this.snapshotTempo();
288
368
  }
@@ -301,29 +381,49 @@ var PayoutReporter = class {
301
381
  * this does not report the caller refund.
302
382
  */
303
383
  async tempoCloseReconciled(event, tx) {
304
- const settled = BigInt(event.settledToPayee);
384
+ let settled = BigInt(event.settledToPayee);
385
+ let hostedIds;
386
+ if (this.hostedObligations) {
387
+ hostedIds = await this.hostedObligations.submittedIds("tempo", "close", event.txHash);
388
+ if (hostedIds.length === 1) {
389
+ const alreadySettled = await this.hostedObligations.originalTempoCloseSettled(
390
+ hostedIds[0],
391
+ event.channelId,
392
+ event.txHash
393
+ );
394
+ settled -= alreadySettled;
395
+ if (settled < 0n) throw new Error("Hosted Tempo repair precedes its original settled amount");
396
+ } else if (await this.hostedObligations.hasCompleteHistory(tx)) {
397
+ throw new Error("Hosted Tempo repair has no original close obligation");
398
+ }
399
+ }
305
400
  if (settled <= 0n) return;
306
- await this.enqueuePayout(
307
- {
308
- dvmId: this.dvmId,
309
- payoutId: `tempo:${event.txHash}`,
310
- rail: "tempo",
311
- kind: "close",
312
- nativeAmount: Number(settled),
313
- nativeAsset: "usdc",
314
- recipient: this.tempo?.recipient ?? null,
315
- refs: {
316
- channelId: event.channelId,
317
- txHash: event.txHash,
318
- trigger: "close",
319
- ...this.tempo?.network ? { network: this.tempo.network } : {}
320
- },
321
- landedAt: this.now(),
322
- repaired: true,
323
- repairKind: "tempo_close_reconciled"
401
+ const payload = {
402
+ dvmId: this.dvmId,
403
+ payoutId: `tempo:${event.txHash}`,
404
+ rail: "tempo",
405
+ kind: "close",
406
+ nativeAmount: Number(settled),
407
+ nativeAsset: "usdc",
408
+ recipient: this.tempo?.recipient ?? null,
409
+ refs: {
410
+ channelId: event.channelId,
411
+ txHash: event.txHash,
412
+ trigger: "close",
413
+ ...this.tempo?.network ? { network: this.tempo.network } : {}
324
414
  },
325
- tx
326
- );
415
+ landedAt: this.now(),
416
+ repaired: true,
417
+ repairKind: "tempo_close_reconciled"
418
+ };
419
+ if (hostedIds?.length === 1) {
420
+ if (!this.transport.enqueueHostedPayout) {
421
+ throw new Error("Hosted Tempo repair lacks its durable report queue");
422
+ }
423
+ await this.transport.enqueueHostedPayout(payload, hostedIds, tx);
424
+ } else {
425
+ await this.enqueuePayout(payload, tx);
426
+ }
327
427
  if (!tx) {
328
428
  void this.flush();
329
429
  void this.snapshotTempo();
@@ -344,6 +444,17 @@ var PayoutReporter = class {
344
444
  const ctx = this.x402;
345
445
  if (ctx) await this.recordX402SettleFailure(ctx, error);
346
446
  void this.snapshotX402();
447
+ },
448
+ recordRefundClaim: async (transaction, delta) => {
449
+ const ctx = this.x402;
450
+ if (!ctx) throw new Error("Hosted x402 refund has no payout context");
451
+ await this.recordX402Claim(ctx, {
452
+ delta,
453
+ vouchers: 1,
454
+ transactions: [transaction],
455
+ operation: "refund"
456
+ });
457
+ void this.snapshotX402();
347
458
  }
348
459
  };
349
460
  }
@@ -371,7 +482,8 @@ var PayoutReporter = class {
371
482
  }
372
483
  await this.recordX402Claim(ctx, {
373
484
  delta,
374
- vouchers: results.reduce((sum, r) => sum + r.vouchers, 0)
485
+ vouchers: results.reduce((sum, r) => sum + r.vouchers, 0),
486
+ transactions: results.map((result) => result.transaction)
375
487
  });
376
488
  void this.snapshotX402();
377
489
  }
@@ -390,10 +502,32 @@ var PayoutReporter = class {
390
502
  throw err;
391
503
  }
392
504
  };
505
+ const refund = async (channelIds) => {
506
+ const ctx = this.x402;
507
+ const before = ctx ? await claimedByChannel(ctx.storage) : /* @__PURE__ */ new Map();
508
+ const results = await manager.refund(channelIds);
509
+ if (ctx && this.hostedObligations) {
510
+ for (const result of results) {
511
+ const original = before.get(result.channel.toLowerCase());
512
+ if (!original) throw new Error("Hosted x402 refund lost its original channel snapshot");
513
+ const delta = original.charged - original.claimed;
514
+ if (delta > 0n) {
515
+ await this.recordX402Claim(ctx, {
516
+ delta,
517
+ vouchers: 1,
518
+ transactions: [result.transaction],
519
+ operation: "refund"
520
+ });
521
+ }
522
+ }
523
+ }
524
+ return results;
525
+ };
393
526
  return new Proxy(manager, {
394
527
  get(target, prop, receiver) {
395
528
  if (prop === "claim") return claim;
396
529
  if (prop === "settle") return settle;
530
+ if (prop === "refund" && target.refund) return refund;
397
531
  const value = Reflect.get(target, prop, receiver);
398
532
  return typeof value === "function" ? value.bind(target) : value;
399
533
  }
@@ -402,7 +536,11 @@ var PayoutReporter = class {
402
536
  async recordX402Claim(ctx, claim) {
403
537
  try {
404
538
  const now = this.now();
405
- const open = await this.batches.get(ctx.scope) ?? {
539
+ const existing = await this.batches.get(ctx.scope);
540
+ if (this.hostedObligations && !existing) {
541
+ throw new Error("Hosted x402 claim lost its pre-effect payout batch");
542
+ }
543
+ const open = existing ?? {
406
544
  scope: ctx.scope,
407
545
  batchId: randomUUID(),
408
546
  claimedNative: 0n,
@@ -417,9 +555,22 @@ var PayoutReporter = class {
417
555
  open.claimedNative += claim.delta;
418
556
  open.voucherCount += claim.vouchers;
419
557
  open.claims += 1;
558
+ if (this.hostedObligations) {
559
+ const ids = (await Promise.all(claim.transactions.map((txHash) => this.hostedObligations.submittedIds(
560
+ "x402",
561
+ claim.operation ?? "claim",
562
+ txHash,
563
+ open.batchId
564
+ )))).flat();
565
+ if (!ids.length || ids.length < claim.vouchers || new Set(ids).size !== ids.length) {
566
+ throw new Error("Hosted x402 claim has no exact reserved voucher set");
567
+ }
568
+ open.obligationIds = [.../* @__PURE__ */ new Set([...open.obligationIds ?? [], ...ids])];
569
+ }
420
570
  await this.batches.put(open);
421
571
  } catch (err) {
422
572
  warn("payout_batch_track_failed", { scope: ctx.scope, error: message(err) });
573
+ if (this.hostedObligations) throw err;
423
574
  }
424
575
  }
425
576
  async recordX402Settle(ctx, transaction) {
@@ -434,6 +585,23 @@ var PayoutReporter = class {
434
585
  warn("payout_batch_empty", { scope: ctx.scope, transaction, batchId: open.batchId });
435
586
  return;
436
587
  }
588
+ if (this.hostedObligations) {
589
+ if (!open.obligationIds?.length) {
590
+ throw new Error("Hosted x402 payout has no original claim identities");
591
+ }
592
+ const settleId = await this.hostedObligations.settlementId(open.batchId, transaction);
593
+ const settled = await this.hostedObligations.observeCanonical(settleId);
594
+ if (BigInt(settled.deltaNative) !== open.claimedNative) {
595
+ throw new Error("Hosted x402 settled amount differs from its original batch");
596
+ }
597
+ let claimed = 0n;
598
+ for (const id of open.obligationIds) {
599
+ claimed += BigInt((await this.hostedObligations.observeCanonical(id)).deltaNative);
600
+ }
601
+ if (claimed !== open.claimedNative) {
602
+ throw new Error("Hosted x402 batch amount differs from finalized original claims");
603
+ }
604
+ }
437
605
  const payload = {
438
606
  dvmId: this.dvmId,
439
607
  payoutId: `x402:batch:${open.batchId}`,
@@ -451,17 +619,25 @@ var PayoutReporter = class {
451
619
  },
452
620
  landedAt: this.now()
453
621
  };
454
- const closed = await this.batches.settle(
455
- ctx.scope,
456
- open.batchId,
457
- (tx) => this.enqueuePayout(payload, tx)
458
- );
622
+ const closed = await this.batches.settle(ctx.scope, open.batchId, async (tx) => {
623
+ if (!this.hostedObligations) return this.enqueuePayout(payload, tx);
624
+ if (!this.transport.enqueueHostedPayout || !open.obligationIds?.length) {
625
+ throw new Error("Hosted x402 payout lacks its original claims or durable queue");
626
+ }
627
+ const settleId = await this.hostedObligations.settlementId(open.batchId, transaction);
628
+ await this.transport.enqueueHostedPayout(
629
+ payload,
630
+ [...open.obligationIds, settleId],
631
+ tx
632
+ );
633
+ });
459
634
  if (!closed) {
460
635
  warn("payout_batch_raced", { scope: ctx.scope, transaction, batchId: open.batchId });
461
636
  return;
462
637
  }
463
638
  } catch (err) {
464
639
  warn("payout_report_failed", { rail: "x402", kind: "batch", error: message(err) });
640
+ if (this.hostedObligations) throw err;
465
641
  return;
466
642
  }
467
643
  void this.flush();