"""Census sibling: TOKEN-SPACE emissions, the family the colon predicate misses.

The v0.65.0 census counted literals shaped `"TOKEN:`. spt-store/src/gate.rs emits
`"ACCESS_REFUSED {surface} ...` — same all-caps machine-shaped token, delimited by
a SPACE instead of a colon, and therefore invisible to that census. This measures
the family and asks the only question that matters for the lane: does any test
parse one?
"""
import re
import subprocess

import sys
SHA = sys.argv[1] if len(sys.argv) > 1 else "4d6007ac"
MACRO = re.compile(r"\b(eprintln!|println!|eprint!|print!)\s*\(")
# an all-caps token at the very start of a literal, followed by a space
TOKEN_SPACE = re.compile(r'"([A-Z][A-Z0-9_]{2,}) ')
TOKEN_COLON = re.compile(r'"([A-Z][A-Z0-9_]{2,}):')

files = subprocess.run(
    ["git", "ls-tree", "-r", "--name-only", SHA, "--", "crates/"],
    capture_output=True, check=True,
).stdout.decode().splitlines()
src = [f for f in files if re.match(r"crates/[^/]+/src/.*\.rs$", f)]
tst = [f for f in files if re.match(r"crates/[^/]+/tests/.*\.rs$", f)]

def blob(p):
    return subprocess.run(
        ["git", "show", f"{SHA}:{p}"], capture_output=True, check=True
    ).stdout.decode("utf-8", "replace")

def truncate_at_test_module(lines):
    for i, ln in enumerate(lines):
        if ln.strip() == "#[cfg(test)]":
            for nxt in lines[i + 1:i + 4]:
                s = nxt.strip()
                if not s:
                    continue
                if s.startswith("mod ") or s.startswith("pub mod "):
                    return lines[:i]
                break
    return lines

hits = []
for path in sorted(src):
    lines = truncate_at_test_module(blob(path).split("\n"))
    for j, ln in enumerate(lines):
        for tok in TOKEN_SPACE.findall(ln):
            if TOKEN_COLON.search(ln):
                continue  # already in the colon census
            macro = None
            for k in range(j, max(-1, j - 5), -1):
                m = MACRO.search(lines[k])
                if m:
                    macro = m.group(1)
                    break
            if macro:
                hits.append((f"{path}:{j+1}", tok, macro))

toks = sorted({t for _, t, _ in hits})
print(f"TOKEN-SPACE emission sites: {len(hits)}  distinct tokens: {len(toks)}")
from collections import Counter
print("by crate: " + ", ".join(f"{c} {n}" for c, n in sorted(
    Counter(p.split("/")[1] for p, _, _ in hits).items())))
for loc, tok, mac in hits:
    print(f"  {loc}\t{tok}\t{mac}")

print("\nCONSUMERS — a test that reads a stderr/log surface AND mentions one of these tokens:")
SURFACES = ("stderr", ".log", "read_to_string", "daemon_stderr", "sink_path", "from_utf8_lossy")
found = 0
for path in sorted(tst):
    text = blob(path)
    if not any(s in text for s in SURFACES):
        continue
    hit = sorted({t for t in toks if (t + " ") in text})
    if hit:
        found += 1
        print(f"  {path}\t{len(hit)}\t{','.join(hit)}")
if not found:
    print("  none")
