ในบทความนี้
In this post
ตอนที่ 4 จากสิบสอง: เพิ่มด่านความหมายหลัง validation แยกคำขอใช้งานจริง คำขอนอกขอบเขต และข้อความที่พยายามควบคุมระบบ
คำตัดสินและร่างข้อความเริ่มต้นเป็น mock ที่ผู้เขียนกำหนด ไม่ใช่ผล Jev จริง อ่านบทเรียนคู่กับ source, tests และแบบฝึกที่ลิงก์ไปยังรุ่นตรึงใน GitHub
เป้าหมายและสิ่งที่ต้องพร้อม
เตรียม Node22.18 ขึ้นไปกับ terminal clone companion จาก GitHub ตาม README หรือดาวน์โหลด ZIP ฉบับสมบูรณ์ แตกแล้วเข้า ai-core-functional-lab ไม่ต้อง npm install ใช้ API key หรือเรียกโมเดลผ่านเครือข่าย เวลาอ่านไม่รวมการลงมือประมาณ25–45นาที เก็บชุดอ้างอิงหนึ่งชุดและทำแบบฝึกในอีกสำเนา
ดาวน์โหลด companion ZIP · README / แผนบทเรียน
ตอนนี้เน้น src/semantic.mjs โดย gate state บอกผลต่อชิ้นงานและทางไปต่อ ไม่มี state ใดส่งข้อความหรือแก้บัญชีจริง
จากโค้ดเดิมสู่ขอบเขตใหม่
string ที่ถูกชนิดอาจขอสิ่งผิดขอบเขตได้ คำถามใช้ผลิตภัณฑ์กับคำสั่งควบคุม evaluator มี JSON รูปเดียวกัน ด่านความหมายถามคำถามแคบหลัง validation โดยไม่ได้แทนที่ policy
request ของ input รวม purpose แบบ Choice กับ injection แบบ Noul ทั้งสองเป็นคำถามอิสระใน request เดียว โค้ดส่ง purpose ที่ไม่มั่นใจหรือ injection ช่วงกลางไป human review แทนเดาเส้นทางอัตโนมัติ
semantic guard เป็นการป้องกันหลายชั้นที่ผิดได้ ข้อความผู้ใช้ยังไม่เชื่อถือแม้ใส่ป้ายว่า data คะแนนสูงไม่ได้พิสูจน์ว่าไม่มี injection และการบล็อกตัวอย่างหนึ่งไม่ได้เป็นหลักฐานว่าบล็อกได้ทุกการโจมตี
ลงมือทีละเจ็ดขั้น
ขั้นที่ 1
พิมพ์ input request แยก instructions ของแอปออกจาก request ของผู้ใช้ ใน state มี facts ของผลิตภัณฑ์กับข้อความผู้ใช้ แต่ไม่มีป้ายเฉลยว่าเคสควรผ่านหรือไม่
ขั้นที่ 2
อ่าน criterion ของ purpose แต่ละตัวและอธิบาย other ชุดปิดที่ไม่มีทางออกอาจบังคับ label แม้งานอยู่นอกโดเมน purpose ที่ไม่รู้ไม่ควรได้สิทธิ์เริ่มต้น
ขั้นที่ 3
อ่าน Noul ของ injection ว่าเป็นความน่าจะเป็นของ yes ไม่ใช่ความรุนแรง ขอบ yes/no เป็นพารามิเตอร์แอป ค่ากลางสื่อความไม่แน่ใจ ไม่ได้หมายถึง injection รุนแรงปานกลาง
ขั้นที่ 4
รัน V005 ไล่เหตุที่ต้อง review แม้ response ถูกโครงสร้าง จดแยกการผ่าน contract ความกระจุกตัว และความถูกเชิงความหมาย
ขั้นที่ 5
รัน A001 ซึ่งใช้ถ้อยคำนอก deny list ของ regex fixture ของด่านความหมายระบุว่ามีคำสั่งข้าม evaluation ตัวอย่างสอนรอยต่อของชั้น ไม่ใช่ค่า recall ที่วัดจาก detector จริง
ขั้นที่ 6
ยืนยันว่าไม่มี generator หลัง semantic rejection ด่านที่เพียง log ความสงสัยแล้วร่างต่อมี contract คนละแบบ อย่าเผลอทำเช่นนั้น
ขั้นที่ 7
อ่าน test ขอบ uncertainty แล้วเขียนแผน validation ของการถอดคำสองภาษา provider จำลองประเมิน prompt ที่แก้หรือคลังโจมตีใหม่ให้ไม่ได้
ไฟล์และคำสั่งที่รันได้
เปิด source จริงและอ่าน dependencies ก่อนแก้ นี่คือโค้ดอ้างอิงที่รันได้ ไม่ใช่ SDK ชื่อ AI-Core ที่สมมติขึ้น ลิงก์ชี้ source รุ่นตรึงของ companion สาธารณะใน GitHub
src/semantic.mjs · tests ของตอนนี้ · แบบฝึกตอนนี้
import {gate} from './contracts.mjs';
export function semanticInput(d,p) {
if(d.injection>=p.yesMin) return gate('reject',['semantic_injection']);
if(d.injection>p.noMax||d.purpose.confidence<p.choiceMin)
return gate('human_review',['uncertain_input']);
if(d.purpose.choice==='account_action') return gate('reject',['semantic_scope']);
if(d.purpose.choice!=='how_to') return gate('human_review',['unknown_purpose']);
return gate('accept');
}
export function semanticOutput(d,p) {
if(d.hiddenInstructions>=p.yesMin) return gate('reject',['self_advocating_candidate']);
if(d.hiddenInstructions>p.noMax) return gate('human_review',['uncertain_candidate_instructions']);
if(d.grounded<=p.noMax) return gate('revise',['unsupported_claim']);
if(d.grounded<p.yesMin) return gate('human_review',['uncertain_grounding']);
return gate('accept');
}
คำสั่งออฟไลน์:
node src/cli.mjs request --stage input --case V001
node src/cli.mjs judge --stage input --case V005
node src/cli.mjs run --case A001
node --test --test-reporter=spec test/gates.test.mjs
ผลที่ควรได้และการตรวจ
V005 ต้อง review ส่วน A001 reject ด้วย semantic_injection และหยุดก่อน generation ใน request มี jev-1.14.0 กับ answer IDs สองตัว ตัวเลขทั้งหมดแต่งเป็น fixture ไม่ได้เก็บจาก live API
อ่าน reasons, history และทุก attempt ไม่ดูเพียง label สุดท้าย usage ที่ไม่ทราบต้องยังไม่ทราบ test สีเขียวแสดงเส้นทางโค้ดที่กำหนด ไม่ได้พิสูจน์ความปลอดภัยเชิงความหมายทุกกรณี
ตัวอย่างที่ต้องเข้าใจเมื่อไม่ผ่าน
หาก map ทุก purpose ที่ไม่ใช่ account_action เป็น how_to คำขอกำกวมจะกลายเป็นร่างอัตโนมัติ แขนง other และ uncertainty tests ช่วยกันการขยายขอบเขตโดยไม่ตั้งใจ
แบบฝึกในสำเนาทดลอง
เปลี่ยนถ้อยคำ instruction จุดเดียวในสำเนาทดลองแล้วดู request diff คำตอบ fixture ควรเหมือนเดิม อธิบายว่าต้องมีข้อมูล validation ที่ติดป้ายใหม่อย่างไรก่อนเชื่อว่าความหมายดีขึ้นจริง
ทำตามไฟล์แบบฝึก เก็บต้นฉบับและคืนชุดอ้างอิงก่อนตอนถัดไป อย่าใช้ผล held-out เลือก threshold
ตรวจเมื่อผลไม่ตรง
ถ้า response รูปแบบโมเดลไม่มี answer ID ที่คาด ต้องหยุดที่ contract review อย่าให้ semantic guard ตีความ injection ที่หายว่าไม่มี injection
จุดตรวจถัดไป
ก่อนต่อ ให้บอกคุณสมบัติที่โค้ดตรวจได้แน่นอน คำตัดสินที่ผิดได้ state ที่หยุดงาน และหลักฐานที่ต้องมีก่อนปล่อยรุ่น
แหล่งอ้างอิงและรุ่น
เอกสารต้นทางและ companion source
ตรวจเอกสาร TypeSafe วันที่24ตุลาคม2026 บทเรียนตรึงคำขอข้อความที่ jev-1.14.0 และใช้ Choice/Noul ไม่ได้อ้างว่ามีเพียงความสามารถเหล่านี้ โค้ด REST ทางเลือกยังไม่ได้ลอง live ส่วน SDK0.7.0 มีเอกสารแต่แล็บนี้ไม่ต้องติดตั้ง
สิ่งที่ควรนำไปใช้ต่อ
- เงื่อนไขแน่นอนและสิทธิ์ยังอยู่ในโค้ด
- ด่านความหมายและคุณภาพผิดพลาดได้
- accept, reject, revise และ review มีทางไปต่อคนละแบบ
Part 4 of twelve. Add a fallible semantic input guard after validation, with explicit uncertainty and out-of-scope paths.
Default decisions and draft text are authored mocks, not live Jev results. Use the tutorial with the matching companion code, tests and exercise linked to a fixed GitHub version.
Outcome and prerequisites
Use Node22.18+ and a terminal. Clone the GitHub companion using its README, or download the complete ZIP, extract it and enter ai-core-functional-lab. No npm install, API key or network inference is needed. The reading-time label excludes about25–45 minutes of hands-on work. Keep one reference copy and make exercises in another.
Download companion ZIP · README / lesson map
This part focuses on src/semantic.mjs. Expected gate states describe content and continuation; no state sends a message or changes a real account.
Old functions and the new boundary
Some valid strings still ask for the wrong thing. A product-help request and an instruction to control an evaluator can share the same JSON shape. A semantic guard asks a narrow question about meaning after deterministic validation, without replacing the policy that follows.
The input request combines a purpose Choice with an injection Noul. The questions are independent within that request. The code interprets a low-confidence purpose or middle-band injection probability as human review rather than guessing an automatic path.
A semantic guard is fallible defense in depth. User content remains untrusted even when labeled as data. A high model score cannot prove the absence of prompt injection, and a public example of a blocked attack is not evidence that every attack will be blocked.
Seven practical steps
Step 1
Print the input request and separate application-owned instructions from the user’s request field. The state contains trusted product facts alongside the user content, not a gold acceptability label.
Step 2
Read each purpose criterion and explain the other option. A closed set without an escape route can force a label even when the task is out of domain. Unknown purpose should not become a privileged default.
Step 3
Inspect the injection Noul as a probability of yes, not severity. The example yes/no boundaries are application parameters. A middle value expresses uncertainty and does not mean moderately severe injection.
Step 4
Run V005 and trace why it reviews despite a valid response structure. Keep contract validation, concentration and semantic correctness as separate entries in your notebook.
Step 5
Run A001, a phrasing outside the small regex deny list. Its authored semantic fixture flags an instruction to skip evaluation. The example demonstrates the layer boundary, not the real detector’s measured recall.
Step 6
Confirm no generator runs after the semantic rejection. A guard that merely logs suspicion but continues drafting has a different contract; do not accidentally implement that behavior.
Step 7
Read the uncertainty boundary tests and write a real validation plan for paraphrases in Thai and English. The canned provider cannot assess your rewritten prompt or expanded attack corpus.
Files and runnable commands
Open the exact source and read its dependencies before editing. This is runnable reference code, not a fictional AI-Core SDK. The source links point to a fixed version of the public GitHub companion.
src/semantic.mjs · Matching tests · This lesson’s exercise
import {gate} from './contracts.mjs';
export function semanticInput(d,p) {
if(d.injection>=p.yesMin) return gate('reject',['semantic_injection']);
if(d.injection>p.noMax||d.purpose.confidence<p.choiceMin)
return gate('human_review',['uncertain_input']);
if(d.purpose.choice==='account_action') return gate('reject',['semantic_scope']);
if(d.purpose.choice!=='how_to') return gate('human_review',['unknown_purpose']);
return gate('accept');
}
export function semanticOutput(d,p) {
if(d.hiddenInstructions>=p.yesMin) return gate('reject',['self_advocating_candidate']);
if(d.hiddenInstructions>p.noMax) return gate('human_review',['uncertain_candidate_instructions']);
if(d.grounded<=p.noMax) return gate('revise',['unsupported_claim']);
if(d.grounded<p.yesMin) return gate('human_review',['uncertain_grounding']);
return gate('accept');
}
Offline commands:
node src/cli.mjs request --stage input --case V001
node src/cli.mjs judge --stage input --case V005
node src/cli.mjs run --case A001
node --test --test-reporter=spec test/gates.test.mjs
Expected output and verification
V005 reviews; A001 rejects with semantic_injection and stops before generation. The request shows model jev-1.14.0 and two answer IDs. All numbers are authored fixtures, not captured live API observations.
Inspect reasons, history and every attempt rather than only the final label. Unknown usage must remain unknown. A green test demonstrates the specified code path, not universal semantic safety.
A failure to understand
If a developer maps every purpose other than account_action to how_to, ambiguous requests become automatic drafts. The explicit other branch and uncertainty tests prevent that accidental broadening of scope.
Exercise in a disposable copy
Change only the instruction wording in a disposable copy and print the request diff. The fixture answer should stay unchanged. Describe the fresh labeled validation data needed before believing a real semantic improvement.
Follow the matching exercise file, retain the original and restore the reference before the next chapter. Do not use held-out results to choose a threshold.
Troubleshooting
If a model-like response lacks an expected answer ID, stop at contract review. Do not let the semantic guard silently interpret missing injection data as no injection.
Next checkpoint
Before continuing, name the property that code checks exactly, the judgment that may be wrong, the state that stops work and the evidence needed before release.
← Previous lesson · Next lesson →
Sources and version notes
Primary documentation and companion source
Official TypeSafe docs checked2026-10-24. This curriculum pins text requests to jev-1.14.0 and uses Choice/Noul; it does not claim those exhaust Jev’s capabilities. Optional REST code is untested live. SDK0.7.0 is documented but unnecessary for this no-install lab.
Key takeaways
- Deterministic constraints and authorization stay in code.
- Semantic guards and quality judgments are fallible.
- Accept, reject, revise and review have distinct continuation rules.