Skip to content

Commit 3e502d0

Browse files
PR Batch Testerclaude
andcommitted
test(server): cover shell replay across a full page and over the wire
Two gaps CodeRabbit found in the shell skip-unknown fix: - The store-level test only had one unknown row, so pagination counting decoded rows instead of raw ones would have passed anyway. Added a test with a full READ_PAGE_SIZE page of unknown rows before a later known event, confirmed it fails if pagination counts post-filter rows (verified by temporarily reintroducing that exact bug and reverting), and exported READ_PAGE_SIZE so the test doesn't hardcode a number that could drift. - No test called subscribeOrchestrationV2Shell itself anywhere in the repo, so the ws.ts wiring (skipUnknownEventTypes: true) had no regression coverage. Added one in ws.test.ts, with a real OrchestrationEventStoreLive + SqlitePersistenceMemory layer and stubs for the four peripheral services (ThreadManagementService and ProjectStoreV2 stay bare since this fixture never looks up a thread or lists projects; ProjectService.getShell answers for the one project the replay actually projects). Confirmed it fails if skipUnknownEventTypes: true is removed from ws.ts. subscribeOrchestrationV2Thread has the same gap (nothing calls it in any test either), but no equivalent case was added there - out of scope for this round. Co-Authored-By: Claude Sonnet 5 <[email protected]>
1 parent c6f38e1 commit 3e502d0

3 files changed

Lines changed: 205 additions & 2 deletions

File tree

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

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
import { PersistenceDecodeError } from "../Errors.ts";
2828
import { toShellApplicationEvent } from "../../orchestration-v2/ShellStream.ts";
2929
import * as OrchestrationEventStore from "../Services/OrchestrationEventStore.ts";
30-
import { OrchestrationEventStoreLive } from "./OrchestrationEventStore.ts";
30+
import { OrchestrationEventStoreLive, READ_PAGE_SIZE } from "./OrchestrationEventStore.ts";
3131
import { SqlitePersistenceMemory } from "./Sqlite.ts";
3232
const isPersistenceDecodeError = Schema.is(PersistenceDecodeError);
3333
const isLiveStreamBufferError = Schema.is(LiveStreamBufferError);
@@ -576,6 +576,85 @@ layer("OrchestrationEventStore", (it) => {
576576
}),
577577
);
578578

579+
it.effect(
580+
"readApplicationEvents with skipUnknownEventTypes pages past a full page of unknown rows",
581+
() =>
582+
Effect.gen(function* () {
583+
const store = yield* OrchestrationEventStore.OrchestrationEventStore;
584+
const sql = yield* SqlClient.SqlClient;
585+
const baseline = yield* store.latestApplicationSequence;
586+
const now = "2026-01-05T00:00:01.000Z";
587+
const threadId = "thread:shell-skip-unknown-page-boundary";
588+
const projectEvent = (suffix: string) => ({
589+
type: "project.created" as const,
590+
eventId: EventId.make(`evt-shell-skip-unknown-page-${suffix}`),
591+
aggregateKind: "project" as const,
592+
aggregateId: ProjectId.make(`project-shell-skip-unknown-page-${suffix}`),
593+
occurredAt: now,
594+
commandId: null,
595+
causationEventId: null,
596+
correlationId: null,
597+
metadata: {},
598+
payload: {
599+
projectId: ProjectId.make(`project-shell-skip-unknown-page-${suffix}`),
600+
title: `Shell skip-unknown page ${suffix}`,
601+
workspaceRoot: `/tmp/project-shell-skip-unknown-page-${suffix}`,
602+
defaultModelSelection: null,
603+
scripts: [],
604+
createdAt: now,
605+
updatedAt: now,
606+
},
607+
});
608+
609+
const before = yield* store.appendProjectEvent(projectEvent("before"));
610+
// A full raw page (READ_PAGE_SIZE) of rows a newer build wrote, none
611+
// of which this build knows -- the skip has to carry the cursor past
612+
// an entire page, not just one row at a time. If pagination counted
613+
// decoded (post-filter) rows instead of raw SQL rows, a page that
614+
// decodes to zero kept events would look like the last page and this
615+
// read would never reach "after".
616+
yield* Effect.forEach(
617+
Array.from({ length: READ_PAGE_SIZE }, (_, index) => index),
618+
(index) => sql`
619+
INSERT INTO orchestration_events (
620+
event_id, aggregate_kind, stream_id, stream_version, event_type,
621+
occurred_at, actor_kind, payload_json, metadata_json, application_event_version
622+
) VALUES (
623+
${`event:shell-skip-unknown-page:${index}`}, 'thread', ${threadId}, ${index},
624+
'thread.turn-item.exotic-future-feature', ${now}, 'server',
625+
'{"anything":true}', '{}', 2
626+
)
627+
`,
628+
{ discard: true },
629+
);
630+
const after = yield* store.appendProjectEvent(projectEvent("after"));
631+
632+
const throughSequence = yield* store.latestApplicationSequence;
633+
const replayed = yield* store
634+
.readApplicationEvents({
635+
afterSequence: baseline,
636+
throughSequence,
637+
skipUnknownEventTypes: true,
638+
})
639+
.pipe(Stream.runCollect);
640+
assert.deepEqual(
641+
Array.from(replayed, (event) => event.sequence),
642+
[before.sequence, after.sequence],
643+
);
644+
645+
// Same range, strict: still fails outright without the opt-in.
646+
const strictResult = yield* Effect.result(
647+
store
648+
.readApplicationEvents({ afterSequence: baseline, throughSequence })
649+
.pipe(Stream.runCollect),
650+
);
651+
assert.equal(strictResult._tag, "Failure");
652+
if (strictResult._tag === "Failure") {
653+
assert.isTrue(isPersistenceDecodeError(strictResult.failure));
654+
}
655+
}),
656+
);
657+
579658
it.effect(
580659
"readApplicationEvents with skipUnknownEventTypes still fails a known type with a broken payload",
581660
() =>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ const ProjectEventPersistedRowSchema = Schema.Struct({
7171
metadata: EventMetadataFromJsonString,
7272
});
7373

74-
const READ_PAGE_SIZE = 500;
74+
/** Exported so tests can exercise a full raw page without hardcoding a number that could drift. */
75+
export const READ_PAGE_SIZE = 500;
7576

7677
interface ApplicationEventRow {
7778
readonly sequence: number;

‎apps/server/src/ws.test.ts‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { assert, it } from "@effect/vitest";
22
import {
3+
EventId,
34
ORCHESTRATION_PROTOCOL_VERSION,
5+
ProjectId,
6+
type OrchestrationProjectShell,
47
type ServerConfig,
58
type ServerConfigStreamEvent,
69
} from "@t3tools/contracts";
@@ -12,17 +15,28 @@ import * as Effect from "effect/Effect";
1215
import * as Fiber from "effect/Fiber";
1316
import * as FileSystem from "effect/FileSystem";
1417
import * as Layer from "effect/Layer";
18+
import * as Option from "effect/Option";
1519
import * as Path from "effect/Path";
20+
import * as PubSub from "effect/PubSub";
1621
import * as Queue from "effect/Queue";
1722
import * as Stream from "effect/Stream";
1823
import * as TestClock from "effect/testing/TestClock";
1924
import { ChildProcessSpawner } from "effect/unstable/process";
25+
import * as SqlClient from "effect/unstable/sql/SqlClient";
2026

27+
import { ThreadManagementService } from "./orchestration-v2/ThreadManagementService.ts";
28+
import * as ProjectStore from "./orchestration-v2/ProjectStore.ts";
29+
import { OrchestrationEventStoreLive } from "./persistence/Layers/OrchestrationEventStore.ts";
30+
import { SqlitePersistenceMemory } from "./persistence/Layers/Sqlite.ts";
31+
import { OrchestrationEventStore } from "./persistence/Services/OrchestrationEventStore.ts";
2132
import * as ExternalLauncher from "./process/externalLauncher.ts";
33+
import { ProjectEnrichmentService } from "./project/ProjectEnrichmentService.ts";
34+
import { ProjectService } from "./project/ProjectService.ts";
2235
import {
2336
hasCompatibleOrchestrationProtocol,
2437
resolveAvailableEditorsForConfig,
2538
shouldUseBoundedThreadSnapshot,
39+
subscribeOrchestrationV2Shell,
2640
withLateEditorConfig,
2741
} from "./ws.ts";
2842

@@ -234,3 +248,112 @@ it.effect("recovers a reveal kind whose real probe outlasts the config timeout",
234248
}
235249
}).pipe(Effect.scoped),
236250
);
251+
252+
// subscribeOrchestrationV2Shell's resume path needs a real event store (for
253+
// readApplicationEvents/skipUnknownEventTypes) plus the four services it
254+
// reads shell state through. Thread and project shells are never looked up
255+
// in this fixture (the only thread-aggregate row is the unknown one, which
256+
// gets filtered before reaching them), so ThreadManagementService and
257+
// ProjectStoreV2 stay bare stubs; ProjectService.getShell is real enough to
258+
// answer for the one project the replay actually projects.
259+
const ShellSubscribeTestLayer = OrchestrationEventStoreLive.pipe(
260+
Layer.provideMerge(SqlitePersistenceMemory),
261+
);
262+
263+
it.effect(
264+
"subscribeOrchestrationV2Shell resumes past an unknown event type and still synchronizes",
265+
() =>
266+
Effect.gen(function* () {
267+
const store = yield* OrchestrationEventStore;
268+
const sql = yield* SqlClient.SqlClient;
269+
const now = "2026-01-05T00:00:02.000Z";
270+
const afterProjectId = ProjectId.make("project-ws-shell-skip-unknown-after");
271+
const projectEvent = (suffix: string, projectId: ReturnType<typeof ProjectId.make>) => ({
272+
type: "project.created" as const,
273+
eventId: EventId.make(`evt-ws-shell-skip-unknown-${suffix}`),
274+
aggregateKind: "project" as const,
275+
aggregateId: projectId,
276+
occurredAt: now,
277+
commandId: null,
278+
causationEventId: null,
279+
correlationId: null,
280+
metadata: {},
281+
payload: {
282+
projectId,
283+
title: `Shell ws skip-unknown ${suffix}`,
284+
workspaceRoot: `/tmp/project-ws-shell-skip-unknown-${suffix}`,
285+
defaultModelSelection: null,
286+
scripts: [],
287+
createdAt: now,
288+
updatedAt: now,
289+
},
290+
});
291+
292+
const before = yield* store.appendProjectEvent(
293+
projectEvent("before", ProjectId.make("project-ws-shell-skip-unknown-before")),
294+
);
295+
// Row a newer build wrote that this build has never learned -- what a
296+
// downgrade leaves behind, between the resume point and the next known
297+
// event in the replay range.
298+
yield* sql`
299+
INSERT INTO orchestration_events (
300+
event_id, aggregate_kind, stream_id, stream_version, event_type,
301+
occurred_at, actor_kind, payload_json, metadata_json, application_event_version
302+
) VALUES (
303+
${"event:ws-shell-skip-unknown:future"}, 'thread', ${"thread:ws-shell-skip-unknown"}, 0,
304+
'thread.turn-item.exotic-future-feature', ${now}, 'server',
305+
'{"anything":true}', '{}', 2
306+
)
307+
`;
308+
const after = yield* store.appendProjectEvent(projectEvent("after", afterProjectId));
309+
310+
const afterShell = {
311+
id: afterProjectId,
312+
title: "Shell ws skip-unknown after",
313+
workspaceRoot: "/tmp/project-ws-shell-skip-unknown-after",
314+
repositoryIdentity: null,
315+
defaultModelSelection: null,
316+
scripts: [],
317+
createdAt: now,
318+
updatedAt: now,
319+
} as unknown as OrchestrationProjectShell;
320+
321+
const updates = yield* PubSub.unbounded();
322+
const subscribeChanges = PubSub.subscribe(updates);
323+
324+
const items: Array<string> = [];
325+
const result = yield* Effect.result(
326+
subscribeOrchestrationV2Shell({
327+
afterSequence: before.sequence,
328+
requestCompletionMarker: true,
329+
}).pipe(
330+
Effect.provideService(ThreadManagementService, {} as never),
331+
Effect.provideService(ProjectStore.ProjectStoreV2, {
332+
listShells: () => Effect.succeed([]),
333+
} as never),
334+
Effect.provideService(ProjectService, {
335+
getShell: (projectId: typeof afterProjectId) =>
336+
Effect.succeed(
337+
projectId === afterProjectId ? Option.some(afterShell) : Option.none(),
338+
),
339+
} as never),
340+
Effect.provideService(ProjectEnrichmentService, { subscribeChanges } as never),
341+
Effect.flatMap((stream) =>
342+
stream.pipe(
343+
Stream.tap((item) => Effect.sync(() => items.push(item.kind))),
344+
// The fix continues past the unknown row into the live tail,
345+
// which never completes on its own -- stop as soon as the
346+
// completion marker lands instead of collecting forever.
347+
Stream.takeUntil((item) => item.kind === "synchronized"),
348+
Stream.runCollect,
349+
),
350+
),
351+
),
352+
);
353+
354+
assert.equal(result._tag, "Success");
355+
assert.include(items, "synchronized");
356+
assert.include(items, "project.updated");
357+
assert.isAbove(after.sequence, before.sequence);
358+
}).pipe(Effect.provide(ShellSubscribeTestLayer)),
359+
);

0 commit comments

Comments
 (0)