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: preserve playlist sync data on transient failures

[._.]/ Adam Eivy24f49e261aac
diff --git a/client/src/components/brain/tabs/YoutubeTab.jsx b/client/src/components/brain/tabs/YoutubeTab.jsx
index 4a12c28ff0..75e889cbfe 100644
--- a/client/src/components/brain/tabs/YoutubeTab.jsx
+++ b/client/src/components/brain/tabs/YoutubeTab.jsx
@@ -78,7 +78,9 @@ export function YoutubeTab() {
setPlaylistsSyncing(true);
const result = await syncYoutubePlaylists({ silent: true }).catch(() => ({ ok: false, error: 'Playlist sync failed' }));
setPlaylistsSyncing(false);
- if (result?.playlistCount !== undefined) {
+ if (!result?.ok && result?.error) {
+ toast.error(result.error);
+ } else if (result?.playlistCount !== undefined) {
await loadPlaylists();
if (result.ok) toast.success(`Synced ${result.playlistCount} playlist(s) and ${result.videoCount} video(s)`);
else toast.error(`Playlist sync completed with ${result.failed || 0} warning(s)`);
diff --git a/server/services/spotifyAuth.js b/server/services/spotifyAuth.js
index f6b964df3f..49b3efbbfd 100644
--- a/server/services/spotifyAuth.js
+++ b/server/services/spotifyAuth.js
@@ -209,7 +209,7 @@ export async function handleCallback(code, options = {}) {
return { ...prior.result, duplicate: true };
}
const active = callbackInFlight.get(key);
- if (active) return active;
+ if (active) return active.then((result) => ({ ...result, duplicate: true }));
const promise = processCallback(code, options).then((result) => {
completedCallbacks.set(key, { completedAt: Date.now(), result });
for (const [cachedKey, cached] of completedCallbacks) {
diff --git a/server/services/spotifyAuth.test.js b/server/services/spotifyAuth.test.js
index 7167395cde..19accfdd14 100644
--- a/server/services/spotifyAuth.test.js
+++ b/server/services/spotifyAuth.test.js
@@ -238,7 +238,7 @@ describe('handleCallback', () => {
while (mocks.fetchWithTimeout.mock.calls.length === 0) await new Promise((resolve) => setTimeout(resolve, 0));
resolveFetch({ ok: true, json: async () => ({ access_token: 'fresh', refresh_token: 'refresh', expires_in: 3600 }) });
- await expect(Promise.all([first, second])).resolves.toEqual([{ success: true }, { success: true }]);
+ await expect(Promise.all([first, second])).resolves.toEqual([{ success: true }, { success: true, duplicate: true }]);
await expect(handleCallback('one-time-code')).resolves.toMatchObject({ success: true, duplicate: true });
expect(mocks.fetchWithTimeout).toHaveBeenCalledTimes(1);
});
diff --git a/server/services/spotifyPlaylists.js b/server/services/spotifyPlaylists.js
index 3d7f27d849..b0a476fbdd 100644
--- a/server/services/spotifyPlaylists.js
+++ b/server/services/spotifyPlaylists.js
@@ -141,7 +141,7 @@ async function doSyncSpotifyPlaylists() {
console.error(`❌ Spotify playlist list failed: ${error.message}`);
return { ok: false, error: error.message };
}
- const playlistSummaries = playlistResult.value;
+ const playlistSummaries = playlistResult.value.filter((playlist) => playlist?.id && playlist?.name);
const results = [];
for (let index = 0; index < playlistSummaries.length; index += PLAYLIST_BATCH_SIZE) {
diff --git a/server/services/spotifyPlaylists.test.js b/server/services/spotifyPlaylists.test.js
index 35ee3f377d..9ca6423efd 100644
--- a/server/services/spotifyPlaylists.test.js
+++ b/server/services/spotifyPlaylists.test.js
@@ -91,4 +91,16 @@ describe('syncSpotifyPlaylists', () => {
expect(maximumActive).toBe(5);
});
+
+ it('skips unresolved playlist entries before fetching items', async () => {
+ mocks.fetchWithTimeout
+ .mockReset()
+ .mockResolvedValueOnce({ ok: true, json: async () => ({ total: 2, items: [null, playlist] }) })
+ .mockResolvedValueOnce({ ok: true, json: async () => ({ total: 1, items: [trackItem] }) });
+
+ const result = await syncSpotifyPlaylists();
+
+ expect(result).toMatchObject({ ok: true, playlistCount: 1, trackCount: 1, scanned: 1 });
+ expect(mocks.fetchWithTimeout).toHaveBeenCalledTimes(2);
+ });
});
diff --git a/server/services/youtubePlaylists.js b/server/services/youtubePlaylists.js
index 235c2a4635..f08fe90630 100644
--- a/server/services/youtubePlaylists.js
+++ b/server/services/youtubePlaylists.js
@@ -189,6 +189,8 @@ async function doSyncYoutubePlaylists() {
const navigated = await evaluateOnPage(loaded.page, `location.assign(${JSON.stringify(`${PLAYLIST_URL}${encodeURIComponent(rawPlaylist.id)}`)}); true`).catch(() => false);
if (!navigated) {
warnings.push(`${rawPlaylist.name || 'Playlist'}: could not open playlist`);
+ const stale = previous?.playlists?.find((playlist) => playlist.id === rawPlaylist.id);
+ if (stale) playlists.push(stale);
continue;
}
await sleep(NAV_SETTLE_MS);
@@ -199,6 +201,12 @@ async function doSyncYoutubePlaylists() {
if (stale) playlists.push(stale);
continue;
}
+ if (!detail.videos.length && Number.isFinite(rawPlaylist.videoCount) && rawPlaylist.videoCount > 0) {
+ warnings.push(`${rawPlaylist.name || 'Playlist'}: no videos read`);
+ const stale = previous?.playlists?.find((playlist) => playlist.id === rawPlaylist.id);
+ if (stale) playlists.push(stale);
+ continue;
+ }
const normalizedVideos = detail.videos.map(normalizeYoutubeVideo).filter(Boolean);
const playlist = normalizeYoutubePlaylist(rawPlaylist, normalizedVideos);
if (playlist) playlists.push(playlist);
diff --git a/server/services/youtubePlaylists.test.js b/server/services/youtubePlaylists.test.js
index c2ac893f2c..e10b112c7e 100644
--- a/server/services/youtubePlaylists.test.js
+++ b/server/services/youtubePlaylists.test.js
@@ -76,4 +76,33 @@ describe('syncYoutubePlaylists', () => {
expect(result).toMatchObject({ ok: false, status: 'extraction-empty', playlistCount: 1, videoCount: 1 });
expect(mocks.atomicWrite).not.toHaveBeenCalled();
});
+
+ it('retains a stale playlist when navigation fails', async () => {
+ const stale = { id: 'PL-example', name: 'Example playlist', videoCount: 1, videos: [{ id: 'video-1' }] };
+ mocks.readJSONFile.mockResolvedValue({ playlists: [stale] });
+ mocks.evaluateOnPage
+ .mockReset()
+ .mockResolvedValueOnce({ signedOut: false, playlists: [{ id: stale.id, name: stale.name, videoCount: 1 }] })
+ .mockResolvedValueOnce(false);
+
+ const result = await syncYoutubePlaylists();
+
+ expect(result).toMatchObject({ ok: false, playlistCount: 1, videoCount: 1, failed: 1 });
+ expect(mocks.atomicWrite).toHaveBeenCalledWith('/tmp/youtube/playlists.json', expect.objectContaining({ playlists: [stale] }));
+ });
+
+ it('retains a stale playlist when a known non-empty page scrapes no videos', async () => {
+ const stale = { id: 'PL-example', name: 'Example playlist', videoCount: 1, videos: [{ id: 'video-1' }] };
+ mocks.readJSONFile.mockResolvedValue({ playlists: [stale] });
+ mocks.evaluateOnPage
+ .mockReset()
+ .mockResolvedValueOnce({ signedOut: false, playlists: [{ id: stale.id, name: stale.name, videoCount: 1 }] })
+ .mockResolvedValueOnce(true)
+ .mockResolvedValueOnce({ signedOut: false, videos: [] });
+
+ const result = await syncYoutubePlaylists();
+
+ expect(result).toMatchObject({ ok: false, playlistCount: 1, videoCount: 1, failed: 1 });
+ expect(result.warnings).toEqual(['Example playlist: no videos read']);
+ });
});