Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review: persist rotation-orphan cull when freshness gate fires

[._.]/ Adam Eivyf98e781b5af8
diff --git a/server/services/sharing/importer.js b/server/services/sharing/importer.js
index 3643198b19..554dfdc989 100644
--- a/server/services/sharing/importer.js
+++ b/server/services/sharing/importer.js
@@ -455,6 +455,12 @@ async function applyInbox(bucket, manifest, manifestFilename, records) {
const inbox = await readInbox(bucket.id);
inbox.items = Array.isArray(inbox.items) ? inbox.items : [];
const sub = manifest.subscription;
+ // Tracks whether the rotation-orphan cull (or any other pre-write mutation)
+ // changed `inbox.items` so early-return paths below can still persist the
+ // pruned list. Without this, a cull followed by an `inbox-has-newer` /
+ // `already-in-inbox` short-circuit would silently drop the cull removals
+ // and the orphan rows would survive until the next non-stale arrival.
+ let inboxMutatedPreWrite = false;
if (sub?.recordKind && sub?.recordId) {
const senderId = manifest.senderInstanceId || null;
const incomingSource = manifest.source || null;
@@ -463,6 +469,7 @@ async function applyInbox(bucket, manifest, manifestFilename, records) {
// — a row with no display name can't be attributed to a peer identity.
if (incomingSource) {
const cullThresholdMs = Date.now() - ROTATION_CULL_MS;
+ const beforeLen = inbox.items.length;
inbox.items = inbox.items.filter((it) => {
if (!it.subscription) return true;
if (it.subscription.recordKind !== sub.recordKind) return true;
@@ -475,6 +482,7 @@ async function applyInbox(bucket, manifest, manifestFilename, records) {
console.log(`🧹 sharing: bucket=${bucket.name} culled rotation-orphan inbox row source="${incomingSource}" recordKind=${sub.recordKind} recordId=${sub.recordId} oldSender=${it.senderInstanceId || 'null'} newSender=${senderId || 'null'}`);
return false;
});
+ if (inbox.items.length !== beforeLen) inboxMutatedPreWrite = true;
}
const existing = inbox.items.find((it) => it.subscription
&& it.subscription.recordKind === sub.recordKind
@@ -489,12 +497,14 @@ async function applyInbox(bucket, manifest, manifestFilename, records) {
// when the incoming manifest is older than what we already have.
if (existing && existing.createdAt && manifest.createdAt
&& existing.createdAt > manifest.createdAt) {
+ if (inboxMutatedPreWrite) await writeInbox(bucket.id, inbox);
return { queued: false, reason: 'inbox-has-newer' };
}
if (existing) {
inbox.items = inbox.items.filter((it) => it !== existing);
}
} else if (inbox.items.some((it) => it.manifestId === manifest.id)) {
+ if (inboxMutatedPreWrite) await writeInbox(bucket.id, inbox);
return { queued: false, reason: 'already-in-inbox' };
}
inbox.items.push({
diff --git a/server/services/sharing/integration.test.js b/server/services/sharing/integration.test.js
index 560afc5519..0b6193582c 100644
--- a/server/services/sharing/integration.test.js
+++ b/server/services/sharing/integration.test.js
@@ -767,6 +767,81 @@ describe('sharing round-trip', () => {
expect(senders).toEqual(['inst-ME', 'inst-OTHER']);
});
+ it('persists a rotation-orphan cull even when the freshness gate forces an early inbox-has-newer return', async () => {
+ // Regression: the cull mutates inbox.items before the freshness gate
+ // runs. If an older manifest from the same sender arrives after the
+ // newer one is already in the inbox, the freshness gate short-circuits
+ // with `inbox-has-newer` — the cull removals on the OTHER (rotated)
+ // sender's row must still be persisted, otherwise the orphan survives
+ // until the next non-stale arrival.
+ const bucket = await buckets.createBucket({ name: 'RotateAndStaleBucket', path: tempBucket, mode: 'inbox' });
+ const universeBuilder = await import('../universeBuilder.js');
+ const u = await universeBuilder.createUniverse({ name: 'Multi-state Universe' });
+
+ const sixtyDaysAgo = new Date(Date.now() - 60 * 24 * 60 * 60 * 1000).toISOString();
+ const yesterday = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString();
+ const fs = await import('fs');
+ const inboxFile = join(tempData, 'sharing', 'inbox', `${bucket.id}.json`);
+ mkdirSync(join(tempData, 'sharing', 'inbox'), { recursive: true });
+ fs.writeFileSync(inboxFile, JSON.stringify({ items: [
+ // Rotation orphan — different sender, same source, 60 days old.
+ // Must be culled by the incoming manifest.
+ {
+ manifestId: 'rotation-orphan',
+ manifestFilename: `sub-universe-${u.id}-inst-OLD.json`,
+ kind: 'universe',
+ subscription: { recordKind: 'universe', recordId: u.id },
+ source: 'remote-peer',
+ sourceBio: null,
+ senderInstanceId: 'inst-OLD',
+ createdAt: sixtyDaysAgo,
+ receivedAt: sixtyDaysAgo,
+ recordIds: [u.id],
+ assetCount: 0,
+ },
+ // Same-sender row newer than the incoming manifest below — triggers
+ // the freshness gate's `inbox-has-newer` early return.
+ {
+ manifestId: 'fresh-newer',
+ manifestFilename: `sub-universe-${u.id}-inst-NEW.json`,
+ kind: 'universe',
+ subscription: { recordKind: 'universe', recordId: u.id },
+ source: 'remote-peer',
+ sourceBio: null,
+ senderInstanceId: 'inst-NEW',
+ createdAt: yesterday,
+ receivedAt: yesterday,
+ recordIds: [u.id],
+ assetCount: 0,
+ },
+ ] }));
+
+ // Synthesize an OLDER manifest from the same fresh sender so the
+ // freshness gate fires (existing.createdAt > manifest.createdAt).
+ const exp = await exporter.exportUniverse(u.id, bucket.id, {
+ subscription: { recordKind: 'universe', recordId: u.id },
+ });
+ simulateRemoteSender(tempBucket, exp.filename, 'inst-NEW');
+ const manifestPath = join(tempBucket, 'manifests', exp.filename);
+ const m = JSON.parse(fs.readFileSync(manifestPath, 'utf-8'));
+ m.source = 'remote-peer';
+ // Older than the pre-seeded `fresh-newer` row.
+ const twoDaysAgo = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000).toISOString();
+ m.createdAt = twoDaysAgo;
+ fs.writeFileSync(manifestPath, JSON.stringify(m));
+
+ const outcome = await importer.processManifest(bucket.id, exp.filename);
+ expect(outcome.outcome?.queued).toBe(false);
+ expect(outcome.outcome?.reason).toBe('inbox-has-newer');
+
+ // Despite the early return, the cull must have persisted: only the
+ // newer same-sender row remains.
+ const after = await importer.listInbox(bucket.id);
+ expect(after).toHaveLength(1);
+ expect(after[0].manifestId).toBe('fresh-newer');
+ expect(after[0].senderInstanceId).toBe('inst-NEW');
+ });
+
it('preserves a different-source row even when older than the rotation cull window', async () => {
const bucket = await buckets.createBucket({ name: 'OtherSourceBucket', path: tempBucket, mode: 'inbox' });
const universeBuilder = await import('../universeBuilder.js');