Skip to content

Commit e708814

Browse files
Alb11747codex
andcommitted
fix(server): let adapters normalize thread load selections
Keep Codex turn-scoped effort semantics at the adapter boundary. Use the optional normalization hook at every native load-key site and verify both opt-in reuse and unchanged default reload behavior. Co-Authored-By: GPT-6.1 Sol at high effort <[email protected]> Co-Authored-By: GPT-6.1 Sol at xhigh effort, review subagent for adapter-owned thread reuse <[email protected]>
1 parent 24d817f commit e708814

4 files changed

Lines changed: 51 additions & 34 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ function codexFileChangeApprovalPrompt(input: {
185185
return input.grantRoot?.trim() || undefined;
186186
}
187187

188+
export function normalizeCodexThreadLoadModelSelection(selection: ModelSelection): ModelSelection {
189+
// Codex applies effort on turn/start; resume discards its pinned request effort.
190+
const { options, ...rest } = selection;
191+
const loadOptions = options?.filter((option) => option.id !== "reasoningEffort");
192+
return { ...rest, ...(loadOptions?.length ? { options: loadOptions } : {}) };
193+
}
194+
188195
// Reasoning-effort changes preserve the tokenizer and context window; other changes need fresh telemetry.
189196
export function canReuseCodexContextUsage(previous: ModelSelection, next: ModelSelection): boolean {
190197
return modelSelectionsEqual(
@@ -7046,6 +7053,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
70467053
providerSession: session,
70477054
events: Stream.fromEffectRepeat(Queue.take(events)),
70487055
canReuseContextUsage: canReuseCodexContextUsage,
7056+
normalizeThreadLoadModelSelection: normalizeCodexThreadLoadModelSelection,
70497057
// Idle release can beat delayed subagent resumes: Codex reports them completed between turns with no resume-expected signal.
70507058
hasPendingBackgroundWork: Effect.gen(function* () {
70517059
for (const items of (yield* Ref.get(runningCommandItemsByTurn)).values()) {

‎apps/server/src/orchestration-v2/ProviderSessionManager.test.ts‎

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderS
4141
import * as McpSessionRegistry from "../mcp/McpSessionRegistry.ts";
4242
import * as SqlitePersistence from "../persistence/Sqlite.ts";
4343
import * as ServerSettings from "../serverSettings.ts";
44-
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
44+
import {
45+
CodexProviderCapabilitiesV2,
46+
normalizeCodexThreadLoadModelSelection,
47+
} from "./Adapters/CodexAdapterV2.ts";
4548
import * as EventSink from "./EventSink.ts";
4649
import * as EventStore from "./EventStore.ts";
4750
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
@@ -422,6 +425,7 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
422425
state: Ref.Ref<TestProviderRuntimeState>,
423426
options: {
424427
readonly failEventStream?: boolean;
428+
readonly normalizeThreadLoadModelSelection?: (selection: ModelSelection) => ModelSelection;
425429
readonly capabilities?: OrchestrationV2ProviderCapabilities;
426430
readonly extensionStatuses?: OrchestrationV2ProviderSession["extensionStatuses"];
427431
readonly mcpConfigs?: Ref.Ref<
@@ -508,6 +512,9 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
508512
instanceId: ProviderInstanceId.make("codex"),
509513
driver: CODEX_DRIVER,
510514
providerSessionId: input.providerSessionId,
515+
...(options.normalizeThreadLoadModelSelection === undefined
516+
? {}
517+
: { normalizeThreadLoadModelSelection: options.normalizeThreadLoadModelSelection }),
511518
get providerSession() {
512519
return session;
513520
},
@@ -576,6 +583,7 @@ function layerTest(input: {
576583
readonly idleTimeoutMs: number;
577584
readonly maxIdlePinMs?: number;
578585
readonly failEventStream?: boolean;
586+
readonly normalizeThreadLoadModelSelection?: (selection: ModelSelection) => ModelSelection;
579587
readonly capabilities?: OrchestrationV2ProviderCapabilities;
580588
readonly extensionStatuses?: OrchestrationV2ProviderSession["extensionStatuses"];
581589
readonly mcpConfigs?: Ref.Ref<
@@ -618,6 +626,9 @@ function layerTest(input: {
618626
const layerRegistry = Layer.unwrap(
619627
makeProviderAdapter(input.state, {
620628
failEventStream: input.failEventStream ?? false,
629+
...(input.normalizeThreadLoadModelSelection === undefined
630+
? {}
631+
: { normalizeThreadLoadModelSelection: input.normalizeThreadLoadModelSelection }),
621632
...(input.capabilities === undefined ? {} : { capabilities: input.capabilities }),
622633
...(input.extensionStatuses === undefined
623634
? {}
@@ -3626,9 +3637,15 @@ it.effect("ProviderSessionManagerV2 persists session-scoped runtime requests wit
36263637
}),
36273638
);
36283639

3629-
it.effect(
3630-
"ProviderSessionManagerV2 opens one shared runtime, broadcasts events, and detaches threads independently",
3631-
() =>
3640+
it.effect.each([
3641+
{ name: "default load key", normalizeThreadLoadModelSelection: undefined },
3642+
{
3643+
name: "Codex load key",
3644+
normalizeThreadLoadModelSelection: normalizeCodexThreadLoadModelSelection,
3645+
},
3646+
])(
3647+
"ProviderSessionManagerV2 opens one shared runtime, broadcasts events, and detaches threads independently ($name)",
3648+
({ normalizeThreadLoadModelSelection }) =>
36323649
Effect.gen(function* () {
36333650
const state = yield* Ref.make(emptyState);
36343651
const settings = yield* ServerSettings.ServerSettingsService.pipe(
@@ -3734,6 +3751,7 @@ it.effect(
37343751
yield* resumeSecondThread;
37353752
yield* resumeSecondThread;
37363753
assert.equal((yield* Ref.get(state)).resumeCount, 1);
3754+
let resumeCount = 1;
37373755
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
37383756
const appThread = (yield* projectionStore.getThreadProjection(secondThreadId)).thread;
37393757
const turnEvents = (yield* Ref.get(state)).eventQueues.get(String(providerSessionId));
@@ -3793,21 +3811,22 @@ it.effect(
37933811
threadDisposition: "reusable",
37943812
});
37953813
yield* Fiber.join(terminal);
3796-
assert.equal((yield* Ref.get(state)).resumeCount, 1, `${effort} effort reloaded Codex`);
3814+
if (normalizeThreadLoadModelSelection === undefined && index > 0) resumeCount++;
3815+
assert.equal((yield* Ref.get(state)).resumeCount, resumeCount, `${effort} effort reload`);
37973816
}
37983817
yield* secondRuntime.resumeThread({
37993818
providerThread: secondProviderThread,
38003819
threadId: secondThreadId,
38013820
modelSelection: { ...modelSelection, model: "gpt-5.4-mini" },
38023821
runtimePolicy,
38033822
});
3804-
assert.equal((yield* Ref.get(state)).resumeCount, 2);
3823+
assert.equal((yield* Ref.get(state)).resumeCount, ++resumeCount);
38053824
yield* resumeSecondThread;
3806-
assert.equal((yield* Ref.get(state)).resumeCount, 3);
3825+
assert.equal((yield* Ref.get(state)).resumeCount, ++resumeCount);
38073826
// regression: #26722 a Linked environments change reloads only the idle thread's tools.
38083827
yield* settings.updateSettings({ linkedEnvironmentsEnabled: true });
38093828
yield* resumeSecondThread;
3810-
assert.equal((yield* Ref.get(state)).resumeCount, 4);
3829+
assert.equal((yield* Ref.get(state)).resumeCount, ++resumeCount);
38113830
assert.deepEqual((yield* Ref.get(state)).unloadedNativeThreadIds, ["native-thread"]);
38123831
const subscribe = firstRuntime.subscribeEvents;
38133832
assert.isDefined(subscribe);
@@ -3840,16 +3859,10 @@ it.effect(
38403859
runtimePolicy,
38413860
});
38423861
yield* resumeSecondThread;
3843-
assert.equal((yield* Ref.get(state)).resumeCount, 5);
3862+
assert.equal((yield* Ref.get(state)).resumeCount, ++resumeCount);
38443863

38453864
assert.deepEqual((yield* Ref.get(state)).unloadedNativeThreadIds, ["native-thread"]);
38463865

3847-
const otherDriver = ProviderDriverKind.make("claudeAgent");
3848-
const otherProviderThread = {
3849-
...secondProviderThread,
3850-
driver: otherDriver,
3851-
nativeThreadRef: { driver: otherDriver, nativeId: "native-other", strength: "strong" },
3852-
} satisfies OrchestrationV2ProviderThread;
38533866
const reloadCases = [
38543867
{
38553868
name: "other model options",
@@ -3866,15 +3879,6 @@ it.effect(
38663879
selection: modelSelection,
38673880
policy: { ...runtimePolicy, interactionMode: "plan" },
38683881
},
3869-
...["medium", "high"].map((effort) => ({
3870-
name: `non-Codex ${effort} effort`,
3871-
providerThread: otherProviderThread,
3872-
selection: {
3873-
...modelSelection,
3874-
options: [{ id: "reasoningEffort", value: effort }],
3875-
},
3876-
policy: runtimePolicy,
3877-
})),
38783882
] satisfies ReadonlyArray<{
38793883
name: string;
38803884
providerThread: OrchestrationV2ProviderThread;
@@ -3912,6 +3916,9 @@ it.effect(
39123916
layerTest({
39133917
state,
39143918
idleTimeoutMs: 1000,
3919+
...(normalizeThreadLoadModelSelection === undefined
3920+
? {}
3921+
: { normalizeThreadLoadModelSelection }),
39153922
serverSettingsLayer: Layer.succeed(ServerSettings.ServerSettingsService, settings),
39163923
}),
39173924
),

‎apps/server/src/orchestration-v2/ProviderSessionManager.ts‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -305,22 +305,18 @@ function providerThreadRuntimeKey(
305305
}
306306

307307
function providerThreadLoadKey(input: {
308+
readonly runtime: ProviderAdapter.ProviderAdapterV2SessionRuntime;
308309
readonly providerThread: Parameters<
309310
ProviderAdapter.ProviderAdapterV2SessionRuntime["resumeThread"]
310311
>[0]["providerThread"];
311312
readonly modelSelection?: ModelSelection;
312313
readonly runtimePolicy?: ProviderAdapter.ProviderAdapterV2RuntimePolicy;
313314
}): string {
314-
let modelSelection = input.modelSelection;
315-
if (input.providerThread.driver === "codex" && modelSelection !== undefined) {
316-
// Codex applies effort on turn/start. Resuming here discards its pinned request effort.
317-
const { options, ...selection } = modelSelection;
318-
const loadOptions = options?.filter((option) => option.id !== "reasoningEffort");
319-
modelSelection = {
320-
...selection,
321-
...(loadOptions?.length ? { options: loadOptions } : {}),
322-
};
323-
}
315+
const modelSelection =
316+
input.modelSelection === undefined
317+
? undefined
318+
: (input.runtime.normalizeThreadLoadModelSelection?.(input.modelSelection) ??
319+
input.modelSelection);
324320
return JSON.stringify({
325321
providerThread: providerThreadRuntimeKey(input.providerThread),
326322
modelSelection: modelSelection ?? null,
@@ -1946,6 +1942,7 @@ export const layerWithOptions = (
19461942
providerSessionId,
19471943
threadId: input.threadId,
19481944
providerThreadKey: providerThreadLoadKey({
1945+
runtime,
19491946
providerThread,
19501947
modelSelection: input.modelSelection,
19511948
runtimePolicy: input.runtimePolicy,
@@ -1961,6 +1958,7 @@ export const layerWithOptions = (
19611958
return runtime.resumeThread(input);
19621959
}
19631960
const providerThreadKey = providerThreadLoadKey({
1961+
runtime,
19641962
providerThread: input.providerThread,
19651963
...(input.modelSelection === undefined
19661964
? {}
@@ -2005,6 +2003,7 @@ export const layerWithOptions = (
20052003
providerSessionId,
20062004
threadId,
20072005
providerThreadKey: providerThreadLoadKey({
2006+
runtime,
20082007
providerThread,
20092008
...(input.modelSelection === undefined
20102009
? {}
@@ -2034,6 +2033,7 @@ export const layerWithOptions = (
20342033
providerSessionId,
20352034
threadId: input.targetThreadId,
20362035
providerThreadKey: providerThreadLoadKey({
2036+
runtime,
20372037
providerThread,
20382038
...(input.modelSelection === undefined
20392039
? {}

‎packages/provider-core/src/server/ProviderAdapter.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,8 @@ export interface ProviderAdapterV2SessionRuntime {
627627
cwd?: string | null,
628628
) => number | undefined;
629629
readonly canReuseContextUsage?: (previous: ModelSelection, next: ModelSelection) => boolean;
630+
/** Excludes turn-scoped options from native thread load matching. */
631+
readonly normalizeThreadLoadModelSelection?: (selection: ModelSelection) => ModelSelection;
630632
readonly ensureThread: (
631633
input: ProviderAdapterV2EnsureThreadInput,
632634
) => Effect.Effect<OrchestrationV2ProviderThread, ProviderAdapterV2Error>;

0 commit comments

Comments
 (0)