| from __future__ import annotations |
|
|
| import json |
| from pathlib import Path |
|
|
| import pytest |
|
|
| from shiftedx_bench.context import ContextSpec, build_context_case, expand_context_config |
| from shiftedx_bench.boundary import probe_context_boundary |
| from shiftedx_bench.holdout import derive_holdout_config |
|
|
|
|
| class CharTokenizer: |
| fingerprint = "char-tokenizer-v1" |
|
|
| def encode(self, text: str) -> list[int]: |
| return [ord(value) for value in text] |
|
|
| def decode(self, token_ids: list[int]) -> str: |
| return "".join(chr(value) for value in token_ids) |
|
|
| def render_chat(self, messages): |
| return "".join(f"<{item['role']}>\n{item['content']}\n" for item in messages) + "<assistant>\n" |
|
|
| def token_offset(self, rendered_text: str, substring: str) -> int: |
| value = rendered_text.index(substring) |
| return len(self.encode(rendered_text[:value])) |
|
|
|
|
| def test_smoke_plan_has_expected_coverage(): |
| root = Path(__file__).resolve().parents[1] |
| config = json.loads((root / "configs/context-smoke-v1.json").read_text()) |
| specs = expand_context_config(config) |
| assert len(specs) == 19 |
| assert {spec.target_tokens for spec in specs} >= {4096, 32768, 131072, 260096} |
| assert {spec.query_placement for spec in specs} == {"before", "after"} |
|
|
|
|
| def test_quant_gate_is_bounded_but_multidimensional(): |
| root = Path(__file__).resolve().parents[1] |
| config = json.loads((root / "configs/context-quant-gate-v1.json").read_text()) |
| specs = expand_context_config(config) |
| assert len(specs) == 15 |
| assert sum(spec.target_tokens for spec in specs) == 372736 |
| assert {spec.target_tokens for spec in specs} == {4096, 16384, 65536, 131072} |
| assert {spec.family for spec in specs} == { |
| "single_key", "binding", "latest_record", "multi_hop", "semantic", "state_tracking" |
| } |
| assert {spec.query_placement for spec in specs} == {"before", "after"} |
|
|
|
|
| def test_max_window_sentinel_is_explicitly_separate(): |
| root = Path(__file__).resolve().parents[1] |
| config = json.loads((root / "configs/context-max-window-v1.json").read_text()) |
| specs = expand_context_config(config) |
| assert len(specs) == 1 |
| assert specs[0].target_tokens == 260096 |
| assert specs[0].family == "single_key" |
|
|
|
|
| @pytest.mark.parametrize( |
| "family", |
| ["single_key", "binding", "latest_record", "multi_hop", "semantic", "state_tracking"], |
| ) |
| @pytest.mark.parametrize("position", [0.01, 0.5, 0.99]) |
| def test_context_is_exact_and_positioned(family: str, position: float): |
| spec = ContextSpec("suite", "matrix", family, 12000, position, 173) |
| case = build_context_case(spec, CharTokenizer()) |
| assert case.metadata["actual_rendered_tokens"] == 12000 |
| assert abs(case.metadata["actual_primary_position"] - position) <= 0.003 |
| assert set(case.expected) >= {"start_sentinel", "end_sentinel", "record"} |
|
|
|
|
| def test_rejects_duplicate_identifiers(): |
| config = { |
| "suite_id": "x", |
| "matrices": [{ |
| "name": "m", "families": ["single_key", "single_key"], "lengths": [4096], |
| "positions": [0.5], "seeds": [1] |
| }], |
| } |
| with pytest.raises(ValueError, match="duplicate"): |
| expand_context_config(config) |
|
|
|
|
| def test_holdout_derivation_is_stable_and_release_specific(): |
| template = { |
| "suite_id": "x", "context_window": 8192, "reserved_output_tokens": 512, |
| "matrices": [{"name":"m","families":["single_key"],"lengths":[4096],"positions":[.5],"seeds":[1,2]}], |
| } |
| first = derive_holdout_config(template, "a sufficiently long private key", "r1") |
| second = derive_holdout_config(template, "a sufficiently long private key", "r1") |
| third = derive_holdout_config(template, "a sufficiently long private key", "r2") |
| assert first == second |
| assert first["matrices"][0]["seeds"] != third["matrices"][0]["seeds"] |
| assert "private key" not in str(first) |
|
|
|
|
| def test_rejects_prompt_beyond_reserved_output_budget(): |
| config = { |
| "suite_id":"x", "context_window":4096, "reserved_output_tokens":512, |
| "matrices":[{"name":"m","families":["single_key"],"lengths":[3585],"positions":[.5],"seeds":[1]}], |
| } |
| with pytest.raises(ValueError, match="exceeds"): |
| expand_context_config(config) |
|
|
|
|
| def test_boundary_probe_requires_explicit_server_rejection(): |
| class RejectingClient: |
| def complete(self, payload, stream=False): |
| raise RuntimeError("HTTP 400: context window exceeded") |
|
|
| result = probe_context_boundary( |
| client=RejectingClient(), tokenizer=CharTokenizer(), model="m", |
| context_window=4096, reserved_output_tokens=512, |
| ) |
| assert result["passed"] |
|
|