Emit `FileSystem` traversal events and subscribe `RepoContext` with `@on_event`, deprecating `traversal_tool_names` sniffing

Merged
DO
3mo ago
+3576-3921

Summary

FileSystem now emits typed capability events for its traversal and mutation operations, and RepoContext subscribes to them instead of sniffing tool names:

  • FileReadEvent, DirectoryListedEvent, FileWrittenEvent (namespace file_system, kinds file_system.file_read etc.), carrying the normalized, authorization-checked path (_model_safe_filename discipline — never absolute host paths) and a content hash / entry count. Emitted only from agent-run tool invocations; direct FileSystemToolset method calls stay event-free and fully compatible.
  • RepoContext's nested-traversal strategy (nested_traversal) is now driven by @on_event(FileReadEvent, DirectoryListedEvent): it reacts to FileSystem's processed data instead of guessing at tool names and argument keys, and delivers the AGENTS.md/CLAUDE.md note via ctx.enqueue() (message tail, cache-safe). Works with any capability that emits these events, not just our FileSystem.
  • traversal_tool_names / traversal_path_arg are deprecated (HarnessDeprecationWarning when set to non-default values); the tool-sniffing fallback stays functional for customized configurations so existing hosts keep working, with _seen_dirs deduplicating if both paths fire.

Tool return strings deliberately keep echoing the caller-supplied path — the normalized path is event payload, not a change to model-visible output.

Requires an unreleased pydantic-ai (pydantic/pydantic-ai#6258); don't merge before a core release carries it.

Linked Issue

Closes #594. Part of #711.

Checklist

  • Linked issue exists and is referenced above
  • Tests added/updated for new behavior
  • make lint && make typecheck && make test passes locally (against pydantic-ai emit-custom-events)
  • pyproject.toml and uv.lock are unchanged, or a maintainer added dependencies:approved to the current head
  • Docstrings use single backticks (not RST double backticks)

Unblocked. pydantic-ai 2.38.0 carries the event API (#6258), and #780 moved the floor onto it, so this branch no longer touches pyproject.toml or uv.lock at all.

The ACP failure is fixed. AcpSessionConfig now takes capabilities=, passed through to Agent.run(capabilities=...), so FileSystem is registered as a capability instead of being reduced to its toolset. _approval_tool_names scans capability-contributed toolsets too, and test_session_capability_owns_the_events_its_tools_emit pins why it matters. That is the minimum unblock; ACP composing sessions from capabilities natively is tracked in #777.

The Coder cassette is re-recorded, and needed a determinism fix to stay that way. RepoContext now delivers its nested-instructions note through ctx.enqueue() rather than by appending to the tool result, which changes the request sequence; the cassette matches on ['method', 'uri'] alone, so it replays strictly in order and the old recording no longer lines up.

Re-recording by itself was not enough. Plan step ids are uuid4().hex[:8], and the model quotes them back in later write_plan and update_task_status* calls, so a cassette bakes in the ids of the run that recorded it. On replay a fresh uuid4() gives the store different ids, every one of those calls misses its step, and the tool results diverge from the recording. The cassette this replaces only worked because that trajectory happened to quote ids the model had invented ('1', '2'), which missed identically in both runs — so this was latent, not something the event change introduced. The test now counts ids (hex[:8] is the top 32 bits, so the counter goes there), and record and replay agree.

Verified against the released pydantic-ai-slim==2.38.0 from PyPI: 5,592 passed, 63 skipped. The Coder test was replayed three times, twice under the default random ordering.