Skip to content

Commit 868b0a6

Browse files
feat: add session resumption support for Copilot CLI and update related configurations
1 parent 8151766 commit 868b0a6

File tree

5 files changed

+80
-30
lines changed

5 files changed

+80
-30
lines changed

registry/coder-labs/modules/copilot-cli/README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ module "copilot_cli" {
3434

3535
## Examples
3636

37-
### Usage with Tasks (Recommended for Development)
37+
### Usage with Tasks
3838

39-
For development environments where you want Copilot CLI to have full access to tools without prompting:
39+
For development environments where you want Copilot CLI to have full access to tools and automatically resume sessions:
4040

4141
```tf
4242
data "coder_parameter" "ai_prompt" {
@@ -56,6 +56,7 @@ module "copilot_cli" {
5656
ai_prompt = data.coder_parameter.ai_prompt.value
5757
copilot_model = "claude-sonnet-4.5"
5858
allow_all_tools = true
59+
resume_session = true
5960
6061
system_prompt = <<-EOT
6162
You are a helpful AI coding assistant working in a development environment.
@@ -205,6 +206,26 @@ This module works with multiple GitHub authentication methods in priority order:
205206

206207
> **Note**: OAuth tokens work best with Copilot CLI. Personal Access Tokens may have limited functionality.
207208
209+
## Session Resumption
210+
211+
By default (`resume_session = true`), this module automatically resumes the latest Copilot CLI session when the workspace is restarted. This provides a seamless experience where:
212+
213+
- **Previous conversations continue** - No need to re-establish context
214+
- **No duplicate prompts** - Initial prompts are only sent on first workspace creation
215+
- **Workspace restart handling** - Automatically detects and resumes existing sessions
216+
217+
```tf
218+
module "copilot_cli" {
219+
source = "registry.coder.com/coder-labs/copilot-cli/coder"
220+
version = "1.0.0"
221+
agent_id = coder_agent.example.id
222+
workdir = "/home/coder/project"
223+
224+
resume_session = true # Default: automatically resume sessions
225+
# resume_session = false # Always start fresh sessions
226+
}
227+
```
228+
208229
## Task Reporting
209230

210231
When `report_tasks = true` (default), this module automatically configures and starts the **Coder MCP server** for task reporting integration:

registry/coder-labs/modules/copilot-cli/main.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,20 @@ describe("copilot-cli module", async () => {
309309
expect(githubTokenEnv.name).toBe("GITHUB_TOKEN");
310310
expect(githubTokenEnv.value).toBe("test_github_token_123");
311311
});
312+
313+
it("supports resume session configuration", async () => {
314+
const resumeSessionVars = {
315+
...requiredVars,
316+
resume_session: false,
317+
};
318+
319+
const state = await runTerraformApply(moduleDir, resumeSessionVars);
320+
321+
const statusSlugEnv = findResourceInstance(
322+
state,
323+
"coder_env",
324+
"mcp_app_status_slug",
325+
);
326+
expect(statusSlugEnv).toBeDefined();
327+
});
312328
});

registry/coder-labs/modules/copilot-cli/main.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ variable "cli_app_display_name" {
149149
default = "Copilot CLI"
150150
}
151151

152+
variable "resume_session" {
153+
type = bool
154+
description = "Whether to automatically resume the latest Copilot CLI session on workspace restart."
155+
default = true
156+
}
157+
152158
variable "pre_install_script" {
153159
type = string
154160
description = "Custom script to run before configuring Copilot CLI."
@@ -237,6 +243,7 @@ module "agentapi" {
237243
ARG_DENY_TOOLS='${join(",", var.deny_tools)}' \
238244
ARG_TRUSTED_DIRECTORIES='${join(",", var.trusted_directories)}' \
239245
ARG_EXTERNAL_AUTH_ID='${var.external_auth_id}' \
246+
ARG_RESUME_SESSION='${var.resume_session}' \
240247
/tmp/start.sh
241248
EOT
242249

registry/coder-labs/modules/copilot-cli/scripts/install.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,11 @@ setup_coder_mcp_server() {
137137
#!/usr/bin/env bash
138138
set -e
139139
140-
# --- Set environment variables ---
141140
export CODER_MCP_APP_STATUS_SLUG="${ARG_MCP_APP_STATUS_SLUG}"
142141
export CODER_MCP_AI_AGENTAPI_URL="http://localhost:3284"
143142
export CODER_AGENT_URL="${CODER_AGENT_URL}"
144143
export CODER_AGENT_TOKEN="${CODER_AGENT_TOKEN}"
145144
146-
# --- Launch the MCP server ---
147145
exec coder exp mcp server
148146
EOF
149147
)
@@ -197,6 +195,16 @@ add_custom_mcp_servers() {
197195
fi
198196
}
199197

198+
configure_copilot_model() {
199+
if [ -n "$ARG_COPILOT_MODEL" ] && [ "$ARG_COPILOT_MODEL" != "claude-sonnet-4" ]; then
200+
echo "Setting Copilot CLI model to: $ARG_COPILOT_MODEL"
201+
copilot config model "$ARG_COPILOT_MODEL" || {
202+
echo "WARNING: Failed to set model via copilot config, will use environment variable fallback"
203+
export COPILOT_MODEL="$ARG_COPILOT_MODEL"
204+
}
205+
fi
206+
}
207+
200208
configure_coder_integration() {
201209
if [ "$ARG_REPORT_TASKS" = "true" ] && [ -n "$ARG_MCP_APP_STATUS_SLUG" ]; then
202210
echo "Configuring Copilot CLI task reporting..."
@@ -214,6 +222,7 @@ validate_prerequisites
214222
install_copilot_cli
215223
check_github_authentication
216224
setup_copilot_configurations
225+
configure_copilot_model
217226
configure_coder_integration
218227

219228
echo "Copilot CLI module setup completed."

registry/coder-labs/modules/copilot-cli/scripts/start.sh

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ ARG_ALLOW_TOOLS=${ARG_ALLOW_TOOLS:-}
1717
ARG_DENY_TOOLS=${ARG_DENY_TOOLS:-}
1818
ARG_TRUSTED_DIRECTORIES=${ARG_TRUSTED_DIRECTORIES:-}
1919
ARG_EXTERNAL_AUTH_ID=${ARG_EXTERNAL_AUTH_ID:-github}
20+
ARG_RESUME_SESSION=${ARG_RESUME_SESSION:-true}
2021

2122
validate_copilot_installation() {
2223
if ! command_exists copilot; then
@@ -62,23 +63,17 @@ Task: $ARG_AI_PROMPT"
6263
fi
6364
}
6465

65-
configure_copilot_model() {
66-
if [ -n "$ARG_COPILOT_MODEL" ]; then
67-
case "$ARG_COPILOT_MODEL" in
68-
"gpt-5")
69-
export COPILOT_MODEL="gpt-5"
70-
;;
71-
"claude-sonnet-4")
72-
export COPILOT_MODEL="claude-sonnet-4"
73-
;;
74-
"claude-sonnet-4.5")
75-
export COPILOT_MODEL="claude-sonnet-4.5"
76-
;;
77-
*)
78-
echo "WARNING: Unknown model '$ARG_COPILOT_MODEL'. Using default."
79-
;;
80-
esac
66+
check_existing_session() {
67+
if [ "$ARG_RESUME_SESSION" = "true" ]; then
68+
if copilot --help > /dev/null 2>&1; then
69+
local session_dir="$HOME/.copilot/sessions"
70+
if [ -d "$session_dir" ] && [ "$(ls -A "$session_dir" 2> /dev/null)" ]; then
71+
echo "Found existing Copilot CLI sessions. Resume mode enabled."
72+
return 0
73+
fi
74+
fi
8175
fi
76+
return 1
8277
}
8378

8479
setup_github_authentication() {
@@ -117,20 +112,22 @@ start_agentapi() {
117112
echo "Starting in directory: $ARG_WORKDIR"
118113
cd "$ARG_WORKDIR"
119114

120-
build_copilot_args
121-
122-
if [ ${#COPILOT_ARGS[@]} -gt 0 ]; then
123-
echo "Copilot arguments: ${COPILOT_ARGS[*]}"
124-
agentapi server --type claude --term-width 120 --term-height 40 -- copilot "${COPILOT_ARGS[@]}"
115+
if check_existing_session; then
116+
echo "Resuming latest Copilot CLI session..."
117+
agentapi server --type claude --term-width 120 --term-height 40 -- copilot --resume
125118
else
126-
agentapi server --type claude --term-width 120 --term-height 40 -- copilot
119+
echo "Starting new Copilot CLI session..."
120+
build_copilot_args
121+
122+
if [ ${#COPILOT_ARGS[@]} -gt 0 ]; then
123+
echo "Copilot arguments: ${COPILOT_ARGS[*]}"
124+
agentapi server --type claude --term-width 120 --term-height 40 -- copilot "${COPILOT_ARGS[@]}"
125+
else
126+
agentapi server --type claude --term-width 120 --term-height 40 -- copilot
127+
fi
127128
fi
128129
}
129130

130-
configure_copilot_model
131-
132-
echo "COPILOT_MODEL=${ARG_COPILOT_MODEL:-${COPILOT_MODEL:-not set}}"
133-
134131
setup_github_authentication
135132
validate_copilot_installation
136133
start_agentapi

0 commit comments

Comments
 (0)