diff --git a/apps/server/src/orchestration-v2/Orchestrator.migration.test.ts b/apps/server/src/orchestration-v2/Orchestrator.migration.test.ts index 0509e054a9..15498c360d 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.migration.test.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.migration.test.ts @@ -1,7 +1,11 @@ import { assert, it } from "@effect/vitest"; -import { ContextHandoffId } from "@t3tools/contracts"; +import { ContextHandoffId, ThreadId } from "@t3tools/contracts"; -import { appendContextHandoffId, shouldPrepareLegacyImportHandoff } from "./Orchestrator.ts"; +import { + appendContextHandoffId, + canReplayCommandReceipt, + shouldPrepareLegacyImportHandoff, +} from "./Orchestrator.ts"; it("reissues imported context until a V2 run completes", () => { assert.isTrue( @@ -47,3 +51,13 @@ it("records a reissued legacy handoff on an existing provider thread", () => { ]); assert.deepEqual(appendContextHandoffId([existingHandoffId], null), [existingHandoffId]); }); + +it("only replays a command receipt for the thread it was recorded against", () => { + const threadA = ThreadId.make("thread-a"); + const threadB = ThreadId.make("thread-b"); + + assert.strictEqual(canReplayCommandReceipt(threadA, threadA), true); + // A reused command id aimed at another thread must not report the first + // thread's success as this thread's (v1 #5246). + assert.strictEqual(canReplayCommandReceipt(threadA, threadB), false); +}); diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index 4bb8f7d56a..186eda17ea 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -129,12 +129,40 @@ export class OrchestratorCommandPreviouslyRejectedError extends Schema.TaggedErr } } +export class OrchestratorCommandIdConflictError extends Schema.TaggedErrorClass()( + "OrchestratorCommandIdConflictError", + { + commandId: CommandId, + commandType: Schema.String, + receiptThreadId: ThreadId, + commandThreadId: ThreadId, + }, +) { + override get message(): string { + return `Command ${this.commandId} was already handled for thread ${this.receiptThreadId} and cannot be replayed for ${this.commandThreadId}.`; + } +} + +/** + * A command receipt only proves that this exact command already ran for the + * thread it was recorded against. Replaying it for a command aimed at another + * thread would report success for work that never happened there, so the + * dispatcher rejects the reuse instead (mirrors v1's command-id conflict). + */ +export function canReplayCommandReceipt( + receiptThreadId: ThreadId, + commandThreadId: ThreadId, +): boolean { + return receiptThreadId === commandThreadId; +} + export const OrchestratorV2Error = Schema.Union([ OrchestratorDispatchError, OrchestratorProjectionError, OrchestratorDomainEventStreamError, OrchestratorProviderAdapterError, OrchestratorCommandPreviouslyRejectedError, + OrchestratorCommandIdConflictError, ]); export type OrchestratorV2Error = typeof OrchestratorV2Error.Type; @@ -6845,6 +6873,18 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio detail: receipt.error ?? "Previously rejected.", }); } + // A receipt only proves this exact command was handled for its own + // thread. Replaying it for a command aimed at another thread would + // report success for work that never happened. + const dispatchThreadId = commandThreadId(command); + if (!canReplayCommandReceipt(receipt.threadId, dispatchThreadId)) { + return yield* new OrchestratorCommandIdConflictError({ + commandId: command.commandId, + commandType: command.type, + receiptThreadId: receipt.threadId, + commandThreadId: dispatchThreadId, + }); + } const storedEvents = yield* eventSink.readByCommandId({ commandId: command.commandId }).pipe( Stream.runCollect, Effect.map((events): ReadonlyArray => Array.from(events)),