KB-5858

OGV-2C — Write Gate + Test Teardown Agent Prompt

5 min read Revision 1
ogv-2cwrite-gatetest-teardownagent-promptvector-hygiene2026-05-07

OGV-2C — Write Gate + Test Teardown — Agent Execution Prompt

Mục tiêu: Chặn rác lọt vào production KB. Fix hệ thống, không fix vụ việc. VPS: ssh contabo. Container: discover trước. Effort: /effort medium Mode: Code changes + test Report: knowledge/dev/laws/dieu44-trien-khai/reports/ogv-2c-write-gate-implementation-report-2026-05-07.md


Bối cảnh

Sau OGV-P0 fix (soft-delete resurrection) và 20B (context-pack purge), hệ thống đã sạch. Nhưng write path của Agent Data API vẫn cho phép:

  1. Test artifacts (test/*) vào production KB
  2. Inline documents chứa local file path (file:///Users/..., .gemini/tmp/...)
  3. Root-level documents không có path prefix hợp lệ

Bằng chứng: 10 file rác tồn dư từ nhiều phiên (OGV-2A evidence report).


KHÔNG ĐƯỢC LÀM

  • ❌ Không sửa Qdrant code
  • ❌ Không sửa embedding/chunking
  • ❌ Không restart services trừ khi cần thiết cho deploy
  • ❌ Không xóa KB documents
  • ❌ Không sửa PG triggers
  • ❌ Không thay đổi API response format
  • ❌ Không sửa Directus

Việc 1 — Write gate validation

Tìm hàm xử lý upload/create/ingest document trong Agent Data API code:

ssh contabo
docker exec -it <agent-data-container> bash
grep -rn "def upload_document\|def create_document\|def ingest_document\|POST.*document" /app/

Thêm validation TRƯỚC khi lưu PG:

Rule 1: Chặn test/* prefix trong production

# Reject path bắt đầu bằng test/ trừ khi có env flag TEST_MODE=true
if path.startswith("test/") and not os.environ.get("TEST_MODE"):
    raise ValueError(f"Test documents blocked in production: {path}")

Rule 2: Chặn inline-* document ID bất hợp lệ

# inline-* chỉ được tạo bởi internal flow, không qua external API
if path.startswith("inline-") and not _is_internal_caller():
    raise ValueError(f"Inline documents blocked from external API: {path}")

Nếu hệ thống không phân biệt internal/external caller → chặn hoàn toàn inline-* prefix.

Rule 3: Chặn content chứa local file path

BLOCKED_CONTENT_PATTERNS = [
    r"file:///Users/",
    r"/Users/[a-zA-Z]+/",
    r"\.gemini/tmp/",
    r"/tmp/.*tool-outputs",
]
for pattern in BLOCKED_CONTENT_PATTERNS:
    if re.search(pattern, content):
        raise ValueError(f"Content contains local file path (blocked): {pattern}")

Rule 4: Chặn root-level document không có prefix hợp lệ

VALID_TOP_PREFIXES = [
    "knowledge/", "operations/", "registries/", "reports/",
    "tham-khao/",
]
if not any(path.startswith(p) for p in VALID_TOP_PREFIXES):
    raise ValueError(f"Document must be under valid prefix: {path}")

Lưu ý: Đây là pseudo-code. Agent phải đọc code thực tế và adapt logic phù hợp với architecture hiện tại (có thể là FastAPI, middleware, hoặc store layer).


Việc 2 — Test teardown enforcement

Tìm bất kỳ test utility hoặc pytest fixture nào:

grep -rn "test.*create.*document\|upload.*test\|def test_" /app/

Nếu có test framework:

  • Thêm teardown fixture xóa mọi doc có prefix test/ sau mỗi test run
  • Hoặc: wrap test document creation trong context manager tự cleanup

Nếu không có test framework chính thức:

  • Tạo helper function create_test_document() + cleanup_test_documents() trong utils
  • Document rõ: "Mọi test tạo KB doc PHẢI dùng create_test_document() — tự cleanup sau 1 giờ hoặc khi test kết thúc"

Việc 3 — Regression test

Tạo test (pytest hoặc shell script) verify:

TEST-POS-1: Upload doc với path "knowledge/dev/test-gate-pos.md" → SUCCESS
TEST-POS-2: Upload doc với path "reports/test-gate-pos.md" → SUCCESS

TEST-NEG-1: Upload doc với path "test/should-block.md" → REJECTED (400/403)
TEST-NEG-2: Upload doc với path "inline-should-block" → REJECTED
TEST-NEG-3: Upload doc với content "file:///Users/hacker/secrets.txt" → REJECTED
TEST-NEG-4: Upload doc với path "random-root-doc" (no prefix) → REJECTED

TEST-CLEANUP: Xóa TEST-POS-1, TEST-POS-2 sau khi test pass

Việc 4 — Git commit

cd /opt/incomex && git add -A && git commit -m "OGV-2C: write gate — block test/*, inline-*, local paths, root-level docs"

Report phải có

  1. Code changes: file nào, function nào, diff tóm tắt
  2. 4 rules: implement hay skip (nếu skip, lý do)
  3. 6 test results: PASS/FAIL
  4. Git commit hash
  5. Bất kỳ side effect nào phát hiện

Hard boundaries

  • Chỉ sửa validation layer của upload/create/ingest
  • Không sửa read path
  • Không sửa delete path
  • Không sửa vector sync
  • Không sửa search
  • Không refactor code không liên quan

OGV-2C Write Gate | 2026-05-07 | Chờ dispatch

Back to Knowledge Hub knowledge/dev/laws/dieu44-trien-khai/prompts/ogv-2c-write-gate-and-test-teardown-agent-prompt-2026-05-07.md