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, 08:30 UTC
fix(server): a Codex speed pick made during a running turn reaches Codex (#38169)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Martin committed
Nov 17, 2026 at 16:32 UTC
8009cd8c3bc173a6baee11f2014357d7d120ad66
2 files changed
+212
-57
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+160
-43
@@ -1820,13 +1820,108 @@ describe("CodexAdapterV2 post-settle continuation", () => {
1820
),
1821
);
1822
1823
- it.effect("a steer that changes the service tier updates Codex's next-turn settings", () =>
1823
+ it.effect.each([
1824
+ { codex: "accepts", reply: { result: {} } },
1825
+ { codex: "rejects", reply: { error: { code: -32601, message: "Method not found" } } },
1826
+ ] as const)(
1827
+ "a steer that changes the service tier still steers when Codex $codex the settings update",
1828
+ ({ codex, reply }) =>
1829
+ Effect.gen(function* () {
1830
+ const nativeThreadId = `steer-tier-thread-${codex}`;
1831
+ const nativeTurnId = `steer-tier-turn-${codex}`;
1832
+ const harness = yield* makeCodexReplayHarness(
1833
+ makeCodexReplayTranscript({
1834
+ scenario: `steer-service-tier-${codex}`,
1835
+ entries: [
1836
+ ...codexReplayPreamble({ nativeThreadId, nativeTurnId, prompt: "Work." }),
1837
+ {
1838
+ type: "expect_outbound",
1839
+ label: "thread/settings/update",
1840
+ frame: {
1841
+ id: 4,
1842
+ method: "thread/settings/update",
1843
+ params: { threadId: nativeThreadId, serviceTier: "fast" },
1844
+ },
1845
+ },
1846
+ { type: "emit_inbound", frame: { id: 4, ...reply } },
1847
+ {
1848
+ type: "expect_outbound",
1849
+ label: "turn/steer",
1850
+ frame: {
1851
+ id: 5,
1852
+ method: "turn/steer",
1853
+ params: {
1854
+ threadId: nativeThreadId,
1855
+ expectedTurnId: nativeTurnId,
1856
+ input: [{ type: "text", text: "Hurry up." }],
1857
+ },
1858
+ },
1859
+ },
1860
+ { type: "emit_inbound", frame: { id: 5, result: { turnId: nativeTurnId } } },
1861
+ {
1862
+ type: "emit_inbound",
1863
+ frame: {
1864
+ method: "turn/completed",
1865
+ params: {
1866
+ threadId: nativeThreadId,
1867
+ turn: makeCodexReplayTurn({ id: nativeTurnId, status: "completed" }),
1868
+ },
1869
+ },
1870
+ },
1871
+ ],
1872
+ }),
1873
+ );
1874
+ const turnInput = makeCodexTestTurnInput({
1875
+ threadId: harness.threadId,
1876
+ providerThread: harness.providerThread,
1877
+ now: yield* DateTime.now,
1878
+ attemptId: RunAttemptId.make("steer-tier-attempt"),
1879
+ text: "Work.",
1880
+ });
1881
+ yield* harness.runtime.startTurn(turnInput);
1882
+ yield* harness.until(() =>
1883
+ harness.events.some(
1884
+ (event) =>
1885
+ event.type === "provider_turn.updated" &&
1886
+ event.providerTurn.nativeTurnRef?.nativeId === nativeTurnId,
1887
+ ),
1888
+ );
1889
+ const providerTurnId = harness.events.flatMap((event) =>
1890
+ event.type === "provider_turn.updated" ? [event.providerTurn.id] : [],
1891
+ )[0];
1892
+ assert.isDefined(providerTurnId);
1893
+ if (providerTurnId === undefined) return;
1894
+ yield* harness.runtime.steerTurn({
1895
+ ...turnInput,
1896
+ providerTurnId,
1897
+ modelSelection: {
1898
+ ...CODEX_TEST_MODEL_SELECTION,
1899
+ options: [{ id: "serviceTier", value: "fast" }],
1900
+ },
1901
+ message: { ...turnInput.message, text: "Hurry up." },
1902
+ });
1903
+ yield* harness.firstTerminal;
1904
+ assert.equal(harness.terminalEvents()[0]?.status, "completed");
1905
+ }).pipe(
1906
+ Effect.scoped,
1907
+ Effect.provide(
1908
+ Layer.mergeAll(
1909
+ IdAllocator.layer,
1910
+ McpProviderSessions.layer,
1911
+ NodeServices.layer,
1912
+ NodeCrypto.layer,
1913
+ ),
1914
+ ),
1915
+ ),
1916
+ );
1917
+
1918
+ it.effect("a speed change with no message updates Codex's next-turn settings", () =>
1919
Effect.gen(function* () {
1825
- const nativeThreadId = "steer-tier-thread";
1826
- const nativeTurnId = "steer-tier-turn";
1920
+ const nativeThreadId = "picker-tier-thread";
1921
+ const nativeTurnId = "picker-tier-turn";
1922
const harness = yield* makeCodexReplayHarness(
1923
makeCodexReplayTranscript({
1829
- scenario: "steer-service-tier",
1924
+ scenario: "picker-service-tier",
1925
entries: [
1926
...codexReplayPreamble({ nativeThreadId, nativeTurnId, prompt: "Work." }),
1927
{
@@ -1835,24 +1930,10 @@ describe("CodexAdapterV2 post-settle continuation", () => {
1930
frame: {
1931
id: 4,
1932
method: "thread/settings/update",
1838
- params: { threadId: nativeThreadId, serviceTier: "fast" },
1933
+ params: { threadId: nativeThreadId, serviceTier: "default" },
1934
},
1935
},
1936
{ type: "emit_inbound", frame: { id: 4, result: {} } },
1842
- {
1843
- type: "expect_outbound",
1844
- label: "turn/steer",
1845
- frame: {
1846
- id: 5,
1847
- method: "turn/steer",
1848
- params: {
1849
- threadId: nativeThreadId,
1850
- expectedTurnId: nativeTurnId,
1851
- input: [{ type: "text", text: "Hurry up." }],
1852
- },
1853
- },
1854
- },
1855
- { type: "emit_inbound", frame: { id: 5, result: { turnId: nativeTurnId } } },
1937
{
1938
type: "emit_inbound",
1939
frame: {
@@ -1866,14 +1947,15 @@ describe("CodexAdapterV2 post-settle continuation", () => {
1947
],
1948
}),
1949
);
1869
- const turnInput = makeCodexTestTurnInput({
1870
- threadId: harness.threadId,
1871
- providerThread: harness.providerThread,
1872
- now: yield* DateTime.now,
1873
- attemptId: RunAttemptId.make("steer-tier-attempt"),
1874
- text: "Work.",
1875
- });
1876
- yield* harness.runtime.startTurn(turnInput);
1950
+ yield* harness.runtime.startTurn(
1951
+ makeCodexTestTurnInput({
1952
+ threadId: harness.threadId,
1953
+ providerThread: harness.providerThread,
1954
+ now: yield* DateTime.now,
1955
+ attemptId: RunAttemptId.make("picker-tier-attempt"),
1956
+ text: "Work.",
1957
+ }),
1958
+ );
1959
yield* harness.until(() =>
1960
harness.events.some(
1961
(event) =>
@@ -1881,20 +1963,16 @@ describe("CodexAdapterV2 post-settle continuation", () => {
1963
event.providerTurn.nativeTurnRef?.nativeId === nativeTurnId,
1964
),
1965
);
1884
- const providerTurnId = harness.events.flatMap((event) =>
1885
- event.type === "provider_turn.updated" ? [event.providerTurn.id] : [],
1886
- )[0];
1887
- assert.isDefined(providerTurnId);
1888
- if (providerTurnId === undefined) return;
1889
- yield* harness.runtime.steerTurn({
1890
- ...turnInput,
1891
- providerTurnId,
1892
- modelSelection: {
1893
- ...CODEX_TEST_MODEL_SELECTION,
1894
- options: [{ id: "serviceTier", value: "fast" }],
1895
- },
1896
- message: { ...turnInput.message, text: "Hurry up." },
1897
- });
1966
+ assert.isDefined(harness.runtime.applyModelSelection);
1967
+ yield* (
1968
+ harness.runtime.applyModelSelection?.({
1969
+ providerThread: harness.providerThread,
1970
+ modelSelection: {
1971
+ ...CODEX_TEST_MODEL_SELECTION,
1972
+ options: [{ id: "serviceTier", value: "default" }],
1973
+ },
1974
+ }) ?? Effect.void
1975
+ );
1976
yield* harness.firstTerminal;
1977
assert.equal(harness.terminalEvents()[0]?.status, "completed");
1978
}).pipe(
@@ -4296,6 +4374,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
4374
"new-run",
4375
"steer",
4376
"start-fails",
4377
+ "keep-changed-tier",
4378
] as const)("handles terminal Codex capacity failures when retries %s", (outcome) =>
4379
Effect.gen(function* () {
4380
const scenario = `capacity-${outcome}`;
@@ -4338,6 +4417,23 @@ describe("CodexAdapterV2 post-settle continuation", () => {
4417
nativeTurnId: nativeTurnId(0),
4418
prompt,
4419
});
4420
+ // A speed picked while the first attempt runs; the retry must not send the old one.
4421
+ const changedTier = outcome === "keep-changed-tier" ? "fast" : undefined;
4422
+ const retryRequestId = (attempt: number) => (changedTier === undefined ? 3 : 4) + attempt;
4423
+ if (changedTier !== undefined) {
4424
+ entries.push(
4425
+ {
4426
+ type: "expect_outbound",
4427
+ label: "thread/settings/update",
4428
+ frame: {
4429
+ id: 4,
4430
+ method: "thread/settings/update",
4431
+ params: { threadId: nativeThreadId, serviceTier: changedTier },
4432
+ },
4433
+ },
4434
+ { type: "emit_inbound", frame: { id: 4, result: {} } },
4435
+ );
4436
+ }
4437
entries.push(
4438
...(outcome === "completion-only" ? failureEntries(0).slice(1) : failureEntries(0)),
4439
);
@@ -4363,7 +4459,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
4459
entries.push({
4460
...entry,
4461
frame: {
4366
- id: 3 + attempt,
4462
+ id: retryRequestId(attempt),
4463
method: "turn/start",
4464
params: {
4465
threadId: nativeThreadId,
@@ -4375,6 +4471,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
4471
sandboxPolicy: { type: "dangerFullAccess" },
4472
summary: "auto",
4473
collaborationMode: { mode: "default", settings: { model: "gpt-5.4" } },
4474
+ ...(changedTier === undefined ? {} : { serviceTier: changedTier }),
4475
},
4476
},
4477
});
@@ -4411,7 +4508,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
4508
error: { code: -32603, message: "Start failed" },
4509
},
4510
}
4414
- : withReplayRequestId(entry, 3 + attempt),
4511
+ : withReplayRequestId(entry, retryRequestId(attempt)),
4512
);
4513
} else if (outcome !== "start-fails") {
4514
entries.push(
@@ -4437,6 +4534,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
4534
if (outcome === "exhaust") entries.push(...failureEntries(attempt));
4535
if (
4536
outcome === "recover" ||
4537
+ outcome === "keep-changed-tier" ||
4538
outcome === "completion-only" ||
4539
outcome === "started-first" ||
4540
outcome === "stop-starting"
@@ -4520,6 +4618,24 @@ describe("CodexAdapterV2 post-settle continuation", () => {
4618
text: prompt,
4619
});
4620
yield* harness.runtime.startTurn(turnInput);
4621
+ if (changedTier !== undefined) {
4622
+ yield* harness.until(() =>
4623
+ harness.events.some(
4624
+ (event) =>
4625
+ event.type === "provider_turn.updated" &&
4626
+ event.providerTurn.nativeTurnRef?.nativeId === nativeTurnId(0),
4627
+ ),
4628
+ );
4629
+ yield* (
4630
+ harness.runtime.applyModelSelection?.({
4631
+ providerThread: harness.providerThread,
4632
+ modelSelection: {
4633
+ ...CODEX_TEST_MODEL_SELECTION,
4634
+ options: [{ id: "serviceTier", value: changedTier }],
4635
+ },
4636
+ }) ?? Effect.void
4637
+ );
4638
+ }
4639
for (let attempt = 1; attempt <= Math.max(retries, 1); attempt++) {
4640
const milestone = yield* Queue.take(milestones);
4641
assert.equal(milestone.type, "turn_item.updated");
@@ -4591,6 +4707,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
4707
assert.equal(
4708
terminal?.status,
4709
outcome === "recover" ||
4710
+ outcome === "keep-changed-tier" ||
4711
outcome === "completion-only" ||
4712
outcome === "steer" ||
4713
outcome === "started-first" ||
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+52
-14
@@ -1278,6 +1278,7 @@ function failureResetAt(
1278
1279
interface ActiveCodexTurnContext {
1280
hookStopReason?: string;
1281
+ appliedServiceTier?: string;
1282
capacityRetryAttempt?: number;
1283
capacityRetryPending?: Deferred.Deferred<ActiveCodexTurnContext | undefined>;
1284
closedCommandItemIds?: ReadonlySet<string>;
@@ -7239,6 +7240,21 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
7240
yield* startNativeTurn(
7241
{
7242
...context.input,
7243
+ // A speed changed during the failed attempt outlives the retry.
7244
+ ...(context.appliedServiceTier === undefined
7245
+ ? {}
7246
+ : {
7247
+ modelSelection: {
7248
+ ...context.input.modelSelection,
7249
+ options: [
7250
+ ...(context.input.modelSelection.options ?? []).filter(
7251
+ (option) =>
7252
+ option.id !== "serviceTier" && option.id !== "fastMode",
7253
+ ),
7254
+ { id: "serviceTier", value: context.appliedServiceTier },
7255
+ ],
7256
+ },
7257
+ }),
7258
providerTurnOrdinal: context.providerTurnOrdinal + 1,
7259
},
7260
[],
@@ -7845,6 +7861,34 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
7861
),
7862
};
7863
7864
+ // turn/steer carries no settings, and the turns Codex starts on its own reuse the thread's.
7865
+ const applyServiceTier = Effect.fn("CodexAdapterV2.applyServiceTier")(function* (
7866
+ threadId: string,
7867
+ modelSelection: ModelSelection | undefined,
7868
+ ) {
7869
+ const serviceTier =
7870
+ adapterOptions.resolveRuntime === undefined
7871
+ ? getCodexServiceTierOptionValue(modelSelection)
7872
+ : undefined;
7873
+ if (serviceTier === undefined) return;
7874
+ const applied = yield* client
7875
+ .request("thread/settings/update", { threadId, serviceTier })
7876
+ .pipe(
7877
+ Effect.as(true),
7878
+ Effect.catch((cause) =>
7879
+ Effect.logWarning("orchestration-v2.codex-service-tier-update-failed", {
7880
+ cause,
7881
+ }).pipe(Effect.as(false)),
7882
+ ),
7883
+ );
7884
+ if (!applied) return;
7885
+ for (const turn of (yield* Ref.get(activeTurns)).values()) {
7886
+ if (turn.providerThread.nativeThreadRef?.nativeId === threadId) {
7887
+ turn.appliedServiceTier = serviceTier;
7888
+ }
7889
+ }
7890
+ });
7891
+
7892
const runtime: ProviderAdapter.ProviderAdapterV2SessionRuntime = {
7893
instanceId: adapterOptions.instanceId,
7894
driver: CODEX_PROVIDER,
@@ -8104,20 +8148,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
8148
);
8149
}
8150
8107
- const serviceTier =
8108
- adapterOptions.resolveRuntime === undefined
8109
- ? getCodexServiceTierOptionValue(turnInput.modelSelection)
8110
- : undefined;
8111
- if (serviceTier !== undefined) {
8112
- // turn/steer carries no settings, and the turns Codex starts on its own reuse the thread's.
8113
- yield* client.request("thread/settings/update", { threadId, serviceTier }).pipe(
8114
- Effect.catch((cause) =>
8115
- Effect.logWarning("orchestration-v2.codex-steer-service-tier-failed", {
8116
- cause,
8117
- }),
8118
- ),
8119
- );
8120
- }
8151
+ yield* applyServiceTier(threadId, turnInput.modelSelection);
8152
const codexInput = yield* toCodexInput(turnInput);
8153
yield* client.request("turn/steer", {
8154
expectedTurnId: activeTurn.nativeTurnId,
@@ -8138,6 +8169,13 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
8169
),
8170
),
8171
),
8172
+ applyModelSelection: Effect.fn("CodexAdapterV2.applyModelSelection")(function* ({
8173
+ providerThread,
8174
+ modelSelection,
8175
+ }) {
8176
+ const threadId = providerThread.nativeThreadRef?.nativeId;
8177
+ if (threadId != null) yield* applyServiceTier(threadId, modelSelection);
8178
+ }),
8179
// Dropping this connection's subscription lets the shared app-server
8180
// shut the native thread (and its MCP servers) down once it is idle.
8181
unloadThread: Effect.fn("CodexAdapterV2.unloadThread")(