@@ -23,11 +23,9 @@ import type { MatrixAuth, MatrixStoragePaths } from "./client/types.js";
2323import {
2424 setMatrixThreadBindingIdleTimeoutBySessionKey ,
2525 setMatrixThreadBindingMaxAgeBySessionKey ,
26+ type MatrixThreadBindingManager ,
2627} from "./thread-bindings-shared.js" ;
27- import {
28- createMatrixThreadBindingManager ,
29- resetMatrixThreadBindingsForTests ,
30- } from "./thread-bindings.js" ;
28+ import { createMatrixThreadBindingManager } from "./thread-bindings.js" ;
3129
3230const sendMessageMatrixMock = vi . hoisted ( ( ) =>
3331 vi . fn ( async ( _to : string , _message : string , opts ?: { threadId ?: string } ) => ( {
@@ -52,10 +50,14 @@ describe("matrix thread bindings", () => {
5250 const accountId = "ops" ;
5351 const idleTimeoutMs = 24 * 60 * 60 * 1000 ;
5452 const matrixClient = { } as never ;
53+ const trackedManagers = new Set < MatrixThreadBindingManager > ( ) ;
5554
5655 function resetThreadBindingAdapters ( ) {
56+ for ( const manager of trackedManagers ) {
57+ manager . stop ( ) ;
58+ }
59+ trackedManagers . clear ( ) ;
5760 testing . resetSessionBindingAdaptersForTests ( ) ;
58- resetMatrixThreadBindingsForTests ( ) ;
5961 }
6062
6163 function currentThreadConversation ( params ?: {
@@ -70,7 +72,7 @@ describe("matrix thread bindings", () => {
7072 } ;
7173 }
7274
73- function createBindingManager (
75+ async function createBindingManager (
7476 params : {
7577 auth ?: MatrixAuth ;
7678 cfg ?: OpenClawConfig ;
@@ -81,7 +83,7 @@ describe("matrix thread bindings", () => {
8183 logVerboseMessage ?: ( message : string ) => void ;
8284 } = { } ,
8385 ) {
84- return createMatrixThreadBindingManager ( {
86+ const manager = await createMatrixThreadBindingManager ( {
8587 cfg : params . cfg ?? { } ,
8688 accountId,
8789 auth : params . auth ?? auth ,
@@ -92,6 +94,8 @@ describe("matrix thread bindings", () => {
9294 enableSweeper : params . enableSweeper ?? false ,
9395 ...( params . logVerboseMessage ? { logVerboseMessage : params . logVerboseMessage } : { } ) ,
9496 } ) ;
97+ trackedManagers . add ( manager ) ;
98+ return manager ;
9599 }
96100
97101 async function createStaticThreadBindingManager ( ) {
@@ -217,15 +221,7 @@ describe("matrix thread bindings", () => {
217221 } ) ;
218222
219223 it ( "creates child Matrix thread bindings from a top-level room context" , async ( ) => {
220- await createMatrixThreadBindingManager ( {
221- cfg : { } ,
222- accountId,
223- auth,
224- client : matrixClient ,
225- idleTimeoutMs,
226- maxAgeMs : 0 ,
227- enableSweeper : false ,
228- } ) ;
224+ await createBindingManager ( ) ;
229225
230226 const binding = await getSessionBindingService ( ) . bind ( {
231227 targetSessionKey : "agent:ops:subagent:child" ,
@@ -286,13 +282,10 @@ describe("matrix thread bindings", () => {
286282 vi . useFakeTimers ( ) ;
287283 vi . setSystemTime ( new Date ( "2026-03-08T12:00:00.000Z" ) ) ;
288284 try {
289- await createMatrixThreadBindingManager ( {
290- cfg : { } ,
291- accountId : "ops" ,
292- auth,
293- client : { } as never ,
285+ await createBindingManager ( {
294286 idleTimeoutMs : 1_000 ,
295287 maxAgeMs : 0 ,
288+ enableSweeper : true ,
296289 } ) ;
297290
298291 await getSessionBindingService ( ) . bind ( {
@@ -331,13 +324,10 @@ describe("matrix thread bindings", () => {
331324 vi . useFakeTimers ( ) ;
332325 vi . setSystemTime ( new Date ( "2026-03-08T12:00:00.000Z" ) ) ;
333326 try {
334- await createMatrixThreadBindingManager ( {
335- cfg : { } ,
336- accountId : "ops" ,
337- auth,
338- client : { } as never ,
327+ await createBindingManager ( {
339328 idleTimeoutMs : 1_000 ,
340329 maxAgeMs : 0 ,
330+ enableSweeper : true ,
341331 } ) ;
342332
343333 await getSessionBindingService ( ) . bind ( {
@@ -389,14 +379,9 @@ describe("matrix thread bindings", () => {
389379 } ) ;
390380
391381 it ( "sends threaded farewell messages when bindings are unbound" , async ( ) => {
392- await createMatrixThreadBindingManager ( {
393- cfg : { } ,
394- accountId : "ops" ,
395- auth,
396- client : { } as never ,
382+ await createBindingManager ( {
397383 idleTimeoutMs : 1_000 ,
398384 maxAgeMs : 0 ,
399- enableSweeper : false ,
400385 } ) ;
401386
402387 const binding = await getSessionBindingService ( ) . bind ( {
@@ -563,20 +548,24 @@ describe("matrix thread bindings", () => {
563548 conversationId : "$thread" ,
564549 targetSessionKey : "agent:ops:subagent:child" ,
565550 } ) ;
551+
552+ initialManager . stop ( ) ;
553+
554+ expect (
555+ replacementManager . getByConversation ( {
556+ conversationId : "$thread-2" ,
557+ parentConversationId : "!room:example" ,
558+ } ) ?. targetSessionKey ,
559+ ) . toBe ( "agent:ops:subagent:replacement" ) ;
566560 } ) ;
567561
568562 it ( "updates lifecycle windows by session key and refreshes activity" , async ( ) => {
569563 vi . useFakeTimers ( ) ;
570564 vi . setSystemTime ( new Date ( "2026-03-06T10:00:00.000Z" ) ) ;
571565 try {
572- const manager = await createMatrixThreadBindingManager ( {
573- cfg : { } ,
574- accountId : "ops" ,
575- auth,
576- client : { } as never ,
566+ const manager = await createBindingManager ( {
577567 idleTimeoutMs : 24 * 60 * 60 * 1000 ,
578568 maxAgeMs : 0 ,
579- enableSweeper : false ,
580569 } ) ;
581570
582571 await getSessionBindingService ( ) . bind ( {
0 commit comments