Repository navigation
Finish deleting threads on removed machines and clean up orphaned thread storage - #4328
Merged
Merged
Conversation
Deleting a thread on a removed machine sent thread.storage.delete to a host that no longer exists. The command never succeeded, so the thread stayed a soft-deleted tombstone and the lifecycle sweep retried it forever. Before #4259 the destroyed-environment prune cleared these by accident. A removed machine's storage is already gone or unreachable, so storage deletion now settles immediately when the environment's host is destroyed. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Threads deleted after their environment row was pruned (before #4259) were hard-deleted without asking the host to remove thread-storage/<threadId>, leaving folders on disk that no server path would ever clean up. When a machine connects, the server now lists the top level of its thread-storage directory with host.browse_directory and removes each thread-id-shaped folder with no thread row (live, archived, or deleted) using host.remove_path scoped to the storage root. Folders are listed before the database is read, so a thread created mid-run is never treated as an orphan. Thread ids are looked up in batches of 1,000 with a yield between batches, so a large listing doesn't stall the server. The cleanup stops if the server move freezes, the server shuts down, or the machine's session changes. It reuses existing daemon commands, so the protocol version is unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Creating a host row also emits host-connected, before the daemon session exists. On a machine's first enrollment the orphan cleanup ran at that moment and logged "Thread storage orphan cleanup failed: Host is not connected". It now returns quietly until a daemon is registered; the registration's own host-connected event runs the cleanup. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A host.remove_path that exceeds the 30s RPC timeout keeps running on the daemon; the server only stops waiting. Catching that error and moving on started the next recursive delete while the previous one was still in progress, so a slow backlog could pile up concurrent deletions. The pass now stops on a timeout, an unavailable host, or an unexpected error, and continues only past failures the daemon reported for a single entry, such as a rejected symlink. The next connect retries the rest. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Move the cleanup off the host-connected listener and onto the periodic sweep framework the database sweeps use. The thread-storage-orphan-cleanup job runs every 60s and skips unless isDatabaseMaintenanceIdle reports no active threads, provisioning, pending interactions, or project deletion. Idle is rechecked before every removal. Each connected host's thread-storage is listed once per daemon session into a queue of orphaned ids. Passes consume the queue in the background with at most 100 removals and a 10s elapsed budget, so the sweep tick never waits on daemon RPCs. An entry the daemon rejects leaves the queue, so it cannot starve later orphans, and an empty queue marks the session clean so it is not listed again until the daemon reconnects. A timeout, unavailable host, or unexpected error parks the session's queue, so later passes cannot stack deletions behind one that may still be running on the daemon. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
ymichael
deleted the
bb/investigate-and-fix-issue-4160-thr_ehpewydf7k
branch
September 26, 2026 04:25
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Human comments
What was wrong
#4259 stopped the destroyed-environment prune, so newly deleted threads now keep their environment and reach the host's
thread.storage.delete. Two gaps remained around #4160:destroyedAtset since Allow machine removal while preserving read-only thread history #4090) sentthread.storage.deleteto a host that no longer exists. The command never succeeded, so the thread stayed a soft-deleted tombstone and the lifecycle sweep retried it forever. Before Keep destroyed environment rows instead of pruning them #4259 the 7-day prune cleared these by accident.thread-storage/<threadId>folders stayed on disk. No thread row is left to recover the owning host, and no code path would ever remove them. The reporter had about 1,400 such folders.What changed
requestThreadStorageDeletion(apps/server/src/services/threads/thread-lifecycle.ts): when the thread's environment is on a destroyed host, storage is marked deleted and the thread finalized straight away. A removed machine's storage is gone or unreachable.apps/server/src/services/threads/thread-storage-orphans.ts, registered inPERIODIC_SWEEP_JOBSasthread-storage-orphan-cleanup, categoryorphan-cleanup, cadence 60s):isDatabaseMaintenanceIdle(getDatabaseMaintenanceActivity(db))reports no active or starting threads, environment provisioning, pending interactions, or project deletions. This is the same gate the thread pruning and database maintenance sweeps use. Idle is checked again before every removal.<dataDir>/thread-storagewithhost.browse_directoryand keeps only folder names that passisRawThreadId. It looks them up inthreads1,000 at a time, yielding between batches, via the newlistExistingThreadIds. The folders with no thread row become that session's queue.host.remove_pathand the storage root asrootPath.invalid_pathfor a symlink) is logged and leaves the queue, so it cannot starve later orphans.command_timeout), an unavailable host (host_unavailable), or an unexpected error parks the session's queue until a new daemon session. A timed-outhost.remove_pathkeeps running on the daemon, so starting more deletions would pile them up.callHostOnlineRpcrequires an active, connected machine and never wakes a suspended onehost.browse_directoryandhost.remove_pathdaemon commands, soHOST_DAEMON_PROTOCOL_VERSIONis unchanged and already-installed machines work.bb server unlockafter a move,bb server importinto an existing install, restoring abb.dbbackup), folders of threads created after that snapshot are deleted once the app is idle.bb thread show --json. Keep destroyed environment rows instead of pruning them #4259 made the first unnecessary. The second is not included.How you verified
apps/server/test/threads/delete-on-removed-machine.test.ts(new): removes a machine throughDELETE /hosts/:id, deletes its thread, runs the lifecycle sweep, and expects the row gone with nothread.storage.deletequeued. Failed on main (the row remained); passes with the change.apps/server/test/threads/thread-storage-orphans.test.ts(new, 7 tests):invalid_pathis followed by the next orphan on the next pass without re-listingworkflowsfolder and a thread-named file, only the orphans are removed, and the live thread sits after the first 1,000-ID batchpnpm start:worktree, 3,000 seeded threads, 300 orphan folders): the idle sweep removed the orphans in 100-attempt passes about 60s apart (99, 100, 100 and 1 removed; the first pass's 100th attempt was the refused symlink). It kept all live folders,workflows, the thread-named file, and the symlink along with its outside target. A base server atdcc58375ccwith the same fixture removed nothing. The removed-machine path was also checked on both through the API andbb:dev thread delete.browseHostDirectoryandremoveHostPathagainst a temporarythread-storagefolder. They listed both thread folders and removed only the targeted one.e0426507c0:pnpm exec turbo run typecheck lint --filter='...[dcc58375cc]': 10/10pnpm exec turbo run test --filter=@bb/server: 3218 passed, 1 skipped, 1 failed. The failure was a 5s timeout ininstall-machine-script.test.ts, the shell-script flake; that file passes alone (47/47).Fixes #4160
🤖 Generated with Claude Code