From 8dbf61a466db5274d8b354abdc5309c9875e3807 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Mon, 14 Apr 2025 12:14:08 -0500 Subject: [PATCH 1/8] print on tmux var --- claude-code/main.tf | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/claude-code/main.tf b/claude-code/main.tf index 349af17f..0e581873 100644 --- a/claude-code/main.tf +++ b/claude-code/main.tf @@ -54,6 +54,12 @@ variable "experiment_use_screen" { default = false } +variable "experiment_use_tmux" { + type = bool + description = "Whether to use tmux instead of screen for running Claude Code in the background." + default = false +} + variable "experiment_report_tasks" { type = bool description = "Whether to enable task reporting." @@ -89,6 +95,11 @@ resource "coder_script" "claude_code" { coder exp mcp configure claude-code ${var.folder} fi + # Check tmux variable + if [ "${var.experiment_use_tmux}" = "true" ]; then + echo "User specified tmux" + fi + # Run with screen if enabled if [ "${var.experiment_use_screen}" = "true" ]; then echo "Running Claude Code in the background..." From ab434a7e570369607d13a848a82cc6762536b4c7 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Mon, 14 Apr 2025 12:21:06 -0500 Subject: [PATCH 2/8] make experiments exclusive --- claude-code/main.tf | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/claude-code/main.tf b/claude-code/main.tf index 0e581873..c6324dd8 100644 --- a/claude-code/main.tf +++ b/claude-code/main.tf @@ -95,7 +95,13 @@ resource "coder_script" "claude_code" { coder exp mcp configure claude-code ${var.folder} fi - # Check tmux variable + # Handle terminal multiplexer selection (tmux or screen) + if [ "${var.experiment_use_tmux}" = "true" ] && [ "${var.experiment_use_screen}" = "true" ]; then + echo "Error: Both experiment_use_tmux and experiment_use_screen cannot be true simultaneously." + echo "Please set only one of them to true." + exit 1 + fi + if [ "${var.experiment_use_tmux}" = "true" ]; then echo "User specified tmux" fi From fc5ef67447ed37dbb09acf2d8e1d71ce3bbce698 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Mon, 14 Apr 2025 12:30:33 -0500 Subject: [PATCH 3/8] use tmux for running claude --- claude-code/main.tf | 47 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/claude-code/main.tf b/claude-code/main.tf index c6324dd8..95380269 100644 --- a/claude-code/main.tf +++ b/claude-code/main.tf @@ -102,8 +102,30 @@ resource "coder_script" "claude_code" { exit 1 fi + # Run with tmux if enabled if [ "${var.experiment_use_tmux}" = "true" ]; then - echo "User specified tmux" + echo "Running Claude Code in the background with tmux..." + + # Check if tmux is installed + if ! command_exists tmux; then + echo "Error: tmux is not installed. Please install tmux manually." + exit 1 + fi + + touch "$HOME/.claude-code.log" + + export LANG=en_US.UTF-8 + export LC_ALL=en_US.UTF-8 + + # Create a new tmux session in detached mode + tmux new-session -d -s claude-code -c ${var.folder} "claude --dangerously-skip-permissions | tee -a \"$HOME/.claude-code.log\"; exec bash" + + # Send the prompt to the tmux session if needed + if [ -n "$CODER_MCP_CLAUDE_TASK_PROMPT" ]; then + tmux send-keys -t claude-code "$CODER_MCP_CLAUDE_TASK_PROMPT" + sleep 5 + tmux send-keys -t claude-code Enter + fi fi # Run with screen if enabled @@ -166,20 +188,27 @@ resource "coder_app" "claude_code" { #!/bin/bash set -e - if [ "${var.experiment_use_screen}" = "true" ]; then + export LANG=en_US.UTF-8 + export LC_ALL=en_US.UTF-8 + + if [ "${var.experiment_use_tmux}" = "true" ]; then + if tmux has-session -t claude-code 2>/dev/null; then + echo "Attaching to existing Claude Code tmux session." | tee -a "$HOME/.claude-code.log" + tmux attach-session -t claude-code + else + echo "Starting a new Claude Code tmux session." | tee -a "$HOME/.claude-code.log" + tmux new-session -s claude-code -c ${var.folder} "claude --dangerously-skip-permissions | tee -a \"$HOME/.claude-code.log\"; exec bash" + fi + elif [ "${var.experiment_use_screen}" = "true" ]; then if screen -list | grep -q "claude-code"; then - export LANG=en_US.UTF-8 - export LC_ALL=en_US.UTF-8 - echo "Attaching to existing Claude Code session." | tee -a "$HOME/.claude-code.log" + echo "Attaching to existing Claude Code screen session." | tee -a "$HOME/.claude-code.log" screen -xRR claude-code else - echo "Starting a new Claude Code session." | tee -a "$HOME/.claude-code.log" - screen -S claude-code bash -c 'export LANG=en_US.UTF-8; export LC_ALL=en_US.UTF-8; claude --dangerously-skip-permissions | tee -a "$HOME/.claude-code.log"; exec bash' + echo "Starting a new Claude Code screen session." | tee -a "$HOME/.claude-code.log" + screen -S claude-code bash -c 'claude --dangerously-skip-permissions | tee -a "$HOME/.claude-code.log"; exec bash' fi else cd ${var.folder} - export LANG=en_US.UTF-8 - export LC_ALL=en_US.UTF-8 claude fi EOT From e1d91e49ac673873424c5f5b931f3725456fe984 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Tue, 15 Apr 2025 09:16:01 -0500 Subject: [PATCH 4/8] try a simpler launch command --- claude-code/main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/claude-code/main.tf b/claude-code/main.tf index 95380269..b0bcd884 100644 --- a/claude-code/main.tf +++ b/claude-code/main.tf @@ -118,7 +118,7 @@ resource "coder_script" "claude_code" { export LC_ALL=en_US.UTF-8 # Create a new tmux session in detached mode - tmux new-session -d -s claude-code -c ${var.folder} "claude --dangerously-skip-permissions | tee -a \"$HOME/.claude-code.log\"; exec bash" + tmux new-session -d -s claude-code "claude" # Send the prompt to the tmux session if needed if [ -n "$CODER_MCP_CLAUDE_TASK_PROMPT" ]; then From e93fba87a931ddc82751cfb9fe35a5c65200f53f Mon Sep 17 00:00:00 2001 From: Benjamin Date: Tue, 15 Apr 2025 09:24:32 -0500 Subject: [PATCH 5/8] use the right folder --- claude-code/main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/claude-code/main.tf b/claude-code/main.tf index b0bcd884..65e61687 100644 --- a/claude-code/main.tf +++ b/claude-code/main.tf @@ -118,7 +118,7 @@ resource "coder_script" "claude_code" { export LC_ALL=en_US.UTF-8 # Create a new tmux session in detached mode - tmux new-session -d -s claude-code "claude" + tmux new-session -d -s claude-code -c ${var.folder} "claude" # Send the prompt to the tmux session if needed if [ -n "$CODER_MCP_CLAUDE_TASK_PROMPT" ]; then From e6e9ffbcb216f74213f33324ced8b99d79bf8114 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Wed, 16 Apr 2025 15:48:05 -0500 Subject: [PATCH 6/8] readme updates claude-code --- claude-code/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/claude-code/README.md b/claude-code/README.md index d851d7cf..e5520a3f 100644 --- a/claude-code/README.md +++ b/claude-code/README.md @@ -25,7 +25,7 @@ module "claude-code" { ### Prerequisites - Node.js and npm must be installed in your workspace to install Claude Code -- `screen` must be installed in your workspace to run Claude Code in the background +- Either `screen` or `tmux` must be installed in your workspace to run Claude Code in the background - You must add the [Coder Login](https://registry.coder.com/modules/coder-login) module to your template The `codercom/oss-dogfood:latest` container image can be used for testing on container-based workspaces. @@ -43,7 +43,7 @@ The `codercom/oss-dogfood:latest` container image can be used for testing on con > Join our [Discord channel](https://discord.gg/coder) or > [contact us](https://coder.com/contact) to get help or share feedback. -Your workspace must have `screen` installed to use this. +Your workspace must have either `screen` or `tmux` installed to use this. ```tf variable "anthropic_api_key" { @@ -90,7 +90,7 @@ module "claude-code" { claude_code_version = "0.2.57" # Enable experimental features - experiment_use_screen = true + experiment_use_screen = true # Or use experiment_use_tmux = true to use tmux instead experiment_report_tasks = true } ``` From bcff5bc3454b55c4e23ed4e68ea8c5550332470c Mon Sep 17 00:00:00 2001 From: Benjamin Date: Wed, 16 Apr 2025 15:51:37 -0500 Subject: [PATCH 7/8] fmt --- claude-code/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/claude-code/README.md b/claude-code/README.md index e5520a3f..25a14960 100644 --- a/claude-code/README.md +++ b/claude-code/README.md @@ -90,7 +90,7 @@ module "claude-code" { claude_code_version = "0.2.57" # Enable experimental features - experiment_use_screen = true # Or use experiment_use_tmux = true to use tmux instead + experiment_use_screen = true # Or use experiment_use_tmux = true to use tmux instead experiment_report_tasks = true } ``` From 6abf9b5852c69cc9a182084e09cc5cf730b99c33 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Thu, 17 Apr 2025 09:51:23 -0500 Subject: [PATCH 8/8] update version in readme --- claude-code/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/claude-code/README.md b/claude-code/README.md index 25a14960..6590a7fa 100644 --- a/claude-code/README.md +++ b/claude-code/README.md @@ -14,7 +14,7 @@ Run the [Claude Code](https://docs.anthropic.com/en/docs/agents-and-tools/claude ```tf module "claude-code" { source = "registry.coder.com/modules/claude-code/coder" - version = "1.0.31" + version = "1.1.0" agent_id = coder_agent.example.id folder = "/home/coder" install_claude_code = true @@ -83,7 +83,7 @@ resource "coder_agent" "main" { module "claude-code" { count = data.coder_workspace.me.start_count source = "registry.coder.com/modules/claude-code/coder" - version = "1.0.31" + version = "1.1.0" agent_id = coder_agent.example.id folder = "/home/coder" install_claude_code = true @@ -102,7 +102,7 @@ Run Claude Code as a standalone app in your workspace. This will install Claude ```tf module "claude-code" { source = "registry.coder.com/modules/claude-code/coder" - version = "1.0.31" + version = "1.1.0" agent_id = coder_agent.example.id folder = "/home/coder" install_claude_code = true