AI-Core Jev Software Architecture

Contracts — เขียนสถานะและสัญญาให้ตรวจได้Make the Workflow a Typed State Machine

กำหนด input ผลตัดสิน และเงื่อนไขหยุดของทุกด่าน แยกร่างที่ผ่านจากการอนุมัติ releaseGive every stage an explicit input, verdict and stop condition; distinguish a draft from a release.

By Anirach Mingkhwan Software Engineering with AI-Core 2026 • Post #3 2 min read
Contracts — เขียนสถานะและสัญญาให้ตรวจได้
ในบทความนี้
  1. 1. ปัญหาทางวิศวกรรม
  2. 2. ขอบเขตที่ต้องออกแบบ
  3. 3. อ่าน implementation
  4. 4. เปรียบเทียบผลที่ได้
  5. 5. รัน ทำให้พัง และอธิบาย
  6. 6. แหล่งอ้างอิงและขอบเขต
In this post
  1. 1. The engineering problem
  2. 2. The boundary to design
  3. 3. Read the implementation
  4. 4. Compare the outcomes
  5. 5. Run, break, and explain
  6. 6. Sources and limits

1. ปัญหาทางวิศวกรรม

เป้าหมายบทนี้

กำหนด input ผลตัดสิน และเงื่อนไขหยุดของทุกด่าน แยกร่างที่ผ่านจากการอนุมัติ release

boolean ชื่อ safe ซ่อนรายละเอียดมากเกินไป เราไม่รู้ว่า input ผิดรูป ไม่มีสิทธิ์ กำกวม หรือถูกแต่คำอธิบายไม่ดี แต่ละกรณีควรตอบต่างกัน จึงใช้ Gate(state, reason) โดยมีสถานะระหว่างทาง pass, reject, review, revise และให้ผลรวมเป็น accepted_draft, reject หรือ review การ revise เป็นการวนภายในที่มีงบจำกัด ไม่ได้สัญญาว่าลองจนกว่าจะผ่าน

ระบุผู้รับผิดชอบการเปลี่ยนสถานะ input validation ปฏิเสธข้อมูลผิดรูป authorization ปฏิเสธ action ที่ทำไม่ได้ ความไม่แน่ใจด้านความหมายส่ง review พฤติกรรมที่ยังผิดอาจส่ง revise ส่วนร่างที่ผ่านยังจบที่มนุษย์ตรวจ ใน companion งาน reject และ review จะมี draft=None ช่วยลดโอกาสที่ UI หยิบร่างที่ไม่ผ่านไปแสดงเหมือนยอมรับแล้ว trace เก็บชื่อด่านและ reason code เพื่อให้แอปไม่ต้องแยกข้อความภาษาอังกฤษเอง

2. ขอบเขตที่ต้องออกแบบ

ตอน 3: boolean เดียวซ่อนเหตุที่งานไม่ผ่าน → ทุกทางออกมีสถานะ เหตุผล และ trace ของด่าน
ภาพที่ 1 ขอบเขตหน้าที่และเส้นทางหยุด เปิดภาพเพื่อดูขนาดเต็ม

ลำดับปกติคือ input → policy → ตรวจ response ของตัวตัดสิน → ตรวจความหมาย input → context → สร้างร่าง → ตรวจรูปแบบ → ตรวจพฤติกรรม → ตรวจคุณภาพ policy มาก่อน inference เพราะ action ที่ไม่มีสิทธิ์ไม่ต้องเสียค่าตีความ ตรวจ output ก่อนนำโค้ดเข้าส่วนคำนวณ และตรวจคุณภาพหลังพฤติกรรม เพราะคำอธิบายดีชดเชยฟังก์ชันผิดไม่ได้ ลำดับนี้เป็นข้อกำหนดสถาปัตยกรรมที่ต้องทดสอบเมื่อมีทางลัดหรือ fallback เพิ่มขึ้น

3. อ่าน implementation

ส่วนโค้ดจาก companion เพื่ออ่านประกอบ ใช้คำสั่งในหัวข้อ 5 เพื่อรันตัวอย่างที่ครบ dependency

from workbench.runtime import run, request, FixtureProvider

result = run(request(), FixtureProvider())
print(result["state"])
print([event["stage"] for event in result["trace"]])

state machine ต้องครอบคลุมเส้นทางผิดพลาดด้วย provider ล้มและงบหมดส่ง review label ที่ไม่รู้จักห้ามกลายเป็น skill เริ่มต้น หลักฐาน release ที่หายห้ามกลายเป็นสำเร็จ วาดทางออกให้ครบก่อนเขียน happy path แผนภาพและ trace ในคอร์สช่วยเปิดให้ตรวจขอบเขตเหล่านี้ได้ โดยเป็นข้อเสนอการออกแบบของคอร์ส ไม่ใช่มาตรฐานภายนอก เวลา review ให้ถามว่ามีเส้นทางใดข้ามด่านบังคับได้หรือไม่ รวมทั้ง retry, cache และการ override ของมนุษย์

4. เปรียบเทียบผลที่ได้

เส้นทางผลที่สังเกตได้
ก่อนเพิ่มด่านนี้boolean เดียวซ่อนเหตุที่งานไม่ผ่าน
หลังเพิ่มด่านนี้ทุกทางออกมีสถานะ เหตุผล และ trace ของด่าน

ผลต่อไปนี้มาจาก fixture ที่กำหนดคำตอบไว้เพื่อตรวจสถาปัตยกรรม ไม่ใช่ผลวัดความแม่น ความเร็ว หรือต้นทุนของ Jev จริง

{
  "state": "accepted_draft",
  "trace": [
    "input",
    "policy",
    "decision",
    "semantic_input",
    "output",
    "behavior",
    "quality"
  ],
  "effects": 0
}

5. รัน ทำให้พัง และอธิบาย

Part 03 code · Implementation · Tests: test_accept_still_has_zero_effects · Exercise

git clone https://github.com/Anirach/ai-core-engineering-workbench.git
cd ai-core-engineering-workbench
git checkout 18cdfcd4156d3d02acb8badf3b3a0a823113e0ed
python3 -m workbench.lesson 3
python3 -m unittest tests.test_boundaries.BoundaryTests.test_accept_still_has_zero_effects -v

ลองทำด้วยตัวเอง

วาดทางออก reject, review, revise ของ handler ที่ใช้อยู่ และตรวจว่างานไม่ผ่านไม่สามารถส่ง artifact แบบยอมรับแล้ว

สิ่งที่ต้องส่ง: input ที่ลอง สถานะและเหตุผลที่ได้ และคำอธิบายว่าด่านใดเป็นผู้รับผิดชอบ ห้ามแก้ expected result เพียงเพื่อให้ test ผ่าน

6. แหล่งอ้างอิงและขอบเขต

เอกสารต้นทางและข้อจำกัด

ตรวจเอกสาร 2 พฤศจิกายน 2026 ตัวอย่างและ threshold เป็นการออกแบบเพื่อการสอน การอ้าง [1], [2] ตรงกับลำดับข้างล่าง ยังไม่ได้วัด live API

  1. Anthropic, Building effective agents — prompt chainingProgrammatic gates within a predefined workflow.

1. The engineering problem

Learning outcome

Give every stage an explicit input, verdict and stop condition; distinguish a draft from a release.

A boolean named safe hides too much. Was the request malformed, unauthorized, ambiguous, or correct but poorly explained? Those failures need different responses. Represent a stage result as Gate(state, reason), with pass, reject, review and revise as explicit intermediate states. The overall workflow returns accepted_draft, reject or review. A revision is an internal transition with a finite budget, not a promise that the user will eventually get a passing answer.

Define ownership for each transition. Input validation may reject malformed data. Authorization may reject an unavailable action. Semantic uncertainty goes to review. A behavior mismatch may request a revision. An accepted candidate still ends at a human review boundary. In the companion a rejected or reviewed run has draft=None; this makes it harder for a careless UI to display an unapproved artifact as an accepted result. The trace carries stage names and reason codes so a caller does not have to parse English error messages.

2. The boundary to design

Part 3: One success boolean conceals every failure mode. The controlled path is Input / policy to Decision / context to Candidate checks to Accepted draft. Every exit has a state, reason and stage trace.
Figure 1. Gate(state, reason) + trace. Open the diagram for full resolution.

The normal sequence is input → policy → decision response validation → semantic input check → context → generation → output shape → behavior → quality. Policy comes before paid inference because a forbidden action needs no semantic interpretation. Output checks come before interpreting candidate arithmetic. Quality comes after behavior because a persuasive explanation cannot compensate for a wrong function. The fixed sequence is an architectural invariant worth testing whenever someone adds a shortcut or a fallback path.

3. Read the implementation

Implementation excerpt from the companion. Run the complete lesson below with its imports and fixtures.

from workbench.runtime import run, request, FixtureProvider

result = run(request(), FixtureProvider())
print(result["state"])
print([event["stage"] for event in result["trace"]])

A state machine is only as complete as its error paths. Provider errors and expired budgets route to review. Unknown labels do not become a default skill. Missing release evidence does not become success. Draw every exit before implementing the happy path. Our diagram and trace make these edges reviewable; they are a course synthesis, not a new external standard. The useful question in a design review is whether any path can skip a mandatory stage, including retries, caches and human overrides.

4. Compare the outcomes

ApproachObservable outcome
Before this controlOne success boolean conceals every failure mode.
After this controlEvery exit has a state, reason and stage trace.

The following output comes from scripted fixtures that verify architecture behavior. It is not a measurement of live Jev accuracy, latency, or cost.

{
  "state": "accepted_draft",
  "trace": [
    "input",
    "policy",
    "decision",
    "semantic_input",
    "output",
    "behavior",
    "quality"
  ],
  "effects": 0
}

5. Run, break, and explain

Part 03 code · Implementation · Tests: test_accept_still_has_zero_effects · Exercise

git clone https://github.com/Anirach/ai-core-engineering-workbench.git
cd ai-core-engineering-workbench
git checkout 18cdfcd4156d3d02acb8badf3b3a0a823113e0ed
python3 -m workbench.lesson 3
python3 -m unittest tests.test_boundaries.BoundaryTests.test_accept_still_has_zero_effects -v

Your turn

Draw the rejected, reviewed and revised exits for your current AI handler. Check that rejected runs cannot expose an accepted artifact.

Keep three artifacts: the input you tried, the resulting state and reason, and an explanation of which boundary owns that result. Do not change an expected result merely to make a test pass.

6. Sources and limits

Primary sources and evidence limits

Sources checked 2 November 2026. Examples and thresholds are teaching designs. Inline [1], [2] follow the order below. Live API performance has not been measured.

  1. Anthropic, Building effective agents — prompt chainingProgrammatic gates within a predefined workflow.
จากซีรีส์ Software Engineering with AI-CoreFrom Software Engineering with AI-Core