





















@@ -1,5 +1,9 @@
11import { 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";
3748describe("agent cleanup timeout", () => {
59const log = {
@@ -35,6 +39,91 @@ describe("agent cleanup timeout", () => {
3539);
3640});
374142+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+38127it("logs cleanup rejection without throwing", async () => {
39128await expect(
40129runAgentCleanupStep({
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。