The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 22:15 UTC
fix(server): keep Codex loaded when reasoning effort changes (#32915)
Co-authored-by: GPT-6.1 Sol at high effort <noreply@openai.com>
Alb11747 committed
Nov 11, 2026 at 18:07 UTC
669aa32e0d98fc2919d540690c2fac05c36ced68
4 files changed
+149
-10
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+8
@@ -188,6 +188,13 @@ function codexFileChangeApprovalPrompt(input: {
188
return input.grantRoot?.trim() || undefined;
189
}
190
191
+export function normalizeCodexThreadLoadModelSelection(selection: ModelSelection): ModelSelection {
192
+ // Codex applies effort on turn/start; resume discards its pinned request effort.
193
+ const { options, ...rest } = selection;
194
+ const loadOptions = options?.filter((option) => option.id !== "reasoningEffort");
195
+ return { ...rest, ...(loadOptions?.length ? { options: loadOptions } : {}) };
196
+}
197
+
198
// Reasoning-effort changes preserve the tokenizer and context window; other changes need fresh telemetry.
199
export function canReuseCodexContextUsage(previous: ModelSelection, next: ModelSelection): boolean {
200
return modelSelectionsEqual(
@@ -7356,6 +7363,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
7363
providerSession: session,
7364
events: Stream.fromEffectRepeat(Queue.take(events)),
7365
canReuseContextUsage: canReuseCodexContextUsage,
7366
+ normalizeThreadLoadModelSelection: normalizeCodexThreadLoadModelSelection,
7367
// Idle release can beat delayed subagent resumes: Codex reports them completed between turns with no resume-expected signal.
7368
hasPendingBackgroundWork: Effect.gen(function* () {
7369
for (const items of (yield* Ref.get(runningCommandItemsByTurn)).values()) {
apps/server/src/orchestration-v2/ProviderSessionManager.test.ts
+128
-9
@@ -42,7 +42,10 @@ import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderS
42
import * as McpSessionRegistry from "../mcp/McpSessionRegistry.ts";
43
import * as SqlitePersistence from "../persistence/Sqlite.ts";
44
import * as ServerSettings from "../serverSettings.ts";
45
-import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
45
+import {
46
+ CodexProviderCapabilitiesV2,
47
+ normalizeCodexThreadLoadModelSelection,
48
+} from "./Adapters/CodexAdapterV2.ts";
49
import * as EventSink from "./EventSink.ts";
50
import * as EventStore from "./EventStore.ts";
51
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
@@ -427,6 +430,7 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
430
state: Ref.Ref<TestProviderRuntimeState>,
431
options: {
432
readonly failEventStream?: boolean;
433
+ readonly normalizeThreadLoadModelSelection?: (selection: ModelSelection) => ModelSelection;
434
readonly capabilities?: OrchestrationV2ProviderCapabilities;
435
readonly extensionStatuses?: OrchestrationV2ProviderSession["extensionStatuses"];
436
readonly mcpConfigs?: Ref.Ref<
@@ -513,6 +517,9 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
517
instanceId: ProviderInstanceId.make("codex"),
518
driver: CODEX_DRIVER,
519
providerSessionId: input.providerSessionId,
520
+ ...(options.normalizeThreadLoadModelSelection === undefined
521
+ ? {}
522
+ : { normalizeThreadLoadModelSelection: options.normalizeThreadLoadModelSelection }),
523
get providerSession() {
524
return session;
525
},
@@ -581,6 +588,7 @@ function layerTest(input: {
588
readonly idleTimeoutMs: number;
589
readonly maxIdlePinMs?: number;
590
readonly failEventStream?: boolean;
591
+ readonly normalizeThreadLoadModelSelection?: (selection: ModelSelection) => ModelSelection;
592
readonly capabilities?: OrchestrationV2ProviderCapabilities;
593
readonly extensionStatuses?: OrchestrationV2ProviderSession["extensionStatuses"];
594
readonly mcpConfigs?: Ref.Ref<
@@ -623,6 +631,9 @@ function layerTest(input: {
631
const layerRegistry = Layer.unwrap(
632
makeProviderAdapter(input.state, {
633
failEventStream: input.failEventStream ?? false,
634
+ ...(input.normalizeThreadLoadModelSelection === undefined
635
+ ? {}
636
+ : { normalizeThreadLoadModelSelection: input.normalizeThreadLoadModelSelection }),
637
...(input.capabilities === undefined ? {} : { capabilities: input.capabilities }),
638
...(input.extensionStatuses === undefined
639
? {}
@@ -4054,9 +4065,15 @@ it.effect(
4065
}),
4066
);
4067
4057
-it.effect(
4058
- "ProviderSessionManagerV2 opens one shared runtime, broadcasts events, and detaches threads independently",
4059
- () =>
4068
+it.effect.each([
4069
+ { name: "default load key", normalizeThreadLoadModelSelection: undefined },
4070
+ {
4071
+ name: "Codex load key",
4072
+ normalizeThreadLoadModelSelection: normalizeCodexThreadLoadModelSelection,
4073
+ },
4074
+])(
4075
+ "ProviderSessionManagerV2 opens one shared runtime, broadcasts events, and detaches threads independently ($name)",
4076
+ ({ normalizeThreadLoadModelSelection }) =>
4077
Effect.gen(function* () {
4078
const state = yield* Ref.make(emptyState);
4079
const settings = yield* ServerSettings.ServerSettingsService.pipe(
@@ -4153,25 +4170,91 @@ it.effect(
4170
const resumeSecondThread = secondRuntime.resumeThread({
4171
providerThread: secondProviderThread,
4172
threadId: secondThreadId,
4156
- modelSelection,
4173
+ modelSelection: {
4174
+ ...modelSelection,
4175
+ options: [{ id: "reasoningEffort", value: "medium" }],
4176
+ },
4177
runtimePolicy,
4178
});
4179
yield* resumeSecondThread;
4180
yield* resumeSecondThread;
4181
assert.equal((yield* Ref.get(state)).resumeCount, 1);
4182
+ let resumeCount = 1;
4183
+ const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
4184
+ const appThread = (yield* projectionStore.getThreadProjection(secondThreadId)).thread;
4185
+ const turnEvents = (yield* Ref.get(state)).eventQueues.get(String(providerSessionId));
4186
+ assert.isDefined(turnEvents);
4187
+ for (const [index, effort] of ["medium", "high", "medium"].entries()) {
4188
+ const selection = {
4189
+ ...modelSelection,
4190
+ options: [{ id: "reasoningEffort", value: effort }],
4191
+ } satisfies ModelSelection;
4192
+ yield* secondRuntime.resumeThread({
4193
+ providerThread: secondProviderThread,
4194
+ threadId: secondThreadId,
4195
+ modelSelection: selection,
4196
+ runtimePolicy,
4197
+ });
4198
+ const ordinal = index + 1;
4199
+ const runId = idAllocator.derive.run({ threadId: secondThreadId, ordinal });
4200
+ yield* secondRuntime.startTurn({
4201
+ appThread,
4202
+ threadId: secondThreadId,
4203
+ runId,
4204
+ runOrdinal: ordinal,
4205
+ providerTurnOrdinal: ordinal,
4206
+ attemptId: idAllocator.derive.runAttempt({ runId, attemptOrdinal: 1 }),
4207
+ rootNodeId: idAllocator.derive.rootNode({ runId }),
4208
+ providerThread: secondProviderThread,
4209
+ message: {
4210
+ createdBy: "user",
4211
+ creationSource: "web",
4212
+ messageId: yield* idAllocator.allocate.message({
4213
+ threadId: secondThreadId,
4214
+ ordinal,
4215
+ }),
4216
+ text: "turn",
4217
+ attachments: [],
4218
+ },
4219
+ modelSelection: selection,
4220
+ runtimePolicy,
4221
+ });
4222
+ const subscription = yield* secondRuntime.subscribeEvents!;
4223
+ const terminal = yield* subscription.events.pipe(
4224
+ Stream.filter((event) => event.type === "turn.terminal"),
4225
+ Stream.runHead,
4226
+ Effect.forkScoped,
4227
+ );
4228
+ yield* Queue.offer(turnEvents!, {
4229
+ type: "turn.terminal",
4230
+ driver: CODEX_DRIVER,
4231
+ providerThreadId: secondProviderThread.id,
4232
+ providerTurnId: idAllocator.derive.providerTurn({
4233
+ driver: CODEX_DRIVER,
4234
+ nativeTurnId: `native-effort-turn-${ordinal}`,
4235
+ }),
4236
+ runOrdinal: ordinal,
4237
+ status: "completed",
4238
+ failure: null,
4239
+ threadDisposition: "reusable",
4240
+ });
4241
+ yield* Fiber.join(terminal);
4242
+ if (normalizeThreadLoadModelSelection === undefined && index > 0) resumeCount++;
4243
+ assert.equal((yield* Ref.get(state)).resumeCount, resumeCount, `${effort} effort reload`);
4244
+ }
4245
yield* secondRuntime.resumeThread({
4246
providerThread: secondProviderThread,
4247
threadId: secondThreadId,
4248
modelSelection: { ...modelSelection, model: "gpt-5.4-mini" },
4249
runtimePolicy,
4250
});
4168
- assert.equal((yield* Ref.get(state)).resumeCount, 2);
4251
+ assert.equal((yield* Ref.get(state)).resumeCount, ++resumeCount);
4252
yield* resumeSecondThread;
4170
- assert.equal((yield* Ref.get(state)).resumeCount, 3);
4253
+ assert.equal((yield* Ref.get(state)).resumeCount, ++resumeCount);
4254
// regression: #26722 a Linked environments change reloads only the idle thread's tools.
4255
yield* settings.updateSettings({ linkedEnvironmentsEnabled: true });
4256
yield* resumeSecondThread;
4174
- assert.equal((yield* Ref.get(state)).resumeCount, 4);
4257
+ assert.equal((yield* Ref.get(state)).resumeCount, ++resumeCount);
4258
assert.deepEqual((yield* Ref.get(state)).unloadedNativeThreadIds, ["native-thread"]);
4259
const subscribe = firstRuntime.subscribeEvents;
4260
assert.isDefined(subscribe);
@@ -4204,10 +4287,43 @@ it.effect(
4287
runtimePolicy,
4288
});
4289
yield* resumeSecondThread;
4207
- assert.equal((yield* Ref.get(state)).resumeCount, 5);
4290
+ assert.equal((yield* Ref.get(state)).resumeCount, ++resumeCount);
4291
4292
assert.deepEqual((yield* Ref.get(state)).unloadedNativeThreadIds, ["native-thread"]);
4293
4294
+ const reloadCases = [
4295
+ {
4296
+ name: "other model options",
4297
+ providerThread: secondProviderThread,
4298
+ selection: {
4299
+ ...modelSelection,
4300
+ options: [{ id: "serviceTier", value: "priority" }],
4301
+ },
4302
+ policy: runtimePolicy,
4303
+ },
4304
+ {
4305
+ name: "runtime policy",
4306
+ providerThread: secondProviderThread,
4307
+ selection: modelSelection,
4308
+ policy: { ...runtimePolicy, interactionMode: "plan" },
4309
+ },
4310
+ ] satisfies ReadonlyArray<{
4311
+ name: string;
4312
+ providerThread: OrchestrationV2ProviderThread;
4313
+ selection: ModelSelection;
4314
+ policy: ProviderAdapter.ProviderAdapterV2RuntimePolicy;
4315
+ }>;
4316
+ for (const { name, providerThread, selection, policy } of reloadCases) {
4317
+ const before = (yield* Ref.get(state)).resumeCount;
4318
+ yield* secondRuntime.resumeThread({
4319
+ providerThread,
4320
+ threadId: secondThreadId,
4321
+ modelSelection: selection,
4322
+ runtimePolicy: policy,
4323
+ });
4324
+ assert.equal((yield* Ref.get(state)).resumeCount, before + 1, `${name} did not reload`);
4325
+ }
4326
+
4327
yield* manager.detach({ providerSessionId, threadId: firstThreadId });
4328
assert.isTrue(Option.isSome(yield* manager.get(providerSessionId)));
4329
assert.equal((yield* Ref.get(state)).closeCount, 0);
@@ -4228,6 +4344,9 @@ it.effect(
4344
layerTest({
4345
state,
4346
idleTimeoutMs: 1000,
4347
+ ...(normalizeThreadLoadModelSelection === undefined
4348
+ ? {}
4349
+ : { normalizeThreadLoadModelSelection }),
4350
serverSettingsLayer: Layer.succeed(ServerSettings.ServerSettingsService, settings),
4351
}),
4352
),
apps/server/src/orchestration-v2/ProviderSessionManager.ts
+11
-1
@@ -329,15 +329,21 @@ function providerThreadRuntimeKey(
329
}
330
331
function providerThreadLoadKey(input: {
332
+ readonly runtime: ProviderAdapter.ProviderAdapterV2SessionRuntime;
333
readonly providerThread: Parameters<
334
ProviderAdapter.ProviderAdapterV2SessionRuntime["resumeThread"]
335
>[0]["providerThread"];
336
readonly modelSelection?: ModelSelection;
337
readonly runtimePolicy?: ProviderAdapter.ProviderAdapterV2RuntimePolicy;
338
}): string {
339
+ const modelSelection =
340
+ input.modelSelection === undefined
341
+ ? undefined
342
+ : (input.runtime.normalizeThreadLoadModelSelection?.(input.modelSelection) ??
343
+ input.modelSelection);
344
return JSON.stringify({
345
providerThread: providerThreadRuntimeKey(input.providerThread),
340
- modelSelection: input.modelSelection ?? null,
346
+ modelSelection: modelSelection ?? null,
347
runtimePolicy: input.runtimePolicy ?? null,
348
});
349
}
@@ -1984,6 +1990,7 @@ export const layerWithOptions = (
1990
providerSessionId,
1991
threadId: input.threadId,
1992
providerThreadKey: providerThreadLoadKey({
1993
+ runtime,
1994
providerThread,
1995
modelSelection: input.modelSelection,
1996
runtimePolicy: input.runtimePolicy,
@@ -1999,6 +2006,7 @@ export const layerWithOptions = (
2006
return runtime.resumeThread(input);
2007
}
2008
const providerThreadKey = providerThreadLoadKey({
2009
+ runtime,
2010
providerThread: input.providerThread,
2011
...(input.modelSelection === undefined
2012
? {}
@@ -2043,6 +2051,7 @@ export const layerWithOptions = (
2051
providerSessionId,
2052
threadId,
2053
providerThreadKey: providerThreadLoadKey({
2054
+ runtime,
2055
providerThread,
2056
...(input.modelSelection === undefined
2057
? {}
@@ -2072,6 +2081,7 @@ export const layerWithOptions = (
2081
providerSessionId,
2082
threadId: input.targetThreadId,
2083
providerThreadKey: providerThreadLoadKey({
2084
+ runtime,
2085
providerThread,
2086
...(input.modelSelection === undefined
2087
? {}
packages/provider-core/src/server/ProviderAdapter.ts
+2
@@ -654,6 +654,8 @@ export interface ProviderAdapterV2SessionRuntime {
654
cwd?: string | null,
655
) => number | undefined;
656
readonly canReuseContextUsage?: (previous: ModelSelection, next: ModelSelection) => boolean;
657
+ /** Excludes turn-scoped options from native thread load matching. */
658
+ readonly normalizeThreadLoadModelSelection?: (selection: ModelSelection) => ModelSelection;
659
readonly ensureThread: (
660
input: ProviderAdapterV2EnsureThreadInput,
661
) => Effect.Effect<OrchestrationV2ProviderThread, ProviderAdapterV2Error>;