Repository navigation
Description
Summary
The hourly sweep in the storage-retention plugin pages through every thread once at the start. It then pages through every thread again before each group it archives or deletes. Each pass is about N/500 threads.list calls, and one sweep can handle up to 50 archive groups and 50 delete groups. With about 7,500 threads that is about 1,500 thread-list queries per sweep. Each group also runs selectCandidates over all N threads.
Repro
Code path (found by reading main):
- Enable an archive or delete retention policy on a workspace with thousands of threads.
- The sweep runs.
candidates(policy)reads all threads once. - For each of the first 50 groups per action, the loop calls
candidates(current)again to check the group is still eligible. Each call reads all threads again.
Cause
Line numbers are from main at 7c1353d8.
plugins/storage-retention/src/service.ts:39-42:candidates()callsreadThreads(bb), which reads every thread.service.ts:66: the firstcandidates(policy)call.service.ts:73-74: theBATCH_LIMIT(50) loop runs for each of the two actions.service.ts:84:(await candidates(current))[action].find(...)re-reads all threads for each group.plugins/storage-retention/src/sdk-data.ts:2-15:readThreadspages 500 threads at a time with no upper bound.sdk-data.ts:16:readThreadsByIdalready exists for reading specific threads.
Suggested fix
Keep one snapshot of all threads for the sweep. Before each group, re-read only that group's threads by ID with readThreadsById. Include the root's parent and source thread, because they affect eligibility. Overlay the fresh rows on the snapshot and run selectCandidates again to confirm the group is still eligible. Refresh the whole snapshot only when it is older than a limit, for example 60 s.
let snapshot = await takeSnapshot(); // one full read
// per group:
if (Date.now() - snapshot.takenAt >= SNAPSHOT_MAX_AGE_MS) snapshot = await takeSnapshot();
const requested = [...derived.memberIds, root.parentThreadId, root.sourceThreadId].filter(isString);
const rereads = await readThreadsById(bb, requested);
// overlay rereads on snapshot (missing rows = gone), then:
const fresh = selectCandidates([...overlay.values()], current, Date.now())[action]
.find((entry) => entry.rootId === candidate.rootId);Tested locally with a regression test.
Activity
bb-slop-cop commentedon Nov 10, 2026
🚨 SLOP COP 🚨 ·
new-issue-autopilotplugins,perf,confirmed-repro.expected 1616 to be less than or equal to 16. All 88 existing plugin tests pass on restored production code.Read every thread once per retention sweep and recheck each group by ID