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([issue-12606]): YouTube history sync and playlist sync now take turns on the shared browser tab

[._.]/ Adam Eivy9e69065f5f29
diff --git a/server/services/youtubeBrowserLock.js b/server/services/youtubeBrowserLock.js
new file mode 100644
index 0000000000..4f2a4fbe28
--- /dev/null
+++ b/server/services/youtubeBrowserLock.js
@@ -0,0 +1,12 @@
+/**
+ * One lease on the managed browser's YouTube tab. History sync and playlist
+ * sync both resolve to the same CDP page (findOrOpenPage matches by hostname),
+ * so a navigation by one tears down the other's extraction. Each job wraps its
+ * whole run in withYoutubeTab so the two take turns; their own single-flight
+ * guards stay in place.
+ */
+import { createMutex } from '../lib/asyncMutex.js';
+
+const withLock = createMutex();
+
+export const withYoutubeTab = (fn) => withLock(fn);
diff --git a/server/services/youtubeBrowserLock.test.js b/server/services/youtubeBrowserLock.test.js
new file mode 100644
index 0000000000..6c6dae16fa
--- /dev/null
+++ b/server/services/youtubeBrowserLock.test.js
@@ -0,0 +1,19 @@
+import { describe, expect, it } from 'vitest';
+import { withYoutubeTab } from './youtubeBrowserLock.js';
+
+describe('withYoutubeTab', () => {
+ it('runs jobs one at a time in arrival order and survives a failing job', async () => {
+ const log = [];
+ const job = (name, fail) => withYoutubeTab(async () => {
+ log.push(`${name}:start`);
+ await new Promise((resolve) => setTimeout(resolve, 5));
+ log.push(`${name}:end`);
+ if (fail) throw new Error('boom');
+ return name;
+ });
+ const results = await Promise.allSettled([job('history', true), job('playlists')]);
+ expect(log).toEqual(['history:start', 'history:end', 'playlists:start', 'playlists:end']);
+ expect(results[0].status).toBe('rejected');
+ expect(results[1]).toMatchObject({ status: 'fulfilled', value: 'playlists' });
+ });
+});
diff --git a/server/services/youtubePlaylists.js b/server/services/youtubePlaylists.js
index 1438d93837..33c626008e 100644
--- a/server/services/youtubePlaylists.js
+++ b/server/services/youtubePlaylists.js
@@ -8,6 +8,7 @@
*/
import { dataPath, ensureDir, atomicWrite, readJSONFile, sleep } from '../lib/fileUtils.js';
import { findOrOpenPage, listCdpPages, isAuthPage, evaluateOnPage } from './browserService.js';
+import { withYoutubeTab } from './youtubeBrowserLock.js';
const PLAYLISTS_URL = 'https://www.youtube.com/feed/playlists';
const PLAYLIST_URL = 'https://www.youtube.com/playlist?list=';
@@ -159,7 +160,7 @@ let syncInFlight = null;
export async function syncYoutubePlaylists() {
if (syncInFlight) return syncInFlight;
- syncInFlight = doSyncYoutubePlaylists().finally(() => { syncInFlight = null; });
+ syncInFlight = withYoutubeTab(doSyncYoutubePlaylists).finally(() => { syncInFlight = null; });
return syncInFlight;
}
diff --git a/server/services/youtubePlaylists.test.js b/server/services/youtubePlaylists.test.js
index db02e7dbb8..b1a1280f20 100644
--- a/server/services/youtubePlaylists.test.js
+++ b/server/services/youtubePlaylists.test.js
@@ -12,6 +12,7 @@ vi.mock('../lib/fileUtils.js', () => ({
ensureDir: (...args) => mocks.ensureDir(...args), atomicWrite: (...args) => mocks.atomicWrite(...args), sleep: vi.fn(async () => {}),
}));
+import { withYoutubeTab } from './youtubeBrowserLock.js';
import { normalizeYoutubePlaylist, normalizeYoutubeVideo, youtubePlaylistSnapshotSummary, syncYoutubePlaylists } from './youtubePlaylists.js';
describe('YouTube playlist normalization', () => {
@@ -53,6 +54,18 @@ describe('syncYoutubePlaylists', () => {
expect(mocks.atomicWrite).toHaveBeenCalledWith('/tmp/youtube/playlists.json', expect.objectContaining({ schemaVersion: 1 }));
});
+ it('waits for the shared YouTube tab lease before touching the browser (history sync holds it)', async () => {
+ let release;
+ const held = withYoutubeTab(() => new Promise((resolve) => { release = resolve; }));
+ const pending = syncYoutubePlaylists();
+ for (let i = 0; i < 20; i += 1) await Promise.resolve();
+ expect(mocks.findOrOpenPage).not.toHaveBeenCalled();
+ release();
+ await held;
+ expect(await pending).toMatchObject({ ok: true });
+ expect(mocks.findOrOpenPage).toHaveBeenCalled();
+ });
+
it('navigates an existing YouTube tab to the playlists feed', async () => {
mocks.findOrOpenPage.mockResolvedValue({ id: 'page-1', url: 'https://www.youtube.com/watch?v=video-1' });
mocks.listCdpPages.mockResolvedValue([{ id: 'page-1', url: 'https://www.youtube.com/feed/playlists' }]);
diff --git a/server/services/youtubeSync.js b/server/services/youtubeSync.js
index ec056d5a75..2cd730035a 100644
--- a/server/services/youtubeSync.js
+++ b/server/services/youtubeSync.js
@@ -34,6 +34,7 @@ import { todayInTimezone, localDayRangeUtc } from '../lib/timezone.js';
import { getUserTimezone } from './userTimezone.js';
import { getSettings } from './settings.js';
import { findOrOpenPage, listCdpPages, isAuthPage, evaluateOnPage } from './browserService.js';
+import { withYoutubeTab } from './youtubeBrowserLock.js';
import { shortSummary } from './humanActivity.js';
import { youtubeVideoIdFromUrl } from '../lib/youtubeUrl.js';
@@ -321,7 +322,7 @@ export async function checkSetup() {
let syncInFlight = null;
export async function runSync() {
if (syncInFlight) return syncInFlight;
- syncInFlight = doRunSync().finally(() => { syncInFlight = null; });
+ syncInFlight = withYoutubeTab(doRunSync).finally(() => { syncInFlight = null; });
return syncInFlight;
}