The best way to control Claude Code, Codex and more from any computer or phone
fix(server): Codex threads open in another client are no longer silently replaced (#21045)
Co-authored-by: maria-rcks <254055478+maria-rcks@users.noreply.github.com>
Derek Trimm committed
Nov 3, 2026 at 17:06 UTC
a445272c789ec5009d23fa5ec6df7155e734dea3
7 files changed
+192
-16
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+72
@@ -3256,6 +3256,78 @@ describe("CodexAdapterV2 post-settle continuation", () => {
3256
),
3257
);
3258
3259
+ it.effect("reports a thread another Codex client is writing to as in use", () =>
3260
+ Effect.scoped(
3261
+ Effect.gen(function* () {
3262
+ const nativeThreadId = "thread-open-elsewhere";
3263
+ const harness = yield* makeCodexReplayHarness(
3264
+ makeCodexReplayTranscript({
3265
+ scenario: "codex-resume-active-writer",
3266
+ entries: [
3267
+ ...codexReplayPreamble({
3268
+ nativeThreadId,
3269
+ nativeTurnId: "unused-turn",
3270
+ prompt: "unused-prompt",
3271
+ }).slice(0, 5),
3272
+ {
3273
+ type: "expect_outbound",
3274
+ label: "thread/resume",
3275
+ frame: {
3276
+ id: 3,
3277
+ method: "thread/resume",
3278
+ params: {
3279
+ threadId: nativeThreadId,
3280
+ excludeTurns: true,
3281
+ cwd: CODEX_TEST_RUNTIME_POLICY.cwd,
3282
+ model: CODEX_TEST_MODEL_SELECTION.model,
3283
+ config: CodexAdapterV2.CODEX_THREAD_CONFIG,
3284
+ },
3285
+ },
3286
+ },
3287
+ {
3288
+ type: "emit_inbound",
3289
+ label: "active writer",
3290
+ frame: {
3291
+ id: 3,
3292
+ error: {
3293
+ code: -32600,
3294
+ message: `thread ${nativeThreadId} already has an active writer`,
3295
+ },
3296
+ },
3297
+ },
3298
+ ],
3299
+ }),
3300
+ );
3301
+
3302
+ const error = yield* Effect.flip(
3303
+ harness.runtime.resumeThread({
3304
+ providerThread: harness.providerThread,
3305
+ modelSelection: CODEX_TEST_MODEL_SELECTION,
3306
+ runtimePolicy: CODEX_TEST_RUNTIME_POLICY,
3307
+ }),
3308
+ );
3309
+
3310
+ assert.equal(error._tag, "ProviderAdapterThreadInUseError");
3311
+ assert.nestedPropertyVal(error, "providerThreadId", harness.providerThread.id);
3312
+ assert.include(error.message, "open in another Codex client");
3313
+ assert.nestedPropertyVal(
3314
+ error,
3315
+ "cause.errorMessage",
3316
+ `thread ${nativeThreadId} already has an active writer`,
3317
+ );
3318
+ }).pipe(
3319
+ Effect.provide(
3320
+ Layer.mergeAll(
3321
+ IdAllocator.layer,
3322
+ McpProviderSessions.layer,
3323
+ NodeServices.layer,
3324
+ NodeCrypto.layer,
3325
+ ),
3326
+ ),
3327
+ ),
3328
+ ),
3329
+ );
3330
+
3331
it.effect("resolves retryable app-server errors on resumed provider activity", () =>
3332
Effect.scoped(
3333
Effect.gen(function* () {
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+29
-8
@@ -341,6 +341,21 @@ function toProtocolError(
341
});
342
}
343
344
+const CODEX_THREAD_IN_USE_MESSAGE =
345
+ "This conversation is open in another Codex client, such as the Codex app, the Codex CLI, or an IDE extension. Codex lets one client write to a conversation at a time. Quit that client, then send your message again.";
346
+
347
+const isCodexAppServerRequestError = Schema.is(CodexErrors.CodexAppServerRequestError);
348
+
349
+// Codex holds a per-thread writer lock for as long as a thread stays loaded in
350
+// any app-server sharing the same CODEX_HOME, and reports a conflict this way.
351
+function isCodexActiveWriterError(error: unknown): boolean {
352
+ return (
353
+ isCodexAppServerRequestError(error) &&
354
+ error.code === -32600 &&
355
+ error.errorMessage.includes("already has an active writer")
356
+ );
357
+}
358
+
359
function codexTimestamp(seconds: number | null | undefined): DateTime.Utc {
360
return seconds === null || seconds === undefined
361
? DateTime.nowUnsafe()
@@ -7126,14 +7141,20 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
7141
},
7142
(effect, threadInput) =>
7143
effect.pipe(
7129
- Effect.mapError(
7130
- (cause) =>
7131
- new ProviderAdapter.ProviderAdapterResumeThreadError({
7132
- driver: CODEX_PROVIDER,
7133
- providerSessionId: input.providerSessionId,
7134
- providerThreadId: threadInput.providerThread.id,
7135
- cause,
7136
- }),
7144
+ Effect.mapError((cause) =>
7145
+ isCodexActiveWriterError(cause)
7146
+ ? new ProviderAdapter.ProviderAdapterThreadInUseError({
7147
+ driver: CODEX_PROVIDER,
7148
+ providerThreadId: threadInput.providerThread.id,
7149
+ detail: CODEX_THREAD_IN_USE_MESSAGE,
7150
+ cause,
7151
+ })
7152
+ : new ProviderAdapter.ProviderAdapterResumeThreadError({
7153
+ driver: CODEX_PROVIDER,
7154
+ providerSessionId: input.providerSessionId,
7155
+ providerThreadId: threadInput.providerThread.id,
7156
+ cause,
7157
+ }),
7158
),
7159
),
7160
),
apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts
+46
-8
@@ -280,6 +280,7 @@ function makeLocalCommandHarness(input: {
280
readonly openFailure?: unknown;
281
readonly ensureThreadFailure?: unknown;
282
readonly historyReadFailureAfterFallback?: unknown;
283
+ readonly resumeFailure?: ProviderAdapter.ProviderAdapterV2Error;
284
readonly interruptOpen?: boolean;
285
readonly interruptRunBeforeOpenFailure?: boolean;
286
readonly writeFailure?: unknown;
@@ -648,7 +649,7 @@ function makeLocalCommandHarness(input: {
649
],
650
};
651
}
651
- if ("historyReadFailureAfterFallback" in input) {
652
+ if ("historyReadFailureAfterFallback" in input || "resumeFailure" in input) {
653
const nativeThreadRef = {
654
driver: providerThread.driver,
655
nativeId: "native-resume-thread",
@@ -755,17 +756,19 @@ function makeLocalCommandHarness(input: {
756
),
757
),
758
);
759
+ const fallbackEnsureThread = vi.fn(() => Effect.succeed(providerThread));
760
const resumeFallbackSession = {
761
driver: providerThread.driver,
762
resumeThread: () =>
763
Effect.fail(
762
- new ProviderAdapter.ProviderAdapterEventStreamError({
763
- driver: providerThread.driver,
764
- providerSessionId,
765
- cause: "native thread is gone",
766
- }),
764
+ input.resumeFailure ??
765
+ new ProviderAdapter.ProviderAdapterEventStreamError({
766
+ driver: providerThread.driver,
767
+ providerSessionId,
768
+ cause: "native thread is gone",
769
+ }),
770
),
768
- ensureThread: () => Effect.succeed(providerThread),
771
+ ensureThread: fallbackEnsureThread,
772
};
773
const providerSession = {
774
id: providerSessionId,
@@ -804,7 +807,7 @@ function makeLocalCommandHarness(input: {
807
? Effect.succeed(sideChatSession as never)
808
: input.interruptOpen === true
809
? Effect.interrupt
807
- : "historyReadFailureAfterFallback" in input
810
+ : "historyReadFailureAfterFallback" in input || "resumeFailure" in input
811
? Effect.succeed(resumeFallbackSession as never)
812
: "ensureThreadFailure" in input
813
? Effect.succeed({ driver: providerThread.driver, ensureThread } as never)
@@ -1012,6 +1015,7 @@ function makeLocalCommandHarness(input: {
1015
resolveRuntimePolicy,
1016
createWorktree,
1017
open,
1018
+ fallbackEnsureThread,
1019
writeIfRunCurrent,
1020
startRootRun,
1021
startTurn,
@@ -1730,6 +1734,40 @@ effectIt.effect("does not overwrite a run interrupted while its provider session
1734
}),
1735
);
1736
1737
+effectIt.effect("fails the run instead of replacing a native thread another client holds", () =>
1738
+ Effect.gen(function* () {
1739
+ const detail = "This conversation is open in another Codex client.";
1740
+ const harness = makeLocalCommandHarness({
1741
+ text: "Continue",
1742
+ resumeFailure: new ProviderAdapter.ProviderAdapterThreadInUseError({
1743
+ driver: ProviderDriverKind.make("codex"),
1744
+ providerThreadId: ProviderThreadId.make("new-provider-thread"),
1745
+ detail,
1746
+ cause: new Error("thread native-resume-thread already has an active writer"),
1747
+ }),
1748
+ });
1749
+
1750
+ const exit = yield* Effect.exit(harness.start);
1751
+
1752
+ expect(harness.fallbackEnsureThread).not.toHaveBeenCalled();
1753
+ expect(exit._tag).toBe("Success");
1754
+ expect(harness.startRootRun).not.toHaveBeenCalled();
1755
+ const projection = harness.projection();
1756
+ expect(projection.providerThreads.at(-1)?.nativeThreadRef?.nativeId).toBe(
1757
+ "native-resume-thread",
1758
+ );
1759
+ expect(projection.contextHandoffs).toEqual([]);
1760
+ expect(projection.runs.at(-1)).toMatchObject({ status: "failed", startedAt: null });
1761
+ expect(projection.turnItems).toMatchObject([
1762
+ {
1763
+ type: "error",
1764
+ title: "Provider turn failed to start",
1765
+ failure: { class: "provider_error", message: detail },
1766
+ },
1767
+ ]);
1768
+ }),
1769
+);
1770
+
1771
effectIt.effect("does not mistake a failed state read for a superseded run", () =>
1772
Effect.gen(function* () {
1773
const harness = makeLocalCommandHarness({ text: "Continue", failReadsAfterRunning: true });
apps/server/src/orchestration-v2/ProviderTurnStartService.ts
+8
@@ -1336,6 +1336,14 @@ export const layer: Layer.Layer<
1336
resumedHandoffContext = undefined;
1337
resumedRetryHandoff = [];
1338
if (!(yield* isCurrentAttemptInStatus("starting"))) return undefined;
1339
+ // Another client is still writing to the native thread. A fresh
1340
+ // session here would silently split the conversation from it.
1341
+ if (
1342
+ resumed._tag === "Failure" &&
1343
+ resumed.failure._tag === "ProviderAdapterThreadInUseError"
1344
+ ) {
1345
+ return yield* loadFromProvider(Effect.fail(resumed.failure));
1346
+ }
1347
1348
yield* Effect.logWarning("Recovering provider context in a fresh native session", {
1349
driver: session.driver,
packages/provider-core/src/server/ProviderAdapter.ts
+19
@@ -235,6 +235,24 @@ export class ProviderAdapterResumeThreadError extends Schema.TaggedError<Provide
235
}
236
}
237
238
+/**
239
+ * The native thread is live in another process that owns its writes. Starting
240
+ * a replacement would split the conversation, so callers must not fall back.
241
+ */
242
+export class ProviderAdapterThreadInUseError extends Schema.TaggedError<ProviderAdapterThreadInUseError>()(
243
+ "ProviderAdapterThreadInUseError",
244
+ {
245
+ driver: ProviderDriverKind,
246
+ providerThreadId: ProviderThreadId,
247
+ detail: Schema.String,
248
+ cause: Schema.Defect(),
249
+ },
250
+) {
251
+ override get message(): string {
252
+ return this.detail;
253
+ }
254
+}
255
+
256
export class ProviderAdapterEnsureThreadError extends Schema.TaggedError<ProviderAdapterEnsureThreadError>()(
257
"ProviderAdapterEnsureThreadError",
258
{
@@ -411,6 +429,7 @@ export const ProviderAdapterV2Error = Schema.Union([
429
ProviderAdapterOpenSessionError,
430
ProviderAdapterCloseSessionError,
431
ProviderAdapterResumeThreadError,
432
+ ProviderAdapterThreadInUseError,
433
ProviderAdapterEnsureThreadError,
434
ProviderAdapterReadThreadSnapshotError,
435
ProviderAdapterRollbackThreadError,
packages/provider-core/src/server/failure.test.ts
+17
@@ -186,6 +186,23 @@ it("uses the innermost category without carrying an outer reason into it", () =>
186
);
187
});
188
189
+it("explains a native thread another client is writing to", () => {
190
+ const inUse = new ProviderAdapter.ProviderAdapterThreadInUseError({
191
+ driver: ProviderDriverKind.make("codex"),
192
+ providerThreadId: ProviderThreadId.make("provider-thread:in-use"),
193
+ detail: "This conversation is open in another Codex client.",
194
+ cause: new Error("thread provider-thread:in-use already has an active writer"),
195
+ });
196
+ const cause = new ProviderAdapter.ProviderAdapterTurnStartError({
197
+ driver: ProviderDriverKind.make("codex"),
198
+ threadId: ThreadId.make("thread:in-use"),
199
+ providerThreadId: ProviderThreadId.make("provider-thread:in-use"),
200
+ runId: RunId.make("run:in-use"),
201
+ cause: inUse,
202
+ });
203
+ assert.equal(makeProviderFailure({ cause: Cause.fail(cause) }).message, inUse.message);
204
+});
205
+
206
it("does not expose defect text nested inside a known error category", () => {
207
const failure = makeProviderFailure({
208
cause: {
packages/provider-core/src/server/failure.ts
+1
@@ -115,6 +115,7 @@ function curatedCauseMessages(cause: unknown): {
115
case "CursorModelUnavailableError":
116
case "CodexGoalObjectiveTooLongError":
117
case "ProviderSetupError":
118
+ case "ProviderAdapterThreadInUseError":
119
return { known: stringField(cause, "message"), category: categoryMessage() };
120
case "CodexAppServerRequestError": {
121
// Only Codex's fixed-shape goal validation text is actionable and