|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { resolveModelProviderRouteOverridePresence } from "../config/model-provider-config.js"; |
| 3 | +import { |
| 4 | + getRuntimeConfigSnapshot, |
| 5 | + getRuntimeConfigSourceSnapshot, |
| 6 | +} from "../config/runtime-snapshot.js"; |
| 7 | +import { projectConfigOntoRuntimeSourceSnapshot } from "../config/runtime-source-projection.js"; |
| 8 | +import type { OpenClawConfig } from "../config/types.openclaw.js"; |
| 9 | +import { |
| 10 | + activateSecretsRuntimeSnapshot, |
| 11 | + activateSecretsRuntimeSnapshotWithSource, |
| 12 | + clearSecretsRuntimeSnapshot, |
| 13 | + getActiveSecretsRuntimeSnapshotRevision, |
| 14 | + prepareSecretsRuntimeSnapshot, |
| 15 | +} from "../secrets/runtime.js"; |
| 16 | +import { buildGatewayReloadPlan } from "./config-reload-plan.js"; |
| 17 | +import type { GatewayConfigReloadTransactionOwnership } from "./config-reload.js"; |
| 18 | +import type { ManagedGatewayConfigReloaderParams } from "./server-reload-contracts.js"; |
| 19 | +import { createManagedReloadSecretHandlers } from "./server-reload-managed-secrets.js"; |
| 20 | +import { createRuntimeSecretsActivator } from "./server-startup-config.js"; |
| 21 | + |
| 22 | +vi.mock("../agents/context.js", () => ({ refreshContextWindowCache: vi.fn() })); |
| 23 | + |
| 24 | +afterEach(() => { |
| 25 | + clearSecretsRuntimeSnapshot(); |
| 26 | + vi.restoreAllMocks(); |
| 27 | +}); |
| 28 | + |
| 29 | +function configPair(runtime: "openclaw" | "codex") { |
| 30 | + const source = { |
| 31 | + agents: { defaults: { models: { "openai/gpt-5.6-luna": { agentRuntime: { id: runtime } } } } }, |
| 32 | + models: { |
| 33 | + providers: { |
| 34 | + openai: { |
| 35 | + api: "openai-responses", |
| 36 | + baseUrl: "https://api.openai.com/v1", |
| 37 | + models: [ |
| 38 | + { |
| 39 | + id: "gpt-5.6-luna", |
| 40 | + name: "Luna", |
| 41 | + reasoning: true, |
| 42 | + input: ["text", "image"], |
| 43 | + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, |
| 44 | + maxTokens: 4096, |
| 45 | + }, |
| 46 | + ], |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + } satisfies OpenClawConfig; |
| 51 | + const config: OpenClawConfig = structuredClone(source); |
| 52 | + // The loader may seed catalog compatibility; this is not authored request policy. |
| 53 | + config.models!.providers!.openai!.models[0]!.compat = { supportsStore: false }; |
| 54 | + return { source, config }; |
| 55 | +} |
| 56 | + |
| 57 | +const prepare = (config: OpenClawConfig) => |
| 58 | + prepareSecretsRuntimeSnapshot({ |
| 59 | + config, |
| 60 | + includeAuthStoreRefs: false, |
| 61 | + env: {}, |
| 62 | + }); |
| 63 | + |
| 64 | +function expectAuthoredSource(source: OpenClawConfig) { |
| 65 | + const config = getRuntimeConfigSnapshot(); |
| 66 | + expect(config).not.toBeNull(); |
| 67 | + expect(getRuntimeConfigSourceSnapshot()).toEqual(source); |
| 68 | + expect( |
| 69 | + resolveModelProviderRouteOverridePresence({ |
| 70 | + provider: "openai", |
| 71 | + modelId: "gpt-5.6-luna", |
| 72 | + authoredConfig: projectConfigOntoRuntimeSourceSnapshot(config!), |
| 73 | + }), |
| 74 | + ).toBe("none"); |
| 75 | +} |
| 76 | + |
| 77 | +async function createReload(canonicalActivator: boolean, commit: () => Promise<void>) { |
| 78 | + const initial = configPair("openclaw"); |
| 79 | + activateSecretsRuntimeSnapshotWithSource(await prepare(initial.config), initial.source); |
| 80 | + expectAuthoredSource(initial.source); |
| 81 | + const activator = createRuntimeSecretsActivator({ |
| 82 | + logSecrets: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, |
| 83 | + emitStateEvent: vi.fn(), |
| 84 | + prepareRuntimeSecretsSnapshot: ({ config }) => prepare(config), |
| 85 | + activateRuntimeSecretsSnapshot: activateSecretsRuntimeSnapshot, |
| 86 | + }); |
| 87 | + const activateRuntimeSecrets: ManagedGatewayConfigReloaderParams["activateRuntimeSecrets"] = |
| 88 | + canonicalActivator ? activator : (config, options) => activator(config, options); |
| 89 | + // Only secret publication is exercised here; the service tail is injected at its existing seam. |
| 90 | + const params = { |
| 91 | + activateRuntimeSecrets, |
| 92 | + resolveSharedGatewaySessionGenerationForConfig: () => undefined, |
| 93 | + sharedGatewaySessionGenerationState: { current: undefined, required: null }, |
| 94 | + clients: [], |
| 95 | + reconcileTerminalSessions: vi.fn(), |
| 96 | + }; |
| 97 | + const { onHotReload } = createManagedReloadSecretHandlers({ |
| 98 | + params, |
| 99 | + prepareRuntimeCandidate: (config) => config, |
| 100 | + tryPrepareRuntimeSecrets: async (config) => ({ |
| 101 | + snapshot: await prepare(config), |
| 102 | + expectedRevision: getActiveSecretsRuntimeSnapshotRevision(), |
| 103 | + }), |
| 104 | + applyHotReload: async (_plan, _config, publication) => { |
| 105 | + let committed = false; |
| 106 | + await publication!.publish( |
| 107 | + async () => { |
| 108 | + await commit(); |
| 109 | + committed = true; |
| 110 | + }, |
| 111 | + () => committed, |
| 112 | + ); |
| 113 | + return "applied"; |
| 114 | + }, |
| 115 | + }); |
| 116 | + const ownership: GatewayConfigReloadTransactionOwnership = { |
| 117 | + isCurrent: () => true, |
| 118 | + markRuntimeCommitted: vi.fn(), |
| 119 | + commitRuntimeEnv: vi.fn(), |
| 120 | + publishRuntimeEnv: vi.fn(), |
| 121 | + rollbackRuntimeEnv: vi.fn(), |
| 122 | + reapplyRuntimeOverlays: (config) => config, |
| 123 | + }; |
| 124 | + const next = configPair("codex"); |
| 125 | + const plan = buildGatewayReloadPlan( |
| 126 | + ["agents.defaults.models.openai/gpt-5.6-luna.agentRuntime.id"], |
| 127 | + { candidateConfig: next.config }, |
| 128 | + ); |
| 129 | + return { initial, next, run: () => onHotReload(plan, next.config, ownership, next.source) }; |
| 130 | +} |
| 131 | + |
| 132 | +describe.each([true, false])( |
| 133 | + "managed reload authored source (canonical activator: %s)", |
| 134 | + (canonical) => { |
| 135 | + it("preserves generated model metadata across a successful hot reload", async () => { |
| 136 | + const { next, run } = await createReload(canonical, async () => {}); |
| 137 | + await expect(run()).resolves.toBe("applied"); |
| 138 | + expect( |
| 139 | + getRuntimeConfigSnapshot()?.agents?.defaults?.models?.["openai/gpt-5.6-luna"]?.agentRuntime |
| 140 | + ?.id, |
| 141 | + ).toBe("codex"); |
| 142 | + expectAuthoredSource(next.source); |
| 143 | + }); |
| 144 | + |
| 145 | + it("restores the predecessor's authored source when runtime commit fails", async () => { |
| 146 | + const { initial, run } = await createReload(canonical, async () => { |
| 147 | + throw new Error("commit failed"); |
| 148 | + }); |
| 149 | + await expect(run()).rejects.toThrow("commit failed"); |
| 150 | + expectAuthoredSource(initial.source); |
| 151 | + }); |
| 152 | + |
| 153 | + it("does not roll back a newer publication's authored source", async () => { |
| 154 | + const newer = configPair("codex"); |
| 155 | + newer.source.models.providers.openai.models[0]!.name = "Newer model"; |
| 156 | + newer.config.models!.providers!.openai!.models[0]!.name = "Newer model"; |
| 157 | + const { run } = await createReload(canonical, async () => { |
| 158 | + activateSecretsRuntimeSnapshotWithSource(await prepare(newer.config), newer.source); |
| 159 | + throw new Error("superseded commit failed"); |
| 160 | + }); |
| 161 | + await expect(run()).rejects.toThrow("superseded commit failed"); |
| 162 | + expectAuthoredSource(newer.source); |
| 163 | + }); |
| 164 | + }, |
| 165 | +); |
0 commit comments