|
1 | 1 | // Verifies process-state persistence across fresh task registry module loads. |
2 | 2 | import { importFreshModule } from "openclaw/plugin-sdk/test-fixtures"; |
3 | | -import { describe, expect, it } from "vitest"; |
| 3 | +import { describe, expect, it, vi } from "vitest"; |
4 | 4 |
|
5 | 5 | describe("task registry process state", () => { |
6 | 6 | it("shares state across duplicate module instances", async () => { |
@@ -39,4 +39,59 @@ describe("task registry process state", () => { |
39 | 39 | ); |
40 | 40 | firstState.tasks.clear(); |
41 | 41 | }); |
| 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 | + }); |
42 | 97 | }); |
0 commit comments