#!/usr/bin/env python3
"""Archive SKILL.md run notes 2026-10-20 -> 2026-10-24 into references/ and leave a pointer."""
import os
BASE = "/home/kiro/.hermes/skills/social-media/x-account-monitor"
SK = os.path.join(BASE, "SKILL.md")
REF = os.path.join(BASE, "references", "run-notes-2026-10-20-to-10-24.md")
lines = open(SK).read().split("\n")
# 1-indexed 226..1042 inclusive
chunk = lines[225:1042]
assert chunk[0].startswith("#### RUN NOTE (2026-10-20)"), chunk[0]
assert "PITFALL (2026-10-24)" in "\n".join(chunk[-12:]), chunk[-12:]
header = ("# X Account Monitor -- run notes 2026-10-20 -> 2026-10-24\n\n"
          "Archived verbatim from SKILL.md on 2026-10-25 (SKILL.md was 82,249 chars; five run notes\n"
          "= 68,100 chars, so archiving restores headroom under the ~90K write limit).\n"
          "The newest run note inside SKILL.md always wins over anything here.\n\n---\n\n")
os.makedirs(os.path.dirname(REF), exist_ok=True)
open(REF, "w").write(header + "\n".join(chunk) + "\n")
pointer = ["<!-- Run notes 2026-10-20 -> 2026-10-24 (68.1K) archived verbatim to",
           "     references/run-notes-2026-10-20-to-10-24.md on 2026-10-25 (SKILL.md at 82.2K,",
           "     five notes would have blown the ~90K write limit). The newest note below always wins. -->",
           "",
           "<!-- CARRY-FORWARD PITFALLS (full detail in the archive file):",
           "  - `qualifying` is sorted globally by score, so `qual[:10]` is NOT the core top 10 -- filter",
           "    against IRRELEVANT/FILM first, then sort.",
           "  - `q[\"date\"]` is \"YYYY-MM-DD HH:MM UTC\" (not %z); `createdAt` in raw IS %a %b %d %H:%M:%S %z %Y.",
           "  - qualifying records have NO `id` -- parse it out of `url` before matching to raw.",
           "  - a zero-hit offer grep means nothing when the owning handle is stale; check max(createdAt) first.",
           "  - an offer can be live AND absent from the data (aged out of window, never re-posted).",
           "  - never infer dormancy from the printed best; recompute best over IN-WINDOW originals only.",
           "  - a fresh finding can be a RE-OFFER at a different price: grep the NUMBER, not the brand.",
           "-->",
           ""]
out = lines[:225] + pointer + lines[1042:]
open(SK, "w").write("\n".join(out))
print("archive bytes:", os.path.getsize(REF))
print("skill bytes:", os.path.getsize(SK))
print("tail:", "\n".join(out[-6:]))
