|
1 | 1 | // Cron policy tests cover per-agent defaults flattening before model resolution. |
2 | 2 | import { describe, expect, it } from "vitest"; |
3 | 3 | import { resolveAgentConfig } from "../../agents/agent-scope.js"; |
| 4 | +import { DEFAULT_PROVIDER } from "../../agents/defaults.js"; |
4 | 5 | import { resolveAllowedModelRefCore } from "../../agents/model-selection-resolve.js"; |
| 6 | +import type { ResolvedPublishedModelCatalogOwner } from "../../agents/prepared-model-catalog.types.js"; |
5 | 7 | import type { OpenClawConfig } from "../../config/types.openclaw.js"; |
| 8 | +import { createPluginMetadataSnapshotFixture } from "../../plugins/plugin-metadata.test-support.js"; |
| 9 | +import { withPluginRuntimeGenerationScope } from "../../plugins/runtime/generation-scope.js"; |
| 10 | +import { resolveCronModelSelection } from "./model-selection.js"; |
6 | 11 | import { resolveCronAgentConfig } from "./run-config.js"; |
7 | 12 |
|
8 | 13 | function buildCronConfig(cfg: OpenClawConfig, agentId: string): OpenClawConfig { |
@@ -61,4 +66,82 @@ describe("resolveCronAgentConfig model policy preservation", () => { |
61 | 66 | ref: { provider: "openai", model: "gpt-5.6-sol" }, |
62 | 67 | }); |
63 | 68 | }); |
| 69 | + |
| 70 | + it.each(["default", "subagent", "agent", "payload", "session", "hook"] as const)( |
| 71 | + "keeps the selected owner's metadata for the %s model", |
| 72 | + async (source) => { |
| 73 | + const snapshot = (workspaceDir: string, model: string) => ({ |
| 74 | + ...createPluginMetadataSnapshotFixture({ |
| 75 | + plugins: [ |
| 76 | + { |
| 77 | + id: "cron-model-policy", |
| 78 | + modelIdNormalization: { |
| 79 | + providers: { |
| 80 | + custom: { aliases: { legacy: model } }, |
| 81 | + [DEFAULT_PROVIDER]: { aliases: { legacy: model } }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + ], |
| 86 | + }), |
| 87 | + workspaceDir, |
| 88 | + }); |
| 89 | + const metadataSnapshot = snapshot("/tmp/cron-owner", "selected"); |
| 90 | + const otherWorkspace = snapshot("/tmp/other-owner", "other"); |
| 91 | + const cfg: OpenClawConfig = { |
| 92 | + agents: { |
| 93 | + defaults: { |
| 94 | + model: { primary: source === "default" ? "custom/legacy" : "custom/baseline" }, |
| 95 | + modelPolicy: { allow: ["custom/legacy", `${DEFAULT_PROVIDER}/legacy`] }, |
| 96 | + ...(source === "subagent" ? { subagents: { model: "custom/legacy" } } : {}), |
| 97 | + }, |
| 98 | + entries: { |
| 99 | + worker: { |
| 100 | + agentDir: "/tmp/cron-agent", |
| 101 | + workspace: metadataSnapshot.workspaceDir, |
| 102 | + ...(source === "agent" ? { model: "custom/legacy" } : {}), |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + ...(source === "hook" ? { hooks: { gmail: { model: "legacy" } } } : {}), |
| 107 | + }; |
| 108 | + const owner: ResolvedPublishedModelCatalogOwner = { |
| 109 | + catalogOwner: { agentId: "worker", workspaceDir: metadataSnapshot.workspaceDir }, |
| 110 | + agentId: "worker", |
| 111 | + agentDir: "/tmp/cron-agent", |
| 112 | + workspaceDir: metadataSnapshot.workspaceDir, |
| 113 | + config: cfg, |
| 114 | + authModes: {}, |
| 115 | + authStore: { version: 1, profiles: {} }, |
| 116 | + metadataSnapshot, |
| 117 | + modelCatalog: { entries: [], routeVariants: [] }, |
| 118 | + }; |
| 119 | + for (const ambient of [metadataSnapshot, otherWorkspace]) { |
| 120 | + const result = await withPluginRuntimeGenerationScope({ metadataSnapshot: ambient }, () => |
| 121 | + resolveCronModelSelection({ |
| 122 | + cfg, |
| 123 | + owner, |
| 124 | + agentConfigOverride: resolveAgentConfig(cfg, owner.agentId), |
| 125 | + agentId: owner.agentId, |
| 126 | + agentDir: owner.agentDir, |
| 127 | + workspaceDir: owner.workspaceDir, |
| 128 | + payload: { |
| 129 | + kind: "agentTurn", |
| 130 | + message: "scheduled work", |
| 131 | + ...(source === "payload" ? { model: "custom/legacy" } : {}), |
| 132 | + }, |
| 133 | + sessionEntry: |
| 134 | + source === "session" ? { providerOverride: "custom", modelOverride: "legacy" } : {}, |
| 135 | + isGmailHook: source === "hook", |
| 136 | + }), |
| 137 | + ); |
| 138 | + expect(result).toMatchObject({ |
| 139 | + ok: true, |
| 140 | + provider: source === "hook" ? DEFAULT_PROVIDER : "custom", |
| 141 | + model: "selected", |
| 142 | + modelSource: source, |
| 143 | + }); |
| 144 | + } |
| 145 | + }, |
| 146 | + ); |
64 | 147 | }); |
0 commit comments