Skip to content

Commit baa977a

Browse files
committed
fix(server): make selective projection replay-safe
Route events only to projectors that can mutate their projections while advancing every live cursor atomically after successful projection. Preserve first ownership bindings for projected identities, order bootstrap dependencies, and prune approvals owned by reverted turns so live projection and replay stay equivalent.
1 parent 5104996 commit baa977a

9 files changed

Lines changed: 1084 additions & 94 deletions

‎apps/server/src/orchestration/Layers/ProjectionPipeline.summaryCost.test.ts‎

Lines changed: 716 additions & 32 deletions
Large diffs are not rendered by default.

‎apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts‎

Lines changed: 100 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -734,38 +734,37 @@ it.layer(
734734
END;
735735
`;
736736

737-
const result = yield* Effect.result(
738-
appendAndProject({
739-
type: "thread.message-sent",
740-
eventId: EventId.make("evt-rollback-3"),
741-
aggregateKind: "thread",
742-
aggregateId: ThreadId.make("thread-rollback"),
743-
occurredAt: now,
744-
commandId: CommandId.make("cmd-rollback-3"),
745-
causationEventId: null,
746-
correlationId: CorrelationId.make("cmd-rollback-3"),
747-
metadata: {},
748-
payload: {
749-
threadId: ThreadId.make("thread-rollback"),
750-
messageId: MessageId.make("message-rollback"),
751-
role: "user",
752-
text: "Rollback me",
753-
attachments: [
754-
{
755-
type: "image",
756-
id: "thread-rollback-att-1",
757-
name: "rollback.png",
758-
mimeType: "image/png",
759-
sizeBytes: 5,
760-
},
761-
],
762-
turnId: null,
763-
streaming: false,
764-
createdAt: now,
765-
updatedAt: now,
766-
},
767-
}),
768-
);
737+
const savedEvent = yield* eventStore.append({
738+
type: "thread.message-sent",
739+
eventId: EventId.make("evt-rollback-3"),
740+
aggregateKind: "thread",
741+
aggregateId: ThreadId.make("thread-rollback"),
742+
occurredAt: now,
743+
commandId: CommandId.make("cmd-rollback-3"),
744+
causationEventId: null,
745+
correlationId: CorrelationId.make("cmd-rollback-3"),
746+
metadata: {},
747+
payload: {
748+
threadId: ThreadId.make("thread-rollback"),
749+
messageId: MessageId.make("message-rollback"),
750+
role: "user",
751+
text: "Rollback me",
752+
attachments: [
753+
{
754+
type: "image",
755+
id: "thread-rollback-att-1",
756+
name: "rollback.png",
757+
mimeType: "image/png",
758+
sizeBytes: 5,
759+
},
760+
],
761+
turnId: null,
762+
streaming: false,
763+
createdAt: now,
764+
updatedAt: now,
765+
},
766+
});
767+
const result = yield* Effect.result(projectionPipeline.projectEvent(savedEvent));
769768
assert.equal(result._tag, "Failure");
770769

771770
const rows = yield* sql<{
@@ -780,7 +779,44 @@ it.layer(
780779
const { attachmentsDir } = yield* ServerConfig;
781780
const attachmentPath = path.join(attachmentsDir, "thread-rollback-att-1.png");
782781
assert.isFalse(yield* exists(attachmentPath));
782+
783+
const stateAfterFailure = yield* sql<{
784+
readonly projector: string;
785+
readonly lastAppliedSequence: number;
786+
}>`
787+
SELECT projector, last_applied_sequence AS "lastAppliedSequence"
788+
FROM projection_state
789+
ORDER BY projector ASC
790+
`;
791+
assert.lengthOf(stateAfterFailure, Object.keys(ORCHESTRATION_PROJECTOR_NAMES).length);
792+
for (const row of stateAfterFailure) {
793+
assert.equal(row.lastAppliedSequence, savedEvent.sequence - 1);
794+
}
795+
783796
yield* sql`DROP TRIGGER IF EXISTS fail_thread_messages_projection_state_update`;
797+
yield* projectionPipeline.projectEvent(savedEvent);
798+
799+
const rowsAfterRetry = yield* sql<{
800+
readonly text: string;
801+
}>`
802+
SELECT text
803+
FROM projection_thread_messages
804+
WHERE message_id = 'message-rollback'
805+
`;
806+
assert.deepEqual(rowsAfterRetry, [{ text: "Rollback me" }]);
807+
808+
const stateAfterRetry = yield* sql<{
809+
readonly projector: string;
810+
readonly lastAppliedSequence: number;
811+
}>`
812+
SELECT projector, last_applied_sequence AS "lastAppliedSequence"
813+
FROM projection_state
814+
ORDER BY projector ASC
815+
`;
816+
assert.lengthOf(stateAfterRetry, Object.keys(ORCHESTRATION_PROJECTOR_NAMES).length);
817+
for (const row of stateAfterRetry) {
818+
assert.equal(row.lastAppliedSequence, savedEvent.sequence);
819+
}
784820
}),
785821
);
786822
});
@@ -1249,7 +1285,13 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
12491285

12501286
yield* projectionPipeline.bootstrap;
12511287

1252-
yield* eventStore.append({
1288+
yield* sql`
1289+
UPDATE projection_state
1290+
SET last_applied_sequence = 2
1291+
WHERE projector = ${ORCHESTRATION_PROJECTOR_NAMES.threadMessages}
1292+
`;
1293+
1294+
const savedDelta = yield* eventStore.append({
12531295
type: "thread.message-sent",
12541296
eventId: EventId.make("evt-a4"),
12551297
aggregateKind: "thread",
@@ -1271,6 +1313,30 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
12711313
},
12721314
});
12731315

1316+
const liveResult = yield* Effect.result(projectionPipeline.projectEvent(savedDelta));
1317+
assert.equal(liveResult._tag, "Failure");
1318+
1319+
const messageBeforeResume = yield* sql<{ readonly text: string }>`
1320+
SELECT text FROM projection_thread_messages WHERE message_id = 'message-a'
1321+
`;
1322+
assert.deepEqual(messageBeforeResume, [{ text: "hello" }]);
1323+
1324+
const stateBeforeResume = yield* sql<{
1325+
readonly projector: string;
1326+
readonly lastAppliedSequence: number;
1327+
}>`
1328+
SELECT projector, last_applied_sequence AS "lastAppliedSequence"
1329+
FROM projection_state
1330+
ORDER BY projector ASC
1331+
`;
1332+
assert.lengthOf(stateBeforeResume, Object.keys(ORCHESTRATION_PROJECTOR_NAMES).length);
1333+
for (const row of stateBeforeResume) {
1334+
assert.equal(
1335+
row.lastAppliedSequence,
1336+
row.projector === ORCHESTRATION_PROJECTOR_NAMES.threadMessages ? 2 : 3,
1337+
);
1338+
}
1339+
12741340
yield* projectionPipeline.bootstrap;
12751341
yield* projectionPipeline.bootstrap;
12761342

@@ -1292,6 +1358,7 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
12921358
SELECT MAX(sequence) AS "maxSequence" FROM orchestration_events
12931359
`;
12941360
const maxSequence = maxSequenceRows[0]?.maxSequence ?? 0;
1361+
assert.lengthOf(stateRows, Object.keys(ORCHESTRATION_PROJECTOR_NAMES).length);
12951362
for (const row of stateRows) {
12961363
assert.equal(row.lastAppliedSequence, maxSequence);
12971364
}

0 commit comments

Comments
 (0)