Skip to content

Commit 05ac751

Browse files
committed
fix(safety): 提交测序策略文件,修复代码规范问题
1 parent 3f8d650 commit 05ac751

8 files changed

Lines changed: 83 additions & 42 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Sample policy for integration testing
2+
# This adds extra allowed domains and commands beyond the built-in defaults.
3+
version: "1.0"
4+
5+
network:
6+
allowed_domains:
7+
- "*.example.com"
8+
- "custom-api.mycompany.io"
9+
override: false # Append to defaults
10+
11+
process:
12+
allowed_commands:
13+
- "docker"
14+
- "go"
15+
- "cargo"
16+
override: false
17+
18+
file_operations:
19+
forbidden_paths:
20+
- "/tmp/sensitive/"
21+
- "~/secrets/"
22+
override: false
23+
24+
resources:
25+
max_timeout_seconds: 600
26+
max_output_size_mb: 200
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Strict policy for integration testing — very restrictive
2+
# Override mode: fully replace default lists with minimal allowlists
3+
version: "1.0"
4+
5+
network:
6+
allowed_domains:
7+
- "pypi.org"
8+
override: true # Only pypi.org allowed, all others blocked
9+
10+
process:
11+
allowed_commands:
12+
- "echo"
13+
- "cat"
14+
override: true # Only echo and cat allowed
15+
16+
file_operations:
17+
forbidden_paths:
18+
- "/etc/"
19+
- "/root/"
20+
- "~/.ssh/"
21+
- "~/.aws/"
22+
- "~/.gnupg/"
23+
- "~/.config/"
24+
- "/var/"
25+
- "/tmp/"
26+
override: true # Strictest set
27+
28+
resources:
29+
max_timeout_seconds: 60
30+
max_output_size_mb: 10

tests/tools/safety/test_guard.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Unit tests for ScriptSafetyGuard — the core coordination engine."""
22

33
import json
4-
import logging
54
from unittest.mock import patch
65

76
import pytest
@@ -257,19 +256,16 @@ def test_multiple_deny_still_deny(self):
257256
class TestAuditLog:
258257
"""Test that audit logging is emitted correctly."""
259258

260-
def test_audit_log_emitted_on_check(self, caplog):
259+
def test_audit_log_emitted_on_check(self):
261260
"""Verify audit log is produced with expected structure."""
262261
guard = ScriptSafetyGuard()
263262
input = _make_input(script="x = 1")
264263

265-
with caplog.at_level(logging.INFO, logger="trpc_agent_sdk.tools.safety.audit"):
264+
with patch("trpc_agent_sdk.tools.safety.guard._audit_logger") as mock_audit:
266265
guard.check(input)
267266

268-
# Find the audit log record
269-
audit_records = [r for r in caplog.records if r.name == "trpc_agent_sdk.tools.safety.audit"]
270-
assert len(audit_records) == 1
271-
272-
entry = json.loads(audit_records[0].message)
267+
mock_audit.info.assert_called_once()
268+
entry = json.loads(mock_audit.info.call_args[0][0])
273269
assert entry["event"] == "safety_check"
274270
assert entry["decision"] == "allow"
275271
assert entry["language"] == "python"
@@ -279,18 +275,16 @@ def test_audit_log_emitted_on_check(self, caplog):
279275
assert "findings_count" in entry
280276
assert "script_length" in entry
281277

282-
def test_audit_log_contains_findings_summary(self, caplog):
278+
def test_audit_log_contains_findings_summary(self):
283279
"""Audit log findings are present with desensitized evidence."""
284280
guard = ScriptSafetyGuard()
285281
input = _make_input(script="import os\nos.system('rm -rf /')")
286282

287-
with caplog.at_level(logging.INFO, logger="trpc_agent_sdk.tools.safety.audit"):
283+
with patch("trpc_agent_sdk.tools.safety.guard._audit_logger") as mock_audit:
288284
guard.check(input)
289285

290-
audit_records = [r for r in caplog.records if r.name == "trpc_agent_sdk.tools.safety.audit"]
291-
assert len(audit_records) == 1
292-
293-
entry = json.loads(audit_records[0].message)
286+
mock_audit.info.assert_called_once()
287+
entry = json.loads(mock_audit.info.call_args[0][0])
294288
assert entry["findings_count"] > 0
295289
assert len(entry["findings"]) > 0
296290
# Each finding has expected fields

tests/tools/safety/test_guard_extended.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,8 @@ def test_audit_write_failure_does_not_raise(self):
206206
class TestEmitAuditLogExtended:
207207
"""Extended tests for the Python logger audit log."""
208208

209-
def test_audit_log_with_findings(self, caplog):
209+
def test_audit_log_with_findings(self):
210210
"""Audit log includes sanitized findings."""
211-
import logging
212-
213211
input_data = _make_input(script="api_key = 'sk-secret1234567890abcdefgh'")
214212
finding = Finding(
215213
rule_id="SEC-001",
@@ -229,31 +227,28 @@ def test_audit_log_with_findings(self, caplog):
229227
invocation_id="inv-x",
230228
)
231229

232-
with caplog.at_level(logging.INFO, logger="trpc_agent_sdk.tools.safety.audit"):
230+
with patch("trpc_agent_sdk.tools.safety.guard._audit_logger") as mock_audit:
233231
_emit_audit_log(input_data, result)
234232

235-
audit_records = [r for r in caplog.records if r.name == "trpc_agent_sdk.tools.safety.audit"]
236-
assert len(audit_records) == 1
237-
entry = json.loads(audit_records[0].message)
233+
mock_audit.info.assert_called_once()
234+
entry = json.loads(mock_audit.info.call_args[0][0])
238235
assert entry["decision"] == "deny"
239236
assert entry["findings_count"] == 1
240237
# Evidence should be sanitized in audit log
241238
f = entry["findings"][0]
242239
assert "****" in f["evidence"]
243240
assert f["line_number"] == 1
244241

245-
def test_audit_log_metadata_fields(self, caplog):
242+
def test_audit_log_metadata_fields(self):
246243
"""Audit log contains agent_name, user_id, script_length."""
247-
import logging
248-
249244
input_data = _make_input(script="x = 42")
250245
result = _make_result()
251246

252-
with caplog.at_level(logging.INFO, logger="trpc_agent_sdk.tools.safety.audit"):
247+
with patch("trpc_agent_sdk.tools.safety.guard._audit_logger") as mock_audit:
253248
_emit_audit_log(input_data, result)
254249

255-
audit_records = [r for r in caplog.records if r.name == "trpc_agent_sdk.tools.safety.audit"]
256-
entry = json.loads(audit_records[0].message)
250+
mock_audit.info.assert_called_once()
251+
entry = json.loads(mock_audit.info.call_args[0][0])
257252
assert entry["agent_name"] == "test_agent"
258253
assert entry["user_id"] == "user-001"
259254
assert entry["script_length"] == len("x = 42")

tests/tools/safety/test_integration.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
from __future__ import annotations
1818

19-
import logging
2019
from pathlib import Path
2120
from unittest.mock import AsyncMock, MagicMock, patch
2221

@@ -382,26 +381,24 @@ def test_hardcoded_secret_bash(self, default_guard: ScriptSafetyGuard):
382381
class TestAuditAndTelemetry:
383382
"""Verify audit logging and OTel recording fires correctly."""
384383

385-
def test_audit_log_emitted(self, default_guard: ScriptSafetyGuard, caplog):
384+
def test_audit_log_emitted(self, default_guard: ScriptSafetyGuard):
386385
"""Guard should emit structured audit log on every check."""
387386
code = 'print("hello")'
388-
with caplog.at_level(logging.INFO, logger="trpc_agent_sdk.tools.safety.audit"):
387+
with patch("trpc_agent_sdk.tools.safety.guard._audit_logger") as mock_audit:
389388
default_guard.check(_make_input(code))
390389

391-
# Verify audit log was emitted
392-
audit_records = [r for r in caplog.records if r.name == "trpc_agent_sdk.tools.safety.audit"]
393-
assert len(audit_records) == 1
394-
assert "safety_check" in audit_records[0].getMessage()
390+
mock_audit.info.assert_called_once()
391+
msg = mock_audit.info.call_args[0][0]
392+
assert "safety_check" in msg
395393

396-
def test_audit_log_contains_decision(self, default_guard: ScriptSafetyGuard, caplog):
394+
def test_audit_log_contains_decision(self, default_guard: ScriptSafetyGuard):
397395
"""Audit log should contain decision and findings info."""
398396
code = 'result = eval("1+1")'
399-
with caplog.at_level(logging.INFO, logger="trpc_agent_sdk.tools.safety.audit"):
397+
with patch("trpc_agent_sdk.tools.safety.guard._audit_logger") as mock_audit:
400398
default_guard.check(_make_input(code))
401399

402-
audit_records = [r for r in caplog.records if r.name == "trpc_agent_sdk.tools.safety.audit"]
403-
assert len(audit_records) == 1
404-
msg = audit_records[0].getMessage()
400+
mock_audit.info.assert_called_once()
401+
msg = mock_audit.info.call_args[0][0]
405402
assert '"decision": "deny"' in msg
406403
assert '"PROC-002"' in msg
407404

trpc_agent_sdk/tools/safety/adapters/filter_adapter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import logging
1919
from typing import Any, Optional
2020

21-
from trpc_agent_sdk.abc import FilterResult, FilterType
21+
from trpc_agent_sdk.abc import FilterResult
2222
from trpc_agent_sdk.context import AgentContext
2323
from trpc_agent_sdk.filter import BaseFilter, register_tool_filter
2424
from trpc_agent_sdk.tools.safety.guard import ScriptSafetyGuard

trpc_agent_sdk/tools/safety/rules/file_ops.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
from __future__ import annotations
99

10-
import ast
1110
import fnmatch
1211
import os
1312
from typing import TYPE_CHECKING

trpc_agent_sdk/tools/safety/scanner/bash_scanner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from __future__ import annotations
88

99
import re
10-
from dataclasses import dataclass, field
10+
from dataclasses import dataclass
1111
from typing import Optional
1212

1313

0 commit comments

Comments
 (0)