Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(codex): normalize app-server file diffs (#18701)

AmAzing-14f30b8e5c67
diff --git a/packages/heterogeneous-agents/src/adapters/codexAppServer.test.ts b/packages/heterogeneous-agents/src/adapters/codexAppServer.test.ts
index a9f5dc5b997..f070f4b6253 100644
--- a/packages/heterogeneous-agents/src/adapters/codexAppServer.test.ts
+++ b/packages/heterogeneous-agents/src/adapters/codexAppServer.test.ts
@@ -1,5 +1,6 @@
import { readFile } from 'node:fs/promises';
+import { parsePatch } from 'diff';
import { describe, expect, it } from 'vitest';
import { CodexAdapter } from './codex';
@@ -158,6 +159,64 @@ describe('CodexAppServerAdapter', () => {
expect(result.pluginState.stdout).toBe(result.content);
});
+ it('normalizes native file changes into complete single-file patches', () => {
+ const adapter = new CodexAppServerAdapter();
+ const events = adapter.adapt('item/completed', {
+ completedAtMs: 2,
+ item: {
+ changes: [
+ {
+ diff: '@@ -1 +1 @@\n-old\n+new\n',
+ kind: { move_path: null, type: 'update' },
+ path: '/workspace/updated.ts',
+ },
+ {
+ diff: 'first\nsecond\n',
+ kind: { type: 'add' },
+ path: 'src/added.ts',
+ },
+ {
+ diff: 'removed\n',
+ kind: { type: 'delete' },
+ path: 'src/deleted.ts',
+ },
+ {
+ diff: '@@ -1 +1 @@\n-before\n+after\n\n\nMoved to: src/after.ts',
+ kind: { move_path: 'src/after.ts', type: 'update' },
+ path: 'src/before.ts',
+ },
+ ],
+ id: 'file-change-1',
+ status: 'completed',
+ type: 'fileChange',
+ },
+ threadId: 'thread-1',
+ turnId: 'turn-1',
+ });
+ const changes = events.find(({ type }) => type === 'tool_result')?.data.pluginState
+ ?.changes as Array<{ diffText: string; kind: string }>;
+
+ expect(changes.map(({ kind }) => kind)).toEqual(['update', 'add', 'delete', 'rename']);
+ expect(changes[0].diffText).toContain(
+ 'diff --git a/workspace/updated.ts b/workspace/updated.ts\n--- a/workspace/updated.ts\n+++ b/workspace/updated.ts\n@@ -1 +1 @@',
+ );
+ expect(changes[1].diffText).toContain('--- /dev/null');
+ expect(changes[1].diffText).toContain('+++ b/src/added.ts');
+ expect(changes[1].diffText).toContain('+first\n+second');
+ expect(changes[2].diffText).toContain('--- a/src/deleted.ts');
+ expect(changes[2].diffText).toContain('+++ /dev/null');
+ expect(changes[2].diffText).toContain('-removed');
+ expect(changes[3].diffText).toContain('diff --git a/src/before.ts b/src/after.ts');
+ expect(changes[3].diffText).not.toContain('Moved to:');
+
+ for (const { diffText } of changes) {
+ const parsed = parsePatch(diffText);
+ expect(parsed).toHaveLength(1);
+ expect(parsed[0].oldFileName).toBeTypeOf('string');
+ expect(parsed[0].newFileName).toBeTypeOf('string');
+ }
+ });
+
it.each([
{
code: 'rate_limit',
diff --git a/packages/heterogeneous-agents/src/adapters/codexAppServer.ts b/packages/heterogeneous-agents/src/adapters/codexAppServer.ts
index d37edc0ea8c..ef6a5746576 100644
--- a/packages/heterogeneous-agents/src/adapters/codexAppServer.ts
+++ b/packages/heterogeneous-agents/src/adapters/codexAppServer.ts
@@ -1,3 +1,5 @@
+import { formatPatch, structuredPatch } from 'diff';
+
import type {
AgentMessageDeltaNotification,
CodexErrorInfo,
@@ -120,9 +122,44 @@ const toToolPayload = (item: ToolItem): ToolCallPayload => ({
type: 'default',
});
+const toGitDiffPath = (prefix: 'a' | 'b', filePath: string): string =>
+ filePath.startsWith('/') ? `${prefix}${filePath}` : `${prefix}/${filePath}`;
+
+// App-server sends raw content for add/delete and headerless hunks for update.
+// Normalize both forms to the complete single-file patch expected by PatchDiff.
+const toCompleteFileDiff = (change: FileUpdateChange): string => {
+ const sourcePath = toGitDiffPath('a', change.path);
+ const destinationPath = toGitDiffPath(
+ 'b',
+ change.kind.type === 'update' && change.kind.move_path ? change.kind.move_path : change.path,
+ );
+ const gitHeader = `diff --git ${sourcePath} ${destinationPath}`;
+
+ if (change.kind.type === 'add' || change.kind.type === 'delete') {
+ const patch = structuredPatch(
+ change.kind.type === 'add' ? '/dev/null' : sourcePath,
+ change.kind.type === 'delete' ? '/dev/null' : destinationPath,
+ change.kind.type === 'delete' ? change.diff : '',
+ change.kind.type === 'add' ? change.diff : '',
+ );
+ return `${gitHeader}\n${formatPatch(patch)}`;
+ }
+
+ const movePath = change.kind.move_path;
+ const moveSuffix = movePath ? `\n\nMoved to: ${movePath}` : '';
+ const hunks =
+ moveSuffix && change.diff.endsWith(moveSuffix)
+ ? change.diff.slice(0, -moveSuffix.length)
+ : change.diff;
+
+ return [gitHeader, `--- ${sourcePath}`, `+++ ${destinationPath}`, hunks]
+ .filter(Boolean)
+ .join('\n');
+};
+
const toFileChangeState = (changes: FileUpdateChange[]) => ({
changes: changes.map((change) => ({
- diffText: change.diff,
+ diffText: toCompleteFileDiff(change),
kind: change.kind.type === 'update' && change.kind.move_path ? 'rename' : change.kind.type,
path: change.path,
})),