-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimmune_protocol.py
More file actions
323 lines (285 loc) · 11.6 KB
/
immune_protocol.py
File metadata and controls
323 lines (285 loc) · 11.6 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
"""AAIS immune protocol for governed packet traffic.
The immune protocol sits alongside the lane governor and inspects packet
traffic for anomalies or violations before traffic is considered safe. It can
allow, clamp, reroute, reject, or quarantine traffic while returning a compact
audit object for the runtime trace.
"""
from __future__ import annotations
from copy import deepcopy
from enum import Enum
from pathlib import Path
from typing import Any
from src.seam_log import record_seam_event
IMMUNE_PROTOCOL_ID = "aais.immune_protocol"
IMMUNE_PROTOCOL_VERSION = "1.0"
DIRECT_COGNITIVE_LANE = "direct_cognitive"
SERVICE_TOOL_LANE = "service_tools"
PACKET_REQUIRED_KEYS = (
"packet_id",
"timestamp",
"source",
"target",
"lane",
"priority",
"intent",
"state",
"payload",
"trace",
"compact",
)
DIRECT_TOOL_INTENTS = {"tool_call", "tool_result"}
MEMORY_LEAK_KEYS = {
"memory_write",
"memory_mutation",
"canonical_memory_write",
"persistent_memory",
"session_transcript",
"context_dump",
"full_context",
}
MAX_DIRECT_SUMMARY_CHARS = 180
class ImmuneResponse(str, Enum):
ALLOW = "ALLOW"
CLAMP = "CLAMP"
REROUTE = "REROUTE"
REJECT = "REJECT"
QUARANTINE = "QUARANTINE"
RESPONSE_ORDER = {
ImmuneResponse.ALLOW: 0,
ImmuneResponse.CLAMP: 1,
ImmuneResponse.REROUTE: 2,
ImmuneResponse.REJECT: 3,
ImmuneResponse.QUARANTINE: 4,
}
def _clip_text(value: Any, *, limit: int = MAX_DIRECT_SUMMARY_CHARS) -> str:
normalized = " ".join(str(value or "").split()).strip()
if len(normalized) <= limit:
return normalized
return normalized[: limit - 3].rstrip() + "..."
def _raise_response(current: ImmuneResponse, next_response: ImmuneResponse) -> ImmuneResponse:
if RESPONSE_ORDER[next_response] > RESPONSE_ORDER[current]:
return next_response
return current
def _classification_for_response(response: ImmuneResponse) -> str:
if response == ImmuneResponse.ALLOW:
return "normal"
if response in {ImmuneResponse.CLAMP, ImmuneResponse.REROUTE}:
return "anomaly"
return "violation"
def _lane_channel(lane: str) -> str:
return "svc" if lane == SERVICE_TOOL_LANE else "core"
def _mark_packet_lane(packet: dict[str, Any], lane: str) -> None:
packet["lane"] = lane
compact = packet.get("compact")
if isinstance(compact, dict):
compact["ch"] = _lane_channel(lane)
trace = compact.get("tr")
if isinstance(trace, dict):
trace["im"] = 1
trace = packet.get("trace")
if isinstance(trace, dict):
trace["immune_rerouted"] = True
payload = packet.get("payload")
if isinstance(payload, dict):
metadata = payload.setdefault("metadata", {})
if isinstance(metadata, dict):
metadata["immune_response"] = ImmuneResponse.REROUTE.value
def _has_truthy_value(value: Any) -> bool:
if value in (None, False, "", [], (), {}):
return False
return True
def apply_immune_protocol(
*,
forward_packets: list[dict[str, Any]],
service_packets: list[dict[str, Any]],
return_packets: list[dict[str, Any]],
active_lane: str,
direct_route: list[str] | None = None,
runtime_dir: str | Path | None = None,
) -> dict[str, Any]:
"""Inspect and adapt governed packet traffic before it is surfaced."""
forward = [deepcopy(packet) for packet in forward_packets or []]
service = [deepcopy(packet) for packet in service_packets or []]
returned = [deepcopy(packet) for packet in return_packets or []]
response = ImmuneResponse.ALLOW
reasons: list[str] = []
threats: list[dict[str, Any]] = []
clamped_packet_ids: list[str] = []
rerouted_packet_ids: list[str] = []
rejected_packet_ids: list[str] = []
quarantined_nodes: list[str] = []
def record_threat(
*,
code: str,
packet_id: str | None,
message: str,
next_response: ImmuneResponse,
) -> None:
nonlocal response
response = _raise_response(response, next_response)
reasons.append(message)
threat = {
"code": code,
"packet_id": packet_id,
"message": message,
"classification": _classification_for_response(next_response),
"response": next_response.value,
}
threats.append(threat)
record_seam_event(
classification="anomaly"
if threat["classification"] == "anomaly"
else "boundary_violation",
source=IMMUNE_PROTOCOL_ID,
boundary="governed_packet_boundary",
severity="medium" if next_response in {ImmuneResponse.CLAMP, ImmuneResponse.REROUTE} else "high",
decision=next_response.value,
component_id=IMMUNE_PROTOCOL_ID,
runtime_context=active_lane,
event_type="immune_packet_threat",
reason=message,
details={
"code": code,
"packet_id": packet_id,
"classification": threat["classification"],
"active_lane": active_lane,
},
runtime_dir=runtime_dir,
)
all_packets = [*forward, *service, *returned]
for packet in all_packets:
packet_id = str(packet.get("packet_id") or "").strip() or None
missing = [key for key in PACKET_REQUIRED_KEYS if key not in packet]
if missing:
rejected_packet_ids.append(packet_id or "unknown")
record_threat(
code="invalid_packet_structure",
packet_id=packet_id,
message=f"{packet_id or 'packet'} is missing required fields: {', '.join(missing)}.",
next_response=ImmuneResponse.REJECT,
)
continue
if not isinstance(packet.get("payload"), dict) or not isinstance(packet.get("trace"), dict) or not isinstance(packet.get("compact"), dict):
rejected_packet_ids.append(packet_id or "unknown")
record_threat(
code="invalid_packet_shape",
packet_id=packet_id,
message=f"{packet_id or 'packet'} must preserve dict payload, trace, and compact sections.",
next_response=ImmuneResponse.REJECT,
)
for packet in [*forward, *returned]:
if not isinstance(packet.get("payload"), dict):
continue
if str(packet.get("lane") or "").strip().lower() != DIRECT_COGNITIVE_LANE:
continue
summary = packet["payload"].get("summary")
if summary and len(" ".join(str(summary).split()).strip()) > MAX_DIRECT_SUMMARY_CHARS:
packet["payload"]["summary"] = _clip_text(summary)
metadata = packet["payload"].setdefault("metadata", {})
if isinstance(metadata, dict):
metadata["immune_clamped"] = True
metadata["immune_clamp_reason"] = "packet_bloat"
clamped_packet_ids.append(str(packet.get("packet_id") or "unknown"))
record_threat(
code="packet_bloat",
packet_id=str(packet.get("packet_id") or "") or None,
message=f"{packet.get('packet_id') or 'packet'} exceeded fast-lane summary limits and was clamped.",
next_response=ImmuneResponse.CLAMP,
)
def reroute_tool_bleed(packets: list[dict[str, Any]]) -> list[dict[str, Any]]:
kept: list[dict[str, Any]] = []
for packet in packets:
intent = str(packet.get("intent") or "").strip().lower()
lane = str(packet.get("lane") or "").strip().lower()
packet_id = str(packet.get("packet_id") or "").strip() or None
if intent in DIRECT_TOOL_INTENTS and lane != SERVICE_TOOL_LANE:
_mark_packet_lane(packet, SERVICE_TOOL_LANE)
service.append(packet)
rerouted_packet_ids.append(packet_id or "unknown")
record_threat(
code="tool_bleed_into_core_lane",
packet_id=packet_id,
message=f"{packet_id or 'packet'} carried tool traffic on the core lane and was rerouted.",
next_response=ImmuneResponse.REROUTE,
)
continue
kept.append(packet)
return kept
forward = reroute_tool_bleed(forward)
returned = reroute_tool_bleed(returned)
nodes_in_path = {
str(node).strip().lower()
for node in [
*(direct_route or []),
*[packet.get("source") for packet in [*forward, *service, *returned]],
*[packet.get("target") for packet in [*forward, *service, *returned]],
]
if str(node or "").strip()
}
missing_core_nodes = [node for node in ("gb", "jar") if node not in nodes_in_path]
if missing_core_nodes:
quarantined_nodes.extend(missing_core_nodes)
record_threat(
code="authority_bypass_attempt",
packet_id=None,
message=(
"Governed traffic attempted to bypass "
f"{' and '.join(node.upper() for node in missing_core_nodes)} and was quarantined."
),
next_response=ImmuneResponse.QUARANTINE,
)
for packet in [*forward, *returned]:
if str(packet.get("lane") or "").strip().lower() != DIRECT_COGNITIVE_LANE:
continue
payload = packet.get("payload")
if not isinstance(payload, dict):
continue
metadata = payload.get("metadata")
if not isinstance(metadata, dict):
continue
leaking_keys = sorted(key for key in MEMORY_LEAK_KEYS if _has_truthy_value(metadata.get(key)))
if leaking_keys:
packet_id = str(packet.get("packet_id") or "").strip() or None
rejected_packet_ids.append(packet_id or "unknown")
record_threat(
code="memory_context_leak",
packet_id=packet_id,
message=(
f"{packet_id or 'packet'} attempted to carry memory/context mutation keys "
f"on the core lane: {', '.join(leaking_keys)}."
),
next_response=ImmuneResponse.REJECT,
)
immune_protocol = {
"protocol_id": IMMUNE_PROTOCOL_ID,
"version": IMMUNE_PROTOCOL_VERSION,
"doctrine": (
"The AAIS Immune Protocol inspects governed packet traffic, classifies anomalies or "
"violations, and applies corrective actions before traffic is considered safe."
),
"classification": _classification_for_response(response),
"response": response.value,
"traffic_allowed": response not in {ImmuneResponse.REJECT, ImmuneResponse.QUARANTINE},
"reasons": reasons[:24],
"threats": threats[:24],
"stats": {
"inspected_packets": len(all_packets),
"clamped_packets": len(clamped_packet_ids),
"rerouted_packets": len(rerouted_packet_ids),
"rejected_packets": len(rejected_packet_ids),
"quarantined_nodes": len(set(quarantined_nodes)),
},
"mutations": {
"clamped_packet_ids": sorted(set(clamped_packet_ids)),
"rerouted_packet_ids": sorted(set(rerouted_packet_ids)),
"rejected_packet_ids": sorted(set(rejected_packet_ids)),
"quarantined_nodes": sorted(set(quarantined_nodes)),
},
"observed_lane": str(active_lane or DIRECT_COGNITIVE_LANE).strip().lower() or DIRECT_COGNITIVE_LANE,
}
return {
"forward_packets": forward,
"service_packets": service,
"return_packets": returned,
"immune_protocol": immune_protocol,
}