Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<string>((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)
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -83,4 +85,4 @@ export const handleProposeWriteFile = (async (
},
],
}
}) as CodebuffToolHandlerFunction<'propose_write_file'>
}) satisfies CodebuffToolHandlerFunction<'propose_write_file'>
Loading