Skip to content

Commit 2f65293

Browse files
fix: improve session resumption logic in Copilot CLI script to identify and resume the latest session if more than one session file exists
1 parent 7c1b2a4 commit 2f65293

File tree

1 file changed

+21
-6
lines changed
  • registry/coder-labs/modules/copilot-cli/scripts

1 file changed

+21
-6
lines changed

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,23 @@ check_existing_session() {
7373
if copilot --help > /dev/null 2>&1; then
7474
local session_dir="$HOME/.copilot/history-session-state"
7575
if [ -d "$session_dir" ] && [ -n "$(ls "$session_dir"/session_*_*.json 2> /dev/null)" ]; then
76-
echo "Found existing Copilot CLI sessions. Resume mode enabled."
77-
return 0
76+
local latest_session_file
77+
latest_session_file=$(ls "$session_dir"/session_*_*.json 2> /dev/null | sort -t_ -k3 -n -r | head -n 1)
78+
79+
if [ -n "$latest_session_file" ]; then
80+
local session_id
81+
session_id=$(basename "$latest_session_file" | sed 's/session_\(.*\)_[0-9]*.json/\1/')
82+
83+
if [ -n "$session_id" ]; then
84+
echo "Found existing Copilot CLI sessions. Will resume latest: $session_id"
85+
echo "$session_id"
86+
return 0
87+
fi
88+
fi
7889
fi
7990
fi
8091
fi
92+
echo ""
8193
return 1
8294
}
8395

@@ -119,13 +131,16 @@ start_agentapi() {
119131

120132
build_copilot_args
121133

122-
if check_existing_session; then
123-
echo "Resuming latest Copilot CLI session..."
134+
local session_id
135+
session_id=$(check_existing_session)
136+
137+
if [ -n "$session_id" ]; then
138+
echo "Resuming Copilot CLI session: $session_id"
124139
if [ ${#COPILOT_ARGS[@]} -gt 0 ]; then
125140
echo "Copilot arguments: ${COPILOT_ARGS[*]}"
126-
agentapi server --type copilot --term-width 120 --term-height 40 -- copilot --resume "${COPILOT_ARGS[@]}"
141+
agentapi server --type copilot --term-width 120 --term-height 40 -- copilot --resume "$session_id" "${COPILOT_ARGS[@]}"
127142
else
128-
agentapi server --type copilot --term-width 120 --term-height 40 -- copilot --resume
143+
agentapi server --type copilot --term-width 120 --term-height 40 -- copilot --resume "$session_id"
129144
fi
130145
else
131146
echo "Starting new Copilot CLI session..."

0 commit comments

Comments
 (0)