Fix NAS stop trap on LinkSpin: snapshot true delivery before every stop, reconcile active ads only, restart stopped ads from the true count

The InstantAdPay 2026-09-17 fix was never ported. nas.deactivate zeroed `remaining` with no snapshot, so served =
assigned - remaining read the undelivered rest as delivered, and reconcileNas read EVERY status: a paused ad was
charged for its zeroed remainder (up to its budget) and stopped ads showed their whole allocation as network views.
Now: nas.deactivate returns the served count read before the stop; ads.stopNas persists it on pause, end and
exhaustion; reconcileNas only looks at active campaigns; resume/top-up of a stopped ad uses the new nas.restart
(re-bases assigned on the true count) instead of a plain top-up. QA: qa/nas-stop.test.cjs (9), loginqueue 13,
refill 12, run.sh member passed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-27 07:56:56 -05:00
parent 2d0413037d
commit fd6f88cab7
3 changed files with 69 additions and 1 deletions
+14 -1
View File
@@ -102,9 +102,22 @@ async function readServed(nasAdId) {
}
// stop a syndicated ad (budget spent, paused, or expired) — remaining:0 halts serving.
// THE TRAP (InstantAdPay, found 2026-09-17, ported here 2026-10-27): NAS has no stop flag, so a stop zeroes `remaining`, and
// served = assigned - remaining then reads the undelivered rest as DELIVERED. The true figure is captured BEFORE the stop
// and returned; callers persist it (ads.stopNas) and never reconcile a stopped ad again.
async function deactivate(nasAdId) {
if (!enabled()) return { skipped: 'flag-off' };
let served = null;
try { const s = await readServed(nasAdId); if (s) served = s.served; } catch (e) {}
await q('UPDATE sponsorads SET remaining=0, EDate=NOW() WHERE ID=?', [Number(nasAdId)]);
return { ok: true, served };
}
// restart a STOPPED ad with `addImpressions` more, re-basing `assigned` on the TRUE served count recorded at the stop
// (a plain topUp would leave the zeroed remainder counted as served)
async function restart(nasAdId, trueServed, addImpressions, days) {
if (!enabled()) return { skipped: 'flag-off' };
const add = Math.max(0, Math.floor(Number(addImpressions) || 0)), base = Math.max(0, Math.floor(Number(trueServed) || 0));
await q('UPDATE sponsorads SET assigned=?, remaining=?, approved=1, EDate=DATE_ADD(NOW(), INTERVAL ? DAY) WHERE ID=?', [base + add, add, Number(days || 30), Number(nasAdId)]);
return { ok: true };
}
@@ -131,4 +144,4 @@ async function topUp(nasAdId, addImpressions, days) {
return { ok: true };
}
module.exports = { setClickUrl, updateCreative, enabled, pushCampaign, readServed, deactivate, topUp, impressionsPerCredit, nasKind };
module.exports = { setClickUrl, updateCreative, enabled, pushCampaign, readServed, deactivate, restart, topUp, impressionsPerCredit, nasKind };