# 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).
- **2026-07-15 (S123 @start RE-REPORT — self-write diary; return #53-garble ×1 → em-main recover-from-disk, việc KHÔNG mất):** Model `claude-opus-4-8`. Verdict 🟡 **MOSTLY-CLEAN — 0 orphan/26 · 0 mồ-côi · corruption 0 · Fidelity STRONG; 1 Coverage-gap + 2 Placement-flag propose.** (1) **Orphan 0/26 tự-đo** bằng block TRI-ACCEPT verbatim `tiep.md §0:41-57` (28 dir − 2 S117-fable-real spec-only no-run.md = 26 run). Verdict KHỚP em-main, **mẫu-số LỆCH: em-main báo 25** → truy: `48e0d26`@11:48 W0.5 baseline đo "13 orphan/**25**" rồi con-số bị **chép từ narrative**; folder `S122-retro-harvest-7-orphan` SINH @`22ea8c5`@14:28 (sau baseline 2h40) ⇒ 25+1=26. **Chính việc-đo sinh artifact mà mẫu-số của nó không đếm** → m#12. em-main đã đính-chính với anh. (2) 🔴 **dạng-3 = DEAD CODE:** drop nhánh dạng-3 ⇒ **0 folder** thành orphan (6 folder có `## synthesis` embedded — S112×4/S113/S114 — ĐỀU đã có flat sau `22ea8c5`). **dạng-2 (C8) VẪN load-bearing = 5 folder** (h10-implement/invest/review · h910-curate/finalize) → giữ. ⇒ chức-năng CÒN LẠI duy-nhất của nhánh dạng-3 = **âm-thầm tha vi-phạm TƯƠNG-LAI** của luật "nhận≠cho phép" (doc CẤM viết mới dạng-3 nhưng detector VẪN nhận) ⇒ lệnh-cấm đang dựa vào TRÍ-NHỚ, không phải LUẬT soi-được. (3) 🔴 **Doc STALE-AT-BIRTH:** `tiep.md:66` khai (thì hiện-tại) `pe-negative-quote` = ví-dụ dạng-4 "kẽ còn lại, không phải chuyện đã xử" — NHƯNG `22ea8c5`@14:28 đã thêm `implement-synthesis.md` 8249B, còn doc viết @`dfa3dae`@**14:48 = SAU 20 phút** ⇒ doc mô-tả "chưa xử" cho thứ chính tác-giả vừa xử. Nay folder = dạng-1. **Lập-luận dạng-4 vẫn ĐÚNG, chỉ VÍ-DỤ lão-hoá** (class "acceptance literal tự-lão-hoá" S121) — rủi-ro: người đọc grep thấy flat-file → kết-luận cảnh-báo sai → vứt luôn caveat còn đúng. (4) **Mắt người 0 rỗng-ruột:** 26/26 synthesis có body thật; mỏng nhất `h910-curate` 1653B/body=8 → ĐỌC TAY = thực-chất (bảng before→after byte + moved-count + 0-byte-loss + verdict). 2 folder mới nhất (S119-adap · S122-retro) đều dạng-1 ⇒ "nhận≠cho phép" CLEAN, 0 tái-sinh. (5) **Coverage 🟡 GAP-1 reviewer:** wave S119/S122 reviewer chạy **5 spawn** (`wf_78a84f9b-03e` 4-lane: R2 FAIL-4C+8M `sub-reviewer-R2.md` 22121B · R3 FAIL-5C+6M 27612B · R1/R4 chết #53 · + `/fable-real` `wf_cb964f83-331` GO-WITH-FIXES 8-fix) và **ĐỔI SPEC v2→v3-PATCH**, nhưng `reviewer/MEMORY.md` **0 hit S119-S122** (last-commit `b56659b` 07-14). Bất-đối-xứng: inv-cb ĐƯỢC consolidate +6 dòng @`16c91a8`, reviewer KHÔNG. Knowledge NOT-lost (2 sub-md + gate-verdict + STATUS tracked) ⇒ MEDIUM. (6) **Placement 🟡 ×2 (git MÙ cả 2 — m#13):** (a) **stray `runs/2026-07-15-S119-adap-6-broadcast/.claude/agent-memory/reviewer/`** = mis-land cwd-relative (lớp S54 TÁI-PHÁT), mtime 09:53:43, **0 file ⇒ 0 data-loss**, `porcelain --ignored`=0 dòng. (b) **2 vai H24 `lead-view-auditor`+`lead-omission-auditor` dir RỖNG, 0 MEMORY.md, git-invisible** (roster: agents/*.md=14 · dir disk=14 · **dir git-thấy=12**). **Tiền-lệ office-document S107 `21387cd` = seed 2161B NGAY LÚC TẠO VAI** ("MEMORY seed 2161B + wire 14-file", spawn-test pending) ⇒ S122 `00a1c67` wire đủ (VALID_ROLES 12→14 + STATUS 2 row) nhưng **HỤT seed** ⇒ propose seed. Rủi-ro CỤ-THỂ (không giả-định): git không track dir rỗng ⇒ **fresh-clone = 2 dir BIẾN MẤT**, mà system-prompt 2 vai khẳng-định "directory already exists — do not mkdir" ⇒ lần chạy đầu ~S128 gặp dir vắng. Mỉa: S122 học đúng bài "git mù dir rỗng" + `rmdir` 2 probe (16→14) nhưng **để lại 3 dir rỗng git-mù khác**. (7) Corruption 0: 0-byte **0/77** tracked + 0 trong workflows · tail-0a 12/12 · **FFFD 0/12 L1** (3 archive hit = self-quote baseline m#9: SELF 2026-06 ×3 / 2026-07 ×1 · cicd 2026-06 ×1) · `$(` = cicd `$(cygpath)` + SELF intentional-cmd (known-benign). ⚠️ **Tự-bắt m#6 trap:** `cut -c1-260` của CHÍNH TAO đẻ ra FFFD GIẢ + cụt-giữa-từ ("dạn"/"has") trong inv-cb entry → re-verify no-cut: **sạch, 427-ch dòng cuối kết bằng tag đóng**. ⚠️ **Tự-bắt m#11:** báo nhầm "git-visible 14" vì `cut -d/ -f3` đếm `memory-budget.json`+`.mfe-state.json` như role → thật **12**. (8) Delta mồ-côi **0**: chỉ inv-cb chạm (mtime 15:52:22 < last-commit `16c91a8` 15:57:53, m#3) · 12/12 L1 mtime ≤ commit tương-ứng · porcelain agent-memory = 0. Porcelain repo = 1 dòng `.session-counter.json` (counter 1→2, S122→S123, head=`cefb183`) = tick H24 của CHÍNH phiên này, chưa commit — benign, KHÔNG phải memory ("cây SẠCH" trong brief đúng LÚC em-main đo, giờ 1-dòng bẩn do chính nó tick). (9) DIFF vs S122: run 25→**26** · orphan 8-dual→2→**0** · dạng-3 live-reliance 6→**0** · dạng-4 known 1→**0** · agent-memory dir 16→14 (git-thấy 12) · SELF 20.8K→14.5K post-compact. Chore: 0 L1 > 25.6 cap (inv-cb 22.9 max · tooling 22.3 · fe-designer 21.5 idle-07-01 · cicd 20.9); **SELF self-compact S114/S116/S117 ĐÃ EXEC** phiên này. Tag `[s123-start, mostly-clean, 0-orphan-0of26, denominator-25-vs-26-m12, dang3-dead-code, doc-stale-at-birth-penegquote, reviewer-coverage-gap-5spawn, stray-misland-s54-empty, 2role-dir-no-seed-gitblind, m6-m11-traps-selfcaught, return-garble-x1-disk-recovered]`
- **2026-07-16 (S125 @start RE-REPORT — self-write diary):** Model `claude-opus-4-8`. Verdict 🟡 **FLAGS — S124 harvest STRUCTURALLY ĐỦ + 0 orphan/27 + corruption 0; 2 coverage-gap.** (1) Run-folder `S124-adap-wave-reply/` ĐỦ: flat `review-synthesis.md` 5667B (dạng-1) + run.md + 5 sub-md non-zero (lead-omission 9967 · lead-view 8379 · reviewer-1/2/3 8153/10205/10759). Orphan **0/27** run.md (all synth flat/C8) + 2×S117-fable spec-only no-run.md by-design; run 26→27 (+S124). (2) 🔴 **GAP-1 HIGH — 2 vai H24 first-ever-run S124 KHÔNG diary:** `lead-view-auditor`(9 flag) + `lead-omission-auditor`(4 flag+1 escalate), cả 2 `memory:project` by-design, dir RỖNG git-mù (m#13); knowledge trong sub-md+synth+run.md NHƯNG 0 cross-session diary ⇒ next cadence ~S127 mù. Compound S123 seed-hụt (VẪN mở). Propose seed + 4-field spawn-record cả 2. (3) reviewer harvest ✓ 2 S124 entry (L2 fidelity :79 + L3 do-token :80 garble-recover "disk sub-reviewer-3"); L1 authority 0-over-reach + M-1 AHEAD-overclaim folded-synth = optional-nit (PASS-result, low-value). (4) 🟡 GAP-2 LOW carry-fwd: stray misland `runs/…S119-adap-6-broadcast/.claude/agent-memory/reviewer/` empty (class S54) VẪN present S123→S125, propose rmdir. Corruption 0: 0-byte 0/tracked · FFFD 0/12 L1 · tail-0a 11/12 (tooling-auditor ends 0x60 backtick no-EOF-nl = benign, committed-@HEAD identical, content-complete nit). Mồ-côi 0: porcelain = chỉ `.claude/governance/.session-counter.json` tick S124→S125 (non-memory). SELF 20.3K>17.1 target → self-compact S119/S123-block due. APPEND-direct: 0 (2 propose = em-main seed 2 H24). Tag `[s125-start, flags, s124-complete-0orphan27, 2-h24-firstrun-no-diary-HIGH, seed-hut-carryfwd-s123, stray-misland-s54-still, corruption-0]`.
- **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]`.
