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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Validate the checked-in YUTABASE RepoSearch MiniEval without dependencies."""
|
| 3 |
+
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import hashlib
|
| 7 |
+
import json
|
| 8 |
+
from collections import Counter
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
from typing import Any, Iterable
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
ROOT = Path(__file__).resolve().parents[1]
|
| 14 |
+
EXPECTED_COMMIT = "d72605d05061b7853a8e9bf3eb73417e3a4457dc"
|
| 15 |
+
EXPECTED_TREE = "592f1b15b14fbe0ee29ef4e4769a43aace7e1e65"
|
| 16 |
+
EXPECTED_QUERY_IDS = {f"Q{number:02d}" for number in range(1, 28)}
|
| 17 |
+
EXPECTED_SPLIT_COUNTS = {"validation": 9, "test": 18}
|
| 18 |
+
EXPECTED_LANGUAGE_COUNTS = {"en": 13, "yue-Hant": 11, "mul": 3}
|
| 19 |
+
EXPECTED_CORPUS_COUNT = 73
|
| 20 |
+
PROHIBITED_MARKERS = (
|
| 21 |
+
"/Users/",
|
| 22 |
+
"/home/",
|
| 23 |
+
".codex/",
|
| 24 |
+
"BEGIN PRIVATE KEY",
|
| 25 |
+
"npm_",
|
| 26 |
+
"hf_",
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class ValidationError(RuntimeError):
|
| 31 |
+
pass
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def read_jsonl(path: Path) -> list[dict[str, Any]]:
|
| 35 |
+
records: list[dict[str, Any]] = []
|
| 36 |
+
with path.open(encoding="utf-8") as handle:
|
| 37 |
+
for line_number, line in enumerate(handle, 1):
|
| 38 |
+
if not line.strip():
|
| 39 |
+
raise ValidationError(f"{path}:{line_number}: blank JSONL row")
|
| 40 |
+
try:
|
| 41 |
+
value = json.loads(line)
|
| 42 |
+
except json.JSONDecodeError as error:
|
| 43 |
+
raise ValidationError(f"{path}:{line_number}: {error}") from error
|
| 44 |
+
if not isinstance(value, dict):
|
| 45 |
+
raise ValidationError(f"{path}:{line_number}: row must be an object")
|
| 46 |
+
records.append(value)
|
| 47 |
+
return records
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def require(condition: bool, message: str) -> None:
|
| 51 |
+
if not condition:
|
| 52 |
+
raise ValidationError(message)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def unique(records: Iterable[dict[str, Any]], key: str, label: str) -> dict[str, dict[str, Any]]:
|
| 56 |
+
indexed: dict[str, dict[str, Any]] = {}
|
| 57 |
+
for record in records:
|
| 58 |
+
value = record.get(key)
|
| 59 |
+
require(isinstance(value, str) and value != "", f"{label}: missing {key}")
|
| 60 |
+
require(value not in indexed, f"{label}: duplicate {key} {value!r}")
|
| 61 |
+
indexed[value] = record
|
| 62 |
+
return indexed
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def validate(root: Path = ROOT) -> dict[str, Any]:
|
| 66 |
+
manifest = json.loads((root / "source-manifest.json").read_text(encoding="utf-8"))
|
| 67 |
+
source = manifest.get("source", {})
|
| 68 |
+
require(
|
| 69 |
+
manifest.get("dataset_profile") == "yutabase.reposearch-minieval/0.1",
|
| 70 |
+
"unexpected dataset profile",
|
| 71 |
+
)
|
| 72 |
+
require(source.get("commit_sha") == EXPECTED_COMMIT, "manifest commit mismatch")
|
| 73 |
+
require(source.get("tree_sha") == EXPECTED_TREE, "manifest tree mismatch")
|
| 74 |
+
require(source.get("license") == "MIT", "manifest source license must be MIT")
|
| 75 |
+
|
| 76 |
+
artifacts = manifest.get("artifacts")
|
| 77 |
+
require(isinstance(artifacts, dict), "manifest artifacts must be an object")
|
| 78 |
+
for relative_path, expected in artifacts.items():
|
| 79 |
+
require(relative_path in {"data/corpus.jsonl", "data/validation.jsonl", "data/test.jsonl"}, f"unexpected artifact {relative_path}")
|
| 80 |
+
raw = (root / relative_path).read_bytes()
|
| 81 |
+
require(
|
| 82 |
+
hashlib.sha256(raw).hexdigest() == expected.get("sha256"),
|
| 83 |
+
f"{relative_path}: artifact SHA-256 mismatch",
|
| 84 |
+
)
|
| 85 |
+
require(
|
| 86 |
+
len(raw.splitlines()) == expected.get("rows"),
|
| 87 |
+
f"{relative_path}: artifact row-count mismatch",
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
source_license = (root / "SOURCE_LICENSES" / "YUTABASE-MIT.txt").read_bytes()
|
| 91 |
+
require(
|
| 92 |
+
hashlib.sha256(source_license).hexdigest() == source.get("license_sha256"),
|
| 93 |
+
"copied YUTABASE license does not match its pinned SHA-256",
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
allowlist_rows = manifest.get("allowlist")
|
| 97 |
+
require(isinstance(allowlist_rows, list) and len(allowlist_rows) == 12, "allowlist must contain 12 files")
|
| 98 |
+
allowlist = unique(allowlist_rows, "path", "allowlist")
|
| 99 |
+
for path, entry in allowlist.items():
|
| 100 |
+
require(not path.startswith("/") and ".." not in Path(path).parts, f"unsafe allowlist path: {path}")
|
| 101 |
+
require(len(entry.get("git_blob_sha1", "")) == 40, f"{path}: invalid Git blob")
|
| 102 |
+
require(len(entry.get("file_sha256", "")) == 64, f"{path}: invalid file SHA-256")
|
| 103 |
+
require(entry.get("source_kind") in {"markdown", "typescript"}, f"{path}: invalid source kind")
|
| 104 |
+
require(isinstance(entry.get("authority_class"), str), f"{path}: missing authority class")
|
| 105 |
+
|
| 106 |
+
corpus_rows = read_jsonl(root / "data" / "corpus.jsonl")
|
| 107 |
+
require(len(corpus_rows) == EXPECTED_CORPUS_COUNT, f"expected {EXPECTED_CORPUS_COUNT} corpus rows")
|
| 108 |
+
corpus = unique(corpus_rows, "chunk_id", "corpus")
|
| 109 |
+
source_prefix = f"{source['repo_url']}/blob/{EXPECTED_COMMIT}/"
|
| 110 |
+
corpus_text = []
|
| 111 |
+
for chunk_id, row in corpus.items():
|
| 112 |
+
path = row.get("path")
|
| 113 |
+
require(path in allowlist, f"{chunk_id}: source path is not allowlisted")
|
| 114 |
+
file_entry = allowlist[path]
|
| 115 |
+
for field in ("repo_id", "repo_url", "commit_sha", "tree_sha", "git_blob_sha1", "file_sha256", "source_kind", "authority_class"):
|
| 116 |
+
expected = source.get(field) if field in source else file_entry.get(field)
|
| 117 |
+
require(row.get(field) == expected, f"{chunk_id}: {field} mismatch")
|
| 118 |
+
require(row.get("license") == "MIT", f"{chunk_id}: license mismatch")
|
| 119 |
+
require(row.get("language") == "en", f"{chunk_id}: unexpected source language")
|
| 120 |
+
start = row.get("line_start")
|
| 121 |
+
end = row.get("line_end")
|
| 122 |
+
require(isinstance(start, int) and isinstance(end, int) and 1 <= start <= end, f"{chunk_id}: invalid line range")
|
| 123 |
+
text = row.get("text")
|
| 124 |
+
require(isinstance(text, str) and text != "", f"{chunk_id}: empty text")
|
| 125 |
+
require(len(text.splitlines()) == end - start + 1, f"{chunk_id}: text/line range mismatch")
|
| 126 |
+
require(
|
| 127 |
+
hashlib.sha256(text.encode("utf-8")).hexdigest() == row.get("chunk_sha256"),
|
| 128 |
+
f"{chunk_id}: chunk SHA-256 mismatch",
|
| 129 |
+
)
|
| 130 |
+
expected_url = f"{source_prefix}{path}#L{start}-L{end}"
|
| 131 |
+
require(row.get("source_url") == expected_url, f"{chunk_id}: source URL mismatch")
|
| 132 |
+
corpus_text.append(text)
|
| 133 |
+
|
| 134 |
+
query_rows: list[dict[str, Any]] = []
|
| 135 |
+
for split, expected_count in EXPECTED_SPLIT_COUNTS.items():
|
| 136 |
+
rows = read_jsonl(root / "data" / f"{split}.jsonl")
|
| 137 |
+
require(len(rows) == expected_count, f"{split}: expected {expected_count} rows")
|
| 138 |
+
for row in rows:
|
| 139 |
+
require(row.get("split") == split, f"{row.get('query_id')}: split mismatch")
|
| 140 |
+
query_rows.extend(rows)
|
| 141 |
+
|
| 142 |
+
queries = unique(query_rows, "query_id", "queries")
|
| 143 |
+
require(set(queries) == EXPECTED_QUERY_IDS, "query IDs must be exactly Q01 through Q27")
|
| 144 |
+
require(Counter(row.get("language") for row in query_rows) == EXPECTED_LANGUAGE_COUNTS, "language totals mismatch")
|
| 145 |
+
require(Counter(row.get("split") for row in query_rows) == EXPECTED_SPLIT_COUNTS, "split totals mismatch")
|
| 146 |
+
|
| 147 |
+
for query_id, row in queries.items():
|
| 148 |
+
query = row.get("query")
|
| 149 |
+
require(isinstance(query, str) and query.strip() == query and query, f"{query_id}: invalid query")
|
| 150 |
+
language = row.get("language")
|
| 151 |
+
languages = row.get("languages")
|
| 152 |
+
require(language in EXPECTED_LANGUAGE_COUNTS, f"{query_id}: invalid language")
|
| 153 |
+
require(isinstance(languages, list) and languages, f"{query_id}: languages must be non-empty")
|
| 154 |
+
if language != "mul":
|
| 155 |
+
require(language in languages, f"{query_id}: primary language missing from languages")
|
| 156 |
+
require(row.get("difficulty") in {"easy", "medium", "hard"}, f"{query_id}: invalid difficulty")
|
| 157 |
+
require(isinstance(row.get("query_type"), str) and row["query_type"], f"{query_id}: missing query type")
|
| 158 |
+
facts = row.get("expected_facts")
|
| 159 |
+
require(isinstance(facts, list) and facts and all(isinstance(f, str) and f for f in facts), f"{query_id}: invalid expected facts")
|
| 160 |
+
require(row.get("annotation_version") == "0.1", f"{query_id}: annotation version mismatch")
|
| 161 |
+
require(row.get("source_snapshot") == EXPECTED_COMMIT, f"{query_id}: source snapshot mismatch")
|
| 162 |
+
|
| 163 |
+
relevance = row.get("relevance")
|
| 164 |
+
require(isinstance(relevance, list) and relevance, f"{query_id}: missing relevance judgments")
|
| 165 |
+
relevant_ids: set[str] = set()
|
| 166 |
+
has_direct = False
|
| 167 |
+
for judgment in relevance:
|
| 168 |
+
require(isinstance(judgment, dict), f"{query_id}: judgment must be an object")
|
| 169 |
+
chunk_id = judgment.get("chunk_id")
|
| 170 |
+
grade = judgment.get("grade")
|
| 171 |
+
require(chunk_id in corpus, f"{query_id}: unknown relevant chunk {chunk_id!r}")
|
| 172 |
+
require(chunk_id not in relevant_ids, f"{query_id}: duplicate relevant chunk {chunk_id}")
|
| 173 |
+
require(type(grade) is int and 1 <= grade <= 3, f"{query_id}: invalid grade")
|
| 174 |
+
require(isinstance(judgment.get("rationale"), str) and judgment["rationale"], f"{query_id}: missing rationale")
|
| 175 |
+
relevant_ids.add(chunk_id)
|
| 176 |
+
has_direct = has_direct or grade == 3
|
| 177 |
+
require(has_direct, f"{query_id}: requires at least one grade-3 target")
|
| 178 |
+
|
| 179 |
+
hard_negatives = row.get("hard_negative_chunk_ids")
|
| 180 |
+
require(isinstance(hard_negatives, list) and hard_negatives, f"{query_id}: missing hard negatives")
|
| 181 |
+
require(len(hard_negatives) == len(set(hard_negatives)), f"{query_id}: duplicate hard negative")
|
| 182 |
+
for chunk_id in hard_negatives:
|
| 183 |
+
require(chunk_id in corpus, f"{query_id}: unknown hard negative {chunk_id!r}")
|
| 184 |
+
require(chunk_id not in relevant_ids, f"{query_id}: hard negative is also relevant: {chunk_id}")
|
| 185 |
+
|
| 186 |
+
checked_text = "\n".join(corpus_text + [json.dumps(row, ensure_ascii=False) for row in query_rows])
|
| 187 |
+
for marker in PROHIBITED_MARKERS:
|
| 188 |
+
require(marker not in checked_text, f"prohibited local/credential marker found: {marker!r}")
|
| 189 |
+
|
| 190 |
+
card = (root / "README.md").read_text(encoding="utf-8")
|
| 191 |
+
for required_fragment in (
|
| 192 |
+
"config_name: corpus",
|
| 193 |
+
"split: corpus",
|
| 194 |
+
"path: data/corpus.jsonl",
|
| 195 |
+
"config_name: queries",
|
| 196 |
+
"split: validation",
|
| 197 |
+
"split: test",
|
| 198 |
+
EXPECTED_COMMIT,
|
| 199 |
+
"not a universal code-search benchmark",
|
| 200 |
+
):
|
| 201 |
+
require(required_fragment in card, f"dataset card missing {required_fragment!r}")
|
| 202 |
+
|
| 203 |
+
return {
|
| 204 |
+
"dataset_profile": manifest["dataset_profile"],
|
| 205 |
+
"corpus_rows": len(corpus_rows),
|
| 206 |
+
"query_rows": len(query_rows),
|
| 207 |
+
"split_counts": dict(sorted(EXPECTED_SPLIT_COUNTS.items())),
|
| 208 |
+
"language_counts": dict(sorted(EXPECTED_LANGUAGE_COUNTS.items())),
|
| 209 |
+
"source_commit": EXPECTED_COMMIT,
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
def main() -> int:
|
| 214 |
+
try:
|
| 215 |
+
summary = validate()
|
| 216 |
+
except (OSError, KeyError, TypeError, ValueError, ValidationError) as error:
|
| 217 |
+
print(f"INVALID: {error}")
|
| 218 |
+
return 1
|
| 219 |
+
print(json.dumps({"valid": True, **summary}, ensure_ascii=False, indent=2, sort_keys=True))
|
| 220 |
+
return 0
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
if __name__ == "__main__":
|
| 224 |
+
raise SystemExit(main())
|