feat: certify codegen chat drafts through loop

This commit is contained in:
thanhnv
2026-07-08 23:20:54 +09:00
parent 780fe86273
commit b3b0544ba8
15 changed files with 359 additions and 24 deletions
@@ -2,7 +2,8 @@
"""Plan-18 deterministic prompt mode router.
MVP-0 emits READ_ONLY/BLOCK/NOT_SUPPORTED. MVP-1 adds OPERATOR only for
registered action phrases; free commands remain NOT_SUPPORTED/BLOCK.
registered action phrases; MVP-2 adds CODEGEN draft mode. Free commands remain
NOT_SUPPORTED/BLOCK.
"""
import argparse
import json
@@ -120,23 +121,39 @@ def classify(message: str, model_verdict: str = ""):
"classified_at": now_iso(),
}
codegen_hits = contains_any(text, policy.get("codegen_terms", []))
if codegen_hits:
return {
"mode": "CODEGEN",
"risk": policy["codegen"]["risk"],
"gates": policy["codegen"]["gates"],
"needs_approval": bool(policy["codegen"]["needs_approval"]),
"reason": "codegen_draft_requested",
"matched_rules": codegen_hits,
"side_effect_allowed": False,
"classified_at": now_iso(),
}
read_terms = policy.get("read_only_terms", [])
read_hits = [t for t in read_terms if re.search(rf"\b{re.escape(t.lower())}\b", text)]
# Model-assisted verdict can only increase caution. In MVP-0 an unsafe model
# verdict is refused, while READ_ONLY from the model cannot override rules.
mv = (model_verdict or "").strip().upper()
if mv in {"BLOCK", "NOT_SUPPORTED", "OPERATOR"}:
if mv in {"BLOCK", "NOT_SUPPORTED", "OPERATOR", "CODEGEN"}:
mode = mv
cfg = policy["block" if mode == "BLOCK" else ("operator" if mode == "OPERATOR" else "not_supported")]
cfg = policy["block" if mode == "BLOCK" else ("operator" if mode == "OPERATOR" else ("codegen" if mode == "CODEGEN" else "not_supported"))]
if mode == "OPERATOR" and not operator_hits:
mode = "NOT_SUPPORTED"
cfg = policy["not_supported"]
if mode == "CODEGEN" and not codegen_hits:
mode = "NOT_SUPPORTED"
cfg = policy["not_supported"]
return {
"mode": mode,
"risk": cfg["risk"],
"gates": cfg["gates"],
"needs_approval": bool(cfg["needs_approval"]),
"reason": "model_escalated" if mode != "NOT_SUPPORTED" else "model_operator_without_registered_action",
"reason": "model_escalated" if mode != "NOT_SUPPORTED" else "model_requested_unsupported_action",
"matched_rules": [f"model:{mode}"],
"side_effect_allowed": mode == "OPERATOR",
"classified_at": now_iso(),