Repository navigation
fix(threads): clean persisted records safely on thread deletion - #5535
Conversation
willem-bd
left a comment
There was a problem hiding this comment.
Reviewed at head 56d4ee9. I ran the six changed test files in a worktree at the exact head SHA with import paths pinned to the PR sources: all 344 pass, and the new fence tests in test_db_event_store_lock_lifecycle.py fail against the merge-base (4889f61) sources, confirming the RED→GREEN claims. The deletion critical section (per-thread asyncio lock → session → advisory fence) now matches the writer path exactly, owner scoping is applied consistently across runs/events/feedback/meta from a single resolved identity, and the operation-kind reservation-preservation semantics in the bulk run delete are the right call and are pinned by tests. One consistency gap worth addressing (inline): the event-store cleanup call is not capability-gated the way the run-store call is, so a store implementing the old delete signature would skip event deletion silently at debug level. Non-blocking.
…dance#5535) * fix(events): serialize DB deletion with thread writers * fix(runs): delete thread history without dropping reservations * fix(feedback): support owner-scoped thread cleanup * fix(threads): clean persisted records on deletion * fix(threads): correct the feedback cleanup rationale * test(runs): drop the wall-clock probe from the in-flight delete test * docs: record the thread-delete and event-store fence contracts * fix(threads): preserve legacy event-store delete compatibility
锚点 bytedance#6 (v2.1.0-rc0, 769589e) → bytedance#7 (v2.1.0, 345f08b),10 commits / 90 files。 注意 v2.1.0 tag 打在 2.1.x-dev 上,与 upstream/main 已在 rc0 处分叉(main 另有 189 commits 待 sync bytedance#8)。 源码侧实质变更 → digest 校正: - 0025_repair_run_change_seq(bytedance#5517/bytedance#5516):修复被"插队"的 0023_run_change_seq 留下的 schema 空洞;补注册 RunChangeClockRow / UserPreferenceRow - 线程删除全量清理(bytedance#5535):DELETE /api/threads/{id} 持 durable delete reservation, 依次清理文件系统/checkpoints/历史 run(只删 operation_kind="run")/events/feedback/thread_meta - 事件存储变更串行域 + owner-scoped 删除签名(bytedance#5535) - CI:6 个 workflow push 触发改 *-dev 通配,skill-review-ci 显式钉 2.1.x-dev(bytedance#5765) - frontend:Projects 侧栏嵌套 SidebarMenu 加 w-auto(bytedance#5681/bytedance#5682) - 文档:subagents 11 页手册 / extensions 10 页手册,root AGENTS.md 精简 同时校正文档侧漂移:AGENTS.md 行号引用与计数、仓库行数/方法数、 _faq_on_digested 14 处基线标注 rc0→v2.1.0。 SYNC.md / SYNC_LOG.md bytedance#7 / UPDATE_PLAN_7.md 已同步记录。
Fixes #5530
Supersedes #2803
Why
DELETE /api/threads/{id}removed the thread directory, the checkpoints, thebrowser session and the
threads_metarow, but left the thread's persistedrecords behind:
run_events(the user-visible conversation feed), historicalrunsrows andfeedbackall survived. Two consequences, both reachable oncurrent
main:GET /api/threads/{id}/messagesstill answers after the delete. The route isowner-checked but read-style (
require_existing=False), andThreadMetaRepository.check_accessreturnsTruefor a missing meta row, soa deleted thread's feed stays readable.
POST /api/threadsaccepts a client-chosenthread_id. Re-creating the idimmediately inherits the deleted thread's messages.
The persistence half of this was identified in #2803 (still open, last touched
2026-06-11). This PR rebases that scope onto the current durable thread-deletion
lifecycle and closes the event-store contract gap tracked in #5530.
What changed
Event store (#5530).
DbRunEventStorewriters already serialize per thread:the in-process
_get_write_lock()plus, on PostgreSQL,pg_advisory_xact_lock(hashtext(thread_id))taken inside_max_seq_for_thread().Deletion bypassed both, so a deletion could return while a concurrently admitted
writer still committed a row for the deleted thread — resurrection after delete.
The advisory lock is now an explicit primitive
(
_acquire_thread_mutation_fence), anddelete_by_thread/delete_by_runtakeboth fences.
JsonlRunEventStorealready serialized deletion through_run_mutation(); the two durable stores now expose the same mutation contract.Thread deletion cleanup (#2803 scope).
_delete_thread_data_with_reservationnow also deletes historical runs, run events and feedback, all inside the
existing
RunManager.reserve_thread_operation(kind=delete)reservation, allowner-scoped with one
get_effective_user_id()value, and all best-effort so theHTTP success contract is unchanged.
Reservations stay reserved.
RunRepository.delete_by_thread/MemoryRunStore.delete_by_threaddelete onlyoperation_kind == "run"rows.The
runstable also holds durable thread-operation reservations(
checkpoint_write,artifact_write,branch,delete); a blanketDELETE ... WHERE thread_id = ?would have removed the very reservationprotecting the in-flight delete and dropped the cross-worker exclusion. This is
the one place this PR deliberately diverges from #2803's implementation, and it
has its own regression test.
Deliberate non-goals. Bulk run cleanup does not bump the run-change clock
(matching the existing single-run
RunRepository.delete(), and keeping threadcleanup away from
run_change_clock— the table #5516/#5517 were about). Nothread-incarnation/generation fencing, no sandbox or MCP teardown, no JSONL
queued-mutation change, no migration.
Verification
RED → GREEN per behaviour change:
the thread lock; deletion's first statement is the
DELETE, not the advisorylock). GREEN:
tests/test_db_event_store_lock_lifecycle.py+tests/test_run_event_store.py→ 95 passed, plus 50 passed across the adjacentevent-store suites.
AttributeError). GREEN:tests/test_run_repository.py→ 70 passed, including the mandatoryreservation-preservation case.
tests/test_feedback.py→ 30 passed.344 passed across
test_threads_router.py,test_run_repository.py,test_feedback.py,test_run_event_store.py,test_db_event_store_lock_lifecycle.py,test_jsonl_event_store_async_io.py.ruff check+ruff format --checkclean;git diff --checkclean.Notes for reviewers
delete_by_thread/delete_by_runnow take an optional owner scope on everyevent-store backend. The memory and JSONL stores are not user-scoped, so they
accept the parameter for interface parity and ignore it — the same convention
their read methods already use — which lets the Gateway call deletion
uniformly. Third-party
RunStoreimplementations without bulk cleanup areskipped through a capability check.
tests/test_browser_automation.pyand
tests/test_browserless_client.pyrequire Playwright and reachablebrowserless/example.comendpoints; they fail or hang in a sandbox withoutthose, independently of this change.