Skip to content

Thread storage is never removed for a deleted thread whose environment row was already pruned #4160

Closed

Description

@mattwyckhouse

Environment. bb 0.43.3 desktop; threads spawned into managed git-worktree environments;
~/.bb/thread-storage/<threadId> directories on the host.

Symptom. bb thread delete --yes on 1,280 archived threads removed their rows and events
from bb.db, but 1,278 of the 1,280 thread-storage/<threadId> directories are still on disk
(directory mtimes unchanged; ≈ 3.3 MB each, ≈ 4 GB total). The server logged no
Thread storage deletion … warning. Afterwards the host holds 3,308 storage directories
(10.4 GB) for 229 non-archived threads; 1,409 directories have no thread row at all.

Mechanism (installed bundle server/dist/start-server.js).

  • DELETE /threads/:id (317933–317957) marks the thread deleted and calls
    requestThreadStorageDeletion(deps, thread, environment) for it and its lifecycle dependents.
  • requestThreadStorageDeletion (235390–235424):
    • if thread.environmentId === null → markThreadStorageDeleted and finalizeStoppedThread,
      without removing anything on disk;
    • if the environment row is missing → warn "Thread storage deletion environment is unavailable"
      and return;
    • otherwise send the host command thread.storage.delete for that environment's host, whose
      handler does rm -rf <threadStorageRoot>/<threadId> (daemon bundle). So bb does delete
      storage when the thread still points at an environment.
  • The thread row is hard-deleted only once environmentId === null or storageDeletedAt is set
    (236048–236064).
  • threads.environment_id is declared REFERENCES environments(id) ON DELETE set null, and
    foreign keys are on (foreign_keys = ON, 30055). pruneDestroyedEnvironments (39606–39660)
    deletes destroyed environment rows 7 days after destruction. Every thread of such an environment
    then has environment_id = NULL, and any later bb thread delete takes the first branch: storage
    is marked deleted and the directory is orphaned forever.

State on this host. 765 archived threads currently have environment_id IS NULL; 289 of them
still have a storage directory and will be orphaned when deleted. Zero threads have a dangling
environment reference (the FK works as declared).

Reproduction. Spawn a thread into a managed worktree, archive it, wait for the worktree to be
destroyed and for the 7-day environment prune (or lower DESTROYED_ENVIRONMENT_TTL_MS), confirm
environment_id is NULL, then bb thread delete the thread. The row disappears; the directory
under thread-storage/ remains.

Proposed fix. The storage root is per host, not per environment, so thread.storage.delete
does not need the environment: record the host id on the thread (or on the thread's storage
record) at start and send the command to that host even when the environment row is gone; when no
host session is available, keep the thread soft-deleted with storage_deleted_at NULL and retry
in the lifecycle sweep instead of marking it deleted. Also report the directory size in
bb thread show --json so operators can see what deletion will reclaim.


Filed from an audit of bb 0.43.3 by the Factory plugin (github.com/mattwyckhouse/bb-plugin-factory). Method, re-derived numbers and the adversarial review of this report: plans/076-host-sustainable-line/bb-core-issues/REVIEW.md in that repository.

Activity

  1. added theissue type on Sep 23, 2026
  2. added
    threads
    workspaces
    confirmed-repro
    on Sep 23, 2026
  3. bb-slop-cop commented on Sep 23, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    Bug · Priority: Medium · Effort: High — applied threads, workspaces, and confirmed-repro.

    REPRODUCED · high confidence. Reproduction and root-cause report.

    Environment pruning clears the thread’s environment reference. Deletion then marks storage complete and hard-deletes the thread without sending host cleanup. The route regression reproduced this in two clean checkouts of 94d77da09: the retained-environment control queued one cleanup request; the pruned case queued none and lost its tombstone. Nine adjacent tests passed.

    No PR: reliable cleanup requires a storage-ownership or retention design change outside the simple-fix limits. The report embeds the regression test; host-scale disk usage was not independently verified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-reprothreadsworkspaces

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions