Skip to content

Commit 21cbec3

Browse files
only21milclaudejuliusmarminge
authored
fix(server): Codex file-change approvals list the files they change (pingdotgg#18248)
Co-authored-by: only21mil <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 0401372 commit 21cbec3

3 files changed

Lines changed: 245 additions & 2 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts‎

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
type OrchestrationV2AppThread,
1818
type OrchestrationV2ProviderThread,
1919
type OrchestrationV2ProviderTurn,
20+
type OrchestrationV2RuntimeRequest,
2021
type OrchestrationV2TurnItem,
2122
ProjectId,
2223
type ProviderApprovalDecision,
@@ -35,6 +36,7 @@ import { SpawnExecutableResolution } from "@t3tools/shared/shell";
3536
import * as CodexClient from "effect-codex-app-server/client";
3637
import * as CodexError from "effect-codex-app-server/errors";
3738
import * as CodexReplay from "effect-codex-app-server/replay";
39+
import type * as CodexSchema from "effect-codex-app-server/schema";
3840
import * as DateTime from "effect/DateTime";
3941
import * as Deferred from "effect/Deferred";
4042
import * as Crypto from "effect/Crypto";
@@ -47,6 +49,7 @@ import * as Fiber from "effect/Fiber";
4749
import * as Layer from "effect/Layer";
4850
import * as Path from "effect/Path";
4951
import * as PlatformError from "effect/PlatformError";
52+
import * as Queue from "effect/Queue";
5053
import * as Ref from "effect/Ref";
5154
import * as Schema from "effect/Schema";
5255
import * as Stream from "effect/Stream";
@@ -9533,4 +9536,214 @@ describe("CodexAdapterV2 post-settle continuation", () => {
95339536
),
95349537
);
95359538
});
9539+
9540+
describe("Codex file-change approval paths", () => {
9541+
const nativeThreadId = "file-approval-thread";
9542+
const nativeTurnId = "file-approval-turn";
9543+
const prompt = "Edit the files.";
9544+
type FileUpdateChange = CodexSchema.V2ItemStartedNotification__FileUpdateChange;
9545+
const addA: FileUpdateChange = { path: "/w/a.ts", kind: { type: "add" }, diff: "export {};" };
9546+
const updateB: FileUpdateChange = {
9547+
path: "/w/b.ts",
9548+
kind: { type: "update", move_path: null },
9549+
diff: "@@",
9550+
};
9551+
const renameC: FileUpdateChange = {
9552+
path: "/w/c.ts",
9553+
kind: { type: "update", move_path: "/w/d.ts" },
9554+
diff: "@@",
9555+
};
9556+
9557+
const deleteE: FileUpdateChange = { path: "/w/e.ts", kind: { type: "delete" }, diff: "gone" };
9558+
9559+
const fileChange = (
9560+
phase: "started" | "completed",
9561+
turnId: string,
9562+
itemId: string,
9563+
changes: ReadonlyArray<FileUpdateChange>,
9564+
): CodexReplay.CodexAppServerReplayEntry => ({
9565+
type: "emit_inbound",
9566+
label: `${phase} ${itemId}`,
9567+
frame: {
9568+
method: `item/${phase}`,
9569+
params: {
9570+
threadId: nativeThreadId,
9571+
turnId,
9572+
...(phase === "started" ? { startedAtMs: 1 } : { completedAtMs: 3 }),
9573+
item: {
9574+
type: "fileChange",
9575+
id: itemId,
9576+
status: phase === "started" ? "inProgress" : "completed",
9577+
changes,
9578+
},
9579+
},
9580+
},
9581+
});
9582+
9583+
const approval = (input: {
9584+
readonly id: number;
9585+
readonly turnId: string;
9586+
readonly itemId: string;
9587+
}): Array<CodexReplay.CodexAppServerReplayEntry> => [
9588+
{
9589+
type: "emit_inbound",
9590+
label: `approval ${input.itemId}`,
9591+
frame: {
9592+
id: input.id,
9593+
method: "item/fileChange/requestApproval",
9594+
params: {
9595+
threadId: nativeThreadId,
9596+
turnId: input.turnId,
9597+
itemId: input.itemId,
9598+
startedAtMs: 2,
9599+
reason: null,
9600+
grantRoot: null,
9601+
},
9602+
},
9603+
},
9604+
{
9605+
type: "expect_outbound",
9606+
label: `approval ${input.itemId}`,
9607+
frame: { id: input.id, result: { decision: "accept" } },
9608+
},
9609+
];
9610+
9611+
const turnCompleted = (turnId: string): CodexReplay.CodexAppServerReplayEntry => ({
9612+
type: "emit_inbound",
9613+
label: `complete ${turnId}`,
9614+
frame: {
9615+
method: "turn/completed",
9616+
params: {
9617+
threadId: nativeThreadId,
9618+
turn: makeCodexReplayTurn({ id: turnId, status: "completed" }),
9619+
},
9620+
},
9621+
});
9622+
9623+
const startApprovalTurn = (
9624+
scenario: string,
9625+
entries: ReadonlyArray<CodexReplay.CodexAppServerReplayEntry>,
9626+
) =>
9627+
Effect.gen(function* () {
9628+
const pending = yield* Queue.unbounded<OrchestrationV2RuntimeRequest>();
9629+
const harness = yield* makeCodexReplayHarness(
9630+
makeCodexReplayTranscript({
9631+
scenario,
9632+
entries: [...codexReplayPreamble({ nativeThreadId, nativeTurnId, prompt }), ...entries],
9633+
}),
9634+
(event) =>
9635+
event.type === "runtime_request.updated" && event.runtimeRequest.status === "pending"
9636+
? Queue.offer(pending, event.runtimeRequest)
9637+
: Effect.void,
9638+
);
9639+
yield* harness.runtime.startTurn(
9640+
makeCodexTestTurnInput({
9641+
threadId: harness.threadId,
9642+
providerThread: harness.providerThread,
9643+
now: yield* DateTime.now,
9644+
attemptId: RunAttemptId.make(`${scenario}-attempt`),
9645+
text: prompt,
9646+
}),
9647+
);
9648+
return {
9649+
harness,
9650+
answerNext: Queue.take(pending).pipe(
9651+
Effect.tap((request) =>
9652+
harness.runtime.respondToRuntimeRequest({
9653+
requestId: request.id,
9654+
decision: "accept",
9655+
}),
9656+
),
9657+
),
9658+
};
9659+
});
9660+
9661+
const approvalItem = (
9662+
events: ReadonlyArray<ProviderAdapter.ProviderAdapterV2Event>,
9663+
request: OrchestrationV2RuntimeRequest,
9664+
) =>
9665+
events.flatMap((event) =>
9666+
event.type === "turn_item.updated" &&
9667+
event.turnItem.type === "approval_request" &&
9668+
event.turnItem.requestId === request.id
9669+
? [event.turnItem]
9670+
: [],
9671+
)[0];
9672+
9673+
it.effect("answers without waiting for late paths or borrowing another item's paths", () =>
9674+
Effect.gen(function* () {
9675+
const { harness, answerNext } = yield* startApprovalTurn("file-approval-after", [
9676+
fileChange("started", nativeTurnId, "other-patch", [updateB]),
9677+
// Late details cannot arrive until the approval response has been sent.
9678+
...approval({ id: 0, turnId: nativeTurnId, itemId: "patch-a" }),
9679+
fileChange("started", nativeTurnId, "patch-a", [addA]),
9680+
fileChange("completed", nativeTurnId, "patch-a", [addA]),
9681+
fileChange("completed", nativeTurnId, "other-patch", [updateB]),
9682+
turnCompleted(nativeTurnId),
9683+
]);
9684+
const request = yield* answerNext;
9685+
yield* harness.firstTerminal;
9686+
9687+
const items = harness.events.flatMap((event) =>
9688+
event.type === "turn_item.updated" &&
9689+
event.turnItem.type === "approval_request" &&
9690+
event.turnItem.requestId === request.id
9691+
? [event.turnItem]
9692+
: [],
9693+
);
9694+
assert.equal(request.nativeRequestRef?.nativeId, "patch-a");
9695+
assert.isNotEmpty(items);
9696+
for (const item of items) {
9697+
assert.equal(item.requestKind, "file-change");
9698+
assert.isUndefined(item.prompt);
9699+
}
9700+
assert.deepEqual(
9701+
harness.terminalEvents().map((event) => event.status),
9702+
["completed"],
9703+
);
9704+
}).pipe(
9705+
Effect.scoped,
9706+
Effect.provide(
9707+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
9708+
),
9709+
),
9710+
);
9711+
9712+
it.effect("matches file paths to items approved out of order", () =>
9713+
Effect.gen(function* () {
9714+
const { harness, answerNext } = yield* startApprovalTurn("file-approval-concurrent", [
9715+
fileChange("started", nativeTurnId, "patch-a", [renameC, deleteE, addA]),
9716+
fileChange("started", nativeTurnId, "patch-b", [updateB]),
9717+
...approval({ id: 0, turnId: nativeTurnId, itemId: "patch-b" }),
9718+
fileChange("completed", nativeTurnId, "patch-b", [updateB]),
9719+
...approval({ id: 1, turnId: nativeTurnId, itemId: "patch-a" }),
9720+
fileChange("completed", nativeTurnId, "patch-a", [renameC, deleteE, addA]),
9721+
turnCompleted(nativeTurnId),
9722+
]);
9723+
const first = yield* answerNext;
9724+
const second = yield* answerNext;
9725+
yield* harness.firstTerminal;
9726+
9727+
assert.deepEqual(
9728+
[first, second].map((request) => [
9729+
request.nativeRequestRef?.nativeId,
9730+
approvalItem(harness.events, request)?.prompt,
9731+
]),
9732+
[
9733+
["patch-b", "update /w/b.ts"],
9734+
["patch-a", "add /w/a.ts\nupdate /w/c.ts -> /w/d.ts\ndelete /w/e.ts"],
9735+
],
9736+
);
9737+
assert.deepEqual(
9738+
harness.terminalEvents().map((event) => event.status),
9739+
["completed"],
9740+
);
9741+
}).pipe(
9742+
Effect.scoped,
9743+
Effect.provide(
9744+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
9745+
),
9746+
),
9747+
);
9748+
});
95369749
});

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,14 @@ const CODEX_PROVIDER = ProviderDriverKind.make("codex");
153153
export const CODEX_DRIVER_KIND = CODEX_PROVIDER;
154154
export const CODEX_DEFAULT_INSTANCE_ID = defaultInstanceIdForDriver(CODEX_DRIVER_KIND);
155155

156+
type CodexFileChangeKinds = Readonly<
157+
Record<string, CodexSchema.V2ItemStartedNotification__PatchChangeKind>
158+
>;
159+
156160
export function codexFileChangeApprovalPrompt(input: {
157161
readonly reason?: string | null;
158162
readonly grantRoot?: string | null;
159-
readonly fileChanges?: CodexSchema.ServerRequest__ApplyPatchApprovalParams["fileChanges"];
163+
readonly fileChanges?: CodexFileChangeKinds;
160164
}): string | undefined {
161165
const reason = input.reason?.trim();
162166
if (reason) return reason;
@@ -1126,6 +1130,8 @@ interface ActiveCodexTurnContext {
11261130
readonly rootNodeId: OrchestrationV2ExecutionNode["id"];
11271131
readonly subagent: CodexSubagentThreadContext | null;
11281132
readonly startedAt: DateTime.Utc;
1133+
// Modern approval requests omit paths; keep the kinds without retaining patch bodies.
1134+
readonly fileChangeKindsByItem: Map<string, CodexFileChangeKinds>;
11291135
readonly itemPositions: Map<
11301136
string,
11311137
{ readonly ordinal: number; readonly startedAt: DateTime.Utc }
@@ -2010,6 +2016,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
20102016
subagent: null,
20112017
startedAt: input.startedAt,
20122018
itemPositions: new Map(),
2019+
fileChangeKindsByItem: new Map(),
20132020
};
20142021
yield* Ref.update(limitedTurnItems, (current) => {
20152022
const next = new Map(current);
@@ -2581,6 +2588,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
25812588
subagent,
25822589
startedAt: turn.startedAt,
25832590
itemPositions: new Map(),
2591+
fileChangeKindsByItem: new Map(),
25842592
};
25852593
beginTurnTokenUsage(activeContext);
25862594
endSessionApprovalsOnModeChange(activeContext);
@@ -4659,6 +4667,14 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
46594667
);
46604668
}
46614669

4670+
if (payload.item.type === "fileChange") {
4671+
context.fileChangeKindsByItem.set(
4672+
payload.item.id,
4673+
Object.fromEntries(payload.item.changes.map(({ path, kind }) => [path, kind])),
4674+
);
4675+
return;
4676+
}
4677+
46624678
if (payload.item.type === "agentMessage") {
46634679
if (payload.item.phase !== "commentary") {
46644680
yield* Ref.update(finalAnswerItemIdsByTurn, (current) => {
@@ -4875,6 +4891,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
48754891
}
48764892

48774893
if (payload.item.type === "fileChange") {
4894+
context.fileChangeKindsByItem.delete(payload.item.id);
48784895
const artifacts = yield* buildFileChangeArtifacts(context, payload.item);
48794896
if (artifacts === null) {
48804897
return;
@@ -5127,7 +5144,11 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
51275144
nativeItemId: payload.itemId,
51285145
nativeRequestId: payload.itemId,
51295146
requestKind: "file-change",
5130-
prompt: codexFileChangeApprovalPrompt(payload) ?? null,
5147+
prompt:
5148+
codexFileChangeApprovalPrompt({
5149+
...payload,
5150+
fileChanges: context.fileChangeKindsByItem.get(payload.itemId) ?? {},
5151+
}) ?? null,
51315152
});
51325153
const decision = yield* Deferred.make<ProviderApprovalDecision, never>();
51335154
yield* Ref.update(pendingRuntimeRequests, (current) => {
@@ -5752,6 +5773,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
57525773
if (current !== input.context) {
57535774
return false;
57545775
}
5776+
input.context.fileChangeKindsByItem.clear();
57555777
const providerRetry = yield* Ref.modify(providerRetries, (current) => {
57565778
const retry = current.get(input.context.providerTurnId);
57575779
if (retry === undefined) {
@@ -6050,6 +6072,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
60506072
subagent: null,
60516073
startedAt: now,
60526074
itemPositions: new Map(),
6075+
fileChangeKindsByItem: new Map(),
60536076
};
60546077
const providerTurn = {
60556078
id: context.providerTurnId,

‎apps/server/src/orchestration-v2/testkit/fixtures/tool_call_restricted_granular/codex_output.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { assert } from "@effect/vitest";
12
import type { ProviderReplayTranscript } from "@t3tools/contracts";
23

34
import type { OrchestratorV2ScenarioResult } from "../../OrchestratorScenario.ts";
@@ -33,4 +34,10 @@ export function assertToolCallRestrictedGranularOutput(
3334
assertRuntimeRequestKinds(projection, ["file-change"]);
3435
assertAllRuntimeRequestsResolved(projection);
3536
assertUserMessagesInclude(projection, [TOOL_CALL_WRITE_PROMPT]);
37+
38+
const approval = projection.turnItems.find((item) => item.type === "approval_request");
39+
assert.equal(
40+
approval?.prompt,
41+
"add /home/replay-user/t3code/packages/effect-codex-app-server/.codex-probe-write-action.txt",
42+
);
3643
}

0 commit comments

Comments
 (0)