TypeScript
fix(codex): show what a file-change approval will change (#8669)
Muhammad Waleed57a6b70e2422
diff --git a/apps/server/src/provider/Layers/CodexAdapter.test.ts b/apps/server/src/provider/Layers/CodexAdapter.test.tsindex 2bce5eceaf9..2ca44a2a5f0 100644--- a/apps/server/src/provider/Layers/CodexAdapter.test.ts+++ b/apps/server/src/provider/Layers/CodexAdapter.test.ts@@ -1877,6 +1877,213 @@ lifecycleLayer("CodexAdapterLive lifecycle", (it) => {}),);+ it.effect("names the edited files in an apply-patch approval without a reason", () =>+ Effect.gen(function* () {+ const { adapter, runtime } = yield* startLifecycleRuntime();+ const firstEventFiber = yield* Stream.runHead(adapter.streamEvents).pipe(Effect.forkChild);++ yield* runtime.emit({+ id: asEventId("evt-apply-patch"),+ kind: "request",+ provider: ProviderDriverKind.make("codex"),+ threadId: asThreadId("thread-1"),+ createdAt: "2026-08-24T00:00:00.000Z",+ method: "applyPatchApproval",+ requestKind: "file-change",+ requestId: ApprovalRequestId.make("req-patch"),+ turnId: asTurnId("turn-1"),+ payload: {+ callId: "call-1",+ conversationId: "provider-thread-1",+ fileChanges: {+ "/tmp/removed.md": { type: "delete", content: "gone" },+ "/tmp/added.ts": { type: "add", content: "export {};" },+ },+ },+ } satisfies ProviderEvent);++ const firstEvent = yield* Fiber.join(firstEventFiber);++ NodeAssert.equal(firstEvent._tag, "Some");+ if (firstEvent._tag !== "Some" || firstEvent.value.type !== "request.opened") {+ return;+ }+ NodeAssert.equal(firstEvent.value.payload.requestType, "apply_patch_approval");+ NodeAssert.equal(+ firstEvent.value.payload.detail,+ "add /tmp/added.ts\ndelete /tmp/removed.md",+ );+ }),+ );++ it.effect("keeps the reason when an apply-patch approval carries one", () =>+ Effect.gen(function* () {+ const { adapter, runtime } = yield* startLifecycleRuntime();+ const firstEventFiber = yield* Stream.runHead(adapter.streamEvents).pipe(Effect.forkChild);++ yield* runtime.emit({+ id: asEventId("evt-apply-patch-reason"),+ kind: "request",+ provider: ProviderDriverKind.make("codex"),+ threadId: asThreadId("thread-1"),+ createdAt: "2026-08-24T00:00:00.000Z",+ method: "applyPatchApproval",+ requestKind: "file-change",+ requestId: ApprovalRequestId.make("req-patch-reason"),+ turnId: asTurnId("turn-1"),+ payload: {+ callId: "call-2",+ conversationId: "provider-thread-1",+ reason: "Needs to rewrite the changelog",+ fileChanges: { "/tmp/CHANGELOG.md": { type: "add", content: "x" } },+ },+ } satisfies ProviderEvent);++ const firstEvent = yield* Fiber.join(firstEventFiber);++ NodeAssert.equal(firstEvent._tag, "Some");+ if (firstEvent._tag !== "Some" || firstEvent.value.type !== "request.opened") {+ return;+ }+ NodeAssert.equal(firstEvent.value.payload.detail, "Needs to rewrite the changelog");+ }),+ );++ it.effect("falls back to the grant root for a file-change approval without a reason", () =>+ Effect.gen(function* () {+ const { adapter, runtime } = yield* startLifecycleRuntime();+ const firstEventFiber = yield* Stream.runHead(adapter.streamEvents).pipe(Effect.forkChild);++ yield* runtime.emit({+ id: asEventId("evt-file-change"),+ kind: "request",+ provider: ProviderDriverKind.make("codex"),+ threadId: asThreadId("thread-1"),+ createdAt: "2026-08-24T00:00:00.000Z",+ method: "item/fileChange/requestApproval",+ requestKind: "file-change",+ requestId: ApprovalRequestId.make("req-file-change"),+ turnId: asTurnId("turn-1"),+ payload: {+ itemId: "item-1",+ grantRoot: "/tmp/workspace",+ startedAtMs: 0,+ threadId: "provider-thread-1",+ turnId: "turn-1",+ },+ } satisfies ProviderEvent);++ const firstEvent = yield* Fiber.join(firstEventFiber);++ NodeAssert.equal(firstEvent._tag, "Some");+ if (firstEvent._tag !== "Some" || firstEvent.value.type !== "request.opened") {+ return;+ }+ NodeAssert.equal(firstEvent.value.payload.requestType, "file_change_approval");+ NodeAssert.equal(firstEvent.value.payload.detail, "/tmp/workspace");+ }),+ );++ it.effect("prefers the edited files over a blank apply-patch reason", () =>+ Effect.gen(function* () {+ const { adapter, runtime } = yield* startLifecycleRuntime();+ const firstEventFiber = yield* Stream.runHead(adapter.streamEvents).pipe(Effect.forkChild);++ yield* runtime.emit({+ id: asEventId("evt-apply-patch-blank"),+ kind: "request",+ provider: ProviderDriverKind.make("codex"),+ threadId: asThreadId("thread-1"),+ createdAt: "2026-08-24T00:00:00.000Z",+ method: "applyPatchApproval",+ requestKind: "file-change",+ requestId: ApprovalRequestId.make("req-patch-blank"),+ turnId: asTurnId("turn-1"),+ payload: {+ callId: "call-3",+ conversationId: "provider-thread-1",+ reason: " ",+ fileChanges: {+ "/tmp/moved.ts": { type: "update", unified_diff: "@@", move_path: "/tmp/renamed.ts" },+ },+ },+ } satisfies ProviderEvent);++ const firstEvent = yield* Fiber.join(firstEventFiber);++ NodeAssert.equal(firstEvent._tag, "Some");+ if (firstEvent._tag !== "Some" || firstEvent.value.type !== "request.opened") {+ return;+ }+ NodeAssert.equal(firstEvent.value.payload.detail, "update /tmp/moved.ts -> /tmp/renamed.ts");+ }),+ );++ it.effect("caps the described files in an oversized apply-patch approval", () =>+ Effect.gen(function* () {+ const { adapter, runtime } = yield* startLifecycleRuntime();+ const firstEventFiber = yield* Stream.runHead(adapter.streamEvents).pipe(Effect.forkChild);++ const fileChanges = Object.fromEntries(+ Array.from({ length: 25 }, (_unused, index) => [+ `/tmp/file-${String(index).padStart(2, "0")}.ts`,+ { type: "add", content: "x" },+ ]),+ );++ yield* runtime.emit({+ id: asEventId("evt-apply-patch-many"),+ kind: "request",+ provider: ProviderDriverKind.make("codex"),+ threadId: asThreadId("thread-1"),+ createdAt: "2026-08-24T00:00:00.000Z",+ method: "applyPatchApproval",+ requestKind: "file-change",+ requestId: ApprovalRequestId.make("req-patch-many"),+ turnId: asTurnId("turn-1"),+ payload: { callId: "call-4", conversationId: "provider-thread-1", fileChanges },+ } satisfies ProviderEvent);++ const firstEvent = yield* Fiber.join(firstEventFiber);++ NodeAssert.equal(firstEvent._tag, "Some");+ if (firstEvent._tag !== "Some" || firstEvent.value.type !== "request.opened") {+ return;+ }+ const detail = firstEvent.value.payload.detail ?? "";+ NodeAssert.equal(detail.split("\n").length, 21);+ NodeAssert.ok(detail.endsWith("+5 more"));+ }),+ );++ it.effect("leaves an apply-patch approval without changes or a reason undetailed", () =>+ Effect.gen(function* () {+ const { adapter, runtime } = yield* startLifecycleRuntime();+ const firstEventFiber = yield* Stream.runHead(adapter.streamEvents).pipe(Effect.forkChild);++ yield* runtime.emit({+ id: asEventId("evt-apply-patch-empty"),+ kind: "request",+ provider: ProviderDriverKind.make("codex"),+ threadId: asThreadId("thread-1"),+ createdAt: "2026-08-24T00:00:00.000Z",+ method: "applyPatchApproval",+ requestKind: "file-change",+ requestId: ApprovalRequestId.make("req-patch-empty"),+ turnId: asTurnId("turn-1"),+ payload: { callId: "call-5", conversationId: "provider-thread-1", fileChanges: {} },+ } satisfies ProviderEvent);++ const firstEvent = yield* Fiber.join(firstEventFiber);++ NodeAssert.equal(firstEvent._tag, "Some");+ if (firstEvent._tag !== "Some" || firstEvent.value.type !== "request.opened") {+ return;+ }+ NodeAssert.equal(firstEvent.value.payload.detail, undefined);+ }),+ );+it.effect("maps MCP elicitation requests into app access approvals", () =>Effect.gen(function* () {const { adapter, runtime } = yield* startLifecycleRuntime();diff --git a/apps/server/src/provider/Layers/CodexAdapter.ts b/apps/server/src/provider/Layers/CodexAdapter.tsindex bdee68387fb..d1981b33d47 100644--- a/apps/server/src/provider/Layers/CodexAdapter.ts+++ b/apps/server/src/provider/Layers/CodexAdapter.ts@@ -787,6 +787,36 @@ function itemDetail(itemType: CanonicalItemType, item: CodexLifecycleItem): strireturn undefined;}+// Codex sends `reason` only sometimes, and sends it blank rather than absent+// often enough to matter, so an empty one must not outrank the paths below.+function nonEmptyDetail(value: string | null | undefined): string | undefined {+ const trimmed = value?.trim();+ return trimmed === undefined || trimmed.length === 0 ? undefined : trimmed;+}++// Keeps one oversized patch from pushing a wall of paths through every consumer+// of the approval, while still saying how much it covers.+const MAX_DESCRIBED_FILE_CHANGES = 20;++// An apply-patch approval carries the edited paths as the keys of `fileChanges`.+// Without them the approval card has nothing to show but its own title — the+// command-execution branch already falls back to the command for the same reason.+function describeFileChanges(+ fileChanges: EffectCodexSchema.ServerRequest__ApplyPatchApprovalParams["fileChanges"] | undefined,+): string | undefined {+ if (fileChanges === undefined) return undefined;+ const entries = Object.entries(fileChanges).toSorted(([left], [right]) =>+ left.localeCompare(right),+ );+ if (entries.length === 0) return undefined;+ const described = entries.slice(0, MAX_DESCRIBED_FILE_CHANGES).map(([path, change]) => {+ const movePath = change.type === "update" ? change.move_path : undefined;+ return movePath ? `${change.type} ${path} -> ${movePath}` : `${change.type} ${path}`;+ });+ const remaining = entries.length - described.length;+ return remaining > 0 ? `${described.join("\n")}\n+${remaining} more` : described.join("\n");+}+function toRequestTypeFromMethod(method: string): CanonicalRequestType {switch (method) {case "item/commandExecution/requestApproval":@@ -1325,7 +1355,9 @@ function mapToRuntimeEvents(EffectCodexSchema.ServerRequest__FileChangeRequestApprovalParams,event.payload,);- return payload?.reason ?? undefined;+ // These params carry no path of their own, only the root the agent+ // wants to write under.+ return nonEmptyDetail(payload?.reason) ?? nonEmptyDetail(payload?.grantRoot);}case "mcpServer/elicitation/request":return elicitation?.message;@@ -1334,7 +1366,11 @@ function mapToRuntimeEvents(EffectCodexSchema.ServerRequest__ApplyPatchApprovalParams,event.payload,);- return payload?.reason ?? undefined;+ return (+ nonEmptyDetail(payload?.reason) ??+ describeFileChanges(payload?.fileChanges) ??+ nonEmptyDetail(payload?.grantRoot)+ );}case "execCommandApproval": {const payload = readPayload(