config-audit/tests/lib/context-window.test.mjs
Kjell Tore Guttormsen cf75249b5e feat(skl,cml): --context-window auto model→window probe (v5.12 B8b) [skip-docs]
Completes the deferred B8 half. `--context-window auto` now probes the
configured model and calibrates SKL/CML budgets to its real window instead
of always falling back to the conservative advisory anchor.

- lib/context-window.mjs: pure modelToContextWindow() maps known 1M-tier
  model IDs (Fable 5, Opus 4.8/4.7/4.6, Sonnet 4.6 — verified June 2026 —
  plus the explicit [1m] tier tag, dated/provider-prefixed IDs, and the
  opus/sonnet/fable aliases) to the 1M window; unknown/unconfirmed -> null
  (caller keeps the conservative anchor). resolveContextWindow() auto branch
  now probes opts.model: recognized -> auto-probed (not advisory); unknown
  or unpinned -> auto-unresolved (advisory, pre-B8b behavior).
- lib/active-model.mjs (new): resolveActiveModel() reads the model the way
  Claude Code resolves it — shell ANTHROPIC_MODEL override, then settings
  cascade local > project > user. Injectable env, hermetic under test HOME.
- scan-orchestrator: resolves the active model only when the flag is `auto`
  and threads it into resolveContextWindow; posture inherits via runAllScanners.

Default (no flag) and explicit --context-window <n> paths ignore the model
and stay byte-stable; frozen v5.0.0 + SC-5 snapshots untouched. TDD: 17 new
tests (context-window mapping/probe + active-model cascade). Suite 1296 green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 22:29:12 +02:00

167 lines
6.9 KiB
JavaScript

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import {
CONTEXT_WINDOW_ANCHOR,
LARGE_CONTEXT_WINDOW,
modelToContextWindow,
resolveContextWindow,
scaleForWindow,
} from '../../scanners/lib/context-window.mjs';
// B8 — context-window calibration. resolveContextWindow turns the raw
// --context-window CLI value into { window, advisory } that SKL/CML calibrate
// their budgets with. The DEFAULT (no flag) must be byte-identical to the
// pre-B8 behavior: the conservative 200k anchor at full severity (advisory=false).
describe('resolveContextWindow — default (no flag) is the conservative anchor', () => {
it('undefined resolves to the 200k anchor, not advisory (byte-stable default)', () => {
const r = resolveContextWindow(undefined);
assert.equal(r.window, CONTEXT_WINDOW_ANCHOR);
assert.equal(r.advisory, false);
assert.equal(r.source, 'default');
});
it('null resolves to the conservative default', () => {
const r = resolveContextWindow(null);
assert.equal(r.window, CONTEXT_WINDOW_ANCHOR);
assert.equal(r.advisory, false);
});
});
describe('resolveContextWindow — explicit window', () => {
it('a numeric string calibrates to that window, not advisory', () => {
const r = resolveContextWindow('1000000');
assert.equal(r.window, 1_000_000);
assert.equal(r.advisory, false);
assert.equal(r.source, 'explicit');
});
it('a plain number is accepted', () => {
const r = resolveContextWindow(1_000_000);
assert.equal(r.window, 1_000_000);
assert.equal(r.advisory, false);
});
});
describe('resolveContextWindow — auto / unknown downgrades to advisory', () => {
it('"auto" with no model keeps the conservative anchor but flags advisory', () => {
const r = resolveContextWindow('auto');
assert.equal(r.window, CONTEXT_WINDOW_ANCHOR, 'window stays the conservative anchor');
assert.equal(r.advisory, true, 'unknown window -> advisory, not a budget breach');
assert.equal(r.source, 'auto-unresolved');
});
it('"auto" with an unrecognized model also stays advisory', () => {
const r = resolveContextWindow('auto', { model: 'totally-made-up-model' });
assert.equal(r.window, CONTEXT_WINDOW_ANCHOR);
assert.equal(r.advisory, true);
assert.equal(r.source, 'auto-unresolved');
});
it('"AUTO" is case-insensitive', () => {
assert.equal(resolveContextWindow('AUTO').advisory, true);
});
it('an invalid value falls back to the conservative default (not advisory)', () => {
for (const bad of ['banana', '0', '-5', '']) {
const r = resolveContextWindow(bad);
assert.equal(r.window, CONTEXT_WINDOW_ANCHOR, `"${bad}" -> anchor`);
assert.equal(r.advisory, false, `"${bad}" -> not advisory`);
}
});
});
describe('scaleForWindow — linear scaling off the 200k anchor', () => {
it('is the identity at the 200k anchor (byte-stable default)', () => {
assert.equal(scaleForWindow(4000, CONTEXT_WINDOW_ANCHOR), 4000);
assert.equal(scaleForWindow(40_000, CONTEXT_WINDOW_ANCHOR), 40_000);
});
it('scales 5x at the 1M window', () => {
assert.equal(scaleForWindow(4000, LARGE_CONTEXT_WINDOW), 20_000);
assert.equal(scaleForWindow(40_000, LARGE_CONTEXT_WINDOW), 200_000);
});
});
// B8b — model -> context-window mapping. `--context-window auto` probes the
// configured model and maps known 1M-tier model IDs to the large window so SKL/CML
// self-calibrate instead of falling back to the conservative advisory anchor.
// Verified June 2026 (platform.claude.com models overview): Fable 5, Opus
// 4.8/4.7/4.6 and Sonnet 4.6 run a 1M context window.
describe('modelToContextWindow — known 1M-tier models map to the large window', () => {
it('the explicit [1m] tier tag wins (the running session model surfaces this way)', () => {
assert.equal(modelToContextWindow('claude-opus-4-8[1m]'), LARGE_CONTEXT_WINDOW);
// Suffix wins even for a base ID we do not otherwise enumerate.
assert.equal(modelToContextWindow('claude-future-9[1m]'), LARGE_CONTEXT_WINDOW);
});
it('known 1M base IDs map to 1M', () => {
for (const id of [
'claude-fable-5',
'claude-opus-4-8',
'claude-opus-4-7',
'claude-opus-4-6',
'claude-sonnet-4-6',
]) {
assert.equal(modelToContextWindow(id), LARGE_CONTEXT_WINDOW, id);
}
});
it('tolerates dated suffixes and provider prefixes (substring match)', () => {
assert.equal(modelToContextWindow('claude-opus-4-8-20260528'), LARGE_CONTEXT_WINDOW);
assert.equal(modelToContextWindow('us.anthropic.claude-sonnet-4-6'), LARGE_CONTEXT_WINDOW);
assert.equal(modelToContextWindow('CLAUDE-OPUS-4-8'), LARGE_CONTEXT_WINDOW); // case-insensitive
});
it('short aliases that resolve to a 1M-tier model map to 1M', () => {
for (const alias of ['opus', 'sonnet', 'fable', 'opusplan']) {
assert.equal(modelToContextWindow(alias), LARGE_CONTEXT_WINDOW, alias);
}
});
});
describe('modelToContextWindow — unknown / unconfirmed models return null (conservative)', () => {
it('returns null for models whose window we do not confirm', () => {
// Haiku is intentionally NOT enumerated: keep the conservative anchor rather
// than guess. null -> caller keeps the advisory 200k anchor.
assert.equal(modelToContextWindow('claude-haiku-4-5-20251001'), null);
assert.equal(modelToContextWindow('haiku'), null);
assert.equal(modelToContextWindow('some-unknown-model'), null);
assert.equal(modelToContextWindow('default'), null);
});
it('returns null for non-string / empty input', () => {
assert.equal(modelToContextWindow(undefined), null);
assert.equal(modelToContextWindow(null), null);
assert.equal(modelToContextWindow(''), null);
assert.equal(modelToContextWindow(' '), null);
assert.equal(modelToContextWindow(42), null);
});
});
describe('resolveContextWindow — auto probes the model (B8b)', () => {
it('"auto" + a recognized 1M model calibrates to 1M, not advisory', () => {
const r = resolveContextWindow('auto', { model: 'claude-opus-4-8[1m]' });
assert.equal(r.window, LARGE_CONTEXT_WINDOW);
assert.equal(r.advisory, false, 'a confirmed window is not advisory');
assert.equal(r.source, 'auto-probed');
});
it('"auto" + a recognized base ID (no tag) also calibrates', () => {
const r = resolveContextWindow('auto', { model: 'claude-sonnet-4-6' });
assert.equal(r.window, LARGE_CONTEXT_WINDOW);
assert.equal(r.source, 'auto-probed');
});
it('the model is ignored on the explicit and default paths (byte-stable)', () => {
const explicit = resolveContextWindow('1000000', { model: 'claude-opus-4-8' });
assert.equal(explicit.source, 'explicit');
assert.equal(explicit.window, 1_000_000);
const def = resolveContextWindow(undefined, { model: 'claude-opus-4-8[1m]' });
assert.equal(def.source, 'default');
assert.equal(def.window, CONTEXT_WINDOW_ANCHOR);
assert.equal(def.advisory, false);
});
});