Session Management
Happy sessions represent individual AI conversations that can run locally or be controlled remotely. Understanding the session lifecycle helps you manage your AI workflows effectively.Session Lifecycle
A Happy session goes through several stages from creation to termination:Session Creation
When you run
happy, a new session is created with:- Unique session ID (UUID)
- Project path (current directory)
- API client for server communication
- WebSocket connection for real-time updates
- Message queue for bidirectional communication
Authentication & Setup
Session authenticates with Happy servers using credentials from
~/.happy/:- Verifies authentication token
- Establishes encrypted WebSocket connection
- Registers with daemon (if running)
Mode Selection
Session starts in one of two modes:
- Local mode: Interactive terminal control (default)
- Remote mode: Mobile app control (via
--happy-starting-mode remote)
Claude Integration
Session connects to Claude Code:
- Local mode: Spawns Claude process with PTY for terminal interaction
- Remote mode: Uses Claude SDK for programmatic control
Active Session
Session runs and processes messages:
- Forwards user input to Claude
- Streams Claude responses back
- Handles tool usage and permissions
- Syncs state with Happy servers
Session Modes
Local Mode (Interactive)
Default mode when you runhappy from the terminal:
- Full terminal interaction
- Real-time output in your terminal
- Keyboard input directly to Claude
- Still synced to Happy cloud for mobile viewing
- Can switch to remote mode dynamically
- Spawns Claude Code as child process
- Uses PTY (pseudo-terminal) for terminal emulation
- File watcher monitors Claude session files
- Messages parsed and forwarded to Happy servers
Remote Mode
Controlled entirely from the mobile app:- No terminal interaction
- All input comes from mobile app
- All output sent to mobile app
- Runs in background (daemon-spawned)
- Uses Claude SDK directly (no PTY)
- Message queue handles mobile ↔ Claude communication
- SDK streaming responses forwarded to mobile
- Permission requests sent to mobile app
Session Components
Session Object
Core session state defined insrc/claude/session.ts:
Session Tracking
The daemon tracks active sessions:Keep-Alive Mechanism
Sessions send periodic heartbeats to the server:- Current thinking state (Claude is processing)
- Current mode (local or remote)
- Timestamp
Session Hooks
Happy uses Claude Code’s SessionStart hook to track session lifecycle:Hook Configuration
Generated insrc/claude/utils/generateHookSettings.ts:
Hook Behavior
When Claude starts or resumes a session:- Fresh session: Claude generates new session ID
- Resume session: Claude uses existing session ID
- Hook triggers:
SessionStarthook executes - Webhook call: Hook POSTs to daemon’s
/session-startedendpoint - Daemon updates: Daemon associates Happy session with Claude session
The hook enables Happy to discover Claude’s session ID dynamically, supporting both fresh starts and session resumption.
Session Resumption
Claude Code supports resuming previous sessions:Resume Behavior
Session ID Changes
Session ID Changes
Important: When resuming, Claude creates a NEW session ID, not the original.Example:
- Original session:
aada10c6-9299-4c45-abc4-91db9c0f935d - Resumed session:
1433467f-ff14-4292-b5b2-2aac77a808f0
History Preservation
History Preservation
The new session file includes:
- Summary of previous conversation
- Complete history from original session (with NEW session ID)
- New messages from current interaction
File Structure
File Structure
Original session file: Remains unchanged in
~/.claude/projects/.../New session file: Created with new ID, contains prefixed historyExample:Session Configuration
Runtime Selection
Choose JavaScript runtime for Claude Code:Environment Variables
Pass custom environment variables to Claude:Chrome Mode
Enable browser access for Claude:Settings File
Message Flow
Local Mode Flow
Remote Mode Flow
Session Cleanup
When a session ends:Cleanup resources
- Kill Claude process (if running)
- Close file watchers
- Clear message queue
- Delete temporary hook settings file
Session Logs
Each session creates a log file: Location:~/.happy/logs/<timestamp>.log
Contents:
- Session creation and termination
- Mode switches (local ↔ remote)
- Claude messages and responses
- Error messages and stack traces
- Debug information (if
DEBUG=1)
Advanced Features
Permission Mode
Configure permission handling:Sandbox Integration
Sessions respect sandbox configuration:MCP Servers
Sessions can load MCP (Model Context Protocol) servers defined in Claude settings:Troubleshooting
Session won’t start
Session ID not found
If Happy can’t detect Claude’s session ID:- Check hook settings are being applied
- Verify daemon is receiving webhook
- Check session logs for errors
Session stuck in thinking
If session appears frozen:- Check Claude process is running:
ps aux | grep claude - View session logs:
tail -f ~/.happy/logs/*.log - Stop session:
happy daemon stop-session <session-id>
Mode switch not working
If you can’t switch between local and remote mode:- Ensure daemon is running:
happy daemon status - Check WebSocket connection in logs
- Restart session
Related Commands
happy daemon list- View active sessionshappy doctor- Diagnose session issueshappy sandbox- Configure session sandboxing