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
fix(cos): preserve external task edits during shared reads (#12225)
Adam Eivy4581dad7b6b1
diff --git a/server/services/cosTaskStore.js b/server/services/cosTaskStore.jsindex 5d9b551481..eb1aec90d6 100644--- a/server/services/cosTaskStore.js+++ b/server/services/cosTaskStore.js@@ -169,7 +169,7 @@ function isContentEdit(updates, existingMetadata = {}) {// Cache the parsed array per file so an unchanged file costs one `stat` plus a// structured clone instead of a read + full parse.//-// Concurrent API/daemon readers share the complete stat/read/parse operation.+// Concurrent API/daemon readers share the read/parse operation for each stamp.// Otherwise a cold cache multiplies whole-file strings and parsed task arrays// by the number of readers (#12225). Correctness rests on two signals://@@ -179,7 +179,7 @@ function isContentEdit(updates, existingMetadata = {}) {// write, including reads started while the rename was pending. mtime can// be as coarse as one second on some filesystems, so a write-then-read in// the same tick must not depend on the stamp having moved.-const parsedTaskCache = new Map(); // filePath -> generation { snapshot? }+const parsedTaskCache = new Map(); // filePath -> generation { stamp, snapshot? }const taskSnapshotReads = createSingleFlight();// `null` = could not stat (missing/unreadable) → do not cache, distinct from a@@ -192,16 +192,18 @@ const taskFileStamp = async (filePath) => {// Private snapshots never leave this module: list readers clone the array,// while ID lookups clone only the matching task. Both share invalidation.async function readTaskSnapshot(filePath) {+ // Every caller checks external edits before joining a flight: a new writer+ // must not consume a read that captured the file before an editor's change.+ const stamp = await taskFileStamp(filePath);let generation = parsedTaskCache.get(filePath);- if (!generation) {- generation = {};+ if (!generation || generation.stamp !== stamp) {+ generation = { stamp };parsedTaskCache.set(filePath, generation);}// The entry identity is the flight key: invalidation detaches older reads// without letting them repopulate the cache or capture post-write callers.return taskSnapshotReads.run(generation, async () => {- const stamp = await taskFileStamp(filePath);- if (stamp && generation.snapshot?.stamp === stamp) return generation.snapshot;+ if (stamp && generation.snapshot) return generation.snapshot;const snapshot = { stamp, tasks: parseTasksMarkdown(await readFile(filePath, 'utf-8')) };if (parsedTaskCache.get(filePath) === generation) {diff --git a/server/services/cosTaskStore.test.js b/server/services/cosTaskStore.test.jsindex c17624510b..780f64e960 100644--- a/server/services/cosTaskStore.test.js+++ b/server/services/cosTaskStore.test.js@@ -432,6 +432,32 @@ describe('cosTaskStore parsed-task cache (#3497)', () => {expect(readFile).toHaveBeenCalledTimes(2);});+ it('preserves an external edit when an update starts after an older read captured the file', async () => {+ const created = await addTask({ description: 'original', metadata: { context: 'before edit' } }, 'user');+ __resetTaskCache();+ const oldContent = mock.files.get(USER_FILE);+ const reading = Promise.withResolvers();+ const finishRead = Promise.withResolvers();+ vi.mocked(readFile).mockImplementationOnce(async () => {+ reading.resolve();+ await finishRead.promise;+ return oldContent;+ });+ const olderRead = getUserTasks();+ await reading.promise;+ mock.files.set(USER_FILE, oldContent.replace('before edit', 'external edit'));+ mock.mtimes.set(USER_FILE, mock.mtimes.get(USER_FILE) + 1000);++ const update = updateTask(created.id, { description: 'API edit' }, 'user');+ // Releasing the old read must not make a newer update overwrite the editor.+ finishRead.resolve();+ expect((await olderRead).tasks[0].metadata.context).toBe('before edit');+ expect((await update).metadata.context).toBe('external edit');+ expect((await getUserTasks()).tasks[0]).toMatchObject({+ description: 'API edit', metadata: { context: 'external edit' }+ });+ });+it('keeps a read overlapping a write from capturing or poisoning post-write readers', async () => {const created = await addTask({ description: 'original' }, 'user');await getUserTasks();