File size: 11,805 Bytes
d197cf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Agent command test mocks replace logging and runtime-heavy modules shared by agent command suites.
import { vi } from "vitest";
import { getAgentHarnessPluginMocks } from "./agent-command-state.test-mocks.js";

// Harness/plugin selection has focused owner coverage in runtime-plugin.test.ts.
// Command suites only need to prove their handoff without loading plugin manifests.
const agentHarnessPluginMocks = getAgentHarnessPluginMocks();

vi.mock("../agents/harness/runtime-plugin.js", () => ({
  ensureSelectedAgentHarnessPlugin: agentHarnessPluginMocks.ensureSelectedAgentHarnessPlugin,
}));

vi.mock("../agents/runtime-plugins.js", () => ({
  withAgentPluginRegistry: ({ run }: { run: () => unknown }) => run(),
}));

vi.mock("../logging/subsystem.js", () => {
  const createMockLogger = () => ({
    subsystem: "test",
    isEnabled: vi.fn(() => true),
    trace: vi.fn(),
    info: vi.fn(),
    warn: vi.fn(),
    error: vi.fn(),
    debug: vi.fn(),
    fatal: vi.fn(),
    raw: vi.fn(),
    child: vi.fn(() => createMockLogger()),
  });
  return {
    createSubsystemLogger: vi.fn(() => createMockLogger()),
  };
});

vi.mock("../cli/deps.js", () => ({
  createDefaultDeps: vi.fn(() => ({})),
}));

const acpManagerMock = vi.hoisted(() => ({
  current: {
    resolveSession: vi.fn(() => null),
  } as unknown,
}));

vi.mock("../acp/control-plane/manager.js", () => ({
  testing: {
    resetAcpSessionManagerForTests: vi.fn(() => {
      acpManagerMock.current = {
        resolveSession: vi.fn(() => null),
      };
    }),
    setAcpSessionManagerForTests: vi.fn((manager: unknown) => {
      acpManagerMock.current = manager;
    }),
  },
  getAcpSessionManager: vi.fn(() => acpManagerMock.current),
}));

vi.mock("../agents/embedded-agent.js", () => ({
  abortEmbeddedAgentRun: vi.fn().mockReturnValue(false),
  runEmbeddedAgent: vi.fn(),
  resolveEmbeddedSessionLane: (key: string) => `session:${key.trim() || "main"}`,
}));

vi.mock("../agents/model-catalog.js", () => ({
  loadManifestModelCatalog: vi.fn(() => []),
}));

vi.mock("../agents/prepared-model-catalog.js", () => ({
  loadProviderScopedThinkingCatalog: vi.fn(async () => []),
  readPreparedModelCatalog: vi.fn(),
  loadPreparedModelCatalogSnapshot: vi.fn(async () => ({
    entries: [],
    routeVariants: [],
  })),
}));

vi.mock("../agents/model-selection.js", () => {
  type ConfigWithModels = {
    meta?: { migrations?: { modelPolicyAllowlist?: boolean } };
    agents?: {
      defaults?: {
        model?: string | { primary?: string; fallbacks?: string[] };
        modelPolicy?: { allow?: string[] };
        models?: Record<string, { params?: { thinking?: string } } | undefined>;
        thinkingDefault?: string;
      };
    };
  };
  type ModelRef = { provider: string; model: string };
  type CatalogEntry = { id?: string; model?: string; name?: string; reasoning?: boolean };

  const parseModelRefImpl = (raw: string, defaultProvider = "openai"): ModelRef | null => {
    const value = raw.trim();
    if (!value) {
      return null;
    }
    const slash = value.indexOf("/");
    if (slash >= 0) {
      return {
        provider: value.slice(0, slash).trim(),
        model: value.slice(slash + 1).trim(),
      };
    }
    return { provider: defaultProvider, model: value };
  };
  const parseModelRef = vi.fn(parseModelRefImpl);
  const normalizeProviderId = (provider: string) => provider.trim().toLowerCase();
  const normalizeModelRef = (provider: string, model: string): ModelRef => ({
    provider: normalizeProviderId(provider),
    model: model.trim(),
  });
  const modelKey = (provider: string, model: string) =>
    `${normalizeProviderId(provider)}/${model.trim().toLowerCase()}`;
  const isModelKeyAllowedBySet = (allowedKeys: ReadonlySet<string>, key: string) => {
    if (allowedKeys.has(key)) {
      return true;
    }
    let separator = key.indexOf("/");
    while (separator > 0) {
      if (allowedKeys.has(`${key.slice(0, separator + 1)}*`)) {
        return true;
      }
      separator = key.indexOf("/", separator + 1);
    }
    return false;
  };
  const resolvePrimary = (cfg?: ConfigWithModels): string | undefined => {
    const primary = cfg?.agents?.defaults?.model;
    if (typeof primary === "string") {
      return primary;
    }
    return primary?.primary;
  };
  const resolveDefaultRef = (cfg?: ConfigWithModels): ModelRef => {
    const parsed = parseModelRefImpl(resolvePrimary(cfg) ?? "openai/gpt-5.5", "openai");
    return parsed ?? { provider: "openai", model: "gpt-5.5" };
  };
  const resolveModelConfig = (cfg: ConfigWithModels | undefined, ref: ModelRef) => {
    const models = cfg?.agents?.defaults?.models ?? {};
    return models[`${ref.provider}/${ref.model}`] ?? models[modelKey(ref.provider, ref.model)];
  };
  const resolvePolicyRefs = (cfg?: ConfigWithModels) => {
    const defaults = cfg?.agents?.defaults;
    const hasExplicitPolicy = Boolean(
      defaults?.modelPolicy && Object.hasOwn(defaults.modelPolicy, "allow"),
    );
    if (hasExplicitPolicy) {
      return {
        refs: defaults?.modelPolicy?.allow ?? [],
        configPath: "agents.defaults.modelPolicy.allow",
      };
    }
    if (cfg?.meta?.migrations?.modelPolicyAllowlist !== true) {
      const refs = Object.keys(defaults?.models ?? {});
      if (refs.length > 0) {
        return { refs, configPath: "agents.defaults.models" };
      }
    }
    return { refs: [], configPath: null };
  };

  return {
    buildAllowedModelSet: vi.fn(({ cfg }: { cfg?: ConfigWithModels; catalog?: CatalogEntry[] }) => {
      const refs = new Set<string>();
      const policyRefs = resolvePolicyRefs(cfg).refs;
      for (const raw of policyRefs) {
        const parsed = parseModelRefImpl(raw, "openai");
        if (parsed) {
          refs.add(modelKey(parsed.provider, parsed.model));
        }
      }
      const primary = resolveDefaultRef(cfg);
      refs.add(modelKey(primary.provider, primary.model));
      return {
        allowedKeys: refs,
        allowedCatalog: [],
        allowAny: policyRefs.length === 0,
      };
    }),
    createModelVisibilityPolicy: vi.fn(
      ({ cfg, catalog = [] }: { cfg?: ConfigWithModels; catalog?: CatalogEntry[] }) => {
        const refs = new Set<string>();
        const policy = resolvePolicyRefs(cfg);
        const policyRefs = policy.refs;
        for (const raw of policyRefs) {
          const parsed = parseModelRefImpl(raw, "openai");
          if (parsed) {
            refs.add(modelKey(parsed.provider, parsed.model));
          }
        }
        const primary = resolveDefaultRef(cfg);
        refs.add(modelKey(primary.provider, primary.model));
        const allowAny = policyRefs.length === 0;
        const wildcardModelKeys = new Set(
          policyRefs.filter((key) => key.endsWith("/*")).map((key) => key.trim().toLowerCase()),
        );
        const wildcardProviders = new Set(
          [...wildcardModelKeys].map((key) => key.slice(0, key.indexOf("/"))),
        );
        const allowsKey = (key: string) => allowAny || isModelKeyAllowedBySet(refs, key);
        return {
          allowAny,
          catalog,
          allowedKeys: refs,
          allowedCatalog: catalog,
          exactModelRefs: policyRefs.filter((key) => !key.endsWith("/*")),
          providerWildcards: wildcardProviders,
          hasConfiguredEntries: policyRefs.length > 0,
          hasProviderWildcards: wildcardModelKeys.size > 0,
          allowConfigPath: policy.configPath,
          allowRepairConfigPath: "agents.defaults.modelPolicy.allow",
          allows: ({ provider, model }: ModelRef) => allowsKey(modelKey(provider, model)),
          allowsByWildcard: ({ provider, model }: ModelRef) =>
            isModelKeyAllowedBySet(wildcardModelKeys, modelKey(provider, model)),
          resolveSelection: ({ provider, model }: ModelRef) => {
            const key = modelKey(provider, model);
            if (allowsKey(key)) {
              return { provider, model };
            }
            const fallback = catalog[0];
            return fallback?.id ? { provider: "openai", model: fallback.id } : null;
          },
          visibleCatalog: ({ catalog: visibleCatalog }: { catalog: CatalogEntry[] }) =>
            visibleCatalog,
        };
      },
    ),
    buildConfiguredModelCatalog: vi.fn(() => []),
    buildModelAliasIndex: vi.fn(() => new Map()),
    isModelKeyAllowedBySet,
    isCliProvider: vi.fn(() => false),
    modelKey,
    normalizeModelRef,
    normalizeProviderId,
    normalizeProviderIdForAuth: normalizeProviderId,
    parseModelRef,
    resolveConfiguredModelRef: vi.fn(
      ({ cfg }: { cfg?: ConfigWithModels; defaultProvider?: string; defaultModel?: string }) =>
        resolveDefaultRef(cfg),
    ),
    resolveDefaultModelForAgent: vi.fn(({ cfg }: { cfg?: ConfigWithModels }) =>
      resolveDefaultRef(cfg),
    ),
    resolveModelRefFromString: vi.fn(
      ({ raw, defaultProvider }: { raw: string; defaultProvider?: string }) => {
        const ref = parseModelRef(raw, defaultProvider ?? "openai");
        return ref ? { ref, source: "parsed" } : null;
      },
    ),
    resolveThinkingDefault: vi.fn(
      ({
        cfg,
        provider,
        model,
        catalog,
      }: {
        cfg?: ConfigWithModels;
        provider: string;
        model: string;
        catalog?: CatalogEntry[];
      }) => {
        const ref = normalizeModelRef(provider, model);
        const modelThinking = resolveModelConfig(cfg, ref)?.params?.thinking;
        if (modelThinking) {
          return modelThinking;
        }
        const defaultThinking = cfg?.agents?.defaults?.thinkingDefault;
        if (defaultThinking) {
          return defaultThinking;
        }
        const entry = catalog?.find((item) => item.id === model || item.model === model);
        if (entry?.reasoning && entry.name?.includes("4.6")) {
          return "adaptive";
        }
        return entry?.reasoning ? "low" : "off";
      },
    ),
  };
});

vi.mock("../agents/subagents/announce/subagent-announce.js", () => ({
  runSubagentAnnounceFlow: vi.fn(),
}));

vi.mock("../gateway/call.js", () => ({
  callGateway: vi.fn(),
}));

vi.mock("../agents/workspace.js", () => ({
  DEFAULT_AGENT_WORKSPACE_DIR: "/tmp/openclaw-workspace",
  DEFAULT_AGENTS_FILENAME: "AGENTS.md",
  DEFAULT_IDENTITY_FILENAME: "IDENTITY.md",
  resolveDefaultAgentWorkspaceDir: () => "/tmp/openclaw-workspace",
  ensureAgentWorkspace: vi.fn(async ({ dir }: { dir: string }) => ({ dir })),
}));

vi.mock("../skills/loading/workspace-skill-prompt.js", () => ({
  buildSkillSnapshot: vi.fn(() => undefined),
}));

vi.mock("../skills/loading/workspace-skill-loader.js", () => {
  return {
    filterWorkspaceSkills: (entries: unknown[]) => entries,
    loadVisibleSkills: vi.fn(() => []),
    loadWorkspaceSkills: vi.fn(() => []),
  };
});

vi.mock("../skills/runtime/remote.js", () => ({
  getRemoteSkillEligibility: vi.fn(() => undefined),
}));

vi.mock("../plugins/bundle-commands.js", () => ({
  loadEnabledClaudeBundleCommands: vi.fn(() => []),
}));

vi.mock("../skills/discovery/agent-filter.js", () => ({
  resolveEffectiveAgentSkillFilter: vi.fn(() => undefined),
}));

vi.mock("../skills/runtime/session-snapshot.js", () => ({
  resolveReusableWorkspaceSkillSnapshot: vi.fn(
    (params?: { existingSnapshot?: unknown; skillFilter?: string[] }) => ({
      snapshot: params?.existingSnapshot ?? {
        prompt: "",
        skills: [],
        resolvedSkills: [],
        ...(params?.skillFilter === undefined ? {} : { skillFilter: params.skillFilter }),
        version: 0,
      },
      shouldRefresh: !params?.existingSnapshot,
      snapshotVersion: 0,
    }),
  ),
}));

vi.mock("../agents/exec-defaults.js", () => ({
  resolveNodeExecEligibility: vi.fn(() => ({ canExec: false })),
}));