Skip to content

Commit 1db88fe

Browse files
authored
refactor(channels): own thread binding cleanup in managers (#124221)
* refactor(channels): own thread binding cleanup in managers * test(channels): prove thread binding lifecycle cleanup
1 parent 8f59226 commit 1db88fe

8 files changed

Lines changed: 178 additions & 118 deletions

File tree

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
// Matrix API module exposes the plugin public contract.
2-
export {
3-
createMatrixThreadBindingManager,
4-
resetMatrixThreadBindingsForTests,
5-
} from "./src/matrix/thread-bindings.js";
2+
export { createMatrixThreadBindingManager } from "./src/matrix/thread-bindings.js";
63
export { setMatrixRuntime } from "./src/runtime.js";

‎extensions/matrix/src/matrix/thread-bindings-shared.ts‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,3 @@ export function setMatrixThreadBindingMaxAgeBySessionKey(params: {
197197
}),
198198
);
199199
}
200-
201-
export function resetMatrixThreadBindingsForTests(): void {
202-
for (const { manager } of MANAGERS_BY_ACCOUNT_ID.values()) {
203-
manager.stop();
204-
}
205-
MANAGERS_BY_ACCOUNT_ID.clear();
206-
BINDINGS_BY_ACCOUNT_CONVERSATION.clear();
207-
}

‎extensions/matrix/src/matrix/thread-bindings.test.ts‎

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ import type { MatrixAuth, MatrixStoragePaths } from "./client/types.js";
2323
import {
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

3230
const 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({

‎extensions/matrix/src/matrix/thread-bindings.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
getMatrixThreadBindingManagerEntry,
2626
listBindingsForAccount,
2727
removeBindingRecord,
28-
resetMatrixThreadBindingsForTests,
2928
resolveBindingKey,
3029
resolveEffectiveBindingExpiry,
3130
setBindingRecord,
@@ -499,9 +498,10 @@ export async function createMatrixThreadBindingManager(params: {
499498
});
500499
if (getMatrixThreadBindingManagerEntry(params.accountId)?.manager === manager) {
501500
deleteMatrixThreadBindingManagerEntry(params.accountId);
502-
}
503-
for (const record of listBindingsForAccount(params.accountId)) {
504-
removeBindingRecord(record);
501+
// Live bindings belong to this manager generation; persisted rows reload on restart.
502+
for (const record of listBindingsForAccount(params.accountId)) {
503+
removeBindingRecord(record);
504+
}
505505
}
506506
},
507507
};
@@ -708,4 +708,4 @@ export async function createMatrixThreadBindingManager(params: {
708708
});
709709
return manager;
710710
}
711-
export { getMatrixThreadBindingManager, resetMatrixThreadBindingsForTests };
711+
export { getMatrixThreadBindingManager };

‎extensions/telegram/session-binding-contract-api.ts‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)