Skip to content

feat(claude-code): run the Bash tool in bash and prune stale session disk - #298

Open
gatezh wants to merge 5 commits into
claude-code/vendor-marketplace-autoupdatefrom
claude-code/bash-shell-and-disk-prune
Open

gatezh wants to merge 5 commits into
claude-code/vendor-marketplace-autoupdatefrom
claude-code/bash-shell-and-disk-prune

Conversation

@gatezh

@gatezh gatezh commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

What

In claude-code and claude-code-sandbox, Claude Code's Bash tool runs bash instead of zsh. A new managed SessionStart hook also removes disk space that earlier sessions left behind, without touching running sessions or unsaved git work.

Why

Shell. The image sets SHELL=/usr/bin/zsh, so the Bash tool ran zsh, while Claude writes bash. Across 85 session transcripts from one downstream project, about 330 Bash calls in 63 sessions failed on zsh-only behavior:

Failures Error zsh cause
167 no matches found: --include=*.md an unmatched glob aborts the command (NOMATCH)
~100 ===== not found a word starting with = becomes a command-path lookup (EQUALS)
37 no such file or directory: git -c safe.directory=… $CMD args doesn't word-split
18 parse error near () oh-my-zsh's g alias breaks g() { … }
5 bad substitution $B:src/… is read as a :s modifier

Each one cost a retry. The interactive terminal stays zsh.

Disk. The container's disk is the host's. In the same project, five sessions had each cloned the same CMS repo into their scratchpads and installed its packages (~730 MB each). npx had fetched the Sanity CLI twice, once as sanity and once as sanity@latest (555 MB each), and the npm cache held 1.6 GB. Claude Code's own sweep deletes a scratchpad only together with its transcript, after cleanupPeriodDays (30 days by default; #173 proposes 365).

Changes

  • Dockerfile: ENV CLAUDE_CODE_SHELL=/bin/bash, which Claude Code reads before $SHELL. It's set at the end of the shared stage, so the Chromium and Claude Code layers stay cached. The script is installed as /usr/local/bin/prune-claude-disk.

  • managed-settings.json:

    • A second SessionStart hook, prune-claude-disk. It's async, with matcher startup|resume.
    • A claudeMd paragraph: keep one checkout per other repo at ~/src/<repo>, don't use npx <pkg>@latest, and clear large scratchpad contents at the end of a task.
  • prune-claude-disk.sh (new). It deletes:

    • another session's scratchpad dir (<tmp>/claude-<uid>/<project>/<session-uuid>/) when all of these hold:

      • the session isn't running. Claude Code lists running sessions in $CLAUDE_CONFIG_DIR/sessions/*.json.
      • neither its transcript nor any of its files changed within SCRATCH_DAYS (1).
      • no repo inside has uncommitted, stashed or unpushed work. Unpushed work includes commits on a detached HEAD, and a repo git can't read counts as unsaved.

      Kept scratchpads are named to Claude in a systemMessage. Linked worktrees it deletes are pruned from their main repo, so their branch isn't left "already used by worktree". <tmp> follows CLAUDE_CODE_TMPDIR, then $TMPDIR, then /tmp. Claude Code's other dirs there (bash-edit-diff/, the s/<hash> short scratchpad) are skipped.

    • _npx entries unused for NPX_DAYS (7). npx bumps an entry's mtime on each use. An entry is skipped when a running npm exec names its package or one of its bins, such as the Playwright plugin's MCP server.

    • the npm cache, once it passes CACHE_MAX_MB (1024). The path comes from npm config get cache.

    The script prints only JSON.

  • verify-image.sh: checks the env value, /bin/bash, the hook wiring and the claudeMd text. It runs the script in a throwaway HOME and temp root: a stale clean scratchpad must be deleted, while a stale one whose only commit is on a detached HEAD and a recent one must be kept.

  • claude-code/README.md: new section "Claude Code shell and disk cleanup", including how to switch back to zsh.

Notes

  • Why an image ENV, not managed env: a managed env value overrides user settings, project settings and containerEnv (settings-reference, env). Nobody could switch back to zsh. With the ENV, a settings env or containerEnv can override it. CLAUDE_CODE_SHELL is documented on the env-vars page and isn't on the list of variables Claude Code ignores in env.

  • Why a setting, not a PreToolUse hook: a hook would have to recognize every zsh pattern, and each warning would cost a round trip. The setting removes the whole class of failures.

  • Async: the first run in a bloated container walks gigabytes. The async hook's systemMessage reaches Claude on its next turn and isn't shown to the user. Claude Code doesn't enforce timeout on async hooks, so none is set.

  • Native options checked:

    • Claude Code's retention sweep only removes scratchpads together with transcripts.
    • npm cache npx rm deletes the same way, with no age filter.
    • npm cache verify garbage-collects but has no size cap.

    The script uses Claude Code's own sessions/ registry for liveness.

  • npm cache clean --force racing an install: probed. Four concurrent npm install --prefer-offline runs all succeeded while the cache was wiped, because npm refetches.

  • Tested:

    • A fixture of 11 sessions plus the two non-session dirs ran in the published claude-code:latest with the script mounted. It covers clean, detached HEAD, stash-only, a repo 6 levels deep, a dirty worktree, no remote, corrupt HEAD, a running session, a fresh session with no transcript, a clean worktree, and a read-only tree. Only the clean ones were deleted.
    • A running npx server's entry, backdated 10 days, was kept.
    • The new verify-image.sh block passes there, and fails with this PR's first version of the script.
    • shellcheck and bash -n are clean. Hadolint v2.12.0 shows no findings on master or the branch. All .sh files are 100755.
    • Not built locally; CI builds the image.
  • Owner's call: the limit variables keep their unprefixed names (SCRATCH_DAYS, NPX_DAYS, CACHE_MAX_MB).

  • ralphex-fe is left out: it has its own managed settings and runs unattended claude -p.

  • Wiki: no page goes stale. After the merge, Docker-Disk-Maintenance could link to the new README section.

…disk

CLAUDE_CODE_SHELL=/bin/bash in managed settings: zsh-only behavior (NOMATCH,
EQUALS, no word splitting, oh-my-zsh aliases) failed ~330 Bash calls across
63 sessions in one downstream project.

prune-claude-disk, an async managed SessionStart hook, deletes other sessions'
scratchpads idle > 1 day (keeping any with unpushed git work), npx installs
> 7 days old and the npm cache above 1 GB. verify-image.sh checks the wiring
and runs it against a clean and a dirty stale scratchpad.
…aude-disk

prune-claude-disk deleted things it should keep (probed in the published image):
- Claude Code's own temp dirs (bash-edit-diff/, the s/<hash> short scratchpad), as
  any second-level dir without a transcript counted as a stale session. Only
  <project>/<session-uuid>/ dirs with a scratchpad/ are considered now.
- scratchpads of running sessions. It now skips sessions listed in
  $CLAUDE_CONFIG_DIR/sessions/*.json and dirs with a file or transcript newer than
  SCRATCH_DAYS, so a missing transcript no longer means stale.
- detached-HEAD commits, stashes, repos nested deeper than four levels, and repos
  git can't read. The repo search has no depth limit (skipping node_modules), HEAD
  and refs/stash count as unsaved, and a git failure keeps the scratchpad.
- the package of a running npx MCP server once its _npx entry is 7 days old. An
  entry named by a running `npm exec` is skipped.

Also: the temp root honours CLAUDE_CODE_TMPDIR and TMPDIR, deleted linked worktrees
are pruned from their main repo, the npm cache path comes from `npm config get
cache`, and git runs with --no-optional-locks.

CLAUDE_CODE_SHELL moves from managed env to an image ENV, because a managed env
value overrides the user's settings and containerEnv. The async hook's timeout is
dropped (Claude Code doesn't enforce it on async hooks), and it runs only on
startup and resume. The README says Claude, not the user, gets the systemMessage.

verify-image.sh runs the hook in a throwaway HOME and temp root, and adds a
detached-HEAD case and a recent-session case. The old script fails it.
…claude-disk

rm -rf can't empty a 0555 dir such as Go's module cache, so those scratchpads were
retried and never freed; make the tree writable first. A running `npx -p <pkg> <bin>`
shows as `npm exec <bin>`, so the running-guard also matches the entry's
node_modules/.bin names. `npm config get cache --logs-max=0` stops a debug log per run.
…d-disk-prune

# Conflicts:
#	claude-code/verify-image.sh
@gatezh
gatezh changed the base branch from master to claude-code/vendor-marketplace-autoupdate November 27, 2026 20:22
@gatezh
gatezh added this pull request to stack #321 November 27, 2026 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant