fix(server): replay all un-applied events during projection bootstrap (#7538)

Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
Tradi3andTheo Browne authored and GitHub committed 2026-08-27 02:58:24 -07:00
1 parent 64ca3b650e
commit a6797b3b97
2 files changed
+72

No files matched your search

@@ -1255,6 +1255,77 @@ it.layer(Layer.fresh(makeProjectionPipelinePrefixedTestLayer("t3-projection-atta
);
it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
it.effect("replays a bootstrap backlog larger than the event store default limit", () =>
Effect.gen(function* () {
const projectionPipeline = yield* OrchestrationProjectionPipeline;
const eventStore = yield* OrchestrationEventStore;
const sql = yield* SqlClient.SqlClient;
const now = "2026-01-01T00:00:00.000Z";
const projectId = ProjectId.make("project-bootstrap-backlog");
const sequenceRows = yield* sql<{ readonly maxSequence: number | null }>`
SELECT MAX(sequence) AS "maxSequence" FROM orchestration_events
`;
const sequenceBeforeBacklog = sequenceRows[0]?.maxSequence ?? 0;
const appendedEvents = yield* Effect.forEach(
Array.from({ length: 1_001 }, (_, index) => index),
(index) => {
const eventId = EventId.make(`evt-bootstrap-backlog-${index}`);
const commandId = CommandId.make(`cmd-bootstrap-backlog-${index}`);
return eventStore.append({
type: "project.created",
eventId,
aggregateKind: "project",
aggregateId: projectId,
occurredAt: now,
commandId,
causationEventId: null,
correlationId: CorrelationId.make(commandId),
metadata: {},
payload: {
projectId,
title: `Bootstrap backlog ${index}`,
workspaceRoot: "/tmp/project-bootstrap-backlog",
defaultModelSelection: null,
scripts: [],
createdAt: now,
updatedAt: now,
},
});
},
);
const lastSequence = appendedEvents[appendedEvents.length - 1]!.sequence;
yield* Effect.forEach(
Object.values(ORCHESTRATION_PROJECTOR_NAMES),
(projector) => {
const lastAppliedSequence =
projector === ORCHESTRATION_PROJECTOR_NAMES.projects
? sequenceBeforeBacklog
: lastSequence;
return sql`
INSERT INTO projection_state (projector, last_applied_sequence, updated_at)
VALUES (${projector}, ${lastAppliedSequence}, ${now})
ON CONFLICT (projector)
DO UPDATE SET
last_applied_sequence = excluded.last_applied_sequence,
updated_at = excluded.updated_at
`;
},
{ discard: true },
);
yield* projectionPipeline.bootstrap;
const stateRows = yield* sql<{ readonly lastAppliedSequence: number }>`
SELECT last_applied_sequence AS "lastAppliedSequence"
FROM projection_state
WHERE projector = ${ORCHESTRATION_PROJECTOR_NAMES.projects}
`;
assert.deepEqual(stateRows, [{ lastAppliedSequence: lastSequence }]);
}),
);
it.effect("resumes from projector last_applied_sequence without replaying older events", () =>
Effect.gen(function* () {
const projectionPipeline = yield* OrchestrationProjectionPipeline;
@@ -1727,6 +1727,7 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
Stream.runForEach(
eventStore.readFromSequence(
Option.isSome(stateRow) ? stateRow.value.lastAppliedSequence : 0,
Number.MAX_SAFE_INTEGER,
),
(event) => runProjectorForEvent(projector, event),
),