fix+test: code quality improvements + 87 unit tests

Fixes:
- Resource leaks: process cleanup in BashTool, GrepTool, NotificationService
- Input validation: null/blank checks in BashTool, FileEditTool, GrepTool
- Path traversal: FileEditTool blocks ../ escape from workDir
- Thread safety: AgentLoop messageHistory now synchronizedList
- Error handling: log instead of silently swallow exceptions
- Bounds validation: RateLimiter constructor validates all params

Tests (87 total):
- TokenEstimationServiceTest: 14 tests (estimation, cost, formatting)
- RateLimiterTest: 12 tests (limits, cooldown, reset, concurrent)
- InternalLoggerTest: 12 tests (levels, structured, file output, export)
- NotificationServiceTest: 6 tests (config, disabled mode)
- FileEditToolTest: 8 tests (validation, traversal, core edit)
- BashToolTest: 9 tests (validation, dangerous commands, metadata)
- GrepToolTest: 5 tests (validation, metadata)
- Phase4CommandsTest: 21 tests (all Phase 4B+4D command metadata)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
abel533
2026-04-05 11:48:00 +08:00
co-authored by Copilot
parent dd47566cb8
commit bd375e6b15
14 changed files with 1105 additions and 11 deletions
@@ -107,6 +107,9 @@ public class GrepTool implements Tool {
@Override
public String execute(Map<String, Object> input, ToolContext context) {
String pattern = (String) input.get("pattern");
if (pattern == null || pattern.isBlank()) {
return "Error: 'pattern' parameter is required.";
}
String searchPath = (String) input.getOrDefault("path", ".");
String include = (String) input.getOrDefault("include", null);
String type = (String) input.getOrDefault("type", null);
@@ -135,10 +138,12 @@ public class GrepTool implements Tool {
while ((line = reader.readLine()) != null && lines.size() < headLimit) {
lines.add(line);
}
} finally {
if (!process.waitFor(30, TimeUnit.SECONDS)) {
process.destroyForcibly();
}
}
process.waitFor(30, TimeUnit.SECONDS);
if (lines.isEmpty()) {
return "No matches found for pattern: " + pattern;
}