Claude Code: Memory-Safe Long Sessions & Worktree Hook Events

Claude Code

Claude Code 2.1.50 delivers a sweeping stability improvement for long-running agent sessions, fixing six separate memory leaks that caused unbounded memory growth over time. Anthropic also expanded the worktree isolation system with new WorktreeCreate and WorktreeRemove hook events, enabling teams to integrate custom VCS workflows beyond git. A new claude agents CLI command simplifies agent configuration inspection, and the CLAUDE_CODE_SIMPLE mode now provides a far more complete minimal-footprint configuration for embedded deployments.


Memory Stability for Long Agent Sessions

Claude Code 2.1.50 represents a significant investment in long-session reliability, addressing six distinct memory leaks that collectively caused unbounded memory growth in extended agentic workflows. For teams running Claude Code autonomously over hours or days, these fixes translate directly into more stable, lower-footprint deployments.

What Was Fixed

Anthropic resolved a series of memory management issues across the session lifecycle:

  • LSP diagnostic data was never cleaned up after delivery, causing memory to grow linearly with session length — this is now properly garbage-collected.
  • Completed agent teammate tasks were never removed from session state in agent team workflows, accumulating indefinitely.
  • Task output objects and TaskOutput recent-lines buffers were retained in memory after tasks completed.
  • CircularBuffer items that were cleared still held references in the backing array.
  • Shell command child processes and AbortController references were retained after command execution finished.
  • File history snapshots grew without bound — they are now capped to prevent unbounded accumulation.

Beyond these targeted fixes, Claude Code 2.1.50 also improves memory usage proactively: internal caches are cleared after context compaction, and large tool results are released from memory once they have been processed.

WorktreeCreate and WorktreeRemove Hook Events

Building on the git worktree isolation introduced in recent releases, Claude Code now fires two new lifecycle hook events: WorktreeCreate and WorktreeRemove. These hooks fire when the agent isolation system creates or destroys a worktree, giving teams a programmatic entry point to run custom VCS setup and teardown logic.

This is particularly valuable for organizations using version control systems other than git — such as SVN, Perforce, or Mercurial — or teams that need to perform additional environment setup (mounting network filesystems, configuring credentials, notifying CI systems) whenever a new isolated workspace is created. Agent definitions can also now declaratively request worktree isolation by specifying isolation: worktree in their definition file, removing the need to pass --worktree flags manually at invocation time.

claude agents CLI Command

A new claude agents command lists all configured agents across built-in, project-level, and plugin-provided sources. This makes it straightforward to audit which agent definitions are active in a given context without manually inspecting the file system or knowing where each agent type is stored.

CLAUDE_CODE_SIMPLE Overhaul

The CLAUDE_CODE_SIMPLE environment variable, which enables a stripped-down mode for embedded or constrained deployments, has been significantly expanded in scope. In 2.1.50, simple mode now disables MCP tools, file attachments, all hook types, CLAUDE.md file loading, skills, session memory, and custom agents. Previously, simple mode only disabled a subset of these. The change makes CLAUDE_CODE_SIMPLE a reliable single-flag "minimal footprint" configuration for SDK integrations that do not need the full Claude Code feature surface.

CLAUDE_CODE_DISABLE_1M_CONTEXT

A new CLAUDE_CODE_DISABLE_1M_CONTEXT environment variable has been added for deployments where the 1M context window should not be activated. This gives operators explicit control over context window size independently of the model selection.

Other Improvements

  • Added startupTimeout configuration support for LSP server connections.
  • Fixed a bug where resumed sessions could be invisible when the working directory involved symlinks.
  • Fixed session data loss on SSH disconnect — session data is now flushed before hooks and analytics run during graceful shutdown.
  • Fixed native modules failing to load on Linux systems with glibc older than 2.30 (such as RHEL 8).
  • Fixed /mcp reconnect freezing the CLI when given a non-existent server name.
  • Improved startup performance in headless mode (-p flag) by deferring Yoga WASM and UI component imports.
  • Fixed a prompt suggestion cache regression that had reduced cache hit rates since a prior release.