|
| 1 | +import { afterEach, describe, expect, mock, spyOn, test } from "bun:test"; |
| 2 | +import type { ToolCall, ToolDefinition } from "@intx/types/runtime"; |
| 3 | + |
| 4 | +import { presentDefinition } from "../agent/director.js"; |
| 5 | +import { normalizeToolDefinitionsForProvider } from "../agent/tool-schema-normalize.js"; |
| 6 | +import { createPermissionGate } from "../permission/gate.js"; |
| 7 | +import * as permissionStore from "../permission/store.js"; |
| 8 | +import type { Approval } from "../permission/types.js"; |
| 9 | +import { createApprovalPersist } from "./runtime-assembly.js"; |
| 10 | +import { applyLiveModelSwitch, providerModelKey, type LiveModelRef } from "./live-model-switch.js"; |
| 11 | + |
| 12 | +const MODEL_A: LiveModelRef = { providerName: "openai", model: "gpt-5" }; |
| 13 | +const MODEL_B: LiveModelRef = { providerName: "anthropic", model: "claude-opus" }; |
| 14 | +const MODEL_KIMI: LiveModelRef = { providerName: "moonshot", model: "kimi-k2" }; |
| 15 | + |
| 16 | +const canonicalDefs: readonly ToolDefinition[] = [presentDefinition]; |
| 17 | + |
| 18 | +function schemaHasRef(value: unknown): boolean { |
| 19 | + if (value === null || typeof value !== "object") return false; |
| 20 | + if (Array.isArray(value)) return value.some(schemaHasRef); |
| 21 | + const obj = value as Record<string, unknown>; |
| 22 | + if ("$ref" in obj) return true; |
| 23 | + return Object.values(obj).some(schemaHasRef); |
| 24 | +} |
| 25 | + |
| 26 | +const shellCall = (command: string): ToolCall => ({ |
| 27 | + id: "c", |
| 28 | + name: "run_shell", |
| 29 | + arguments: { command }, |
| 30 | +}); |
| 31 | + |
| 32 | +const providerModelScope = { |
| 33 | + id: "provider-model", |
| 34 | + label: "", |
| 35 | + pattern: "npm *", |
| 36 | + grant: "provider-model" as const, |
| 37 | +}; |
| 38 | + |
| 39 | +function presentSchema(defs: readonly ToolDefinition[]): unknown { |
| 40 | + return defs.find((d) => d.name === "present")?.inputSchema; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Same collaborators the TUI `/model` handler wires through |
| 45 | + * `applyLiveModelSwitch`: live identity (persist reads it), permission gate, |
| 46 | + * inference rebuild, and canonical-then-family-gate advertise. |
| 47 | + */ |
| 48 | +function createProductionSwitch() { |
| 49 | + let identity: LiveModelRef = MODEL_A; |
| 50 | + let inference: LiveModelRef = MODEL_A; |
| 51 | + let advertised = normalizeToolDefinitionsForProvider(canonicalDefs, MODEL_A); |
| 52 | + |
| 53 | + const persist = createApprovalPersist("/tmp/proj", () => providerModelKey(identity)); |
| 54 | + const gate = createPermissionGate({ |
| 55 | + approvals: [], |
| 56 | + requestApproval: async () => ({ allow: true, persist: providerModelScope }), |
| 57 | + persist, |
| 58 | + interactive: true, |
| 59 | + skipPermissions: false, |
| 60 | + auto: false, |
| 61 | + providerName: identity.providerName, |
| 62 | + model: identity.model, |
| 63 | + }); |
| 64 | + |
| 65 | + const switchTo = (next: LiveModelRef): void => { |
| 66 | + applyLiveModelSwitch(next, { |
| 67 | + applyIdentity: (ref) => { |
| 68 | + identity = ref; |
| 69 | + }, |
| 70 | + setPermissionIdentity: (providerName, model) => { |
| 71 | + gate.setProviderIdentity(providerName, model); |
| 72 | + }, |
| 73 | + rebuildInference: (ref) => { |
| 74 | + inference = ref; |
| 75 | + }, |
| 76 | + refreshAdvertisedSchemas: (ref) => { |
| 77 | + advertised = normalizeToolDefinitionsForProvider(canonicalDefs, ref); |
| 78 | + }, |
| 79 | + }); |
| 80 | + }; |
| 81 | + |
| 82 | + return { |
| 83 | + gate, |
| 84 | + switchTo, |
| 85 | + identity: () => identity, |
| 86 | + inference: () => inference, |
| 87 | + advertised: () => advertised, |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +describe("applyLiveModelSwitch", () => { |
| 92 | + afterEach(() => { |
| 93 | + mock.restore(); |
| 94 | + }); |
| 95 | + |
| 96 | + test("refreshes identity, permission, inference, and schemas as one operation", () => { |
| 97 | + const order: string[] = []; |
| 98 | + applyLiveModelSwitch(MODEL_B, { |
| 99 | + applyIdentity: () => { |
| 100 | + order.push("identity"); |
| 101 | + }, |
| 102 | + setPermissionIdentity: () => { |
| 103 | + order.push("permission"); |
| 104 | + }, |
| 105 | + rebuildInference: () => { |
| 106 | + order.push("inference"); |
| 107 | + }, |
| 108 | + refreshAdvertisedSchemas: () => { |
| 109 | + order.push("schemas"); |
| 110 | + }, |
| 111 | + }); |
| 112 | + expect(order).toEqual(["identity", "permission", "inference", "schemas"]); |
| 113 | + }); |
| 114 | + |
| 115 | + test("a grant scoped to A does not cover the action after switching to B; new grants store under B", async () => { |
| 116 | + const saved: string[] = []; |
| 117 | + spyOn(permissionStore, "saveProviderModelApproval").mockImplementation(async (key: string) => { |
| 118 | + saved.push(key); |
| 119 | + }); |
| 120 | + spyOn(permissionStore, "saveProjectApproval").mockResolvedValue(undefined); |
| 121 | + spyOn(permissionStore, "saveGlobalApproval").mockResolvedValue(undefined); |
| 122 | + |
| 123 | + const session = createProductionSwitch(); |
| 124 | + |
| 125 | + expect((await session.gate.evaluate(shellCall("npm test"))).allowed).toBe(true); |
| 126 | + expect(saved).toEqual([providerModelKey(MODEL_A)]); |
| 127 | + const grantA = session.gate |
| 128 | + .getApprovals() |
| 129 | + .find((a: Approval) => a.providerModel === providerModelKey(MODEL_A)); |
| 130 | + expect(grantA).toBeDefined(); |
| 131 | + |
| 132 | + session.switchTo(MODEL_B); |
| 133 | + |
| 134 | + expect(session.identity()).toEqual(MODEL_B); |
| 135 | + expect(session.inference()).toEqual(MODEL_B); |
| 136 | + |
| 137 | + expect((await session.gate.evaluate(shellCall("npm test"))).allowed).toBe(true); |
| 138 | + expect(saved).toEqual([providerModelKey(MODEL_A), providerModelKey(MODEL_B)]); |
| 139 | + expect( |
| 140 | + session.gate.getApprovals().some((a) => a.providerModel === providerModelKey(MODEL_B)), |
| 141 | + ).toBe(true); |
| 142 | + }); |
| 143 | + |
| 144 | + test("non-kimi to kimi rewrites advertised present; switching away restores canonical", () => { |
| 145 | + const session = createProductionSwitch(); |
| 146 | + expect(schemaHasRef(presentSchema(session.advertised()))).toBe(true); |
| 147 | + expect(presentSchema(session.advertised())).toBe(presentDefinition.inputSchema); |
| 148 | + |
| 149 | + session.switchTo(MODEL_KIMI); |
| 150 | + |
| 151 | + expect(session.inference()).toEqual(MODEL_KIMI); |
| 152 | + expect(schemaHasRef(presentSchema(session.advertised()))).toBe(false); |
| 153 | + expect(presentSchema(session.advertised())).not.toBe(presentDefinition.inputSchema); |
| 154 | + |
| 155 | + session.switchTo(MODEL_A); |
| 156 | + |
| 157 | + expect(session.inference()).toEqual(MODEL_A); |
| 158 | + expect(schemaHasRef(presentSchema(session.advertised()))).toBe(true); |
| 159 | + expect(presentSchema(session.advertised())).toBe(presentDefinition.inputSchema); |
| 160 | + }); |
| 161 | +}); |
0 commit comments