Skip to content

Add skills code review agent example#140

Open
zzp1221 wants to merge 2 commits into
trpc-group:mainfrom
zzp1221:feat/issue-92-code-review-agent
Open

Add skills code review agent example#140
zzp1221 wants to merge 2 commits into
trpc-group:mainfrom
zzp1221:feat/issue-92-code-review-agent

Conversation

@zzp1221

@zzp1221 zzp1221 commented Jul 8, 2026

Copy link
Copy Markdown

概要

  • 新增 skills_code_review_agent 示例,并保持与 quickstart 一致的目录结构。
  • 实现基于 Skill 的自动代码评审流程,包含沙箱执行、Filter 治理、SQLite 持久化、敏感信息脱敏、finding 去重和结构化报
    告。
  • 新增 8 条公开 diff 样例和验收测试,覆盖报告生成、数据库存储、沙箱失败、超时与输出限制、高风险脚本拦截、敏感信息脱敏
    等场景。

主要内容

  • examples/skills_code_review_agent/run_agent.py
  • examples/skills_code_review_agent/agent/
  • examples/skills_code_review_agent/skills/code-review/
  • examples/skills_code_review_agent/schema.sql
  • examples/skills_code_review_agent/fixtures/
  • examples/skills_code_review_agent/sample_outputs/
  • tests/examples/test_skills_code_review_agent.py

测试

.venv\Scripts\python.exe -m pytest tests\examples\test_skills_code_review_agent.py
py -3.13 -m compileall examples\skills_code_review_agent tests\examples\test_skills_code_review_agent.py

结果:

- 15 passed
- compileall passed

@codecov

codecov Bot commented Jul 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@cbb6979). Learn more about missing BASE report.

Additional details and impacted files
@@            Coverage Diff             @@
##             main        #140   +/-   ##
==========================================
  Coverage        ?   87.51591%           
==========================================
  Files           ?         467           
  Lines           ?       44008           
  Branches        ?           0           
==========================================
  Hits            ?       38514           
  Misses          ?        5494           
  Partials        ?           0           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@helloopenworld

Copy link
Copy Markdown
Contributor

AI Code Review

我现在已经完全理解了。让我来整理一下我的发现。

让我重新考虑一下严重程度。_truncatereplace 问题确实存在,但这是示例代码。根据规则 10,示例/测试代码应按真实执行风险进行评估。CLI 路径 (run_review) 通过 WorkspaceSandboxRunner(容器/立方体)或 SandboxRunner(本地)使用 _truncate。对于容器/立方体运行时,WorkspaceSandboxRunner._truncate 是实际用于绑定沙箱 stdout/stderr/artifacts 的实现。因此,它确实在真实执行路径上运行。字节溢出意味着 max_output_bytes 无法严格强制执行——输出可能会略微超出。这是一个稳定性/正确性方面的警告,而非安全性(输出仍然有限,只是大约 8KB 的余量加上标记大小,而不是无限)。考虑到脱敏已经应用,我会将其标记为警告。

让我再考虑一下:还有一个问题——工作空间 _truncate 在切片上追加 "\n[output truncated]",而没有将其计入限制内,因此 result.stdout 的长度可能为 limit + len(marker)。这确实超出了 max_output_bytes。同样的模式。我会将它们合并为一个警告。

让我再思考一下,这是否属于关键问题——不,输出仍然有限(不是无界的),只是超出了声明的上限几个字节。属于警告。

现在让我写下审查意见。我还想再次确认 container_ws_runtime 中是否存在一个真正的问题:在 _create_tar_from_files 中,成员名称现在使用了 _container_relative_path(f.path),它会拒绝绝对路径和 ..。但原始代码使用的是 f.path 直接作为 tar 成员名称。如果调用者传入像 subdir/file.py 这样的相对路径,这可以通过。如果传入绝对路径,现在会引发异常。这是一项破坏性变更——之前传入绝对路径的调用者现在会收到 ValueError。但这是一种强化,破坏性变更仅针对无效/危险输入。可以接受。不标记为问题。

再考虑一个:_container_path(ws.path) 在 glob 的 cd 中使用——但 _container_path 不验证 ..。然而 ws.path 来自 create_workspace,是受控的。没问题。

让我完成最终定稿。

发现的问题

🚨 Critical

无。

⚠️ Warning

  • examples/skills_code_review_agent/agent/workspace_sandbox.py:799_truncate 截断后用 errors="replace" 解码并额外追加 marker,违反字节上限
    • 该方法紧邻的注释(附近行 792-794)自己说明 replace 会把一个残缺字节膨胀成 3 字节替换符,使“本应有界的可见前缀超出字节上限”,但实现仍用 replace,且 "\n[output truncated]" 是在切片之外追加的,未计入预算。同仓 sandbox.py:489bounded_runtime._slice_utf8 都用 errors="ignore" 且为 marker 预留空间,唯独此处不一致。WorkspaceSandboxRunner 是 container/cube 真实执行路径上绑定 stdout/stderr/产物 的实现,导致 max_output_bytes 上限可被超出(超出量约为 marker 长度+若干替换符字节)。建议与 bounded_runtime._redact_and_bound 对齐:先 redact,再在 limit - len(marker) 内切片,用 errors="ignore" 解码,最后追加 marker。

💡 Suggestion

  • trpc_agent_sdk/code_executors/container/_container_ws_runtime.py:604-610_stage_host_input/_stage_workspace_input 仍把 container_srcdstparent 直接单引号插值进 bash -lc,未走 _shell_quote
    • 新增的 _container_relative_path 已拒绝 .. 与绝对路径,显著收敛了路径穿越风险,但路径中若含单引号仍可逃出单引号字面量。该写法在本 PR 之前就存在且非新引入缺陷,属可选加固:对这几处插值改用 _shell_quote 与现有 mkdir -p {_shell_quote(...)} 风格保持一致,可彻底闭合命令注入面。

总结

整体为一个较成熟的代码评审 Agent 示例与配套 SDK 容器路径加固,沙箱隔离、脱敏、过滤、资源清理设计完整,未发现阻塞性安全或逻辑错误。唯一实质问题是 WorkspaceSandboxRunner._truncate 的字节边界实现与同仓其他两处不一致且违反自身注释,建议修复以守住 max_output_bytes 契约。

测试建议

  • 暂无额外测试建议。现有 test_skills_code_review_agent_workspace_sandbox.py 已覆盖捕获协议;可酌情补一条用例:构造单字节位于多字节 UTF-8 字符中间的输出,断言 WorkspaceSandboxRunner 截断后 stdout 字节数不超过 max_output_bytes(当前实现会超)。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants