@@ -60,12 +60,6 @@ variable "experiment_use_screen" {
6060 default = false
6161}
6262
63- variable "experiment_use_tmux" {
64- type = bool
65- description = " Whether to use tmux instead of screen for running Claude Code in the background."
66- default = false
67- }
68-
6963variable "experiment_report_tasks" {
7064 type = bool
7165 description = " Whether to enable task reporting."
@@ -84,17 +78,6 @@ variable "experiment_post_install_script" {
8478 default = null
8579}
8680
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- }
9881
9982variable "install_agentapi" {
10083 type = bool
@@ -180,24 +163,6 @@ resource "coder_script" "claude_code" {
180163 command -v "$1" >/dev/null 2>&1
181164 }
182165
183- install_tmux() {
184- echo "Installing tmux..."
185- if command_exists apt-get; then
186- sudo apt-get update && sudo apt-get install -y tmux
187- elif command_exists yum; then
188- sudo yum install -y tmux
189- elif command_exists dnf; then
190- sudo dnf install -y tmux
191- elif command_exists pacman; then
192- sudo pacman -S --noconfirm tmux
193- elif command_exists apk; then
194- sudo apk add tmux
195- else
196- echo "Error: Unable to install tmux automatically. Package manager not recognized."
197- exit 1
198- fi
199- }
200-
201166 if [ ! -d "${ local . workdir } " ]; then
202167 echo "Warning: The specified folder '${ local . workdir } ' does not exist."
203168 echo "Creating the folder..."
@@ -283,133 +248,43 @@ resource "coder_script" "claude_code" {
283248 /tmp/post_install.sh
284249 fi
285250
286- if [ "${ var . experiment_use_tmux } " = "true" ] && [ "${ var . experiment_use_screen } " = "true" ]; then
287- echo "Error: Both experiment_use_tmux and experiment_use_screen cannot be true simultaneously."
288- echo "Please set only one of them to true."
251+ if ! command_exists claude; then
252+ echo "Error: Claude Code is not installed. Please enable install_claude_code or install it manually."
289253 exit 1
290254 fi
291255
292- if [ " ${ var . experiment_tmux_session_persistence } " = "true" ] && [ " ${ var . experiment_use_tmux } " != "true" ]; then
293- echo "Error: Session persistence requires tmux to be enabled."
294- echo "Please set experiment_use_tmux = true when using session persistence ."
256+ echo "Running Claude Code in the background..."
257+ if ! command_exists screen; then
258+ echo "Error: screen is not installed. Please install screen manually ."
295259 exit 1
296260 fi
297261
298- if [ "${ var . experiment_use_tmux } " = "true" ]; then
299- if ! command_exists tmux; then
300- install_tmux
301- fi
302-
303- if [ "${ var . experiment_tmux_session_persistence } " = "true" ]; then
304- echo "Setting up tmux session persistence..."
305- if ! command_exists git; then
306- echo "Git not found, installing git..."
307- if command_exists apt-get; then
308- sudo apt-get update && sudo apt-get install -y git
309- elif command_exists yum; then
310- sudo yum install -y git
311- elif command_exists dnf; then
312- sudo dnf install -y git
313- elif command_exists pacman; then
314- sudo pacman -S --noconfirm git
315- elif command_exists apk; then
316- sudo apk add git
317- else
318- echo "Error: Unable to install git automatically. Package manager not recognized."
319- echo "Please install git manually to enable session persistence."
320- exit 1
321- fi
322- fi
323-
324- mkdir -p ~/.tmux/plugins
325- if [ ! -d ~/.tmux/plugins/tpm ]; then
326- git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
327- fi
328-
329- cat > ~/.tmux.conf << EOF
330- # Claude Code tmux persistence configuration
331- set -g @plugin 'tmux-plugins/tmux-resurrect'
332- set -g @plugin 'tmux-plugins/tmux-continuum'
333-
334- # Configure session persistence
335- set -g @resurrect-processes ':all:'
336- set -g @resurrect-capture-pane-contents 'on'
337- set -g @resurrect-save-bash-history 'on'
338- set -g @continuum-restore 'on'
339- set -g @continuum-save-interval '${ var . experiment_tmux_session_save_interval } '
340- set -g @continuum-boot 'on'
341- set -g @continuum-save-on 'on'
342-
343- # Initialize plugin manager
344- run '~/.tmux/plugins/tpm/tpm'
345- EOF
346-
347- ~/.tmux/plugins/tpm/scripts/install_plugins.sh
348- fi
349-
350- echo "Running Claude Code in the background with tmux..."
351- touch "$HOME/.claude-code.log"
352- export LANG=en_US.UTF-8
353- export LC_ALL=en_US.UTF-8
354-
355- tmux new-session -d -s agentapi-cc -c ${ local . workdir } '~/.agentapi-start-command true; exec bash'
356- ~/.agentapi-wait-for-start-command
357-
358- if [ "${ var . experiment_tmux_session_persistence } " = "true" ]; then
359- sleep 3
360- fi
361-
362- if ! tmux has-session -t claude-code 2>/dev/null; then
363- # Only create a new session if one doesn't exist
364- tmux new-session -d -s claude-code -c ${ local . workdir } "agentapi attach; exec bash"
365- fi
262+ touch "$HOME/.claude-code.log"
263+ if [ ! -f "$HOME/.screenrc" ]; then
264+ echo "Creating ~/.screenrc and adding multiuser settings..." | tee -a "$HOME/.claude-code.log"
265+ echo -e "multiuser on\nacladd $(whoami)" > "$HOME/.screenrc"
366266 fi
367267
368- if [ "${ var . experiment_use_screen } " = "true" ]; then
369- echo "Running Claude Code in the background..."
370- if ! command_exists screen; then
371- echo "Error: screen is not installed. Please install screen manually."
372- exit 1
373- fi
374-
375- touch "$HOME/.claude-code.log"
376- if [ ! -f "$HOME/.screenrc" ]; then
377- echo "Creating ~/.screenrc and adding multiuser settings..." | tee -a "$HOME/.claude-code.log"
378- echo -e "multiuser on\nacladd $(whoami)" > "$HOME/.screenrc"
379- fi
380-
381- if ! grep -q "^multiuser on$" "$HOME/.screenrc"; then
382- echo "Adding 'multiuser on' to ~/.screenrc..." | tee -a "$HOME/.claude-code.log"
383- echo "multiuser on" >> "$HOME/.screenrc"
384- fi
385-
386- if ! grep -q "^acladd $(whoami)$" "$HOME/.screenrc"; then
387- echo "Adding 'acladd $(whoami)' to ~/.screenrc..." | tee -a "$HOME/.claude-code.log"
388- echo "acladd $(whoami)" >> "$HOME/.screenrc"
389- fi
268+ if ! grep -q "^multiuser on$" "$HOME/.screenrc"; then
269+ echo "Adding 'multiuser on' to ~/.screenrc..." | tee -a "$HOME/.claude-code.log"
270+ echo "multiuser on" >> "$HOME/.screenrc"
271+ fi
390272
391- export LANG=en_US.UTF-8
392- export LC_ALL=en_US.UTF-8
273+ if ! grep -q "^acladd $(whoami)$" "$HOME/.screenrc"; then
274+ echo "Adding 'acladd $(whoami)' to ~/.screenrc..." | tee -a "$HOME/.claude-code.log"
275+ echo "acladd $(whoami)" >> "$HOME/.screenrc"
276+ fi
393277
394- screen -U -dmS agentapi-cc bash -c '
395- cd ${ local . workdir }
396- # setting the first argument will make claude use the prompt
397- ~/.agentapi-start-command true
398- exec bash
399- '
400- ~/.agentapi-wait-for-start-command
278+ export LANG=en_US.UTF-8
279+ export LC_ALL=en_US.UTF-8
401280
402- screen -U -dmS claude-code bash -c '
403- cd ${ local . workdir }
404- agentapi attach
405- exec bash
406- '
407- else
408- if ! command_exists claude; then
409- echo "Error: Claude Code is not installed. Please enable install_claude_code or install it manually."
410- exit 1
411- fi
412- fi
281+ screen -U -dmS agentapi-cc bash -c '
282+ cd ${ local . workdir }
283+ # setting the first argument will make claude use the prompt
284+ ~/.agentapi-start-command true
285+ exec bash
286+ '
287+ ~/.agentapi-wait-for-start-command
413288 EOT
414289 run_on_start = true
415290}
@@ -440,40 +315,16 @@ resource "coder_app" "claude_code" {
440315 export LANG=en_US.UTF-8
441316 export LC_ALL=en_US.UTF-8
442317
443- if [ "${ var . experiment_use_tmux } " = "true" ]; then
444- if ! tmux has-session -t agentapi-cc 2>/dev/null; then
318+ if ! screen -list | grep -q "agentapi-cc"; then
319+ screen -S agentapi-cc bash -c '
320+ cd ${ local . workdir }
445321 # start agentapi without claude using the prompt (no argument)
446- tmux new-session -d -s agentapi-cc -c ${ local . workdir } '~/.agentapi-start-command; exec bash'
447- ~/.agentapi-wait-for-start-command
448- fi
449-
450- if tmux has-session -t claude-code 2>/dev/null; then
451- echo "Attaching to existing Claude Code tmux session." | tee -a "$HOME/.claude-code.log"
452- tmux attach-session -t claude-code
453- else
454- echo "Starting a new Claude Code tmux session." | tee -a "$HOME/.claude-code.log"
455- tmux new-session -s claude-code -c ${ local . workdir } "agentapi attach; exec bash"
456- fi
457- elif [ "${ var . experiment_use_screen } " = "true" ]; then
458- if ! screen -list | grep -q "agentapi-cc"; then
459- screen -S agentapi-cc bash -c '
460- cd ${ local . workdir }
461- # start agentapi without claude using the prompt (no argument)
462- ~/.agentapi-start-command
463- exec bash
464- '
465- fi
466- if screen -list | grep -q "claude-code"; then
467- echo "Attaching to existing Claude Code screen session." | tee -a "$HOME/.claude-code.log"
468- screen -xRR claude-code
469- else
470- echo "Starting a new Claude Code screen session." | tee -a "$HOME/.claude-code.log"
471- screen -S claude-code bash -c 'agentapi attach; exec bash'
472- fi
473- else
474- cd ${ local . workdir }
475- agentapi attach
322+ ~/.agentapi-start-command
323+ exec bash
324+ '
476325 fi
326+
327+ agentapi attach
477328 EOT
478329 icon = var. icon
479330 order = var. order
0 commit comments