Skip to content

Commit ff7b24a

Browse files
authored
fix(server): reject thread.create for an existing id (pingdotgg#19704)
1 parent c387779 commit ff7b24a

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
@@ -21,6 +21,8 @@ import {
2121
import * as DateTime from "effect/DateTime";
2222
import * as Effect from "effect/Effect";
2323
import * as Layer from "effect/Layer";
24+
import * as Result from "effect/Result";
25+
import * as TestClock from "effect/testing/TestClock";
2426
import * as SqlClient from "effect/sql/SqlClient";
2527
import * as SqlitePersistence from "../persistence/Sqlite.ts";
2628
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
@@ -953,3 +955,86 @@ it.effect("wakes the parent once when its delegated task asks a question", () =>
953955
});
954956
}).pipe(Effect.provide(DelegatedTaskQuestionReactor.layer.pipe(Layer.provideMerge(layerTest)))),
955957
);
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‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@ export class OrchestratorDispatchError extends Schema.TaggedError<OrchestratorDi
159159
}
160160
}
161161

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+
162171
export class OrchestratorCommandRejectedError extends Schema.TaggedError<OrchestratorCommandRejectedError>()(
163172
"OrchestratorCommandRejectedError",
164173
{ commandId: CommandId, commandType: Schema.String, cause: Schema.optional(Schema.Defect()) },
@@ -270,6 +279,7 @@ export function canReplayCommandReceipt(
270279

271280
export const OrchestratorV2Error = Schema.Union([
272281
OrchestratorDispatchError,
282+
OrchestratorThreadAlreadyExistsError,
273283
OrchestratorCommandRejectedError,
274284
OrchestratorProjectionError,
275285
OrchestratorDomainEventStreamError,
@@ -2233,6 +2243,20 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
22332243
"orchestration_v2.driver": command.modelSelection.instanceId,
22342244
});
22352245

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+
22362260
const now = yield* DateTime.now;
22372261
const emitEvent = emit(events, command);
22382262
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
@@ -37,6 +37,7 @@ import * as Fiber from "effect/Fiber";
3737
import * as Layer from "effect/Layer";
3838
import * as Option from "effect/Option";
3939
import * as Ref from "effect/Ref";
40+
import * as Result from "effect/Result";
4041
import * as Stream from "effect/Stream";
4142
import * as Schema from "effect/Schema";
4243
import * as TestClock from "effect/testing/TestClock";
@@ -2468,6 +2469,19 @@ it.effect("shared intake preserves durable attachment bytes after a lost launch
24682469
);
24692470
assert.equal((yield* claimedFiles).length, 1);
24702471

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+
24712485
const missingProject = yield* ThreadMessageIntake.launchThread({
24722486
...input,
24732487
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
@@ -18,6 +18,7 @@ function dispatchWasNotAccepted(
1818
) {
1919
switch (error._tag) {
2020
case "OrchestratorCommandRejectedError":
21+
case "OrchestratorThreadAlreadyExistsError":
2122
case "OrchestratorProjectionError":
2223
case "OrchestratorProviderAdapterError":
2324
case "OrchestratorCommandPreviouslyRejectedError":

0 commit comments

Comments
 (0)