
























11import fs from "node:fs/promises";
22import path from "node:path";
33import {
4+abortAndDrainAgentHarnessRun,
45nativeHookRelayTesting,
56queueAgentHarnessMessage,
67resetAgentEventsForTest,
@@ -30,6 +31,8 @@ const appServerHarnessWait = { interval: 1, timeout: 120_000 } as const;
3031const activeAppServerAttemptsForTest = new Set<{
3132abortController?: AbortController;
3233promise: Promise<unknown>;
34+sessionId: string;
35+sessionKey?: string;
3336}>();
34373538type RunCodexAppServerAttemptOptions = NonNullable<
@@ -62,6 +65,8 @@ export function runCodexAppServerAttempt(
6265const entry = {
6366 abortController,
6467promise: undefined as unknown as Promise<unknown>,
68+sessionId: params.sessionId,
69+sessionKey: params.sessionKey,
6570};
6671const promise = runCodexAppServerAttemptImpl(
6772trackedParams,
@@ -76,19 +81,41 @@ export function runCodexAppServerAttempt(
7681}
77827883async function drainActiveAppServerAttemptsForTest(): Promise<void> {
84+vi.useRealTimers();
7985const attempts = [...activeAppServerAttemptsForTest];
8086if (attempts.length === 0) {
8187return;
8288}
8389for (const attempt of attempts) {
8490attempt.abortController?.abort("test_cleanup");
8591}
86-await Promise.race([
87-Promise.allSettled(attempts.map((attempt) => attempt.promise)),
88-new Promise<void>((resolve) => {
89-setTimeout(resolve, 5_000);
92+const drainedSessions = new Set<string>();
93+const sessionDrains = attempts.flatMap((attempt) => {
94+if (!attempt.sessionId || drainedSessions.has(attempt.sessionId)) {
95+return [];
96+}
97+drainedSessions.add(attempt.sessionId);
98+return [
99+abortAndDrainAgentHarnessRun({
100+sessionId: attempt.sessionId,
101+sessionKey: attempt.sessionKey,
102+settleMs: 1_000,
103+forceClear: true,
104+reason: "test_cleanup",
105+}).catch(() => undefined),
106+];
107+});
108+const drainResult = await Promise.race([
109+Promise.allSettled([...attempts.map((attempt) => attempt.promise), ...sessionDrains]).then(
110+() => "settled" as const,
111+),
112+new Promise<"timeout">((resolve) => {
113+setTimeout(() => resolve("timeout"), 5_000);
90114}),
91115]);
116+if (drainResult === "settled") {
117+activeAppServerAttemptsForTest.clear();
118+}
92119}
9312094121export function createParams(sessionFile: string, workspaceDir: string): EmbeddedRunAttemptParams {
@@ -490,8 +517,8 @@ export function setupRunAttemptTestHooks(): void {
490517resetGlobalHookRunner();
491518clearInternalHooks();
492519defaultCodexAppInventoryCache.clear();
493-vi.useRealTimers();
494520vi.restoreAllMocks();
521+vi.useRealTimers();
495522vi.unstubAllEnvs();
496523await closeCodexSandboxExecServersForTests();
497524await fs.rm(tempDir, { recursive: true, force: true });
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。