feat: add AgentAdapter abstraction with Codex CLI support#95
Conversation
132c6b4 to
d6a4cba
Compare
Replace hardcoded Claude Code transcript parsing with an extensible AgentAdapter trait. Each agent gets its own adapter for event mapping, file change extraction, transcript parsing, and token/model extraction. - AgentAdapter trait with ClaudeCode, Codex, and Default adapters - Codex transcript parsing: response_item, custom_tool_call, event_msg, apply_patch file changes from transcript chunks - CLI: protocol v2, repeatable --agent flag for init/stream - tracevault init --agent codex installs .codex/hooks.json - AgentBadge component with per-agent icon on session list/detail - Server uses AgentAdapterRegistry on AppState - Removes old hardcoded extract_file_change/is_file_modifying_tool Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
d6a4cba to
555e6c2
Compare
…hooks and CLI flag Claude Code was rewritten in #95 instead of being ported from session_detail.rs::parse_record on main, which introduced display regressions: Bash/Glob toolUseResult lost their tool_name (wrong nested-key lookup), tool_result blocks lost their text body (read `text` instead of `content`), and assistant text formatting lost the \n\n separator and `[thinking] ` prefix. The parser is now a faithful port — same fields, same fallbacks, same format strings. Token extraction now mirrors main: presence of `usage` gates the whole RecordUsage and individual missing fields default to 0, instead of aborting on missing input/output_tokens. Codex adapter: - SessionStart matcher widened from "startup|resume" to "" so the hook also fires on /clear (verified against openai/codex sources). - The user-message system-prompt filter no longer drops every message starting with `<`. It now matches only the seven known Codex injection tags from codex protocol.rs (user_instructions, environment_context, apps_instructions, skills_instructions, plugins_instructions, collaboration_mode, realtime_conversation), preserving legitimate <div>/<svg>/<T>-style user questions. - File changes extracted from transcript chunks now use the chunk's own RFC 3339 timestamp (with fallback to the hook delivery time) rather than stamping every batched patch with the hook arrival time. CLI: - `tracevault init --agent <name>` is now additive: Claude Code hooks are always installed, additional --agent values are appended and deduplicated (with `claude` aliased to `claude-code`). Previously --agent codex replaced rather than augmented the default, so users following the README ended up without Claude hooks. - The success print now reflects which agents were actually installed instead of unconditionally claiming "Claude Code hooks installed". - README CLI table reworded to match the additive behavior. Cleanup: deduplicated adapter.is_file_modifying call in service/stream.rs (the result is already in `store_response`). Tests: 16 new adapter tests cover the regressed Claude Code parser paths (Bash/Glob/tool_result/thinking/system unknown subtype/progress edge cases) plus Codex token_usage edge cases and the Codex system-prompt whitelist. 5 new init tests cover the additive --agent behavior, dedup of `claude`/`claude-code` aliases, and the Codex SessionStart match-all matcher. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…hic API
Pull adapter-specific knowledge out of `service/stream.rs`. Previously
the stream service hardcoded Codex chunk-shape lookups (`payload.name`,
`payload` cloning, RFC 3339 timestamp parsing) and used two extraction
methods with different return types (`Vec<ExtractedFileChange>` vs
`Vec<TranscriptFileChange>`), so the call site had to reach into chunk
internals to fill in tool_name / tool_input / timestamp.
The trait now exposes two symmetric methods returning the same
`FileChangeRecord` type:
fn file_changes_from_hook(&self, tool, input, ts) -> Vec<FileChangeRecord>
fn file_changes_from_transcript(&self, chunk, fallback_ts)
-> Vec<FileChangeRecord>
Each adapter overrides at most one. Defaults return empty. The
`FileChangeRecord` carries everything the persistence layer needs
(change, tool_name, tool_input, timestamp), so `stream.rs` just
iterates and inserts — no chunk shape knowledge anywhere outside the
adapter that owns that format.
Claude path is preserved bit-for-bit against main:
* `is_file_modifying` gate around the hook-extract loop is kept, so
Read/Glob/etc. skip the call entirely (matches main's
`if is_file_modifying_tool { ... }`).
* New `provides_transcript_file_changes()` capability flag (default
false) gates the per-line transcript-extract loop. Claude returns
false → the `file_changes_from_transcript` method is never invoked
for Claude transcript lines, exactly as on main where no equivalent
call existed.
* `file_changes_from_hook` for Claude wraps the same Write/Edit logic
that lived in `extract_file_change` on main; the resulting DB writes
have identical fields and timestamps (record.timestamp = req.timestamp).
CLI: replace the hardcoded `match agent.as_str() { "claude-code" => ...,
"codex" => ... }` in `main.rs` with `adapter.display_name()` and
`adapter.hooks_install_path()` from the trait, so adding a new agent
no longer requires touching the print-message code.
Codex: `file_changes_from_transcript` now resolves the chunk's RFC 3339
timestamp internally and returns it in each record, replacing the
duplicated timestamp logic that previously lived in `stream.rs`.
The `provides_transcript_file_changes` override is `true`.
Tests: 51 adapter tests (was 50), including a new fallback case
verifying that a chunk with no top-level timestamp falls back to the
hook delivery time. All hook/transcript extraction tests updated to
the new method names and return type.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…ing --agent Move the "claude"/"claude-code" alias resolution and dedup off the CLI and onto the AgentAdapter::name() canonical id — the registry already maps both strings to the same adapter, so the manual match was redundant. Dedup now runs against the adapter's own id, not the user-provided string. Change semantics: --agent codex installs only Codex hooks. Claude Code is installed only when --agent is omitted entirely (default), instead of being appended unconditionally to every --agent invocation. .gitignore entries are derived from each installed adapter's hooks_install_path(), so a codex-only init no longer pins .claude/settings.json into the ignore list.
Multi-agent split caused subtle drift on the Claude code path. Restore parity with pre-multi-agent main: - wire_protocol_version() trait method (default v2); Claude overrides to v1 so request bytes match main - persists_model_without_usage() capability flag (default false); Codex sets it true. Server stream gate becomes has_tokens || (flag && model.is_some()), so Claude's update_tokens stays token-presence-only as in main - ClaudeCodeAdapter parser locks onto first tool_use block via seen_tool_use flag (matches main's arr.iter().find() semantics) - CLI stream uses adapter.wire_protocol_version() / adapter.name() for protocol_version + tool fields - init.rs installs hooks after .gitignore update (matches main order) Also: CLI init prints actually-installed gitignore entries instead of hardcoded paths, and a comment marks _event_type as unused (routing is via hook_event_name from stdin). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…E with --agent semantics
Codex Stop hook entry was missing the `matcher` field that all other
lifecycle hooks (SessionStart, PreToolUse, PostToolUse) already carry,
risking a silent no-op if Codex requires the field. README also still
described `--agent` as additive ("in addition to the Claude Code hooks")
even though the flag has been replacement-only since 6fad80f.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
hashedone
left a comment
There was a problem hiding this comment.
Review notes
Thanks for this — the adapter abstraction is clean and the capability-flag pattern is the right approach. A few things worth addressing before merge, ranging from a potential silent data loss to minor maintenance notes.
🟡 Medium: chunk_index collision between transcript chunks and synthetic tool events
In service/stream.rs, when provides_transcript_file_changes() is true (Codex), synthetic tool events are inserted with event_index: chunk_index:
event_index: chunk_index, // reusing transcript chunk indexThe events table has UNIQUE(session_id, event_index) with ON CONFLICT DO NOTHING. If a Codex session also receives hook ToolUse events whose event_index values overlap with transcript chunk indices, the second insert is silently dropped. There is no namespace separation between the two index spaces. This does not affect CC (gated by provides_transcript_file_changes = false), but for Codex sessions it could silently lose events without any error.
Suggestion: either offset synthetic event indices (e.g. use a separate counter namespace), or assert they come from a different range.
🟡 Medium: AgentAdapterRegistry constructed per hook call in CLI
In crates/tracevault-cli/src/commands/stream.rs:
let registry = AgentAdapterRegistry::new();
let adapter = registry.get(agent);This constructs a new registry (allocating all adapters) on every hook invocation — PostToolUse fires on every tool call. The allocation is cheap but unnecessary. Consider constructing just the needed adapter directly, or passing the agent name through to a shared/static registry.
🔵 Low: Codex hooks.json wraps entries under a "hooks" key — needs spec verification
In CodexAdapter::install_hooks:
config_obj.insert("hooks".to_string(), hooks_json());This writes {"hooks": {"PostToolUse": [...]}} to .codex/hooks.json. But based on the Codex docs, hooks are defined at the top level of hooks.json — the file contents should be {"PostToolUse": [...]} directly, not wrapped under a "hooks" key. Worth verifying against the current Codex hook spec before shipping, otherwise the hooks may silently not fire.
🔵 Low: store_response variable reused as file-change extraction gate
let store_response = adapter.is_file_modifying(tool_name);
// ...
if store_response {
// file change extractionstore_response was semantically named for "should we persist the tool_response blob" but is reused as the gate for file change extraction. For CC these happen to be the same tool set (Write/Edit/Bash), but the coupling is implicit. A future adapter that has a file-modifying tool with large/binary output would need to override is_file_modifying to false to avoid storing the blob — which would also skip file change extraction. Worth separating into two distinct capability queries.
🔵 Low: CC protocol v1 fallback fragility
let agent_name = tool.as_deref().unwrap_or("claude-code");This works correctly for v1 (where tool is absent). Worth a comment noting that if a future CC version sends v2 without tool: "claude-code", it would fall through to DefaultAdapter and lose all token/file extraction silently. The CLI sets tool explicitly so this is safe today, but the silent fallback is worth documenting.
🔵 Low: Hardcoded Codex system prompt XML tags — maintenance drift risk
const CODEX_SYSTEM_PROMPT_TAGS: &[&str] = &[
"<user_instructions>",
"<environment_context>",
...These are sourced from openai/codex protocol.rs. If Codex adds or renames tags in a future release, this filter will either pass system prompts through (display noise) or incorrectly filter legitimate user messages. A comment noting the Codex source file and version where these were verified would help future maintainers know when to re-check.
Description
Replace hardcoded Claude Code transcript parsing with an extensible AgentAdapter trait and registry. Each AI coding agent gets its own adapter for event mapping, file change extraction, and transcript record parsing. Adds full Codex CLI support.
What's included
tracevault-coreresponse_item/message,custom_tool_call,event_msg,apply_patchfile changes from transcript chunks--agentflag forstreamcommand, Codex-compatible hook response formattracevault init --agent codex— installs Codex hooks in.codex/hooks.jsonextract_file_change/is_file_modifying_toolfromstreaming.rsHow it works
The server resolves the adapter from
sessions.toolcolumn (set by CLI via--agentflag). During ingestion (stream.rs), the adapter extracts tokens and file changes. During display (session_detail.rs,traces_ui.rs), it parses transcript chunks intoTranscriptRecords for the frontend.Codex file modifications come exclusively through transcript chunks (
custom_tool_callwithapply_patch), not through hook ToolUse events — the adapter handles this viaextract_file_changes_from_transcript.Checklist
cargo fmtpassescargo clippypasses🤖 Generated with Claude Code