#!python3 """Route 128 step (2)+(3): the reversion sweep, extended to every prefix and joined to QUESTIONS.md. Endpoints only, no compute on the documents themselves: * every candidate path comes from the served return records (`revision_path` of every return that carries a revision) — the walk of ids 1..1530 was already made and is cached in the local index; * `/history/` gives the version list; a document is a *three-version revert* when sha(v3) == sha(v1) != sha(v2) and the served `/docs/` bytes hash to v1; * the served `QUESTIONS.md` and the discarded v2 of `QUESTIONS.md` give each ledger row's status, so the served status can be printed beside the status the discarded revision carried; * the row's own `records` column is the one-level citation list for that ledger. All paths are relative: the script finds the workspace root from its own location. No absolute paths are written into any output. """ import hashlib, json, os, re, sys HERE = os.path.dirname(os.path.abspath(__file__)) ROOT = os.path.abspath(os.path.join(HERE, "..", "..", "..", "..")) # workspace root sys.path.insert(0, os.path.join(ROOT, ".solveathome", "tools")) import sah RUN = os.environ.get("SAH_RUN", open(os.path.join(ROOT, ".solveathome", "private", "current_launch_id")).read().strip()) CACHE = os.path.join(HERE, "cache") STATE = os.path.join(ROOT, ".solveathome", "private") LAUNCH = json.load(open(os.path.join(STATE, "runs", RUN, "launch.json"))) BIND = json.load(open(os.path.join(STATE, "device-binding.json"))) IDEN = sah.identity() H = {"Authorization": "Bearer " + sah.cred(), "Accept": "application/json", "X-Model": IDEN["model"], "X-Effort": IDEN["effort"], "X-Department": BIND["department_id"], "X-Session": LAUNCH["session"], "X-Attempt": LAUNCH["attempt_id"], "User-Agent": "deepseek-harness/sah-tools " + sah.TOOL_VERSION} sha = lambda t: hashlib.sha256(t.encode()).hexdigest() def get(url, root=False): """GET with an on-disk cache. `root=True` reads at the server root (/files/).""" key = hashlib.sha256(("root:" + url if root else url).encode()).hexdigest()[:24] cp = os.path.join(CACHE, key + ".txt") if os.path.exists(cp): return open(cp, errors="replace").read() full = (sah.SERVER + url) if root else (sah.PROJECT + url) x, s, t = sah.request(RUN, "GET", full, headers=H) if s != 200: return None open(cp, "w").write(t) return t def history(path): t = get("/history/" + path) return json.loads(t) if t else None def classify(path): """Return the audit record for one candidate path.""" h = history(path) served = get("/docs/" + path) rec = {"path": path, "prefix": path.split("/")[0], "n_versions": 0, "kind": "no-history"} if h is None: return rec vs = h.get("versions") or [] rec["n_versions"] = len(vs) rec["version_shas"] = [v.get("content_sha") for v in vs] rec["version_returns"] = [v.get("return_id") for v in vs] rec["served_sha"] = sha(served) if served is not None else None if h.get("skipped"): rec["skipped"] = h["skipped"] if len(vs) == 1: rec["kind"] = "single_version_control" return rec if len(vs) == 3: s1, s2, s3 = rec["version_shas"] if s3 == s1 and s2 != s1: rec["kind"] = "three_version_revert" rec["discarded_sha"] = s2 rec["discarded_return"] = rec["version_returns"][1] rec["served_is_v1"] = (rec["served_sha"] == s1) if not rec["served_is_v1"]: rec["kind"] = "three_version_pattern_but_served_elsewhere" else: rec["kind"] = "three_version_other_pattern" else: rec["kind"] = "other" return rec ROW = re.compile(r"^\|\s*`(Q-[a-z0-9-]+)`\s*\|\s*([A-Z]+)\s*\|\s*([^|]*)\|\s*([^|]*)\|") def parse_rows(text): """QUESTIONS.md rows -> {slug: {status, question, verdict, records}}. The served table's row shape is | `Q-slug` | STATUS | question | verdict | [records] | and the document has two sections, so a slug may appear more than once: later rows win. """ out = {} for ln in (text or "").splitlines(): m = ROW.match(ln) if m: slug, status, question, verdict = m.groups() out[slug] = {"status": status.strip(), "question": question.strip(), "verdict": verdict.strip(), "records": [int(x) for x in re.findall(r"(\d{1,4})", ln.split("|")[-2])]} return out def ledger_id(path): t = get("/docs/" + path) or "" m = re.search(r"