"""H2 re-check: count legacy chunks with scope == agent_self per agent store. Provenance: written by worldtree-dev against the v1.0.0b192 tag and delivered 2026-09-30 (thread 01M3SGEQDRQD7DWBVT4K73FAHP) for the U11b step-5 gate. Run it VERBATIM, right before each instance's legacy-data deletion, inside that instance's worldtree-api container (it pins the chromadb that wrote the stores): docker exec -i python - < scripts/wt-h2-count.py ; echo "exit=$?" Exit 0 = global count 0, go. Exit 2 = non-zero: STOP, send the output to worldtree-dev before deleting anything. Attach the full stdout to the stamp. ⚠ chromadb rewrites HNSW sidecars and the sqlite file ON OPEN: the store's bytes change even though no row does. Compare live sha256s against the archive manifest BEFORE running this, or run it against a `cp -a` copy (point WORLDTREE_DATA_PATH at the copy's root). Opens each /agents//memory/.chroma with chromadb and counts rows whose metadata scope is "agent_self" (the legacy reader's own where-clause). A missing store dir or collection counts as 0 and says so. Exit 0 when the global total is 0, exit 2 otherwise. """ import os, sys from pathlib import Path import chromadb root = Path(os.environ["WORLDTREE_DATA_PATH"]) agents = sys.argv[1:] or ["forseti", "lofn", "mimir"] total = 0 for agent in agents: d = root / "agents" / agent / "memory" / f"{agent}.chroma" if not d.is_dir(): print(f"{agent}: store absent ({d}) -> agent_self=0") continue client = chromadb.PersistentClient(path=str(d)) try: col = client.get_collection("memory_chunks") except Exception as exc: print(f"{agent}: no memory_chunks collection ({type(exc).__name__}) -> agent_self=0") continue n_all = col.count() n_self = len(col.get(where={"scope": "agent_self"}, include=[])["ids"]) total += n_self print(f"{agent}: rows={n_all} agent_self={n_self}") print(f"H2 global agent_self count: {total}") sys.exit(0 if total == 0 else 2)