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

fix(sharing): also prune orphaned inbox items pointing at deleted manifests

[._.]/ Adam Eivy0438fe446544
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index ef8c3fed09..4170749506 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -52,8 +52,11 @@
on the next `processBacklog` pass (which runs on server startup per
bucket): missing `senderInstanceId` fields are filled from the manifest
file on disk, then any item matching the local instance id is dropped
- with an `inbox-updated` socket event so the UI clears immediately.
- Touches: `server/services/sharing/importer.js`,
+ with an `inbox-updated` socket event so the UI clears immediately. The
+ same pass also drops *orphan* inbox items whose manifest file no longer
+ exists in the bucket — one-shot manifests that were superseded by a
+ subscription-mode share leave zombie inbox rows that `promoteInboxItem`
+ would 404 on anyway. Touches: `server/services/sharing/importer.js`,
`server/services/sharing/integration.test.js`.
- **`adoptImportedSubscription` no longer mislabels deliberately-subscribed
diff --git a/server/services/sharing/importer.js b/server/services/sharing/importer.js
index 34ce4de568..8187f80593 100644
--- a/server/services/sharing/importer.js
+++ b/server/services/sharing/importer.js
@@ -437,21 +437,30 @@ export async function processManifest(bucketId, manifestFilename) {
}
/**
- * Drop any inbox items the local instance itself produced. Pre-fix releases
- * could surface self-shares as pending imports; this prunes those on the
- * next backlog pass. Items predating the senderInstanceId field are
- * backfilled by reading the underlying manifest, so the pruning works for
- * legacy inbox state too.
+ * Drop inbox items that are no longer importable:
+ * - Self-authored: this instance produced the manifest (pre-fix releases
+ * surfaced these as pending imports). Items predating the
+ * `senderInstanceId` field are backfilled by reading the underlying
+ * manifest.
+ * - Orphan: the manifest file is gone from the bucket. `promoteInboxItem`
+ * would fail anyway; clearing them lets the UI advance instead of
+ * stranding zombie rows the user must hand-dismiss.
*/
async function pruneSelfAuthoredInbox(bucket, localInstanceId) {
- if (!localInstanceId) return { pruned: 0 };
const inbox = await readInbox(bucket.id);
const items = Array.isArray(inbox.items) ? inbox.items : [];
- if (items.length === 0) return { pruned: 0 };
+ if (items.length === 0) return { pruned: 0, orphaned: 0 };
let changed = false;
+ let orphaned = 0;
const kept = [];
for (const it of items) {
+ const manifestPath = join(bucket.path, 'manifests', it.manifestFilename);
+ if (!existsSync(manifestPath)) {
+ orphaned += 1;
+ changed = true;
+ continue;
+ }
let sender = it.senderInstanceId || null;
let backfilled = it;
if (!sender) {
@@ -468,16 +477,16 @@ async function pruneSelfAuthoredInbox(bucket, localInstanceId) {
}
kept.push(backfilled);
}
- const pruned = items.length - kept.length;
+ const pruned = items.length - kept.length - orphaned;
if (changed) {
inbox.items = kept;
await writeInbox(bucket.id, inbox);
- if (pruned > 0) {
+ if (pruned > 0 || orphaned > 0) {
sharingEvents.emit('inbox-updated', { bucketId: bucket.id });
- console.log(`🧹 sharing: bucket=${bucket.name} pruned ${pruned} self-authored inbox item(s)`);
+ console.log(`🧹 sharing: bucket=${bucket.name} pruned ${pruned} self-authored + ${orphaned} orphan inbox item(s)`);
}
}
- return { pruned };
+ return { pruned, orphaned };
}
/** On startup or registration, scan the manifests dir for unprocessed entries. */
diff --git a/server/services/sharing/integration.test.js b/server/services/sharing/integration.test.js
index ae8639e035..1165bc46e7 100644
--- a/server/services/sharing/integration.test.js
+++ b/server/services/sharing/integration.test.js
@@ -465,6 +465,37 @@ describe('sharing round-trip', () => {
expect(updates[0]).toMatchObject({ bucketId: bucket.id });
});
+ it('processBacklog prunes orphaned inbox items whose manifest file is no longer in the bucket', async () => {
+ const { sharingEvents } = await import('./importer.js');
+ const bucket = await buckets.createBucket({ name: 'OrphanBucket', path: tempBucket, mode: 'inbox' });
+
+ 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: [{
+ manifestId: 'orphan-mid',
+ manifestFilename: 'this-file-was-deleted-from-the-bucket.json',
+ kind: 'universe',
+ subscription: null,
+ source: 'remote-peer',
+ sourceBio: null,
+ senderInstanceId: 'some-other-peer',
+ createdAt: new Date().toISOString(),
+ receivedAt: new Date().toISOString(),
+ recordIds: ['u-orphan'],
+ assetCount: 0,
+ }] }));
+
+ const updates = [];
+ const onUpdate = (p) => updates.push(p);
+ sharingEvents.on('inbox-updated', onUpdate);
+ await importer.processBacklog(bucket.id);
+ sharingEvents.off('inbox-updated', onUpdate);
+
+ expect(await importer.listInbox(bucket.id)).toEqual([]);
+ expect(updates).toHaveLength(1);
+ });
+
it('refuses a manifest with a sharingSchemaVersion newer than local + emits incompatible event', async () => {
const { SHARING_SCHEMA_VERSION } = await import('./version.js');
const { sharingEvents } = await import('./importer.js');