TypeScript
fix(server): replay all un-applied events during projection bootstrap (#7538)
Tradi3a6797b3b97dc
diff --git a/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts b/apps/server/src/orchestration/Layers/ProjectionPipeline.test.tsindex b15d2f679ee..d27caed4a8e 100644--- a/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts+++ b/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts@@ -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;diff --git a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts b/apps/server/src/orchestration/Layers/ProjectionPipeline.tsindex e048826b9cc..348615b1033 100644--- a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts+++ b/apps/server/src/orchestration/Layers/ProjectionPipeline.ts@@ -1727,6 +1727,7 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjectiStream.runForEach(eventStore.readFromSequence(Option.isSome(stateRow) ? stateRow.value.lastAppliedSequence : 0,+ Number.MAX_SAFE_INTEGER,),(event) => runProjectorForEvent(projector, event),),