File size: 14,256 Bytes
f0634fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import { afterEach, beforeEach, describe, expect, it } from 'vitest';

import { SyncDescriptor } from '#/_base/di/descriptors';
import { DisposableStore } from '#/_base/di/lifecycle';
import { TestInstantiationService } from '#/_base/di/test';
import { IConfigService } from '#/app/config/config';
import { ErrorCodes, Error2, isError2 } from '#/errors';
import { IModelCatalog, type Model } from '#/llm-adapter/model/catalog';
import {
  SECONDARY_MODEL_SECTION,
  SUBAGENT_SECTION,
} from '#/session/subagent/configSection';
import { ISessionSubagentModelsValidationService } from '#/session/subagent/subagentModelsValidation';
import { SessionSubagentModelsValidationService } from '#/session/subagent/subagentModelsValidationService';

import { StubConfigService } from '../../stubs';

describe('SessionSubagentModelsValidationService', () => {
  let disposables: DisposableStore;
  let ix: TestInstantiationService;
  let modelIds: Set<string>;
  let modelMeta: Map<string, Record<string, unknown>>;

  beforeEach(() => {
    disposables = new DisposableStore();
    ix = disposables.add(new TestInstantiationService());
    modelIds = new Set();
    modelMeta = new Map();
  });
  afterEach(() => {
    disposables.dispose();
  });

  function setup(configValues: Record<string, unknown>): void {
    ix.stub(IConfigService, new StubConfigService(configValues));
    ix.stub(IModelCatalog, {
      _serviceBrand: undefined,
      get: (id: string) => {
        if (!modelIds.has(id)) {
          throw new Error2(
            ErrorCodes.CONFIG_INVALID,
            `Model "${id}" is not configured in config.toml.`,
            { details: { model: id } },
          );
        }
        return { id, ...modelMeta.get(id) } as Model;
      },
    } as unknown as IModelCatalog);
    ix.set(
      ISessionSubagentModelsValidationService,
      new SyncDescriptor(SessionSubagentModelsValidationService),
    );
  }

  function resolve(): unknown {
    try {
      ix.get(ISessionSubagentModelsValidationService);
      return undefined;
    } catch (error) {
      return error;
    }
  }

  it('is a no-op when no secondary_model section is configured', () => {
    setup({});
    expect(resolve()).toBeUndefined();
  });

  it('is a no-op when only the [subagent] timeout is configured', () => {
    setup({ [SUBAGENT_SECTION]: { timeoutMs: 5000 } });
    expect(resolve()).toBeUndefined();
  });

  it('constructs fine when default_model alone forms an implicit single-entry pool', () => {
    modelIds.add('provider/fast');
    setup({ [SECONDARY_MODEL_SECTION]: { defaultModel: 'provider/fast' } });
    expect(resolve()).toBeUndefined();
  });

  it('constructs fine when the legacy model key alone forms the fallback pool', () => {
    modelIds.add('provider/fast');
    setup({ [SECONDARY_MODEL_SECTION]: { model: 'provider/fast' } });
    expect(resolve()).toBeUndefined();
  });

  it('fails session creation when the legacy model fallback does not resolve', () => {
    setup({ [SECONDARY_MODEL_SECTION]: { model: 'provider/typo' } });
    const error = resolve();
    expect(isError2(error)).toBe(true);
    expect((error as Error2).code).toBe(ErrorCodes.CONFIG_INVALID);
    expect((error as Error2).message).toContain(
      '[secondary_model.models] entry "provider/typo" could not be resolved',
    );
  });

  it('constructs fine when force pins the legacy model fallback', () => {
    modelIds.add('provider/fast');
    setup({ [SECONDARY_MODEL_SECTION]: { model: 'provider/fast', force: true } });
    expect(resolve()).toBeUndefined();
  });

  it('fails session creation when a pool table relies on the legacy model key for its default', () => {
    modelIds.add('provider/fast');
    setup({
      [SECONDARY_MODEL_SECTION]: {
        model: 'provider/fast',
        models: { 'provider/fast': 'fast and cheap' },
      },
    });
    const error = resolve();
    expect(isError2(error)).toBe(true);
    expect((error as Error2).code).toBe(ErrorCodes.CONFIG_INVALID);
    expect((error as Error2).message).toContain(
      '[secondary_model].default_model is required when [secondary_model.models] is configured',
    );
  });

  it('fails session creation when a pool-less default_model does not resolve', () => {
    setup({ [SECONDARY_MODEL_SECTION]: { defaultModel: 'provider/typo' } });
    const error = resolve();
    expect(isError2(error)).toBe(true);
    expect((error as Error2).code).toBe(ErrorCodes.CONFIG_INVALID);
    expect((error as Error2).message).toContain(
      '[secondary_model.models] entry "provider/typo" could not be resolved',
    );
  });

  it('constructs fine for a valid pool', () => {
    modelIds.add('provider/fast').add('provider/smart');
    setup({
      [SECONDARY_MODEL_SECTION]: {
        defaultModel: 'provider/fast',
        models: { 'provider/fast': 'fast and cheap', 'provider/smart': 'hard tasks' },
      },
    });
    expect(resolve()).toBeUndefined();
  });

  it('fails session creation when the pool has no default_model', () => {
    modelIds.add('provider/fast');
    setup({ [SECONDARY_MODEL_SECTION]: { models: { 'provider/fast': 'fast and cheap' } } });
    const error = resolve();
    expect(isError2(error)).toBe(true);
    expect((error as Error2).code).toBe(ErrorCodes.CONFIG_INVALID);
    expect((error as Error2).message).toContain(
      '[secondary_model].default_model is required when [secondary_model.models] is configured',
    );
  });

  it('fails session creation when default_model is not a pool key, listing the pool', () => {
    modelIds.add('provider/fast').add('provider/smart');
    setup({
      [SECONDARY_MODEL_SECTION]: {
        defaultModel: 'provider/typo',
        models: { 'provider/fast': 'fast and cheap', 'provider/smart': 'hard tasks' },
      },
    });
    const error = resolve();
    expect(isError2(error)).toBe(true);
    expect((error as Error2).code).toBe(ErrorCodes.CONFIG_INVALID);
    expect((error as Error2).message).toContain('"provider/typo"');
    expect((error as Error2).message).toContain(
      'Available models: provider/fast, provider/smart.',
    );
  });

  it('fails session creation when a pool key uses the reserved "primary" alias', () => {
    modelIds.add('primary').add('provider/fast');
    setup({
      [SECONDARY_MODEL_SECTION]: {
        defaultModel: 'provider/fast',
        models: { primary: 'looks like a model', 'provider/fast': 'fast and cheap' },
      },
    });
    const error = resolve();
    expect(isError2(error)).toBe(true);
    expect((error as Error2).code).toBe(ErrorCodes.CONFIG_INVALID);
    expect((error as Error2).message).toContain(
      '[secondary_model.models] key "primary" is reserved',
    );
  });

  it('fails session creation when a pool key does not resolve, naming the key', () => {
    modelIds.add('provider/fast');
    setup({
      [SECONDARY_MODEL_SECTION]: {
        defaultModel: 'provider/fast',
        models: { 'provider/fast': 'fast and cheap', 'provider/typo': 'hard tasks' },
      },
    });
    const error = resolve();
    expect(isError2(error)).toBe(true);
    expect((error as Error2).code).toBe(ErrorCodes.CONFIG_INVALID);
    expect((error as Error2).message).toContain(
      '[secondary_model.models] entry "provider/typo" could not be resolved',
    );
    expect((error as Error2).message).toContain('"provider/typo" is not configured');
    expect(isError2((error as Error2).cause)).toBe(true);
  });

  it('constructs fine when force pins a resolvable default_model', () => {
    modelIds.add('provider/fast');
    setup({ [SECONDARY_MODEL_SECTION]: { defaultModel: 'provider/fast', force: true } });
    expect(resolve()).toBeUndefined();
  });

  it('fails session creation when force is set without default_model', () => {
    setup({ [SECONDARY_MODEL_SECTION]: { force: true } });
    const error = resolve();
    expect(isError2(error)).toBe(true);
    expect((error as Error2).code).toBe(ErrorCodes.CONFIG_INVALID);
    expect((error as Error2).message).toContain(
      '[secondary_model].default_model is required when [secondary_model].force is set',
    );
  });

  it('fails session creation when force is combined with a models table', () => {
    modelIds.add('provider/fast');
    setup({
      [SECONDARY_MODEL_SECTION]: {
        defaultModel: 'provider/fast',
        models: { 'provider/fast': 'fast and cheap' },
        force: true,
      },
    });
    const error = resolve();
    expect(isError2(error)).toBe(true);
    expect((error as Error2).code).toBe(ErrorCodes.CONFIG_INVALID);
    expect((error as Error2).message).toContain(
      '[secondary_model].force cannot be combined with [secondary_model.models]',
    );
  });

  it('fails session creation when the forced default_model does not resolve', () => {
    setup({ [SECONDARY_MODEL_SECTION]: { defaultModel: 'provider/typo', force: true } });
    const error = resolve();
    expect(isError2(error)).toBe(true);
    expect((error as Error2).code).toBe(ErrorCodes.CONFIG_INVALID);
    expect((error as Error2).message).toContain('"provider/typo"');
  });

  it('fails session creation when default_effort is not supported by a pool model', () => {
    modelIds.add('provider/fast').add('provider/smart');
    modelMeta.set('provider/fast', {
      capabilities: { thinking: true },
      supportEfforts: ['low', 'high', 'max'],
    });
    modelMeta.set('provider/smart', {
      capabilities: { thinking: true },
      supportEfforts: ['low', 'medium', 'high', 'xhigh', 'max', 'ultra'],
    });
    setup({
      [SECONDARY_MODEL_SECTION]: {
        defaultModel: 'provider/smart',
        models: { 'provider/fast': 'fast and cheap', 'provider/smart': 'hard tasks' },
        defaultEffort: 'xhigh',
      },
    });
    const error = resolve();
    expect(isError2(error)).toBe(true);
    expect((error as Error2).code).toBe(ErrorCodes.CONFIG_INVALID);
    expect((error as Error2).message).toContain('[secondary_model].default_effort "xhigh"');
    expect((error as Error2).message).toContain('"provider/fast"');
    expect((error as Error2).message).toContain('low, high, max');
  });

  it('constructs fine when every pool model supports default_effort', () => {
    modelIds.add('provider/fast').add('provider/smart');
    modelMeta.set('provider/fast', {
      capabilities: { thinking: true },
      supportEfforts: ['low', 'high', 'xhigh', 'max'],
    });
    modelMeta.set('provider/smart', {
      capabilities: { thinking: true },
      supportEfforts: ['low', 'medium', 'high', 'xhigh', 'max', 'ultra'],
    });
    setup({
      [SECONDARY_MODEL_SECTION]: {
        defaultModel: 'provider/fast',
        models: { 'provider/fast': 'fast and cheap', 'provider/smart': 'hard tasks' },
        defaultEffort: 'xhigh',
      },
    });
    expect(resolve()).toBeUndefined();
  });

  it('fails session creation when the forced model does not support default_effort', () => {
    modelIds.add('provider/fast');
    modelMeta.set('provider/fast', {
      capabilities: { thinking: true },
      supportEfforts: ['low', 'high', 'max'],
    });
    setup({
      [SECONDARY_MODEL_SECTION]: {
        defaultModel: 'provider/fast',
        force: true,
        defaultEffort: 'xhigh',
      },
    });
    const error = resolve();
    expect(isError2(error)).toBe(true);
    expect((error as Error2).code).toBe(ErrorCodes.CONFIG_INVALID);
    expect((error as Error2).message).toContain('not supported by model "provider/fast"');
  });

  it('constructs fine when a pool model declares no effort list', () => {
    modelIds.add('provider/fast');
    modelMeta.set('provider/fast', { capabilities: { thinking: true } });
    setup({
      [SECONDARY_MODEL_SECTION]: {
        defaultModel: 'provider/fast',
        models: { 'provider/fast': 'fast and cheap' },
        defaultEffort: 'xhigh',
      },
    });
    expect(resolve()).toBeUndefined();
  });

  it('constructs fine for an adaptive-thinking pool model without the thinking capability', () => {
    modelIds.add('provider/adaptive');
    modelMeta.set('provider/adaptive', {
      capabilities: { thinking: false },
      adaptiveThinking: true,
    });
    setup({
      [SECONDARY_MODEL_SECTION]: {
        defaultModel: 'provider/adaptive',
        models: { 'provider/adaptive': 'adaptive thinking' },
        defaultEffort: 'high',
      },
    });
    expect(resolve()).toBeUndefined();
  });

  it('fails session creation when a pool model does not support thinking', () => {
    modelIds.add('provider/plain');
    modelMeta.set('provider/plain', { capabilities: { thinking: false } });
    setup({
      [SECONDARY_MODEL_SECTION]: {
        defaultModel: 'provider/plain',
        models: { 'provider/plain': 'no thinking' },
        defaultEffort: 'xhigh',
      },
    });
    const error = resolve();
    expect(isError2(error)).toBe(true);
    expect((error as Error2).code).toBe(ErrorCodes.CONFIG_INVALID);
    expect((error as Error2).message).toContain(
      'model "provider/plain" does not support thinking',
    );
  });

  it('constructs fine when default_effort is off even for a non-thinking pool model', () => {
    modelIds.add('provider/plain');
    modelMeta.set('provider/plain', { capabilities: { thinking: false } });
    setup({
      [SECONDARY_MODEL_SECTION]: {
        defaultModel: 'provider/plain',
        models: { 'provider/plain': 'no thinking' },
        defaultEffort: 'off',
      },
    });
    expect(resolve()).toBeUndefined();
  });

  it('fails session creation when default_effort is off but a pool model always thinks', () => {
    modelIds.add('provider/always');
    modelMeta.set('provider/always', {
      capabilities: { thinking: true },
      alwaysThinking: true,
      supportEfforts: ['low', 'high', 'max'],
    });
    setup({
      [SECONDARY_MODEL_SECTION]: {
        defaultModel: 'provider/always',
        models: { 'provider/always': 'always reasoning' },
        defaultEffort: 'off',
      },
    });
    const error = resolve();
    expect(isError2(error)).toBe(true);
    expect((error as Error2).code).toBe(ErrorCodes.CONFIG_INVALID);
    expect((error as Error2).message).toContain('[secondary_model].default_effort "off"');
    expect((error as Error2).message).toContain('"provider/always"');
  });
});