"""What the shorter traces stop doing: reconsideration and verification markers in the four 256-question cells. uv run --no-project --with zstandard --with tokenizers python code/recheck_markers.py path/to/Qwen3.5-9B/tokenizer.json Decodes each trace's thinking token IDs with the Qwen3.5-9B tokenizer, finds reconsideration and verification markers with their token positions, and reports per arm (Table 6 and Figure 1c): - markers per trace and per 1,000 thinking tokens (all 256 questions); - markers in the first 2,048 thinking tokens; - on the 64-question forced-readout subset, markers before and after the trace's own stabilization point (earliest 2,048-token grid point after which every forced readout's answer key equals the trace's final key; no matching terminal run -> the full thinking length). Intervals: paired question bootstrap within source, 10,000 draws, seed 20261130. Exploratory (not registered). Writes outputs/recheck_markers.json, which equals stats/audit/recheck_markers.json. """ import bisect, json, os, random, re, statistics, sys from pathlib import Path from tokenizers import Tokenizer sys.path.insert(0, str(Path(__file__).resolve().parent)) from common import RECORDS, ROOT, iter_jsonl # noqa: E402 N, TOK = str(RECORDS), sys.argv[1] ARMS = {"parent": ("confirm_baseline", "base"), "forward": ("confirm_full_mlp_forward", "base_mlp_all"), "reverse": ("confirm_full_mlp_reverse", "concise_restore_mlp_all"), "donor": ("confirm_concise", "concise")} tok = Tokenizer.from_file(TOK) cache = {} def piece(i): s = cache.get(i) if s is None: s = cache[i] = tok.decode([i], skip_special_tokens=False) return s RECONSIDER = re.compile(r"\b(Wait|wait|Hmm|Hold on|Alternatively|On second thought|reconsider|Let me re-?think|Let me re-?read)\b") VERIFY = re.compile(r"(double[- ]check|Let me (?:verify|check|confirm|re-?(?:check|examine|verify|calculate|compute|evaluate|do))|\bre-?check|sanity check|\bverify\b)", re.I) def markers(ids): pieces = [piece(i) for i in ids]; cum = []; c = 0 for p in pieces: c += len(p); cum.append(c) text = "".join(pieces) pos = lambda m: bisect.bisect_right(cum, m.start()) return [pos(m) for m in RECONSIDER.finditer(text)], [pos(m) for m in VERIFY.finditer(text)] def readout_fixed_points(d): sub = [x for x in os.listdir(os.path.join(N, d)) if x.startswith("readout_subset_")] if not sub: return {} sd = os.path.join(N, d, sub[0], "scored") final = {} for l in open(os.path.join(sd, "traces.scored.jsonl")): r = json.loads(l); final[r["id"]] = r.get("answer_key") reads = {} for l in open(os.path.join(sd, "readouts.scored.jsonl")): r = json.loads(l); reads.setdefault(r["id"], []).append((int(r["t"]), r.get("answer_key"))) fp = {} for i, rs in reads.items(): rs.sort(); k = len(rs) while k > 0 and final.get(i) is not None and rs[k - 1][1] == final[i]: k -= 1 fp[i] = rs[k][0] if k < len(rs) else None # None -> full length for i in final: fp.setdefault(i, None) return fp per = {} for arm, (d, stem) in ARMS.items(): fp = readout_fixed_points(d) rows = {} if True: for r in iter_jsonl(os.path.join(N, d, f"{stem}.jsonl")): ids = r.get("thinking_ids") or [] rc, vf = markers(ids); L = len(ids) row = {"area": r.get("area"), "L": L, "rc": len(rc), "vf": len(vf), "rc_early": sum(p < 2048 for p in rc), "vf_early": sum(p < 2048 for p in vf), "early_len": min(L, 2048)} if r["id"] in fp: s = fp[r["id"]] if fp[r["id"]] is not None else L s = min(s, L) row.update(sub=True, stab=s, rc_after=sum(p >= s for p in rc), vf_after=sum(p >= s for p in vf), rc_before=sum(p < s for p in rc), vf_before=sum(p < s for p in vf), after_len=L - s) rows[r["id"]] = row per[arm] = rows print(arm, "traces", len(rows), "subset", sum(1 for v in rows.values() if v.get("sub")), flush=True) ids = sorted(set.intersection(*(set(v) for v in per.values()))) sub_ids = [i for i in ids if all(per[a][i].get("sub") for a in ARMS)] strata = {} for i in ids: strata.setdefault(per["parent"][i]["area"], []).append(i) rng = random.Random(20261130) draws = [[rng.choice(v) for s, v in sorted(strata.items()) for _ in v] for _ in range(10000)] sstr = {} for i in sub_ids: sstr.setdefault(per["parent"][i]["area"], []).append(i) sdraws = [[rng.choice(v) for s, v in sorted(sstr.items()) for _ in v] for _ in range(10000)] def ci(stat, base, dr): vals = sorted(stat(s) for s in dr); return round(stat(base), 3), round(vals[249], 3), round(vals[9749], 3) mean = lambda arm, key: (lambda s: statistics.fmean(per[arm][i][key] for i in s)) diff = lambda a, b, key: (lambda s: statistics.fmean(per[a][i][key] - per[b][i][key] for i in s)) rate = lambda arm, key, lkey: (lambda s: 1000 * sum(per[arm][i][key] for i in s) / max(1, sum(per[arm][i][lkey] for i in s))) out = {"n": len(ids), "n_subset": len(sub_ids)} for key in ("rc", "vf"): out[f"{key}_per_trace"] = {a: ci(mean(a, key), ids, draws) for a in ARMS} out[f"{key}_per_1k_thinking"] = {a: ci(rate(a, key, "L"), ids, draws) for a in ARMS} out[f"{key}_early_per_1k"] = {a: ci(rate(a, f"{key}_early", "early_len"), ids, draws) for a in ARMS} out[f"{key}_forward_minus_parent_per_trace"] = ci(diff("forward", "parent", key), ids, draws) out[f"{key}_reverse_minus_donor_per_trace"] = ci(diff("reverse", "donor", key), ids, draws) out[f"{key}_early_forward_minus_parent_per_1k"] = ci(lambda s, k=key: rate("forward", f"{k}_early", "early_len")(s) - rate("parent", f"{k}_early", "early_len")(s), ids, draws) for a in ARMS: out[f"{key}_after_stab_per_trace_{a}"] = ci(mean(a, f"{key}_after"), sub_ids, sdraws) out[f"{key}_before_stab_per_trace_{a}"] = ci(mean(a, f"{key}_before"), sub_ids, sdraws) out[f"{key}_after_stab_per_1k_{a}"] = ci(rate(a, f"{key}_after", "after_len"), sub_ids, sdraws) removed_after = lambda s, k=key: sum(per["parent"][i][f"{k}_after"] - per["forward"][i][f"{k}_after"] for i in s) removed_all = lambda s, k=key: sum(per["parent"][i][k] - per["forward"][i][k] for i in s) out[f"{key}_share_of_removed_markers_after_stab_forward"] = ci(lambda s: removed_after(s) / removed_all(s) if removed_all(s) else float("nan"), sub_ids, sdraws) (ROOT / "outputs").mkdir(exist_ok=True) json.dump(out, open(ROOT / "outputs/recheck_markers.json", "w"), indent=1) for k, v in out.items(): print(k, ":", v)