@dvmkit/dvm-cli 0.15.1 → 0.15.2

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.
@@ -3359,49 +3359,80 @@ async function settleTakenPayment(jobId, requestSeq) {
3359
3359
  return booked2;
3360
3360
  }
3361
3361
 
3362
- // src/lib/wallet-agent/submission-verdicts.ts
3362
+ // src/lib/wallet-agent/journal-file.ts
3363
3363
  var import_proper_lockfile2 = __toESM(require_proper_lockfile(), 1);
3364
3364
  import { closeSync as closeSync4, existsSync as existsSync4, fsyncSync as fsyncSync3, openSync as openSync4, readFileSync as readFileSync4, renameSync as renameSync2, writeFileSync as writeFileSync3 } from "fs";
3365
3365
  import { dirname as dirname2, join as join3 } from "path";
3366
- var verdictsFile = () => join3(dirname2(JOBS_FILE), "submission-verdicts.json");
3367
- function readVerdicts() {
3368
- if (!existsSync4(verdictsFile())) return [];
3369
- try {
3370
- const parsed = JSON.parse(readFileSync4(verdictsFile(), "utf8"));
3371
- return Array.isArray(parsed) ? parsed : [];
3372
- } catch {
3373
- return [];
3366
+ function journalFile(name) {
3367
+ const file = () => join3(dirname2(JOBS_FILE), name);
3368
+ function read() {
3369
+ if (!existsSync4(file())) return [];
3370
+ try {
3371
+ const parsed = JSON.parse(readFileSync4(file(), "utf8"));
3372
+ return Array.isArray(parsed) ? parsed : [];
3373
+ } catch {
3374
+ return [];
3375
+ }
3374
3376
  }
3375
- }
3376
- function withVerdictsLock(change) {
3377
- ensureConfigDir();
3378
- if (!existsSync4(verdictsFile())) writeFileSync3(verdictsFile(), "[]\n", { mode: 384, flag: "a" });
3379
- let release;
3380
- for (let attempt = 0; !release; attempt++) {
3377
+ function withLock(change) {
3378
+ ensureConfigDir();
3379
+ if (!existsSync4(file())) writeFileSync3(file(), "[]\n", { mode: 384, flag: "a" });
3380
+ let release;
3381
+ for (let attempt = 0; !release; attempt++) {
3382
+ try {
3383
+ release = import_proper_lockfile2.default.lockSync(file(), { realpath: false });
3384
+ } catch (error) {
3385
+ if (attempt >= 40) throw error;
3386
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 25);
3387
+ }
3388
+ }
3381
3389
  try {
3382
- release = import_proper_lockfile2.default.lockSync(verdictsFile(), { realpath: false });
3383
- } catch (error) {
3384
- if (attempt >= 40) throw error;
3385
- Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 25);
3390
+ return change();
3391
+ } finally {
3392
+ release();
3386
3393
  }
3387
3394
  }
3388
- try {
3389
- return change();
3390
- } finally {
3391
- release();
3395
+ function write(entries) {
3396
+ const temporary = `${file()}.${process.pid}.tmp`;
3397
+ const fd = openSync4(temporary, "w", 384);
3398
+ try {
3399
+ writeFileSync3(fd, JSON.stringify(entries, null, 2) + "\n");
3400
+ fsyncSync3(fd);
3401
+ } finally {
3402
+ closeSync4(fd);
3403
+ }
3404
+ renameSync2(temporary, file());
3392
3405
  }
3406
+ return { read, withLock, write };
3393
3407
  }
3394
- function writeVerdicts(verdicts) {
3395
- const temporary = `${verdictsFile()}.${process.pid}.tmp`;
3396
- const fd = openSync4(temporary, "w", 384);
3397
- try {
3398
- writeFileSync3(fd, JSON.stringify(verdicts, null, 2) + "\n");
3399
- fsyncSync3(fd);
3400
- } finally {
3401
- closeSync4(fd);
3402
- }
3403
- renameSync2(temporary, verdictsFile());
3408
+
3409
+ // src/lib/wallet-agent/reclaim-intents.ts
3410
+ var intents = journalFile("reclaim-intents.json");
3411
+ function recordReclaimIntent(intent) {
3412
+ intents.withLock(() => {
3413
+ intents.write([...intents.read().filter((held) => held.requestId !== intent.requestId), intent]);
3414
+ });
3415
+ }
3416
+ function loadReclaimIntent(requestId) {
3417
+ return intents.read().find((intent) => intent.requestId === requestId);
3418
+ }
3419
+ function allReclaimIntents() {
3420
+ return intents.read();
3421
+ }
3422
+ function clearReclaimIntent(requestId, createdAt) {
3423
+ const matches = (held) => held.requestId === requestId && (createdAt === void 0 || held.createdAt === createdAt);
3424
+ if (!intents.read().some(matches)) return;
3425
+ intents.withLock(() => intents.write(intents.read().filter((held) => !matches(held))));
3426
+ }
3427
+ function reclaimIntentIds() {
3428
+ return new Set(intents.read().map((intent) => intent.requestId));
3404
3429
  }
3430
+
3431
+ // src/lib/wallet-agent/submission-verdicts.ts
3432
+ var verdictJournal = journalFile("submission-verdicts.json");
3433
+ var readVerdicts = verdictJournal.read;
3434
+ var withVerdictsLock = verdictJournal.withLock;
3435
+ var writeVerdicts = verdictJournal.write;
3405
3436
  function recordSubmissionVerdicts(verdicts) {
3406
3437
  if (verdicts.length === 0) return;
3407
3438
  withVerdictsLock(() => {
@@ -3413,10 +3444,24 @@ function recordSubmissionVerdicts(verdicts) {
3413
3444
  });
3414
3445
  }
3415
3446
  async function applySubmissionVerdicts() {
3416
- const verdicts = readVerdicts();
3417
- if (verdicts.length === 0) return;
3447
+ let failure;
3448
+ try {
3449
+ finalizeLandedReclaims();
3450
+ } catch (error) {
3451
+ failure = { error };
3452
+ }
3453
+ try {
3454
+ await applyJournalledVerdicts();
3455
+ } catch (error) {
3456
+ failure ??= { error };
3457
+ }
3458
+ if (failure) throw failure.error;
3459
+ }
3460
+ async function applyJournalledVerdicts() {
3461
+ const journalled = readVerdicts();
3462
+ if (journalled.length === 0) return;
3418
3463
  const held = new Set(loadAgentWallet()?.pending_submissions.map((entry) => entry.request_id) ?? []);
3419
- const landed = verdicts.filter((verdict) => !held.has(verdict.requestId));
3464
+ const landed = journalled.filter((verdict) => !held.has(verdict.requestId));
3420
3465
  if (landed.length === 0) return;
3421
3466
  releaseReclaimedSubmissions(new Set(landed.filter((verdict) => verdict.verdict === "reclaimed").map((verdict) => verdict.requestId)));
3422
3467
  for (const verdict of landed) {
@@ -3433,6 +3478,18 @@ async function applySubmissionVerdicts() {
3433
3478
  const applied = new Set(landed.map((verdict) => verdict.requestId));
3434
3479
  withVerdictsLock(() => writeVerdicts(readVerdicts().filter((verdict) => !applied.has(verdict.requestId))));
3435
3480
  }
3481
+ function finalizeLandedReclaims() {
3482
+ const intents2 = allReclaimIntents();
3483
+ if (intents2.length === 0) return;
3484
+ const wallet = loadAgentWallet();
3485
+ if (!wallet) return;
3486
+ const held = new Set(wallet.pending_submissions.map((entry) => entry.request_id));
3487
+ for (const intent of intents2) {
3488
+ if (held.has(intent.requestId)) continue;
3489
+ recordSubmissionVerdicts([intent.verdict]);
3490
+ clearReclaimIntent(intent.requestId, intent.createdAt);
3491
+ }
3492
+ }
3436
3493
 
3437
3494
  // src/lib/yield-state.ts
3438
3495
  var JOB_READ_TIMEOUT_MS = 6e4;
@@ -3590,10 +3647,17 @@ function pollNextAction(jobId, hint) {
3590
3647
  };
3591
3648
  }
3592
3649
  function heldPaymentResendable(job, requestSeq) {
3650
+ return heldPaymentResendability(job, requestSeq) === "resendable";
3651
+ }
3652
+ function heldPaymentResendability(job, requestSeq) {
3593
3653
  const submission = job?.unconfirmedPaymentSubmissions?.[requestSeq];
3594
- if (!submission || submission.blocked) return false;
3595
- if (submission.rail !== "cashu_p2pk") return true;
3596
- return loadAgentWallet()?.pending_submissions.some((entry) => entry.request_id === submission.cashuRequestId) === true;
3654
+ if (!submission || submission.blocked) return "unavailable";
3655
+ if (submission.rail !== "cashu_p2pk") return "resendable";
3656
+ try {
3657
+ return loadAgentWallet()?.pending_submissions.some((entry) => entry.request_id === submission.cashuRequestId) ? "resendable" : "unavailable";
3658
+ } catch {
3659
+ return "unreadable";
3660
+ }
3597
3661
  }
3598
3662
  function paymentInFlightHint(jobId, job, requestSeq) {
3599
3663
  const arg = guidanceJobArg(jobId);
@@ -3601,8 +3665,12 @@ function paymentInFlightHint(jobId, job, requestSeq) {
3601
3665
  if (!job || requestSeq === void 0) return resend;
3602
3666
  const submission = job.unconfirmedPaymentSubmissions?.[requestSeq];
3603
3667
  const stop = `To stop instead, \`dvm decline ${arg}\` ends the job and keeps the finished work, or \`dvm cancel ${arg}\` halts the work now and charges the work in progress.`;
3604
- if (submission && !submission.blocked)
3605
- return heldPaymentResendable(job, requestSeq) ? resend : `The wallet no longer holds the earlier Cashu payment for this request, so it cannot be re-sent. ${unresendablePaymentHint(jobId)}`;
3668
+ if (submission && !submission.blocked) {
3669
+ const resendability = heldPaymentResendability(job, requestSeq);
3670
+ if (resendability === "unreadable")
3671
+ return "The wallet is unreadable, so the earlier Cashu payment cannot be checked or re-sent. Preserve the wallet file; consult `dvm wallet recover --help` for recovery from your backed-up secret. " + unresendablePaymentHint(jobId);
3672
+ return resendability === "resendable" ? resend : `The wallet no longer holds the earlier Cashu payment for this request, so it cannot be re-sent. ${unresendablePaymentHint(jobId)}`;
3673
+ }
3606
3674
  if (submission?.blocked?.reason === "not_resendable")
3607
3675
  return `${submission.blocked.detail} ${unresendablePaymentHint(jobId)}`;
3608
3676
  const askBlock = job.askPaymentBlocks?.[requestSeq];
@@ -8792,8 +8860,13 @@ export {
8792
8860
  withAcceptedUnresolved,
8793
8861
  midJobSpendLabel,
8794
8862
  finishBookedSubmission,
8863
+ recordReclaimIntent,
8864
+ loadReclaimIntent,
8865
+ clearReclaimIntent,
8866
+ reclaimIntentIds,
8795
8867
  recordSubmissionVerdicts,
8796
8868
  applySubmissionVerdicts,
8869
+ finalizeLandedReclaims,
8797
8870
  JOB_READ_TIMEOUT_MS,
8798
8871
  answeredYieldSeqs,
8799
8872
  settleAnsweredAsk,