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: coalesce overlapping task watcher starts and make stop await an in-flight start (#11779)

[._.]/ Adam Eivy99ba50674be2
diff --git a/server/services/taskWatcher.js b/server/services/taskWatcher.js
index 1d3b6832d1..6f9352f7cb 100644
--- a/server/services/taskWatcher.js
+++ b/server/services/taskWatcher.js
@@ -15,6 +15,8 @@ const ROOT_DIR = PATHS.root;
let watcher = null;
let isWatching = false;
+// In-flight start; set synchronously so overlapping starts coalesce and a stop can await it.
+let startPromise = null;
let lastUserTasks = null;
let lastCosTasks = null;
const pendingFileEvents = new Map();
@@ -40,10 +42,16 @@ function queueFileEvent(path, event, operation) {
/**
* Start watching task files
*/
-export async function startWatching() {
+export function startWatching() {
+ if (startPromise) return startPromise;
if (isWatching) {
- return { success: false, error: 'Already watching' };
+ return Promise.resolve({ success: false, error: 'Already watching' });
}
+ startPromise = doStartWatching().finally(() => { startPromise = null; });
+ return startPromise;
+}
+
+async function doStartWatching() {
const config = await getConfig();
const userTasksPath = join(ROOT_DIR, config.userTasksFile);
@@ -112,6 +120,8 @@ export async function startWatching() {
* Stop watching task files
*/
export async function stopWatching() {
+ // A stop issued mid-start must tear down the watcher that start creates.
+ if (startPromise) await startPromise.catch(() => {});
if (!isWatching || !watcher) {
return { success: false, error: 'Not watching' };
}
diff --git a/server/services/taskWatcher.test.js b/server/services/taskWatcher.test.js
index b7fbe616fe..8df0e3e49d 100644
--- a/server/services/taskWatcher.test.js
+++ b/server/services/taskWatcher.test.js
@@ -157,3 +157,30 @@ describe('taskWatcher change detection', () => {
]);
});
});
+
+describe('taskWatcher start/stop lifecycle', () => {
+ it('coalesces overlapping starts into one watcher that one stop closes', async () => {
+ const watcher = new FakeWatcher();
+ mocks.watch.mockReturnValue(watcher);
+ const results = await Promise.all([startWatching(), startWatching()]);
+ expect(results).toEqual([{ success: true }, { success: true }]);
+ expect(mocks.watch).toHaveBeenCalledTimes(1);
+ expect(await startWatching()).toEqual({ success: false, error: 'Already watching' });
+ expect(await stopWatching()).toEqual({ success: true });
+ expect(watcher.close).toHaveBeenCalledTimes(1);
+ });
+
+ it('stop issued during start waits for it and closes the created watcher', async () => {
+ const watcher = new FakeWatcher();
+ const gate = deferred();
+ mocks.watch.mockReturnValue(watcher);
+ mocks.getUserTasks.mockImplementationOnce(async () => { await gate.promise; return { tasks: [] }; });
+ const starting = startWatching();
+ const stopping = stopWatching();
+ gate.resolve();
+ await starting;
+ expect(await stopping).toEqual({ success: true });
+ expect(getWatcherStatus().watching).toBe(false);
+ expect(watcher.close).toHaveBeenCalledTimes(1);
+ });
+});