|
1 | 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
2 | 2 | import { assert, describe, it } from "@effect/vitest"; |
3 | | -import { ProviderDriverKind, type ProviderReplayEntry } from "@t3tools/contracts"; |
| 3 | +import { EventId, ProviderDriverKind, type ProviderReplayEntry } from "@t3tools/contracts"; |
| 4 | +import * as DateTime from "effect/DateTime"; |
4 | 5 | import * as Effect from "effect/Effect"; |
5 | 6 | import * as FileSystem from "effect/FileSystem"; |
6 | 7 | import * as Layer from "effect/Layer"; |
7 | 8 | import * as Path from "effect/Path"; |
| 9 | +import * as SqlClient from "effect/sql/SqlClient"; |
8 | 10 |
|
9 | 11 | import { |
10 | 12 | ClaudeOrchestratorReplayHarness, |
11 | 13 | makeClaudeRestartReplayHarness, |
12 | 14 | } from "../Adapters/ClaudeAdapterV2.testkit.ts"; |
13 | 15 | import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; |
14 | 16 | import * as EffectWorker from "../EffectWorker.ts"; |
| 17 | +import * as EventSink from "../EventSink.ts"; |
| 18 | +import * as Orchestrator from "../Orchestrator.ts"; |
15 | 19 | import * as SqlitePersistence from "../../persistence/Sqlite.ts"; |
16 | 20 | import { provideDeterministicTestRuntime } from "./DeterministicRuntime.ts"; |
17 | 21 | import { CLAUDE_BACKGROUND_SUBAGENT_AFTER_ROOT_PROMPT } from "./fixtures/claude_background_subagent_after_root/input.ts"; |
@@ -25,6 +29,7 @@ import * as ProviderReplayHarness from "./ProviderReplayHarness.ts"; |
25 | 29 | import { runOrchestratorV2Scenario } from "./OrchestratorScenario.ts"; |
26 | 30 | import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace"; |
27 | 31 | import { readProviderReplayTranscript } from "@t3tools/provider-testing/replayTranscript"; |
| 32 | +import { makeProviderReplayGate } from "@t3tools/provider-testing/replayGate"; |
28 | 33 |
|
29 | 34 | const SCENARIO = "claude_background_subagent_after_root"; |
30 | 35 | const SESSION_ID = "cca274e4-25ae-4171-b972-bbb31118517e"; |
@@ -119,8 +124,9 @@ const readRestartTranscript = Effect.fn("readRestartTranscript")(function* ( |
119 | 124 | ...recorded, |
120 | 125 | entries: [ |
121 | 126 | ...recorded.entries.slice(0, rootResult + 1), |
122 | | - // The server dies here, with the background subagent still running. |
123 | | - { type: "runtime_exit", status: "success" }, |
| 127 | + // Leave the provider query open so the first runtime can be snapshotted |
| 128 | + // while the background subagent is still running. The runtime is then |
| 129 | + // torn down normally; only the durable snapshot is used for recovery. |
124 | 130 | { |
125 | 131 | type: "expect_outbound", |
126 | 132 | label: "query.open:resume", |
@@ -163,6 +169,7 @@ const runRestart = Effect.fn("runRestart")(function* (input: { |
163 | 169 | const layerDatabase = SqlitePersistence.layerFromPath(path.join(tempDir, "state.sqlite")).pipe( |
164 | 170 | Layer.provide(NodeServices.layer), |
165 | 171 | ); |
| 172 | + const snapshotPath = path.join(tempDir, "restart-state.sqlite"); |
166 | 173 | const scenario = (name: string, steps: typeof materialized.steps) => ({ |
167 | 174 | name: `${SCENARIO}:${name}`, |
168 | 175 | transcript, |
@@ -172,10 +179,58 @@ const runRestart = Effect.fn("runRestart")(function* (input: { |
172 | 179 | runtimePolicyOverride: { cwd: workspace }, |
173 | 180 | }); |
174 | 181 |
|
| 182 | + const beforeScenario = scenario("before-restart", phase1Steps); |
| 183 | + const beforeReplayGate = makeProviderReplayGate( |
| 184 | + phase1Steps.flatMap((step) => |
| 185 | + step.type === "release_replay_gate" || step.type === "release_replay_gate_after_waiting" |
| 186 | + ? [step.label] |
| 187 | + : [], |
| 188 | + ), |
| 189 | + ); |
| 190 | + const beforeLayer = ProviderReplayHarness.layerProviderReplay(beforeScenario, harness, { |
| 191 | + databaseLayer: layerDatabase, |
| 192 | + replayGate: beforeReplayGate, |
| 193 | + }); |
| 194 | + const snapshotBeforeProviderTeardown = Effect.gen(function* () { |
| 195 | + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; |
| 196 | + yield* worker.drain(); |
| 197 | + const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 198 | + const eventSink = yield* EventSink.EventSinkV2; |
| 199 | + const threadId = materialized.projectionThreadIds[0]; |
| 200 | + if (threadId === undefined) { |
| 201 | + return yield* Effect.die("restart scenario has no projection thread"); |
| 202 | + } |
| 203 | + const projection = yield* orchestrator.getThreadProjection(threadId); |
| 204 | + const session = projection.providerSessions[0]; |
| 205 | + if (session === undefined) { |
| 206 | + return yield* Effect.die("restart scenario has no provider session"); |
| 207 | + } |
| 208 | + // Older runtimes could persist this orphaned shape across a crash; current |
| 209 | + // cleanup settles the provider session when its CLI exits, so seed it here. |
| 210 | + const now = yield* DateTime.now; |
| 211 | + yield* eventSink.write({ |
| 212 | + events: [ |
| 213 | + { |
| 214 | + id: EventId.make("event:restart-background-note:stopped-provider-session"), |
| 215 | + type: "provider-session.updated", |
| 216 | + threadId, |
| 217 | + providerInstanceId: session.providerInstanceId, |
| 218 | + occurredAt: now, |
| 219 | + payload: { ...session, status: "stopped", updatedAt: now, lastError: null }, |
| 220 | + }, |
| 221 | + ], |
| 222 | + }); |
| 223 | + const sql = yield* SqlClient.SqlClient; |
| 224 | + yield* sql`VACUUM INTO ${snapshotPath}`; |
| 225 | + }).pipe(Effect.provide(Layer.merge(beforeLayer, layerDatabase)), Effect.orDie); |
| 226 | + const snapshotDatabase = SqlitePersistence.layerFromPath(snapshotPath).pipe( |
| 227 | + Layer.provide(NodeServices.layer), |
| 228 | + ); |
175 | 229 | const before = yield* Effect.scoped( |
176 | | - runOrchestratorV2ProviderReplayScenario(scenario("before-restart", phase1Steps), harness, { |
177 | | - databaseLayer: layerDatabase, |
178 | | - }), |
| 230 | + runOrchestratorV2Scenario(beforeScenario, { |
| 231 | + replayGate: beforeReplayGate, |
| 232 | + afterSteps: snapshotBeforeProviderTeardown, |
| 233 | + }).pipe(Effect.provide(beforeLayer)), |
179 | 234 | ); |
180 | 235 | const settled = projectionFor(before, SCENARIO); |
181 | 236 | assert.equal(settled.runs[0]?.status, "completed"); |
@@ -192,7 +247,9 @@ const runRestart = Effect.fn("runRestart")(function* (input: { |
192 | 247 | }).pipe( |
193 | 248 | Effect.provide( |
194 | 249 | ProviderReplayHarness.layerProviderReplay(restartScenario, harness, { |
195 | | - databaseLayer: layerDatabase, |
| 250 | + databaseLayer: SqlitePersistence.layerFromPath(snapshotPath).pipe( |
| 251 | + Layer.provide(NodeServices.layer), |
| 252 | + ), |
196 | 253 | recoverOnStartup: true, |
197 | 254 | continueThreadsAfterServerUpdate: input.continueThreadsAfterServerUpdate, |
198 | 255 | }), |
@@ -209,7 +266,7 @@ const runRestart = Effect.fn("runRestart")(function* (input: { |
209 | 266 |
|
210 | 267 | const after = yield* Effect.scoped( |
211 | 268 | runOrchestratorV2ProviderReplayScenario(scenario("after-restart", phase2Steps), harness, { |
212 | | - databaseLayer: layerDatabase, |
| 269 | + databaseLayer: snapshotDatabase, |
213 | 270 | continueThreadsAfterServerUpdate: input.continueThreadsAfterServerUpdate, |
214 | 271 | }), |
215 | 272 | ); |
|