The best way to control Claude Code, Codex and more from any computer or phone
test(server): strengthen v1 cutover proof with event-stream waits and content snapshots (#11869)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Alex Southwell committed
Nov 5, 2026 at 18:40 UTC
221d8b8812d10a59b81c960c5fbc23a50753d4a7
1 file changed
+189
-58
apps/server/src/orchestration-v2/legacy/LegacyV1Cutover.integration.test.ts
+189
-58
@@ -1,4 +1,5 @@
1
import { assert, describe, it } from "@effect/vitest";
2
+import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
3
import * as NodeServices from "@effect/platform-node/NodeServices";
4
import {
5
CommandId,
@@ -14,8 +15,10 @@ import {
15
type OrchestrationV2ThreadProjection,
16
} from "@t3tools/contracts";
17
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
18
+import * as Crypto from "effect/Crypto";
19
import * as DateTime from "effect/DateTime";
20
import * as Effect from "effect/Effect";
21
+import * as Hex from "effect/encoding/Hex";
22
import * as FileSystem from "effect/FileSystem";
23
import * as Layer from "effect/Layer";
24
import * as Logger from "effect/Logger";
@@ -400,6 +403,37 @@ const seedV1Database = (fixturePath: string, workspace: string) =>
403
});
404
405
yield* sql`PRAGMA wal_checkpoint(TRUNCATE);`;
406
+
407
+ // Deterministic pre-migration snapshot of the legacy recovery tables;
408
+ // post-boot assertions compare preserved column contents, not counts.
409
+ // Migrations may append columns, so the comparison is restricted to the
410
+ // column set recorded here.
411
+ const tableColumns = Effect.fn("seedV1Database.tableColumns")(function* (table: string) {
412
+ const info = yield* sql<{
413
+ readonly name: string;
414
+ }>`SELECT name FROM pragma_table_info(${table})`;
415
+ return info.map((row) => row.name);
416
+ });
417
+ return {
418
+ projects: {
419
+ columns: yield* tableColumns("projection_projects"),
420
+ rows: yield* sql<Record<string, unknown>>`
421
+ SELECT * FROM projection_projects ORDER BY project_id
422
+ `,
423
+ },
424
+ threads: {
425
+ columns: yield* tableColumns("projection_threads"),
426
+ rows: yield* sql<Record<string, unknown>>`
427
+ SELECT * FROM projection_threads ORDER BY thread_id
428
+ `,
429
+ },
430
+ messages: {
431
+ columns: yield* tableColumns("projection_thread_messages"),
432
+ rows: yield* sql<Record<string, unknown>>`
433
+ SELECT * FROM projection_thread_messages ORDER BY message_id
434
+ `,
435
+ },
436
+ };
437
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: fixturePath }))),
438
);
439
@@ -551,32 +585,62 @@ const makeCodexAdapter = (capturedTurns: Ref.Ref<ReadonlyArray<CapturedTurn>>) =
585
}),
586
}) satisfies ProviderAdapter.ProviderAdapterV2["Service"];
587
554
-const waitForIdle = Effect.fn("LegacyV1Cutover.waitForIdle")(function* (threadId: ThreadId) {
555
- const orchestrator = yield* Orchestrator.OrchestratorV2;
556
- const eventSink = yield* EventSink.EventSinkV2;
557
- const isIdle = (projection: OrchestrationV2ThreadProjection) =>
558
- projection.runs.every(
559
- (run) => !["queued", "starting", "running", "waiting"].includes(run.status),
588
+// Await the dispatched run's terminal event on the persisted domain-event stream
589
+// instead of polling the projection, then drain remaining outbox effects.
590
+const dispatchMessageAwaitingRun = Effect.fn("LegacyV1Cutover.dispatchMessageAwaitingRun")(
591
+ function* (input: {
592
+ readonly threadId: ThreadId;
593
+ readonly messageId: string;
594
+ readonly commandId: string;
595
+ readonly text: string;
596
+ }) {
597
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
598
+ const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
599
+ const eventSink = yield* EventSink.EventSinkV2;
600
+ const messageId = MessageId.make(input.messageId);
601
+ // Capture the cursor before dispatch so even a late subscriber replays
602
+ // the terminal event instead of starting its live tail after completion.
603
+ const afterSequence = yield* eventSink.latestSequence();
604
+ const terminal = eventSink.stream({ afterSequence }).pipe(
605
+ Stream.map((stored) => stored.event),
606
+ Stream.filter((event) => event.type === "run.updated"),
607
+ Stream.filter(
608
+ (event) =>
609
+ ["completed", "failed", "interrupted", "cancelled", "rolled_back"].includes(
610
+ event.payload.status,
611
+ ) &&
612
+ event.payload.threadId === input.threadId &&
613
+ event.payload.userMessageId === messageId,
614
+ ),
615
+ Stream.take(1),
616
+ Stream.runForEach((event) =>
617
+ Effect.sync(() =>
618
+ assert.equal(event.payload.status, "completed", `Run for ${messageId} did not complete`),
619
+ ),
620
+ ),
621
);
561
- const afterSequence = yield* eventSink.latestSequence();
562
- const current = yield* orchestrator.getThreadProjection(threadId);
563
- if (isIdle(current)) {
564
- return current;
565
- }
566
- // Projections commit with their events, so each later event is the only time to re-read.
567
- const [idle] = yield* eventSink.stream({ afterSequence }).pipe(
568
- Stream.mapEffect(() => orchestrator.getThreadProjection(threadId)),
569
- Stream.filter(isIdle),
570
- Stream.take(1),
571
- Stream.runCollect,
572
- );
573
- if (idle === undefined) {
574
- return yield* Effect.die(
575
- new Error("Cutover test event stream ended before the thread went idle"),
622
+ yield* orchestrator.dispatch({
623
+ type: "message.dispatch",
624
+ createdBy: "user",
625
+ creationSource: "web",
626
+ commandId: CommandId.make(input.commandId),
627
+ threadId: input.threadId,
628
+ messageId,
629
+ text: input.text,
630
+ attachments: [],
631
+ dispatchMode: { type: "start_immediately" },
632
+ });
633
+ yield* worker.drain();
634
+ yield* terminal.pipe(
635
+ Effect.timeoutOrElse({
636
+ duration: "30 seconds",
637
+ orElse: () => Effect.die(`No terminal run.updated for ${messageId}`),
638
+ }),
639
);
577
- }
578
- return idle;
579
-});
640
+ yield* worker.drain();
641
+ return yield* orchestrator.getThreadProjection(input.threadId);
642
+ },
643
+);
644
645
const layerBoot = (input: {
646
readonly name: string;
@@ -639,6 +703,26 @@ const makeCapturingLogger = (logs: CapturedLog[]) =>
703
});
704
});
705
706
+const sha256File = Effect.fn("LegacyV1Cutover.sha256File")(function* (filePath: string) {
707
+ const fs = yield* FileSystem.FileSystem;
708
+ const crypto = yield* Crypto.Crypto;
709
+ return Hex.encode(
710
+ yield* crypto.digest("SHA-256", yield* fs.readFile(filePath)).pipe(Effect.orDie),
711
+ );
712
+}, Effect.provide(NodeCrypto.layer));
713
+
714
+interface LegacyTableSnapshot {
715
+ readonly columns: ReadonlyArray<string>;
716
+ readonly rows: ReadonlyArray<Record<string, unknown>>;
717
+}
718
+
719
+// Migrations legitimately append columns; preservation means the columns the
720
+// v1 schema already had keep their exact values.
721
+const restrictToSnapshotColumns = (
722
+ rows: ReadonlyArray<Record<string, unknown>>,
723
+ snapshot: LegacyTableSnapshot,
724
+) => rows.map((row) => Object.fromEntries(snapshot.columns.map((column) => [column, row[column]])));
725
+
726
const messageOrdinals = (projection: OrchestrationV2ThreadProjection) =>
727
projection.turnItems
728
.filter(
@@ -663,7 +747,8 @@ describe("orchestration v2 legacy v1 cutover", () => {
747
const copyPath = path.join(stateDir, "userdata", "state.sqlite");
748
yield* fs.makeDirectory(path.join(stateDir, "userdata"), { recursive: true });
749
666
- yield* seedV1Database(fixturePath, workspace);
750
+ const v1Snapshot = yield* seedV1Database(fixturePath, workspace);
751
+ const fixtureHashBefore = yield* sha256File(fixturePath);
752
yield* fs.copyFile(fixturePath, copyPath);
753
754
const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
@@ -676,7 +761,6 @@ describe("orchestration v2 legacy v1 cutover", () => {
761
const importer = yield* LegacyV1ThreadImporter.LegacyV1ThreadImporter;
762
const maintenance = yield* ProjectionReplay.ProjectionReplay;
763
const projections = yield* ProjectionStore.ProjectionStoreV2;
679
- const orchestrator = yield* Orchestrator.OrchestratorV2;
764
765
assert.equal(yield* importer.pendingThreadCount, ALL_THREADS.length);
766
const shellImport = yield* importer.reconcileShells;
@@ -808,6 +892,22 @@ describe("orchestration v2 legacy v1 cutover", () => {
892
`;
893
assert.isNull(snoozedImportRow[0]?.transcript_imported_at);
894
895
+ // A failed hydration leaves the legacy rows and pending marker
896
+ // available for retry in a fresh runtime after restart.
897
+ yield* sql`
898
+ CREATE TRIGGER fail_snoozed_hydration
899
+ BEFORE INSERT ON orchestration_events
900
+ WHEN NEW.event_id = 'migration:v1:message:message:cutover:snoozed:2'
901
+ BEGIN
902
+ SELECT RAISE(ABORT, 'injected cutover hydration failure');
903
+ END
904
+ `;
905
+ const hydrationError = yield* importer
906
+ .ensureTranscript(ThreadId.make(SNOOZED_THREAD))
907
+ .pipe(Effect.flip);
908
+ assert.equal(hydrationError._tag, "LegacyV1ThreadImportError");
909
+ assert.equal(yield* importer.pendingThreadCount, 1);
910
+
911
const interrupted = yield* projections.getThreadProjection(
912
ThreadId.make(INTERRUPTED_THREAD),
913
);
@@ -816,18 +916,12 @@ describe("orchestration v2 legacy v1 cutover", () => {
916
["message:cutover:interrupted:2", 2, "interrupted"],
917
]);
918
819
- yield* orchestrator.dispatch({
820
- type: "message.dispatch",
821
- createdBy: "user",
822
- creationSource: "web",
823
- commandId: CommandId.make("command:cutover:continue"),
919
+ const continued = yield* dispatchMessageAwaitingRun({
920
threadId: longThreadId,
825
- messageId: MessageId.make("message:cutover:long:continuation"),
921
+ messageId: "message:cutover:long:continuation",
922
+ commandId: "command:cutover:continue",
923
text: CONTINUATION_PROMPT,
827
- attachments: [],
828
- dispatchMode: { type: "start_immediately" },
924
});
830
- const continued = yield* waitForIdle(longThreadId);
925
926
const runs = continued.runs.filter((run) => run.ordinal >= 1);
927
assert.equal(runs.at(-1)?.status, "completed");
@@ -844,18 +938,12 @@ describe("orchestration v2 legacy v1 cutover", () => {
938
assert.include(turns[0]!.text, "Context handoff (manual_context):");
939
assert.include(turns[0]!.text, `User message:\n${CONTINUATION_PROMPT}`);
940
847
- yield* orchestrator.dispatch({
848
- type: "message.dispatch",
849
- createdBy: "user",
850
- creationSource: "web",
851
- commandId: CommandId.make("command:cutover:continue:again"),
941
+ const continuedAgain = yield* dispatchMessageAwaitingRun({
942
threadId: longThreadId,
853
- messageId: MessageId.make("message:cutover:long:continuation:2"),
943
+ messageId: "message:cutover:long:continuation:2",
944
+ commandId: "command:cutover:continue:again",
945
text: "One more.",
855
- attachments: [],
856
- dispatchMode: { type: "start_immediately" },
946
});
858
- const continuedAgain = yield* waitForIdle(longThreadId);
947
assert.equal(continuedAgain.contextHandoffs.length, 1);
948
assert.equal((yield* Ref.get(capturedTurns)).length, 2);
949
@@ -876,11 +964,14 @@ describe("orchestration v2 legacy v1 cutover", () => {
964
FROM orchestration_v2_legacy_imports
965
ORDER BY thread_id
966
`;
879
- const legacyMessageCount = yield* sql<{ readonly count: number }>`
880
- SELECT COUNT(*) AS count FROM projection_thread_messages
967
+ const legacyProjects = yield* sql<Record<string, unknown>>`
968
+ SELECT * FROM projection_projects ORDER BY project_id
969
+ `;
970
+ const legacyThreads = yield* sql<Record<string, unknown>>`
971
+ SELECT * FROM projection_threads ORDER BY thread_id
972
`;
882
- const legacyThreadCount = yield* sql<{ readonly count: number }>`
883
- SELECT COUNT(*) AS count FROM projection_threads
973
+ const legacyMessages = yield* sql<Record<string, unknown>>`
974
+ SELECT * FROM projection_thread_messages ORDER BY message_id
975
`;
976
const recordedMigration41 = yield* sql<{ readonly name: string }>`
977
SELECT name FROM effect_sql_migrations WHERE migration_id = 41
@@ -891,8 +982,9 @@ describe("orchestration v2 legacy v1 cutover", () => {
982
return {
983
migrationEventCount: migrationEventCount[0]?.count ?? 0,
984
importRows,
894
- legacyMessageCount: legacyMessageCount[0]?.count ?? 0,
895
- legacyThreadCount: legacyThreadCount[0]?.count ?? 0,
985
+ legacyProjects,
986
+ legacyThreads,
987
+ legacyMessages,
988
longProjection: continuedAgain,
989
migration41Name: recordedMigration41[0]?.name ?? null,
990
authSessionColumnNames: authSessionColumns.map((column) => column.name),
@@ -924,6 +1016,21 @@ describe("orchestration v2 legacy v1 cutover", () => {
1016
assert.notInclude(firstBoot.authSessionColumnNames, "client_surface");
1017
assert.notInclude(firstBoot.authSessionColumnNames, "client_app_version");
1018
1019
+ // Full row contents of the legacy recovery tables survive migration
1020
+ // and continuation unchanged, not just their counts.
1021
+ assert.deepStrictEqual(
1022
+ restrictToSnapshotColumns(firstBoot.legacyProjects, v1Snapshot.projects),
1023
+ v1Snapshot.projects.rows,
1024
+ );
1025
+ assert.deepStrictEqual(
1026
+ restrictToSnapshotColumns(firstBoot.legacyThreads, v1Snapshot.threads),
1027
+ v1Snapshot.threads.rows,
1028
+ );
1029
+ assert.deepStrictEqual(
1030
+ restrictToSnapshotColumns(firstBoot.legacyMessages, v1Snapshot.messages),
1031
+ v1Snapshot.messages.rows,
1032
+ );
1033
+
1034
assert.equal(firstBoot.importRows.length, ALL_THREADS.length);
1035
const unhydratedRows = firstBoot.importRows.filter(
1036
(row) => row.transcript_imported_at === null,
@@ -945,6 +1052,7 @@ describe("orchestration v2 legacy v1 cutover", () => {
1052
importedThreadCount: 0,
1053
importedMessageCount: 0,
1054
});
1055
+ yield* sql`DROP TRIGGER fail_snoozed_hydration`;
1056
assert.deepStrictEqual(
1057
yield* importer.ensureTranscript(ThreadId.make(SNOOZED_THREAD)),
1058
{ importedThreadCount: 1, importedMessageCount: 1 },
@@ -983,14 +1091,33 @@ describe("orchestration v2 legacy v1 cutover", () => {
1091
firstBoot.longProjection.runs.map((run) => run.status),
1092
);
1093
986
- const legacyMessageCount = yield* sql<{ readonly count: number }>`
987
- SELECT COUNT(*) AS count FROM projection_thread_messages
988
- `;
989
- const legacyThreadCount = yield* sql<{ readonly count: number }>`
990
- SELECT COUNT(*) AS count FROM projection_threads
991
- `;
992
- assert.equal(legacyMessageCount[0]?.count, firstBoot.legacyMessageCount);
993
- assert.equal(legacyThreadCount[0]?.count, firstBoot.legacyThreadCount);
1094
+ assert.deepStrictEqual(
1095
+ restrictToSnapshotColumns(
1096
+ yield* sql<Record<string, unknown>>`
1097
+ SELECT * FROM projection_projects ORDER BY project_id
1098
+ `,
1099
+ v1Snapshot.projects,
1100
+ ),
1101
+ v1Snapshot.projects.rows,
1102
+ );
1103
+ assert.deepStrictEqual(
1104
+ restrictToSnapshotColumns(
1105
+ yield* sql<Record<string, unknown>>`
1106
+ SELECT * FROM projection_threads ORDER BY thread_id
1107
+ `,
1108
+ v1Snapshot.threads,
1109
+ ),
1110
+ v1Snapshot.threads.rows,
1111
+ );
1112
+ assert.deepStrictEqual(
1113
+ restrictToSnapshotColumns(
1114
+ yield* sql<Record<string, unknown>>`
1115
+ SELECT * FROM projection_thread_messages ORDER BY message_id
1116
+ `,
1117
+ v1Snapshot.messages,
1118
+ ),
1119
+ v1Snapshot.messages.rows,
1120
+ );
1121
const quickCheck = yield* sql<{ readonly quick_check: string }>`
1122
PRAGMA quick_check
1123
`;
@@ -1017,6 +1144,10 @@ describe("orchestration v2 legacy v1 cutover", () => {
1144
assert.isTrue(
1145
boot2Logs.some((log) => String(log.message).includes("migration history diverges")),
1146
);
1147
+
1148
+ // The seeded source file is the untouched recovery copy: migration
1149
+ // only ever ran against the file copied out of it.
1150
+ assert.equal(yield* sha256File(fixturePath), fixtureHashBefore);
1151
}).pipe(Effect.provide(NodeServices.layer)),
1152
),
1153
);