File size: 1,912 Bytes
76289e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { requireActivePluginRegistry } from "../plugins/runtime.js";
import { resolveGlobalSingleton } from "../shared/global-singleton.js";
import {
  clearPersistedContextEngineQuarantineForProcess,
  recordPersistedContextEngineQuarantine,
} from "./quarantine-health.js";

type ContextEngineRuntimeQuarantineForTests = {
  engineId: string;
  owner?: string;
  operation: string;
  reason: string;
  failedAt: Date;
};

type ContextEngineRegistryStateForTests = {
  quarantinedEngines: Map<string, ContextEngineRuntimeQuarantineForTests>;
};

const CONTEXT_ENGINE_REGISTRY_STATE = Symbol.for("openclaw.contextEngineRegistryState");

function getContextEngineRegistryStateForTests(): ContextEngineRegistryStateForTests {
  return resolveGlobalSingleton<ContextEngineRegistryStateForTests>(
    CONTEXT_ENGINE_REGISTRY_STATE,
    () => ({ quarantinedEngines: new Map() }),
  );
}

export function captureContextEngineRegistryStateForTests(): () => void {
  const state = getContextEngineRegistryStateForTests();
  const registry = requireActivePluginRegistry();
  const engines = new Map(registry.contextEngines);
  const quarantinedEngines = new Map(state.quarantinedEngines);

  return () => {
    registry.contextEngines.clear();
    for (const [engineId, registration] of engines) {
      registry.contextEngines.set(engineId, registration as never);
    }

    state.quarantinedEngines.clear();
    clearPersistedContextEngineQuarantineForProcess(undefined, process.pid);
    for (const [engineId, quarantine] of quarantinedEngines) {
      state.quarantinedEngines.set(engineId, quarantine);
      recordPersistedContextEngineQuarantine(quarantine);
    }
  };
}

export function resetContextEngineRuntimeQuarantineForTests(): void {
  const state = getContextEngineRegistryStateForTests();
  state.quarantinedEngines.clear();
  clearPersistedContextEngineQuarantineForProcess(undefined, process.pid);
}