Skip to content

fix: per-process scope for Claude Code sub-agent cache isolation (#1292) - #1293

Closed
yvgude wants to merge 0 commit into
mainfrom
fix/1292-claude-code-subagent-scope
Closed

fix: per-process scope for Claude Code sub-agent cache isolation (#1292)#1293
yvgude wants to merge 0 commit into
mainfrom
fix/1292-claude-code-subagent-scope

Conversation

@yvgude

@yvgude yvgude commented Jul 28, 2026

Copy link
Copy Markdown
Owner

Summary

Closes #1292.

Claude Code sub-agents (spawned via the Agent tool) share the parent's session_id — Claude Code does not set CURSOR_TASK_ID or any equivalent per-subagent identifier. Each sub-agent gets its own lean-ctx MCP process (stdio mode), but they all share the on-disk read_stub_index. The cold stub gate passed because both parent and sub-agent resolved to the same session_id, serving [unchanged] stubs for files the sub-agent never received.

Fix

Extend subagent_scope() with a multi-host scope resolution chain:

  1. LEAN_CTX_SCOPE — explicit operator/integration override (new)
  2. CURSOR_TASK_ID — Cursor subagents (existing)
  3. CLAUDECODE=1 — per-process proc:{pid-ts} scope (new, ctx_read cache returns stub to sub-agents that never received file contents #1292)

When CLAUDECODE=1 is detected, each lean-ctx process generates a unique, lifetime-stable scope ID. Since each MCP connection is a separate stdio process, parent and sub-agent caches are fully isolated — warm stubs stay per-process, and cold stubs from the persisted index never match across processes.

Also extends is_subagent_context() to detect CLAUDE_CODE_ENTRYPOINT=local-agent (future Claude Code sub-agent marker) as a defense-in-depth fallback for when scoping is disabled.

Tradeoff

In Claude Code, cold stubs (persisted index) no longer survive process restarts — each new lean-ctx process gets a fresh scope. This is acceptable: cold stubs are a minor optimization, and the warm cache handles 95%+ of re-reads within a session.

Changes

  • rust/src/core/conversation.rs: New resolve_scope() pure-core resolver, process_unique_id() generator, updated subagent_scope(), 8 new tests
  • rust/src/tools/ctx_read/dispatch.rs: Extended is_subagent_context() for CLAUDE_CODE_ENTRYPOINT=local-agent

Test plan

  • 21 conversation tests pass (13 existing + 8 new)
  • All delta/dispatch/stub_index tests pass
  • Zero clippy warnings (--lib)
  • cargo fmt --check clean
  • CI green

Made with Cursor

@yvgude yvgude added this to the v3.9.11 milestone Jul 28, 2026
@yvgude yvgude closed this Jul 28, 2026
@yvgude
yvgude force-pushed the fix/1292-claude-code-subagent-scope branch from ccd0a98 to 1587891 Compare July 28, 2026 16:12
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 28, 2026
@yvgude
yvgude deleted the fix/1292-claude-code-subagent-scope branch July 28, 2026 16:13
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ctx_read cache returns stub to sub-agents that never received file contents

1 participant