Skip to content

Commit 3b24202

Browse files
committed
fix(server): reject thread.create for an existing id
1 parent ec1313c commit 3b24202

4 files changed

Lines changed: 124 additions & 0 deletions

File tree

‎apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
import * as DateTime from "effect/DateTime";
2121
import * as Effect from "effect/Effect";
2222
import * as Layer from "effect/Layer";
23+
import * as Result from "effect/Result";
24+
import * as TestClock from "effect/testing/TestClock";
2325
import * as SqlClient from "effect/sql/SqlClient";
2426
import * as SqlitePersistence from "../persistence/Sqlite.ts";
2527
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
@@ -624,3 +626,86 @@ it.effect("keeps delegated child pull-request links independent of the parent",
624626
assert.deepEqual(parentAfterChildLink.thread.pullRequests, parent.thread.pullRequests);
625627
}).pipe(Effect.provide(layerTest)),
626628
);
629+
630+
it.effect.each([false, true])("rejects thread.create for an existing id (deleted: %s)", (deleted) =>
631+
Effect.gen(function* () {
632+
const orchestrator = yield* Orchestrator.OrchestratorV2;
633+
const projections = yield* ProjectionStore.ProjectionStoreV2;
634+
const threadId = ThreadId.make(`thread:duplicate-create:${deleted}`);
635+
const create = {
636+
type: "thread.create",
637+
commandId: CommandId.make(`create:${threadId}`),
638+
threadId,
639+
projectId: ProjectId.make("project:duplicate-create"),
640+
title: "Original",
641+
modelSelection,
642+
runtimeMode: "full-access",
643+
interactionMode: "default",
644+
branch: null,
645+
worktreePath: null,
646+
createdBy: "user",
647+
creationSource: "web",
648+
importedNativeThread: {
649+
ref: { driver: adapter.driver, nativeId: "native-original", strength: "strong" },
650+
},
651+
} as const;
652+
yield* orchestrator.dispatch(create);
653+
if (deleted) {
654+
yield* orchestrator.dispatch({
655+
type: "thread.delete",
656+
commandId: CommandId.make(`delete:${threadId}`),
657+
threadId,
658+
});
659+
}
660+
const before = yield* projections.getThread(threadId);
661+
assert.isNotNull(before.activeProviderThreadId);
662+
assert.equal(before.deletedAt !== null, deleted);
663+
const sequence = yield* orchestrator.getThreadEventSequence(threadId);
664+
yield* TestClock.adjust("1 second");
665+
const duplicate = {
666+
...create,
667+
commandId: CommandId.make(`duplicate:${threadId}`),
668+
title: "Replacement",
669+
importedNativeThread: undefined,
670+
};
671+
const outcome = yield* orchestrator.dispatch(duplicate).pipe(Effect.result);
672+
assert.ok(Result.isFailure(outcome), "duplicate create must be rejected");
673+
const refused = outcome.failure;
674+
assert.equal(refused._tag, "OrchestratorThreadAlreadyExistsError");
675+
assert.equal(refused.message, `Thread ${threadId} already exists. Use a new thread id.`);
676+
assert.deepEqual(yield* projections.getThread(threadId), before);
677+
assert.equal(yield* orchestrator.getThreadEventSequence(threadId), sequence);
678+
const retried = yield* orchestrator.dispatch(duplicate).pipe(Effect.flip);
679+
assert.equal(retried._tag, "OrchestratorCommandPreviouslyRejectedError");
680+
assert.include(retried.message, refused.message);
681+
}).pipe(Effect.provide(layerTest)),
682+
);
683+
684+
it.effect("replays a thread.create receipt without changing the thread", () =>
685+
Effect.gen(function* () {
686+
const orchestrator = yield* Orchestrator.OrchestratorV2;
687+
const projections = yield* ProjectionStore.ProjectionStoreV2;
688+
const threadId = ThreadId.make("thread:create-receipt-replay");
689+
const create = {
690+
type: "thread.create",
691+
commandId: CommandId.make("create:receipt-replay"),
692+
threadId,
693+
projectId: ProjectId.make("project:duplicate-create"),
694+
title: "Original",
695+
modelSelection,
696+
runtimeMode: "full-access",
697+
interactionMode: "default",
698+
branch: null,
699+
worktreePath: null,
700+
createdBy: "user",
701+
creationSource: "web",
702+
} as const;
703+
const original = yield* orchestrator.dispatch(create);
704+
const before = yield* projections.getThread(threadId);
705+
const replayed = yield* orchestrator.dispatch(create);
706+
assert.equal(replayed.sequence, original.sequence);
707+
assert.deepEqual(replayed, original);
708+
assert.deepEqual(yield* projections.getThread(threadId), before);
709+
assert.equal(yield* orchestrator.getThreadEventSequence(threadId), original.sequence);
710+
}).pipe(Effect.provide(layerTest)),
711+
);

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ export class OrchestratorDispatchError extends Schema.TaggedError<OrchestratorDi
150150
}
151151
}
152152

153+
export class OrchestratorThreadAlreadyExistsError extends Schema.TaggedError<OrchestratorThreadAlreadyExistsError>()(
154+
"OrchestratorThreadAlreadyExistsError",
155+
{ commandId: CommandId, threadId: ThreadId },
156+
) {
157+
override get message(): string {
158+
return `Thread ${this.threadId} already exists. Use a new thread id.`;
159+
}
160+
}
161+
153162
export class OrchestratorCommandRejectedError extends Schema.TaggedError<OrchestratorCommandRejectedError>()(
154163
"OrchestratorCommandRejectedError",
155164
{ commandId: CommandId, commandType: Schema.String, cause: Schema.optional(Schema.Defect()) },
@@ -262,6 +271,7 @@ export function canReplayCommandReceipt(
262271

263272
export const OrchestratorV2Error = Schema.Union([
264273
OrchestratorDispatchError,
274+
OrchestratorThreadAlreadyExistsError,
265275
OrchestratorCommandRejectedError,
266276
OrchestratorProjectionError,
267277
OrchestratorDomainEventStreamError,
@@ -2250,6 +2260,20 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
22502260
"orchestration_v2.driver": command.modelSelection.instanceId,
22512261
});
22522262

2263+
// Tombstones reserve their ids too; recreating one would overwrite its history.
2264+
const existing = yield* projectionStore.getThread(command.threadId).pipe(
2265+
Effect.catchTags({ ProjectionStoreThreadNotFoundError: () => Effect.succeed(null) }),
2266+
Effect.mapError(
2267+
(cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }),
2268+
),
2269+
);
2270+
if (existing !== null) {
2271+
return yield* new OrchestratorThreadAlreadyExistsError({
2272+
commandId: command.commandId,
2273+
threadId: command.threadId,
2274+
});
2275+
}
2276+
22532277
const now = yield* DateTime.now;
22542278
const emitEvent = emit(events, command);
22552279
const thread: OrchestrationV2AppThread = {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import * as Exit from "effect/Exit";
3636
import * as Layer from "effect/Layer";
3737
import * as Option from "effect/Option";
3838
import * as Ref from "effect/Ref";
39+
import * as Result from "effect/Result";
3940
import * as Stream from "effect/Stream";
4041
import * as Schema from "effect/Schema";
4142
import * as TestClock from "effect/testing/TestClock";
@@ -2308,6 +2309,19 @@ it.effect("shared intake preserves durable attachment bytes after a lost launch
23082309
);
23092310
assert.equal((yield* claimedFiles).length, 1);
23102311

2312+
const duplicate = yield* ThreadMessageIntake.launchThread({
2313+
...input,
2314+
commandId: CommandId.make("intake-duplicate-create"),
2315+
initialMessage: { ...input.initialMessage, messageId: MessageId.make("intake-duplicate") },
2316+
}).pipe(Effect.result);
2317+
assert.ok(Result.isFailure(duplicate));
2318+
assert.ok(duplicate.failure._tag === "ThreadLaunchError");
2319+
assert.equal(duplicate.failure.operation, "create-thread");
2320+
assert.propertyVal(duplicate.failure.cause, "_tag", "OrchestratorThreadAlreadyExistsError");
2321+
assert.equal((yield* claimedFiles).length, 1);
2322+
assert.deepEqual(yield* fs.readFile(storedPath), new Uint8Array([1, 2, 3, 4]));
2323+
assert.deepEqual(yield* fs.readFile(pendingPath), new Uint8Array([1, 2, 3, 4]));
2324+
23112325
const missingProject = yield* ThreadMessageIntake.launchThread({
23122326
...input,
23132327
commandId: CommandId.make("intake-no-project"),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function dispatchWasNotAccepted(
2020
) {
2121
switch (error._tag) {
2222
case "OrchestratorCommandRejectedError":
23+
case "OrchestratorThreadAlreadyExistsError":
2324
case "OrchestratorProjectionError":
2425
case "OrchestratorProviderAdapterError":
2526
case "OrchestratorCommandPreviouslyRejectedError":

0 commit comments

Comments
 (0)