fix(claude-code): derive task session ID from workspace; add transcript_retention_days; emit Stop-hook idle sentinel#866
Open
morganl-ant wants to merge 1 commit intocoder:mainfrom
Conversation
…pt_retention_days; emit Stop-hook idle sentinel The hardcoded TASK_SESSION_ID caused 'Session ID already in use' collisions whenever a home directory was templated or shared across workspaces. Derive a per-workspace UUIDv5 from data.coder_workspace.me.id instead so the session is stable across restarts of the same workspace but unique across workspaces. While here: - Quarantine invalid session files to .bak instead of silently deleting them so transcripts are recoverable. - Add transcript_retention_days input that maps to Claude Code's cleanupPeriodDays setting via /etc/claude-code/managed-settings.d/. - Register a Stop hook that touches ~/.claude-module/last-stop so template authors can wire workspace autostop or activity tracking off the file mtime. Fixes coder#726
matifali
approved these changes
Apr 23, 2026
Member
matifali
left a comment
There was a problem hiding this comment.
@morganl-ant thanks for your contribution. I think we can merge this to main and release a new patch version to unblock your users. Bit for other PRs, let's rebase them once #861 (likely today) lands.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The module pins task-mode Claude sessions to a single hardcoded UUID (
cd32e253-ca16-4fd3-9825-d837e74ae3c2). When a workspace home directory is templated, snapshot-restored, or otherwise shared across workspaces, every workspace tries to claim the same Claude session ID and the CLI rejects the second one with "Session ID X is already in use". See #726.Two related lifecycle gaps while in here:
~/.claude/projects/are never pruned by the module, so long-lived workspaces accumulate unbounded transcript files unless the user knows to setcleanupPeriodDays.Changes
start.sh: deriveTASK_SESSION_IDasuuid5(NAMESPACE_URL, "coder-workspace://" + data.coder_workspace.me.id). The result is stable across restarts of a given workspace and unique across workspaces. Falls back to using the workspace ID directly ifpython3is unavailable, and to the legacy hardcoded value only when no workspace ID is provided.start.sh: whenis_valid_sessionrejects a transcript file, rename it to*.jsonl.bakinstead of deleting it so the conversation is recoverable.main.tf: passARG_WORKSPACE_IDto both scripts; addtranscript_retention_days(number, default null, validated >= 1).install.sh: write/etc/claude-code/managed-settings.d/30-coder-lifecycle.jsoncontaining aStophook that touches~/.claude-module/last-stopon every turn end, pluscleanupPeriodDayswhentranscript_retention_daysis set. The local managed-settings file is read by the Claude CLI on every backend (Anthropic API, Bedrock, Vertex, gateway), so this works regardless of how inference is routed. Skips with a warning if/etc/claude-codeis not writable.deriveTaskSessionIdhelper that recomputes the UUIDv5 from the rendered script (the coder provider returns environment-dependent workspace IDs). Adds tests for the derived ID, the.bakquarantine, and the lifecycle settings file.transcript_retention_days.Relationship to #861
#861 deletes
start.shand removes session/continue handling from this module entirely. If that direction lands, the session ID derivation and quarantine logic here should move to whatever module owns Tasks start orchestration. Filing this againstmainso #726 is fixed for current users in the meantime; thetranscript_retention_daysinput and Stop-hook drop-in are install-time and remain useful regardless.Validation
terraform fmtcleanterraform validatecleanterraform test19/19 passshellcheck --severity=warningclean on both scriptsbash -nclean on both scriptsFixes #726
Disclosure: I work on Claude Code at Anthropic. This is one of a small set of contributions we are proposing to the
claude-codemodule; happy to discuss the overall direction in whichever venue the maintainers prefer.