Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

bedrock-thinking-guides.test.ts

503 lines17.3 KB

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 twelve adaptive rows and restricts max/xhigh to their own named scopes', () => {
177 const controls = parseBedrockAdaptiveThinking(adaptive)
178 expect(Object.keys(controls)).toHaveLength(12)
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: null,
187 efforts: ['max', 'high', 'medium', 'low'],
188 })
189 expect(controls['anthropic.claude-opus-5-5']).toEqual({
190 mode: 'adaptive',
191 mandatory: null,
192 efforts: ['high', 'medium', 'low'],
193 })
194 expect(controls['anthropic.claude-fable-5']).toEqual({
195 mode: 'adaptive',
196 mandatory: true,
197 efforts: ['high', 'medium', 'low'],
198 })
199 expect(controls['anthropic.claude-opus-4-7']?.mandatory).toBe(false)
200 expect(controls['anthropic.claude-haiku-5-5']?.mandatory).toBe(false)
201 })
202 it('does not infer mandatory from always-thinks effort descriptions, a default or examples', () => {
203 const noOperationalNotes = adaptive
204 .replace(
205 /^.*(?:supports adaptive and disabled thinking|also supports disabled thinking|disabled thinking[^\n]*not supported on these models).*$/gm,
206 '',
207 )
208 .replace(/^\*\*Adaptive thinking is on by default[^\n]*\n[^\n]*$/gm, '')
209 const controls = parseBedrockAdaptiveThinking(noOperationalNotes)
210 expect(
211 Object.values(controls).every((control) => control.mandatory === null),
212 ).toBe(true)
213 const negated =
214 noOperationalNotes +
215 '\nDo not assume Claude Opus 5.5 supports adaptive and disabled thinking.\n'
216 expect(
217 parseBedrockAdaptiveThinking(negated)['anthropic.claude-opus-5-5']
218 ?.mandatory,
219 ).toBeNull()
220 const fenced =
221 adaptive +
222 '\n```\n| invented | all models |\nClaude Opus 5.5 supports adaptive and disabled thinking.\n```\n'
223 expect(parseBedrockAdaptiveThinking(fenced)).toEqual(
224 parseBedrockAdaptiveThinking(adaptive),
225 )
226 })
227 it('rejects missing tables, malformed native rows and unreadable restricted model scopes', () => {
228 expect(() =>
229 parseBedrockAdaptiveThinking(
230 adaptive.replace('| Model | Model ID |', '| Changed | Model ID |'),
231 ),
232 ).toThrow('model table')
233 expect(() =>
234 parseBedrockAdaptiveThinking(
235 adaptive.replace('`anthropic.claude-opus-5-5`', '`unknown`'),
236 ),
237 ).toThrow('model row')
238 expect(() =>
239 parseBedrockAdaptiveThinking(
240 adaptive.replace(
241 'Claude Opus 5, Claude Opus 4.6, and Claude Haiku 5.5 only.',
242 'Claude Opus 5 and Claude Future 9 only.',
243 ),
244 ),
245 ).toThrow('unsupported named scope')
246 expect(() =>
247 parseBedrockAdaptiveThinking(
248 adaptive.replace(
249 'Claude Opus 5, Claude Opus 4.6, and Claude Haiku 5.5 only.',
250 'Selected future models only.',
251 ),
252 ),
253 ).toThrow('model scope')
254 expect(() =>
255 parseBedrockAdaptiveThinking(
256 adaptive.replace('| low |', '| low |\n| low |'),
257 ),
258 ).toThrow('effort row')
259 expect(() =>
260 parseBedrockExtendedThinking(
261 extended.replace(
262 'The `budget_tokens` parameter determines',
263 'The undocumented parameter determines',
264 ),
265 ),
266 ).toThrow('budget declaration')
267 })
268 it('rejects embedded or negated mode declarations and never stamps mandatory from a negated restriction', () => {
269 expect(() =>
270 parseBedrockExtendedThinking(
271 extended.replace(
272 'To turn on extended thinking, add',
273 'Do not assume To turn on extended thinking, add',
274 ),
275 ),
276 ).toThrow('budget declaration')
277 expect(() =>
278 parseBedrockAdaptiveThinking(
279 adaptive.replace(
280 'Set `thinking.type` to',
281 'Do not assume Set `thinking.type` to',
282 ),
283 ),
284 ).toThrow('mode or effort table')
285 const negated = adaptive.replace(
286 'Claude Fable 5.1, Claude Mythos 5.1, Claude Mythos 5, and Claude Fable 5 *only* support',
287 'Never infer Claude Fable 5.1, Claude Mythos 5.1, Claude Mythos 5, and Claude Fable 5 *only* support',
288 )
289 expect(
290 parseBedrockAdaptiveThinking(negated)['anthropic.claude-fable-5']
291 ?.mandatory,
292 ).toBeNull()
293 })
294 it('joins only the card-owned IDs, preserving existing controls and the source for every filled object', () => {
295 const base = 'anthropic.claude-opus-5'
296 const catalog = [
297 row(base),
298 row(`eu.${base}`),
299 row(`us.${base}`),
300 row(`${base}-future`),
301 ]
302 const models = applyBedrockThinkingGuides(
303 catalog,
304 [{ modelName: null, baseIds: [base], rowIds: [base, `eu.${base}`] }],
305 [
306 {
307 controls: parseBedrockAdaptiveThinking(adaptive),
308 url: BEDROCK_ADAPTIVE_THINKING_URL,
309 hash: 'own-guide-hash',
310 },
311 ],
312 )
313 expect(models[0]?.reasoning?.mode).toBe('adaptive')
314 expect(models[1]?.reasoning).toEqual(models[0]?.reasoning)
315 expect(models[2]?.reasoning).toBeNull()
316 expect(models[3]?.reasoning).toBeNull()
317 expect(models[0]?.factSources?.reasoning?.sourceUrl).toBe(
318 BEDROCK_ADAPTIVE_THINKING_URL,
319 )
320 expect(models[1]?.factSources?.reasoning?.sourceHash).toBe('own-guide-hash')
321 expect(catalog[0]?.reasoning).toBeNull()
322 })
323 it('fills missing card effort values only when the own guide supports retained values completely', () => {
324 const own = {
325 ...row('anthropic.claude-sonnet-5'),
326 reasoning: { mode: 'adaptive', mandatory: false } as const,
327 factSources: {
328 reasoning: {
329 derivation: 'docs-derived',
330 sourceUrl: 'https://docs.aws.amazon.com/card.md',
331 } as const,
332 },
333 }
334 const incompatible = {
335 ...row('anthropic.claude-opus-4-6-v1'),
336 reasoning: { mode: 'budget', mandatory: null } as const,
337 }
338 const full = {
339 ...row('anthropic.claude-sonnet-5-5'),
340 reasoning: {
341 mode: 'adaptive',
342 mandatory: null,
343 efforts: ['low', 'medium', 'high', 'xhigh', 'max'],
344 } as ModelReasoning,
345 }
346 const guide = {
347 controls: parseBedrockAdaptiveThinking(adaptive),
348 url: BEDROCK_ADAPTIVE_THINKING_URL,
349 hash: 'guide',
350 }
351 const result = applyBedrockThinkingGuides(
352 [own, incompatible, full],
353 [own, incompatible, full].map((model) => ({
354 modelName: null,
355 baseIds: [model.rawId],
356 rowIds: [model.rawId],
357 })),
358 [guide],
359 )
360 expect(result[0]?.reasoning?.efforts).toEqual(['high', 'medium', 'low'])
361 expect(result[0]?.factSources?.reasoning?.sourceUrl).toBe(
362 BEDROCK_ADAPTIVE_THINKING_URL,
363 )
364 expect(result[1]).toBe(incompatible)
365 expect(result[2]).toBe(full)
366 })
367 it('preserves capability maps and their sources, and exposes explicit native contradictions', async () => {
368 const id = 'anthropic.claude-opus-5'
369 const guide = {
370 controls: parseBedrockAdaptiveThinking(adaptive),
371 url: BEDROCK_ADAPTIVE_THINKING_URL,
372 hash: 'adaptive',
373 }
374 const previous = {
375 derivation: 'docs-derived' as const,
376 sourceUrl: 'https://docs.aws.amazon.com/card.md',
377 }
378 const model = {
379 ...row(id),
380 capabilities: { tools: false },
381 factSources: { capabilities: { tools: previous } },
382 }
383 const result = applyBedrockThinkingGuides(
384 [model],
385 [{ modelName: null, baseIds: [id], rowIds: [id] }],
386 [guide],
387 )
388 expect(result[0]?.capabilities).toEqual({ tools: false, reasoning: true })
389 expect(result[0]?.factSources?.capabilities?.tools).toEqual(previous)
390 globalThis.fetch = ((input: string) =>
391 Promise.resolve(
392 new Response(native[String(input) as keyof typeof native]),
393 )) as typeof fetch
394 const run = docsRun()
395 const negative = {
396 ...model,
397 capabilities: { reasoning: false, tools: false },
398 }
399 const guarded = await loadBedrockThinkingGuides(
400 [negative],
401 [{ modelName: null, baseIds: [id], rowIds: [id] }],
402 run,
403 )
404 expect(guarded[0]?.capabilities).toEqual({ reasoning: false, tools: false })
405 expect(guarded[0]?.reasoning).toBeNull()
406 expect(guarded[0]?.absent?.reasoning).toBe('unavailable')
407 expect(docsReport(run).failed).toBe(1)
408 expect(docsReport(run).first[0]?.source).toBe(BEDROCK_ADAPTIVE_THINKING_URL)
409 expect(docsReport(run).first[0]?.error).toContain(
410 'contradicts explicit negative',
411 )
412 const fable = {
413 ...row('anthropic.claude-fable-5'),
414 reasoning: { mode: 'adaptive', mandatory: false } as const,
415 }
416 expect(() =>
417 applyBedrockThinkingGuides(
418 [fable],
419 [{ modelName: null, baseIds: [fable.rawId], rowIds: [fable.rawId] }],
420 [guide],
421 ),
422 ).toThrow('contradicts card mandatory')
423 })
424 it('retains card enum order while filling a natively supported mandatory leaf', () => {
425 const id = 'anthropic.claude-opus-4-7'
426 const model = {
427 ...row(id),
428 reasoning: {
429 mode: 'adaptive',
430 mandatory: null,
431 efforts: ['low', 'medium', 'high'],
432 } as ModelReasoning,
433 }
434 const result = applyBedrockThinkingGuides(
435 [model],
436 [{ modelName: null, baseIds: [id], rowIds: [id] }],
437 [
438 {
439 controls: parseBedrockAdaptiveThinking(adaptive),
440 url: BEDROCK_ADAPTIVE_THINKING_URL,
441 hash: 'adaptive',
442 },
443 ],
444 )
445 expect(result[0]?.reasoning).toEqual({
446 mode: 'adaptive',
447 mandatory: false,
448 efforts: ['low', 'medium', 'high'],
449 })
450 expect(result[0]?.factSources?.reasoning?.sourceUrl).toBe(
451 BEDROCK_ADAPTIVE_THINKING_URL,
452 )
453 })
454 it('prefers native adaptive controls over deprecated budget when both guides list the exact ID', () => {
455 const id = 'anthropic.claude-opus-4-6-v1'
456 const result = applyBedrockThinkingGuides(
457 [row(id)],
458 [{ modelName: null, baseIds: [id], rowIds: [id] }],
459 [
460 {
461 controls: parseBedrockExtendedThinking(extended),
462 url: BEDROCK_EXTENDED_THINKING_URL,
463 hash: 'extended',
464 },
465 {
466 controls: parseBedrockAdaptiveThinking(adaptive),
467 url: BEDROCK_ADAPTIVE_THINKING_URL,
468 hash: 'adaptive',
469 },
470 ],
471 )
472 expect(result[0]?.reasoning?.mode).toBe('adaptive')
473 })
474 it('reports malformed guide sources visibly and protects unknown controls without masking valid native facts', async () => {
475 globalThis.fetch = () =>
476 Promise.resolve(new Response('Changed source markup'))
477 const run = docsRun()
478 const id = 'anthropic.claude-sonnet-4-6'
479 const model = {
480 ...row(id),
481 maxOutput: 123,
482 absent: { pricing: 'cleared' } as const,
483 }
484 const other = row('amazon.nova-2-sonic-v1:0')
485 const result = await loadBedrockThinkingGuides(
486 [model, other],
487 [{ modelName: null, baseIds: [id], rowIds: [id] }],
488 run,
489 )
490 expect(docsReport(run).failed).toBe(3)
491 expect(docsReport(run).first.map((failure) => failure.source)).toEqual([
492 BEDROCK_EXTENDED_THINKING_URL,
493 BEDROCK_ADAPTIVE_THINKING_URL,
494 BEDROCK_NOVA_THINKING_URL,
495 ])
496 expect(result[0]?.absent).toEqual({
497 pricing: 'cleared',
498 reasoning: 'unavailable',
499 })
500 expect(result[0]?.maxOutput).toBe(123)
501 expect(result[1]).toBe(other)
502 })
503})