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 (copilot): importer cursor no longer advances on failed insert (silent data drop); defer getChecksum exclude-set resolution to cache-miss path

Adam Eivye4560d9071b5
diff --git a/server/services/dataSync.js b/server/services/dataSync.js
index 1347a80f32..317d6c2a1a 100644
--- a/server/services/dataSync.js
+++ b/server/services/dataSync.js
@@ -806,7 +806,6 @@ async function resolveExcludeSet(category, forPeerId) {
export async function getChecksum(category, { forPeerId } = {}) {
const cat = CATEGORIES[category];
if (!cat) return null;
- const exclude = await resolveExcludeSet(category, forPeerId);
const paths = CHECKSUM_PATHS[category];
if (paths) {
const fingerprints = await readFingerprintMap(paths);
@@ -819,10 +818,16 @@ export async function getChecksum(category, { forPeerId } = {}) {
if (cached && fingerprintsEqual(cached.fingerprints, fingerprints)) {
return { checksum: cached.checksum };
}
+ // Cache miss only: resolve the per-peer exclude-set (a dynamic peerSync
+ // import + subscription read) and build the fresh scoped snapshot. The
+ // cache-hit path above never uses `exclude`, so deferring it here saves
+ // that I/O on every poll that hits the cache.
+ const exclude = await resolveExcludeSet(category, forPeerId);
const snapshot = await cat.getSnapshot({ exclude });
checksumCache.set(cacheKey, { fingerprints, checksum: snapshot.checksum });
return { checksum: snapshot.checksum };
}
+ const exclude = await resolveExcludeSet(category, forPeerId);
const snapshot = await cat.getSnapshot({ exclude });
return { checksum: snapshot.checksum };
}
diff --git a/server/services/sharing/importer.js b/server/services/sharing/importer.js
index 6bf7cf1391..a1f0abff07 100644
--- a/server/services/sharing/importer.js
+++ b/server/services/sharing/importer.js
@@ -402,6 +402,11 @@ async function readReferencedRecords(bucketPath, manifest) {
async function applyAutoMerge(bucket, manifest, records, { availableAssetKeys = null } = {}) {
let applied = 0;
const overridden = [];
+ // Records whose insert threw an UNEXPECTED (non-duplicate) error — they did
+ // NOT land. Surfaced as a pending condition (like legacyCanonPendingFailures)
+ // so processManifest leaves the cursor un-advanced and the watcher retries,
+ // instead of marking the manifest processed and silently dropping the record.
+ const failedInserts = [];
const inboundSub = manifest.subscription?.recordKind && manifest.subscription?.recordId
? { bucketId: bucket.id, recordKind: manifest.subscription.recordKind, recordId: manifest.subscription.recordId }
: null;
@@ -422,12 +427,20 @@ async function applyAutoMerge(bucket, manifest, records, { availableAssetKeys =
// into the bucket we're importing from. The peer-sync propagation that
// resurrection ALSO triggers (autoSubscribeRecordToAllPeers) is a separate
// mechanism and stays intact — only the bucket re-export is suppressed.
- await withReexportSuppressed(suppressKind, suppressId, () => insertFn(record)).catch((err) => {
- // Duplicate id is benign — a parallel manifest already inserted it.
- if (err?.code?.endsWith('_DUPLICATE')) return null;
- console.log(`⚠️ sharing.importer: insertWithId(${kind}=${record.id}) failed: ${err.message}`);
- });
- applied++;
+ const insertOk = await withReexportSuppressed(suppressKind, suppressId, () => insertFn(record))
+ .then(() => true)
+ .catch((err) => {
+ // Duplicate id is benign — a parallel manifest already inserted it, so
+ // the record IS present; count it as applied.
+ if (err?.code?.endsWith('_DUPLICATE')) return true;
+ // Unexpected failure: the record did NOT land. Record it so the
+ // manifest stays pending and retries — incrementing `applied` and
+ // advancing the cursor here would silently drop the record.
+ console.log(`⚠️ sharing.importer: insertWithId(${kind}=${record.id}) failed: ${err.message}`);
+ failedInserts.push(`${kind}:${record.id}`);
+ return false;
+ });
+ if (insertOk) applied++;
return;
}
if (remoteWins(existing.updatedAt, record.updatedAt)) {
@@ -583,6 +596,7 @@ async function applyAutoMerge(bucket, manifest, records, { availableAssetKeys =
collectionTombstonedUniverse,
legacyCanonPendingUniverses: legacyCanonPendingUniverses.length > 0 ? legacyCanonPendingUniverses : null,
legacyCanonPendingFailures: legacyCanonPendingFailures.length > 0 ? legacyCanonPendingFailures : null,
+ recordImportFailures: failedInserts.length > 0 ? failedInserts : null,
adoptedSubscription: adoptedSubscription
? { id: adoptedSubscription.id, bucketId: adoptedSubscription.bucketId, recordKind: adoptedSubscription.recordKind, recordId: adoptedSubscription.recordId }
: null,
@@ -838,6 +852,7 @@ export async function processManifest(bucketId, manifestFilename) {
const collectionTombstonedUniverse = outcome?.collectionTombstonedUniverse || null;
const legacyCanonPendingUniverses = outcome?.legacyCanonPendingUniverses || null;
const legacyCanonPendingFailures = outcome?.legacyCanonPendingFailures || null;
+ const recordImportFailures = outcome?.recordImportFailures || null;
// Tombstoned universe: the collection owner IS on disk but locally deleted.
// Unlike the truly-missing case this will never self-resolve via sync, so we
// advance the cursor (no infinite pending loop) and emit a clear signal. The
@@ -848,15 +863,16 @@ export async function processManifest(bucketId, manifestFilename) {
console.log(`⚠️ sharing: bucket=${bucket.name} manifest=${manifest.id} kind=${manifest.kind} collectionUniverse=${collectionTombstonedUniverse} is deleted locally — ${outcome.collectionItemsDeferred ?? 0} item(s) skipped; restore universe to import`);
return { processed: true, manifest, outcome };
}
- if (assetCopy.missing.length > 0 || missingRecords.length > 0 || collectionPendingUniverse || collectionPendingSeries || legacyCanonPendingUniverses || legacyCanonPendingFailures) {
+ if (assetCopy.missing.length > 0 || missingRecords.length > 0 || collectionPendingUniverse || collectionPendingSeries || legacyCanonPendingUniverses || legacyCanonPendingFailures || recordImportFailures) {
if (assetCopy.missing.length > 0) outcome.pendingAssets = assetCopy.missing;
if (missingRecords.length > 0) outcome.pendingRecords = missingRecords;
if (collectionPendingUniverse) outcome.pendingCollectionUniverse = collectionPendingUniverse;
if (collectionPendingSeries) outcome.pendingCollectionSeries = collectionPendingSeries;
if (legacyCanonPendingUniverses) outcome.pendingLegacyCanonUniverses = legacyCanonPendingUniverses;
if (legacyCanonPendingFailures) outcome.pendingLegacyCanonFailures = legacyCanonPendingFailures;
+ if (recordImportFailures) outcome.pendingRecordImportFailures = recordImportFailures;
sharingEvents.emit('manifest-processed', { bucketId, manifestId: manifest.id, manifestFilename, outcome });
- console.log(`⏳ sharing: bucket=${bucket.name} manifest=${manifest.id} kind=${manifest.kind} mode=${bucket.mode} waitingForAssets=${assetCopy.missing.length} waitingForRecords=${missingRecords.length}${collectionPendingUniverse ? ` waitingForUniverse=${collectionPendingUniverse}` : ''}${collectionPendingSeries ? ` waitingForSeries=${collectionPendingSeries}` : ''}${legacyCanonPendingUniverses ? ` waitingForLegacyCanonUniverses=${legacyCanonPendingUniverses.join(',')}` : ''}${legacyCanonPendingFailures ? ` legacyCanonFailures=${legacyCanonPendingFailures.join(',')}` : ''}`);
+ console.log(`⏳ sharing: bucket=${bucket.name} manifest=${manifest.id} kind=${manifest.kind} mode=${bucket.mode} waitingForAssets=${assetCopy.missing.length} waitingForRecords=${missingRecords.length}${collectionPendingUniverse ? ` waitingForUniverse=${collectionPendingUniverse}` : ''}${collectionPendingSeries ? ` waitingForSeries=${collectionPendingSeries}` : ''}${legacyCanonPendingUniverses ? ` waitingForLegacyCanonUniverses=${legacyCanonPendingUniverses.join(',')}` : ''}${legacyCanonPendingFailures ? ` legacyCanonFailures=${legacyCanonPendingFailures.join(',')}` : ''}${recordImportFailures ? ` recordImportFailures=${recordImportFailures.join(',')}` : ''}`);
return { processed: true, pending: true, manifest, outcome };
}
await markProcessed(bucketId, manifestFilename, manifest.id);
diff --git a/server/services/sharing/integration.test.js b/server/services/sharing/integration.test.js
index f2ddb7a316..0b2aa57a91 100644
--- a/server/services/sharing/integration.test.js
+++ b/server/services/sharing/integration.test.js
@@ -551,6 +551,40 @@ describe('sharing round-trip', () => {
expect(hasBeenProcessed(cursor, exp.filename, exp.manifestId)).toBe(true);
});
+ it('keeps a manifest retryable (does not advance the cursor) when a record insert fails unexpectedly', async () => {
+ // A non-duplicate insert failure (disk error, transient fs error) must NOT
+ // advance the cursor — otherwise the manifest is marked processed and the
+ // record is silently dropped. It must stay pending and retry.
+ const bucket = await buckets.createBucket({ name: 'InsertFailBucket', path: tempBucket, mode: 'auto-merge' });
+ const universeBuilder = await import('../universeBuilder.js');
+ const { readCursor, hasBeenProcessed } = await import('./manifest.js');
+ const u = await universeBuilder.createUniverse({ name: 'Fails To Insert Once' });
+
+ const exp = await exporter.exportUniverse(u.id, bucket.id);
+ simulateRemoteSender(tempBucket, exp.filename);
+ await universeBuilder.deleteUniverse(u.id);
+
+ // Force the first insert to throw a non-duplicate error (not a *_DUPLICATE).
+ const spy = vi.spyOn(universeBuilder, 'insertUniverseWithId')
+ .mockRejectedValueOnce(new Error('simulated transient disk failure'));
+
+ const first = await importer.processManifest(bucket.id, exp.filename);
+ expect(first.pending).toBe(true);
+ expect(first.outcome.pendingRecordImportFailures).toEqual([`universe:${u.id}`]);
+ let cursor = await readCursor(bucket.id);
+ expect(hasBeenProcessed(cursor, exp.filename, exp.manifestId)).toBe(false);
+ await expect(universeBuilder.getUniverse(u.id)).rejects.toThrow();
+
+ // Insert succeeds on retry → manifest finally marked processed.
+ spy.mockRestore();
+ const retry = await importer.processBacklog(bucket.id);
+ expect(retry.processed).toBe(1);
+ const restored = await universeBuilder.getUniverse(u.id);
+ expect(restored.name).toBe('Fails To Insert Once');
+ cursor = await readCursor(bucket.id);
+ expect(hasBeenProcessed(cursor, exp.filename, exp.manifestId)).toBe(true);
+ });
+
it('keeps series manifests retryable while issue record files are still syncing', async () => {
const bucket = await buckets.createBucket({ name: 'SlowIssueSync', path: tempBucket, mode: 'auto-merge' });
const { readCursor, hasBeenProcessed } = await import('./manifest.js');