Open History to see the last commit
1import { afterEach, describe, expect, it } from 'vitest'
2
3import { bedrockCardRuntimeIds } from './bedrock-cards.ts'
4import native from './fixtures/bedrock-native-thinking-guides.json'
5import {
6 applyBedrockThinkingGuides,
7 BEDROCK_ADAPTIVE_THINKING_URL,
8 BEDROCK_EXTENDED_THINKING_URL,
9 BEDROCK_NOVA_THINKING_URL,
10 loadBedrockThinkingGuides,
11 parseBedrockAdaptiveThinking,
12 parseBedrockExtendedThinking,
13 parseBedrockNovaThinking,
14} from './bedrock-thinking-guides.ts'
15import { docsReport, docsRun } from './model-facts.ts'
16import type { ModelInfo, ModelReasoning } from './types.ts'
17
18const extended = native[BEDROCK_EXTENDED_THINKING_URL as keyof typeof native]
19const adaptive = native[BEDROCK_ADAPTIVE_THINKING_URL as keyof typeof native]
20const row = (rawId: string): ModelInfo => ({
21 rawId,
22 activity: 'chat',
23 reasoning: null,
24})
25const originalFetch = globalThis.fetch
26afterEach(() => {
27 globalThis.fetch = originalFetch
28})
29
30describe('AWS native thinking guides', () => {
31 it('parses the exact hosted Nova name and normative effort/disabled declarations without using examples', () => {
32 const nova = native[BEDROCK_NOVA_THINKING_URL as keyof typeof native]
33 expect(parseBedrockNovaThinking(nova)).toEqual({
34 modelName: 'Nova 2 Lite',
35 reasoning: {
36 mode: 'effort',
37 mandatory: false,
38 efforts: ['low', 'medium', 'high'],
39 },
40 })
41 expect(
42 parseBedrockNovaThinking(
43 nova +
44 '\n```\nFuture Nova supports extended thinking for complex problem-solving. Enable reasoning with `reasoningConfig`.\n```',
45 ),
46 ).toEqual(parseBedrockNovaThinking(nova))
47 expect(() =>
48 parseBedrockNovaThinking(
49 nova.replace('+ `type`: `enabled` or `disabled`', '+ `type`: unknown'),
50 ),
51 ).toThrow('type or effort')
52 })
53 it('joins Nova only by its exact native card title and retains only explicitly declared profile rows', async () => {
54 globalThis.fetch = ((input: string) =>
55 Promise.resolve(
56 new Response(native[String(input) as keyof typeof native]),
57 )) as typeof fetch
58 const run = docsRun()
59 const id = 'amazon.nova-2-lite-v1:0'
60 const models = await loadBedrockThinkingGuides(
61 [
62 row(id),
63 row(`eu.${id}`),
64 row(`us.${id}`),
65 row('amazon.nova-2-sonic-v1:0'),
66 ],
67 [
68 {
69 modelName: 'Nova 2 Lite',
70 baseIds: [id],
71 rowIds: [id, `eu.${id}`],
72 runtimeRowIds: [id, `eu.${id}`],
73 },
74 ],
75 run,
76 )
77 expect(models[0]?.reasoning).toEqual({
78 mode: 'effort',
79 mandatory: false,
80 efforts: ['low', 'medium', 'high'],
81 })
82 expect(models[0]?.capabilities).toEqual(['reasoning'])
83 expect(models[0]?.factSources?.capabilities?.reasoning?.sourceUrl).toBe(
84 BEDROCK_NOVA_THINKING_URL,
85 )
86 expect(models[1]?.reasoning).toEqual(models[0]?.reasoning)
87 expect(models[2]?.reasoning).toBeNull()
88 expect(models[3]?.reasoning).toBeNull()
89 expect(models[0]?.factSources?.reasoning?.sourceUrl).toBe(
90 BEDROCK_NOVA_THINKING_URL,
91 )
92 expect(docsReport(run).failed).toBe(0)
93 })
94 it('derives Haiku runtime ownership from its actual native table and rejects malformed protocol bindings', () => {
95 const card =
96 native[
97 'https://docs.aws.amazon.com/bedrock/latest/userguide/model-card-anthropic-claude-haiku-4-5.md'
98 ]
99 const ids = bedrockCardRuntimeIds(card)
100 expect(ids).toHaveLength(6)
101 expect(ids).toContain('eu.anthropic.claude-haiku-4-5-20251001-v1:0')
102 expect(ids).not.toContain('anthropic.claude-haiku-4-5')
103 expect(() =>
104 bedrockCardRuntimeIds(
105 card.replace(
106 '| bedrock-runtime | N/A | N/A |',
107 '| bedrock-runtime | N/A |',
108 ),
109 ),
110 ).toThrow('runtime access table')
111 })
112 it('binds a guide model title only to explicitly declared runtime profiles, never mantle-only base IDs', async () => {
113 globalThis.fetch = ((input: string) =>
114 Promise.resolve(
115 new Response(native[String(input) as keyof typeof native]),
116 )) as typeof fetch
117 const base = 'anthropic.claude-haiku-4-5'
118 const profile = 'eu.anthropic.claude-haiku-4-5-20251001-v1:0'
119 const group = {
120 modelName: 'Claude Haiku 4.5',
121 baseIds: [base],
122 rowIds: [base, profile],
123 runtimeRowIds: [profile],
124 }
125 const run = docsRun()
126 const result = await loadBedrockThinkingGuides(
127 [
128 row(base),
129 row(profile),
130 row('us.anthropic.claude-haiku-4-5-20251001-v1:0'),
131 ],
132 [group],
133 run,
134 )
135 expect(result[0]?.reasoning).toBeNull()
136 expect(result[1]?.reasoning).toEqual({ mode: 'budget', mandatory: null })
137 expect(result[1]?.factSources?.reasoning?.sourceUrl).toBe(
138 BEDROCK_EXTENDED_THINKING_URL,
139 )
140 expect(result[2]?.reasoning).toBeNull()
141 expect(docsReport(run).failed).toBe(0)
142 })
143 it('rejects an incomplete cached runtime binding and ambiguous duplicate card titles', () => {
144 const id = 'amazon.nova-2-lite-v1:0'
145 const guide = {
146 controls: {},
147 namedControls: {
148 'Nova 2 Lite': parseBedrockNovaThinking(
149 native[BEDROCK_NOVA_THINKING_URL as keyof typeof native],
150 ).reasoning,
151 },
152 url: BEDROCK_NOVA_THINKING_URL,
153 hash: 'nova',
154 }
155 const group = { modelName: 'Nova 2 Lite', baseIds: [id], rowIds: [id] }
156 expect(() =>
157 applyBedrockThinkingGuides([row(id)], [group], [guide]),
158 ).toThrow('runtime card binding')
159 const valid = { ...group, runtimeRowIds: [id] }
160 expect(() =>
161 applyBedrockThinkingGuides([row(id)], [valid, valid], [guide]),
162 ).toThrow('ambiguous native card title')
163 })
164 it('derives eight exact extended model IDs and budget from normative own text', () => {
165 const controls = parseBedrockExtendedThinking(extended)
166 expect(Object.keys(controls)).toHaveLength(8)
167 expect(controls['anthropic.claude-opus-4-5-20251101-v1:0']).toEqual({
168 mode: 'budget',
169 mandatory: null,
170 })
171 expect(
172 controls['us.anthropic.claude-opus-4-5-20251101-v1:0'],
173 ).toBeUndefined()
174 expect(controls['anthropic.claude-fable-5']).toBeUndefined()
175 })
176 it('reads all thirteen linked adaptive rows and scopes max/xhigh to their Supported-on lists', () => {
177 const controls = parseBedrockAdaptiveThinking(adaptive)
178 expect(Object.keys(controls)).toHaveLength(13)
179 expect(controls['anthropic.claude-opus-5']).toEqual({
180 mode: 'adaptive',
181 mandatory: false,
182 efforts: ['max', 'xhigh', 'high', 'medium', 'low'],
183 })
184 expect(controls['anthropic.claude-sonnet-4-6']).toEqual({
185 mode: 'adaptive',
186 mandatory: false,
187 efforts: ['max', 'high', 'medium', 'low'],
188 })
189 expect(controls['anthropic.claude-opus-4-6-v1']?.efforts).not.toContain(
190 'xhigh',
191 )
192 })
193 it('reads mandatory from the thinking configuration table only', () => {
194 const controls = parseBedrockAdaptiveThinking(adaptive)
195 // "disabled" and "between_tools" both 400.
196 expect(controls['anthropic.claude-opus-5-5']?.mandatory).toBe(true)
197 expect(controls['anthropic.claude-fable-5']?.mandatory).toBe(true)
198 // "Thinking off", with or without an effort limit.
199 expect(controls['anthropic.claude-haiku-5-5']?.mandatory).toBe(false)
200 expect(controls['anthropic.claude-opus-4-7']?.mandatory).toBe(false)
201 // between_tools drops only up-front thinking: stated neither way.
202 expect(controls['anthropic.claude-sonnet-5-5']?.mandatory).toBeNull()
203 const prose =
204 adaptive + '\nClaude Opus 5.5 supports adaptive and disabled thinking.\n'
205 expect(parseBedrockAdaptiveThinking(prose)).toEqual(controls)
206 const fenced =
207 adaptive +
208 '\n```\n| Claude Opus 5.5 | Adaptive | Adaptive | 400 error | 400 error | Thinking off |\n```\n'
209 expect(parseBedrockAdaptiveThinking(fenced)).toEqual(controls)
210 })
211 it('rejects missing tables, malformed native rows and unreadable restricted model scopes', () => {
212 const maxRow = /^\| max \| .*$/m
213 expect(() =>
214 parseBedrockAdaptiveThinking(
215 adaptive.replace('| Model | Model ID |', '| Changed | Model ID |'),
216 ),
217 ).toThrow('model table')
218 expect(() =>
219 parseBedrockAdaptiveThinking(
220 adaptive.replace('`anthropic.claude-opus-5-5`', '`unknown`'),
221 ),
222 ).toThrow('model row')
223 expect(() =>
224 parseBedrockAdaptiveThinking(
225 adaptive.replace(
226 '(model-card-anthropic-claude-haiku-5-5.md)',
227 '(model-card-anthropic-claude-haiku-5-5.md "card")',
228 ),
229 ),
230 ).toThrow('unsupported named scope')
231 expect(() =>
232 parseBedrockAdaptiveThinking(
233 adaptive.replace(
234 maxRow,
235 '| max | Deepest. Supported on Claude Opus 5 and Claude Future 9. |',
236 ),
237 ),
238 ).toThrow('unsupported named scope')
239 expect(() =>
240 parseBedrockAdaptiveThinking(
241 adaptive.replace(
242 maxRow,
243 '| max | Deepest. Supported on selected future models. |',
244 ),
245 ),
246 ).toThrow('model scope')
247 expect(() =>
248 parseBedrockAdaptiveThinking(
249 adaptive.replace('| low |', '| low |\n| low |'),
250 ),
251 ).toThrow('effort row')
252 expect(() =>
253 parseBedrockExtendedThinking(
254 extended.replace(
255 'The `budget_tokens` parameter determines',
256 'The undocumented parameter determines',
257 ),
258 ),
259 ).toThrow('budget declaration')
260 })
261 it('rejects a missing or unreadable thinking configuration table', () => {
262 expect(() =>
263 parseBedrockAdaptiveThinking(
264 adaptive.replace('## Thinking configuration by model', '## Renamed'),
265 ),
266 ).toThrow('missing thinking configuration table')
267 expect(() =>
268 parseBedrockAdaptiveThinking(
269 adaptive.replace(
270 /^(\| Claude Sonnet 5 \|.*\| )Thinking off( \|\s*)$/m,
271 '$1Thinking reduced$2',
272 ),
273 ),
274 ).toThrow('unreadable thinking configuration row')
275 expect(() =>
276 parseBedrockAdaptiveThinking(
277 adaptive.replace('| Claude Sonnet 5 |', '| Claude Sonnet 9 |'),
278 ),
279 ).toThrow('unsupported named scope')
280 expect(() =>
281 parseBedrockAdaptiveThinking(
282 adaptive.replace('| Claude Sonnet 5 |', '| Claude Opus 5 |'),
283 ),
284 ).toThrow('duplicate thinking configuration')
285 })
286 it('rejects an embedded or negated mode declaration', () => {
287 expect(() =>
288 parseBedrockExtendedThinking(
289 extended.replace(
290 'To turn on extended thinking, add',
291 'Do not assume To turn on extended thinking, add',
292 ),
293 ),
294 ).toThrow('budget declaration')
295 expect(() =>
296 parseBedrockAdaptiveThinking(
297 adaptive.replace(
298 'Set `thinking.type` to',
299 'Do not assume Set `thinking.type` to',
300 ),
301 ),
302 ).toThrow('mode or effort table')
303 })
304 it('joins only the card-owned IDs, preserving existing controls and the source for every filled object', () => {
305 const base = 'anthropic.claude-opus-5'
306 const catalog = [
307 row(base),
308 row(`eu.${base}`),
309 row(`us.${base}`),
310 row(`${base}-future`),
311 ]
312 const models = applyBedrockThinkingGuides(
313 catalog,
314 [{ modelName: null, baseIds: [base], rowIds: [base, `eu.${base}`] }],
315 [
316 {
317 controls: parseBedrockAdaptiveThinking(adaptive),
318 url: BEDROCK_ADAPTIVE_THINKING_URL,
319 hash: 'own-guide-hash',
320 },
321 ],
322 )
323 expect(models[0]?.reasoning?.mode).toBe('adaptive')
324 expect(models[1]?.reasoning).toEqual(models[0]?.reasoning)
325 expect(models[2]?.reasoning).toBeNull()
326 expect(models[3]?.reasoning).toBeNull()
327 expect(models[0]?.factSources?.reasoning?.sourceUrl).toBe(
328 BEDROCK_ADAPTIVE_THINKING_URL,
329 )
330 expect(models[1]?.factSources?.reasoning?.sourceHash).toBe('own-guide-hash')
331 expect(catalog[0]?.reasoning).toBeNull()
332 })
333 it('fills missing card effort values only when the own guide supports retained values completely', () => {
334 const own = {
335 ...row('anthropic.claude-sonnet-5'),
336 reasoning: { mode: 'adaptive', mandatory: false } as const,
337 factSources: {
338 reasoning: {
339 derivation: 'docs-derived',
340 sourceUrl: 'https://docs.aws.amazon.com/card.md',
341 } as const,
342 },
343 }
344 const incompatible = {
345 ...row('anthropic.claude-opus-4-6-v1'),
346 reasoning: { mode: 'budget', mandatory: null } as const,
347 }
348 const full = {
349 ...row('anthropic.claude-sonnet-5-5'),
350 reasoning: {
351 mode: 'adaptive',
352 mandatory: null,
353 efforts: ['low', 'medium', 'high', 'xhigh', 'max'],
354 } as ModelReasoning,
355 }
356 const guide = {
357 controls: parseBedrockAdaptiveThinking(adaptive),
358 url: BEDROCK_ADAPTIVE_THINKING_URL,
359 hash: 'guide',
360 }
361 const result = applyBedrockThinkingGuides(
362 [own, incompatible, full],
363 [own, incompatible, full].map((model) => ({
364 modelName: null,
365 baseIds: [model.rawId],
366 rowIds: [model.rawId],
367 })),
368 [guide],
369 )
370 expect(result[0]?.reasoning?.efforts).toEqual([
371 'max',
372 'xhigh',
373 'high',
374 'medium',
375 'low',
376 ])
377 expect(result[0]?.factSources?.reasoning?.sourceUrl).toBe(
378 BEDROCK_ADAPTIVE_THINKING_URL,
379 )
380 expect(result[1]).toBe(incompatible)
381 expect(result[2]).toBe(full)
382 })
383 it('preserves capability maps and their sources, and exposes explicit native contradictions', async () => {
384 const id = 'anthropic.claude-opus-5'
385 const guide = {
386 controls: parseBedrockAdaptiveThinking(adaptive),
387 url: BEDROCK_ADAPTIVE_THINKING_URL,
388 hash: 'adaptive',
389 }
390 const previous = {
391 derivation: 'docs-derived' as const,
392 sourceUrl: 'https://docs.aws.amazon.com/card.md',
393 }
394 const model = {
395 ...row(id),
396 capabilities: { tools: false },
397 factSources: { capabilities: { tools: previous } },
398 }
399 const result = applyBedrockThinkingGuides(
400 [model],
401 [{ modelName: null, baseIds: [id], rowIds: [id] }],
402 [guide],
403 )
404 expect(result[0]?.capabilities).toEqual({ tools: false, reasoning: true })
405 expect(result[0]?.factSources?.capabilities?.tools).toEqual(previous)
406 globalThis.fetch = ((input: string) =>
407 Promise.resolve(
408 new Response(native[String(input) as keyof typeof native]),
409 )) as typeof fetch
410 const run = docsRun()
411 const negative = {
412 ...model,
413 capabilities: { reasoning: false, tools: false },
414 }
415 const guarded = await loadBedrockThinkingGuides(
416 [negative],
417 [{ modelName: null, baseIds: [id], rowIds: [id] }],
418 run,
419 )
420 expect(guarded[0]?.capabilities).toEqual({ reasoning: false, tools: false })
421 expect(guarded[0]?.reasoning).toBeNull()
422 expect(guarded[0]?.absent?.reasoning).toBe('unavailable')
423 expect(docsReport(run).failed).toBe(1)
424 expect(docsReport(run).first[0]?.source).toBe(BEDROCK_ADAPTIVE_THINKING_URL)
425 expect(docsReport(run).first[0]?.error).toContain(
426 'contradicts explicit negative',
427 )
428 const fable = {
429 ...row('anthropic.claude-fable-5'),
430 reasoning: { mode: 'adaptive', mandatory: false } as const,
431 }
432 expect(() =>
433 applyBedrockThinkingGuides(
434 [fable],
435 [{ modelName: null, baseIds: [fable.rawId], rowIds: [fable.rawId] }],
436 [guide],
437 ),
438 ).toThrow('contradicts card mandatory')
439 })
440 it('retains card enum order while filling a natively supported mandatory leaf', () => {
441 const id = 'anthropic.claude-opus-4-7'
442 const model = {
443 ...row(id),
444 reasoning: {
445 mode: 'adaptive',
446 mandatory: null,
447 efforts: ['low', 'medium', 'high', 'xhigh', 'max'],
448 } as ModelReasoning,
449 }
450 const result = applyBedrockThinkingGuides(
451 [model],
452 [{ modelName: null, baseIds: [id], rowIds: [id] }],
453 [
454 {
455 controls: parseBedrockAdaptiveThinking(adaptive),
456 url: BEDROCK_ADAPTIVE_THINKING_URL,
457 hash: 'adaptive',
458 },
459 ],
460 )
461 expect(result[0]?.reasoning).toEqual({
462 mode: 'adaptive',
463 mandatory: false,
464 efforts: ['low', 'medium', 'high', 'xhigh', 'max'],
465 })
466 expect(result[0]?.factSources?.reasoning?.sourceUrl).toBe(
467 BEDROCK_ADAPTIVE_THINKING_URL,
468 )
469 })
470 it('prefers native adaptive controls over deprecated budget when both guides list the exact ID', () => {
471 const id = 'anthropic.claude-opus-4-6-v1'
472 const result = applyBedrockThinkingGuides(
473 [row(id)],
474 [{ modelName: null, baseIds: [id], rowIds: [id] }],
475 [
476 {
477 controls: parseBedrockExtendedThinking(extended),
478 url: BEDROCK_EXTENDED_THINKING_URL,
479 hash: 'extended',
480 },
481 {
482 controls: parseBedrockAdaptiveThinking(adaptive),
483 url: BEDROCK_ADAPTIVE_THINKING_URL,
484 hash: 'adaptive',
485 },
486 ],
487 )
488 expect(result[0]?.reasoning?.mode).toBe('adaptive')
489 })
490 it('reports malformed guide sources visibly and protects unknown controls without masking valid native facts', async () => {
491 globalThis.fetch = () =>
492 Promise.resolve(new Response('Changed source markup'))
493 const run = docsRun()
494 const id = 'anthropic.claude-sonnet-4-6'
495 const model = {
496 ...row(id),
497 maxOutput: 123,
498 absent: { pricing: 'cleared' } as const,
499 }
500 const other = row('amazon.nova-2-sonic-v1:0')
501 const result = await loadBedrockThinkingGuides(
502 [model, other],
503 [{ modelName: null, baseIds: [id], rowIds: [id] }],
504 run,
505 )
506 expect(docsReport(run).failed).toBe(3)
507 expect(docsReport(run).first.map((failure) => failure.source)).toEqual([
508 BEDROCK_EXTENDED_THINKING_URL,
509 BEDROCK_ADAPTIVE_THINKING_URL,
510 BEDROCK_NOVA_THINKING_URL,
511 ])
512 expect(result[0]?.absent).toEqual({
513 pricing: 'cleared',
514 reasoning: 'unavailable',
515 })
516 expect(result[0]?.maxOutput).toBe(123)
517 expect(result[1]).toBe(other)
518 })
519})