Skip to content

Commit 97770e0

Browse files
committed
move to tools
1 parent 8ba001b commit 97770e0

55 files changed

Lines changed: 212 additions & 261 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/mkdocs/en/tool_safety_guard.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ permissions, and runtime resource limits.
6262

6363
```bash
6464
python scripts/tool_safety_check.py \
65-
--policy tool/safety/examples/tool_safety_policy.yaml \
65+
--policy trpc_agent_sdk/tools/safety/examples/tool_safety_policy.yaml \
6666
--language python \
67-
--script-file tool/safety/examples/samples/03_dangerous_delete.py \
67+
--script-file trpc_agent_sdk/tools/safety/examples/samples/03_dangerous_delete.py \
6868
--output tool_safety_report.json \
6969
--audit-file tool_safety_audit.jsonl
7070
echo $? # 0=allow, 2=deny, 3=review, 4=input/policy error
@@ -74,23 +74,23 @@ Run the manifest to scan all 14 public samples:
7474

7575
```bash
7676
python scripts/tool_safety_check.py \
77-
--policy tool/safety/examples/tool_safety_policy.yaml \
78-
--manifest tool/safety/examples/samples/manifest.yaml \
79-
--manifest-output tool/safety/examples/manifest_run.json \
80-
--audit-file tool/safety/examples/tool_safety_audit.jsonl
77+
--policy trpc_agent_sdk/tools/safety/examples/tool_safety_policy.yaml \
78+
--manifest trpc_agent_sdk/tools/safety/examples/samples/manifest.yaml \
79+
--manifest-output trpc_agent_sdk/tools/safety/examples/manifest_run.json \
80+
--audit-file trpc_agent_sdk/tools/safety/examples/tool_safety_audit.jsonl
8181
```
8282

8383
## Programmatic usage
8484

8585
```python
86-
from tool.safety import (
86+
from trpc_agent_sdk.tools.safety import (
8787
ToolSafetyGuard,
8888
load_safety_policy,
8989
SafetyScanRequest,
9090
ScriptLanguage,
9191
)
9292

93-
policy = load_safety_policy("tool/safety/examples/tool_safety_policy.yaml")
93+
policy = load_safety_policy("trpc_agent_sdk/tools/safety/examples/tool_safety_policy.yaml")
9494
guard = ToolSafetyGuard(policy)
9595

9696
request = SafetyScanRequest(
@@ -108,8 +108,8 @@ print(report.decision, report.rule_ids)
108108

109109
```python
110110
import subprocess
111-
from tool.wrapper import SafetyWrappedCallable
112-
from tool.safety import ToolSafetyGuard, load_safety_policy, ScriptLanguage
111+
from trpc_agent_sdk.tools.safety import SafetyWrappedCallable
112+
from trpc_agent_sdk.tools.safety import ToolSafetyGuard, load_safety_policy, ScriptLanguage
113113

114114
guard = ToolSafetyGuard(load_safety_policy("policy.yaml"))
115115
safe_run = SafetyWrappedCallable(
@@ -131,8 +131,8 @@ names so the normalized request contains every available execution field.
131131
### Wrapping a code executor
132132

133133
```python
134-
from tool.wrapper import SafetyCheckedExecutor
135-
from tool.safety import ToolSafetyGuard, load_safety_policy, ScriptLanguage
134+
from trpc_agent_sdk.tools.safety import SafetyCheckedExecutor
135+
from trpc_agent_sdk.tools.safety import ToolSafetyGuard, load_safety_policy, ScriptLanguage
136136

137137
guard = ToolSafetyGuard(load_safety_policy("policy.yaml"))
138138
safe_executor = SafetyCheckedExecutor(
@@ -255,13 +255,13 @@ When OpenTelemetry is active the guard sets these low-cardinality span
255255
attributes on the current span:
256256
257257
```text
258-
tool.safety.decision
259-
tool.safety.risk_level
260-
tool.safety.rule_id # comma-separated, bounded to 8 entries
261-
tool.safety.blocked
262-
tool.safety.redacted
263-
tool.safety.scan_duration_ms
264-
tool.safety.policy_hash
258+
trpc_agent_sdk.tools.safety.decision
259+
trpc_agent_sdk.tools.safety.risk_level
260+
trpc_agent_sdk.tools.safety.rule_id # comma-separated, bounded to 8 entries
261+
trpc_agent_sdk.tools.safety.blocked
262+
trpc_agent_sdk.tools.safety.redacted
263+
trpc_agent_sdk.tools.safety.scan_duration_ms
264+
trpc_agent_sdk.tools.safety.policy_hash
265265
```
266266

267267
Metrics emitted (no-op when OTel is absent):
@@ -302,7 +302,7 @@ all argument-mutating callbacks. The wrapper remains mandatory until then.
302302
Implement :class:`SafetyRule` and pass the rule list explicitly:
303303

304304
```python
305-
from tool.safety import ToolSafetyGuard, SafetyScanRequest
305+
from trpc_agent_sdk.tools.safety import ToolSafetyGuard, SafetyScanRequest
306306

307307
class MyRule:
308308
rule_id = "CUSTOM001_MY_RULE"
@@ -397,7 +397,7 @@ tool/
397397
scripts/
398398
tool_safety_check.py # CLI
399399
tests/tool_safety/ # safety guard tests
400-
tool/safety/examples/
400+
trpc_agent_sdk/tools/safety/examples/
401401
tool_safety_policy.yaml # sample policy
402402
samples/ # 14 public samples + manifest
403403
tool_safety_report.json # generated report

docs/mkdocs/zh/tool_safety_guard.zh_CN.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ span 属性/指标输出。
5353

5454
```bash
5555
python scripts/tool_safety_check.py \
56-
--policy tool/safety/examples/tool_safety_policy.yaml \
56+
--policy trpc_agent_sdk/tools/safety/examples/tool_safety_policy.yaml \
5757
--language python \
58-
--script-file tool/safety/examples/samples/03_dangerous_delete.py \
58+
--script-file trpc_agent_sdk/tools/safety/examples/samples/03_dangerous_delete.py \
5959
--output tool_safety_report.json \
6060
--audit-file tool_safety_audit.jsonl
6161
echo $? # 0=allow, 2=deny, 3=review, 4=输入/策略/必需审计错误
@@ -65,23 +65,23 @@ echo $? # 0=allow, 2=deny, 3=review, 4=输入/策略/必需审计错误
6565

6666
```bash
6767
python scripts/tool_safety_check.py \
68-
--policy tool/safety/examples/tool_safety_policy.yaml \
69-
--manifest tool/safety/examples/samples/manifest.yaml \
70-
--manifest-output tool/safety/examples/manifest_run.json \
71-
--audit-file tool/safety/examples/tool_safety_audit.jsonl
68+
--policy trpc_agent_sdk/tools/safety/examples/tool_safety_policy.yaml \
69+
--manifest trpc_agent_sdk/tools/safety/examples/samples/manifest.yaml \
70+
--manifest-output trpc_agent_sdk/tools/safety/examples/manifest_run.json \
71+
--audit-file trpc_agent_sdk/tools/safety/examples/tool_safety_audit.jsonl
7272
```
7373

7474
## 作为库使用
7575

7676
```python
77-
from tool.safety import (
77+
from trpc_agent_sdk.tools.safety import (
7878
ToolSafetyGuard,
7979
load_safety_policy,
8080
SafetyScanRequest,
8181
ScriptLanguage,
8282
)
8383

84-
policy = load_safety_policy("tool/safety/examples/tool_safety_policy.yaml")
84+
policy = load_safety_policy("trpc_agent_sdk/tools/safety/examples/tool_safety_policy.yaml")
8585
guard = ToolSafetyGuard(policy)
8686

8787
request = SafetyScanRequest(
@@ -99,8 +99,8 @@ print(report.decision, report.rule_ids)
9999

100100
```python
101101
import subprocess
102-
from tool.wrapper import SafetyWrappedCallable
103-
from tool.safety import ToolSafetyGuard, load_safety_policy, ScriptLanguage
102+
from trpc_agent_sdk.tools.safety import SafetyWrappedCallable
103+
from trpc_agent_sdk.tools.safety import ToolSafetyGuard, load_safety_policy, ScriptLanguage
104104

105105
guard = ToolSafetyGuard(load_safety_policy("policy.yaml"))
106106
safe_run = SafetyWrappedCallable(
@@ -121,8 +121,8 @@ safe_run("ls -la") # 策略拒绝时抛出 BlockedExecutionError
121121
### 包装 CodeExecutor
122122

123123
```python
124-
from tool.wrapper import SafetyCheckedExecutor
125-
from tool.safety import ToolSafetyGuard, load_safety_policy, ScriptLanguage
124+
from trpc_agent_sdk.tools.safety import SafetyCheckedExecutor
125+
from trpc_agent_sdk.tools.safety import ToolSafetyGuard, load_safety_policy, ScriptLanguage
126126

127127
guard = ToolSafetyGuard(load_safety_policy("policy.yaml"))
128128
safe_executor = SafetyCheckedExecutor(
@@ -249,13 +249,13 @@ audit:
249249
启用 OpenTelemetry 后,Guard 在当前 span 上写入低基数属性:
250250
251251
```text
252-
tool.safety.decision
253-
tool.safety.risk_level
254-
tool.safety.rule_id # 逗号分隔,最多 8 项
255-
tool.safety.blocked
256-
tool.safety.redacted
257-
tool.safety.scan_duration_ms
258-
tool.safety.policy_hash
252+
trpc_agent_sdk.tools.safety.decision
253+
trpc_agent_sdk.tools.safety.risk_level
254+
trpc_agent_sdk.tools.safety.rule_id # 逗号分隔,最多 8 项
255+
trpc_agent_sdk.tools.safety.blocked
256+
trpc_agent_sdk.tools.safety.redacted
257+
trpc_agent_sdk.tools.safety.scan_duration_ms
258+
trpc_agent_sdk.tools.safety.policy_hash
259259
```
260260

261261
OTel 不存在时指标会安全地 no-op。可用指标:
@@ -295,7 +295,7 @@ TOCTOU 绕过。因此目前必须使用 ``SafetyWrappedCallable`` 或
295295
实现 ``SafetyRule`` 并显式传入规则列表:
296296

297297
```python
298-
from tool.safety import ToolSafetyGuard, SafetyScanRequest
298+
from trpc_agent_sdk.tools.safety import ToolSafetyGuard, SafetyScanRequest
299299

300300
class MyRule:
301301
rule_id = "CUSTOM001_MY_RULE"
@@ -351,7 +351,7 @@ tool/
351351
scripts/
352352
tool_safety_check.py # CLI
353353
tests/tool_safety/ # 安全检查器测试
354-
tool/safety/examples/ # 策略、14 个样例、报告和审计样例
354+
trpc_agent_sdk/tools/safety/examples/ # 策略、14 个样例、报告和审计样例
355355
docs/
356356
tool_safety_guard.md # English version
357357
tool_safety_guard.zh_CN.md # 本文

scripts/tool_safety_check.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Usage::
44
55
python scripts/tool_safety_check.py \\
6-
--policy tool/safety/examples/tool_safety_policy.yaml \\
6+
--policy trpc_agent_sdk/tools/safety/examples/tool_safety_policy.yaml \\
77
--language python \\
88
--script-file path/to/script.py \\
99
--tool-name demo
@@ -30,24 +30,24 @@
3030

3131
import yaml
3232

33-
# Ensure the repo root is on sys.path so ``import tool.safety`` works
33+
# Ensure the repo root is on sys.path so ``import trpc_agent_sdk.tools.safety`` works
3434
# when the CLI is invoked directly via ``python scripts/...``.
3535
_REPO_ROOT = Path(__file__).resolve().parents[1]
3636
if str(_REPO_ROOT) not in sys.path:
3737
sys.path.insert(0, str(_REPO_ROOT))
3838

39-
from tool.safety._audit import InMemoryAuditSink, JsonlAuditSink # noqa: E402
40-
from tool.safety._exceptions import SafetyAuditError # noqa: E402
41-
from tool.safety._guard import ToolSafetyGuard # noqa: E402
42-
from tool.safety._models import ( # noqa: E402
39+
from trpc_agent_sdk.tools.safety._audit import InMemoryAuditSink, JsonlAuditSink # noqa: E402
40+
from trpc_agent_sdk.tools.safety._exceptions import SafetyAuditError # noqa: E402
41+
from trpc_agent_sdk.tools.safety._guard import ToolSafetyGuard # noqa: E402
42+
from trpc_agent_sdk.tools.safety._models import ( # noqa: E402
4343
SafetyDecision,
4444
SafetyReport,
4545
SafetyScanRequest,
4646
ScriptLanguage,
4747
ToolKind,
4848
)
49-
from tool.safety._policy import load_safety_policy # noqa: E402
50-
from tool.safety._telemetry import build_audit_event # noqa: E402
49+
from trpc_agent_sdk.tools.safety._policy import load_safety_policy # noqa: E402
50+
from trpc_agent_sdk.tools.safety._telemetry import build_audit_event # noqa: E402
5151

5252

5353
def main(argv: Sequence[str] | None = None) -> int:

tests/tool_safety/conftest.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66

77
import pytest
88

9-
from tool.safety._guard import ToolSafetyGuard
10-
from tool.safety._policy import load_safety_policy
9+
from trpc_agent_sdk.tools.safety._guard import ToolSafetyGuard
10+
from trpc_agent_sdk.tools.safety._policy import load_safety_policy
1111

1212

1313
REPO_ROOT = Path(__file__).resolve().parents[2]
14-
EXAMPLE_POLICY = REPO_ROOT / "examples" / "tool_safety" / "tool_safety_policy.yaml"
14+
EXAMPLE_POLICY = (
15+
REPO_ROOT / "trpc_agent_sdk" / "tools" / "safety" / "examples"
16+
/ "tool_safety_policy.yaml"
17+
)
1518

1619

1720
@pytest.fixture

tests/tool_safety/test_audit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
import pytest
1111

12-
from tool.safety._audit import InMemoryAuditSink, JsonlAuditSink
13-
from tool.safety._models import (
12+
from trpc_agent_sdk.tools.safety._audit import InMemoryAuditSink, JsonlAuditSink
13+
from trpc_agent_sdk.tools.safety._models import (
1414
RiskLevel,
1515
SafetyAuditEvent,
1616
SafetyDecision,

tests/tool_safety/test_bash_scanner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
import pytest
66

7-
from tool.safety._guard import ToolSafetyGuard
8-
from tool.safety._models import SafetyDecision, SafetyScanRequest, ScriptLanguage
9-
from tool.safety._policy import load_safety_policy_dict
7+
from trpc_agent_sdk.tools.safety._guard import ToolSafetyGuard
8+
from trpc_agent_sdk.tools.safety._models import SafetyDecision, SafetyScanRequest, ScriptLanguage
9+
from trpc_agent_sdk.tools.safety._policy import load_safety_policy_dict
1010

1111

1212
@pytest.fixture

tests/tool_safety/test_cli.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

1212
REPO_ROOT = Path(__file__).resolve().parents[2]
1313
CLI = REPO_ROOT / "scripts" / "tool_safety_check.py"
14-
POLICY = REPO_ROOT / "tool" / "safety" / "examples" / "tool_safety_policy.yaml"
14+
POLICY = (
15+
REPO_ROOT / "trpc_agent_sdk" / "tools" / "safety" / "examples"
16+
/ "tool_safety_policy.yaml"
17+
)
1518

1619

1720
def _run_cli(*args: str) -> tuple[int, str, str]:
@@ -74,7 +77,10 @@ def test_required_audit_failure_exits_4(tmp_path):
7477

7578

7679
def test_manifest_writes_output(tmp_path):
77-
manifest = REPO_ROOT / "tool" / "safety" / "examples" / "samples" / "manifest.yaml"
80+
manifest = (
81+
REPO_ROOT / "trpc_agent_sdk" / "tools" / "safety" / "examples"
82+
/ "samples" / "manifest.yaml"
83+
)
7884
output = tmp_path / "out.json"
7985
audit = tmp_path / "audit.jsonl"
8086
cmd = [sys.executable, str(CLI), "--policy", str(POLICY),

tests/tool_safety/test_cross_field_scanner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
import pytest
66

7-
from tool.safety._guard import ToolSafetyGuard
8-
from tool.safety._models import SafetyDecision, SafetyScanRequest, ScriptLanguage
9-
from tool.safety._policy import load_safety_policy_dict
7+
from trpc_agent_sdk.tools.safety._guard import ToolSafetyGuard
8+
from trpc_agent_sdk.tools.safety._models import SafetyDecision, SafetyScanRequest, ScriptLanguage
9+
from trpc_agent_sdk.tools.safety._policy import load_safety_policy_dict
1010

1111

1212
@pytest.fixture

tests/tool_safety/test_filter.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
import pytest
99

10-
from tool.safety._audit import InMemoryAuditSink
11-
from tool.safety._filter import BlockedExecutionError, ToolScriptSafetyFilter
12-
from tool.safety._guard import ToolSafetyGuard
13-
from tool.safety._models import SafetyDecision, ToolKind
14-
from tool.safety._policy import load_safety_policy_dict
10+
from trpc_agent_sdk.tools.safety._audit import InMemoryAuditSink
11+
from trpc_agent_sdk.tools.safety._filter import BlockedExecutionError, ToolScriptSafetyFilter
12+
from trpc_agent_sdk.tools.safety._guard import ToolSafetyGuard
13+
from trpc_agent_sdk.tools.safety._models import SafetyDecision, ToolKind
14+
from trpc_agent_sdk.tools.safety._policy import load_safety_policy_dict
1515

1616

1717
@pytest.fixture

tests/tool_safety/test_guard.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
import pytest
66

7-
from tool.safety._guard import ToolSafetyGuard
8-
from tool.safety._models import SafetyDecision, SafetyScanRequest, ScriptLanguage
9-
from tool.safety._policy import load_safety_policy_dict
7+
from trpc_agent_sdk.tools.safety._guard import ToolSafetyGuard
8+
from trpc_agent_sdk.tools.safety._models import SafetyDecision, SafetyScanRequest, ScriptLanguage
9+
from trpc_agent_sdk.tools.safety._policy import load_safety_policy_dict
1010

1111

1212
def test_allow_path_uses_safe000(strict_policy_dict):
@@ -61,8 +61,8 @@ def test_policy_hash_propagates(strict_policy_dict):
6161

6262

6363
def test_duplicate_rule_ids_rejected(strict_policy_dict):
64-
from tool.safety._python_scanner import PythonScannerRule
65-
from tool.safety._exceptions import SafetyGuardError
64+
from trpc_agent_sdk.tools.safety._python_scanner import PythonScannerRule
65+
from trpc_agent_sdk.tools.safety._exceptions import SafetyGuardError
6666

6767
policy = load_safety_policy_dict(strict_policy_dict)
6868
with pytest.raises(SafetyGuardError):

0 commit comments

Comments
 (0)