fischer-agentkit/tests/unit/tools
chiguyong 2932ee51ed feat(tools): add str_replace_editor tool with workspace-root security (U1, R1)
Replaces the broken write_file placeholder (no real implementation, only
_FakeTool stubs in cli/benchmark.py) with a structured editor offering four
commands: create, str_replace, insert_at_line, view.

Security model (file-system analog of the 6-layer terminal security paradigm,
reject-by-default + prefix match):
  1. Reject absolute paths (force relative interpretation vs workspace root).
  2. Reject any .. path component (path traversal).
  3. Path.resolve() follows symlinks, then relative_to(workspace_root)
     rejects symlink escape and residual traversal.

Data-loss guard: create refuses to overwrite existing files. str_replace
requires a unique anchor (0 or >1 matches error). insert_at_line is 1-based
(0 = prepend, > EOF = append). All FS I/O wrapped in asyncio.to_thread.

Registers str_replace_editor in _DEFAULT_CORE_TOOLS (replacing write_file)
so its full description is always injected into the LLM prompt. Updates
test_tool_search.py which used write_file as a sample core tool.

Tests: 34 cases in test_str_replace_editor.py cover happy path, edge cases
(empty file, multi-match, insert at 0/beyond EOF, view range), error paths
(overwrite refusal, anchor not found, path traversal, absolute path, symlink
escape, unknown command, missing args), and integration contract (in
_DEFAULT_CORE_TOOLS, exported from agentkit.tools, schema enum, prompt
injection via _build_tool_use_prompt).

Verification: ruff check clean; targeted regression suite 412 passed
(the single failure in test_calendar_tool.py is a pre-existing date-sensitive
bug in an untouched file, today 2026-07-03 Friday makes the next-Wednesday
assertion fail).
2026-07-03 11:42:59 +08:00
..
__init__.py fix: resolve code review issues from deferred improvements 2026-06-11 13:49:02 +08:00
test_calendar_tool.py fix(calendar): 修复 agent 创建日历事件后 UI 不刷新 + 文档化三根因三部曲 2026-06-29 02:20:33 +08:00
test_computer_use.py feat(phase4): implement Computer Use integration (U12) 2026-06-10 00:54:31 +08:00
test_pty_session.py fix(security): resolve all P0/P1 findings from code review 2026-06-10 07:12:41 +08:00
test_terminal_session.py feat(phase2): implement self-evolution and smart terminal (U6-U8) 2026-06-10 00:22:36 +08:00
test_tool_search.py feat(tools): add str_replace_editor tool with workspace-root security (U1, R1) 2026-07-03 11:42:59 +08:00