
























@@ -25,13 +25,14 @@ import {
2525resetTaskRegistryForTests,
2626} from "../../tasks/task-registry.js";
2727import { withTempDir } from "../../test-helpers/temp-dir.js";
28+import { captureEnv, setTestEnvValue } from "../../test-utils/env.js";
2829import { setGatewayDedupeEntry } from "./agent-wait-dedupe.js";
2930import { agentHandlers } from "./agent.js";
3031import { chatHandlers } from "./chat.js";
3132import { expectSubagentFollowupReactivation } from "./subagent-followup.test-helpers.js";
3233import type { GatewayRequestContext } from "./types.js";
333434-const ORIGINAL_STATE_DIR = process.env.OPENCLAW_STATE_DIR;
35+const envSnapshot = captureEnv(["OPENCLAW_STATE_DIR"]);
35363637const mocks = vi.hoisted(() => ({
3738loadSessionEntry: vi.fn(),
@@ -402,6 +403,10 @@ function resetTimeConfig() {
402403vi.useRealTimers();
403404}
404405406+function useTestStateDir(root: string): void {
407+setTestEnvValue("OPENCLAW_STATE_DIR", root);
408+}
409+405410async function expectResetCall(expectedMessage: string) {
406411const call = await waitForAgentCommandCall();
407412expect(mocks.performGatewaySessionReset).toHaveBeenCalledTimes(1);
@@ -562,11 +567,7 @@ async function invokeAgentIdentityGet(
562567563568describe("gateway agent handler", () => {
564569afterEach(() => {
565-if (ORIGINAL_STATE_DIR === undefined) {
566-delete process.env.OPENCLAW_STATE_DIR;
567-} else {
568-process.env.OPENCLAW_STATE_DIR = ORIGINAL_STATE_DIR;
569-}
570+envSnapshot.restore();
570571resetDetachedTaskLifecycleRuntimeForTests();
571572resetTaskRegistryForTests();
572573resetSubagentRegistryForTests({ persist: false });
@@ -3219,7 +3220,7 @@ describe("gateway agent handler", () => {
3219322032203221it("terminalizes successful async gateway agent runs in the shared task registry", async () => {
32213222await withTempDir({ prefix: "openclaw-gateway-agent-task-" }, async (root) => {
3222-process.env.OPENCLAW_STATE_DIR = root;
3223+useTestStateDir(root);
32233224resetTaskRegistryForTests();
32243225primeMainAgentRun();
32253226@@ -3245,7 +3246,7 @@ describe("gateway agent handler", () => {
3245324632463247it("tracks plugin SDK subagent agent runs through the subagent registry only", async () => {
32473248await withTempDir({ prefix: "openclaw-gateway-plugin-subagent-task-" }, async (root) => {
3248-process.env.OPENCLAW_STATE_DIR = root;
3249+useTestStateDir(root);
32493250resetTaskRegistryForTests();
32503251resetSubagentRegistryForTests({ persist: false });
32513252const runId = "plugin-subagent-task-run";
@@ -3365,7 +3366,7 @@ describe("gateway agent handler", () => {
33653366await withTempDir(
33663367{ prefix: "openclaw-gateway-plugin-subagent-registry-fail-" },
33673368async (root) => {
3368-process.env.OPENCLAW_STATE_DIR = root;
3369+useTestStateDir(root);
33693370resetTaskRegistryForTests();
33703371resetSubagentRegistryForTests({ persist: false });
33713372subagentRegistryTesting.setDepsForTest({
@@ -3444,7 +3445,7 @@ describe("gateway agent handler", () => {
3444344534453446it("terminalizes failed async gateway agent runs in the shared task registry", async () => {
34463447await withTempDir({ prefix: "openclaw-gateway-agent-task-error-" }, async (root) => {
3447-process.env.OPENCLAW_STATE_DIR = root;
3448+useTestStateDir(root);
34483449resetTaskRegistryForTests();
34493450primeMainAgentRun();
34503451mocks.agentCommand.mockRejectedValueOnce(new Error("agent unavailable"));
@@ -3471,7 +3472,7 @@ describe("gateway agent handler", () => {
3471347234723473it("preserves aborted async gateway agent runs as timed out", async () => {
34733474await withTempDir({ prefix: "openclaw-gateway-agent-task-aborted-" }, async (root) => {
3474-process.env.OPENCLAW_STATE_DIR = root;
3475+useTestStateDir(root);
34753476resetTaskRegistryForTests();
34763477primeMainAgentRun();
34773478mocks.agentCommand.mockResolvedValueOnce({
@@ -3507,7 +3508,7 @@ describe("gateway agent handler", () => {
3507350835083509it("classifies aborted async gateway agent rejections as timed out", async () => {
35093510await withTempDir({ prefix: "openclaw-gateway-agent-task-abort-error-" }, async (root) => {
3510-process.env.OPENCLAW_STATE_DIR = root;
3511+useTestStateDir(root);
35113512resetTaskRegistryForTests();
35123513primeMainAgentRun();
35133514const abortError = new Error("This operation was aborted");
@@ -3550,7 +3551,7 @@ describe("gateway agent handler", () => {
3550355135513552it("preserves restart ownership for aborted async gateway agent rejections", async () => {
35523553await withTempDir({ prefix: "openclaw-gateway-agent-task-restart-abort-" }, async (root) => {
3553-process.env.OPENCLAW_STATE_DIR = root;
3554+useTestStateDir(root);
35543555resetTaskRegistryForTests();
35553556primeMainAgentRun();
35563557const abortError = createAgentRunRestartAbortError();
@@ -3587,7 +3588,7 @@ describe("gateway agent handler", () => {
3587358835883589it("classifies timeout async gateway agent rejections as timed out", async () => {
35893590await withTempDir({ prefix: "openclaw-gateway-agent-task-timeout-error-" }, async (root) => {
3590-process.env.OPENCLAW_STATE_DIR = root;
3591+useTestStateDir(root);
35913592resetTaskRegistryForTests();
35923593primeMainAgentRun();
35933594const timeoutError = new Error("chat run timed out");
@@ -3632,7 +3633,7 @@ describe("gateway agent handler", () => {
36323633await withTempDir(
36333634{ prefix: "openclaw-gateway-agent-task-wrapped-timeout-error-" },
36343635async (root) => {
3635-process.env.OPENCLAW_STATE_DIR = root;
3636+useTestStateDir(root);
36363637resetTaskRegistryForTests();
36373638primeMainAgentRun();
36383639const timeoutReason = new Error("chat run timed out");
@@ -3681,7 +3682,7 @@ describe("gateway agent handler", () => {
3681368236823683it("does not hide provider timeout async gateway agent rejections", async () => {
36833684await withTempDir({ prefix: "openclaw-gateway-agent-task-provider-timeout-" }, async (root) => {
3684-process.env.OPENCLAW_STATE_DIR = root;
3685+useTestStateDir(root);
36853686resetTaskRegistryForTests();
36863687primeMainAgentRun();
36873688const providerError = new Error("provider request timed out");
@@ -3722,7 +3723,7 @@ describe("gateway agent handler", () => {
3722372337233724it("does not overwrite operator-cancelled async gateway agent tasks after late completion", async () => {
37243725await withTempDir({ prefix: "openclaw-gateway-agent-task-cancelled-" }, async (root) => {
3725-process.env.OPENCLAW_STATE_DIR = root;
3726+useTestStateDir(root);
37263727resetTaskRegistryForTests();
37273728primeMainAgentRun();
37283729let resolveRun: (value: {
@@ -4616,7 +4617,7 @@ describe("gateway agent handler", () => {
4616461746174618it("dispatches async gateway agent task creation through the detached task runtime seam", async () => {
46184619await withTempDir({ prefix: "openclaw-gateway-agent-seam-" }, async (root) => {
4619-process.env.OPENCLAW_STATE_DIR = root;
4620+useTestStateDir(root);
46204621resetTaskRegistryForTests();
46214622primeMainAgentRun();
46224623@@ -4675,7 +4676,7 @@ describe("gateway agent handler", () => {
4675467646764677it("logs a swallowed finalize error without blocking the background run", async () => {
46774678await withTempDir({ prefix: "openclaw-gateway-agent-finalize-throw-" }, async (root) => {
4678-process.env.OPENCLAW_STATE_DIR = root;
4679+useTestStateDir(root);
46794680resetTaskRegistryForTests();
46804681primeMainAgentRun();
46814682@@ -5646,11 +5647,7 @@ describe("gateway agent handler", () => {
5646564756475648describe("gateway agent handler chat.abort integration", () => {
56485649function resetIntegrationState() {
5649-if (ORIGINAL_STATE_DIR === undefined) {
5650-delete process.env.OPENCLAW_STATE_DIR;
5651-} else {
5652-process.env.OPENCLAW_STATE_DIR = ORIGINAL_STATE_DIR;
5653-}
5650+envSnapshot.restore();
56545651resetDetachedTaskLifecycleRuntimeForTests();
56555652resetTaskRegistryForTests();
56565653mocks.agentCommand.mockReset();
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。