Sponsor follow + one network holding tank + Sponsor check (Marty, 2026-10-22, approved 1-3)

Deliberate sponsor changes (tank adoption, dormant-lead rescue, release, adoption window ending, admin edit) are now
recorded (sponsorfollow.json: when, how, the sponsor before, origin) and travel on the LinkSpin sync rows as
sponsorSetAt/By/PrevEmail/Origin. A deliberate change from LinkSpin is applied here (a clear back to the tank included)
only when the member is not bound on the InstantAdPay contract (memberId or wallet on chain), it is newer than our own
deliberate change, and both sides agreed before it (or we have no sponsor). Bound, diverged and race cases change
nothing and are logged. Race within 15 min: the earlier change wins on both. Ordinary bindings keep the first-platform
rule. Accounts with no record behave as before; rows without the new facts hash exactly as before (no resend on deploy).

Network tank: rows carry this site's own adoptions of a person; the waiting list hides someone adopted on LinkSpin, the
two-adoptions limit counts both sites, the 3-day hold applies to a LinkSpin adoption too, follows close local open
adoptions quietly (no chat, mail or feed post).

Admin > Members > Sponsor check: shared members whose sponsor differs or is one-sided, each side's when/how, chain
binding, and Use InstantAdPay's / Use LinkSpin's sponsor (applied on the other site, refused when bound there).
Partner endpoints /api/partner/sponsor-facts and /api/partner/sponsor-apply (same HMAC as the registry rows).

Tests: qa/sponsorfollow.test.cjs 27/27, qa/sponsorfollow-e2e.mjs 20/20 (local IAP + local LinkSpin, throwaway data),
QA member walk 0 bugs. No sponsor was changed on production.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-22 09:05:07 -05:00
parent 06b0924651
commit 4e51470cf5
10 changed files with 954 additions and 35 deletions
+35 -8
View File
@@ -16,7 +16,15 @@
// then accounts.listAll leaves unclaimed shells out of every sweep that mails, counts or adopts members.
// - Loop guard: each email is muted in lssync while its row is applied, then lssync records the result as already
// pushed when it equals what LinkSpin sent, so it is not bounced back (see lssync.release). No-op rows change nothing.
// - SPONSOR FOLLOW (2026-10-22, sponsorfollow.js): a row that carries a DELIBERATE change made on LinkSpin (sponsorSetBy
// adopt / rescue / release / expire / admin) is the one exception to the first-platform rule: it is applied, a clear
// back to the tank included, when the member is not bound on InstantAdPay's contract (no memberId, not on chain), the
// change is newer than our own deliberate change, and the two sides agreed before it (or we have no sponsor). Bound,
// diverged and race cases change nothing here and are logged for Admin > Members > Sponsor check. Applied quietly.
// - NETWORK TANK: a row's `tank` (LinkSpin's own adoptions of the person) is kept as remote state, so our waiting list
// hides someone adopted on LinkSpin and the two-adoptions limit counts both sites.
'use strict';
const SF = require('./sponsorfollow');
const fs = require('fs');
const path = require('path');
@@ -35,7 +43,7 @@ function load() {
}
function save() { try { const tmp = FILE() + '.tmp'; fs.writeFileSync(tmp, JSON.stringify(load())); fs.renameSync(tmp, FILE()); } catch (e) { console.error('lsinbound save', e.message); } }
function note(ev) { const s = load(); s.log.push(Object.assign({ ts: Date.now() }, ev)); if (s.log.length > 300) s.log = s.log.slice(-300); }
function init(deps) { R = deps; load(); } // { dataDir, accounts, lssync }
function init(deps) { R = deps; load(); } // { dataDir, accounts, lssync, follow (sponsorfollow glue), store (sponsorfollow store) }
function clean(x) {
if (!x || typeof x !== 'object') return null;
@@ -44,9 +52,18 @@ function clean(x) {
const wallet = /^0x[0-9a-f]{40}$/.test(norm(x.wallet)) ? norm(x.wallet) : null;
let sponsorEmail = norm(x.sponsorEmail); if (!EMAIL_RE.test(sponsorEmail) || sponsorEmail === email) sponsorEmail = null;
const profile = x.profile && typeof x.profile === 'object' ? x.profile : null;
return { email, username, wallet, sponsorEmail, joinedAt: Number(x.joinedAt) || null, profile };
const out = { email, username, wallet, sponsorEmail, joinedAt: Number(x.joinedAt) || null, profile };
if (SF.isDeliberate(x.sponsorSetBy) && Number(x.sponsorSetAt) > 0) { // a deliberate change on LinkSpin (sponsorfollow.js)
out.sponsorSetAt = Number(x.sponsorSetAt); out.sponsorSetBy = String(x.sponsorSetBy);
const pv = norm(x.sponsorPrevEmail); out.sponsorPrevEmail = EMAIL_RE.test(pv) ? pv : null;
out.sponsorSetOrigin = norm(x.sponsorSetOrigin).replace(/[^a-z]/g, '').slice(0, 20) || 'linkspin';
}
if (Object.prototype.hasOwnProperty.call(x, 'tank')) out.tank = SF.cleanTank(x.tank); // null = LinkSpin has no adoption of them now
return out;
}
const tokenOf = a => a.username || a.code;
// released / rescued / expired back to the tank on purpose here: an ordinary row must not refill the sponsor (sponsorfollow.js)
const emptiedOnPurpose = email => { const pv = R.store && R.store.get(email); return !!(pv && SF.isDeliberate(pv.by) && !pv.sponsorEmail); };
function conflict(sum, email, kind, detail) { sum.conflicts++; note({ type: 'conflict', email, kind, detail: String(detail || '').slice(0, 120) }); }
// would binding `email` under `sp` close a loop (sp already sits below email)?
@@ -94,7 +111,14 @@ async function place(email, depth, ctx) {
ctx.done.add(email); R.lssync.mute(email);
let a = await A.byEmail(email); let wrote = false;
if (!r) return a;
if (!a) {
const inc = SF.incomingOf(r);
if (a && inc && R.follow) {
// a deliberate sponsor change made on LinkSpin: the follow rule decides (never on a member bound here)
const d = await R.follow.applyIncoming(email, r, { sponsorAccount: em => sponsorAccount(em, depth, ctx) });
if (d.apply) { ctx.sum.followed++; wrote = true; delete s.pending[email]; note({ type: 'follow', email, by: r.sponsorSetBy, sponsor: r.sponsorEmail || null, action: d.action }); }
else if (d.action !== 'same' && d.action !== 'ordinary') { ctx.sum.followKept++; note({ type: 'follow-kept', email, by: r.sponsorSetBy, action: d.action, detail: String(d.reason || '').slice(0, 120) }); }
a = await A.byEmail(email);
} else if (!a) {
let tok = '';
if (r.sponsorEmail) {
const sp = await sponsorAccount(r.sponsorEmail, depth, ctx);
@@ -104,7 +128,9 @@ async function place(email, depth, ctx) {
if (!x || x.error) { conflict(ctx.sum, email, 'create', x && x.error); return null; }
a = x.account;
if (x.created) { ctx.sum.created++; wrote = true; note({ type: 'shell', email, sponsor: tok || null, pending: tok ? null : (r.sponsorEmail || null) }); }
} else if (A.isUnclaimedShell(a) && !a.sponsorRef && r.sponsorEmail) {
// a new waiting account built from a deliberate change carries the same record, so its row does not echo back
if (x.created && inc && R.store && (!r.sponsorEmail || tok)) R.store.record(email, { at: inc.at, by: inc.by, sponsorEmail: inc.sponsorEmail, prevEmail: inc.prevEmail, origin: inc.origin || 'linkspin' });
} else if (A.isUnclaimedShell(a) && !a.sponsorRef && r.sponsorEmail && !emptiedOnPurpose(email)) {
// our own waiting account, still without a sponsor: this row (or a later one) names one
const sp = await sponsorAccount(r.sponsorEmail, depth, ctx);
if (sp && sp.email !== email && !(await wouldCycle(email, sp))) { await A.setSponsorRef(email, tokenOf(sp)); delete s.pending[email]; ctx.sum.sponsored++; wrote = true; }
@@ -119,7 +145,7 @@ async function resolvePending(ctx) {
const s = load(); const A = R.accounts;
for (const [email, spEmail] of Object.entries(s.pending)) {
const a = await A.byEmail(email);
if (!a || !A.isUnclaimedShell(a) || a.sponsorRef) { delete s.pending[email]; continue; } // claimed or bound meanwhile: leave it
if (!a || !A.isUnclaimedShell(a) || a.sponsorRef || emptiedOnPurpose(email)) { delete s.pending[email]; continue; } // claimed, bound or emptied on purpose meanwhile: leave it
const sp = await sponsorAccount(spEmail, 0, ctx); if (!sp) continue;
if (sp.email === email || await wouldCycle(email, sp)) { delete s.pending[email]; conflict(ctx.sum, email, 'sponsor-cycle', spEmail); continue; }
R.lssync.mute(email); ctx.done.add(email);
@@ -129,9 +155,10 @@ async function resolvePending(ctx) {
}
async function handle(rows) {
const s = load(); s.stats.batches++;
const sum = { rows: 0, noop: 0, created: 0, filled: 0, sponsored: 0, conflicts: 0, pendingSponsor: 0, errors: 0 };
const sum = { rows: 0, noop: 0, created: 0, filled: 0, sponsored: 0, conflicts: 0, pendingSponsor: 0, errors: 0, followed: 0, followKept: 0, tank: 0 };
const ctx = { sum, done: new Set() }; const batch = [];
for (const x of rows || []) { const r = clean(x); if (!r) continue; sum.rows++; s.rows[r.email] = r; batch.push(r); }
if (R.store) for (const r of batch) if (Object.prototype.hasOwnProperty.call(r, 'tank')) { R.store.setRemote(r.email, r.tank); sum.tank++; } // network tank
try {
for (const r of batch) {
try { await place(r.email, 0, ctx); }
@@ -141,9 +168,9 @@ async function handle(rows) {
} finally {
for (const email of ctx.done) await R.lssync.release(email, s.rows[email]); // loop guard: equal results are not echoed
}
s.stats.received += sum.rows; for (const k of ['noop', 'created', 'filled', 'sponsored', 'conflicts', 'errors']) s.stats[k] += sum[k];
s.stats.received += sum.rows; for (const k of ['noop', 'created', 'filled', 'sponsored', 'conflicts', 'errors', 'followed', 'followKept']) s.stats[k] = (s.stats[k] || 0) + sum[k];
s.last = Object.assign({ at: Date.now() }, sum); save();
return sum;
return Object.assign({ proto: SF.PROTO }, sum); // proto 2: this side understands sponsor follow + network tank facts
}
// batches are applied one at a time (two overlapping posts must not both create the same sponsor shell)
function receive(rows) { const run = chain_.then(() => handle(rows)); chain_ = run.catch(() => {}); return run; }