Repository navigation
Description
Symptom
POST /api/v1/threads/:id/archive returns 409 thread_environment_unavailable for any thread whose environment_id is NULL. The thread can never be archived and stays permanently visible in the sidebar. Both /archive and /archive-all fail, so there is no UI or CLI workaround — the only way to get rid of the thread is delete, which destroys its transcript.
$ bb thread archive thr_xxx
Error: Failed to archive thread thr_xxx: HTTP 409: Thread environment is unavailable
$ curl -XPOST .../api/v1/threads/thr_xxx/archive
{"code":"thread_environment_unavailable","message":"Thread environment is unavailable",
"details":{"reason":"never_attached","environmentStatus":null}}
The reported reason never_attached is itself misleading: these threads did run in an environment, they just outlived its row.
How a thread ends up with a NULL environment
- The thread runs in a managed environment. The environment is later destroyed.
pruneDestroyedEnvironmentshard-deletesdestroyedenvironments older thanDESTROYED_ENVIRONMENT_TTL_MS(7 days) —packages/db/src/data/sweeps.ts.threads.environment_idis declaredON DELETE SET NULL, so that hard delete silently strips the pointer off threads that are still unarchived.- Archive then reads NULL as "never attached" and rejects.
The sweep filters only on environment status and age. Its sibling sweepManagedEnvironments does guard on live threads (threads.archived_at IS NULL AND threads.deleted_at IS NULL); pruneDestroyedEnvironments has no equivalent guard, so it will keep re-manufacturing this state.
Repro
- Create a thread in a managed environment and let it go idle.
- Destroy the environment; leave the thread unarchived.
- Wait out the 7-day TTL (or set
environments.updated_atback 8 days and let the sweep run). - Try to archive the thread — 409.
Why archive diverges from delete
The delete route already handles exactly this state, skipping the environment-scoped work:
// apps/server/src/routes/threads/base.ts
if (thread.environmentId === null) {
finalizeStoppedThread(deps, { threadId: thread.id });
return context.json({ ok: true });
}Archive has no equivalent branch. It calls requireThreadHostCommandEnvironment, which throws for a NULL environment (apps/server/src/services/threads/thread-command-environment.ts). The environment is only needed to stop active runtime work, and a thread with no environment has no runtime left to stop — so the requirement is not load-bearing. This reads as an oversight in archive rather than an intentional difference.
Affected call sites:
apps/server/src/routes/threads/actions.ts— theroutes.archivehandlerapps/server/src/services/threads/thread-archive.ts—archiveThreadAndHiddenSourceForksandarchiveThreadAndChildren
Proposed fix
- Mirror delete's NULL-environment handling in archive: resolve the environment as nullable and skip
requestActiveRuntimeThreadStopIfNeededwhen it is absent. Every other archive effect (terminal close, event pruning, plugin event, child release) still applies. - Add the live-thread guard to
pruneDestroyedEnvironments, mirroringsweepManagedEnvironments, so the sweep stops creating the state in the first place.
Tradeoff
This needs a PR and a new build before archiving works for affected threads. There is no runtime workaround in the meantime.
Impact
Affected threads archive normally and stay recoverable through unarchive, instead of forcing a permanent delete that destroys the transcript. And nobody hits this again.
Affected threads observed
Two threads on one install hit this — both idle, both codex, both with environments pruned after the TTL:
thr_vyaz3wdwjv("Match Sidebar Workflow Icon", created 2026-06-25)thr_ijwvxv9hy6("Disk And CPU Cleanup", created 2026-07-18)
Both had to be deleted rather than archived, permanently destroying their transcripts. Any thread will hit this roughly 7 days after its environment is destroyed while it remains unarchived.
Activity
SawyerHood commentedon Aug 20, 2026
Reproduction & root-cause report: https://get-bb.github.io/reports/issues/1924.html
The report has copy-paste repro steps, screenshots where the bug is visual, code permalinks at the base commit, and repro artifacts. It was independently re-run by a second agent (see its Verification section).
Allow archiving a thread after its environment row is pruned (get-bb#…