Skip to content

Commit 8cfabbf

Browse files
fix: use specific session id's when continue is true
1 parent 3ff8f07 commit 8cfabbf

File tree

2 files changed

+20
-80
lines changed

2 files changed

+20
-80
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,24 +198,22 @@ describe("claude-code", async () => {
198198
expect(startLog.stdout).toContain(`--model ${model}`);
199199
});
200200

201-
test("claude-continue-previous-conversation", async () => {
201+
test("claude-continue-resume-existing-session", async () => {
202202
const { id } = await setup({
203203
moduleVariables: {
204204
continue: "true",
205205
ai_prompt: "test prompt",
206206
},
207207
});
208208

209-
// Create a mock session file so get_latest_session_id can extract the session ID
210-
const projectDir = "/home/coder/project";
211-
const projectDirName = projectDir.replace(/\//g, "-");
212-
const sessionDir = `/home/coder/.claude/projects/${projectDirName}`;
213-
const testSessionId = "test-session-123";
209+
// Create a mock session file with the predefined task session ID
210+
const taskSessionId = "cd32e253-ca16-4fd3-9825-d837e74ae3c2";
211+
const sessionDir = `/home/coder/.claude/projects/-home-coder-project`;
214212
await execContainer(id, ["mkdir", "-p", sessionDir]);
215213
await execContainer(id, [
216214
"bash",
217215
"-c",
218-
`echo '{"type":"user","isSidechain":false,"sessionId":"${testSessionId}"}' > ${sessionDir}/session-123.jsonl`,
216+
`touch ${sessionDir}/session-${taskSessionId}.jsonl`,
219217
]);
220218

221219
await execModuleScript(id);
@@ -226,7 +224,8 @@ describe("claude-code", async () => {
226224
"cat /home/coder/.claude-module/agentapi-start.log",
227225
]);
228226
expect(startLog.stdout).toContain("--resume");
229-
expect(startLog.stdout).toContain(testSessionId);
227+
expect(startLog.stdout).toContain(taskSessionId);
228+
expect(startLog.stdout).toContain("Resuming existing task session");
230229
});
231230

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

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

Lines changed: 13 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -64,55 +64,10 @@ function validate_claude_installation() {
6464
fi
6565
}
6666

67-
has_session_for_workdir() {
68-
local workdir="$1"
69-
local workdir_abs=$(realpath "$workdir" 2> /dev/null || echo "$workdir")
70-
71-
local project_dir_name=$(echo "$workdir_abs" | sed 's|/|-|g')
72-
local project_sessions_dir="$HOME/.claude/projects/$project_dir_name"
73-
74-
if [ -d "$project_sessions_dir" ]; then
75-
for file in "$project_sessions_dir"/*.jsonl; do
76-
[ -f "$file" ] || continue
77-
if grep -q '"type":"user"' "$file" 2> /dev/null; then
78-
if grep -q '"isSidechain":false' "$file" 2> /dev/null; then
79-
return 0
80-
fi
81-
fi
82-
done
83-
fi
84-
return 1
85-
}
86-
87-
get_latest_session_id() {
88-
local workdir="$1"
89-
local workdir_abs=$(realpath "$workdir" 2> /dev/null || echo "$workdir")
90-
local project_dir_name=$(echo "$workdir_abs" | sed 's|/|-|g')
91-
local project_sessions_dir="$HOME/.claude/projects/$project_dir_name"
92-
93-
if [ ! -d "$project_sessions_dir" ]; then
94-
return 1
95-
fi
96-
97-
local latest_session_id=""
98-
local latest_time=0
99-
100-
for file in "$project_sessions_dir"/*.jsonl; do
101-
[ -f "$file" ] || continue
67+
TASK_SESSION_ID="cd32e253-ca16-4fd3-9825-d837e74ae3c2"
10268

103-
if grep -q '"type":"user"' "$file" 2> /dev/null; then
104-
if grep -q '"isSidechain":false' "$file" 2> /dev/null; then
105-
local file_time=$(stat -c %Y "$file" 2> /dev/null || stat -f %m "$file" 2> /dev/null || echo 0)
106-
if [ "$file_time" -gt "$latest_time" ]; then
107-
latest_time=$file_time
108-
latest_session_id=$(grep '"isSidechain":false' "$file" | grep '"sessionId"' | head -1 | grep -o '"sessionId":"[^"]*"' | cut -d'"' -f4)
109-
fi
110-
fi
111-
fi
112-
done
113-
114-
if [ -n "$latest_session_id" ]; then
115-
echo "$latest_session_id"
69+
task_session_exists() {
70+
if find "$HOME/.claude" -type f -name "*${TASK_SESSION_ID}*" 2> /dev/null | grep -q .; then
11671
return 0
11772
else
11873
return 1
@@ -140,38 +95,24 @@ function start_agentapi() {
14095
ARGS+=(--dangerously-skip-permissions)
14196
fi
14297
elif [ "$ARG_CONTINUE" = "true" ]; then
143-
if has_session_for_workdir "$ARG_WORKDIR"; then
144-
local session_id=$(get_latest_session_id "$ARG_WORKDIR")
145-
if [ -n "$session_id" ]; then
146-
echo "Session detected for workdir: $ARG_WORKDIR"
147-
echo "Latest session ID: $session_id"
148-
ARGS+=(--resume "$session_id")
149-
if [ "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" = "true" ]; then
150-
ARGS+=(--dangerously-skip-permissions)
151-
fi
152-
echo "Resuming existing session with explicit session ID"
153-
else
154-
echo "Could not extract session ID, starting new session"
155-
if [ -n "$ARG_AI_PROMPT" ]; then
156-
ARGS+=(--dangerously-skip-permissions "$ARG_AI_PROMPT")
157-
echo "Starting new session with prompt"
158-
else
159-
if [ "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" = "true" ]; then
160-
ARGS+=(--dangerously-skip-permissions)
161-
fi
162-
echo "Starting claude code session"
163-
fi
98+
if task_session_exists; then
99+
echo "Task session detected (ID: $TASK_SESSION_ID)"
100+
ARGS+=(--resume "$TASK_SESSION_ID")
101+
if [ "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" = "true" ]; then
102+
ARGS+=(--dangerously-skip-permissions)
164103
fi
104+
echo "Resuming existing task session"
165105
else
166-
echo "No existing session for workdir: $ARG_WORKDIR"
106+
echo "No existing task session found"
107+
ARGS+=(--session-id "$TASK_SESSION_ID")
167108
if [ -n "$ARG_AI_PROMPT" ]; then
168109
ARGS+=(--dangerously-skip-permissions "$ARG_AI_PROMPT")
169-
echo "Starting new session with prompt"
110+
echo "Starting new task session with prompt"
170111
else
171112
if [ "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" = "true" ]; then
172113
ARGS+=(--dangerously-skip-permissions)
173114
fi
174-
echo "Starting claude code session"
115+
echo "Starting new task session"
175116
fi
176117
fi
177118
else

0 commit comments

Comments
 (0)