mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-27 11:02:33 +01:00
07278eddc4
## Summary Registry agents installed from an archive live in one directory per version, and every launch removed all older version directories. An agent still running from one of them then broke, because adapters resolve files next to their own binary on demand. After another session installed a newer Claude Agent, the older session failed with "The Claude CLI is missing" (agentclientprotocol/claude-agent-acp#1596). A running agent now holds a lease on the version it was launched from, and cleanup only removes versions that nobody holds a lease on. - **The lease** is a shared file lock on `<version dir>.lease`. Every Zed process that shares the data directory (stable, preview, nightly) honors it, and the operating system releases it if Zed crashes. - **Taking it** happens before checking whether the version is installed, so a cleanup in another process can't remove the version between that check and the agent starting. - **Releasing it** happens when the agent process exits, not when the connection is dropped. Threads keep a connection alive long after its agent has exited, and that should not pin the version. - **Cleanup** retires a version only if it can lock that version's lease exclusively. A per-agent `leases.lock` serializes taking and removing leases, so a lease file is never deleted between another process opening and locking it. - **Retiring** renames the version to `retired_<name>` while the leases are locked, and deletes it after the lock is released. A version is therefore never left half-deleted under a name that still looks installed, and launching an agent doesn't wait for a deletion. A later cleanup deletes any `retired_` directory that an interrupted one left behind. - **Installing** takes an exclusive per-agent `install.lock` and re-checks for the version afterwards. Installing replaces the version directory, so two windows installing the same version at once could previously remove the directory the first agent was already running from. - **Remote projects**: the client launches the agent, so the remote server never sees it exit. The server keeps the lease of every version it resolved until the server itself exits. A failure on one stale version is logged and the remaining ones are still cleaned up. ### File locks in `Fs` Locking goes through the `Fs` trait, like the rest of the store's file access. This adds `Fs::lock_file` (waits for conflicting locks) and `Fs::try_lock_file` (returns `None` instead of waiting). Both take a `FileLockKind` (`Shared` or `Exclusive`), create the file if needed, and return a `FileLock` that is released on drop. - `RealFs` takes OS file locks through std. To wait, it opens the file once and retries a non-blocking lock with a backoff (5 ms up to 250 ms). A wait uses no thread and stops when its future is dropped. Waiting inside the operating system instead would hold a blocking-pool thread per waiter, even after cancellation, and enough waiters would starve the lock holder's own file operations. - `FakeFs` keeps an in-memory lock table keyed by inode and resolves the file once before waiting. A lock is therefore on the file that was at the path when it was requested, even if that file is removed or replaced, as on a real file system. To keep that true, `FakeFs::create_file` with `overwrite: true` now keeps an existing file's inode instead of assigning a new one, which is how `FakeFs` already treats writes to an existing file. ### Other API changes `get_command` now returns a `ResolvedAgentServerCommand`, which bundles the command with its lease, and the connect functions take that type. Callers without an installation to protect build one with `ResolvedAgentServerCommand::without_installation_lease`. ### Known limits - A Zed build without this change takes no lease and still removes old versions unconditionally. Two channels running side by side stay exposed until both have the fix. - The terminal login command is not leased, because the terminal outlives the task that resolves the command. - On a file system without lock support, the agent launches without a lease and cleanup removes nothing. - A Zed process that hangs while installing an agent keeps other processes from installing that agent until it exits. - Lock waiters poll, so one can notice a released lock up to 250 ms late, and waiters are not served in order. - Agents distributed through npx install in place and are not covered by this change. ## Testing Ran locally on macOS; CI covers Linux and Windows. ```sh cargo test -p fs cargo test -p project --lib -- agent_server_store cargo test -p agent_servers --lib cargo test -p remote_server -- agent ./script/clippy ``` New or changed tests: - `test_realfs_file_locks` and `test_fake_fs_file_locks` run the same assertions against both implementations: shared locks coexist, shared and exclusive locks conflict, a waiting lock is granted once the conflicting ones are released, a cancelled wait doesn't take the lock, a waiter is granted the lock on the file it started waiting on even if that file is removed or replaced, locking a dangling symlink creates its target (resolving `..` through symlinked directories), and truncating a file keeps the locks on it. On Windows, `RealFs` skips the removed-file, truncation and symlink cases. - `test_realfs_lock_waiters_leave_threads_for_file_operations`: with 512 waiters queued on a held lock, the holder can still use the file system. It times out if waiting blocks a pool thread. - `test_remove_stale_versioned_archive_cache_dirs` (on `FakeFs`): stale versions, leftover lease files and leftover `retired_` directories are removed; an older version with a held lease survives until the lease is released; a version that can't be deleted doesn't keep the others on disk; and a lease file is kept if its version exists. - `registry_archive_version_is_installed_once_by_concurrent_launches` (on `RealFs`): two launches of the same uninstalled version download it once. It fails without the install lock. - `registry_archive_version_is_kept_while_its_lease_is_held` (on `RealFs`): installs through `get_command`, checks the version survives cleanup while the lease or a clone of it is held, is removed once released, and reinstalls afterwards. - `installation_lease_is_released_when_the_agent_exits`: the lease is released when the agent process exits, while its owner is still alive. Checked by hand, not in the repo: - With a throwaway program: another process sees a lock as held, it is released when the holder is killed with SIGKILL, and a child spawned by the holder does not inherit it. Not tried against a real adapter update in a running Zed. ## Self-review - [x] I've reviewed my diff for quality, security, reliability, and performance. - [ ] UI changes follow the [checklist](https://zed.dev/docs/development/ui-checklist). (No UI changes.) - [x] Tests cover the new or changed behavior. Release Notes: - Fixed external agents installed from the ACP registry breaking (for example Claude Agent reporting "The Claude CLI is missing") when a newer version was installed while an older one was still running.