55 TurnId ,
66 ProviderDriverKind ,
77 ProviderInstanceId ,
8+ type ProviderSession ,
89} from "@t3tools/contracts" ;
910import * as Clock from "effect/Clock" ;
1011import * as DateTime from "effect/DateTime" ;
@@ -147,6 +148,7 @@ describe("ProviderSessionReaper", () => {
147148
148149 async function createHarness ( input : {
149150 readonly readModel : ReturnType < typeof makeReadModel > ;
151+ readonly activeSessions ?: ReadonlyArray < ProviderSession > ;
150152 readonly stopSessionImplementation ?: ( input : {
151153 readonly threadId : ThreadId ;
152154 } ) => ReturnType < ProviderServiceShape [ "stopSession" ] > ;
@@ -169,7 +171,7 @@ describe("ProviderSessionReaper", () => {
169171 respondToRequest : ( ) => unsupported ( ) ,
170172 respondToUserInput : ( ) => unsupported ( ) ,
171173 stopSession,
172- listSessions : ( ) => Effect . succeed ( [ ] ) ,
174+ listSessions : ( ) => Effect . succeed ( input . activeSessions ?? [ ] ) ,
173175 getCapabilities : ( ) => Effect . succeed ( { sessionModelSwitch : "in-session" } ) ,
174176 assertConversationRollbackSupported : ( ) => unsupported ( ) ,
175177 getInstanceInfo : ( instanceId ) => {
@@ -247,7 +249,7 @@ describe("ProviderSessionReaper", () => {
247249 session : {
248250 threadId,
249251 status : "ready" ,
250- providerName : "claudeAgent " ,
252+ providerName : "codex " ,
251253 runtimeMode : "full-access" ,
252254 activeTurnId : null ,
253255 lastError : null ,
@@ -263,9 +265,9 @@ describe("ProviderSessionReaper", () => {
263265 await runtime ! . runPromise (
264266 repository . upsert ( {
265267 threadId,
266- providerName : "claudeAgent " ,
268+ providerName : "codex " ,
267269 providerInstanceId : null ,
268- adapterKey : "claudeAgent " ,
270+ adapterKey : "codex " ,
269271 runtimeMode : "full-access" ,
270272 status : "running" ,
271273 lastSeenAt : "2026-04-14T00:00:00.000Z" ,
@@ -284,6 +286,66 @@ describe("ProviderSessionReaper", () => {
284286 expect ( harness . stoppedThreadIds . has ( threadId ) ) . toBe ( true ) ;
285287 } ) ;
286288
289+ it . each ( [ "ready" , "error" ] as const ) (
290+ "keeps stale %s Claude sessions alive for cross-session messaging" ,
291+ async ( status ) => {
292+ const threadId = ThreadId . make ( `thread-reaper-claude-messaging-${ status } ` ) ;
293+ const now = "2026-01-01T00:00:00.000Z" ;
294+ const harness = await createHarness ( {
295+ activeSessions : [
296+ {
297+ threadId,
298+ provider : ProviderDriverKind . make ( "claudeAgent" ) ,
299+ status : "ready" ,
300+ runtimeMode : "full-access" ,
301+ createdAt : now ,
302+ updatedAt : now ,
303+ } ,
304+ ] ,
305+ readModel : makeReadModel ( [
306+ {
307+ id : threadId ,
308+ session : {
309+ threadId,
310+ status,
311+ providerName : "claudeAgent" ,
312+ runtimeMode : "full-access" ,
313+ activeTurnId : null ,
314+ lastError : null ,
315+ updatedAt : now ,
316+ } ,
317+ } ,
318+ ] ) ,
319+ } ) ;
320+ const repository = await runtime ! . runPromise (
321+ Effect . service ( ProviderSessionRuntime . ProviderSessionRuntimeRepository ) ,
322+ ) ;
323+
324+ await runtime ! . runPromise (
325+ repository . upsert ( {
326+ threadId,
327+ providerName : "claudeAgent" ,
328+ providerInstanceId : null ,
329+ adapterKey : "claudeAgent" ,
330+ runtimeMode : "full-access" ,
331+ status : "running" ,
332+ lastSeenAt : "2026-04-14T00:00:00.000Z" ,
333+ resumeCursor : {
334+ opaque : "resume-claude-messaging" ,
335+ } ,
336+ runtimePayload : null ,
337+ } ) ,
338+ ) ;
339+
340+ await startReaper ( ) ;
341+ await runtime ! . runPromise ( drainFibers ) ;
342+
343+ expect ( harness . stopSession ) . not . toHaveBeenCalled ( ) ;
344+ const remaining = await runtime ! . runPromise ( repository . getByThreadId ( { threadId } ) ) ;
345+ expect ( Option . isSome ( remaining ) ) . toBe ( true ) ;
346+ } ,
347+ ) ;
348+
287349 it ( "skips stale sessions when the thread still has an active turn" , async ( ) => {
288350 const threadId = ThreadId . make ( "thread-reaper-active-turn" ) ;
289351 const turnId = TurnId . make ( "turn-reaper-active" ) ;
@@ -484,7 +546,7 @@ describe("ProviderSessionReaper", () => {
484546 id : failedThreadId ,
485547 session : {
486548 threadId : failedThreadId ,
487- status : "ready " ,
549+ status : "error " ,
488550 providerName : "claudeAgent" ,
489551 runtimeMode : "full-access" ,
490552 activeTurnId : null ,
@@ -570,7 +632,7 @@ describe("ProviderSessionReaper", () => {
570632 id : defectThreadId ,
571633 session : {
572634 threadId : defectThreadId ,
573- status : "ready " ,
635+ status : "error " ,
574636 providerName : "claudeAgent" ,
575637 runtimeMode : "full-access" ,
576638 activeTurnId : null ,
0 commit comments