LSP on git worktree
What
We all understand the idea behind LSP to be eager, with less token consumption and faster search on the agentic side. However, when Git worktree is involved and you have a swarm of agents making multiple changes, it would be beneficial to have an LSP per worktree integration.
Why
It would solve the main problem where any other spawned sub-agents and then subsequently agents would read from the correct worktree folder instead of the one main folder.
Thanks for raising this @sbogza — happy to dig in. Short version: LSP-per-worktree already works today, and the friction you described is mostly a configuration/discoverability issue plus a soft pool-cap. Details below, plus a small change I just landed for the swarm case.
How it works today
The LSP pool is keyed by (language, project_root), not just by language:
https://github.com/mareurs/codescout/blob/master/src/lsp/manager.rs#L32-L35
pub struct LspKey {
pub language: String,
pub project_root: PathBuf,
}
That means every distinct project root — including a git worktree path — gets its own pooled LspClient (its own child rust-analyzer / kotlin-language-server / etc.). Two worktrees of the same repo end up as two independent LSP processes side-by-side, no cross-contamination.
The trigger is an explicit workspace(action="activate", path=...) call with the worktree's absolute path:
workspace(action: activate, path: "/abs/path/to/.worktrees/feat-a")
After that, all reads / writes / symbol navigation / shell commands target that worktree, and the LSP client backing it is pinned to that root. Switch back to the main repo with another workspace(action: activate) when done.
To prevent silent "wrote to wrong tree" bugs, there's also a hard guard: if worktrees exist and the agent hasn't explicitly activated, any write tool (edit_file, create_file, edit_code) returns a RecoverableError with the exact workspace(action: activate) call to unblock — implemented in guard_worktree_write.
Full writeup: docs/manual/src/concepts/worktrees.md.
Recommended setup for a "swarm"
For genuinely parallel agents, the cleanest setup is one Claude (and thus one MCP server) per worktree, launched inside the worktree:
git worktree add .worktrees/feat-a feat-a
cd .worktrees/feat-a && claude # session rooted in the worktree from message #1
git worktree add .worktrees/feat-b feat-b
cd .worktrees/feat-b && claude # independent session, independent MCP server, independent LSPs
Each session has its own codescout server process, its own project_root, its own LSP pool. No activate dance needed; no risk of subagents inheriting the wrong root.
If you do want a single parent agent driving multiple worktrees (subagent fan-out from one MCP server), it still works — each subagent just needs to call workspace(action: activate) on its target worktree before touching anything, and (per Iron Law #4) restore the parent's project before returning.
What I just changed
There was one real ceiling in the swarm case: the pool's max_clients was hardcoded at 5, so a swarm hitting 4 worktrees × 2 languages = 8 LSPs would trigger LRU eviction and pay cold-start cost on every switch. Just bumped it to 10 on experiments (will ship in the next release): 06b07375.
That should cover realistic parallel-agent setups. If 10 still isn't enough for your workflow, a follow-up to expose it as a CODESCOUT_MAX_LSP_CLIENTS env var or per-project config field would be straightforward — happy to add it if you (or anyone reading this) actually hit the ceiling. Let me know.
Closing this once the next release goes out unless you spot a gap I missed.