Skip to content

Commit e6a99f1

Browse files
author
Peter Steinberger
committed
test(gateway): use in-memory task registry
1 parent f4766de commit e6a99f1

3 files changed

Lines changed: 42 additions & 25 deletions

File tree

‎src/gateway/server-methods/agent.sessions-and-models.test-utils.ts‎

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ import {
1414
listTaskRecords,
1515
markTaskTerminalById,
1616
} from "../../tasks/task-registry.js";
17-
import {
18-
resetTaskRegistryForTests,
19-
setDetachedTaskLifecycleRuntime,
20-
} from "../../tasks/task-runtime.test-helpers.js";
17+
import { setDetachedTaskLifecycleRuntime } from "../../tasks/task-runtime.test-helpers.js";
2118
import { withTempDir } from "../../test-helpers/temp-dir.js";
2219
import { dispatchAgentRunFromGateway } from "./agent-run-dispatch.js";
2320
import { registerPluginSubagentRunFromGateway } from "./agent-task-tracking.js";
@@ -38,6 +35,7 @@ import {
3835
primeMainAgentRun,
3936
backendGatewayClient,
4037
operatorWriteGatewayClient,
38+
resetAgentTaskRegistryForTests,
4139
waitForAgentCommandCall,
4240
invokeAgent,
4341
describe0AfterEach0,
@@ -157,7 +155,7 @@ describe("gateway agent handler", () => {
157155
it("terminalizes successful async gateway agent runs in the shared task registry", async () => {
158156
await withTempDir({ prefix: "openclaw-gateway-agent-task-" }, async (root) => {
159157
useTestStateDir(root);
160-
resetTaskRegistryForTests();
158+
resetAgentTaskRegistryForTests();
161159
primeMainAgentRun();
162160

163161
await invokeAgent(
@@ -188,7 +186,7 @@ describe("gateway agent handler", () => {
188186
])("tracks plugin subagent $identity runs through the registry", async ({ runId }) => {
189187
await withTempDir({ prefix: "openclaw-gateway-plugin-subagent-task-" }, async (root) => {
190188
useTestStateDir(root);
191-
resetTaskRegistryForTests();
189+
resetAgentTaskRegistryForTests();
192190
resetSubagentRegistryForTests({ persist: false });
193191
const childSessionKey = "agent:work:subagent:plugin-helper";
194192
const cfg = {
@@ -375,7 +373,7 @@ describe("gateway agent handler", () => {
375373
{ prefix: "openclaw-gateway-plugin-subagent-registry-fail-" },
376374
async (root) => {
377375
useTestStateDir(root);
378-
resetTaskRegistryForTests();
376+
resetAgentTaskRegistryForTests();
379377
resetSubagentRegistryForTests({ persist: false });
380378
const persistSubagentRunsToDiskOrThrow = vi.fn(() => {
381379
throw new Error("disk full");
@@ -456,7 +454,7 @@ describe("gateway agent handler", () => {
456454
it("terminalizes failed async gateway agent runs in the shared task registry", async () => {
457455
await withTempDir({ prefix: "openclaw-gateway-agent-task-error-" }, async (root) => {
458456
useTestStateDir(root);
459-
resetTaskRegistryForTests();
457+
resetAgentTaskRegistryForTests();
460458
primeMainAgentRun();
461459
mocks.agentCommand.mockRejectedValueOnce(new Error("agent unavailable"));
462460

@@ -483,7 +481,7 @@ describe("gateway agent handler", () => {
483481
it("preserves aborted async gateway agent runs as cancelled", async () => {
484482
await withTempDir({ prefix: "openclaw-gateway-agent-task-aborted-" }, async (root) => {
485483
useTestStateDir(root);
486-
resetTaskRegistryForTests();
484+
resetAgentTaskRegistryForTests();
487485
primeMainAgentRun();
488486
mocks.agentCommand.mockResolvedValueOnce({
489487
payloads: [],
@@ -783,7 +781,7 @@ describe("gateway agent handler", () => {
783781
it("classifies RPC-aborted async gateway agent rejections as cancelled", async () => {
784782
await withTempDir({ prefix: "openclaw-gateway-agent-task-abort-error-" }, async (root) => {
785783
useTestStateDir(root);
786-
resetTaskRegistryForTests();
784+
resetAgentTaskRegistryForTests();
787785
primeMainAgentRun();
788786
const abortError = new Error("This operation was aborted");
789787
abortError.name = "AbortError";
@@ -829,7 +827,7 @@ describe("gateway agent handler", () => {
829827
it("preserves restart ownership for aborted async gateway agent rejections", async () => {
830828
await withTempDir({ prefix: "openclaw-gateway-agent-task-restart-abort-" }, async (root) => {
831829
useTestStateDir(root);
832-
resetTaskRegistryForTests();
830+
resetAgentTaskRegistryForTests();
833831
primeMainAgentRun();
834832
const abortError = createAgentRunRestartAbortError();
835833
const wrappedError = new Error("ACP turn failed before completion", {
@@ -870,7 +868,7 @@ describe("gateway agent handler", () => {
870868
it("classifies timeout async gateway agent rejections as timed out", async () => {
871869
await withTempDir({ prefix: "openclaw-gateway-agent-task-timeout-error-" }, async (root) => {
872870
useTestStateDir(root);
873-
resetTaskRegistryForTests();
871+
resetAgentTaskRegistryForTests();
874872
primeMainAgentRun();
875873
const timeoutError = new Error("chat run timed out");
876874
timeoutError.name = "TimeoutError";
@@ -918,7 +916,7 @@ describe("gateway agent handler", () => {
918916
{ prefix: "openclaw-gateway-agent-task-wrapped-timeout-error-" },
919917
async (root) => {
920918
useTestStateDir(root);
921-
resetTaskRegistryForTests();
919+
resetAgentTaskRegistryForTests();
922920
primeMainAgentRun();
923921
const timeoutReason = new Error("chat run timed out");
924922
timeoutReason.name = "TimeoutError";
@@ -967,7 +965,7 @@ describe("gateway agent handler", () => {
967965
it("does not hide provider timeout async gateway agent rejections", async () => {
968966
await withTempDir({ prefix: "openclaw-gateway-agent-task-provider-timeout-" }, async (root) => {
969967
useTestStateDir(root);
970-
resetTaskRegistryForTests();
968+
resetAgentTaskRegistryForTests();
971969
primeMainAgentRun();
972970
const providerError = new Error("provider request timed out");
973971
providerError.name = "TimeoutError";
@@ -1147,7 +1145,7 @@ describe("gateway agent handler", () => {
11471145
it("does not overwrite operator-cancelled async gateway agent tasks after late completion", async () => {
11481146
await withTempDir({ prefix: "openclaw-gateway-agent-task-cancelled-" }, async (root) => {
11491147
useTestStateDir(root);
1150-
resetTaskRegistryForTests();
1148+
resetAgentTaskRegistryForTests();
11511149
primeMainAgentRun();
11521150
let resolveRun: (value: {
11531151
payloads: Array<{ text: string }>;
@@ -2225,7 +2223,7 @@ describe("gateway agent handler", () => {
22252223
it("dispatches async gateway agent task creation through the detached task runtime seam", async () => {
22262224
await withTempDir({ prefix: "openclaw-gateway-agent-seam-" }, async (root) => {
22272225
useTestStateDir(root);
2228-
resetTaskRegistryForTests();
2226+
resetAgentTaskRegistryForTests();
22292227
primeMainAgentRun();
22302228

22312229
const defaultRuntime = getDetachedTaskLifecycleRuntime();
@@ -2323,7 +2321,7 @@ describe("gateway agent handler", () => {
23232321
it("suppresses the gateway CLI task row for confirmed ACP manual-spawn child turns", async () => {
23242322
await withTempDir({ prefix: "openclaw-gateway-acp-suppress-" }, async (root) => {
23252323
useTestStateDir(root);
2326-
resetTaskRegistryForTests();
2324+
resetAgentTaskRegistryForTests();
23272325
const childSessionKey = "agent:main:acp:child-confirmed";
23282326
mockAcpChildSessionEntry(childSessionKey);
23292327
mocks.readAcpSessionMeta.mockReturnValue(confirmedAcpMeta);
@@ -2348,7 +2346,7 @@ describe("gateway agent handler", () => {
23482346
it("keeps CLI tracking when a non-backend operator-write caller sets acpTurnSource", async () => {
23492347
await withTempDir({ prefix: "openclaw-gateway-acp-operator-write-" }, async (root) => {
23502348
useTestStateDir(root);
2351-
resetTaskRegistryForTests();
2349+
resetAgentTaskRegistryForTests();
23522350
const childSessionKey = "agent:main:acp:child-operator-write";
23532351
mockAcpChildSessionEntry(childSessionKey);
23542352
// Persisted ACP metadata is present and the turn looks like a manual
@@ -2388,7 +2386,7 @@ describe("gateway agent handler", () => {
23882386
it("keeps CLI tracking for ACP-shaped manual-spawn turns without persisted ACP metadata", async () => {
23892387
await withTempDir({ prefix: "openclaw-gateway-acp-no-meta-" }, async (root) => {
23902388
useTestStateDir(root);
2391-
resetTaskRegistryForTests();
2389+
resetAgentTaskRegistryForTests();
23922390
const childSessionKey = "agent:main:acp:child-missing-meta";
23932391
mockAcpChildSessionEntry(childSessionKey);
23942392
mocks.readAcpSessionMeta.mockReturnValue(undefined);
@@ -2423,7 +2421,7 @@ describe("gateway agent handler", () => {
24232421
it("keeps dispatch and CLI tracking when ACP metadata read fails", async () => {
24242422
await withTempDir({ prefix: "openclaw-gateway-acp-meta-throw-" }, async (root) => {
24252423
useTestStateDir(root);
2426-
resetTaskRegistryForTests();
2424+
resetAgentTaskRegistryForTests();
24272425
const childSessionKey = "agent:main:acp:child-meta-throw";
24282426
mockAcpChildSessionEntry(childSessionKey);
24292427
const metadataError = new Error("state db unavailable");
@@ -2474,7 +2472,7 @@ describe("gateway agent handler", () => {
24742472
it("keeps CLI tracking for ACP-shaped turns that are not manual spawns", async () => {
24752473
await withTempDir({ prefix: "openclaw-gateway-acp-not-manual-spawn-" }, async (root) => {
24762474
useTestStateDir(root);
2477-
resetTaskRegistryForTests();
2475+
resetAgentTaskRegistryForTests();
24782476
const childSessionKey = "agent:main:acp:child-not-spawn";
24792477
mockAcpChildSessionEntry(childSessionKey);
24802478
// Metadata is present but the turn lacks acpTurnSource, so the spawn
@@ -2504,7 +2502,7 @@ describe("gateway agent handler", () => {
25042502
it("does not affect plugin-subagent tracking for confirmed ACP conditions", async () => {
25052503
await withTempDir({ prefix: "openclaw-gateway-acp-plugin-subagent-" }, async (root) => {
25062504
useTestStateDir(root);
2507-
resetTaskRegistryForTests();
2505+
resetAgentTaskRegistryForTests();
25082506
resetSubagentRegistryForTests({ persist: false });
25092507
const childSessionKey = "agent:main:acp:plugin-child";
25102508
const runId = "acp-plugin-subagent-run";
@@ -2557,7 +2555,7 @@ describe("gateway agent handler", () => {
25572555
it("logs a swallowed finalize error without blocking the background run", async () => {
25582556
await withTempDir({ prefix: "openclaw-gateway-agent-finalize-throw-" }, async (root) => {
25592557
useTestStateDir(root);
2560-
resetTaskRegistryForTests();
2558+
resetAgentTaskRegistryForTests();
25612559
primeMainAgentRun();
25622560

25632561
const defaultRuntime = getDetachedTaskLifecycleRuntime();

‎src/gateway/server-methods/agent.test-harness.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
resetTaskRegistryForTests,
1515
} from "../../tasks/task-runtime.test-helpers.js";
1616
import { captureEnv, setTestEnvValue } from "../../test-utils/env.js";
17+
import { installInMemoryTaskRegistryRuntime } from "../../test-utils/task-registry-runtime.js";
1718
import { createChatRunState } from "../server-chat-state.js";
1819
import { agentIdentityHandlers } from "./agent-identity.js";
1920
import { agentHandlers } from "./agent.js";
@@ -936,11 +937,21 @@ export function applyGatewaySubagentRegistryTestDeps(
936937

937938
applyGatewaySubagentRegistryTestDeps();
938939

940+
/** Keep handler tests on the real task lifecycle without paying for SQLite durability. */
941+
export function resetAgentTaskRegistryForTests(): void {
942+
resetTaskRegistryForTests({ persist: false });
943+
installInMemoryTaskRegistryRuntime();
944+
}
945+
946+
export function restoreAgentTaskRegistryRuntimeAfterTests(): void {
947+
resetTaskRegistryForTests({ persist: false });
948+
}
949+
939950
export const describe0AfterEach0 = () => {
940951
envSnapshot.restore();
941952
resetDetachedTaskLifecycleRuntimeForTests();
942953
resetDiagnosticEventsForTest();
943-
resetTaskRegistryForTests();
954+
resetAgentTaskRegistryForTests();
944955
resetSubagentRegistryForTests({ persist: false });
945956
applyGatewaySubagentRegistryTestDeps();
946957
mocks.loadConfigReturn = {};
@@ -970,7 +981,7 @@ export const describe0AfterEach0 = () => {
970981
function resetIntegrationState() {
971982
envSnapshot.restore();
972983
resetDetachedTaskLifecycleRuntimeForTests();
973-
resetTaskRegistryForTests();
984+
resetAgentTaskRegistryForTests();
974985
mocks.agentCommand.mockReset();
975986
mocks.loadConfigReturn = {};
976987
mocks.loadGatewaySessionRow.mockReset();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
import { afterAll } from "vitest";
2+
import {
3+
resetAgentTaskRegistryForTests,
4+
restoreAgentTaskRegistryRuntimeAfterTests,
5+
} from "./agent.test-harness.js";
16
import "./agent.base.test-utils.js";
27
import "./agent.media-and-routing.test-utils.js";
38
import "./agent.events-and-subagents.test-utils.js";
49
import "./agent.sessions-and-models.test-utils.js";
510
import "./agent.reset-and-identity.test-utils.js";
611
import "./agent.abort-integration.test-utils.js";
12+
13+
resetAgentTaskRegistryForTests();
14+
afterAll(restoreAgentTaskRegistryRuntimeAfterTests);

0 commit comments

Comments
 (0)