Repository navigation
Bound destroyed environment event cleanup - #2914
Conversation
|
🚨 SLOP COP 🚨 · I am reviewing this pull request now. I will check security, code quality, performance, architecture, and the relevant product route. |
| const COMPLETED_EVENT_OUTPUT_TRUNCATION_CURSOR_VERSION = 1; | ||
| export const DEFAULT_CLOSED_SESSION_PRUNE_BATCH_SIZE = 1_000; | ||
| export const DEFAULT_COMPLETED_EVENT_OUTPUT_TRUNCATION_BATCH_SIZE = 250; | ||
| export const DEFAULT_DESTROYED_ENVIRONMENT_EVENT_DETACH_BATCH_SIZE = 50; |
There was a problem hiding this comment.
🚨 slopcop/review — This runtime default belongs to the server boundary.
The batch size controls the server event-loop work. The database function requires a value and does not apply this default. Please keep this value in periodic-sweeps.ts. Database tests can use explicit sizes.
There was a problem hiding this comment.
🚨 SLOP COP 🚨 · review
Plain English summary: This PR splits old environment cleanup into small database batches. This reduces long server pauses while it keeps thread history.
I found no blocking issue. The transaction keeps the final database state intact, and the deletion notice occurs after the commit.
I left one non-blocking architecture note. The new batch default belongs in the server because it controls server work.
The security review found no unsafe input, permission, credential, protocol, or file behavior. The SQL uses bound values and an existing index.
The performance review confirmed the 50-row limit and a yield after each advance. A row limit cannot guarantee a strict time limit for unusually large rows.
The focused database and server tests passed with 52 tests. The package type checks passed, and GitHub CI is green.
A browser test does not apply because this change has no user route or interface. The relevant archive route test passed.
The codebase has no duplicate cleanup path. A shared helper would add complexity without a second use.
Human comments
What was wrong
Destroyed-environment retention deleted an eligible environment directly. Because
events.environment_idusesON DELETE SET NULL, that one SQLite statement synchronously rewrote the environment’s entire historical event child set before Node could yield; the existing outer loop only yielded between environments, so one large environment could monopolize the main event loop.What changed
pruneDestroyedEnvironmentsnow treats one call as a bounded advance: it selects up to 50 event rowids in deterministic index order through the existingevents_environment_idx, sets only those references toNULL, and leaves every historical event row and payload intact.setImmediateafter every synchronous advance.EXPLAIN QUERY PLAN. The plan usesevents_environment_idxas a covering index and does not create a temporary sort.events.environment_id, its existing index, and all event rows remain in place.Files changed:
packages/db/src/data/sweeps.tspackages/db/src/data/index.tspackages/db/test/data/sweeps.test.tspackages/db/test/query-plans.test.tsapps/server/src/services/system/periodic-sweeps.tsapps/server/test/services/periodic-sweeps.test.tsapps/server/test/threads/archive-pruned-environment.test.tsHow you verified
The main-compatible regression
clears a large environment event set across event-loop turnsfailed against the pre-changeorigin/mainimplementation and passes on HEAD.pnpm exec turbo run test --filter=@bb/db --filter=@bb/server --force -- --run test/data/sweeps.test.ts test/query-plans.test.ts test/services/periodic-sweeps.test.ts test/threads/archive-pruned-environment.test.ts: 40 DB and 12 server tests passed.pnpm exec turbo run typecheck build --filter=@bb/db --filter=@bb/server --force: all 6 Turbo tasks passed (@bb/dband@bb/servertypechecks;@bb/serverbuild plus generated upstreams;@bb/dbhas no build task).@bb/dbsuite: 31 files and 446 tests passed.@bb/serversuite: 224 files / 2,141 tests passed, 1 file / 10 tests failed, and 1 file was skipped. The only failing file wastest/app/install-machine-script.test.ts, whose fake host daemons could not connect. An isolated rerun produced 10 failures / 7 passes on HEAD, and the exact isolated command on untouchedorigin/mainproduced the identical 10 failures / 7 passes, so this is a pre-existing environment failure.EXPLAIN QUERY PLANfor the detach reportsSEARCH events USING COVERING INDEX events_environment_idx (environment_id=?)with no temporary B-tree.The same benchmark harness ran against
origin/main(eeaaa3e8db7b3aeb3c4ab46873816c84cb6ea513) and HEAD (1d469a2ac96ba58a904bb101c5773cd079723bf2) on an Apple M4 Max. The fully migrated 118,054,912-byte SQLite fixture contained 45,625 production-shaped event payloads: stale destroyed environments with 44,000, 1,200, 125, and 0 references, plus fresh-destroyed and stale-destroying controls. Payload sizes were 630-byte p50, 5,453-byte p95, and 1,102,415-byte max. Each scenario used 1 warmup and 5 measured iterations; percentiles are nearest-rank. The CPU-load scenario ran 12 busy child processes while cleanup remained on the main Node event loop.Times below are milliseconds. Sync occupancy is one contiguous pruning work frame; target E2E ends when the 44,000-reference environment is deleted.
Per measured iteration,
origin/mainused 8 SQL statements, 4 progress ticks, and 4 yields; HEAD used 1,826 statements, 911 ticks, and 911 yields. Both detached 45,325 event references, deleted 4 environments, deleted 0 event rows, emitted 4 deletion notifications, preserved both controls, and produced the same deterministic final-state SHA-256 (c8e5d616505428fe72891f0aea8fbcd50a446d8beb53301d13cf48c78792d85e) in every run.Residual tradeoff: bounded frames deliberately increase total work and compute-only end-to-end time. The server caps each 10-second periodic pass at 10 advances, so a continuously present 44,000-reference backlog can take about 88 passes (roughly 14 minutes 40 seconds) to reach deletion, while remaining responsive and restart-safe throughout.