Skip to content

Commit 2053af3

Browse files
fix: implement session ID extraction in start script and update tests to validate session resumption
1 parent 60cc4ee commit 2053af3

File tree

2 files changed

+63
-10
lines changed

2 files changed

+63
-10
lines changed

registry/coder/modules/claude-code/main.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,16 @@ describe("claude-code", async () => {
206206
},
207207
});
208208

209+
// Create a mock session file so get_latest_session_id can extract the session ID
209210
const projectDir = "/home/coder/project";
210211
const projectDirName = projectDir.replace(/\//g, "-");
211212
const sessionDir = `/home/coder/.claude/projects/${projectDirName}`;
213+
const testSessionId = "test-session-123";
212214
await execContainer(id, ["mkdir", "-p", sessionDir]);
213215
await execContainer(id, [
214216
"bash",
215217
"-c",
216-
`echo '{"type":"user","isSidechain":false}' > ${sessionDir}/session-123.jsonl`,
218+
`echo '{"type":"user","isSidechain":false,"sessionId":"${testSessionId}"}' > ${sessionDir}/session-123.jsonl`,
217219
]);
218220

219221
await execModuleScript(id);
@@ -223,7 +225,8 @@ describe("claude-code", async () => {
223225
"-c",
224226
"cat /home/coder/.claude-module/agentapi-start.log",
225227
]);
226-
expect(startLog.stdout).toContain("--continue");
228+
expect(startLog.stdout).toContain("--resume");
229+
expect(startLog.stdout).toContain(testSessionId);
227230
});
228231

229232
test("pre-post-install-scripts", async () => {

registry/coder/modules/claude-code/scripts/start.sh

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,41 @@ has_session_for_workdir() {
6464
return 1
6565
}
6666

67+
get_latest_session_id() {
68+
local workdir="$1"
69+
local workdir_abs=$(realpath "$workdir" 2> /dev/null || echo "$workdir")
70+
local project_dir_name=$(echo "$workdir_abs" | sed 's|/|-|g')
71+
local project_sessions_dir="$HOME/.claude/projects/$project_dir_name"
72+
73+
if [ ! -d "$project_sessions_dir" ]; then
74+
return 1
75+
fi
76+
77+
local latest_session_id=""
78+
local latest_time=0
79+
80+
for file in "$project_sessions_dir"/*.jsonl; do
81+
[ -f "$file" ] || continue
82+
83+
if grep -q '"type":"user"' "$file" 2> /dev/null; then
84+
if grep -q '"isSidechain":false' "$file" 2> /dev/null; then
85+
local file_time=$(stat -c %Y "$file" 2> /dev/null || stat -f %m "$file" 2> /dev/null || echo 0)
86+
if [ "$file_time" -gt "$latest_time" ]; then
87+
latest_time=$file_time
88+
latest_session_id=$(grep '"isSidechain":false' "$file" | grep '"sessionId"' | head -1 | grep -o '"sessionId":"[^"]*"' | cut -d'"' -f4)
89+
fi
90+
fi
91+
fi
92+
done
93+
94+
if [ -n "$latest_session_id" ]; then
95+
echo "$latest_session_id"
96+
return 0
97+
else
98+
return 1
99+
fi
100+
}
101+
67102
ARGS=()
68103

69104
function start_agentapi() {
@@ -81,24 +116,39 @@ function start_agentapi() {
81116
if [ -n "$ARG_RESUME_SESSION_ID" ]; then
82117
echo "Using explicit resume_session_id: $ARG_RESUME_SESSION_ID"
83118
ARGS+=(--resume "$ARG_RESUME_SESSION_ID")
84-
if [ -n "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" ]; then
119+
if [ "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" = "true" ]; then
85120
ARGS+=(--dangerously-skip-permissions)
86121
fi
87122
elif [ "$ARG_CONTINUE" = "true" ]; then
88123
if has_session_for_workdir "$ARG_WORKDIR"; then
89-
echo "Session detected for workdir: $ARG_WORKDIR"
90-
ARGS+=(--continue)
91-
if [ -n "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" ]; then
92-
ARGS+=(--dangerously-skip-permissions)
124+
local session_id=$(get_latest_session_id "$ARG_WORKDIR")
125+
if [ -n "$session_id" ]; then
126+
echo "Session detected for workdir: $ARG_WORKDIR"
127+
echo "Latest session ID: $session_id"
128+
ARGS+=(--resume "$session_id")
129+
if [ "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" = "true" ]; then
130+
ARGS+=(--dangerously-skip-permissions)
131+
fi
132+
echo "Resuming existing session with explicit session ID"
133+
else
134+
echo "Could not extract session ID, starting new session"
135+
if [ -n "$ARG_AI_PROMPT" ]; then
136+
ARGS+=(--dangerously-skip-permissions "$ARG_AI_PROMPT")
137+
echo "Starting new session with prompt"
138+
else
139+
if [ "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" = "true" ]; then
140+
ARGS+=(--dangerously-skip-permissions)
141+
fi
142+
echo "Starting claude code session"
143+
fi
93144
fi
94-
echo "Resuming existing session"
95145
else
96146
echo "No existing session for workdir: $ARG_WORKDIR"
97147
if [ -n "$ARG_AI_PROMPT" ]; then
98148
ARGS+=(--dangerously-skip-permissions "$ARG_AI_PROMPT")
99149
echo "Starting new session with prompt"
100150
else
101-
if [ -n "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" ]; then
151+
if [ "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" = "true" ]; then
102152
ARGS+=(--dangerously-skip-permissions)
103153
fi
104154
echo "Starting claude code session"
@@ -110,7 +160,7 @@ function start_agentapi() {
110160
ARGS+=(--dangerously-skip-permissions "$ARG_AI_PROMPT")
111161
echo "Starting new session with prompt"
112162
else
113-
if [ -n "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" ]; then
163+
if [ "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" = "true" ]; then
114164
ARGS+=(--dangerously-skip-permissions)
115165
fi
116166
echo "Starting claude code session"

0 commit comments

Comments
 (0)