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

perf: compare watched tasks with deep equality instead of JSON strings (#9678)

[._.]/ Adam Eivy3090f9291892
diff --git a/server/services/taskWatcher.js b/server/services/taskWatcher.js
index 7746c11aad..1d3b6832d1 100644
--- a/server/services/taskWatcher.js
+++ b/server/services/taskWatcher.js
@@ -7,6 +7,7 @@
import { watch } from 'chokidar';
import { join } from 'path';
+import { isDeepStrictEqual } from 'util';
import { cosEvents, getUserTasks, getCosTasks, getConfig } from './cos.js';
import { PATHS } from '../lib/fileUtils.js';
@@ -204,6 +205,29 @@ async function handleCosTasksChange() {
cosEvents.emit('tasks:cos:changed', current);
}
+// Legacy parsed files can carry undefined-valued entries (a bare `undefined`
+// metadata value); the old JSON comparison dropped them, so treat them as absent.
+// Shallow copies share the prompt strings by reference - nothing is serialized.
+function withoutUndefined(obj) {
+ const out = {};
+ for (const [key, value] of Object.entries(obj)) {
+ if (value !== undefined) out[key] = value;
+ }
+ return out;
+}
+
+function normalizeTask(task) {
+ const out = withoutUndefined(task);
+ if (out.metadata && typeof out.metadata === 'object' && !Array.isArray(out.metadata)) {
+ out.metadata = withoutUndefined(out.metadata);
+ }
+ return out;
+}
+
+function tasksEqual(a, b) {
+ return isDeepStrictEqual(normalizeTask(a), normalizeTask(b));
+}
+
/**
* Diff two task arrays to find changes
*/
@@ -224,7 +248,7 @@ function diffTasks(oldTasks, newTasks) {
added.push(task);
} else if (oldTask.status !== 'completed' && task.status === 'completed') {
completed.push(task);
- } else if (JSON.stringify(oldTask) !== JSON.stringify(task)) {
+ } else if (!tasksEqual(oldTask, task)) {
modified.push({ old: oldTask, new: task });
}
}
diff --git a/server/services/taskWatcher.test.js b/server/services/taskWatcher.test.js
index ddda902ba9..b7fbe616fe 100644
--- a/server/services/taskWatcher.test.js
+++ b/server/services/taskWatcher.test.js
@@ -102,3 +102,58 @@ describe('taskWatcher event queue', () => {
}));
});
});
+
+describe('taskWatcher change detection', () => {
+ const bigPrompt = 'p'.repeat(17408);
+ const makeTasks = (n) => Array.from({ length: n }, (_, i) => ({
+ id: `t${i}`, status: 'completed', description: `task ${i}`, priority: 'MEDIUM',
+ metadata: { prompt: bigPrompt, tags: ['a', { b: null }] },
+ }));
+
+ const runChange = async (before, after, file = '/repo/TASKS.md') => {
+ const watcher = new FakeWatcher();
+ mocks.watch.mockReturnValue(watcher);
+ mocks.getUserTasks
+ .mockResolvedValueOnce({ tasks: before })
+ .mockResolvedValueOnce({ tasks: after });
+ await startWatching();
+ watcher.emit('change', file);
+ await stopWatching();
+ return mocks.emit.mock.calls.filter(([name]) => !['tasks:user:changed', 'watcher:started', 'watcher:stopped'].includes(name));
+ };
+
+ it('emits only the one modified task in a large prompt-heavy history without JSON-encoding it', async () => {
+ const before = makeTasks(4000);
+ const after = before.map((t) => ({ ...t, metadata: { ...t.metadata } }));
+ after[1234] = { ...after[1234], description: 'changed' };
+ const stringify = vi.spyOn(JSON, 'stringify');
+ const events = await runChange(before, after);
+ const bigEncodings = stringify.mock.results.filter((r) => typeof r.value === 'string' && r.value.length >= bigPrompt.length).length;
+ expect(bigEncodings).toBe(0);
+ expect(events).toEqual([['tasks:user:modified', { tasks: [{ old: before[1234], new: after[1234] }] }]]);
+ });
+
+ it('detects nested metadata changes but ignores key order and undefined entries', async () => {
+ const base = { id: 'a', status: 'pending', description: 'd', metadata: { x: 1, y: [1, { z: null }], gone: undefined } };
+ const same = { ...base, metadata: { y: [1, { z: null }], x: 1 } };
+ expect(await runChange([base], [same])).toEqual([]);
+ vi.clearAllMocks();
+ mocks.getConfig.mockResolvedValue({ userTasksFile: 'TASKS.md', cosTasksFile: 'COS-TASKS.md' });
+ const nested = { ...base, metadata: { x: 1, y: [1, { z: 0 }] } };
+ const events = await runChange([base], [nested]);
+ expect(events.map(([n]) => n)).toEqual(['tasks:user:modified']);
+ });
+
+ it('keeps completed, pending-revival, added and removed event protocol and order', async () => {
+ const before = [
+ { id: 'c', status: 'pending' }, { id: 'r', status: 'completed' }, { id: 'gone', status: 'pending' },
+ ];
+ const after = [
+ { id: 'c', status: 'completed' }, { id: 'r', status: 'pending' }, { id: 'new', status: 'pending' },
+ ];
+ const events = await runChange(before, after);
+ expect(events.map(([n]) => n)).toEqual([
+ 'tasks:user:added', 'tasks:user:completed', 'tasks:user:modified', 'tasks:user:added', 'tasks:user:removed',
+ ]);
+ });
+});