diff --git a/packages/agent-runtime/src/tools/handlers/tool/__tests__/propose-write-file.test.ts b/packages/agent-runtime/src/tools/handlers/tool/__tests__/propose-write-file.test.ts new file mode 100644 index 0000000000..42969bc3b6 --- /dev/null +++ b/packages/agent-runtime/src/tools/handlers/tool/__tests__/propose-write-file.test.ts @@ -0,0 +1,82 @@ +import { getInitialAgentState } from '@codebuff/common/types/session-state' +import { expect, test } from 'bun:test' +import { applyPatch } from 'diff' + +import { handleProposeStrReplace } from '../propose-str-replace' +import { handleProposeWriteFile } from '../propose-write-file' +import { + clearProposedContentForRun, + getProposedContent, +} from '../proposed-content-store' + +test('a replacement follows a write proposal while its disk read is pending', async () => { + const runId = 'pending-write-proposal' + const path = 'file.ts' + let release!: (content: string) => void + const diskRead = new Promise((resolve) => { + release = resolve + }) + let reads = 0 + const shared = { + runId, + agentState: getInitialAgentState(), + logger: { debug() {}, info() {}, warn() {}, error() {} }, + requestOptionalFile: () => { + reads++ + return diskRead + }, + } + try { + const first = handleProposeWriteFile({ + ...shared, + previousToolCallFinished: Promise.resolve(), + toolCall: { + toolName: 'propose_write_file', + toolCallId: 'write', + input: { path, content: 'first proposal\n' }, + }, + }) + const second = handleProposeStrReplace({ + ...shared, + previousToolCallFinished: first.then(() => {}), + toolCall: { + toolName: 'propose_str_replace', + toolCallId: 'replace', + input: { + path, + replacements: [ + { + oldString: 'first proposal', + newString: 'second proposal', + allowMultiple: false, + }, + ], + }, + }, + }) + release('disk contents\n') + const [writeResult, replacementResult] = await Promise.all([first, second]) + const value = replacementResult.output[0].value + expect(value).not.toHaveProperty('errorMessage') + if (!('unifiedDiff' in value)) + throw new Error('Expected a replacement diff') + expect( + applyPatch( + 'first proposal\n', + `--- file.ts\n+++ file.ts\n${value.unifiedDiff}`, + ), + ).toBe('second proposal\n') + const initial = writeResult.output[0].value + if (!('unifiedDiff' in initial)) throw new Error('Expected a write diff') + expect( + applyPatch( + 'disk contents\n', + `--- file.ts\n+++ file.ts\n${initial.unifiedDiff}`, + ), + ).toBe('first proposal\n') + expect(await getProposedContent(runId, path)).toBe('second proposal\n') + expect(reads).toBe(1) + } finally { + clearProposedContentForRun(runId) + } +}) diff --git a/packages/agent-runtime/src/tools/handlers/tool/propose-write-file.ts b/packages/agent-runtime/src/tools/handlers/tool/propose-write-file.ts index 0b1847966e..417a359bce 100644 --- a/packages/agent-runtime/src/tools/handlers/tool/propose-write-file.ts +++ b/packages/agent-runtime/src/tools/handlers/tool/propose-write-file.ts @@ -47,14 +47,16 @@ export const handleProposeWriteFile = (async ( return requestOptionalFile({ ...params, filePath: path }) } - const initialContent = await getProposedOrDiskContent() + const initialContentPromise = getProposedOrDiskContent() // Normalize content (remove leading newline if present) const newContent = content.startsWith('\n') ? content.slice(1) : content - // Store the proposed content for future propose calls on the same file (by runId) + // Reserve the complete replacement before awaiting its base content, so + // later proposals see it while this call prepares its diff. setProposedContent(runId, path, Promise.resolve(newContent)) + const initialContent = await initialContentPromise await previousToolCallFinished // Generate unified diff @@ -83,4 +85,4 @@ export const handleProposeWriteFile = (async ( }, ], } -}) as CodebuffToolHandlerFunction<'propose_write_file'> +}) satisfies CodebuffToolHandlerFunction<'propose_write_file'>