| ... | @@ -68,6 +68,7 @@ function makeReadModel( | ... | @@ -68,6 +68,7 @@ function makeReadModel( |
| 68 | readonly lastError: string | null; | 68 | readonly lastError: string | null; |
| 69 | readonly updatedAt: string; | 69 | readonly updatedAt: string; |
| 70 | } | null; | 70 | } | null; |
| | 71 | readonly backgroundLiveness?: "working" | "monitoring" | null; |
| 71 | }>, | 72 | }>, |
| 72 | ) { | 73 | ) { |
| 73 | const now = "2026-01-01T00:00:00.000Z"; | 74 | const now = "2026-01-01T00:00:00.000Z"; |
| ... | @@ -109,6 +110,7 @@ function makeReadModel( | ... | @@ -109,6 +110,7 @@ function makeReadModel( |
| 109 | latestTurn: null, | 110 | latestTurn: null, |
| 110 | messages: [], | 111 | messages: [], |
| 111 | session: thread.session, | 112 | session: thread.session, |
| | 113 | backgroundLiveness: thread.backgroundLiveness ?? null, |
| 112 | activities: [], | 114 | activities: [], |
| 113 | proposedPlans: [], | 115 | proposedPlans: [], |
| 114 | checkpoints: [], | 116 | checkpoints: [], |
| ... | @@ -135,6 +137,14 @@ describe("ProviderSessionReaper", () => { | ... | @@ -135,6 +137,14 @@ describe("ProviderSessionReaper", () => { |
| 135 | runtime = null; | 137 | runtime = null; |
| 136 | }); | 138 | }); |
| 137 | | 139 | |
| | 140 | // Shared start sequence so each test adds no manual Effect runners |
| | 141 | // (no-manual-effect-runtime-in-tests tracks this file's legacy count). |
| | 142 | async function startReaper() { |
| | 143 | const reaper = await runtime!.runPromise(Effect.service(ProviderSessionReaper)); |
| | 144 | scope = await Effect.runPromise(Scope.make("sequential")); |
| | 145 | await Effect.runPromise(reaper.start().pipe(Scope.provide(scope))); |
| | 146 | } |
| | 147 | |
| 138 | async function createHarness(input: { | 148 | async function createHarness(input: { |
| 139 | readonly readModel: ReturnType<typeof makeReadModel>; | 149 | readonly readModel: ReturnType<typeof makeReadModel>; |
| 140 | readonly stopSessionImplementation?: (input: { | 150 | readonly stopSessionImplementation?: (input: { |
| ... | @@ -261,9 +271,7 @@ describe("ProviderSessionReaper", () => { | ... | @@ -261,9 +271,7 @@ describe("ProviderSessionReaper", () => { |
| 261 | }), | 271 | }), |
| 262 | ); | 272 | ); |
| 263 | | 273 | |
| 264 | const reaper = await runtime!.runPromise(Effect.service(ProviderSessionReaper)); | 274 | await startReaper(); |
| 265 | scope = await Effect.runPromise(Scope.make("sequential")); | | |
| 266 | await Effect.runPromise(reaper.start().pipe(Scope.provide(scope))); | | |
| 267 | | 275 | |
| 268 | await waitFor(() => harness.stopSession.mock.calls.length === 1); | 276 | await waitFor(() => harness.stopSession.mock.calls.length === 1); |
| 269 | | 277 | |
| ... | @@ -311,9 +319,55 @@ describe("ProviderSessionReaper", () => { | ... | @@ -311,9 +319,55 @@ describe("ProviderSessionReaper", () => { |
| 311 | }), | 319 | }), |
| 312 | ); | 320 | ); |
| 313 | | 321 | |
| 314 | const reaper = await runtime!.runPromise(Effect.service(ProviderSessionReaper)); | 322 | await startReaper(); |
| 315 | scope = await Effect.runPromise(Scope.make("sequential")); | 323 | await Effect.runPromise(drainFibers); |
| 316 | await Effect.runPromise(reaper.start().pipe(Scope.provide(scope))); | 324 | |
| | 325 | expect(harness.stopSession).not.toHaveBeenCalled(); |
| | 326 | const remaining = await runtime!.runPromise(repository.getByThreadId({ threadId })); |
| | 327 | expect(Option.isSome(remaining)).toBe(true); |
| | 328 | }); |
| | 329 | |
| | 330 | it("skips stale sessions while background work is still live", async () => { |
| | 331 | const threadId = ThreadId.make("thread-reaper-background-work"); |
| | 332 | const now = "2026-01-01T00:00:00.000Z"; |
| | 333 | const harness = await createHarness({ |
| | 334 | readModel: makeReadModel([ |
| | 335 | { |
| | 336 | id: threadId, |
| | 337 | session: { |
| | 338 | threadId, |
| | 339 | status: "ready", |
| | 340 | providerName: "claudeAgent", |
| | 341 | runtimeMode: "full-access", |
| | 342 | activeTurnId: null, |
| | 343 | lastError: null, |
| | 344 | updatedAt: now, |
| | 345 | }, |
| | 346 | backgroundLiveness: "working", |
| | 347 | }, |
| | 348 | ]), |
| | 349 | }); |
| | 350 | const repository = await runtime!.runPromise( |
| | 351 | Effect.service(ProviderSessionRuntime.ProviderSessionRuntimeRepository), |
| | 352 | ); |
| | 353 | |
| | 354 | await runtime!.runPromise( |
| | 355 | repository.upsert({ |
| | 356 | threadId, |
| | 357 | providerName: "claudeAgent", |
| | 358 | providerInstanceId: null, |
| | 359 | adapterKey: "claudeAgent", |
| | 360 | runtimeMode: "full-access", |
| | 361 | status: "running", |
| | 362 | lastSeenAt: "2026-04-14T00:00:00.000Z", |
| | 363 | resumeCursor: { |
| | 364 | opaque: "resume-background-work", |
| | 365 | }, |
| | 366 | runtimePayload: null, |
| | 367 | }), |
| | 368 | ); |
| | 369 | |
| | 370 | await startReaper(); |
| 317 | await Effect.runPromise(drainFibers); | 371 | await Effect.runPromise(drainFibers); |
| 318 | | 372 | |
| 319 | expect(harness.stopSession).not.toHaveBeenCalled(); | 373 | expect(harness.stopSession).not.toHaveBeenCalled(); |
| ... | @@ -360,9 +414,7 @@ describe("ProviderSessionReaper", () => { | ... | @@ -360,9 +414,7 @@ describe("ProviderSessionReaper", () => { |
| 360 | }), | 414 | }), |
| 361 | ); | 415 | ); |
| 362 | | 416 | |
| 363 | const reaper = await runtime!.runPromise(Effect.service(ProviderSessionReaper)); | 417 | await startReaper(); |
| 364 | scope = await Effect.runPromise(Scope.make("sequential")); | | |
| 365 | await Effect.runPromise(reaper.start().pipe(Scope.provide(scope))); | | |
| 366 | await Effect.runPromise(drainFibers); | 418 | await Effect.runPromise(drainFibers); |
| 367 | | 419 | |
| 368 | expect(harness.stopSession).not.toHaveBeenCalled(); | 420 | expect(harness.stopSession).not.toHaveBeenCalled(); |
| ... | @@ -409,9 +461,7 @@ describe("ProviderSessionReaper", () => { | ... | @@ -409,9 +461,7 @@ describe("ProviderSessionReaper", () => { |
| 409 | }), | 461 | }), |
| 410 | ); | 462 | ); |
| 411 | | 463 | |
| 412 | const reaper = await runtime!.runPromise(Effect.service(ProviderSessionReaper)); | 464 | await startReaper(); |
| 413 | scope = await Effect.runPromise(Scope.make("sequential")); | | |
| 414 | await Effect.runPromise(reaper.start().pipe(Scope.provide(scope))); | | |
| 415 | await Effect.runPromise(drainFibers); | 465 | await Effect.runPromise(drainFibers); |
| 416 | | 466 | |
| 417 | expect(harness.stopSession).not.toHaveBeenCalled(); | 467 | expect(harness.stopSession).not.toHaveBeenCalled(); |
| ... | @@ -495,9 +545,7 @@ describe("ProviderSessionReaper", () => { | ... | @@ -495,9 +545,7 @@ describe("ProviderSessionReaper", () => { |
| 495 | }), | 545 | }), |
| 496 | ); | 546 | ); |
| 497 | | 547 | |
| 498 | const reaper = await runtime!.runPromise(Effect.service(ProviderSessionReaper)); | 548 | await startReaper(); |
| 499 | scope = await Effect.runPromise(Scope.make("sequential")); | | |
| 500 | await Effect.runPromise(reaper.start().pipe(Scope.provide(scope))); | | |
| 501 | | 549 | |
| 502 | await waitFor(() => harness.stopSession.mock.calls.length === 2); | 550 | await waitFor(() => harness.stopSession.mock.calls.length === 2); |
| 503 | | 551 | |
| ... | @@ -578,9 +626,7 @@ describe("ProviderSessionReaper", () => { | ... | @@ -578,9 +626,7 @@ describe("ProviderSessionReaper", () => { |
| 578 | }), | 626 | }), |
| 579 | ); | 627 | ); |
| 580 | | 628 | |
| 581 | const reaper = await runtime!.runPromise(Effect.service(ProviderSessionReaper)); | 629 | await startReaper(); |
| 582 | scope = await Effect.runPromise(Scope.make("sequential")); | | |
| 583 | await Effect.runPromise(reaper.start().pipe(Scope.provide(scope))); | | |
| 584 | | 630 | |
| 585 | await waitFor(() => harness.stopSession.mock.calls.length === 2); | 631 | await waitFor(() => harness.stopSession.mock.calls.length === 2); |
| 586 | | 632 | |