fix: lock session usage tracking to specific Claude session ID - #2
Merged
Merged
Conversation
Greptile SummaryThis PR fixes usage tracking by locking each tmux session to a specific Claude session ID at discovery time, preventing incorrect usage attribution when multiple sessions exist in the same working directory. Key Changes:
Issues Found:
Confidence Score: 3/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Monitor as Monitor (daemon)
participant Tmux as Tmux Client
participant Usage as Usage Parser
participant Watcher as Usage Watcher
participant FS as File System
Note over Monitor: Session Discovery
Monitor->>Tmux: ListSessions()
Tmux-->>Monitor: tmux sessions
Monitor->>Tmux: GetSessionPath(session)
Tmux-->>Monitor: workingDir
Note over Monitor,Usage: Lock Session ID (NEW)
Monitor->>Usage: FindActiveSessionID(workingDir)
Usage->>FS: FindProjectDir & list *.jsonl files
FS-->>Usage: session files
Usage->>FS: Stat files for most recent
Usage-->>Monitor: claudeSessionID
Monitor->>Usage: GetSessionByID(workingDir, claudeSessionID)
Usage->>FS: Read specific session JSONL file
FS-->>Usage: session data
Usage-->>Monitor: SessionUsage
Note over Monitor: Store locked session ID
Monitor->>Monitor: Store ClaudeSessionID in SessionState
Monitor->>Watcher: WatchSession(name, workingDir, claudeSessionID)
Note over Watcher: Continuous Monitoring
loop Every pollInterval
Watcher->>FS: Stat(sessionFile)
FS-->>Watcher: file info
alt File changed
Watcher->>Usage: ParseSessionFile(sessionFile)
Usage->>FS: Read JSONL lines
FS-->>Usage: token usage data
Usage-->>Watcher: Updated SessionUsage
Watcher->>Monitor: Notify update
end
end
Note over Monitor: Usage Updates (NEW)
loop Every 5 poll cycles
Monitor->>Usage: GetSessionByID(workingDir, claudeSessionID)
Usage-->>Monitor: SessionUsage for locked session
Monitor->>Monitor: Update SessionState.Usage
end
|
Comment on lines
164
to
165
| // Claude Code uses path encoding: /Users/foo/bar -> -Users-foo-bar | ||
| encodedPath := strings.ReplaceAll(workingDir, "/", "-") |
There was a problem hiding this comment.
logic: Removing TrimPrefix breaks path encoding for root-level directories. For example, /Users/foo/bar now encodes to -Users-foo-bar instead of Users-foo-bar, which won't match Claude's actual directory structure.
Suggested change
| // Claude Code uses path encoding: /Users/foo/bar -> -Users-foo-bar | |
| encodedPath := strings.ReplaceAll(workingDir, "/", "-") | |
| // Claude Code uses path encoding: /Users/foo/bar -> -Users-foo-bar | |
| encodedPath := strings.ReplaceAll(workingDir, "/", "-") | |
| encodedPath = strings.TrimPrefix(encodedPath, "-") |
Prompt To Fix With AI
This is a comment left during a code review.
Path: internal/usage/parser.go
Line: 164:165
Comment:
**logic:** Removing `TrimPrefix` breaks path encoding for root-level directories. For example, `/Users/foo/bar` now encodes to `-Users-foo-bar` instead of `Users-foo-bar`, which won't match Claude's actual directory structure.
```suggestion
// Claude Code uses path encoding: /Users/foo/bar -> -Users-foo-bar
encodedPath := strings.ReplaceAll(workingDir, "/", "-")
encodedPath = strings.TrimPrefix(encodedPath, "-")
```
How can I resolve this? If you propose a fix, please make it concise.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.