File size: 6,804 Bytes
f039f41
 
 
 
 
335e669
a642242
f039f41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ddbe3c
 
 
 
 
 
 
 
 
 
 
 
f039f41
 
 
 
 
 
 
 
 
 
 
a642242
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
335e669
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from __future__ import annotations

from pathlib import Path

from shiftedx_bench.models import BenchCase
from shiftedx_bench.cli import make_parser
from shiftedx_bench.release import sanitize_manifest, sanitize_result_row, scan_public_tree
from shiftedx_bench.runner import run_cases
from shiftedx_bench.summary import summarize_results
from shiftedx_bench.util import read_jsonl


class FakeClient:
    def __init__(self, prompt_tokens=10): self.prompt_tokens = prompt_tokens
    def complete(self, payload, stream=False):
        return {
            "content": '{"answer":7}', "reasoning_content": "", "tool_calls": [],
            "finish_reason": "stop", "usage": {"prompt_tokens": self.prompt_tokens, "completion_tokens": 5},
            "prompt_tokens": self.prompt_tokens, "completion_tokens": 5, "wall_s": 1.0,
            "ttft_s": 0.2, "end_to_end_tokens_per_second": 5.0, "mtplx_stats": {},
        }


def test_runner_resumes_and_enforces_server_token_count(tmp_path):
    case = BenchCase(
        case_id="c", suite_id="s", lane="long-context", messages=[{"role":"user","content":"x"}],
        scorer="strict_json_exact", expected={"answer": 7},
        metadata={"target_prompt_tokens": 10, "requested_position": .5, "family": "single"},
    )
    output = tmp_path / "results.jsonl"
    run_cases([case], client=FakeClient(), model="m", output_path=output)
    run_cases([case], client=FakeClient(), model="m", output_path=output)
    rows = read_jsonl(output)
    assert len(rows) == 1 and rows[0]["passed"]

    mismatch = tmp_path / "mismatch.jsonl"
    run_cases([case], client=FakeClient(prompt_tokens=9), model="m", output_path=mismatch)
    assert not read_jsonl(mismatch)[0]["passed"]


def test_summary_separates_scorecards_and_context():
    rows = []
    for length, passed in [(4096, True), (8192, True), (16384, False)]:
        rows.append({
            "variant":"ar", "lane":"long-context", "case_id":str(length), "passed":passed,
            "metadata":{"target_prompt_tokens":length,"requested_position":.5,"family":"single"},
            "telemetry":{"wall_s":1.0,"ttft_s":.1,"end_to_end_tokens_per_second":10.0},
        })
    summary = summarize_results(rows, effective_threshold=.9)
    assert summary["context"]["ar"]["effective_context_length"] == 8192
    assert summary["interpretation"]["single_intelligence_score"] is None


def test_summary_uses_explicit_baseline_and_reports_regressions():
    rows = [
        {"variant": "parent", "lane": "quality", "case_id": "a", "passed": True, "metadata": {}, "telemetry": {}},
        {"variant": "candidate", "lane": "quality", "case_id": "a", "passed": False, "metadata": {}, "telemetry": {}},
    ]
    summary = summarize_results(rows, baseline_variant="parent")
    comparison = summary["parity"]["candidate"]
    assert comparison["baseline"] == "parent"
    assert not comparison["zero_regression_gate"]
    assert comparison["regressions"] == [{"case_id": "a", "lane": "quality"}]


def test_public_scan_detects_tokens_and_user_paths(tmp_path):
    clean = tmp_path / "clean"
    clean.mkdir()
    (clean / "README.md").write_text("public documentation")
    assert scan_public_tree(clean)["ok"]
    fake_token = "h" + "f_" + "abcdefghijklmnopqrstuvwxyz1234"
    fake_path = "/" + "Users/example/private"
    (clean / "bad.txt").write_text(fake_token + "\n" + fake_path)
    result = scan_public_tree(clean)
    assert not result["ok"]
    assert {item["issue"] for item in result["failures"]} >= {"Hugging Face access token", "macOS user path"}


def test_public_result_export_omits_responses_paths_and_identifiers():
    row = {
        "schema_version": "1.0",
        "run_id": "private-run",
        "suite_id": "shiftedx-agentic-v1",
        "case_id": "repair",
        "lane": "agentic",
        "variant": "candidate",
        "passed": False,
        "score": 0,
        "score_max": 1,
        "error": "FileNotFoundError: /" + "Users/example/private.py",
        "response": {"content": "private output", "request_id": "request-private"},
        "telemetry": {
            "wall_s": 2.0,
            "prompt_tokens": 10,
            "harness_receipts": [{"tool": "read_file"}],
            "turns": [{"request_id": "private"}],
        },
        "metadata": {
            "agentic_control_profile": "shiftedx-harness-v1",
            "agentic_family": "repair_loop",
            "forbidden_calls": ["delete_file"],
        },
        "request_hash": "a" * 64,
    }
    public = sanitize_result_row(row)
    assert public["error_category"] == "evaluation_error"
    assert public["telemetry"] == {"wall_s": 2.0, "prompt_tokens": 10}
    assert public["metadata"] == {
        "agentic_control_profile": "shiftedx-harness-v1",
        "agentic_family": "repair_loop",
    }
    text = str(public)
    assert "private" not in text and ("/" + "Users/") not in text and "response" not in public


def test_public_manifest_export_keeps_revisions_and_drops_host_state():
    manifest = {
        "run_id": "private",
        "created_at": "private",
        "suite_id": "shiftedx-agentic-v1",
        "benchmark_version": "0.5.0",
        "model": "local-model",
        "config_sha256": "a" * 64,
        "tokenizer_fingerprint": "b" * 64,
        "planned_cases": 30,
        "variants": [{
            "label": "candidate",
            "request_overrides": {
                "reasoning_effort": "medium",
                "thinking": {"enabled": True},
                "private_path": "/" + "Users/example",
            },
        }],
        "environment": {"platform": "private-host"},
        "evaluated_artifact": {"repo_id": "Org/Model", "revision": "c" * 40},
        "benchmark_source": {"repo_id": "Shiftedx/shiftedx-bench", "revision": "d" * 40},
        "agentic_control_profile": "shiftedx-harness-v1",
    }
    public = sanitize_manifest(manifest)
    assert public["evaluated_artifact"]["revision"] == "c" * 40
    assert public["benchmark_source"]["revision"] == "d" * 40
    assert public["variants"][0]["request_overrides"] == {
        "reasoning_effort": "medium",
        "thinking": {"enabled": True},
    }
    assert "run_id" not in public and "environment" not in public and "model" not in public


def test_run_suite_accepts_immutable_post_publish_provenance():
    args = make_parser().parse_args([
        "run-suite",
        "--base-url", "http://example.invalid/v1",
        "--model", "served-model",
        "--output", "results.jsonl",
        "--config", "configs/suites-smoke-v1.json",
        "--suite", "agentic",
        "--hf-repo", "Org/Model",
        "--hf-revision", "a" * 40,
        "--benchmark-revision", "b" * 40,
    ])
    assert args.hf_repo == "Org/Model"
    assert args.hf_revision == "a" * 40
    assert args.benchmark_revision == "b" * 40