This document provides context for implementing the session-based refactor. Delete this file once the refactor is complete.
We are introducing sessions as a new abstraction layer between users and instances. A session is a persistent, attachable/detachable process (shell, agent CLI, etc.) running within an instance, managed by tmux.
The original design allowed multiple resume calls to the same instance but provided no way to:
- List what's currently running in an instance
- Reattach to a specific process after detaching
- Name or organize concurrent processes
- Track which session was most recently used
| Concept | Definition |
|---|---|
| Instance | Git worktree + container (unchanged) |
| Session | A tmux-managed process within an instance (new) |
An instance can have zero or more sessions. Sessions persist across detach/attach cycles.
| Old | New | Notes |
|---|---|---|
new |
run |
Creates instance if needed, always creates new session. Supports --detached |
resume |
attach |
Reattaches to existing session (MRU-based) |
list |
ps |
Lists instances, or sessions if branch specified |
| — | logs |
View session output without attaching |
| — | kill |
Terminates a specific session |
stop |
stop |
Unchanged (stops container, kills all sessions) |
rm |
rm |
Unchanged |
recreate |
recreate |
Unchanged (kills all sessions) |
hjk attach uses MRU strategy:
- No args: attach to most recent session globally
- Branch only: attach to most recent session for that instance
- Branch + session: attach to explicit session
Requires tracking last_accessed timestamp per session.
Auto-generated names (Docker-style, e.g., happy-panda). Override with --name flag on run.
hjk run supports -d, --detached flag to create a session without attaching:
hjk run feat/auth --agent claude -d "Refactor auth module"
hjk run feat/auth --agent claude -d "Write tests"
# Two agents now running in parallelThis enables parallel agent workflows.
All session output is captured to log files regardless of attached/detached mode:
~/.local/share/headjack/logs/<instance-id>/<session-id>.log
hjk logs reads these files:
hjk logs feat/auth happy-panda # View recent output
hjk logs feat/auth happy-panda -f # Follow in real-timeThis is implemented by teeing stdout/stderr when spawning the session process.
Sessions are implemented using tmux. tmux handles:
- Terminal multiplexing
- Attach/detach mechanics
- Process lifecycle within sessions
Headjack manages session metadata (name, type, timestamps) in the catalog.
Before:
{
"instances": [
{
"id": "...",
"branch": "feat/auth",
"container_id": "...",
"status": "running"
}
]
}After:
{
"instances": [
{
"id": "...",
"branch": "feat/auth",
"container_id": "...",
"status": "running",
"sessions": [
{
"id": "sess-abc",
"name": "happy-panda",
"type": "claude",
"tmux_session": "hjk-<instance-id>-sess-abc",
"created_at": "2025-12-30T10:00:00Z",
"last_accessed": "2025-12-30T14:30:00Z"
}
]
}
]
}internal/cmd/— Replacenew.go,resume.go,list.gowithrun.go,attach.go,ps.go,logs.go,kill.gointernal/catalog/— Add session tracking,last_accessedupdatesinternal/instance/— Session CRUD operations, tmux integration, log file management
internal/container/— Ensure tmux is available in containersdocs/designs/base-image.md— Add tmux to base image
- tmux interaction layer (create session, attach, list, kill)
- Session name generator (word-based, Docker-style)
- Session logging layer (tee output to log files, read logs for
hjk logs)
See docs/designs/cli-interface.md for the complete CLI specification.