Skip to content

Commit 293f579

Browse files
feat: add tmux session persistence configuration to claude-code module
1 parent 6d1e99d commit 293f579

File tree

1 file changed

+72
-2
lines changed
  • registry/coder/modules/claude-code

1 file changed

+72
-2
lines changed

registry/coder/modules/claude-code/main.tf

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ variable "experiment_post_install_script" {
8484
default = null
8585
}
8686

87+
variable "experiment_tmux_session_persistence" {
88+
type = bool
89+
description = "Whether to enable tmux session persistence across workspace restarts."
90+
default = false
91+
}
92+
93+
variable "experiment_tmux_session_save_interval" {
94+
type = string
95+
description = "How often to save tmux sessions in minutes."
96+
default = "15"
97+
}
98+
8799
locals {
88100
encoded_pre_install_script = var.experiment_pre_install_script != null ? base64encode(var.experiment_pre_install_script) : ""
89101
encoded_post_install_script = var.experiment_post_install_script != null ? base64encode(var.experiment_post_install_script) : ""
@@ -151,6 +163,62 @@ resource "coder_script" "claude_code" {
151163
exit 1
152164
fi
153165
166+
# Configure tmux session persistence if enabled
167+
if [ "${var.experiment_tmux_session_persistence}" = "true" ] && [ "${var.experiment_use_tmux}" = "true" ]; then
168+
echo "Setting up tmux session persistence..."
169+
170+
# Check and install git if needed
171+
if ! command_exists git; then
172+
echo "Git not found, installing git..."
173+
if command_exists apt-get; then
174+
apt-get update && apt-get install -y git
175+
elif command_exists yum; then
176+
yum install -y git
177+
elif command_exists dnf; then
178+
dnf install -y git
179+
elif command_exists pacman; then
180+
pacman -S --noconfirm git
181+
elif command_exists apk; then
182+
apk add git
183+
else
184+
echo "Error: Unable to install git automatically. Package manager not recognized."
185+
echo "Please install git manually to enable session persistence."
186+
exit 1
187+
fi
188+
fi
189+
190+
# Install TPM and plugins
191+
mkdir -p ~/.tmux/plugins
192+
if [ ! -d ~/.tmux/plugins/tpm ]; then
193+
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
194+
fi
195+
196+
# Configure .tmux.conf for persistence
197+
cat > ~/.tmux.conf << EOF
198+
# Claude Code tmux persistence configuration
199+
set -g @plugin 'tmux-plugins/tmux-resurrect'
200+
set -g @plugin 'tmux-plugins/tmux-continuum'
201+
202+
# Configure session persistence
203+
set -g @resurrect-processes 'claude'
204+
set -g @continuum-restore 'on'
205+
set -g @continuum-save-interval '${var.experiment_tmux_session_save_interval}'
206+
207+
# Initialize plugin manager
208+
run '~/.tmux/plugins/tpm/tpm'
209+
EOF
210+
211+
# Install plugins
212+
~/.tmux/plugins/tpm/scripts/install_plugins.sh
213+
fi
214+
215+
# Validate session persistence requirements
216+
if [ "${var.experiment_tmux_session_persistence}" = "true" ] && [ "${var.experiment_use_tmux}" != "true" ]; then
217+
echo "Error: Session persistence requires tmux to be enabled."
218+
echo "Please set experiment_use_tmux = true when using session persistence."
219+
exit 1
220+
fi
221+
154222
# Run with tmux if enabled
155223
if [ "${var.experiment_use_tmux}" = "true" ]; then
156224
echo "Running Claude Code in the background with tmux..."
@@ -166,8 +234,10 @@ resource "coder_script" "claude_code" {
166234
export LANG=en_US.UTF-8
167235
export LC_ALL=en_US.UTF-8
168236
169-
# Create a new tmux session in detached mode
170-
tmux new-session -d -s claude-code -c ${var.folder} "claude --dangerously-skip-permissions \"$CODER_MCP_CLAUDE_TASK_PROMPT\""
237+
# Create or attach to session (persistence handles restoration automatically)
238+
if ! tmux has-session -t claude-code 2>/dev/null; then
239+
tmux new-session -d -s claude-code -c ${var.folder} "claude --dangerously-skip-permissions \"$CODER_MCP_CLAUDE_TASK_PROMPT\""
240+
fi
171241
172242
fi
173243

0 commit comments

Comments
 (0)