|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { SyncDescriptor } from '#/_base/di/descriptors'; |
| 4 | +import { DisposableStore } from '#/_base/di/lifecycle'; |
| 5 | +import { TestInstantiationService } from '#/_base/di/test'; |
| 6 | +import { IConfigService } from '#/app/config/config'; |
| 7 | +import { THINKING_SECTION } from '#/app/kosongConfig/configSection'; |
| 8 | +import { ErrorCodes, Error2, isError2 } from '#/errors'; |
| 9 | +import { UNKNOWN_CAPABILITY } from '#/llm-adapter/contract/capability'; |
| 10 | +import { IModelCatalog, type Model } from '#/llm-adapter/model/catalog'; |
| 11 | +import { SECONDARY_MODEL_SECTION } from '#/session/subagent/policy'; |
| 12 | +import { |
| 13 | + ISubagentModelPolicyService, |
| 14 | + type SubagentModelPolicySnapshot, |
| 15 | +} from '#/session/subagent/subagentModelPolicy'; |
| 16 | +import { SubagentModelPolicyService } from '#/session/subagent/subagentModelPolicyService'; |
| 17 | + |
| 18 | +import { StubConfigService } from '../../stubs'; |
| 19 | + |
| 20 | +function testModel(id: string, extra: Partial<Model> = {}): Model { |
| 21 | + return { |
| 22 | + id, |
| 23 | + name: id, |
| 24 | + aliases: [], |
| 25 | + protocol: 'openai', |
| 26 | + headers: {}, |
| 27 | + capabilities: UNKNOWN_CAPABILITY, |
| 28 | + maxContextSize: 128_000, |
| 29 | + alwaysThinking: false, |
| 30 | + providerName: 'acme', |
| 31 | + ...extra, |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +describe('SubagentModelPolicyService', () => { |
| 36 | + let disposables: DisposableStore; |
| 37 | + let ix: TestInstantiationService; |
| 38 | + let config: StubConfigService; |
| 39 | + let models: Map<string, Model>; |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + disposables = new DisposableStore(); |
| 43 | + ix = disposables.add(new TestInstantiationService()); |
| 44 | + models = new Map([ |
| 45 | + ['acme/sol', testModel('acme/sol', { supportEfforts: ['low', 'high'], defaultEffort: 'high' })], |
| 46 | + ['acme/luna', testModel('acme/luna', { supportEfforts: ['low', 'high'] })], |
| 47 | + ]); |
| 48 | + }); |
| 49 | + afterEach(() => disposables.dispose()); |
| 50 | + |
| 51 | + function setup(sections: Record<string, unknown>): ISubagentModelPolicyService { |
| 52 | + config = new StubConfigService(sections); |
| 53 | + ix.stub(IConfigService, config); |
| 54 | + ix.stub(IModelCatalog, { |
| 55 | + _serviceBrand: undefined, |
| 56 | + get: (id: string) => { |
| 57 | + const model = models.get(id); |
| 58 | + if (model === undefined) { |
| 59 | + throw new Error2(ErrorCodes.CONFIG_INVALID, `Model "${id}" is not configured in config.toml.`, { |
| 60 | + details: { model: id }, |
| 61 | + }); |
| 62 | + } |
| 63 | + return model; |
| 64 | + }, |
| 65 | + }); |
| 66 | + ix.set(ISubagentModelPolicyService, new SyncDescriptor(SubagentModelPolicyService)); |
| 67 | + return ix.get(ISubagentModelPolicyService); |
| 68 | + } |
| 69 | + |
| 70 | + async function codeOf(fn: () => Promise<unknown> | unknown): Promise<string | undefined> { |
| 71 | + try { |
| 72 | + await fn(); |
| 73 | + return undefined; |
| 74 | + } catch (error) { |
| 75 | + return isError2(error) ? error.code : 'not-error2'; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + it('reads the persisted policy with a resource version and reports inherit for an absent section', () => { |
| 80 | + const service = setup({}); |
| 81 | + const snapshot = service.get(); |
| 82 | + expect(snapshot.policy).toEqual({ mode: 'inherit' }); |
| 83 | + expect(snapshot.resourceVersion).toMatch(/^subagent-policy-v1:/); |
| 84 | + }); |
| 85 | + |
| 86 | + it('set validates against the live catalog and persists the canonical section', async () => { |
| 87 | + const service = setup({}); |
| 88 | + await expect( |
| 89 | + service.set({ mode: 'default', defaultModel: 'acme/nope' }), |
| 90 | + ).rejects.toMatchObject({ code: ErrorCodes.CONFIG_INVALID }); |
| 91 | + expect(config.get(SECONDARY_MODEL_SECTION)).toBeUndefined(); |
| 92 | + |
| 93 | + const snapshot = await service.set({ mode: 'force', defaultModel: 'acme/sol', defaultEffort: 'low' }); |
| 94 | + expect(snapshot.policy).toEqual({ mode: 'force', defaultModel: 'acme/sol', defaultEffort: 'low' }); |
| 95 | + expect(config.get(SECONDARY_MODEL_SECTION)).toEqual({ |
| 96 | + defaultModel: 'acme/sol', |
| 97 | + force: true, |
| 98 | + defaultEffort: 'low', |
| 99 | + }); |
| 100 | + |
| 101 | + await service.set({ mode: 'default', defaultModel: 'acme/luna' }); |
| 102 | + expect(config.get(SECONDARY_MODEL_SECTION)).toEqual({ |
| 103 | + defaultModel: 'acme/luna', |
| 104 | + defaultEffort: undefined, |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('clear removes the section and expectedVersion guards both set and clear', async () => { |
| 109 | + const service = setup({ [SECONDARY_MODEL_SECTION]: { defaultModel: 'acme/sol' } }); |
| 110 | + const before = service.get().resourceVersion; |
| 111 | + expect(await codeOf(() => service.set({ mode: 'inherit' }, 'subagent-policy-v1:stale'))).toBe( |
| 112 | + ErrorCodes.CONFIG_VERSION_CONFLICT, |
| 113 | + ); |
| 114 | + expect(config.get(SECONDARY_MODEL_SECTION)).toEqual({ defaultModel: 'acme/sol' }); |
| 115 | + const cleared = await service.clear(before); |
| 116 | + expect(cleared.policy).toEqual({ mode: 'inherit' }); |
| 117 | + expect(config.get(SECONDARY_MODEL_SECTION)).toBeUndefined(); |
| 118 | + expect(cleared.resourceVersion).not.toBe(before); |
| 119 | + expect(await codeOf(() => service.clear(before))).toBe(ErrorCodes.CONFIG_VERSION_CONFLICT); |
| 120 | + }); |
| 121 | + |
| 122 | + it('serializes concurrent commits so a stale expectedVersion cannot slip past the version check', async () => { |
| 123 | + const service = setup({ [SECONDARY_MODEL_SECTION]: { defaultModel: 'acme/sol' } }); |
| 124 | + const replace = config.replace.bind(config); |
| 125 | + config.replace = async (domain, value) => { |
| 126 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 127 | + await replace(domain, value); |
| 128 | + }; |
| 129 | + const version = service.get().resourceVersion; |
| 130 | + const [first, second] = await Promise.allSettled([ |
| 131 | + service.set({ mode: 'default', defaultModel: 'acme/luna' }, version), |
| 132 | + service.set({ mode: 'force', defaultModel: 'acme/sol' }, version), |
| 133 | + ]); |
| 134 | + expect(first.status).toBe('fulfilled'); |
| 135 | + expect(second.status).toBe('rejected'); |
| 136 | + expect((second as PromiseRejectedResult).reason).toMatchObject({ |
| 137 | + code: ErrorCodes.CONFIG_VERSION_CONFLICT, |
| 138 | + }); |
| 139 | + expect(config.get(SECONDARY_MODEL_SECTION)).toEqual({ defaultModel: 'acme/luna', defaultEffort: undefined }); |
| 140 | + const recovered = await service.set( |
| 141 | + { mode: 'force', defaultModel: 'acme/sol' }, |
| 142 | + (first as PromiseFulfilledResult<SubagentModelPolicySnapshot>).value.resourceVersion, |
| 143 | + ); |
| 144 | + expect(recovered.policy).toEqual({ mode: 'force', defaultModel: 'acme/sol' }); |
| 145 | + expect(config.get(SECONDARY_MODEL_SECTION)).toEqual({ defaultModel: 'acme/sol', force: true, defaultEffort: undefined }); |
| 146 | + }); |
| 147 | + |
| 148 | + it('prepareLegacyMutation validates against the supplied prospective context, not the live catalog', () => { |
| 149 | + const service = setup({}); |
| 150 | + const prospective = { |
| 151 | + resolveModel: (alias: string) => (alias === 'acme/future' ? { id: alias } : undefined), |
| 152 | + }; |
| 153 | + const prepared = service.prepareLegacyMutation( |
| 154 | + { defaultModel: 'acme/future', models: { 'acme/future': 'soon' } }, |
| 155 | + prospective, |
| 156 | + ); |
| 157 | + expect(prepared.policy).toEqual({ |
| 158 | + mode: 'pool', |
| 159 | + defaultModel: 'acme/future', |
| 160 | + models: { 'acme/future': 'soon' }, |
| 161 | + defaultEffort: undefined, |
| 162 | + }); |
| 163 | + expect(prepared.section).toEqual({ |
| 164 | + defaultModel: 'acme/future', |
| 165 | + models: { 'acme/future': 'soon' }, |
| 166 | + defaultEffort: undefined, |
| 167 | + }); |
| 168 | + expect(config.get(SECONDARY_MODEL_SECTION)).toBeUndefined(); |
| 169 | + expect(() => service.prepareLegacyMutation({ defaultModel: 'acme/sol' }, prospective)).toThrow(); |
| 170 | + expect(() => service.prepareLegacyMutation({ defaultModel: 'acme/sol' })).not.toThrow(); |
| 171 | + expect(service.prepareLegacyMutation(null)).toEqual({ policy: { mode: 'inherit' }, section: undefined }); |
| 172 | + expect(() => service.prepareLegacyMutation({ defaultModel: 42 })).toThrow(); |
| 173 | + }); |
| 174 | + |
| 175 | + it('resolveRevision changes only with ambient routing inputs', async () => { |
| 176 | + const service = setup({ [SECONDARY_MODEL_SECTION]: { defaultModel: 'acme/luna' } }); |
| 177 | + const caller = { modelAlias: 'acme/sol', thinkingLevel: 'high' }; |
| 178 | + const a = service.resolveRevision(caller); |
| 179 | + expect(a).toMatch(/^route-env:v1:/); |
| 180 | + expect(service.resolveRevision({ ...caller })).toBe(a); |
| 181 | + expect(service.resolveRevision({ modelAlias: 'acme/luna', thinkingLevel: 'high' })).not.toBe(a); |
| 182 | + await config.replace(THINKING_SECTION, { enabled: false }); |
| 183 | + expect(service.resolveRevision(caller)).not.toBe(a); |
| 184 | + }); |
| 185 | +}); |
0 commit comments