|
1 | 1 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
2 | | -import { AGENT_CLEANUP_STEP_TIMEOUT_MS, runAgentCleanupStep } from "./run-cleanup-timeout.js"; |
| 2 | +import { |
| 3 | + AGENT_CLEANUP_STEP_TIMEOUT_MS, |
| 4 | + resolveAgentCleanupStepTimeoutMs, |
| 5 | + runAgentCleanupStep, |
| 6 | +} from "./run-cleanup-timeout.js"; |
3 | 7 |
|
4 | 8 | describe("agent cleanup timeout", () => { |
5 | 9 | const log = { |
@@ -35,6 +39,91 @@ describe("agent cleanup timeout", () => { |
35 | 39 | ); |
36 | 40 | }); |
37 | 41 |
|
| 42 | + it("uses the trajectory flush timeout environment override for trajectory cleanup", async () => { |
| 43 | + const cleanup = vi.fn(async () => new Promise<never>(() => {})); |
| 44 | + |
| 45 | + const result = runAgentCleanupStep({ |
| 46 | + runId: "run-trajectory", |
| 47 | + sessionId: "session-trajectory", |
| 48 | + step: "pi-trajectory-flush", |
| 49 | + cleanup, |
| 50 | + log, |
| 51 | + env: { |
| 52 | + OPENCLAW_TRAJECTORY_FLUSH_TIMEOUT_MS: "25000", |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + await vi.advanceTimersByTimeAsync(24_999); |
| 57 | + expect(log.warn).not.toHaveBeenCalled(); |
| 58 | + |
| 59 | + await vi.advanceTimersByTimeAsync(1); |
| 60 | + await expect(result).resolves.toBeUndefined(); |
| 61 | + |
| 62 | + expect(cleanup).toHaveBeenCalledTimes(1); |
| 63 | + expect(log.warn).toHaveBeenCalledWith( |
| 64 | + "agent cleanup timed out: runId=run-trajectory sessionId=session-trajectory step=pi-trajectory-flush timeoutMs=25000", |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it("uses the general cleanup timeout environment override for other cleanup steps", async () => { |
| 69 | + const cleanup = vi.fn(async () => new Promise<never>(() => {})); |
| 70 | + |
| 71 | + const result = runAgentCleanupStep({ |
| 72 | + runId: "run-general", |
| 73 | + sessionId: "session-general", |
| 74 | + step: "bundle-mcp-retire", |
| 75 | + cleanup, |
| 76 | + log, |
| 77 | + env: { |
| 78 | + OPENCLAW_AGENT_CLEANUP_TIMEOUT_MS: "1500", |
| 79 | + }, |
| 80 | + }); |
| 81 | + |
| 82 | + await vi.advanceTimersByTimeAsync(1_500); |
| 83 | + await expect(result).resolves.toBeUndefined(); |
| 84 | + |
| 85 | + expect(log.warn).toHaveBeenCalledWith( |
| 86 | + "agent cleanup timed out: runId=run-general sessionId=session-general step=bundle-mcp-retire timeoutMs=1500", |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + it("prefers explicit cleanup timeout values over environment overrides", () => { |
| 91 | + expect( |
| 92 | + resolveAgentCleanupStepTimeoutMs({ |
| 93 | + step: "pi-trajectory-flush", |
| 94 | + timeoutMs: 2_000, |
| 95 | + env: { |
| 96 | + OPENCLAW_TRAJECTORY_FLUSH_TIMEOUT_MS: "25000", |
| 97 | + OPENCLAW_AGENT_CLEANUP_TIMEOUT_MS: "15000", |
| 98 | + }, |
| 99 | + }), |
| 100 | + ).toBe(2_000); |
| 101 | + }); |
| 102 | + |
| 103 | + it("keeps explicit zero cleanup timeouts as a one millisecond timeout", () => { |
| 104 | + expect( |
| 105 | + resolveAgentCleanupStepTimeoutMs({ |
| 106 | + step: "pi-trajectory-flush", |
| 107 | + timeoutMs: 0, |
| 108 | + env: { |
| 109 | + OPENCLAW_TRAJECTORY_FLUSH_TIMEOUT_MS: "25000", |
| 110 | + }, |
| 111 | + }), |
| 112 | + ).toBe(1); |
| 113 | + }); |
| 114 | + |
| 115 | + it("ignores invalid cleanup timeout environment values", () => { |
| 116 | + expect( |
| 117 | + resolveAgentCleanupStepTimeoutMs({ |
| 118 | + step: "pi-trajectory-flush", |
| 119 | + env: { |
| 120 | + OPENCLAW_TRAJECTORY_FLUSH_TIMEOUT_MS: "0", |
| 121 | + OPENCLAW_AGENT_CLEANUP_TIMEOUT_MS: "not-a-number", |
| 122 | + }, |
| 123 | + }), |
| 124 | + ).toBe(AGENT_CLEANUP_STEP_TIMEOUT_MS); |
| 125 | + }); |
| 126 | + |
38 | 127 | it("logs cleanup rejection without throwing", async () => { |
39 | 128 | await expect( |
40 | 129 | runAgentCleanupStep({ |
|
0 commit comments