File size: 8,693 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
import { createContextEngineLogicalTurnLease } from "../agents/harness/context-engine-logical-turn.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import { createEmptyPluginRegistry } from "../plugins/registry-empty.js";
import { withPluginRuntimeRegistryScope } from "../plugins/runtime/gateway-request-scope.js";
import { setPluginEnabledInConfig } from "../plugins/toggle-config.js";
import { LegacyContextEngine } from "./legacy.js";
import {
  listContextEngineQuarantines,
  registerContextEngineInRegistry,
  resolveContextEngine,
  resolveContextEngineOwnerPluginId,
  resolveLogicalTurnContextEngines,
} from "./registry.js";
import { resetContextEngineRuntimeQuarantineForTests } from "./registry.test-support.js";

const tempDirs = useAutoCleanupTempDirTracker(afterEach);
const engineId = "synthetic-engine";
const ownerId = "synthetic-owner";

beforeEach(() => {
  vi.stubEnv("OPENCLAW_STATE_DIR", tempDirs.make("context-engine-selection-"));
  resetContextEngineRuntimeQuarantineForTests();
});
afterEach(() => {
  resetContextEngineRuntimeQuarantineForTests();
  vi.restoreAllMocks();
  vi.unstubAllEnvs();
});

// Each case owns a registry; factories expose invocation independently from selection metadata.
function fixture(owner = engineId, registered = true) {
  const registry = createEmptyPluginRegistry();
  const factory = vi.fn(() => ({
    info: { id: engineId, name: "Synthetic" },
    ingest: async () => ({ ingested: false }),
    assemble: async () => ({ messages: [], estimatedTokens: 0 }),
    compact: async () => ({ ok: true, compacted: false, reason: "fixture" }),
  }));
  registerContextEngineInRegistry(registry, "legacy", () => new LegacyContextEngine(), "core");
  if (registered) {
    registerContextEngineInRegistry(registry, engineId, factory, `plugin:${owner}`);
  }
  return { registry, factory };
}

for (const path of ["standalone", "logical-turn"] as const) {
  describe(path, () => {
    // Exercise both public resolvers and release every returned instance before assertions.
    async function resolve(config: OpenClawConfig) {
      if (path === "standalone") {
        const engine = await resolveContextEngine(config);
        const result = {
          id: engine.info.id,
          owner: resolveContextEngineOwnerPluginId(engine),
          failure: undefined,
        };
        await engine.dispose?.();
        return result;
      }
      const result = await resolveLogicalTurnContextEngines(config);
      await result.configured.engine.dispose?.();
      if (result.configured !== result.fallback) {
        await result.fallback.engine.dispose?.();
      }
      return {
        id: result.configuredId,
        owner: result.configured.ownerPluginId,
        failure: result.configuredFailure,
      };
    }

    it.each([
      ["disabled absent", engineId, false, { entries: { [engineId]: { enabled: false } } }],
      ["disabled registered", engineId, true, { entries: { [engineId]: { enabled: false } } }],
      ["global disable absent", engineId, false, { enabled: false }],
      ["global disable registered", engineId, true, { enabled: false }],
      ["denied absent", engineId, false, { deny: [engineId] }],
      ["denied registered", engineId, true, { deny: [engineId] }],
      ["distinct owner disabled", ownerId, true, { entries: { [ownerId]: { enabled: false } } }],
      ["distinct owner denied", ownerId, true, { deny: [ownerId] }],
    ] satisfies Array<[string, string, boolean, OpenClawConfig["plugins"]]>)(
      "uses normal default for %s",
      async (_name, owner, registered, policy) => {
        const { registry, factory } = fixture(owner, registered);
        const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
        const error = vi.spyOn(console, "error").mockImplementation(() => {});
        const config = { plugins: { ...policy, slots: { contextEngine: engineId } } };
        await withPluginRuntimeRegistryScope(registry, async () => {
          expect(await resolve(config)).toEqual({
            id: "legacy",
            owner: undefined,
            failure: undefined,
          });
          expect(listContextEngineQuarantines()).toEqual([]);
        });
        expect(factory).not.toHaveBeenCalled();
        expect(warn).not.toHaveBeenCalled();
        expect(error).not.toHaveBeenCalled();
        expect(config.plugins.slots.contextEngine).toBe(engineId);
      },
    );

    it.each([undefined, "", "legacy", "none"])("uses default for slot %s", async (slot) => {
      const { registry, factory } = fixture();
      await withPluginRuntimeRegistryScope(registry, async () => {
        expect(
          await resolve({ plugins: { enabled: false, slots: { contextEngine: slot } } }),
        ).toEqual({ id: "legacy", owner: undefined, failure: undefined });
        expect(listContextEngineQuarantines()).toEqual([]);
      });
      expect(factory).not.toHaveBeenCalled();
    });

    it.each([engineId, ownerId])(
      "keeps enabled engine ID separate from owner %s",
      async (owner) => {
        const { registry, factory } = fixture(owner);
        await withPluginRuntimeRegistryScope(registry, async () => {
          expect(
            await resolve({
              plugins: {
                slots: { contextEngine: engineId },
                entries: { [owner]: { enabled: true } },
              },
            }),
          ).toEqual({ id: engineId, owner, failure: undefined });
        });
        expect(factory).toHaveBeenCalledOnce();
      },
    );

    it.each(["missing", "factory"])("preserves enabled %s failure", async (failure) => {
      const { registry, factory } = fixture(engineId, failure !== "missing");
      factory.mockImplementation(() => {
        throw new Error("synthetic factory failure");
      });
      vi.spyOn(console, "error").mockImplementation(() => {});
      await withPluginRuntimeRegistryScope(registry, async () => {
        const result = await resolve({ plugins: { slots: { contextEngine: engineId } } });
        if (path === "logical-turn") {
          expect(result.failure).toContain(
            failure === "missing" ? "not registered" : "synthetic factory failure",
          );
        } else {
          expect(result.id).toBe("legacy");
          expect(listContextEngineQuarantines()).toEqual([
            expect.objectContaining({
              engineId,
              operation: failure === "missing" ? "resolve" : "factory",
            }),
          ]);
        }
      });
    });

    it("disables then re-enables without clearing the retained slot or registering again", async () => {
      const { registry, factory } = fixture();
      const initial = { plugins: { slots: { contextEngine: engineId } } };
      const disabled = setPluginEnabledInConfig(initial, engineId, false);
      await withPluginRuntimeRegistryScope(registry, async () => {
        expect((await resolve(disabled)).id).toBe("legacy");
        expect(factory).not.toHaveBeenCalled();
        expect((await resolve(setPluginEnabledInConfig(disabled, engineId, true))).id).toBe(
          engineId,
        );
        expect(listContextEngineQuarantines()).toEqual([]);
      });
      expect(disabled.plugins?.slots?.contextEngine).toBe(engineId);
      expect(factory).toHaveBeenCalledOnce();
    });
  });
}

it.each([true, false])(
  "plugin disable produces subsequent ordinary logical turns without degradation warnings (registered=%s)",
  async (registered) => {
    const { registry, factory } = fixture(engineId, registered);
    const config = setPluginEnabledInConfig(
      { plugins: { slots: { contextEngine: engineId } } },
      engineId,
      false,
    );
    const warn = vi.fn();
    await withPluginRuntimeRegistryScope(registry, async () => {
      for (const runId of ["first", "second"]) {
        const lease = await createContextEngineLogicalTurnLease({
          config,
          identity: { runId, sessionId: "synthetic-session" },
          warn,
        });
        try {
          lease.begin();
          expect(lease.effectiveEngineId).toBe("legacy");
          expect(lease.degraded).toBe(false);
          await lease.engine.assemble({
            sessionId: "synthetic-session",
            messages: [],
            tokenBudget: 100,
          });
        } finally {
          await lease.dispose();
        }
      }
      expect(listContextEngineQuarantines()).toEqual([]);
    });
    expect(factory).not.toHaveBeenCalled();
    expect(warn).not.toHaveBeenCalled();
  },
);