fix(server): reject replaying a command receipt across threads in v2

Ports v1's #5246 guard into the v2 dispatcher: a stored receipt only
proves that this exact command already ran for the thread it was recorded
against, so returning it for a command aimed at a different thread reports
success for work that never happened there. The check is extracted as
canReplayCommandReceipt so the rule is unit-testable, and reuse now fails
with OrchestratorCommandIdConflictError like the v1 path.

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
Julius MarmingeandClaude Fable 5 committed 2026-08-15 17:25:53 +02:00
1 parent 0c68291bc6
commit 993407dd9e
2 files changed
+56 -2

No files matched your search

@@ -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);
});
@@ -129,12 +129,40 @@ export class OrchestratorCommandPreviouslyRejectedError extends Schema.TaggedErr
}
}
export class OrchestratorCommandIdConflictError extends Schema.TaggedErrorClass<OrchestratorCommandIdConflictError>()(
"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<OrchestrationV2StoredEvent> => Array.from(events)),