Skip to content

Commit 8327224

Browse files
committed
Improve the debugability of the sessiont tracker
1 parent 4220ed1 commit 8327224

20 files changed

+918
-569
lines changed

.beads/issues.jsonl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
{"id":"agent-tracker-41","title":"Fix session selection jumping when activity events arrive","description":"When viewing a TranscriptViewer, receiving activity events (stop/notification) causes the selected session to jump to a different session.\n\n**Root Cause**: \n- Sessions array is sorted by priority (awaiting input \u003e active \u003e inactive \u003e ended)\n- When activity events arrive, sessions get re-sorted\n- App.tsx effect at lines 119-130 runs whenever sessions array changes\n- Even though it checks if selected session exists, there may be edge cases or race conditions\n\n**Events that trigger**:\n```json\n{\"event_type\":\"activity\",\"activity_type\":\"stop\",\"session_id\":\"...\",\"hook_event_name\":\"Stop\"}\n{\"event_type\":\"activity\",\"activity_type\":\"notification\",\"notification_message\":\"Claude is waiting for your input\",\"hook_event_name\":\"Notification\"}\n```\n\n**Solution**: Make the selection management more defensive and ensure it never changes when in transcript view mode.","notes":"Fixed session jumping issue with stack-based navigation refactoring.\n\n**Root cause:** The selection effect was running every time the sessions array changed (including re-sorting), even though the session IDs didn't actually change.\n\n**Solution (App.tsx:114-135):**\n1. Added `useMemo` to track session IDs: `sessionIds = sessions.map(s =\u003e s.id).join(',')`\n2. Changed effect dependency from `sessions` to `sessionIds`\n3. Now effect only runs when sessions are added/removed, not when re-sorted\n\n**Result:** When activity events arrive and sessions get re-sorted by priority, the selected session stays stable because sessionIds doesn't change.\n\nThis was discovered during stack refactoring - need to verify fix works correctly.","status":"open","priority":0,"issue_type":"bug","created_at":"2025-10-20T23:00:04.531865+11:00","updated_at":"2025-10-20T23:12:35.765071+11:00","closed_at":"2025-10-20T23:00:36.613499+11:00"}
3737
{"id":"agent-tracker-42","title":"Complete navigation stack refactoring","description":"The navigation stack refactoring is functionally complete but needs final touches:\n\n**Completed:**\n- ✅ Created `useNavigation` hook with reducer pattern\n- ✅ Created `SessionListView` component with keyboard handling\n- ✅ Refactored `App.tsx` to use navigation stack (5 useState → 1 hook)\n- ✅ Updated `ToolDetailView` to make onBack optional\n- ✅ Build succeeds, app runs correctly\n\n**Remaining:**\n- [ ] Add unit tests for `useNavigation` reducer\n- [ ] Remove any unused imports/code from refactoring\n- [ ] Verify no duplicate React keys remain (saw warning in dev output)\n- [ ] Update documentation/comments as needed\n- [ ] Test all navigation flows manually (list → transcript → tool detail → back)\n\nThe core refactoring is done and working - this issue tracks the polish/cleanup work.","status":"open","priority":2,"issue_type":"task","created_at":"2025-10-20T23:10:20.28883+11:00","updated_at":"2025-10-20T23:10:20.60385+11:00"}
3838
{"id":"agent-tracker-43","title":"implement simple MCP server as part of plugin to allow sessions to communicate work summary","description":"We should implement a very simple cli based MCP server using the npm package: @modelcontextprotocol/sdk\n\nThis should be installed as part of the plugin, and should simply be a command that can be called set_title (or something idiomatic like that).\n\nOnce installed the plugin should instruct the agent to call set_title when a piece of work is started or general change of direction has happened. This should be a 5 word summary. \n\nThe MCP itself should just simply echo back the title, but we can then pick up the call to the MCP through the transcript or through a Tools usage and utilise it to update our session model.","design":"## Implementation Plan\n\n1. **MCP Server Setup**\n - Install @modelcontextprotocol/sdk as dependency\n - Create CLI-based MCP server script in `.claude-plugin/mcp/`\n - Implement `set_title` tool that accepts a 5-word summary\n\n2. **Plugin Integration**\n - Update plugin.json to register the MCP server\n - Configure server to be available to Claude sessions\n - Add instructions for agents to call set_title at key moments\n\n3. **Event Capture**\n - Monitor transcript for MCP tool calls\n - Extract set_title invocations and summaries\n - Update session model with work summaries\n\n4. **Testing**\n - Verify MCP server is discoverable by Claude\n - Test set_title tool from within a session\n - Confirm summaries are captured in session data","acceptance_criteria":"- [ ] MCP server installed and runnable\n- [ ] set_title tool available in Claude sessions\n- [ ] Tool accepts 5-word summary parameter\n- [ ] Plugin instructions guide agents to use set_title\n- [ ] Session model updated with work summaries\n- [ ] Integration tested end-to-end","notes":"Implementation completed! All components are in place:\n\n**Phase 1 - MCP Server** ✅\n- Installed @modelcontextprotocol/sdk\n- Created mcp/src/index.ts with stdio-based server\n- Implemented set_work_summary tool\n- Added build infrastructure\n\n**Phase 2 - Plugin Integration** ✅\n- Updated plugin.json with mcpServers configuration\n- Created .claude-plugin/instructions.md with usage guide\n- Server uses ${CLAUDE_PLUGIN_ROOT} for portability\n\n**Phase 3 - Event Capture \u0026 State** ✅\n- Added workSummary field to Session type\n- Created UPDATE_WORK_SUMMARY action type\n- Enhanced activity-handler.ts to capture tool_input\n- Updated SessionTrackerService to detect MCP calls\n- Modified ActivityStore reducer to update work summaries\n\n**Phase 4 - UI Integration** ✅\n- Updated SessionList to display work summaries (dimmed, italic)\n- Updated SessionDetail to show current work (magenta, italic)\n- Both components conditionally render when workSummary exists\n\n**Build \u0026 Testing** ✅\n- All TypeScript compiles successfully\n- MCP server responds correctly to protocol requests\n- Tool schema validated\n\n**Ready for E2E Testing:**\nNext step is to test in an actual Claude session to verify:\n1. MCP server is discoverable\n2. Tool appears in tool list\n3. PostToolUse hook captures parameters\n4. TUI updates with work summary","status":"in_progress","priority":2,"issue_type":"feature","created_at":"2025-10-21T01:41:03.518997+11:00","updated_at":"2025-10-21T01:57:06.833297+11:00"}
39+
{"id":"agent-tracker-44","title":"Improve the SessionDetails page. Lots of noise and unnecessary stuff.\n\n","description":"W eneed to rework the SessionDetails page and have a bit more focus at the top on the repository and the branch. Second: the summary.\n\nwe don't need to show the transcript, have little focus on the session transcript and then finally the least amount of focus on the terminal","status":"open","priority":2,"issue_type":"task","created_at":"2025-10-22T23:44:08.81075+11:00","updated_at":"2025-10-22T23:44:08.81075+11:00"}
3940
{"id":"agent-tracker-5","title":"Session Tracking \u0026 State Management","description":"Build the core logic for tracking session states and activity","acceptance_criteria":"- [ ] Create session data model/interface\n- [ ] Implement session registry/store\n- [ ] Track session lifecycle (start, active, inactive, ended)\n- [ ] Calculate last activity timestamp\n- [ ] Implement sorting by activity (most recent first)\n- [ ] Mark inactive sessions for visual distinction\n- [ ] Handle session cleanup on end events\n- [ ] Persist session data (optional, for recovery)","notes":"Session persistence is intentionally skipped for MVP - sessions are ephemeral and only exist while the TUI is running. Future enhancement could add persistence if needed.","status":"closed","priority":1,"issue_type":"feature","created_at":"2025-10-20T15:12:41.174583+11:00","updated_at":"2025-10-20T15:12:41.174583+11:00"}
4041
{"id":"agent-tracker-6","title":"TUI Interface Development","description":"Build the visual Ink-based TUI with 2-column layout","acceptance_criteria":"- [ ] Create basic Ink app structure with React components\n- [ ] Implement 2-column layout (left: session list, right: details)\n- [ ] Build session list component with sorting\n- [ ] Style active sessions (highlighted/colored)\n- [ ] Style inactive sessions (grey/dimmed)\n- [ ] Implement session selection/navigation\n- [ ] Build detail panel showing session info\n- [ ] Add real-time updates when sessions change\n- [ ] Handle terminal resize gracefully","status":"closed","priority":1,"issue_type":"feature","created_at":"2025-10-20T15:12:41.175056+11:00","updated_at":"2025-10-20T15:12:41.175056+11:00"}
4142
{"id":"agent-tracker-7","title":"Testing Infrastructure \u0026 Demo Setup","description":"Set up demo-repo and testing framework for feedback loop development","acceptance_criteria":"- [ ] Create demo-repo/ directory\n- [ ] Set up test Claude sessions in demo-repo\n- [ ] Create test scenarios (start, stop, multiple sessions)\n- [ ] Document testing workflow\n- [ ] Add scripts for easy testing\n- [ ] Test session detection and tracking\n- [ ] Verify TUI updates correctly","notes":"Testing infrastructure setup complete. Created demo-repo with sample files, test scenarios, workflow documentation, and helper scripts. \n\nThe last two acceptance criteria (test session detection and verify TUI updates) will be completed during integration testing once the hooks, communication layer, and TUI are implemented.","status":"closed","priority":0,"issue_type":"task","created_at":"2025-10-20T15:12:41.175524+11:00","updated_at":"2025-10-20T15:12:41.175524+11:00"}
Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
---
2+
description: Debug Agent Tracker session tracking issues with a systematic diagnostic workflow
3+
proactive: false
4+
---
5+
6+
# Debug Agent Tracker
7+
8+
You are helping debug Agent Tracker session tracking issues. Use this systematic workflow to diagnose problems.
9+
10+
## Overview
11+
12+
Agent Tracker tracks Claude Code sessions via hooks that write events to `~/.agent-tracker/sessions.jsonl`. The TUI reads this file and displays sessions in real-time.
13+
14+
## Quick Diagnosis Workflow
15+
16+
When a session isn't showing up or behaving correctly, follow these steps:
17+
18+
### Step 1: Check if Events are Being Written
19+
20+
First, verify that events are being captured:
21+
22+
```bash
23+
# Watch for new events in real-time
24+
tail -f ~/.agent-tracker/sessions.jsonl
25+
```
26+
27+
**Expected**: You should see JSON events appearing as Claude activity happens.
28+
29+
**If no events appear**:
30+
- Hooks may not be installed or enabled
31+
- Check `.claude/settings.json` for enabled plugins
32+
- Verify plugin installation with `claude plugin list`
33+
34+
### Step 2: View Event Timeline for the Session
35+
36+
Check the chronological sequence of events:
37+
38+
```bash
39+
# View timeline for specific session
40+
npm run debug:timeline -- --session-id <SESSION_ID> --limit 30
41+
42+
# Or filter by project directory
43+
npm run debug:timeline -- --cwd /path/to/project
44+
```
45+
46+
**Look for:**
47+
-`session_start` event exists
48+
- ✓ Activity events (tool_use, prompt_submit, stop)
49+
- ⚠️ `session_end` followed by more activity → **Session was re-opened**
50+
- ⚠️ Large gaps in activity → **Hooks may have stopped firing**
51+
- ⚠️ No `session_start`**Hook didn't fire on session start**
52+
53+
**Common patterns:**
54+
```
55+
12:30:48 session_start
56+
12:31:15 tool_use Read
57+
12:45:54 session_end
58+
12:50:15 tool_use Read ← Activity after end = reopened session
59+
```
60+
61+
### Step 3: Check Current Session State
62+
63+
View exactly what the TUI sees:
64+
65+
```bash
66+
# View specific session state
67+
npm run debug:sessions -- --session-id <SESSION_ID>
68+
69+
# View all sessions
70+
npm run debug:sessions
71+
```
72+
73+
**Key fields to check:**
74+
75+
```json
76+
{
77+
"status": "active", // Should be "active" for running sessions
78+
"isPhantom": false, // Should be false for real sessions
79+
"awaitingInput": true, // True = waiting for user, false = Claude working
80+
"lastActivityTime": "...", // Should match recent activity
81+
"endTime": null, // Should be null/undefined for active sessions
82+
"workSummary": "..." // Current work description
83+
}
84+
```
85+
86+
### Step 4: Diagnose Common Issues
87+
88+
#### Issue: Session showing as "ended" but is still active
89+
90+
**Symptoms:**
91+
- Timeline shows activity after `session_end`
92+
- Session state shows `status: "ended"`
93+
94+
**Cause:** Session was ended and then re-opened. Activity events should re-activate the session.
95+
96+
**Fix:** Session re-activation logic should handle this automatically (fixed in recent versions). If not working, check ActivityStore reducer.
97+
98+
#### Issue: Session marked as phantom (`isPhantom: true`)
99+
100+
**Symptoms:**
101+
- Session exists but doesn't appear in TUI
102+
- `isPhantom: true` in session state
103+
104+
**Cause:** Transcript file appears to be a phantom (very small, not modified after creation)
105+
106+
**Check:**
107+
```bash
108+
# Check transcript file size and times
109+
ls -lh ~/.claude/projects/.../SESSION_ID.jsonl
110+
stat -f "size=%z birthtime=%SB mtime=%Sm" ~/.claude/projects/.../SESSION_ID.jsonl
111+
```
112+
113+
**Criteria for phantom:**
114+
- File size < 50KB AND
115+
- Modified time - birth time < 60 seconds
116+
117+
#### Issue: Missing session (not in output at all)
118+
119+
**Symptoms:**
120+
- No session found in `debug:sessions` output
121+
- Timeline shows no events for session
122+
123+
**Debugging steps:**
124+
1. Check if session_start event was written to sessions.jsonl
125+
2. Verify hooks are enabled for this project
126+
3. Check if session was started before hooks were installed
127+
4. Look for errors in hook scripts
128+
129+
#### Issue: `lastActivityTime` is old but timeline shows recent activity
130+
131+
**Symptoms:**
132+
- Timeline has recent events
133+
- Session state shows old `lastActivityTime`
134+
135+
**Cause:** Activity events aren't updating session state, or transcript polling isn't working
136+
137+
**Check:**
138+
- Are activity events being dispatched to ActivityStore?
139+
- Is transcript polling enabled in App.tsx?
140+
141+
## Debug Tool Reference
142+
143+
### `npm run debug:sessions`
144+
145+
**Purpose:** Dump current session state as JSON
146+
147+
**Options:**
148+
- `--session-id <id>` - View single session
149+
- `--format pretty|compact` - Output formatting (default: pretty)
150+
- `--no-counts` - Exclude session counts
151+
- `--no-stats` - Exclude activity statistics
152+
153+
**Output includes:**
154+
- All session metadata
155+
- Session status and states
156+
- Activity counts and statistics
157+
- Recent activity events
158+
159+
### `npm run debug:timeline`
160+
161+
**Purpose:** Show chronological event timeline
162+
163+
**Options:**
164+
- `--session-id <id>` - Filter by session
165+
- `--cwd <path>` - Filter by project directory
166+
- `--limit <n>` - Number of events to show (default: 50)
167+
- `--format table|json` - Output format (default: table)
168+
169+
**Event types shown:**
170+
- `session_start` - Session began
171+
- `session_end` - Session ended
172+
- `tool_use` - Claude used a tool (shows tool name)
173+
- `prompt_submit` - User submitted input
174+
- `stop` - Claude finished responding
175+
- `notification` - System notification
176+
- `subagent_stop` - Subagent finished
177+
178+
## Advanced Debugging
179+
180+
### Check Raw Event Structure
181+
182+
If you need to see the exact event format:
183+
184+
```bash
185+
# View specific event types
186+
grep "session_start" ~/.agent-tracker/sessions.jsonl | tail -1 | jq .
187+
188+
# View events for a session
189+
grep "SESSION_ID" ~/.agent-tracker/sessions.jsonl | jq .
190+
```
191+
192+
### Test Hook Installation
193+
194+
Create a test session to verify hooks fire:
195+
196+
```bash
197+
# In a separate terminal
198+
cd /tmp && claude "echo test"
199+
```
200+
201+
Check if events appear in `~/.agent-tracker/sessions.jsonl`.
202+
203+
### Monitor in Real-Time
204+
205+
Set up multi-terminal debugging:
206+
207+
**Terminal 1:** Run Agent Tracker TUI
208+
```bash
209+
npm run dev
210+
```
211+
212+
**Terminal 2:** Watch events
213+
```bash
214+
tail -f ~/.agent-tracker/sessions.jsonl
215+
```
216+
217+
**Terminal 3:** Start test Claude session
218+
```bash
219+
claude "list files"
220+
```
221+
222+
You should see events in Terminal 2 and session in Terminal 1 immediately.
223+
224+
## Key Architecture Points
225+
226+
- **Redux-style state**: ActivityStore uses pure reducers
227+
- **Event-driven**: Everything flows from JSONL events
228+
- **Session re-activation**: Activity events re-activate ended sessions
229+
- **Phantom detection**: Based on transcript file size and timestamps
230+
- **Transcript polling**: Fallback for when hooks don't fire
231+
232+
## Quick Fixes
233+
234+
### Hooks not firing
235+
```bash
236+
# Reinstall plugin
237+
claude plugin uninstall agent-tracker
238+
claude plugin install agent-tracker
239+
```
240+
241+
### Session state incorrect
242+
```bash
243+
# Check if ActivityStore reducer is handling events
244+
npm run debug:timeline -- --session-id <ID>
245+
npm run debug:sessions -- --session-id <ID>
246+
# Compare timestamps - should match
247+
```
248+
249+
### Phantom session issue
250+
Check transcript file - if it's actually large and actively used, the phantom detection threshold may need adjustment in ActivityStore.ts:50.
251+
252+
## When to Escalate
253+
254+
If after following this workflow you can't identify the issue:
255+
256+
1. Gather diagnostics:
257+
```bash
258+
npm run debug:sessions -- --format compact > session-state.json
259+
npm run debug:timeline -- --session-id <ID> --format json > timeline.json
260+
```
261+
262+
2. Check for console errors in the TUI
263+
264+
3. Review recent changes to ActivityStore, SessionTrackerService, or event processing
265+
266+
4. File an issue with diagnostics attached

CLAUDE.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
This is a TypeScript CLI TUI written using Ink (React for CLIs).
22

3+
## Quick Start: Debugging Agent Tracker
4+
5+
If you're debugging Agent Tracker issues (sessions not showing, incorrect states, etc.), use the debug skill:
6+
7+
```bash
8+
/debug-agent-tracker
9+
```
10+
11+
This provides a guided workflow with debug commands, common issues, and troubleshooting steps.
12+
313
## Development Guidelines
414

515
As an LLM agent working on this project:
@@ -170,6 +180,42 @@ Both commands will trigger the SessionStart hook and write an event to the JSONL
170180

171181
You should see the session appear in the TUI and events logged in terminal 2.
172182

183+
## Debug Tools Quick Reference
184+
185+
**For detailed debugging workflow and troubleshooting, use the debug skill:**
186+
187+
```bash
188+
/debug-agent-tracker
189+
```
190+
191+
The skill provides step-by-step diagnosis, common issue patterns, and fixes.
192+
193+
### Quick Commands
194+
195+
```bash
196+
# View current session state (what the TUI sees)
197+
npm run debug:sessions -- --session-id <session-id>
198+
199+
# View event timeline for a session
200+
npm run debug:timeline -- --session-id <session-id> --limit 30
201+
202+
# Watch events in real-time
203+
tail -f ~/.agent-tracker/sessions.jsonl
204+
```
205+
206+
### Common Quick Checks
207+
208+
**Session not showing or incorrect state?**
209+
1. Run: `npm run debug:timeline -- --session-id <id>`
210+
2. Look for: `session_end` followed by activity → Session was re-opened
211+
3. Check state: `npm run debug:sessions -- --session-id <id>`
212+
213+
**Key fields to verify:**
214+
- `status`: Should be "active" for running sessions
215+
- `isPhantom`: Should be false for real sessions
216+
- `lastActivityTime`: Should match recent activity
217+
- `endTime`: Should be undefined for active sessions
218+
173219
# Important references
174220

175221
[Claude Hooks](https://docs.claude.com/en/docs/claude-code/hooks.md)

0 commit comments

Comments
 (0)