2626from trpc_agent_sdk .tools .safety ._types import ScanInput
2727from trpc_agent_sdk .tools .safety ._types import ScriptType
2828
29-
3029# ── Fixtures ──────────────────────────────────────────────────────────────
3130
3231
3332@pytest .fixture (scope = "module" )
3433def policy () -> SafetyPolicy :
3534 """Load the default policy file."""
3635 policy_path = os .path .join (
37- os .path .dirname (os .path .dirname (os .path .dirname (os .path .dirname (
38- os .path .abspath (__file__ ))))),
39- "trpc_agent_sdk" , "tools" , "safety" , "tool_safety_policy.yaml" ,
36+ os .path .dirname (os .path .dirname (os .path .dirname (os .path .dirname (os .path .abspath (__file__ ))))),
37+ "trpc_agent_sdk" ,
38+ "tools" ,
39+ "safety" ,
40+ "tool_safety_policy.yaml" ,
4041 )
4142 return SafetyPolicy .from_file (policy_path )
4243
@@ -63,8 +64,7 @@ def test_safetyreport_properties(self):
6364 """SafetyReport properties should work correctly."""
6465 from trpc_agent_sdk .tools .safety ._types import RuleMatch
6566
66- r = RuleMatch ("R001" , RiskCategory .DANGEROUS_FILE_OPERATION ,
67- RiskLevel .CRITICAL , "rm -rf /" , 1 , "Remove" )
67+ r = RuleMatch ("R001" , RiskCategory .DANGEROUS_FILE_OPERATION , RiskLevel .CRITICAL , "rm -rf /" , 1 , "Remove" )
6868 report = SafetyReport (SafetyDecision .DENY , RiskLevel .CRITICAL , [r ])
6969 assert report .is_blocked is True
7070 assert report .is_allowed is False
@@ -75,10 +75,11 @@ def test_safetyreport_to_dict(self):
7575 """SafetyReport.to_dict() should produce correct JSON structure."""
7676 from trpc_agent_sdk .tools .safety ._types import RuleMatch
7777
78- r = RuleMatch ("R001" , RiskCategory .DANGEROUS_FILE_OPERATION ,
79- RiskLevel .CRITICAL , "rm -rf /" , 1 , "Remove" )
80- report = SafetyReport (SafetyDecision .DENY , RiskLevel .CRITICAL , [r ],
81- tool_name = "Bash" , script_type = ScriptType .BASH )
78+ r = RuleMatch ("R001" , RiskCategory .DANGEROUS_FILE_OPERATION , RiskLevel .CRITICAL , "rm -rf /" , 1 , "Remove" )
79+ report = SafetyReport (SafetyDecision .DENY ,
80+ RiskLevel .CRITICAL , [r ],
81+ tool_name = "Bash" ,
82+ script_type = ScriptType .BASH )
8283 d = report .to_dict ()
8384 assert d ["decision" ] == "DENY"
8485 assert d ["risk_level" ] == "CRITICAL"
@@ -92,8 +93,7 @@ def test_auditevent_otel_attributes(self):
9293 """AuditEvent should produce 6 OTel attributes."""
9394 from trpc_agent_sdk .tools .safety ._types import AuditEvent
9495
95- event = AuditEvent ("Bash" , "DENY" , "CRITICAL" , "R001" , 1.23 ,
96- False , True , "2026-07-20T00:00:00" )
96+ event = AuditEvent ("Bash" , "DENY" , "CRITICAL" , "R001" , 1.23 , False , True , "2026-07-20T00:00:00" )
9797 otel = event .to_otel_attributes ()
9898 assert len (otel ) == 6
9999 assert otel ["tool.safety.decision" ] == "DENY"
@@ -154,7 +154,7 @@ def test_bash_pipe_sensitive(self, scanner: SafetyScanner):
154154 """cat /etc/passwd | grep root should be detected."""
155155 report = scanner .scan (ScanInput ("cat /etc/passwd | grep root" , ScriptType .BASH ))
156156 # Should detect sensitive file read
157- sensitive = [m for m in report .matches if m .rule_id in ("R002" ,)]
157+ sensitive = [m for m in report .matches if m .rule_id in ("R002" , )]
158158 assert len (sensitive ) > 0 or report .decision == SafetyDecision .DENY
159159
160160 def test_fork_bomb_detected (self , scanner : SafetyScanner ):
@@ -283,8 +283,12 @@ def test_log_decision_creates_event(self):
283283 """AuditLogger.log_decision should create a valid event."""
284284 logger = AuditLogger ()
285285 event = logger .log_decision (
286- tool_name = "Bash" , decision = "DENY" , risk_level = "CRITICAL" ,
287- rule_id = "R001" , scan_duration_ms = 1.23 , blocked = True ,
286+ tool_name = "Bash" ,
287+ decision = "DENY" ,
288+ risk_level = "CRITICAL" ,
289+ rule_id = "R001" ,
290+ scan_duration_ms = 1.23 ,
291+ blocked = True ,
288292 )
289293 assert event .tool_name == "Bash"
290294 assert event .decision == "DENY"
@@ -293,8 +297,7 @@ def test_log_decision_creates_event(self):
293297 def test_audit_event_to_dict (self ):
294298 """AuditEvent.to_dict() should produce correct structure."""
295299 from trpc_agent_sdk .tools .safety ._types import AuditEvent
296- event = AuditEvent ("Bash" , "DENY" , "CRITICAL" , "R001" , 1.23 ,
297- False , True , "2026-07-20T00:00:00" )
300+ event = AuditEvent ("Bash" , "DENY" , "CRITICAL" , "R001" , 1.23 , False , True , "2026-07-20T00:00:00" )
298301 d = event .to_dict ()
299302 assert d ["tool_name" ] == "Bash"
300303 assert d ["decision" ] == "DENY"
@@ -304,12 +307,14 @@ def test_audit_event_to_dict(self):
304307 def test_audit_event_otel_attributes (self ):
305308 """AuditEvent should produce OTel-compatible attributes."""
306309 from trpc_agent_sdk .tools .safety ._types import AuditEvent
307- event = AuditEvent ("Bash" , "DENY" , "CRITICAL" , "R001" , 1.23 ,
308- False , True , "2026-07-20T00:00:00" )
310+ event = AuditEvent ("Bash" , "DENY" , "CRITICAL" , "R001" , 1.23 , False , True , "2026-07-20T00:00:00" )
309311 otel = event .to_otel_attributes ()
310312 expected_keys = {
311- "tool.safety.decision" , "tool.safety.risk_level" ,
312- "tool.safety.rule_id" , "tool.safety.blocked" ,
313- "tool.safety.masked" , "tool.safety.duration_ms" ,
313+ "tool.safety.decision" ,
314+ "tool.safety.risk_level" ,
315+ "tool.safety.rule_id" ,
316+ "tool.safety.blocked" ,
317+ "tool.safety.masked" ,
318+ "tool.safety.duration_ms" ,
314319 }
315- assert set (otel .keys ()) == expected_keys
320+ assert set (otel .keys ()) == expected_keys
0 commit comments