Skip to content

fix(server): keep only the newest batch reports in reportDirectory - #197

Merged
sweepies merged 3 commits into
mainfrom
fix/server-report-retention
Nov 6, 2026
Merged

sweepies merged 3 commits into
mainfrom
fix/server-report-retention

Conversation

@sweepies

@sweepies sweepies commented Nov 6, 2026 •

Copy link
Copy Markdown
Owner

Problem

With reportDirectory set, every batch writes a new JSON report, and none is ever deleted. A failing repository used to rerun its batch every minute, writing a full report each time, so the directory grew without limit.

Change

  • New setting. server.reportsKept: how many reports the directory keeps. Default 200, allowed 1 to 100000, validated with the other server settings.
  • Pruning. After each report is written, the oldest batch-*.json files beyond the limit are deleted. Report names start with a fixed-width UTC timestamp, so sorting by name sorts by age.
  • Left alone:
    • temporary .batch-*.tmp files, which another worker may still be writing
    • files that aren't reports
    • a report another worker already deleted (not treated as an error)
  • Docs. The settings reference, the Pkl schema doc comment, and a paragraph in guide/server.mdx.

Tests

  • the_report_directory_keeps_only_the_newest_reports: writes six reports with a limit of three. The newest three are kept, and the .tmp and unrelated files survive.
  • reports_kept_is_read_and_bounded: checks the default, a custom value, and that 0 and 100001 are rejected.

View guided diff Turn on auto-fix

Every batch wrote a new report file and none was ever deleted, so a busy or
failing server filled its volume, one report per batch. The directory now
keeps the newest `server.reportsKept` reports (200 by default, 1 to 100000)
and deletes older ones after each new report. Temporary files of reports
still being written, and files that are not reports, are left alone.

@olivia-colomar-app olivia-colomar-app Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, ni un solo agujero serio — the diff holds up. Sombra checked every corner of this one. 🕸️

Reviewed c4e6617 — keep only the newest reportsKept batch reports in reportDirectory. (Superseded: follow-up review of d8acd8b posted below.)

Findings

P0–P2

None. The pruning logic is correct: it runs inside the same spawn_blocking after the atomic rename, so the just-written report is always in the snapshot; NotFound on delete is correctly ignored (a racing worker pruned first); and since every pruner deletes a prefix of its own sorted snapshot, concurrent workers can leave more than kept transiently but never fewer — it converges on the next write. Validation flows through Settings::load, so reportsKept bounds apply on both startup and live Reload::refresh. Docs and schema comment are accurate.

Resolved in d8acd8b

  • P3 — prune_reports counts any batch-*.json as a report (inline). ✅ Fixed: report_name now matches the writer's exact batch-YYYYMMDDTHHMMSS.NNNNNNNNNZ-<pid>-<counter>.json shape, so foreign files are neither counted nor deleted. Thread resolved.
  • P4 — read_dir / entry errors silently swallowed. ✅ Fixed: read_dir failure now logs a tracing::warn!.
  • P4 — stale .batch-*.tmp files accumulate forever. ✅ Fixed: report-named temp files are deleted once older than 24h (ABANDONED_REPORT). (Test coverage gap noted in follow-up review.)
  • P4 — 100_000 boundary untested. ✅ Fixed: boundary accept now asserted in reports_kept_is_read_and_bounded.

Standing / intentional

  • P4 — clock regressions break name-order = age-order. Left as-is per author; inherent to name-based ordering and now noted in the doc comment ("as long as the clock does not step backwards").
  • P4 — non-UTF-8 batch-*.json names are filtered out by into_string() and can never be counted or pruned. Harmless asymmetry; writer only emits ASCII.

Verification

Ran cargo test --locked -p patchula-server --lib filtered to the new/changed tests on the project boat VM at c4e6617: the_report_directory_keeps_only_the_newest_reports, reports_kept_is_read_and_bounded, report_directory_preserves_versioned_contract, discovery_skip_is_explicit_and_each_repository_report_gets_a_file — all pass. Compiles clean.

Nicely done — this one's close to mergeable. Apocopando vuvuzelas, etc. 💜

Comment thread crates/server/src/runtime.rs Resolved
…y files

- A report is a file named exactly as the writer names one, so a foreign
  batch-*.json can no longer hold a newest slot and crowd out real reports.
- Temporary files a crashed writer left behind are deleted once a day old.
- An unreadable report directory is logged instead of silently stopping
  pruning.
Repository owner deleted a comment from olivia-colomar-app Bot Nov 6, 2026

@olivia-colomar-app olivia-colomar-app Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

¡Volví! Sombra's back for round two — d8acd8b under the microscope. 🔎

Follow-up review of d8acd8b. (Superseded: follow-up review of 38e8570 posted below.)

Resolved

  • P3 — report_name predicate too broad (inline, runtime.rs). ✅ Fixed in d8acd8b: report_name now matches the writer's exact batch-YYYYMMDDTHHMMSS.NNNNNNNNNZ-<pid>-<counter>.json shape; foreign batch-*.json files can no longer occupy retention slots. Thread resolved.
  • P3 — the abandoned-tmp branch was untested. ✅ Fixed in 38e8570: the_report_directory_keeps_only_the_newest_reports now ages a validly-named .batch-…Z-1-0.json.tmp two days and asserts deletion, keeps a fresh validly-named tmp, and keeps batch-notes.json. Verified running on the boat VM.

Standing / noted

  • P4 — same-instant ordering can invert write order across counter digit boundaries. Lexicographic sort puts ...-10.json before ...-2.json; within a shared timestamp the counter (not age) decides what counts as "oldest." Impact essentially nil — same inherent name-ordering caveat as the clock note. Completeness only.

The previous commit meant to, but the edit missed: the test now ages a
validly named temporary report two days and checks it is deleted, keeps a
fresh one, and keeps a foreign batch-notes.json.
@sweepies

sweepies commented Nov 6, 2026

Copy link
Copy Markdown
Owner Author

You're right, my earlier reply was wrong: the test edit never applied (cargo fmt had reflowed the lines my edit was matching). Fixed for real in 38e8570. The pruning test now ages a validly named .batch-…Z-1-0.json.tmp by two days and asserts it's gone, keeps a fresh validly named one, and keeps batch-notes.json. I also checked the test can fail: raising the abandoned age to 30 days leaves the old file in place and the assertion fails.

@olivia-colomar-app olivia-colomar-app Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, qué limpio — the gap is closed and Sombra found nothing left to steal. 🕸️

Follow-up review of 38e8570 (live head) — the test-only commit covering the abandoned-tmp branch I flagged last round.

Verified against prune_reports

  • The abandoned branch is genuinely exercised. .batch-20261104T120000.000000000Z-1-0.json.tmp strips to a valid report_name, its mtime is aged two days via File::set_modified, and elapsed() > ABANDONED_REPORT pushes it onto the delete list. If it survived, it would land in others and fail the assertion — so the test covers the deletion direction for real, not just by accident. (runtime.rs:693-705)
  • Both directions are covered. The fresh .batch-20261106T120000.000000000Z-1-0.json.tmp has a valid inner name but a fresh mtime, so it survives — proving the age check, not just the name check, gates deletion.
  • batch-notes.json keeps the P3 fix honest — a foreign file that sorts after every real report stays put.
  • The others ordering assertion is correct — . sorts before letters, and batch-notes.json < batch-notes.txt, so the expected list matches sort().

Findings

P0–P4

None. Test-only change, correctly constructed.

Verification

Ran cargo test --locked -p patchula-server --lib report on the project's boat VM at 38e8570: all 5 report tests pass, including the_report_directory_keeps_only_the_newest_reports and only_names_the_report_writer_makes_are_reports.

Hasta la vista — this one merges clean from where I'm sitting. 💜

@sweepies
sweepies merged commit 0b0b077 into main Nov 6, 2026
11 checks passed
@sweepies
sweepies deleted the fix/server-report-retention branch November 6, 2026 18:23
@sweepies sweepies mentioned this pull request Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant