fix(maf): harden stage-4 quality gate (P0/P1)

P0-1 migration: detect the same-named stage-3 execution_stage CHECK by sqltext (must contain rework + manager_review), SQLite rebuild / PG-MySQL drop+add, PG boolean default TRUE, validate_schema checks constraint content + seat_tasks status CHECK/unique/FKs; tests start from a real stage-3 schema.

P0-2: a missing/invalid manager review (timeout/exception/empty/invalid JSON/missing per-seat decision) is never an acceptance - system rework under the cap, contract fallback (reassign/controlled_gap/ask_user) afterwards, source=system events.

P0-3: unaccepted deliveries are isolated from the fact ledger, cross-review and final report (deferred merge, delivery_json envelope, accepted_task_results in checkpoint, accepted-only synthesis, no turn_results fallback).

P0-4: seat-task init is atomic get-or-create over the full expected set; recovery fills missing tasks without overwriting frozen contracts; quality gate blocks missing required tasks.

P1: zero config values honoured; duplicate detection persists SHA-256 conclusion digests in history (survives restarts); findings never satisfy evidence, evidence_refs must resolve to trusted ledger refs (unverified_evidence_ref); conversation SSE projection tests added and manager/finalizer messages are no longer mislabelled as seat.delivery.submitted.
This commit is contained in:
董宏凯 committed 2026-08-07 00:04:55 +08:00
1 parent a786cdcad0
commit 1aeee86984
24 files changed
+4706 -47

No files matched your search

@@ -75,12 +75,17 @@ def make_turn_completed(
content: str,
usage: dict[str, Any] | None = None,
metadata: dict[str, Any] | None = None,
execution_stage: str | None = None,
) -> dict[str, Any]:
payload: dict[str, Any] = {"content": content}
if usage:
payload["usage"] = usage
if metadata:
payload["metadata"] = metadata
# The conversation projection must be able to tell a real seat delivery
# apart from manager / review / finalizer messages — carry the stage.
if execution_stage:
payload["execution_stage"] = execution_stage
return _make("agent.message.completed", agent_id, turn_index, payload)
@@ -154,6 +159,99 @@ def make_workflow_checkpointed(agent_id: str | None, turn_index: int, *, checkpo
return _make("workflow.checkpointed", agent_id or "", turn_index, {"checkpoint_id": checkpoint_id, "reason": reason})
# ── Stage 4 (R3-Q) quality gate events ──────────────────────────────────
def make_seat_delivery_submitted(
agent_id: str,
turn_index: int,
*,
task_key: str,
attempt: int,
deterministic_passed: bool,
failure_codes: list[str] | None = None,
) -> dict[str, Any]:
"""A seat task submitted its delivery and passed/failed deterministic checks."""
return _make("seat.delivery.submitted", agent_id, turn_index, {
"task_key": task_key,
"attempt": attempt,
"deterministic_passed": deterministic_passed,
"failure_codes": list(failure_codes or []),
})
def make_manager_review_decision(
agent_id: str,
turn_index: int,
*,
task_key: str,
decision: str,
reason: str,
source: str,
attempt: int,
) -> dict[str, Any]:
"""Server-finalized manager review decision for one seat task."""
return _make("manager.review.decision", agent_id, turn_index, {
"task_key": task_key,
"decision": decision,
"reason": _truncate(reason, 500),
"source": source,
"attempt": attempt,
})
def make_seat_rework_assigned(
agent_id: str,
turn_index: int,
*,
task_key: str,
rework_count: int,
requirements: list[str] | None = None,
) -> dict[str, Any]:
"""A seat task is sent back for rework with explicit revision requirements."""
return _make("seat.rework.assigned", agent_id, turn_index, {
"task_key": task_key,
"rework_count": rework_count,
"revision_requirements": [str(r) for r in (requirements or [])][:16],
})
def make_seat_reassigned(
agent_id: str,
turn_index: int,
*,
task_key: str,
from_agent_id: str,
reassign_count: int,
) -> dict[str, Any]:
"""A seat task is reassigned to a backup seat (agent_id = new owner)."""
return _make("seat.reassigned", agent_id, turn_index, {
"task_key": task_key,
"from_agent_id": from_agent_id,
"reassign_count": reassign_count,
})
def make_stage_quality_gate(
agent_id: str | None,
turn_index: int,
*,
decision: str,
total: int,
accepted: int,
controlled_gap: int,
blocked_required: list[str] | None = None,
) -> dict[str, Any]:
"""Stage quality gate outcome: passed / blocked / passed_with_gaps."""
return _make("stage.quality_gate", agent_id or "", turn_index, {
"decision": decision,
"total": total,
"accepted": accepted,
"controlled_gap": controlled_gap,
"blocked_required": list(blocked_required or []),
})
# ── internal helpers ──────────────────────────────────────────────────────────