Skip to content

Commit 1432264

Browse files
steipetePeter Steinberger
andauthored
fix(tasks): keep process listener singleton (#117766)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent 5327965 commit 1432264

3 files changed

Lines changed: 63 additions & 9 deletions

File tree

‎src/tasks/task-registry-state.ts‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ export const taskIdsByOwnerKey = taskRegistryProcessState.taskIdsByOwnerKey;
2929
export const taskIdsByParentFlowId = taskRegistryProcessState.taskIdsByParentFlowId;
3030
export const taskIdsByRelatedSessionKey = taskRegistryProcessState.taskIdsByRelatedSessionKey;
3131
export const tasksWithPendingDelivery = taskRegistryProcessState.tasksWithPendingDelivery;
32-
let listenerStarted = false;
33-
let listenerStop: (() => void) | null = null;
3432
type TaskRegistryRestoreState =
3533
| { status: "uninitialized" }
3634
| { status: "restoring" }
@@ -83,21 +81,20 @@ export function setTaskRegistryListenerStarter(starter: () => void): void {
8381
}
8482

8583
export function claimTaskRegistryListenerStart(): boolean {
86-
if (listenerStarted) {
84+
if (taskRegistryProcessState.listenerStop !== undefined) {
8785
return false;
8886
}
89-
listenerStarted = true;
87+
taskRegistryProcessState.listenerStop = null;
9088
return true;
9189
}
9290

9391
export function setTaskRegistryListenerStop(stop: (() => void) | null): void {
94-
listenerStop = stop;
92+
taskRegistryProcessState.listenerStop = stop;
9593
}
9694

9795
export function resetTaskRegistryListenerState(): void {
98-
listenerStop?.();
99-
listenerStop = null;
100-
listenerStarted = false;
96+
taskRegistryProcessState.listenerStop?.();
97+
taskRegistryProcessState.listenerStop = undefined;
10198
}
10299

103100
function clearTaskFlowSyncRetries(): void {

‎src/tasks/task-registry.process-state.test.ts‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Verifies process-state persistence across fresh task registry module loads.
22
import { importFreshModule } from "openclaw/plugin-sdk/test-fixtures";
3-
import { describe, expect, it } from "vitest";
3+
import { describe, expect, it, vi } from "vitest";
44

55
describe("task registry process state", () => {
66
it("shares state across duplicate module instances", async () => {
@@ -39,4 +39,59 @@ describe("task registry process state", () => {
3939
);
4040
firstState.tasks.clear();
4141
});
42+
43+
it("does not duplicate task event listeners when modules are reset", async () => {
44+
const events = await import("../infra/agent-events.js");
45+
const firstStore = await import("./task-registry.store.js");
46+
const store = {
47+
loadSnapshot: () => ({ tasks: new Map(), deliveryStates: new Map() }),
48+
saveSnapshot: () => {},
49+
};
50+
firstStore.configureTaskRegistryRuntime({ store });
51+
const firstRegistry = await import("./task-registry.js");
52+
const firstState = await import("./task-registry-state.js");
53+
firstState.resetTaskRegistryListenerState();
54+
events.resetAgentEventsForTest();
55+
firstRegistry.ensureTaskRegistryReady();
56+
57+
vi.resetModules();
58+
59+
const secondStore = await import("./task-registry.store.js");
60+
secondStore.configureTaskRegistryRuntime({ store });
61+
const secondRegistry = await import("./task-registry.js");
62+
const secondState = await import("./task-registry-state.js");
63+
64+
try {
65+
secondRegistry.ensureTaskRegistryReady();
66+
const task = secondRegistry.createTaskRecord({
67+
runtime: "subagent",
68+
requesterSessionKey: "agent:main:main",
69+
ownerKey: "agent:main:main",
70+
scopeKind: "session",
71+
childSessionKey: "agent:main:subagent:listener-reload",
72+
runId: "run-task-listener-reload",
73+
task: "Count tool events once",
74+
status: "running",
75+
deliveryStatus: "not_applicable",
76+
});
77+
expect(task).not.toBeNull();
78+
79+
for (const name of ["read", "exec"]) {
80+
events.emitAgentEvent({
81+
runId: "run-task-listener-reload",
82+
stream: "tool",
83+
data: { phase: "start", name },
84+
});
85+
}
86+
87+
expect(secondRegistry.getTaskById(task!.taskId)?.toolUseCount).toBe(2);
88+
expect(secondRegistry.getTaskById(task!.taskId)?.lastToolName).toBe("exec");
89+
} finally {
90+
firstState.resetTaskRegistryListenerState();
91+
secondState.resetTaskRegistryListenerState();
92+
events.resetAgentEventsForTest();
93+
firstStore.resetTaskRegistryRuntimeForTests();
94+
secondStore.resetTaskRegistryRuntimeForTests();
95+
}
96+
});
4297
});

‎src/tasks/task-registry.process-state.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ type TaskRegistryProcessState = {
1010
taskIdsByParentFlowId: Map<string, Set<string>>;
1111
taskIdsByRelatedSessionKey: Map<string, Set<string>>;
1212
tasksWithPendingDelivery: Set<string>;
13+
// Listener ownership must survive module reloads alongside the task indexes it updates.
14+
listenerStop?: (() => void) | null;
1315
};
1416

1517
const TASK_REGISTRY_PROCESS_STATE_KEY = Symbol.for("openclaw.taskRegistry.state");

0 commit comments

Comments
 (0)