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
+73 -11
View File
@@ -11,8 +11,11 @@
// exists here keeps its sponsor and only gets empty fields filled. Joins from this site are skipped.
// wallet.connected filled only on a waiting account that still has no wallet (lsinbound fill rules). A claimed account
// links its own wallet here; the hub's wallet is not proof of ownership on InstantAdPay.
// sponsor.changed TODO: no clean path yet. lsinbound's follow rule needs sponsorSetAt / By / PrevEmail, which the hub
// view does not expose, and a 'contract' change is not one of the deliberate kinds. Ignored (logged).
// sponsor.changed ignored on purpose (2026-11-14): lsinbound's follow rule needs sponsorSetAt / By / PrevEmail, which the
// hub view does not expose, and someone the InstantAdPay contract holds never moves anyway.
// Rule 2 (2026-11-14): the sponsor written for an arrival is person.locks.iap's sponsor when that lock exists, never the
// plain network sponsor. QUIET: nothing on this path sends mail, a notice or a post, and lssync.js never
// pushes a hub waiting account to LinkSpin (qa/hubsync.test.cjs, mail audit).
// anything else ignored.
// Each seq is applied once (done map). A failing event answers 503 so the hub retries it; after 5 tries it is set aside
// in the log so one bad row cannot hold every later event back.
@@ -69,9 +72,9 @@ function verify(headers, raw) {
const want = sign(ts, raw);
return want.length === sig.length && crypto.timingSafeEqual(Buffer.from(want), Buffer.from(sig));
}
async function hubPost(p, body) {
async function hubPost(p, body, timeout) {
const raw = JSON.stringify(body); const ts = String(Date.now());
const r = await fetch(URL_() + p, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Hub-Site': SITE(), 'X-Hub-Ts': ts, 'X-Hub-Sig': sign(ts, raw) }, body: raw, signal: AbortSignal.timeout(15000) });
const r = await fetch(URL_() + p, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Hub-Site': SITE(), 'X-Hub-Ts': ts, 'X-Hub-Sig': sign(ts, raw) }, body: raw, signal: AbortSignal.timeout(timeout || 15000) });
let j = null; try { j = await r.json(); } catch (e) {}
return { status: r.status, body: j };
}
@@ -81,7 +84,11 @@ async function resolve(q) {
return (r.body && r.body.person) || null;
}
const doorOf = p => norm(p && p.firstDoor && p.firstDoor.site).replace(/[^a-z0-9_-]/g, '').slice(0, 30) || 'network';
const rowOf = p => ({ email: norm(p.email), username: p.username || null, wallet: p.wallet || null, sponsorEmail: (p.sponsor && p.sponsor.email) || null, joinedAt: Number(p.firstDoor && p.firstDoor.at) || null, profile: null });
// Rule 2 (Marty 2026-11-14): the sponsor written here is the one the InstantAdPay CONTRACT holds for them when it holds them
// (person.locks.iap), never the plain network sponsor, which may come from another contract where they spent money
const CONTRACT = 'iap';
const sponsorFor = p => { const l = p && p.locks && p.locks[CONTRACT]; if (l) return (l.sponsor && l.sponsor.email) || null; return (p && p.sponsor && p.sponsor.email) || null; };
const rowOf = p => ({ email: norm(p.email), username: p.username || null, wallet: p.wallet || null, sponsorEmail: sponsorFor(p), joinedAt: Number(p.firstDoor && p.firstDoor.at) || null, profile: null });
// what InstantAdPay and the hub each say the sponsor is; a difference is recorded, never forced
async function compare(email, hubSponsorEmail, how) {
@@ -102,8 +109,8 @@ async function memberJoined(ev) {
if (existing && !A.isUnclaimedShell(existing)) rows[0].wallet = null; // a real account links its own wallet here
// sponsors not on InstantAdPay yet come along as waiting accounts too (lsinbound builds them from rows in the same batch)
let p = first;
for (let i = 0; i < MAX_UP && p.sponsor && p.sponsor.email; i++) {
const se = norm(p.sponsor.email); if (seen.has(se) || await A.byEmail(se)) break;
for (let i = 0; i < MAX_UP && sponsorFor(p); i++) {
const se = norm(sponsorFor(p)); if (seen.has(se) || await A.byEmail(se)) break;
const up = await resolve({ email: se }); if (!up || !up.email || up.blocked) break;
seen.add(se); rows.push(rowOf(up)); doors[se] = doorOf(up); p = up;
}
@@ -126,8 +133,9 @@ async function walletConnected(ev) {
async function apply(ev) {
if (ev.type === 'member.joined') return memberJoined(ev);
if (ev.type === 'wallet.connected') return walletConnected(ev);
// TODO sponsor.changed: needs the deliberate-change record (at / by / previous sponsor) in the hub view before it can go
// through sponsorfollow's rule like LinkSpin's changes do. Until then the hub's change is ignored here.
// sponsor.changed is IGNORED here on purpose (checked 2026-11-14 for the switch-on): it would need the deliberate-change
// record (at / by / previous sponsor) to go through sponsorfollow's rule, and a member locked on the InstantAdPay contract
// must never move anyway (the contract pays whoever it holds). qa/hubsync.test.cjs proves a locked member stays put.
return 'ignored';
}
function prune() {
@@ -286,9 +294,63 @@ function start() {
const rec = setInterval(() => reconcile().catch(e => console.error('hubsync reconcile', e.message)), 10 * 60000);
for (const t of [interval, rec]) if (t.unref) t.unref();
}
// ---------- rule 3: the holding tank asks the hub (Marty 2026-11-14) ----------
// The tank never offers anyone locked on ANY network contract, or anyone the hub already knows a sponsor for. tank.js asks
// through its hooks (networkHeld, networkLocked, network); server.js wires them only with HUB_ENABLED, so with the flag off
// the tank works exactly as before. One /v1/locked {emails, detail} call per 2,000 emails, cached per email: fresh for 10
// minutes. CONSERVATIVE when the hub cannot be reached: the last answer stands for up to 1 hour, after that the person is
// held ('unknown', not offered, not moved to the tank, not auto-assigned) until the hub answers again. A held tank for a
// while is the safe side; offering someone a contract already holds is not. After a failed call the hub is left alone
// for 2 minutes (page views must not each wait for a timeout).
const HELD_FRESH_MS = 10 * 60000, HELD_STALE_MS = 60 * 60000, HELD_DOWN_MS = 2 * 60000, HELD_CHUNK = 2000;
const held = new Map(); let heldDownUntil = 0, heldBusy = Promise.resolve();
async function refreshHeld(emails, timeout) {
const run = heldBusy.then(async () => {
const now = Date.now(); const miss = emails.filter(e => { const c = held.get(e); return !c || now - c.at > HELD_FRESH_MS; });
if (!miss.length || now < heldDownUntil) return;
try {
for (let i = 0; i < miss.length; i += HELD_CHUNK) {
const part = miss.slice(i, i + HELD_CHUNK);
const r = await hubPost('/v1/locked', { emails: part, detail: true }, timeout || 5000);
if (r.status !== 200 || !r.body || !r.body.found || typeof r.body.found !== 'object') throw new Error('hub locked ' + r.status);
if (held.size > 60000) held.clear();
const at = Date.now(); for (const e of part) held.set(e, { at, row: r.body.found[e] || null });
}
} catch (e) { heldDownUntil = Date.now() + HELD_DOWN_MS; load().stats.heldErrors = (load().stats.heldErrors || 0) + 1; console.log('hubsync: hub /v1/locked unreachable:', e.message); }
});
heldBusy = run.catch(() => {}); return run;
}
const whyHeld = (c, now) => (!c || now - c.at > HELD_STALE_MS ? 'unknown' : c.row && c.row.locked ? 'locked' : c.row && c.row.sponsored ? 'sponsored' : null);
// accounts -> Map email -> 'locked' | 'sponsored' | 'unknown' for the ones the tank must not offer
async function heldAmong(accts, opts) {
const out = new Map(); if (!R || !on()) return out;
const emails = [...new Set((accts || []).map(a => norm(a && a.email)).filter(Boolean))]; if (!emails.length) return out;
await refreshHeld(emails, opts && opts.timeout);
const now = Date.now(); for (const e of emails) { const w = whyHeld(held.get(e), now); if (w) out.set(e, w); }
return out;
}
// release, rescue and expiry moves: 'locked' (on any network contract) | 'unknown' (hub not answering) | null
async function lockedAnywhere(email) {
if (!R || !on()) return null; const e = norm(email); if (!e) return null;
await refreshHeld([e]); const w = whyHeld(held.get(e), Date.now());
return w === 'locked' || w === 'unknown' ? w : null;
}
// the sponsor the hub knows for a tank member, for tank.alignToNetwork: this contract's lock first, else the network sponsor
// OFF unless HUB_TANK_ALIGN=on: aligning moves sponsors (and on LinkSpin a linked wallet then has its payouts switched on, which
// locks that sponsor on the contract), so the first run over a full tank is Marty's deliberate step, not a side effect of the
// deploy. While it is off the people the hub knows a sponsor for are simply held out of the tank (rule 3).
const alignOn = () => /^(1|true|on|yes)$/i.test(String(process.env.HUB_TANK_ALIGN || '').trim());
async function hubSponsor(email) {
if (!R || !on() || !alignOn()) return null; const e = norm(email); if (!e) return null;
await refreshHeld([e]); const c = held.get(e); if (!c || !c.row || Date.now() - c.at > HELD_STALE_MS) return null;
const l = c.row.locks && Object.prototype.hasOwnProperty.call(c.row.locks, CONTRACT) ? c.row.locks[CONTRACT] : undefined;
const sp = norm(l !== undefined ? l : c.row.sponsorEmail);
return sp && sp !== e ? { sponsorEmail: sp, via: l !== undefined ? 'network hub, InstantAdPay contract' : 'network hub' } : null;
}
function adminView() {
const s = load();
return Object.assign({ enabled: enabled(), on: on(), site: SITE(), url: URL_(), since: s.since, outbox: s.outbox.length, retrying: Object.keys(s.tries).length,
disagree: Object.keys(s.disagree).length }, s.stats, { disagreements: Object.entries(s.disagree).slice(-25).map(([email, d]) => Object.assign({ email }, d)), recent: s.log.slice(-25).reverse() });
disagree: Object.keys(s.disagree).length, tankCache: held.size, tankHubDownUntil: heldDownUntil || null }, s.stats, { disagreements: Object.entries(s.disagree).slice(-25).map(([email, d]) => Object.assign({ email }, d)), recent: s.log.slice(-25).reverse() });
}
module.exports = { init, enabled, on, receive, touch, wallet, onChain, flush, reconcile, start, adminView, _sign: sign };
module.exports = { init, enabled, on, receive, touch, wallet, onChain, flush, reconcile, start, adminView, heldAmong, lockedAnywhere, hubSponsor, _sign: sign, _heldReset: () => { held.clear(); heldDownUntil = 0; }, _heldAge: ms => { for (const c of held.values()) c.at -= ms; } };