TypeScript
fix(server): Codex threads open in another client are no longer silently replaced (#21045)
Derek Trimma445272c789e
diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.tsindex 4471be814bf..3ea5a4cce15 100644--- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts+++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts@@ -3256,6 +3256,78 @@ describe("CodexAdapterV2 post-settle continuation", () => {),);+ it.effect("reports a thread another Codex client is writing to as in use", () =>+ Effect.scoped(+ Effect.gen(function* () {+ const nativeThreadId = "thread-open-elsewhere";+ const harness = yield* makeCodexReplayHarness(+ makeCodexReplayTranscript({+ scenario: "codex-resume-active-writer",+ entries: [+ ...codexReplayPreamble({+ nativeThreadId,+ nativeTurnId: "unused-turn",+ prompt: "unused-prompt",+ }).slice(0, 5),+ {+ type: "expect_outbound",+ label: "thread/resume",+ frame: {+ id: 3,+ method: "thread/resume",+ params: {+ threadId: nativeThreadId,+ excludeTurns: true,+ cwd: CODEX_TEST_RUNTIME_POLICY.cwd,+ model: CODEX_TEST_MODEL_SELECTION.model,+ config: CodexAdapterV2.CODEX_THREAD_CONFIG,+ },+ },+ },+ {+ type: "emit_inbound",+ label: "active writer",+ frame: {+ id: 3,+ error: {+ code: -32600,+ message: `thread ${nativeThreadId} already has an active writer`,+ },+ },+ },+ ],+ }),+ );++ const error = yield* Effect.flip(+ harness.runtime.resumeThread({+ providerThread: harness.providerThread,+ modelSelection: CODEX_TEST_MODEL_SELECTION,+ runtimePolicy: CODEX_TEST_RUNTIME_POLICY,+ }),+ );++ assert.equal(error._tag, "ProviderAdapterThreadInUseError");+ assert.nestedPropertyVal(error, "providerThreadId", harness.providerThread.id);+ assert.include(error.message, "open in another Codex client");+ assert.nestedPropertyVal(+ error,+ "cause.errorMessage",+ `thread ${nativeThreadId} already has an active writer`,+ );+ }).pipe(+ Effect.provide(+ Layer.mergeAll(+ IdAllocator.layer,+ McpProviderSessions.layer,+ NodeServices.layer,+ NodeCrypto.layer,+ ),+ ),+ ),+ ),+ );+it.effect("resolves retryable app-server errors on resumed provider activity", () =>Effect.scoped(Effect.gen(function* () {diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.tsindex 688de092c03..78e14a0b9fc 100644--- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts+++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts@@ -341,6 +341,21 @@ function toProtocolError(});}+const CODEX_THREAD_IN_USE_MESSAGE =+ "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.";++const isCodexAppServerRequestError = Schema.is(CodexErrors.CodexAppServerRequestError);++// Codex holds a per-thread writer lock for as long as a thread stays loaded in+// any app-server sharing the same CODEX_HOME, and reports a conflict this way.+function isCodexActiveWriterError(error: unknown): boolean {+ return (+ isCodexAppServerRequestError(error) &&+ error.code === -32600 &&+ error.errorMessage.includes("already has an active writer")+ );+}+function codexTimestamp(seconds: number | null | undefined): DateTime.Utc {return seconds === null || seconds === undefined? DateTime.nowUnsafe()@@ -7126,14 +7141,20 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (},(effect, threadInput) =>effect.pipe(- Effect.mapError(- (cause) =>- new ProviderAdapter.ProviderAdapterResumeThreadError({- driver: CODEX_PROVIDER,- providerSessionId: input.providerSessionId,- providerThreadId: threadInput.providerThread.id,- cause,- }),+ Effect.mapError((cause) =>+ isCodexActiveWriterError(cause)+ ? new ProviderAdapter.ProviderAdapterThreadInUseError({+ driver: CODEX_PROVIDER,+ providerThreadId: threadInput.providerThread.id,+ detail: CODEX_THREAD_IN_USE_MESSAGE,+ cause,+ })+ : new ProviderAdapter.ProviderAdapterResumeThreadError({+ driver: CODEX_PROVIDER,+ providerSessionId: input.providerSessionId,+ providerThreadId: threadInput.providerThread.id,+ cause,+ }),),),),diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.tsindex f7f9dc25ddc..cdbd259440a 100644--- a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts+++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts@@ -280,6 +280,7 @@ function makeLocalCommandHarness(input: {readonly openFailure?: unknown;readonly ensureThreadFailure?: unknown;readonly historyReadFailureAfterFallback?: unknown;+ readonly resumeFailure?: ProviderAdapter.ProviderAdapterV2Error;readonly interruptOpen?: boolean;readonly interruptRunBeforeOpenFailure?: boolean;readonly writeFailure?: unknown;@@ -648,7 +649,7 @@ function makeLocalCommandHarness(input: {],};}- if ("historyReadFailureAfterFallback" in input) {+ if ("historyReadFailureAfterFallback" in input || "resumeFailure" in input) {const nativeThreadRef = {driver: providerThread.driver,nativeId: "native-resume-thread",@@ -755,17 +756,19 @@ function makeLocalCommandHarness(input: {),),);+ const fallbackEnsureThread = vi.fn(() => Effect.succeed(providerThread));const resumeFallbackSession = {driver: providerThread.driver,resumeThread: () =>Effect.fail(- new ProviderAdapter.ProviderAdapterEventStreamError({- driver: providerThread.driver,- providerSessionId,- cause: "native thread is gone",- }),+ input.resumeFailure ??+ new ProviderAdapter.ProviderAdapterEventStreamError({+ driver: providerThread.driver,+ providerSessionId,+ cause: "native thread is gone",+ }),),- ensureThread: () => Effect.succeed(providerThread),+ ensureThread: fallbackEnsureThread,};const providerSession = {id: providerSessionId,@@ -804,7 +807,7 @@ function makeLocalCommandHarness(input: {? Effect.succeed(sideChatSession as never): input.interruptOpen === true? Effect.interrupt- : "historyReadFailureAfterFallback" in input+ : "historyReadFailureAfterFallback" in input || "resumeFailure" in input? Effect.succeed(resumeFallbackSession as never): "ensureThreadFailure" in input? Effect.succeed({ driver: providerThread.driver, ensureThread } as never)@@ -1012,6 +1015,7 @@ function makeLocalCommandHarness(input: {resolveRuntimePolicy,createWorktree,open,+ fallbackEnsureThread,writeIfRunCurrent,startRootRun,startTurn,@@ -1730,6 +1734,40 @@ effectIt.effect("does not overwrite a run interrupted while its provider session}),);+effectIt.effect("fails the run instead of replacing a native thread another client holds", () =>+ Effect.gen(function* () {+ const detail = "This conversation is open in another Codex client.";+ const harness = makeLocalCommandHarness({+ text: "Continue",+ resumeFailure: new ProviderAdapter.ProviderAdapterThreadInUseError({+ driver: ProviderDriverKind.make("codex"),+ providerThreadId: ProviderThreadId.make("new-provider-thread"),+ detail,+ cause: new Error("thread native-resume-thread already has an active writer"),+ }),+ });++ const exit = yield* Effect.exit(harness.start);++ expect(harness.fallbackEnsureThread).not.toHaveBeenCalled();+ expect(exit._tag).toBe("Success");+ expect(harness.startRootRun).not.toHaveBeenCalled();+ const projection = harness.projection();+ expect(projection.providerThreads.at(-1)?.nativeThreadRef?.nativeId).toBe(+ "native-resume-thread",+ );+ expect(projection.contextHandoffs).toEqual([]);+ expect(projection.runs.at(-1)).toMatchObject({ status: "failed", startedAt: null });+ expect(projection.turnItems).toMatchObject([+ {+ type: "error",+ title: "Provider turn failed to start",+ failure: { class: "provider_error", message: detail },+ },+ ]);+ }),+);+effectIt.effect("does not mistake a failed state read for a superseded run", () =>Effect.gen(function* () {const harness = makeLocalCommandHarness({ text: "Continue", failReadsAfterRunning: true });diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.tsindex cd121c7115a..e0797e784ba 100644--- a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts+++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts@@ -1336,6 +1336,14 @@ export const layer: Layer.Layer<resumedHandoffContext = undefined;resumedRetryHandoff = [];if (!(yield* isCurrentAttemptInStatus("starting"))) return undefined;+ // Another client is still writing to the native thread. A fresh+ // session here would silently split the conversation from it.+ if (+ resumed._tag === "Failure" &&+ resumed.failure._tag === "ProviderAdapterThreadInUseError"+ ) {+ return yield* loadFromProvider(Effect.fail(resumed.failure));+ }yield* Effect.logWarning("Recovering provider context in a fresh native session", {driver: session.driver,diff --git a/packages/provider-core/src/server/ProviderAdapter.ts b/packages/provider-core/src/server/ProviderAdapter.tsindex 9d83da489b3..f9f55ec1311 100644--- a/packages/provider-core/src/server/ProviderAdapter.ts+++ b/packages/provider-core/src/server/ProviderAdapter.ts@@ -235,6 +235,24 @@ export class ProviderAdapterResumeThreadError extends Schema.TaggedError<Provide}}+/**+ * The native thread is live in another process that owns its writes. Starting+ * a replacement would split the conversation, so callers must not fall back.+ */+export class ProviderAdapterThreadInUseError extends Schema.TaggedError<ProviderAdapterThreadInUseError>()(+ "ProviderAdapterThreadInUseError",+ {+ driver: ProviderDriverKind,+ providerThreadId: ProviderThreadId,+ detail: Schema.String,+ cause: Schema.Defect(),+ },+) {+ override get message(): string {+ return this.detail;+ }+}+export class ProviderAdapterEnsureThreadError extends Schema.TaggedError<ProviderAdapterEnsureThreadError>()("ProviderAdapterEnsureThreadError",{@@ -411,6 +429,7 @@ export const ProviderAdapterV2Error = Schema.Union([ProviderAdapterOpenSessionError,ProviderAdapterCloseSessionError,ProviderAdapterResumeThreadError,+ ProviderAdapterThreadInUseError,ProviderAdapterEnsureThreadError,ProviderAdapterReadThreadSnapshotError,ProviderAdapterRollbackThreadError,diff --git a/packages/provider-core/src/server/failure.test.ts b/packages/provider-core/src/server/failure.test.tsindex 5ca689509c0..04829ad8f26 100644--- a/packages/provider-core/src/server/failure.test.ts+++ b/packages/provider-core/src/server/failure.test.ts@@ -186,6 +186,23 @@ it("uses the innermost category without carrying an outer reason into it", () =>);});+it("explains a native thread another client is writing to", () => {+ const inUse = new ProviderAdapter.ProviderAdapterThreadInUseError({+ driver: ProviderDriverKind.make("codex"),+ providerThreadId: ProviderThreadId.make("provider-thread:in-use"),+ detail: "This conversation is open in another Codex client.",+ cause: new Error("thread provider-thread:in-use already has an active writer"),+ });+ const cause = new ProviderAdapter.ProviderAdapterTurnStartError({+ driver: ProviderDriverKind.make("codex"),+ threadId: ThreadId.make("thread:in-use"),+ providerThreadId: ProviderThreadId.make("provider-thread:in-use"),+ runId: RunId.make("run:in-use"),+ cause: inUse,+ });+ assert.equal(makeProviderFailure({ cause: Cause.fail(cause) }).message, inUse.message);+});+it("does not expose defect text nested inside a known error category", () => {const failure = makeProviderFailure({cause: {diff --git a/packages/provider-core/src/server/failure.ts b/packages/provider-core/src/server/failure.tsindex 8927a8c0ae9..edaf0aa97a7 100644--- a/packages/provider-core/src/server/failure.ts+++ b/packages/provider-core/src/server/failure.ts@@ -115,6 +115,7 @@ function curatedCauseMessages(cause: unknown): {case "CursorModelUnavailableError":case "CodexGoalObjectiveTooLongError":case "ProviderSetupError":+ case "ProviderAdapterThreadInUseError":return { known: stringField(cause, "message"), category: categoryMessage() };case "CodexAppServerRequestError": {// Only Codex's fixed-shape goal validation text is actionable and