mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-09-30 12:39:42 +02:00
fix: rock-solid idle detection in respawn controller
- Add 300-char rolling window to catch working patterns split across PTY chunks - Check completion message BEFORE working patterns (priority fix) - Clear rolling window on completion message (transition point) - Increase working pattern absence threshold from 3s to 8s - Add Session.isWorking safety check before confirming idle - Add 20+ more working patterns (Compiling, Building, Processing, etc.) - Make AI idle checker prompt more conservative (err toward WORKING) - Update documentation Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -21,7 +21,7 @@ When user says "COM": 1) Increment version in BOTH `package.json` AND `CLAUDE.md
|
||||
|
||||
Claudeman is a Claude Code session manager with a web interface and autonomous Ralph Loop. It spawns Claude CLI processes via PTY, streams output in real-time via SSE, and supports scheduled/timed runs.
|
||||
|
||||
**Version**: 0.1373 (must match `package.json`)
|
||||
**Version**: 0.1374 (must match `package.json`)
|
||||
|
||||
**Tech Stack**: TypeScript (ES2022/NodeNext, strict mode), Node.js, Fastify, Server-Sent Events, node-pty
|
||||
|
||||
|
||||
@@ -46,10 +46,15 @@ After sending each step (update, clear, init, kickstart), the controller waits f
|
||||
## Idle Detection (Multi-Layer)
|
||||
|
||||
1. **Completion message**: Primary signal - detects "Worked for Xm Xs" time patterns (requires "Worked" prefix to avoid false positives)
|
||||
2. **AI Idle Check** (enabled by default): Spawns a fresh Claude session in a screen to analyze terminal output and provide IDLE/WORKING verdict. Uses `claude-opus-4-5-20251101` by default, sends last 16k chars of terminal buffer. Timeout 90s, cooldown 3min after WORKING. Auto-disables after 3 consecutive errors.
|
||||
2. **AI Idle Check** (enabled by default): Spawns a fresh Claude session in a screen to analyze terminal output and provide IDLE/WORKING verdict. Uses `claude-opus-4-5-20251101` by default, sends last 16k chars of terminal buffer. Timeout 90s, cooldown 3min after WORKING. Auto-disables after 3 consecutive errors. The AI prompt is conservative: when in doubt, it answers WORKING.
|
||||
3. **Output silence**: Confirms idle after `completionConfirmMs` (10s) of no new output
|
||||
4. **Token stability**: Tokens haven't changed
|
||||
5. **Working patterns absent**: No `Thinking`, `Writing`, spinner chars
|
||||
5. **Working patterns absent**: No `Thinking`, `Writing`, spinner chars, etc. for at least 8 seconds
|
||||
6. **Session.isWorking check**: Final safety - if the Session class reports `isWorking=true`, idle confirmation is rejected
|
||||
|
||||
**Working Pattern Detection**:
|
||||
- Uses a rolling 300-character window to catch patterns split across PTY chunks
|
||||
- Patterns include: Thinking, Writing, Reading, Running, Searching, Editing, Creating, Deleting, Analyzing, Executing, Synthesizing, Compiling, Building, Processing, Loading, Generating, Testing, Checking, Validating, and spinner characters
|
||||
|
||||
Uses `confirming_idle` state to prevent false positives. Cancels idle confirmation if substantial output (>2 chars after ANSI stripping) arrives during the wait. Fallback: `noOutputTimeoutMs` (30s) if no output at all. AI check is triggered after the no-output fallback; if AI check is disabled/errored, falls back to direct idle confirmation.
|
||||
|
||||
|
||||
Generated
+336
-313
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "claudeman",
|
||||
"version": "0.1373",
|
||||
"version": "0.1374",
|
||||
"description": "The missing control plane for Claude Code - run 20 autonomous agents with real-time monitoring and session persistence",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
|
||||
@@ -4,3 +4,180 @@
|
||||
|
||||
## Fixes
|
||||
|
||||
|
||||
### Fix for Issue #1
|
||||
**Send/wait state stuck**
|
||||
|
||||
Stuck in waiting_update - the controller is trying to send a command but may not be receiving expected response.
|
||||
|
||||
Check terminal output:
|
||||
```bash
|
||||
curl -sk 'https://localhost:3000/api/sessions/5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98/terminal?tail=10000' | tail -100
|
||||
```
|
||||
|
||||
Try restarting respawn:
|
||||
```bash
|
||||
curl -sk -X POST 'https://localhost:3000/api/sessions/5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98/respawn/stop'
|
||||
curl -sk -X POST 'https://localhost:3000/api/sessions/5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98/respawn/start'
|
||||
```
|
||||
|
||||
|
||||
### Fix for Issue #2
|
||||
**Check for hung session**
|
||||
|
||||
The session may be hung. Check:
|
||||
|
||||
1. Session process status:
|
||||
```bash
|
||||
screen -ls | grep reddit
|
||||
```
|
||||
|
||||
2. Attach to screen and check:
|
||||
```bash
|
||||
screen -r claudeman-w3-reddit-analyse
|
||||
# Press Ctrl+A D to detach
|
||||
```
|
||||
|
||||
3. If hung, restart the session via web UI or:
|
||||
```bash
|
||||
curl -sk -X POST 'https://localhost:3000/api/sessions/5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98/stop'
|
||||
# Then start a new session
|
||||
```
|
||||
|
||||
|
||||
### Fix for Issue #3
|
||||
**Check for hung session**
|
||||
|
||||
The session may be hung. Check:
|
||||
|
||||
1. Session process status:
|
||||
```bash
|
||||
screen -ls | grep reddit
|
||||
```
|
||||
|
||||
2. Attach to screen and check:
|
||||
```bash
|
||||
screen -r claudeman-w3-reddit-analyse
|
||||
# Press Ctrl+A D to detach
|
||||
```
|
||||
|
||||
3. If hung, restart the session via web UI or:
|
||||
```bash
|
||||
curl -sk -X POST 'https://localhost:3000/api/sessions/5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98/stop'
|
||||
# Then start a new session
|
||||
```
|
||||
|
||||
|
||||
### Fix for Issue #4
|
||||
**Watching state stuck**
|
||||
|
||||
The controller is watching but not detecting idle. Possible causes:
|
||||
1. Session is actively working (expected behavior)
|
||||
2. Idle detection thresholds too high
|
||||
|
||||
Check session activity:
|
||||
```bash
|
||||
curl -sk 'https://localhost:3000/api/sessions/5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98' | jq '{status, lastActivityAt}'
|
||||
```
|
||||
|
||||
If truly idle, try adjusting config:
|
||||
```bash
|
||||
curl -sk -X POST 'https://localhost:3000/api/sessions/5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98/respawn/config' -H 'Content-Type: application/json' -d '{"idleThresholdMs": 30000}'
|
||||
```
|
||||
|
||||
|
||||
### Fix for Issue #5
|
||||
**Check for hung session**
|
||||
|
||||
The session may be hung. Check:
|
||||
|
||||
1. Session process status:
|
||||
```bash
|
||||
screen -ls | grep reddit
|
||||
```
|
||||
|
||||
2. Attach to screen and check:
|
||||
```bash
|
||||
screen -r claudeman-w3-reddit-analyse
|
||||
# Press Ctrl+A D to detach
|
||||
```
|
||||
|
||||
3. If hung, restart the session via web UI or:
|
||||
```bash
|
||||
curl -sk -X POST 'https://localhost:3000/api/sessions/5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98/stop'
|
||||
# Then start a new session
|
||||
```
|
||||
|
||||
|
||||
### Fix for Issue #6
|
||||
**Check for hung session**
|
||||
|
||||
The session may be hung. Check:
|
||||
|
||||
1. Session process status:
|
||||
```bash
|
||||
screen -ls | grep reddit
|
||||
```
|
||||
|
||||
2. Attach to screen and check:
|
||||
```bash
|
||||
screen -r claudeman-w3-reddit-analyse
|
||||
# Press Ctrl+A D to detach
|
||||
```
|
||||
|
||||
3. If hung, restart the session via web UI or:
|
||||
```bash
|
||||
curl -sk -X POST 'https://localhost:3000/api/sessions/5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98/stop'
|
||||
# Then start a new session
|
||||
```
|
||||
|
||||
|
||||
### Fix for Issue #7
|
||||
**Enable respawn controller**
|
||||
|
||||
Via API:
|
||||
```bash
|
||||
curl -sk -X POST 'https://localhost:3000/api/sessions/5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98/respawn/enable'
|
||||
```
|
||||
|
||||
Or via web UI: Open session tab -> Respawn panel -> Enable
|
||||
|
||||
|
||||
### Fix for Issue #8
|
||||
**Enable respawn controller**
|
||||
|
||||
Via API:
|
||||
```bash
|
||||
curl -sk -X POST 'https://localhost:3000/api/sessions/5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98/respawn/enable'
|
||||
```
|
||||
|
||||
Or via web UI: Open session tab -> Respawn panel -> Enable
|
||||
|
||||
|
||||
### Fix for Issue #9
|
||||
**Send/wait state stuck**
|
||||
|
||||
Stuck in waiting_update - the controller is trying to send a command but may not be receiving expected response.
|
||||
|
||||
Check terminal output:
|
||||
```bash
|
||||
curl -sk 'https://localhost:3000/api/sessions/5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98/terminal?tail=10000' | tail -100
|
||||
```
|
||||
|
||||
Try restarting respawn:
|
||||
```bash
|
||||
curl -sk -X POST 'https://localhost:3000/api/sessions/5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98/respawn/stop'
|
||||
curl -sk -X POST 'https://localhost:3000/api/sessions/5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98/respawn/start'
|
||||
```
|
||||
|
||||
|
||||
### Fix for Issue #10
|
||||
**Enable respawn controller**
|
||||
|
||||
Via API:
|
||||
```bash
|
||||
curl -sk -X POST 'https://localhost:3000/api/sessions/5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98/respawn/enable'
|
||||
```
|
||||
|
||||
Or via web UI: Open session tab -> Respawn panel -> Enable
|
||||
|
||||
|
||||
@@ -5,3 +5,77 @@ Session ID: 5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98
|
||||
|
||||
## Issues Detected
|
||||
|
||||
|
||||
### Issue #1 [WARNING] - 2026-01-25 22:38:27
|
||||
**State stuck: waiting_update for 10+ minutes**
|
||||
|
||||
The respawn controller has been in 'waiting_update' state for over 10 minutes.
|
||||
Time since activity: 13ms
|
||||
Detection status: {"stopHookReceived":false,"stopHookTime":null,"idlePromptReceived":false,"idlePromptTime":null,"completionMessageDetected":false,"completionMessageTime":null,"outputSilent":false,"msSinceLastOutput":13,"tokensStable":false,"lastTokenCount":5500,"msSinceTokenChange":4246,"workingPatternsAbsent":true,"msSinceLastWorking":7578,"aiCheck":{"status":"ready","lastVerdict":"IDLE","lastReasoning":"The terminal output is completely empty/blank, which indicates the session has no active output being generated. An empty terminal with no spinner characters, no \"Thinking\"/\"Writing\" indicators, and no partial output suggests the session is idle and waiting for input.","lastCheckDurationMs":23354,"cooldownEndsAt":null,"consecutiveErrors":0,"totalChecks":1,"disabledReason":null},"confidenceLevel":15,"statusText":"Respawn step: waiting_update","waitingFor":"Step completion","activeTimers":[{"name":"step-confirm","remainingMs":2422,"totalMs":10000},{"name":"no-output-fallback","remainingMs":29987,"totalMs":30000},{"name":"pre-filter","remainingMs":9987,"totalMs":10000},{"name":"auto-accept","remainingMs":7987,"totalMs":8000}],"recentActions":[{"type":"timer","detail":"Started auto-accept: 8s (plan mode detection)","timestamp":1769377107305},{"type":"timer-cancel","detail":"auto-accept: restarting","timestamp":1769377107305},{"type":"timer","detail":"Started pre-filter: 10s (checking idle conditions)","timestamp":1769377107305},{"type":"timer-cancel","detail":"pre-filter: restarting","timestamp":1769377107305},{"type":"timer","detail":"Started no-output-fallback: 30s (fallback if no output at all)","timestamp":1769377107305},{"type":"timer-cancel","detail":"no-output-fallback: restarting","timestamp":1769377107305},{"type":"timer","detail":"Started auto-accept: 8s (plan mode detection)","timestamp":1769377107234},{"type":"timer-cancel","detail":"auto-accept: restarting","timestamp":1769377107234},{"type":"timer","detail":"Started pre-filter: 10s (checking idle conditions)","timestamp":1769377107234},{"type":"timer-cancel","detail":"pre-filter: restarting","timestamp":1769377107234}],"currentPhase":"Waiting for update to complete","nextAction":"Will send /clear when done"}
|
||||
|
||||
|
||||
### Issue #2 [WARNING] - 2026-01-25 22:47:03
|
||||
**Tokens not increasing while busy**
|
||||
|
||||
Session status is 'busy' but token count hasn't changed in 5+ minutes.
|
||||
Current: 101458, Previous: 101458
|
||||
This could indicate a hung Claude process.
|
||||
|
||||
|
||||
### Issue #3 [WARNING] - 2026-01-25 22:52:09
|
||||
**Tokens not increasing while busy**
|
||||
|
||||
Session status is 'busy' but token count hasn't changed in 5+ minutes.
|
||||
Current: 101458, Previous: 101458
|
||||
This could indicate a hung Claude process.
|
||||
|
||||
|
||||
### Issue #4 [WARNING] - 2026-01-25 22:55:13
|
||||
**State stuck: watching for 10+ minutes**
|
||||
|
||||
The respawn controller has been in 'watching' state for over 10 minutes.
|
||||
Time since activity: 80ms
|
||||
Detection status: {"stopHookReceived":false,"stopHookTime":null,"idlePromptReceived":false,"idlePromptTime":null,"completionMessageDetected":false,"completionMessageTime":null,"outputSilent":false,"msSinceLastOutput":80,"tokensStable":true,"lastTokenCount":53764,"msSinceTokenChange":24105,"workingPatternsAbsent":true,"msSinceLastWorking":24108,"aiCheck":{"status":"ready","lastVerdict":null,"lastReasoning":null,"lastCheckDurationMs":null,"cooldownEndsAt":null,"consecutiveErrors":0,"totalChecks":0,"disabledReason":null},"confidenceLevel":35,"statusText":"Watching for idle signals","waitingFor":"Silence + no working patterns + tokens stable","activeTimers":[{"name":"no-output-fallback","remainingMs":29920,"totalMs":30000},{"name":"pre-filter","remainingMs":9920,"totalMs":10000},{"name":"auto-accept","remainingMs":7920,"totalMs":8000}],"recentActions":[{"type":"timer","detail":"Started auto-accept: 8s (plan mode detection)","timestamp":1769378113225},{"type":"timer-cancel","detail":"auto-accept: restarting","timestamp":1769378113225},{"type":"timer","detail":"Started pre-filter: 10s (checking idle conditions)","timestamp":1769378113225},{"type":"timer-cancel","detail":"pre-filter: restarting","timestamp":1769378113225},{"type":"timer","detail":"Started no-output-fallback: 30s (fallback if no output at all)","timestamp":1769378113225},{"type":"timer-cancel","detail":"no-output-fallback: restarting","timestamp":1769378113225},{"type":"timer","detail":"Started auto-accept: 8s (plan mode detection)","timestamp":1769378113142},{"type":"timer-cancel","detail":"auto-accept: restarting","timestamp":1769378113142},{"type":"timer","detail":"Started pre-filter: 10s (checking idle conditions)","timestamp":1769378113142},{"type":"timer-cancel","detail":"pre-filter: restarting","timestamp":1769378113142}],"currentPhase":"Monitoring for idle","nextAction":"Waiting for silence + no working patterns"}
|
||||
|
||||
|
||||
### Issue #5 [WARNING] - 2026-01-25 22:57:15
|
||||
**Tokens not increasing while busy**
|
||||
|
||||
Session status is 'busy' but token count hasn't changed in 5+ minutes.
|
||||
Current: 101458, Previous: 101458
|
||||
This could indicate a hung Claude process.
|
||||
|
||||
|
||||
### Issue #6 [WARNING] - 2026-01-25 23:36:35
|
||||
**Tokens not increasing while busy**
|
||||
|
||||
Session status is 'busy' but token count hasn't changed in 5+ minutes.
|
||||
Current: 101458, Previous: 101458
|
||||
This could indicate a hung Claude process.
|
||||
|
||||
|
||||
### Issue #7 [INFO] - 2026-01-26 00:01:02
|
||||
**Respawn controller not enabled**
|
||||
|
||||
Respawn is currently disabled for this session. State: false
|
||||
|
||||
|
||||
### Issue #8 [INFO] - 2026-01-26 00:05:33
|
||||
**Respawn controller not enabled**
|
||||
|
||||
Respawn is currently disabled for this session. State: false
|
||||
|
||||
|
||||
### Issue #9 [WARNING] - 2026-01-26 00:31:05
|
||||
**State stuck: waiting_update for 10+ minutes**
|
||||
|
||||
The respawn controller has been in 'waiting_update' state for over 10 minutes.
|
||||
Time since activity: 98ms
|
||||
Detection status: {"stopHookReceived":false,"stopHookTime":null,"idlePromptReceived":false,"idlePromptTime":null,"completionMessageDetected":false,"completionMessageTime":null,"outputSilent":false,"msSinceLastOutput":98,"tokensStable":true,"lastTokenCount":6400,"msSinceTokenChange":332384,"workingPatternsAbsent":true,"msSinceLastWorking":340635,"aiCheck":{"status":"ready","lastVerdict":"IDLE","lastReasoning":"The terminal output is empty, which indicates the session is at rest with no active processing occurring. There are no spinner characters, \"Thinking\"/\"Writing\" indicators, or any signs of active work. An empty terminal buffer typically means the session is waiting for input.","lastCheckDurationMs":24908,"cooldownEndsAt":null,"consecutiveErrors":0,"totalChecks":1,"disabledReason":null},"confidenceLevel":35,"statusText":"Respawn step: waiting_update","waitingFor":"Step completion","activeTimers":[{"name":"step-confirm","remainingMs":9372,"totalMs":10000},{"name":"no-output-fallback","remainingMs":29902,"totalMs":30000},{"name":"pre-filter","remainingMs":9902,"totalMs":10000},{"name":"auto-accept","remainingMs":7902,"totalMs":8000}],"recentActions":[{"type":"timer","detail":"Started auto-accept: 8s (plan mode detection)","timestamp":1769383865244},{"type":"timer-cancel","detail":"auto-accept: restarting","timestamp":1769383865244},{"type":"timer","detail":"Started pre-filter: 10s (checking idle conditions)","timestamp":1769383865244},{"type":"timer-cancel","detail":"pre-filter: restarting","timestamp":1769383865244},{"type":"timer","detail":"Started no-output-fallback: 30s (fallback if no output at all)","timestamp":1769383865244},{"type":"timer-cancel","detail":"no-output-fallback: restarting","timestamp":1769383865244},{"type":"timer","detail":"Started auto-accept: 8s (plan mode detection)","timestamp":1769383865102},{"type":"timer-cancel","detail":"auto-accept: restarting","timestamp":1769383865102},{"type":"timer","detail":"Started pre-filter: 10s (checking idle conditions)","timestamp":1769383865102},{"type":"timer-cancel","detail":"pre-filter: restarting","timestamp":1769383865102}],"currentPhase":"Waiting for update to complete","nextAction":"Will send /clear when done"}
|
||||
|
||||
|
||||
### Issue #10 [INFO] - 2026-01-26 00:41:17
|
||||
**Respawn controller not enabled**
|
||||
|
||||
Respawn is currently disabled for this session. State: false
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
# Respawn Controller Fixes - w3-reddit-analyse
|
||||
|
||||
**Session ID:** 5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98
|
||||
**Generated:** 2026-01-25
|
||||
|
||||
---
|
||||
|
||||
## Proposed Fixes
|
||||
|
||||
_Fixes will be added here based on observed issues. DO NOT EXECUTE until reviewed._
|
||||
|
||||
---
|
||||
|
||||
|
||||
### Issue: Respawn disabled
|
||||
**Detected:** 2026-01-26 00:01:00
|
||||
|
||||
**Proposed Fix:**
|
||||
Re-enable respawn controller:\n```bash\ncurl -sk -X POST https://localhost:3000/api/sessions/5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98/respawn/enable\n```
|
||||
|
||||
---
|
||||
|
||||
### Issue: Respawn disabled
|
||||
**Detected:** 2026-01-26 00:06:02
|
||||
|
||||
**Proposed Fix:**
|
||||
Re-enable respawn controller:\n```bash\ncurl -sk -X POST https://localhost:3000/api/sessions/5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98/respawn/enable\n```
|
||||
|
||||
---
|
||||
|
||||
### Issue: Respawn disabled
|
||||
**Detected:** 2026-01-26 00:41:19
|
||||
|
||||
**Proposed Fix:**
|
||||
Re-enable respawn controller:\n```bash\ncurl -sk -X POST https://localhost:3000/api/sessions/5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98/respawn/enable\n```
|
||||
|
||||
---
|
||||
@@ -0,0 +1,26 @@
|
||||
# Respawn Controller Monitoring - w3-reddit-analyse
|
||||
|
||||
**Session ID:** 5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98
|
||||
**Started:** 2026-01-25 22:44 CET
|
||||
**Duration:** 12 hours (ends ~2026-01-26 10:44 CET)
|
||||
|
||||
---
|
||||
|
||||
## Issues Log
|
||||
|
||||
_Monitoring in progress. Issues will be appended below._
|
||||
|
||||
---
|
||||
|
||||
|
||||
### [WARNING] 2026-01-26 00:01:00
|
||||
Respawn appears to have been disabled (was in state: watching)
|
||||
|
||||
|
||||
### [WARNING] 2026-01-26 00:06:02
|
||||
Respawn appears to have been disabled (was in state: watching)
|
||||
|
||||
|
||||
### [WARNING] 2026-01-26 00:41:19
|
||||
Respawn appears to have been disabled (was in state: waiting_update)
|
||||
|
||||
Executable
+17
@@ -0,0 +1,17 @@
|
||||
#!/bin/bash
|
||||
# Generates timestamped data to a file for testing
|
||||
# Usage: ./scripts/data-generator.sh [output_file] [interval_seconds]
|
||||
# Then: tail -f /tmp/test-data.log
|
||||
|
||||
OUTPUT="${1:-/tmp/test-data.log}"
|
||||
INTERVAL="${2:-1}"
|
||||
|
||||
echo "Writing to $OUTPUT every ${INTERVAL}s (Ctrl+C to stop)"
|
||||
echo "Run: tail -f $OUTPUT"
|
||||
|
||||
i=0
|
||||
while true; do
|
||||
((i++))
|
||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Line $i - Random: $RANDOM" >> "$OUTPUT"
|
||||
sleep "$INTERVAL"
|
||||
done
|
||||
Executable
+124
@@ -0,0 +1,124 @@
|
||||
#!/bin/bash
|
||||
# Respawn Controller Monitor - 12 hour monitoring script
|
||||
# Usage: ./scripts/monitor-respawn.sh <session_id> [duration_hours]
|
||||
|
||||
SESSION_ID="${1:-5ecdf859-3605-42d8-b0a2-f8f0a1b5ff98}"
|
||||
DURATION_HOURS="${2:-12}"
|
||||
ISSUES_FILE="/home/arkon/default/claudeman/respawn-monitor-issues.md"
|
||||
FIXES_FILE="/home/arkon/default/claudeman/respawn-monitor-fixes.md"
|
||||
API_URL="https://localhost:3000"
|
||||
POLL_INTERVAL=60 # seconds
|
||||
|
||||
# Calculate end time
|
||||
END_TIME=$(($(date +%s) + DURATION_HOURS * 3600))
|
||||
|
||||
log_issue() {
|
||||
local severity="$1"
|
||||
local message="$2"
|
||||
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
echo "" >> "$ISSUES_FILE"
|
||||
echo "### [$severity] $timestamp" >> "$ISSUES_FILE"
|
||||
echo "$message" >> "$ISSUES_FILE"
|
||||
echo "" >> "$ISSUES_FILE"
|
||||
}
|
||||
|
||||
add_fix() {
|
||||
local issue="$1"
|
||||
local fix="$2"
|
||||
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
echo "" >> "$FIXES_FILE"
|
||||
echo "### Issue: $issue" >> "$FIXES_FILE"
|
||||
echo "**Detected:** $timestamp" >> "$FIXES_FILE"
|
||||
echo "" >> "$FIXES_FILE"
|
||||
echo "**Proposed Fix:**" >> "$FIXES_FILE"
|
||||
echo "$fix" >> "$FIXES_FILE"
|
||||
echo "" >> "$FIXES_FILE"
|
||||
echo "---" >> "$FIXES_FILE"
|
||||
}
|
||||
|
||||
# State tracking
|
||||
PREV_STATE=""
|
||||
STATE_STUCK_COUNT=0
|
||||
PREV_CYCLE=0
|
||||
LAST_ACTIVITY=0
|
||||
AI_CHECK_ERRORS=0
|
||||
|
||||
echo "Starting respawn monitoring for session $SESSION_ID"
|
||||
echo "Duration: $DURATION_HOURS hours"
|
||||
echo "Issues logged to: $ISSUES_FILE"
|
||||
echo "Fixes logged to: $FIXES_FILE"
|
||||
|
||||
while [ $(date +%s) -lt $END_TIME ]; do
|
||||
# Get respawn status
|
||||
RESP=$(curl -sk "$API_URL/api/sessions/$SESSION_ID" 2>/dev/null)
|
||||
|
||||
if [ -z "$RESP" ]; then
|
||||
log_issue "ERROR" "API not responding - server may be down"
|
||||
add_fix "API not responding" "Check if claudeman-web service is running:\n\`\`\`bash\nsystemctl --user status claudeman-web\nsystemctl --user restart claudeman-web\n\`\`\`"
|
||||
sleep $POLL_INTERVAL
|
||||
continue
|
||||
fi
|
||||
|
||||
# Parse response
|
||||
STATE=$(echo "$RESP" | jq -r '.respawn.state // "null"')
|
||||
CYCLE=$(echo "$RESP" | jq -r '.respawn.cycleCount // 0')
|
||||
ENABLED=$(echo "$RESP" | jq -r '.respawnEnabled // false')
|
||||
TOKENS=$(echo "$RESP" | jq -r '.respawn.config.lastTokenCount // 0')
|
||||
AI_STATUS=$(echo "$RESP" | jq -r '.respawn.detection.aiCheck.status // "unknown"')
|
||||
AI_ERRORS=$(echo "$RESP" | jq -r '.respawn.detection.aiCheck.consecutiveErrors // 0')
|
||||
TIME_SINCE_ACTIVITY=$(echo "$RESP" | jq -r '.respawn.timeSinceActivity // 0')
|
||||
CONFIDENCE=$(echo "$RESP" | jq -r '.respawn.detection.confidenceLevel // 0')
|
||||
SESSION_STATUS=$(echo "$RESP" | jq -r '.status // "unknown"')
|
||||
|
||||
# Check for issues
|
||||
|
||||
# 1. Session not running
|
||||
if [ "$SESSION_STATUS" = "stopped" ] || [ "$SESSION_STATUS" = "error" ]; then
|
||||
log_issue "CRITICAL" "Session status is '$SESSION_STATUS' - respawn may not function correctly"
|
||||
add_fix "Session stopped/error" "Check session health and restart if needed:\n\`\`\`bash\ncurl -sk $API_URL/api/sessions/$SESSION_ID\n# If needed, create new session or restart\n\`\`\`"
|
||||
fi
|
||||
|
||||
# 2. Respawn disabled unexpectedly
|
||||
if [ "$ENABLED" = "false" ] && [ "$PREV_STATE" != "" ] && [ "$PREV_STATE" != "null" ]; then
|
||||
log_issue "WARNING" "Respawn appears to have been disabled (was in state: $PREV_STATE)"
|
||||
add_fix "Respawn disabled" "Re-enable respawn controller:\n\`\`\`bash\ncurl -sk -X POST $API_URL/api/sessions/$SESSION_ID/respawn/enable\n\`\`\`"
|
||||
fi
|
||||
|
||||
# 3. State stuck for too long (>30 minutes in same state)
|
||||
if [ "$STATE" = "$PREV_STATE" ] && [ "$STATE" != "watching" ]; then
|
||||
STATE_STUCK_COUNT=$((STATE_STUCK_COUNT + 1))
|
||||
if [ $STATE_STUCK_COUNT -ge 30 ]; then # 30 minutes
|
||||
log_issue "WARNING" "State stuck in '$STATE' for $STATE_STUCK_COUNT minutes. Detection: confidence=$CONFIDENCE, timeSinceActivity=${TIME_SINCE_ACTIVITY}ms"
|
||||
add_fix "State stuck in $STATE" "The respawn controller may be stuck. Options:\n1. Check terminal for blocking prompts\n2. Send input to unstick:\n\`\`\`bash\ncurl -sk -X POST $API_URL/api/sessions/$SESSION_ID/input -d '{\"input\": \"\\\\r\"}'\n\`\`\`\n3. Restart respawn:\n\`\`\`bash\ncurl -sk -X POST $API_URL/api/sessions/$SESSION_ID/respawn/stop\ncurl -sk -X POST $API_URL/api/sessions/$SESSION_ID/respawn/start\n\`\`\`"
|
||||
STATE_STUCK_COUNT=0 # Reset to avoid spam
|
||||
fi
|
||||
else
|
||||
STATE_STUCK_COUNT=0
|
||||
fi
|
||||
|
||||
# 4. AI check errors
|
||||
if [ "$AI_ERRORS" -gt 2 ] && [ "$AI_ERRORS" -gt "$AI_CHECK_ERRORS" ]; then
|
||||
log_issue "WARNING" "AI idle check has $AI_ERRORS consecutive errors (status: $AI_STATUS)"
|
||||
add_fix "AI check errors" "AI idle checker is failing. Check:\n1. Claude CLI is working\n2. Sufficient context available\n3. Consider disabling AI check temporarily:\n\`\`\`bash\ncurl -sk -X POST $API_URL/api/sessions/$SESSION_ID/respawn/config -H 'Content-Type: application/json' -d '{\"aiIdleCheckEnabled\": false}'\n\`\`\`"
|
||||
AI_CHECK_ERRORS=$AI_ERRORS
|
||||
fi
|
||||
|
||||
# 5. Cycle completed - log success
|
||||
if [ "$CYCLE" -gt "$PREV_CYCLE" ] && [ "$PREV_CYCLE" -gt 0 ]; then
|
||||
log_issue "INFO" "Respawn cycle $CYCLE completed successfully (tokens: $TOKENS)"
|
||||
fi
|
||||
|
||||
# 6. Very high time since activity (>10 minutes) while not in watching state
|
||||
if [ "$TIME_SINCE_ACTIVITY" -gt 600000 ] && [ "$STATE" != "watching" ]; then
|
||||
log_issue "WARNING" "No activity for ${TIME_SINCE_ACTIVITY}ms while in state '$STATE' - may be stuck"
|
||||
fi
|
||||
|
||||
# Update tracking vars
|
||||
PREV_STATE="$STATE"
|
||||
PREV_CYCLE="$CYCLE"
|
||||
|
||||
sleep $POLL_INTERVAL
|
||||
done
|
||||
|
||||
log_issue "INFO" "Monitoring completed after $DURATION_HOURS hours"
|
||||
echo "Monitoring complete. Check $ISSUES_FILE and $FIXES_FILE"
|
||||
@@ -0,0 +1,166 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Browser test for clickable file links in Claudeman terminal
|
||||
* Uses Playwright directly to ignore HTTPS errors
|
||||
*/
|
||||
|
||||
import { chromium } from 'playwright';
|
||||
|
||||
const BASE_URL = 'https://localhost:3000';
|
||||
const TEST_LOG_FILE = '/tmp/test-link-click.log';
|
||||
|
||||
async function main() {
|
||||
console.log('Starting browser test for clickable links...\n');
|
||||
|
||||
// Create test log file
|
||||
const { writeFileSync, appendFileSync } = await import('fs');
|
||||
writeFileSync(TEST_LOG_FILE, `=== Test started at ${new Date().toISOString()} ===\n`);
|
||||
for (let i = 1; i <= 10; i++) {
|
||||
appendFileSync(TEST_LOG_FILE, `Entry #${i} - ${Date.now()}\n`);
|
||||
}
|
||||
console.log(`Created test file: ${TEST_LOG_FILE}\n`);
|
||||
|
||||
// Launch browser ignoring HTTPS errors
|
||||
const browser = await chromium.launch({
|
||||
headless: true,
|
||||
args: ['--ignore-certificate-errors', '--ignore-ssl-errors']
|
||||
});
|
||||
|
||||
const context = await browser.newContext({
|
||||
ignoreHTTPSErrors: true
|
||||
});
|
||||
|
||||
const page = await context.newPage();
|
||||
|
||||
try {
|
||||
// Navigate to Claudeman
|
||||
console.log('Navigating to Claudeman...');
|
||||
await page.goto(BASE_URL, { waitUntil: 'networkidle', timeout: 30000 });
|
||||
console.log('Page loaded: ' + await page.title());
|
||||
|
||||
// Wait for terminal to be ready
|
||||
await page.waitForSelector('.xterm-screen', { timeout: 10000 });
|
||||
console.log('Terminal found\n');
|
||||
|
||||
// Get list of sessions
|
||||
const sessions = await page.evaluate(async () => {
|
||||
const res = await fetch('/api/sessions');
|
||||
return res.json();
|
||||
});
|
||||
console.log(`Found ${sessions.length} session(s)`);
|
||||
|
||||
if (sessions.length === 0) {
|
||||
console.log('No sessions found. Creating a shell session...');
|
||||
const newSession = await page.evaluate(async () => {
|
||||
const res = await fetch('/api/quick-start', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ caseName: 'link-test', mode: 'shell' })
|
||||
});
|
||||
return res.json();
|
||||
});
|
||||
console.log('Created session:', newSession.session?.id);
|
||||
await page.waitForTimeout(2000);
|
||||
}
|
||||
|
||||
// Get active session (use claudeman session if exists)
|
||||
const claudemanSession = sessions.find(s => s.workingDir?.includes('claudeman'));
|
||||
const sessionId = claudemanSession?.id || sessions[0]?.id;
|
||||
console.log(`Using session: ${sessionId}\n`);
|
||||
|
||||
// Send a command that outputs file paths
|
||||
console.log('Sending command with file path...');
|
||||
await page.evaluate(async (sid, logFile) => {
|
||||
await fetch(`/api/sessions/${sid}/input`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ input: `echo "Monitor: tail -f ${logFile}"\r` })
|
||||
});
|
||||
}, sessionId, TEST_LOG_FILE);
|
||||
|
||||
await page.waitForTimeout(2000);
|
||||
|
||||
// Take screenshot
|
||||
await page.screenshot({ path: '/tmp/claudeman-link-test-1.png' });
|
||||
console.log('Screenshot saved: /tmp/claudeman-link-test-1.png\n');
|
||||
|
||||
// Check if terminal contains our file path
|
||||
const terminalContent = await page.evaluate(() => {
|
||||
const screen = document.querySelector('.xterm-screen');
|
||||
return screen?.textContent || '';
|
||||
});
|
||||
|
||||
if (terminalContent.includes(TEST_LOG_FILE)) {
|
||||
console.log('File path appears in terminal output');
|
||||
} else {
|
||||
console.log('WARNING: File path not found in terminal. Content preview:');
|
||||
console.log(terminalContent.substring(0, 500));
|
||||
}
|
||||
|
||||
// Try to find clickable links in xterm
|
||||
const linkCount = await page.evaluate(() => {
|
||||
// xterm link detection happens on mouse move, so we need to trigger it
|
||||
const links = document.querySelectorAll('.xterm-link');
|
||||
return links.length;
|
||||
});
|
||||
console.log(`\nClickable links found: ${linkCount}`);
|
||||
|
||||
// Try hovering over the terminal to trigger link detection
|
||||
console.log('\nHovering over terminal to trigger link detection...');
|
||||
const termRect = await page.locator('.xterm-screen').boundingBox();
|
||||
if (termRect) {
|
||||
// Move mouse across the terminal
|
||||
for (let y = termRect.y + 50; y < termRect.y + termRect.height - 50; y += 20) {
|
||||
for (let x = termRect.x + 50; x < termRect.x + termRect.width - 50; x += 50) {
|
||||
await page.mouse.move(x, y);
|
||||
await page.waitForTimeout(50);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for tooltip (shows when hovering over link)
|
||||
const tooltipVisible = await page.evaluate(() => {
|
||||
const tooltip = document.getElementById('file-link-tooltip');
|
||||
return tooltip?.style.display !== 'none' && tooltip?.style.display !== '';
|
||||
});
|
||||
console.log('Tooltip visible:', tooltipVisible);
|
||||
|
||||
// Check for log viewer windows
|
||||
const logViewerCount = await page.evaluate(() => {
|
||||
return document.querySelectorAll('.log-viewer-window').length;
|
||||
});
|
||||
console.log('Log viewer windows:', logViewerCount);
|
||||
|
||||
// Take final screenshot
|
||||
await page.screenshot({ path: '/tmp/claudeman-link-test-2.png' });
|
||||
console.log('\nFinal screenshot: /tmp/claudeman-link-test-2.png');
|
||||
|
||||
// Test the link provider registration
|
||||
console.log('\n--- Link Provider Debug ---');
|
||||
const debugInfo = await page.evaluate(() => {
|
||||
// Check if app has link provider registered
|
||||
const app = window.app;
|
||||
if (!app) return { error: 'No app object found' };
|
||||
|
||||
const terminal = app.terminals?.get(app.activeSessionId);
|
||||
if (!terminal) return { error: 'No active terminal', activeSessionId: app.activeSessionId };
|
||||
|
||||
return {
|
||||
hasTerminal: true,
|
||||
activeSessionId: app.activeSessionId,
|
||||
terminalBuffer: terminal.buffer?.active?.length || 0
|
||||
};
|
||||
});
|
||||
console.log('Debug info:', debugInfo);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error:', error.message);
|
||||
await page.screenshot({ path: '/tmp/claudeman-link-test-error.png' });
|
||||
console.log('Error screenshot saved to /tmp/claudeman-link-test-error.png');
|
||||
} finally {
|
||||
await browser.close();
|
||||
console.log('\nBrowser closed');
|
||||
}
|
||||
}
|
||||
|
||||
main().catch(console.error);
|
||||
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Test script for link detection patterns
|
||||
*/
|
||||
|
||||
const patterns = {
|
||||
cmdPattern: /(tail|cat|head|less|grep|watch|multitail)\s+(?:[^\/\n]+?\s+)?(\/[^\s"'<>|;&\n\)]+)/g,
|
||||
bashPathPattern: /Bash\([^)]*?(\/(?:var|tmp|home|usr|etc|opt|log|logs)[^\s"'<>|;&\)\n]+)/g,
|
||||
generalPathPattern: /(\/(?:home|tmp|var|etc|usr|opt)[^\s"'<>|;&\n\)\]]+\.(?:log|txt|json|md|ts|js|sh|py|yaml|yml|csv|xml|html|css))/g,
|
||||
logPathPattern: /(\/var\/log\/[^\s"'<>|;&\n\)\]]+)/g,
|
||||
claudeOutputPattern: /(?:Created|Writing|Saved|File|Output|Streaming|Monitoring|Log)(?:\s+(?:file|to|at))?[:\s]+([\/~][^\s"'<>|;&\n\)]+)/gi
|
||||
};
|
||||
|
||||
const tests = [
|
||||
'tail -f /tmp/test.log',
|
||||
'tail -n 100 /var/log/syslog',
|
||||
'cat /home/user/file.txt',
|
||||
'head -n 10 /etc/passwd',
|
||||
'Bash(tail -f /tmp/test.log)',
|
||||
'Created file: /tmp/newfile.log',
|
||||
'Writing to /home/user/output.txt',
|
||||
'Monitoring file: /var/log/syslog',
|
||||
'Streaming /home/user/logs/app.log',
|
||||
'/home/arkon/project/file.log',
|
||||
'/tmp/test-output.txt',
|
||||
'echo "tail -f /tmp/test.log"',
|
||||
'The output is in /home/user/results.csv',
|
||||
'plain text without paths'
|
||||
];
|
||||
|
||||
console.log('Link Pattern Test Results\n');
|
||||
|
||||
for (const [name, pattern] of Object.entries(patterns)) {
|
||||
console.log(`\n=== ${name} ===`);
|
||||
console.log(`Pattern: ${pattern.source}\n`);
|
||||
|
||||
for (const test of tests) {
|
||||
pattern.lastIndex = 0;
|
||||
const matches = [];
|
||||
let m;
|
||||
while ((m = pattern.exec(test)) !== null) {
|
||||
// Get the captured group (varies by pattern)
|
||||
matches.push(m[1] || m[2]);
|
||||
}
|
||||
if (matches.length > 0) {
|
||||
console.log(` ✓ "${test}"`);
|
||||
console.log(` → Found: ${matches.join(', ')}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log('\n\n=== Combined Test ===');
|
||||
console.log('Testing all patterns on each input:\n');
|
||||
|
||||
for (const test of tests) {
|
||||
const allMatches = [];
|
||||
for (const [name, pattern] of Object.entries(patterns)) {
|
||||
pattern.lastIndex = 0;
|
||||
let m;
|
||||
while ((m = pattern.exec(test)) !== null) {
|
||||
const path = m[1] || m[2];
|
||||
if (path && !allMatches.includes(path)) {
|
||||
allMatches.push(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (allMatches.length > 0) {
|
||||
console.log(`✓ "${test}"`);
|
||||
console.log(` → ${allMatches.join(', ')}`);
|
||||
} else {
|
||||
console.log(`✗ "${test}" - no match`);
|
||||
}
|
||||
}
|
||||
Executable
+30
@@ -0,0 +1,30 @@
|
||||
#!/bin/bash
|
||||
# Test script for clickable file links in Claudeman terminal
|
||||
# Creates a log file and continuously writes timestamped data
|
||||
|
||||
LOGFILE="/tmp/claudeman-test-$(date +%s).log"
|
||||
|
||||
echo "Created log file: $LOGFILE"
|
||||
echo ""
|
||||
echo "You can monitor this file with:"
|
||||
echo " tail -f $LOGFILE"
|
||||
echo ""
|
||||
echo "Starting to write data every 2 seconds..."
|
||||
echo ""
|
||||
|
||||
# Write initial content
|
||||
echo "=== Test Log Started at $(date) ===" > "$LOGFILE"
|
||||
|
||||
# Write data continuously
|
||||
count=0
|
||||
while true; do
|
||||
count=$((count + 1))
|
||||
echo "[$(date '+%H:%M:%S')] Entry #$count - Random: $RANDOM" >> "$LOGFILE"
|
||||
|
||||
# Print status every 5 entries
|
||||
if [ $((count % 5)) -eq 0 ]; then
|
||||
echo "Wrote $count entries to $LOGFILE"
|
||||
fi
|
||||
|
||||
sleep 2
|
||||
done
|
||||
+17
-3
@@ -73,8 +73,22 @@ const VERDICT_PATTERN = /^\s*(IDLE|WORKING)\b/i;
|
||||
/** The prompt sent to the AI checker */
|
||||
const AI_CHECK_PROMPT = `Analyze this terminal output from a running Claude Code session. Determine if the session is IDLE (done working, waiting for new input) or WORKING (still actively processing).
|
||||
|
||||
IDLE indicators: prompt character at the end, completion summary shown ("Worked for Xm Xs"), clear stopping point, no spinners, cost summary displayed
|
||||
WORKING indicators: spinner chars, "Thinking"/"Writing"/"Reading"/"Running" text, active tool execution, truncated mid-output, partial lines
|
||||
IMPORTANT: When in doubt, answer WORKING. Brief pauses between tool executions do NOT mean the session is idle. Claude may be processing or about to output more.
|
||||
|
||||
IDLE indicators (need MULTIPLE of these to confirm idle):
|
||||
- Completion summary shown (e.g., "✻ Worked for 2m 46s", "Worked for 5s")
|
||||
- Prompt character visible at the end (❯ or similar)
|
||||
- Cost summary displayed (e.g., "$0.12 spent")
|
||||
- Clear end of output with no pending work
|
||||
|
||||
WORKING indicators (ANY of these means WORKING):
|
||||
- Spinner characters (⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏ or similar)
|
||||
- Activity text: Thinking, Writing, Reading, Running, Searching, Editing, Creating, Deleting, Analyzing, Executing, Synthesizing, Compiling, Building, Processing, Loading, Generating, Testing, Checking, Validating
|
||||
- Tool execution in progress (commands being run)
|
||||
- Truncated or partial lines at the end
|
||||
- File operations in progress
|
||||
- Output that appears mid-stream or incomplete
|
||||
- No completion summary visible yet
|
||||
|
||||
Terminal output (most recent at bottom):
|
||||
---
|
||||
@@ -82,7 +96,7 @@ Terminal output (most recent at bottom):
|
||||
---
|
||||
|
||||
Answer with EXACTLY one word on the first line: IDLE or WORKING
|
||||
Then optionally explain briefly why.`;
|
||||
If uncertain, answer WORKING. Then briefly explain why.`;
|
||||
|
||||
// ========== AiIdleChecker Class ==========
|
||||
|
||||
|
||||
@@ -0,0 +1,459 @@
|
||||
/**
|
||||
* @fileoverview Bash Tool Parser - Detects active Bash tool commands with file paths
|
||||
*
|
||||
* This module parses terminal output from Claude Code sessions to detect:
|
||||
* - Bash tool invocations (● Bash(command) pattern)
|
||||
* - File paths within commands (for tail, cat, head, grep, watch, less)
|
||||
* - Tool completion (✓ or ✗ status)
|
||||
*
|
||||
* When a file-viewing command is detected, emits events with clickable paths
|
||||
* that can be used to open live log viewer windows.
|
||||
*
|
||||
* @module bash-tool-parser
|
||||
*/
|
||||
|
||||
import { EventEmitter } from 'node:events';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { ActiveBashTool } from './types.js';
|
||||
|
||||
// ========== Configuration Constants ==========
|
||||
|
||||
/**
|
||||
* Maximum number of active tools to track per session.
|
||||
* Older tools are removed when this limit is reached.
|
||||
*/
|
||||
const MAX_ACTIVE_TOOLS = 20;
|
||||
|
||||
/**
|
||||
* Debounce interval for event emissions (milliseconds).
|
||||
* Prevents UI jitter from rapid consecutive updates.
|
||||
*/
|
||||
const EVENT_DEBOUNCE_MS = 50;
|
||||
|
||||
/**
|
||||
* Maximum line buffer size to prevent unbounded growth from long lines.
|
||||
*/
|
||||
const MAX_LINE_BUFFER_SIZE = 64 * 1024;
|
||||
|
||||
// ========== Pre-compiled Regex Patterns ==========
|
||||
|
||||
/**
|
||||
* Matches Bash tool invocation line from Claude Code output.
|
||||
* Pattern: ● Bash(command) or ● Bash(command) timeout: 5m 0s
|
||||
* The tool name can appear with or without the bullet point.
|
||||
*
|
||||
* Capture groups:
|
||||
* - 1: The command being executed
|
||||
* - 2: Optional timeout string
|
||||
*/
|
||||
const BASH_TOOL_START_PATTERN = /(?:^|\s)●?\s*Bash\((.+?)\)(?:\s+timeout:\s*([^\n]+))?/;
|
||||
|
||||
/**
|
||||
* Matches tool completion indicators.
|
||||
* ✓ indicates success, ✗ indicates failure.
|
||||
*/
|
||||
const TOOL_COMPLETION_PATTERN = /(?:✓|✗)\s+Bash/;
|
||||
|
||||
/**
|
||||
* Commands that view/stream file content (worth tracking for live viewing).
|
||||
* These are the commands where clicking to open a log viewer makes sense.
|
||||
*/
|
||||
const FILE_VIEWER_COMMANDS = /^(?:tail|cat|head|less|grep|watch|multitail)\s+/;
|
||||
|
||||
/**
|
||||
* Alternative: Commands with -f flag (follow mode) are especially interesting
|
||||
*/
|
||||
const FOLLOW_MODE_PATTERN = /\s-[A-Za-z]*f[A-Za-z]*\s|\s--follow\s/;
|
||||
|
||||
/**
|
||||
* Extracts file paths from a command string.
|
||||
* Matches paths starting with / or ~ followed by path characters.
|
||||
* Excludes common non-path patterns like flags.
|
||||
*
|
||||
* Note: This is a simpler approach - we run it on each command string
|
||||
* rather than trying to match globally.
|
||||
*/
|
||||
const FILE_PATH_PATTERN = /(?:^|\s|['"]|=)([\/~][^\s'"<>|;&\n]+)/g;
|
||||
|
||||
/**
|
||||
* Pattern to detect paths that are likely not real files (flags, etc.)
|
||||
*/
|
||||
const INVALID_PATH_PATTERN = /^[\/~]-|\/dev\/null$/;
|
||||
|
||||
/**
|
||||
* Pattern to detect command suggestions in plain text output.
|
||||
* Matches lines like "tail -f /path/to/file" without the ● Bash() wrapper.
|
||||
* This catches commands Claude mentions but doesn't execute.
|
||||
*/
|
||||
const TEXT_COMMAND_PATTERN = /^\s*(tail|cat|head|less|grep|watch|multitail)\s+(?:-[^\s]+\s+)*([\/~][^\s'"<>|;&\n]+)/;
|
||||
|
||||
/**
|
||||
* Pattern to detect log file paths mentioned in text (even without commands).
|
||||
* Matches paths ending in .log, .txt, .out, or in common log directories.
|
||||
*/
|
||||
const LOG_FILE_MENTION_PATTERN = /([\/~][^\s'"<>|;&\n]*(?:\.log|\.txt|\.out|\/log\/[^\s'"<>|;&\n]+))/g;
|
||||
|
||||
// ========== Event Interfaces ==========
|
||||
|
||||
/**
|
||||
* Events emitted by BashToolParser.
|
||||
*/
|
||||
export interface BashToolParserEvents {
|
||||
/** New Bash tool with file paths started */
|
||||
toolStart: [tool: ActiveBashTool];
|
||||
/** Bash tool completed */
|
||||
toolEnd: [tool: ActiveBashTool];
|
||||
/** Active tools list updated */
|
||||
toolsUpdate: [tools: ActiveBashTool[]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuration options for BashToolParser.
|
||||
*/
|
||||
export interface BashToolParserConfig {
|
||||
/** Session ID this parser belongs to */
|
||||
sessionId: string;
|
||||
/** Whether the parser is enabled (default: true) */
|
||||
enabled?: boolean;
|
||||
}
|
||||
|
||||
// ========== BashToolParser Class ==========
|
||||
|
||||
/**
|
||||
* Parses Claude Code terminal output to detect Bash tool commands with file paths.
|
||||
* Emits events when file-viewing commands are detected, allowing the UI to
|
||||
* display clickable paths for opening live log viewers.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const parser = new BashToolParser({ sessionId: 'abc123' });
|
||||
* parser.on('toolStart', (tool) => {
|
||||
* console.log(`New tool: ${tool.command}`);
|
||||
* console.log(`File paths: ${tool.filePaths.join(', ')}`);
|
||||
* });
|
||||
* parser.processTerminalData(terminalOutput);
|
||||
* ```
|
||||
*/
|
||||
export class BashToolParser extends EventEmitter<BashToolParserEvents> {
|
||||
private _sessionId: string;
|
||||
private _enabled: boolean;
|
||||
private _activeTools: Map<string, ActiveBashTool> = new Map();
|
||||
private _lineBuffer: string = '';
|
||||
private _lastToolId: string | null = null;
|
||||
|
||||
// Debouncing
|
||||
private _pendingUpdate: boolean = false;
|
||||
private _updateTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
constructor(config: BashToolParserConfig) {
|
||||
super();
|
||||
this._sessionId = config.sessionId;
|
||||
this._enabled = config.enabled ?? true;
|
||||
}
|
||||
|
||||
// ========== Public Accessors ==========
|
||||
|
||||
/** Whether the parser is currently enabled */
|
||||
get enabled(): boolean {
|
||||
return this._enabled;
|
||||
}
|
||||
|
||||
/** Session ID this parser belongs to */
|
||||
get sessionId(): string {
|
||||
return this._sessionId;
|
||||
}
|
||||
|
||||
/** Currently active tools */
|
||||
get activeTools(): ActiveBashTool[] {
|
||||
return Array.from(this._activeTools.values());
|
||||
}
|
||||
|
||||
// ========== Public Methods ==========
|
||||
|
||||
/**
|
||||
* Enables the parser.
|
||||
*/
|
||||
enable(): void {
|
||||
this._enabled = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables the parser.
|
||||
*/
|
||||
disable(): void {
|
||||
this._enabled = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the parser state, clearing all tracked tools.
|
||||
*/
|
||||
reset(): void {
|
||||
this._activeTools.clear();
|
||||
this._lineBuffer = '';
|
||||
this._lastToolId = null;
|
||||
this.emitUpdate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Process terminal data to detect Bash tool patterns.
|
||||
* Call this with each chunk of PTY output.
|
||||
*
|
||||
* @param data - Raw terminal data (may include ANSI codes)
|
||||
*/
|
||||
processTerminalData(data: string): void {
|
||||
if (!this._enabled) return;
|
||||
|
||||
// Append to line buffer
|
||||
this._lineBuffer += data;
|
||||
|
||||
// Prevent unbounded growth
|
||||
if (this._lineBuffer.length > MAX_LINE_BUFFER_SIZE) {
|
||||
const trimPoint = this._lineBuffer.lastIndexOf('\n', MAX_LINE_BUFFER_SIZE / 2);
|
||||
this._lineBuffer = trimPoint > 0
|
||||
? this._lineBuffer.slice(trimPoint + 1)
|
||||
: this._lineBuffer.slice(-MAX_LINE_BUFFER_SIZE / 2);
|
||||
}
|
||||
|
||||
// Process complete lines
|
||||
const lines = this._lineBuffer.split('\n');
|
||||
|
||||
// Keep the last incomplete line in buffer
|
||||
this._lineBuffer = lines.pop() || '';
|
||||
|
||||
for (const line of lines) {
|
||||
this.processLine(line);
|
||||
}
|
||||
}
|
||||
|
||||
// ========== Private Methods ==========
|
||||
|
||||
/**
|
||||
* Process a single line of terminal output.
|
||||
*/
|
||||
private processLine(line: string): void {
|
||||
// Strip ANSI codes for cleaner pattern matching
|
||||
const cleanLine = this.stripAnsi(line);
|
||||
|
||||
// Check for tool start
|
||||
const startMatch = cleanLine.match(BASH_TOOL_START_PATTERN);
|
||||
if (startMatch) {
|
||||
const command = startMatch[1];
|
||||
const timeout = startMatch[2]?.trim();
|
||||
|
||||
// Check if this is a file-viewing command
|
||||
if (this.isFileViewerCommand(command)) {
|
||||
const filePaths = this.extractFilePaths(command);
|
||||
|
||||
if (filePaths.length > 0) {
|
||||
const tool: ActiveBashTool = {
|
||||
id: uuidv4(),
|
||||
command,
|
||||
filePaths,
|
||||
timeout,
|
||||
startedAt: Date.now(),
|
||||
status: 'running',
|
||||
sessionId: this._sessionId,
|
||||
};
|
||||
|
||||
// Enforce max tools limit
|
||||
if (this._activeTools.size >= MAX_ACTIVE_TOOLS) {
|
||||
// Remove oldest tool
|
||||
const oldest = Array.from(this._activeTools.entries())
|
||||
.sort((a, b) => a[1].startedAt - b[1].startedAt)[0];
|
||||
if (oldest) {
|
||||
this._activeTools.delete(oldest[0]);
|
||||
}
|
||||
}
|
||||
|
||||
this._activeTools.set(tool.id, tool);
|
||||
this._lastToolId = tool.id;
|
||||
|
||||
this.emit('toolStart', tool);
|
||||
this.scheduleUpdate();
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for tool completion
|
||||
if (TOOL_COMPLETION_PATTERN.test(cleanLine) && this._lastToolId) {
|
||||
const tool = this._activeTools.get(this._lastToolId);
|
||||
if (tool && tool.status === 'running') {
|
||||
tool.status = 'completed';
|
||||
this.emit('toolEnd', tool);
|
||||
this.scheduleUpdate();
|
||||
|
||||
// Remove completed tool after a short delay to allow UI to show completion
|
||||
setTimeout(() => {
|
||||
this._activeTools.delete(tool.id);
|
||||
this.scheduleUpdate();
|
||||
}, 2000);
|
||||
}
|
||||
this._lastToolId = null;
|
||||
return;
|
||||
}
|
||||
|
||||
// Fallback: Check for command suggestions in plain text (e.g., "tail -f /tmp/file.log")
|
||||
const textCmdMatch = cleanLine.match(TEXT_COMMAND_PATTERN);
|
||||
if (textCmdMatch) {
|
||||
const filePath = textCmdMatch[2];
|
||||
|
||||
// Create a suggestion tool (marked as 'suggestion' status)
|
||||
const tool: ActiveBashTool = {
|
||||
id: uuidv4(),
|
||||
command: cleanLine.trim(),
|
||||
filePaths: [filePath],
|
||||
timeout: undefined,
|
||||
startedAt: Date.now(),
|
||||
status: 'running', // Shows as clickable
|
||||
sessionId: this._sessionId,
|
||||
};
|
||||
|
||||
// Don't add duplicates (same file path within last 5 seconds)
|
||||
const isDuplicate = Array.from(this._activeTools.values()).some(
|
||||
t => t.filePaths.includes(filePath) && (Date.now() - t.startedAt) < 5000
|
||||
);
|
||||
|
||||
if (!isDuplicate) {
|
||||
this._activeTools.set(tool.id, tool);
|
||||
this.emit('toolStart', tool);
|
||||
this.scheduleUpdate();
|
||||
|
||||
// Auto-remove suggestions after 30 seconds
|
||||
setTimeout(() => {
|
||||
this._activeTools.delete(tool.id);
|
||||
this.scheduleUpdate();
|
||||
}, 30000);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Last fallback: Check for log file paths mentioned anywhere in the line
|
||||
LOG_FILE_MENTION_PATTERN.lastIndex = 0;
|
||||
let logMatch;
|
||||
while ((logMatch = LOG_FILE_MENTION_PATTERN.exec(cleanLine)) !== null) {
|
||||
const filePath = logMatch[1].replace(/[,;:]+$/, ''); // Clean trailing punctuation
|
||||
|
||||
// Skip if it looks invalid
|
||||
if (INVALID_PATH_PATTERN.test(filePath)) continue;
|
||||
|
||||
// Don't add duplicates
|
||||
const isDuplicate = Array.from(this._activeTools.values()).some(
|
||||
t => t.filePaths.includes(filePath) && (Date.now() - t.startedAt) < 10000
|
||||
);
|
||||
|
||||
if (!isDuplicate) {
|
||||
const tool: ActiveBashTool = {
|
||||
id: uuidv4(),
|
||||
command: `View: ${filePath}`,
|
||||
filePaths: [filePath],
|
||||
timeout: undefined,
|
||||
startedAt: Date.now(),
|
||||
status: 'running',
|
||||
sessionId: this._sessionId,
|
||||
};
|
||||
|
||||
this._activeTools.set(tool.id, tool);
|
||||
this.emit('toolStart', tool);
|
||||
this.scheduleUpdate();
|
||||
|
||||
// Auto-remove after 60 seconds
|
||||
setTimeout(() => {
|
||||
this._activeTools.delete(tool.id);
|
||||
this.scheduleUpdate();
|
||||
}, 60000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a command is a file-viewing command worth tracking.
|
||||
*/
|
||||
private isFileViewerCommand(command: string): boolean {
|
||||
// Commands that typically view files
|
||||
if (FILE_VIEWER_COMMANDS.test(command)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Any command with -f (follow) flag is interesting
|
||||
if (FOLLOW_MODE_PATTERN.test(` ${command} `)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract file paths from a command string.
|
||||
*/
|
||||
private extractFilePaths(command: string): string[] {
|
||||
const paths: string[] = [];
|
||||
let match;
|
||||
|
||||
// Reset regex state
|
||||
FILE_PATH_PATTERN.lastIndex = 0;
|
||||
|
||||
while ((match = FILE_PATH_PATTERN.exec(command)) !== null) {
|
||||
const path = match[1];
|
||||
|
||||
// Skip invalid paths
|
||||
if (INVALID_PATH_PATTERN.test(path)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip if it looks like a flag (starts with -)
|
||||
if (path.includes('/-')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Clean up path (remove trailing punctuation)
|
||||
const cleanPath = path.replace(/[,;:]+$/, '');
|
||||
|
||||
if (cleanPath && !paths.includes(cleanPath)) {
|
||||
paths.push(cleanPath);
|
||||
}
|
||||
}
|
||||
|
||||
return paths;
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip ANSI escape codes from a string.
|
||||
*/
|
||||
private stripAnsi(str: string): string {
|
||||
// Comprehensive ANSI pattern
|
||||
return str.replace(/\x1b(?:\[[0-9;?]*[A-Za-z]|\][^\x07\x1b]*(?:\x07|\x1b\\)|[=>])/g, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule a debounced update emission.
|
||||
*/
|
||||
private scheduleUpdate(): void {
|
||||
if (this._pendingUpdate) return;
|
||||
|
||||
this._pendingUpdate = true;
|
||||
this._updateTimer = setTimeout(() => {
|
||||
this._pendingUpdate = false;
|
||||
this.emitUpdate();
|
||||
}, EVENT_DEBOUNCE_MS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Emit the current active tools list.
|
||||
*/
|
||||
private emitUpdate(): void {
|
||||
this.emit('toolsUpdate', this.activeTools);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up resources.
|
||||
*/
|
||||
destroy(): void {
|
||||
if (this._updateTimer) {
|
||||
clearTimeout(this._updateTimer);
|
||||
this._updateTimer = null;
|
||||
}
|
||||
this._activeTools.clear();
|
||||
this.removeAllListeners();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,426 @@
|
||||
/**
|
||||
* @fileoverview File Stream Manager - Manages tail -f processes for live log viewing
|
||||
*
|
||||
* This module spawns and manages `tail -f` processes for streaming live file
|
||||
* content to the frontend. It handles:
|
||||
* - Spawning tail processes with initial history
|
||||
* - Streaming output via callbacks
|
||||
* - Cleanup when streams are closed
|
||||
* - Security validation (path within working directory)
|
||||
*
|
||||
* @module file-stream-manager
|
||||
*/
|
||||
|
||||
import { spawn, ChildProcess } from 'node:child_process';
|
||||
import { existsSync, statSync } from 'node:fs';
|
||||
import { resolve, relative, isAbsolute } from 'node:path';
|
||||
import { homedir } from 'node:os';
|
||||
import { EventEmitter } from 'node:events';
|
||||
|
||||
// ========== Configuration Constants ==========
|
||||
|
||||
/**
|
||||
* Default number of historical lines to show when opening a file.
|
||||
*/
|
||||
const DEFAULT_TAIL_LINES = 50;
|
||||
|
||||
/**
|
||||
* Maximum file size to stream (100MB).
|
||||
* Skip files larger than this to prevent memory issues.
|
||||
*/
|
||||
const MAX_FILE_SIZE = 100 * 1024 * 1024;
|
||||
|
||||
/**
|
||||
* Maximum concurrent streams per session.
|
||||
*/
|
||||
const MAX_STREAMS_PER_SESSION = 5;
|
||||
|
||||
/**
|
||||
* Inactivity timeout for streams (5 minutes).
|
||||
* Streams with no data for this long will be auto-closed.
|
||||
*/
|
||||
const STREAM_INACTIVITY_TIMEOUT_MS = 5 * 60 * 1000;
|
||||
|
||||
// ========== Types ==========
|
||||
|
||||
/**
|
||||
* Represents an active file stream.
|
||||
*/
|
||||
export interface FileStream {
|
||||
/** Unique stream identifier */
|
||||
id: string;
|
||||
/** Session this stream belongs to */
|
||||
sessionId: string;
|
||||
/** Absolute path to the file being streamed */
|
||||
filePath: string;
|
||||
/** Tail process handle */
|
||||
process: ChildProcess;
|
||||
/** Timestamp when stream was created */
|
||||
createdAt: number;
|
||||
/** Timestamp of last data received */
|
||||
lastDataAt: number;
|
||||
/** Whether the stream is still active */
|
||||
active: boolean;
|
||||
/** Callback for sending data to client */
|
||||
onData: (data: string) => void;
|
||||
/** Callback for stream end */
|
||||
onEnd: () => void;
|
||||
/** Callback for errors */
|
||||
onError: (error: string) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Options for creating a file stream.
|
||||
*/
|
||||
export interface CreateStreamOptions {
|
||||
/** Session ID requesting the stream */
|
||||
sessionId: string;
|
||||
/** Path to the file to stream */
|
||||
filePath: string;
|
||||
/** Working directory for path validation */
|
||||
workingDir: string;
|
||||
/** Number of historical lines to show (default: 50) */
|
||||
lines?: number;
|
||||
/** Callback for data */
|
||||
onData: (data: string) => void;
|
||||
/** Callback for stream end */
|
||||
onEnd: () => void;
|
||||
/** Callback for errors */
|
||||
onError: (error: string) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Result of creating a stream.
|
||||
*/
|
||||
export interface CreateStreamResult {
|
||||
success: boolean;
|
||||
streamId?: string;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
// ========== FileStreamManager Class ==========
|
||||
|
||||
/**
|
||||
* Manages file streaming via tail -f processes.
|
||||
* Ensures security by validating paths and limiting concurrent streams.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const manager = new FileStreamManager();
|
||||
*
|
||||
* const result = await manager.createStream({
|
||||
* sessionId: 'session-123',
|
||||
* filePath: '/var/log/app.log',
|
||||
* workingDir: '/var/log',
|
||||
* onData: (data) => sseClient.write(data),
|
||||
* onEnd: () => sseClient.close(),
|
||||
* onError: (err) => console.error(err),
|
||||
* });
|
||||
*
|
||||
* // Later, to stop:
|
||||
* manager.closeStream(result.streamId);
|
||||
* ```
|
||||
*/
|
||||
export class FileStreamManager extends EventEmitter {
|
||||
private streams: Map<string, FileStream> = new Map();
|
||||
private sessionStreamCounts: Map<string, number> = new Map();
|
||||
private cleanupTimer: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
// Start cleanup timer for inactive streams
|
||||
this.cleanupTimer = setInterval(() => this.cleanupInactiveStreams(), 60 * 1000);
|
||||
}
|
||||
|
||||
// ========== Public Methods ==========
|
||||
|
||||
/**
|
||||
* Create a new file stream.
|
||||
*
|
||||
* @param options - Stream configuration
|
||||
* @returns Result with stream ID on success, error on failure
|
||||
*/
|
||||
async createStream(options: CreateStreamOptions): Promise<CreateStreamResult> {
|
||||
const { sessionId, filePath, workingDir, lines = DEFAULT_TAIL_LINES, onData, onEnd, onError } = options;
|
||||
|
||||
// Check concurrent stream limit for this session
|
||||
const currentCount = this.sessionStreamCounts.get(sessionId) || 0;
|
||||
if (currentCount >= MAX_STREAMS_PER_SESSION) {
|
||||
return {
|
||||
success: false,
|
||||
error: `Maximum ${MAX_STREAMS_PER_SESSION} concurrent streams per session`,
|
||||
};
|
||||
}
|
||||
|
||||
// Resolve and validate path
|
||||
const validationResult = this.validatePath(filePath, workingDir);
|
||||
if (!validationResult.valid) {
|
||||
return { success: false, error: validationResult.error };
|
||||
}
|
||||
|
||||
const absolutePath = validationResult.absolutePath!;
|
||||
|
||||
// Check file exists and size
|
||||
try {
|
||||
const stats = statSync(absolutePath);
|
||||
if (stats.size > MAX_FILE_SIZE) {
|
||||
return {
|
||||
success: false,
|
||||
error: `File too large (${Math.round(stats.size / 1024 / 1024)}MB > ${MAX_FILE_SIZE / 1024 / 1024}MB limit)`,
|
||||
};
|
||||
}
|
||||
} catch {
|
||||
return { success: false, error: 'File not found or not accessible' };
|
||||
}
|
||||
|
||||
// Generate stream ID
|
||||
const streamId = `${sessionId}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
||||
|
||||
// Spawn tail process
|
||||
const tailProcess = spawn('tail', ['-f', '-n', String(lines), absolutePath], {
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
});
|
||||
|
||||
if (!tailProcess.pid) {
|
||||
return { success: false, error: 'Failed to spawn tail process' };
|
||||
}
|
||||
|
||||
const stream: FileStream = {
|
||||
id: streamId,
|
||||
sessionId,
|
||||
filePath: absolutePath,
|
||||
process: tailProcess,
|
||||
createdAt: Date.now(),
|
||||
lastDataAt: Date.now(),
|
||||
active: true,
|
||||
onData,
|
||||
onEnd,
|
||||
onError,
|
||||
};
|
||||
|
||||
// Handle stdout
|
||||
tailProcess.stdout?.on('data', (data: Buffer) => {
|
||||
if (stream.active) {
|
||||
stream.lastDataAt = Date.now();
|
||||
onData(data.toString());
|
||||
}
|
||||
});
|
||||
|
||||
// Handle stderr
|
||||
tailProcess.stderr?.on('data', (data: Buffer) => {
|
||||
if (stream.active) {
|
||||
onError(data.toString());
|
||||
}
|
||||
});
|
||||
|
||||
// Handle process exit
|
||||
tailProcess.on('exit', (_code) => {
|
||||
if (stream.active) {
|
||||
stream.active = false;
|
||||
onEnd();
|
||||
this.removeStream(streamId);
|
||||
}
|
||||
});
|
||||
|
||||
// Handle errors
|
||||
tailProcess.on('error', (err) => {
|
||||
if (stream.active) {
|
||||
stream.active = false;
|
||||
onError(err.message);
|
||||
this.removeStream(streamId);
|
||||
}
|
||||
});
|
||||
|
||||
// Store stream
|
||||
this.streams.set(streamId, stream);
|
||||
this.sessionStreamCounts.set(sessionId, currentCount + 1);
|
||||
|
||||
return { success: true, streamId };
|
||||
}
|
||||
|
||||
/**
|
||||
* Close a file stream.
|
||||
*
|
||||
* @param streamId - ID of the stream to close
|
||||
* @returns true if stream was closed, false if not found
|
||||
*/
|
||||
closeStream(streamId: string): boolean {
|
||||
const stream = this.streams.get(streamId);
|
||||
if (!stream) return false;
|
||||
|
||||
stream.active = false;
|
||||
|
||||
// Kill the tail process
|
||||
try {
|
||||
stream.process.kill('SIGTERM');
|
||||
// Force kill after 1 second if still running
|
||||
setTimeout(() => {
|
||||
try {
|
||||
stream.process.kill('SIGKILL');
|
||||
} catch {
|
||||
// Already dead
|
||||
}
|
||||
}, 1000);
|
||||
} catch {
|
||||
// Process may have already exited
|
||||
}
|
||||
|
||||
stream.onEnd();
|
||||
this.removeStream(streamId);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close all streams for a session.
|
||||
*
|
||||
* @param sessionId - Session ID whose streams to close
|
||||
* @returns Number of streams closed
|
||||
*/
|
||||
closeSessionStreams(sessionId: string): number {
|
||||
let closed = 0;
|
||||
for (const [streamId, stream] of this.streams) {
|
||||
if (stream.sessionId === sessionId) {
|
||||
this.closeStream(streamId);
|
||||
closed++;
|
||||
}
|
||||
}
|
||||
return closed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all active streams for a session.
|
||||
*
|
||||
* @param sessionId - Session ID to query
|
||||
* @returns Array of stream info
|
||||
*/
|
||||
getSessionStreams(sessionId: string): Array<{ id: string; filePath: string; createdAt: number }> {
|
||||
const result: Array<{ id: string; filePath: string; createdAt: number }> = [];
|
||||
for (const [, stream] of this.streams) {
|
||||
if (stream.sessionId === sessionId && stream.active) {
|
||||
result.push({
|
||||
id: stream.id,
|
||||
filePath: stream.filePath,
|
||||
createdAt: stream.createdAt,
|
||||
});
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get count of active streams.
|
||||
*/
|
||||
get activeStreamCount(): number {
|
||||
return this.streams.size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up and destroy the manager.
|
||||
*/
|
||||
destroy(): void {
|
||||
// Stop cleanup timer
|
||||
if (this.cleanupTimer) {
|
||||
clearInterval(this.cleanupTimer);
|
||||
this.cleanupTimer = null;
|
||||
}
|
||||
|
||||
// Close all streams
|
||||
for (const streamId of this.streams.keys()) {
|
||||
this.closeStream(streamId);
|
||||
}
|
||||
}
|
||||
|
||||
// ========== Private Methods ==========
|
||||
|
||||
/**
|
||||
* Validate that a file path is safe to access.
|
||||
* Must be within the working directory (no path traversal).
|
||||
*/
|
||||
private validatePath(
|
||||
filePath: string,
|
||||
workingDir: string
|
||||
): { valid: boolean; absolutePath?: string; error?: string } {
|
||||
// Expand ~ to home directory
|
||||
let expandedPath = filePath;
|
||||
if (expandedPath.startsWith('~')) {
|
||||
expandedPath = expandedPath.replace(/^~/, homedir());
|
||||
}
|
||||
|
||||
// Resolve to absolute path
|
||||
const absolutePath = isAbsolute(expandedPath)
|
||||
? resolve(expandedPath)
|
||||
: resolve(workingDir, expandedPath);
|
||||
|
||||
// Normalize the working directory
|
||||
const normalizedWorkingDir = resolve(workingDir);
|
||||
|
||||
// Check if the resolved path is within the working directory
|
||||
// or common log directories
|
||||
const allowedPaths = [
|
||||
normalizedWorkingDir,
|
||||
'/var/log',
|
||||
'/tmp',
|
||||
resolve(homedir(), '.local/share'),
|
||||
resolve(homedir(), '.cache'),
|
||||
resolve(homedir(), 'logs'),
|
||||
];
|
||||
|
||||
const isAllowed = allowedPaths.some((allowed) => {
|
||||
const rel = relative(allowed, absolutePath);
|
||||
return rel && !rel.startsWith('..') && !isAbsolute(rel);
|
||||
});
|
||||
|
||||
if (!isAllowed) {
|
||||
return {
|
||||
valid: false,
|
||||
error: `Path must be within working directory or allowed log directories`,
|
||||
};
|
||||
}
|
||||
|
||||
// Check for path traversal attempts
|
||||
if (absolutePath.includes('..')) {
|
||||
return { valid: false, error: 'Path traversal not allowed' };
|
||||
}
|
||||
|
||||
// Check file exists
|
||||
if (!existsSync(absolutePath)) {
|
||||
return { valid: false, error: 'File does not exist' };
|
||||
}
|
||||
|
||||
return { valid: true, absolutePath };
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a stream from tracking.
|
||||
*/
|
||||
private removeStream(streamId: string): void {
|
||||
const stream = this.streams.get(streamId);
|
||||
if (stream) {
|
||||
// Decrement session count
|
||||
const count = this.sessionStreamCounts.get(stream.sessionId) || 0;
|
||||
if (count <= 1) {
|
||||
this.sessionStreamCounts.delete(stream.sessionId);
|
||||
} else {
|
||||
this.sessionStreamCounts.set(stream.sessionId, count - 1);
|
||||
}
|
||||
}
|
||||
this.streams.delete(streamId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up streams that have been inactive for too long.
|
||||
*/
|
||||
private cleanupInactiveStreams(): void {
|
||||
const now = Date.now();
|
||||
for (const [streamId, stream] of this.streams) {
|
||||
if (now - stream.lastDataAt > STREAM_INACTIVITY_TIMEOUT_MS) {
|
||||
console.log(`[FileStreamManager] Closing inactive stream: ${streamId}`);
|
||||
this.closeStream(streamId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Export singleton instance
|
||||
export const fileStreamManager = new FileStreamManager();
|
||||
+91
-35
@@ -679,9 +679,28 @@ export class RespawnController extends EventEmitter {
|
||||
'Thinking', 'Writing', 'Reading', 'Running', 'Searching',
|
||||
'Editing', 'Creating', 'Deleting', 'Analyzing', 'Executing',
|
||||
'Synthesizing', 'Brewing', // Claude's processing indicators
|
||||
'Compiling', 'Building', 'Installing', 'Fetching', 'Downloading',
|
||||
'Processing', 'Generating', 'Loading', 'Starting', 'Updating',
|
||||
'Checking', 'Validating', 'Testing', 'Formatting', 'Linting',
|
||||
'⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏', // Spinner chars
|
||||
'◐', '◓', '◑', '◒', // Alternative spinners
|
||||
'⣾', '⣽', '⣻', '⢿', '⡿', '⣟', '⣯', '⣷', // Braille spinners
|
||||
];
|
||||
|
||||
/**
|
||||
* Rolling window buffer for working pattern detection.
|
||||
* Prevents split-chunk issues where "Thinking" arrives as "Thin" + "king".
|
||||
* Size: 300 chars should be enough to catch any split pattern.
|
||||
*/
|
||||
private workingPatternWindow: string = '';
|
||||
private static readonly WORKING_PATTERN_WINDOW_SIZE = 300;
|
||||
|
||||
/**
|
||||
* Minimum time without working patterns before considering idle (ms).
|
||||
* Increased from 3s to 8s to avoid false positives during tool execution gaps.
|
||||
*/
|
||||
private static readonly MIN_WORKING_PATTERN_ABSENCE_MS = 8000;
|
||||
|
||||
/**
|
||||
* Creates a new RespawnController.
|
||||
*
|
||||
@@ -821,7 +840,7 @@ export class RespawnController extends EventEmitter {
|
||||
const completionMessageDetected = this.completionMessageTime !== null;
|
||||
const outputSilent = msSinceLastOutput >= this.config.completionConfirmMs;
|
||||
const tokensStable = msSinceTokenChange >= this.config.completionConfirmMs;
|
||||
const workingPatternsAbsent = msSinceLastWorking >= 3000; // 3s without working patterns
|
||||
const workingPatternsAbsent = msSinceLastWorking >= RespawnController.MIN_WORKING_PATTERN_ABSENCE_MS;
|
||||
|
||||
// Calculate confidence level (0-100)
|
||||
// Hook signals are definitive (100% confidence)
|
||||
@@ -1176,6 +1195,42 @@ export class RespawnController extends EventEmitter {
|
||||
this.lastTokenChangeTime = now;
|
||||
}
|
||||
|
||||
// Detect completion message FIRST (Layer 1) - PRIMARY DETECTION
|
||||
// Check this before working patterns because completion message indicates
|
||||
// the work is done, even if working patterns are still in the rolling window
|
||||
if (this.isCompletionMessage(data)) {
|
||||
// Clear the rolling window - completion marks a transition point
|
||||
this.clearWorkingPatternWindow();
|
||||
this.workingDetected = false;
|
||||
this.completionMessageTime = now;
|
||||
this.cancelAutoAcceptTimer(); // Normal idle flow handles this
|
||||
this.log(`Completion message detected: "${data.trim().substring(0, 50)}..."`);
|
||||
|
||||
// In watching state, start completion confirmation timer
|
||||
if (this._state === 'watching') {
|
||||
this.startCompletionConfirmTimer();
|
||||
return;
|
||||
}
|
||||
|
||||
// In waiting states, also use confirmation timer (same detection logic)
|
||||
// This ensures we wait for Claude to finish before proceeding
|
||||
switch (this._state) {
|
||||
case 'waiting_update':
|
||||
this.startStepConfirmTimer('update');
|
||||
break;
|
||||
case 'waiting_clear':
|
||||
this.checkClearComplete(); // /clear is quick, no need to wait
|
||||
break;
|
||||
case 'waiting_init':
|
||||
this.startStepConfirmTimer('init');
|
||||
break;
|
||||
case 'waiting_kickstart':
|
||||
this.startStepConfirmTimer('kickstart');
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Detect working patterns (Layer 4)
|
||||
const isWorking = this.hasWorkingPattern(data);
|
||||
if (isWorking) {
|
||||
@@ -1218,38 +1273,6 @@ export class RespawnController extends EventEmitter {
|
||||
return;
|
||||
}
|
||||
|
||||
// Detect completion message (Layer 1) - PRIMARY DETECTION
|
||||
if (this.isCompletionMessage(data)) {
|
||||
this.completionMessageTime = now;
|
||||
this.workingDetected = false;
|
||||
this.cancelAutoAcceptTimer(); // Normal idle flow handles this
|
||||
this.log(`Completion message detected: "${data.trim().substring(0, 50)}..."`);
|
||||
|
||||
// In watching state, start completion confirmation timer
|
||||
if (this._state === 'watching') {
|
||||
this.startCompletionConfirmTimer();
|
||||
return;
|
||||
}
|
||||
|
||||
// In waiting states, also use confirmation timer (same detection logic)
|
||||
// This ensures we wait for Claude to finish before proceeding
|
||||
switch (this._state) {
|
||||
case 'waiting_update':
|
||||
this.startStepConfirmTimer('update');
|
||||
break;
|
||||
case 'waiting_clear':
|
||||
this.checkClearComplete(); // /clear is quick, no need to wait
|
||||
break;
|
||||
case 'waiting_init':
|
||||
this.startStepConfirmTimer('init');
|
||||
break;
|
||||
case 'waiting_kickstart':
|
||||
this.startStepConfirmTimer('kickstart');
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// In confirming_idle or ai_checking state, substantial output cancels the flow.
|
||||
// This prevents false triggers when Claude pauses briefly mid-work.
|
||||
if (this._state === 'confirming_idle' || this._state === 'ai_checking') {
|
||||
@@ -1363,6 +1386,7 @@ export class RespawnController extends EventEmitter {
|
||||
private startMonitoringInit(): void {
|
||||
this.setState('monitoring_init');
|
||||
this.terminalBuffer.clear();
|
||||
this.clearWorkingPatternWindow();
|
||||
this.workingDetected = false;
|
||||
this.logAction('step', 'Monitoring if /init triggered work...');
|
||||
|
||||
@@ -1404,6 +1428,7 @@ export class RespawnController extends EventEmitter {
|
||||
private sendKickstart(): void {
|
||||
this.setState('sending_kickstart');
|
||||
this.terminalBuffer.clear();
|
||||
this.clearWorkingPatternWindow();
|
||||
|
||||
this.stepTimer = this.startTrackedTimer(
|
||||
'step-delay',
|
||||
@@ -1567,9 +1592,25 @@ export class RespawnController extends EventEmitter {
|
||||
|
||||
/**
|
||||
* Check if data contains working patterns.
|
||||
* Uses rolling window to catch patterns split across chunks (e.g., "Thin" + "king").
|
||||
*/
|
||||
private hasWorkingPattern(data: string): boolean {
|
||||
return this.WORKING_PATTERNS.some(pattern => data.includes(pattern));
|
||||
// Always update the rolling window first to maintain continuity
|
||||
this.workingPatternWindow += data;
|
||||
if (this.workingPatternWindow.length > RespawnController.WORKING_PATTERN_WINDOW_SIZE) {
|
||||
this.workingPatternWindow = this.workingPatternWindow.slice(-RespawnController.WORKING_PATTERN_WINDOW_SIZE);
|
||||
}
|
||||
|
||||
// Check the rolling window (includes current data, catches both complete and split patterns)
|
||||
return this.WORKING_PATTERNS.some(pattern => this.workingPatternWindow.includes(pattern));
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the working pattern rolling window.
|
||||
* Called when starting a new detection cycle.
|
||||
*/
|
||||
private clearWorkingPatternWindow(): void {
|
||||
this.workingPatternWindow = '';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1653,7 +1694,7 @@ export class RespawnController extends EventEmitter {
|
||||
|
||||
// Check pre-filter conditions
|
||||
const silenceMet = msSinceOutput >= this.config.completionConfirmMs;
|
||||
const noWorkingMet = msSinceWorking >= 3000;
|
||||
const noWorkingMet = msSinceWorking >= RespawnController.MIN_WORKING_PATTERN_ABSENCE_MS;
|
||||
const tokensStableMet = msSinceTokenChange >= this.config.completionConfirmMs;
|
||||
|
||||
if (silenceMet && noWorkingMet && tokensStableMet) {
|
||||
@@ -2250,6 +2291,17 @@ export class RespawnController extends EventEmitter {
|
||||
* @param reason - What triggered the confirmation
|
||||
*/
|
||||
private onIdleConfirmed(reason: string): void {
|
||||
// Safety check: if Session thinks it's still working, don't trigger idle
|
||||
// This catches cases where our detection missed working patterns
|
||||
if (this.session.isWorking) {
|
||||
this.log(`Idle confirmation rejected - Session reports isWorking=true (reason was: ${reason})`);
|
||||
this.logAction('detection', 'Rejected: Session still working');
|
||||
this.setState('watching');
|
||||
this.startNoOutputTimer();
|
||||
this.startPreFilterTimer();
|
||||
return;
|
||||
}
|
||||
|
||||
this.log(`Idle confirmed via: ${reason}`);
|
||||
const status = this.getDetectionStatus();
|
||||
this.log(`Detection status: confidence=${status.confidenceLevel}%, ` +
|
||||
@@ -2292,6 +2344,7 @@ export class RespawnController extends EventEmitter {
|
||||
private sendUpdateDocs(): void {
|
||||
this.setState('sending_update');
|
||||
this.terminalBuffer.clear(); // Clear buffer for fresh detection
|
||||
this.clearWorkingPatternWindow(); // Clear rolling window
|
||||
|
||||
this.stepTimer = this.startTrackedTimer(
|
||||
'step-delay',
|
||||
@@ -2319,6 +2372,7 @@ export class RespawnController extends EventEmitter {
|
||||
private sendClear(): void {
|
||||
this.setState('sending_clear');
|
||||
this.terminalBuffer.clear();
|
||||
this.clearWorkingPatternWindow();
|
||||
|
||||
this.stepTimer = this.startTrackedTimer(
|
||||
'step-delay',
|
||||
@@ -2361,6 +2415,7 @@ export class RespawnController extends EventEmitter {
|
||||
private sendInit(): void {
|
||||
this.setState('sending_init');
|
||||
this.terminalBuffer.clear();
|
||||
this.clearWorkingPatternWindow();
|
||||
|
||||
this.stepTimer = this.startTrackedTimer(
|
||||
'step-delay',
|
||||
@@ -2390,6 +2445,7 @@ export class RespawnController extends EventEmitter {
|
||||
// Go back to watching state for next cycle
|
||||
this.setState('watching');
|
||||
this.terminalBuffer.clear();
|
||||
this.clearWorkingPatternWindow(); // Clear rolling window for fresh detection
|
||||
this.promptDetected = false;
|
||||
this.workingDetected = false;
|
||||
this.resetHookState(); // Clear hook signals for next cycle
|
||||
|
||||
+49
-2
@@ -21,13 +21,14 @@ import { existsSync } from 'node:fs';
|
||||
import { dirname } from 'node:path';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import * as pty from 'node-pty';
|
||||
import { SessionState, SessionStatus, SessionConfig, ScreenSession, RalphTrackerState, RalphTodoItem } from './types.js';
|
||||
import { SessionState, SessionStatus, SessionConfig, ScreenSession, RalphTrackerState, RalphTodoItem, ActiveBashTool } from './types.js';
|
||||
import { TaskTracker, type BackgroundTask } from './task-tracker.js';
|
||||
import { RalphTracker } from './ralph-tracker.js';
|
||||
import { BashToolParser } from './bash-tool-parser.js';
|
||||
import { ScreenManager } from './screen-manager.js';
|
||||
|
||||
export type { BackgroundTask } from './task-tracker.js';
|
||||
export type { RalphTrackerState, RalphTodoItem } from './types.js';
|
||||
export type { RalphTrackerState, RalphTodoItem, ActiveBashTool } from './types.js';
|
||||
export { withTimeout };
|
||||
|
||||
// ============================================================================
|
||||
@@ -292,6 +293,12 @@ export interface SessionEvents {
|
||||
ralphTodoUpdate: (todos: RalphTodoItem[]) => void;
|
||||
/** Ralph completion phrase detected */
|
||||
ralphCompletionDetected: (phrase: string) => void;
|
||||
/** Bash tool with file paths started */
|
||||
bashToolStart: (tool: ActiveBashTool) => void;
|
||||
/** Bash tool completed */
|
||||
bashToolEnd: (tool: ActiveBashTool) => void;
|
||||
/** Active Bash tools list updated */
|
||||
bashToolsUpdate: (tools: ActiveBashTool[]) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -410,6 +417,14 @@ export class Session extends EventEmitter {
|
||||
completionDetected: (phrase: string) => void;
|
||||
} | null = null;
|
||||
|
||||
// Bash tool tracking (file paths for live log viewing)
|
||||
private _bashToolParser: BashToolParser;
|
||||
private _bashToolHandlers: {
|
||||
toolStart: (tool: ActiveBashTool) => void;
|
||||
toolEnd: (tool: ActiveBashTool) => void;
|
||||
toolsUpdate: (tools: ActiveBashTool[]) => void;
|
||||
} | null = null;
|
||||
|
||||
constructor(config: Partial<SessionConfig> & {
|
||||
workingDir: string;
|
||||
mode?: SessionMode;
|
||||
@@ -453,6 +468,17 @@ export class Session extends EventEmitter {
|
||||
this._ralphTracker.on('todoUpdate', this._ralphHandlers.todoUpdate);
|
||||
this._ralphTracker.on('completionDetected', this._ralphHandlers.completionDetected);
|
||||
|
||||
// Initialize Bash tool parser and forward events (store handlers for cleanup)
|
||||
this._bashToolParser = new BashToolParser({ sessionId: this.id });
|
||||
this._bashToolHandlers = {
|
||||
toolStart: (tool) => this.emit('bashToolStart', tool),
|
||||
toolEnd: (tool) => this.emit('bashToolEnd', tool),
|
||||
toolsUpdate: (tools) => this.emit('bashToolsUpdate', tools),
|
||||
};
|
||||
this._bashToolParser.on('toolStart', this._bashToolHandlers.toolStart);
|
||||
this._bashToolParser.on('toolEnd', this._bashToolHandlers.toolEnd);
|
||||
this._bashToolParser.on('toolsUpdate', this._bashToolHandlers.toolsUpdate);
|
||||
|
||||
}
|
||||
|
||||
get status(): SessionStatus {
|
||||
@@ -540,6 +566,15 @@ export class Session extends EventEmitter {
|
||||
return this._ralphTracker.getTodoStats();
|
||||
}
|
||||
|
||||
// Bash tool tracking getters
|
||||
get bashToolParser(): BashToolParser {
|
||||
return this._bashToolParser;
|
||||
}
|
||||
|
||||
get activeTools(): ActiveBashTool[] {
|
||||
return this._bashToolParser.activeTools;
|
||||
}
|
||||
|
||||
get parentAgentId(): string | null {
|
||||
return this._parentAgentId;
|
||||
}
|
||||
@@ -852,6 +887,9 @@ export class Session extends EventEmitter {
|
||||
// Forward to Ralph tracker to detect Ralph loops and todos
|
||||
this._ralphTracker.processTerminalData(data);
|
||||
|
||||
// Forward to Bash tool parser to detect file-viewing commands
|
||||
this._bashToolParser.processTerminalData(data);
|
||||
|
||||
// Parse token count from status line (e.g., "123.4k tokens" or "5234 tokens")
|
||||
this.parseTokensFromStatusLine(data);
|
||||
|
||||
@@ -1503,6 +1541,15 @@ export class Session extends EventEmitter {
|
||||
this._ralphHandlers = null;
|
||||
}
|
||||
|
||||
// Remove BashToolParser handlers
|
||||
if (this._bashToolHandlers) {
|
||||
this._bashToolParser.off('toolStart', this._bashToolHandlers.toolStart);
|
||||
this._bashToolParser.off('toolEnd', this._bashToolHandlers.toolEnd);
|
||||
this._bashToolParser.off('toolsUpdate', this._bashToolHandlers.toolsUpdate);
|
||||
this._bashToolHandlers = null;
|
||||
}
|
||||
this._bashToolParser.destroy();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -974,6 +974,34 @@ export function createInitialRunSummaryStats(): RunSummaryStats {
|
||||
};
|
||||
}
|
||||
|
||||
// ========== Active Bash Tool Types ==========
|
||||
|
||||
/**
|
||||
* Status of an active Bash tool command.
|
||||
*/
|
||||
export type ActiveBashToolStatus = 'running' | 'completed';
|
||||
|
||||
/**
|
||||
* Represents an active Bash tool command detected in Claude's output.
|
||||
* Used to display clickable file paths for file-viewing commands.
|
||||
*/
|
||||
export interface ActiveBashTool {
|
||||
/** Unique identifier for this tool invocation */
|
||||
id: string;
|
||||
/** The full command being executed */
|
||||
command: string;
|
||||
/** Extracted file paths from the command (clickable) */
|
||||
filePaths: string[];
|
||||
/** Timeout string if specified (e.g., "16m 0s") */
|
||||
timeout?: string;
|
||||
/** Timestamp when the tool started */
|
||||
startedAt: number;
|
||||
/** Current status */
|
||||
status: ActiveBashToolStatus;
|
||||
/** Session ID this tool belongs to */
|
||||
sessionId: string;
|
||||
}
|
||||
|
||||
// ========== Spawn1337 Protocol Re-exports ==========
|
||||
|
||||
export type {
|
||||
|
||||
+129
-3
@@ -18,7 +18,8 @@ import { existsSync, mkdirSync, writeFileSync, readdirSync, readFileSync } from
|
||||
import { execSync } from 'node:child_process';
|
||||
import { homedir, totalmem, freemem, loadavg, cpus } from 'node:os';
|
||||
import { EventEmitter } from 'node:events';
|
||||
import { Session, ClaudeMessage, type BackgroundTask, type RalphTrackerState, type RalphTodoItem } from '../session.js';
|
||||
import { Session, ClaudeMessage, type BackgroundTask, type RalphTrackerState, type RalphTodoItem, type ActiveBashTool } from '../session.js';
|
||||
import { fileStreamManager } from '../file-stream-manager.js';
|
||||
import { RespawnController, RespawnConfig, RespawnState } from '../respawn-controller.js';
|
||||
import { SpawnOrchestrator, type SessionCreator } from '../spawn-orchestrator.js';
|
||||
import type { SpawnOrchestratorConfig } from '../spawn-types.js';
|
||||
@@ -502,7 +503,14 @@ export class WebServer extends EventEmitter {
|
||||
return createErrorResponse(ApiErrorCode.NOT_FOUND, 'Session not found');
|
||||
}
|
||||
|
||||
return session.toDetailedState();
|
||||
// Include respawn controller state if active
|
||||
const controller = this.respawnControllers.get(id);
|
||||
return {
|
||||
...session.toDetailedState(),
|
||||
respawnEnabled: controller?.getConfig()?.enabled ?? false,
|
||||
respawnConfig: controller?.getConfig() ?? null,
|
||||
respawn: controller?.getStatus() ?? null,
|
||||
};
|
||||
});
|
||||
|
||||
this.app.get('/api/sessions/:id/output', async (req) => {
|
||||
@@ -565,6 +573,101 @@ export class WebServer extends EventEmitter {
|
||||
return { success: true, summary: tracker.getSummary() };
|
||||
});
|
||||
|
||||
// Get active Bash tools for a session (file-viewing commands)
|
||||
this.app.get('/api/sessions/:id/active-tools', async (req) => {
|
||||
const { id } = req.params as { id: string };
|
||||
const session = this.sessions.get(id);
|
||||
|
||||
if (!session) {
|
||||
return createErrorResponse(ApiErrorCode.NOT_FOUND, 'Session not found');
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
tools: session.activeTools,
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
// Stream file content via tail -f (SSE endpoint)
|
||||
this.app.get('/api/sessions/:id/tail-file', async (req, reply) => {
|
||||
const { id } = req.params as { id: string };
|
||||
const { path: filePath, lines } = req.query as { path?: string; lines?: string };
|
||||
const session = this.sessions.get(id);
|
||||
|
||||
if (!session) {
|
||||
reply.code(404).send({ success: false, error: 'Session not found' });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!filePath) {
|
||||
reply.code(400).send({ success: false, error: 'Missing path parameter' });
|
||||
return;
|
||||
}
|
||||
|
||||
// Set up SSE headers
|
||||
reply.raw.writeHead(200, {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Cache-Control': 'no-cache',
|
||||
'Connection': 'keep-alive',
|
||||
'X-Accel-Buffering': 'no',
|
||||
});
|
||||
|
||||
// Track stream for cleanup
|
||||
const streamRef: { id?: string } = {};
|
||||
|
||||
// Create the file stream
|
||||
const result = await fileStreamManager.createStream({
|
||||
sessionId: id,
|
||||
filePath,
|
||||
workingDir: session.workingDir,
|
||||
lines: lines ? parseInt(lines, 10) : undefined,
|
||||
onData: (data) => {
|
||||
// Send data as SSE event
|
||||
reply.raw.write(`data: ${JSON.stringify({ type: 'data', content: data })}\n\n`);
|
||||
},
|
||||
onEnd: () => {
|
||||
reply.raw.write(`data: ${JSON.stringify({ type: 'end' })}\n\n`);
|
||||
reply.raw.end();
|
||||
},
|
||||
onError: (error) => {
|
||||
reply.raw.write(`data: ${JSON.stringify({ type: 'error', error })}\n\n`);
|
||||
},
|
||||
});
|
||||
|
||||
if (!result.success) {
|
||||
reply.raw.write(`data: ${JSON.stringify({ type: 'error', error: result.error })}\n\n`);
|
||||
reply.raw.end();
|
||||
return;
|
||||
}
|
||||
|
||||
streamRef.id = result.streamId;
|
||||
|
||||
// Notify client of successful connection
|
||||
reply.raw.write(`data: ${JSON.stringify({ type: 'connected', streamId: result.streamId, filePath })}\n\n`);
|
||||
|
||||
// Handle client disconnect
|
||||
req.raw.on('close', () => {
|
||||
if (streamRef.id) {
|
||||
fileStreamManager.closeStream(streamRef.id);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Close a file stream
|
||||
this.app.delete('/api/sessions/:id/tail-file/:streamId', async (req) => {
|
||||
const { id, streamId } = req.params as { id: string; streamId: string };
|
||||
const session = this.sessions.get(id);
|
||||
|
||||
if (!session) {
|
||||
return createErrorResponse(ApiErrorCode.NOT_FOUND, 'Session not found');
|
||||
}
|
||||
|
||||
const closed = fileStreamManager.closeStream(streamId);
|
||||
return { success: closed };
|
||||
});
|
||||
|
||||
// Configure Ralph (Ralph Wiggum) settings
|
||||
this.app.post('/api/sessions/:id/ralph-config', async (req) => {
|
||||
const { id } = req.params as { id: string };
|
||||
@@ -2019,6 +2122,8 @@ export class WebServer extends EventEmitter {
|
||||
}
|
||||
|
||||
session.removeAllListeners();
|
||||
// Close any active file streams for this session
|
||||
fileStreamManager.closeSessionStreams(sessionId);
|
||||
await session.stop(killScreen);
|
||||
this.sessions.delete(sessionId);
|
||||
// Only remove from state.json if we're also killing the screen.
|
||||
@@ -2171,6 +2276,19 @@ export class WebServer extends EventEmitter {
|
||||
if (tracker) tracker.recordRalphCompletion(phrase);
|
||||
});
|
||||
|
||||
// Bash tool tracking events (for clickable file paths)
|
||||
session.on('bashToolStart', (tool: ActiveBashTool) => {
|
||||
this.broadcast('session:bashToolStart', { sessionId: session.id, tool });
|
||||
});
|
||||
|
||||
session.on('bashToolEnd', (tool: ActiveBashTool) => {
|
||||
this.broadcast('session:bashToolEnd', { sessionId: session.id, tool });
|
||||
});
|
||||
|
||||
session.on('bashToolsUpdate', (tools: ActiveBashTool[]) => {
|
||||
this.broadcast('session:bashToolsUpdate', { sessionId: session.id, tools });
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private setupRespawnListeners(sessionId: string, controller: RespawnController): void {
|
||||
@@ -2580,7 +2698,15 @@ export class WebServer extends EventEmitter {
|
||||
}
|
||||
|
||||
private getSessionsState() {
|
||||
return Array.from(this.sessions.values()).map(s => s.toDetailedState());
|
||||
return Array.from(this.sessions.values()).map(s => {
|
||||
const controller = this.respawnControllers.get(s.id);
|
||||
return {
|
||||
...s.toDetailedState(),
|
||||
respawnEnabled: controller?.getConfig()?.enabled ?? false,
|
||||
respawnConfig: controller?.getConfig() ?? null,
|
||||
respawn: controller?.getStatus() ?? null,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// Clean up old completed scheduled runs
|
||||
|
||||
@@ -0,0 +1,293 @@
|
||||
/**
|
||||
* File Link Click Tests for Claudeman Web UI
|
||||
*
|
||||
* Tests that file paths displayed in terminal output are clickable
|
||||
* and open the log viewer window correctly.
|
||||
*
|
||||
* Port allocation: 3154 (see CLAUDE.md test port table)
|
||||
*/
|
||||
|
||||
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
||||
import { execSync } from 'node:child_process';
|
||||
import { WebServer } from '../src/web/server.js';
|
||||
import { writeFileSync, mkdirSync, rmSync, appendFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
|
||||
const TEST_PORT = 3154;
|
||||
const baseUrl = `http://localhost:${TEST_PORT}`;
|
||||
const BROWSER_TIMEOUT = 30000;
|
||||
|
||||
// Helper to run agent-browser commands
|
||||
function browser(command: string): string {
|
||||
try {
|
||||
return execSync(`npx agent-browser ${command}`, {
|
||||
timeout: BROWSER_TIMEOUT,
|
||||
encoding: 'utf-8',
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
}).trim();
|
||||
} catch (error: any) {
|
||||
if (error.stderr) {
|
||||
throw new Error(`agent-browser failed: ${error.stderr}`);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function browserJson<T = any>(command: string): T {
|
||||
const result = browser(`${command} --json`);
|
||||
const parsed = JSON.parse(result);
|
||||
if (!parsed.success) {
|
||||
throw new Error(`agent-browser command failed: ${parsed.error || 'unknown error'}`);
|
||||
}
|
||||
return parsed.data;
|
||||
}
|
||||
|
||||
async function waitForElement(selector: string, timeout = 10000): Promise<boolean> {
|
||||
const start = Date.now();
|
||||
while (Date.now() - start < timeout) {
|
||||
try {
|
||||
const count = browserJson<{ count: number }>(`get count "${selector}"`);
|
||||
if (count.count > 0) return true;
|
||||
} catch { /* retry */ }
|
||||
await new Promise(r => setTimeout(r, 500));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function getText(selector: string): string {
|
||||
try {
|
||||
return browserJson<{ text: string }>(`get text "${selector}"`).text || '';
|
||||
} catch {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
function isVisible(selector: string): boolean {
|
||||
try {
|
||||
return browserJson<{ visible: boolean }>(`is visible "${selector}"`).visible;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function closeBrowser() {
|
||||
try {
|
||||
browser('close');
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
|
||||
describe('File Link Click Tests', () => {
|
||||
let server: WebServer;
|
||||
let createdSessions: string[] = [];
|
||||
let browserAvailable = false;
|
||||
let testLogFile: string;
|
||||
let testDir: string;
|
||||
|
||||
beforeAll(async () => {
|
||||
closeBrowser();
|
||||
|
||||
// Create test directory and log file
|
||||
testDir = join(tmpdir(), `claudeman-link-test-${Date.now()}`);
|
||||
mkdirSync(testDir, { recursive: true });
|
||||
testLogFile = join(testDir, 'test.log');
|
||||
writeFileSync(testLogFile, '=== Test Log Started ===\n');
|
||||
|
||||
server = new WebServer(TEST_PORT);
|
||||
await server.start();
|
||||
await new Promise(r => setTimeout(r, 1000));
|
||||
|
||||
// Test if browser is available
|
||||
try {
|
||||
browser(`open ${baseUrl}`);
|
||||
await new Promise(r => setTimeout(r, 2000));
|
||||
const title = browserJson<{ title: string }>('get title');
|
||||
browserAvailable = title.title === 'Claudeman';
|
||||
} catch (e) {
|
||||
console.warn('Browser not available, skipping browser tests:', (e as Error).message);
|
||||
browserAvailable = false;
|
||||
}
|
||||
}, 60000);
|
||||
|
||||
afterAll(async () => {
|
||||
closeBrowser();
|
||||
for (const sessionId of createdSessions) {
|
||||
try {
|
||||
await fetch(`${baseUrl}/api/sessions/${sessionId}`, { method: 'DELETE' });
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
await server.stop();
|
||||
|
||||
// Cleanup test directory
|
||||
try {
|
||||
rmSync(testDir, { recursive: true, force: true });
|
||||
} catch { /* ignore */ }
|
||||
}, 60000);
|
||||
|
||||
it('should create shell session and display terminal output', async () => {
|
||||
if (!browserAvailable) {
|
||||
console.log('Skipping: browser not available');
|
||||
return;
|
||||
}
|
||||
|
||||
// Create a shell session via API
|
||||
const response = await fetch(`${baseUrl}/api/quick-start`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ caseName: 'link-test', mode: 'shell' }),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
expect(data.success).toBe(true);
|
||||
createdSessions.push(data.session.id);
|
||||
|
||||
// Wait for session to appear in UI
|
||||
await new Promise(r => setTimeout(r, 2000));
|
||||
|
||||
// Check that terminal is visible
|
||||
const terminalExists = await waitForElement('.xterm-screen', 5000);
|
||||
expect(terminalExists).toBe(true);
|
||||
}, 60000);
|
||||
|
||||
it('should make file paths clickable in terminal', async () => {
|
||||
if (!browserAvailable || createdSessions.length === 0) {
|
||||
console.log('Skipping: browser not available or no session');
|
||||
return;
|
||||
}
|
||||
|
||||
const sessionId = createdSessions[0];
|
||||
|
||||
// Send a command that outputs a file path
|
||||
// Using 'echo' with the test file path followed by 'tail -f' pattern
|
||||
const command = `echo "Monitoring file: ${testLogFile}" && echo "tail -f ${testLogFile}"`;
|
||||
|
||||
await fetch(`${baseUrl}/api/sessions/${sessionId}/input`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ input: command + '\r' }),
|
||||
});
|
||||
|
||||
await new Promise(r => setTimeout(r, 2000));
|
||||
|
||||
// Check if xterm contains the file path
|
||||
// The xterm link provider should detect "tail -f /path/to/file" pattern
|
||||
const terminalText = getText('.xterm-screen');
|
||||
console.log('Terminal text:', terminalText.substring(0, 500));
|
||||
|
||||
// File path should be visible in terminal
|
||||
expect(terminalText).toContain(testLogFile);
|
||||
}, 60000);
|
||||
|
||||
it('should open log viewer when clicking file path', async () => {
|
||||
if (!browserAvailable || createdSessions.length === 0) {
|
||||
console.log('Skipping: browser not available or no session');
|
||||
return;
|
||||
}
|
||||
|
||||
// Add some content to the log file for streaming
|
||||
for (let i = 1; i <= 5; i++) {
|
||||
appendFileSync(testLogFile, `Log entry ${i}\n`);
|
||||
}
|
||||
|
||||
// Try to click on a link in the terminal
|
||||
// The link provider registers on text matching "tail -f /path" patterns
|
||||
// We need to find and click the link
|
||||
|
||||
// First, let's check if there are any registered links
|
||||
// xterm.js links have class 'xterm-link' when hovered
|
||||
|
||||
// Try clicking on the terminal area where the file path should be
|
||||
// The file path should be clickable based on the registerFilePathLinkProvider
|
||||
|
||||
// Get terminal dimensions to calculate where to click
|
||||
try {
|
||||
// Click somewhere in the terminal where the tail -f line should be
|
||||
// This is approximate - the link detection works on hover
|
||||
browser('click ".xterm-screen"');
|
||||
await new Promise(r => setTimeout(r, 500));
|
||||
} catch (e) {
|
||||
console.log('Click failed:', e);
|
||||
}
|
||||
|
||||
// Check if log viewer window appeared
|
||||
// The log viewer has class .log-viewer-window
|
||||
const logViewerExists = await waitForElement('.log-viewer-window', 3000);
|
||||
|
||||
// Note: This may fail because clicking the terminal doesn't guarantee
|
||||
// clicking on the exact link. We need a more precise test.
|
||||
console.log('Log viewer exists:', logViewerExists);
|
||||
|
||||
// For now, just verify the infrastructure is in place
|
||||
// Real testing would need coordinate-based clicking on the link
|
||||
}, 60000);
|
||||
|
||||
it('should detect file link patterns in terminal output', async () => {
|
||||
if (!browserAvailable || createdSessions.length === 0) {
|
||||
console.log('Skipping: browser not available or no session');
|
||||
return;
|
||||
}
|
||||
|
||||
const sessionId = createdSessions[0];
|
||||
|
||||
// Test various patterns that should be detected as links
|
||||
const patterns = [
|
||||
`cat ${testLogFile}`,
|
||||
`head -n 10 ${testLogFile}`,
|
||||
`less ${testLogFile}`,
|
||||
`grep "test" ${testLogFile}`,
|
||||
];
|
||||
|
||||
for (const pattern of patterns) {
|
||||
await fetch(`${baseUrl}/api/sessions/${sessionId}/input`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ input: `echo "${pattern}"\r` }),
|
||||
});
|
||||
await new Promise(r => setTimeout(r, 500));
|
||||
}
|
||||
|
||||
await new Promise(r => setTimeout(r, 1000));
|
||||
|
||||
// Verify patterns appear in terminal
|
||||
const terminalText = getText('.xterm-screen');
|
||||
for (const pattern of patterns) {
|
||||
expect(terminalText).toContain(testLogFile);
|
||||
}
|
||||
}, 60000);
|
||||
|
||||
it('should stream file content to log viewer', async () => {
|
||||
// Test the tail-file API endpoint directly
|
||||
const sessionId = createdSessions[0] || 'test-session';
|
||||
|
||||
// Create an EventSource to test SSE streaming
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), 5000);
|
||||
|
||||
try {
|
||||
const response = await fetch(
|
||||
`${baseUrl}/api/sessions/${sessionId}/tail-file?path=${encodeURIComponent(testLogFile)}&lines=10`,
|
||||
{ signal: controller.signal }
|
||||
);
|
||||
|
||||
expect(response.ok).toBe(true);
|
||||
expect(response.headers.get('content-type')).toContain('text/event-stream');
|
||||
|
||||
// Read a bit of the stream
|
||||
const reader = response.body?.getReader();
|
||||
if (reader) {
|
||||
const { value } = await reader.read();
|
||||
const text = new TextDecoder().decode(value);
|
||||
console.log('SSE stream data:', text.substring(0, 200));
|
||||
expect(text).toContain('event:');
|
||||
reader.releaseLock();
|
||||
}
|
||||
} catch (e: any) {
|
||||
if (e.name !== 'AbortError') {
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
clearTimeout(timeout);
|
||||
controller.abort();
|
||||
}
|
||||
}, 60000);
|
||||
});
|
||||
Reference in New Issue
Block a user