Skip to content

Commit 670f997

Browse files
feat(goose): add support for tmux as an alternative to screen
- Introduced new variables `experiment_use_tmux` and `session_name` for configuring tmux or screen sessions. - Updated script logic to handle both tmux and screen, ensuring only one can be enabled at a time. - Enhanced error handling for missing dependencies and session management for both tmux and screen.
1 parent ae6cf8c commit 670f997

File tree

1 file changed

+73
-28
lines changed
  • registry/coder/modules/goose

1 file changed

+73
-28
lines changed

registry/coder/modules/goose/main.tf

Lines changed: 73 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ variable "experiment_use_screen" {
5454
default = false
5555
}
5656

57+
variable "experiment_use_tmux" {
58+
type = bool
59+
description = "Whether to use tmux instead of screen for running Goose in the background."
60+
default = false
61+
}
62+
63+
variable "session_name" {
64+
type = string
65+
description = "Name for the persistent session (screen or tmux)"
66+
default = "goose"
67+
}
68+
5769
variable "experiment_report_tasks" {
5870
type = bool
5971
description = "Whether to enable task reporting."
@@ -187,8 +199,52 @@ EOL
187199
mkdir -p "$HOME/.config/goose"
188200
echo "$GOOSE_SYSTEM_PROMPT" > "$HOME/.config/goose/.goosehints"
189201
190-
# Run with screen if enabled
191-
if [ "${var.experiment_use_screen}" = "true" ]; then
202+
# Handle terminal multiplexer selection (tmux or screen)
203+
if [ "${var.experiment_use_tmux}" = "true" ] && [ "${var.experiment_use_screen}" = "true" ]; then
204+
echo "Error: Both experiment_use_tmux and experiment_use_screen cannot be true simultaneously."
205+
echo "Please set only one of them to true."
206+
exit 1
207+
fi
208+
209+
# Determine goose command
210+
if command_exists goose; then
211+
GOOSE_CMD=goose
212+
elif [ -f "$HOME/.local/bin/goose" ]; then
213+
GOOSE_CMD="$HOME/.local/bin/goose"
214+
else
215+
echo "Error: Goose is not installed. Please enable install_goose or install it manually."
216+
exit 1
217+
fi
218+
219+
# Run with tmux if enabled
220+
if [ "${var.experiment_use_tmux}" = "true" ]; then
221+
echo "Running Goose in the background with tmux..."
222+
223+
# Check if tmux is installed
224+
if ! command_exists tmux; then
225+
echo "Error: tmux is not installed. Please install tmux manually."
226+
exit 1
227+
fi
228+
229+
touch "$HOME/.goose.log"
230+
231+
export LANG=en_US.UTF-8
232+
export LC_ALL=en_US.UTF-8
233+
234+
# Configure tmux for shared sessions
235+
if [ ! -f "$HOME/.tmux.conf" ]; then
236+
echo "Creating ~/.tmux.conf with shared session settings..."
237+
echo "set -g mouse on" > "$HOME/.tmux.conf"
238+
fi
239+
240+
if ! grep -q "^set -g mouse on$" "$HOME/.tmux.conf"; then
241+
echo "Adding 'set -g mouse on' to ~/.tmux.conf..."
242+
echo "set -g mouse on" >> "$HOME/.tmux.conf"
243+
fi
244+
245+
# Create a new tmux session in detached mode
246+
tmux new-session -d -s ${var.session_name} -c ${var.folder} "\"$GOOSE_CMD\" run --text \"Review your goosehints. Every step of the way, report tasks to Coder with proper descriptions and statuses. Your task at hand: $GOOSE_TASK_PROMPT\" --interactive | tee -a \"$HOME/.goose.log\"; exec bash"
247+
elif [ "${var.experiment_use_screen}" = "true" ]; then
192248
echo "Running Goose in the background..."
193249
194250
# Check if screen is installed
@@ -217,31 +273,11 @@ EOL
217273
export LANG=en_US.UTF-8
218274
export LC_ALL=en_US.UTF-8
219275
220-
# Determine goose command
221-
if command_exists goose; then
222-
GOOSE_CMD=goose
223-
elif [ -f "$HOME/.local/bin/goose" ]; then
224-
GOOSE_CMD="$HOME/.local/bin/goose"
225-
else
226-
echo "Error: Goose is not installed. Please enable install_goose or install it manually."
227-
exit 1
228-
fi
229-
230-
screen -U -dmS goose bash -c "
276+
screen -U -dmS ${var.session_name} bash -c "
231277
cd ${var.folder}
232278
\"$GOOSE_CMD\" run --text \"Review your goosehints. Every step of the way, report tasks to Coder with proper descriptions and statuses. Your task at hand: $GOOSE_TASK_PROMPT\" --interactive | tee -a \"$HOME/.goose.log\"
233279
/bin/bash
234280
"
235-
else
236-
# Check if goose is installed before running
237-
if command_exists goose; then
238-
GOOSE_CMD=goose
239-
elif [ -f "$HOME/.local/bin/goose" ]; then
240-
GOOSE_CMD="$HOME/.local/bin/goose"
241-
else
242-
echo "Error: Goose is not installed. Please enable install_goose or install it manually."
243-
exit 1
244-
fi
245281
fi
246282
EOT
247283
run_on_start = true
@@ -270,18 +306,27 @@ resource "coder_app" "goose" {
270306
exit 1
271307
fi
272308
273-
if [ "${var.experiment_use_screen}" = "true" ]; then
309+
export LANG=en_US.UTF-8
310+
export LC_ALL=en_US.UTF-8
311+
312+
if [ "${var.experiment_use_tmux}" = "true" ]; then
313+
if tmux has-session -t ${var.session_name} 2>/dev/null; then
314+
echo "Attaching to existing Goose tmux session." | tee -a "$HOME/.goose.log"
315+
tmux attach-session -t ${var.session_name}
316+
else
317+
echo "Starting a new Goose tmux session." | tee -a "$HOME/.goose.log"
318+
tmux new-session -s ${var.session_name} -c ${var.folder} "\"$GOOSE_CMD\" run --text \"Review your goosehints. Every step of the way, report tasks to Coder with proper descriptions and statuses. Your task at hand: $GOOSE_TASK_PROMPT\" --interactive | tee -a \"$HOME/.goose.log\"; exec bash"
319+
fi
320+
elif [ "${var.experiment_use_screen}" = "true" ]; then
274321
# Check if session exists first
275-
if ! screen -list | grep -q "goose"; then
322+
if ! screen -list | grep -q "${var.session_name}"; then
276323
echo "Error: No existing Goose session found. Please wait for the script to start it."
277324
exit 1
278325
fi
279326
# Only attach to existing session
280-
screen -xRR goose
327+
screen -xRR ${var.session_name}
281328
else
282329
cd ${var.folder}
283-
export LANG=en_US.UTF-8
284-
export LC_ALL=en_US.UTF-8
285330
"$GOOSE_CMD" run --text "Review goosehints. Your task: $GOOSE_TASK_PROMPT" --interactive
286331
fi
287332
EOT

0 commit comments

Comments
 (0)