Skip to content

CI: save caches only on main, so PR runs stop evicting the caches they need - #7990

Merged
danielhanchen merged 4 commits into
mainfrom
ci/cache-save-only-on-main
Aug 6, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
ci/cache-save-only-on-main

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Measured on this repo: 33.3 GB across 30 caches against GitHub's 10 GB per-repo limit — 3.3x over.

Being over the limit is not a soft condition. GitHub evicts by least-recently-used until usage is back under, which its own docs call "cache thrashing."

The worst case

One 4.6 GB model, Linux-gguf-unsloth/gemma-4-E4B-it, is held four times on four different PR refs — and not on main at all:

4.61 GB  refs/pull/7633/merge
4.61 GB  refs/pull/7870/merge
4.61 GB  refs/pull/7938/merge
4.61 GB  refs/pull/7973/merge
         18.42 GB total, 13.82 GB pure duplication

None of those four copies is reachable from any other run. GitHub scopes a cache created on a PR to that merge ref; the docs say such a cache "can only be restored by re-runs of the pull request."

So this is a self-sustaining loop:

PR misses -> downloads 4.6 GB -> saves a copy only it can use
          -> repo goes further over 10 GB
          -> LRU evicts main's copies, the ones every PR may restore
          -> next PR misses

The fix

Every PR run can restore from the default branch. So keep exactly one copy per key, on main.

All 20 cache/save steps across 10 workflows are now gated on github.ref == 'refs/heads/main'.

Restores are untouched. A PR still restores from main, and still falls back to downloading on a miss, so no job can break on this — the worst case for any PR is exactly what it experiences today.

Coverage of the gate

populates main via
9 of 10 workflows push: [main, pip] — repopulates immediately
local-agent-guides-ci.yml weekly schedule (schedules run on the default branch)

Worth watching: that weekly cron sits right at the 7-day unused-cache retention window. If its cache proves to expire between runs, the follow-up is to give it a push trigger on the same paths filter. I did not add one here, since it would run four ubuntu-latest jobs on every matching main push.

Expected effect

The four orphaned PR copies stop being created — roughly 13.8 GB freed — and what remains is the shared main-branch copy every PR can hit.

Still over budget after this

~19.5 GB remains, and main alone is 14.5 GB, so eviction will continue. The difference is that every evicted entry is now one all PRs could have used, rather than a dead PR-scoped copy.

Bringing it under 10 GB needs a separate decision I did not want to make unilaterally, because there is a real trade-off. Measured on staging, cache restore runs at ~57-61 MB/s and the HF download at ~55 MB/s — essentially identical. So caching a multi-GB GGUF buys little or no time while consuming most of the budget that keeps the small, genuinely fast caches (pip, rust, setup-python) alive. Against that, the cache does buy resilience when Hugging Face is flaky, which is presumably why hf-download-with-retry.sh exists. Happy to take that on as a follow-up with numbers if you want it.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: e022339502

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

CI status: the two red checks are pre-existing, not from this PR

pip scan-packages :: hf-stack and :: studio fail identically on main, including on this branch's own parent commit c4144da5b:

run branch failing
31074306122 main 9568efd27 both
31067141853 main c4144da5b (this branch's parent) both
31062375590 main 8c79e81de both

Every non-cancelled security-audit.yml run since 2026-08-05T17:28Z has failed on exactly these two jobs; the last green was 2026-08-03T08:12Z. Same counts, same findings.

The mechanism rules this PR out: the scanner resolves audit-reqs/*.txt against live PyPI with --with-deps and no pins, so its result drifts with upstream releases. The break window matches torchao 0.18.0, unsloth_zoo 2026.8.2+ and caio 0.12.x, all published after the last green run. Seven benign CRITICAL/HIGH findings reopened, two of which already have baseline entries whose evidence_hash went stale when the upstream code changed. A workflow-YAML-only diff cannot affect a scanner that takes no repo Python as input, and the single line this PR adds to security-audit.yml is in the cargo-audit job, not pip-scan-packages.

Fixing it means seven individually triaged baseline entries keyed on (normalized package, package-relative file, check, evidence_hash) with provenance, not a blind regeneration. That belongs in its own PR, as #7741, #7480 and #7325 each were.

One real issue found and fixed here

local-agent-guides-ci.yml was the only one of the ten gated workflows with no main-ref trigger at all, so main's copy of the 4.6 GB GGUF could only be written by the Monday cron. On a repo over the 10 GB budget that loses to LRU eviction, and PR runs can no longer self-heal because PR-scoped copies are exactly what the gate stops creating.

Added a push: [main] trigger carrying the same 13-entry paths filter as the pull_request trigger, so it seeds on merge without running on every commit to main. This was the follow-up flagged in the PR description; it is now in.

Diff re-verified

  • 20/20 cache/save steps gated; 0/20 cache/restore steps gated, so no PR loses its restore path
  • save-if confirmed a real Swatinem/rust-cache@v2 input, read only in src/save.ts, never in src/restore.ts
  • always() still leads every gate, so cancel/timeout saves behave as their comments describe
  • All 12 touched files parse clean

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 39f4aa514a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +47 to +48
push:
branches: [main]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Restrict the weekly file-edit matrix on push events

Adding this push event also activates the existing file-edit job because its condition is only github.event_name != 'pull_request'. Consequently, every matching main push launches six model-heavy matrix jobs that may each run for up to an hour, despite the workflow explicitly designating that job as weekly/dispatch-only. Gate file-edit to schedule or workflow_dispatch, or separate cache seeding from the full workflow.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in d45124c. Correct catch, and it was a real side effect of the push trigger I added one commit earlier: file-edit was gated as "not a pull_request", which a push satisfies, so every matching merge to main would have started its 2-job matrix at 60 minutes each.

Verified that excluding it costs no seeding, which was the only reason the trigger exists: connection writes the identical gguf key (same GGUF_REPO unsloth/gemma-4-E4B-it-GGUF and same GGUF_FILE), and prompt-cache covers the separate hf key. file-edit seeds nothing connection has not already seeded.

Gated it positively on schedule or workflow_dispatch rather than negatively, so a future trigger cannot pull it back in by accident. On a main push the workflow now runs connection and prompt-cache only.

One correction to the item: the count is four jobs on a matching push before this fix (connection 1, file-edit 2, prompt-cache 1), not six. resume is workflow_dispatch-only and was never in scope. The substance stands.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: d45124ca29

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…y need

Measured on unslothai/unsloth: 33.3GB across 30 caches against GitHub's 10GB
per-repo limit, 3.3x over.

The single worst offender is one 4.6GB model, Linux-gguf-unsloth/gemma-4-E4B-it,
held FOUR times on four different PR refs (7633, 7870, 7938, 7973) for 18.4GB
total, with no copy on main at all. None of those four copies is reachable from
any other run: GitHub scopes a cache created on a PR to that merge ref, and its
docs say such a cache "can only be restored by re-runs of the pull request".

That produces a self-sustaining thrash loop. A PR misses, downloads the model,
saves a copy only it can ever use, and pushes the repo further over the 10GB
budget; GitHub then evicts by least-recently-used, which deletes main's copies
-- the ones every PR is allowed to restore -- so the next PR misses too.

Every PR run can restore from the default branch, so the fix is to keep exactly
one copy per key, on main. All 20 cache/save steps across 10 workflows are now
gated on github.ref == 'refs/heads/main'. Restores are untouched: a PR still
restores from main, and still falls back to downloading when it misses, so no
job can break on this.

Nine of the ten workflows already run on push to main and will repopulate
immediately. local-agent-guides-ci.yml has no push trigger, but its weekly
schedule runs on the default branch, so its save still lands on main -- just
once a week, which is also roughly the 7-day unused-cache retention window. If
its cache proves to expire between runs, giving it a push trigger on the same
paths filter is the follow-up.

Expected effect: the four orphaned PR copies stop being created, freeing about
13.8GB, and what remains is the shared main-branch copy that every PR can hit.
Same reasoning as the model caches, applied to the other producer that writes
from PR runs. swatinem/rust-cache saves on every ref by default, and the rust
caches here are 550-840MB each. A PR-scoped copy can only be restored by re-runs
of that same PR, yet it still counts against the 10GB per-repo budget and pushes
LRU eviction onto main's copy, which every PR is allowed to restore.

Covers the three uses in workflows that run on pull_request: security-audit.yml
and both jobs in studio-tauri-smoke.yml. release-desktop.yml is left alone --
it is workflow_dispatch only and already runs against main, so it has no PR
refs to pollute.

Restores are unaffected: PRs still restore main's rust cache and still fall back
to a cold build on a miss.
…eeded

This workflow's four cache saves are now gated to refs/heads/main, but unlike
the other ten workflows in that change it had no main-ref trigger at all: only
a weekly cron, workflow_dispatch and pull_request. So main's copy of the 4.6GB
gemma-4-E4B GGUF could be written at most once a week.

That is a poor match for a 7-day unused-cache retention on a repo that is over
the 10GB budget. Once LRU evicts main's copy, every PR touching these paths
re-downloads 4.6GB until the Monday cron repairs it, and PR runs can no longer
self-heal from a PR-scoped copy because those are exactly what the gate stops
creating.

Seeding on merge fixes it. The push trigger carries the same 13-entry paths
filter as the pull_request trigger, so it only runs when the guarded sources
actually change rather than on every commit to main.
The push trigger added for cache seeding also activated file-edit, because that
job was gated as "not a pull_request" rather than on the events it actually
wants. A push satisfies that, so every matching merge to main would have started
its 2-job matrix at 60 minutes each.

That runtime buys nothing. The seeding only needs the jobs that write the cache
keys, and connection already writes the identical gguf key: same GGUF_REPO
(unsloth/gemma-4-E4B-it-GGUF) and same GGUF_FILE, so file-edit seeds nothing
connection has not already seeded. prompt-cache covers the separate hf key.

Stating the intent positively (schedule or workflow_dispatch) also stops the
next added trigger from silently pulling this job in again.

On a main push the workflow now runs connection and prompt-cache only, which is
exactly the pair that populates the caches the save steps are gated to main
for.
@danielhanchen
danielhanchen force-pushed the ci/cache-save-only-on-main branch from d45124c to 9850393 Compare August 6, 2026 07:37
@danielhanchen

Copy link
Copy Markdown
Member Author

Rebased onto main after #7974, #7975, #7980 landed.

One conflict, and it resolved by deletion rather than by merge: #7975 absorbed studio-mac-api-smoke.yml into studio-mac-ui-smoke.yml, so this branch's gating edit to that file no longer had a file to apply to. I took main's deletion. No coverage is lost, because the two workflows shared a cache key and model, and the surviving trio in studio-mac-ui-smoke.yml carries the gate.

File count went 12 to 11 for that reason. Re-audited the whole tree afterwards rather than trusting the rebase: 22 main-gated save steps, 0 ungated on any PR-triggered workflow. The single remaining ungated save-if is release-desktop.yml, deliberately so, since it fires on tag pushes and dispatch only. Gating it to refs/heads/main would disable its Rust cache on exactly the runs that need it.

Merging on the existing review, since the rebase only narrowed the diff.

@danielhanchen
danielhanchen merged commit 9a68a9e into main Aug 6, 2026
33 of 36 checks passed
@danielhanchen
danielhanchen deleted the ci/cache-save-only-on-main branch August 6, 2026 07:38

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9850393920

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +47 to +48
push:
branches: [main]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Isolate scheduled runs from push cancellation

When a matching main push occurs while Monday's scheduled run is active, both runs use ${{ github.workflow }}-refs/heads/main; with cancel-in-progress: true, the push cancels the scheduled workflow. Because file-edit is now restricted to schedule/workflow_dispatch and does not run in the replacement push workflow, its weekly six-agent matrix is silently lost until the next cron or a manual dispatch. Include the event name in the concurrency group or otherwise prevent push runs from cancelling scheduled runs.

Useful? React with 👍 / 👎.

Comment on lines +47 to +48
push:
branches: [main]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Seed the model without running the full connection matrix

Although the new file-edit predicate removes the earlier six-cell side effect, connection remains unconditional and expands to six agents for every matching main push. When the main cache has been evicted—the exact scenario this trigger is intended to repair—all six runners restore the same missing key concurrently, each downloads the same 4.6 GB GGUF, and each then runs up to 40 minutes of agent testing before racing to save one immutable cache entry. Use a single seed job or restrict push events to one matrix cell so cache repair does not consume roughly six downloads and runner slots.

Useful? React with 👍 / 👎.

danielhanchen added a commit that referenced this pull request Aug 6, 2026
…gate

#7990 gated every cache save on refs/heads/main, and this branch had renamed
the three phases' step ids and cache dirs so one job could hold all three.
Both are kept: the ref gate, and download-gguf-tools / gguf-cache-tools and
download-gguf-vision / gguf-cache-vision in place of the shared names.

The save step names stay literal rather than env.GGUF_REPO, which is now
reassigned three times in the one job.
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