Trace
Trace Commands
The Arc Memory CLI provides commands for tracing the history of specific lines in files, allowing you to follow the decision trail through commits, PRs, issues, and ADRs.
Related Documentation:
- Build Commands - Build your knowledge graph before tracing
- Doctor Commands - Verify your graph status
- Tracing History Examples - Detailed examples of tracing history
- Trace API - Programmatic access to trace functionality
Overview
The trace command uses a breadth-first search (BFS) algorithm to traverse the knowledge graph, starting from the commit that last modified a specific line in a file. It follows edges to related entities, providing a comprehensive view of the decision trail.
Commands
arc trace file
Trace the history of a specific line in a file.
This command traces the history of a specific line in a file, showing the commit that last modified it and related entities such as PRs, issues, and ADRs.
Arguments
FILE_PATH
: Path to the file, relative to the repository root.LINE_NUMBER
: Line number to trace (1-based).
Options
--max-results
,-m INTEGER
: Maximum number of results to return (default: 3).--max-hops
,-h INTEGER
: Maximum number of hops in the graph traversal (default: 2).--format
,-f [text|json]
: Output format (default: text).--debug
: Enable debug logging.
Example
Output Formats
The trace command supports two output formats: text (default) and JSON.
Text Format
When using the default text format (--format text
), the command outputs a table with the following columns:
The trace command outputs a table with the following columns:
- Type: The type of the node (commit, pr, issue, adr, file).
- ID: The unique identifier of the node.
- Title: The title or description of the node.
- Timestamp: When the node was created or last modified.
- Details: Additional details specific to the node type.
Example output:
JSON Format
When using the JSON format (--format json
), the command outputs a JSON array of objects, where each object represents a node in the history trail. This format is particularly useful for programmatic consumption, such as by the VS Code extension.
Example JSON output:
Each object in the JSON array contains:
-
Common fields for all node types:
type
: The type of the node (commit, pr, issue, adr, file)id
: The unique identifier of the nodetitle
: The title or description of the nodetimestamp
: When the node was created or last modified (ISO format)
-
Type-specific fields:
- For
commit
:author
,sha
- For
pr
:number
,state
,url
- For
issue
:number
,state
,url
- For
adr
:status
,decision_makers
,path
- For
The JSON output is sorted chronologically, with the newest events first, matching the behavior of the text output.
Interpreting Trace Results
Understanding Node Types
The trace results include different types of nodes that represent different entities in the software development process:
-
commit: A Git commit that modified the file
- This is typically the starting point of the trace
- Shows who made the change and when
-
pr: A GitHub Pull Request
- Shows how the commit was merged into the codebase
- Links to discussions about the code changes
-
issue: A GitHub Issue
- Represents the problem or feature that motivated the change
- Often contains requirements or bug reports
-
adr: An Architectural Decision Record
- Documents important design decisions
- Explains the rationale behind architectural choices
-
file: A file in the repository
- Represents a file that’s related to the traced line
- May be referenced by other nodes
Understanding Relationships
The relationships between nodes tell a story about how the code evolved:
-
commit → pr: The PR that merged this commit
- Example: “Commit abc123 was merged via PR #42”
-
pr → issue: The issue that the PR addresses
- Example: “PR #42 fixes Issue #123”
-
issue → adr: The architectural decision related to the issue
- Example: “Issue #123 implements the design described in ADR-001”
-
commit → file: The file modified by the commit
- Example: “Commit abc123 modified src/login.py”
Reading the Decision Trail
To understand the full decision trail, read the results from bottom to top (oldest to newest):
-
Start with the issue (if present)
- This explains why the change was needed
- Look at the issue title, state, and URL
-
Look at any adr nodes
- These explain architectural decisions
- Help understand the design rationale
-
Examine the pr nodes
- These show how the change was reviewed and merged
- The PR description often contains important context
-
Finally, look at the commit nodes
- These show the actual code changes
- The commit message often explains implementation details
Example Interpretation
For the example output above, the decision trail reads:
-
Issue #123 “Login form bug” was opened (2023-04-10)
- This identified a problem with the login form
-
Commit abc123 “Fix bug in login form” was created by John Doe (2023-04-15)
- This implemented a fix for the bug
-
PR #42 “Fix login issues” was merged (2023-04-16)
- This PR included the fix and was reviewed and approved
This tells us that the line we traced was modified to fix a bug in the login form, which was tracked as Issue #123 and merged through PR #42.
How It Works
The trace algorithm works as follows:
- Uses
git blame
to find the commit that last modified the specified line. - Starts a breadth-first search from that commit node in the knowledge graph.
- Traverses edges to related entities (PRs, issues, ADRs) up to the specified maximum number of hops.
- Returns the results sorted by timestamp (newest first).
Requirements
- The knowledge graph must be built before tracing history. If the database doesn’t exist, the command will prompt you to run
arc build
. - Git must be installed and available in the PATH.
- The file must exist in the repository.
Troubleshooting
If you encounter issues with the trace command:
- No Results: If no history is found, ensure the file and line number are correct, and that the knowledge graph has been built with
arc build
. - Database Not Found: If the database doesn’t exist, run
arc build
to create it. - Git Blame Errors: Ensure the file is tracked by Git and has commit history.
- Debug Mode: Run with
--debug
flag to see detailed logs:arc trace file path/to/file.py 42 --debug