test(agents): isolate full-suite shared state (#104682)

This commit is contained in:
Peter Steinberger
2026-07-11 13:28:21 -07:00
committed by GitHub
parent 8fe6ac7fb9
commit 262baedcf7
2 changed files with 41 additions and 0 deletions
@@ -4,6 +4,7 @@
* system events, and process lifecycle behavior.
*/
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import type { GatewayActiveWorkInspectors } from "../infra/gateway-active-work.js";
import type { RunExit } from "../process/supervisor/types.js";
import { MAX_SAFE_TIMEOUT_DELAY_MS } from "../utils/timer-delay.js";
import type { BashSandboxConfig } from "./bash-tools.shared.js";
@@ -80,10 +81,29 @@ function createDeferred<T>() {
}
function prepareSuspension(requestId: string) {
// This test owns only the background-exec registry. Other process-global
// activity counters may legitimately stay busy in the non-isolated suite.
const inspect: GatewayActiveWorkInspectors = {
getQueueSize: () => 0,
getPendingReplies: () => 0,
getEmbeddedRuns: () => 0,
getBackgroundExecSessions: getActiveBackgroundExecSessionCount,
getCronRuns: () => 0,
getActiveTasks: () => 0,
getTaskBlockers: () => [],
getRootRequests: () => 0,
getSessionAdmissions: () => 0,
getSessionMutations: () => 0,
getChatRuns: () => 0,
getQueuedTurns: () => 0,
getTerminalPersistence: () => 0,
getTerminalSessions: () => 0,
};
return prepareGatewaySuspend({
requestId,
pauseScheduling: vi.fn(),
resumeScheduling: vi.fn(),
inspect,
});
}
+21
View File
@@ -22,6 +22,7 @@ type TestRunnerInternals = {
const SHARED_TEST_SETUP = Symbol.for("openclaw.sharedTestSetup");
const EMBEDDED_RUN_STATE = Symbol.for("openclaw.embeddedRunState");
const REPLY_RUN_REGISTRY = Symbol.for("openclaw.replyRunRegistry");
const DIAGNOSTIC_EVENTS_STATE = Symbol.for("openclaw.diagnosticEvents.state.v1");
const nativeTimerGlobals = {
setTimeout: globalThis.setTimeout,
clearTimeout: globalThis.clearTimeout,
@@ -134,6 +135,13 @@ type ReplyRunStateForTest = {
waitersByKey?: Map<unknown, Set<ReplyRunWaiter>>;
};
type DiagnosticEventsStateForTest = {
listeners?: Set<unknown>;
trustedListeners?: Set<unknown>;
toolExecutionListeners?: Set<unknown>;
asyncQueue?: unknown[];
};
function runCleanupActions(actions: CleanupAction[]): unknown {
let firstError: unknown;
for (const action of actions) {
@@ -207,6 +215,18 @@ function resetOpenClawGlobalRunState(): void {
replyRunState?.waitersByKey?.clear();
}
function resetOpenClawGlobalDiagnosticState(): void {
const globalStore = globalThis as Record<PropertyKey, unknown>;
const state = globalStore[DIAGNOSTIC_EVENTS_STATE] as DiagnosticEventsStateForTest | undefined;
// The dispatcher intentionally survives module reloads. Mirror isolate mode
// without duplicating its private state defaults in the test runner.
state?.listeners?.clear();
state?.trustedListeners?.clear();
state?.toolExecutionListeners?.clear();
state?.asyncQueue?.splice(0);
Reflect.deleteProperty(globalStore, DIAGNOSTIC_EVENTS_STATE);
}
export default class OpenClawNonIsolatedRunner extends TestRunner {
override onCollectStart(file: RunnerTestFile) {
super.onCollectStart(file);
@@ -252,6 +272,7 @@ export default class OpenClawNonIsolatedRunner extends TestRunner {
restoreSharedTestHomeAfterEnvUnstub(testHome);
vi.clearAllMocks();
resetOpenClawGlobalRunState();
resetOpenClawGlobalDiagnosticState();
vi.resetModules();
const internals = this as unknown as TestRunnerInternals;
internals.moduleRunner?.mocker?.reset?.();