Skip to content

Commit c6f38e1

Browse files
PR Batch Testerclaude
andcommitted
fix(server): shell resync also skips an unknown event type
subscribeOrchestrationV2Shell's resume path replayed with readApplicationEvents, which stayed strict even after the thread fix: a shell cursor from before a row this build doesn't know still failed the whole resubscribe with OrchestrationV2GetShellSnapshotError instead of sending the resumed shell and synchronized. readApplicationEvents now takes the same skipUnknownEventTypes opt-in as readAgentEvents, applied only by the shell's resume call in ws.ts. Pagination now counts raw SQL rows instead of post-filter decoded events, so a page that drops an unknown row mid-page still advances its cursor and "more pages?" check correctly - the gap this change closes also applies to project rows, which readApplicationEvents reads alongside thread rows, so the known-type check now branches on aggregate_kind. Everything that builds server state (EventSink catch-up, ingest, projections, the live tail) still calls through strict, since only the shell's one-shot replay call sets the flag. Store-level tests cover the skip and the still-fails-a-broken-known-type case, mirroring the thread-level tests. They do not reach ws.ts's subscribeOrchestrationV2Shell itself (the synchronized marker, snapshot fallback, or shell-stream projection), which would need a layer providing ThreadManagementService, ProjectStoreV2, ProjectService, and ProjectEnrichmentService; verified that path manually with a scripted call to subscribeOrchestrationV2Shell against a real SQLite-backed store, which now emits synchronized and logs exactly one skip warning. Co-Authored-By: Claude Sonnet 5 <[email protected]>
1 parent bc5a7df commit c6f38e1

4 files changed

Lines changed: 171 additions & 32 deletions

File tree

‎apps/server/src/persistence/Layers/OrchestrationEventStore.test.ts‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,123 @@ layer("OrchestrationEventStore", (it) => {
495495
}
496496
}),
497497
);
498+
499+
// Shell resume (ws.ts subscribeOrchestrationV2Shell) replays with
500+
// readApplicationEvents rather than readAgentEvents, over a stream that
501+
// mixes project and thread rows. These two tests are the store-level
502+
// equivalent of the readAgentEvents skip-unknown tests above; they do not
503+
// reach the ws.ts subscription itself (the "synchronized" completion
504+
// marker, project-metadata snapshot fallback, or shell-stream projection),
505+
// which would need a layer providing ThreadManagementService,
506+
// ProjectStoreV2, ProjectService, and ProjectEnrichmentService.
507+
it.effect(
508+
"readApplicationEvents with skipUnknownEventTypes drops a shell-resume row with an unknown event type",
509+
() =>
510+
Effect.gen(function* () {
511+
const store = yield* OrchestrationEventStore.OrchestrationEventStore;
512+
const sql = yield* SqlClient.SqlClient;
513+
const baseline = yield* store.latestApplicationSequence;
514+
const now = "2026-01-05T00:00:00.000Z";
515+
const projectEvent = (suffix: string) => ({
516+
type: "project.created" as const,
517+
eventId: EventId.make(`evt-shell-skip-unknown-${suffix}`),
518+
aggregateKind: "project" as const,
519+
aggregateId: ProjectId.make(`project-shell-skip-unknown-${suffix}`),
520+
occurredAt: now,
521+
commandId: null,
522+
causationEventId: null,
523+
correlationId: null,
524+
metadata: {},
525+
payload: {
526+
projectId: ProjectId.make(`project-shell-skip-unknown-${suffix}`),
527+
title: `Shell skip-unknown ${suffix}`,
528+
workspaceRoot: `/tmp/project-shell-skip-unknown-${suffix}`,
529+
defaultModelSelection: null,
530+
scripts: [],
531+
createdAt: now,
532+
updatedAt: now,
533+
},
534+
});
535+
536+
const before = yield* store.appendProjectEvent(projectEvent("before"));
537+
// Row a newer build wrote that this build has never learned -- what a
538+
// downgrade leaves behind. Same shape as the thread-replay fixture
539+
// above, on the aggregate shell replay also reads.
540+
yield* sql`
541+
INSERT INTO orchestration_events (
542+
event_id, aggregate_kind, stream_id, stream_version, event_type,
543+
occurred_at, actor_kind, payload_json, metadata_json, application_event_version
544+
) VALUES (
545+
${"event:shell-skip-unknown:future"}, 'thread', ${"thread:shell-skip-unknown"}, 0,
546+
'thread.turn-item.exotic-future-feature', ${now}, 'server',
547+
'{"anything":true}', '{}', 2
548+
)
549+
`;
550+
const after = yield* store.appendProjectEvent(projectEvent("after"));
551+
552+
const throughSequence = yield* store.latestApplicationSequence;
553+
const replayed = yield* store
554+
.readApplicationEvents({
555+
afterSequence: baseline,
556+
throughSequence,
557+
skipUnknownEventTypes: true,
558+
})
559+
.pipe(Stream.runCollect);
560+
assert.deepEqual(
561+
Array.from(replayed, (event) => event.sequence),
562+
[before.sequence, after.sequence],
563+
);
564+
565+
// Without the opt-in the exact same range still fails outright -- the
566+
// same line the thread replay fix drew, now on the shell's own reader.
567+
const strictResult = yield* Effect.result(
568+
store
569+
.readApplicationEvents({ afterSequence: baseline, throughSequence })
570+
.pipe(Stream.runCollect),
571+
);
572+
assert.equal(strictResult._tag, "Failure");
573+
if (strictResult._tag === "Failure") {
574+
assert.isTrue(isPersistenceDecodeError(strictResult.failure));
575+
}
576+
}),
577+
);
578+
579+
it.effect(
580+
"readApplicationEvents with skipUnknownEventTypes still fails a known type with a broken payload",
581+
() =>
582+
Effect.gen(function* () {
583+
const store = yield* OrchestrationEventStore.OrchestrationEventStore;
584+
const sql = yield* SqlClient.SqlClient;
585+
const baseline = yield* store.latestApplicationSequence;
586+
587+
yield* sql`
588+
INSERT INTO orchestration_events (
589+
event_id, aggregate_kind, stream_id, stream_version, event_type,
590+
occurred_at, actor_kind, payload_json, metadata_json, application_event_version
591+
) VALUES (
592+
${"event:shell-skip-unknown-broken-known:broken"}, 'project',
593+
${"project:shell-skip-unknown-broken-known"}, 0,
594+
'project.created', '2026-01-05T00:00:03.000Z', 'server',
595+
${'{"this":"is not a valid project.created payload"}'}, '{}', 2
596+
)
597+
`;
598+
599+
const throughSequence = yield* store.latestApplicationSequence;
600+
const result = yield* Effect.result(
601+
store
602+
.readApplicationEvents({
603+
afterSequence: baseline,
604+
throughSequence,
605+
skipUnknownEventTypes: true,
606+
})
607+
.pipe(Stream.runCollect),
608+
);
609+
assert.equal(result._tag, "Failure");
610+
if (result._tag === "Failure") {
611+
assert.isTrue(isPersistenceDecodeError(result.failure));
612+
}
613+
}),
614+
);
498615
});
499616

500617
it.effect.each(["high-water", "replay"] as const)(

‎apps/server/src/persistence/Layers/OrchestrationEventStore.ts‎

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,17 @@ function rowToApplicationStoredEvent(
158158
return row.aggregate_kind === "project" ? rowToProjectEvent(row) : rowToV2StoredEvent(row);
159159
}
160160

161+
const isKnownProjectEventType = Schema.is(ProjectEventType);
162+
163+
/** Whether `row.event_type` is a discriminant this build knows for its aggregate kind. */
164+
function isKnownApplicationEventType(
165+
row: Pick<ApplicationEventRow, "aggregate_kind" | "event_type">,
166+
): boolean {
167+
return row.aggregate_kind === "project"
168+
? isKnownProjectEventType(row.event_type)
169+
: isKnownOrchestrationV2EventType(row.event_type);
170+
}
171+
161172
function inferActorKind(
162173
event: OrchestrationEventStore.UnsequencedProjectEvent,
163174
): typeof ActorKind.Type {
@@ -530,53 +541,53 @@ const makeEventStore = Effect.gen(function* () {
530541
),
531542
);
532543

533-
const readApplicationEventPage = (input: {
534-
readonly afterSequence: number;
535-
readonly throughSequence: number;
536-
readonly limit: number;
537-
}): Stream.Stream<ApplicationStoredEvent, OrchestrationEventStoreError> =>
538-
Stream.fromEffect(
539-
readApplicationRows(input).pipe(
540-
Effect.mapError(
541-
toPersistenceSqlError("OrchestrationEventStore.readApplicationEvents:query"),
542-
),
543-
),
544-
).pipe(
545-
Stream.flatMap(Stream.fromIterable),
546-
Stream.mapEffect((row) =>
547-
rowToApplicationStoredEvent(row).pipe(
548-
Effect.mapError(
549-
toPersistenceDecodeError("OrchestrationEventStore.readApplicationEvents:decode"),
550-
),
551-
),
552-
),
553-
);
554-
544+
// Pagination counts raw SQL rows (not post-filter decoded events), exactly
545+
// like readAgentEvents: a page that reads a full READ_PAGE_SIZE of raw rows
546+
// must keep paginating even if some of those rows get dropped below as
547+
// unknown, or the cursor and the "more pages?" check would both undercount.
555548
const catchUpApplicationEvents = (input: {
556549
readonly afterSequence: number;
557550
readonly throughSequence: number;
558-
}): Stream.Stream<ApplicationStoredEvent, OrchestrationEventStoreError> => {
559-
return Stream.paginate(input.afterSequence, (afterSequence) =>
560-
readApplicationEventPage({
551+
readonly skipUnknownEventTypes?: boolean;
552+
}): Stream.Stream<ApplicationStoredEvent, OrchestrationEventStoreError> =>
553+
Stream.paginate(input.afterSequence, (afterSequence) =>
554+
readApplicationRows({
561555
afterSequence,
562556
throughSequence: input.throughSequence,
563557
limit: READ_PAGE_SIZE,
564558
}).pipe(
565-
Stream.runCollect,
566-
Effect.map((events) => {
567-
const last = events.at(-1);
559+
Effect.mapError(
560+
toPersistenceSqlError("OrchestrationEventStore.readApplicationEvents:query"),
561+
),
562+
Effect.map((rows) => {
563+
const last = rows.at(-1);
568564
return [
569-
events,
565+
rows,
570566
last === undefined ||
571-
events.length < READ_PAGE_SIZE ||
567+
rows.length < READ_PAGE_SIZE ||
572568
last.sequence >= input.throughSequence
573569
? Option.none()
574570
: Option.some(last.sequence),
575571
] as const;
576572
}),
577573
),
574+
).pipe(
575+
Stream.filterMapEffect((row) =>
576+
input.skipUnknownEventTypes === true && !isKnownApplicationEventType(row)
577+
? // Written by a newer build; a known type with a bad payload still fails.
578+
Effect.logWarning("Skipping an application event row with an unknown event type", {
579+
threadId: row.stream_id,
580+
sequence: row.sequence,
581+
eventTypeLength: row.event_type.length,
582+
}).pipe(Effect.as(Result.failVoid))
583+
: rowToApplicationStoredEvent(row).pipe(
584+
Effect.mapError(
585+
toPersistenceDecodeError("OrchestrationEventStore.readApplicationEvents:decode"),
586+
),
587+
Effect.map(Result.succeed),
588+
),
589+
),
578590
);
579-
};
580591

581592
const streamProjectedApplicationEvents: OrchestrationEventStore.OrchestrationEventStoreShape["streamProjectedApplicationEvents"] =
582593
(input) =>

‎apps/server/src/persistence/Services/OrchestrationEventStore.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,17 @@ export interface OrchestrationEventStoreShape {
104104

105105
readonly latestApplicationSequence: Effect.Effect<number, OrchestrationEventStoreError>;
106106

107-
/** Read the finite retained application-event range `(afterSequence, throughSequence]`. */
107+
/**
108+
* Read the finite retained application-event range `(afterSequence, throughSequence]`.
109+
*
110+
* A row whose `event_type` this build does not know fails the read unless
111+
* `skipUnknownEventTypes` is set, which logs and drops it instead. Only
112+
* client shell replay opts in; anything that builds server state stays strict.
113+
*/
108114
readonly readApplicationEvents: (input: {
109115
readonly afterSequence: number;
110116
readonly throughSequence: number;
117+
readonly skipUnknownEventTypes?: boolean;
111118
}) => Stream.Stream<ApplicationStoredEvent, OrchestrationEventStoreError>;
112119

113120
/** Publish only after the surrounding event/projection transaction commits. */

‎apps/server/src/ws.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,10 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
11501150
applicationEvents.readApplicationEvents({
11511151
afterSequence: input.afterSequence,
11521152
throughSequence: highWater,
1153+
// A row a newer build wrote (e.g. after a downgrade) must not fail
1154+
// every shell resubscribe. The live tail below resumes from
1155+
// highWater, so a dropped row never hides later events.
1156+
skipUnknownEventTypes: true,
11531157
}),
11541158
);
11551159
return composeShellStreamWithEnrichment({

0 commit comments

Comments
 (0)