Skip to content

Commit afa23b8

Browse files
feat(goose): Add tmux support and Session Name variable to Goose (#113)
- Add Multiplexer, and tmux with mouse support. - Add check to make sure tmux and screen are not set at the same time. - Add Variable for session name so the name of the screen or tmux session can be customized. Tested in dev environment. Reference: #31
1 parent fae5215 commit afa23b8

File tree

2 files changed

+84
-35
lines changed

2 files changed

+84
-35
lines changed

registry/coder/modules/goose/README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Run the [Goose](https://block.github.io/goose/) agent in your workspace to gener
1414
```tf
1515
module "goose" {
1616
source = "registry.coder.com/coder/goose/coder"
17-
version = "1.1.1"
17+
version = "1.2.0"
1818
agent_id = coder_agent.example.id
1919
folder = "/home/coder"
2020
install_goose = true
@@ -24,14 +24,14 @@ module "goose" {
2424

2525
## Prerequisites
2626

27-
- `screen` must be installed in your workspace to run Goose in the background
27+
- `screen` or `tmux` must be installed in your workspace to run Goose in the background
2828
- You must add the [Coder Login](https://registry.coder.com/modules/coder-login) module to your template
2929

3030
The `codercom/oss-dogfood:latest` container image can be used for testing on container-based workspaces.
3131

3232
## Examples
3333

34-
Your workspace must have `screen` installed to use this.
34+
Your workspace must have `screen` or `tmux` installed to use the background session functionality.
3535

3636
### Run in the background and report tasks (Experimental)
3737

@@ -90,7 +90,7 @@ resource "coder_agent" "main" {
9090
module "goose" {
9191
count = data.coder_workspace.me.start_count
9292
source = "registry.coder.com/coder/goose/coder"
93-
version = "1.1.1"
93+
version = "1.2.0"
9494
agent_id = coder_agent.example.id
9595
folder = "/home/coder"
9696
install_goose = true
@@ -99,8 +99,12 @@ module "goose" {
9999
# Enable experimental features
100100
experiment_report_tasks = true
101101
102-
# Run Goose in the background
102+
# Run Goose in the background with screen (pick one: screen or tmux)
103103
experiment_use_screen = true
104+
# experiment_use_tmux = true # Alternative: use tmux instead of screen
105+
106+
# Optional: customize the session name (defaults to "goose")
107+
# session_name = "goose-session"
104108
105109
# Avoid configuring Goose manually
106110
experiment_auto_configure = true
@@ -143,12 +147,12 @@ Note: The indentation in the heredoc is preserved, so you can write the YAML nat
143147

144148
## Run standalone
145149

146-
Run Goose as a standalone app in your workspace. This will install Goose and run it directly without using screen or any task reporting to the Coder UI.
150+
Run Goose as a standalone app in your workspace. This will install Goose and run it directly without using screen or tmux, and without any task reporting to the Coder UI.
147151

148152
```tf
149153
module "goose" {
150154
source = "registry.coder.com/coder/goose/coder"
151-
version = "1.1.1"
155+
version = "1.2.0"
152156
agent_id = coder_agent.example.id
153157
folder = "/home/coder"
154158
install_goose = true

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)