Skip to content

Storage-retention sweep re-reads every thread before each group it archives or deletes #7340

Open

Description

@k0d3r1s

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):

  1. Enable an archive or delete retention policy on a workspace with thousands of threads.
  2. The sweep runs. candidates(policy) reads all threads once.
  3. 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() calls readThreads(bb), which reads every thread.
  • service.ts:66: the first candidates(policy) call.
  • service.ts:73-74: the BATCH_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: readThreads pages 500 threads at a time with no upper bound.
  • sdk-data.ts:16: readThreadsById already 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

  1. added theissue type on Nov 10, 2026
  2. added
    plugins
    confirmed-repro
    on Nov 10, 2026
  3. bb-slop-cop commented on Nov 10, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    • Classification: Bug · Priority: Medium · Effort: Low · labels: plugins, perf, confirmed-repro.
    • Report: Reproduction and root cause.
    • Verdict: REPRODUCED · Confidence: high. Both trusted-base checkouts produced 1,616 unfiltered thread-list requests and 766,375 returned rows for 7,600 synthetic threads and two 50-group batches.
    • Root cause: Each group's revalidation repeats full catalog pagination and candidate selection instead of restricting work to the affected group.
    • Reproduction test: The report includes the complete scheduler-level regression and both clean-run results; the expected budget failure is expected 1616 to be less than or equal to 16. All 88 existing plugin tests pass on restored production code.
    • No PR: The evaluated snapshot optimization missed newly attached hidden lifecycle dependents and failed the archive safety check. It was reverted rather than weakening retention safeguards.

    AGENT GENERATED

  4. added a commit that references this issue on Nov 24, 2026
    ff666fb
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-reproperfplugins

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions