The best way to control Claude Code, Codex and more from any computer or phone
fix(server): reject thread.create for an existing id (#19704)
Scott Norteman committed
Oct 18, 2026 at 16:11 UTC
ff7b24a704c88522a8faa0b2e1e172c52cf3d72c
4 files changed
+124
apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts
+85
@@ -21,6 +21,8 @@ import {
21
import * as DateTime from "effect/DateTime";
22
import * as Effect from "effect/Effect";
23
import * as Layer from "effect/Layer";
24
+import * as Result from "effect/Result";
25
+import * as TestClock from "effect/testing/TestClock";
26
import * as SqlClient from "effect/sql/SqlClient";
27
import * as SqlitePersistence from "../persistence/Sqlite.ts";
28
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
@@ -953,3 +955,86 @@ it.effect("wakes the parent once when its delegated task asks a question", () =>
955
});
956
}).pipe(Effect.provide(DelegatedTaskQuestionReactor.layer.pipe(Layer.provideMerge(layerTest)))),
957
);
958
+
959
+it.effect.each([false, true])("rejects thread.create for an existing id (deleted: %s)", (deleted) =>
960
+ Effect.gen(function* () {
961
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
962
+ const projections = yield* ProjectionStore.ProjectionStoreV2;
963
+ const threadId = ThreadId.make(`thread:duplicate-create:${deleted}`);
964
+ const create = {
965
+ type: "thread.create",
966
+ commandId: CommandId.make(`create:${threadId}`),
967
+ threadId,
968
+ projectId: ProjectId.make("project:duplicate-create"),
969
+ title: "Original",
970
+ modelSelection,
971
+ runtimeMode: "full-access",
972
+ interactionMode: "default",
973
+ branch: null,
974
+ worktreePath: null,
975
+ createdBy: "user",
976
+ creationSource: "web",
977
+ importedNativeThread: {
978
+ ref: { driver: adapter.driver, nativeId: "native-original", strength: "strong" },
979
+ },
980
+ } as const;
981
+ yield* orchestrator.dispatch(create);
982
+ if (deleted) {
983
+ yield* orchestrator.dispatch({
984
+ type: "thread.delete",
985
+ commandId: CommandId.make(`delete:${threadId}`),
986
+ threadId,
987
+ });
988
+ }
989
+ const before = yield* projections.getThread(threadId);
990
+ assert.isNotNull(before.activeProviderThreadId);
991
+ assert.equal(before.deletedAt !== null, deleted);
992
+ const sequence = yield* orchestrator.getThreadEventSequence(threadId);
993
+ yield* TestClock.adjust("1 second");
994
+ const duplicate = {
995
+ ...create,
996
+ commandId: CommandId.make(`duplicate:${threadId}`),
997
+ title: "Replacement",
998
+ importedNativeThread: undefined,
999
+ };
1000
+ const outcome = yield* orchestrator.dispatch(duplicate).pipe(Effect.result);
1001
+ assert.ok(Result.isFailure(outcome), "duplicate create must be rejected");
1002
+ const refused = outcome.failure;
1003
+ assert.equal(refused._tag, "OrchestratorThreadAlreadyExistsError");
1004
+ assert.equal(refused.message, `Thread ${threadId} already exists. Use a new thread id.`);
1005
+ assert.deepEqual(yield* projections.getThread(threadId), before);
1006
+ assert.equal(yield* orchestrator.getThreadEventSequence(threadId), sequence);
1007
+ const retried = yield* orchestrator.dispatch(duplicate).pipe(Effect.flip);
1008
+ assert.equal(retried._tag, "OrchestratorCommandPreviouslyRejectedError");
1009
+ assert.include(retried.message, refused.message);
1010
+ }).pipe(Effect.provide(layerTest)),
1011
+);
1012
+
1013
+it.effect("replays a thread.create receipt without changing the thread", () =>
1014
+ Effect.gen(function* () {
1015
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
1016
+ const projections = yield* ProjectionStore.ProjectionStoreV2;
1017
+ const threadId = ThreadId.make("thread:create-receipt-replay");
1018
+ const create = {
1019
+ type: "thread.create",
1020
+ commandId: CommandId.make("create:receipt-replay"),
1021
+ threadId,
1022
+ projectId: ProjectId.make("project:duplicate-create"),
1023
+ title: "Original",
1024
+ modelSelection,
1025
+ runtimeMode: "full-access",
1026
+ interactionMode: "default",
1027
+ branch: null,
1028
+ worktreePath: null,
1029
+ createdBy: "user",
1030
+ creationSource: "web",
1031
+ } as const;
1032
+ const original = yield* orchestrator.dispatch(create);
1033
+ const before = yield* projections.getThread(threadId);
1034
+ const replayed = yield* orchestrator.dispatch(create);
1035
+ assert.equal(replayed.sequence, original.sequence);
1036
+ assert.deepEqual(replayed, original);
1037
+ assert.deepEqual(yield* projections.getThread(threadId), before);
1038
+ assert.equal(yield* orchestrator.getThreadEventSequence(threadId), original.sequence);
1039
+ }).pipe(Effect.provide(layerTest)),
1040
+);
apps/server/src/orchestration-v2/Orchestrator.ts
+24
@@ -159,6 +159,15 @@ export class OrchestratorDispatchError extends Schema.TaggedError<OrchestratorDi
159
}
160
}
161
162
+export class OrchestratorThreadAlreadyExistsError extends Schema.TaggedError<OrchestratorThreadAlreadyExistsError>()(
163
+ "OrchestratorThreadAlreadyExistsError",
164
+ { commandId: CommandId, threadId: ThreadId },
165
+) {
166
+ override get message(): string {
167
+ return `Thread ${this.threadId} already exists. Use a new thread id.`;
168
+ }
169
+}
170
+
171
export class OrchestratorCommandRejectedError extends Schema.TaggedError<OrchestratorCommandRejectedError>()(
172
"OrchestratorCommandRejectedError",
173
{ commandId: CommandId, commandType: Schema.String, cause: Schema.optional(Schema.Defect()) },
@@ -270,6 +279,7 @@ export function canReplayCommandReceipt(
279
280
export const OrchestratorV2Error = Schema.Union([
281
OrchestratorDispatchError,
282
+ OrchestratorThreadAlreadyExistsError,
283
OrchestratorCommandRejectedError,
284
OrchestratorProjectionError,
285
OrchestratorDomainEventStreamError,
@@ -2233,6 +2243,20 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
2243
"orchestration_v2.driver": command.modelSelection.instanceId,
2244
});
2245
2246
+ // Tombstones reserve their ids too; recreating one would overwrite its history.
2247
+ const existing = yield* projectionStore.getThread(command.threadId).pipe(
2248
+ Effect.catchTags({ ProjectionStoreThreadNotFoundError: () => Effect.succeed(null) }),
2249
+ Effect.mapError(
2250
+ (cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }),
2251
+ ),
2252
+ );
2253
+ if (existing !== null) {
2254
+ return yield* new OrchestratorThreadAlreadyExistsError({
2255
+ commandId: command.commandId,
2256
+ threadId: command.threadId,
2257
+ });
2258
+ }
2259
+
2260
const now = yield* DateTime.now;
2261
const emitEvent = emit(events, command);
2262
const thread: OrchestrationV2AppThread = {
apps/server/src/orchestration-v2/ThreadLaunchService.test.ts
+14
@@ -37,6 +37,7 @@ import * as Fiber from "effect/Fiber";
37
import * as Layer from "effect/Layer";
38
import * as Option from "effect/Option";
39
import * as Ref from "effect/Ref";
40
+import * as Result from "effect/Result";
41
import * as Stream from "effect/Stream";
42
import * as Schema from "effect/Schema";
43
import * as TestClock from "effect/testing/TestClock";
@@ -2468,6 +2469,19 @@ it.effect("shared intake preserves durable attachment bytes after a lost launch
2469
);
2470
assert.equal((yield* claimedFiles).length, 1);
2471
2472
+ const duplicate = yield* ThreadMessageIntake.launchThread({
2473
+ ...input,
2474
+ commandId: CommandId.make("intake-duplicate-create"),
2475
+ initialMessage: { ...input.initialMessage, messageId: MessageId.make("intake-duplicate") },
2476
+ }).pipe(Effect.result);
2477
+ assert.ok(Result.isFailure(duplicate));
2478
+ assert.ok(duplicate.failure._tag === "ThreadLaunchError");
2479
+ assert.equal(duplicate.failure.operation, "create-thread");
2480
+ assert.propertyVal(duplicate.failure.cause, "_tag", "OrchestratorThreadAlreadyExistsError");
2481
+ assert.equal((yield* claimedFiles).length, 1);
2482
+ assert.deepEqual(yield* fs.readFile(storedPath), new Uint8Array([1, 2, 3, 4]));
2483
+ assert.deepEqual(yield* fs.readFile(pendingPath), new Uint8Array([1, 2, 3, 4]));
2484
+
2485
const missingProject = yield* ThreadMessageIntake.launchThread({
2486
...input,
2487
commandId: CommandId.make("intake-no-project"),
apps/server/src/orchestration-v2/ThreadMessageIntake.ts
+1
@@ -18,6 +18,7 @@ function dispatchWasNotAccepted(
18
) {
19
switch (error._tag) {
20
case "OrchestratorCommandRejectedError":
21
+ case "OrchestratorThreadAlreadyExistsError":
22
case "OrchestratorProjectionError":
23
case "OrchestratorProviderAdapterError":
24
case "OrchestratorCommandPreviouslyRejectedError":