Skip to content

Fix stale file read after patch causing empty diff content#178

Open
SteffenDE wants to merge 2 commits into
agentclientprotocol:mainfrom
SteffenDE:sd-edit-fix
Open

Fix stale file read after patch causing empty diff content#178
SteffenDE wants to merge 2 commits into
agentclientprotocol:mainfrom
SteffenDE:sd-edit-fix

Conversation

@SteffenDE
Copy link
Copy Markdown
Contributor

Also ensure events are always handled in order.

#167 fixed some diff errors, but there was still a race condition when using codex in full access mode, where the file was already updated when codex-acp read it, making the patch fail to apply and returning empty content:

const oldContent = await readFileContent(change.path);
if (oldContent !== null) {
const patchedContent = applyPatch(oldContent, unifiedDiff);
if (patchedContent === false) return null;

Additionally, because each event was processed individually, the await readFile could cause the item/completed event to be processed and sent as tool_call_update via ACP before the initial tool_call was sent.

Comment on lines +297 to +308
if (patchedContent === false) {
// If Codex runs in full access mode, the file might already be patched.
// we can verify this by checking if the reverted patch applies.
const revertedPatch = revertPatch(unifiedDiff);
if (revertedPatch) {
const revertedContent = applyPatch(oldContent, revertedPatch);
if (revertedContent !== false) {
return createUpdateDiffContent(change.path, revertedContent, oldContent);
}
}
return null;
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one handles the empty content. The other changes are all for ensuring correct message order.

"sessionId": "test-session-id",
"update": {
"sessionUpdate": "tool_call_update",
"sessionUpdate": "tool_call",
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those test fixtures actually encoded the bad order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants