Skip to content

Commit 8d9ec2f

Browse files
fix(server): Codex restores T3 context after the compacting turn completes
Injecting additionalContext mid-turn after a contextCompaction made Codex queue it as pending input and sample again, which with a tight auto-compact limit compacted again and never completed. Defer the restore per native thread until that thread's turn/completed. Fixes #25891 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6625caa commit 8d9ec2f

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,8 +1646,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
16461646
NonNullable<CodexTurnStartParamsWithCollaborationMode["additionalContext"]>
16471647
>(),
16481648
);
1649-
// Codex drops client developer messages during compaction but only
1650-
// resends additionalContext when it changes.
1649+
const threadsAwaitingContextRestore = yield* Ref.make(new Set<string>());
1650+
// Codex drops client developer messages during compaction but only resends additionalContext
1651+
// when it changes. Restore after turn/completed: items injected mid-turn make the turn sample again.
16511652
const restoreAdditionalContext = Effect.fn("CodexAdapterV2.restoreAdditionalContext")(
16521653
function* (threadId: string) {
16531654
const context = (yield* Ref.get(additionalContextByThread)).get(threadId);
@@ -4738,9 +4739,8 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
47384739
yield* client.handleServerNotification("item/completed", (payload) =>
47394740
Effect.gen(function* () {
47404741
if (payload.item.type === "contextCompaction")
4741-
// Fork requests off the notification reader so it can read their responses.
4742-
yield* restoreAdditionalContext(payload.threadId).pipe(
4743-
Effect.forkIn(scope, { startImmediately: true }),
4742+
yield* Ref.update(threadsAwaitingContextRestore, (current) =>
4743+
new Set(current).add(payload.threadId),
47444744
);
47454745
if (
47464746
(payload.item.type === "subAgentActivity" ||
@@ -6094,6 +6094,17 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
60946094

60956095
yield* client.handleServerNotification("turn/completed", (payload) =>
60966096
Effect.gen(function* () {
6097+
const compacted = yield* Ref.modify(threadsAwaitingContextRestore, (current) => {
6098+
if (!current.has(payload.threadId)) return [false, current] as const;
6099+
const next = new Set(current);
6100+
next.delete(payload.threadId);
6101+
return [true, next] as const;
6102+
});
6103+
if (compacted)
6104+
// Fork requests off the notification reader so it can read their responses.
6105+
yield* restoreAdditionalContext(payload.threadId).pipe(
6106+
Effect.forkIn(scope, { startImmediately: true }),
6107+
);
60976108
const context = (yield* Ref.get(activeTurns)).get(payload.turn.id);
60986109
if (context === undefined) {
60996110
return;

0 commit comments

Comments
 (0)