KB-FD33

20 — Search/Vector Hygiene: Stage 1–3 Execution Pack

7 min read Revision 1
vector-hygienesearch-filtercontext-packkb-governancedesign-pack

20 — Search/Vector Hygiene: Stage 1–3 Execution Pack

Date: 2026-05-05 | Status: DESIGN — chờ GPT/User review Source: Vector hygiene report + Opus review + GPT directive adjusted Scope: 3 gates (20A feasibility, 20B primary fix, 20C governance). Không mutation trong design stage.


§0. Bối cảnh

  • 1.174 context-pack docs = 37.5% KB, ước >60% vector pool
  • 2/7 query bị pollution nghiêm trọng (Q5: 100%, Q6: 80%)
  • Agent Data search API hiện chỉ filter tags/status, không có source/path exclude
  • Đ43 không có TTL/retention/deindex policy
  • DOT build script accumulate, không xóa bản cũ — mâu thuẫn README "overwrite-in-place"
  • Pipeline degradation: 9 build gần nhất chỉ 1 file

20A — Search Filter Feasibility Gate

Mục tiêu: Xác định có thể thêm exclude filter vào search API không.

Không assume khả thi. Đây là gate kiểm tra, không phải triển khai.

Câu hỏi cần trả lời:

  1. Agent Data search API code nằm ở đâu? (managed service hay self-hosted?)
  2. Params hiện có: query, tags, status — còn gì khác?
  3. Có thể thêm exclude_source hoặc exclude_prefix param không?
  4. Nếu thêm được → patch nhỏ hay cần refactor lớn?
  5. Nếu không thêm được → mark 20A NOT VIABLE, chuyển 20B thành primary.

Kết quả gate:

Kết quả Hành động
Filter khả thi + patch nhỏ Thiết kế filter + regression tests → triển khai
Filter không khả thi 20A đóng, 20B là primary fix

20B — Context-Pack Lifecycle + Delete-Old (Primary Fix nếu 20A không khả thi)

Mục tiêu: Chặn phát sinh ô nhiễm mới + dọn 1.174 bản cũ.

20B-P0: Inspection (read-only, Agent dispatch)

  1. Inspect DOT build upload/publish logic:

    • File: dot-context-pack-build.sh (đã có trên KB)
    • Tìm: dòng upload_document, có delete_document sau upload không?
    • Xác nhận: accumulation pattern hay overwrite?
  2. Inspect filesystem retention:

    ls -la /opt/incomex/context-pack/current/
    ls -la /opt/incomex/context-pack/staging/
    find /opt/incomex/context-pack/ -maxdepth 2 -type f | head -30
    du -sh /opt/incomex/context-pack/
    

    Trả lời: FS giữ bao nhiêu bản? Có cron dọn không?

  3. Inspect PG manifest:

    SELECT column_name, data_type FROM information_schema.columns
    WHERE table_name IN ('context_pack_manifest','context_pack_requests','context_pack_sections')
    ORDER BY table_name, ordinal_position;
    
    SELECT count(*) FROM context_pack_manifest;
    SELECT count(*) FROM context_pack_requests;
    
    SELECT * FROM v_context_pack_latest LIMIT 3;
    
  4. Latest build_id xác nhận:

    • KB latest (lexicographic): 20260505-070008-4860b7 (1 file — partial)
    • KB latest complete: 20260504-040018-372b48 (9 files)
    • PG manifest latest: query từ P0

20B-P1: Lifecycle Design (sau P0 report)

Hướng đề xuất (chờ P0 xác nhận):

  1. DOT build script sửa: Sau upload bản mới thành công → deleteDocument tất cả docs của build cũ (trừ latest). Hoặc tốt hơn: ngừng upload context-pack lên KB hoàn toàn — giữ trên filesystem, agent đọc trực tiếp.

  2. FS retention: Giữ 7 ngày. Cron job dọn bản > 7 ngày.

  3. PG manifest: Giữ vĩnh viễn (audit trail nhẹ, chỉ metadata + checksum, không content).

  4. Dọn 1.174 bản cũ trên KB:

    • Dry-run: list tất cả docs prefix context-pack/, trừ latest build
    • Delete: batch deleteDocument
    • Verify: search pollution test lại 7 query → so sánh before/after
    • Rollback: nếu search tệ hơn → re-upload từ FS (nếu còn)
  5. Search regression suite (7 query + expected canonical results):

    Query Expected canonical top-5
    birth pipeline QT-001 QT-002 birth-process-v1.md, birth-registry-law.md
    DOT-119 birth trigger setup dot119 reports, 18a design
    orphan ghost birth registry law-19-orphan-scanner.md, birth-registry-law.md
    information_unit unit_version schema P2B-P0 report, file 04 schema
    description_policy structured_exempt description policy design/report
    Điều 43 context pack lifecycle dieu43-system-context-law.md
    birth-process-v1 QT-002 birth-process-v1.md

20B Hard boundaries:

  • Không sửa DOT script trong design stage
  • Không delete trong design stage
  • Không patch Đ43 trong design stage
  • P0 = read-only inspection
  • P1 = design only, chờ approve trước execute

20C — KB Governance Framework (Dài hạn)

Mục tiêu: Không lặp lại vấn đề ở 100x data.

4 Tiers

Tier Mô tả Search priority TTL Ví dụ prefix
T0 Canonical Luật, HP, architecture, process Highest — always include Vĩnh viễn knowledge/dev/laws/, knowledge/dev/architecture/, knowledge/ops/processes/
T1 Working Design, reports, reviews, prompts High — default include Vĩnh viễn hoặc archive sau enacted knowledge/dev/laws/*/design/, */reports/, */reviews/
T2 Ephemeral Snapshots, staging, context-pack Low — exclude default, opt-in only Latest only trên KB, N ngày FS context-pack/
T3 Registry Auto-generated catalog entries Medium — structured search Vĩnh viễn registries/

Upload metadata bổ sung (future)

Field Mục đích
kb_tier T0/T1/T2/T3 — suy từ path prefix hoặc explicit
retention_policy permanent / latest_only / ttl_7d
source_type manual / dot_generated / cron_snapshot
is_ephemeral true/false
is_latest true/false (cho T2)

Quota alerts

  • Nếu 1 prefix chiếm > 20% tổng KB docs → WARNING
  • Nếu 1 prefix chiếm > 30% tổng KB docs → ALERT + auto-flag

Luật hóa

Đề xuất: Phụ lục Đ43 (KB/Vector Retrieval Policy) thay vì luật mới. Vì Đ43 đã quản context-pack, chỉ cần bổ sung TTL + retrieval tier + quota.

20C Hard boundaries:

  • Chỉ design, không implement
  • Không tạo Đ45 mới
  • Không sửa API
  • Không sửa Đ43

§1. Execution order

  1. 20A gate → nếu filter khả thi → triển khai filter + 20B. Nếu không → 20B là primary.
  2. 20B-P0 inspection → report → review
  3. 20B-P1 lifecycle design → review → approve → execute (delete old + sửa DOT)
  4. 20C design → review → phụ lục Đ43

20 Search/Vector Hygiene design pack | 2026-05-05 | 20A feasibility gate + 20B primary fix + 20C governance. Chờ review.

Back to Knowledge Hub knowledge/dev/laws/dieu44-trien-khai/design/20-search-vector-hygiene-stage1-stage2-execution-pack.md