# Harvest-Curator Agent — Persistent Memory

> **Persistent diary cross-session.** Auto-injected first ~200 lines at spawn (L1 HOT).
> Update BEFORE every stop. Tiered Memory v1: **L1 self-target ≤17.1KB (hook read-limit 24.4KB)** · L2 `archive/` on-demand · L3 RAG `search_memory` just-in-time. Keep entry ≤ 1.5K chars.
> **NEW agent 2026-06-07** (adopt AI_INFRA Harness 1 — H2 harvest-integrity; TÁCH khỏi tooling-auditor H1 per anh-mandate "H1/H2 hay quên+nhầm → riêng-biệt").

---

## 🎯 Role baseline
H2 harvest-MD-integrity auditor **SOLUTION_ERP-self**. Read-only + **propose-only** (em main = single-writer, VERIFY→APPEND B3; ngoại lệ duy nhất = diary chính mình khi em main cho phép). Verify HARVEST mỗi session ĐỦ+ĐÚNG **5-trục**: Coverage · Completeness · Fidelity-FLAG · Placement · Corruption. @session-end GATE trước đóng + gom run-folder; @session-start báo harvest mới + delta mồ-côi + orphan run vs ledger.

## 🚫 Split boundary
- ✅ MINE: harvest-memory integrity (5-trục) + spawn-record 4-field + run-folder gom (Harness-10) + delta mồ-côi — SOLUTION_ERP-self
- ❌ NOT: tooling/skill/plugin/docs-freshness → **tooling-auditor** (H1) · corpus/RAG/deploy → cicd-monitor · Fidelity tự-phán "bịa" → escalate **reviewer** · write/decide → em main

## 📋 Baseline state (re-ground S99)
- **Spawn-record 4-field SE:** `{task · verdict · learned · surprise}` (memoryDelta R1 contract HMW).
- **agent-memory:** 12 folder `.claude/agent-memory/<role>/MEMORY.md` (placement target — S107 +office-document seed). Cap L1 = 25.6KB (H11 `memory-archive-gate.ps1`); hook self-target 17.1KB.
- **Run-trace (Harness-10, thay wave-folder Harness-2):** `.claude/workflows/runs/<run-id>/` git-TRACKED, FLAT (`run.md` + `sub-*.md` + `*-synthesis.md`); 5 run cũ 06-18 giữ `sub-md/`+`harvest/` subfolder (dual-accept). **🧊 Ledger `runs/_ledger.md` RETIRED H18-B S100** (true-catch 0/15 lifetime + mode-A coverage 0/8; 15 row frozen giữ lịch-sử) — **orphan-authority = folder-scan:** folder có `run.md` mà `*-synthesis.md` VẮNG/0-byte. Mode-A ephemeral (S98 note): HMW default KHÔNG tạo run-folder = by-design, vắng folder ≠ orphan.
- **Nền H2 gốc:** `session-end.md` Phase 1.5 §L.b = 3/5 trục; sub này nâng +Fidelity-escalate +Placement = đủ 5.

## ⚠️ Anti-patterns
❌ Tự ghi/overwrite memory ngoài diary mình (propose→em main APPEND) · ❌ tự phán Fidelity "bịa" (→ reviewer) · ❌ tick "đã flush?" nhị-phân (phải đủ 5-trục) · ❌ đụng tooling-freshness (đó là tooling-auditor) · ❌ G-015 overclaim "read-only enforced" · ❌ final-message trả narrative memory-curation thay verdict (#53 return-garble).

## ⭐ Methods (giữ vĩnh viễn — đúc từ S50-S66, verbatim → `archive/2026-06.md`)
1. Chunk-count qua **RAG MCP `list_projects`**, KHÔNG shell-probe Qdrant (MCP trỏ host khác → false-negative).
2. Sub killed-mid-write → verify tail **terminates-clean** (`wc -l` + closing-Tag), KHÔNG chỉ `-size 0` (partial-write ≠ 0-byte).
3. Detect interrupted-session: so **mtime agent-memory vs closeout-commit timestamp**.
4. Commit-stat "có append" ≠ "có fanout-record" — classify bằng **grep tag session + mtime**, đừng tin stat.
5. Extract added-bullet trong diff: `grep "^+-"` (filter `grep -v "^[+-][+-]"` NUỐT dòng `+- **…`).
6. `tail -c N` cắt giữa UTF-8 multibyte in ra ký-tự-thay-thế GIẢ — corruption thật = **grep literal U+FFFD** (đừng viết literal đó vào L1 kẻo tự false-alarm).
7. Mojibake quoted-cũ vs mới: **arithmetic line-shift** = đúng số dòng insert phía trên → cũ.
8. Sub die không để delta → **commit-body = spawn-evidence** (S60 reviewer tự khai trong msg).
9. U+FFFD self-quote trong file MÌNH + archive = quoted-evidence cố-ý — đừng false-alarm.
10. Trend self-tag session-drift (cicd s50→s51 · inv s56→s57 · inv s57bis→s58 · cicd s98→s97 ×4): entry mới soi tag vs session thật.
11. **Đếm role-dir từ path-field DÍNH file root-level:** `git ls-files .claude/agent-memory/ | cut -d/ -f3 | sort -u` nuốt `memory-budget.json` + `.mfe-state.json` → ra 14 thay 12 (tự-bắt S123). Guard: `awk -F/ 'NF>3{print $3}'` hoặc `find -mindepth 1 -maxdepth 1 -type d`.
12. **Mẫu-số có NGÀY-ĐO — hỏi "đếm LÚC NÀO" trước khi so:** count freeze ở commit-time; nếu chính việc-đo sinh artifact mới (run-folder retro-harvest) thì mẫu-số tự lỗi-thời (S123: em-main 25 = baseline `48e0d26`@11:48, thật 26 sau `22ea8c5`@14:28). Verdict khớp ≠ mẫu-số khớp.
13. **git MÙ thư-mục RỖNG** → empty-dir residue (stray cwd-relative `<runfolder>/.claude/agent-memory/<role>/`, role-dir chưa seed) KHÔNG BAO GIỜ hiện `porcelain --ignored`. Quét bằng `find .claude -type d -empty`, đừng tin git.

## 📅 Recent activity (FIFO — older → `archive/`)
> **S50→S66 (14 entry) verbatim → `archive/2026-06.md`** — curate S99 (hook 19.9KB > self-target 17.1KB), moved-not-cut grep-verified 5/5 + entry-count 14. Digest: verified-runtime S50 · GATE PASS 5/5 ×6 (S50/51/58/59/63/66) · chuỗi 4-MISS post-S57bis rushed-closeout + reviewer die ×3 (S57bis ×2 + S60) · cicd over-cap saga 32→86.8KB → curate S66 86.8→28.9KB.
> **S98→S101 (4 entry) verbatim → `archive/2026-07.md`** — self-compact 2026-07-10 @S109-start (plan đứng từ S107/S108; block-md5 `e2b906336b4cdad3034329cffc09525a` moved-not-cut). Digest: S98/S99/S100 @start-report chuỗi CLEAN (3 orphan-append S97 đóng · cicd #485 orphan-sane arithmetic-fidelity · tooling live-delta bootstrap) · S101 GATE PASS 5/5 + AS-10 Fidelity-call LEGIT-KEEP (keep-if-correct, 0 fabrication).
> **S102→S106 (5 entry) verbatim → `archive/2026-07.md`** — self-compact 2026-07-12 @S111-start HOOK-directed (plan ĐỨNG từ S107/S108/S110; mở-rộng +S106 để đạt <17.1KB; block-md5 `3163566688e277f628daf349c1230142` 6652B moved-not-cut, giữ file-order gốc — S106 nằm trước S105-close). Digest: S102 CATCH ×2 (reviewer cut-not-moved f229b07 → recover + dead-satellite pointer) · S104-retro self-record blind-spot gate-agent · S105 @start mồ-côi-×1-đóng / @close moved-not-cut byte-level tooling 19380→16305 · S106 ref-table = S98-snapshot benign + cicd-not-spawned-S105 resolved-false-alarm.
> **S107→S109 (5 entry) verbatim → `archive/2026-07.md`** — self-compact 2026-07-12 @S113-end HOOK-directed (plan ĐỨNG từ S107-end/S108; +S109 đạt <17.1KB; block-md5 `75b69b41534cb67cdf349b6e1164d47f` 5623B moved-not-cut, file-order verbatim). Digest: S107 md5-3of3 all-clean flip-8 · S107-end/S108 GATE PASS 5/5 (od-12th-seed · engine-label-first md5-4of4) · S109-start sleep-resume disk-MATCH + 0-gist-new + self-compact-S98→S101-exec (luot-a-transcript-lost) · S109-sleep-P1 gather 5+5-gist propose-only.
> **S110→S111 (4 entry) verbatim → `archive/2026-07.md`** — self-compact 2026-07-13 @S116-end HOOK-directed (SELF 20KB>read-limit; plan ĐỨNG từ S114-start; block-md5 `640f80e452b42badb12bc2a3c57cf9f7` 6638B moved-not-cut, file-order verbatim, anchor 4×{L1=0·AR=1}). Digest: S110-start rerun-DEDUP luot-sang-transcript-lost (gist byte-exact `cat-file`==disk) · S110-end GATE PASS 5/5 close-gate-15of15 garble-x2-recovered + FFFD-baseline-1-selfquote seed · S111-start self-compact-S102→S106-EXECUTED + gist-frozen-x2 · S111-end GATE PASS 5/5 close-gate-0of16 h22-first-subtask-runtrace + `--stat`-truncate-trap.
> **S113 ×2 entry (start+end) verbatim → `archive/2026-07.md`** — self-compact 2026-07-14 @S118-start (plan ĐỨNG từ S117-end soft-due; block-md5 `0c782c7cebc8f1d8b16f65b8e1474820` 4064B moved-not-cut, file-order verbatim, anchor 2×{L1=0·AR=1}). Digest: S113-start MOSTLY-CLEAN 0-orphan/20 + 2-coverage-gap propose (impl-be thiếu Supplier-BE · test-spec thiếu narrative; embedded-synth pattern mới · maxdepth-1 false-flag lesson) · S113-end GATE PASS 5/5 0/21 (2-gap CLOSED idempotent · wal-flush-landing 6-diary · Fidelity `5fa11b5` file-list exact · nit journal-jsonl dead-pointer + backfill-22-vs-37).
> **S114/S116/S117 (3 entry) verbatim → `archive/2026-07.md`** — self-compact 2026-07-15 @S123-start HOOK-directed (plan ĐỨNG từ S119-start soft-due; block-md5 `3d7033e0253c3dfaeba7aad1a1067efe` 7163B moved-not-cut, file-order verbatim, anchor 3×{L1=0·ARC=1}, append-BEFORE-trim m#S119). Digest: S114-start ALL-CLEAN 0/21 garble-×2-captured + 4-BUG-captured · S116-end GATE PASS 5/5 0/24 (review-Lane2 mirror-gap · DOUBLE session-drift-tag m#10 · Fidelity 7-file exact) · S117-end GATE PASS 5/5 0-orphan (fable-real spec-only dual-accept · 3 nit S118-mistag/test-500/cicd-garble-claim → ALL FIXED @S118 `1b8b065`).
- **2026-07-17 (S132 DỒN S127→S131 — re-spawn sau session-limit-death S131 [E-011]; by em-main VERIFY→APPEND):** GAPS-FOUND→**CLOSED-in-session**. Coverage PASS **31 run.md/33 dirs** (2 = spec-only S117 by-design; interim "26" = S123-baseline stale) · **0-orphan/31** · 2 folder mới S127+S129 flat dạng-1 đủ · roster-harvest đủ (reviewer S127/S128×2/S129/S131×2 · inv-cb S128/S129 · cicd S128/S129; wave-workers generic = no-diary by-design; S130 em-main-solo 0-spawn = không nợ). Fidelity STRONG (entries tự-flag overclaim mình) · Corruption PASS (FFFD 0/3 · porcelain agent-memory 0 mồ-côi; inv-cb "�24.1KB" = m#6 tail-cut artifact tự-bắt). Placement FLAG ×2 (git-invisible m#13): 2 dirs H24 RỖNG since-S123 + stray S119 → em-main CLOSED cùng phiên (seed 2 diary retro từ `sub-lead-{view,omission}.md` 8379B/9967B + rmdir stray 3-dir-rỗng). Lesson: (1) folder S127 chứa subs tag S128 = **trigger-session naming**, KHÔNG gap; (2) `sort -u -k2` collapse entry cùng-pattern = measurement-artifact — targeted-grep recover; (3) H24 knowledge sống CHỈ ở run-folder+counter-json ⇒ **per-ROLE diary = lớp không ai sở-hữu** tới khi seed (E-011/GAP-3).
> **S118 (start+end) + S119-start (3 entry) verbatim → `archive/2026-07.md`** — self-compact 2026-07-16 @S125-start HOOK-directed (SELF 22.5KB>read-limit; block-md5 `9f7a11dcd057a27d03b06843bbdf2274` 6252B moved-not-cut, file-order verbatim, anchor 3×{L1=0·ARC=1}). Digest: S118-start ALL-CLEAN 0/26 (3nit-S117-fixed `1b8b065` · FFFD-baseline 1→0 self-quote-left-L1) · S118-end GATE PASS 5/5 0-orphan-no-runtrace (cicd-garble-×2-return-label-nit em-main-FIXED) · S119-start ALL-CLEAN 0/26 77/77-tracked + LIVE mid-flight catch (sibling append-BEFORE-trim=safe-order · m#5 archive-diff-grep-swallow self-caught).
> **S123→S125 (2 entry @start RE-REPORT) verbatim → `archive/2026-07.md`** — self-compact 2026-07-18 @S140-gate HOOK-directed (SELF 19.7KB>17.1 read-limit; block-md5 `cbcd17aba6461d57a5fbb34e9be0e301` 7427B moved-not-cut byte-identical, file-order verbatim, anchor 2×{L1=0·ARC=1}). Digest: S123 @start MOSTLY-CLEAN 0-orphan/26 (m#12 mẫu-số-25-vs-26 · dạng-3 dead-code · doc-stale-at-birth pe-neg-quote · reviewer-coverage-gap-5spawn · 2 H24-dir-no-seed + stray-S54 → ALL CLOSED @S132) · S125 @start FLAGS 0-orphan/27 (2 H24-firstrun-no-diary HIGH · stray-S54-still → CLOSED @S132). m#12/#13 preserved in Methods §⭐.
- **S126 (2026-07-16) session-end GATE [opus-worker, em-main VERIFY+APPEND]:** **PASS 5/5 trục** · orphan **0/29** run.md-folder (2 folder phiên này: S125→`spec-execution-synthesis.md` dạng-1 · S126→2 synthesis dạng-1) · Sàn-5 **0 lệch** (S126 born-in-session có `run=` marker session-log; S125 first-commit 13:29:43 = 77s PRE-session → ngoài scope birth-criterion) · corruption 0 (U+FFFD 0/4 + iconv VALID 4/4) · moved-not-cut 2 archive `@S126 hook-curate batch` VERIFIED khớp digest-pointer L1. Learned: grep `Ã|Æ°|â€` = **FALSE-POSITIVE generator trên VN UTF-8** (m#6 pattern MỚI — byte ư/ơ trùng-prefix + literal "mojibake" trong RCA-quote = m#9 self-quote); signal THẬT = U+FFFD-count + iconv round-trip. Coverage khi `wf_*` transcript KHÔNG git-tracked = **triangulation 3-nguồn** (session-log[wf+timing] ⋂ synthesis[cite-jsonl per-lane] ⋂ harvest-entry[name lane]). Surprise: Sàn-5 marker phải grep SESSION-LOG (`run=` prefix) KHÔNG phải WAL (path-form cite — 2 doc khác convention). Tag `[s126-end, gate-pass-5truc, 0orphan29, san5-0lech, m6-new-pattern-vn-utf8]`.
- **S140 (2026-07-18) session-end GATE [opus-worker, self-write diary lượt-GATE — KHÁC lượt GATHER 0-ghi]:** **GATE PASS 5/5** · **8/8 invocation có vết** = reviewer 1-spawn/3-task (S140 fidelity-gate + S140b outward-gate; probe-5-nhãn 17s agentId `a56dcf6d` folded Vòng5 + cross-ref lead-view:15) · lead-view S140 1-FLAG-LOW `view-stale-status` · lead-omission S140 1-FLAG-MED `gap-owner-specifics`+2-INFORM+META (**S137-backfill CLOSED in-session** = GAP-3 tự-vá) · harvest ×5 GATHER → 5 gist committed `aa88d01` (5 file .gist.md verify-exist). **Coverage PASS** · Completeness 4-field đủ · **Placement PASS** (0 stray-S54 · 0 empty-dir m#13 · 0 role-thiếu-MEMORY · porcelain agent-memory 0 mồ-côi — S119-stray+2 H24-dir đã seed/rmdir từ S132, KHÔNG tái). **Corruption PASS** (FFFD 0/4 diary+archive · iconv VALID 3/3 · reviewer curate **moved-not-cut** S139+S134b verbatim `archive:81-82` + BATCH-marker`:79`; em-main +7/−0, tao verify 2-entry-verbatim). **Fidelity STRONG:** reviewer FIDELITY-FAIL inv-cb (2 recon-cluster `:28`/`:31` + AREA-2 anchor DROPPED, "Drop-thấp:0"=claim FALSIFIABLE-FALSE) → **re-distill LANDED verbatim** `## CỤM 11 [BỔ SUNG Fidelity-gate S140]` + honest-disclose "'0 drop' chỉ đúng từ bản này" ⇒ **FAIL→fix loop CLOSED traceable** (KHÁC propose-suông); lead-view/omission FLAG khớp `.session-counter.json _note` S140 EXACT. **Close-gate C5:** 0 run-folder SINH S140 (toàn Agent-tool mode-A ephemeral, latest=S138) ⇒ Sàn-5 0-marker-cần vacuous PASS · orphan-scan DUAL-ACCEPT **0/37**. GỘP 5-GATHER: đọc 5 verbatim→5 gist propose-only (≈183KB→69KB FROZEN), 4-PASS+1-FAIL(inv-cb)→re-distill +CUM11, 0-ghi by-design (prompt cấm cả diary). Chore: SELF 19.3KB>17.1 target → self-compact S123/S125/S126-block due (defer — no cut-mid-gate). Learned: FIDELITY-gate có RĂNG khi bắt "Drop-thấp:0" falsifiable + đòi re-distill verbatim đóng loop, KHÁC gate tick-nhị-phân; moved-not-cut chèn-TRƯỚC-stub same-source-string = byte-id by-construction. Surprise: cùng phiên FAIL→fix LANDED trọn (gist committed CÓ CỤM11 :28/:31) = harvest-integrity self-healing trong-phiên, hiếm. Tag `[s140-gate, gate-pass-5truc, 8of8-invocation, inv-cb-fidelity-fail-redistill-closed, s137-meta-backfill-closed, 0runfolder-modeA, orphan-0of37, moved-not-cut-verified]`
