"""Do compressed-input misses trace to text hidden behind <<ccr:...>> markers? Decodes Headroom's table (markers kept as-is),
reports which rows/fields carry markers, and checks the row/field each question needs. Covers the earlier Qwen misses too."""
import json, csv, io, re
CCR = re.compile(r"<<ccr:[^>]*>>")
def table(c):
    marks = {}
    def ph(m): k = "@@%d@@" % len(marks); marks[k] = m.group(0); return k
    t = CCR.sub(ph, c); h = re.match(r"\[(\d+)\]\{([^}]*)\}\n", t); spec = [f.split(":")[0] for f in h.group(2).split(",")]
    rows = [dict(zip(spec, r)) for r in csv.reader(io.StringIO(t[h.end():])) if r]
    return rows, {(i, k) for i, r in enumerate(rows) for k, v in r.items() if "@@" in v}
out = {}
def check(name, c, needs):
    rows, mk = table(c)
    out[name] = {"ccr_markers": c.count("<<ccr:"), "rows": len(rows), "fields_with_markers": sorted({k for _, k in mk}),
                 "rows_with_markers": sorted({i for i, _ in mk}),
                 "question_rows": [{"q": q, "row": i, "field_needed": f, "behind_marker": (i, f) in mk, "any_marker_in_row": any(j == i for j, _ in mk)} for q, i, f in needs]}
sl = "payloads_slices/"
check("wiki_search_slice", open(sl + "wiki_search_slice.compressed.txt").read(),
      [("pageid of wk[5]", 5, "pageid"), ("wordcount of wk[20]", 20, "wordcount"), ("title of pageid wk[40]", 40, "title"), ("size of wk[70]", 70, "size"), ("title of first", 0, "title")])
check("synthetic_logs_slice", open(sl + "synthetic_logs_slice.compressed.txt").read(), [])
c = open("../payloads/wiki_search.compressed.txt").read(); c = json.loads(c) if c.startswith('"') else c
check("wiki_search_full", c, [("title of first", 0, "title"), ("pageid of wk[49]", 49, "pageid"), ("wordcount of wk[149]", 149, "wordcount"), ("snippet of wk[3]", 3, "snippet")])
json.dump(out, open("ccr_trace.json", "w"), indent=1)
for k, v in out.items(): print(k, v["ccr_markers"], v["fields_with_markers"], v["rows_with_markers"][:50], [(x["q"], x["behind_marker"], x["any_marker_in_row"]) for x in v["question_rows"]])
