-
Notifications
You must be signed in to change notification settings - Fork 80
refactor(claude-code): simplify session resumption logic for standalone and task mode #579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR refactors the session resumption logic for the claude-code module to simplify and improve reliability across both standalone and task modes. The changes remove the problematic remove-last-session-id.sh script that attempted to manage stale session data, replacing it with more robust session validation and detection functions directly in the start script.
Key changes:
- Introduces three new helper functions (
task_session_exists,is_valid_session,has_any_sessions) to improve session state detection - Simplifies the session resumption decision tree by consolidating conditional logic and removing external script dependencies
- Adds comprehensive test coverage for edge cases like partial initialization, session validation, and mode-specific behavior
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
registry/coder/modules/claude-code/scripts/start.sh |
Refactored session resumption logic with new validation functions; removed dependency on external session cleanup script; simplified conditional branches for task vs standalone modes |
registry/coder/modules/claude-code/scripts/remove-last-session-id.sh |
Removed entirely - session cleanup logic no longer needed with improved validation approach |
registry/coder/modules/claude-code/main.tf |
Removed references to remove-last-session-id.sh script; updated local variables to remove unused script encoding |
registry/coder/modules/claude-code/main.test.ts |
Added four new test cases for partial initialization detection, first build scenarios, session continuation, and task mode isolation; updated existing test to create valid JSONL session files with proper content |
registry/coder/modules/claude-code/README.md |
Updated module version to 4.2.3 across all examples; updated version pins in examples (claude_code_version, agentapi_version, boundary_version); removed cli_app from standalone mode example |
…ale, empty, or malformed session files
35C4n0r
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
…start.sh for fresh sessions
…Y to ANTHROPIC_API_KEY
…dd standalone mode configuration
…I_KEY in standalone mode configuration
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
|
|
||
| if [ -d "$project_dir" ] && find "$project_dir" -type f -name "*${TASK_SESSION_ID}*" 2> /dev/null | grep -q .; then | ||
| printf "TASK_SESSION_ID: %s file found\n" "$TASK_SESSION_ID" | ||
| if [ -d "$project_dir" ] && find "$project_dir" -name "*.jsonl" 2> /dev/null | grep -q .; then |
Copilot
AI
Dec 5, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The find command with -name "*.jsonl" will match all files recursively in subdirectories, not just direct children. If the project directory structure includes nested directories with .jsonl files, this could incorrectly report sessions that aren't actually valid Claude session files. Consider adding -maxdepth 1 to restrict the search to immediate children only: find "$project_dir" -maxdepth 1 -name "*.jsonl"
| if [ -d "$project_dir" ] && find "$project_dir" -name "*.jsonl" 2> /dev/null | grep -q .; then | |
| if [ -d "$project_dir" ] && find "$project_dir" -maxdepth 1 -name "*.jsonl" 2> /dev/null | grep -q .; then |
| if ! head -3 "$session_file" | jq empty 2> /dev/null; then | ||
| printf "Session validation failed: invalid JSONL format, removing corrupt file\n" | ||
| rm -f "$session_file" | ||
| return 1 | ||
| fi | ||
|
|
||
| # Check for minimum session content | ||
| # Valid sessions need at least 2 lines: initial message and first response | ||
| local line_count | ||
| line_count=$(wc -l < "$session_file") | ||
| if [ "$line_count" -lt 2 ]; then |
Copilot
AI
Dec 5, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The JSONL validation (line 124) is performed before checking the minimum line count (line 134). For files with only 1 line, head -3 will successfully validate that single line as valid JSON, passing validation, and then fail on the line count check. This ordering works correctly, but for efficiency, consider checking the line count before running jq to avoid unnecessary JSON parsing for files that are too short.
| agent_id = coder_agent.example.id | ||
| workdir = "/home/coder" | ||
| install_claude_code = true | ||
| claude_code_version = "4.2.2" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the version bump script is not foolproof.
Description
Brings session resumption up to speed with current claude-code capabilities, and should make session resumption less prone to errors across the board.
I am still testing this further to ensure that all logic path's are verified.
Type of Change
Module Information
Path:
registry/coder/modules/claude-codeNew version:
v4.2.3Breaking change: [ ] Yes [ ] No
Testing & Validation
bun test)bun fmt)Related Issues