Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from 'vitest'
2
3import { takeIngestEvents } from '../ingest/ingest-signals.ts'
4import {
5 groqModelReasoning,
6 keepValidReasoning,
7 openRouterReasoning,
8 parseByteplusReasoning,
9 parseCohereReasoning,
10 parseGroqPageEfforts,
11 parseGroqReasoning,
12 parseMistralReasoning,
13} from './reasoning-config.ts'
14import { docFixtureHtml } from './fixtures/byteplus-doc.ts'
15import { docsRun } from './model-facts.ts'
16import { reasoningViolation } from './types.ts'
17
18describe('reasoningViolation', () => {
19 it.each([
20 { mode: 'effort', mandatory: true },
21 { mode: 'effort', mandatory: null, efforts: ['low', 'max'] },
22 { mode: 'effort', mandatory: false, efforts: ['none', 'low'] },
23 { mode: 'effort', mandatory: null, efforts: ['none', 'low'] },
24 { mode: 'adaptive', mandatory: false },
25 // One prod budget row carries efforts; that is allowed.
26 { mode: 'budget', mandatory: false, efforts: ['low'] },
27 { mode: 'toggle', mandatory: false },
28 { mode: 'toggle', mandatory: true },
29 ])('accepts %j', (value) => {
30 expect(reasoningViolation(value)).toBeNull()
31 })
32
33 it.each([
34 ['a non-object', 'effort', 'not an object'],
35 ['an unknown mode', { mode: 'switch', mandatory: false }, 'unknown mode'],
36 ['a missing mandatory', { mode: 'effort' }, 'mandatory is not'],
37 ['a string mandatory', { mode: 'effort', mandatory: 'no' }, 'mandatory'],
38 [
39 'a toggle with an unstated mandatory',
40 { mode: 'toggle', mandatory: null },
41 'a toggle needs a stated mandatory',
42 ],
43 [
44 'efforts on a toggle',
45 { mode: 'toggle', mandatory: false, efforts: ['low'] },
46 'a toggle takes no efforts',
47 ],
48 [
49 'empty efforts',
50 { mode: 'effort', mandatory: true, efforts: [] },
51 'efforts',
52 ],
53 [
54 'a non-string effort',
55 { mode: 'effort', mandatory: true, efforts: ['low', 1] },
56 'efforts',
57 ],
58 [
59 'a mandatory true with a none effort',
60 { mode: 'effort', mandatory: true, efforts: ['none', 'low'] },
61 'mandatory is true but efforts include none',
62 ],
63 [
64 'null efforts',
65 { mode: 'effort', mandatory: true, efforts: null },
66 'efforts',
67 ],
68 ])('refuses %s', (_case, value, reason) => {
69 expect(reasoningViolation(value)).toContain(reason)
70 })
71})
72
73describe('keepValidReasoning', () => {
74 const source = { derivation: 'listing' as const, sourceUrl: 'https://x.test' }
75
76 it('passes a valid or absent object through untouched', () => {
77 const valid = {
78 rawId: 'm',
79 reasoning: { mode: 'toggle', mandatory: false },
80 }
81 expect(keepValidReasoning('p', valid as never)).toBe(valid)
82 const none = { rawId: 'm', reasoning: null }
83 expect(keepValidReasoning('p', none)).toBe(none)
84 expect(takeIngestEvents()).toEqual([])
85 })
86
87 it('keeps the prior value and source, and reports the refusal', () => {
88 const prior = {
89 reasoning: { mode: 'effort', mandatory: true },
90 factSources: { reasoning: { ...source, path: 'old' } },
91 }
92 const kept = keepValidReasoning(
93 'prov',
94 {
95 rawId: 'm-1',
96 reasoning: { mode: 'toggle', mandatory: null },
97 factSources: { reasoning: source, contextWindow: source },
98 },
99 prior,
100 )
101 expect(kept.reasoning).toEqual(prior.reasoning)
102 expect(kept.factSources).toEqual({
103 contextWindow: source,
104 reasoning: prior.factSources.reasoning,
105 })
106 expect(takeIngestEvents()).toEqual([
107 {
108 event: 'ingest_failed',
109 properties: {
110 job: 'models-poll',
111 providerId: 'prov',
112 error: 'm-1: reasoning not stored: a toggle needs a stated mandatory',
113 },
114 },
115 ])
116 })
117
118 it('keeps the prior value over a mandatory true with a none effort', () => {
119 const prior = {
120 reasoning: { mode: 'effort', mandatory: false, efforts: ['none', 'low'] },
121 factSources: null,
122 }
123 const kept = keepValidReasoning(
124 'prov',
125 {
126 rawId: 'm-3',
127 reasoning: {
128 mode: 'effort',
129 mandatory: true,
130 efforts: ['none', 'low'],
131 },
132 },
133 prior,
134 )
135 expect(kept.reasoning).toEqual(prior.reasoning)
136 expect(takeIngestEvents()).toEqual([
137 {
138 event: 'ingest_failed',
139 properties: {
140 job: 'models-poll',
141 providerId: 'prov',
142 error:
143 'm-3: reasoning not stored: mandatory is true but efforts include none',
144 },
145 },
146 ])
147 })
148
149 it('stores nothing on a new row', () => {
150 const kept = keepValidReasoning('prov', {
151 rawId: 'm-2',
152 reasoning: { mode: 'toggle', mandatory: null },
153 factSources: { reasoning: source },
154 })
155 expect(kept.reasoning).toBeNull()
156 expect(kept.factSources).toBeUndefined()
157 takeIngestEvents()
158 })
159})
160
161const MISTRAL = `
162# Reasoning
163
164- \`mistral-small-latest\`: Supports adjustable reasoning via the \`reasoning_effort\` parameter. No extra configuration required.
165- \`mistral-medium-3-5\`: Supports adjustable reasoning via the \`reasoning_effort\` parameter.
166- \`zai-glm-5-3\`: Supports adjustable reasoning via the \`reasoning_effort\` parameter. Supported values are \`low\`, \`high\`, and \`max\` (see note below).
167
168- \`reasoning_effort = "high"\`: full thinking chunk.
169- \`reasoning_effort = "none"\`: thinking chunk omitted.
170
171\`zai-glm-5-3\` supports \`low\`, \`high\`, and \`max\` — not \`none\`.
172
173## Handling thinking chunks
174`
175
176const NATIVE = `
177# Native reasoning (deprecated)
178
179Native reasoning models always generate thinking traces without any extra parameters:
180
181- \`magistral-small-latest\`: open smaller version.
182- \`magistral-medium-latest\`: more powerful reasoning model.
183`
184
185const GROQ = `
186# Reasoning
187
188## [Supported Models](#supported-models)
189
190| Model ID | Model |
191| --- | --- |
192| openai/gpt-oss-120b | [OpenAI GPT-OSS 120B](https://example.com/120) |
193| openai/gpt-oss-safeguard-20b | [OpenAI GPT-OSS-Safeguard 20B](https://example.com/safe) |
194| qwen/qwen3.8-27b | [Qwen 3.8 27B](https://example.com/qwen) |
195| minimaxai/minimax-m2.7 | [MiniMax M2.7](https://example.com/mini) |
196
197### Options for Reasoning Effort (Qwen 3.8 27B)
198
199[Qwen 3.8 27B](https://example.com/qwen) supports the following options:
200
201| reasoning_effort Options | Description |
202| --- | --- |
203| none | Disable reasoning. |
204| default | Default behavior. |
205| low | Low effort. |
206| medium | Medium effort. |
207| high | High effort. |
208
209### Options for Reasoning Effort (GPT-OSS)
210
211This is only supported by [GPT-OSS 120B](https://example.com/120).
212
213| reasoning_effort Options | Description |
214| --- | --- |
215| low | Low effort. |
216| medium | Medium effort. |
217| high | High effort. |
218`
219
220const COHERE = `
221# Reasoning
222
223\`\`\`python
224response = co.chat(
225 model="command-a-reasoning-08-2025",
226 messages=[{"role": "user", "content": "Hi"}],
227)
228\`\`\`
229
230thinking={
231 "type": "disabled" # turns off thinking. It is set to "enabled" by default.
232}
233
234thinking = {"token_budget": 500}
235`
236
237const COHERE_SILENT = `
238# Reasoning
239
240model="command-r-08-2024"
241
242Reasoning is on by default. No budget or effort parameter is documented.
243`
244
245// Shaped like https://docs.byteplus.com/en/docs/ModelArk/1449737 on
246// 2026-10-23: the effort table's cells are their own zones, and other
247// tables on the page list ids that are not in it.
248const BYTEPLUS = docFixtureHtml([
249 'Thinking can be switched off with thinking.type for seed-1-6-250615.',
250 '## Adjust chain-of-thought length',
251 'Use the ',
252 'reasoning_effort',
253 ' parameter. Valid values are:',
254 'none',
255 ': Turn off reasoning.',
256 'minimal',
257 'low',
258 'high',
259 'The table below shows the models that support adjusting the chain-of-thought length.',
260 [
261 ['Supported models', 'Default value', 'Compatibility mapping'],
262 ['* dola-seed-2-1-turbo-260628', '', ''],
263 [
264 'seed-2-0-lite-260228 * glm-5-2-260617',
265 'medium',
266 'If none is passed, reasoning is turned off.',
267 ],
268 ],
269 'Supported models:',
270 'deepseek-v4-flash-260425',
271 [
272 ['API', 'Chat API'],
273 ['Supported models', 'deepseek-v4-pro-260425 deepseek-v4-flash-260425'],
274 ],
275 '## Pass thinking content back',
276 'seed-1-8-251228',
277])
278
279describe('openRouterReasoning', () => {
280 it('copies published efforts and does not invent a mode', () => {
281 expect(
282 openRouterReasoning({
283 reasoning: {
284 supported_efforts: ['high', 'low', null],
285 mandatory: false,
286 },
287 }),
288 ).toEqual({
289 mode: 'effort',
290 mandatory: false,
291 efforts: ['high', 'low'],
292 })
293 expect(
294 openRouterReasoning({
295 reasoning: { supported_efforts: null, mandatory: true },
296 }),
297 ).toEqual({ mode: 'effort', mandatory: true })
298 expect(
299 openRouterReasoning({
300 reasoning: { supports_max_tokens: true, mandatory: true },
301 }),
302 ).toEqual({ mode: 'budget', mandatory: true })
303 expect(
304 openRouterReasoning({
305 reasoning: { mandatory: true },
306 }),
307 ).toBeNull()
308 expect(
309 openRouterReasoning({
310 reasoning: null,
311 }),
312 ).toBeNull()
313 })
314})
315
316describe('parseMistralReasoning', () => {
317 it('reads effort names the guide states, and leaves native models unset', () => {
318 const parsed = parseMistralReasoning(MISTRAL)
319 expect(parsed.get('mistral-small-latest')).toEqual({
320 mode: 'effort',
321 mandatory: false,
322 })
323 expect(parsed.get('mistral-medium-3-5')).toEqual({
324 mode: 'effort',
325 mandatory: false,
326 })
327 expect(parsed.get('mistral-medium-3-5')?.mode).toBe('effort')
328 expect(parsed.get('zai-glm-5-3')).toEqual({
329 mode: 'effort',
330 mandatory: null,
331 efforts: ['low', 'high', 'max'],
332 })
333 expect(parseMistralReasoning(NATIVE).get('magistral-medium-latest')).toBe(
334 undefined,
335 )
336 })
337})
338
339describe('parseGroqReasoning', () => {
340 it('reads each effort table and leaves models with no options unset', () => {
341 const parsed = parseGroqReasoning(GROQ)
342 expect(parsed.get('qwen/qwen3.8-27b')).toEqual({
343 mode: 'effort',
344 mandatory: false,
345 efforts: ['none', 'default', 'low', 'medium', 'high'],
346 })
347 expect(parsed.get('openai/gpt-oss-120b')).toEqual({
348 mode: 'effort',
349 mandatory: true,
350 efforts: ['low', 'medium', 'high'],
351 })
352 expect(parsed.get('openai/gpt-oss-safeguard-20b')).toBeUndefined()
353 expect(parsed.get('minimaxai/minimax-m2.7')).toBeUndefined()
354 })
355})
356
357describe('parseCohereReasoning', () => {
358 it('uses token_budget as budget mode and stays null without that knob', () => {
359 expect(
360 parseCohereReasoning(COHERE).get('command-a-reasoning-08-2025'),
361 ).toEqual({ mode: 'budget', mandatory: false })
362 expect(
363 parseCohereReasoning(COHERE_SILENT).get('command-r-08-2024'),
364 ).toBeUndefined()
365 })
366
367 it('covers every reasoning model only while the guide calls them hybrid', () => {
368 // The sentence as https://docs.cohere.com/docs/reasoning.md had it on
369 // 2026-10-07.
370 const hybrid = `Cohere's reasoning models are *hybrid*, meaning reasoning can be enabled (in which case they generate internal reasoning processes before delivering their final responses) or disabled (in which case they function the way any other LLM would).`
371 expect(parseCohereReasoning(COHERE).has('*')).toBe(false)
372 expect(parseCohereReasoning(`${hybrid}\n${COHERE}`).get('*')).toEqual({
373 mode: 'budget',
374 mandatory: false,
375 })
376 expect(
377 parseCohereReasoning(
378 `${hybrid.replace('or disabled', 'but not disabled')}\n${COHERE}`,
379 ).has('*'),
380 ).toBe(false)
381 // Without the budget knob the sentence configures nothing.
382 expect(parseCohereReasoning(`${hybrid}\n${COHERE_SILENT}`).size).toBe(0)
383 })
384})
385
386describe('parseByteplusReasoning', () => {
387 it('copies the published effort list onto supported-model ids only', () => {
388 const parsed = parseByteplusReasoning(BYTEPLUS)
389 expect(parsed.get('seed-2-0-lite-260228')).toEqual({
390 mode: 'effort',
391 mandatory: false,
392 efforts: ['none', 'minimal', 'low', 'high'],
393 })
394 expect(parsed.get('dola-seed-2-1-turbo-260628')?.mode).toBe('effort')
395 expect(parsed.get('glm-5-2-260617')?.efforts).toEqual([
396 'none',
397 'minimal',
398 'low',
399 'high',
400 ])
401 expect(parsed.get('seed-1-6-250615')).toBeUndefined()
402 expect(parsed.get('deepseek-v4-flash-260425')).toBeUndefined()
403 expect(parsed.get('seed-1-8-251228')).toBeUndefined()
404 expect(parsed.size).toBe(3)
405 })
406
407 it('configures nothing when the effort table is gone', () => {
408 expect(
409 parseByteplusReasoning(
410 docFixtureHtml(['## Adjust chain-of-thought length', 'none', 'high']),
411 ).size,
412 ).toBe(0)
413 })
414
415 it('takes effort names only from the list before the table', () => {
416 const parsed = parseByteplusReasoning(
417 docFixtureHtml([
418 '## Adjust chain-of-thought length',
419 'high',
420 [['Supported models'], ['seed-2-0-lite-260228']],
421 'max',
422 ]),
423 )
424 expect(parsed.get('seed-2-0-lite-260228')?.efforts).toEqual(['high'])
425 })
426})
427
428describe('parseGroqPageEfforts', () => {
429 it('reads the model page sentence and fails closed when it changes', () => {
430 // openai/gpt-oss-safeguard-20b.md on 2026-10-23.
431 const line =
432 '* Harmony format for structured reasoning with low/medium/high reasoning effort'
433 expect(parseGroqPageEfforts(line)).toEqual(['low', 'medium', 'high'])
434 expect(parseGroqPageEfforts('Supports configurable reasoning.')).toBeNull()
435 expect(() => parseGroqPageEfforts(line.replace('medium', 'turbo'))).toThrow(
436 /unreadable reasoning effort/,
437 )
438 })
439})
440
441describe('groqModelReasoning', () => {
442 afterEach(() => vi.unstubAllGlobals())
443
444 const GUIDE = `
445## [Supported Models](#supported-models)
446
447| Model ID | Model |
448| --- | --- |
449| openai/gpt-oss-20b | [GPT-OSS 20B](https://console.groq.com/docs/model/openai/gpt-oss-20b) |
450| openai/gpt-oss-safeguard-20b | [GPT-OSS-Safeguard 20B](https://console.groq.com/docs/model/openai/gpt-oss-safeguard-20b) |
451| minimaxai/minimax-m2.7 | [MiniMax M2.7](https://console.groq.com/docs/model/minimaxai/minimax-m2.7) |
452
453### [Options for Reasoning Effort (GPT-OSS)](#gpt-oss)
454
455Only supported by [GPT-OSS 20B](https://console.groq.com/docs/model/openai/gpt-oss-20b).
456
457| Option | Description |
458| --- | --- |
459| low | Low effort |
460| high | High effort |
461`
462
463 function serve(pages: Record<string, string | null>) {
464 vi.stubGlobal(
465 'fetch',
466 vi.fn((url: string) => {
467 const body = url.endsWith('/reasoning.md') ? GUIDE : pages[url]
468 return Promise.resolve(
469 body == null
470 ? new Response('down', { status: 503 })
471 : new Response(body),
472 )
473 }),
474 )
475 }
476 const page = (id: string) => `https://console.groq.com/docs/model/${id}.md`
477 const ids = [
478 'openai/gpt-oss-20b',
479 'openai/gpt-oss-safeguard-20b',
480 'minimaxai/minimax-m2.7',
481 ]
482
483 it('reads efforts from the model page of a guide model with no options', async () => {
484 serve({
485 [page('openai/gpt-oss-safeguard-20b')]:
486 'Harmony format with low/medium/high reasoning effort',
487 [page('minimaxai/minimax-m2.7')]: 'A reasoning model.',
488 })
489 const run = docsRun()
490 const reasoning = await groqModelReasoning(undefined, run, ids)
491 expect(reasoning('openai/gpt-oss-safeguard-20b', true)).toEqual({
492 reasoning: {
493 mode: 'effort',
494 mandatory: null,
495 efforts: ['low', 'medium', 'high'],
496 },
497 factSources: {
498 reasoning: expect.objectContaining({
499 sourceUrl: page('openai/gpt-oss-safeguard-20b'),
500 path: 'reasoning',
501 }) as unknown,
502 },
503 })
504 expect(reasoning('minimaxai/minimax-m2.7', true).reasoning).toBeUndefined()
505 expect(reasoning('openai/gpt-oss-20b', true).reasoning?.efforts).toEqual([
506 'low',
507 'high',
508 ])
509 expect(run.failed).toBe(0)
510 })
511
512 it('keeps stored reasoning when a model page fails to load', async () => {
513 serve({ [page('minimaxai/minimax-m2.7')]: 'A reasoning model.' })
514 const run = docsRun()
515 const reasoning = await groqModelReasoning(undefined, run, ids)
516 expect(reasoning('openai/gpt-oss-safeguard-20b', true)).toEqual({
517 absent: { reasoning: 'unavailable' },
518 })
519 expect(run.failed).toBe(1)
520 })
521
522 it('keeps stored reasoning when the guide fails to load', async () => {
523 vi.stubGlobal(
524 'fetch',
525 vi.fn(() => Promise.resolve(new Response('down', { status: 503 }))),
526 )
527 const run = docsRun()
528 const reasoning = await groqModelReasoning(undefined, run, ids)
529 expect(reasoning('openai/gpt-oss-20b', true)).toEqual({
530 absent: { reasoning: 'unavailable' },
531 })
532 expect(run.failed).toBe(1)
533 })
534})