"""Diff patches and regex-dense sources for the structured-output slice. The requested structured slice is "JSON, YAML, SQL, regular expressions, diff patches". JSON/YAML/TOML/SQL fall out of the repositories directly; the other two need explicit work: * **Diff patches** are real `git log -p` output. The repositories are cloned shallow, so history is fetched with `--deepen` first; without that there is exactly one commit and no patches at all. * **Regexes** are not a file type, so instead of inventing them we promote genuinely regex-dense real sources (lexers, validators, route matchers) into this slice, where they carry real patterns in real context. """ from __future__ import annotations import os import re import subprocess from collect import doc_id from sources import RAW, REPOS COMMIT_SEP = "===COMMIT===" # A slash-delimited literal only counts if it actually contains a regex # metacharacter, and is not preceded by ':' or a word character. Without those # guards every `https://host/path` in the corpus reads as a regex and the # promotion below swallows most of the JavaScript in the repositories. REGEX_LITERAL = re.compile( r"(?= max_per_repo: break if not (min_chars <= len(chunk) <= max_chars): continue if "diff --git" not in chunk or "Binary files" in chunk: continue # skip patches that are almost entirely lockfile / generated churn if re.search(r"diff --git .*(package-lock\.json|Cargo\.lock|\.min\.)", chunk): continue docs.append({ "id": doc_id(chunk), "domain": "structured", "source": repo, "license": lic, "url": url, "path": f"git-history/{repo}.patch", "lang": "diff", "text": chunk, "origin": "git_diff", "split_hint": None, }) got += 1 return docs def promote_regex_dense(docs, min_hits=8, min_density=0.02, max_promote=140): """Reclassify the most regex-heavy real sources into the structured slice. Hard-capped at `max_promote`: a threshold alone is fragile to calibrate, and an over-firing detector would turn the structured slice into a dumping ground for ordinary JavaScript. """ scored = [] for d in docs: if d.get("origin") != "repo_file" or d["domain"] == "structured": continue if d["lang"] not in ("javascript", "typescript", "python", "rust", "cpp"): continue hits = len(REGEX_LITERAL.findall(d["text"])) lines = d["text"].count("\n") + 1 density = hits / lines if hits >= min_hits and density >= min_density: scored.append((density, hits, d)) scored.sort(key=lambda t: (-t[0], -t[1], t[2]["id"])) for _, _, d in scored[:max_promote]: d["domain"] = "structured" d["origin"] = "regex_dense" return min(len(scored), max_promote)