Repository navigation
Expand file tree
/
Copy pathprotocol.py
More file actions
171 lines (148 loc) · 6.32 KB
/
Copy pathprotocol.py
File metadata and controls
171 lines (148 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""MOP — the stateful protocol object.
One instance per CC session. Holds:
- the rule set (loaded once at construction)
- the LLM evaluator closure (host-provided)
- the deliver closure (host-provided)
- per-session state (pending_message, sent_message_this_turn, justification_attempts)
Tools (`submit_message`, `submit_justification`, `get_rules`, `get_status`)
are async methods on this class. The MCP wrapper in `mop/mcp.py` exposes
them via @tool decorators.
State transitions are centralized in `apply()` so submit_message and
submit_justification stay consistent.
Status — PARKED / experimental (ADR-0005; docs/mop-enforce-decision.md Issue 2 /
option 2A). This gate predates the ADR-0002 two-phase engine and is NOT
deterministic-authoritative: `submit_message` feeds only lint-tagged hints
(`collect_lint_hints`) to the evaluator and lets its verdict stand with no
deterministic re-check, so the 1A delivery guarantee in `host.py`/`cli.py` does
NOT hold here. No live integration uses it; it is positioned for a future
Agent-SDK host (Claude Code on `claude_agent_sdk`). Do not patch new semantics
into this path piecemeal — when a real Agent-SDK host arrives, rebuild
`submit_message`/`submit_justification` as a thin state wrapper (justification
loop + failed-open) around `cli.check()`, so one engine owns verdict semantics.
The justification-loop design here is worth preserving; the verdict logic is not.
"""
from __future__ import annotations
from .audit import Auditor
from .rules import Rule, collect_lint_hints, collect_regex_hints
from .types import (
Accepted,
AcceptedFailedOpen,
Deliver,
Evaluator,
NoPendingMessageError,
Rejected,
Rewritten,
Verdict,
)
class MOP:
"""Stateful protocol object. One per CC session."""
def __init__(
self,
*,
rules: list[Rule],
evaluator: Evaluator,
deliver: Deliver,
max_justification_attempts: int = 4,
auditor: Auditor | None = None,
) -> None:
self.rules = rules
self.evaluator = evaluator
self.deliver = deliver
self.max_justification_attempts = max_justification_attempts
self.auditor = auditor
# Per-session state.
self.pending_message: str | None = None
self.justification_attempts: int = 0
self.sent_message_this_turn: bool = False
# ─── Tools ────────────────────────────────────────────────────────
async def submit_message(self, msg: str) -> Verdict:
lint_hints = collect_lint_hints(msg, self.rules)
verdict = await self.evaluator(msg, lint_hints, None)
return await self._apply(verdict, source=msg, attempt=0, justification=None)
async def submit_justification(self, justification: str) -> Verdict:
if self.pending_message is None:
raise NoPendingMessageError(
"submit_justification called with no pending message"
)
pending = self.pending_message
self.justification_attempts += 1
attempt = self.justification_attempts
if self.justification_attempts > self.max_justification_attempts:
return await self._failed_open(pending, attempt=attempt, justification=justification)
lint_hints = collect_lint_hints(pending, self.rules)
verdict = await self.evaluator(pending, lint_hints, justification)
return await self._apply(verdict, source=pending, attempt=attempt, justification=justification)
def get_rules(self, regex_filter: str | None = None) -> list[Rule]:
if regex_filter is None:
return list(self.rules)
import re
pat = re.compile(regex_filter)
return [
r
for r in self.rules
if pat.search(r.name) or pat.search(r.guidance)
]
def get_status(self) -> dict:
return {
"pending": self.pending_message,
"sent_this_turn": self.sent_message_this_turn,
"just_attempts": self.justification_attempts,
}
# ─── Internal ─────────────────────────────────────────────────────
async def _apply(
self,
verdict: Verdict,
*,
source: str,
attempt: int,
justification: str | None,
) -> Verdict:
"""State transitions + deliver-side-effect for any verdict."""
self._audit(source, verdict, attempt, justification)
if isinstance(verdict, Accepted):
await self.deliver(source, None)
self._reset_after_send()
return verdict
if isinstance(verdict, Rewritten):
await self.deliver(verdict.rewritten, None)
self._reset_after_send()
return verdict
if isinstance(verdict, Rejected):
self.pending_message = source
return verdict
# AcceptedFailedOpen is only produced by _failed_open(), not the evaluator.
# If we ever get one here it's a bug — pass through without state change.
return verdict
async def _failed_open(
self, pending: str, *, attempt: int, justification: str | None
) -> Verdict:
"""Justification budget exhausted. Deliver original + system note. Reset."""
note = (
f"MOP failed-open after {self.max_justification_attempts} "
f"justification attempts — original delivered despite rule violations"
)
verdict = AcceptedFailedOpen(system_note=note)
self._audit(pending, verdict, attempt, justification)
await self.deliver(pending, note)
self._reset_after_send()
return verdict
def _audit(
self,
source: str,
verdict: Verdict,
attempt: int,
justification: str | None,
) -> None:
if self.auditor is None:
return
self.auditor.record(
original=source,
verdict=verdict,
rule_names=[r.name for r in self.rules],
attempt=attempt,
justification=justification,
)
def _reset_after_send(self) -> None:
self.pending_message = None
self.justification_attempts = 0
self.sent_message_this_turn = True