@@ -1156,6 +1156,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
11561156 Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })),
11571157 );
11581158
1159+ /** Includes tombstones, which still reserve their id. */
1160+ const findThread = (threadId: ThreadId) =>
1161+ projectionStore.getThread(threadId).pipe(
1162+ Effect.catchTags({ ProjectionStoreThreadNotFoundError: () => Effect.succeed(null) }),
1163+ Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })),
1164+ );
1165+
11591166 /** Side chats live inside their parent, so deleting the parent deletes them too. */
11601167 const liveSideChatIds = Effect.fn("Orchestrator.liveSideChatIds")(
11611168 function* (threadId: ThreadId) {
@@ -4144,6 +4151,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
41444151 cause: `Thread ${command.sourceThreadId} was deleted.`,
41454152 });
41464153 }
4154+ if (command.sideChat === true && (yield* findThread(command.targetThreadId)) !== null) {
4155+ return yield* new OrchestratorThreadAlreadyExistsError({
4156+ commandId: command.commandId,
4157+ threadId: command.targetThreadId,
4158+ });
4159+ }
41474160
41484161 const sourceRun = runForSourcePoint(sourceProjection, command.sourcePoint);
41494162
@@ -12547,6 +12560,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1254712560
1254812561 const dispatchOnce = Effect.fn("orchestrationV2.dispatch.once")(function* (
1254912562 command: OrchestrationV2ServerCommand,
12563+ sideChatIds: ReadonlyArray<ThreadId>,
1255012564 ): Effect.fn.Return<
1255112565 {
1255212566 readonly events: ReadonlyArray<OrchestrationV2DomainEvent>;
@@ -12659,7 +12673,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1265912673 // The dispatch wrapper holds each side chat's lock, so none can commit after this plan.
1266012674 const plans = [
1266112675 yield* planDeletion(command),
12662- ...(yield* Effect.forEach(yield* liveSideChatIds(command.threadId) , (threadId) =>
12676+ ...(yield* Effect.forEach(sideChatIds , (threadId) =>
1266312677 planDeletion({
1266412678 ...command,
1266512679 commandId: CommandId.make(`${command.commandId}:side-chat:${threadId}`),
@@ -12955,8 +12969,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1295512969 };
1295612970 });
1295712971
12972+ /** `sideChatIds` are the live side chats a `thread.delete` cascades to, read under their locks. */
1295812973 const dispatchWithReceiptEffect = Effect.fn("orchestrationV2.dispatch.withReceipt")(function* (
1295912974 command: OrchestrationV2ServerCommand,
12975+ sideChatIds: ReadonlyArray<ThreadId> = [],
1296012976 ): Effect.fn.Return<OrchestratorV2DispatchResult, OrchestratorV2Error> {
1296112977 yield* Effect.annotateCurrentSpan({
1296212978 "orchestration_v2.command_id": command.commandId,
@@ -13032,7 +13048,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1303213048 if (shell !== null) yield* refuseAboveDispatchModeLimit(command, threadId, shell);
1303313049 }
1303413050
13035- const plan = yield* dispatchOnce(command).pipe(
13051+ const plan = yield* dispatchOnce(command, sideChatIds ).pipe(
1303613052 Effect.filterOrFail(
1303713053 (planned) =>
1303813054 planned.events.length > 0 ||
@@ -13184,21 +13200,27 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1318413200 dispatchWithReceiptEffect(command),
1318513201 );
1318613202 // A parent's lock comes before its side chats' locks, so a delete cascade and a side chat
13187- // fork or command never interleave.
13203+ // fork or command never interleave. A fork from a side chat or onto an existing thread would
13204+ // invert that order; dispatch rejects both, so they skip the source lock.
1318813205 if (
1318913206 command.type === "thread.fork" &&
1319013207 command.sideChat === true &&
1319113208 command.sourceThreadId !== command.targetThreadId
13192- )
13193- return yield* threadDispatch.withLock(command.sourceThreadId, dispatch);
13209+ ) {
13210+ const source = yield* findThread(command.sourceThreadId);
13211+ const target = yield* findThread(command.targetThreadId);
13212+ return yield* source === null || source.lineage.sideChat === true || target !== null
13213+ ? dispatch
13214+ : threadDispatch.withLock(command.sourceThreadId, dispatch);
13215+ }
1319413216 if (command.type === "thread.delete")
1319513217 return yield* threadDispatch.withLock(
1319613218 command.threadId,
1319713219 liveSideChatIds(command.threadId).pipe(
1319813220 Effect.flatMap((sideChatIds) =>
1319913221 sideChatIds.reduce(
1320013222 (locked, sideChatId) => threadDispatch.withLock(sideChatId, locked),
13201- dispatchWithReceiptEffect(command),
13223+ dispatchWithReceiptEffect(command, sideChatIds ),
1320213224 ),
1320313225 ),
1320413226 ),
0 commit comments