picoclaw/pkg/tools
Zhaoyikaiii 1f7cbd9164
fix: cache system prompt with mtime-based auto-invalidation (#607)
Avoid rebuilding the entire system prompt on every BuildMessages() call
by caching the static portion (identity, bootstrap, skills summary,
memory) and only recomputing it when workspace source files change.

Key changes:

- ContextBuilder caches the static prompt behind an RWMutex with
  double-checked locking. Source file changes are detected via cheap
  os.Stat mtime checks so no explicit invalidation is needed.

- Track file existence at cache time (existedAtCache map) so that
  newly created or deleted bootstrap/memory files also trigger a
  rebuild — the old modifiedSince() silently returned false on
  os.IsNotExist.

- Walk the skills directory recursively with filepath.WalkDir to
  catch content-only edits at any nesting depth; directory mtime
  alone misses in-place file modifications on most filesystems.

- ToolRegistry.sortedToolNames() sorts tool names before iteration,
  ensuring deterministic tool definition order across calls — a
  prerequisite for LLM-side prefix/KV cache reuse.

- Merge all context (static + dynamic + summary) into a single
  system message for provider compatibility: the Anthropic adapter
  extracts messages[0] as the top-level system parameter, and Codex
  reads only the first system message as instructions.

- Fix a data race in BuildMessages() where cachedSystemPrompt was
  read without holding the lock in a debug log statement.

- Add tests: single system message invariant, mtime auto-invalidation,
  new-file creation detection, skill file content change, explicit
  InvalidateCache, cache stability, concurrent access (20 goroutines
  x 50 iterations, passes go test -race), and a benchmark.
2026-02-25 15:27:45 +08:00
..
base.go feat(fmt): Run formatters 2026-02-18 21:48:23 +02:00
cron.go feat(fmt): Run formatters 2026-02-18 21:48:23 +02:00
edit.go fix(security): workspace sandbox avoid time-of-check/time-of-use (TOCTOU) races (#464) 2026-02-23 20:09:53 +11:00
edit_test.go fix(security): workspace sandbox avoid time-of-check/time-of-use (TOCTOU) races (#464) 2026-02-23 20:09:53 +11:00
filesystem.go fix(security): workspace sandbox avoid time-of-check/time-of-use (TOCTOU) races (#464) 2026-02-23 20:09:53 +11:00
filesystem_test.go fix(security): workspace sandbox avoid time-of-check/time-of-use (TOCTOU) races (#464) 2026-02-23 20:09:53 +11:00
i2c.go refactor: cleanup dead code and turn on dead code detection in CI (#515) 2026-02-24 21:52:25 +08:00
i2c_linux.go feat(linter): Fix govet linter 2026-02-20 22:35:16 +02:00
i2c_other.go feat(fmt): Run formatters 2026-02-18 21:48:23 +02:00
message.go feat(fmt): Run formatters 2026-02-18 21:48:23 +02:00
message_test.go feat(fmt): Run formatters 2026-02-18 21:48:23 +02:00
registry.go fix: cache system prompt with mtime-based auto-invalidation (#607) 2026-02-25 15:27:45 +08:00
registry_test.go feat: integrate Tavily search (#340) 2026-02-23 00:30:14 +08:00
result.go feat: US-002 - Modify Tool interface to return *ToolResult 2026-02-12 19:28:56 +08:00
result_test.go feat(fmt): Run formatters 2026-02-18 21:48:23 +02:00
shell.go fix(security): ensure custom deny patterns extend defaults instead of replacing them (#479) 2026-02-24 09:02:44 +11:00
shell_process_unix.go fix(exec): terminate process tree on timeout 2026-02-18 02:01:29 +08:00
shell_process_windows.go fix(exec): terminate process tree on timeout 2026-02-18 02:01:29 +08:00
shell_test.go fix: golangci-lint run --fix 2026-02-21 16:20:15 +08:00
shell_timeout_unix_test.go feat(fmt): Fix formatting 2026-02-20 20:03:11 +02:00
skills_install.go feat(fmt): Fix formatting 2026-02-20 20:03:11 +02:00
skills_install_test.go feat(fmt): Fix formatting 2026-02-20 20:03:11 +02:00
skills_search.go feat(fmt): Fix formatting 2026-02-20 20:03:11 +02:00
skills_search_test.go feat(fmt): Fix formatting 2026-02-20 20:03:11 +02:00
spawn.go fix: reject empty task in spawn tool (#740) 2026-02-25 07:39:49 +11:00
spawn_test.go fix: reject empty task in spawn tool (#740) 2026-02-25 07:39:49 +11:00
spi.go refactor: cleanup dead code and turn on dead code detection in CI (#515) 2026-02-24 21:52:25 +08:00
spi_linux.go feat(fmt): Run formatters 2026-02-18 21:48:23 +02:00
spi_other.go feat(fmt): Run formatters 2026-02-18 21:48:23 +02:00
subagent.go Merge branch 'main' into fix-formatting 2026-02-19 22:04:48 +02:00
subagent_tool_test.go feat(fmt): Fix formatting 2026-02-19 22:05:15 +02:00
toolloop.go Merge branch 'main' into fix-formatting 2026-02-20 20:02:53 +02:00
types.go feat(fmt): Run formatters 2026-02-18 21:48:23 +02:00
web.go feat: improve web proxy handling and coverage 2026-02-24 17:17:14 +08:00
web_test.go feat: improve web proxy handling and coverage 2026-02-24 17:17:14 +08:00