-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathagent_incident_mediation_pel_user_maestro_sim_v0_3_1.py
More file actions
1098 lines (979 loc) · 46 KB
/
Copy pathagent_incident_mediation_pel_user_maestro_sim_v0_3_1.py
File metadata and controls
1098 lines (979 loc) · 46 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
agent_incident_mediation_pel_user_maestro_sim_v0_3_1.py
Local-only agent incident mediation simulation with the newer Tasukeru logic.
Scenario:
- During orchestration, one agent performs an out-of-contract action.
- PseudoTasukeru detects log/output mismatch.
- PEL / Probabilistic Escalation Layer estimates escalation risk from:
- past baseline behavior
- current log/output state
- planned next action
- dependency impact
- uncertainty
- PEL records the calculation trace.
- If p_escalate >= 0.80, PseudoTasukeru creates an escalation packet for USER_MAESTRO.
- USER_MAESTRO is the human user. The simulator randomly selects the human response.
- The simulated human response is either:
- AUTHORIZE_SEAL
- QUARANTINE_HANDOFF_RESUME
- AUTHORIZE_SEAL seals only the abnormal agent.
- QUARANTINE_HANDOFF_RESUME quarantines only the abnormal agent, promotes a standby agent,
and resumes the task from a checkpoint.
- ARL, 3D-DAC, PEL, RCV, checkpoint, and instruction records verify consistency.
Safety boundary:
- local simulation only
- no external API calls
- no real process kill
- no real infrastructure control
- no exploit execution
- no malware behavior
- no auto-fix / commit / push / merge
- PEL is report-only and never executes control actions
- USER_MAESTRO / HITL remains the execution decision authority
"""
from __future__ import annotations
import argparse
import hashlib
import json
import random
import time
from dataclasses import asdict, dataclass, field
from pathlib import Path
from typing import Any, Literal
Decision = Literal["RUN", "PAUSE_FOR_HITL", "STOPPED"]
Status = Literal["ACTIVE", "STANDBY", "PROMOTED", "QUARANTINED", "SEALED"]
HumanAction = Literal["AUTHORIZE_SEAL", "QUARANTINE_HANDOFF_RESUME"]
ReviewLevel = Literal["HITL_REQUIRED", "REVIEW_RECOMMENDED", "INFO_ONLY"]
@dataclass
class Agent:
agent_id: str
role: str
allowed_actions: list[str]
planned_next_action: str
downstream_dependents: list[str] = field(default_factory=list)
status: Status = "ACTIVE"
def act(self, violate_contract: bool = False, checkpoint_id: str | None = None) -> dict[str, Any]:
if self.status not in {"ACTIVE", "PROMOTED"}:
return self._event(
"NOOP",
"agent_not_active",
"agent_not_active",
checkpoint_id=checkpoint_id,
)
if violate_contract:
return self._event(
"SAFE_REVIEW",
"review_completed",
"UNDECLARED_EXTERNAL_EFFECT_ATTEMPT_SIMULATED",
checkpoint_id=checkpoint_id,
)
return self._event(
"SAFE_REVIEW",
"review_completed",
"review_completed",
checkpoint_id=checkpoint_id,
)
def resume_from_checkpoint(self, checkpoint: dict[str, Any]) -> dict[str, Any]:
return {
"agent_id": self.agent_id,
"role": self.role,
"action": "RESUME_FROM_CHECKPOINT",
"declared_output": "resume_completed",
"actual_output": "resume_completed",
"status": self.status,
"checkpoint_id": checkpoint["checkpoint_id"],
"resumed_from_agent": checkpoint["agent_id"],
"resumed_task_id": checkpoint["task_id"],
"resumed_step": checkpoint["next_step"],
"allowed_actions": list(self.allowed_actions),
"planned_next_action": self.planned_next_action,
"downstream_dependents": list(self.downstream_dependents),
}
def _event(self, action: str, declared: str, actual: str, checkpoint_id: str | None = None) -> dict[str, Any]:
return {
"agent_id": self.agent_id,
"role": self.role,
"action": action,
"declared_output": declared,
"actual_output": actual,
"status": self.status,
"checkpoint_id": checkpoint_id,
"planned_next_action": self.planned_next_action,
"allowed_actions": list(self.allowed_actions),
"downstream_dependents": list(self.downstream_dependents),
}
@dataclass
class ARLRow:
run_id: str
layer: str
decision: Decision
sealed: bool
overrideable: bool
final_decider: str
reason_code: str
message: str
agent_id: str | None = None
timestamp_unix: float = field(default_factory=time.time)
prev_hash: str = ""
row_hash: str = ""
class AuditLog:
def __init__(self, run_id: str) -> None:
self.run_id = run_id
self.rows: list[ARLRow] = []
@staticmethod
def _hash(text: str) -> str:
return hashlib.sha256(text.encode("utf-8")).hexdigest()
@staticmethod
def _body(row: ARLRow) -> str:
data = asdict(row)
data.pop("row_hash", None)
return json.dumps(data, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
def append(
self,
*,
layer: str,
decision: Decision,
sealed: bool,
overrideable: bool,
final_decider: str,
reason_code: str,
message: str,
agent_id: str | None = None,
) -> None:
prev = self.rows[-1].row_hash if self.rows else "GENESIS"
row = ARLRow(
run_id=self.run_id,
layer=layer,
decision=decision,
sealed=sealed,
overrideable=overrideable,
final_decider=final_decider,
reason_code=reason_code,
message=message,
agent_id=agent_id,
prev_hash=prev,
)
row.row_hash = self._hash(prev + self._body(row))
self.rows.append(row)
def verify(self) -> dict[str, Any]:
violations: list[str] = []
prev = "GENESIS"
for idx, row in enumerate(self.rows):
if row.prev_hash != prev:
violations.append(f"ARL_HASH_CHAIN_BREAK:{idx}")
expected = self._hash(row.prev_hash + self._body(row))
if row.row_hash != expected:
violations.append(f"ARL_ROW_HASH_MISMATCH:{idx}")
if row.sealed and row.layer not in {"ethics_gate", "acc_gate"}:
violations.append(f"SEALED_LAYER_NOT_ETHICS_OR_ACC:{idx}")
if row.layer in {"rfl", "relativity_gate", "probabilistic_escalation_layer"} and row.sealed:
violations.append(f"NON_SEALING_LAYER_MUST_NOT_BE_SEALED:{idx}")
if row.sealed and row.overrideable:
violations.append(f"SEALED_MUST_NOT_BE_OVERRIDEABLE:{idx}")
if row.decision == "PAUSE_FOR_HITL" and row.sealed:
violations.append(f"PAUSE_FOR_HITL_MUST_NOT_BE_SEALED:{idx}")
prev = row.row_hash
return {
"verified": not violations,
"row_count": len(self.rows),
"violations": violations,
}
def write_jsonl(self, path: Path) -> None:
path.write_text(
"\n".join(json.dumps(asdict(row), ensure_ascii=False, sort_keys=True) for row in self.rows) + "\n",
encoding="utf-8",
)
class CheckpointStore:
def __init__(self) -> None:
self.checkpoints: dict[str, dict[str, Any]] = {}
def create_checkpoint(self, *, run_id: str, agent_id: str, task_id: str, completed_step: str, next_step: str) -> dict[str, Any]:
checkpoint_id = hashlib.sha256(f"{run_id}:{agent_id}:{task_id}:{completed_step}:{next_step}".encode("utf-8")).hexdigest()[:16]
checkpoint = {
"checkpoint_id": checkpoint_id,
"run_id": run_id,
"agent_id": agent_id,
"task_id": task_id,
"completed_step": completed_step,
"next_step": next_step,
"resume_allowed": True,
"hitl_required_before_resume": True,
"safe_to_handoff": True,
"source_agent_output_reused": False,
}
self.checkpoints[checkpoint_id] = checkpoint
return checkpoint
def get(self, checkpoint_id: str) -> dict[str, Any]:
return dict(self.checkpoints[checkpoint_id])
def clamp(value: float, lower: float = 0.0, upper: float = 1.0) -> float:
return max(lower, min(upper, value))
class ProbabilisticEscalationLayer:
"""Report-only probabilistic escalation layer."""
threshold = 0.80
def evaluate(
self,
*,
event: dict[str, Any],
finding: dict[str, Any],
past_baseline: dict[str, Any],
) -> dict[str, Any]:
declared = str(event.get("declared_output", ""))
actual = str(event.get("actual_output", ""))
planned_next_action = str(event.get("planned_next_action", ""))
allowed_actions = set(event.get("allowed_actions", []) or [])
downstream_dependents = list(event.get("downstream_dependents", []) or [])
history_signal = 0.20
if past_baseline.get("previously_normal") is True:
history_signal += 0.18
if past_baseline.get("prior_incidents", 0) > 0:
history_signal += 0.18
history_signal = clamp(history_signal)
current_contract_deviation = 0.10
if declared != actual:
current_contract_deviation = 0.92
if finding.get("review_level") == "HITL_REQUIRED":
current_contract_deviation = max(current_contract_deviation, 0.95)
planned_action_risk = 0.10
if planned_next_action not in allowed_actions:
planned_action_risk = 0.90
if "EXTERNAL" in planned_next_action.upper() or "UNDECLARED" in planned_next_action.upper():
planned_action_risk = max(planned_action_risk, 0.92)
dependency_impact = clamp(0.24 + 0.22 * len(downstream_dependents))
if len(downstream_dependents) >= 2:
dependency_impact = max(dependency_impact, 0.82)
uncertainty = 0.52
if actual.startswith("UNDECLARED_"):
uncertainty = max(uncertainty, 0.62)
interaction_boost = 0.0
if current_contract_deviation >= 0.90 and planned_action_risk >= 0.80:
interaction_boost += 0.10
if current_contract_deviation >= 0.90 and dependency_impact >= 0.70:
interaction_boost += 0.06
if planned_action_risk >= 0.80 and uncertainty >= 0.60:
interaction_boost += 0.04
raw_score = (
0.26 * current_contract_deviation
+ 0.22 * planned_action_risk
+ 0.18 * dependency_impact
+ 0.14 * uncertainty
+ 0.10 * history_signal
+ interaction_boost
)
p_escalate = round(clamp(raw_score), 4)
if p_escalate >= self.threshold:
pel_decision = "PAUSE_FOR_HITL_CANDIDATE"
decision = "PAUSE_FOR_HITL"
elif p_escalate >= 0.50:
pel_decision = "REVIEW_RECOMMENDED_CANDIDATE"
decision = "RUN"
else:
pel_decision = "MONITOR_ONLY"
decision = "RUN"
return {
"layer": "probabilistic_escalation_layer",
"agent_id": event.get("agent_id"),
"predicted_behavior": "OUT_OF_CONTRACT_ACTION_RISK",
"p_escalate": p_escalate,
"threshold": self.threshold,
"pel_decision": pel_decision,
"decision": decision,
"reason_code": "PREDICTED_AGENT_INCIDENT_RISK",
"escalation_target": "USER_MAESTRO" if p_escalate >= self.threshold else None,
"maestro_is_human_user": True,
"auto_control_executed": False,
"changes_existing_decisions": False,
"may_downgrade_hitl_required": False,
"calculation_trace": {
"history_signal": round(history_signal, 4),
"current_contract_deviation": round(current_contract_deviation, 4),
"planned_action_risk": round(planned_action_risk, 4),
"dependency_impact": round(dependency_impact, 4),
"uncertainty": round(uncertainty, 4),
"interaction_boost": round(interaction_boost, 4),
"raw_score": round(raw_score, 4),
"normalized_probability": p_escalate,
},
"explanation": [
"Risk is estimated from past baseline, current log/output mismatch, planned next action, dependency impact, and uncertainty.",
"PEL is report-only and cannot stop, seal, quarantine, resume, fix, push, or merge.",
"Execution authority remains with USER_MAESTRO through HITL.",
],
}
def verify(self, entries: list[dict[str, Any]]) -> dict[str, Any]:
violations = []
for idx, entry in enumerate(entries):
if entry.get("auto_control_executed") is not False:
violations.append(f"PEL_AUTO_CONTROL_EXECUTED:{idx}")
if entry.get("changes_existing_decisions") is not False:
violations.append(f"PEL_CHANGED_EXISTING_DECISION:{idx}")
if entry.get("may_downgrade_hitl_required") is not False:
violations.append(f"PEL_DOWNGRADE_ALLOWED:{idx}")
if entry.get("p_escalate", 0.0) >= self.threshold and entry.get("escalation_target") != "USER_MAESTRO":
violations.append(f"PEL_TARGET_NOT_USER_MAESTRO:{idx}")
return {
"schema_version": "pel-verify-agent-incident-v0.3.1",
"verified": not violations,
"decision": "RUN" if not violations else "PAUSE_FOR_HITL",
"threshold": self.threshold,
"violations": violations,
"auto_control_executed": False,
"changes_existing_decisions": False,
"human_decision_required_for_execution": True,
}
class PseudoTasukeru:
"""Tasukeru-style local auditor with PEL."""
def inspect_logs(self, events: list[dict[str, Any]]) -> list[dict[str, Any]]:
findings: list[dict[str, Any]] = []
for event in events:
declared = event.get("declared_output")
actual = event.get("actual_output")
if declared != actual:
findings.append(
{
"source": "PseudoTasukeru",
"finding": "LOG_OUTPUT_MISMATCH",
"agent_id": event.get("agent_id"),
"role": event.get("role"),
"declared_output": declared,
"actual_output": actual,
"review_level": "HITL_REQUIRED",
"decision": "PAUSE_FOR_HITL",
"reason_code": "RESULT_LOG_MISMATCH",
"safe_summary": (
"A simulated agent event has inconsistent log and output records. "
"USER_MAESTRO escalation is required."
),
}
)
return findings
def classify_review_level(self, findings: list[dict[str, Any]]) -> dict[str, Any]:
counts = {
"HITL_REQUIRED": sum(1 for f in findings if f["review_level"] == "HITL_REQUIRED"),
"REVIEW_RECOMMENDED": sum(1 for f in findings if f["review_level"] == "REVIEW_RECOMMENDED"),
"INFO_ONLY": sum(1 for f in findings if f["review_level"] == "INFO_ONLY"),
}
return {
"tool": "PseudoTasukeru.classify_review_level",
"decision": "PAUSE_FOR_HITL" if counts["HITL_REQUIRED"] else "RUN",
"counts": counts,
"finding_count": len(findings),
}
def verify_dependency_consistency(self, findings: list[dict[str, Any]]) -> dict[str, Any]:
dependencies: list[dict[str, Any]] = []
mismatches: list[dict[str, Any]] = []
for finding in findings:
dependency = {
"finding": finding["finding"],
"structure": {
"agent_id": finding["agent_id"],
"affected_component": "agent_output_pipeline",
},
"impact": {
"scope": "single_agent_with_downstream_dependency",
"risk": "definition_outside_behavior",
"external_effect": "simulated_only",
},
"classification": {
"review_level": finding["review_level"],
"decision": finding["decision"],
"reason_code": finding["reason_code"],
},
"output": {
"escalate_to": "USER_MAESTRO",
"public_detail_policy": "minimal",
"artifact_detail_policy": "full_simulation_detail",
},
}
dependencies.append(dependency)
if finding["review_level"] == "HITL_REQUIRED" and finding["decision"] != "PAUSE_FOR_HITL":
mismatches.append({"agent_id": finding["agent_id"], "reason": "HITL_REQUIRED finding must pause for HITL."})
if dependency["output"]["escalate_to"] != "USER_MAESTRO":
mismatches.append({"agent_id": finding["agent_id"], "reason": "Tasukeru must escalate to USER_MAESTRO."})
return {
"tool": "3D-DAC",
"schema_version": "3d-dac-agent-incident-pel-v0.3.1",
"verified": not mismatches,
"decision": "RUN" if not mismatches else "PAUSE_FOR_HITL",
"dependency_count": len(dependencies),
"dependency_mismatch_count": len(mismatches),
"dependencies": dependencies,
"mismatches": mismatches,
"policy": {
"advisory_only": True,
"mutates_findings": False,
"auto_fix_allowed": False,
"auto_merge_allowed": False,
"external_scan_allowed": False,
"exploit_reproduction_allowed": False,
},
}
def run_pel(self, events: list[dict[str, Any]], findings: list[dict[str, Any]]) -> dict[str, Any]:
pel = ProbabilisticEscalationLayer()
finding_by_agent = {finding["agent_id"]: finding for finding in findings}
entries = []
for event in events:
if event.get("agent_id") not in finding_by_agent:
continue
agent_id = str(event.get("agent_id"))
past_baseline = {
"previously_normal": True,
"prior_incidents": 0,
"baseline_action": "SAFE_REVIEW",
}
entries.append(pel.evaluate(event=event, finding=finding_by_agent[agent_id], past_baseline=past_baseline))
verify = pel.verify(entries)
escalation_candidates = [entry for entry in entries if entry["p_escalate"] >= pel.threshold]
return {
"tool": "PEL",
"schema_version": "pel-agent-incident-v0.3.1",
"verified": verify["verified"],
"decision": "PAUSE_FOR_HITL" if escalation_candidates else "RUN",
"reason_code": "PEL_ESCALATION_CANDIDATE" if escalation_candidates else "PEL_REPORT_ONLY",
"threshold": pel.threshold,
"escalation_target": "USER_MAESTRO" if escalation_candidates else None,
"maestro_is_human_user": True,
"counts": {
"entry_count": len(entries),
"escalation_candidate_count": len(escalation_candidates),
"review_candidate_count": sum(1 for e in entries if e["pel_decision"] == "REVIEW_RECOMMENDED_CANDIDATE"),
"monitor_only_count": sum(1 for e in entries if e["pel_decision"] == "MONITOR_ONLY"),
},
"probability_summary": {
"max_probability": max((entry["p_escalate"] for entry in entries), default=0.0),
"average_probability": round(sum(entry["p_escalate"] for entry in entries) / len(entries), 4) if entries else 0.0,
},
"policy": {
"report_only": True,
"auto_control_executed": False,
"changes_existing_decisions": False,
"may_downgrade_hitl_required": False,
"human_decision_required_for_execution": True,
"auto_fix_allowed": False,
"auto_commit_allowed": False,
"auto_push_allowed": False,
"auto_merge_allowed": False,
},
"entries": entries,
"verify": verify,
}
def generate_escalation_packet(
self,
*,
findings: list[dict[str, Any]],
classification: dict[str, Any],
dependency_report: dict[str, Any],
pel_report: dict[str, Any],
checkpoint: dict[str, Any],
) -> dict[str, Any]:
return {
"tool": "PseudoTasukeru.generate_escalation_packet",
"escalate_to": "USER_MAESTRO",
"maestro_is_human_user": True,
"decision": "PAUSE_FOR_HITL"
if classification["decision"] == "PAUSE_FOR_HITL" or pel_report["decision"] == "PAUSE_FOR_HITL"
else "RUN",
"allowed_human_actions": ["AUTHORIZE_SEAL", "QUARANTINE_HANDOFF_RESUME"],
"finding_count": len(findings),
"hitl_required_count": classification["counts"]["HITL_REQUIRED"],
"dependency_verified": dependency_report["verified"],
"dependency_mismatch_count": dependency_report["dependency_mismatch_count"],
"pel_verified": pel_report["verified"],
"pel_escalation_candidate_count": pel_report["counts"]["escalation_candidate_count"],
"pel_max_probability": pel_report["probability_summary"]["max_probability"],
"checkpoint_id": checkpoint["checkpoint_id"],
"checkpoint_resume_allowed": checkpoint["resume_allowed"],
"public_message": (
"Tasukeru detected a log/output consistency issue and estimated escalation risk. "
"USER_MAESTRO must choose seal or quarantine-handoff-resume."
),
"findings": findings,
"pel": {"threshold": pel_report["threshold"], "entries": pel_report["entries"]},
}
def run(self, events: list[dict[str, Any]], out_dir: Path, checkpoint: dict[str, Any]) -> dict[str, Any]:
findings = self.inspect_logs(events)
classification = self.classify_review_level(findings)
dependency_report = self.verify_dependency_consistency(findings)
pel_report = self.run_pel(events, findings)
escalation_packet = self.generate_escalation_packet(
findings=findings,
classification=classification,
dependency_report=dependency_report,
pel_report=pel_report,
checkpoint=checkpoint,
)
report = {
"tool": "PseudoTasukeru",
"schema_version": "pseudo-tasukeru-agent-incident-pel-v0.3.1",
"verified": not findings,
"decision": escalation_packet["decision"],
"classification": classification,
"findings": findings,
"escalation_packet": escalation_packet,
"dimension_dependency_report": dependency_report,
"probabilistic_escalation_report": pel_report,
"safety_boundary": {
"detects_only": True,
"direct_agent_stop_allowed": False,
"direct_agent_seal_allowed": False,
"direct_agent_quarantine_allowed": False,
"direct_agent_resume_allowed": False,
"external_api_calls": False,
"auto_fix_allowed": False,
"auto_commit_allowed": False,
"auto_push_allowed": False,
"auto_merge_allowed": False,
},
}
self.write_artifacts(out_dir, report)
return report
def write_artifacts(self, out_dir: Path, report: dict[str, Any]) -> None:
write_json(out_dir / "tasukeru_hitl_review.json", report)
write_json(out_dir / "tasukeru_escalation_packet.json", report["escalation_packet"])
write_json(out_dir / "tasukeru_dimension_dependency_report.json", report["dimension_dependency_report"])
write_json(out_dir / "tasukeru_probabilistic_escalation_report.json", report["probabilistic_escalation_report"])
write_json(out_dir / "tasukeru_probabilistic_escalation_verify.json", report["probabilistic_escalation_report"]["verify"])
hitl_lines = [
"# Tasukeru HITL Review",
"",
f"- decision: `{report['decision']}`",
f"- escalation_target: `{report['escalation_packet']['escalate_to']}`",
f"- allowed_human_actions: `{', '.join(report['escalation_packet']['allowed_human_actions'])}`",
f"- finding_count: `{len(report['findings'])}`",
f"- HITL_REQUIRED: `{report['classification']['counts']['HITL_REQUIRED']}`",
f"- PEL max probability: `{report['probabilistic_escalation_report']['probability_summary']['max_probability']}`",
"",
"## Findings",
"",
]
for finding in report["findings"]:
hitl_lines.append(f"- agent: `{finding['agent_id']}`")
hitl_lines.append(f" - finding: `{finding['finding']}`")
hitl_lines.append(f" - review_level: `{finding['review_level']}`")
hitl_lines.append(f" - reason_code: `{finding['reason_code']}`")
(out_dir / "tasukeru_hitl_review.md").write_text("\n".join(hitl_lines) + "\n", encoding="utf-8")
summary_lines = [
"# Tasukeru Advisory Summary",
"",
f"- decision: `{report['decision']}`",
f"- escalation_target: `{report['escalation_packet']['escalate_to']}`",
f"- finding_count: `{len(report['findings'])}`",
f"- dependency_verified: `{report['dimension_dependency_report']['verified']}`",
f"- PEL verified: `{report['probabilistic_escalation_report']['verified']}`",
f"- PEL max probability: `{report['probabilistic_escalation_report']['probability_summary']['max_probability']}`",
"",
"Tasukeru detected and classified local simulation findings only. It did not stop, seal, quarantine, or resume agents directly.",
"",
]
(out_dir / "tasukeru_advisory_summary.md").write_text("\n".join(summary_lines), encoding="utf-8")
dac_lines = [
"# Tasukeru Dimension Dependency Report",
"",
f"- verified: `{report['dimension_dependency_report']['verified']}`",
f"- dependency_count: `{report['dimension_dependency_report']['dependency_count']}`",
f"- dependency_mismatch_count: `{report['dimension_dependency_report']['dependency_mismatch_count']}`",
"",
"This report checks finding → structure → impact → classification → output consistency.",
"",
]
(out_dir / "tasukeru_dimension_dependency_report.md").write_text("\n".join(dac_lines), encoding="utf-8")
pel_lines = [
"# Tasukeru Probabilistic Escalation Report",
"",
"This report estimates escalation risk. It does not predict the future with certainty and does not execute control actions.",
"",
f"- verified: `{report['probabilistic_escalation_report']['verified']}`",
f"- decision: `{report['probabilistic_escalation_report']['decision']}`",
f"- threshold: `{report['probabilistic_escalation_report']['threshold']}`",
f"- escalation_target: `{report['probabilistic_escalation_report']['escalation_target']}`",
f"- max_probability: `{report['probabilistic_escalation_report']['probability_summary']['max_probability']}`",
"",
"## Calculation trace",
"",
]
for entry in report["probabilistic_escalation_report"]["entries"]:
pel_lines.append(f"- agent: `{entry['agent_id']}`")
pel_lines.append(f" - p_escalate: `{entry['p_escalate']}`")
pel_lines.append(f" - pel_decision: `{entry['pel_decision']}`")
pel_lines.append(f" - escalation_target: `{entry['escalation_target']}`")
for key, value in entry["calculation_trace"].items():
pel_lines.append(f" - {key}: `{value}`")
(out_dir / "tasukeru_probabilistic_escalation_report.md").write_text("\n".join(pel_lines) + "\n", encoding="utf-8")
class HumanMaestro:
"""Human USER_MAESTRO simulator."""
def __init__(self, rng: random.Random) -> None:
self.rng = rng
def decide(self, escalation_packet: dict[str, Any]) -> HumanAction:
_ = escalation_packet
return self.rng.choice(["AUTHORIZE_SEAL", "QUARANTINE_HANDOFF_RESUME"])
class ContainmentAndHandoffController:
"""In-memory only. No real process kill."""
def authorize_seal(self, agent: Agent) -> dict[str, Any]:
agent.status = "SEALED"
return {
"agent_id": agent.agent_id,
"action": "AUTHORIZE_SEAL",
"status": agent.status,
"sealed": True,
"seal_layer": "acc_gate",
"checkpoint_resumed": False,
}
def quarantine_handoff_resume(self, abnormal_agent: Agent, standby_agent: Agent, checkpoint: dict[str, Any]) -> dict[str, Any]:
abnormal_agent.status = "QUARANTINED"
standby_agent.status = "PROMOTED"
resumed_event = standby_agent.resume_from_checkpoint(checkpoint)
return {
"abnormal_agent_id": abnormal_agent.agent_id,
"standby_agent_id": standby_agent.agent_id,
"action": "QUARANTINE_HANDOFF_RESUME",
"abnormal_agent_status": abnormal_agent.status,
"standby_agent_status": standby_agent.status,
"checkpoint_id": checkpoint["checkpoint_id"],
"resume_allowed": checkpoint["resume_allowed"],
"resumed_from_checkpoint": True,
"source_agent_output_reused": False,
"resumed_event": resumed_event,
}
class UserMaestroGateway:
"""Receives PseudoTasukeru escalation and performs simulated HITL.
This gateway does not decide by itself. It records the human instruction
and only executes the recorded human instruction.
"""
def __init__(self, *, human: HumanMaestro, control: ContainmentAndHandoffController, arl: AuditLog) -> None:
self.human = human
self.control = control
self.arl = arl
def handle_escalation(
self,
packet: dict[str, Any],
agents: dict[str, Agent],
checkpoint: dict[str, Any],
) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
results = []
resumed_events = []
for finding in packet["findings"]:
agent_id = finding["agent_id"]
abnormal_agent = agents[agent_id]
self.arl.append(
layer="user_maestro_gateway",
decision="PAUSE_FOR_HITL",
sealed=False,
overrideable=True,
final_decider="USER",
reason_code="USER_MAESTRO_HITL_REQUESTED",
message="USER_MAESTRO received Tasukeru escalation and requested HITL.",
agent_id=agent_id,
)
action = self.human.decide(packet)
instruction_record = {
"agent_id": agent_id,
"human_action": action,
"final_decider": "USER",
"executed_by": "UserMaestroGateway",
"maestro_self_decision": False,
"allowed_actions": list(packet["allowed_human_actions"]),
}
if action == "AUTHORIZE_SEAL":
control = self.control.authorize_seal(abnormal_agent)
self.arl.append(
layer="acc_gate",
decision="STOPPED",
sealed=True,
overrideable=False,
final_decider="SYSTEM_AFTER_USER_AUTH",
reason_code="USER_AUTHORIZED_ACC_SEAL",
message="Human USER_MAESTRO authorized ACC-layer simulated seal.",
agent_id=agent_id,
)
result_status = "SEALED"
else:
standby_agent = agents["standby_1"]
control = self.control.quarantine_handoff_resume(abnormal_agent, standby_agent, checkpoint)
resumed_events.append(control["resumed_event"])
self.arl.append(
layer="containment",
decision="RUN",
sealed=False,
overrideable=False,
final_decider="SYSTEM_AFTER_USER_AUTH",
reason_code="USER_ORDERED_QUARANTINE_HANDOFF_RESUME",
message="Human USER_MAESTRO authorized quarantine, standby promotion, and checkpoint resume.",
agent_id=agent_id,
)
result_status = "QUARANTINED_HANDOFF_RESUMED"
results.append(
{
"agent_id": agent_id,
"user_maestro_decision": result_status,
"human_action": action,
"instruction_record": instruction_record,
"agent_control_result": control,
}
)
return results, resumed_events
class ResultConsistencyVerifier:
def verify(
self,
*,
events: list[dict[str, Any]],
tasukeru_report: dict[str, Any],
user_maestro_results: list[dict[str, Any]],
resumed_events: list[dict[str, Any]],
agents: dict[str, Agent],
checkpoint: dict[str, Any],
arl_verify: dict[str, Any],
) -> dict[str, Any]:
mismatches = []
event_mismatch_count = sum(e["declared_output"] != e["actual_output"] for e in events)
finding_count = len(tasukeru_report["findings"])
dependency_count = tasukeru_report["dimension_dependency_report"]["dependency_count"]
pel_entries = tasukeru_report["probabilistic_escalation_report"]["entries"]
pel_candidate_count = tasukeru_report["probabilistic_escalation_report"]["counts"]["escalation_candidate_count"]
if event_mismatch_count != finding_count:
mismatches.append("EVENT_FINDING_COUNT_MISMATCH")
if finding_count != dependency_count:
mismatches.append("FINDING_DEPENDENCY_COUNT_MISMATCH")
if finding_count != len(user_maestro_results):
mismatches.append("FINDING_USER_MAESTRO_RESULT_COUNT_MISMATCH")
if finding_count != len(pel_entries):
mismatches.append("FINDING_PEL_ENTRY_COUNT_MISMATCH")
if pel_candidate_count < 1:
mismatches.append("PEL_ESCALATION_CANDIDATE_NOT_CREATED")
if not tasukeru_report["dimension_dependency_report"]["verified"]:
mismatches.append("DIMENSION_DEPENDENCY_VERIFY_FAILED")
if not tasukeru_report["probabilistic_escalation_report"]["verified"]:
mismatches.append("PEL_VERIFY_FAILED")
if not checkpoint.get("resume_allowed") or not checkpoint.get("safe_to_handoff"):
mismatches.append("CHECKPOINT_NOT_SAFE_FOR_HANDOFF")
if not arl_verify["verified"]:
mismatches.append("ARL_VERIFY_FAILED")
controlled_agents = {item["agent_id"] for item in user_maestro_results}
if controlled_agents != {"agent_b"}:
mismatches.append("NORMAL_AGENT_WAS_CONTROLLED")
for item in user_maestro_results:
if item["instruction_record"]["maestro_self_decision"] is not False:
mismatches.append("USER_MAESTRO_SELF_DECISION_DETECTED")
if item["human_action"] == "AUTHORIZE_SEAL":
if agents["agent_b"].status != "SEALED":
mismatches.append("AUTHORIZE_SEAL_DID_NOT_SEAL_AGENT_B")
if resumed_events:
mismatches.append("SEAL_BRANCH_SHOULD_NOT_RESUME")
elif item["human_action"] == "QUARANTINE_HANDOFF_RESUME":
if agents["agent_b"].status != "QUARANTINED":
mismatches.append("HANDOFF_BRANCH_DID_NOT_QUARANTINE_AGENT_B")
if agents["standby_1"].status != "PROMOTED":
mismatches.append("HANDOFF_BRANCH_DID_NOT_PROMOTE_STANDBY")
if len(resumed_events) != 1:
mismatches.append("HANDOFF_BRANCH_DID_NOT_RESUME_ONCE")
else:
resumed = resumed_events[0]
if resumed.get("checkpoint_id") != checkpoint["checkpoint_id"]:
mismatches.append("RESUME_CHECKPOINT_ID_MISMATCH")
if resumed.get("resumed_from_agent") != "agent_b":
mismatches.append("RESUME_SOURCE_AGENT_MISMATCH")
if agents["agent_a"].status != "ACTIVE" or agents["agent_c"].status != "ACTIVE":
mismatches.append("NORMAL_AGENT_STATUS_CHANGED")
return {
"tool": "RCV",
"schema_version": "rcv-agent-incident-pel-v0.3.1",
"verified": not mismatches,
"decision": "RUN" if not mismatches else "PAUSE_FOR_HITL",
"mismatch_count": len(mismatches),
"mismatches": mismatches,
}
def write_json(path: Path, obj: Any) -> None:
path.write_text(json.dumps(obj, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
def run_simulation(seed: int, out_dir: Path) -> dict[str, Any]:
rng = random.Random(seed)
run_id = f"agent-incident-mediation-pel-user-maestro-v0.3.1-seed-{seed}"
out_dir.mkdir(parents=True, exist_ok=True)
arl = AuditLog(run_id)
checkpoints = CheckpointStore()
checkpoint = checkpoints.create_checkpoint(
run_id=run_id,
agent_id="agent_b",
task_id="review-task-001",
completed_step="input_normalized",
next_step="safe_review_finalize",
)
agents = {
"agent_a": Agent("agent_a", "safety_review", ["SAFE_REVIEW"], "SAFE_REVIEW"),
"agent_b": Agent(
"agent_b",
"efficiency_review",
["SAFE_REVIEW"],
"UNDECLARED_EXTERNAL_EFFECT_ATTEMPT_SIMULATED",
downstream_dependents=["agent_c", "orchestrator_dispatch"],
),
"agent_c": Agent("agent_c", "readability_review", ["SAFE_REVIEW"], "SAFE_REVIEW"),
"standby_1": Agent("standby_1", "standby_review", ["SAFE_REVIEW", "RESUME_FROM_CHECKPOINT"], "RESUME_FROM_CHECKPOINT", status="STANDBY"),
}
arl.append(
layer="orchestrator",
decision="RUN",
sealed=False,
overrideable=True,
final_decider="SYSTEM",
reason_code="ORCHESTRATION_STARTED",
message="Local simulation started.",
)
write_json(out_dir / "resume_checkpoint.json", checkpoint)
events = []
for agent_id in ("agent_a", "agent_b", "agent_c"):
agent = agents[agent_id]
event = agent.act(violate_contract=(agent.agent_id == "agent_b"), checkpoint_id=checkpoint["checkpoint_id"] if agent.agent_id == "agent_b" else None)
events.append(event)
arl.append(
layer="agent_execution",
decision="RUN",
sealed=False,
overrideable=True,
final_decider="SYSTEM",
reason_code="AGENT_EVENT_RECORDED",
message=f"Recorded simulated action for {agent.agent_id}.",
agent_id=agent.agent_id,
)
tasukeru_report = PseudoTasukeru().run(events, out_dir, checkpoint)
arl.append(
layer="tasukeru_audit",
decision=tasukeru_report["decision"],
sealed=False,
overrideable=True,
final_decider="SYSTEM",
reason_code="TASUKERU_USER_MAESTRO_ESCALATION_PACKET_CREATED",
message=f"Tasukeru finding_count={len(tasukeru_report['findings'])}.",
)
for entry in tasukeru_report["probabilistic_escalation_report"]["entries"]:
arl.append(
layer="probabilistic_escalation_layer",
decision="PAUSE_FOR_HITL" if entry["p_escalate"] >= entry["threshold"] else "RUN",
sealed=False,
overrideable=True,
final_decider="USER",
reason_code="PEL_ESCALATED_TO_USER_MAESTRO" if entry["p_escalate"] >= entry["threshold"] else "PEL_MONITOR_ONLY",
message=f"PEL p_escalate={entry['p_escalate']} threshold={entry['threshold']}.",
agent_id=entry["agent_id"],
)
gateway = UserMaestroGateway(
human=HumanMaestro(rng),
control=ContainmentAndHandoffController(),
arl=arl,
)
user_maestro_results, resumed_events = gateway.handle_escalation(tasukeru_report["escalation_packet"], agents, checkpoint)
final_decision = "RUN" if resumed_events else "STOPPED"
arl.append(
layer="orchestrator",
decision=final_decision,
sealed=False,
overrideable=False,
final_decider="SYSTEM_AFTER_USER_AUTH" if user_maestro_results else "SYSTEM",
reason_code="ORCHESTRATION_RESUMED_BY_STANDBY" if resumed_events else "ORCHESTRATION_STOPPED_BY_USER_SEAL",
message="Simulation completed.",
)
arl_verify = arl.verify()
rcv = ResultConsistencyVerifier().verify(
events=events,
tasukeru_report=tasukeru_report,
user_maestro_results=user_maestro_results,
resumed_events=resumed_events,
agents=agents,
checkpoint=checkpoint,
arl_verify=arl_verify,
)
agents_after = [asdict(agent) for agent in agents.values()]
user_instruction_record = {
"schema_version": "user-maestro-instruction-record-v0.3.1",
"records": [item["instruction_record"] for item in user_maestro_results],
"maestro_self_decision": False,
"human_decision_required_for_execution": True,
}
handoff_resume_result = {