Skip to content

Commit 5c86d1c

Browse files
test(server): snapshot orphaned Claude subagent before recovery
1 parent 6951489 commit 5c86d1c

1 file changed

Lines changed: 47 additions & 16 deletions

File tree

‎apps/server/src/orchestration-v2/testkit/OrchestratorReplayRecovery.integration.test.ts‎

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as FileSystem from "effect/FileSystem";
55
import * as Layer from "effect/Layer";
66
import * as Path from "effect/Path";
77
import * as Schema from "effect/Schema";
8+
import * as SqlClient from "effect/sql/SqlClient";
89
import * as CodexReplay from "effect-codex-app-server/replay";
910
import {
1011
type OrchestrationV2ThreadProjection,
@@ -25,6 +26,7 @@ import {
2526
} from "../Adapters/CursorAdapterV2.testkit.ts";
2627
import * as CursorAdapterV2Testkit from "../Adapters/CursorAdapterV2.testkit.ts";
2728
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
29+
import * as EffectWorker from "../EffectWorker.ts";
2830
import * as SqlitePersistence from "../../persistence/Sqlite.ts";
2931
import { provideDeterministicTestRuntime } from "./DeterministicRuntime.ts";
3032
import {
@@ -46,8 +48,14 @@ import {
4648
assertUserMessagesInclude,
4749
projectionFor,
4850
} from "./fixtures/shared.ts";
49-
import type { OrchestratorV2ScenarioResult } from "./OrchestratorScenario.ts";
50-
import { runOrchestratorV2ProviderReplayScenario } from "./ProviderReplayHarness.ts";
51+
import {
52+
runOrchestratorV2Scenario,
53+
type OrchestratorV2ScenarioResult,
54+
} from "./OrchestratorScenario.ts";
55+
import {
56+
layerProviderReplay,
57+
runOrchestratorV2ProviderReplayScenario,
58+
} from "./ProviderReplayHarness.ts";
5159
import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace";
5260
import { materializeReplayTranscriptRuntimeInstructions } from "./ReplayRuntimeInstructions.ts";
5361
import {
@@ -111,11 +119,15 @@ const readClaudeCancelledSubagentResumeTranscript = Effect.fn(
111119
const firstOutcome = indices.find(
112120
(index) => frameField(index, "subtype") === "task_notification",
113121
);
114-
if (firstSubagentFrame === undefined || firstOutcome === undefined) {
122+
const firstExit = recorded.entries.findIndex((entry) => entry.type === "runtime_exit");
123+
if (firstSubagentFrame === undefined || firstOutcome === undefined || firstExit < 0) {
115124
throw new Error("Expected the recorded subagent's first run.");
116125
}
117126
const entries = recorded.entries
118-
.filter((_, index) => index < firstSubagentFrame || index > firstOutcome)
127+
// A CLI exit settles the task; hold at query.open:2 until the crash snapshot instead.
128+
.filter(
129+
(_, index) => (index < firstSubagentFrame || index > firstOutcome) && index !== firstExit,
130+
)
119131
.map((entry) => {
120132
if (entry.type !== "expect_outbound" || entry.label !== "prompt.offer:2") return entry;
121133
const frame = entry.frame as {
@@ -524,18 +536,31 @@ describe("orchestrator replay recovery", () => {
524536
const childRootStatus = (child: OrchestrationV2ThreadProjection | undefined) =>
525537
child?.nodes.find((node) => node.kind === "root_turn" && node.runId === null)?.status;
526538

539+
const snapshotPath = path.join(tempDir, "restart-state.sqlite");
540+
const beforeScenario = {
541+
name: `${transcript.scenario}:cancelled-first-runtime`,
542+
transcript,
543+
commands: phase1Commands,
544+
steps: phase1Steps,
545+
projectionThreadIds: materialized.projectionThreadIds,
546+
runtimePolicyOverride: { cwd: workspace },
547+
};
527548
const before = yield* Effect.scoped(
528-
runOrchestratorV2ProviderReplayScenario(
529-
{
530-
name: `${transcript.scenario}:cancelled-first-runtime`,
531-
transcript,
532-
commands: phase1Commands,
533-
steps: phase1Steps,
534-
projectionThreadIds: materialized.projectionThreadIds,
535-
runtimePolicyOverride: { cwd: workspace },
536-
},
537-
harness,
538-
options,
549+
Effect.gen(function* () {
550+
const result = yield* runOrchestratorV2Scenario(beforeScenario);
551+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
552+
yield* worker.drain();
553+
// Preserve the orphan before scoped teardown settles its provider work.
554+
const sql = yield* SqlClient.SqlClient;
555+
yield* sql`VACUUM INTO ${snapshotPath}`;
556+
return result;
557+
}).pipe(
558+
Effect.provide(
559+
Layer.merge(
560+
layerProviderReplay(beforeScenario, harness, options),
561+
options.databaseLayer,
562+
),
563+
),
539564
),
540565
);
541566
const settled = projectionFor(before, transcript.scenario);
@@ -555,7 +580,13 @@ describe("orchestrator replay recovery", () => {
555580
runtimePolicyOverride: { cwd: workspace },
556581
},
557582
harness,
558-
{ ...options, recoverOnStartup: true, continueThreadsAfterServerUpdate: false },
583+
{
584+
databaseLayer: SqlitePersistence.layerFromPath(snapshotPath).pipe(
585+
Layer.provide(NodeServices.layer),
586+
),
587+
recoverOnStartup: true,
588+
continueThreadsAfterServerUpdate: false,
589+
},
559590
),
560591
);
561592
yield* assertComplete;

0 commit comments

Comments
 (0)