Research Document: Issue #611 - Sessions Files and By-File Subcommands
Status: Approved Author: Claude Code Date: 2026-03-11 Reviewers: Engineering Team
Executive Summary
Issue #611 requests adding sessions files <session-id> and sessions by-file <file-path> subcommands to terraphim-agent. Research found that the session infrastructure already exists in terraphim_sessions crate with JSONL parsing capability. The implementation requires extending the SessionsSubcommand enum and SessionService to extract file paths from tool invocations.
Essential Questions Check
| Question | Answer | Evidence | |----------|--------|----------| | Energizing? | Yes | Extends session analysis capabilities | | Leverages strengths? | Yes | Builds on existing terraphim_sessions infrastructure | | Meets real need? | Yes | Issue #611 explicitly requests this |
Proceed: Yes - 3/3 YES
Problem Statement
Description
Add file-level tracking to the sessions subcommand group:
sessions files <session-id>- List files touched by a specific sessionsessions by-file <file-path>- List sessions that touched a given file path
Impact
Enables developers to:
- Track which files were modified in a specific coding session
- Find all sessions that touched a particular file
- Understand code evolution across AI-assisted sessions
Success Criteria
sessions files <session-id>extracts and displays file paths from tool invocationssessions by-file <file-path>returns sessions touching that file (substring match)- Files are categorized as "read" vs "written"
- Both commands support
--jsonfor programmatic use - Human-readable table output by default
Current State Analysis
Existing Implementation
terraphim_sessions crate structure:
crates/terraphim_sessions/
├── src/
│ ├── lib.rs
│ ├── model.rs # Session, Message, ContentBlock
│ ├── service.rs # SessionService
│ ├── connector/
│ │ └── native.rs # Native JSONL connector
│ └── enrichment/
│ └── enricher.rs # Session enrichmentSession Model (model.rs:159-179):
Message Model (model.rs:89-107):
ContentBlock (model.rs:57-76):
Existing SessionsSubcommands (terraphim_agent/src/repl/commands.rs:155-194):
Code Locations
| Component | Location | Purpose |
|-----------|----------|---------|
| SessionsSubcommand | terraphim_agent/src/repl/commands.rs:155 | CLI command enum |
| handle_sessions | terraphim_agent/src/repl/handler.rs:1762 | Command handler |
| SessionService | terraphim_sessions/src/service.rs:14 | Business logic |
| Session model | terraphim_sessions/src/model.rs:159 | Data structure |
| ContentBlock | terraphim_sessions/src/model.rs:57 | Tool invocation data |
Data Flow
terraphim-agent CLI
↓
SessionsSubcommand parsing (commands.rs)
↓
handle_sessions() (handler.rs)
↓
SessionService methods (terraphim_sessions)
↓
Session cache (HashMap<SessionId, Session>)
↓
Extract file paths from ContentBlock::ToolUseConstraints
Technical Constraints
- Must extract file paths from
serde_json::Valuetool input - Tool names vary by source (Claude Code, Cursor, etc.)
- File paths may be relative or absolute
- Must handle missing/invalid tool input gracefully
Business Constraints
- Maintain backward compatibility with existing session commands
- Keep
--jsonoutput consistent with other commands - Don't break existing session parsing
Non-Functional Requirements
| Requirement | Target | |-------------|--------| | Extraction latency | < 100ms per session | | Memory overhead | Minimal (streaming extraction) | | JSON output | Machine-parseable format |
Vital Few
Essential Constraints
| Constraint | Why It's Vital | Evidence | |------------|----------------|----------| | Correct file path extraction | Core feature requirement | Issue #611 | | Read vs Write categorization | User needs to understand impact | Issue description | | Support --json flag | Programmatic use case | Issue description |
Eliminated from Scope
| Eliminated Item | Why Eliminated | |-----------------|----------------| | File content diff | Too complex for this issue | | Git integration | Out of scope - just session tracking | | Real-time file watching | Different feature entirely |
Dependencies
Internal Dependencies
| Dependency | Impact | Risk | |------------|--------|------| | terraphim_sessions | Core models and service | Low - stable API | | terraphim_agent | CLI integration point | Low - existing patterns | | comfy_table | Table formatting | Low - already used |
External Dependencies
| Dependency | Version | Risk | |------------|---------|------| | serde_json | 1.0 | Low - for tool input parsing |
Risks and Unknowns
Known Risks
| Risk | Likelihood | Impact | Mitigation | |------|------------|--------|------------| | Tool input format variations | Medium | Medium | Test with multiple sources | | Performance on large sessions | Low | Low | Lazy extraction, caching | | Path normalization issues | Medium | Low | Use PathBuf, canonicalize |
Open Questions
- Should we normalize file paths (absolute vs relative)? - Use as-is from tool input
- How to handle glob patterns in paths? - Treat as literal strings
Research Findings
Key Insights
- Tool Input Structure: File paths are in
ContentBlock::ToolUse.inputasserde_json::Value - Tool Name Mapping: Different tools use different field names:
- Read:
file_path - Edit/Write/MultiEdit:
file_path - NotebookEdit:
notebook_path - Glob/Grep:
path
- Read:
- Read vs Write: Can be determined by tool name:
- Read:
Read,Glob,Grep - Write:
Edit,Write,MultiEdit,NotebookEdit
- Read:
Tool Input Extraction Targets
| Tool | Input Field | Operation |
|------|-------------|-----------|
| Read | tool_input.file_path | read |
| Edit | tool_input.file_path | write |
| Write | tool_input.file_path | write |
| MultiEdit | tool_input.file_path | write |
| NotebookEdit | tool_input.notebook_path | write |
| Glob | tool_input.path | read |
| Grep | tool_input.path | read |
Relevant Prior Art
terraphim_agent/src/repl/handler.rs:1762- handle_sessions implementationterraphim_sessions/src/service.rs:110- search() method patterncomfy_tableusage for table formatting
Recommendations
Proceed/No-Proceed
Proceed - Clear requirement, existing infrastructure, straightforward implementation.
Scope
- Add
Filesvariant toSessionsSubcommand - Add
ByFilevariant toSessionsSubcommand - Implement file extraction logic in
terraphim_sessions - Add handler methods in
terraphim_agent - Support both table and JSON output
Risk Mitigation
- Add unit tests for tool input parsing
- Test with real session files from multiple sources
- Handle malformed tool input gracefully
Next Steps
- Create design document with implementation steps
- Add
FileAccessstruct and extraction methods toterraphim_sessions - Extend
SessionsSubcommandenum - Implement handlers in
terraphim_agent - Add tests and documentation
Appendix
Proposed Data Structures
/// File access record extracted from session
Proposed Service Methods