Repository navigation
feat(studio-server): cap total project history and drop gone histories under low disk - #9229
Conversation
jrusso1020
left a comment
There was a problem hiding this comment.
Approve at f43f62e. I found no blocker in the code. The tests leave several of its safety guards unchecked, though, and the PR body claims some of them are covered. Please add the should-fix tests below in a follow-up.
What I checked by reading
- Busy histories are left alone.
takeHistoryOwnership(home, 0)does not re-enter within one process:claim()gets EEXIST, the holder is a live pid, and the zero wait throwsHistoryBusyError. A project this same studio server has open is skipped, not trimmed. - It keeps what an opened project keeps. It keeps
referencedHashes(log, manifestAt(last entry)), which is exactly whatEngineseedstrackedfrom on open (projectHistory.ts:764-767). The other state files (went-ahead.json,adopting.json) name paths, not hashes, so pruning doesn't strand a blob those files still need. That matches the "Pruning keeps only what this log names" rule intakeLog. - Order of writes. The folded log is written atomically (
replaceFileAtomically) before any blob is removed. - Low disk. It only skips the 14-day grace period.
projectGone, including the check for an unmounted drive, still decides.
Tests: 428 of 428 pass in src/history (3 skipped). Mutations:
| Change | Result |
|---|---|
remove the entries.length > 1 newest-entry stop |
a test fails ✅ |
| remove hard-link dedupe | a test fails ✅ |
ignore lowDisk |
2 tests fail ✅ |
| remove the 24h "opened today" filter | all pass |
skip the lock (takeLockOrNull returns a no-op) |
all pass |
skip writeLog but still remove blobs |
all pass |
drop the pruneHistoryToGlobalCap call from the daily pass |
all pass |
Should-fix (tests). The body says the tests cover "24h protection". In the fixtures the cap is met before the loop ever reaches today's project, so the filter is never what saves it.
- Add a fixture where the cap can only be met by trimming today's project, and assert that it is left alone.
- Run a prune while a history's
owner.pidis held, and assert that it is skipped. - After a trim, reopen the history from disk and check that every hash the log names still exists. That catches a log/blob mismatch, which the current check (Undo on the newest step) can't see.
- Add a daily-pass test that asserts the global cap runs and that
.last-pruneis stamped only after it finishes.
Simpler (nit): the stale-first bucket in rankedCandidates changes nothing. A project unused for 30+ days always has an older lastUsed than one used within 30 days, so sorting oldest-first alone gives the same order, which is why that mutant survives. Either drop the bucket or say why it's there. Also, USE_MARKS is now defined in both pruneHistories.ts and globalHistoryCap.ts.
Need ✅ · Size foldOldest, referencedHashes, the owner lock) · Simpler
Risk: medium. This deletes users' undo history from projects that aren't open. The code guards are right as written, but four of them have no test, so a later change could break one without any test failing.
— Rames
|
Added all four should-fix mutation tests and both nits in 56fa37a:
|
Edit accuracy: accurate 275 (base branch 275), smooth 59 of thoseThe gate passes. Quarantined, measured but not gated (0) |
|
Raised @hyperframes/studio-server packed size budget in scripts/package-size-budgets.json from 203563 to 207000 bytes in b4c24fc to cover the new globalHistoryCap module (~2.9 KB packed, actual 206471 bytes). |
jrusso1020
left a comment
There was a problem hiding this comment.
Re-review at b4c24fc7 (diff from my approval at f43f62ec).
- The stale-first sort removal is equivalent. Every project 30+ days unused also has an older
lastUsedthan any newer one, solastUsedascending gives the same order. No behaviour change, as claimed. MovingUSE_MARKSinto one shared export is fine. - Test run: studio-server
src/history: 26 files, 431 passed, 3 skipped. - Mutations:
- drop the opened-today filter → 1 red ✅
- drop the log rewrite after folding → 2 red ✅
- busy lock throws instead of skipping → 29/29 still pass. The new "skips a history whose owner.pid is held" test seeds only the locked project, so skipping it and aborting the whole pass both end with
trimmed: [].
- Packed budget 203563 → 207000 (+1.7%) is fine.
Nit (follow-up OK): add a second, unlocked old project to that test and assert it is trimmed. That pins "skip one, keep going".
Need ✅ · Size ✅ · Reuse ✅ · Simpler ✅ · Structure ✅
Risk: low; this round changes tests and refactors only, with sort behaviour the same.
— Rames
What
Cap total project history across
historyRootatmin(10 GB, 25% of free disk)(pruneHistoryToGlobalCap) and release deleted projects' histories immediately when free disk falls below5 GB, wired into the daily background prune pass (pruneGoneProjectHistoriesDaily).Why
Each project has a 2 GB history budget, but across many projects total history had no global ceiling and deleted projects kept their history for 14 days even when the volume was running out of space.
Related work
Follows #9227 (which brings
aside/set-aside undo copies into the per-project budget).How
packages/studio-server/src/history/globalHistoryCap.ts:computeGlobalHistoryCap(freeBytes)returnsmin(10 GB, floor(freeBytes * 0.25))(falling back to 10 GB ifstatfsis unavailable).pruneHistoryToGlobalCap(historyRoot, options)measures deduped bytes acrosshistoryRoot(counting hard-linked inodes once vialstat(..., { bigint: true })), skips projects opened within the last 24 hours and any locked/active history, trims 30+ day unused projects first (oldest first), folds oldest unpinned entries while always keeping the newest entry (log.entries.length > 1), writeslog.jsonlatomically before deleting unreferenced blobs inblobs/andaside/, and stops as soon as total history is at or under the cap.packages/studio-server/src/history/pruneHistories.ts:LOW_DISK_FREE_BYTES(5 GB) and accept an optionalfreeBytesprobe onpruneGoneProjectHistories.5 GB, bypass the 14-day grace window for deleted projects whose volume is still mounted (unmounted drives are still preserved).pruneHistoryToGlobalCapafterpruneWithinfinishes inpruneGoneProjectHistoriesDaily, sharing the remainingbudgetMsand stop signal, and only stamp.last-pruneonce both complete.packages/studio-server/src/history/index.ts: exportcomputeGlobalHistoryCap,freeDiskBytesAt,pruneHistoryToGlobalCap,GLOBAL_HISTORY_CAP_BYTES,GLOBAL_HISTORY_FREE_SHARE,STALE_PROJECT_AGE_MS,LOW_DISK_FREE_BYTES, and their result/option types.Test plan
fails without the fix:
packages/studio-server/src/history/globalHistoryCap.test.ts(new file: fails module import beforeglobalHistoryCap.ts, coveringcomputeGlobalHistoryCap, stale-first ordering, 24h protection, keeping newest version, hard-link dedupe, and a 30 GB apparent-size multi-project fixture proving total drops under 10 GB while Undo still works byte-for-byte on every kept project).packages/studio-server/src/history/pruneHistories.test.ts("drops a deleted project's history immediately when free disk is under 5 GB, while still keeping an unmounted drive" failed before the low-disk bypass becausepruneGoneProjectHistoriesreturned[]during the 14-day grace window).Ran
globalHistoryCap.test.tsandpruneHistories.test.ts3 times shuffled (26/26 passed each run).Ran
oxlint,oxfmt --check, andbun run --filter @hyperframes/studio-server build.Unit tests added/updated
Manual testing performed
Documentation updated (if applicable)
Comments follow CONTRIBUTING.md "Comments": they say why, not what, and a bug fix says what the code must do and how to reproduce the bug