Skip to content

feat(studio-server): cap total project history and drop gone histories under low disk - #9229

Merged
miguel-heygen merged 4 commits into
mainfrom
dlimit/history-global-cap-and-low-disk
Dec 2, 2026
Merged

miguel-heygen merged 4 commits into
mainfrom
dlimit/history-global-cap-and-low-disk

Conversation

@miguel-heygen

Copy link
Copy Markdown
Collaborator

What

Cap total project history across historyRoot at min(10 GB, 25% of free disk) (pruneHistoryToGlobalCap) and release deleted projects' histories immediately when free disk falls below 5 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) returns min(10 GB, floor(freeBytes * 0.25)) (falling back to 10 GB if statfs is unavailable).
    • pruneHistoryToGlobalCap(historyRoot, options) measures deduped bytes across historyRoot (counting hard-linked inodes once via lstat(..., { 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), writes log.jsonl atomically before deleting unreferenced blobs in blobs/ and aside/, and stops as soon as total history is at or under the cap.
  • packages/studio-server/src/history/pruneHistories.ts:
    • Export LOW_DISK_FREE_BYTES (5 GB) and accept an optional freeBytes probe on pruneGoneProjectHistories.
    • When free disk is below 5 GB, bypass the 14-day grace window for deleted projects whose volume is still mounted (unmounted drives are still preserved).
    • Run pruneHistoryToGlobalCap after pruneWithin finishes in pruneGoneProjectHistoriesDaily, sharing the remaining budgetMs and stop signal, and only stamp .last-prune once both complete.
  • packages/studio-server/src/history/index.ts: export computeGlobalHistoryCap, 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 before globalHistoryCap.ts, covering computeGlobalHistoryCap, 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 because pruneGoneProjectHistories returned [] during the 14-day grace window).
  • Ran globalHistoryCap.test.ts and pruneHistories.test.ts 3 times shuffled (26/26 passed each run).

  • Ran oxlint, oxfmt --check, and bun 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

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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 throws HistoryBusyError. 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 what Engine seeds tracked from 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 in takeLog.
  • 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.

  1. Add a fixture where the cap can only be met by trimming today's project, and assert that it is left alone.
  2. Run a prune while a history's owner.pid is held, and assert that it is skipped.
  3. 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.
  4. Add a daily-pass test that asserts the global cap runs and that .last-prune is 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 ⚠️ (+686, about 400 of it tests) · Reuse ✅ (foldOldest, referencedHashes, the owner lock) · Simpler ⚠️ (the stale bucket above) · Structure ✅
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

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Added all four should-fix mutation tests and both nits in 56fa37a:

  1. Today-only fixture (cap below today's size) asserting today's project is left untouched.
  2. Held owner.pid fixture asserting the locked history is skipped.
  3. Saved log.jsonl integrity check verifying every hash referenced after trim exists in blobs/ or aside/.
  4. Daily pass test verifying pruneHistoryToGlobalCap runs and .last-prune is stamped only after it finishes.
  5. Dropped the redundant stale-first sort bucket in rankedCandidates and shared USE_MARKS between globalHistoryCap.ts and pruneHistories.ts.

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 275 (base branch 275), smooth 59 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Quarantined, measured but not gated (0)

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

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 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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 lastUsed than any newer one, so lastUsed ascending gives the same order. No behaviour change, as claimed. Moving USE_MARKS into 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

@miguel-heygen
miguel-heygen added this pull request to the merge queue Dec 2, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Dec 2, 2026
@miguel-heygen
miguel-heygen added this pull request to the merge queue Dec 2, 2026
Merged via the queue into main with commit 898a015 Dec 2, 2026
91 checks passed
@miguel-heygen
miguel-heygen deleted the dlimit/history-global-cap-and-low-disk branch December 2, 2026 02:04
@miguel-heygen miguel-heygen mentioned this pull request Dec 2, 2026
4 of 5 tasks
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.

2 participants