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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (claude): retry partial media-library pulls
[._.]/ Adam Eivy29cc3aa5a7bd
diff --git a/server/services/sharing/peerSync.js b/server/services/sharing/peerSync.jsindex ff246aa317..0554bece9f 100644--- a/server/services/sharing/peerSync.js+++ b/server/services/sharing/peerSync.js@@ -2902,14 +2902,25 @@ export async function syncMediaLibraryFromPeer(peer) {// Reuse the per-record pull worker (in-flight dedup, image-sidecar fetch,// video-thumbnail regen).await pullMissingAssetsFromPeer(peer.instanceId, missing);- // Rebuild the derived media_assets index so the gallery/Media tab reflects- // the new image+video bytes. Idempotent; best-effort.- await reconcileMediaLibraryIndex();- // Record the hash only AFTER a full sweep so a partial pull (peer dropped- // mid-transfer) re-attempts next tick instead of being marked done.- lastLibraryManifestHash.set(peer.instanceId, manifest.manifestHash);- console.log(`📥 peerSync: media-library sweep from ${peer.name || peer.instanceId} — requested ${requested} missing asset(s)`);- return { pulled: requested };+ // `pullMissingAssetsFromPeer` swallows per-asset failures (peer drops+ // mid-sweep, 404, size-cap reject) and always resolves — so a resolved pull+ // does NOT mean every byte landed. Re-diff against disk to see what actually+ // arrived; this is the authoritative signal, not the pull's resolution.+ const stillMissing = await diffAssetManifestAgainstLocal(manifest.assets);+ const pulled = requested - stillMissing.length;+ // Rebuild the derived media_assets index when any image/video bytes landed so+ // the gallery/Media tab reflects them. Idempotent; best-effort.+ if (pulled > 0) await reconcileMediaLibraryIndex();+ if (stillMissing.length === 0) {+ // Full sweep — safe to short-circuit future ticks on this manifestHash.+ lastLibraryManifestHash.set(peer.instanceId, manifest.manifestHash);+ console.log(`📥 peerSync: media-library sweep from ${peer.name || peer.instanceId} — pulled ${pulled} asset(s)`);+ } else {+ // Partial pull — do NOT record the hash, so the next tick re-diffs and+ // retries the still-missing assets instead of being marked done.+ console.log(`⚠️ peerSync: media-library sweep from ${peer.name || peer.instanceId} — pulled ${pulled}/${requested}, ${stillMissing.length} still missing; retrying next tick`);+ }+ return { pulled, missing: stillMissing.length };} finally {librarySweepInFlight.delete(peer.instanceId);}diff --git a/server/services/sharing/peerSync.test.js b/server/services/sharing/peerSync.test.jsindex cee2efafd0..0d421764e4 100644--- a/server/services/sharing/peerSync.test.js+++ b/server/services/sharing/peerSync.test.js@@ -3836,7 +3836,7 @@ describe('media-library federation (#1566)', () => {expect(res2.skipped).toBe('unchanged');});- it('requests missing bytes and reconciles the index when the library diverges', async () => {+ it('does NOT record the manifestHash when a byte pull fails — the next tick retries', async () => {const peer = mkPeer('fs-diverge');// pullMissingAssetsFromPeer re-resolves the peer via getPeers — include it.vi.mocked(getPeers).mockResolvedValue([peer]);@@ -3847,13 +3847,49 @@ describe('media-library federation (#1566)', () => {};vi.mocked(peerFetch).mockImplementation(async (url) => {if (String(url).endsWith('/library-manifest')) return { ok: true, json: async () => manifest };- return { ok: false }; // byte pull fails gracefully — mechanics tested elsewhere+ return { ok: false }; // byte pull fails → nothing lands on disk});const res = await syncMediaLibraryFromPeer(peer);- expect(res.pulled).toBe(1);+ // The asset URL was requested...const urls = vi.mocked(peerFetch).mock.calls.map((c) => String(c[0]));expect(urls.some((u) => u.includes('/data/audio/remote.wav'))).toBe(true);+ // ...but nothing landed (re-diff still sees it missing), so pulled=0,+ // no reconcile, and the hash is NOT recorded.+ expect(res.pulled).toBe(0);+ expect(res.missing).toBe(1);+ expect(reconcileMediaAssets).not.toHaveBeenCalled();+ // Second call must re-diff (NOT short-circuit on an 'unchanged' hash).+ const res2 = await syncMediaLibraryFromPeer(peer);+ expect(res2.skipped).not.toBe('unchanged');+ });++ it('records the manifestHash + reconciles once missing bytes actually land', async () => {+ const peer = mkPeer('fs-success');+ vi.mocked(getPeers).mockResolvedValue([peer]);+ // No sha256 on the entry → diff is existence-only, so once the byte pull+ // writes the file the re-diff sees it present (no hash to mismatch).+ const manifest = {+ schemaVersion: PORTOS_SCHEMA_VERSIONS.mediaLibrary,+ manifestHash: 'e'.repeat(64),+ assets: [{ filename: 'land.wav', kind: 'audio' }],+ };+ const bytes = Buffer.from('audio-bytes-that-landed');+ vi.mocked(peerFetch).mockImplementation(async (url) => {+ if (String(url).endsWith('/library-manifest')) return { ok: true, json: async () => manifest };+ // Asset byte fetch — a Response-like the cap-checker accepts.+ return {+ ok: true,+ headers: new Headers({ 'content-length': String(bytes.length) }),+ arrayBuffer: async () => bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength),+ };+ });+ const res1 = await syncMediaLibraryFromPeer(peer);+ expect(res1.pulled).toBe(1);+ expect(res1.missing).toBe(0);expect(reconcileMediaAssets).toHaveBeenCalled();+ // Hash recorded → second call short-circuits as unchanged.+ const res2 = await syncMediaLibraryFromPeer(peer);+ expect(res2.skipped).toBe('unchanged');});});