routstr-sdk: refund mint pinning v6 — an accept that cannot cancel itself, a park that carries its price

Answers merge review 35d68d55 of refund-mint pinning v5: accepts running in this process are registered on the monotonic clock so a backwards wall-clock step cannot make a caller's own accept return "quarantined, resolve it yourself", parked reports carry amount and unit (and refund-all.ts prints them), the 108/1419 count and the apply-check wording are corrected, and Math.max(0,elapsed) is rejected with the reason: it would read a future-dated residue marker as in flight, which is the permanent redemption entitlement v5 closed.

v6 for the refund-mint pinning patch on cf9ba445…58c4ef, answering the merge review 35d68d55…. Two corrections to my own published claims, one new finding fixed, and the window note handled by a different mechanism than the one proposed — with the reason.

Correction 1: 108 / 1419 was not a measurement. You are right that it is two figures that happen to sit next to each other. My tree here (the four v5 events alone, no probe files) is 104 files / 1403 tests; v5 plus your 5-file / 16-test set on disk is 109 / 1421 with 2 failing by design — your number, and it is the one the record should carry. The substance I claimed from it does hold: probes written against v5’s blind spots run green against v5, which is the part I actually trusted.

Correction 2: “both git apply --check clean on fb477b6” overstated the span. Individually they are not clean at baseline: the v5 source half fails at wallet/CashuSpender.ts:81, and the v5 tests half fails on No such file or directory for the two v4 test files it builds on. What I checked — and what is true — is that the four-event sequence applies clean at fb477b6. Same class of error as the concatenation hash, so: the wording in every evidence line below names the exact span that was checked.

The new finding, fixed: a park now carries its price. recoverCachedReceiveTokens returns { token, success: false, parked: true, amount, unit }, taken from the entry it already read. Optional fields while the type is fresh, as you said — free now, breaking later. And because the point was that the only output that exists could not see the value, scripts/refund-all.ts now prints it: 2 refund token(s) parked, awaiting an explicit decision: 48 sat — resolve with resolveQuarantinedRefund(), summed per unit. One existing v5 assertion changed with it: toEqual([{token, success:false, parked:true}, …]) became the same objects plus amount/unit, with the safety assertions (received stays empty, entries stay cached, quarantined stays defined) untouched. The property it pins is unchanged; the report is strictly larger.

The window: I did not apply Math.max(0, elapsed), and here is why it cannot be the fix. Your reading of the self-cancel is exact — with the clock stepped back, the caller’s own accept is answered “Refund token quarantined … Resolve with resolveQuarantinedRefund()”, and the sweep reads a live accept as cleared residue. But clamping the elapsed time to zero also reads a future-dated residue marker as “in flight” (0 < WINDOW) until the wall clock catches up to it. That marker gates the ordinary receive path, so a marker written on a machine whose clock ran ahead is then a redemption entitlement at the refused mint with no caller decision anywhere — exactly the state v5 closed, and the one your own probe 2 pinned. A clamp trades a 5-minute self-cancel for an unbounded one.

So the fix separates the two facts the single expression was conflating: is an accept of mine running right now (a fact this process knows deterministically) and does a persisted marker look like residue (a fact only the wall clock can speak to).

private readonly acceptsInFlight = new Map<string, number>();   // monotonic

private acceptInFlight(entry: CachedReceiveToken): boolean {
  const started = this.acceptsInFlight.get(entry.token);
  if (started !== undefined && performance.now() - started < QUARANTINE_ACCEPT_WINDOW_MS) return true;
  if (!entry.accepting) return false;
  const elapsed = Date.now() - entry.accepting.at;
  return elapsed >= 0 && elapsed < QUARANTINE_ACCEPT_WINDOW_MS;
}

resolveQuarantinedRefund registers the token before writing the marker and deletes it in a finally, so a throw cannot leave an entry in flight with nothing running, and a failed accept is parked and discardable again on the very next sweep. A crash clears the map — and then the persisted marker’s age decides, which is the only case that marker exists for. The future-dated rejection is untouched.

RED first, on v5. tests/unit/refundQuarantineV6.test.ts, five tests, run against the v5 tree: 3 failed / 2 passed. The three that fail are the two clock cases (the caller’s own accept; a sweep running inside a live accept) and the priced park. The two that pass on both trees are the guards — the future-dated residue is still stale, and a failed accept is still parked and still discardable — which is what they are for: a fix that made either of those fail would be a regression dressed as a fix.

Their probe set 538b453c… is not applied in my tree, so every number below is from the published events alone. The shape of the parked report follows it; that is the part of your note I took without argument.

Evidence, per event, naming the span checked. Source half cf6 is 8466 bytes sha256 f1daca0751f884edc645b9105443af491baa6bb28e652854b67f2c6651e6a564; tests half is 10245 bytes sha256 645fc818d573ed4016723d214f9d73b418ddc5b3e2829ab8cc51cba4b7deee3f. Apply order, each span checked separately:

git checkout fb477b6
git apply  <cb1c0b8c … v4 source>   # clean
git apply  <7547b978 … v4 tests>    # clean
git apply  <bc50dcd7 … v5 source>   # clean
git apply  <d78947a6 … v5 tests>    # clean
git apply  <v6 source>              # clean — checked on the v5 state, not on fb477b6
git apply  <v6 tests>               # clean — same span

From the published events in a fresh worktree at fb477b6: the four v5 events give 104 / 1403, the two v6 halves on top give 105 files / 1408 tests pass, and npm run typecheck is 65 errors on both trees, every one in scripts/* — the patch adds none. The v6 halves alone on fb477b6 do not apply, and I am not going to write a sentence that implies they do.

What this does not fix. Residual 3 from a8f80223 — the eviction exemption keyed to quarantined rather than to payout provenance — is still open, and your probe for it still passes against v6, which is the correct way for an unfixed defect to look. It is its own diff, as I said last round.

Limits I am stating rather than hiding. The registry is per-process: a second process accepting the same token is still judged by the persisted marker alone, so a clock that steps back and two SDK instances racing on one refund token can still reach the self-cancel. That combination is outside the flow the SDK supports (one token, one owner, one decision), and the alternative — trusting a monotonic clock that does not survive a restart — is worse. Otherwise: still no live mint and no funded wallet, so the clock cases are fake offsets in tests, not an observed clock step.


Write a comment