Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

tech(CDX-1): file_edit serialisiert pro Pfad — kein stiller Verlust bei Mehrfach-Edits

David44ca4739ebde
diff --git a/src/api/agents/__tests__/side-effect-key.test.ts b/src/api/agents/__tests__/side-effect-key.test.ts
index 59b50342..e231c480 100644
--- a/src/api/agents/__tests__/side-effect-key.test.ts
+++ b/src/api/agents/__tests__/side-effect-key.test.ts
@@ -39,6 +39,49 @@ describe('side-effect-key', () => {
expect(k1).not.toBe(k2)
})
+ it('serializes two file_edits of the SAME path', () => {
+ const k1 = deriveSideEffectKey('file_edit', { path: '/tmp/a.txt', old_string: 'a', new_string: 'b' })
+ const k2 = deriveSideEffectKey('file_edit', { path: '/tmp/a.txt', old_string: 'b', new_string: 'c' })
+ expect(k1).toBeDefined()
+ expect(k1).toBe(k2)
+ })
+
+ it('keeps file_edits of DIFFERENT paths parallel', () => {
+ const k1 = deriveSideEffectKey('file_edit', { path: '/tmp/a.txt', old_string: 'a', new_string: 'b' })
+ const k2 = deriveSideEffectKey('file_edit', { path: '/tmp/b.txt', old_string: 'a', new_string: 'b' })
+ expect(k1).not.toBe(k2)
+ })
+
+ // The scheduler groups by the key STRING alone (tool-executor's `groups` Map
+ // is keyed by it, not by tool name), so a shared key across tools is the only
+ // way to serialize a write and an edit of one file. It has to be: the edit's
+ // old_string is matched against whatever the write left on disk.
+ it('file_edit shares the file_write key for the same path (write+edit serialize)', () => {
+ const write = deriveSideEffectKey('file_write', { path: '/tmp/a.txt', content: 'x' })
+ const edit = deriveSideEffectKey('file_edit', { path: '/tmp/a.txt', old_string: 'x', new_string: 'y' })
+ expect(edit).toBe(write)
+ })
+
+ it('normalizes file_edit paths exactly like file_write', () => {
+ // Windows: case-insensitive + backslashes.
+ expect(deriveSideEffectKey('file_edit', { path: 'C:\\Users\\me\\a.txt' }))
+ .toBe(deriveSideEffectKey('file_edit', { path: 'c:/users/me/a.txt' }))
+ // …and it collides with the file_write key for the same Windows path.
+ expect(deriveSideEffectKey('file_edit', { path: 'C:\\Users\\me\\a.txt' }))
+ .toBe(deriveSideEffectKey('file_write', { path: 'c:/users/me/a.txt', content: '' }))
+ // Unix: case-sensitive.
+ expect(deriveSideEffectKey('file_edit', { path: '/tmp/Foo.txt' }))
+ .not.toBe(deriveSideEffectKey('file_edit', { path: '/tmp/foo.txt' }))
+ // Trailing + doubled slashes collapse.
+ expect(deriveSideEffectKey('file_edit', { path: '/tmp/a/' }))
+ .toBe(deriveSideEffectKey('file_edit', { path: '/tmp//a' }))
+ })
+
+ it('file_edit without path falls back to the same unknown sentinel', () => {
+ // Conservative: every pathless file mutation shares one serial queue.
+ expect(deriveSideEffectKey('file_edit', {})).toBe('file_write:unknown')
+ })
+
it('shell_execute and code_execute share the "exec" queue', () => {
expect(deriveSideEffectKey('shell_execute', { command: 'ls' })).toBe('exec')
expect(deriveSideEffectKey('code_execute', { code: '1' })).toBe('exec')
@@ -49,6 +92,18 @@ describe('side-effect-key', () => {
expect(deriveSideEffectKey('git_push', {})).toBe('exec')
expect(deriveSideEffectKey('run_tests', {})).toBe('exec')
expect(deriveSideEffectKey('shell_execute_background', { command: 'x' })).toBe('exec')
+ // `gh pr create` pushes the branch — same .git/index.lock as git_push.
+ expect(deriveSideEffectKey('gh_pr_create', { title: 'x' })).toBe('exec')
+ })
+
+ it('mutating-but-shell-less tools stay parallel', () => {
+ // todo_write is in-memory conversation state; project_init only renders a
+ // plan; a task kill must not queue behind the command it cancels.
+ expect(deriveSideEffectKey('todo_write', { todos: [] })).toBeUndefined()
+ expect(deriveSideEffectKey('project_init', { recipe: 'x' })).toBeUndefined()
+ expect(deriveSideEffectKey('shell_task_kill', { id: 'x' })).toBeUndefined()
+ expect(deriveSideEffectKey('shell_task_status', { id: 'x' })).toBeUndefined()
+ expect(deriveSideEffectKey('pr_resume', { url: 'x' })).toBeUndefined()
})
it('read-only git tools stay parallel (no key)', () => {
diff --git a/src/api/agents/side-effect-key.ts b/src/api/agents/side-effect-key.ts
index b82f1440..43d1cb6c 100644
--- a/src/api/agents/side-effect-key.ts
+++ b/src/api/agents/side-effect-key.ts
@@ -10,8 +10,20 @@
* - Pure reads (file_read, web_fetch, web_search, file_list,
* file_search, screenshot) → no key (fully parallel-safe).
*
- * - file_write → key is `file_write:<normalized-path>`. Two writes to
- * the SAME path serialize; writes to DIFFERENT paths remain parallel.
+ * - file_write, file_edit → key is `file_write:<normalized-path>`. Two
+ * mutations of the SAME path serialize; mutations of DIFFERENT paths
+ * remain parallel. The `file_write:` prefix is a shared namespace for
+ * "something rewrites this file", not the tool name: keys are compared as
+ * plain strings across tools (tool-executor groups by the key alone), so
+ * file_write + file_edit on one path land in one serial group — which is
+ * what we want, since an edit's old_string match depends on whatever the
+ * write left behind.
+ *
+ * file_edit used to fall through to `default` and therefore ran keyless.
+ * Two edits of the same file in one batch (the normal shape of a refactor
+ * round) then ran concurrently on a read-modify-write: last writer won,
+ * the other edit was lost silently, and BOTH reported success. In Stage
+ * mode the user reviewed a queue that was missing an approved change.
*
* - shell_execute → key 'exec'. We do not know what a shell command
* touches, and concurrent repo writes race on `.git/index.lock`, so all
@@ -28,7 +40,9 @@ export function deriveSideEffectKey(
args: Record<string, any>
): string | undefined {
switch (toolName) {
- case 'file_write': {
+ case 'file_write':
+ // Same path → same key as file_write, on purpose (see header).
+ case 'file_edit': {
const path = typeof args?.path === 'string' ? normalizePath(args.path) : ''
return path ? `file_write:${path}` : 'file_write:unknown'
}
@@ -41,6 +55,9 @@ export function deriveSideEffectKey(
case 'run_tests':
case 'git_commit':
case 'git_push':
+ // `gh pr create` pushes the branch and writes refs, so it raced the same
+ // `.git/index.lock` as git_commit/git_push while it ran keyless.
+ case 'gh_pr_create':
// Everything that shells out shares ONE serial queue. We can't know
// what a command touches, and two concurrent git writes race on
// `.git/index.lock` and one fails.
@@ -53,7 +70,15 @@ export function deriveSideEffectKey(
// both queued on the hand-off and a back-to-back gen could survive Stop.
return 'comfyui'
default:
- // file_read, file_list, file_search, web_search, web_fetch, screenshot.
+ // file_read, file_list, file_search, web_search, web_fetch, screenshot,
+ // pr_resume (read-only `gh pr view`/`diff`).
+ //
+ // Deliberately keyless although they are classified as mutating:
+ // - todo_write — synchronous in-memory conversation state, no file.
+ // - project_init — renders a scaffold PLAN as text, runs nothing.
+ // - shell_task_status / _kill / _list — backend task-manager calls,
+ // they spawn no shell. A kill must NOT queue behind the long shell
+ // command it is meant to cancel.
return undefined
}
}