Hub switch-on (Marty 2026-11-14). Rule 2: a hub arrival (and a sponsor climbed for it) gets person.locks.iap's sponsor when the InstantAdPay contract holds them, never the plain network sponsor; sponsor.changed stays ignored. Quiet: lssync never pushes a hub waiting account to LinkSpin (its registry route would switch payouts on and email them). Rule 3: the holding tank (waiting, release, expiry, rescue, readyTick, autoAssign) leaves out anyone the hub reports locked on any contract or sponsored; /v1/locked detail cached 10 min, last answer kept 1 h, after that held (unknown). Alignment to the hub sponsor only with HUB_TANK_ALIGN=on. Needs network-hub b44dba6. qa/hubsync.test.cjs 19/19 (mail audit with recording mailer, messages, fetch and https), iap-qa member: ALL PASSED

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-11-14 08:51:58 -06:00
parent a01d6f2e8c
commit 836c7b17c2
5 changed files with 231 additions and 21 deletions
+37 -6
View File
@@ -86,11 +86,26 @@ const countAll = async email => (await impl().countFor(email)) + remoteCount(ema
const mask = e => String(e || '').replace(/^(.).*(@.*)$/, '$1***$2');
const nameOf = a => a.username ? '@' + a.username : (a.memberId ? 'member #' + a.memberId : mask(a.email));
const inTank = a => !a.sponsorRef && !a.memberId; // arrived with no sponsor (or fell back), still free
// Rule 3 (Marty 2026-11-14): never offer anyone locked on ANY network contract or whom the network hub already knows a sponsor
// for (hooks.networkHeld = hubsync.heldAmong, wired only with HUB_ENABLED). A hook that throws holds everyone it was asked
// about: the conservative side. No hook (flag off, tests) = nothing held, the tank as before.
const lc = v => String(v || '').trim().toLowerCase();
async function heldSet(accts) {
if (!hooks.networkHeld || !accts.length) return new Map();
try { return (await hooks.networkHeld(accts)) || new Map(); } catch (e) { console.error('tank networkHeld', e.message); return new Map(accts.map(a => [lc(a.email), 'unknown'])); }
}
// release / rescue / expiry: 'locked' | 'unknown' | null (hooks.networkLocked = hubsync.lockedAnywhere)
const HELD_WHY = { locked: 'they are locked on a network contract', sponsored: 'it already knows a sponsor for them', unknown: 'nothing (it is not answering)' };
async function lockedWhy(email) {
if (!hooks.networkLocked) return null;
try { return (await hooks.networkLocked(email)) || null; } catch (e) { return 'unknown'; }
}
// waiting list: newest sign-in first so a live one is easy to spot
async function waiting() {
const all = await accounts.listAll(5000);
const rows = all.filter(a => inTank(a) && !remoteOpen(a.email)); // one network tank: someone adopted on LinkSpin is not waiting here
const cand = all.filter(a => inTank(a) && !remoteOpen(a.email)); // one network tank: someone adopted on LinkSpin is not waiting here
const held = await heldSet(cand); const rows = cand.filter(a => !held.has(lc(a.email))); // rule 3: locked or sponsored elsewhere
// what each of them has earned by surfing on their own, no sponsor (Marty, 2026-09-29), so an adopter can spot the workers
const tot = ads && ads.earnedTotals ? await ads.earnedTotals(rows.map(a => a.email)) : {};
return rows.map(a => ({ email: a.email, name: nameOf(a), username: a.username || null, joined: a.created, lastSeen: a.lastSeen || 0,
@@ -178,6 +193,8 @@ async function release(ownerEmail, directEmail) {
const toks = [owner.code, owner.username, owner.memberId ? String(owner.memberId) : null].filter(Boolean).map(String);
if (!toks.includes(String(direct.sponsorRef || ''))) return { error: 'That member is not in your line.' };
if (direct.memberId) return { error: 'That member has already bought; on-chain sponsorship cannot move.' };
const lk = await lockedWhy(d); // rule 3: someone a network contract holds never goes to the tank
if (lk) return { error: lk === 'locked' ? nameOf(direct) + ' is locked to a sponsor on a network contract, so they cannot go to the holding tank.' : 'The network record for ' + nameOf(direct) + ' cannot be checked right now. Try again in a few minutes.' };
// picked up from the tank recently? then it is not theirs to drop yet (Bradley churned three in 80 seconds, 2026-09-13)
const openAds = (await impl().open(null)).filter(ad => ad.adoptee === d);
let young = openAds.find(ad => Date.now() - ad.ts < HOLD_MS);
@@ -208,6 +225,9 @@ async function sweep() {
if (!a) { await impl().setStatus(ad.id, 'expired'); continue; }
if (a.address || a.memberId) { await impl().setStatus(ad.id, 'done'); done++; continue; }
if (await impl().countFor(ad.adoptee) >= MAX_ADOPTIONS) { await impl().setStatus(ad.id, 'expired'); kept++; continue; }
const lk = await lockedWhy(ad.adoptee); // rule 3: a person a network contract holds stays with the adopter, never back in the tank
if (lk === 'unknown') continue; // the hub is not answering: try again next hour
if (lk === 'locked') { await impl().setStatus(ad.id, 'expired'); kept++; continue; }
await setSponsor(ad.adoptee, '', 'expire', 'adoption window ended');
await impl().setStatus(ad.id, 'expired'); back++;
}
@@ -272,11 +292,14 @@ async function markContacted(sponsorEmail, leadEmail) {
async function rescueSweep() {
const now = Date.now();
let warned = 0, rescued = 0, dead = 0;
for (const a of await accounts.listAll(5000)) {
const all = await accounts.listAll(5000);
await heldSet(all.filter(a => !a.memberId && !a.address && a.sponsorRef)); // one batched hub call warms the cache for lockedWhy below
for (const a of all) {
if (a.memberId || a.address || !a.sponsorRef) continue; // bound, wallet, or already in the tank
if (await impl().hasRecord(a.email)) continue; // adopted / rescued before: the tank's window rules
const pv = store && store.get(a.email);
if (remoteCount(a.email) || (pv && pv.by !== 'admin')) continue; // adopted / released / rescued on LinkSpin: the tank's window rules there
if (await lockedWhy(a.email)) continue; // rule 3: locked on a network contract (or the hub is not answering): no warning, no move
const sp = await sponsorOf(a);
if (!sp) { // dead sponsor link: nobody can ever coach them, straight to the tank after a day
if (now - (a.created || 0) < 86400000) continue;
@@ -385,7 +408,7 @@ async function previousSponsors(email) {
async function alignToNetwork(a) {
if (!a || !inTank(a) || !hooks.network) return null;
const e = String(a.email || '').toLowerCase(); if (!e) return null;
const n = hooks.network(e, a.address || null); if (!n || !n.sponsorEmail) return null;
const n = await Promise.resolve(hooks.network(e, a.address || null)).catch(() => null); if (!n || !n.sponsorEmail) return null; // the hub's answer is async
const spEmail = String(n.sponsorEmail).toLowerCase(); if (spEmail === e) return null;
if ((await previousSponsors(e)).has(spEmail)) return null; // never back to someone they were released or rescued from
const sp = await accounts.byEmail(spEmail);
@@ -396,7 +419,7 @@ async function alignToNetwork(a) {
for (let i = 0; cur && i < 60; i++) { if (cur.email === e || seen.has(cur.email)) return null; seen.add(cur.email); cur = await accounts.sponsorOf(cur.email); }
const fresh = await accounts.byEmail(e); if (!fresh || !inTank(fresh)) return null;
const r = await accounts.setSponsorRef(e, token); if (r && r.error) return null;
const st = RS.load(); const why = 'aligned to network sponsor ' + nameOf(sp) + ' from LinkSpin (' + (n.via || 'email') + ')';
const st = RS.load(); const why = 'aligned to network sponsor ' + nameOf(sp) + (/hub/.test(n.via || '') ? ' (' + n.via + ')' : ' from LinkSpin (' + (n.via || 'email') + ')');
st.aligned.push({ email: e, to: sp.email, toName: nameOf(sp), via: n.via || 'email', at: clock(), why });
if (st.aligned.length > 300) st.aligned = st.aligned.slice(-300);
if (st.ready[e] && !st.ready[e].done) { st.ready[e].done = clock(); st.ready[e].aligned = sp.email; }
@@ -447,8 +470,14 @@ async function autoAssignRun() {
due.push([e, rec, a]);
}
due.sort((x, y) => x[1].readyAt - y[1].readyAt);
const netHeld = await heldSet(due.map(x => x[2]));
let pool = null;
for (const [e, rec, a] of due) {
if (netHeld.has(e)) { // rule 3: never auto-assigned; the admin hears once (their network sponsor has no usable account here yet, or the hub is not answering)
out.held++;
if (!rec.networkHeldAlerted) { rec.networkHeldAlerted = clock(); RS.save(); if (hooks.adminAlert) { try { await hooks.adminAlert(BRAND + ' holding tank: ' + nameOf(a) + ' (' + e + ') is held and NOT auto-assigned: the network hub says ' + (HELD_WHY[netHeld.get(e)] || netHeld.get(e)) + ', and that sponsor could not be used here. Set their sponsor in Admin if it should be someone here.'); } catch (err) {} } }
continue;
}
if (!pool) pool = await rotation(e);
let done = null;
for (const c of pool) {
@@ -481,10 +510,12 @@ async function autoAssignRun() {
// run the safety net. `fresh` = members who became ready and were not announced yet (server.js posts them).
async function readyTick() {
const st = RS.load(); const fresh = []; let aligned = 0;
for (const a of await accounts.listAll(5000)) {
if (!inTank(a)) continue;
const tankers = (await accounts.listAll(5000)).filter(inTank);
const netHeld = await heldSet(tankers); // one batched hub call; alignToNetwork below reads the same cache
for (const a of tankers) {
if (await alignToNetwork(a).catch(() => null)) { aligned++; continue; }
if (remoteOpen(a.email)) continue;
if (netHeld.has(lc(a.email))) continue; // rule 3: not announced, no 24-hour clock while the network holds them
const rec = markReady(a);
// linkedNow: the member was on the site when they became ready, so they linked the wallet just now; otherwise the
// wallet predates this tank visit (e.g. a dormant member returned by a sponsor; Marty 2026-10-29: the notice read as a