verifying-ai-output
Skill1 starsChanged 4 months agoMITView on GitHub
Verify code that an AI agent has just generated before accepting it, specifically catching hallucinated imports, fake API methods, non-existent library versions, and schema mismatches. Use this skill before committing any AI-generated code, after the AI claims a library does X without citing official docs, or when reviewing a pull request from an autonomous coding agent.
In this repository9 other files it gives its agents.
- AGENTS.mdAGENTS.md
- template/AGENTS.mdAGENTS.md
- concurrency-and-hidden-bugsSkill
- debugging-disciplineSkill
- backing-up-solo-projectsSkill
- complying-with-thai-pdpaSkill
- preparing-mobile-store-submissionSkill
- verifying-ai-outputSkill
- writing-feature-specsSkill
The file
Files
Verifying AI Output
Codex reference: P-23 (AI Hallucination Guardrails) When to load: ก่อน commit โค้ดจาก AI agent ทุกครั้ง, หลัง AI อ้าง library behavior โดยไม่มี docs URL, ก่อน merge PR จาก autonomous agent
หลักการ
AI ทุกตัว (ไม่ว่า frontier model แค่ไหน) hallucinate ได้ — โดยเฉพาะ:
- Import statement ที่ library ไม่มีจริง
- API method ที่ deprecated หรือไม่เคยมี
- Type signature ที่ใกล้เคียงของจริงแต่ไม่ตรง
- Config schema ที่ generic จาก similar pattern
กฎทอง: ถ้า AI ตอบ "ตามปกติ library นี้จะ..." โดยไม่อ้าง docs URL จริง → เป็นสัญญาณ hallucination 80%
Mandatory Verification Steps (ก่อน accept โค้ด)
Step 1: Import / Dependency Check
# JavaScript/TypeScript
npm ls --depth=0 # ดู unused / missing
npm view <package-name> versions # ตรวจว่ามี version นั้นจริงไหม
# Python
pip show <package-name> # ตรวจ installed + metadata
pip index versions <package-name> # ตรวจ available versionsRed flags:
- Package name แปลกๆ ไม่เคยเห็น → search npm/PyPI ก่อนใช้
- Version pin ที่ specific เกินไป (เช่น
1.2.3-beta.42) → check release history - AI บอก "ใช้ library ABC" แต่หาใน registry ไม่เจอ → hallucination ชัดเจน
Step 2: API / Method Check
# JavaScript
grep -rn "library_name\." src/ # หาทุกที่ที่เรียก method ของ library นั้น
# แล้วเทียบกับ docs จริงของ providerProcess:
- List ทุก
library.method()ที่ AI generate - เปิด official docs (ไม่ใช่ Stack Overflow ที่ AI อาจ paraphrase ผิด)
- ตรวจ method exists + signature ตรง + return type ตรง
Common hallucination patterns:
- AI generate method ที่ "น่าจะมี" จาก naming convention (เช่น
.findOrCreate()ที่ ORM นั้นเรียก.upsert()) - AI ผสม API จาก 2 library คล้ายกัน (Firebase v8 vs v9 syntax)
- AI generate deprecated method ที่อยู่ใน training data
Step 3: Schema / Config Verification
# Firebase rules — ตรวจ path ใน code ตรงกับ rules
cat database.rules.json | grep -A2 "your_path"
# DB column — query schema จริงจาก provider
# Postgres
psql -c "\d your_table"
# Firebase Realtime DB
firebase database:get /your_path --shallowVerify:
- Field names ตรง case-sensitive
- Type ตรง (number vs string vs boolean)
- Required vs optional ตรง
- Validation rules ตรง
Step 4: Run in Sandbox
# สร้าง isolated test ก่อน integrate
# Node.js
node -e "const X = require('./new-code.js'); X.test()"
# Python
python3 -c "from new_module import test; test()"
# Browser
# เปิด DevTools → paste function → run with mock inputกฎ: ทุกโค้ดใหม่ต้อง run อย่างน้อย 1 รอบ ก่อน commit — ไม่ใช่หลัง
Step 5: Static Analysis Pass
# JavaScript
npx eslint --no-eslintrc --rule '{"no-undef": "error"}' new-code.js
npx tsc --noEmit new-code.ts
# Python
mypy new-code.py
pyflakes new-code.pyจับ:
- Undefined function calls (hallucinated)
- Type mismatch ที่ AI ไม่เห็น
- Unused imports (AI generate แต่ไม่ใช้)
Quick Decision Tree
AI generate โค้ด
│
├── อ้าง library / method ที่คุ้น? ─── No ──→ Step 1+2 บังคับ
│ Yes
│ ↓
├── เคย run library นี้สำเร็จมาก่อน? ─── No ──→ Step 3+4 บังคับ
│ Yes
│ ↓
├── โค้ดแตะ schema / config? ─── Yes ──→ Step 3 บังคับ
│ No
│ ↓
└──────────────→ Step 4 + 5 อย่างน้อย ──→ Commit ได้Red Flag Phrases (signal hallucination)
ถ้า AI ตอบด้วยวลีพวกนี้ → ขอ docs URL ก่อนรับโค้ด:
- "ตามปกติ library นี้จะ..."
- "I believe this method..."
- "This should work because..."
- "Most libraries handle this by..."
- "Based on similar APIs..."
ถามต่อ: "อ้างจาก docs ตรงไหน? ขอ URL"
ถ้าตอบไม่ได้ / ให้ URL ปลอม → hallucination ยืนยัน
Automation Pattern (CI / pre-commit)
# .github/workflows/verify.yml
name: Verify AI Output
on: pull_request
jobs:
verify:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Check dependencies exist
run: npm ci && npm ls --depth=0
- name: Lint for undefined
run: npx eslint . --rule '{"no-undef": "error"}'
- name: Type check
run: npx tsc --noEmit
- name: Run sandbox tests
run: npm testLogging Pattern
ทุกครั้งที่จับ hallucination ได้ → log ลง CAPTURE_LOG.md:
## YYYY-MM-DD — Hallucination caught
- **Context:** AI claimed `firebase.database().refOnce()` (method doesn't exist)
- **Reality:** Correct method is `.once('value')`
- **Cost saved:** ~30 min debugging if had merged
- **Pattern:** AI mixed v8 + v9 Firebase syntax
- **Defense:** Added "verify Firebase method version" to checklistAnti-patterns (สิ่งที่ห้ามทำ)
- ❌ Trust AI's claim about library behavior without docs URL
- ❌ Skip sandbox run because "looks correct"
- ❌ Accept code with TODO/FIXME ที่ AI ใส่ไว้
- ❌ Merge โดยไม่ run lint + type check
- ❌ Believe Stack Overflow citation ที่ AI paste — ตรวจกับ original
Success Criteria
ก่อน mark task complete:
- ทุก import ตรวจมีจริงใน registry
- ทุก API method ตรวจกับ official docs
- Schema ตรงกับ source จริง
- Sandbox run pass อย่างน้อย 1 รอบ
- Static analysis pass (no undef, no type error)
- ถ้าจับ hallucination ได้ → log ลง CAPTURE_LOG
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

