Skip to content

Commit 2a0cdc8

Browse files
fix(server): scope Codex approval paths to their native turn
1 parent 1a5f57a commit 2a0cdc8

1 file changed

Lines changed: 11 additions & 46 deletions

File tree

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

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ 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-
// Change kind by path. Legacy apply-patch payloads fit as they are.
157156
type CodexFileChangeKinds = Readonly<
158157
Record<string, CodexSchema.V2ItemStartedNotification__PatchChangeKind>
159158
>;
@@ -1131,6 +1130,8 @@ interface ActiveCodexTurnContext {
11311130
readonly rootNodeId: OrchestrationV2ExecutionNode["id"];
11321131
readonly subagent: CodexSubagentThreadContext | null;
11331132
readonly startedAt: DateTime.Utc;
1133+
// Modern approval requests omit paths; keep the kinds without retaining patch bodies.
1134+
readonly fileChangeKindsByItem: Map<string, CodexFileChangeKinds>;
11341135
readonly itemPositions: Map<
11351136
string,
11361137
{ readonly ordinal: number; readonly startedAt: DateTime.Utc }
@@ -1901,11 +1902,6 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
19011902
const offeredContinuationItemsByTurn = yield* Ref.make(new Map<string, Set<string>>());
19021903
const finalAnswerItemIdsByTurn = yield* Ref.make(new Map<string, Set<string>>());
19031904
const completedFinalAnswerTextsByTurn = yield* Ref.make(new Map<string, Set<string>>());
1904-
// File change kinds from item/started, by turn and item, so the item's
1905-
// approval request can list its paths. Diffs are not kept.
1906-
const fileChangeKindsByTurn = yield* Ref.make(
1907-
new Map<string, Map<string, CodexFileChangeKinds>>(),
1908-
);
19091905
// Native completion and the interrupt timeout share one finalization
19101906
// path. Serialize the race so only one can publish terminal events.
19111907
const turnTerminalizationPermit = yield* Semaphore.make(1);
@@ -2019,6 +2015,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
20192015
subagent: null,
20202016
startedAt: input.startedAt,
20212017
itemPositions: new Map(),
2018+
fileChangeKindsByItem: new Map(),
20222019
};
20232020
yield* Ref.update(limitedTurnItems, (current) => {
20242021
const next = new Map(current);
@@ -2590,6 +2587,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
25902587
subagent,
25912588
startedAt: turn.startedAt,
25922589
itemPositions: new Map(),
2590+
fileChangeKindsByItem: new Map(),
25932591
};
25942592
beginTurnTokenUsage(activeContext);
25952593
endSessionApprovalsOnModeChange(activeContext);
@@ -4665,16 +4663,10 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
46654663
}
46664664

46674665
if (payload.item.type === "fileChange") {
4668-
const kinds = Object.fromEntries(
4669-
payload.item.changes.map((change) => [change.path, change.kind]),
4666+
context.fileChangeKindsByItem.set(
4667+
payload.item.id,
4668+
Object.fromEntries(payload.item.changes.map(({ path, kind }) => [path, kind])),
46704669
);
4671-
yield* Ref.update(fileChangeKindsByTurn, (current) => {
4672-
const updated = new Map(current);
4673-
const items = new Map(updated.get(payload.turnId) ?? []);
4674-
items.set(payload.item.id, kinds);
4675-
updated.set(payload.turnId, items);
4676-
return updated;
4677-
});
46784670
return;
46794671
}
46804672

@@ -4782,23 +4774,6 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
47824774
yield* updateSubagentStates({ item: payload.item });
47834775
return;
47844776
}
4785-
if (payload.item.type === "fileChange") {
4786-
yield* Ref.update(fileChangeKindsByTurn, (current) => {
4787-
const items = current.get(payload.turnId);
4788-
if (items === undefined || !items.has(payload.item.id)) {
4789-
return current;
4790-
}
4791-
const remaining = new Map(items);
4792-
remaining.delete(payload.item.id);
4793-
const updated = new Map(current);
4794-
if (remaining.size === 0) {
4795-
updated.delete(payload.turnId);
4796-
} else {
4797-
updated.set(payload.turnId, remaining);
4798-
}
4799-
return updated;
4800-
});
4801-
}
48024777
const resolved = yield* resolveItemEventContext(payload.turnId);
48034778
if (resolved === undefined) {
48044779
return;
@@ -4911,6 +4886,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
49114886
}
49124887

49134888
if (payload.item.type === "fileChange") {
4889+
context.fileChangeKindsByItem.delete(payload.item.id);
49144890
const artifacts = yield* buildFileChangeArtifacts(context, payload.item);
49154891
if (artifacts === null) {
49164892
return;
@@ -5158,11 +5134,6 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
51585134
} satisfies CodexSchema.FileChangeRequestApprovalResponse;
51595135
}
51605136

5161-
// Codex sends item/started before this request, so the item's paths
5162-
// are cached. Without them the prompt falls back as before.
5163-
const fileChanges = (yield* Ref.get(fileChangeKindsByTurn))
5164-
.get(payload.turnId)
5165-
?.get(payload.itemId);
51665137
const artifacts = yield* buildApprovalRequestArtifacts({
51675138
context,
51685139
nativeItemId: payload.itemId,
@@ -5171,7 +5142,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
51715142
prompt:
51725143
codexFileChangeApprovalPrompt({
51735144
...payload,
5174-
...(fileChanges === undefined ? {} : { fileChanges }),
5145+
fileChanges: context.fileChangeKindsByItem.get(payload.itemId) ?? {},
51755146
}) ?? null,
51765147
});
51775148
const decision = yield* Deferred.make<ProviderApprovalDecision, never>();
@@ -5797,6 +5768,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
57975768
if (current !== input.context) {
57985769
return false;
57995770
}
5771+
input.context.fileChangeKindsByItem.clear();
58005772
const providerRetry = yield* Ref.modify(providerRetries, (current) => {
58015773
const retry = current.get(input.context.providerTurnId);
58025774
if (retry === undefined) {
@@ -5994,14 +5966,6 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
59945966
updated.delete(input.nativeTurnId);
59955967
return updated;
59965968
});
5997-
yield* Ref.update(fileChangeKindsByTurn, (current) => {
5998-
if (!current.has(input.nativeTurnId)) {
5999-
return current;
6000-
}
6001-
const updated = new Map(current);
6002-
updated.delete(input.nativeTurnId);
6003-
return updated;
6004-
});
60055969
if (input.context.nativeStartReady !== undefined) {
60065970
yield* Deferred.succeed(input.context.nativeStartReady, undefined);
60075971
}
@@ -6103,6 +6067,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
61036067
subagent: null,
61046068
startedAt: now,
61056069
itemPositions: new Map(),
6070+
fileChangeKindsByItem: new Map(),
61066071
};
61076072
const providerTurn = {
61086073
id: context.providerTurnId,

0 commit comments

Comments
 (0)