Use Effect Random for orchestration event ids

Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
Cursor AgentandJulius Marminge committed 2026-05-07 19:21:14 +00:00
1 parent 2afe2abe5e
commit 2e8b8740a1
2 files changed
+64 -56

No files matched your search

@@ -1,6 +1,6 @@
import { CommandId, EventId, ProjectId } from "@t3tools/contracts";
import { assert, it } from "@effect/vitest";
import { DateTime, Duration, Effect, Layer } from "effect";
import { DateTime, Duration, Effect, Random } from "effect";
import { TestClock } from "effect/testing";
import { decideOrchestrationCommand } from "./decider.ts";
@@ -8,6 +8,10 @@ import { createEmptyReadModel, projectEvent } from "./projector.ts";
const projectId = ProjectId.make("project-clock");
const createdAt = "2026-01-01T00:00:00.000Z";
const zeroRandomService = {
nextIntUnsafe: () => 0,
nextDoubleUnsafe: () => 0,
};
it.effect("uses the Effect clock for generated project update timestamps", () =>
Effect.gen(function* () {
@@ -57,6 +61,10 @@ it.effect("uses the Effect clock for generated project update timestamps", () =>
}
assert.equal(event.occurredAt, expectedNow);
assert.equal(event.eventId, EventId.make("00000000-0000-4000-8000-000000000000"));
assert.equal(event.payload.updatedAt, expectedNow);
}).pipe(Effect.provide(Layer.merge(TestClock.layer(), Layer.empty))),
}).pipe(
Effect.provide(TestClock.layer()),
Effect.provideService(Random.Random, zeroRandomService),
),
);
+54 -54
View File
@@ -3,7 +3,7 @@ import type {
OrchestrationEvent,
OrchestrationReadModel,
} from "@t3tools/contracts";
import { DateTime, Effect } from "effect";
import { DateTime, Effect, Random } from "effect";
import { OrchestrationCommandInvariantError } from "./Errors.ts";
import {
@@ -30,17 +30,17 @@ function withEventBase(
readonly occurredAt: string;
readonly metadata?: OrchestrationEvent["metadata"];
},
): Omit<OrchestrationEvent, "sequence" | "type" | "payload"> {
return {
eventId: crypto.randomUUID() as OrchestrationEvent["eventId"],
): Effect.Effect<Omit<OrchestrationEvent, "sequence" | "type" | "payload">> {
return Effect.map(Random.nextUUIDv4, (eventId) => ({
...defaultMetadata,
eventId: eventId as OrchestrationEvent["eventId"],
aggregateKind: input.aggregateKind,
aggregateId: input.aggregateId,
occurredAt: input.occurredAt,
commandId: input.commandId,
causationEventId: null,
correlationId: input.commandId,
metadata: input.metadata ?? {},
};
}));
}
type PlannedOrchestrationEvent = Omit<OrchestrationEvent, "sequence">;
@@ -95,12 +95,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
});
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "project",
aggregateId: command.projectId,
occurredAt: command.createdAt,
commandId: command.commandId,
}),
})),
type: "project.created",
payload: {
projectId: command.projectId,
@@ -122,12 +122,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
});
const occurredAt = yield* nowIso;
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "project",
aggregateId: command.projectId,
occurredAt,
commandId: command.commandId,
}),
})),
type: "project.meta-updated",
payload: {
projectId: command.projectId,
@@ -179,12 +179,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
const occurredAt = yield* nowIso;
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "project",
aggregateId: command.projectId,
occurredAt,
commandId: command.commandId,
}),
})),
type: "project.deleted" as const,
payload: {
projectId: command.projectId,
@@ -205,12 +205,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
threadId: command.threadId,
});
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt: command.createdAt,
commandId: command.commandId,
}),
})),
type: "thread.created",
payload: {
threadId: command.threadId,
@@ -235,12 +235,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
});
const occurredAt = yield* nowIso;
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt,
commandId: command.commandId,
}),
})),
type: "thread.deleted",
payload: {
threadId: command.threadId,
@@ -257,12 +257,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
});
const occurredAt = yield* nowIso;
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt,
commandId: command.commandId,
}),
})),
type: "thread.archived",
payload: {
threadId: command.threadId,
@@ -280,12 +280,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
});
const occurredAt = yield* nowIso;
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt,
commandId: command.commandId,
}),
})),
type: "thread.unarchived",
payload: {
threadId: command.threadId,
@@ -302,12 +302,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
});
const occurredAt = yield* nowIso;
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt,
commandId: command.commandId,
}),
})),
type: "thread.meta-updated",
payload: {
threadId: command.threadId,
@@ -330,12 +330,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
});
const occurredAt = yield* nowIso;
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt,
commandId: command.commandId,
}),
})),
type: "thread.runtime-mode-set",
payload: {
threadId: command.threadId,
@@ -353,12 +353,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
});
const occurredAt = yield* nowIso;
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt,
commandId: command.commandId,
}),
})),
type: "thread.interaction-mode-set",
payload: {
threadId: command.threadId,
@@ -399,12 +399,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
});
}
const userMessageEvent: Omit<OrchestrationEvent, "sequence"> = {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt: command.createdAt,
commandId: command.commandId,
}),
})),
type: "thread.message-sent",
payload: {
threadId: command.threadId,
@@ -419,12 +419,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
},
};
const turnStartRequestedEvent: Omit<OrchestrationEvent, "sequence"> = {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt: command.createdAt,
commandId: command.commandId,
}),
})),
causationEventId: userMessageEvent.eventId,
type: "thread.turn-start-requested",
payload: {
@@ -450,12 +450,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
threadId: command.threadId,
});
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt: command.createdAt,
commandId: command.commandId,
}),
})),
type: "thread.turn-interrupt-requested",
payload: {
threadId: command.threadId,
@@ -472,7 +472,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
threadId: command.threadId,
});
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt: command.createdAt,
@@ -480,7 +480,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
metadata: {
requestId: command.requestId,
},
}),
})),
type: "thread.approval-response-requested",
payload: {
threadId: command.threadId,
@@ -498,7 +498,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
threadId: command.threadId,
});
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt: command.createdAt,
@@ -506,7 +506,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
metadata: {
requestId: command.requestId,
},
}),
})),
type: "thread.user-input-response-requested",
payload: {
threadId: command.threadId,
@@ -524,12 +524,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
threadId: command.threadId,
});
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt: command.createdAt,
commandId: command.commandId,
}),
})),
type: "thread.checkpoint-revert-requested",
payload: {
threadId: command.threadId,
@@ -546,12 +546,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
threadId: command.threadId,
});
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt: command.createdAt,
commandId: command.commandId,
}),
})),
type: "thread.session-stop-requested",
payload: {
threadId: command.threadId,
@@ -567,13 +567,13 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
threadId: command.threadId,
});
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt: command.createdAt,
commandId: command.commandId,
metadata: {},
}),
})),
type: "thread.session-set",
payload: {
threadId: command.threadId,
@@ -589,12 +589,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
threadId: command.threadId,
});
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt: command.createdAt,
commandId: command.commandId,
}),
})),
type: "thread.message-sent",
payload: {
threadId: command.threadId,
@@ -616,12 +616,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
threadId: command.threadId,
});
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt: command.createdAt,
commandId: command.commandId,
}),
})),
type: "thread.message-sent",
payload: {
threadId: command.threadId,
@@ -643,12 +643,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
threadId: command.threadId,
});
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt: command.createdAt,
commandId: command.commandId,
}),
})),
type: "thread.proposed-plan-upserted",
payload: {
threadId: command.threadId,
@@ -664,12 +664,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
threadId: command.threadId,
});
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt: command.createdAt,
commandId: command.commandId,
}),
})),
type: "thread.turn-diff-completed",
payload: {
threadId: command.threadId,
@@ -691,12 +691,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
threadId: command.threadId,
});
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt: command.createdAt,
commandId: command.commandId,
}),
})),
type: "thread.reverted",
payload: {
threadId: command.threadId,
@@ -720,13 +720,13 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
.requestId as OrchestrationEvent["metadata"]["requestId"])
: undefined;
return {
...withEventBase({
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt: command.createdAt,
commandId: command.commandId,
...(requestId !== undefined ? { metadata: { requestId } } : {}),
}),
})),
type: "thread.activity-appended",
payload: {
threadId: command.threadId,