From bc6e54c4879f7d114037f75be7ffa735d76216c1 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Tue, 22 Apr 2025 10:24:58 -0500 Subject: [PATCH] tmux support for goose --- goose/README.md | 7 +++--- goose/main.tf | 62 +++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/goose/README.md b/goose/README.md index ff28fcc1..8e617d56 100644 --- a/goose/README.md +++ b/goose/README.md @@ -24,14 +24,14 @@ module "goose" { ### Prerequisites -- `screen` must be installed in your workspace to run Goose in the background +- `screen` or `tmux` must be installed in your workspace to run Goose 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. ## Examples -Your workspace must have `screen` installed to use this. +Your workspace must have `screen` or `tmux` installed to use this. ### Run in the background and report tasks (Experimental) @@ -99,8 +99,9 @@ module "goose" { # Enable experimental features experiment_report_tasks = true - # Run Goose in the background + # Run Goose in the background (use only one of these options) experiment_use_screen = true + # experiment_use_tmux = true # Avoid configuring Goose manually experiment_auto_configure = true diff --git a/goose/main.tf b/goose/main.tf index 0043000e..05127074 100644 --- a/goose/main.tf +++ b/goose/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 Goose in the background." + default = false +} + variable "experiment_report_tasks" { type = bool description = "Whether to enable task reporting." @@ -187,8 +193,42 @@ EOL mkdir -p "$HOME/.config/goose" echo "$GOOSE_SYSTEM_PROMPT" > "$HOME/.config/goose/.goosehints" + # 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 + + # Determine goose command + if command_exists goose; then + GOOSE_CMD=goose + elif [ -f "$HOME/.local/bin/goose" ]; then + GOOSE_CMD="$HOME/.local/bin/goose" + else + echo "Error: Goose is not installed. Please enable install_goose or install it manually." + exit 1 + fi + + # Run with tmux if enabled + if [ "${var.experiment_use_tmux}" = "true" ]; then + echo "Running Goose 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/.goose.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 goose -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\"" # Run with screen if enabled - if [ "${var.experiment_use_screen}" = "true" ]; then + elif [ "${var.experiment_use_screen}" = "true" ]; then echo "Running Goose in the background..." # Check if screen is installed @@ -217,16 +257,6 @@ EOL export LANG=en_US.UTF-8 export LC_ALL=en_US.UTF-8 - # Determine goose command - if command_exists goose; then - GOOSE_CMD=goose - elif [ -f "$HOME/.local/bin/goose" ]; then - GOOSE_CMD="$HOME/.local/bin/goose" - else - echo "Error: Goose is not installed. Please enable install_goose or install it manually." - exit 1 - fi - screen -U -dmS goose bash -c " cd ${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\" @@ -270,7 +300,15 @@ resource "coder_app" "goose" { exit 1 fi - if [ "${var.experiment_use_screen}" = "true" ]; then + if [ "${var.experiment_use_tmux}" = "true" ]; then + if tmux has-session -t goose 2>/dev/null; then + echo "Attaching to existing Goose tmux session." | tee -a "$HOME/.goose.log" + tmux attach-session -t goose + else + echo "Starting a new Goose tmux session." | tee -a "$HOME/.goose.log" + tmux new-session -s goose -c ${var.folder} "$GOOSE_CMD run --text \"Review goosehints. Your task: $GOOSE_TASK_PROMPT\" --interactive | tee -a \"$HOME/.goose.log\"; exec bash" + fi + elif [ "${var.experiment_use_screen}" = "true" ]; then # Check if session exists first if ! screen -list | grep -q "goose"; then echo "Error: No existing Goose session found. Please wait for the script to start it."