Replace PID-based temp files with AGENT_ID env var

- Simplified Claude hooks to use AGENT_ID instead of PID lookup
- Updated 9p-read-inbox to use AGENT_ID env var
- Removed temporary file creation in tmux.go
- Much cleaner and more reliable approach
This commit is contained in:
Levi Neely 2026-02-18 20:09:12 +01:00
parent 67fbc83efd
commit 5399d709c4
5 changed files with 23 additions and 67 deletions

View File

@ -1,37 +1,22 @@
#!/bin/sh
echo "$(date '+%Y-%m-%d %H:%M:%S') - Hook fired: Stop (PPID=$PPID)" >> /tmp/claude-hooks.log
echo "$(date '+%Y-%m-%d %H:%M:%S') - Hook fired: Stop" >> /tmp/claude-hooks.log
cat > /dev/null
pid=$PPID
while [ $pid -gt 1 ]; do
if ps -p $pid -o comm= | grep -q claude; then
break
fi
pid=$(ps -p $pid -o ppid= | tr -d ' ')
done
if [ $pid -le 1 ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR: Claude PID not found" >> /tmp/claude-hooks.log
if [ -z "$AGENT_ID" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR: AGENT_ID not set" >> /tmp/claude-hooks.log
exit 0
fi
echo "$(date '+%Y-%m-%d %H:%M:%S') - Found Claude PID: $pid" >> /tmp/claude-hooks.log
# Read session ID from filesystem directly (9p read doesn't work in sandbox)
ns_path="/tmp/ns.$USER.:0/anvillm-session-id-$pid"
if [ ! -f "$ns_path" ]; then
ns_path="/tmp/ns.$USER/anvillm-session-id-$pid"
fi
session=$(cat "$ns_path" 2>/dev/null)
if [ -z "$session" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR: Session ID not found for PID $pid" >> /tmp/claude-hooks.log
exit 0
fi
echo "$(date '+%Y-%m-%d %H:%M:%S') - Found session ID: $session" >> /tmp/claude-hooks.log
if echo idle | 9p write agent/$session/state 2>>/tmp/claude-hooks.log; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - SUCCESS: Wrote 'idle' to agent/$session/state" >> /tmp/claude-hooks.log
echo "$(date '+%Y-%m-%d %H:%M:%S') - Using AGENT_ID: $AGENT_ID" >> /tmp/claude-hooks.log
if echo idle | 9p write agent/$AGENT_ID/state 2>>/tmp/claude-hooks.log; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - SUCCESS: Wrote 'idle' to agent/$AGENT_ID/state" >> /tmp/claude-hooks.log
# Check for pending messages in inbox
if [ -n "$(9p ls agent/$session/inbox/ 2>/dev/null)" ]; then
if [ -n "$(9p ls agent/$AGENT_ID/inbox/ 2>/dev/null)" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - INBOX: Pending messages detected" >> /tmp/claude-hooks.log
echo "[INBOX] You have pending messages. Check with: 9p-read-inbox"
fi
else
echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR: Failed to write to agent/$session/state" >> /tmp/claude-hooks.log
echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR: Failed to write to agent/$AGENT_ID/state" >> /tmp/claude-hooks.log
fi

View File

@ -1,31 +1,16 @@
#!/bin/sh
echo "$(date '+%Y-%m-%d %H:%M:%S') - Hook fired: UserPromptSubmit (PPID=$PPID)" >> /tmp/claude-hooks.log
echo "$(date '+%Y-%m-%d %H:%M:%S') - Hook fired: UserPromptSubmit" >> /tmp/claude-hooks.log
cat > /dev/null
pid=$PPID
while [ $pid -gt 1 ]; do
if ps -p $pid -o comm= | grep -q claude; then
break
fi
pid=$(ps -p $pid -o ppid= | tr -d ' ')
done
if [ $pid -le 1 ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR: Claude PID not found" >> /tmp/claude-hooks.log
if [ -z "$AGENT_ID" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR: AGENT_ID not set" >> /tmp/claude-hooks.log
exit 0
fi
echo "$(date '+%Y-%m-%d %H:%M:%S') - Found Claude PID: $pid" >> /tmp/claude-hooks.log
# Read session ID from filesystem directly (9p read doesn't work in sandbox)
ns_path="/tmp/ns.$USER.:0/anvillm-session-id-$pid"
if [ ! -f "$ns_path" ]; then
ns_path="/tmp/ns.$USER/anvillm-session-id-$pid"
fi
session=$(cat "$ns_path" 2>/dev/null)
if [ -z "$session" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR: Session ID not found for PID $pid" >> /tmp/claude-hooks.log
exit 0
fi
echo "$(date '+%Y-%m-%d %H:%M:%S') - Found session ID: $session" >> /tmp/claude-hooks.log
if echo running | 9p write agent/$session/state 2>>/tmp/claude-hooks.log; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - SUCCESS: Wrote 'running' to agent/$session/state" >> /tmp/claude-hooks.log
echo "$(date '+%Y-%m-%d %H:%M:%S') - Using AGENT_ID: $AGENT_ID" >> /tmp/claude-hooks.log
if echo running | 9p write agent/$AGENT_ID/state 2>>/tmp/claude-hooks.log; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - SUCCESS: Wrote 'running' to agent/$AGENT_ID/state" >> /tmp/claude-hooks.log
else
echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR: Failed to write to agent/$session/state" >> /tmp/claude-hooks.log
echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR: Failed to write to agent/$AGENT_ID/state" >> /tmp/claude-hooks.log
fi

View File

@ -363,17 +363,6 @@ func (b *Backend) CreateSession(ctx context.Context, opts backend.SessionOptions
}
sess.idleCond = sync.NewCond(&sess.mu)
// Write session ID to file for hook access (if kiro-cli or claude backend)
if (b.cfg.Name == "kiro-cli" || b.cfg.Name == "claude") && pid != 0 {
ns := client.Namespace()
sessionIDFile := filepath.Join(ns, fmt.Sprintf("anvillm-session-id-%d", pid))
if err := os.WriteFile(sessionIDFile, []byte(id), 0644); err != nil {
debug.Log("[session %s] failed to write session ID file: %v", id, err)
} else {
debug.Log("[session %s] wrote session ID file: %s", id, sessionIDFile)
}
}
// 10. Start reader goroutine
go sess.reader()

1
mkfile
View File

@ -7,6 +7,7 @@ build:V:
go build -o $INSTALL_PATH/Assist ./cmd/Assist
go build -o $INSTALL_PATH/anvillm ./cmd/anvillm
go build -o $INSTALL_PATH/anvilweb ./cmd/anvilweb
go build -o $INSTALL_PATH/anvilmcp ./cmd/anvilmcp
cp scripts/Alog $INSTALL_PATH/Alog
chmod 0755 $INSTALL_PATH/Alog
cp scripts/anvillm-skills $INSTALL_PATH/anvillm-skills

View File

@ -10,12 +10,8 @@ if [ "$1" = "--help" ]; then
exit 0
fi
# Get agent ID from namespace file
KIRO_PID=$(ps -o ppid= -p $PPID | tr -d ' ')
AGENT_ID=$(cat $(namespace)/anvillm-session-id-$KIRO_PID 2>/dev/null)
if [ -z "$AGENT_ID" ]; then
echo "Error: Cannot determine agent ID" >&2
echo "Error: AGENT_ID environment variable not set" >&2
exit 1
fi